blob: 5bc7708b6c2bf01ef98b1ab898bf48b8487602ee [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200140 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000155 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200168static garray_T dict_stack = GA_EMPTY;
169
170/*
171 * Put a value on the dict stack. This consumes "tv".
172 */
173 static int
174dict_stack_save(typval_T *tv)
175{
176 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000177 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200178 if (ga_grow(&dict_stack, 1) == FAIL)
179 return FAIL;
180 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
181 ++dict_stack.ga_len;
182 return OK;
183}
184
185/*
186 * Get the typval at top of the dict stack.
187 */
188 static typval_T *
189dict_stack_get_tv(void)
190{
191 if (dict_stack.ga_len == 0)
192 return NULL;
193 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
194}
195
196/*
197 * Get the dict at top of the dict stack.
198 */
199 static dict_T *
200dict_stack_get_dict(void)
201{
202 typval_T *tv;
203
204 if (dict_stack.ga_len == 0)
205 return NULL;
206 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
207 if (tv->v_type == VAR_DICT)
208 return tv->vval.v_dict;
209 return NULL;
210}
211
212/*
213 * Drop an item from the dict stack.
214 */
215 static void
216dict_stack_drop(void)
217{
218 if (dict_stack.ga_len == 0)
219 {
220 iemsg("Dict stack underflow");
221 return;
222 }
223 --dict_stack.ga_len;
224 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
225}
226
227/*
228 * Drop items from the dict stack until the length is equal to "len".
229 */
230 static void
231dict_stack_clear(int len)
232{
233 while (dict_stack.ga_len > len)
234 dict_stack_drop();
235}
236
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200237/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000238 * Get a pointer to useful "pt_outer" of "pt".
239 */
240 static outer_T *
241get_pt_outer(partial_T *pt)
242{
243 partial_T *ptref = pt->pt_outer_partial;
244
245 if (ptref == NULL)
246 return &pt->pt_outer;
247
248 // partial using partial (recursively)
249 while (ptref->pt_outer_partial != NULL)
250 ptref = ptref->pt_outer_partial;
251 return &ptref->pt_outer;
252}
253
254/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100256 * This adds a stack frame and sets the instruction pointer to the start of the
257 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200258 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 *
260 * Stack has:
261 * - current arguments (already there)
262 * - omitted optional argument (default values) added here
263 * - stack frame:
264 * - pointer to calling function
265 * - Index of next instruction in calling function
266 * - previous frame pointer
267 * - reserved space for local variables
268 */
269 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100270call_dfunc(
271 int cdf_idx,
272 partial_T *pt,
273 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100274 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100276 int argcount = argcount_arg;
277 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
278 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200279 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100280 int arg_to_add;
281 int vararg_count = 0;
282 int varcount;
283 int idx;
284 estack_T *entry;
285 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200286 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000287 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288
289 if (dfunc->df_deleted)
290 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100291 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000292 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100293 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 return FAIL;
295 }
296
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100297#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100298 if (do_profiling == PROF_YES)
299 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200300 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100301 {
302 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
303 + profile_info_ga.ga_len;
304 ++profile_info_ga.ga_len;
305 CLEAR_POINTER(info);
306 profile_may_start_func(info, ufunc,
307 (((dfunc_T *)def_functions.ga_data)
308 + ectx->ec_dfunc_idx)->df_ufunc);
309 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100310 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100311#endif
312
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200313 // When debugging and using "cont" switches to the not-debugged
314 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000315 compile_type = get_compile_type(ufunc);
316 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200317 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000318 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200319
320 // compile_def_function() may cause def_functions.ga_data to change
321 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
322 }
323 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200324 {
325 if (did_emsg_cumul + did_emsg == did_emsg_before)
326 semsg(_(e_function_is_not_compiled_str),
327 printable_func_name(ufunc));
328 return FAIL;
329 }
330
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200331 if (ufunc->uf_va_name != NULL)
332 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200333 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200334 // Stack at time of call with 2 varargs:
335 // normal_arg
336 // optional_arg
337 // vararg_1
338 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200339 // After creating the list:
340 // normal_arg
341 // optional_arg
342 // vararg-list
343 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200344 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200345 // After creating the list
346 // normal_arg
347 // (space for optional_arg)
348 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200349 vararg_count = argcount - ufunc->uf_args.ga_len;
350 if (vararg_count < 0)
351 vararg_count = 0;
352 else
353 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200354 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200355 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200356
357 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200358 }
359
Bram Moolenaarfe270812020-04-11 22:31:27 +0200360 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200361 if (arg_to_add < 0)
362 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200363 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200364 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200365 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200366 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200367 return FAIL;
368 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000369 else if (arg_to_add > ufunc->uf_def_args.ga_len)
370 {
371 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
372
373 if (missing == 1)
374 emsg(_(e_one_argument_too_few));
375 else
376 semsg(_(e_nr_arguments_too_few), missing);
377 return FAIL;
378 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200379
380 // Reserve space for:
381 // - missing arguments
382 // - stack frame
383 // - local variables
384 // - if needed: a counter for number of closures created in
385 // ectx->ec_funcrefs.
386 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200387 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100388 return FAIL;
389
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100390 // If depth of calling is getting too high, don't execute the function.
391 if (funcdepth_increment() == FAIL)
392 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200393 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100394
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100395 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200396 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100397 {
398 floc = ALLOC_ONE(funclocal_T);
399 if (floc == NULL)
400 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200401 *floc = ectx->ec_funclocal;
402 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100403 }
404
Bram Moolenaarfe270812020-04-11 22:31:27 +0200405 // Move the vararg-list to below the missing optional arguments.
406 if (vararg_count > 0 && arg_to_add > 0)
407 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100408
409 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200410 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200411 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200412 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413
414 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100415 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
416 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200417 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200418 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
419 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100420 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100421 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200422 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423
424 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200425 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000426 {
427 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
428
429 tv->v_type = VAR_NUMBER;
430 tv->vval.v_number = 0;
431 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200432 if (dfunc->df_has_closure)
433 {
434 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
435
436 tv->v_type = VAR_NUMBER;
437 tv->vval.v_number = 0;
438 }
439 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100440
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100441 if (pt != NULL || ufunc->uf_partial != NULL
442 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100443 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200444 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100445
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200446 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100447 return FAIL;
448 if (pt != NULL)
449 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000450 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200451 ++pt->pt_refcount;
452 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100453 }
454 else if (ufunc->uf_partial != NULL)
455 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000456 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200457 ++ufunc->uf_partial->pt_refcount;
458 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100459 }
460 else
461 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200462 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200463 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200464 {
465 vim_free(ref);
466 return FAIL;
467 }
468 ref->or_outer_allocated = TRUE;
469 ref->or_outer->out_stack = &ectx->ec_stack;
470 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
471 if (ectx->ec_outer_ref != NULL)
472 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100473 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200474 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100475 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100476 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200477 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100478
Bram Moolenaarc970e422021-03-17 15:03:04 +0100479 ++ufunc->uf_calls;
480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 // Set execution state to the start of the called function.
482 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100483 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100484 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200485 if (entry != NULL)
486 {
487 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200488 // Save the current context so it can be restored on return.
489 entry->es_save_sctx = current_sctx;
490 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200491 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100492
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200493 // Start execution at the first instruction.
494 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495
496 return OK;
497}
498
499// Get pointer to item in the stack.
500#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
501
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000502// Double linked list of funcstack_T in use.
503static funcstack_T *first_funcstack = NULL;
504
505 static void
506add_funcstack_to_list(funcstack_T *funcstack)
507{
508 // Link in list of funcstacks.
509 if (first_funcstack != NULL)
510 first_funcstack->fs_prev = funcstack;
511 funcstack->fs_next = first_funcstack;
512 funcstack->fs_prev = NULL;
513 first_funcstack = funcstack;
514}
515
516 static void
517remove_funcstack_from_list(funcstack_T *funcstack)
518{
519 if (funcstack->fs_prev == NULL)
520 first_funcstack = funcstack->fs_next;
521 else
522 funcstack->fs_prev->fs_next = funcstack->fs_next;
523 if (funcstack->fs_next != NULL)
524 funcstack->fs_next->fs_prev = funcstack->fs_prev;
525}
526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200528 * Used when returning from a function: Check if any closure is still
529 * referenced. If so then move the arguments and variables to a separate piece
530 * of stack to be used when the closure is called.
531 * When "free_arguments" is TRUE the arguments are to be freed.
532 * Returns FAIL when out of memory.
533 */
534 static int
535handle_closure_in_use(ectx_T *ectx, int free_arguments)
536{
537 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
538 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200539 int argcount;
540 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200541 int idx;
542 typval_T *tv;
543 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200544 garray_T *gap = &ectx->ec_funcrefs;
545 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200546
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200547 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200548 return OK; // function was freed
549 if (dfunc->df_has_closure == 0)
550 return OK; // no closures
551 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
552 closure_count = tv->vval.v_number;
553 if (closure_count == 0)
554 return OK; // no funcrefs created
555
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200556 argcount = ufunc_argcount(dfunc->df_ufunc);
557 top = ectx->ec_frame_idx - argcount;
558
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200559 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200560 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200561 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200562 partial_T *pt;
563 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200564
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200565 if (off < 0)
566 continue; // count is off or already done
567 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200568 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200569 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200570 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200571 int i;
572
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000573 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200574 // unreferenced on return.
575 for (i = 0; i < dfunc->df_varcount; ++i)
576 {
577 typval_T *stv = STACK_TV(ectx->ec_frame_idx
578 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200579 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200580 --refcount;
581 }
582 if (refcount > 1)
583 {
584 closure_in_use = TRUE;
585 break;
586 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200587 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200588 }
589
590 if (closure_in_use)
591 {
592 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
593 typval_T *stack;
594
595 // A closure is using the arguments and/or local variables.
596 // Move them to the called function.
597 if (funcstack == NULL)
598 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000599
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200600 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
601 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200602 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
603 funcstack->fs_ga.ga_data = stack;
604 if (stack == NULL)
605 {
606 vim_free(funcstack);
607 return FAIL;
608 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000609 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200610
611 // Move or copy the arguments.
612 for (idx = 0; idx < argcount; ++idx)
613 {
614 tv = STACK_TV(top + idx);
615 if (free_arguments)
616 {
617 *(stack + idx) = *tv;
618 tv->v_type = VAR_UNKNOWN;
619 }
620 else
621 copy_tv(tv, stack + idx);
622 }
623 // Move the local variables.
624 for (idx = 0; idx < dfunc->df_varcount; ++idx)
625 {
626 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200627
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200628 // A partial created for a local function, that is also used as a
629 // local variable, has a reference count for the variable, thus
630 // will never go down to zero. When all these refcounts are one
631 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000632 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200633 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
634 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200635 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200636
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200637 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200638 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
639 gap->ga_len - closure_count + i])
640 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200641 }
642
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200643 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200644 tv->v_type = VAR_UNKNOWN;
645 }
646
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200647 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200648 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200649 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
650 - closure_count + idx];
651 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200652 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200653 ++funcstack->fs_refcount;
654 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100655 pt->pt_outer.out_stack = &funcstack->fs_ga;
656 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200657 }
658 }
659 }
660
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200661 for (idx = 0; idx < closure_count; ++idx)
662 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
663 - closure_count + idx]);
664 gap->ga_len -= closure_count;
665 if (gap->ga_len == 0)
666 ga_clear(gap);
667
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200668 return OK;
669}
670
671/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200672 * Called when a partial is freed or its reference count goes down to one. The
673 * funcstack may be the only reference to the partials in the local variables.
674 * Go over all of them, the funcref and can be freed if all partials
675 * referencing the funcstack have a reference count of one.
676 */
677 void
678funcstack_check_refcount(funcstack_T *funcstack)
679{
680 int i;
681 garray_T *gap = &funcstack->fs_ga;
682 int done = 0;
683
684 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
685 return;
686 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
687 {
688 typval_T *tv = ((typval_T *)gap->ga_data) + i;
689
690 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
691 && tv->vval.v_partial->pt_funcstack == funcstack
692 && tv->vval.v_partial->pt_refcount == 1)
693 ++done;
694 }
695 if (done == funcstack->fs_min_refcount)
696 {
697 typval_T *stack = gap->ga_data;
698
699 // All partials referencing the funcstack have a reference count of
700 // one, thus the funcstack is no longer of use.
701 for (i = 0; i < gap->ga_len; ++i)
702 clear_tv(stack + i);
703 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000704 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200705 vim_free(funcstack);
706 }
707}
708
709/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000710 * For garbage collecting: set references in all variables referenced by
711 * all funcstacks.
712 */
713 int
714set_ref_in_funcstacks(int copyID)
715{
716 funcstack_T *funcstack;
717
718 for (funcstack = first_funcstack; funcstack != NULL;
719 funcstack = funcstack->fs_next)
720 {
721 typval_T *stack = funcstack->fs_ga.ga_data;
722 int i;
723
724 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
725 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
726 return TRUE; // abort
727 }
728 return FALSE;
729}
730
731/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 * Return from the current function.
733 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200734 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200735func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100738 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200739 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
740 + ectx->ec_dfunc_idx;
741 int argcount = ufunc_argcount(dfunc->df_ufunc);
742 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200743 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100744 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
745 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200746 funclocal_T *floc;
747#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100748 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
749 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750
Bram Moolenaar12d26532021-02-19 19:13:21 +0100751 if (do_profiling == PROF_YES)
752 {
753 ufunc_T *caller = prev_dfunc->df_ufunc;
754
755 if (dfunc->df_ufunc->uf_profiling
756 || (caller != NULL && caller->uf_profiling))
757 {
758 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
759 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
760 --profile_info_ga.ga_len;
761 }
762 }
763#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000764
765 // No check for uf_refcount being zero, cannot think of a way that would
766 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100767 --dfunc->df_ufunc->uf_calls;
768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200770 entry = estack_pop();
771 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200772 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200774 if (handle_closure_in_use(ectx, TRUE) == FAIL)
775 return FAIL;
776
777 // Clear the arguments.
778 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
779 clear_tv(STACK_TV(idx));
780
781 // Clear local variables and temp values, but not the return value.
782 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100783 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100785
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100786 // The return value should be on top of the stack. However, when aborting
787 // it may not be there and ec_frame_idx is the top of the stack.
788 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100789 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100790 ret_idx = 0;
791
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200792 if (ectx->ec_outer_ref != NULL)
793 {
794 if (ectx->ec_outer_ref->or_outer_allocated)
795 vim_free(ectx->ec_outer_ref->or_outer);
796 partial_unref(ectx->ec_outer_ref->or_partial);
797 vim_free(ectx->ec_outer_ref);
798 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100799
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100800 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100801 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100802 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
803 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200804 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
805 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200806 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100807 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100808 floc = (void *)STACK_TV(ectx->ec_frame_idx
809 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200810 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100811 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
812 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200813
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100814 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200815 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100816 else
817 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200818 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100819 vim_free(floc);
820 }
821
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100822 if (ret_idx > 0)
823 {
824 // Reset the stack to the position before the call, with a spot for the
825 // return value, moved there from above the frame.
826 ectx->ec_stack.ga_len = top + 1;
827 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
828 }
829 else
830 // Reset the stack to the position before the call.
831 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200832
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100833 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200834 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200835 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836}
837
838#undef STACK_TV
839
840/*
841 * Prepare arguments and rettv for calling a builtin or user function.
842 */
843 static int
844call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
845{
846 int idx;
847 typval_T *tv;
848
849 // Move arguments from bottom of the stack to argvars[] and add terminator.
850 for (idx = 0; idx < argcount; ++idx)
851 argvars[idx] = *STACK_TV_BOT(idx - argcount);
852 argvars[argcount].v_type = VAR_UNKNOWN;
853
854 // Result replaces the arguments on the stack.
855 if (argcount > 0)
856 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200857 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 return FAIL;
859 else
860 ++ectx->ec_stack.ga_len;
861
862 // Default return value is zero.
863 tv = STACK_TV_BOT(-1);
864 tv->v_type = VAR_NUMBER;
865 tv->vval.v_number = 0;
866
867 return OK;
868}
869
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200870// Ugly global to avoid passing the execution context around through many
871// layers.
872static ectx_T *current_ectx = NULL;
873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874/*
875 * Call a builtin function by index.
876 */
877 static int
878call_bfunc(int func_idx, int argcount, ectx_T *ectx)
879{
880 typval_T argvars[MAX_FUNC_ARGS];
881 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100882 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200883 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200884 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885
886 if (call_prepare(argcount, argvars, ectx) == FAIL)
887 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200888 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200890 // Call the builtin function. Set "current_ectx" so that when it
891 // recursively invokes call_def_function() a closure context can be set.
892 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200894 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200895 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896
897 // Clear the arguments.
898 for (idx = 0; idx < argcount; ++idx)
899 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200900
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100901 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200902 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903 return OK;
904}
905
906/*
907 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100908 * If the function is compiled this will add a stack frame and set the
909 * instruction pointer at the start of the function.
910 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200911 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100912 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913 */
914 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100915call_ufunc(
916 ufunc_T *ufunc,
917 partial_T *pt,
918 int argcount,
919 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200920 isn_T *iptr,
921 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922{
923 typval_T argvars[MAX_FUNC_ARGS];
924 funcexe_T funcexe;
925 int error;
926 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100927 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000928 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929
Bram Moolenaare99d4222021-06-13 14:01:26 +0200930 if (func_needs_compiling(ufunc, compile_type)
931 && compile_def_function(ufunc, FALSE, compile_type, NULL)
932 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200933 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200934 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100935 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100936 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100937 if (error != FCERR_UNKNOWN)
938 {
939 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000940 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100941 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000942 semsg(_(e_not_enough_arguments_for_function_str),
943 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100944 return FAIL;
945 }
946
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100947 // The function has been compiled, can call it quickly. For a function
948 // that was defined later: we can call it directly next time.
949 if (iptr != NULL)
950 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100951 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100952 iptr->isn_type = ISN_DCALL;
953 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
954 iptr->isn_arg.dfunc.cdf_argcount = argcount;
955 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200956 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100957 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958
959 if (call_prepare(argcount, argvars, ectx) == FAIL)
960 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200961 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000962 funcexe.fe_evaluate = TRUE;
963 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964
965 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000967 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968
969 // Clear the arguments.
970 for (idx = 0; idx < argcount; ++idx)
971 clear_tv(&argvars[idx]);
972
973 if (error != FCERR_NONE)
974 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000975 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 return FAIL;
977 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100978 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200979 // Error other than from calling the function itself.
980 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 return OK;
982}
983
984/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100985 * If command modifiers were applied restore them.
986 */
987 static void
988may_restore_cmdmod(funclocal_T *funclocal)
989{
990 if (funclocal->floc_restore_cmdmod)
991 {
992 cmdmod.cmod_filter_regmatch.regprog = NULL;
993 undo_cmdmod(&cmdmod);
994 cmdmod = funclocal->floc_save_cmdmod;
995 funclocal->floc_restore_cmdmod = FALSE;
996 }
997}
998
999/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001000 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1001 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001002 */
1003 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001004vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001005{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001006 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001007}
1008
1009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 * Execute a function by "name".
1011 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001012 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 * Returns FAIL if not found without an error message.
1014 */
1015 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001016call_by_name(
1017 char_u *name,
1018 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001019 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001020 isn_T *iptr,
1021 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022{
1023 ufunc_T *ufunc;
1024
1025 if (builtin_function(name, -1))
1026 {
1027 int func_idx = find_internal_func(name);
1028
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001029 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001031 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 return FAIL;
1033 return call_bfunc(func_idx, argcount, ectx);
1034 }
1035
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001036 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001037
1038 if (ufunc == NULL)
1039 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001040 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001041
1042 if (script_autoload(name, TRUE))
1043 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001044 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001045
1046 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001047 return FAIL; // bail out if loading the script caused an error
1048 }
1049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001051 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001052 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001053 {
1054 int i;
1055 typval_T *argv = STACK_TV_BOT(0) - argcount;
1056
1057 // The function can change at runtime, check that the argument
1058 // types are correct.
1059 for (i = 0; i < argcount; ++i)
1060 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001061 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001062
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001063 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001064 type = ufunc->uf_arg_types[i];
1065 else if (ufunc->uf_va_type != NULL)
1066 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001067 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001068 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001069 return FAIL;
1070 }
1071 }
1072
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001073 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
1076 return FAIL;
1077}
1078
1079 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001080call_partial(
1081 typval_T *tv,
1082 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001083 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001085 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001086 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001088 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001089 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090
1091 if (tv->v_type == VAR_PARTIAL)
1092 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001093 partial_T *pt = tv->vval.v_partial;
1094 int i;
1095
1096 if (pt->pt_argc > 0)
1097 {
1098 // Make space for arguments from the partial, shift the "argcount"
1099 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001100 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001101 return FAIL;
1102 for (i = 1; i <= argcount; ++i)
1103 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1104 ectx->ec_stack.ga_len += pt->pt_argc;
1105 argcount += pt->pt_argc;
1106
1107 // copy the arguments from the partial onto the stack
1108 for (i = 0; i < pt->pt_argc; ++i)
1109 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1110 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001111 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112
1113 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001114 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 name = pt->pt_name;
1117 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001118 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001120 if (name != NULL)
1121 {
1122 char_u fname_buf[FLEN_FIXED + 1];
1123 char_u *tofree = NULL;
1124 int error = FCERR_NONE;
1125 char_u *fname;
1126
1127 // May need to translate <SNR>123_ to K_SNR.
1128 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1129 if (error != FCERR_NONE)
1130 res = FAIL;
1131 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001132 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001133 vim_free(tofree);
1134 }
1135
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001136 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 {
1138 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001139 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001140 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 return FAIL;
1142 }
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001147 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1148 * TRUE.
1149 */
1150 static int
1151error_if_locked(int lock, char *error)
1152{
1153 if (lock & (VAR_LOCKED | VAR_FIXED))
1154 {
1155 emsg(_(error));
1156 return TRUE;
1157 }
1158 return FALSE;
1159}
1160
1161/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001162 * Give an error if "tv" is not a number and return FAIL.
1163 */
1164 static int
1165check_for_number(typval_T *tv)
1166{
1167 if (tv->v_type != VAR_NUMBER)
1168 {
1169 semsg(_(e_expected_str_but_got_str),
1170 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1171 return FAIL;
1172 }
1173 return OK;
1174}
1175
1176/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001177 * Store "tv" in variable "name".
1178 * This is for s: and g: variables.
1179 */
1180 static void
1181store_var(char_u *name, typval_T *tv)
1182{
1183 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001184 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001185
Bram Moolenaard877a572021-04-01 19:42:48 +02001186 if (tv->v_lock)
1187 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001188 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001189 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001190 restore_funccal();
1191}
1192
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001193/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001194 * Convert "tv" to a string.
1195 * Return FAIL if not allowed.
1196 */
1197 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001198do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001199{
1200 if (tv->v_type != VAR_STRING)
1201 {
1202 char_u *str;
1203
1204 if (is_2string_any)
1205 {
1206 switch (tv->v_type)
1207 {
1208 case VAR_SPECIAL:
1209 case VAR_BOOL:
1210 case VAR_NUMBER:
1211 case VAR_FLOAT:
1212 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001213
1214 case VAR_LIST:
1215 if (tolerant)
1216 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001217 char_u *s, *e, *p;
1218 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001219
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001220 ga_init2(&ga, sizeof(char_u *), 1);
1221
1222 // Convert to NL separated items, then
1223 // escape the items and replace the NL with
1224 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001225 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001226 if (str == NULL)
1227 return FAIL;
1228 s = str;
1229 while ((e = vim_strchr(s, '\n')) != NULL)
1230 {
1231 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001232 p = vim_strsave_fnameescape(s,
1233 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001234 if (p != NULL)
1235 {
1236 ga_concat(&ga, p);
1237 ga_concat(&ga, (char_u *)" ");
1238 vim_free(p);
1239 }
1240 s = e + 1;
1241 }
1242 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001243 clear_tv(tv);
1244 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001245 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001246 return OK;
1247 }
1248 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001249 default: to_string_error(tv->v_type);
1250 return FAIL;
1251 }
1252 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001253 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001254 clear_tv(tv);
1255 tv->v_type = VAR_STRING;
1256 tv->vval.v_string = str;
1257 }
1258 return OK;
1259}
1260
1261/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001262 * When the value of "sv" is a null list of dict, allocate it.
1263 */
1264 static void
1265allocate_if_null(typval_T *tv)
1266{
1267 switch (tv->v_type)
1268 {
1269 case VAR_LIST:
1270 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001271 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001272 break;
1273 case VAR_DICT:
1274 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001275 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001276 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001277 case VAR_BLOB:
1278 if (tv->vval.v_blob == NULL)
1279 (void)rettv_blob_alloc(tv);
1280 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001281 default:
1282 break;
1283 }
1284}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001285
Bram Moolenaare7525c52021-01-09 13:20:37 +01001286/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001287 * Return the character "str[index]" where "index" is the character index,
1288 * including composing characters.
1289 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001290 */
1291 char_u *
1292char_from_string(char_u *str, varnumber_T index)
1293{
1294 size_t nbyte = 0;
1295 varnumber_T nchar = index;
1296 size_t slen;
1297
1298 if (str == NULL)
1299 return NULL;
1300 slen = STRLEN(str);
1301
Bram Moolenaarff871402021-03-26 13:34:05 +01001302 // Do the same as for a list: a negative index counts from the end.
1303 // Optimization to check the first byte to be below 0x80 (and no composing
1304 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001305 if (index < 0)
1306 {
1307 int clen = 0;
1308
1309 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001310 {
1311 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1312 ++nbyte;
1313 else if (enc_utf8)
1314 nbyte += utfc_ptr2len(str + nbyte);
1315 else
1316 nbyte += mb_ptr2len(str + nbyte);
1317 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001318 nchar = clen + index;
1319 if (nchar < 0)
1320 // unlike list: index out of range results in empty string
1321 return NULL;
1322 }
1323
1324 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001325 {
1326 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1327 ++nbyte;
1328 else if (enc_utf8)
1329 nbyte += utfc_ptr2len(str + nbyte);
1330 else
1331 nbyte += mb_ptr2len(str + nbyte);
1332 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001333 if (nbyte >= slen)
1334 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001335 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001336}
1337
1338/*
1339 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001340 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001341 * If going over the end return "str_len".
1342 * If "idx" is negative count from the end, -1 is the last character.
1343 * When going over the start return -1.
1344 */
1345 static long
1346char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1347{
1348 varnumber_T nchar = idx;
1349 size_t nbyte = 0;
1350
1351 if (nchar >= 0)
1352 {
1353 while (nchar > 0 && nbyte < str_len)
1354 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001355 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001356 --nchar;
1357 }
1358 }
1359 else
1360 {
1361 nbyte = str_len;
1362 while (nchar < 0 && nbyte > 0)
1363 {
1364 --nbyte;
1365 nbyte -= mb_head_off(str, str + nbyte);
1366 ++nchar;
1367 }
1368 if (nchar < 0)
1369 return -1;
1370 }
1371 return (long)nbyte;
1372}
1373
1374/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001375 * Return the slice "str[first : last]" using character indexes. Composing
1376 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001377 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001378 * Return NULL when the result is empty.
1379 */
1380 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001381string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001382{
1383 long start_byte, end_byte;
1384 size_t slen;
1385
1386 if (str == NULL)
1387 return NULL;
1388 slen = STRLEN(str);
1389 start_byte = char_idx2byte(str, slen, first);
1390 if (start_byte < 0)
1391 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001392 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001393 end_byte = (long)slen;
1394 else
1395 {
1396 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001397 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001398 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001399 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001400 }
1401
1402 if (start_byte >= (long)slen || end_byte <= start_byte)
1403 return NULL;
1404 return vim_strnsave(str + start_byte, end_byte - start_byte);
1405}
1406
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001407/*
1408 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1409 * When "dfunc_idx" is negative don't give an error.
1410 * Returns NULL for an error.
1411 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001412 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001413get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001414{
1415 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001416 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1417 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001418 svar_T *sv;
1419
1420 if (sref->sref_seq != si->sn_script_seq)
1421 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001422 // The script was reloaded after the function was compiled, the
1423 // script_idx may not be valid.
1424 if (dfunc != NULL)
1425 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1426 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001427 return NULL;
1428 }
1429 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001430 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001431 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001432 if (dfunc != NULL)
1433 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001434 return NULL;
1435 }
1436 return sv;
1437}
1438
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001439/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001440 * Function passed to do_cmdline() for splitting a script joined by NL
1441 * characters.
1442 */
1443 static char_u *
1444get_split_sourceline(
1445 int c UNUSED,
1446 void *cookie,
1447 int indent UNUSED,
1448 getline_opt_T options UNUSED)
1449{
1450 source_cookie_T *sp = (source_cookie_T *)cookie;
1451 char_u *p;
1452 char_u *line;
1453
Bram Moolenaar20677332021-06-06 17:02:53 +02001454 p = vim_strchr(sp->nextline, '\n');
1455 if (p == NULL)
1456 {
1457 line = vim_strsave(sp->nextline);
1458 sp->nextline += STRLEN(sp->nextline);
1459 }
1460 else
1461 {
1462 line = vim_strnsave(sp->nextline, p - sp->nextline);
1463 sp->nextline = p + 1;
1464 }
1465 return line;
1466}
1467
1468/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 * Execute a function by "name".
1470 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001471 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 */
1473 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001474call_eval_func(
1475 char_u *name,
1476 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001477 ectx_T *ectx,
1478 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479{
Bram Moolenaared677f52020-08-12 16:38:10 +02001480 int called_emsg_before = called_emsg;
1481 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001483 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001484 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001486 dictitem_T *v;
1487
1488 v = find_var(name, NULL, FALSE);
1489 if (v == NULL)
1490 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001491 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001492 return FAIL;
1493 }
1494 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1495 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001496 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001497 return FAIL;
1498 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001499 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001501 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502}
1503
1504/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001505 * When a function reference is used, fill a partial with the information
1506 * needed, especially when it is used as a closure.
1507 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001508 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001509fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1510{
1511 pt->pt_func = ufunc;
1512 pt->pt_refcount = 1;
1513
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001514 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001515 {
1516 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1517 + ectx->ec_dfunc_idx;
1518
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001519 // The closure may need to find arguments and local variables in the
1520 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001521 pt->pt_outer.out_stack = &ectx->ec_stack;
1522 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001523 if (ectx->ec_outer_ref != NULL)
1524 {
1525 // The current context already has a context, link to that one.
1526 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1527 if (ectx->ec_outer_ref->or_partial != NULL)
1528 {
1529 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1530 ++pt->pt_outer.out_up_partial->pt_refcount;
1531 }
1532 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001533
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001534 // If this function returns and the closure is still being used, we
1535 // need to make a copy of the context (arguments and local variables).
1536 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001537 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001538 {
1539 vim_free(pt);
1540 return FAIL;
1541 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001542 // Extra variable keeps the count of closures created in the current
1543 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001544 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1545 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1546
1547 ((partial_T **)ectx->ec_funcrefs.ga_data)
1548 [ectx->ec_funcrefs.ga_len] = pt;
1549 ++pt->pt_refcount;
1550 ++ectx->ec_funcrefs.ga_len;
1551 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001552 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001553 return OK;
1554}
1555
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001556/*
1557 * Execute iptr->isn_arg.string as an Ex command.
1558 */
1559 static int
1560exec_command(isn_T *iptr)
1561{
1562 source_cookie_T cookie;
1563
1564 SOURCING_LNUM = iptr->isn_lnum;
1565 // Pass getsourceline to get an error for a missing ":end"
1566 // command.
1567 CLEAR_FIELD(cookie);
1568 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1569 if (do_cmdline(iptr->isn_arg.string,
1570 getsourceline, &cookie,
1571 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1572 || did_emsg)
1573 return FAIL;
1574 return OK;
1575}
1576
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001577// used for v_instr of typval of VAR_INSTR
1578struct instr_S {
1579 ectx_T *instr_ectx;
1580 isn_T *instr_instr;
1581};
1582
Bram Moolenaar4c137212021-04-19 16:48:48 +02001583// used for substitute_instr
1584typedef struct subs_expr_S {
1585 ectx_T *subs_ectx;
1586 isn_T *subs_instr;
1587 int subs_status;
1588} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589
1590// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001591#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
1593// Get pointer to item at the bottom of the stack, -1 is the bottom.
1594#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001595#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 +01001596
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001597// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001598#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001600// Set when calling do_debug().
1601static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001602static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001603
1604/*
1605 * When debugging lookup "name" and return the typeval.
1606 * When not found return NULL.
1607 */
1608 typval_T *
1609lookup_debug_var(char_u *name)
1610{
1611 int idx;
1612 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001613 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001614 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001615 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001616
1617 if (ectx == NULL)
1618 return NULL;
1619 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1620
1621 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001622 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001623 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001624 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1625
1626 // the variable name may be NULL when not available in this block
1627 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001628 return STACK_TV_VAR(idx);
1629 }
1630
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001631 // Go through argument names.
1632 ufunc = dfunc->df_ufunc;
1633 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1634 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1635 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1636 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1637 - varargs_off + idx);
1638 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1639 return STACK_TV(ectx->ec_frame_idx - 1);
1640
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001641 return NULL;
1642}
1643
Bram Moolenaar26a44842021-09-02 18:49:06 +02001644/*
1645 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1646 * breakpoint was set in that function or when there is any expression.
1647 */
1648 int
1649may_break_in_function(ufunc_T *ufunc)
1650{
1651 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1652}
1653
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001654 static void
1655handle_debug(isn_T *iptr, ectx_T *ectx)
1656{
1657 char_u *line;
1658 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1659 + ectx->ec_dfunc_idx)->df_ufunc;
1660 isn_T *ni;
1661 int end_lnum = iptr->isn_lnum;
1662 garray_T ga;
1663 int lnum;
1664
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001665 if (ex_nesting_level > debug_break_level)
1666 {
1667 linenr_T breakpoint;
1668
Bram Moolenaar26a44842021-09-02 18:49:06 +02001669 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001670 return;
1671
1672 // check for the next breakpoint if needed
1673 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001674 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001675 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1676 return;
1677 }
1678
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001679 SOURCING_LNUM = iptr->isn_lnum;
1680 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001681 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001682
1683 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1684 if (ni->isn_type == ISN_DEBUG
1685 || ni->isn_type == ISN_RETURN
1686 || ni->isn_type == ISN_RETURN_VOID)
1687 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001688 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001689 break;
1690 }
1691
1692 if (end_lnum > iptr->isn_lnum)
1693 {
1694 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001695 for (lnum = iptr->isn_lnum; lnum < end_lnum
1696 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001697 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001698 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001699
Bram Moolenaar303215d2021-07-07 20:10:43 +02001700 if (p == NULL)
1701 continue; // left over from continuation line
1702 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001703 if (*p == '#')
1704 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001705 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001706 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1707 if (STRNCMP(p, "def ", 4) == 0)
1708 break;
1709 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001710 line = ga_concat_strings(&ga, " ");
1711 vim_free(ga.ga_data);
1712 }
1713 else
1714 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001715
Dominique Pelle6e969552021-06-17 13:53:41 +02001716 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001717 debug_context = NULL;
1718
1719 if (end_lnum > iptr->isn_lnum)
1720 vim_free(line);
1721}
1722
Bram Moolenaar4c137212021-04-19 16:48:48 +02001723/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001724 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001725 * Returns OK, FAIL or NOTDONE (uncatchable error).
1726 */
1727 static int
1728execute_storeindex(isn_T *iptr, ectx_T *ectx)
1729{
1730 vartype_T dest_type = iptr->isn_arg.vartype;
1731 typval_T *tv;
1732 typval_T *tv_idx = STACK_TV_BOT(-2);
1733 typval_T *tv_dest = STACK_TV_BOT(-1);
1734 int status = OK;
1735
1736 // Stack contains:
1737 // -3 value to be stored
1738 // -2 index
1739 // -1 dict or list
1740 tv = STACK_TV_BOT(-3);
1741 SOURCING_LNUM = iptr->isn_lnum;
1742 if (dest_type == VAR_ANY)
1743 {
1744 dest_type = tv_dest->v_type;
1745 if (dest_type == VAR_DICT)
1746 status = do_2string(tv_idx, TRUE, FALSE);
1747 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1748 {
1749 emsg(_(e_number_expected));
1750 status = FAIL;
1751 }
1752 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001753
1754 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001755 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001756 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001757 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001758 long lidx = (long)tv_idx->vval.v_number;
1759 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001760
Bram Moolenaare08be092022-02-17 13:08:26 +00001761 if (list == NULL)
1762 {
1763 emsg(_(e_list_not_set));
1764 return FAIL;
1765 }
1766 if (lidx < 0 && list->lv_len + lidx >= 0)
1767 // negative index is relative to the end
1768 lidx = list->lv_len + lidx;
1769 if (lidx < 0 || lidx > list->lv_len)
1770 {
1771 semsg(_(e_list_index_out_of_range_nr), lidx);
1772 return FAIL;
1773 }
1774 if (lidx < list->lv_len)
1775 {
1776 listitem_T *li = list_find(list, lidx);
1777
1778 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001779 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001780 return FAIL;
1781 // overwrite existing list item
1782 clear_tv(&li->li_tv);
1783 li->li_tv = *tv;
1784 }
1785 else
1786 {
1787 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1788 return FAIL;
1789 // append to list, only fails when out of memory
1790 if (list_append_tv(list, tv) == FAIL)
1791 return NOTDONE;
1792 clear_tv(tv);
1793 }
1794 }
1795 else if (dest_type == VAR_DICT)
1796 {
1797 char_u *key = tv_idx->vval.v_string;
1798 dict_T *dict = tv_dest->vval.v_dict;
1799 dictitem_T *di;
1800
1801 SOURCING_LNUM = iptr->isn_lnum;
1802 if (dict == NULL)
1803 {
1804 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001805 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001806 }
1807 if (key == NULL)
1808 key = (char_u *)"";
1809 di = dict_find(dict, key, -1);
1810 if (di != NULL)
1811 {
1812 if (error_if_locked(di->di_tv.v_lock,
1813 e_cannot_change_dict_item))
1814 return FAIL;
1815 // overwrite existing value
1816 clear_tv(&di->di_tv);
1817 di->di_tv = *tv;
1818 }
1819 else
1820 {
1821 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1822 return FAIL;
1823 // add to dict, only fails when out of memory
1824 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1825 return NOTDONE;
1826 clear_tv(tv);
1827 }
1828 }
1829 else if (dest_type == VAR_BLOB)
1830 {
1831 long lidx = (long)tv_idx->vval.v_number;
1832 blob_T *blob = tv_dest->vval.v_blob;
1833 varnumber_T nr;
1834 int error = FALSE;
1835 int len;
1836
1837 if (blob == NULL)
1838 {
1839 emsg(_(e_blob_not_set));
1840 return FAIL;
1841 }
1842 len = blob_len(blob);
1843 if (lidx < 0 && len + lidx >= 0)
1844 // negative index is relative to the end
1845 lidx = len + lidx;
1846
1847 // Can add one byte at the end.
1848 if (lidx < 0 || lidx > len)
1849 {
1850 semsg(_(e_blob_index_out_of_range_nr), lidx);
1851 return FAIL;
1852 }
1853 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1854 return FAIL;
1855 nr = tv_get_number_chk(tv, &error);
1856 if (error)
1857 return FAIL;
1858 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001859 }
1860 else
1861 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001862 status = FAIL;
1863 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001864 }
1865 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001866
1867 clear_tv(tv_idx);
1868 clear_tv(tv_dest);
1869 ectx->ec_stack.ga_len -= 3;
1870 if (status == FAIL)
1871 {
1872 clear_tv(tv);
1873 return FAIL;
1874 }
1875 return OK;
1876}
1877
1878/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001879 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001880 */
1881 static int
1882execute_storerange(isn_T *iptr, ectx_T *ectx)
1883{
1884 typval_T *tv;
1885 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1886 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1887 typval_T *tv_dest = STACK_TV_BOT(-1);
1888 int status = OK;
1889
1890 // Stack contains:
1891 // -4 value to be stored
1892 // -3 first index or "none"
1893 // -2 second index or "none"
1894 // -1 destination list or blob
1895 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001896 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001897 if (tv_dest->v_type == VAR_LIST)
1898 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001899 long n1;
1900 long n2;
1901 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001902
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001903 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
1904 if (tv_idx2->v_type == VAR_SPECIAL
1905 && tv_idx2->vval.v_number == VVAL_NONE)
1906 n2 = list_len(tv_dest->vval.v_list) - 1;
1907 else
1908 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
1909
1910 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, FALSE);
1911 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001912 status = FAIL;
1913 else
1914 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001915 status = check_range_index_two(tv_dest->vval.v_list,
1916 &n1, li1, &n2, FALSE);
1917 if (status != FAIL)
1918 status = list_assign_range(
1919 tv_dest->vval.v_list,
1920 tv->vval.v_list,
1921 n1,
1922 n2,
1923 tv_idx2->v_type == VAR_SPECIAL,
1924 (char_u *)"=",
1925 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001926 }
1927 }
1928 else if (tv_dest->v_type == VAR_BLOB)
1929 {
1930 varnumber_T n1;
1931 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001932 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001933
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001934 n1 = tv_get_number_chk(tv_idx1, NULL);
1935 if (tv_idx2->v_type == VAR_SPECIAL
1936 && tv_idx2->vval.v_number == VVAL_NONE)
1937 n2 = blob_len(tv_dest->vval.v_blob) - 1;
1938 else
1939 n2 = tv_get_number_chk(tv_idx2, NULL);
1940 bloblen = blob_len(tv_dest->vval.v_blob);
1941
1942 if (check_blob_index(bloblen, n1, FALSE) == FAIL
1943 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001944 status = FAIL;
1945 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001946 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001947 }
1948 else
1949 {
1950 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001951 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001952 }
1953
1954 clear_tv(tv_idx1);
1955 clear_tv(tv_idx2);
1956 clear_tv(tv_dest);
1957 ectx->ec_stack.ga_len -= 4;
1958 clear_tv(tv);
1959
1960 return status;
1961}
1962
1963/*
1964 * Unlet item in list or dict variable.
1965 */
1966 static int
1967execute_unletindex(isn_T *iptr, ectx_T *ectx)
1968{
1969 typval_T *tv_idx = STACK_TV_BOT(-2);
1970 typval_T *tv_dest = STACK_TV_BOT(-1);
1971 int status = OK;
1972
1973 // Stack contains:
1974 // -2 index
1975 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00001976 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001977 if (tv_dest->v_type == VAR_DICT)
1978 {
1979 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001980 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001981 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001982 semsg(_(e_expected_str_but_got_str),
1983 vartype_name(VAR_STRING),
1984 vartype_name(tv_idx->v_type));
1985 status = FAIL;
1986 }
1987 else
1988 {
1989 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001990 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001991 dictitem_T *di = NULL;
1992
1993 if (d != NULL && value_check_lock(
1994 d->dv_lock, NULL, FALSE))
1995 status = FAIL;
1996 else
1997 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001998 if (tv_idx->v_type == VAR_STRING)
1999 {
2000 key = tv_idx->vval.v_string;
2001 if (key == NULL)
2002 key = (char_u *)"";
2003 }
2004 else
2005 {
2006 key = tv_get_string(tv_idx);
2007 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002008 if (d != NULL)
2009 di = dict_find(d, key, (int)STRLEN(key));
2010 if (di == NULL)
2011 {
2012 // NULL dict is equivalent to empty dict
2013 semsg(_(e_key_not_present_in_dictionary),
2014 key);
2015 status = FAIL;
2016 }
2017 else if (var_check_fixed(di->di_flags,
2018 NULL, FALSE)
2019 || var_check_ro(di->di_flags,
2020 NULL, FALSE))
2021 status = FAIL;
2022 else
2023 dictitem_remove(d, di);
2024 }
2025 }
2026 }
2027 else if (tv_dest->v_type == VAR_LIST)
2028 {
2029 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002030 if (check_for_number(tv_idx) == FAIL)
2031 {
2032 status = FAIL;
2033 }
2034 else
2035 {
2036 list_T *l = tv_dest->vval.v_list;
2037 long n = (long)tv_idx->vval.v_number;
2038
2039 if (l != NULL && value_check_lock(
2040 l->lv_lock, NULL, FALSE))
2041 status = FAIL;
2042 else
2043 {
2044 listitem_T *li = list_find(l, n);
2045
2046 if (li == NULL)
2047 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002048 semsg(_(e_list_index_out_of_range_nr), n);
2049 status = FAIL;
2050 }
2051 else if (value_check_lock(li->li_tv.v_lock,
2052 NULL, FALSE))
2053 status = FAIL;
2054 else
2055 listitem_remove(l, li);
2056 }
2057 }
2058 }
2059 else
2060 {
2061 status = FAIL;
2062 semsg(_(e_cannot_index_str),
2063 vartype_name(tv_dest->v_type));
2064 }
2065
2066 clear_tv(tv_idx);
2067 clear_tv(tv_dest);
2068 ectx->ec_stack.ga_len -= 2;
2069
2070 return status;
2071}
2072
2073/*
2074 * Unlet a range of items in a list variable.
2075 */
2076 static int
2077execute_unletrange(isn_T *iptr, ectx_T *ectx)
2078{
2079 // Stack contains:
2080 // -3 index1
2081 // -2 index2
2082 // -1 dict or list
2083 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2084 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2085 typval_T *tv_dest = STACK_TV_BOT(-1);
2086 int status = OK;
2087
2088 if (tv_dest->v_type == VAR_LIST)
2089 {
2090 // indexes must be a number
2091 SOURCING_LNUM = iptr->isn_lnum;
2092 if (check_for_number(tv_idx1) == FAIL
2093 || (tv_idx2->v_type != VAR_SPECIAL
2094 && check_for_number(tv_idx2) == FAIL))
2095 {
2096 status = FAIL;
2097 }
2098 else
2099 {
2100 list_T *l = tv_dest->vval.v_list;
2101 long n1 = (long)tv_idx1->vval.v_number;
2102 long n2 = tv_idx2->v_type == VAR_SPECIAL
2103 ? 0 : (long)tv_idx2->vval.v_number;
2104 listitem_T *li;
2105
2106 li = list_find_index(l, &n1);
2107 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002108 {
2109 semsg(_(e_list_index_out_of_range_nr),
2110 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002111 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002112 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002113 else
2114 {
2115 if (n1 < 0)
2116 n1 = list_idx_of_item(l, li);
2117 if (n2 < 0)
2118 {
2119 listitem_T *li2 = list_find(l, n2);
2120
2121 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002122 {
2123 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002124 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002125 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002126 else
2127 n2 = list_idx_of_item(l, li2);
2128 }
2129 if (status != FAIL
2130 && tv_idx2->v_type != VAR_SPECIAL
2131 && n2 < n1)
2132 {
2133 semsg(_(e_list_index_out_of_range_nr), n2);
2134 status = FAIL;
2135 }
2136 if (status != FAIL
2137 && list_unlet_range(l, li, NULL, n1,
2138 tv_idx2->v_type != VAR_SPECIAL, n2)
2139 == FAIL)
2140 status = FAIL;
2141 }
2142 }
2143 }
2144 else
2145 {
2146 status = FAIL;
2147 SOURCING_LNUM = iptr->isn_lnum;
2148 semsg(_(e_cannot_index_str),
2149 vartype_name(tv_dest->v_type));
2150 }
2151
2152 clear_tv(tv_idx1);
2153 clear_tv(tv_idx2);
2154 clear_tv(tv_dest);
2155 ectx->ec_stack.ga_len -= 3;
2156
2157 return status;
2158}
2159
2160/*
2161 * Top of a for loop.
2162 */
2163 static int
2164execute_for(isn_T *iptr, ectx_T *ectx)
2165{
2166 typval_T *tv;
2167 typval_T *ltv = STACK_TV_BOT(-1);
2168 typval_T *idxtv =
2169 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2170
2171 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2172 return FAIL;
2173 if (ltv->v_type == VAR_LIST)
2174 {
2175 list_T *list = ltv->vval.v_list;
2176
2177 // push the next item from the list
2178 ++idxtv->vval.v_number;
2179 if (list == NULL
2180 || idxtv->vval.v_number >= list->lv_len)
2181 {
2182 // past the end of the list, jump to "endfor"
2183 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2184 may_restore_cmdmod(&ectx->ec_funclocal);
2185 }
2186 else if (list->lv_first == &range_list_item)
2187 {
2188 // non-materialized range() list
2189 tv = STACK_TV_BOT(0);
2190 tv->v_type = VAR_NUMBER;
2191 tv->v_lock = 0;
2192 tv->vval.v_number = list_find_nr(
2193 list, idxtv->vval.v_number, NULL);
2194 ++ectx->ec_stack.ga_len;
2195 }
2196 else
2197 {
2198 listitem_T *li = list_find(list,
2199 idxtv->vval.v_number);
2200
2201 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2202 ++ectx->ec_stack.ga_len;
2203 }
2204 }
2205 else if (ltv->v_type == VAR_STRING)
2206 {
2207 char_u *str = ltv->vval.v_string;
2208
2209 // The index is for the last byte of the previous
2210 // character.
2211 ++idxtv->vval.v_number;
2212 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2213 {
2214 // past the end of the string, jump to "endfor"
2215 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2216 may_restore_cmdmod(&ectx->ec_funclocal);
2217 }
2218 else
2219 {
2220 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2221
2222 // Push the next character from the string.
2223 tv = STACK_TV_BOT(0);
2224 tv->v_type = VAR_STRING;
2225 tv->vval.v_string = vim_strnsave(
2226 str + idxtv->vval.v_number, clen);
2227 ++ectx->ec_stack.ga_len;
2228 idxtv->vval.v_number += clen - 1;
2229 }
2230 }
2231 else if (ltv->v_type == VAR_BLOB)
2232 {
2233 blob_T *blob = ltv->vval.v_blob;
2234
2235 // When we get here the first time make a copy of the
2236 // blob, so that the iteration still works when it is
2237 // changed.
2238 if (idxtv->vval.v_number == -1 && blob != NULL)
2239 {
2240 blob_copy(blob, ltv);
2241 blob_unref(blob);
2242 blob = ltv->vval.v_blob;
2243 }
2244
2245 // The index is for the previous byte.
2246 ++idxtv->vval.v_number;
2247 if (blob == NULL
2248 || idxtv->vval.v_number >= blob_len(blob))
2249 {
2250 // past the end of the blob, jump to "endfor"
2251 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2252 may_restore_cmdmod(&ectx->ec_funclocal);
2253 }
2254 else
2255 {
2256 // Push the next byte from the blob.
2257 tv = STACK_TV_BOT(0);
2258 tv->v_type = VAR_NUMBER;
2259 tv->vval.v_number = blob_get(blob,
2260 idxtv->vval.v_number);
2261 ++ectx->ec_stack.ga_len;
2262 }
2263 }
2264 else
2265 {
2266 semsg(_(e_for_loop_on_str_not_supported),
2267 vartype_name(ltv->v_type));
2268 return FAIL;
2269 }
2270 return OK;
2271}
2272
2273/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002274 * Load instruction for w:/b:/g:/t: variable.
2275 * "isn_type" is used instead of "iptr->isn_type".
2276 */
2277 static int
2278load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2279{
2280 dictitem_T *di = NULL;
2281 hashtab_T *ht = NULL;
2282 char namespace;
2283
2284 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2285 return NOTDONE;
2286 switch (isn_type)
2287 {
2288 case ISN_LOADG:
2289 ht = get_globvar_ht();
2290 namespace = 'g';
2291 break;
2292 case ISN_LOADB:
2293 ht = &curbuf->b_vars->dv_hashtab;
2294 namespace = 'b';
2295 break;
2296 case ISN_LOADW:
2297 ht = &curwin->w_vars->dv_hashtab;
2298 namespace = 'w';
2299 break;
2300 case ISN_LOADT:
2301 ht = &curtab->tp_vars->dv_hashtab;
2302 namespace = 't';
2303 break;
2304 default: // Cannot reach here
2305 return NOTDONE;
2306 }
2307 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2308
Bram Moolenaar06b77222022-01-25 15:51:56 +00002309 if (di == NULL)
2310 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002311 if (isn_type == ISN_LOADG)
2312 {
2313 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2314
2315 // g:Something could be a function
2316 if (ufunc != NULL)
2317 {
2318 typval_T *tv = STACK_TV_BOT(0);
2319
2320 ++ectx->ec_stack.ga_len;
2321 tv->v_type = VAR_FUNC;
2322 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2323 if (tv->vval.v_string == NULL)
2324 return FAIL;
2325 STRCPY(tv->vval.v_string, "g:");
2326 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2327 return OK;
2328 }
2329 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002330 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002331 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002332 // no check if the item exists in the script but
2333 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002334 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002335 else
2336 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002337 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002338 return FAIL;
2339 }
2340 else
2341 {
2342 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2343 ++ectx->ec_stack.ga_len;
2344 }
2345 return OK;
2346}
2347
2348/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002349 * Execute instructions in execution context "ectx".
2350 * Return OK or FAIL;
2351 */
2352 static int
2353exec_instructions(ectx_T *ectx)
2354{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002355 int ret = FAIL;
2356 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002357 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002358
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002359 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002360 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002361
Bram Moolenaarff652882021-05-16 15:24:49 +02002362 // Only catch exceptions in this instruction list.
2363 ectx->ec_trylevel_at_start = trylevel;
2364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 for (;;)
2366 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002367 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002369 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002370
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002371 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002372 {
2373 line_breakcheck();
2374 breakcheck_count = 0;
2375 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002376 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002377 {
2378 // Turn CTRL-C into an exception.
2379 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002380 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002381 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002382 did_throw = TRUE;
2383 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002385 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002386 {
2387 // Turn an error message into an exception.
2388 did_emsg = FALSE;
2389 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002390 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002391 did_throw = TRUE;
2392 *msg_list = NULL;
2393 }
2394
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002395 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002397 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002398 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002399 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400
2401 // An exception jumps to the first catch, finally, or returns from
2402 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002403 while (index > 0)
2404 {
2405 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002406 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002407 break;
2408 // In the catch and finally block of this try we have to go up
2409 // one level.
2410 --index;
2411 trycmd = NULL;
2412 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002413 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002415 if (trycmd->tcd_in_catch)
2416 {
2417 // exception inside ":catch", jump to ":finally" once
2418 ectx->ec_iidx = trycmd->tcd_finally_idx;
2419 trycmd->tcd_finally_idx = 0;
2420 }
2421 else
2422 // jump to first ":catch"
2423 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002424 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002425 did_throw = FALSE; // don't come back here until :endtry
2426 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 }
2428 else
2429 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002430 // Not inside try or need to return from current functions.
2431 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002432 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002433 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002434 tv = STACK_TV_BOT(0);
2435 tv->v_type = VAR_NUMBER;
2436 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002437 ++ectx->ec_stack.ga_len;
2438 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002440 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002441 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002442 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002443 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 goto done;
2445 }
2446
Bram Moolenaar4c137212021-04-19 16:48:48 +02002447 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002448 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 }
2450 continue;
2451 }
2452
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002453 /*
2454 * Big switch on the instruction. Most compilers will be turning this
2455 * into an efficient lookup table, since the "case" values are an enum
2456 * with sequential numbers. It may look ugly, but it should be the
2457 * most efficient way.
2458 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002459 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 switch (iptr->isn_type)
2461 {
2462 // execute Ex command line
2463 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002464 if (exec_command(iptr) == FAIL)
2465 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 break;
2467
Bram Moolenaar20677332021-06-06 17:02:53 +02002468 // execute Ex command line split at NL characters.
2469 case ISN_EXEC_SPLIT:
2470 {
2471 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002472 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002473
2474 SOURCING_LNUM = iptr->isn_lnum;
2475 CLEAR_FIELD(cookie);
2476 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2477 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002478 line = get_split_sourceline(0, &cookie, 0, 0);
2479 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002480 get_split_sourceline, &cookie,
2481 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2482 == FAIL
2483 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002484 {
2485 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002486 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002487 }
2488 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002489 }
2490 break;
2491
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002492 // execute Ex command line that is only a range
2493 case ISN_EXECRANGE:
2494 {
2495 exarg_T ea;
2496 char *error = NULL;
2497
2498 CLEAR_FIELD(ea);
2499 ea.cmdidx = CMD_SIZE;
2500 ea.addr_type = ADDR_LINES;
2501 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002502 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002503 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002504 if (ea.cmd == NULL)
2505 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002506 // error is always NULL when using ADDR_LINES
2507 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002508 if (error != NULL)
2509 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002510 emsg(error);
2511 goto on_error;
2512 }
2513 }
2514 break;
2515
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002516 // Evaluate an expression with legacy syntax, push it onto the
2517 // stack.
2518 case ISN_LEGACY_EVAL:
2519 {
2520 char_u *arg = iptr->isn_arg.string;
2521 int res;
2522 int save_flags = cmdmod.cmod_flags;
2523
Bram Moolenaar35578162021-08-02 19:10:38 +02002524 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002525 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002526 tv = STACK_TV_BOT(0);
2527 init_tv(tv);
2528 cmdmod.cmod_flags |= CMOD_LEGACY;
2529 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2530 cmdmod.cmod_flags = save_flags;
2531 if (res == FAIL)
2532 goto on_error;
2533 ++ectx->ec_stack.ga_len;
2534 }
2535 break;
2536
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002537 // push typeval VAR_INSTR with instructions to be executed
2538 case ISN_INSTR:
2539 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002540 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002541 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002542 tv = STACK_TV_BOT(0);
2543 tv->vval.v_instr = ALLOC_ONE(instr_T);
2544 if (tv->vval.v_instr == NULL)
2545 goto on_error;
2546 ++ectx->ec_stack.ga_len;
2547
2548 tv->v_type = VAR_INSTR;
2549 tv->vval.v_instr->instr_ectx = ectx;
2550 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2551 }
2552 break;
2553
Bram Moolenaar4c137212021-04-19 16:48:48 +02002554 // execute :substitute with an expression
2555 case ISN_SUBSTITUTE:
2556 {
2557 subs_T *subs = &iptr->isn_arg.subs;
2558 source_cookie_T cookie;
2559 struct subs_expr_S *save_instr = substitute_instr;
2560 struct subs_expr_S subs_instr;
2561 int res;
2562
2563 subs_instr.subs_ectx = ectx;
2564 subs_instr.subs_instr = subs->subs_instr;
2565 subs_instr.subs_status = OK;
2566 substitute_instr = &subs_instr;
2567
2568 SOURCING_LNUM = iptr->isn_lnum;
2569 // This is very much like ISN_EXEC
2570 CLEAR_FIELD(cookie);
2571 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2572 res = do_cmdline(subs->subs_cmd,
2573 getsourceline, &cookie,
2574 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2575 substitute_instr = save_instr;
2576
2577 if (res == FAIL || did_emsg
2578 || subs_instr.subs_status == FAIL)
2579 goto on_error;
2580 }
2581 break;
2582
2583 case ISN_FINISH:
2584 goto done;
2585
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002586 case ISN_REDIRSTART:
2587 // create a dummy entry for var_redir_str()
2588 if (alloc_redir_lval() == FAIL)
2589 goto on_error;
2590
2591 // The output is stored in growarray "redir_ga" until
2592 // redirection ends.
2593 init_redir_ga();
2594 redir_vname = 1;
2595 break;
2596
2597 case ISN_REDIREND:
2598 {
2599 char_u *res = get_clear_redir_ga();
2600
2601 // End redirection, put redirected text on the stack.
2602 clear_redir_lval();
2603 redir_vname = 0;
2604
Bram Moolenaar35578162021-08-02 19:10:38 +02002605 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002606 {
2607 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002608 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002609 }
2610 tv = STACK_TV_BOT(0);
2611 tv->v_type = VAR_STRING;
2612 tv->vval.v_string = res;
2613 ++ectx->ec_stack.ga_len;
2614 }
2615 break;
2616
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002617 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002618#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002619 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2620 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002621#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002622 break;
2623
2624 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002625#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002626 {
2627 exarg_T ea;
2628 int res;
2629
2630 CLEAR_FIELD(ea);
2631 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2632 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2633 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2634 --ectx->ec_stack.ga_len;
2635 tv = STACK_TV_BOT(0);
2636 res = cexpr_core(&ea, tv);
2637 clear_tv(tv);
2638 if (res == FAIL)
2639 goto on_error;
2640 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002641#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002642 break;
2643
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002644 // execute Ex command from pieces on the stack
2645 case ISN_EXECCONCAT:
2646 {
2647 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002648 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002649 int pass;
2650 int i;
2651 char_u *cmd = NULL;
2652 char_u *str;
2653
2654 for (pass = 1; pass <= 2; ++pass)
2655 {
2656 for (i = 0; i < count; ++i)
2657 {
2658 tv = STACK_TV_BOT(i - count);
2659 str = tv->vval.v_string;
2660 if (str != NULL && *str != NUL)
2661 {
2662 if (pass == 2)
2663 STRCPY(cmd + len, str);
2664 len += STRLEN(str);
2665 }
2666 if (pass == 2)
2667 clear_tv(tv);
2668 }
2669 if (pass == 1)
2670 {
2671 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002672 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002673 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002674 len = 0;
2675 }
2676 }
2677
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002678 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002679 do_cmdline_cmd(cmd);
2680 vim_free(cmd);
2681 }
2682 break;
2683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 // execute :echo {string} ...
2685 case ISN_ECHO:
2686 {
2687 int count = iptr->isn_arg.echo.echo_count;
2688 int atstart = TRUE;
2689 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002690 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691
2692 for (idx = 0; idx < count; ++idx)
2693 {
2694 tv = STACK_TV_BOT(idx - count);
2695 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2696 &atstart, &needclr);
2697 clear_tv(tv);
2698 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002699 if (needclr)
2700 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002701 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 }
2703 break;
2704
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002705 // :execute {string} ...
2706 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002707 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002708 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002709 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002710 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002711 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002712 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002713 {
2714 int count = iptr->isn_arg.number;
2715 garray_T ga;
2716 char_u buf[NUMBUFLEN];
2717 char_u *p;
2718 int len;
2719 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002720 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002721
2722 ga_init2(&ga, 1, 80);
2723 for (idx = 0; idx < count; ++idx)
2724 {
2725 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002726 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002727 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002728 if (tv->v_type == VAR_CHANNEL
2729 || tv->v_type == VAR_JOB)
2730 {
2731 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002732 semsg(_(e_using_invalid_value_as_string_str),
2733 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002734 break;
2735 }
2736 else
2737 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002738 }
2739 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002740 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002741
2742 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002743 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002744 failed = TRUE;
2745 else
2746 {
2747 if (ga.ga_len > 0)
2748 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2749 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2750 ga.ga_len += len;
2751 }
2752 clear_tv(tv);
2753 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002754 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002755 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002756 {
2757 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002758 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002759 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002760
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002761 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002762 {
2763 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002764 {
2765 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002766 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002767 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002768 {
2769 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002770 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002771 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002772 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002773 else
2774 {
2775 msg_sb_eol();
2776 if (iptr->isn_type == ISN_ECHOMSG)
2777 {
2778 msg_attr(ga.ga_data, echo_attr);
2779 out_flush();
2780 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002781 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2782 {
2783 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2784 TRUE);
2785 ui_write((char_u *)"\r\n", 2, TRUE);
2786 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002787 else
2788 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002789 SOURCING_LNUM = iptr->isn_lnum;
2790 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002791 }
2792 }
2793 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002794 ga_clear(&ga);
2795 }
2796 break;
2797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 // load local variable or argument
2799 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002800 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002801 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002803 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 break;
2805
2806 // load v: variable
2807 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002808 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002809 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002811 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 break;
2813
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002814 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 case ISN_LOADSCRIPT:
2816 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002817 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 svar_T *sv;
2819
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002820 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002821 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002822 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002823 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002824 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002825 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002827 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 }
2829 break;
2830
2831 // load s: variable in old script
2832 case ISN_LOADS:
2833 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002834 hashtab_T *ht = &SCRIPT_VARS(
2835 iptr->isn_arg.loadstore.ls_sid);
2836 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 if (di == NULL)
2840 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002841 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002842 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002843 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 }
2845 else
2846 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002847 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002848 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002850 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 }
2852 }
2853 break;
2854
Bram Moolenaard3aac292020-04-19 14:32:17 +02002855 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002857 case ISN_LOADB:
2858 case ISN_LOADW:
2859 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002861 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002862
Bram Moolenaar06b77222022-01-25 15:51:56 +00002863 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002864 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002865 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002866 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 break;
2870
Bram Moolenaar03290b82020-12-19 16:30:44 +01002871 // load autoload variable
2872 case ISN_LOADAUTO:
2873 {
2874 char_u *name = iptr->isn_arg.string;
2875
Bram Moolenaar35578162021-08-02 19:10:38 +02002876 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002877 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002878 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002879 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002880 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002881 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002882 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002883 }
2884 break;
2885
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002886 // load g:/b:/w:/t: namespace
2887 case ISN_LOADGDICT:
2888 case ISN_LOADBDICT:
2889 case ISN_LOADWDICT:
2890 case ISN_LOADTDICT:
2891 {
2892 dict_T *d = NULL;
2893
2894 switch (iptr->isn_type)
2895 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002896 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2897 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2898 case ISN_LOADWDICT: d = curwin->w_vars; break;
2899 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002900 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002901 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002902 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002903 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002904 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002905 tv = STACK_TV_BOT(0);
2906 tv->v_type = VAR_DICT;
2907 tv->v_lock = 0;
2908 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002909 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002910 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002911 }
2912 break;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 // load &option
2915 case ISN_LOADOPT:
2916 {
2917 typval_T optval;
2918 char_u *name = iptr->isn_arg.string;
2919
Bram Moolenaara8c17702020-04-01 21:17:24 +02002920 // This is not expected to fail, name is checked during
2921 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002922 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002923 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002924 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002925 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002927 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 }
2929 break;
2930
2931 // load $ENV
2932 case ISN_LOADENV:
2933 {
2934 typval_T optval;
2935 char_u *name = iptr->isn_arg.string;
2936
Bram Moolenaar35578162021-08-02 19:10:38 +02002937 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002938 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002939 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002940 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002942 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 }
2944 break;
2945
2946 // load @register
2947 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002948 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002949 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 tv = STACK_TV_BOT(0);
2951 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002952 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002953 // This may result in NULL, which should be equivalent to an
2954 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 tv->vval.v_string = get_reg_contents(
2956 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002957 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 break;
2959
2960 // store local variable
2961 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002962 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 tv = STACK_TV_VAR(iptr->isn_arg.number);
2964 clear_tv(tv);
2965 *tv = *STACK_TV_BOT(0);
2966 break;
2967
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002968 // store s: variable in old script
2969 case ISN_STORES:
2970 {
2971 hashtab_T *ht = &SCRIPT_VARS(
2972 iptr->isn_arg.loadstore.ls_sid);
2973 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002974 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002975
Bram Moolenaar4c137212021-04-19 16:48:48 +02002976 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002977 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002978 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002979 else
2980 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002981 SOURCING_LNUM = iptr->isn_lnum;
2982 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002983 {
2984 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002985 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002986 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002987 clear_tv(&di->di_tv);
2988 di->di_tv = *STACK_TV_BOT(0);
2989 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002990 }
2991 break;
2992
2993 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 case ISN_STORESCRIPT:
2995 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002996 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2997 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002999 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003000 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003001 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003002 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003003
3004 // "const" and "final" are checked at compile time, locking
3005 // the value needs to be checked here.
3006 SOURCING_LNUM = iptr->isn_lnum;
3007 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003008 {
3009 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003010 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003011 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 clear_tv(sv->sv_tv);
3014 *sv->sv_tv = *STACK_TV_BOT(0);
3015 }
3016 break;
3017
3018 // store option
3019 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003020 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003022 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3023 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 long n = 0;
3025 char_u *s = NULL;
3026 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003027 char_u numbuf[NUMBUFLEN];
3028 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029
Bram Moolenaar4c137212021-04-19 16:48:48 +02003030 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 tv = STACK_TV_BOT(0);
3032 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003033 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003035 if (s == NULL)
3036 s = (char_u *)"";
3037 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003038 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3039 {
3040 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003041 // If the option can be set to a function reference or
3042 // a lambda and the passed value is a function
3043 // reference, then convert it to the name (string) of
3044 // the function reference.
3045 s = tv2string(tv, &tofree, numbuf, 0);
3046 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003047 {
3048 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003049 goto on_error;
3050 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003053 // must be VAR_NUMBER, CHECKTYPE makes sure
3054 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003055 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003056 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003057 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 if (msg != NULL)
3059 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003062 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 }
3065 break;
3066
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003067 // store $ENV
3068 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003069 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003070 tv = STACK_TV_BOT(0);
3071 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3072 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003073 break;
3074
3075 // store @r
3076 case ISN_STOREREG:
3077 {
3078 int reg = iptr->isn_arg.number;
3079
Bram Moolenaar4c137212021-04-19 16:48:48 +02003080 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003081 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003082 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003083 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003084 }
3085 break;
3086
3087 // store v: variable
3088 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003089 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003090 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3091 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003092 // should not happen, type is checked when compiling
3093 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003094 break;
3095
Bram Moolenaard3aac292020-04-19 14:32:17 +02003096 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003098 case ISN_STOREB:
3099 case ISN_STOREW:
3100 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003102 dictitem_T *di;
3103 hashtab_T *ht;
3104 char_u *name = iptr->isn_arg.string + 2;
3105
Bram Moolenaard3aac292020-04-19 14:32:17 +02003106 switch (iptr->isn_type)
3107 {
3108 case ISN_STOREG:
3109 ht = get_globvar_ht();
3110 break;
3111 case ISN_STOREB:
3112 ht = &curbuf->b_vars->dv_hashtab;
3113 break;
3114 case ISN_STOREW:
3115 ht = &curwin->w_vars->dv_hashtab;
3116 break;
3117 case ISN_STORET:
3118 ht = &curtab->tp_vars->dv_hashtab;
3119 break;
3120 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003121 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123
Bram Moolenaar4c137212021-04-19 16:48:48 +02003124 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003125 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003127 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 else
3129 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003130 SOURCING_LNUM = iptr->isn_lnum;
3131 if (var_check_permission(di, name) == FAIL)
3132 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 clear_tv(&di->di_tv);
3134 di->di_tv = *STACK_TV_BOT(0);
3135 }
3136 }
3137 break;
3138
Bram Moolenaar03290b82020-12-19 16:30:44 +01003139 // store an autoload variable
3140 case ISN_STOREAUTO:
3141 SOURCING_LNUM = iptr->isn_lnum;
3142 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3143 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003145 break;
3146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 // store number in local variable
3148 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003149 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 clear_tv(tv);
3151 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003152 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 break;
3154
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003155 // store value in list or dict variable
3156 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003157 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003158 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003159
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003160 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003161 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003162 if (res == NOTDONE)
3163 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003164 }
3165 break;
3166
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003167 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003168 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003169 if (execute_storerange(iptr, ectx) == FAIL)
3170 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003171 break;
3172
Bram Moolenaar0186e582021-01-10 18:33:11 +01003173 // load or store variable or argument from outer scope
3174 case ISN_LOADOUTER:
3175 case ISN_STOREOUTER:
3176 {
3177 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003178 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3179 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003180
3181 while (depth > 1 && outer != NULL)
3182 {
3183 outer = outer->out_up;
3184 --depth;
3185 }
3186 if (outer == NULL)
3187 {
3188 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003189 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3190 || ectx->ec_outer_ref == NULL)
3191 // Possibly :def function called from legacy
3192 // context.
3193 emsg(_(e_closure_called_from_invalid_context));
3194 else
3195 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003196 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003197 }
3198 tv = ((typval_T *)outer->out_stack->ga_data)
3199 + outer->out_frame_idx + STACK_FRAME_SIZE
3200 + iptr->isn_arg.outer.outer_idx;
3201 if (iptr->isn_type == ISN_LOADOUTER)
3202 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003203 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003204 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003205 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003206 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003207 }
3208 else
3209 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003210 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003211 clear_tv(tv);
3212 *tv = *STACK_TV_BOT(0);
3213 }
3214 }
3215 break;
3216
Bram Moolenaar752fc692021-01-04 21:57:11 +01003217 // unlet item in list or dict variable
3218 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003219 if (execute_unletindex(iptr, ectx) == FAIL)
3220 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003221 break;
3222
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003223 // unlet range of items in list variable
3224 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003225 if (execute_unletrange(iptr, ectx) == FAIL)
3226 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003227 break;
3228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 // push constant
3230 case ISN_PUSHNR:
3231 case ISN_PUSHBOOL:
3232 case ISN_PUSHSPEC:
3233 case ISN_PUSHF:
3234 case ISN_PUSHS:
3235 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003236 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003237 case ISN_PUSHCHANNEL:
3238 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003239 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003240 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003242 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003243 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 switch (iptr->isn_type)
3245 {
3246 case ISN_PUSHNR:
3247 tv->v_type = VAR_NUMBER;
3248 tv->vval.v_number = iptr->isn_arg.number;
3249 break;
3250 case ISN_PUSHBOOL:
3251 tv->v_type = VAR_BOOL;
3252 tv->vval.v_number = iptr->isn_arg.number;
3253 break;
3254 case ISN_PUSHSPEC:
3255 tv->v_type = VAR_SPECIAL;
3256 tv->vval.v_number = iptr->isn_arg.number;
3257 break;
3258#ifdef FEAT_FLOAT
3259 case ISN_PUSHF:
3260 tv->v_type = VAR_FLOAT;
3261 tv->vval.v_float = iptr->isn_arg.fnumber;
3262 break;
3263#endif
3264 case ISN_PUSHBLOB:
3265 blob_copy(iptr->isn_arg.blob, tv);
3266 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003267 case ISN_PUSHFUNC:
3268 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003269 if (iptr->isn_arg.string == NULL)
3270 tv->vval.v_string = NULL;
3271 else
3272 tv->vval.v_string =
3273 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003274 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003275 case ISN_PUSHCHANNEL:
3276#ifdef FEAT_JOB_CHANNEL
3277 tv->v_type = VAR_CHANNEL;
3278 tv->vval.v_channel = iptr->isn_arg.channel;
3279 if (tv->vval.v_channel != NULL)
3280 ++tv->vval.v_channel->ch_refcount;
3281#endif
3282 break;
3283 case ISN_PUSHJOB:
3284#ifdef FEAT_JOB_CHANNEL
3285 tv->v_type = VAR_JOB;
3286 tv->vval.v_job = iptr->isn_arg.job;
3287 if (tv->vval.v_job != NULL)
3288 ++tv->vval.v_job->jv_refcount;
3289#endif
3290 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 default:
3292 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003293 tv->vval.v_string = iptr->isn_arg.string == NULL
3294 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 }
3296 break;
3297
Bram Moolenaar06b77222022-01-25 15:51:56 +00003298 case ISN_AUTOLOAD:
3299 {
3300 char_u *name = iptr->isn_arg.string;
3301
3302 (void)script_autoload(name, FALSE);
3303 if (find_func(name, TRUE))
3304 {
3305 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3306 goto theend;
3307 tv = STACK_TV_BOT(0);
3308 tv->v_lock = 0;
3309 ++ectx->ec_stack.ga_len;
3310 tv->v_type = VAR_FUNC;
3311 tv->vval.v_string = vim_strsave(name);
3312 }
3313 else
3314 {
3315 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3316
3317 if (res == NOTDONE)
3318 goto theend;
3319 if (res == FAIL)
3320 goto on_error;
3321 }
3322 }
3323 break;
3324
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003325 case ISN_UNLET:
3326 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3327 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003328 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003329 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003330 case ISN_UNLETENV:
3331 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3332 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003333
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003334 case ISN_LOCKUNLOCK:
3335 {
3336 typval_T *lval_root_save = lval_root;
3337 int res;
3338
3339 // Stack has the local variable, argument the whole :lock
3340 // or :unlock command, like ISN_EXEC.
3341 --ectx->ec_stack.ga_len;
3342 lval_root = STACK_TV_BOT(0);
3343 res = exec_command(iptr);
3344 clear_tv(lval_root);
3345 lval_root = lval_root_save;
3346 if (res == FAIL)
3347 goto on_error;
3348 }
3349 break;
3350
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003351 case ISN_LOCKCONST:
3352 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3353 break;
3354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 // create a list from items on the stack; uses a single allocation
3356 // for the list header and the items
3357 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003358 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003359 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 break;
3361
3362 // create a dict from items on the stack
3363 case ISN_NEWDICT:
3364 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003365 int count = iptr->isn_arg.number;
3366 dict_T *dict = dict_alloc();
3367 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003368 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003369 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003371 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003372 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 for (idx = 0; idx < count; ++idx)
3374 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003375 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003377 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003378 key = tv->vval.v_string == NULL
3379 ? (char_u *)"" : tv->vval.v_string;
3380 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003381 if (item != NULL)
3382 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003383 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar74409f62022-01-01 15:58:22 +00003384 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003385 dict_unref(dict);
3386 goto on_error;
3387 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003388 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003390 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003391 {
3392 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003393 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3396 item->di_tv.v_lock = 0;
3397 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003398 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003399 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003400 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003401 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003402 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 }
3404
3405 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003406 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003407 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003408 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003410 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 tv = STACK_TV_BOT(-1);
3412 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003413 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 tv->vval.v_dict = dict;
3415 ++dict->dv_refcount;
3416 }
3417 break;
3418
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003419 // create a partial with NULL value
3420 case ISN_NEWPARTIAL:
3421 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3422 goto theend;
3423 ++ectx->ec_stack.ga_len;
3424 tv = STACK_TV_BOT(-1);
3425 tv->v_type = VAR_PARTIAL;
3426 tv->v_lock = 0;
3427 tv->vval.v_partial = NULL;
3428 break;
3429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 // call a :def function
3431 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003432 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003433 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3434 NULL,
3435 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003436 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003437 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 break;
3439
3440 // call a builtin function
3441 case ISN_BCALL:
3442 SOURCING_LNUM = iptr->isn_lnum;
3443 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3444 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003445 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003446 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 break;
3448
3449 // call a funcref or partial
3450 case ISN_PCALL:
3451 {
3452 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3453 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003454 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455
3456 SOURCING_LNUM = iptr->isn_lnum;
3457 if (pfunc->cpf_top)
3458 {
3459 // funcref is above the arguments
3460 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3461 }
3462 else
3463 {
3464 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003465 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003466 partial_tv = *STACK_TV_BOT(0);
3467 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003469 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003470 if (tv == &partial_tv)
3471 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003473 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 }
3475 break;
3476
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003477 case ISN_PCALL_END:
3478 // PCALL finished, arguments have been consumed and replaced by
3479 // the return value. Now clear the funcref from the stack,
3480 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003481 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003482 clear_tv(STACK_TV_BOT(-1));
3483 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3484 break;
3485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 // call a user defined function or funcref/partial
3487 case ISN_UCALL:
3488 {
3489 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3490
3491 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003492 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003493 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003494 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 }
3496 break;
3497
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003498 // return from a :def function call without a value
3499 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003500 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003501 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003502 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003503 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003504 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003505 tv->vval.v_number = 0;
3506 tv->v_lock = 0;
3507 // FALLTHROUGH
3508
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003509 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 case ISN_RETURN:
3511 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003513 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003514
3515 if (trystack->ga_len > 0)
3516 trycmd = ((trycmd_T *)trystack->ga_data)
3517 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003518 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003519 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003521 // jump to ":finally" or ":endtry"
3522 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003523 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003524 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003525 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 trycmd->tcd_return = TRUE;
3527 }
3528 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003529 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 }
3531 break;
3532
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003533 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 case ISN_FUNCREF:
3535 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003536 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003537 ufunc_T *ufunc;
3538 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003541 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003542 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003543 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003544 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003545 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003546 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003547 if (funcref->fr_func_name == NULL)
3548 {
3549 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3550 + funcref->fr_dfunc_idx;
3551
3552 ufunc = pt_dfunc->df_ufunc;
3553 }
3554 else
3555 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003556 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003557 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003558 if (ufunc == NULL)
3559 {
3560 SOURCING_LNUM = iptr->isn_lnum;
3561 iemsg("ufunc unexpectedly NULL for FUNCREF");
3562 goto theend;
3563 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003564 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003565 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 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 tv->vval.v_partial = pt;
3569 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003570 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 }
3572 break;
3573
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003574 // Create a global function from a lambda.
3575 case ISN_NEWFUNC:
3576 {
3577 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3578
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003579 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003580 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003581 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003582 }
3583 break;
3584
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003585 // List functions
3586 case ISN_DEF:
3587 if (iptr->isn_arg.string == NULL)
3588 list_functions(NULL);
3589 else
3590 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003591 exarg_T ea;
3592 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003593
3594 CLEAR_FIELD(ea);
3595 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003596 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3597 define_function(&ea, NULL, &lines_to_free);
3598 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003599 }
3600 break;
3601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 // jump if a condition is met
3603 case ISN_JUMP:
3604 {
3605 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003606 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 int jump = TRUE;
3608
3609 if (when != JUMP_ALWAYS)
3610 {
3611 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003612 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003613 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003614 || when == JUMP_IF_COND_TRUE)
3615 {
3616 SOURCING_LNUM = iptr->isn_lnum;
3617 jump = tv_get_bool_chk(tv, &error);
3618 if (error)
3619 goto on_error;
3620 }
3621 else
3622 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003624 || when == JUMP_AND_KEEP_IF_FALSE
3625 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003627 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 {
3629 // drop the value from the stack
3630 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003631 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 }
3633 }
3634 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003635 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 }
3637 break;
3638
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003639 // Jump if an argument with a default value was already set and not
3640 // v:none.
3641 case ISN_JUMP_IF_ARG_SET:
3642 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3643 if (tv->v_type != VAR_UNKNOWN
3644 && !(tv->v_type == VAR_SPECIAL
3645 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003646 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003647 break;
3648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 // top of a for loop
3650 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003651 if (execute_for(iptr, ectx) == FAIL)
3652 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 break;
3654
3655 // start of ":try" block
3656 case ISN_TRY:
3657 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003658 trycmd_T *trycmd = NULL;
3659
Bram Moolenaar35578162021-08-02 19:10:38 +02003660 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003661 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003662 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3663 + ectx->ec_trystack.ga_len;
3664 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003666 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003667 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3668 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003669 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003670 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003671 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003672 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003673 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003674 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 }
3676 break;
3677
3678 case ISN_PUSHEXC:
3679 if (current_exception == NULL)
3680 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003681 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003683 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003685 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003686 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003688 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003690 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 tv->vval.v_string = vim_strsave(
3692 (char_u *)current_exception->value);
3693 break;
3694
3695 case ISN_CATCH:
3696 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003697 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698
Bram Moolenaar4c137212021-04-19 16:48:48 +02003699 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 if (trystack->ga_len > 0)
3701 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003702 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 + trystack->ga_len - 1;
3704 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003705 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 }
3707 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003708 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 catch_exception(current_exception);
3710 }
3711 break;
3712
Bram Moolenaarc150c092021-02-13 15:02:46 +01003713 case ISN_TRYCONT:
3714 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003715 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003716 trycont_T *trycont = &iptr->isn_arg.trycont;
3717 int i;
3718 trycmd_T *trycmd;
3719 int iidx = trycont->tct_where;
3720
3721 if (trystack->ga_len < trycont->tct_levels)
3722 {
3723 siemsg("TRYCONT: expected %d levels, found %d",
3724 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003725 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003726 }
3727 // Make :endtry jump to any outer try block and the last
3728 // :endtry inside the loop to the loop start.
3729 for (i = trycont->tct_levels; i > 0; --i)
3730 {
3731 trycmd = ((trycmd_T *)trystack->ga_data)
3732 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003733 // Add one to tcd_cont to be able to jump to
3734 // instruction with index zero.
3735 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003736 iidx = trycmd->tcd_finally_idx == 0
3737 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003738 }
3739 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003740 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003741 }
3742 break;
3743
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003744 case ISN_FINALLY:
3745 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003746 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003747 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3748 + trystack->ga_len - 1;
3749
3750 // Reset the index to avoid a return statement jumps here
3751 // again.
3752 trycmd->tcd_finally_idx = 0;
3753 break;
3754 }
3755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 // end of ":try" block
3757 case ISN_ENDTRY:
3758 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003759 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760
3761 if (trystack->ga_len > 0)
3762 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003763 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 --trystack->ga_len;
3766 --trylevel;
3767 trycmd = ((trycmd_T *)trystack->ga_data)
3768 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003769 if (trycmd->tcd_did_throw)
3770 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003771 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 {
3773 // discard the exception
3774 if (caught_stack == current_exception)
3775 caught_stack = caught_stack->caught;
3776 discard_current_exception();
3777 }
3778
3779 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003780 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003781
Bram Moolenaar4c137212021-04-19 16:48:48 +02003782 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003783 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003785 clear_tv(STACK_TV_BOT(0));
3786 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003787 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003788 // handling :continue: jump to outer try block or
3789 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003790 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 }
3792 }
3793 break;
3794
3795 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003796 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003797 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003798
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003799 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3800 {
3801 // throwing an exception while using "silent!" causes
3802 // the function to abort but not display an error.
3803 tv = STACK_TV_BOT(-1);
3804 clear_tv(tv);
3805 tv->v_type = VAR_NUMBER;
3806 tv->vval.v_number = 0;
3807 goto done;
3808 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003809 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003810 tv = STACK_TV_BOT(0);
3811 if (tv->vval.v_string == NULL
3812 || *skipwhite(tv->vval.v_string) == NUL)
3813 {
3814 vim_free(tv->vval.v_string);
3815 SOURCING_LNUM = iptr->isn_lnum;
3816 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003817 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003818 }
3819
3820 // Inside a "catch" we need to first discard the caught
3821 // exception.
3822 if (trystack->ga_len > 0)
3823 {
3824 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3825 + trystack->ga_len - 1;
3826 if (trycmd->tcd_caught && current_exception != NULL)
3827 {
3828 // discard the exception
3829 if (caught_stack == current_exception)
3830 caught_stack = caught_stack->caught;
3831 discard_current_exception();
3832 trycmd->tcd_caught = FALSE;
3833 }
3834 }
3835
Bram Moolenaar90a57162022-02-12 14:23:17 +00003836 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003837 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3838 == FAIL)
3839 {
3840 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003841 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003842 }
3843 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 break;
3846
3847 // compare with special values
3848 case ISN_COMPAREBOOL:
3849 case ISN_COMPARESPECIAL:
3850 {
3851 typval_T *tv1 = STACK_TV_BOT(-2);
3852 typval_T *tv2 = STACK_TV_BOT(-1);
3853 varnumber_T arg1 = tv1->vval.v_number;
3854 varnumber_T arg2 = tv2->vval.v_number;
3855 int res;
3856
3857 switch (iptr->isn_arg.op.op_type)
3858 {
3859 case EXPR_EQUAL: res = arg1 == arg2; break;
3860 case EXPR_NEQUAL: res = arg1 != arg2; break;
3861 default: res = 0; break;
3862 }
3863
Bram Moolenaar4c137212021-04-19 16:48:48 +02003864 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 tv1->v_type = VAR_BOOL;
3866 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3867 }
3868 break;
3869
Bram Moolenaar7a222242022-03-01 19:23:24 +00003870 case ISN_COMPARENULL:
3871 {
3872 typval_T *tv1 = STACK_TV_BOT(-2);
3873 typval_T *tv2 = STACK_TV_BOT(-1);
3874 int res;
3875
3876 res = typval_compare_null(tv1, tv2);
3877 if (res == MAYBE)
3878 goto on_error;
3879 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3880 res = !res;
3881 clear_tv(tv1);
3882 clear_tv(tv2);
3883 --ectx->ec_stack.ga_len;
3884 tv1->v_type = VAR_BOOL;
3885 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3886 }
3887 break;
3888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 // Operation with two number arguments
3890 case ISN_OPNR:
3891 case ISN_COMPARENR:
3892 {
3893 typval_T *tv1 = STACK_TV_BOT(-2);
3894 typval_T *tv2 = STACK_TV_BOT(-1);
3895 varnumber_T arg1 = tv1->vval.v_number;
3896 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003897 varnumber_T res = 0;
3898 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899
3900 switch (iptr->isn_arg.op.op_type)
3901 {
3902 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003903 case EXPR_DIV: if (arg2 == 0)
3904 div_zero = TRUE;
3905 else
3906 res = arg1 / arg2;
3907 break;
3908 case EXPR_REM: if (arg2 == 0)
3909 div_zero = TRUE;
3910 else
3911 res = arg1 % arg2;
3912 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 case EXPR_SUB: res = arg1 - arg2; break;
3914 case EXPR_ADD: res = arg1 + arg2; break;
3915
3916 case EXPR_EQUAL: res = arg1 == arg2; break;
3917 case EXPR_NEQUAL: res = arg1 != arg2; break;
3918 case EXPR_GREATER: res = arg1 > arg2; break;
3919 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3920 case EXPR_SMALLER: res = arg1 < arg2; break;
3921 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003922 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 }
3924
Bram Moolenaar4c137212021-04-19 16:48:48 +02003925 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 if (iptr->isn_type == ISN_COMPARENR)
3927 {
3928 tv1->v_type = VAR_BOOL;
3929 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3930 }
3931 else
3932 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003933 if (div_zero)
3934 {
3935 SOURCING_LNUM = iptr->isn_lnum;
3936 emsg(_(e_divide_by_zero));
3937 goto on_error;
3938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 }
3940 break;
3941
3942 // Computation with two float arguments
3943 case ISN_OPFLOAT:
3944 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003945#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 {
3947 typval_T *tv1 = STACK_TV_BOT(-2);
3948 typval_T *tv2 = STACK_TV_BOT(-1);
3949 float_T arg1 = tv1->vval.v_float;
3950 float_T arg2 = tv2->vval.v_float;
3951 float_T res = 0;
3952 int cmp = FALSE;
3953
3954 switch (iptr->isn_arg.op.op_type)
3955 {
3956 case EXPR_MULT: res = arg1 * arg2; break;
3957 case EXPR_DIV: res = arg1 / arg2; break;
3958 case EXPR_SUB: res = arg1 - arg2; break;
3959 case EXPR_ADD: res = arg1 + arg2; break;
3960
3961 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3962 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3963 case EXPR_GREATER: cmp = arg1 > arg2; break;
3964 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3965 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3966 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3967 default: cmp = 0; break;
3968 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003969 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 if (iptr->isn_type == ISN_COMPAREFLOAT)
3971 {
3972 tv1->v_type = VAR_BOOL;
3973 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3974 }
3975 else
3976 tv1->vval.v_float = res;
3977 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003978#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 break;
3980
3981 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00003982 case ISN_COMPAREDICT:
3983 case ISN_COMPAREFUNC:
3984 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 case ISN_COMPAREBLOB:
3986 {
3987 typval_T *tv1 = STACK_TV_BOT(-2);
3988 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00003989 exprtype_T exprtype = iptr->isn_arg.op.op_type;
3990 int ic = iptr->isn_arg.op.op_ic;
3991 int res = FALSE;
3992 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993
Bram Moolenaar265f8112021-12-19 12:33:05 +00003994 SOURCING_LNUM = iptr->isn_lnum;
3995 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00003997 status = typval_compare_list(tv1, tv2,
3998 exprtype, ic, &res);
3999 }
4000 else if (iptr->isn_type == ISN_COMPAREDICT)
4001 {
4002 status = typval_compare_dict(tv1, tv2,
4003 exprtype, ic, &res);
4004 }
4005 else if (iptr->isn_type == ISN_COMPAREFUNC)
4006 {
4007 status = typval_compare_func(tv1, tv2,
4008 exprtype, ic, &res);
4009 }
4010 else if (iptr->isn_type == ISN_COMPARESTRING)
4011 {
4012 status = typval_compare_string(tv1, tv2,
4013 exprtype, ic, &res);
4014 }
4015 else
4016 {
4017 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004019 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 clear_tv(tv1);
4021 clear_tv(tv2);
4022 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004023 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4024 if (status == FAIL)
4025 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 }
4027 break;
4028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 case ISN_COMPAREANY:
4030 {
4031 typval_T *tv1 = STACK_TV_BOT(-2);
4032 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004033 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004035 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036
Bram Moolenaareb26f432020-09-14 16:50:05 +02004037 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004038 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004040 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004041 if (status == FAIL)
4042 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 }
4044 break;
4045
4046 case ISN_ADDLIST:
4047 case ISN_ADDBLOB:
4048 {
4049 typval_T *tv1 = STACK_TV_BOT(-2);
4050 typval_T *tv2 = STACK_TV_BOT(-1);
4051
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004052 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004054 {
4055 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4056 && tv1->vval.v_list != NULL)
4057 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4058 NULL);
4059 else
4060 eval_addlist(tv1, tv2);
4061 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 else
4063 eval_addblob(tv1, tv2);
4064 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004065 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 }
4067 break;
4068
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004069 case ISN_LISTAPPEND:
4070 {
4071 typval_T *tv1 = STACK_TV_BOT(-2);
4072 typval_T *tv2 = STACK_TV_BOT(-1);
4073 list_T *l = tv1->vval.v_list;
4074
4075 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004076 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004077 if (l == NULL)
4078 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004079 emsg(_(e_cannot_add_to_null_list));
4080 goto on_error;
4081 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004082 if (value_check_lock(l->lv_lock, NULL, FALSE))
4083 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004084 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004085 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004086 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004087 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004088 }
4089 break;
4090
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004091 case ISN_BLOBAPPEND:
4092 {
4093 typval_T *tv1 = STACK_TV_BOT(-2);
4094 typval_T *tv2 = STACK_TV_BOT(-1);
4095 blob_T *b = tv1->vval.v_blob;
4096 int error = FALSE;
4097 varnumber_T n;
4098
4099 // add a number to a blob
4100 if (b == NULL)
4101 {
4102 SOURCING_LNUM = iptr->isn_lnum;
4103 emsg(_(e_cannot_add_to_null_blob));
4104 goto on_error;
4105 }
4106 n = tv_get_number_chk(tv2, &error);
4107 if (error)
4108 goto on_error;
4109 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004110 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004111 }
4112 break;
4113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 // Computation with two arguments of unknown type
4115 case ISN_OPANY:
4116 {
4117 typval_T *tv1 = STACK_TV_BOT(-2);
4118 typval_T *tv2 = STACK_TV_BOT(-1);
4119 varnumber_T n1, n2;
4120#ifdef FEAT_FLOAT
4121 float_T f1 = 0, f2 = 0;
4122#endif
4123 int error = FALSE;
4124
4125 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4126 {
4127 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4128 {
4129 eval_addlist(tv1, tv2);
4130 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004131 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 break;
4133 }
4134 else if (tv1->v_type == VAR_BLOB
4135 && tv2->v_type == VAR_BLOB)
4136 {
4137 eval_addblob(tv1, tv2);
4138 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004139 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 break;
4141 }
4142 }
4143#ifdef FEAT_FLOAT
4144 if (tv1->v_type == VAR_FLOAT)
4145 {
4146 f1 = tv1->vval.v_float;
4147 n1 = 0;
4148 }
4149 else
4150#endif
4151 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004152 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 n1 = tv_get_number_chk(tv1, &error);
4154 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004155 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156#ifdef FEAT_FLOAT
4157 if (tv2->v_type == VAR_FLOAT)
4158 f1 = n1;
4159#endif
4160 }
4161#ifdef FEAT_FLOAT
4162 if (tv2->v_type == VAR_FLOAT)
4163 {
4164 f2 = tv2->vval.v_float;
4165 n2 = 0;
4166 }
4167 else
4168#endif
4169 {
4170 n2 = tv_get_number_chk(tv2, &error);
4171 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004172 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173#ifdef FEAT_FLOAT
4174 if (tv1->v_type == VAR_FLOAT)
4175 f2 = n2;
4176#endif
4177 }
4178#ifdef FEAT_FLOAT
4179 // if there is a float on either side the result is a float
4180 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4181 {
4182 switch (iptr->isn_arg.op.op_type)
4183 {
4184 case EXPR_MULT: f1 = f1 * f2; break;
4185 case EXPR_DIV: f1 = f1 / f2; break;
4186 case EXPR_SUB: f1 = f1 - f2; break;
4187 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004188 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004189 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004190 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 }
4192 clear_tv(tv1);
4193 clear_tv(tv2);
4194 tv1->v_type = VAR_FLOAT;
4195 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004196 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 }
4198 else
4199#endif
4200 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004201 int failed = FALSE;
4202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 switch (iptr->isn_arg.op.op_type)
4204 {
4205 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004206 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4207 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004208 goto on_error;
4209 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 case EXPR_SUB: n1 = n1 - n2; break;
4211 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004212 default: n1 = num_modulus(n1, n2, &failed);
4213 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004214 goto on_error;
4215 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 }
4217 clear_tv(tv1);
4218 clear_tv(tv2);
4219 tv1->v_type = VAR_NUMBER;
4220 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004221 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 }
4223 }
4224 break;
4225
4226 case ISN_CONCAT:
4227 {
4228 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4229 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4230 char_u *res;
4231
4232 res = concat_str(str1, str2);
4233 clear_tv(STACK_TV_BOT(-2));
4234 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004235 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 STACK_TV_BOT(-1)->vval.v_string = res;
4237 }
4238 break;
4239
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004240 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004241 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004242 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004243 int is_slice = iptr->isn_type == ISN_STRSLICE;
4244 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004245 char_u *res;
4246
4247 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004248 // string slice: string is at stack-3, first index at
4249 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004250 if (is_slice)
4251 {
4252 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004253 n1 = tv->vval.v_number;
4254 }
4255
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004256 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004257 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004258
Bram Moolenaar4c137212021-04-19 16:48:48 +02004259 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004260 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004261 if (is_slice)
4262 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004263 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004264 else
4265 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004266 // single character (including composing characters).
4267 // If the index is too big or negative the result is
4268 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004269 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004270 vim_free(tv->vval.v_string);
4271 tv->vval.v_string = res;
4272 }
4273 break;
4274
4275 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004276 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004277 case ISN_BLOBINDEX:
4278 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004280 int is_slice = iptr->isn_type == ISN_LISTSLICE
4281 || iptr->isn_type == ISN_BLOBSLICE;
4282 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4283 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004284 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004285 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286
4287 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004288 // list slice: list is at stack-3, indexes at stack-2 and
4289 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004290 // Same for blob.
4291 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292
4293 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004294 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004296
4297 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 {
Bram Moolenaared591872020-08-15 22:14:53 +02004299 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004300 n1 = tv->vval.v_number;
4301 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 }
Bram Moolenaared591872020-08-15 22:14:53 +02004303
Bram Moolenaar4c137212021-04-19 16:48:48 +02004304 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004305 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004306 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004307 if (is_blob)
4308 {
4309 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4310 n1, n2, FALSE, tv) == FAIL)
4311 goto on_error;
4312 }
4313 else
4314 {
4315 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4316 n1, n2, FALSE, tv, TRUE) == FAIL)
4317 goto on_error;
4318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 }
4320 break;
4321
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004322 case ISN_ANYINDEX:
4323 case ISN_ANYSLICE:
4324 {
4325 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4326 typval_T *var1, *var2;
4327 int res;
4328
4329 // index: composite is at stack-2, index at stack-1
4330 // slice: composite is at stack-3, indexes at stack-2 and
4331 // stack-1
4332 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004333 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004334 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4335 goto on_error;
4336 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4337 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004338 res = eval_index_inner(tv, is_slice, var1, var2,
4339 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004340 clear_tv(var1);
4341 if (is_slice)
4342 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004343 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004344 if (res == FAIL)
4345 goto on_error;
4346 }
4347 break;
4348
Bram Moolenaar9af78762020-06-16 11:34:42 +02004349 case ISN_SLICE:
4350 {
4351 list_T *list;
4352 int count = iptr->isn_arg.number;
4353
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004354 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004355 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004356 list = tv->vval.v_list;
4357
4358 // no error for short list, expect it to be checked earlier
4359 if (list != NULL && list->lv_len >= count)
4360 {
4361 list_T *newlist = list_slice(list,
4362 count, list->lv_len - 1);
4363
4364 if (newlist != NULL)
4365 {
4366 list_unref(list);
4367 tv->vval.v_list = newlist;
4368 ++newlist->lv_refcount;
4369 }
4370 }
4371 }
4372 break;
4373
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004374 case ISN_GETITEM:
4375 {
4376 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004377 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004378
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004379 // Get list item: list is at stack-1, push item.
4380 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004381 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4382 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004383
Bram Moolenaar35578162021-08-02 19:10:38 +02004384 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004385 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004386 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004387 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004388
4389 // Useful when used in unpack assignment. Reset at
4390 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004391 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004392 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004393 }
4394 break;
4395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 case ISN_MEMBER:
4397 {
4398 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004399 char_u *key;
4400 dictitem_T *di;
4401
4402 // dict member: dict is at stack-2, key at stack-1
4403 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004404 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004405 dict = tv->vval.v_dict;
4406
4407 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004408 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004409 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004410 if (key == NULL)
4411 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004412
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004413 if ((di = dict_find(dict, key, -1)) == NULL)
4414 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004415 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004416 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004417
4418 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004419 // stack contents makes sense and the dict stack is
4420 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004421 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004422 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004423 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004424 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004425 tv->v_type = VAR_NUMBER;
4426 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004427 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004428 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004429 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004430 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004431 // Put the dict used on the dict stack, it might be used by
4432 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004433 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004434 if (dict_stack_save(tv) == FAIL)
4435 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004436 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004437 }
4438 break;
4439
4440 // dict member with string key
4441 case ISN_STRINGMEMBER:
4442 {
4443 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 dictitem_T *di;
4445
4446 tv = STACK_TV_BOT(-1);
4447 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4448 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004449 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004450 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004451 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 }
4453 dict = tv->vval.v_dict;
4454
4455 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4456 == NULL)
4457 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004458 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004459 semsg(_(e_key_not_present_in_dictionary),
4460 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004461 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004463 // Put the dict used on the dict stack, it might be used by
4464 // a dict function later.
4465 if (dict_stack_save(tv) == FAIL)
4466 goto on_fatal_error;
4467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004469 }
4470 break;
4471
4472 case ISN_CLEARDICT:
4473 dict_stack_drop();
4474 break;
4475
4476 case ISN_USEDICT:
4477 {
4478 typval_T *dict_tv = dict_stack_get_tv();
4479
4480 // Turn "dict.Func" into a partial for "Func" bound to
4481 // "dict". Don't do this when "Func" is already a partial
4482 // that was bound explicitly (pt_auto is FALSE).
4483 tv = STACK_TV_BOT(-1);
4484 if (dict_tv != NULL
4485 && dict_tv->v_type == VAR_DICT
4486 && dict_tv->vval.v_dict != NULL
4487 && (tv->v_type == VAR_FUNC
4488 || (tv->v_type == VAR_PARTIAL
4489 && (tv->vval.v_partial->pt_auto
4490 || tv->vval.v_partial->pt_dict == NULL))))
4491 dict_tv->vval.v_dict =
4492 make_partial(dict_tv->vval.v_dict, tv);
4493 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 }
4495 break;
4496
4497 case ISN_NEGATENR:
4498 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004499 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004500#ifdef FEAT_FLOAT
4501 if (tv->v_type == VAR_FLOAT)
4502 tv->vval.v_float = -tv->vval.v_float;
4503 else
4504#endif
4505 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 break;
4507
4508 case ISN_CHECKNR:
4509 {
4510 int error = FALSE;
4511
4512 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004513 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004515 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 (void)tv_get_number_chk(tv, &error);
4517 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004518 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 }
4520 break;
4521
4522 case ISN_CHECKTYPE:
4523 {
4524 checktype_T *ct = &iptr->isn_arg.type;
4525
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004526 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004527 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004528 if (!ectx->ec_where.wt_variable)
4529 ectx->ec_where.wt_index = ct->ct_arg_idx;
4530 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4531 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004532 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004533 if (!ectx->ec_where.wt_variable)
4534 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004535
4536 // number 0 is FALSE, number 1 is TRUE
4537 if (tv->v_type == VAR_NUMBER
4538 && ct->ct_type->tt_type == VAR_BOOL
4539 && (tv->vval.v_number == 0
4540 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004542 tv->v_type = VAR_BOOL;
4543 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004544 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 }
4546 }
4547 break;
4548
Bram Moolenaar9af78762020-06-16 11:34:42 +02004549 case ISN_CHECKLEN:
4550 {
4551 int min_len = iptr->isn_arg.checklen.cl_min_len;
4552 list_T *list = NULL;
4553
4554 tv = STACK_TV_BOT(-1);
4555 if (tv->v_type == VAR_LIST)
4556 list = tv->vval.v_list;
4557 if (list == NULL || list->lv_len < min_len
4558 || (list->lv_len > min_len
4559 && !iptr->isn_arg.checklen.cl_more_OK))
4560 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004562 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004563 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004564 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004565 }
4566 }
4567 break;
4568
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004569 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004570 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004571 break;
4572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004574 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 {
4576 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004577 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004579 if (iptr->isn_type == ISN_2BOOL)
4580 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004581 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004582 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004583 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004584 n = !n;
4585 }
4586 else
4587 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004588 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004589 SOURCING_LNUM = iptr->isn_lnum;
4590 n = tv_get_bool_chk(tv, &error);
4591 if (error)
4592 goto on_error;
4593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 clear_tv(tv);
4595 tv->v_type = VAR_BOOL;
4596 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4597 }
4598 break;
4599
4600 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004601 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004602 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004603 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4604 iptr->isn_type == ISN_2STRING_ANY,
4605 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004606 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 break;
4608
Bram Moolenaar08597872020-12-10 19:43:40 +01004609 case ISN_RANGE:
4610 {
4611 exarg_T ea;
4612 char *errormsg;
4613
Bram Moolenaarece0b872021-01-08 20:40:45 +01004614 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004615 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004616 ea.addr_type = ADDR_LINES;
4617 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004618 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004619 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004620 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004621
Bram Moolenaar35578162021-08-02 19:10:38 +02004622 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004623 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004624 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004625 tv = STACK_TV_BOT(-1);
4626 tv->v_type = VAR_NUMBER;
4627 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004628 if (ea.addr_count == 0)
4629 tv->vval.v_number = curwin->w_cursor.lnum;
4630 else
4631 tv->vval.v_number = ea.line2;
4632 }
4633 break;
4634
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004635 case ISN_PUT:
4636 {
4637 int regname = iptr->isn_arg.put.put_regname;
4638 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4639 char_u *expr = NULL;
4640 int dir = FORWARD;
4641
Bram Moolenaar08597872020-12-10 19:43:40 +01004642 if (lnum < -2)
4643 {
4644 // line number was put on the stack by ISN_RANGE
4645 tv = STACK_TV_BOT(-1);
4646 curwin->w_cursor.lnum = tv->vval.v_number;
4647 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4648 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004649 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004650 }
4651 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004652 // :put! above cursor
4653 dir = BACKWARD;
4654 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004655 {
4656 curwin->w_cursor.lnum = lnum;
4657 if (lnum == 0)
4658 // check_cursor() below will move to line 1
4659 dir = BACKWARD;
4660 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004661
4662 if (regname == '=')
4663 {
4664 tv = STACK_TV_BOT(-1);
4665 if (tv->v_type == VAR_STRING)
4666 expr = tv->vval.v_string;
4667 else
4668 {
4669 expr = typval2string(tv, TRUE); // allocates value
4670 clear_tv(tv);
4671 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004672 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004673 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004674 check_cursor();
4675 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4676 vim_free(expr);
4677 }
4678 break;
4679
Bram Moolenaar02194d22020-10-24 23:08:38 +02004680 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004681 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4682 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4683 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4684 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004685 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4686 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004687 break;
4688
Bram Moolenaar02194d22020-10-24 23:08:38 +02004689 case ISN_CMDMOD_REV:
4690 // filter regprog is owned by the instruction, don't free it
4691 cmdmod.cmod_filter_regmatch.regprog = NULL;
4692 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004693 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4694 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004695 break;
4696
Bram Moolenaar792f7862020-11-23 08:31:18 +01004697 case ISN_UNPACK:
4698 {
4699 int count = iptr->isn_arg.unpack.unp_count;
4700 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4701 list_T *l;
4702 listitem_T *li;
4703 int i;
4704
4705 // Check there is a valid list to unpack.
4706 tv = STACK_TV_BOT(-1);
4707 if (tv->v_type != VAR_LIST)
4708 {
4709 SOURCING_LNUM = iptr->isn_lnum;
4710 emsg(_(e_for_argument_must_be_sequence_of_lists));
4711 goto on_error;
4712 }
4713 l = tv->vval.v_list;
4714 if (l == NULL
4715 || l->lv_len < (semicolon ? count - 1 : count))
4716 {
4717 SOURCING_LNUM = iptr->isn_lnum;
4718 emsg(_(e_list_value_does_not_have_enough_items));
4719 goto on_error;
4720 }
4721 else if (!semicolon && l->lv_len > count)
4722 {
4723 SOURCING_LNUM = iptr->isn_lnum;
4724 emsg(_(e_list_value_has_more_items_than_targets));
4725 goto on_error;
4726 }
4727
4728 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004729 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004730 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004731 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004732
4733 // Variable after semicolon gets a list with the remaining
4734 // items.
4735 if (semicolon)
4736 {
4737 list_T *rem_list =
4738 list_alloc_with_items(l->lv_len - count + 1);
4739
4740 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004741 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004742 tv = STACK_TV_BOT(-count);
4743 tv->vval.v_list = rem_list;
4744 ++rem_list->lv_refcount;
4745 tv->v_lock = 0;
4746 li = l->lv_first;
4747 for (i = 0; i < count - 1; ++i)
4748 li = li->li_next;
4749 for (i = 0; li != NULL; ++i)
4750 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004751 typval_T tvcopy;
4752
4753 copy_tv(&li->li_tv, &tvcopy);
4754 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004755 li = li->li_next;
4756 }
4757 --count;
4758 }
4759
4760 // Produce the values in reverse order, first item last.
4761 li = l->lv_first;
4762 for (i = 0; i < count; ++i)
4763 {
4764 tv = STACK_TV_BOT(-i - 1);
4765 copy_tv(&li->li_tv, tv);
4766 li = li->li_next;
4767 }
4768
4769 list_unref(l);
4770 }
4771 break;
4772
Bram Moolenaarb2049902021-01-24 12:53:53 +01004773 case ISN_PROF_START:
4774 case ISN_PROF_END:
4775 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004776#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004777 funccall_T cookie;
4778 ufunc_T *cur_ufunc =
4779 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004780 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004781
4782 cookie.func = cur_ufunc;
4783 if (iptr->isn_type == ISN_PROF_START)
4784 {
4785 func_line_start(&cookie, iptr->isn_lnum);
4786 // if we get here the instruction is executed
4787 func_line_exec(&cookie);
4788 }
4789 else
4790 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004791#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004792 }
4793 break;
4794
Bram Moolenaare99d4222021-06-13 14:01:26 +02004795 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004796 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004797 break;
4798
Bram Moolenaar389df252020-07-09 21:20:47 +02004799 case ISN_SHUFFLE:
4800 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004801 typval_T tmp_tv;
4802 int item = iptr->isn_arg.shuffle.shfl_item;
4803 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004804
4805 tmp_tv = *STACK_TV_BOT(-item);
4806 for ( ; up > 0 && item > 1; --up)
4807 {
4808 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4809 --item;
4810 }
4811 *STACK_TV_BOT(-item) = tmp_tv;
4812 }
4813 break;
4814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004816 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004818 ectx->ec_where.wt_index = 0;
4819 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 break;
4821 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004822 continue;
4823
Bram Moolenaard032f342020-07-18 18:13:02 +02004824func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004825 // Restore previous function. If the frame pointer is where we started
4826 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004827 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004828 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004829
Bram Moolenaar4c137212021-04-19 16:48:48 +02004830 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004831 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004832 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004833 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004834
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004835on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004836 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004837 // If "emsg_silent" is set then ignore the error, unless it was set
4838 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004839 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004840 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004841 {
4842 // If a sequence of instructions causes an error while ":silent!"
4843 // was used, restore the stack length and jump ahead to restoring
4844 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004845 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004846 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004847 while (ectx->ec_stack.ga_len
4848 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004849 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004851 clear_tv(STACK_TV_BOT(0));
4852 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004853 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4854 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004855 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004856 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004857 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004858on_fatal_error:
4859 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004860 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004861 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004862 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 }
4864
4865done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004866 ret = OK;
4867theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004868 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004869 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4870 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004871}
4872
4873/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004874 * Execute the instructions from a VAR_INSTR typeval and put the result in
4875 * "rettv".
4876 * Return OK or FAIL.
4877 */
4878 int
4879exe_typval_instr(typval_T *tv, typval_T *rettv)
4880{
4881 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4882 isn_T *save_instr = ectx->ec_instr;
4883 int save_iidx = ectx->ec_iidx;
4884 int res;
4885
4886 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4887 res = exec_instructions(ectx);
4888 if (res == OK)
4889 {
4890 *rettv = *STACK_TV_BOT(-1);
4891 --ectx->ec_stack.ga_len;
4892 }
4893
4894 ectx->ec_instr = save_instr;
4895 ectx->ec_iidx = save_iidx;
4896
4897 return res;
4898}
4899
4900/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004901 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4902 * "substitute_instr".
4903 */
4904 char_u *
4905exe_substitute_instr(void)
4906{
4907 ectx_T *ectx = substitute_instr->subs_ectx;
4908 isn_T *save_instr = ectx->ec_instr;
4909 int save_iidx = ectx->ec_iidx;
4910 char_u *res;
4911
4912 ectx->ec_instr = substitute_instr->subs_instr;
4913 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004914 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004915 typval_T *tv = STACK_TV_BOT(-1);
4916
Bram Moolenaar27523602021-06-05 21:36:19 +02004917 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004918 --ectx->ec_stack.ga_len;
4919 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004920 }
4921 else
4922 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004923 substitute_instr->subs_status = FAIL;
4924 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926
Bram Moolenaar4c137212021-04-19 16:48:48 +02004927 ectx->ec_instr = save_instr;
4928 ectx->ec_iidx = save_iidx;
4929
4930 return res;
4931}
4932
4933/*
4934 * Call a "def" function from old Vim script.
4935 * Return OK or FAIL.
4936 */
4937 int
4938call_def_function(
4939 ufunc_T *ufunc,
4940 int argc_arg, // nr of arguments
4941 typval_T *argv, // arguments
4942 partial_T *partial, // optional partial for context
4943 typval_T *rettv) // return value
4944{
4945 ectx_T ectx; // execution context
4946 int argc = argc_arg;
4947 typval_T *tv;
4948 int idx;
4949 int ret = FAIL;
4950 int defcount = ufunc->uf_args.ga_len - argc;
4951 sctx_T save_current_sctx = current_sctx;
4952 int did_emsg_before = did_emsg_cumul + did_emsg;
4953 int save_suppress_errthrow = suppress_errthrow;
4954 msglist_T **saved_msg_list = NULL;
4955 msglist_T *private_msg_list = NULL;
4956 int save_emsg_silent_def = emsg_silent_def;
4957 int save_did_emsg_def = did_emsg_def;
4958 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004959 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004960
4961// Get pointer to item in the stack.
4962#undef STACK_TV
4963#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4964
4965// Get pointer to item at the bottom of the stack, -1 is the bottom.
4966#undef STACK_TV_BOT
4967#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4968
4969// Get pointer to a local variable on the stack. Negative for arguments.
4970#undef STACK_TV_VAR
4971#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4972
4973 if (ufunc->uf_def_status == UF_NOT_COMPILED
4974 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00004975 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
4976 && compile_def_function(ufunc, FALSE,
4977 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004978 {
4979 if (did_emsg_cumul + did_emsg == did_emsg_before)
4980 semsg(_(e_function_is_not_compiled_str),
4981 printable_func_name(ufunc));
4982 return FAIL;
4983 }
4984
4985 {
4986 // Check the function was really compiled.
4987 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4988 + ufunc->uf_dfunc_idx;
4989 if (INSTRUCTIONS(dfunc) == NULL)
4990 {
4991 iemsg("using call_def_function() on not compiled function");
4992 return FAIL;
4993 }
4994 }
4995
4996 // If depth of calling is getting too high, don't execute the function.
4997 orig_funcdepth = funcdepth_get();
4998 if (funcdepth_increment() == FAIL)
4999 return FAIL;
5000
5001 CLEAR_FIELD(ectx);
5002 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5003 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005004 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005005 {
5006 funcdepth_decrement();
5007 return FAIL;
5008 }
5009 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5010 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5011 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005012 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005013
5014 idx = argc - ufunc->uf_args.ga_len;
5015 if (idx > 0 && ufunc->uf_va_name == NULL)
5016 {
5017 if (idx == 1)
5018 emsg(_(e_one_argument_too_many));
5019 else
5020 semsg(_(e_nr_arguments_too_many), idx);
5021 goto failed_early;
5022 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005023 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5024 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005025 {
5026 if (idx == -1)
5027 emsg(_(e_one_argument_too_few));
5028 else
5029 semsg(_(e_nr_arguments_too_few), -idx);
5030 goto failed_early;
5031 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005032
5033 // Put arguments on the stack, but no more than what the function expects.
5034 // A lambda can be called with more arguments than it uses.
5035 for (idx = 0; idx < argc
5036 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5037 ++idx)
5038 {
5039 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5040 && argv[idx].v_type == VAR_SPECIAL
5041 && argv[idx].vval.v_number == VVAL_NONE)
5042 {
5043 // Use the default value.
5044 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5045 }
5046 else
5047 {
5048 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5049 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005050 ufunc->uf_arg_types[idx], &argv[idx],
5051 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005052 goto failed_early;
5053 copy_tv(&argv[idx], STACK_TV_BOT(0));
5054 }
5055 ++ectx.ec_stack.ga_len;
5056 }
5057
5058 // Turn varargs into a list. Empty list if no args.
5059 if (ufunc->uf_va_name != NULL)
5060 {
5061 int vararg_count = argc - ufunc->uf_args.ga_len;
5062
5063 if (vararg_count < 0)
5064 vararg_count = 0;
5065 else
5066 argc -= vararg_count;
5067 if (exe_newlist(vararg_count, &ectx) == FAIL)
5068 goto failed_early;
5069
5070 // Check the type of the list items.
5071 tv = STACK_TV_BOT(-1);
5072 if (ufunc->uf_va_type != NULL
5073 && ufunc->uf_va_type != &t_list_any
5074 && ufunc->uf_va_type->tt_member != &t_any
5075 && tv->vval.v_list != NULL)
5076 {
5077 type_T *expected = ufunc->uf_va_type->tt_member;
5078 listitem_T *li = tv->vval.v_list->lv_first;
5079
5080 for (idx = 0; idx < vararg_count; ++idx)
5081 {
5082 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005083 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005084 goto failed_early;
5085 li = li->li_next;
5086 }
5087 }
5088
5089 if (defcount > 0)
5090 // Move varargs list to below missing default arguments.
5091 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5092 --ectx.ec_stack.ga_len;
5093 }
5094
5095 // Make space for omitted arguments, will store default value below.
5096 // Any varargs list goes after them.
5097 if (defcount > 0)
5098 for (idx = 0; idx < defcount; ++idx)
5099 {
5100 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5101 ++ectx.ec_stack.ga_len;
5102 }
5103 if (ufunc->uf_va_name != NULL)
5104 ++ectx.ec_stack.ga_len;
5105
5106 // Frame pointer points to just after arguments.
5107 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5108 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5109
5110 {
5111 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5112 + ufunc->uf_dfunc_idx;
5113 ufunc_T *base_ufunc = dfunc->df_ufunc;
5114
5115 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5116 // by copy_func().
5117 if (partial != NULL || base_ufunc->uf_partial != NULL)
5118 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005119 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5120 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005121 goto failed_early;
5122 if (partial != NULL)
5123 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005124 outer_T *outer = get_pt_outer(partial);
5125
5126 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005127 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005128 if (current_ectx != NULL)
5129 {
5130 if (current_ectx->ec_outer_ref != NULL
5131 && current_ectx->ec_outer_ref->or_outer != NULL)
5132 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005133 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005134 }
5135 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005136 }
5137 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005138 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005139 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005140 ++partial->pt_refcount;
5141 ectx.ec_outer_ref->or_partial = partial;
5142 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005143 }
5144 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005145 {
5146 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5147 ++base_ufunc->uf_partial->pt_refcount;
5148 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5149 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005150 }
5151 }
5152
5153 // dummy frame entries
5154 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5155 {
5156 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5157 ++ectx.ec_stack.ga_len;
5158 }
5159
5160 {
5161 // Reserve space for local variables and any closure reference count.
5162 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5163 + ufunc->uf_dfunc_idx;
5164
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005165 // Initialize variables to zero. That avoids having to generate
5166 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005167 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005168 {
5169 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5170 STACK_TV_VAR(idx)->vval.v_number = 0;
5171 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005172 ectx.ec_stack.ga_len += dfunc->df_varcount;
5173 if (dfunc->df_has_closure)
5174 {
5175 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5176 STACK_TV_VAR(idx)->vval.v_number = 0;
5177 ++ectx.ec_stack.ga_len;
5178 }
5179
5180 ectx.ec_instr = INSTRUCTIONS(dfunc);
5181 }
5182
5183 // Following errors are in the function, not the caller.
5184 // Commands behave like vim9script.
5185 estack_push_ufunc(ufunc, 1);
5186 current_sctx = ufunc->uf_script_ctx;
5187 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5188
5189 // Use a specific location for storing error messages to be converted to an
5190 // exception.
5191 saved_msg_list = msg_list;
5192 msg_list = &private_msg_list;
5193
5194 // Do turn errors into exceptions.
5195 suppress_errthrow = FALSE;
5196
5197 // Do not delete the function while executing it.
5198 ++ufunc->uf_calls;
5199
5200 // When ":silent!" was used before calling then we still abort the
5201 // function. If ":silent!" is used in the function then we don't.
5202 emsg_silent_def = emsg_silent;
5203 did_emsg_def = 0;
5204
5205 ectx.ec_where.wt_index = 0;
5206 ectx.ec_where.wt_variable = FALSE;
5207
5208 // Execute the instructions until done.
5209 ret = exec_instructions(&ectx);
5210 if (ret == OK)
5211 {
5212 // function finished, get result from the stack.
5213 if (ufunc->uf_ret_type == &t_void)
5214 {
5215 rettv->v_type = VAR_VOID;
5216 }
5217 else
5218 {
5219 tv = STACK_TV_BOT(-1);
5220 *rettv = *tv;
5221 tv->v_type = VAR_UNKNOWN;
5222 }
5223 }
5224
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005225 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005226 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5227 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005228
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005229 // Deal with any remaining closures, they may be in use somewhere.
5230 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005231 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005232 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005233 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005234 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005235
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005236 estack_pop();
5237 current_sctx = save_current_sctx;
5238
Bram Moolenaar2336c372021-12-06 15:06:54 +00005239 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5240 // Function was unreferenced while being used, free it now.
5241 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005242
Bram Moolenaar352134b2020-10-17 22:04:08 +02005243 if (*msg_list != NULL && saved_msg_list != NULL)
5244 {
5245 msglist_T **plist = saved_msg_list;
5246
5247 // Append entries from the current msg_list (uncaught exceptions) to
5248 // the saved msg_list.
5249 while (*plist != NULL)
5250 plist = &(*plist)->next;
5251
5252 *plist = *msg_list;
5253 }
5254 msg_list = saved_msg_list;
5255
Bram Moolenaar4c137212021-04-19 16:48:48 +02005256 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005257 {
5258 cmdmod.cmod_filter_regmatch.regprog = NULL;
5259 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005260 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005261 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005262 emsg_silent_def = save_emsg_silent_def;
5263 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005264
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005265failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005266 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005267 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5268 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005269 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005272 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005273 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005274 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005275 if (ectx.ec_outer_ref->or_outer_allocated)
5276 vim_free(ectx.ec_outer_ref->or_outer);
5277 partial_unref(ectx.ec_outer_ref->or_partial);
5278 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005279 }
5280
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005281 // Not sure if this is necessary.
5282 suppress_errthrow = save_suppress_errthrow;
5283
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005284 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5285 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005286 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005287 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005288 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289 return ret;
5290}
5291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005293 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5294 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5295 * "pfx" is prefixed to every line.
5296 */
5297 static void
5298list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5299{
5300 int line_idx = 0;
5301 int prev_current = 0;
5302 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005303 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005304
5305 for (current = 0; current < instr_count; ++current)
5306 {
5307 isn_T *iptr = &instr[current];
5308 char *line;
5309
5310 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005311 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005312 while (line_idx < iptr->isn_lnum
5313 && line_idx < ufunc->uf_lines.ga_len)
5314 {
5315 if (current > prev_current)
5316 {
5317 msg_puts("\n\n");
5318 prev_current = current;
5319 }
5320 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5321 if (line != NULL)
5322 msg(line);
5323 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005324 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5325 {
5326 int first_def_arg = ufunc->uf_args.ga_len
5327 - ufunc->uf_def_args.ga_len;
5328
5329 if (def_arg_idx > 0)
5330 msg_puts("\n\n");
5331 msg_start();
5332 msg_puts(" ");
5333 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5334 first_def_arg + def_arg_idx]);
5335 msg_puts(" = ");
5336 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5337 msg_clr_eos();
5338 msg_end();
5339 }
5340 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005341
5342 switch (iptr->isn_type)
5343 {
5344 case ISN_EXEC:
5345 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5346 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005347 case ISN_EXEC_SPLIT:
5348 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5349 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005350 case ISN_EXECRANGE:
5351 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5352 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005353 case ISN_LEGACY_EVAL:
5354 smsg("%s%4d EVAL legacy %s", pfx, current,
5355 iptr->isn_arg.string);
5356 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005357 case ISN_REDIRSTART:
5358 smsg("%s%4d REDIR", pfx, current);
5359 break;
5360 case ISN_REDIREND:
5361 smsg("%s%4d REDIR END%s", pfx, current,
5362 iptr->isn_arg.number ? " append" : "");
5363 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005364 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005365#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005366 smsg("%s%4d CEXPR pre %s", pfx, current,
5367 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005368#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005369 break;
5370 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005371#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005372 {
5373 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5374
5375 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5376 cexpr_get_auname(cer->cer_cmdidx),
5377 cer->cer_forceit ? "!" : "",
5378 cer->cer_cmdline);
5379 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005380#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005381 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005382 case ISN_INSTR:
5383 {
5384 smsg("%s%4d INSTR", pfx, current);
5385 list_instructions(" ", iptr->isn_arg.instr,
5386 INT_MAX, NULL);
5387 msg(" -------------");
5388 }
5389 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005390 case ISN_SUBSTITUTE:
5391 {
5392 subs_T *subs = &iptr->isn_arg.subs;
5393
5394 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5395 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5396 msg(" -------------");
5397 }
5398 break;
5399 case ISN_EXECCONCAT:
5400 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5401 (varnumber_T)iptr->isn_arg.number);
5402 break;
5403 case ISN_ECHO:
5404 {
5405 echo_T *echo = &iptr->isn_arg.echo;
5406
5407 smsg("%s%4d %s %d", pfx, current,
5408 echo->echo_with_white ? "ECHO" : "ECHON",
5409 echo->echo_count);
5410 }
5411 break;
5412 case ISN_EXECUTE:
5413 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005414 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005415 break;
5416 case ISN_ECHOMSG:
5417 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005418 (varnumber_T)(iptr->isn_arg.number));
5419 break;
5420 case ISN_ECHOCONSOLE:
5421 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5422 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005423 break;
5424 case ISN_ECHOERR:
5425 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005426 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005427 break;
5428 case ISN_LOAD:
5429 {
5430 if (iptr->isn_arg.number < 0)
5431 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5432 (varnumber_T)(iptr->isn_arg.number
5433 + STACK_FRAME_SIZE));
5434 else
5435 smsg("%s%4d LOAD $%lld", pfx, current,
5436 (varnumber_T)(iptr->isn_arg.number));
5437 }
5438 break;
5439 case ISN_LOADOUTER:
5440 {
5441 if (iptr->isn_arg.number < 0)
5442 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5443 iptr->isn_arg.outer.outer_depth,
5444 iptr->isn_arg.outer.outer_idx
5445 + STACK_FRAME_SIZE);
5446 else
5447 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5448 iptr->isn_arg.outer.outer_depth,
5449 iptr->isn_arg.outer.outer_idx);
5450 }
5451 break;
5452 case ISN_LOADV:
5453 smsg("%s%4d LOADV v:%s", pfx, current,
5454 get_vim_var_name(iptr->isn_arg.number));
5455 break;
5456 case ISN_LOADSCRIPT:
5457 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005458 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5459 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5460 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005461
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005462 sv = get_script_svar(sref, -1);
5463 if (sv == NULL)
5464 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5465 pfx, current, si->sn_name);
5466 else
5467 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005468 sv->sv_name,
5469 sref->sref_idx,
5470 si->sn_name);
5471 }
5472 break;
5473 case ISN_LOADS:
5474 {
5475 scriptitem_T *si = SCRIPT_ITEM(
5476 iptr->isn_arg.loadstore.ls_sid);
5477
5478 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5479 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5480 }
5481 break;
5482 case ISN_LOADAUTO:
5483 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5484 break;
5485 case ISN_LOADG:
5486 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5487 break;
5488 case ISN_LOADB:
5489 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5490 break;
5491 case ISN_LOADW:
5492 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5493 break;
5494 case ISN_LOADT:
5495 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5496 break;
5497 case ISN_LOADGDICT:
5498 smsg("%s%4d LOAD g:", pfx, current);
5499 break;
5500 case ISN_LOADBDICT:
5501 smsg("%s%4d LOAD b:", pfx, current);
5502 break;
5503 case ISN_LOADWDICT:
5504 smsg("%s%4d LOAD w:", pfx, current);
5505 break;
5506 case ISN_LOADTDICT:
5507 smsg("%s%4d LOAD t:", pfx, current);
5508 break;
5509 case ISN_LOADOPT:
5510 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5511 break;
5512 case ISN_LOADENV:
5513 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5514 break;
5515 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005516 smsg("%s%4d LOADREG @%c", pfx, current,
5517 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005518 break;
5519
5520 case ISN_STORE:
5521 if (iptr->isn_arg.number < 0)
5522 smsg("%s%4d STORE arg[%lld]", pfx, current,
5523 iptr->isn_arg.number + STACK_FRAME_SIZE);
5524 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005525 smsg("%s%4d STORE $%lld", pfx, current,
5526 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005527 break;
5528 case ISN_STOREOUTER:
5529 {
5530 if (iptr->isn_arg.number < 0)
5531 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5532 iptr->isn_arg.outer.outer_depth,
5533 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5534 else
5535 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5536 iptr->isn_arg.outer.outer_depth,
5537 iptr->isn_arg.outer.outer_idx);
5538 }
5539 break;
5540 case ISN_STOREV:
5541 smsg("%s%4d STOREV v:%s", pfx, current,
5542 get_vim_var_name(iptr->isn_arg.number));
5543 break;
5544 case ISN_STOREAUTO:
5545 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5546 break;
5547 case ISN_STOREG:
5548 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5549 break;
5550 case ISN_STOREB:
5551 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5552 break;
5553 case ISN_STOREW:
5554 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5555 break;
5556 case ISN_STORET:
5557 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5558 break;
5559 case ISN_STORES:
5560 {
5561 scriptitem_T *si = SCRIPT_ITEM(
5562 iptr->isn_arg.loadstore.ls_sid);
5563
5564 smsg("%s%4d STORES %s in %s", pfx, current,
5565 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5566 }
5567 break;
5568 case ISN_STORESCRIPT:
5569 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005570 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5571 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5572 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005573
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005574 sv = get_script_svar(sref, -1);
5575 if (sv == NULL)
5576 smsg("%s%4d STORESCRIPT [deleted] in %s",
5577 pfx, current, si->sn_name);
5578 else
5579 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005580 sv->sv_name,
5581 sref->sref_idx,
5582 si->sn_name);
5583 }
5584 break;
5585 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005586 case ISN_STOREFUNCOPT:
5587 smsg("%s%4d %s &%s", pfx, current,
5588 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005589 iptr->isn_arg.storeopt.so_name);
5590 break;
5591 case ISN_STOREENV:
5592 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5593 break;
5594 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005595 smsg("%s%4d STOREREG @%c", pfx, current,
5596 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005597 break;
5598 case ISN_STORENR:
5599 smsg("%s%4d STORE %lld in $%d", pfx, current,
5600 iptr->isn_arg.storenr.stnr_val,
5601 iptr->isn_arg.storenr.stnr_idx);
5602 break;
5603
5604 case ISN_STOREINDEX:
5605 smsg("%s%4d STOREINDEX %s", pfx, current,
5606 vartype_name(iptr->isn_arg.vartype));
5607 break;
5608
5609 case ISN_STORERANGE:
5610 smsg("%s%4d STORERANGE", pfx, current);
5611 break;
5612
5613 // constants
5614 case ISN_PUSHNR:
5615 smsg("%s%4d PUSHNR %lld", pfx, current,
5616 (varnumber_T)(iptr->isn_arg.number));
5617 break;
5618 case ISN_PUSHBOOL:
5619 case ISN_PUSHSPEC:
5620 smsg("%s%4d PUSH %s", pfx, current,
5621 get_var_special_name(iptr->isn_arg.number));
5622 break;
5623 case ISN_PUSHF:
5624#ifdef FEAT_FLOAT
5625 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5626#endif
5627 break;
5628 case ISN_PUSHS:
5629 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5630 break;
5631 case ISN_PUSHBLOB:
5632 {
5633 char_u *r;
5634 char_u numbuf[NUMBUFLEN];
5635 char_u *tofree;
5636
5637 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5638 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5639 vim_free(tofree);
5640 }
5641 break;
5642 case ISN_PUSHFUNC:
5643 {
5644 char *name = (char *)iptr->isn_arg.string;
5645
5646 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5647 name == NULL ? "[none]" : name);
5648 }
5649 break;
5650 case ISN_PUSHCHANNEL:
5651#ifdef FEAT_JOB_CHANNEL
5652 {
5653 channel_T *channel = iptr->isn_arg.channel;
5654
5655 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5656 channel == NULL ? 0 : channel->ch_id);
5657 }
5658#endif
5659 break;
5660 case ISN_PUSHJOB:
5661#ifdef FEAT_JOB_CHANNEL
5662 {
5663 typval_T tv;
5664 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005665 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005666
5667 tv.v_type = VAR_JOB;
5668 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005669 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005670 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5671 }
5672#endif
5673 break;
5674 case ISN_PUSHEXC:
5675 smsg("%s%4d PUSH v:exception", pfx, current);
5676 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005677 case ISN_AUTOLOAD:
5678 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5679 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005680 case ISN_UNLET:
5681 smsg("%s%4d UNLET%s %s", pfx, current,
5682 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5683 iptr->isn_arg.unlet.ul_name);
5684 break;
5685 case ISN_UNLETENV:
5686 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5687 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5688 iptr->isn_arg.unlet.ul_name);
5689 break;
5690 case ISN_UNLETINDEX:
5691 smsg("%s%4d UNLETINDEX", pfx, current);
5692 break;
5693 case ISN_UNLETRANGE:
5694 smsg("%s%4d UNLETRANGE", pfx, current);
5695 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005696 case ISN_LOCKUNLOCK:
5697 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5698 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005699 case ISN_LOCKCONST:
5700 smsg("%s%4d LOCKCONST", pfx, current);
5701 break;
5702 case ISN_NEWLIST:
5703 smsg("%s%4d NEWLIST size %lld", pfx, current,
5704 (varnumber_T)(iptr->isn_arg.number));
5705 break;
5706 case ISN_NEWDICT:
5707 smsg("%s%4d NEWDICT size %lld", pfx, current,
5708 (varnumber_T)(iptr->isn_arg.number));
5709 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005710 case ISN_NEWPARTIAL:
5711 smsg("%s%4d NEWPARTIAL", pfx, current);
5712 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005713
5714 // function call
5715 case ISN_BCALL:
5716 {
5717 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5718
5719 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5720 internal_func_name(cbfunc->cbf_idx),
5721 cbfunc->cbf_argcount);
5722 }
5723 break;
5724 case ISN_DCALL:
5725 {
5726 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5727 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5728 + cdfunc->cdf_idx;
5729
5730 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005731 printable_func_name(df->df_ufunc),
5732 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005733 }
5734 break;
5735 case ISN_UCALL:
5736 {
5737 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5738
5739 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5740 cufunc->cuf_name, cufunc->cuf_argcount);
5741 }
5742 break;
5743 case ISN_PCALL:
5744 {
5745 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5746
5747 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5748 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5749 }
5750 break;
5751 case ISN_PCALL_END:
5752 smsg("%s%4d PCALL end", pfx, current);
5753 break;
5754 case ISN_RETURN:
5755 smsg("%s%4d RETURN", pfx, current);
5756 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005757 case ISN_RETURN_VOID:
5758 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005759 break;
5760 case ISN_FUNCREF:
5761 {
5762 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005763 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005764
Bram Moolenaar38453522021-11-28 22:00:12 +00005765 if (funcref->fr_func_name == NULL)
5766 {
5767 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5768 + funcref->fr_dfunc_idx;
5769 name = df->df_ufunc->uf_name;
5770 }
5771 else
5772 name = funcref->fr_func_name;
5773 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005774 }
5775 break;
5776
5777 case ISN_NEWFUNC:
5778 {
5779 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5780
5781 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5782 newfunc->nf_lambda, newfunc->nf_global);
5783 }
5784 break;
5785
5786 case ISN_DEF:
5787 {
5788 char_u *name = iptr->isn_arg.string;
5789
5790 smsg("%s%4d DEF %s", pfx, current,
5791 name == NULL ? (char_u *)"" : name);
5792 }
5793 break;
5794
5795 case ISN_JUMP:
5796 {
5797 char *when = "?";
5798
5799 switch (iptr->isn_arg.jump.jump_when)
5800 {
5801 case JUMP_ALWAYS:
5802 when = "JUMP";
5803 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005804 case JUMP_NEVER:
5805 iemsg("JUMP_NEVER should not be used");
5806 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005807 case JUMP_AND_KEEP_IF_TRUE:
5808 when = "JUMP_AND_KEEP_IF_TRUE";
5809 break;
5810 case JUMP_IF_FALSE:
5811 when = "JUMP_IF_FALSE";
5812 break;
5813 case JUMP_AND_KEEP_IF_FALSE:
5814 when = "JUMP_AND_KEEP_IF_FALSE";
5815 break;
5816 case JUMP_IF_COND_FALSE:
5817 when = "JUMP_IF_COND_FALSE";
5818 break;
5819 case JUMP_IF_COND_TRUE:
5820 when = "JUMP_IF_COND_TRUE";
5821 break;
5822 }
5823 smsg("%s%4d %s -> %d", pfx, current, when,
5824 iptr->isn_arg.jump.jump_where);
5825 }
5826 break;
5827
5828 case ISN_JUMP_IF_ARG_SET:
5829 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5830 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5831 iptr->isn_arg.jump.jump_where);
5832 break;
5833
5834 case ISN_FOR:
5835 {
5836 forloop_T *forloop = &iptr->isn_arg.forloop;
5837
5838 smsg("%s%4d FOR $%d -> %d", pfx, current,
5839 forloop->for_idx, forloop->for_end);
5840 }
5841 break;
5842
5843 case ISN_TRY:
5844 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005845 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005846
5847 if (try->try_ref->try_finally == 0)
5848 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5849 pfx, current,
5850 try->try_ref->try_catch,
5851 try->try_ref->try_endtry);
5852 else
5853 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5854 pfx, current,
5855 try->try_ref->try_catch,
5856 try->try_ref->try_finally,
5857 try->try_ref->try_endtry);
5858 }
5859 break;
5860 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861 smsg("%s%4d CATCH", pfx, current);
5862 break;
5863 case ISN_TRYCONT:
5864 {
5865 trycont_T *trycont = &iptr->isn_arg.trycont;
5866
5867 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5868 trycont->tct_levels,
5869 trycont->tct_levels == 1 ? "" : "s",
5870 trycont->tct_where);
5871 }
5872 break;
5873 case ISN_FINALLY:
5874 smsg("%s%4d FINALLY", pfx, current);
5875 break;
5876 case ISN_ENDTRY:
5877 smsg("%s%4d ENDTRY", pfx, current);
5878 break;
5879 case ISN_THROW:
5880 smsg("%s%4d THROW", pfx, current);
5881 break;
5882
5883 // expression operations on number
5884 case ISN_OPNR:
5885 case ISN_OPFLOAT:
5886 case ISN_OPANY:
5887 {
5888 char *what;
5889 char *ins;
5890
5891 switch (iptr->isn_arg.op.op_type)
5892 {
5893 case EXPR_MULT: what = "*"; break;
5894 case EXPR_DIV: what = "/"; break;
5895 case EXPR_REM: what = "%"; break;
5896 case EXPR_SUB: what = "-"; break;
5897 case EXPR_ADD: what = "+"; break;
5898 default: what = "???"; break;
5899 }
5900 switch (iptr->isn_type)
5901 {
5902 case ISN_OPNR: ins = "OPNR"; break;
5903 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5904 case ISN_OPANY: ins = "OPANY"; break;
5905 default: ins = "???"; break;
5906 }
5907 smsg("%s%4d %s %s", pfx, current, ins, what);
5908 }
5909 break;
5910
5911 case ISN_COMPAREBOOL:
5912 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00005913 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005914 case ISN_COMPARENR:
5915 case ISN_COMPAREFLOAT:
5916 case ISN_COMPARESTRING:
5917 case ISN_COMPAREBLOB:
5918 case ISN_COMPARELIST:
5919 case ISN_COMPAREDICT:
5920 case ISN_COMPAREFUNC:
5921 case ISN_COMPAREANY:
5922 {
5923 char *p;
5924 char buf[10];
5925 char *type;
5926
5927 switch (iptr->isn_arg.op.op_type)
5928 {
5929 case EXPR_EQUAL: p = "=="; break;
5930 case EXPR_NEQUAL: p = "!="; break;
5931 case EXPR_GREATER: p = ">"; break;
5932 case EXPR_GEQUAL: p = ">="; break;
5933 case EXPR_SMALLER: p = "<"; break;
5934 case EXPR_SEQUAL: p = "<="; break;
5935 case EXPR_MATCH: p = "=~"; break;
5936 case EXPR_IS: p = "is"; break;
5937 case EXPR_ISNOT: p = "isnot"; break;
5938 case EXPR_NOMATCH: p = "!~"; break;
5939 default: p = "???"; break;
5940 }
5941 STRCPY(buf, p);
5942 if (iptr->isn_arg.op.op_ic == TRUE)
5943 strcat(buf, "?");
5944 switch(iptr->isn_type)
5945 {
5946 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5947 case ISN_COMPARESPECIAL:
5948 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00005949 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005950 case ISN_COMPARENR: type = "COMPARENR"; break;
5951 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5952 case ISN_COMPARESTRING:
5953 type = "COMPARESTRING"; break;
5954 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5955 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5956 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5957 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5958 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5959 default: type = "???"; break;
5960 }
5961
5962 smsg("%s%4d %s %s", pfx, current, type, buf);
5963 }
5964 break;
5965
5966 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5967 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5968
5969 // expression operations
5970 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5971 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5972 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5973 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5974 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5975 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5976 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5977 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5978 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5979 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5980 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5981 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005982 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005983 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5984 iptr->isn_arg.getitem.gi_index,
5985 iptr->isn_arg.getitem.gi_with_op ?
5986 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005987 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5988 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5989 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005990 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5991 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5992
Bram Moolenaar4c137212021-04-19 16:48:48 +02005993 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5994
5995 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5996 case ISN_CHECKTYPE:
5997 {
5998 checktype_T *ct = &iptr->isn_arg.type;
5999 char *tofree;
6000
6001 if (ct->ct_arg_idx == 0)
6002 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6003 type_name(ct->ct_type, &tofree),
6004 (int)ct->ct_off);
6005 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006006 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6007 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006008 type_name(ct->ct_type, &tofree),
6009 (int)ct->ct_off,
6010 (int)ct->ct_arg_idx);
6011 vim_free(tofree);
6012 break;
6013 }
6014 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6015 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6016 iptr->isn_arg.checklen.cl_min_len);
6017 break;
6018 case ISN_SETTYPE:
6019 {
6020 char *tofree;
6021
6022 smsg("%s%4d SETTYPE %s", pfx, current,
6023 type_name(iptr->isn_arg.type.ct_type, &tofree));
6024 vim_free(tofree);
6025 break;
6026 }
6027 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006028 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6029 smsg("%s%4d INVERT %d (!val)", pfx, current,
6030 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006031 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006032 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6033 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006034 break;
6035 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006036 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006037 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006038 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6039 pfx, current,
6040 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006041 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006042 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6043 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006044 break;
6045 case ISN_PUT:
6046 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6047 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006048 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006049 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6050 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006051 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006052 else
6053 smsg("%s%4d PUT %c %ld", pfx, current,
6054 iptr->isn_arg.put.put_regname,
6055 (long)iptr->isn_arg.put.put_lnum);
6056 break;
6057
Bram Moolenaar4c137212021-04-19 16:48:48 +02006058 case ISN_CMDMOD:
6059 {
6060 char_u *buf;
6061 size_t len = produce_cmdmods(
6062 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6063
6064 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006065 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006066 {
6067 (void)produce_cmdmods(
6068 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6069 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6070 vim_free(buf);
6071 }
6072 break;
6073 }
6074 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6075
6076 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006077 smsg("%s%4d PROFILE START line %d", pfx, current,
6078 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006079 break;
6080
6081 case ISN_PROF_END:
6082 smsg("%s%4d PROFILE END", pfx, current);
6083 break;
6084
Bram Moolenaare99d4222021-06-13 14:01:26 +02006085 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006086 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6087 iptr->isn_arg.debug.dbg_break_lnum + 1,
6088 iptr->isn_lnum,
6089 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006090 break;
6091
Bram Moolenaar4c137212021-04-19 16:48:48 +02006092 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6093 iptr->isn_arg.unpack.unp_count,
6094 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6095 break;
6096 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6097 iptr->isn_arg.shuffle.shfl_item,
6098 iptr->isn_arg.shuffle.shfl_up);
6099 break;
6100 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6101
6102 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6103 return;
6104 }
6105
6106 out_flush(); // output one line at a time
6107 ui_breakcheck();
6108 if (got_int)
6109 break;
6110 }
6111}
6112
6113/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006114 * Handle command line completion for the :disassemble command.
6115 */
6116 void
6117set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6118{
6119 char_u *p;
6120
6121 // Default: expand user functions, "debug" and "profile"
6122 xp->xp_context = EXPAND_DISASSEMBLE;
6123 xp->xp_pattern = arg;
6124
6125 // first argument already typed: only user function names
6126 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6127 {
6128 xp->xp_context = EXPAND_USER_FUNC;
6129 xp->xp_pattern = skipwhite(p);
6130 }
6131}
6132
6133/*
6134 * Function given to ExpandGeneric() to obtain the list of :disassemble
6135 * arguments.
6136 */
6137 char_u *
6138get_disassemble_argument(expand_T *xp, int idx)
6139{
6140 if (idx == 0)
6141 return (char_u *)"debug";
6142 if (idx == 1)
6143 return (char_u *)"profile";
6144 return get_user_func_name(xp, idx - 2);
6145}
6146
6147/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006148 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006149 * We don't really need this at runtime, but we do have tests that require it,
6150 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151 */
6152 void
6153ex_disassemble(exarg_T *eap)
6154{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006155 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006156 char_u *fname;
6157 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158 dfunc_T *dfunc;
6159 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006160 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006161 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006162 compiletype_T compile_type = CT_NONE;
6163
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006164 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006165 {
6166 compile_type = CT_PROFILE;
6167 arg = skipwhite(arg + 7);
6168 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006169 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006170 {
6171 compile_type = CT_DEBUG;
6172 arg = skipwhite(arg + 5);
6173 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006175 if (STRNCMP(arg, "<lambda>", 8) == 0)
6176 {
6177 arg += 8;
6178 (void)getdigits(&arg);
6179 fname = vim_strnsave(eap->arg, arg - eap->arg);
6180 }
6181 else
6182 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006183 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006184 if (fname == NULL)
6185 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006186 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006187 return;
6188 }
6189
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006190 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006191 if (ufunc == NULL)
6192 {
6193 char_u *p = untrans_function_name(fname);
6194
6195 if (p != NULL)
6196 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006197 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006198 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006199 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200 if (ufunc == NULL)
6201 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006202 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006203 return;
6204 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006205 if (func_needs_compiling(ufunc, compile_type)
6206 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006207 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006208 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006210 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 return;
6212 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006213 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214
6215 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006216 switch (compile_type)
6217 {
6218 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006219#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006220 instr = dfunc->df_instr_prof;
6221 instr_count = dfunc->df_instr_prof_count;
6222 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006223#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006224 // FALLTHROUGH
6225 case CT_NONE:
6226 instr = dfunc->df_instr;
6227 instr_count = dfunc->df_instr_count;
6228 break;
6229 case CT_DEBUG:
6230 instr = dfunc->df_instr_debug;
6231 instr_count = dfunc->df_instr_debug_count;
6232 break;
6233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234
Bram Moolenaar4c137212021-04-19 16:48:48 +02006235 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236}
6237
6238/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006239 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 * list, etc. Mostly like what JavaScript does, except that empty list and
6241 * empty dictionary are FALSE.
6242 */
6243 int
6244tv2bool(typval_T *tv)
6245{
6246 switch (tv->v_type)
6247 {
6248 case VAR_NUMBER:
6249 return tv->vval.v_number != 0;
6250 case VAR_FLOAT:
6251#ifdef FEAT_FLOAT
6252 return tv->vval.v_float != 0.0;
6253#else
6254 break;
6255#endif
6256 case VAR_PARTIAL:
6257 return tv->vval.v_partial != NULL;
6258 case VAR_FUNC:
6259 case VAR_STRING:
6260 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6261 case VAR_LIST:
6262 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6263 case VAR_DICT:
6264 return tv->vval.v_dict != NULL
6265 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6266 case VAR_BOOL:
6267 case VAR_SPECIAL:
6268 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6269 case VAR_JOB:
6270#ifdef FEAT_JOB_CHANNEL
6271 return tv->vval.v_job != NULL;
6272#else
6273 break;
6274#endif
6275 case VAR_CHANNEL:
6276#ifdef FEAT_JOB_CHANNEL
6277 return tv->vval.v_channel != NULL;
6278#else
6279 break;
6280#endif
6281 case VAR_BLOB:
6282 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6283 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006284 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006286 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 break;
6288 }
6289 return FALSE;
6290}
6291
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006292 void
6293emsg_using_string_as(typval_T *tv, int as_number)
6294{
6295 semsg(_(as_number ? e_using_string_as_number_str
6296 : e_using_string_as_bool_str),
6297 tv->vval.v_string == NULL
6298 ? (char_u *)"" : tv->vval.v_string);
6299}
6300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301/*
6302 * If "tv" is a string give an error and return FAIL.
6303 */
6304 int
6305check_not_string(typval_T *tv)
6306{
6307 if (tv->v_type == VAR_STRING)
6308 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006309 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 clear_tv(tv);
6311 return FAIL;
6312 }
6313 return OK;
6314}
6315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316
6317#endif // FEAT_EVAL