blob: 18cebdba8c52542f6eef1cdb6303244aff5e1abf [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
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000232 key = tv->vval.v_string == NULL ? (char_u *)"" : tv->vval.v_string;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000236 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
263 return FAIL;
264 else
265 ++ectx->ec_stack.ga_len;
266 tv = STACK_TV_BOT(-1);
267 tv->v_type = VAR_DICT;
268 tv->v_lock = 0;
269 tv->vval.v_dict = dict;
270 if (dict != NULL)
271 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 return OK;
273}
274
275/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200276 * If debug_tick changed check if "ufunc" has a breakpoint and update
277 * "uf_has_breakpoint".
278 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000279 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200280update_has_breakpoint(ufunc_T *ufunc)
281{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000282 if (ufunc->uf_debug_tick == debug_tick)
283 return;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200284
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000285 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;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200290}
291
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200292static garray_T dict_stack = GA_EMPTY;
293
294/*
295 * Put a value on the dict stack. This consumes "tv".
296 */
297 static int
298dict_stack_save(typval_T *tv)
299{
300 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000301 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200302 if (ga_grow(&dict_stack, 1) == FAIL)
303 return FAIL;
304 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
305 ++dict_stack.ga_len;
306 return OK;
307}
308
309/*
310 * Get the typval at top of the dict stack.
311 */
312 static typval_T *
313dict_stack_get_tv(void)
314{
315 if (dict_stack.ga_len == 0)
316 return NULL;
317 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
318}
319
320/*
321 * Get the dict at top of the dict stack.
322 */
323 static dict_T *
324dict_stack_get_dict(void)
325{
326 typval_T *tv;
327
328 if (dict_stack.ga_len == 0)
329 return NULL;
330 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
331 if (tv->v_type == VAR_DICT)
332 return tv->vval.v_dict;
333 return NULL;
334}
335
336/*
337 * Drop an item from the dict stack.
338 */
339 static void
340dict_stack_drop(void)
341{
342 if (dict_stack.ga_len == 0)
343 {
344 iemsg("Dict stack underflow");
345 return;
346 }
347 --dict_stack.ga_len;
348 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
349}
350
351/*
352 * Drop items from the dict stack until the length is equal to "len".
353 */
354 static void
355dict_stack_clear(int len)
356{
357 while (dict_stack.ga_len > len)
358 dict_stack_drop();
359}
360
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200361/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000362 * Get a pointer to useful "pt_outer" of "pt".
363 */
364 static outer_T *
365get_pt_outer(partial_T *pt)
366{
367 partial_T *ptref = pt->pt_outer_partial;
368
369 if (ptref == NULL)
370 return &pt->pt_outer;
371
372 // partial using partial (recursively)
373 while (ptref->pt_outer_partial != NULL)
374 ptref = ptref->pt_outer_partial;
375 return &ptref->pt_outer;
376}
377
378/*
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000379 * Check "argcount" arguments on the stack against what "ufunc" expects.
380 * "off" is the offset of arguments on the stack.
381 * Return OK or FAIL.
382 */
383 static int
384check_ufunc_arg_types(ufunc_T *ufunc, int argcount, int off, ectx_T *ectx)
385{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000386 if (ufunc->uf_arg_types == NULL && ufunc->uf_va_type == NULL)
387 return OK;
388
389 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
390
391 // The function can change at runtime, check that the argument
392 // types are correct.
393 for (int i = 0; i < argcount; ++i)
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000394 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000395 type_T *type = NULL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000396
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000397 // assume a v:none argument, using the default value, is always OK
398 if (argv[i].v_type == VAR_SPECIAL
399 && argv[i].vval.v_number == VVAL_NONE)
400 continue;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000401
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000402 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
403 type = ufunc->uf_arg_types[i];
404 else if (ufunc->uf_va_type != NULL)
405 type = ufunc->uf_va_type->tt_member;
406 if (type != NULL && check_typval_arg_type(type,
407 &argv[i], NULL, i + 1) == FAIL)
408 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000409 }
410 return OK;
411}
412
413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100415 * This adds a stack frame and sets the instruction pointer to the start of the
416 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100417 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 *
419 * Stack has:
420 * - current arguments (already there)
421 * - omitted optional argument (default values) added here
422 * - stack frame:
423 * - pointer to calling function
424 * - Index of next instruction in calling function
425 * - previous frame pointer
426 * - reserved space for local variables
427 */
428 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100429call_dfunc(
430 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100431 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100432 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100433 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100435 int argcount = argcount_arg;
436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
437 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200438 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100439 int arg_to_add;
440 int vararg_count = 0;
441 int varcount;
442 int idx;
443 estack_T *entry;
444 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200445 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000446 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100447
448 if (dfunc->df_deleted)
449 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100450 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000451 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100452 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 return FAIL;
454 }
455
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100456#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100457 if (do_profiling == PROF_YES)
458 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200459 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100460 {
461 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
462 + profile_info_ga.ga_len;
463 ++profile_info_ga.ga_len;
464 CLEAR_POINTER(info);
465 profile_may_start_func(info, ufunc,
466 (((dfunc_T *)def_functions.ga_data)
467 + ectx->ec_dfunc_idx)->df_ufunc);
468 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100469 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100470#endif
471
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200472 // When debugging and using "cont" switches to the not-debugged
473 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000474 compile_type = get_compile_type(ufunc);
475 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200476 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000477 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200478
479 // compile_def_function() may cause def_functions.ga_data to change
480 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
481 }
482 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200483 {
484 if (did_emsg_cumul + did_emsg == did_emsg_before)
485 semsg(_(e_function_is_not_compiled_str),
486 printable_func_name(ufunc));
487 return FAIL;
488 }
489
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200490 if (ufunc->uf_va_name != NULL)
491 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200492 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200493 // Stack at time of call with 2 varargs:
494 // normal_arg
495 // optional_arg
496 // vararg_1
497 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200498 // After creating the list:
499 // normal_arg
500 // optional_arg
501 // vararg-list
502 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200503 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200504 // After creating the list
505 // normal_arg
506 // (space for optional_arg)
507 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200508 vararg_count = argcount - ufunc->uf_args.ga_len;
509 if (vararg_count < 0)
510 vararg_count = 0;
511 else
512 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200513 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200514 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200515
516 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200517 }
518
Bram Moolenaarfe270812020-04-11 22:31:27 +0200519 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200520 if (arg_to_add < 0)
521 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100522 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
523 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200524 return FAIL;
525 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000526 else if (arg_to_add > ufunc->uf_def_args.ga_len)
527 {
528 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
529
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100530 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
531 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000532 return FAIL;
533 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534
Bram Moolenaar574950d2023-01-03 19:08:50 +0000535 // If this is an object method, the object is just before the arguments.
536 typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
537
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000538 // Check the argument types.
539 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
540 return FAIL;
541
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200542 // Reserve space for:
543 // - missing arguments
544 // - stack frame
545 // - local variables
546 // - if needed: a counter for number of closures created in
547 // ectx->ec_funcrefs.
548 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100549 if (GA_GROW_FAILS(&ectx->ec_stack,
550 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 return FAIL;
552
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100553 // If depth of calling is getting too high, don't execute the function.
554 if (funcdepth_increment() == FAIL)
555 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200556 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100557
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100558 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200559 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100560 {
561 floc = ALLOC_ONE(funclocal_T);
562 if (floc == NULL)
563 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200564 *floc = ectx->ec_funclocal;
565 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100566 }
567
Bram Moolenaarfe270812020-04-11 22:31:27 +0200568 // Move the vararg-list to below the missing optional arguments.
569 if (vararg_count > 0 && arg_to_add > 0)
570 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100571
572 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200573 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200574 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200575 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576
577 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100578 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
579 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200580 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200581 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
582 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100583 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100584 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200585 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586
Bram Moolenaar5800c792022-09-22 17:34:01 +0100587 // Initialize all local variables to number zero. Also initialize the
588 // variable that counts how many closures were created. This is used in
589 // handle_closure_in_use().
590 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
591 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000592 {
593 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
594
595 tv->v_type = VAR_NUMBER;
596 tv->vval.v_number = 0;
597 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200598 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599
Bram Moolenaar574950d2023-01-03 19:08:50 +0000600 // For an object method move the object from just before the arguments to
601 // the first local variable.
602 if (ufunc->uf_flags & FC_OBJECT)
603 {
604 *STACK_TV_VAR(0) = *obj;
605 obj->v_type = VAR_UNKNOWN;
606 }
607
Bram Moolenaar5800c792022-09-22 17:34:01 +0100608 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
609 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100610 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200611 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100612
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200613 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100614 return FAIL;
615 if (pt != NULL)
616 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000617 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200618 ++pt->pt_refcount;
619 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100620 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100621 else
622 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200623 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200624 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200625 {
626 vim_free(ref);
627 return FAIL;
628 }
629 ref->or_outer_allocated = TRUE;
630 ref->or_outer->out_stack = &ectx->ec_stack;
631 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
632 if (ectx->ec_outer_ref != NULL)
633 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100634 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200635 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100636 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100637 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200638 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100639
Bram Moolenaarc970e422021-03-17 15:03:04 +0100640 ++ufunc->uf_calls;
641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 // Set execution state to the start of the called function.
643 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100644 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100645 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200646 if (entry != NULL)
647 {
648 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200649 // Save the current context so it can be restored on return.
650 entry->es_save_sctx = current_sctx;
651 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200652 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100653
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200654 // Start execution at the first instruction.
655 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 return OK;
658}
659
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000660// Double linked list of funcstack_T in use.
661static funcstack_T *first_funcstack = NULL;
662
663 static void
664add_funcstack_to_list(funcstack_T *funcstack)
665{
666 // Link in list of funcstacks.
667 if (first_funcstack != NULL)
668 first_funcstack->fs_prev = funcstack;
669 funcstack->fs_next = first_funcstack;
670 funcstack->fs_prev = NULL;
671 first_funcstack = funcstack;
672}
673
674 static void
675remove_funcstack_from_list(funcstack_T *funcstack)
676{
677 if (funcstack->fs_prev == NULL)
678 first_funcstack = funcstack->fs_next;
679 else
680 funcstack->fs_prev->fs_next = funcstack->fs_next;
681 if (funcstack->fs_next != NULL)
682 funcstack->fs_next->fs_prev = funcstack->fs_prev;
683}
684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200686 * Used when returning from a function: Check if any closure is still
687 * referenced. If so then move the arguments and variables to a separate piece
688 * of stack to be used when the closure is called.
689 * When "free_arguments" is TRUE the arguments are to be freed.
690 * Returns FAIL when out of memory.
691 */
692 static int
693handle_closure_in_use(ectx_T *ectx, int free_arguments)
694{
695 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
696 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200697 int argcount;
698 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200699 int idx;
700 typval_T *tv;
701 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200702 garray_T *gap = &ectx->ec_funcrefs;
703 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200704
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200705 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200706 return OK; // function was freed
707 if (dfunc->df_has_closure == 0)
708 return OK; // no closures
709 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
710 closure_count = tv->vval.v_number;
711 if (closure_count == 0)
712 return OK; // no funcrefs created
713
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100714 // Compute "top": the first entry in the stack used by the function.
715 // This is the first argument (after that comes the stack frame and then
716 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200717 argcount = ufunc_argcount(dfunc->df_ufunc);
718 top = ectx->ec_frame_idx - argcount;
719
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200721 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200722 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200723 partial_T *pt;
724 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200725
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200726 if (off < 0)
727 continue; // count is off or already done
728 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200729 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200730 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200731 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200732 int i;
733
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000734 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200735 // unreferenced on return.
736 for (i = 0; i < dfunc->df_varcount; ++i)
737 {
738 typval_T *stv = STACK_TV(ectx->ec_frame_idx
739 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200740 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200741 --refcount;
742 }
743 if (refcount > 1)
744 {
745 closure_in_use = TRUE;
746 break;
747 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200748 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200749 }
750
751 if (closure_in_use)
752 {
753 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
754 typval_T *stack;
755
756 // A closure is using the arguments and/or local variables.
757 // Move them to the called function.
758 if (funcstack == NULL)
759 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000760
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200761 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100762 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
763 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200764 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
765 funcstack->fs_ga.ga_data = stack;
766 if (stack == NULL)
767 {
768 vim_free(funcstack);
769 return FAIL;
770 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000771 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200772
773 // Move or copy the arguments.
774 for (idx = 0; idx < argcount; ++idx)
775 {
776 tv = STACK_TV(top + idx);
777 if (free_arguments)
778 {
779 *(stack + idx) = *tv;
780 tv->v_type = VAR_UNKNOWN;
781 }
782 else
783 copy_tv(tv, stack + idx);
784 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100785 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 // Move the local variables.
787 for (idx = 0; idx < dfunc->df_varcount; ++idx)
788 {
789 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200790
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200791 // A partial created for a local function, that is also used as a
792 // local variable, has a reference count for the variable, thus
793 // will never go down to zero. When all these refcounts are one
794 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000795 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200796 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
797 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200798 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200799
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200800 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200801 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
802 gap->ga_len - closure_count + i])
803 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200804 }
805
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200806 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200807 tv->v_type = VAR_UNKNOWN;
808 }
809
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200810 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200811 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200812 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
813 - closure_count + idx];
814 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200815 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200816 ++funcstack->fs_refcount;
817 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100818 pt->pt_outer.out_stack = &funcstack->fs_ga;
819 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200820 }
821 }
822 }
823
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200824 for (idx = 0; idx < closure_count; ++idx)
825 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
826 - closure_count + idx]);
827 gap->ga_len -= closure_count;
828 if (gap->ga_len == 0)
829 ga_clear(gap);
830
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200831 return OK;
832}
833
834/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200835 * Called when a partial is freed or its reference count goes down to one. The
836 * funcstack may be the only reference to the partials in the local variables.
837 * Go over all of them, the funcref and can be freed if all partials
838 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100839 * Returns TRUE if the funcstack is freed, the partial referencing it will then
840 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200841 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100842 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200843funcstack_check_refcount(funcstack_T *funcstack)
844{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100845 int i;
846 garray_T *gap = &funcstack->fs_ga;
847 int done = 0;
848 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200849
850 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100851 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200852 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
853 {
854 typval_T *tv = ((typval_T *)gap->ga_data) + i;
855
856 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
857 && tv->vval.v_partial->pt_funcstack == funcstack
858 && tv->vval.v_partial->pt_refcount == 1)
859 ++done;
860 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100861 if (done != funcstack->fs_min_refcount)
862 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200863
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100864 stack = gap->ga_data;
865
866 // All partials referencing the funcstack have a reference count of
867 // one, thus the funcstack is no longer of use.
868 for (i = 0; i < gap->ga_len; ++i)
869 clear_tv(stack + i);
870 vim_free(stack);
871 remove_funcstack_from_list(funcstack);
872 vim_free(funcstack);
873
874 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200875}
876
877/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000878 * For garbage collecting: set references in all variables referenced by
879 * all funcstacks.
880 */
881 int
882set_ref_in_funcstacks(int copyID)
883{
884 funcstack_T *funcstack;
885
886 for (funcstack = first_funcstack; funcstack != NULL;
887 funcstack = funcstack->fs_next)
888 {
889 typval_T *stack = funcstack->fs_ga.ga_data;
890 int i;
891
892 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
893 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
894 return TRUE; // abort
895 }
896 return FALSE;
897}
898
Bram Moolenaar806a2732022-09-04 15:40:36 +0100899// Ugly static to avoid passing the execution context around through many
900// layers.
901static ectx_T *current_ectx = NULL;
902
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000903/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100904 * Return TRUE if currently executing a :def function.
905 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100906 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100907 int
908in_def_function(void)
909{
910 return current_ectx != NULL;
911}
912
913/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100914 * Clear "current_ectx" and return the previous value. To be used when calling
915 * a user function.
916 */
917 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000918clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100919{
920 ectx_T *r = current_ectx;
921
922 current_ectx = NULL;
923 return r;
924}
925
926 void
927restore_current_ectx(ectx_T *ectx)
928{
929 if (current_ectx != NULL)
930 iemsg("Restoring current_ectx while it is not NULL");
931 current_ectx = ectx;
932}
933
934/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100935 * Add an entry for a deferred function call to the currently executing
936 * function.
937 * Return the list or NULL when failed.
938 */
939 static list_T *
940add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100941{
942 typval_T *defer_tv = STACK_TV_VAR(var_idx);
943 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100944 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100945 typval_T listval;
946
947 if (defer_tv->v_type != VAR_LIST)
948 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100949 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100950 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100951 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100952 }
953 defer_l = defer_tv->vval.v_list;
954
955 l = list_alloc_with_items(argcount + 1);
956 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100957 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100958 listval.v_type = VAR_LIST;
959 listval.vval.v_list = l;
960 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100961 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100962 {
963 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100964 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100965 }
966
Bram Moolenaar806a2732022-09-04 15:40:36 +0100967 return l;
968}
969
970/*
971 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
972 * The local variable that lists deferred functions is "var_idx".
973 * Returns OK or FAIL.
974 */
975 static int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000976defer_command(int var_idx, int has_obj, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977{
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000978 int obj_off = has_obj ? 1 : 0;
979 list_T *l = add_defer_item(var_idx, argcount + obj_off, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100980 int i;
981 typval_T *func_tv;
982
983 if (l == NULL)
984 return FAIL;
985
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100986 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000987 if (has_obj ? func_tv->v_type != VAR_PARTIAL : func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100988 {
989 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000990 has_obj ? "partial" : "function",
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100991 vartype_name(func_tv->v_type));
992 return FAIL;
993 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100994 list_set_item(l, 0, func_tv);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000995 if (has_obj)
996 list_set_item(l, 1, STACK_TV_BOT(-argcount - 2));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100997
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100998 for (i = 0; i < argcount; ++i)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000999 list_set_item(l, i + 1 + obj_off, STACK_TV_BOT(-argcount + i));
1000 ectx->ec_stack.ga_len -= argcount + 1 + obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001001 return OK;
1002}
1003
1004/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001005 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001006 * Consumes "name", also on failure.
1007 * Only to be called when in_def_function() returns TRUE.
1008 */
1009 int
1010add_defer_function(char_u *name, int argcount, typval_T *argvars)
1011{
1012 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1013 + current_ectx->ec_dfunc_idx;
1014 list_T *l;
1015 typval_T func_tv;
1016 int i;
1017
1018 if (dfunc->df_defer_var_idx == 0)
1019 {
1020 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001021 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001022 return FAIL;
1023 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001024
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001025 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001026 if (l == NULL)
1027 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001028 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001029 return FAIL;
1030 }
1031
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001032 func_tv.v_type = VAR_FUNC;
1033 func_tv.v_lock = 0;
1034 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001035 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001036
Bram Moolenaar806a2732022-09-04 15:40:36 +01001037 for (i = 0; i < argcount; ++i)
1038 list_set_item(l, i + 1, argvars + i);
1039 return OK;
1040}
1041
1042/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001043 * Invoked when returning from a function: Invoke any deferred calls.
1044 */
1045 static void
1046invoke_defer_funcs(ectx_T *ectx)
1047{
1048 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1049 + ectx->ec_dfunc_idx;
1050 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1051 listitem_T *li;
1052
1053 if (defer_tv->v_type != VAR_LIST)
1054 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001055 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001056 {
1057 list_T *l = li->li_tv.vval.v_list;
1058 typval_T rettv;
1059 typval_T argvars[MAX_FUNC_ARGS];
1060 int i;
1061 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001062 typval_T *functv = &l->lv_first->li_tv;
1063 int obj_off = functv->v_type == VAR_PARTIAL ? 1 : 0;
1064 int argcount = l->lv_len - 1 - obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001065
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001066 if (obj_off == 1)
1067 arg_li = arg_li->li_next; // second list item is the object
1068 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001069 {
1070 arg_li = arg_li->li_next;
1071 argvars[i] = arg_li->li_tv;
1072 }
1073
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001074 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001075 CLEAR_FIELD(funcexe);
1076 funcexe.fe_evaluate = TRUE;
1077 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001078 if (functv->v_type == VAR_PARTIAL)
1079 {
1080 funcexe.fe_partial = functv->vval.v_partial;
1081 funcexe.fe_object = l->lv_first->li_next->li_tv.vval.v_object;
1082 if (funcexe.fe_object != NULL)
1083 ++funcexe.fe_object->obj_refcount;
1084 }
1085 (void)call_func(functv->vval.v_string, -1,
1086 &rettv, argcount, argvars, &funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001087 clear_tv(&rettv);
1088 }
1089}
1090
1091/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 * Return from the current function.
1093 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001094 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001095func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001098 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001099 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1100 + ectx->ec_dfunc_idx;
1101 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001102 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001103 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1104 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001105 funclocal_T *floc;
1106#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001107 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1108 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109
Bram Moolenaar12d26532021-02-19 19:13:21 +01001110 if (do_profiling == PROF_YES)
1111 {
1112 ufunc_T *caller = prev_dfunc->df_ufunc;
1113
1114 if (dfunc->df_ufunc->uf_profiling
1115 || (caller != NULL && caller->uf_profiling))
1116 {
1117 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1118 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1119 --profile_info_ga.ga_len;
1120 }
1121 }
1122#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001123
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001124 if (dfunc->df_defer_var_idx > 0)
1125 invoke_defer_funcs(ectx);
1126
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001127 // No check for uf_refcount being zero, cannot think of a way that would
1128 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001129 --dfunc->df_ufunc->uf_calls;
1130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001132 entry = estack_pop();
1133 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001134 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001136 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1137 return FAIL;
1138
Bram Moolenaar574950d2023-01-03 19:08:50 +00001139 // Clear the arguments. If this was an object method also clear the
1140 // object, it is just before the arguments.
1141 int top = ectx->ec_frame_idx - argcount;
1142 if (dfunc->df_ufunc->uf_flags & FC_OBJECT)
1143 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001144 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1145 clear_tv(STACK_TV(idx));
1146
1147 // Clear local variables and temp values, but not the return value.
1148 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001149 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001151
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001152 // The return value should be on top of the stack. However, when aborting
1153 // it may not be there and ec_frame_idx is the top of the stack.
1154 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001155 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001156 ret_idx = 0;
1157
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001158 if (ectx->ec_outer_ref != NULL)
1159 {
1160 if (ectx->ec_outer_ref->or_outer_allocated)
1161 vim_free(ectx->ec_outer_ref->or_outer);
1162 partial_unref(ectx->ec_outer_ref->or_partial);
1163 vim_free(ectx->ec_outer_ref);
1164 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001165
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001166 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001167 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001168 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1169 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001170 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1171 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001172 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001173 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001174 floc = (void *)STACK_TV(ectx->ec_frame_idx
1175 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001176 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001177 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1178 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001179
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001180 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001181 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001182 else
1183 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001184 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001185 vim_free(floc);
1186 }
1187
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001188 if (ret_idx > 0)
1189 {
1190 // Reset the stack to the position before the call, with a spot for the
1191 // return value, moved there from above the frame.
1192 ectx->ec_stack.ga_len = top + 1;
1193 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1194 }
1195 else
1196 // Reset the stack to the position before the call.
1197 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001198
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001199 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001200 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001201 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202}
1203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204/*
1205 * Prepare arguments and rettv for calling a builtin or user function.
1206 */
1207 static int
1208call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1209{
1210 int idx;
1211 typval_T *tv;
1212
1213 // Move arguments from bottom of the stack to argvars[] and add terminator.
1214 for (idx = 0; idx < argcount; ++idx)
1215 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1216 argvars[argcount].v_type = VAR_UNKNOWN;
1217
1218 // Result replaces the arguments on the stack.
1219 if (argcount > 0)
1220 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001221 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 return FAIL;
1223 else
1224 ++ectx->ec_stack.ga_len;
1225
1226 // Default return value is zero.
1227 tv = STACK_TV_BOT(-1);
1228 tv->v_type = VAR_NUMBER;
1229 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001230 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231
1232 return OK;
1233}
1234
1235/*
1236 * Call a builtin function by index.
1237 */
1238 static int
1239call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1240{
1241 typval_T argvars[MAX_FUNC_ARGS];
1242 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001243 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001244 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001245 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246
1247 if (call_prepare(argcount, argvars, ectx) == FAIL)
1248 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001249 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001251 // Call the builtin function. Set "current_ectx" so that when it
1252 // recursively invokes call_def_function() a closure context can be set.
1253 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001255 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001256 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257
1258 // Clear the arguments.
1259 for (idx = 0; idx < argcount; ++idx)
1260 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001261
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001262 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001263 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 return OK;
1265}
1266
1267/*
1268 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001269 * If the function is compiled this will add a stack frame and set the
1270 * instruction pointer at the start of the function.
1271 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001272 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001273 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 */
1275 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001276call_ufunc(
1277 ufunc_T *ufunc,
1278 partial_T *pt,
1279 int argcount,
1280 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001281 isn_T *iptr,
1282 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283{
1284 typval_T argvars[MAX_FUNC_ARGS];
1285 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001286 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001288 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001289 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290
Bram Moolenaare99d4222021-06-13 14:01:26 +02001291 if (func_needs_compiling(ufunc, compile_type)
1292 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1293 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001294 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001295 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001296 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001297 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001298 if (error != FCERR_UNKNOWN)
1299 {
1300 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001301 semsg(_(e_too_many_arguments_for_function_str),
1302 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001303 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001304 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001305 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001306 return FAIL;
1307 }
1308
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001309 // The function has been compiled, can call it quickly. For a function
1310 // that was defined later: we can call it directly next time.
1311 if (iptr != NULL)
1312 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001313 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001314 iptr->isn_type = ISN_DCALL;
1315 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1316 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1317 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001318 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320
1321 if (call_prepare(argcount, argvars, ectx) == FAIL)
1322 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001323 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001324 funcexe.fe_evaluate = TRUE;
1325 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326
1327 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001329 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330
1331 // Clear the arguments.
1332 for (idx = 0; idx < argcount; ++idx)
1333 clear_tv(&argvars[idx]);
1334
1335 if (error != FCERR_NONE)
1336 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001337 user_func_error(error, printable_func_name(ufunc),
1338 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 return FAIL;
1340 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001341 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001342 // Error other than from calling the function itself.
1343 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 return OK;
1345}
1346
1347/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001348 * If command modifiers were applied restore them.
1349 */
1350 static void
1351may_restore_cmdmod(funclocal_T *funclocal)
1352{
1353 if (funclocal->floc_restore_cmdmod)
1354 {
1355 cmdmod.cmod_filter_regmatch.regprog = NULL;
1356 undo_cmdmod(&cmdmod);
1357 cmdmod = funclocal->floc_save_cmdmod;
1358 funclocal->floc_restore_cmdmod = FALSE;
1359 }
1360}
1361
1362/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001363 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1364 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001365 */
1366 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001367vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001368{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001369 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001370}
1371
1372/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 * Execute a function by "name".
1374 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001375 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 * Returns FAIL if not found without an error message.
1377 */
1378 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001379call_by_name(
1380 char_u *name,
1381 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001382 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001383 isn_T *iptr,
1384 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385{
1386 ufunc_T *ufunc;
1387
1388 if (builtin_function(name, -1))
1389 {
1390 int func_idx = find_internal_func(name);
1391
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001392 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001394 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 return FAIL;
1396 return call_bfunc(func_idx, argcount, ectx);
1397 }
1398
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001399 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001400
1401 if (ufunc == NULL)
1402 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001403 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001404
1405 if (script_autoload(name, TRUE))
1406 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001407 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001408
1409 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001410 return FAIL; // bail out if loading the script caused an error
1411 }
1412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001414 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001415 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1416 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001417
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001418 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420
1421 return FAIL;
1422}
1423
1424 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001425call_partial(
1426 typval_T *tv,
1427 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001428 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001430 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001431 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001433 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001434 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435
1436 if (tv->v_type == VAR_PARTIAL)
1437 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001438 partial_T *pt = tv->vval.v_partial;
1439 int i;
1440
1441 if (pt->pt_argc > 0)
1442 {
1443 // Make space for arguments from the partial, shift the "argcount"
1444 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001445 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001446 return FAIL;
1447 for (i = 1; i <= argcount; ++i)
1448 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1449 ectx->ec_stack.ga_len += pt->pt_argc;
1450 argcount += pt->pt_argc;
1451
1452 // copy the arguments from the partial onto the stack
1453 for (i = 0; i < pt->pt_argc; ++i)
1454 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1455 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001456 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457
1458 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001459 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 name = pt->pt_name;
1462 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001463 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001465 if (name != NULL)
1466 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001467 char_u fname_buf[FLEN_FIXED + 1];
1468 char_u *tofree = NULL;
1469 funcerror_T error = FCERR_NONE;
1470 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001471
1472 // May need to translate <SNR>123_ to K_SNR.
1473 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1474 if (error != FCERR_NONE)
1475 res = FAIL;
1476 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001477 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001478 vim_free(tofree);
1479 }
1480
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001481 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 {
1483 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001484 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001485 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 return FAIL;
1487 }
1488 return OK;
1489}
1490
1491/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001492 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1493 * TRUE.
1494 */
1495 static int
1496error_if_locked(int lock, char *error)
1497{
1498 if (lock & (VAR_LOCKED | VAR_FIXED))
1499 {
1500 emsg(_(error));
1501 return TRUE;
1502 }
1503 return FALSE;
1504}
1505
1506/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001507 * Give an error if "tv" is not a number and return FAIL.
1508 */
1509 static int
1510check_for_number(typval_T *tv)
1511{
1512 if (tv->v_type != VAR_NUMBER)
1513 {
1514 semsg(_(e_expected_str_but_got_str),
1515 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1516 return FAIL;
1517 }
1518 return OK;
1519}
1520
1521/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001522 * Store "tv" in variable "name".
1523 * This is for s: and g: variables.
1524 */
1525 static void
1526store_var(char_u *name, typval_T *tv)
1527{
1528 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001529 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001530
Bram Moolenaard877a572021-04-01 19:42:48 +02001531 if (tv->v_lock)
1532 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001533 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001534 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001535 restore_funccal();
1536}
1537
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001538/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001539 * Convert "tv" to a string.
1540 * Return FAIL if not allowed.
1541 */
1542 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001543do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001544{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001545 if (tv->v_type == VAR_STRING)
1546 return OK;
1547
1548 char_u *str;
1549
1550 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001551 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001552 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001553 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001554 case VAR_SPECIAL:
1555 case VAR_BOOL:
1556 case VAR_NUMBER:
1557 case VAR_FLOAT:
1558 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001559
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001560 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001561 if (tolerant)
1562 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001563 char_u *s, *e, *p;
1564 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001565
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001566 ga_init2(&ga, sizeof(char_u *), 1);
1567
1568 // Convert to NL separated items, then
1569 // escape the items and replace the NL with
1570 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001571 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001572 if (str == NULL)
1573 return FAIL;
1574 s = str;
1575 while ((e = vim_strchr(s, '\n')) != NULL)
1576 {
1577 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001578 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001579 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001580 if (p != NULL)
1581 {
1582 ga_concat(&ga, p);
1583 ga_concat(&ga, (char_u *)" ");
1584 vim_free(p);
1585 }
1586 s = e + 1;
1587 }
1588 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001589 clear_tv(tv);
1590 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001591 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001592 return OK;
1593 }
1594 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001595 default: to_string_error(tv->v_type);
1596 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001597 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001598 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001599 str = typval_tostring(tv, TRUE);
1600 clear_tv(tv);
1601 tv->v_type = VAR_STRING;
1602 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001603 return OK;
1604}
1605
1606/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001607 * When the value of "sv" is a null list of dict, allocate it.
1608 */
1609 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001610allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001611{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001612 typval_T *tv = sv->sv_tv;
1613
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001614 switch (tv->v_type)
1615 {
1616 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001617 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001618 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001619 break;
1620 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001621 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001622 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001623 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001624 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001625 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001626 (void)rettv_blob_alloc(tv);
1627 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001628 default:
1629 break;
1630 }
1631}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001632
Bram Moolenaare7525c52021-01-09 13:20:37 +01001633/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001634 * Return the character "str[index]" where "index" is the character index,
1635 * including composing characters.
1636 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001637 */
1638 char_u *
1639char_from_string(char_u *str, varnumber_T index)
1640{
1641 size_t nbyte = 0;
1642 varnumber_T nchar = index;
1643 size_t slen;
1644
1645 if (str == NULL)
1646 return NULL;
1647 slen = STRLEN(str);
1648
Bram Moolenaarff871402021-03-26 13:34:05 +01001649 // Do the same as for a list: a negative index counts from the end.
1650 // Optimization to check the first byte to be below 0x80 (and no composing
1651 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001652 if (index < 0)
1653 {
1654 int clen = 0;
1655
1656 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001657 {
1658 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1659 ++nbyte;
1660 else if (enc_utf8)
1661 nbyte += utfc_ptr2len(str + nbyte);
1662 else
1663 nbyte += mb_ptr2len(str + nbyte);
1664 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001665 nchar = clen + index;
1666 if (nchar < 0)
1667 // unlike list: index out of range results in empty string
1668 return NULL;
1669 }
1670
1671 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001672 {
1673 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1674 ++nbyte;
1675 else if (enc_utf8)
1676 nbyte += utfc_ptr2len(str + nbyte);
1677 else
1678 nbyte += mb_ptr2len(str + nbyte);
1679 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001680 if (nbyte >= slen)
1681 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001682 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001683}
1684
1685/*
1686 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001687 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001688 * If going over the end return "str_len".
1689 * If "idx" is negative count from the end, -1 is the last character.
1690 * When going over the start return -1.
1691 */
1692 static long
1693char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1694{
1695 varnumber_T nchar = idx;
1696 size_t nbyte = 0;
1697
1698 if (nchar >= 0)
1699 {
1700 while (nchar > 0 && nbyte < str_len)
1701 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001702 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001703 --nchar;
1704 }
1705 }
1706 else
1707 {
1708 nbyte = str_len;
1709 while (nchar < 0 && nbyte > 0)
1710 {
1711 --nbyte;
1712 nbyte -= mb_head_off(str, str + nbyte);
1713 ++nchar;
1714 }
1715 if (nchar < 0)
1716 return -1;
1717 }
1718 return (long)nbyte;
1719}
1720
1721/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001722 * Return the slice "str[first : last]" using character indexes. Composing
1723 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001724 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001725 * Return NULL when the result is empty.
1726 */
1727 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001728string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001729{
1730 long start_byte, end_byte;
1731 size_t slen;
1732
1733 if (str == NULL)
1734 return NULL;
1735 slen = STRLEN(str);
1736 start_byte = char_idx2byte(str, slen, first);
1737 if (start_byte < 0)
1738 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001739 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001740 end_byte = (long)slen;
1741 else
1742 {
1743 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001744 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001745 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001746 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001747 }
1748
1749 if (start_byte >= (long)slen || end_byte <= start_byte)
1750 return NULL;
1751 return vim_strnsave(str + start_byte, end_byte - start_byte);
1752}
1753
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001754/*
1755 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1756 * When "dfunc_idx" is negative don't give an error.
1757 * Returns NULL for an error.
1758 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001759 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001760get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001761{
1762 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001763 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1764 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001765 svar_T *sv;
1766
1767 if (sref->sref_seq != si->sn_script_seq)
1768 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001769 // The script was reloaded after the function was compiled, the
1770 // script_idx may not be valid.
1771 if (dfunc != NULL)
1772 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1773 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001774 return NULL;
1775 }
1776 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001777 if (sv->sv_name == NULL)
1778 {
1779 if (dfunc != NULL)
1780 emsg(_(e_script_variable_was_deleted));
1781 return NULL;
1782 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001783 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001784 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001785 if (dfunc != NULL)
1786 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001787 return NULL;
1788 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001789
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001790 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1791 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001792 {
1793 if (dfunc != NULL)
1794 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1795 return NULL;
1796 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001797 return sv;
1798}
1799
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001800/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001801 * Function passed to do_cmdline() for splitting a script joined by NL
1802 * characters.
1803 */
1804 static char_u *
1805get_split_sourceline(
1806 int c UNUSED,
1807 void *cookie,
1808 int indent UNUSED,
1809 getline_opt_T options UNUSED)
1810{
1811 source_cookie_T *sp = (source_cookie_T *)cookie;
1812 char_u *p;
1813 char_u *line;
1814
Bram Moolenaar20677332021-06-06 17:02:53 +02001815 p = vim_strchr(sp->nextline, '\n');
1816 if (p == NULL)
1817 {
1818 line = vim_strsave(sp->nextline);
1819 sp->nextline += STRLEN(sp->nextline);
1820 }
1821 else
1822 {
1823 line = vim_strnsave(sp->nextline, p - sp->nextline);
1824 sp->nextline = p + 1;
1825 }
1826 return line;
1827}
1828
1829/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 * Execute a function by "name".
1831 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001832 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 */
1834 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001835call_eval_func(
1836 char_u *name,
1837 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001838 ectx_T *ectx,
1839 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840{
Bram Moolenaared677f52020-08-12 16:38:10 +02001841 int called_emsg_before = called_emsg;
1842 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001844 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001845 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001847 dictitem_T *v;
1848
1849 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001850 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1851 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001852 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001853 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001854 return FAIL;
1855 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001856 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001858 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859}
1860
1861/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001862 * When a function reference is used, fill a partial with the information
1863 * needed, especially when it is used as a closure.
1864 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001865 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001866fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001867 partial_T *pt,
1868 ufunc_T *ufunc,
1869 loopvarinfo_T *lvi,
1870 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001871{
1872 pt->pt_func = ufunc;
1873 pt->pt_refcount = 1;
1874
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001875 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001876 {
1877 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1878 + ectx->ec_dfunc_idx;
1879
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001880 // The closure may need to find arguments and local variables of the
1881 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001882 pt->pt_outer.out_stack = &ectx->ec_stack;
1883 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001884 if (ectx->ec_outer_ref != NULL)
1885 {
1886 // The current context already has a context, link to that one.
1887 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1888 if (ectx->ec_outer_ref->or_partial != NULL)
1889 {
1890 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1891 ++pt->pt_outer.out_up_partial->pt_refcount;
1892 }
1893 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001894
Bram Moolenaarcc341812022-09-19 15:54:34 +01001895 if (lvi != NULL)
1896 {
1897 int depth;
1898
1899 // The closure may need to find variables defined inside a loop,
1900 // for every nested loop. A new reference is made every time,
1901 // ISN_ENDLOOP will check if they are actually used.
1902 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1903 {
1904 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1905 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1906 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1907 pt->pt_outer.out_loop[depth].var_count =
1908 lvi->lvi_loop[depth].var_count;
1909 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001910 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001911 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001912 else
1913 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001914
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001915 // If the function currently executing returns and the closure is still
1916 // being referenced, we need to make a copy of the context (arguments
1917 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001918 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001919 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001920 {
1921 vim_free(pt);
1922 return FAIL;
1923 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001924 // Extra variable keeps the count of closures created in the current
1925 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001926 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1927 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1928
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001929 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1930 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001931 ++pt->pt_refcount;
1932 ++ectx->ec_funcrefs.ga_len;
1933 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001934 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001935 return OK;
1936}
1937
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001938/*
1939 * Execute iptr->isn_arg.string as an Ex command.
1940 */
1941 static int
1942exec_command(isn_T *iptr)
1943{
1944 source_cookie_T cookie;
1945
1946 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00001947 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001948 CLEAR_FIELD(cookie);
1949 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1950 if (do_cmdline(iptr->isn_arg.string,
1951 getsourceline, &cookie,
1952 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1953 || did_emsg)
1954 return FAIL;
1955 return OK;
1956}
1957
Bram Moolenaar89445512022-04-14 12:58:23 +01001958/*
1959 * If script "sid" is not loaded yet then load it now.
1960 * Caller must make sure "sid" is a valid script ID.
1961 * "loaded" is set to TRUE if the script had to be loaded.
1962 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1963 */
1964 int
1965may_load_script(int sid, int *loaded)
1966{
1967 scriptitem_T *si = SCRIPT_ITEM(sid);
1968
1969 if (si->sn_state == SN_STATE_NOT_LOADED)
1970 {
1971 *loaded = TRUE;
1972 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1973 {
1974 semsg(_(e_cant_open_file_str), si->sn_name);
1975 return FAIL;
1976 }
1977 }
1978 return OK;
1979}
1980
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001981// used for v_instr of typval of VAR_INSTR
1982struct instr_S {
1983 ectx_T *instr_ectx;
1984 isn_T *instr_instr;
1985};
1986
Bram Moolenaar4c137212021-04-19 16:48:48 +02001987// used for substitute_instr
1988typedef struct subs_expr_S {
1989 ectx_T *subs_ectx;
1990 isn_T *subs_instr;
1991 int subs_status;
1992} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001994// Set when calling do_debug().
1995static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001996static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001997
1998/*
1999 * When debugging lookup "name" and return the typeval.
2000 * When not found return NULL.
2001 */
2002 typval_T *
2003lookup_debug_var(char_u *name)
2004{
2005 int idx;
2006 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002007 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002008 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002009 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002010
2011 if (ectx == NULL)
2012 return NULL;
2013 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2014
2015 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002016 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002017 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002018 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2019
2020 // the variable name may be NULL when not available in this block
2021 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002022 return STACK_TV_VAR(idx);
2023 }
2024
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002025 // Go through argument names.
2026 ufunc = dfunc->df_ufunc;
2027 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2028 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2029 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2030 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2031 - varargs_off + idx);
2032 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2033 return STACK_TV(ectx->ec_frame_idx - 1);
2034
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002035 return NULL;
2036}
2037
Bram Moolenaar26a44842021-09-02 18:49:06 +02002038/*
2039 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2040 * breakpoint was set in that function or when there is any expression.
2041 */
2042 int
2043may_break_in_function(ufunc_T *ufunc)
2044{
2045 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2046}
2047
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002048 static void
2049handle_debug(isn_T *iptr, ectx_T *ectx)
2050{
2051 char_u *line;
2052 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2053 + ectx->ec_dfunc_idx)->df_ufunc;
2054 isn_T *ni;
2055 int end_lnum = iptr->isn_lnum;
2056 garray_T ga;
2057 int lnum;
2058
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002059 if (ex_nesting_level > debug_break_level)
2060 {
2061 linenr_T breakpoint;
2062
Bram Moolenaar26a44842021-09-02 18:49:06 +02002063 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002064 return;
2065
2066 // check for the next breakpoint if needed
2067 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002068 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002069 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2070 return;
2071 }
2072
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002073 SOURCING_LNUM = iptr->isn_lnum;
2074 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002075 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002076
2077 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2078 if (ni->isn_type == ISN_DEBUG
2079 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002080 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002081 || ni->isn_type == ISN_RETURN_VOID)
2082 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002083 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002084 break;
2085 }
2086
2087 if (end_lnum > iptr->isn_lnum)
2088 {
2089 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002090 for (lnum = iptr->isn_lnum; lnum < end_lnum
2091 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002092 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002093 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002094
Bram Moolenaar303215d2021-07-07 20:10:43 +02002095 if (p == NULL)
2096 continue; // left over from continuation line
2097 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002098 if (*p == '#')
2099 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002100 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002101 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2102 if (STRNCMP(p, "def ", 4) == 0)
2103 break;
2104 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002105 line = ga_concat_strings(&ga, " ");
2106 vim_free(ga.ga_data);
2107 }
2108 else
2109 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002110
Dominique Pelle6e969552021-06-17 13:53:41 +02002111 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002112 debug_context = NULL;
2113
2114 if (end_lnum > iptr->isn_lnum)
2115 vim_free(line);
2116}
2117
Bram Moolenaar4c137212021-04-19 16:48:48 +02002118/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002119 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002120 * Returns OK, FAIL or NOTDONE (uncatchable error).
2121 */
2122 static int
2123execute_storeindex(isn_T *iptr, ectx_T *ectx)
2124{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002125 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002126 typval_T *tv;
2127 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002128 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002129 typval_T *tv_dest = STACK_TV_BOT(-1);
2130 int status = OK;
2131
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002132 if (tv_idx->v_type == VAR_NUMBER)
2133 lidx = (long)tv_idx->vval.v_number;
2134
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002135 // Stack contains:
2136 // -3 value to be stored
2137 // -2 index
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002138 // -1 dict, list, blob or object
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002139 tv = STACK_TV_BOT(-3);
2140 SOURCING_LNUM = iptr->isn_lnum;
2141 if (dest_type == VAR_ANY)
2142 {
2143 dest_type = tv_dest->v_type;
2144 if (dest_type == VAR_DICT)
2145 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002146 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2147 {
2148 // Need to get the member index now that the class is known.
2149 object_T *obj = tv_dest->vval.v_object;
2150 class_T *cl = obj->obj_class;
2151 char_u *member = tv_idx->vval.v_string;
2152
2153 ocmember_T *m = NULL;
2154 for (int i = 0; i < cl->class_obj_member_count; ++i)
2155 {
2156 m = &cl->class_obj_members[i];
2157 if (STRCMP(member, m->ocm_name) == 0)
2158 {
2159 if (*member == '_')
2160 {
2161 semsg(_(e_cannot_access_private_member_str),
2162 m->ocm_name);
2163 status = FAIL;
2164 }
2165
2166 lidx = i;
2167 break;
2168 }
2169 m = NULL;
2170 }
2171
2172 if (m == NULL)
2173 {
2174 semsg(_(e_member_not_found_on_object_str_str),
2175 cl->class_name, member);
2176 status = FAIL;
2177 }
2178 }
2179 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2180 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002181 {
2182 emsg(_(e_number_expected));
2183 status = FAIL;
2184 }
2185 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002186
2187 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002188 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002189 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002190 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002191 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002192
Bram Moolenaare08be092022-02-17 13:08:26 +00002193 if (list == NULL)
2194 {
2195 emsg(_(e_list_not_set));
2196 return FAIL;
2197 }
2198 if (lidx < 0 && list->lv_len + lidx >= 0)
2199 // negative index is relative to the end
2200 lidx = list->lv_len + lidx;
2201 if (lidx < 0 || lidx > list->lv_len)
2202 {
2203 semsg(_(e_list_index_out_of_range_nr), lidx);
2204 return FAIL;
2205 }
2206 if (lidx < list->lv_len)
2207 {
2208 listitem_T *li = list_find(list, lidx);
2209
2210 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002211 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002212 return FAIL;
2213 // overwrite existing list item
2214 clear_tv(&li->li_tv);
2215 li->li_tv = *tv;
2216 }
2217 else
2218 {
2219 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2220 return FAIL;
2221 // append to list, only fails when out of memory
2222 if (list_append_tv(list, tv) == FAIL)
2223 return NOTDONE;
2224 clear_tv(tv);
2225 }
2226 }
2227 else if (dest_type == VAR_DICT)
2228 {
2229 char_u *key = tv_idx->vval.v_string;
2230 dict_T *dict = tv_dest->vval.v_dict;
2231 dictitem_T *di;
2232
2233 SOURCING_LNUM = iptr->isn_lnum;
2234 if (dict == NULL)
2235 {
2236 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002237 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002238 }
2239 if (key == NULL)
2240 key = (char_u *)"";
2241 di = dict_find(dict, key, -1);
2242 if (di != NULL)
2243 {
2244 if (error_if_locked(di->di_tv.v_lock,
2245 e_cannot_change_dict_item))
2246 return FAIL;
2247 // overwrite existing value
2248 clear_tv(&di->di_tv);
2249 di->di_tv = *tv;
2250 }
2251 else
2252 {
2253 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2254 return FAIL;
2255 // add to dict, only fails when out of memory
2256 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2257 return NOTDONE;
2258 clear_tv(tv);
2259 }
2260 }
2261 else if (dest_type == VAR_BLOB)
2262 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002263 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002264 varnumber_T nr;
2265 int error = FALSE;
2266 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002267
2268 if (blob == NULL)
2269 {
2270 emsg(_(e_blob_not_set));
2271 return FAIL;
2272 }
2273 len = blob_len(blob);
2274 if (lidx < 0 && len + lidx >= 0)
2275 // negative index is relative to the end
2276 lidx = len + lidx;
2277
2278 // Can add one byte at the end.
2279 if (lidx < 0 || lidx > len)
2280 {
2281 semsg(_(e_blob_index_out_of_range_nr), lidx);
2282 return FAIL;
2283 }
2284 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2285 return FAIL;
2286 nr = tv_get_number_chk(tv, &error);
2287 if (error)
2288 return FAIL;
2289 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002290 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002291 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2292 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002293 object_T *obj = tv_dest->vval.v_object;
2294 typval_T *otv = (typval_T *)(obj + 1);
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002295
2296 class_T *itf = iptr->isn_arg.storeindex.si_class;
2297 if (itf != NULL)
2298 // convert interface member index to class member index
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002299 lidx = object_index_from_itf_index(itf, FALSE,
2300 lidx, obj->obj_class);
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002301
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002302 clear_tv(&otv[lidx]);
2303 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002304 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002305 else
2306 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002307 status = FAIL;
2308 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002309 }
2310 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002311
2312 clear_tv(tv_idx);
2313 clear_tv(tv_dest);
2314 ectx->ec_stack.ga_len -= 3;
2315 if (status == FAIL)
2316 {
2317 clear_tv(tv);
2318 return FAIL;
2319 }
2320 return OK;
2321}
2322
2323/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002324 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002325 */
2326 static int
2327execute_storerange(isn_T *iptr, ectx_T *ectx)
2328{
2329 typval_T *tv;
2330 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2331 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2332 typval_T *tv_dest = STACK_TV_BOT(-1);
2333 int status = OK;
2334
2335 // Stack contains:
2336 // -4 value to be stored
2337 // -3 first index or "none"
2338 // -2 second index or "none"
2339 // -1 destination list or blob
2340 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002341 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002342 if (tv_dest->v_type == VAR_LIST)
2343 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002344 long n1;
2345 long n2;
2346 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002347
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002348 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2349 if (tv_idx2->v_type == VAR_SPECIAL
2350 && tv_idx2->vval.v_number == VVAL_NONE)
2351 n2 = list_len(tv_dest->vval.v_list) - 1;
2352 else
2353 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2354
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002355 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002356 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002357 status = FAIL;
2358 else
2359 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002360 status = check_range_index_two(tv_dest->vval.v_list,
2361 &n1, li1, &n2, FALSE);
2362 if (status != FAIL)
2363 status = list_assign_range(
2364 tv_dest->vval.v_list,
2365 tv->vval.v_list,
2366 n1,
2367 n2,
2368 tv_idx2->v_type == VAR_SPECIAL,
2369 (char_u *)"=",
2370 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002371 }
2372 }
2373 else if (tv_dest->v_type == VAR_BLOB)
2374 {
2375 varnumber_T n1;
2376 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002377 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002378
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002379 n1 = tv_get_number_chk(tv_idx1, NULL);
2380 if (tv_idx2->v_type == VAR_SPECIAL
2381 && tv_idx2->vval.v_number == VVAL_NONE)
2382 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2383 else
2384 n2 = tv_get_number_chk(tv_idx2, NULL);
2385 bloblen = blob_len(tv_dest->vval.v_blob);
2386
2387 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2388 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002389 status = FAIL;
2390 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002391 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002392 }
2393 else
2394 {
2395 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002396 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002397 }
2398
2399 clear_tv(tv_idx1);
2400 clear_tv(tv_idx2);
2401 clear_tv(tv_dest);
2402 ectx->ec_stack.ga_len -= 4;
2403 clear_tv(tv);
2404
2405 return status;
2406}
2407
2408/*
2409 * Unlet item in list or dict variable.
2410 */
2411 static int
2412execute_unletindex(isn_T *iptr, ectx_T *ectx)
2413{
2414 typval_T *tv_idx = STACK_TV_BOT(-2);
2415 typval_T *tv_dest = STACK_TV_BOT(-1);
2416 int status = OK;
2417
2418 // Stack contains:
2419 // -2 index
2420 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002422 if (tv_dest->v_type == VAR_DICT)
2423 {
2424 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002425 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002426 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002427 semsg(_(e_expected_str_but_got_str),
2428 vartype_name(VAR_STRING),
2429 vartype_name(tv_idx->v_type));
2430 status = FAIL;
2431 }
2432 else
2433 {
2434 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002435 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002436 dictitem_T *di = NULL;
2437
2438 if (d != NULL && value_check_lock(
2439 d->dv_lock, NULL, FALSE))
2440 status = FAIL;
2441 else
2442 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002443 if (tv_idx->v_type == VAR_STRING)
2444 {
2445 key = tv_idx->vval.v_string;
2446 if (key == NULL)
2447 key = (char_u *)"";
2448 }
2449 else
2450 {
2451 key = tv_get_string(tv_idx);
2452 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002453 if (d != NULL)
2454 di = dict_find(d, key, (int)STRLEN(key));
2455 if (di == NULL)
2456 {
2457 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002458 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002459 status = FAIL;
2460 }
2461 else if (var_check_fixed(di->di_flags,
2462 NULL, FALSE)
2463 || var_check_ro(di->di_flags,
2464 NULL, FALSE))
2465 status = FAIL;
2466 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002467 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002468 }
2469 }
2470 }
2471 else if (tv_dest->v_type == VAR_LIST)
2472 {
2473 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002474 if (check_for_number(tv_idx) == FAIL)
2475 {
2476 status = FAIL;
2477 }
2478 else
2479 {
2480 list_T *l = tv_dest->vval.v_list;
2481 long n = (long)tv_idx->vval.v_number;
2482
2483 if (l != NULL && value_check_lock(
2484 l->lv_lock, NULL, FALSE))
2485 status = FAIL;
2486 else
2487 {
2488 listitem_T *li = list_find(l, n);
2489
2490 if (li == NULL)
2491 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002492 semsg(_(e_list_index_out_of_range_nr), n);
2493 status = FAIL;
2494 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002495 else
2496 listitem_remove(l, li);
2497 }
2498 }
2499 }
2500 else
2501 {
2502 status = FAIL;
2503 semsg(_(e_cannot_index_str),
2504 vartype_name(tv_dest->v_type));
2505 }
2506
2507 clear_tv(tv_idx);
2508 clear_tv(tv_dest);
2509 ectx->ec_stack.ga_len -= 2;
2510
2511 return status;
2512}
2513
2514/*
2515 * Unlet a range of items in a list variable.
2516 */
2517 static int
2518execute_unletrange(isn_T *iptr, ectx_T *ectx)
2519{
2520 // Stack contains:
2521 // -3 index1
2522 // -2 index2
2523 // -1 dict or list
2524 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2525 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2526 typval_T *tv_dest = STACK_TV_BOT(-1);
2527 int status = OK;
2528
2529 if (tv_dest->v_type == VAR_LIST)
2530 {
2531 // indexes must be a number
2532 SOURCING_LNUM = iptr->isn_lnum;
2533 if (check_for_number(tv_idx1) == FAIL
2534 || (tv_idx2->v_type != VAR_SPECIAL
2535 && check_for_number(tv_idx2) == FAIL))
2536 {
2537 status = FAIL;
2538 }
2539 else
2540 {
2541 list_T *l = tv_dest->vval.v_list;
2542 long n1 = (long)tv_idx1->vval.v_number;
2543 long n2 = tv_idx2->v_type == VAR_SPECIAL
2544 ? 0 : (long)tv_idx2->vval.v_number;
2545 listitem_T *li;
2546
2547 li = list_find_index(l, &n1);
2548 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002549 {
2550 semsg(_(e_list_index_out_of_range_nr),
2551 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002552 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002553 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002554 else
2555 {
2556 if (n1 < 0)
2557 n1 = list_idx_of_item(l, li);
2558 if (n2 < 0)
2559 {
2560 listitem_T *li2 = list_find(l, n2);
2561
2562 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002563 {
2564 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002565 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002566 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002567 else
2568 n2 = list_idx_of_item(l, li2);
2569 }
2570 if (status != FAIL
2571 && tv_idx2->v_type != VAR_SPECIAL
2572 && n2 < n1)
2573 {
2574 semsg(_(e_list_index_out_of_range_nr), n2);
2575 status = FAIL;
2576 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002577 if (status != FAIL)
2578 list_unlet_range(l, li, n1,
2579 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002580 }
2581 }
2582 }
2583 else
2584 {
2585 status = FAIL;
2586 SOURCING_LNUM = iptr->isn_lnum;
2587 semsg(_(e_cannot_index_str),
2588 vartype_name(tv_dest->v_type));
2589 }
2590
2591 clear_tv(tv_idx1);
2592 clear_tv(tv_idx2);
2593 clear_tv(tv_dest);
2594 ectx->ec_stack.ga_len -= 3;
2595
2596 return status;
2597}
2598
2599/*
2600 * Top of a for loop.
2601 */
2602 static int
2603execute_for(isn_T *iptr, ectx_T *ectx)
2604{
2605 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002606 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002607 typval_T *ltv = STACK_TV_BOT(-1);
2608 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002609 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002610
2611 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2612 return FAIL;
2613 if (ltv->v_type == VAR_LIST)
2614 {
2615 list_T *list = ltv->vval.v_list;
2616
2617 // push the next item from the list
2618 ++idxtv->vval.v_number;
2619 if (list == NULL
2620 || idxtv->vval.v_number >= list->lv_len)
2621 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002622 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002623 }
2624 else if (list->lv_first == &range_list_item)
2625 {
2626 // non-materialized range() list
2627 tv = STACK_TV_BOT(0);
2628 tv->v_type = VAR_NUMBER;
2629 tv->v_lock = 0;
2630 tv->vval.v_number = list_find_nr(
2631 list, idxtv->vval.v_number, NULL);
2632 ++ectx->ec_stack.ga_len;
2633 }
2634 else
2635 {
2636 listitem_T *li = list_find(list,
2637 idxtv->vval.v_number);
2638
2639 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2640 ++ectx->ec_stack.ga_len;
2641 }
2642 }
2643 else if (ltv->v_type == VAR_STRING)
2644 {
2645 char_u *str = ltv->vval.v_string;
2646
2647 // The index is for the last byte of the previous
2648 // character.
2649 ++idxtv->vval.v_number;
2650 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2651 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002652 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002653 }
2654 else
2655 {
2656 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2657
2658 // Push the next character from the string.
2659 tv = STACK_TV_BOT(0);
2660 tv->v_type = VAR_STRING;
2661 tv->vval.v_string = vim_strnsave(
2662 str + idxtv->vval.v_number, clen);
2663 ++ectx->ec_stack.ga_len;
2664 idxtv->vval.v_number += clen - 1;
2665 }
2666 }
2667 else if (ltv->v_type == VAR_BLOB)
2668 {
2669 blob_T *blob = ltv->vval.v_blob;
2670
2671 // When we get here the first time make a copy of the
2672 // blob, so that the iteration still works when it is
2673 // changed.
2674 if (idxtv->vval.v_number == -1 && blob != NULL)
2675 {
2676 blob_copy(blob, ltv);
2677 blob_unref(blob);
2678 blob = ltv->vval.v_blob;
2679 }
2680
2681 // The index is for the previous byte.
2682 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002683 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002684 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002685 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002686 }
2687 else
2688 {
2689 // Push the next byte from the blob.
2690 tv = STACK_TV_BOT(0);
2691 tv->v_type = VAR_NUMBER;
2692 tv->vval.v_number = blob_get(blob,
2693 idxtv->vval.v_number);
2694 ++ectx->ec_stack.ga_len;
2695 }
2696 }
2697 else
2698 {
2699 semsg(_(e_for_loop_on_str_not_supported),
2700 vartype_name(ltv->v_type));
2701 return FAIL;
2702 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002703
2704 if (jump)
2705 {
2706 // past the end of the list/string/blob, jump to "endfor"
2707 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2708 may_restore_cmdmod(&ectx->ec_funclocal);
2709 }
2710 else
2711 {
2712 // Store the current number of funcrefs, this may be used in
2713 // ISN_LOOPEND. The variable index is always one more than the loop
2714 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002715 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002716 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2717 }
2718
2719 return OK;
2720}
2721
2722/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002723 * Code for handling variables declared inside a loop and used in a closure.
2724 * This is very similar to what is done with funcstack_T. The difference is
2725 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2726 * scope of the block inside a loop and each loop may have its own.
2727 */
2728
2729// Double linked list of loopvars_T in use.
2730static loopvars_T *first_loopvars = NULL;
2731
2732 static void
2733add_loopvars_to_list(loopvars_T *loopvars)
2734{
2735 // Link in list of loopvarss.
2736 if (first_loopvars != NULL)
2737 first_loopvars->lvs_prev = loopvars;
2738 loopvars->lvs_next = first_loopvars;
2739 loopvars->lvs_prev = NULL;
2740 first_loopvars = loopvars;
2741}
2742
2743 static void
2744remove_loopvars_from_list(loopvars_T *loopvars)
2745{
2746 if (loopvars->lvs_prev == NULL)
2747 first_loopvars = loopvars->lvs_next;
2748 else
2749 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2750 if (loopvars->lvs_next != NULL)
2751 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2752}
2753
2754/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002755 * End of a for or while loop: Handle any variables used by a closure.
2756 */
2757 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002758execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002759{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002760 endloop_T *endloop = &iptr->isn_arg.endloop;
2761 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2762 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002763 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002764 garray_T *gap = &ectx->ec_funcrefs;
2765 int closure_in_use = FALSE;
2766 loopvars_T *loopvars;
2767 typval_T *stack;
2768 int idx;
2769
Bram Moolenaarcc341812022-09-19 15:54:34 +01002770 // Check if any created closure is still being referenced and loopvars have
2771 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002772 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2773 {
2774 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2775
Bram Moolenaarcc341812022-09-19 15:54:34 +01002776 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002777 {
2778 int refcount = pt->pt_refcount;
2779 int i;
2780
2781 // A Reference in a variable inside the loop doesn't count, it gets
2782 // unreferenced at the end of the loop.
2783 for (i = 0; i < endloop->end_var_count; ++i)
2784 {
2785 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2786
2787 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2788 --refcount;
2789 }
2790 if (refcount > 1)
2791 {
2792 closure_in_use = TRUE;
2793 break;
2794 }
2795 }
2796 }
2797
2798 // If no function reference were created since the start of the loop block
2799 // or it is no longer referenced there is nothing to do.
2800 if (!closure_in_use)
2801 return OK;
2802
2803 // A closure is using variables declared inside the loop.
2804 // Move them to the called function.
2805 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2806 if (loopvars == NULL)
2807 return FAIL;
2808
2809 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2810 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2811 loopvars->lvs_ga.ga_data = stack;
2812 if (stack == NULL)
2813 {
2814 vim_free(loopvars);
2815 return FAIL;
2816 }
2817 add_loopvars_to_list(loopvars);
2818
2819 // Move the variable values.
2820 for (idx = 0; idx < endloop->end_var_count; ++idx)
2821 {
2822 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2823
2824 *(stack + idx) = *tv;
2825 tv->v_type = VAR_UNKNOWN;
2826 }
2827
2828 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2829 {
2830 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2831
Bram Moolenaarcc341812022-09-19 15:54:34 +01002832 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002833 {
2834 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002835 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002836
Bram Moolenaarcc341812022-09-19 15:54:34 +01002837 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2838 pt->pt_outer.out_loop[depth].var_idx -=
2839 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002840 }
2841 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002842
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002843 return OK;
2844}
2845
2846/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002847 * Called when a partial is freed or its reference count goes down to one. The
2848 * loopvars may be the only reference to the partials in the local variables.
2849 * Go over all of them, the funcref and can be freed if all partials
2850 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002851 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002852 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002853 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002854loopvars_check_refcount(loopvars_T *loopvars)
2855{
2856 int i;
2857 garray_T *gap = &loopvars->lvs_ga;
2858 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002859 typval_T *stack = gap->ga_data;
2860
Bram Moolenaarcc341812022-09-19 15:54:34 +01002861 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2862 return FALSE;
2863 for (i = 0; i < gap->ga_len; ++i)
2864 {
2865 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2866
2867 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2868 && tv->vval.v_partial->pt_refcount == 1)
2869 {
2870 int depth;
2871
2872 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2873 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2874 ++done;
2875 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002876 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002877 if (done != loopvars->lvs_min_refcount)
2878 return FALSE;
2879
2880 // All partials referencing the loopvars have a reference count of
2881 // one, thus the loopvars is no longer of use.
2882 stack = gap->ga_data;
2883 for (i = 0; i < gap->ga_len; ++i)
2884 clear_tv(stack + i);
2885 vim_free(stack);
2886 remove_loopvars_from_list(loopvars);
2887 vim_free(loopvars);
2888 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002889}
2890
2891/*
2892 * For garbage collecting: set references in all variables referenced by
2893 * all loopvars.
2894 */
2895 int
2896set_ref_in_loopvars(int copyID)
2897{
2898 loopvars_T *loopvars;
2899
2900 for (loopvars = first_loopvars; loopvars != NULL;
2901 loopvars = loopvars->lvs_next)
2902 {
2903 typval_T *stack = loopvars->lvs_ga.ga_data;
2904 int i;
2905
2906 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2907 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2908 return TRUE; // abort
2909 }
2910 return FALSE;
2911}
2912
2913/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002914 * Load instruction for w:/b:/g:/t: variable.
2915 * "isn_type" is used instead of "iptr->isn_type".
2916 */
2917 static int
2918load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2919{
2920 dictitem_T *di = NULL;
2921 hashtab_T *ht = NULL;
2922 char namespace;
2923
2924 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2925 return NOTDONE;
2926 switch (isn_type)
2927 {
2928 case ISN_LOADG:
2929 ht = get_globvar_ht();
2930 namespace = 'g';
2931 break;
2932 case ISN_LOADB:
2933 ht = &curbuf->b_vars->dv_hashtab;
2934 namespace = 'b';
2935 break;
2936 case ISN_LOADW:
2937 ht = &curwin->w_vars->dv_hashtab;
2938 namespace = 'w';
2939 break;
2940 case ISN_LOADT:
2941 ht = &curtab->tp_vars->dv_hashtab;
2942 namespace = 't';
2943 break;
2944 default: // Cannot reach here
2945 return NOTDONE;
2946 }
2947 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2948
Bram Moolenaar06b77222022-01-25 15:51:56 +00002949 if (di == NULL)
2950 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002951 if (isn_type == ISN_LOADG)
2952 {
2953 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2954
2955 // g:Something could be a function
2956 if (ufunc != NULL)
2957 {
2958 typval_T *tv = STACK_TV_BOT(0);
2959
2960 ++ectx->ec_stack.ga_len;
2961 tv->v_type = VAR_FUNC;
2962 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2963 if (tv->vval.v_string == NULL)
2964 return FAIL;
2965 STRCPY(tv->vval.v_string, "g:");
2966 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2967 return OK;
2968 }
2969 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002970 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002971 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002972 // no check if the item exists in the script but
2973 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002974 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002975 else
2976 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002977 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002978 return FAIL;
2979 }
2980 else
2981 {
2982 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2983 ++ectx->ec_stack.ga_len;
2984 }
2985 return OK;
2986}
2987
Bram Moolenaard0200c82023-01-28 15:19:40 +00002988
2989 static void
2990object_required_error(typval_T *tv)
2991{
2992 garray_T type_list;
2993 ga_init2(&type_list, sizeof(type_T *), 10);
2994 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
2995 char *tofree = NULL;
2996 char *typename = type_name(type, &tofree);
2997 semsg(_(e_object_required_found_str), typename);
2998 vim_free(tofree);
2999 clear_type_list(&type_list);
3000}
3001
Bram Moolenaar06b77222022-01-25 15:51:56 +00003002/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003003 * Execute instructions in execution context "ectx".
3004 * Return OK or FAIL;
3005 */
3006 static int
3007exec_instructions(ectx_T *ectx)
3008{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003009 int ret = FAIL;
3010 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003011 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003012
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003013 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003014 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003015
Bram Moolenaarff652882021-05-16 15:24:49 +02003016 // Only catch exceptions in this instruction list.
3017 ectx->ec_trylevel_at_start = trylevel;
3018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 for (;;)
3020 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003021 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003023 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003024
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003025 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003026 {
3027 line_breakcheck();
3028 breakcheck_count = 0;
3029 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003030 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003031 {
3032 // Turn CTRL-C into an exception.
3033 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003034 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003035 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003036 did_throw = TRUE;
3037 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003039 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003040 {
3041 // Turn an error message into an exception.
3042 did_emsg = FALSE;
3043 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003044 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003045 did_throw = TRUE;
3046 *msg_list = NULL;
3047 }
3048
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003049 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003051 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003052 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003053 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054
3055 // An exception jumps to the first catch, finally, or returns from
3056 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003057 while (index > 0)
3058 {
3059 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02003060 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003061 break;
3062 // In the catch and finally block of this try we have to go up
3063 // one level.
3064 --index;
3065 trycmd = NULL;
3066 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003067 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003069 if (trycmd->tcd_in_catch)
3070 {
3071 // exception inside ":catch", jump to ":finally" once
3072 ectx->ec_iidx = trycmd->tcd_finally_idx;
3073 trycmd->tcd_finally_idx = 0;
3074 }
3075 else
3076 // jump to first ":catch"
3077 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003078 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003079 did_throw = FALSE; // don't come back here until :endtry
3080 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 }
3082 else
3083 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003084 // Not inside try or need to return from current functions.
3085 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003086 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003087 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003088 tv = STACK_TV_BOT(0);
3089 tv->v_type = VAR_NUMBER;
3090 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003091 ++ectx->ec_stack.ga_len;
3092 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003094 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003095 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003096 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003097 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 goto done;
3099 }
3100
Bram Moolenaar4c137212021-04-19 16:48:48 +02003101 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003102 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 }
3104 continue;
3105 }
3106
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003107 /*
3108 * Big switch on the instruction. Most compilers will be turning this
3109 * into an efficient lookup table, since the "case" values are an enum
3110 * with sequential numbers. It may look ugly, but it should be the
3111 * most efficient way.
3112 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003113 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 switch (iptr->isn_type)
3115 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003116 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003117 case ISN_CONSTRUCT:
3118 // "this" is always the local variable at index zero
3119 tv = STACK_TV_VAR(0);
3120 tv->v_type = VAR_OBJECT;
3121 tv->vval.v_object = alloc_clear(
3122 iptr->isn_arg.construct.construct_size);
3123 tv->vval.v_object->obj_class =
3124 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003125 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003126 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003127 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003128 break;
3129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 // execute Ex command line
3131 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003132 if (exec_command(iptr) == FAIL)
3133 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 break;
3135
Bram Moolenaar20677332021-06-06 17:02:53 +02003136 // execute Ex command line split at NL characters.
3137 case ISN_EXEC_SPLIT:
3138 {
3139 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003140 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003141
3142 SOURCING_LNUM = iptr->isn_lnum;
3143 CLEAR_FIELD(cookie);
3144 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3145 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003146 line = get_split_sourceline(0, &cookie, 0, 0);
3147 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003148 get_split_sourceline, &cookie,
3149 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3150 == FAIL
3151 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003152 {
3153 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003154 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003155 }
3156 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003157 }
3158 break;
3159
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003160 // execute Ex command line that is only a range
3161 case ISN_EXECRANGE:
3162 {
3163 exarg_T ea;
3164 char *error = NULL;
3165
3166 CLEAR_FIELD(ea);
3167 ea.cmdidx = CMD_SIZE;
3168 ea.addr_type = ADDR_LINES;
3169 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003170 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003171 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003172 if (ea.cmd == NULL)
3173 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003174 // error is always NULL when using ADDR_LINES
3175 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003176 if (error != NULL)
3177 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003178 emsg(error);
3179 goto on_error;
3180 }
3181 }
3182 break;
3183
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003184 // Evaluate an expression with legacy syntax, push it onto the
3185 // stack.
3186 case ISN_LEGACY_EVAL:
3187 {
3188 char_u *arg = iptr->isn_arg.string;
3189 int res;
3190 int save_flags = cmdmod.cmod_flags;
3191
Bram Moolenaar35578162021-08-02 19:10:38 +02003192 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003193 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003194 tv = STACK_TV_BOT(0);
3195 init_tv(tv);
3196 cmdmod.cmod_flags |= CMOD_LEGACY;
3197 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3198 cmdmod.cmod_flags = save_flags;
3199 if (res == FAIL)
3200 goto on_error;
3201 ++ectx->ec_stack.ga_len;
3202 }
3203 break;
3204
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003205 // push typeval VAR_INSTR with instructions to be executed
3206 case ISN_INSTR:
3207 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003208 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003209 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003210 tv = STACK_TV_BOT(0);
3211 tv->vval.v_instr = ALLOC_ONE(instr_T);
3212 if (tv->vval.v_instr == NULL)
3213 goto on_error;
3214 ++ectx->ec_stack.ga_len;
3215
3216 tv->v_type = VAR_INSTR;
3217 tv->vval.v_instr->instr_ectx = ectx;
3218 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3219 }
3220 break;
3221
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003222 case ISN_SOURCE:
3223 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003224 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003225
Bram Moolenaar89445512022-04-14 12:58:23 +01003226 SOURCING_LNUM = iptr->isn_lnum;
3227 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003228 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003229 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003230 }
3231 break;
3232
Bram Moolenaar4c137212021-04-19 16:48:48 +02003233 // execute :substitute with an expression
3234 case ISN_SUBSTITUTE:
3235 {
3236 subs_T *subs = &iptr->isn_arg.subs;
3237 source_cookie_T cookie;
3238 struct subs_expr_S *save_instr = substitute_instr;
3239 struct subs_expr_S subs_instr;
3240 int res;
3241
3242 subs_instr.subs_ectx = ectx;
3243 subs_instr.subs_instr = subs->subs_instr;
3244 subs_instr.subs_status = OK;
3245 substitute_instr = &subs_instr;
3246
3247 SOURCING_LNUM = iptr->isn_lnum;
3248 // This is very much like ISN_EXEC
3249 CLEAR_FIELD(cookie);
3250 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3251 res = do_cmdline(subs->subs_cmd,
3252 getsourceline, &cookie,
3253 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3254 substitute_instr = save_instr;
3255
3256 if (res == FAIL || did_emsg
3257 || subs_instr.subs_status == FAIL)
3258 goto on_error;
3259 }
3260 break;
3261
3262 case ISN_FINISH:
3263 goto done;
3264
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003265 case ISN_REDIRSTART:
3266 // create a dummy entry for var_redir_str()
3267 if (alloc_redir_lval() == FAIL)
3268 goto on_error;
3269
3270 // The output is stored in growarray "redir_ga" until
3271 // redirection ends.
3272 init_redir_ga();
3273 redir_vname = 1;
3274 break;
3275
3276 case ISN_REDIREND:
3277 {
3278 char_u *res = get_clear_redir_ga();
3279
3280 // End redirection, put redirected text on the stack.
3281 clear_redir_lval();
3282 redir_vname = 0;
3283
Bram Moolenaar35578162021-08-02 19:10:38 +02003284 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003285 {
3286 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003287 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003288 }
3289 tv = STACK_TV_BOT(0);
3290 tv->v_type = VAR_STRING;
3291 tv->vval.v_string = res;
3292 ++ectx->ec_stack.ga_len;
3293 }
3294 break;
3295
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003296 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003297#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003298 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003299 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3300 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003301 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003302#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003303 break;
3304
3305 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003306#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003307 {
3308 exarg_T ea;
3309 int res;
3310
3311 CLEAR_FIELD(ea);
3312 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3313 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3314 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3315 --ectx->ec_stack.ga_len;
3316 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003317 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003318 res = cexpr_core(&ea, tv);
3319 clear_tv(tv);
3320 if (res == FAIL)
3321 goto on_error;
3322 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003323#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003324 break;
3325
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003326 // execute Ex command from pieces on the stack
3327 case ISN_EXECCONCAT:
3328 {
3329 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003330 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003331 int pass;
3332 int i;
3333 char_u *cmd = NULL;
3334 char_u *str;
3335
3336 for (pass = 1; pass <= 2; ++pass)
3337 {
3338 for (i = 0; i < count; ++i)
3339 {
3340 tv = STACK_TV_BOT(i - count);
3341 str = tv->vval.v_string;
3342 if (str != NULL && *str != NUL)
3343 {
3344 if (pass == 2)
3345 STRCPY(cmd + len, str);
3346 len += STRLEN(str);
3347 }
3348 if (pass == 2)
3349 clear_tv(tv);
3350 }
3351 if (pass == 1)
3352 {
3353 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003354 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003355 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003356 len = 0;
3357 }
3358 }
3359
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003360 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003361 do_cmdline_cmd(cmd);
3362 vim_free(cmd);
3363 }
3364 break;
3365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 // execute :echo {string} ...
3367 case ISN_ECHO:
3368 {
3369 int count = iptr->isn_arg.echo.echo_count;
3370 int atstart = TRUE;
3371 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003372 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373
3374 for (idx = 0; idx < count; ++idx)
3375 {
3376 tv = STACK_TV_BOT(idx - count);
3377 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3378 &atstart, &needclr);
3379 clear_tv(tv);
3380 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003381 if (needclr)
3382 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003383 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 }
3385 break;
3386
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003387 // :execute {string} ...
3388 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003389 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003390 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003391 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003392 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003393 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003394 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003395 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003396 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003397 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003398 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003399 garray_T ga;
3400 char_u buf[NUMBUFLEN];
3401 char_u *p;
3402 int len;
3403 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003404 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003405
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003406 if (iptr->isn_type == ISN_ECHOWINDOW)
3407 count = iptr->isn_arg.echowin.ewin_count;
3408 else
3409 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003410 ga_init2(&ga, 1, 80);
3411 for (idx = 0; idx < count; ++idx)
3412 {
3413 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003414 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003415 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003416 if (tv->v_type == VAR_CHANNEL
3417 || tv->v_type == VAR_JOB)
3418 {
3419 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003420 semsg(_(e_using_invalid_value_as_string_str),
3421 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003422 break;
3423 }
3424 else
3425 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003426 }
3427 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003428 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003429
3430 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003431 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003432 failed = TRUE;
3433 else
3434 {
3435 if (ga.ga_len > 0)
3436 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3437 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3438 ga.ga_len += len;
3439 }
3440 clear_tv(tv);
3441 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003442 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003443 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003444 {
3445 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003446 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003447 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003448
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003449 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003450 {
3451 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003452 {
3453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003454 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003455 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003456 {
3457 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003458 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003459 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003460 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003461 else
3462 {
3463 msg_sb_eol();
3464 if (iptr->isn_type == ISN_ECHOMSG)
3465 {
3466 msg_attr(ga.ga_data, echo_attr);
3467 out_flush();
3468 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003469#ifdef HAS_MESSAGE_WINDOW
3470 else if (iptr->isn_type == ISN_ECHOWINDOW)
3471 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003472 start_echowindow(
3473 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003474 msg_attr(ga.ga_data, echo_attr);
3475 end_echowindow();
3476 }
3477#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003478 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3479 {
3480 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3481 TRUE);
3482 ui_write((char_u *)"\r\n", 2, TRUE);
3483 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003484 else
3485 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003486 SOURCING_LNUM = iptr->isn_lnum;
3487 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003488 }
3489 }
3490 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003491 ga_clear(&ga);
3492 }
3493 break;
3494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 // load local variable or argument
3496 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003497 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003498 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003500 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 break;
3502
3503 // load v: variable
3504 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003505 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003506 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003508 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 break;
3510
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003511 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 case ISN_LOADSCRIPT:
3513 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003514 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 svar_T *sv;
3516
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003517 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003518 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003519 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003520 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003521 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003522 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003524 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 }
3526 break;
3527
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003528 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003530 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003532 int sid = iptr->isn_arg.loadstore.ls_sid;
3533 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003534 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 if (di == NULL)
3538 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003539 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003540 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003541 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 }
3543 else
3544 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003545 if (iptr->isn_type == ISN_LOADEXPORT)
3546 {
3547 int idx = get_script_item_idx(sid, name, 0,
3548 NULL, NULL);
3549 svar_T *sv;
3550
3551 if (idx >= 0)
3552 {
3553 sv = ((svar_T *)SCRIPT_ITEM(sid)
3554 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003555 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003556 {
3557 SOURCING_LNUM = iptr->isn_lnum;
3558 semsg(_(e_item_not_exported_in_script_str),
3559 name);
3560 goto on_error;
3561 }
3562 }
3563 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003564 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003565 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003567 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 }
3569 }
3570 break;
3571
Bram Moolenaard3aac292020-04-19 14:32:17 +02003572 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003574 case ISN_LOADB:
3575 case ISN_LOADW:
3576 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003578 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003579
Bram Moolenaar06b77222022-01-25 15:51:56 +00003580 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003581 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003582 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003583 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 break;
3587
Bram Moolenaar03290b82020-12-19 16:30:44 +01003588 // load autoload variable
3589 case ISN_LOADAUTO:
3590 {
3591 char_u *name = iptr->isn_arg.string;
3592
Bram Moolenaar35578162021-08-02 19:10:38 +02003593 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003594 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003595 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003596 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003597 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003598 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003599 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003600 }
3601 break;
3602
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003603 // load g:/b:/w:/t: namespace
3604 case ISN_LOADGDICT:
3605 case ISN_LOADBDICT:
3606 case ISN_LOADWDICT:
3607 case ISN_LOADTDICT:
3608 {
3609 dict_T *d = NULL;
3610
3611 switch (iptr->isn_type)
3612 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003613 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3614 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3615 case ISN_LOADWDICT: d = curwin->w_vars; break;
3616 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003617 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003618 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003619 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003620 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003621 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003622 tv = STACK_TV_BOT(0);
3623 tv->v_type = VAR_DICT;
3624 tv->v_lock = 0;
3625 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003626 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003627 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003628 }
3629 break;
3630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 // load &option
3632 case ISN_LOADOPT:
3633 {
3634 typval_T optval;
3635 char_u *name = iptr->isn_arg.string;
3636
Bram Moolenaara8c17702020-04-01 21:17:24 +02003637 // This is not expected to fail, name is checked during
3638 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003639 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003640 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003641 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003642 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003644 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 }
3646 break;
3647
3648 // load $ENV
3649 case ISN_LOADENV:
3650 {
3651 typval_T optval;
3652 char_u *name = iptr->isn_arg.string;
3653
Bram Moolenaar35578162021-08-02 19:10:38 +02003654 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003655 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003656 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003657 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003659 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 }
3661 break;
3662
3663 // load @register
3664 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003665 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003666 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 tv = STACK_TV_BOT(0);
3668 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003669 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003670 // This may result in NULL, which should be equivalent to an
3671 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 tv->vval.v_string = get_reg_contents(
3673 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003674 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 break;
3676
3677 // store local variable
3678 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003679 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 tv = STACK_TV_VAR(iptr->isn_arg.number);
3681 clear_tv(tv);
3682 *tv = *STACK_TV_BOT(0);
3683 break;
3684
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003685 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003686 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003687 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003688 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003689 int sid = iptr->isn_arg.loadstore.ls_sid;
3690 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003691 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003692 dictitem_T *di = find_var_in_ht(ht, 0,
3693 iptr->isn_type == ISN_STORES
3694 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003695
Bram Moolenaar4c137212021-04-19 16:48:48 +02003696 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003697 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003698 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003699 {
3700 if (iptr->isn_type == ISN_STOREEXPORT)
3701 {
3702 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003703 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003704 goto on_error;
3705 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003706 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003707 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003708 else
3709 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003710 if (iptr->isn_type == ISN_STOREEXPORT)
3711 {
3712 int idx = get_script_item_idx(sid, name, 0,
3713 NULL, NULL);
3714
Bram Moolenaar06651632022-04-27 17:54:25 +01003715 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003716 if (idx >= 0)
3717 {
3718 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3719 ->sn_var_vals.ga_data) + idx;
3720
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003721 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003722 {
3723 semsg(_(e_item_not_exported_in_script_str),
3724 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003725 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003726 goto on_error;
3727 }
3728 }
3729 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003730 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003731 {
3732 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003733 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003734 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003735 clear_tv(&di->di_tv);
3736 di->di_tv = *STACK_TV_BOT(0);
3737 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003738 }
3739 break;
3740
3741 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 case ISN_STORESCRIPT:
3743 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003744 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3745 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003747 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003748 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003749 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003750 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003751
3752 // "const" and "final" are checked at compile time, locking
3753 // the value needs to be checked here.
3754 SOURCING_LNUM = iptr->isn_lnum;
3755 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003756 {
3757 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003758 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003759 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 clear_tv(sv->sv_tv);
3762 *sv->sv_tv = *STACK_TV_BOT(0);
3763 }
3764 break;
3765
3766 // store option
3767 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003768 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003770 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3771 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 long n = 0;
3773 char_u *s = NULL;
3774 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003775 char_u numbuf[NUMBUFLEN];
3776 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777
Bram Moolenaar4c137212021-04-19 16:48:48 +02003778 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 tv = STACK_TV_BOT(0);
3780 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003781 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003783 if (s == NULL)
3784 s = (char_u *)"";
3785 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003786 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3787 {
3788 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003789 // If the option can be set to a function reference or
3790 // a lambda and the passed value is a function
3791 // reference, then convert it to the name (string) of
3792 // the function reference.
3793 s = tv2string(tv, &tofree, numbuf, 0);
3794 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003795 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003796 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003797 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003798 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003799 goto on_error;
3800 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003803 // must be VAR_NUMBER, CHECKTYPE makes sure
3804 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003805 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003806 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003807 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 if (msg != NULL)
3809 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003810 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003812 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 }
3815 break;
3816
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003817 // store $ENV
3818 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003819 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003820 tv = STACK_TV_BOT(0);
3821 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3822 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003823 break;
3824
3825 // store @r
3826 case ISN_STOREREG:
3827 {
3828 int reg = iptr->isn_arg.number;
3829
Bram Moolenaar4c137212021-04-19 16:48:48 +02003830 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003831 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003832 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003833 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003834 }
3835 break;
3836
3837 // store v: variable
3838 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003840 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3841 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003842 // should not happen, type is checked when compiling
3843 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003844 break;
3845
Bram Moolenaard3aac292020-04-19 14:32:17 +02003846 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003848 case ISN_STOREB:
3849 case ISN_STOREW:
3850 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003852 dictitem_T *di;
3853 hashtab_T *ht;
3854 char_u *name = iptr->isn_arg.string + 2;
3855
Bram Moolenaard3aac292020-04-19 14:32:17 +02003856 switch (iptr->isn_type)
3857 {
3858 case ISN_STOREG:
3859 ht = get_globvar_ht();
3860 break;
3861 case ISN_STOREB:
3862 ht = &curbuf->b_vars->dv_hashtab;
3863 break;
3864 case ISN_STOREW:
3865 ht = &curwin->w_vars->dv_hashtab;
3866 break;
3867 case ISN_STORET:
3868 ht = &curtab->tp_vars->dv_hashtab;
3869 break;
3870 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003871 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003872 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873
Bram Moolenaar4c137212021-04-19 16:48:48 +02003874 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003875 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003877 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 else
3879 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003880 SOURCING_LNUM = iptr->isn_lnum;
3881 if (var_check_permission(di, name) == FAIL)
3882 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 clear_tv(&di->di_tv);
3884 di->di_tv = *STACK_TV_BOT(0);
3885 }
3886 }
3887 break;
3888
Bram Moolenaar03290b82020-12-19 16:30:44 +01003889 // store an autoload variable
3890 case ISN_STOREAUTO:
3891 SOURCING_LNUM = iptr->isn_lnum;
3892 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3893 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003894 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003895 break;
3896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 // store number in local variable
3898 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003899 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 clear_tv(tv);
3901 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003902 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 break;
3904
Bram Moolenaar590162c2022-12-24 21:24:06 +00003905 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003906 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003907 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003908 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003909
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003910 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003911 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003912 if (res == NOTDONE)
3913 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003914 }
3915 break;
3916
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003917 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003918 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003919 if (execute_storerange(iptr, ectx) == FAIL)
3920 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003921 break;
3922
Bram Moolenaard505d172022-12-18 21:42:55 +00003923 case ISN_LOAD_CLASSMEMBER:
3924 {
3925 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3926 goto theend;
3927 classmember_T *cm = &iptr->isn_arg.classmember;
3928 *STACK_TV_BOT(0) =
3929 cm->cm_class->class_members_tv[cm->cm_idx];
3930 ++ectx->ec_stack.ga_len;
3931 }
3932 break;
3933
3934 case ISN_STORE_CLASSMEMBER:
3935 {
3936 classmember_T *cm = &iptr->isn_arg.classmember;
3937 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3938 clear_tv(tv);
3939 *tv = *STACK_TV_BOT(-1);
3940 --ectx->ec_stack.ga_len;
3941 }
3942 break;
3943
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003944 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003945 case ISN_LOADOUTER:
3946 case ISN_STOREOUTER:
3947 {
3948 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003949 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3950 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003951
3952 while (depth > 1 && outer != NULL)
3953 {
3954 outer = outer->out_up;
3955 --depth;
3956 }
3957 if (outer == NULL)
3958 {
3959 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003960 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3961 || ectx->ec_outer_ref == NULL)
3962 // Possibly :def function called from legacy
3963 // context.
3964 emsg(_(e_closure_called_from_invalid_context));
3965 else
3966 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003967 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003968 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003969 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003970 // Variable declared in loop. May be copied if the
3971 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003972 tv = ((typval_T *)outer->out_loop[-depth - 1]
3973 .stack->ga_data)
3974 + outer->out_loop[-depth - 1].var_idx
3975 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003976 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003977 // Variable declared in a function. May be copied if
3978 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003979 tv = ((typval_T *)outer->out_stack->ga_data)
3980 + outer->out_frame_idx + STACK_FRAME_SIZE
3981 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003982 if (iptr->isn_type == ISN_LOADOUTER)
3983 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003984 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003985 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003986 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003987 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003988 }
3989 else
3990 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003991 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003992 clear_tv(tv);
3993 *tv = *STACK_TV_BOT(0);
3994 }
3995 }
3996 break;
3997
Bram Moolenaar752fc692021-01-04 21:57:11 +01003998 // unlet item in list or dict variable
3999 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004000 if (execute_unletindex(iptr, ectx) == FAIL)
4001 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004002 break;
4003
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004004 // unlet range of items in list variable
4005 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004006 if (execute_unletrange(iptr, ectx) == FAIL)
4007 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004008 break;
4009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 // push constant
4011 case ISN_PUSHNR:
4012 case ISN_PUSHBOOL:
4013 case ISN_PUSHSPEC:
4014 case ISN_PUSHF:
4015 case ISN_PUSHS:
4016 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004017 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004018 case ISN_PUSHCHANNEL:
4019 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004020 case ISN_PUSHOBJ:
4021 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004022 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004023 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004025 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004026 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 switch (iptr->isn_type)
4028 {
4029 case ISN_PUSHNR:
4030 tv->v_type = VAR_NUMBER;
4031 tv->vval.v_number = iptr->isn_arg.number;
4032 break;
4033 case ISN_PUSHBOOL:
4034 tv->v_type = VAR_BOOL;
4035 tv->vval.v_number = iptr->isn_arg.number;
4036 break;
4037 case ISN_PUSHSPEC:
4038 tv->v_type = VAR_SPECIAL;
4039 tv->vval.v_number = iptr->isn_arg.number;
4040 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 case ISN_PUSHF:
4042 tv->v_type = VAR_FLOAT;
4043 tv->vval.v_float = iptr->isn_arg.fnumber;
4044 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 case ISN_PUSHBLOB:
4046 blob_copy(iptr->isn_arg.blob, tv);
4047 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004048 case ISN_PUSHFUNC:
4049 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004050 if (iptr->isn_arg.string == NULL)
4051 tv->vval.v_string = NULL;
4052 else
4053 tv->vval.v_string =
4054 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004055 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004056 case ISN_PUSHCHANNEL:
4057#ifdef FEAT_JOB_CHANNEL
4058 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004059 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004060#endif
4061 break;
4062 case ISN_PUSHJOB:
4063#ifdef FEAT_JOB_CHANNEL
4064 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004065 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004066#endif
4067 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004068 case ISN_PUSHOBJ:
4069 tv->v_type = VAR_OBJECT;
4070 tv->vval.v_object = NULL;
4071 break;
4072 case ISN_PUSHCLASS:
4073 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004074 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004075 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 default:
4077 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004078 tv->vval.v_string = iptr->isn_arg.string == NULL
4079 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 }
4081 break;
4082
Bram Moolenaar06b77222022-01-25 15:51:56 +00004083 case ISN_AUTOLOAD:
4084 {
4085 char_u *name = iptr->isn_arg.string;
4086
4087 (void)script_autoload(name, FALSE);
4088 if (find_func(name, TRUE))
4089 {
4090 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4091 goto theend;
4092 tv = STACK_TV_BOT(0);
4093 tv->v_lock = 0;
4094 ++ectx->ec_stack.ga_len;
4095 tv->v_type = VAR_FUNC;
4096 tv->vval.v_string = vim_strsave(name);
4097 }
4098 else
4099 {
4100 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4101
4102 if (res == NOTDONE)
4103 goto theend;
4104 if (res == FAIL)
4105 goto on_error;
4106 }
4107 }
4108 break;
4109
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004110 case ISN_UNLET:
4111 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4112 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004113 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004114 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004115 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004116 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004117 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004118
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004119 case ISN_LOCKUNLOCK:
4120 {
4121 typval_T *lval_root_save = lval_root;
4122 int res;
4123
4124 // Stack has the local variable, argument the whole :lock
4125 // or :unlock command, like ISN_EXEC.
4126 --ectx->ec_stack.ga_len;
4127 lval_root = STACK_TV_BOT(0);
4128 res = exec_command(iptr);
4129 clear_tv(lval_root);
4130 lval_root = lval_root_save;
4131 if (res == FAIL)
4132 goto on_error;
4133 }
4134 break;
4135
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004136 case ISN_LOCKCONST:
4137 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4138 break;
4139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 // create a list from items on the stack; uses a single allocation
4141 // for the list header and the items
4142 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004143 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004144 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 break;
4146
4147 // create a dict from items on the stack
4148 case ISN_NEWDICT:
4149 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004150 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004152 SOURCING_LNUM = iptr->isn_lnum;
4153 res = exe_newdict(iptr->isn_arg.number, ectx);
4154 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004155 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004156 if (res == MAYBE)
4157 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 }
4159 break;
4160
LemonBoy372bcce2022-04-25 12:43:20 +01004161 case ISN_CONCAT:
4162 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4163 goto theend;
4164 break;
4165
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004166 // create a partial with NULL value
4167 case ISN_NEWPARTIAL:
4168 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4169 goto theend;
4170 ++ectx->ec_stack.ga_len;
4171 tv = STACK_TV_BOT(-1);
4172 tv->v_type = VAR_PARTIAL;
4173 tv->v_lock = 0;
4174 tv->vval.v_partial = NULL;
4175 break;
4176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 // call a :def function
4178 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004179 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004180 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4181 NULL,
4182 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004183 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004184 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 break;
4186
Bram Moolenaard0200c82023-01-28 15:19:40 +00004187 // call a method on an interface
4188 case ISN_METHODCALL:
4189 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004190 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4191
Bram Moolenaard0200c82023-01-28 15:19:40 +00004192 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004193 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004194 if (tv->v_type != VAR_OBJECT)
4195 {
4196 object_required_error(tv);
4197 goto on_error;
4198 }
4199 object_T *obj = tv->vval.v_object;
4200 class_T *cl = obj->obj_class;
4201
4202 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004203 int idx = object_index_from_itf_index(mfunc->cmf_itf,
4204 TRUE, mfunc->cmf_idx, cl);
4205
4206 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4207 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4208 goto on_error;
4209 }
4210 break;
4211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 // call a builtin function
4213 case ISN_BCALL:
4214 SOURCING_LNUM = iptr->isn_lnum;
4215 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4216 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004217 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004218 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 break;
4220
4221 // call a funcref or partial
4222 case ISN_PCALL:
4223 {
4224 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4225 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004226 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227
4228 SOURCING_LNUM = iptr->isn_lnum;
4229 if (pfunc->cpf_top)
4230 {
4231 // funcref is above the arguments
4232 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4233 }
4234 else
4235 {
4236 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004237 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004238 partial_tv = *STACK_TV_BOT(0);
4239 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004241 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004242 if (tv == &partial_tv)
4243 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004245 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 }
4247 break;
4248
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004249 case ISN_PCALL_END:
4250 // PCALL finished, arguments have been consumed and replaced by
4251 // the return value. Now clear the funcref from the stack,
4252 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004253 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004254 clear_tv(STACK_TV_BOT(-1));
4255 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4256 break;
4257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 // call a user defined function or funcref/partial
4259 case ISN_UCALL:
4260 {
4261 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4262
4263 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004264 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004265 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004266 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 }
4268 break;
4269
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004270 // :defer func(arg)
4271 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004272 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004273 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004274 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004275 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4276 goto on_error;
4277 break;
4278
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004279 // Return from a :def function call without a value.
4280 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004281 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004282 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004283 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004284 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004285 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004286 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004287 if (iptr->isn_type == ISN_RETURN_VOID)
4288 {
4289 tv->v_type = VAR_VOID;
4290 tv->vval.v_number = 0;
4291 tv->v_lock = 0;
4292 }
4293 else
4294 {
4295 *tv = *STACK_TV_VAR(0);
4296 ++tv->vval.v_object->obj_refcount;
4297 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004298 // FALLTHROUGH
4299
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004300 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 case ISN_RETURN:
4302 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004303 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004304 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004305
4306 if (trystack->ga_len > 0)
4307 trycmd = ((trycmd_T *)trystack->ga_data)
4308 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004309 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004310 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004312 // jump to ":finally" or ":endtry"
4313 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004314 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004315 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004316 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 trycmd->tcd_return = TRUE;
4318 }
4319 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004320 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 }
4322 break;
4323
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004324 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 case ISN_FUNCREF:
4326 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004327 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4328 ufunc_T *ufunc;
4329 funcref_T *funcref = &iptr->isn_arg.funcref;
4330 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004333 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004334 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004335 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004336 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004337 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004338 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004339 if (extra != NULL && extra->fre_class != NULL)
4340 {
4341 tv = STACK_TV_BOT(-1);
4342 if (tv->v_type != VAR_OBJECT)
4343 {
4344 object_required_error(tv);
4345 vim_free(pt);
4346 goto on_error;
4347 }
4348 object_T *obj = tv->vval.v_object;
4349 class_T *cl = obj->obj_class;
4350
4351 // convert the interface index to the object index
4352 int idx = object_index_from_itf_index(extra->fre_class,
4353 TRUE, extra->fre_method_idx, cl);
4354 ufunc = cl->class_obj_methods[idx];
4355 }
4356 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004357 {
4358 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4359 + funcref->fr_dfunc_idx;
4360
4361 ufunc = pt_dfunc->df_ufunc;
4362 }
4363 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004364 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004365 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004366 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004367 if (ufunc == NULL)
4368 {
4369 SOURCING_LNUM = iptr->isn_lnum;
4370 iemsg("ufunc unexpectedly NULL for FUNCREF");
4371 goto theend;
4372 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004373 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004374 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004375 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004376 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004378 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 tv->vval.v_partial = pt;
4380 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004381 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 }
4383 break;
4384
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004385 // Create a global function from a lambda.
4386 case ISN_NEWFUNC:
4387 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004388 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004389
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004390 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004391 arg->nfa_global, &arg->nfa_loopvar_info,
4392 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004393 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004394 }
4395 break;
4396
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004397 // List functions
4398 case ISN_DEF:
4399 if (iptr->isn_arg.string == NULL)
4400 list_functions(NULL);
4401 else
4402 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004403 exarg_T ea;
4404 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004405
4406 CLEAR_FIELD(ea);
4407 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004408 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00004409 define_function(&ea, NULL, &lines_to_free, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004410 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004411 }
4412 break;
4413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 // jump if a condition is met
4415 case ISN_JUMP:
4416 {
4417 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004418 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 int jump = TRUE;
4420
4421 if (when != JUMP_ALWAYS)
4422 {
4423 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004424 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004425 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004426 || when == JUMP_IF_COND_TRUE)
4427 {
4428 SOURCING_LNUM = iptr->isn_lnum;
4429 jump = tv_get_bool_chk(tv, &error);
4430 if (error)
4431 goto on_error;
4432 }
4433 else
4434 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004435 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004437 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 {
4439 // drop the value from the stack
4440 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004441 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 }
4443 }
4444 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004445 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 }
4447 break;
4448
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004449 // "while": jump to end if a condition is false
4450 case ISN_WHILE:
4451 {
4452 int error = FALSE;
4453 int jump = TRUE;
4454
4455 tv = STACK_TV_BOT(-1);
4456 SOURCING_LNUM = iptr->isn_lnum;
4457 jump = !tv_get_bool_chk(tv, &error);
4458 if (error)
4459 goto on_error;
4460 // drop the value from the stack
4461 clear_tv(tv);
4462 --ectx->ec_stack.ga_len;
4463 if (jump)
4464 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4465
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004466 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004467 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004468 tv = STACK_TV_VAR(
4469 iptr->isn_arg.whileloop.while_funcref_idx);
4470 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4471 }
4472 break;
4473
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004474 // Jump if an argument with a default value was already set and not
4475 // v:none.
4476 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004477 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004478 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004479 int arg_set = tv->v_type != VAR_UNKNOWN
4480 && !(tv->v_type == VAR_SPECIAL
4481 && tv->vval.v_number == VVAL_NONE);
4482 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004483 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004484 break;
4485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 // top of a for loop
4487 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004488 if (execute_for(iptr, ectx) == FAIL)
4489 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 break;
4491
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004492 // end of a for or while loop
4493 case ISN_ENDLOOP:
4494 if (execute_endloop(iptr, ectx) == FAIL)
4495 goto theend;
4496 break;
4497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 // start of ":try" block
4499 case ISN_TRY:
4500 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004501 trycmd_T *trycmd = NULL;
4502
Bram Moolenaar35578162021-08-02 19:10:38 +02004503 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004504 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004505 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4506 + ectx->ec_trystack.ga_len;
4507 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004509 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004510 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4511 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004512 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004513 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004514 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004515 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004516 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004517 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 }
4519 break;
4520
4521 case ISN_PUSHEXC:
4522 if (current_exception == NULL)
4523 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004524 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004526 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004528 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004529 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004531 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004533 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 tv->vval.v_string = vim_strsave(
4535 (char_u *)current_exception->value);
4536 break;
4537
4538 case ISN_CATCH:
4539 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004540 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004541 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542
Bram Moolenaar4c137212021-04-19 16:48:48 +02004543 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004544 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004546 trycmd->tcd_caught = TRUE;
4547 trycmd->tcd_did_throw = FALSE;
4548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004550 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 catch_exception(current_exception);
4552 }
4553 break;
4554
Bram Moolenaarc150c092021-02-13 15:02:46 +01004555 case ISN_TRYCONT:
4556 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004557 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004558 trycont_T *trycont = &iptr->isn_arg.trycont;
4559 int i;
4560 trycmd_T *trycmd;
4561 int iidx = trycont->tct_where;
4562
4563 if (trystack->ga_len < trycont->tct_levels)
4564 {
4565 siemsg("TRYCONT: expected %d levels, found %d",
4566 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004567 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004568 }
4569 // Make :endtry jump to any outer try block and the last
4570 // :endtry inside the loop to the loop start.
4571 for (i = trycont->tct_levels; i > 0; --i)
4572 {
4573 trycmd = ((trycmd_T *)trystack->ga_data)
4574 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004575 // Add one to tcd_cont to be able to jump to
4576 // instruction with index zero.
4577 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004578 iidx = trycmd->tcd_finally_idx == 0
4579 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004580 }
4581 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004582 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004583 }
4584 break;
4585
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004586 case ISN_FINALLY:
4587 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004588 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004589 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4590 + trystack->ga_len - 1;
4591
4592 // Reset the index to avoid a return statement jumps here
4593 // again.
4594 trycmd->tcd_finally_idx = 0;
4595 break;
4596 }
4597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 // end of ":try" block
4599 case ISN_ENDTRY:
4600 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004601 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004602 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603
Bram Moolenaar06651632022-04-27 17:54:25 +01004604 --trystack->ga_len;
4605 --trylevel;
4606 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4607 if (trycmd->tcd_did_throw)
4608 did_throw = TRUE;
4609 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004611 // discard the exception
4612 if (caught_stack == current_exception)
4613 caught_stack = caught_stack->caught;
4614 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004616
4617 if (trycmd->tcd_return)
4618 goto func_return;
4619
4620 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4621 {
4622 --ectx->ec_stack.ga_len;
4623 clear_tv(STACK_TV_BOT(0));
4624 }
4625 if (trycmd->tcd_cont != 0)
4626 // handling :continue: jump to outer try block or
4627 // start of the loop
4628 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 }
4630 break;
4631
4632 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004633 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004634 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004635
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004636 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4637 {
4638 // throwing an exception while using "silent!" causes
4639 // the function to abort but not display an error.
4640 tv = STACK_TV_BOT(-1);
4641 clear_tv(tv);
4642 tv->v_type = VAR_NUMBER;
4643 tv->vval.v_number = 0;
4644 goto done;
4645 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004646 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004647 tv = STACK_TV_BOT(0);
4648 if (tv->vval.v_string == NULL
4649 || *skipwhite(tv->vval.v_string) == NUL)
4650 {
4651 vim_free(tv->vval.v_string);
4652 SOURCING_LNUM = iptr->isn_lnum;
4653 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004654 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004655 }
4656
4657 // Inside a "catch" we need to first discard the caught
4658 // exception.
4659 if (trystack->ga_len > 0)
4660 {
4661 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4662 + trystack->ga_len - 1;
4663 if (trycmd->tcd_caught && current_exception != NULL)
4664 {
4665 // discard the exception
4666 if (caught_stack == current_exception)
4667 caught_stack = caught_stack->caught;
4668 discard_current_exception();
4669 trycmd->tcd_caught = FALSE;
4670 }
4671 }
4672
Bram Moolenaar90a57162022-02-12 14:23:17 +00004673 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004674 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4675 == FAIL)
4676 {
4677 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004678 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004679 }
4680 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 break;
4683
4684 // compare with special values
4685 case ISN_COMPAREBOOL:
4686 case ISN_COMPARESPECIAL:
4687 {
4688 typval_T *tv1 = STACK_TV_BOT(-2);
4689 typval_T *tv2 = STACK_TV_BOT(-1);
4690 varnumber_T arg1 = tv1->vval.v_number;
4691 varnumber_T arg2 = tv2->vval.v_number;
4692 int res;
4693
Bram Moolenaar06651632022-04-27 17:54:25 +01004694 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4695 res = arg1 == arg2;
4696 else
4697 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698
Bram Moolenaar4c137212021-04-19 16:48:48 +02004699 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 tv1->v_type = VAR_BOOL;
4701 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4702 }
4703 break;
4704
Bram Moolenaar7a222242022-03-01 19:23:24 +00004705 case ISN_COMPARENULL:
4706 {
4707 typval_T *tv1 = STACK_TV_BOT(-2);
4708 typval_T *tv2 = STACK_TV_BOT(-1);
4709 int res;
4710
4711 res = typval_compare_null(tv1, tv2);
4712 if (res == MAYBE)
4713 goto on_error;
4714 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4715 res = !res;
4716 clear_tv(tv1);
4717 clear_tv(tv2);
4718 --ectx->ec_stack.ga_len;
4719 tv1->v_type = VAR_BOOL;
4720 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4721 }
4722 break;
4723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 // Operation with two number arguments
4725 case ISN_OPNR:
4726 case ISN_COMPARENR:
4727 {
4728 typval_T *tv1 = STACK_TV_BOT(-2);
4729 typval_T *tv2 = STACK_TV_BOT(-1);
4730 varnumber_T arg1 = tv1->vval.v_number;
4731 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004732 varnumber_T res = 0;
4733 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004735 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4736 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4737 {
4738 if (arg2 < 0)
4739 {
4740 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004741 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004742 goto on_error;
4743 }
4744 }
4745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 switch (iptr->isn_arg.op.op_type)
4747 {
4748 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004749 case EXPR_DIV: if (arg2 == 0)
4750 div_zero = TRUE;
4751 else
4752 res = arg1 / arg2;
4753 break;
4754 case EXPR_REM: if (arg2 == 0)
4755 div_zero = TRUE;
4756 else
4757 res = arg1 % arg2;
4758 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 case EXPR_SUB: res = arg1 - arg2; break;
4760 case EXPR_ADD: res = arg1 + arg2; break;
4761
4762 case EXPR_EQUAL: res = arg1 == arg2; break;
4763 case EXPR_NEQUAL: res = arg1 != arg2; break;
4764 case EXPR_GREATER: res = arg1 > arg2; break;
4765 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4766 case EXPR_SMALLER: res = arg1 < arg2; break;
4767 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004768 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4769 res = 0;
4770 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004771 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004772 break;
4773 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4774 res = 0;
4775 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004776 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004777 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004778 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 }
4780
Bram Moolenaar4c137212021-04-19 16:48:48 +02004781 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 if (iptr->isn_type == ISN_COMPARENR)
4783 {
4784 tv1->v_type = VAR_BOOL;
4785 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4786 }
4787 else
4788 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004789 if (div_zero)
4790 {
4791 SOURCING_LNUM = iptr->isn_lnum;
4792 emsg(_(e_divide_by_zero));
4793 goto on_error;
4794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 }
4796 break;
4797
4798 // Computation with two float arguments
4799 case ISN_OPFLOAT:
4800 case ISN_COMPAREFLOAT:
4801 {
4802 typval_T *tv1 = STACK_TV_BOT(-2);
4803 typval_T *tv2 = STACK_TV_BOT(-1);
4804 float_T arg1 = tv1->vval.v_float;
4805 float_T arg2 = tv2->vval.v_float;
4806 float_T res = 0;
4807 int cmp = FALSE;
4808
4809 switch (iptr->isn_arg.op.op_type)
4810 {
4811 case EXPR_MULT: res = arg1 * arg2; break;
4812 case EXPR_DIV: res = arg1 / arg2; break;
4813 case EXPR_SUB: res = arg1 - arg2; break;
4814 case EXPR_ADD: res = arg1 + arg2; break;
4815
4816 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4817 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4818 case EXPR_GREATER: cmp = arg1 > arg2; break;
4819 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4820 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4821 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4822 default: cmp = 0; break;
4823 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004824 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 if (iptr->isn_type == ISN_COMPAREFLOAT)
4826 {
4827 tv1->v_type = VAR_BOOL;
4828 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4829 }
4830 else
4831 tv1->vval.v_float = res;
4832 }
4833 break;
4834
4835 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004836 case ISN_COMPAREDICT:
4837 case ISN_COMPAREFUNC:
4838 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004840 case ISN_COMPARECLASS:
4841 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 {
4843 typval_T *tv1 = STACK_TV_BOT(-2);
4844 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004845 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4846 int ic = iptr->isn_arg.op.op_ic;
4847 int res = FALSE;
4848 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849
Bram Moolenaar265f8112021-12-19 12:33:05 +00004850 SOURCING_LNUM = iptr->isn_lnum;
4851 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004853 status = typval_compare_list(tv1, tv2,
4854 exprtype, ic, &res);
4855 }
4856 else if (iptr->isn_type == ISN_COMPAREDICT)
4857 {
4858 status = typval_compare_dict(tv1, tv2,
4859 exprtype, ic, &res);
4860 }
4861 else if (iptr->isn_type == ISN_COMPAREFUNC)
4862 {
4863 status = typval_compare_func(tv1, tv2,
4864 exprtype, ic, &res);
4865 }
4866 else if (iptr->isn_type == ISN_COMPARESTRING)
4867 {
4868 status = typval_compare_string(tv1, tv2,
4869 exprtype, ic, &res);
4870 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004871 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004872 {
4873 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004875 else if (iptr->isn_type == ISN_COMPARECLASS)
4876 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004877 status = typval_compare_class(tv1, tv2,
4878 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004879 }
4880 else // ISN_COMPAREOBJECT
4881 {
4882 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004883 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004884 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004885 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 clear_tv(tv1);
4887 clear_tv(tv2);
4888 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004889 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4890 if (status == FAIL)
4891 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 }
4893 break;
4894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 case ISN_COMPAREANY:
4896 {
4897 typval_T *tv1 = STACK_TV_BOT(-2);
4898 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004899 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004901 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902
Bram Moolenaareb26f432020-09-14 16:50:05 +02004903 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004904 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004906 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004907 if (status == FAIL)
4908 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 }
4910 break;
4911
4912 case ISN_ADDLIST:
4913 case ISN_ADDBLOB:
4914 {
4915 typval_T *tv1 = STACK_TV_BOT(-2);
4916 typval_T *tv2 = STACK_TV_BOT(-1);
4917
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004918 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004920 {
4921 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4922 && tv1->vval.v_list != NULL)
4923 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4924 NULL);
4925 else
4926 eval_addlist(tv1, tv2);
4927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 else
4929 eval_addblob(tv1, tv2);
4930 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004931 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 }
4933 break;
4934
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004935 case ISN_LISTAPPEND:
4936 {
4937 typval_T *tv1 = STACK_TV_BOT(-2);
4938 typval_T *tv2 = STACK_TV_BOT(-1);
4939 list_T *l = tv1->vval.v_list;
4940
4941 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004942 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004943 if (l == NULL)
4944 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004945 emsg(_(e_cannot_add_to_null_list));
4946 goto on_error;
4947 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004948 if (value_check_lock(l->lv_lock, NULL, FALSE))
4949 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004950 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004951 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004952 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004953 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004954 }
4955 break;
4956
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004957 case ISN_BLOBAPPEND:
4958 {
4959 typval_T *tv1 = STACK_TV_BOT(-2);
4960 typval_T *tv2 = STACK_TV_BOT(-1);
4961 blob_T *b = tv1->vval.v_blob;
4962 int error = FALSE;
4963 varnumber_T n;
4964
4965 // add a number to a blob
4966 if (b == NULL)
4967 {
4968 SOURCING_LNUM = iptr->isn_lnum;
4969 emsg(_(e_cannot_add_to_null_blob));
4970 goto on_error;
4971 }
4972 n = tv_get_number_chk(tv2, &error);
4973 if (error)
4974 goto on_error;
4975 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004976 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004977 }
4978 break;
4979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 // Computation with two arguments of unknown type
4981 case ISN_OPANY:
4982 {
4983 typval_T *tv1 = STACK_TV_BOT(-2);
4984 typval_T *tv2 = STACK_TV_BOT(-1);
4985 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 int error = FALSE;
4988
4989 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4990 {
4991 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4992 {
4993 eval_addlist(tv1, tv2);
4994 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004995 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 break;
4997 }
4998 else if (tv1->v_type == VAR_BLOB
4999 && tv2->v_type == VAR_BLOB)
5000 {
5001 eval_addblob(tv1, tv2);
5002 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005003 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 break;
5005 }
5006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 if (tv1->v_type == VAR_FLOAT)
5008 {
5009 f1 = tv1->vval.v_float;
5010 n1 = 0;
5011 }
5012 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005014 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 n1 = tv_get_number_chk(tv1, &error);
5016 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005017 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 if (tv2->v_type == VAR_FLOAT)
5019 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021 if (tv2->v_type == VAR_FLOAT)
5022 {
5023 f2 = tv2->vval.v_float;
5024 n2 = 0;
5025 }
5026 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 {
5028 n2 = tv_get_number_chk(tv2, &error);
5029 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005030 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 if (tv1->v_type == VAR_FLOAT)
5032 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034 // if there is a float on either side the result is a float
5035 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5036 {
5037 switch (iptr->isn_arg.op.op_type)
5038 {
5039 case EXPR_MULT: f1 = f1 * f2; break;
5040 case EXPR_DIV: f1 = f1 / f2; break;
5041 case EXPR_SUB: f1 = f1 - f2; break;
5042 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005043 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005044 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005045 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 }
5047 clear_tv(tv1);
5048 clear_tv(tv2);
5049 tv1->v_type = VAR_FLOAT;
5050 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005051 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 }
5053 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005055 int failed = FALSE;
5056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 switch (iptr->isn_arg.op.op_type)
5058 {
5059 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005060 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5061 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005062 goto on_error;
5063 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 case EXPR_SUB: n1 = n1 - n2; break;
5065 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005066 default: n1 = num_modulus(n1, n2, &failed);
5067 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005068 goto on_error;
5069 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 }
5071 clear_tv(tv1);
5072 clear_tv(tv2);
5073 tv1->v_type = VAR_NUMBER;
5074 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005075 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 }
5077 }
5078 break;
5079
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005080 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005081 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005082 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005083 int is_slice = iptr->isn_type == ISN_STRSLICE;
5084 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005085 char_u *res;
5086
5087 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005088 // string slice: string is at stack-3, first index at
5089 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005090 if (is_slice)
5091 {
5092 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005093 n1 = tv->vval.v_number;
5094 }
5095
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005096 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005097 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005098
Bram Moolenaar4c137212021-04-19 16:48:48 +02005099 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005100 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005101 if (is_slice)
5102 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005103 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005104 else
5105 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005106 // single character (including composing characters).
5107 // If the index is too big or negative the result is
5108 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005109 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005110 vim_free(tv->vval.v_string);
5111 tv->vval.v_string = res;
5112 }
5113 break;
5114
5115 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005116 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005117 case ISN_BLOBINDEX:
5118 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005120 int is_slice = iptr->isn_type == ISN_LISTSLICE
5121 || iptr->isn_type == ISN_BLOBSLICE;
5122 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5123 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005124 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005125 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126
5127 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005128 // list slice: list is at stack-3, indexes at stack-2 and
5129 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005130 // Same for blob.
5131 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132
5133 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005134 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005136
5137 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 {
Bram Moolenaared591872020-08-15 22:14:53 +02005139 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005140 n1 = tv->vval.v_number;
5141 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142 }
Bram Moolenaared591872020-08-15 22:14:53 +02005143
Bram Moolenaar4c137212021-04-19 16:48:48 +02005144 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005145 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005146 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005147 if (is_blob)
5148 {
5149 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5150 n1, n2, FALSE, tv) == FAIL)
5151 goto on_error;
5152 }
5153 else
5154 {
5155 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5156 n1, n2, FALSE, tv, TRUE) == FAIL)
5157 goto on_error;
5158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 }
5160 break;
5161
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005162 case ISN_ANYINDEX:
5163 case ISN_ANYSLICE:
5164 {
5165 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5166 typval_T *var1, *var2;
5167 int res;
5168
5169 // index: composite is at stack-2, index at stack-1
5170 // slice: composite is at stack-3, indexes at stack-2 and
5171 // stack-1
5172 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005173 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005174 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5175 goto on_error;
5176 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5177 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005178 res = eval_index_inner(tv, is_slice, var1, var2,
5179 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005180 clear_tv(var1);
5181 if (is_slice)
5182 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005183 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005184 if (res == FAIL)
5185 goto on_error;
5186 }
5187 break;
5188
Bram Moolenaar9af78762020-06-16 11:34:42 +02005189 case ISN_SLICE:
5190 {
5191 list_T *list;
5192 int count = iptr->isn_arg.number;
5193
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005194 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005195 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005196 list = tv->vval.v_list;
5197
5198 // no error for short list, expect it to be checked earlier
5199 if (list != NULL && list->lv_len >= count)
5200 {
5201 list_T *newlist = list_slice(list,
5202 count, list->lv_len - 1);
5203
5204 if (newlist != NULL)
5205 {
5206 list_unref(list);
5207 tv->vval.v_list = newlist;
5208 ++newlist->lv_refcount;
5209 }
5210 }
5211 }
5212 break;
5213
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005214 case ISN_GETITEM:
5215 {
5216 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005217 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005218
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005219 // Get list item: list is at stack-1, push item.
5220 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005221 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5222 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005223
Bram Moolenaar35578162021-08-02 19:10:38 +02005224 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005225 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005226 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005227 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005228
5229 // Useful when used in unpack assignment. Reset at
5230 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005231 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005232 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233 }
5234 break;
5235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 case ISN_MEMBER:
5237 {
5238 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005239 char_u *key;
5240 dictitem_T *di;
5241
5242 // dict member: dict is at stack-2, key at stack-1
5243 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005244 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005245 dict = tv->vval.v_dict;
5246
5247 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005248 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005249 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005250 if (key == NULL)
5251 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005252
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005253 if ((di = dict_find(dict, key, -1)) == NULL)
5254 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005255 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005256 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005257
5258 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005259 // stack contents makes sense and the dict stack is
5260 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005261 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005262 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005263 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005264 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005265 tv->v_type = VAR_NUMBER;
5266 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005267 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005268 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005269 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005270 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005271 // Put the dict used on the dict stack, it might be used by
5272 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005273 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005274 if (dict_stack_save(tv) == FAIL)
5275 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005276 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005277 }
5278 break;
5279
5280 // dict member with string key
5281 case ISN_STRINGMEMBER:
5282 {
5283 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 dictitem_T *di;
5285
5286 tv = STACK_TV_BOT(-1);
5287 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5288 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005289 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005290 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005291 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292 }
5293 dict = tv->vval.v_dict;
5294
5295 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5296 == NULL)
5297 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005298 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005299 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005300 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005301 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005303 // Put the dict used on the dict stack, it might be used by
5304 // a dict function later.
5305 if (dict_stack_save(tv) == FAIL)
5306 goto on_fatal_error;
5307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005309 }
5310 break;
5311
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005312 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005313 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005314 {
5315 tv = STACK_TV_BOT(-1);
5316 if (tv->v_type != VAR_OBJECT)
5317 {
5318 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005319 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005320 goto on_error;
5321 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005322
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005323 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005324 if (obj == NULL)
5325 {
5326 SOURCING_LNUM = iptr->isn_lnum;
5327 emsg(_(e_using_null_object));
5328 goto on_error;
5329 }
5330
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005331 int idx;
5332 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
5333 idx = iptr->isn_arg.number;
5334 else
5335 {
5336 idx = iptr->isn_arg.classmember.cm_idx;
5337 // convert the interface index to the object index
5338 idx = object_index_from_itf_index(
Bram Moolenaard0200c82023-01-28 15:19:40 +00005339 iptr->isn_arg.classmember.cm_class,
5340 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005341 }
5342
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005343 // the members are located right after the object struct
5344 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005345 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005346
5347 // Unreference the object after getting the member, it may
5348 // be freed.
5349 object_unref(obj);
5350 }
5351 break;
5352
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005353 case ISN_STORE_THIS:
5354 {
5355 int idx = iptr->isn_arg.number;
5356 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5357 // the members are located right after the object struct
5358 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5359 clear_tv(mtv);
5360 *mtv = *STACK_TV_BOT(-1);
5361 --ectx->ec_stack.ga_len;
5362 }
5363 break;
5364
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005365 case ISN_CLEARDICT:
5366 dict_stack_drop();
5367 break;
5368
5369 case ISN_USEDICT:
5370 {
5371 typval_T *dict_tv = dict_stack_get_tv();
5372
5373 // Turn "dict.Func" into a partial for "Func" bound to
5374 // "dict". Don't do this when "Func" is already a partial
5375 // that was bound explicitly (pt_auto is FALSE).
5376 tv = STACK_TV_BOT(-1);
5377 if (dict_tv != NULL
5378 && dict_tv->v_type == VAR_DICT
5379 && dict_tv->vval.v_dict != NULL
5380 && (tv->v_type == VAR_FUNC
5381 || (tv->v_type == VAR_PARTIAL
5382 && (tv->vval.v_partial->pt_auto
5383 || tv->vval.v_partial->pt_dict == NULL))))
5384 dict_tv->vval.v_dict =
5385 make_partial(dict_tv->vval.v_dict, tv);
5386 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387 }
5388 break;
5389
5390 case ISN_NEGATENR:
5391 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005392 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005393 if (tv->v_type == VAR_FLOAT)
5394 tv->vval.v_float = -tv->vval.v_float;
5395 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005396 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 break;
5398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005399 case ISN_CHECKTYPE:
5400 {
5401 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005402 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005403 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005405 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005406 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005407 if (!ectx->ec_where.wt_variable)
5408 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005409 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005410 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005411 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005412 if (r == FAIL)
5413 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005414 if (!ectx->ec_where.wt_variable)
5415 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005416
5417 // number 0 is FALSE, number 1 is TRUE
5418 if (tv->v_type == VAR_NUMBER
5419 && ct->ct_type->tt_type == VAR_BOOL
5420 && (tv->vval.v_number == 0
5421 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005423 tv->v_type = VAR_BOOL;
5424 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005425 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 }
5427 }
5428 break;
5429
Bram Moolenaar9af78762020-06-16 11:34:42 +02005430 case ISN_CHECKLEN:
5431 {
5432 int min_len = iptr->isn_arg.checklen.cl_min_len;
5433 list_T *list = NULL;
5434
5435 tv = STACK_TV_BOT(-1);
5436 if (tv->v_type == VAR_LIST)
5437 list = tv->vval.v_list;
5438 if (list == NULL || list->lv_len < min_len
5439 || (list->lv_len > min_len
5440 && !iptr->isn_arg.checklen.cl_more_OK))
5441 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005442 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005443 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005444 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005445 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005446 }
5447 }
5448 break;
5449
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005450 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005451 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005452 break;
5453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005455 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456 {
5457 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005458 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005460 if (iptr->isn_type == ISN_2BOOL)
5461 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005462 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005463 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005464 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005465 n = !n;
5466 }
5467 else
5468 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005469 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005470 SOURCING_LNUM = iptr->isn_lnum;
5471 n = tv_get_bool_chk(tv, &error);
5472 if (error)
5473 goto on_error;
5474 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475 clear_tv(tv);
5476 tv->v_type = VAR_BOOL;
5477 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5478 }
5479 break;
5480
5481 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005482 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005483 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005484 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5485 iptr->isn_type == ISN_2STRING_ANY,
5486 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005487 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005488 break;
5489
Bram Moolenaar08597872020-12-10 19:43:40 +01005490 case ISN_RANGE:
5491 {
5492 exarg_T ea;
5493 char *errormsg;
5494
Bram Moolenaarece0b872021-01-08 20:40:45 +01005495 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005496 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005497 ea.addr_type = ADDR_LINES;
5498 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005499 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005500 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005501 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005502
Bram Moolenaar35578162021-08-02 19:10:38 +02005503 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005504 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005505 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005506 tv = STACK_TV_BOT(-1);
5507 tv->v_type = VAR_NUMBER;
5508 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005509 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005510 }
5511 break;
5512
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005513 case ISN_PUT:
5514 {
5515 int regname = iptr->isn_arg.put.put_regname;
5516 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5517 char_u *expr = NULL;
5518 int dir = FORWARD;
5519
Bram Moolenaar08597872020-12-10 19:43:40 +01005520 if (lnum < -2)
5521 {
5522 // line number was put on the stack by ISN_RANGE
5523 tv = STACK_TV_BOT(-1);
5524 curwin->w_cursor.lnum = tv->vval.v_number;
5525 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5526 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005527 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005528 }
5529 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005530 // :put! above cursor
5531 dir = BACKWARD;
5532 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005533 {
5534 curwin->w_cursor.lnum = lnum;
5535 if (lnum == 0)
5536 // check_cursor() below will move to line 1
5537 dir = BACKWARD;
5538 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005539
5540 if (regname == '=')
5541 {
5542 tv = STACK_TV_BOT(-1);
5543 if (tv->v_type == VAR_STRING)
5544 expr = tv->vval.v_string;
5545 else
5546 {
5547 expr = typval2string(tv, TRUE); // allocates value
5548 clear_tv(tv);
5549 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005550 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005551 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005552 check_cursor();
5553 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5554 vim_free(expr);
5555 }
5556 break;
5557
Bram Moolenaar02194d22020-10-24 23:08:38 +02005558 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005559 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5560 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5561 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5562 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005563 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5564 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005565 break;
5566
Bram Moolenaar02194d22020-10-24 23:08:38 +02005567 case ISN_CMDMOD_REV:
5568 // filter regprog is owned by the instruction, don't free it
5569 cmdmod.cmod_filter_regmatch.regprog = NULL;
5570 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005571 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5572 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005573 break;
5574
Bram Moolenaar792f7862020-11-23 08:31:18 +01005575 case ISN_UNPACK:
5576 {
5577 int count = iptr->isn_arg.unpack.unp_count;
5578 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5579 list_T *l;
5580 listitem_T *li;
5581 int i;
5582
5583 // Check there is a valid list to unpack.
5584 tv = STACK_TV_BOT(-1);
5585 if (tv->v_type != VAR_LIST)
5586 {
5587 SOURCING_LNUM = iptr->isn_lnum;
5588 emsg(_(e_for_argument_must_be_sequence_of_lists));
5589 goto on_error;
5590 }
5591 l = tv->vval.v_list;
5592 if (l == NULL
5593 || l->lv_len < (semicolon ? count - 1 : count))
5594 {
5595 SOURCING_LNUM = iptr->isn_lnum;
5596 emsg(_(e_list_value_does_not_have_enough_items));
5597 goto on_error;
5598 }
5599 else if (!semicolon && l->lv_len > count)
5600 {
5601 SOURCING_LNUM = iptr->isn_lnum;
5602 emsg(_(e_list_value_has_more_items_than_targets));
5603 goto on_error;
5604 }
5605
5606 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005607 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005608 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005609 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005610
5611 // Variable after semicolon gets a list with the remaining
5612 // items.
5613 if (semicolon)
5614 {
5615 list_T *rem_list =
5616 list_alloc_with_items(l->lv_len - count + 1);
5617
5618 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005619 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005620 tv = STACK_TV_BOT(-count);
5621 tv->vval.v_list = rem_list;
5622 ++rem_list->lv_refcount;
5623 tv->v_lock = 0;
5624 li = l->lv_first;
5625 for (i = 0; i < count - 1; ++i)
5626 li = li->li_next;
5627 for (i = 0; li != NULL; ++i)
5628 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005629 typval_T tvcopy;
5630
5631 copy_tv(&li->li_tv, &tvcopy);
5632 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005633 li = li->li_next;
5634 }
5635 --count;
5636 }
5637
5638 // Produce the values in reverse order, first item last.
5639 li = l->lv_first;
5640 for (i = 0; i < count; ++i)
5641 {
5642 tv = STACK_TV_BOT(-i - 1);
5643 copy_tv(&li->li_tv, tv);
5644 li = li->li_next;
5645 }
5646
5647 list_unref(l);
5648 }
5649 break;
5650
Bram Moolenaarb2049902021-01-24 12:53:53 +01005651 case ISN_PROF_START:
5652 case ISN_PROF_END:
5653 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005654#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005655 funccall_T cookie;
5656 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005657 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005658 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005659
Bram Moolenaarca16c602022-09-06 18:57:08 +01005660 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005661 if (iptr->isn_type == ISN_PROF_START)
5662 {
5663 func_line_start(&cookie, iptr->isn_lnum);
5664 // if we get here the instruction is executed
5665 func_line_exec(&cookie);
5666 }
5667 else
5668 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005669#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005670 }
5671 break;
5672
Bram Moolenaare99d4222021-06-13 14:01:26 +02005673 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005674 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005675 break;
5676
Bram Moolenaar389df252020-07-09 21:20:47 +02005677 case ISN_SHUFFLE:
5678 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005679 typval_T tmp_tv;
5680 int item = iptr->isn_arg.shuffle.shfl_item;
5681 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005682
5683 tmp_tv = *STACK_TV_BOT(-item);
5684 for ( ; up > 0 && item > 1; --up)
5685 {
5686 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5687 --item;
5688 }
5689 *STACK_TV_BOT(-item) = tmp_tv;
5690 }
5691 break;
5692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005694 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005696 ectx->ec_where.wt_index = 0;
5697 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698 break;
5699 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005700 continue;
5701
Bram Moolenaard032f342020-07-18 18:13:02 +02005702func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005703 // Restore previous function. If the frame pointer is where we started
5704 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005705 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005706 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005707
Bram Moolenaar4c137212021-04-19 16:48:48 +02005708 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005709 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005710 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005711 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005712
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005713on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005714 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005715 // If "emsg_silent" is set then ignore the error, unless it was set
5716 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005717 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005718 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005719 {
5720 // If a sequence of instructions causes an error while ":silent!"
5721 // was used, restore the stack length and jump ahead to restoring
5722 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005723 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005724 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005725 while (ectx->ec_stack.ga_len
5726 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005727 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005728 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005729 clear_tv(STACK_TV_BOT(0));
5730 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005731 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5732 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005733 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005734 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005735 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005736on_fatal_error:
5737 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005738 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005739 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005740 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741 }
5742
5743done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005744 ret = OK;
5745theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005746 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005747
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005748 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005749 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5750 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005751}
5752
5753/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005754 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005755 * "rettv".
5756 * Return OK or FAIL.
5757 */
5758 int
5759exe_typval_instr(typval_T *tv, typval_T *rettv)
5760{
5761 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5762 isn_T *save_instr = ectx->ec_instr;
5763 int save_iidx = ectx->ec_iidx;
5764 int res;
5765
LemonBoyf3b48952022-05-05 13:53:03 +01005766 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5767 // even when the compilation fails.
5768 rettv->v_type = VAR_UNKNOWN;
5769
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005770 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5771 res = exec_instructions(ectx);
5772 if (res == OK)
5773 {
5774 *rettv = *STACK_TV_BOT(-1);
5775 --ectx->ec_stack.ga_len;
5776 }
5777
5778 ectx->ec_instr = save_instr;
5779 ectx->ec_iidx = save_iidx;
5780
5781 return res;
5782}
5783
5784/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005785 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5786 * "substitute_instr".
5787 */
5788 char_u *
5789exe_substitute_instr(void)
5790{
5791 ectx_T *ectx = substitute_instr->subs_ectx;
5792 isn_T *save_instr = ectx->ec_instr;
5793 int save_iidx = ectx->ec_iidx;
5794 char_u *res;
5795
5796 ectx->ec_instr = substitute_instr->subs_instr;
5797 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005798 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005799 typval_T *tv = STACK_TV_BOT(-1);
5800
Bram Moolenaar27523602021-06-05 21:36:19 +02005801 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005802 --ectx->ec_stack.ga_len;
5803 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005804 }
5805 else
5806 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005807 substitute_instr->subs_status = FAIL;
5808 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810
Bram Moolenaar4c137212021-04-19 16:48:48 +02005811 ectx->ec_instr = save_instr;
5812 ectx->ec_iidx = save_iidx;
5813
5814 return res;
5815}
5816
5817/*
5818 * Call a "def" function from old Vim script.
5819 * Return OK or FAIL.
5820 */
5821 int
5822call_def_function(
5823 ufunc_T *ufunc,
5824 int argc_arg, // nr of arguments
5825 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005826 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005827 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005828 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005829 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005830 typval_T *rettv) // return value
5831{
5832 ectx_T ectx; // execution context
5833 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005834 int partial_argc = partial == NULL
5835 || (flags & DEF_USE_PT_ARGV) == 0
5836 ? 0 : partial->pt_argc;
5837 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005838 typval_T *tv;
5839 int idx;
5840 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005841 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005842 sctx_T save_current_sctx = current_sctx;
5843 int did_emsg_before = did_emsg_cumul + did_emsg;
5844 int save_suppress_errthrow = suppress_errthrow;
5845 msglist_T **saved_msg_list = NULL;
5846 msglist_T *private_msg_list = NULL;
5847 int save_emsg_silent_def = emsg_silent_def;
5848 int save_did_emsg_def = did_emsg_def;
5849 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005850 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005851
5852// Get pointer to item in the stack.
5853#undef STACK_TV
5854#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5855
5856// Get pointer to item at the bottom of the stack, -1 is the bottom.
5857#undef STACK_TV_BOT
5858#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5859
5860// Get pointer to a local variable on the stack. Negative for arguments.
5861#undef STACK_TV_VAR
5862#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5863
5864 if (ufunc->uf_def_status == UF_NOT_COMPILED
5865 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005866 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5867 && compile_def_function(ufunc, FALSE,
5868 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005869 {
5870 if (did_emsg_cumul + did_emsg == did_emsg_before)
5871 semsg(_(e_function_is_not_compiled_str),
5872 printable_func_name(ufunc));
5873 return FAIL;
5874 }
5875
5876 {
5877 // Check the function was really compiled.
5878 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5879 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005880 if (dfunc->df_ufunc == NULL)
5881 {
5882 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5883 return FAIL;
5884 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005885 if (INSTRUCTIONS(dfunc) == NULL)
5886 {
5887 iemsg("using call_def_function() on not compiled function");
5888 return FAIL;
5889 }
5890 }
5891
5892 // If depth of calling is getting too high, don't execute the function.
5893 orig_funcdepth = funcdepth_get();
5894 if (funcdepth_increment() == FAIL)
5895 return FAIL;
5896
5897 CLEAR_FIELD(ectx);
5898 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5899 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005900 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005901 {
5902 funcdepth_decrement();
5903 return FAIL;
5904 }
5905 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5906 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5907 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005908 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005909
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005910 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005911 if (idx > 0 && ufunc->uf_va_name == NULL)
5912 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005913 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005914 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005915 goto failed_early;
5916 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005917 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005918 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005919 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005920 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005921 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005922 goto failed_early;
5923 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005924
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005925 // Put values from the partial and arguments on the stack, but no more than
5926 // what the function expects. A lambda can be called with more arguments
5927 // than it uses.
5928 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005929 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5930 ++idx)
5931 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005932 int argv_idx = idx - partial_argc;
5933
5934 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005935 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005936 && tv->v_type == VAR_SPECIAL
5937 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005938 {
5939 // Use the default value.
5940 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5941 }
5942 else
5943 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00005944 int done = FALSE;
5945 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
5946 {
5947 type_T *expected = ufunc->uf_arg_types[idx];
5948 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
5949 {
5950 // When a float is expected and a number was given, convert
5951 // the value.
5952 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
5953 STACK_TV_BOT(0)->v_lock = 0;
5954 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
5955 done = TRUE;
5956 }
5957 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005958 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00005959 goto failed_early;
5960 }
5961 if (!done)
5962 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005963 }
5964 ++ectx.ec_stack.ga_len;
5965 }
5966
5967 // Turn varargs into a list. Empty list if no args.
5968 if (ufunc->uf_va_name != NULL)
5969 {
5970 int vararg_count = argc - ufunc->uf_args.ga_len;
5971
5972 if (vararg_count < 0)
5973 vararg_count = 0;
5974 else
5975 argc -= vararg_count;
5976 if (exe_newlist(vararg_count, &ectx) == FAIL)
5977 goto failed_early;
5978
5979 // Check the type of the list items.
5980 tv = STACK_TV_BOT(-1);
5981 if (ufunc->uf_va_type != NULL
5982 && ufunc->uf_va_type != &t_list_any
5983 && ufunc->uf_va_type->tt_member != &t_any
5984 && tv->vval.v_list != NULL)
5985 {
5986 type_T *expected = ufunc->uf_va_type->tt_member;
5987 listitem_T *li = tv->vval.v_list->lv_first;
5988
5989 for (idx = 0; idx < vararg_count; ++idx)
5990 {
5991 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005992 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005993 goto failed_early;
5994 li = li->li_next;
5995 }
5996 }
5997
5998 if (defcount > 0)
5999 // Move varargs list to below missing default arguments.
6000 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6001 --ectx.ec_stack.ga_len;
6002 }
6003
6004 // Make space for omitted arguments, will store default value below.
6005 // Any varargs list goes after them.
6006 if (defcount > 0)
6007 for (idx = 0; idx < defcount; ++idx)
6008 {
6009 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6010 ++ectx.ec_stack.ga_len;
6011 }
6012 if (ufunc->uf_va_name != NULL)
6013 ++ectx.ec_stack.ga_len;
6014
6015 // Frame pointer points to just after arguments.
6016 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6017 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6018
6019 {
6020 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6021 + ufunc->uf_dfunc_idx;
6022 ufunc_T *base_ufunc = dfunc->df_ufunc;
6023
6024 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006025 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006026 if (partial != NULL || base_ufunc->uf_partial != NULL)
6027 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006028 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6029 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006030 goto failed_early;
6031 if (partial != NULL)
6032 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006033 outer_T *outer = get_pt_outer(partial);
6034
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006035 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006036 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006037 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006038 if (current_ectx != NULL)
6039 {
6040 if (current_ectx->ec_outer_ref != NULL
6041 && current_ectx->ec_outer_ref->or_outer != NULL)
6042 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006043 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006044 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006045 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006046 }
6047 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006048 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006049 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006050 ++partial->pt_refcount;
6051 ectx.ec_outer_ref->or_partial = partial;
6052 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006053 }
6054 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006055 {
6056 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6057 ++base_ufunc->uf_partial->pt_refcount;
6058 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6059 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006060 }
6061 }
6062
6063 // dummy frame entries
6064 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6065 {
6066 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6067 ++ectx.ec_stack.ga_len;
6068 }
6069
6070 {
6071 // Reserve space for local variables and any closure reference count.
6072 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6073 + ufunc->uf_dfunc_idx;
6074
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006075 // Initialize variables to zero. That avoids having to generate
6076 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006077 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006078 {
6079 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6080 STACK_TV_VAR(idx)->vval.v_number = 0;
6081 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006082 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006083
6084 if (object != NULL)
6085 {
6086 // the object is always the variable at index zero
6087 tv = STACK_TV_VAR(0);
6088 tv->v_type = VAR_OBJECT;
6089 tv->vval.v_object = object;
6090 }
6091
Bram Moolenaar4c137212021-04-19 16:48:48 +02006092 if (dfunc->df_has_closure)
6093 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006094 // Initialize the variable that counts how many closures were
6095 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006096 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6097 STACK_TV_VAR(idx)->vval.v_number = 0;
6098 ++ectx.ec_stack.ga_len;
6099 }
6100
6101 ectx.ec_instr = INSTRUCTIONS(dfunc);
6102 }
6103
Bram Moolenaar58779852022-09-06 18:31:14 +01006104 // Store the execution context in funccal, used by invoke_all_defer().
6105 if (funccal != NULL)
6106 funccal->fc_ectx = &ectx;
6107
Bram Moolenaar4c137212021-04-19 16:48:48 +02006108 // Following errors are in the function, not the caller.
6109 // Commands behave like vim9script.
6110 estack_push_ufunc(ufunc, 1);
6111 current_sctx = ufunc->uf_script_ctx;
6112 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6113
6114 // Use a specific location for storing error messages to be converted to an
6115 // exception.
6116 saved_msg_list = msg_list;
6117 msg_list = &private_msg_list;
6118
6119 // Do turn errors into exceptions.
6120 suppress_errthrow = FALSE;
6121
6122 // Do not delete the function while executing it.
6123 ++ufunc->uf_calls;
6124
6125 // When ":silent!" was used before calling then we still abort the
6126 // function. If ":silent!" is used in the function then we don't.
6127 emsg_silent_def = emsg_silent;
6128 did_emsg_def = 0;
6129
6130 ectx.ec_where.wt_index = 0;
6131 ectx.ec_where.wt_variable = FALSE;
6132
Bram Moolenaar5800c792022-09-22 17:34:01 +01006133 /*
6134 * Execute the instructions until done.
6135 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006136 ret = exec_instructions(&ectx);
6137 if (ret == OK)
6138 {
6139 // function finished, get result from the stack.
6140 if (ufunc->uf_ret_type == &t_void)
6141 {
6142 rettv->v_type = VAR_VOID;
6143 }
6144 else
6145 {
6146 tv = STACK_TV_BOT(-1);
6147 *rettv = *tv;
6148 tv->v_type = VAR_UNKNOWN;
6149 }
6150 }
6151
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006152 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006153 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006154
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006155 // Deal with any remaining closures, they may be in use somewhere.
6156 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006157 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006158 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006159 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006160 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006161
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006162 estack_pop();
6163 current_sctx = save_current_sctx;
6164
Bram Moolenaar2336c372021-12-06 15:06:54 +00006165 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6166 // Function was unreferenced while being used, free it now.
6167 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006168
Bram Moolenaar352134b2020-10-17 22:04:08 +02006169 if (*msg_list != NULL && saved_msg_list != NULL)
6170 {
6171 msglist_T **plist = saved_msg_list;
6172
6173 // Append entries from the current msg_list (uncaught exceptions) to
6174 // the saved msg_list.
6175 while (*plist != NULL)
6176 plist = &(*plist)->next;
6177
6178 *plist = *msg_list;
6179 }
6180 msg_list = saved_msg_list;
6181
Bram Moolenaar4c137212021-04-19 16:48:48 +02006182 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006183 {
6184 cmdmod.cmod_filter_regmatch.regprog = NULL;
6185 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006186 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006187 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006188 emsg_silent_def = save_emsg_silent_def;
6189 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006190
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006191failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006192 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006194 {
6195 tv = STACK_TV(idx);
6196 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6197 clear_tv(tv);
6198 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006199 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006201 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006202 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006203 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006204 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006205 if (ectx.ec_outer_ref->or_outer_allocated)
6206 vim_free(ectx.ec_outer_ref->or_outer);
6207 partial_unref(ectx.ec_outer_ref->or_partial);
6208 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006209 }
6210
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006211 // Not sure if this is necessary.
6212 suppress_errthrow = save_suppress_errthrow;
6213
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006214 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6215 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006216 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006217 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006218 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 return ret;
6220}
6221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006223 * Called when a def function has finished (possibly failed).
6224 * Invoke all the function returns to clean up and invoke deferred functions,
6225 * except the toplevel one.
6226 */
6227 void
6228unwind_def_callstack(ectx_T *ectx)
6229{
6230 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6231 func_return(ectx);
6232}
6233
6234/*
dundargocc57b5bc2022-11-02 13:30:51 +00006235 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006236 */
6237 void
6238may_invoke_defer_funcs(ectx_T *ectx)
6239{
6240 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6241
6242 if (dfunc->df_defer_var_idx > 0)
6243 invoke_defer_funcs(ectx);
6244}
6245
6246/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006247 * Return loopvarinfo in a printable form in allocated memory.
6248 */
6249 static char_u *
6250printable_loopvarinfo(loopvarinfo_T *lvi)
6251{
6252 garray_T ga;
6253 int depth;
6254
6255 ga_init2(&ga, 1, 100);
6256 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6257 {
6258 if (ga_grow(&ga, 50) == FAIL)
6259 break;
6260 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006261 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006262 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006263 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006264 lvi->lvi_loop[depth].var_idx,
6265 lvi->lvi_loop[depth].var_idx
6266 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006267 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006268 }
6269 return ga.ga_data;
6270}
6271
6272/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006273 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6274 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6275 * "pfx" is prefixed to every line.
6276 */
6277 static void
6278list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6279{
6280 int line_idx = 0;
6281 int prev_current = 0;
6282 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006283 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006284
6285 for (current = 0; current < instr_count; ++current)
6286 {
6287 isn_T *iptr = &instr[current];
6288 char *line;
6289
6290 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006291 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006292 while (line_idx < iptr->isn_lnum
6293 && line_idx < ufunc->uf_lines.ga_len)
6294 {
6295 if (current > prev_current)
6296 {
6297 msg_puts("\n\n");
6298 prev_current = current;
6299 }
6300 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6301 if (line != NULL)
6302 msg(line);
6303 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006304 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6305 {
6306 int first_def_arg = ufunc->uf_args.ga_len
6307 - ufunc->uf_def_args.ga_len;
6308
6309 if (def_arg_idx > 0)
6310 msg_puts("\n\n");
6311 msg_start();
6312 msg_puts(" ");
6313 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6314 first_def_arg + def_arg_idx]);
6315 msg_puts(" = ");
6316 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6317 msg_clr_eos();
6318 msg_end();
6319 }
6320 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006321
6322 switch (iptr->isn_type)
6323 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006324 case ISN_CONSTRUCT:
6325 smsg("%s%4d NEW %s size %d", pfx, current,
6326 iptr->isn_arg.construct.construct_class->class_name,
6327 (int)iptr->isn_arg.construct.construct_size);
6328 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006329 case ISN_EXEC:
6330 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6331 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006332 case ISN_EXEC_SPLIT:
6333 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6334 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006335 case ISN_EXECRANGE:
6336 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6337 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006338 case ISN_LEGACY_EVAL:
6339 smsg("%s%4d EVAL legacy %s", pfx, current,
6340 iptr->isn_arg.string);
6341 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006342 case ISN_REDIRSTART:
6343 smsg("%s%4d REDIR", pfx, current);
6344 break;
6345 case ISN_REDIREND:
6346 smsg("%s%4d REDIR END%s", pfx, current,
6347 iptr->isn_arg.number ? " append" : "");
6348 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006349 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006350#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006351 smsg("%s%4d CEXPR pre %s", pfx, current,
6352 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006353#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006354 break;
6355 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006356#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006357 {
6358 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6359
6360 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6361 cexpr_get_auname(cer->cer_cmdidx),
6362 cer->cer_forceit ? "!" : "",
6363 cer->cer_cmdline);
6364 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006365#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006366 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006367 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006368 smsg("%s%4d INSTR", pfx, current);
6369 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6370 msg(" -------------");
6371 break;
6372 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006373 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006374 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6375
6376 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006377 }
6378 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006379 case ISN_SUBSTITUTE:
6380 {
6381 subs_T *subs = &iptr->isn_arg.subs;
6382
6383 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6384 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6385 msg(" -------------");
6386 }
6387 break;
6388 case ISN_EXECCONCAT:
6389 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6390 (varnumber_T)iptr->isn_arg.number);
6391 break;
6392 case ISN_ECHO:
6393 {
6394 echo_T *echo = &iptr->isn_arg.echo;
6395
6396 smsg("%s%4d %s %d", pfx, current,
6397 echo->echo_with_white ? "ECHO" : "ECHON",
6398 echo->echo_count);
6399 }
6400 break;
6401 case ISN_EXECUTE:
6402 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006403 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006404 break;
6405 case ISN_ECHOMSG:
6406 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006407 (varnumber_T)(iptr->isn_arg.number));
6408 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006409 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006410 if (iptr->isn_arg.echowin.ewin_time > 0)
6411 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6412 iptr->isn_arg.echowin.ewin_count,
6413 iptr->isn_arg.echowin.ewin_time);
6414 else
6415 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6416 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006417 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006418 case ISN_ECHOCONSOLE:
6419 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6420 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006421 break;
6422 case ISN_ECHOERR:
6423 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006424 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006425 break;
6426 case ISN_LOAD:
6427 {
6428 if (iptr->isn_arg.number < 0)
6429 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6430 (varnumber_T)(iptr->isn_arg.number
6431 + STACK_FRAME_SIZE));
6432 else
6433 smsg("%s%4d LOAD $%lld", pfx, current,
6434 (varnumber_T)(iptr->isn_arg.number));
6435 }
6436 break;
6437 case ISN_LOADOUTER:
6438 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006439 isn_outer_T *outer = &iptr->isn_arg.outer;
6440
6441 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006442 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006443 outer->outer_depth,
6444 outer->outer_idx + STACK_FRAME_SIZE);
6445 else if (outer->outer_depth < 0)
6446 smsg("%s%4d LOADOUTER $%d in loop level %d",
6447 pfx, current,
6448 outer->outer_idx,
6449 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006450 else
6451 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006452 outer->outer_depth,
6453 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006454 }
6455 break;
6456 case ISN_LOADV:
6457 smsg("%s%4d LOADV v:%s", pfx, current,
6458 get_vim_var_name(iptr->isn_arg.number));
6459 break;
6460 case ISN_LOADSCRIPT:
6461 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006462 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6463 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6464 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006465
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006466 sv = get_script_svar(sref, -1);
6467 if (sv == NULL)
6468 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6469 pfx, current, si->sn_name);
6470 else
6471 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006472 sv->sv_name,
6473 sref->sref_idx,
6474 si->sn_name);
6475 }
6476 break;
6477 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006478 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006479 {
6480 scriptitem_T *si = SCRIPT_ITEM(
6481 iptr->isn_arg.loadstore.ls_sid);
6482
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006483 smsg("%s%4d %s s:%s from %s", pfx, current,
6484 iptr->isn_type == ISN_LOADS ? "LOADS"
6485 : "LOADEXPORT",
6486 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006487 }
6488 break;
6489 case ISN_LOADAUTO:
6490 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6491 break;
6492 case ISN_LOADG:
6493 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6494 break;
6495 case ISN_LOADB:
6496 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6497 break;
6498 case ISN_LOADW:
6499 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6500 break;
6501 case ISN_LOADT:
6502 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6503 break;
6504 case ISN_LOADGDICT:
6505 smsg("%s%4d LOAD g:", pfx, current);
6506 break;
6507 case ISN_LOADBDICT:
6508 smsg("%s%4d LOAD b:", pfx, current);
6509 break;
6510 case ISN_LOADWDICT:
6511 smsg("%s%4d LOAD w:", pfx, current);
6512 break;
6513 case ISN_LOADTDICT:
6514 smsg("%s%4d LOAD t:", pfx, current);
6515 break;
6516 case ISN_LOADOPT:
6517 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6518 break;
6519 case ISN_LOADENV:
6520 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6521 break;
6522 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006523 smsg("%s%4d LOADREG @%c", pfx, current,
6524 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006525 break;
6526
6527 case ISN_STORE:
6528 if (iptr->isn_arg.number < 0)
6529 smsg("%s%4d STORE arg[%lld]", pfx, current,
6530 iptr->isn_arg.number + STACK_FRAME_SIZE);
6531 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006532 smsg("%s%4d STORE $%lld", pfx, current,
6533 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006534 break;
6535 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006536 {
6537 isn_outer_T *outer = &iptr->isn_arg.outer;
6538
6539 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6540 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6541 pfx, current, outer->outer_idx);
6542 else
6543 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6544 outer->outer_depth, outer->outer_idx);
6545 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006546 break;
6547 case ISN_STOREV:
6548 smsg("%s%4d STOREV v:%s", pfx, current,
6549 get_vim_var_name(iptr->isn_arg.number));
6550 break;
6551 case ISN_STOREAUTO:
6552 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6553 break;
6554 case ISN_STOREG:
6555 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6556 break;
6557 case ISN_STOREB:
6558 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6559 break;
6560 case ISN_STOREW:
6561 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6562 break;
6563 case ISN_STORET:
6564 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6565 break;
6566 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006567 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006568 {
6569 scriptitem_T *si = SCRIPT_ITEM(
6570 iptr->isn_arg.loadstore.ls_sid);
6571
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006572 smsg("%s%4d %s %s in %s", pfx, current,
6573 iptr->isn_type == ISN_STORES
6574 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006575 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6576 }
6577 break;
6578 case ISN_STORESCRIPT:
6579 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006580 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6581 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6582 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006583
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006584 sv = get_script_svar(sref, -1);
6585 if (sv == NULL)
6586 smsg("%s%4d STORESCRIPT [deleted] in %s",
6587 pfx, current, si->sn_name);
6588 else
6589 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006590 sv->sv_name,
6591 sref->sref_idx,
6592 si->sn_name);
6593 }
6594 break;
6595 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006596 case ISN_STOREFUNCOPT:
6597 smsg("%s%4d %s &%s", pfx, current,
6598 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006599 iptr->isn_arg.storeopt.so_name);
6600 break;
6601 case ISN_STOREENV:
6602 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6603 break;
6604 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006605 smsg("%s%4d STOREREG @%c", pfx, current,
6606 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006607 break;
6608 case ISN_STORENR:
6609 smsg("%s%4d STORE %lld in $%d", pfx, current,
6610 iptr->isn_arg.storenr.stnr_val,
6611 iptr->isn_arg.storenr.stnr_idx);
6612 break;
6613
6614 case ISN_STOREINDEX:
6615 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006616 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006617 break;
6618
6619 case ISN_STORERANGE:
6620 smsg("%s%4d STORERANGE", pfx, current);
6621 break;
6622
Bram Moolenaard505d172022-12-18 21:42:55 +00006623 case ISN_LOAD_CLASSMEMBER:
6624 case ISN_STORE_CLASSMEMBER:
6625 {
6626 class_T *cl = iptr->isn_arg.classmember.cm_class;
6627 int idx = iptr->isn_arg.classmember.cm_idx;
6628 ocmember_T *ocm = &cl->class_class_members[idx];
6629 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6630 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6631 ? "LOAD" : "STORE",
6632 cl->class_name, ocm->ocm_name);
6633 }
6634 break;
6635
Bram Moolenaar4c137212021-04-19 16:48:48 +02006636 // constants
6637 case ISN_PUSHNR:
6638 smsg("%s%4d PUSHNR %lld", pfx, current,
6639 (varnumber_T)(iptr->isn_arg.number));
6640 break;
6641 case ISN_PUSHBOOL:
6642 case ISN_PUSHSPEC:
6643 smsg("%s%4d PUSH %s", pfx, current,
6644 get_var_special_name(iptr->isn_arg.number));
6645 break;
6646 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006647 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006648 break;
6649 case ISN_PUSHS:
6650 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6651 break;
6652 case ISN_PUSHBLOB:
6653 {
6654 char_u *r;
6655 char_u numbuf[NUMBUFLEN];
6656 char_u *tofree;
6657
6658 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6659 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6660 vim_free(tofree);
6661 }
6662 break;
6663 case ISN_PUSHFUNC:
6664 {
6665 char *name = (char *)iptr->isn_arg.string;
6666
6667 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6668 name == NULL ? "[none]" : name);
6669 }
6670 break;
6671 case ISN_PUSHCHANNEL:
6672#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006673 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006674#endif
6675 break;
6676 case ISN_PUSHJOB:
6677#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006678 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006679#endif
6680 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006681 case ISN_PUSHOBJ:
6682 smsg("%s%4d PUSHOBJ null", pfx, current);
6683 break;
6684 case ISN_PUSHCLASS:
6685 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006686 iptr->isn_arg.classarg == NULL ? "null"
6687 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006688 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006689 case ISN_PUSHEXC:
6690 smsg("%s%4d PUSH v:exception", pfx, current);
6691 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006692 case ISN_AUTOLOAD:
6693 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6694 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006695 case ISN_UNLET:
6696 smsg("%s%4d UNLET%s %s", pfx, current,
6697 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6698 iptr->isn_arg.unlet.ul_name);
6699 break;
6700 case ISN_UNLETENV:
6701 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6702 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6703 iptr->isn_arg.unlet.ul_name);
6704 break;
6705 case ISN_UNLETINDEX:
6706 smsg("%s%4d UNLETINDEX", pfx, current);
6707 break;
6708 case ISN_UNLETRANGE:
6709 smsg("%s%4d UNLETRANGE", pfx, current);
6710 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006711 case ISN_LOCKUNLOCK:
6712 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6713 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006714 case ISN_LOCKCONST:
6715 smsg("%s%4d LOCKCONST", pfx, current);
6716 break;
6717 case ISN_NEWLIST:
6718 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006719 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006720 break;
6721 case ISN_NEWDICT:
6722 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006723 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006724 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006725 case ISN_NEWPARTIAL:
6726 smsg("%s%4d NEWPARTIAL", pfx, current);
6727 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006728
6729 // function call
6730 case ISN_BCALL:
6731 {
6732 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6733
6734 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6735 internal_func_name(cbfunc->cbf_idx),
6736 cbfunc->cbf_argcount);
6737 }
6738 break;
6739 case ISN_DCALL:
6740 {
6741 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6742 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6743 + cdfunc->cdf_idx;
6744
6745 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006746 printable_func_name(df->df_ufunc),
6747 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006748 }
6749 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006750 case ISN_METHODCALL:
6751 {
6752 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6753
6754 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6755 mfunc->cmf_itf->class_name,
6756 mfunc->cmf_itf->class_obj_methods[
6757 mfunc->cmf_idx]->uf_name,
6758 mfunc->cmf_argcount);
6759 }
6760 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006761 case ISN_UCALL:
6762 {
6763 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6764
6765 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6766 cufunc->cuf_name, cufunc->cuf_argcount);
6767 }
6768 break;
6769 case ISN_PCALL:
6770 {
6771 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6772
6773 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6774 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6775 }
6776 break;
6777 case ISN_PCALL_END:
6778 smsg("%s%4d PCALL end", pfx, current);
6779 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006780 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006781 case ISN_DEFEROBJ:
6782 smsg("%s%4d %s %d args", pfx, current,
6783 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006784 (int)iptr->isn_arg.defer.defer_argcount);
6785 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006786 case ISN_RETURN:
6787 smsg("%s%4d RETURN", pfx, current);
6788 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006789 case ISN_RETURN_VOID:
6790 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006791 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006792 case ISN_RETURN_OBJECT:
6793 smsg("%s%4d RETURN object", pfx, current);
6794 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006795 case ISN_FUNCREF:
6796 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006797 funcref_T *funcref = &iptr->isn_arg.funcref;
6798 funcref_extra_T *extra = funcref->fr_extra;
6799 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006800
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006801 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006802 {
6803 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6804 + funcref->fr_dfunc_idx;
6805 name = df->df_ufunc->uf_name;
6806 }
6807 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006808 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006809 if (extra != NULL && extra->fre_class != NULL)
6810 {
6811 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6812 extra->fre_class->class_name, name);
6813 }
6814 else if (extra == NULL
6815 || extra->fre_loopvar_info.lvi_depth == 0)
6816 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006817 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006818 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006819 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006820 {
6821 char_u *info = printable_loopvarinfo(
6822 &extra->fre_loopvar_info);
6823
6824 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6825 name, info);
6826 vim_free(info);
6827 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006828 }
6829 break;
6830
6831 case ISN_NEWFUNC:
6832 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006833 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006834
Bram Moolenaarcc341812022-09-19 15:54:34 +01006835 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006836 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6837 arg->nfa_lambda, arg->nfa_global);
6838 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006839 {
6840 char_u *info = printable_loopvarinfo(
6841 &arg->nfa_loopvar_info);
6842
6843 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6844 arg->nfa_lambda, arg->nfa_global, info);
6845 vim_free(info);
6846 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006847 }
6848 break;
6849
6850 case ISN_DEF:
6851 {
6852 char_u *name = iptr->isn_arg.string;
6853
6854 smsg("%s%4d DEF %s", pfx, current,
6855 name == NULL ? (char_u *)"" : name);
6856 }
6857 break;
6858
6859 case ISN_JUMP:
6860 {
6861 char *when = "?";
6862
6863 switch (iptr->isn_arg.jump.jump_when)
6864 {
6865 case JUMP_ALWAYS:
6866 when = "JUMP";
6867 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006868 case JUMP_NEVER:
6869 iemsg("JUMP_NEVER should not be used");
6870 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006871 case JUMP_AND_KEEP_IF_TRUE:
6872 when = "JUMP_AND_KEEP_IF_TRUE";
6873 break;
6874 case JUMP_IF_FALSE:
6875 when = "JUMP_IF_FALSE";
6876 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006877 case JUMP_WHILE_FALSE:
6878 when = "JUMP_WHILE_FALSE"; // unused
6879 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006880 case JUMP_IF_COND_FALSE:
6881 when = "JUMP_IF_COND_FALSE";
6882 break;
6883 case JUMP_IF_COND_TRUE:
6884 when = "JUMP_IF_COND_TRUE";
6885 break;
6886 }
6887 smsg("%s%4d %s -> %d", pfx, current, when,
6888 iptr->isn_arg.jump.jump_where);
6889 }
6890 break;
6891
6892 case ISN_JUMP_IF_ARG_SET:
6893 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6894 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6895 iptr->isn_arg.jump.jump_where);
6896 break;
6897
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006898 case ISN_JUMP_IF_ARG_NOT_SET:
6899 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6900 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6901 iptr->isn_arg.jump.jump_where);
6902 break;
6903
Bram Moolenaar4c137212021-04-19 16:48:48 +02006904 case ISN_FOR:
6905 {
6906 forloop_T *forloop = &iptr->isn_arg.forloop;
6907
6908 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006909 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006910 }
6911 break;
6912
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006913 case ISN_ENDLOOP:
6914 {
6915 endloop_T *endloop = &iptr->isn_arg.endloop;
6916
Bram Moolenaarcc341812022-09-19 15:54:34 +01006917 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6918 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006919 endloop->end_funcref_idx,
6920 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006921 endloop->end_var_idx + endloop->end_var_count - 1,
6922 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006923 }
6924 break;
6925
6926 case ISN_WHILE:
6927 {
6928 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6929
6930 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6931 whileloop->while_funcref_idx,
6932 whileloop->while_end);
6933 }
6934 break;
6935
Bram Moolenaar4c137212021-04-19 16:48:48 +02006936 case ISN_TRY:
6937 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006938 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006939
6940 if (try->try_ref->try_finally == 0)
6941 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6942 pfx, current,
6943 try->try_ref->try_catch,
6944 try->try_ref->try_endtry);
6945 else
6946 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6947 pfx, current,
6948 try->try_ref->try_catch,
6949 try->try_ref->try_finally,
6950 try->try_ref->try_endtry);
6951 }
6952 break;
6953 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006954 smsg("%s%4d CATCH", pfx, current);
6955 break;
6956 case ISN_TRYCONT:
6957 {
6958 trycont_T *trycont = &iptr->isn_arg.trycont;
6959
6960 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6961 trycont->tct_levels,
6962 trycont->tct_levels == 1 ? "" : "s",
6963 trycont->tct_where);
6964 }
6965 break;
6966 case ISN_FINALLY:
6967 smsg("%s%4d FINALLY", pfx, current);
6968 break;
6969 case ISN_ENDTRY:
6970 smsg("%s%4d ENDTRY", pfx, current);
6971 break;
6972 case ISN_THROW:
6973 smsg("%s%4d THROW", pfx, current);
6974 break;
6975
6976 // expression operations on number
6977 case ISN_OPNR:
6978 case ISN_OPFLOAT:
6979 case ISN_OPANY:
6980 {
6981 char *what;
6982 char *ins;
6983
6984 switch (iptr->isn_arg.op.op_type)
6985 {
6986 case EXPR_MULT: what = "*"; break;
6987 case EXPR_DIV: what = "/"; break;
6988 case EXPR_REM: what = "%"; break;
6989 case EXPR_SUB: what = "-"; break;
6990 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006991 case EXPR_LSHIFT: what = "<<"; break;
6992 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006993 default: what = "???"; break;
6994 }
6995 switch (iptr->isn_type)
6996 {
6997 case ISN_OPNR: ins = "OPNR"; break;
6998 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6999 case ISN_OPANY: ins = "OPANY"; break;
7000 default: ins = "???"; break;
7001 }
7002 smsg("%s%4d %s %s", pfx, current, ins, what);
7003 }
7004 break;
7005
7006 case ISN_COMPAREBOOL:
7007 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007008 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007009 case ISN_COMPARENR:
7010 case ISN_COMPAREFLOAT:
7011 case ISN_COMPARESTRING:
7012 case ISN_COMPAREBLOB:
7013 case ISN_COMPARELIST:
7014 case ISN_COMPAREDICT:
7015 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007016 case ISN_COMPARECLASS:
7017 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007018 case ISN_COMPAREANY:
7019 {
7020 char *p;
7021 char buf[10];
7022 char *type;
7023
7024 switch (iptr->isn_arg.op.op_type)
7025 {
7026 case EXPR_EQUAL: p = "=="; break;
7027 case EXPR_NEQUAL: p = "!="; break;
7028 case EXPR_GREATER: p = ">"; break;
7029 case EXPR_GEQUAL: p = ">="; break;
7030 case EXPR_SMALLER: p = "<"; break;
7031 case EXPR_SEQUAL: p = "<="; break;
7032 case EXPR_MATCH: p = "=~"; break;
7033 case EXPR_IS: p = "is"; break;
7034 case EXPR_ISNOT: p = "isnot"; break;
7035 case EXPR_NOMATCH: p = "!~"; break;
7036 default: p = "???"; break;
7037 }
7038 STRCPY(buf, p);
7039 if (iptr->isn_arg.op.op_ic == TRUE)
7040 strcat(buf, "?");
7041 switch(iptr->isn_type)
7042 {
7043 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7044 case ISN_COMPARESPECIAL:
7045 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007046 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007047 case ISN_COMPARENR: type = "COMPARENR"; break;
7048 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7049 case ISN_COMPARESTRING:
7050 type = "COMPARESTRING"; break;
7051 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7052 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7053 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7054 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007055 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7056 case ISN_COMPAREOBJECT:
7057 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007058 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7059 default: type = "???"; break;
7060 }
7061
7062 smsg("%s%4d %s %s", pfx, current, type, buf);
7063 }
7064 break;
7065
7066 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7067 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7068
7069 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007070 case ISN_CONCAT:
7071 smsg("%s%4d CONCAT size %lld", pfx, current,
7072 (varnumber_T)(iptr->isn_arg.number));
7073 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007074 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7075 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7076 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7077 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7078 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7079 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7080 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7081 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7082 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7083 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7084 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007085 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007086 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7087 iptr->isn_arg.getitem.gi_index,
7088 iptr->isn_arg.getitem.gi_with_op ?
7089 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007090 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7091 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7092 iptr->isn_arg.string); break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007093 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7094 (int)iptr->isn_arg.number); break;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007095 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
7096 pfx, current,
7097 (int)iptr->isn_arg.classmember.cm_idx,
7098 iptr->isn_arg.classmember.cm_class->class_name);
7099 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007100 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007101 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007102 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7103 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7104
Bram Moolenaar4c137212021-04-19 16:48:48 +02007105 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7106
Bram Moolenaar4c137212021-04-19 16:48:48 +02007107 case ISN_CHECKTYPE:
7108 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007109 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007110 char *tofree = NULL;
7111 char *typename;
7112
7113 if (ct->ct_type->tt_type == VAR_FLOAT
7114 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7115 typename = "float|number";
7116 else
7117 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007118
7119 if (ct->ct_arg_idx == 0)
7120 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007121 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007122 (int)ct->ct_off);
7123 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007124 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007125 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007126 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007127 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007128 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007129 (int)ct->ct_arg_idx);
7130 vim_free(tofree);
7131 break;
7132 }
7133 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7134 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7135 iptr->isn_arg.checklen.cl_min_len);
7136 break;
7137 case ISN_SETTYPE:
7138 {
7139 char *tofree;
7140
7141 smsg("%s%4d SETTYPE %s", pfx, current,
7142 type_name(iptr->isn_arg.type.ct_type, &tofree));
7143 vim_free(tofree);
7144 break;
7145 }
7146 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007147 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7148 smsg("%s%4d INVERT %d (!val)", pfx, current,
7149 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007150 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007151 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7152 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007153 break;
7154 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007155 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007156 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007157 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7158 pfx, current,
7159 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007160 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007161 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7162 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007163 break;
7164 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007165 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007166 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007167 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007168 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7169 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007170 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007171 else
7172 smsg("%s%4d PUT %c %ld", pfx, current,
7173 iptr->isn_arg.put.put_regname,
7174 (long)iptr->isn_arg.put.put_lnum);
7175 break;
7176
Bram Moolenaar4c137212021-04-19 16:48:48 +02007177 case ISN_CMDMOD:
7178 {
7179 char_u *buf;
7180 size_t len = produce_cmdmods(
7181 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7182
7183 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007184 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007185 {
7186 (void)produce_cmdmods(
7187 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7188 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7189 vim_free(buf);
7190 }
7191 break;
7192 }
7193 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7194
7195 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007196 smsg("%s%4d PROFILE START line %d", pfx, current,
7197 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007198 break;
7199
7200 case ISN_PROF_END:
7201 smsg("%s%4d PROFILE END", pfx, current);
7202 break;
7203
Bram Moolenaare99d4222021-06-13 14:01:26 +02007204 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007205 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7206 iptr->isn_arg.debug.dbg_break_lnum + 1,
7207 iptr->isn_lnum,
7208 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007209 break;
7210
Bram Moolenaar4c137212021-04-19 16:48:48 +02007211 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7212 iptr->isn_arg.unpack.unp_count,
7213 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7214 break;
7215 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7216 iptr->isn_arg.shuffle.shfl_item,
7217 iptr->isn_arg.shuffle.shfl_up);
7218 break;
7219 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7220
7221 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7222 return;
7223 }
7224
7225 out_flush(); // output one line at a time
7226 ui_breakcheck();
7227 if (got_int)
7228 break;
7229 }
7230}
7231
7232/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007233 * Handle command line completion for the :disassemble command.
7234 */
7235 void
7236set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7237{
7238 char_u *p;
7239
7240 // Default: expand user functions, "debug" and "profile"
7241 xp->xp_context = EXPAND_DISASSEMBLE;
7242 xp->xp_pattern = arg;
7243
7244 // first argument already typed: only user function names
7245 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7246 {
7247 xp->xp_context = EXPAND_USER_FUNC;
7248 xp->xp_pattern = skipwhite(p);
7249 }
7250}
7251
7252/*
7253 * Function given to ExpandGeneric() to obtain the list of :disassemble
7254 * arguments.
7255 */
7256 char_u *
7257get_disassemble_argument(expand_T *xp, int idx)
7258{
7259 if (idx == 0)
7260 return (char_u *)"debug";
7261 if (idx == 1)
7262 return (char_u *)"profile";
7263 return get_user_func_name(xp, idx - 2);
7264}
7265
7266/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007267 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007268 * We don't really need this at runtime, but we do have tests that require it,
7269 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007270 */
7271 void
7272ex_disassemble(exarg_T *eap)
7273{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007274 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007275 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007277 isn_T *instr = NULL; // init to shut up gcc warning
7278 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007279 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007280
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007281 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007282 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007283 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007284 if (func_needs_compiling(ufunc, compile_type)
7285 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007286 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007287 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007288 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007289 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290 return;
7291 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007292 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293
7294 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007295 switch (compile_type)
7296 {
7297 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007298#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007299 instr = dfunc->df_instr_prof;
7300 instr_count = dfunc->df_instr_prof_count;
7301 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007302#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007303 // FALLTHROUGH
7304 case CT_NONE:
7305 instr = dfunc->df_instr;
7306 instr_count = dfunc->df_instr_count;
7307 break;
7308 case CT_DEBUG:
7309 instr = dfunc->df_instr_debug;
7310 instr_count = dfunc->df_instr_debug_count;
7311 break;
7312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007313
Bram Moolenaar4c137212021-04-19 16:48:48 +02007314 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315}
7316
7317/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007318 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 * list, etc. Mostly like what JavaScript does, except that empty list and
7320 * empty dictionary are FALSE.
7321 */
7322 int
7323tv2bool(typval_T *tv)
7324{
7325 switch (tv->v_type)
7326 {
7327 case VAR_NUMBER:
7328 return tv->vval.v_number != 0;
7329 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 case VAR_PARTIAL:
7332 return tv->vval.v_partial != NULL;
7333 case VAR_FUNC:
7334 case VAR_STRING:
7335 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7336 case VAR_LIST:
7337 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7338 case VAR_DICT:
7339 return tv->vval.v_dict != NULL
7340 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7341 case VAR_BOOL:
7342 case VAR_SPECIAL:
7343 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7344 case VAR_JOB:
7345#ifdef FEAT_JOB_CHANNEL
7346 return tv->vval.v_job != NULL;
7347#else
7348 break;
7349#endif
7350 case VAR_CHANNEL:
7351#ifdef FEAT_JOB_CHANNEL
7352 return tv->vval.v_channel != NULL;
7353#else
7354 break;
7355#endif
7356 case VAR_BLOB:
7357 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7358 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007359 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007361 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007362 case VAR_CLASS:
7363 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 break;
7365 }
7366 return FALSE;
7367}
7368
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007369 void
7370emsg_using_string_as(typval_T *tv, int as_number)
7371{
7372 semsg(_(as_number ? e_using_string_as_number_str
7373 : e_using_string_as_bool_str),
7374 tv->vval.v_string == NULL
7375 ? (char_u *)"" : tv->vval.v_string);
7376}
7377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378/*
7379 * If "tv" is a string give an error and return FAIL.
7380 */
7381 int
7382check_not_string(typval_T *tv)
7383{
7384 if (tv->v_type == VAR_STRING)
7385 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007386 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 clear_tv(tv);
7388 return FAIL;
7389 }
7390 return OK;
7391}
7392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393
7394#endif // FEAT_EVAL