blob: 681847a793e4a859a0bcf71da9422bb5c808248d [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100287
288 if (dfunc->df_deleted)
289 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100290 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000291 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100292 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 return FAIL;
294 }
295
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100296#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100297 if (do_profiling == PROF_YES)
298 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200299 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100300 {
301 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
302 + profile_info_ga.ga_len;
303 ++profile_info_ga.ga_len;
304 CLEAR_POINTER(info);
305 profile_may_start_func(info, ufunc,
306 (((dfunc_T *)def_functions.ga_data)
307 + ectx->ec_dfunc_idx)->df_ufunc);
308 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100309 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100310#endif
311
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200312 // Update uf_has_breakpoint if needed.
313 update_has_breakpoint(ufunc);
314
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200315 // When debugging and using "cont" switches to the not-debugged
316 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200317 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
318 {
319 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
320
321 // compile_def_function() may cause def_functions.ga_data to change
322 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
323 }
324 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200325 {
326 if (did_emsg_cumul + did_emsg == did_emsg_before)
327 semsg(_(e_function_is_not_compiled_str),
328 printable_func_name(ufunc));
329 return FAIL;
330 }
331
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200332 if (ufunc->uf_va_name != NULL)
333 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200334 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200335 // Stack at time of call with 2 varargs:
336 // normal_arg
337 // optional_arg
338 // vararg_1
339 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200340 // After creating the list:
341 // normal_arg
342 // optional_arg
343 // vararg-list
344 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200345 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200346 // After creating the list
347 // normal_arg
348 // (space for optional_arg)
349 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200350 vararg_count = argcount - ufunc->uf_args.ga_len;
351 if (vararg_count < 0)
352 vararg_count = 0;
353 else
354 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200355 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200356 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200357
358 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200359 }
360
Bram Moolenaarfe270812020-04-11 22:31:27 +0200361 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200362 if (arg_to_add < 0)
363 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200364 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200365 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200366 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200367 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200368 return FAIL;
369 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000370 else if (arg_to_add > ufunc->uf_def_args.ga_len)
371 {
372 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
373
374 if (missing == 1)
375 emsg(_(e_one_argument_too_few));
376 else
377 semsg(_(e_nr_arguments_too_few), missing);
378 return FAIL;
379 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200380
381 // Reserve space for:
382 // - missing arguments
383 // - stack frame
384 // - local variables
385 // - if needed: a counter for number of closures created in
386 // ectx->ec_funcrefs.
387 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200388 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389 return FAIL;
390
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100391 // If depth of calling is getting too high, don't execute the function.
392 if (funcdepth_increment() == FAIL)
393 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200394 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100395
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100396 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200397 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 {
399 floc = ALLOC_ONE(funclocal_T);
400 if (floc == NULL)
401 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200402 *floc = ectx->ec_funclocal;
403 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100404 }
405
Bram Moolenaarfe270812020-04-11 22:31:27 +0200406 // Move the vararg-list to below the missing optional arguments.
407 if (vararg_count > 0 && arg_to_add > 0)
408 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100409
410 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200411 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200412 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200413 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414
415 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100416 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
417 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200418 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200419 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
420 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100421 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100422 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100424
425 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200426 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000427 {
428 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
429
430 tv->v_type = VAR_NUMBER;
431 tv->vval.v_number = 0;
432 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200433 if (dfunc->df_has_closure)
434 {
435 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
436
437 tv->v_type = VAR_NUMBER;
438 tv->vval.v_number = 0;
439 }
440 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100442 if (pt != NULL || ufunc->uf_partial != NULL
443 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100444 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200445 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100446
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200447 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100448 return FAIL;
449 if (pt != NULL)
450 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000451 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200452 ++pt->pt_refcount;
453 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100454 }
455 else if (ufunc->uf_partial != NULL)
456 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000457 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200458 ++ufunc->uf_partial->pt_refcount;
459 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100460 }
461 else
462 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200463 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200464 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200465 {
466 vim_free(ref);
467 return FAIL;
468 }
469 ref->or_outer_allocated = TRUE;
470 ref->or_outer->out_stack = &ectx->ec_stack;
471 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
472 if (ectx->ec_outer_ref != NULL)
473 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100474 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200475 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100476 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100477 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200478 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100479
Bram Moolenaarc970e422021-03-17 15:03:04 +0100480 ++ufunc->uf_calls;
481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 // Set execution state to the start of the called function.
483 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100484 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100485 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200486 if (entry != NULL)
487 {
488 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200489 // Save the current context so it can be restored on return.
490 entry->es_save_sctx = current_sctx;
491 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200492 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100493
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200494 // Start execution at the first instruction.
495 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496
497 return OK;
498}
499
500// Get pointer to item in the stack.
501#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
502
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000503// Double linked list of funcstack_T in use.
504static funcstack_T *first_funcstack = NULL;
505
506 static void
507add_funcstack_to_list(funcstack_T *funcstack)
508{
509 // Link in list of funcstacks.
510 if (first_funcstack != NULL)
511 first_funcstack->fs_prev = funcstack;
512 funcstack->fs_next = first_funcstack;
513 funcstack->fs_prev = NULL;
514 first_funcstack = funcstack;
515}
516
517 static void
518remove_funcstack_from_list(funcstack_T *funcstack)
519{
520 if (funcstack->fs_prev == NULL)
521 first_funcstack = funcstack->fs_next;
522 else
523 funcstack->fs_prev->fs_next = funcstack->fs_next;
524 if (funcstack->fs_next != NULL)
525 funcstack->fs_next->fs_prev = funcstack->fs_prev;
526}
527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200529 * Used when returning from a function: Check if any closure is still
530 * referenced. If so then move the arguments and variables to a separate piece
531 * of stack to be used when the closure is called.
532 * When "free_arguments" is TRUE the arguments are to be freed.
533 * Returns FAIL when out of memory.
534 */
535 static int
536handle_closure_in_use(ectx_T *ectx, int free_arguments)
537{
538 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
539 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200540 int argcount;
541 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200542 int idx;
543 typval_T *tv;
544 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200545 garray_T *gap = &ectx->ec_funcrefs;
546 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200547
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200548 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200549 return OK; // function was freed
550 if (dfunc->df_has_closure == 0)
551 return OK; // no closures
552 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
553 closure_count = tv->vval.v_number;
554 if (closure_count == 0)
555 return OK; // no funcrefs created
556
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200557 argcount = ufunc_argcount(dfunc->df_ufunc);
558 top = ectx->ec_frame_idx - argcount;
559
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200560 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200561 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200562 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200563 partial_T *pt;
564 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200565
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200566 if (off < 0)
567 continue; // count is off or already done
568 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200569 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200570 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200571 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200572 int i;
573
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000574 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200575 // unreferenced on return.
576 for (i = 0; i < dfunc->df_varcount; ++i)
577 {
578 typval_T *stv = STACK_TV(ectx->ec_frame_idx
579 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200580 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200581 --refcount;
582 }
583 if (refcount > 1)
584 {
585 closure_in_use = TRUE;
586 break;
587 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200588 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200589 }
590
591 if (closure_in_use)
592 {
593 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
594 typval_T *stack;
595
596 // A closure is using the arguments and/or local variables.
597 // Move them to the called function.
598 if (funcstack == NULL)
599 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000600
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200601 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
602 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200603 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
604 funcstack->fs_ga.ga_data = stack;
605 if (stack == NULL)
606 {
607 vim_free(funcstack);
608 return FAIL;
609 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000610 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200611
612 // Move or copy the arguments.
613 for (idx = 0; idx < argcount; ++idx)
614 {
615 tv = STACK_TV(top + idx);
616 if (free_arguments)
617 {
618 *(stack + idx) = *tv;
619 tv->v_type = VAR_UNKNOWN;
620 }
621 else
622 copy_tv(tv, stack + idx);
623 }
624 // Move the local variables.
625 for (idx = 0; idx < dfunc->df_varcount; ++idx)
626 {
627 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200628
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200629 // A partial created for a local function, that is also used as a
630 // local variable, has a reference count for the variable, thus
631 // will never go down to zero. When all these refcounts are one
632 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000633 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200634 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
635 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200636 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200637
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200638 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200639 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
640 gap->ga_len - closure_count + i])
641 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200642 }
643
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200644 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200645 tv->v_type = VAR_UNKNOWN;
646 }
647
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200648 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200649 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200650 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
651 - closure_count + idx];
652 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200653 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200654 ++funcstack->fs_refcount;
655 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100656 pt->pt_outer.out_stack = &funcstack->fs_ga;
657 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200658 }
659 }
660 }
661
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200662 for (idx = 0; idx < closure_count; ++idx)
663 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
664 - closure_count + idx]);
665 gap->ga_len -= closure_count;
666 if (gap->ga_len == 0)
667 ga_clear(gap);
668
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200669 return OK;
670}
671
672/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200673 * Called when a partial is freed or its reference count goes down to one. The
674 * funcstack may be the only reference to the partials in the local variables.
675 * Go over all of them, the funcref and can be freed if all partials
676 * referencing the funcstack have a reference count of one.
677 */
678 void
679funcstack_check_refcount(funcstack_T *funcstack)
680{
681 int i;
682 garray_T *gap = &funcstack->fs_ga;
683 int done = 0;
684
685 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
686 return;
687 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
688 {
689 typval_T *tv = ((typval_T *)gap->ga_data) + i;
690
691 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
692 && tv->vval.v_partial->pt_funcstack == funcstack
693 && tv->vval.v_partial->pt_refcount == 1)
694 ++done;
695 }
696 if (done == funcstack->fs_min_refcount)
697 {
698 typval_T *stack = gap->ga_data;
699
700 // All partials referencing the funcstack have a reference count of
701 // one, thus the funcstack is no longer of use.
702 for (i = 0; i < gap->ga_len; ++i)
703 clear_tv(stack + i);
704 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000705 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200706 vim_free(funcstack);
707 }
708}
709
710/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000711 * For garbage collecting: set references in all variables referenced by
712 * all funcstacks.
713 */
714 int
715set_ref_in_funcstacks(int copyID)
716{
717 funcstack_T *funcstack;
718
719 for (funcstack = first_funcstack; funcstack != NULL;
720 funcstack = funcstack->fs_next)
721 {
722 typval_T *stack = funcstack->fs_ga.ga_data;
723 int i;
724
725 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
726 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
727 return TRUE; // abort
728 }
729 return FALSE;
730}
731
732/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 * Return from the current function.
734 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200735 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200736func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100739 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200740 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
741 + ectx->ec_dfunc_idx;
742 int argcount = ufunc_argcount(dfunc->df_ufunc);
743 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200744 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100745 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
746 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200747 funclocal_T *floc;
748#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100749 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
750 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751
Bram Moolenaar12d26532021-02-19 19:13:21 +0100752 if (do_profiling == PROF_YES)
753 {
754 ufunc_T *caller = prev_dfunc->df_ufunc;
755
756 if (dfunc->df_ufunc->uf_profiling
757 || (caller != NULL && caller->uf_profiling))
758 {
759 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
760 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
761 --profile_info_ga.ga_len;
762 }
763 }
764#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000765
766 // No check for uf_refcount being zero, cannot think of a way that would
767 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100768 --dfunc->df_ufunc->uf_calls;
769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200771 entry = estack_pop();
772 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200773 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200775 if (handle_closure_in_use(ectx, TRUE) == FAIL)
776 return FAIL;
777
778 // Clear the arguments.
779 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
780 clear_tv(STACK_TV(idx));
781
782 // Clear local variables and temp values, but not the return value.
783 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100784 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100786
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100787 // The return value should be on top of the stack. However, when aborting
788 // it may not be there and ec_frame_idx is the top of the stack.
789 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100790 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100791 ret_idx = 0;
792
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200793 if (ectx->ec_outer_ref != NULL)
794 {
795 if (ectx->ec_outer_ref->or_outer_allocated)
796 vim_free(ectx->ec_outer_ref->or_outer);
797 partial_unref(ectx->ec_outer_ref->or_partial);
798 vim_free(ectx->ec_outer_ref);
799 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100800
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100801 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100802 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100803 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
804 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200805 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
806 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200807 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100808 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100809 floc = (void *)STACK_TV(ectx->ec_frame_idx
810 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200811 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100812 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
813 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200814
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100815 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200816 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100817 else
818 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200819 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100820 vim_free(floc);
821 }
822
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100823 if (ret_idx > 0)
824 {
825 // Reset the stack to the position before the call, with a spot for the
826 // return value, moved there from above the frame.
827 ectx->ec_stack.ga_len = top + 1;
828 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
829 }
830 else
831 // Reset the stack to the position before the call.
832 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200833
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100834 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200835 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200836 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837}
838
839#undef STACK_TV
840
841/*
842 * Prepare arguments and rettv for calling a builtin or user function.
843 */
844 static int
845call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
846{
847 int idx;
848 typval_T *tv;
849
850 // Move arguments from bottom of the stack to argvars[] and add terminator.
851 for (idx = 0; idx < argcount; ++idx)
852 argvars[idx] = *STACK_TV_BOT(idx - argcount);
853 argvars[argcount].v_type = VAR_UNKNOWN;
854
855 // Result replaces the arguments on the stack.
856 if (argcount > 0)
857 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200858 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 return FAIL;
860 else
861 ++ectx->ec_stack.ga_len;
862
863 // Default return value is zero.
864 tv = STACK_TV_BOT(-1);
865 tv->v_type = VAR_NUMBER;
866 tv->vval.v_number = 0;
867
868 return OK;
869}
870
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200871// Ugly global to avoid passing the execution context around through many
872// layers.
873static ectx_T *current_ectx = NULL;
874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875/*
876 * Call a builtin function by index.
877 */
878 static int
879call_bfunc(int func_idx, int argcount, ectx_T *ectx)
880{
881 typval_T argvars[MAX_FUNC_ARGS];
882 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100883 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200884 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200885 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886
887 if (call_prepare(argcount, argvars, ectx) == FAIL)
888 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200889 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200891 // Call the builtin function. Set "current_ectx" so that when it
892 // recursively invokes call_def_function() a closure context can be set.
893 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200895 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200896 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897
898 // Clear the arguments.
899 for (idx = 0; idx < argcount; ++idx)
900 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200901
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100902 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200903 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 return OK;
905}
906
907/*
908 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100909 * If the function is compiled this will add a stack frame and set the
910 * instruction pointer at the start of the function.
911 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200912 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100913 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 */
915 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100916call_ufunc(
917 ufunc_T *ufunc,
918 partial_T *pt,
919 int argcount,
920 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200921 isn_T *iptr,
922 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923{
924 typval_T argvars[MAX_FUNC_ARGS];
925 funcexe_T funcexe;
926 int error;
927 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100928 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200929 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930
Bram Moolenaare99d4222021-06-13 14:01:26 +0200931 if (func_needs_compiling(ufunc, compile_type)
932 && compile_def_function(ufunc, FALSE, compile_type, NULL)
933 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200934 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200935 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100936 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100937 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100938 if (error != FCERR_UNKNOWN)
939 {
940 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000941 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100942 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000943 semsg(_(e_not_enough_arguments_for_function_str),
944 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100945 return FAIL;
946 }
947
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100948 // The function has been compiled, can call it quickly. For a function
949 // that was defined later: we can call it directly next time.
950 if (iptr != NULL)
951 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100952 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100953 iptr->isn_type = ISN_DCALL;
954 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
955 iptr->isn_arg.dfunc.cdf_argcount = argcount;
956 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200957 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 if (call_prepare(argcount, argvars, ectx) == FAIL)
961 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200962 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000963 funcexe.fe_evaluate = TRUE;
964 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965
966 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000968 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969
970 // Clear the arguments.
971 for (idx = 0; idx < argcount; ++idx)
972 clear_tv(&argvars[idx]);
973
974 if (error != FCERR_NONE)
975 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000976 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 return FAIL;
978 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100979 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200980 // Error other than from calling the function itself.
981 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 return OK;
983}
984
985/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100986 * If command modifiers were applied restore them.
987 */
988 static void
989may_restore_cmdmod(funclocal_T *funclocal)
990{
991 if (funclocal->floc_restore_cmdmod)
992 {
993 cmdmod.cmod_filter_regmatch.regprog = NULL;
994 undo_cmdmod(&cmdmod);
995 cmdmod = funclocal->floc_save_cmdmod;
996 funclocal->floc_restore_cmdmod = FALSE;
997 }
998}
999
1000/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001001 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1002 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001003 */
1004 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001005vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001006{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001007 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001008}
1009
1010/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 * Execute a function by "name".
1012 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001013 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 * Returns FAIL if not found without an error message.
1015 */
1016 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001017call_by_name(
1018 char_u *name,
1019 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001020 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001021 isn_T *iptr,
1022 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023{
1024 ufunc_T *ufunc;
1025
1026 if (builtin_function(name, -1))
1027 {
1028 int func_idx = find_internal_func(name);
1029
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001030 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001032 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 return FAIL;
1034 return call_bfunc(func_idx, argcount, ectx);
1035 }
1036
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001037 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001038
1039 if (ufunc == NULL)
1040 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001041 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001042
1043 if (script_autoload(name, TRUE))
1044 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001045 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001046
1047 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001048 return FAIL; // bail out if loading the script caused an error
1049 }
1050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001052 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001053 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001054 {
1055 int i;
1056 typval_T *argv = STACK_TV_BOT(0) - argcount;
1057
1058 // The function can change at runtime, check that the argument
1059 // types are correct.
1060 for (i = 0; i < argcount; ++i)
1061 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001062 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001063
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001064 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001065 type = ufunc->uf_arg_types[i];
1066 else if (ufunc->uf_va_type != NULL)
1067 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001068 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001069 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001070 return FAIL;
1071 }
1072 }
1073
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001074 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076
1077 return FAIL;
1078}
1079
1080 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001081call_partial(
1082 typval_T *tv,
1083 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001084 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001086 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001087 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001089 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001090 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091
1092 if (tv->v_type == VAR_PARTIAL)
1093 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001094 partial_T *pt = tv->vval.v_partial;
1095 int i;
1096
1097 if (pt->pt_argc > 0)
1098 {
1099 // Make space for arguments from the partial, shift the "argcount"
1100 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001101 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001102 return FAIL;
1103 for (i = 1; i <= argcount; ++i)
1104 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1105 ectx->ec_stack.ga_len += pt->pt_argc;
1106 argcount += pt->pt_argc;
1107
1108 // copy the arguments from the partial onto the stack
1109 for (i = 0; i < pt->pt_argc; ++i)
1110 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1111 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001112 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113
1114 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001115 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 name = pt->pt_name;
1118 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001119 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001121 if (name != NULL)
1122 {
1123 char_u fname_buf[FLEN_FIXED + 1];
1124 char_u *tofree = NULL;
1125 int error = FCERR_NONE;
1126 char_u *fname;
1127
1128 // May need to translate <SNR>123_ to K_SNR.
1129 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1130 if (error != FCERR_NONE)
1131 res = FAIL;
1132 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001133 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001134 vim_free(tofree);
1135 }
1136
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001137 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 {
1139 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001140 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001141 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 return FAIL;
1143 }
1144 return OK;
1145}
1146
1147/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001148 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1149 * TRUE.
1150 */
1151 static int
1152error_if_locked(int lock, char *error)
1153{
1154 if (lock & (VAR_LOCKED | VAR_FIXED))
1155 {
1156 emsg(_(error));
1157 return TRUE;
1158 }
1159 return FALSE;
1160}
1161
1162/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001163 * Give an error if "tv" is not a number and return FAIL.
1164 */
1165 static int
1166check_for_number(typval_T *tv)
1167{
1168 if (tv->v_type != VAR_NUMBER)
1169 {
1170 semsg(_(e_expected_str_but_got_str),
1171 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1172 return FAIL;
1173 }
1174 return OK;
1175}
1176
1177/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001178 * Store "tv" in variable "name".
1179 * This is for s: and g: variables.
1180 */
1181 static void
1182store_var(char_u *name, typval_T *tv)
1183{
1184 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001185 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001186
Bram Moolenaard877a572021-04-01 19:42:48 +02001187 if (tv->v_lock)
1188 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001189 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001190 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001191 restore_funccal();
1192}
1193
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001194/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001195 * Convert "tv" to a string.
1196 * Return FAIL if not allowed.
1197 */
1198 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001199do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001200{
1201 if (tv->v_type != VAR_STRING)
1202 {
1203 char_u *str;
1204
1205 if (is_2string_any)
1206 {
1207 switch (tv->v_type)
1208 {
1209 case VAR_SPECIAL:
1210 case VAR_BOOL:
1211 case VAR_NUMBER:
1212 case VAR_FLOAT:
1213 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001214
1215 case VAR_LIST:
1216 if (tolerant)
1217 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001218 char_u *s, *e, *p;
1219 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001220
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001221 ga_init2(&ga, sizeof(char_u *), 1);
1222
1223 // Convert to NL separated items, then
1224 // escape the items and replace the NL with
1225 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001226 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001227 if (str == NULL)
1228 return FAIL;
1229 s = str;
1230 while ((e = vim_strchr(s, '\n')) != NULL)
1231 {
1232 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001233 p = vim_strsave_fnameescape(s,
1234 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001235 if (p != NULL)
1236 {
1237 ga_concat(&ga, p);
1238 ga_concat(&ga, (char_u *)" ");
1239 vim_free(p);
1240 }
1241 s = e + 1;
1242 }
1243 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001244 clear_tv(tv);
1245 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001246 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001247 return OK;
1248 }
1249 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001250 default: to_string_error(tv->v_type);
1251 return FAIL;
1252 }
1253 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001254 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001255 clear_tv(tv);
1256 tv->v_type = VAR_STRING;
1257 tv->vval.v_string = str;
1258 }
1259 return OK;
1260}
1261
1262/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001263 * When the value of "sv" is a null list of dict, allocate it.
1264 */
1265 static void
1266allocate_if_null(typval_T *tv)
1267{
1268 switch (tv->v_type)
1269 {
1270 case VAR_LIST:
1271 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001272 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001273 break;
1274 case VAR_DICT:
1275 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001276 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001277 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001278 case VAR_BLOB:
1279 if (tv->vval.v_blob == NULL)
1280 (void)rettv_blob_alloc(tv);
1281 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001282 default:
1283 break;
1284 }
1285}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001286
Bram Moolenaare7525c52021-01-09 13:20:37 +01001287/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001288 * Return the character "str[index]" where "index" is the character index,
1289 * including composing characters.
1290 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001291 */
1292 char_u *
1293char_from_string(char_u *str, varnumber_T index)
1294{
1295 size_t nbyte = 0;
1296 varnumber_T nchar = index;
1297 size_t slen;
1298
1299 if (str == NULL)
1300 return NULL;
1301 slen = STRLEN(str);
1302
Bram Moolenaarff871402021-03-26 13:34:05 +01001303 // Do the same as for a list: a negative index counts from the end.
1304 // Optimization to check the first byte to be below 0x80 (and no composing
1305 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001306 if (index < 0)
1307 {
1308 int clen = 0;
1309
1310 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001311 {
1312 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1313 ++nbyte;
1314 else if (enc_utf8)
1315 nbyte += utfc_ptr2len(str + nbyte);
1316 else
1317 nbyte += mb_ptr2len(str + nbyte);
1318 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001319 nchar = clen + index;
1320 if (nchar < 0)
1321 // unlike list: index out of range results in empty string
1322 return NULL;
1323 }
1324
1325 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001326 {
1327 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1328 ++nbyte;
1329 else if (enc_utf8)
1330 nbyte += utfc_ptr2len(str + nbyte);
1331 else
1332 nbyte += mb_ptr2len(str + nbyte);
1333 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001334 if (nbyte >= slen)
1335 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001336 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001337}
1338
1339/*
1340 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001341 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001342 * If going over the end return "str_len".
1343 * If "idx" is negative count from the end, -1 is the last character.
1344 * When going over the start return -1.
1345 */
1346 static long
1347char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1348{
1349 varnumber_T nchar = idx;
1350 size_t nbyte = 0;
1351
1352 if (nchar >= 0)
1353 {
1354 while (nchar > 0 && nbyte < str_len)
1355 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001356 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001357 --nchar;
1358 }
1359 }
1360 else
1361 {
1362 nbyte = str_len;
1363 while (nchar < 0 && nbyte > 0)
1364 {
1365 --nbyte;
1366 nbyte -= mb_head_off(str, str + nbyte);
1367 ++nchar;
1368 }
1369 if (nchar < 0)
1370 return -1;
1371 }
1372 return (long)nbyte;
1373}
1374
1375/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001376 * Return the slice "str[first : last]" using character indexes. Composing
1377 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001378 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001379 * Return NULL when the result is empty.
1380 */
1381 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001382string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001383{
1384 long start_byte, end_byte;
1385 size_t slen;
1386
1387 if (str == NULL)
1388 return NULL;
1389 slen = STRLEN(str);
1390 start_byte = char_idx2byte(str, slen, first);
1391 if (start_byte < 0)
1392 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001393 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001394 end_byte = (long)slen;
1395 else
1396 {
1397 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001398 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001399 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001400 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001401 }
1402
1403 if (start_byte >= (long)slen || end_byte <= start_byte)
1404 return NULL;
1405 return vim_strnsave(str + start_byte, end_byte - start_byte);
1406}
1407
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001408/*
1409 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1410 * When "dfunc_idx" is negative don't give an error.
1411 * Returns NULL for an error.
1412 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001413 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001414get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001415{
1416 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001417 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1418 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001419 svar_T *sv;
1420
1421 if (sref->sref_seq != si->sn_script_seq)
1422 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001423 // The script was reloaded after the function was compiled, the
1424 // script_idx may not be valid.
1425 if (dfunc != NULL)
1426 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1427 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001428 return NULL;
1429 }
1430 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001431 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001432 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001433 if (dfunc != NULL)
1434 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001435 return NULL;
1436 }
1437 return sv;
1438}
1439
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001440/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001441 * Function passed to do_cmdline() for splitting a script joined by NL
1442 * characters.
1443 */
1444 static char_u *
1445get_split_sourceline(
1446 int c UNUSED,
1447 void *cookie,
1448 int indent UNUSED,
1449 getline_opt_T options UNUSED)
1450{
1451 source_cookie_T *sp = (source_cookie_T *)cookie;
1452 char_u *p;
1453 char_u *line;
1454
Bram Moolenaar20677332021-06-06 17:02:53 +02001455 p = vim_strchr(sp->nextline, '\n');
1456 if (p == NULL)
1457 {
1458 line = vim_strsave(sp->nextline);
1459 sp->nextline += STRLEN(sp->nextline);
1460 }
1461 else
1462 {
1463 line = vim_strnsave(sp->nextline, p - sp->nextline);
1464 sp->nextline = p + 1;
1465 }
1466 return line;
1467}
1468
1469/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 * Execute a function by "name".
1471 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001472 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 */
1474 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001475call_eval_func(
1476 char_u *name,
1477 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001478 ectx_T *ectx,
1479 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480{
Bram Moolenaared677f52020-08-12 16:38:10 +02001481 int called_emsg_before = called_emsg;
1482 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001484 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001485 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001487 dictitem_T *v;
1488
1489 v = find_var(name, NULL, FALSE);
1490 if (v == NULL)
1491 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001492 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001493 return FAIL;
1494 }
1495 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1496 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001497 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001498 return FAIL;
1499 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001500 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001502 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503}
1504
1505/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001506 * When a function reference is used, fill a partial with the information
1507 * needed, especially when it is used as a closure.
1508 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001509 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001510fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1511{
1512 pt->pt_func = ufunc;
1513 pt->pt_refcount = 1;
1514
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001515 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001516 {
1517 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1518 + ectx->ec_dfunc_idx;
1519
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001520 // The closure may need to find arguments and local variables in the
1521 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001522 pt->pt_outer.out_stack = &ectx->ec_stack;
1523 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001524 if (ectx->ec_outer_ref != NULL)
1525 {
1526 // The current context already has a context, link to that one.
1527 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1528 if (ectx->ec_outer_ref->or_partial != NULL)
1529 {
1530 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1531 ++pt->pt_outer.out_up_partial->pt_refcount;
1532 }
1533 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001534
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001535 // If this function returns and the closure is still being used, we
1536 // need to make a copy of the context (arguments and local variables).
1537 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001538 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001539 {
1540 vim_free(pt);
1541 return FAIL;
1542 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001543 // Extra variable keeps the count of closures created in the current
1544 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001545 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1546 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1547
1548 ((partial_T **)ectx->ec_funcrefs.ga_data)
1549 [ectx->ec_funcrefs.ga_len] = pt;
1550 ++pt->pt_refcount;
1551 ++ectx->ec_funcrefs.ga_len;
1552 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001553 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001554 return OK;
1555}
1556
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001557/*
1558 * Execute iptr->isn_arg.string as an Ex command.
1559 */
1560 static int
1561exec_command(isn_T *iptr)
1562{
1563 source_cookie_T cookie;
1564
1565 SOURCING_LNUM = iptr->isn_lnum;
1566 // Pass getsourceline to get an error for a missing ":end"
1567 // command.
1568 CLEAR_FIELD(cookie);
1569 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1570 if (do_cmdline(iptr->isn_arg.string,
1571 getsourceline, &cookie,
1572 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1573 || did_emsg)
1574 return FAIL;
1575 return OK;
1576}
1577
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001578// used for v_instr of typval of VAR_INSTR
1579struct instr_S {
1580 ectx_T *instr_ectx;
1581 isn_T *instr_instr;
1582};
1583
Bram Moolenaar4c137212021-04-19 16:48:48 +02001584// used for substitute_instr
1585typedef struct subs_expr_S {
1586 ectx_T *subs_ectx;
1587 isn_T *subs_instr;
1588 int subs_status;
1589} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590
1591// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001592#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593
1594// Get pointer to item at the bottom of the stack, -1 is the bottom.
1595#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001596#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 +01001597
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001598// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001599#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 +01001600
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001601// Set when calling do_debug().
1602static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001603static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001604
1605/*
1606 * When debugging lookup "name" and return the typeval.
1607 * When not found return NULL.
1608 */
1609 typval_T *
1610lookup_debug_var(char_u *name)
1611{
1612 int idx;
1613 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001614 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001615 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001616 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001617
1618 if (ectx == NULL)
1619 return NULL;
1620 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1621
1622 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001623 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001624 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001625 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1626
1627 // the variable name may be NULL when not available in this block
1628 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001629 return STACK_TV_VAR(idx);
1630 }
1631
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001632 // Go through argument names.
1633 ufunc = dfunc->df_ufunc;
1634 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1635 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1636 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1637 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1638 - varargs_off + idx);
1639 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1640 return STACK_TV(ectx->ec_frame_idx - 1);
1641
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001642 return NULL;
1643}
1644
Bram Moolenaar26a44842021-09-02 18:49:06 +02001645/*
1646 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1647 * breakpoint was set in that function or when there is any expression.
1648 */
1649 int
1650may_break_in_function(ufunc_T *ufunc)
1651{
1652 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1653}
1654
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001655 static void
1656handle_debug(isn_T *iptr, ectx_T *ectx)
1657{
1658 char_u *line;
1659 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1660 + ectx->ec_dfunc_idx)->df_ufunc;
1661 isn_T *ni;
1662 int end_lnum = iptr->isn_lnum;
1663 garray_T ga;
1664 int lnum;
1665
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001666 if (ex_nesting_level > debug_break_level)
1667 {
1668 linenr_T breakpoint;
1669
Bram Moolenaar26a44842021-09-02 18:49:06 +02001670 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001671 return;
1672
1673 // check for the next breakpoint if needed
1674 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001675 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001676 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1677 return;
1678 }
1679
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001680 SOURCING_LNUM = iptr->isn_lnum;
1681 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001682 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001683
1684 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1685 if (ni->isn_type == ISN_DEBUG
1686 || ni->isn_type == ISN_RETURN
1687 || ni->isn_type == ISN_RETURN_VOID)
1688 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001689 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001690 break;
1691 }
1692
1693 if (end_lnum > iptr->isn_lnum)
1694 {
1695 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001696 for (lnum = iptr->isn_lnum; lnum < end_lnum
1697 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001698 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001699 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001700
Bram Moolenaar303215d2021-07-07 20:10:43 +02001701 if (p == NULL)
1702 continue; // left over from continuation line
1703 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001704 if (*p == '#')
1705 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001706 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001707 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1708 if (STRNCMP(p, "def ", 4) == 0)
1709 break;
1710 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001711 line = ga_concat_strings(&ga, " ");
1712 vim_free(ga.ga_data);
1713 }
1714 else
1715 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001716
Dominique Pelle6e969552021-06-17 13:53:41 +02001717 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001718 debug_context = NULL;
1719
1720 if (end_lnum > iptr->isn_lnum)
1721 vim_free(line);
1722}
1723
Bram Moolenaar4c137212021-04-19 16:48:48 +02001724/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001725 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001726 * Returns OK, FAIL or NOTDONE (uncatchable error).
1727 */
1728 static int
1729execute_storeindex(isn_T *iptr, ectx_T *ectx)
1730{
1731 vartype_T dest_type = iptr->isn_arg.vartype;
1732 typval_T *tv;
1733 typval_T *tv_idx = STACK_TV_BOT(-2);
1734 typval_T *tv_dest = STACK_TV_BOT(-1);
1735 int status = OK;
1736
1737 // Stack contains:
1738 // -3 value to be stored
1739 // -2 index
1740 // -1 dict or list
1741 tv = STACK_TV_BOT(-3);
1742 SOURCING_LNUM = iptr->isn_lnum;
1743 if (dest_type == VAR_ANY)
1744 {
1745 dest_type = tv_dest->v_type;
1746 if (dest_type == VAR_DICT)
1747 status = do_2string(tv_idx, TRUE, FALSE);
1748 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1749 {
1750 emsg(_(e_number_expected));
1751 status = FAIL;
1752 }
1753 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001754
1755 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001756 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001757 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001758 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001759 long lidx = (long)tv_idx->vval.v_number;
1760 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001761
Bram Moolenaare08be092022-02-17 13:08:26 +00001762 if (list == NULL)
1763 {
1764 emsg(_(e_list_not_set));
1765 return FAIL;
1766 }
1767 if (lidx < 0 && list->lv_len + lidx >= 0)
1768 // negative index is relative to the end
1769 lidx = list->lv_len + lidx;
1770 if (lidx < 0 || lidx > list->lv_len)
1771 {
1772 semsg(_(e_list_index_out_of_range_nr), lidx);
1773 return FAIL;
1774 }
1775 if (lidx < list->lv_len)
1776 {
1777 listitem_T *li = list_find(list, lidx);
1778
1779 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001780 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001781 return FAIL;
1782 // overwrite existing list item
1783 clear_tv(&li->li_tv);
1784 li->li_tv = *tv;
1785 }
1786 else
1787 {
1788 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1789 return FAIL;
1790 // append to list, only fails when out of memory
1791 if (list_append_tv(list, tv) == FAIL)
1792 return NOTDONE;
1793 clear_tv(tv);
1794 }
1795 }
1796 else if (dest_type == VAR_DICT)
1797 {
1798 char_u *key = tv_idx->vval.v_string;
1799 dict_T *dict = tv_dest->vval.v_dict;
1800 dictitem_T *di;
1801
1802 SOURCING_LNUM = iptr->isn_lnum;
1803 if (dict == NULL)
1804 {
1805 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001806 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001807 }
1808 if (key == NULL)
1809 key = (char_u *)"";
1810 di = dict_find(dict, key, -1);
1811 if (di != NULL)
1812 {
1813 if (error_if_locked(di->di_tv.v_lock,
1814 e_cannot_change_dict_item))
1815 return FAIL;
1816 // overwrite existing value
1817 clear_tv(&di->di_tv);
1818 di->di_tv = *tv;
1819 }
1820 else
1821 {
1822 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1823 return FAIL;
1824 // add to dict, only fails when out of memory
1825 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1826 return NOTDONE;
1827 clear_tv(tv);
1828 }
1829 }
1830 else if (dest_type == VAR_BLOB)
1831 {
1832 long lidx = (long)tv_idx->vval.v_number;
1833 blob_T *blob = tv_dest->vval.v_blob;
1834 varnumber_T nr;
1835 int error = FALSE;
1836 int len;
1837
1838 if (blob == NULL)
1839 {
1840 emsg(_(e_blob_not_set));
1841 return FAIL;
1842 }
1843 len = blob_len(blob);
1844 if (lidx < 0 && len + lidx >= 0)
1845 // negative index is relative to the end
1846 lidx = len + lidx;
1847
1848 // Can add one byte at the end.
1849 if (lidx < 0 || lidx > len)
1850 {
1851 semsg(_(e_blob_index_out_of_range_nr), lidx);
1852 return FAIL;
1853 }
1854 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1855 return FAIL;
1856 nr = tv_get_number_chk(tv, &error);
1857 if (error)
1858 return FAIL;
1859 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001860 }
1861 else
1862 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001863 status = FAIL;
1864 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001865 }
1866 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001867
1868 clear_tv(tv_idx);
1869 clear_tv(tv_dest);
1870 ectx->ec_stack.ga_len -= 3;
1871 if (status == FAIL)
1872 {
1873 clear_tv(tv);
1874 return FAIL;
1875 }
1876 return OK;
1877}
1878
1879/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001880 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001881 */
1882 static int
1883execute_storerange(isn_T *iptr, ectx_T *ectx)
1884{
1885 typval_T *tv;
1886 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1887 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1888 typval_T *tv_dest = STACK_TV_BOT(-1);
1889 int status = OK;
1890
1891 // Stack contains:
1892 // -4 value to be stored
1893 // -3 first index or "none"
1894 // -2 second index or "none"
1895 // -1 destination list or blob
1896 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001897 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001898 if (tv_dest->v_type == VAR_LIST)
1899 {
1900 long n1;
1901 long n2;
1902 int error = FALSE;
1903
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001904 n1 = (long)tv_get_number_chk(tv_idx1, &error);
1905 if (error)
1906 status = FAIL;
1907 else
1908 {
1909 if (tv_idx2->v_type == VAR_SPECIAL
1910 && tv_idx2->vval.v_number == VVAL_NONE)
1911 n2 = list_len(tv_dest->vval.v_list) - 1;
1912 else
1913 n2 = (long)tv_get_number_chk(tv_idx2, &error);
1914 if (error)
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001915 status = FAIL; // cannot happen?
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001916 else
1917 {
1918 listitem_T *li1 = check_range_index_one(
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001919 tv_dest->vval.v_list, &n1, FALSE);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001920
1921 if (li1 == NULL)
1922 status = FAIL;
1923 else
1924 {
1925 status = check_range_index_two(
1926 tv_dest->vval.v_list,
1927 &n1, li1, &n2, FALSE);
1928 if (status != FAIL)
1929 status = list_assign_range(
1930 tv_dest->vval.v_list,
1931 tv->vval.v_list,
1932 n1,
1933 n2,
1934 tv_idx2->v_type == VAR_SPECIAL,
1935 (char_u *)"=",
1936 (char_u *)"[unknown]");
1937 }
1938 }
1939 }
1940 }
1941 else if (tv_dest->v_type == VAR_BLOB)
1942 {
1943 varnumber_T n1;
1944 varnumber_T n2;
1945 int error = FALSE;
1946
1947 n1 = tv_get_number_chk(tv_idx1, &error);
1948 if (error)
1949 status = FAIL;
1950 else
1951 {
1952 if (tv_idx2->v_type == VAR_SPECIAL
1953 && tv_idx2->vval.v_number == VVAL_NONE)
1954 n2 = blob_len(tv_dest->vval.v_blob) - 1;
1955 else
1956 n2 = tv_get_number_chk(tv_idx2, &error);
1957 if (error)
1958 status = FAIL;
1959 else
1960 {
1961 long bloblen = blob_len(tv_dest->vval.v_blob);
1962
1963 if (check_blob_index(bloblen,
1964 n1, FALSE) == FAIL
1965 || check_blob_range(bloblen,
1966 n1, n2, FALSE) == FAIL)
1967 status = FAIL;
1968 else
1969 status = blob_set_range(
1970 tv_dest->vval.v_blob, n1, n2, tv);
1971 }
1972 }
1973 }
1974 else
1975 {
1976 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001977 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001978 }
1979
1980 clear_tv(tv_idx1);
1981 clear_tv(tv_idx2);
1982 clear_tv(tv_dest);
1983 ectx->ec_stack.ga_len -= 4;
1984 clear_tv(tv);
1985
1986 return status;
1987}
1988
1989/*
1990 * Unlet item in list or dict variable.
1991 */
1992 static int
1993execute_unletindex(isn_T *iptr, ectx_T *ectx)
1994{
1995 typval_T *tv_idx = STACK_TV_BOT(-2);
1996 typval_T *tv_dest = STACK_TV_BOT(-1);
1997 int status = OK;
1998
1999 // Stack contains:
2000 // -2 index
2001 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002002 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002003 if (tv_dest->v_type == VAR_DICT)
2004 {
2005 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002006 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002007 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002008 semsg(_(e_expected_str_but_got_str),
2009 vartype_name(VAR_STRING),
2010 vartype_name(tv_idx->v_type));
2011 status = FAIL;
2012 }
2013 else
2014 {
2015 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002016 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002017 dictitem_T *di = NULL;
2018
2019 if (d != NULL && value_check_lock(
2020 d->dv_lock, NULL, FALSE))
2021 status = FAIL;
2022 else
2023 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002024 if (tv_idx->v_type == VAR_STRING)
2025 {
2026 key = tv_idx->vval.v_string;
2027 if (key == NULL)
2028 key = (char_u *)"";
2029 }
2030 else
2031 {
2032 key = tv_get_string(tv_idx);
2033 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002034 if (d != NULL)
2035 di = dict_find(d, key, (int)STRLEN(key));
2036 if (di == NULL)
2037 {
2038 // NULL dict is equivalent to empty dict
2039 semsg(_(e_key_not_present_in_dictionary),
2040 key);
2041 status = FAIL;
2042 }
2043 else if (var_check_fixed(di->di_flags,
2044 NULL, FALSE)
2045 || var_check_ro(di->di_flags,
2046 NULL, FALSE))
2047 status = FAIL;
2048 else
2049 dictitem_remove(d, di);
2050 }
2051 }
2052 }
2053 else if (tv_dest->v_type == VAR_LIST)
2054 {
2055 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002056 if (check_for_number(tv_idx) == FAIL)
2057 {
2058 status = FAIL;
2059 }
2060 else
2061 {
2062 list_T *l = tv_dest->vval.v_list;
2063 long n = (long)tv_idx->vval.v_number;
2064
2065 if (l != NULL && value_check_lock(
2066 l->lv_lock, NULL, FALSE))
2067 status = FAIL;
2068 else
2069 {
2070 listitem_T *li = list_find(l, n);
2071
2072 if (li == NULL)
2073 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002074 semsg(_(e_list_index_out_of_range_nr), n);
2075 status = FAIL;
2076 }
2077 else if (value_check_lock(li->li_tv.v_lock,
2078 NULL, FALSE))
2079 status = FAIL;
2080 else
2081 listitem_remove(l, li);
2082 }
2083 }
2084 }
2085 else
2086 {
2087 status = FAIL;
2088 semsg(_(e_cannot_index_str),
2089 vartype_name(tv_dest->v_type));
2090 }
2091
2092 clear_tv(tv_idx);
2093 clear_tv(tv_dest);
2094 ectx->ec_stack.ga_len -= 2;
2095
2096 return status;
2097}
2098
2099/*
2100 * Unlet a range of items in a list variable.
2101 */
2102 static int
2103execute_unletrange(isn_T *iptr, ectx_T *ectx)
2104{
2105 // Stack contains:
2106 // -3 index1
2107 // -2 index2
2108 // -1 dict or list
2109 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2110 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2111 typval_T *tv_dest = STACK_TV_BOT(-1);
2112 int status = OK;
2113
2114 if (tv_dest->v_type == VAR_LIST)
2115 {
2116 // indexes must be a number
2117 SOURCING_LNUM = iptr->isn_lnum;
2118 if (check_for_number(tv_idx1) == FAIL
2119 || (tv_idx2->v_type != VAR_SPECIAL
2120 && check_for_number(tv_idx2) == FAIL))
2121 {
2122 status = FAIL;
2123 }
2124 else
2125 {
2126 list_T *l = tv_dest->vval.v_list;
2127 long n1 = (long)tv_idx1->vval.v_number;
2128 long n2 = tv_idx2->v_type == VAR_SPECIAL
2129 ? 0 : (long)tv_idx2->vval.v_number;
2130 listitem_T *li;
2131
2132 li = list_find_index(l, &n1);
2133 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002134 {
2135 semsg(_(e_list_index_out_of_range_nr),
2136 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002137 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002138 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002139 else
2140 {
2141 if (n1 < 0)
2142 n1 = list_idx_of_item(l, li);
2143 if (n2 < 0)
2144 {
2145 listitem_T *li2 = list_find(l, n2);
2146
2147 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002148 {
2149 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002150 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002151 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002152 else
2153 n2 = list_idx_of_item(l, li2);
2154 }
2155 if (status != FAIL
2156 && tv_idx2->v_type != VAR_SPECIAL
2157 && n2 < n1)
2158 {
2159 semsg(_(e_list_index_out_of_range_nr), n2);
2160 status = FAIL;
2161 }
2162 if (status != FAIL
2163 && list_unlet_range(l, li, NULL, n1,
2164 tv_idx2->v_type != VAR_SPECIAL, n2)
2165 == FAIL)
2166 status = FAIL;
2167 }
2168 }
2169 }
2170 else
2171 {
2172 status = FAIL;
2173 SOURCING_LNUM = iptr->isn_lnum;
2174 semsg(_(e_cannot_index_str),
2175 vartype_name(tv_dest->v_type));
2176 }
2177
2178 clear_tv(tv_idx1);
2179 clear_tv(tv_idx2);
2180 clear_tv(tv_dest);
2181 ectx->ec_stack.ga_len -= 3;
2182
2183 return status;
2184}
2185
2186/*
2187 * Top of a for loop.
2188 */
2189 static int
2190execute_for(isn_T *iptr, ectx_T *ectx)
2191{
2192 typval_T *tv;
2193 typval_T *ltv = STACK_TV_BOT(-1);
2194 typval_T *idxtv =
2195 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2196
2197 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2198 return FAIL;
2199 if (ltv->v_type == VAR_LIST)
2200 {
2201 list_T *list = ltv->vval.v_list;
2202
2203 // push the next item from the list
2204 ++idxtv->vval.v_number;
2205 if (list == NULL
2206 || idxtv->vval.v_number >= list->lv_len)
2207 {
2208 // past the end of the list, jump to "endfor"
2209 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2210 may_restore_cmdmod(&ectx->ec_funclocal);
2211 }
2212 else if (list->lv_first == &range_list_item)
2213 {
2214 // non-materialized range() list
2215 tv = STACK_TV_BOT(0);
2216 tv->v_type = VAR_NUMBER;
2217 tv->v_lock = 0;
2218 tv->vval.v_number = list_find_nr(
2219 list, idxtv->vval.v_number, NULL);
2220 ++ectx->ec_stack.ga_len;
2221 }
2222 else
2223 {
2224 listitem_T *li = list_find(list,
2225 idxtv->vval.v_number);
2226
2227 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2228 ++ectx->ec_stack.ga_len;
2229 }
2230 }
2231 else if (ltv->v_type == VAR_STRING)
2232 {
2233 char_u *str = ltv->vval.v_string;
2234
2235 // The index is for the last byte of the previous
2236 // character.
2237 ++idxtv->vval.v_number;
2238 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2239 {
2240 // past the end of the string, jump to "endfor"
2241 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2242 may_restore_cmdmod(&ectx->ec_funclocal);
2243 }
2244 else
2245 {
2246 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2247
2248 // Push the next character from the string.
2249 tv = STACK_TV_BOT(0);
2250 tv->v_type = VAR_STRING;
2251 tv->vval.v_string = vim_strnsave(
2252 str + idxtv->vval.v_number, clen);
2253 ++ectx->ec_stack.ga_len;
2254 idxtv->vval.v_number += clen - 1;
2255 }
2256 }
2257 else if (ltv->v_type == VAR_BLOB)
2258 {
2259 blob_T *blob = ltv->vval.v_blob;
2260
2261 // When we get here the first time make a copy of the
2262 // blob, so that the iteration still works when it is
2263 // changed.
2264 if (idxtv->vval.v_number == -1 && blob != NULL)
2265 {
2266 blob_copy(blob, ltv);
2267 blob_unref(blob);
2268 blob = ltv->vval.v_blob;
2269 }
2270
2271 // The index is for the previous byte.
2272 ++idxtv->vval.v_number;
2273 if (blob == NULL
2274 || idxtv->vval.v_number >= blob_len(blob))
2275 {
2276 // past the end of the blob, jump to "endfor"
2277 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2278 may_restore_cmdmod(&ectx->ec_funclocal);
2279 }
2280 else
2281 {
2282 // Push the next byte from the blob.
2283 tv = STACK_TV_BOT(0);
2284 tv->v_type = VAR_NUMBER;
2285 tv->vval.v_number = blob_get(blob,
2286 idxtv->vval.v_number);
2287 ++ectx->ec_stack.ga_len;
2288 }
2289 }
2290 else
2291 {
2292 semsg(_(e_for_loop_on_str_not_supported),
2293 vartype_name(ltv->v_type));
2294 return FAIL;
2295 }
2296 return OK;
2297}
2298
2299/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002300 * Load instruction for w:/b:/g:/t: variable.
2301 * "isn_type" is used instead of "iptr->isn_type".
2302 */
2303 static int
2304load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2305{
2306 dictitem_T *di = NULL;
2307 hashtab_T *ht = NULL;
2308 char namespace;
2309
2310 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2311 return NOTDONE;
2312 switch (isn_type)
2313 {
2314 case ISN_LOADG:
2315 ht = get_globvar_ht();
2316 namespace = 'g';
2317 break;
2318 case ISN_LOADB:
2319 ht = &curbuf->b_vars->dv_hashtab;
2320 namespace = 'b';
2321 break;
2322 case ISN_LOADW:
2323 ht = &curwin->w_vars->dv_hashtab;
2324 namespace = 'w';
2325 break;
2326 case ISN_LOADT:
2327 ht = &curtab->tp_vars->dv_hashtab;
2328 namespace = 't';
2329 break;
2330 default: // Cannot reach here
2331 return NOTDONE;
2332 }
2333 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2334
Bram Moolenaar06b77222022-01-25 15:51:56 +00002335 if (di == NULL)
2336 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002337 if (isn_type == ISN_LOADG)
2338 {
2339 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2340
2341 // g:Something could be a function
2342 if (ufunc != NULL)
2343 {
2344 typval_T *tv = STACK_TV_BOT(0);
2345
2346 ++ectx->ec_stack.ga_len;
2347 tv->v_type = VAR_FUNC;
2348 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2349 if (tv->vval.v_string == NULL)
2350 return FAIL;
2351 STRCPY(tv->vval.v_string, "g:");
2352 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2353 return OK;
2354 }
2355 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002356 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002357 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002358 // no check if the item exists in the script but
2359 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002360 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002361 else
2362 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002363 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002364 return FAIL;
2365 }
2366 else
2367 {
2368 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2369 ++ectx->ec_stack.ga_len;
2370 }
2371 return OK;
2372}
2373
2374/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002375 * Execute instructions in execution context "ectx".
2376 * Return OK or FAIL;
2377 */
2378 static int
2379exec_instructions(ectx_T *ectx)
2380{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002381 int ret = FAIL;
2382 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002383 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002384
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002385 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002386 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002387
Bram Moolenaarff652882021-05-16 15:24:49 +02002388 // Only catch exceptions in this instruction list.
2389 ectx->ec_trylevel_at_start = trylevel;
2390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 for (;;)
2392 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002393 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002395 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002396
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002397 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002398 {
2399 line_breakcheck();
2400 breakcheck_count = 0;
2401 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002402 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002403 {
2404 // Turn CTRL-C into an exception.
2405 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002406 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002407 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002408 did_throw = TRUE;
2409 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002411 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002412 {
2413 // Turn an error message into an exception.
2414 did_emsg = FALSE;
2415 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002416 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002417 did_throw = TRUE;
2418 *msg_list = NULL;
2419 }
2420
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002421 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002423 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002424 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002425 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426
2427 // An exception jumps to the first catch, finally, or returns from
2428 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002429 while (index > 0)
2430 {
2431 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002432 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002433 break;
2434 // In the catch and finally block of this try we have to go up
2435 // one level.
2436 --index;
2437 trycmd = NULL;
2438 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002439 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002441 if (trycmd->tcd_in_catch)
2442 {
2443 // exception inside ":catch", jump to ":finally" once
2444 ectx->ec_iidx = trycmd->tcd_finally_idx;
2445 trycmd->tcd_finally_idx = 0;
2446 }
2447 else
2448 // jump to first ":catch"
2449 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002450 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002451 did_throw = FALSE; // don't come back here until :endtry
2452 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 }
2454 else
2455 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002456 // Not inside try or need to return from current functions.
2457 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002458 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002459 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002460 tv = STACK_TV_BOT(0);
2461 tv->v_type = VAR_NUMBER;
2462 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002463 ++ectx->ec_stack.ga_len;
2464 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002466 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002467 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002468 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002469 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 goto done;
2471 }
2472
Bram Moolenaar4c137212021-04-19 16:48:48 +02002473 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002474 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 }
2476 continue;
2477 }
2478
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002479 /*
2480 * Big switch on the instruction. Most compilers will be turning this
2481 * into an efficient lookup table, since the "case" values are an enum
2482 * with sequential numbers. It may look ugly, but it should be the
2483 * most efficient way.
2484 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002485 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 switch (iptr->isn_type)
2487 {
2488 // execute Ex command line
2489 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002490 if (exec_command(iptr) == FAIL)
2491 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 break;
2493
Bram Moolenaar20677332021-06-06 17:02:53 +02002494 // execute Ex command line split at NL characters.
2495 case ISN_EXEC_SPLIT:
2496 {
2497 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002498 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002499
2500 SOURCING_LNUM = iptr->isn_lnum;
2501 CLEAR_FIELD(cookie);
2502 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2503 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002504 line = get_split_sourceline(0, &cookie, 0, 0);
2505 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002506 get_split_sourceline, &cookie,
2507 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2508 == FAIL
2509 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002510 {
2511 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002512 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002513 }
2514 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002515 }
2516 break;
2517
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002518 // execute Ex command line that is only a range
2519 case ISN_EXECRANGE:
2520 {
2521 exarg_T ea;
2522 char *error = NULL;
2523
2524 CLEAR_FIELD(ea);
2525 ea.cmdidx = CMD_SIZE;
2526 ea.addr_type = ADDR_LINES;
2527 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002528 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002529 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002530 if (ea.cmd == NULL)
2531 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002532 // error is always NULL when using ADDR_LINES
2533 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002534 if (error != NULL)
2535 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002536 emsg(error);
2537 goto on_error;
2538 }
2539 }
2540 break;
2541
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002542 // Evaluate an expression with legacy syntax, push it onto the
2543 // stack.
2544 case ISN_LEGACY_EVAL:
2545 {
2546 char_u *arg = iptr->isn_arg.string;
2547 int res;
2548 int save_flags = cmdmod.cmod_flags;
2549
Bram Moolenaar35578162021-08-02 19:10:38 +02002550 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002551 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002552 tv = STACK_TV_BOT(0);
2553 init_tv(tv);
2554 cmdmod.cmod_flags |= CMOD_LEGACY;
2555 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2556 cmdmod.cmod_flags = save_flags;
2557 if (res == FAIL)
2558 goto on_error;
2559 ++ectx->ec_stack.ga_len;
2560 }
2561 break;
2562
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002563 // push typeval VAR_INSTR with instructions to be executed
2564 case ISN_INSTR:
2565 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002566 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002567 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002568 tv = STACK_TV_BOT(0);
2569 tv->vval.v_instr = ALLOC_ONE(instr_T);
2570 if (tv->vval.v_instr == NULL)
2571 goto on_error;
2572 ++ectx->ec_stack.ga_len;
2573
2574 tv->v_type = VAR_INSTR;
2575 tv->vval.v_instr->instr_ectx = ectx;
2576 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2577 }
2578 break;
2579
Bram Moolenaar4c137212021-04-19 16:48:48 +02002580 // execute :substitute with an expression
2581 case ISN_SUBSTITUTE:
2582 {
2583 subs_T *subs = &iptr->isn_arg.subs;
2584 source_cookie_T cookie;
2585 struct subs_expr_S *save_instr = substitute_instr;
2586 struct subs_expr_S subs_instr;
2587 int res;
2588
2589 subs_instr.subs_ectx = ectx;
2590 subs_instr.subs_instr = subs->subs_instr;
2591 subs_instr.subs_status = OK;
2592 substitute_instr = &subs_instr;
2593
2594 SOURCING_LNUM = iptr->isn_lnum;
2595 // This is very much like ISN_EXEC
2596 CLEAR_FIELD(cookie);
2597 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2598 res = do_cmdline(subs->subs_cmd,
2599 getsourceline, &cookie,
2600 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2601 substitute_instr = save_instr;
2602
2603 if (res == FAIL || did_emsg
2604 || subs_instr.subs_status == FAIL)
2605 goto on_error;
2606 }
2607 break;
2608
2609 case ISN_FINISH:
2610 goto done;
2611
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002612 case ISN_REDIRSTART:
2613 // create a dummy entry for var_redir_str()
2614 if (alloc_redir_lval() == FAIL)
2615 goto on_error;
2616
2617 // The output is stored in growarray "redir_ga" until
2618 // redirection ends.
2619 init_redir_ga();
2620 redir_vname = 1;
2621 break;
2622
2623 case ISN_REDIREND:
2624 {
2625 char_u *res = get_clear_redir_ga();
2626
2627 // End redirection, put redirected text on the stack.
2628 clear_redir_lval();
2629 redir_vname = 0;
2630
Bram Moolenaar35578162021-08-02 19:10:38 +02002631 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002632 {
2633 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002634 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002635 }
2636 tv = STACK_TV_BOT(0);
2637 tv->v_type = VAR_STRING;
2638 tv->vval.v_string = res;
2639 ++ectx->ec_stack.ga_len;
2640 }
2641 break;
2642
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002643 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002644#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002645 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2646 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002647#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002648 break;
2649
2650 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002651#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002652 {
2653 exarg_T ea;
2654 int res;
2655
2656 CLEAR_FIELD(ea);
2657 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2658 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2659 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2660 --ectx->ec_stack.ga_len;
2661 tv = STACK_TV_BOT(0);
2662 res = cexpr_core(&ea, tv);
2663 clear_tv(tv);
2664 if (res == FAIL)
2665 goto on_error;
2666 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002667#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002668 break;
2669
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002670 // execute Ex command from pieces on the stack
2671 case ISN_EXECCONCAT:
2672 {
2673 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002674 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002675 int pass;
2676 int i;
2677 char_u *cmd = NULL;
2678 char_u *str;
2679
2680 for (pass = 1; pass <= 2; ++pass)
2681 {
2682 for (i = 0; i < count; ++i)
2683 {
2684 tv = STACK_TV_BOT(i - count);
2685 str = tv->vval.v_string;
2686 if (str != NULL && *str != NUL)
2687 {
2688 if (pass == 2)
2689 STRCPY(cmd + len, str);
2690 len += STRLEN(str);
2691 }
2692 if (pass == 2)
2693 clear_tv(tv);
2694 }
2695 if (pass == 1)
2696 {
2697 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002698 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002699 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002700 len = 0;
2701 }
2702 }
2703
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002704 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002705 do_cmdline_cmd(cmd);
2706 vim_free(cmd);
2707 }
2708 break;
2709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 // execute :echo {string} ...
2711 case ISN_ECHO:
2712 {
2713 int count = iptr->isn_arg.echo.echo_count;
2714 int atstart = TRUE;
2715 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002716 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717
2718 for (idx = 0; idx < count; ++idx)
2719 {
2720 tv = STACK_TV_BOT(idx - count);
2721 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2722 &atstart, &needclr);
2723 clear_tv(tv);
2724 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002725 if (needclr)
2726 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002727 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 }
2729 break;
2730
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002731 // :execute {string} ...
2732 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002733 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002734 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002735 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002736 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002737 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002738 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002739 {
2740 int count = iptr->isn_arg.number;
2741 garray_T ga;
2742 char_u buf[NUMBUFLEN];
2743 char_u *p;
2744 int len;
2745 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002746 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002747
2748 ga_init2(&ga, 1, 80);
2749 for (idx = 0; idx < count; ++idx)
2750 {
2751 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002752 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002753 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002754 if (tv->v_type == VAR_CHANNEL
2755 || tv->v_type == VAR_JOB)
2756 {
2757 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002758 semsg(_(e_using_invalid_value_as_string_str),
2759 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002760 break;
2761 }
2762 else
2763 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002764 }
2765 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002766 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002767
2768 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002769 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002770 failed = TRUE;
2771 else
2772 {
2773 if (ga.ga_len > 0)
2774 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2775 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2776 ga.ga_len += len;
2777 }
2778 clear_tv(tv);
2779 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002780 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002781 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002782 {
2783 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002784 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002785 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002786
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002787 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002788 {
2789 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002790 {
2791 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002792 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002793 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002794 {
2795 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002796 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002797 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002798 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002799 else
2800 {
2801 msg_sb_eol();
2802 if (iptr->isn_type == ISN_ECHOMSG)
2803 {
2804 msg_attr(ga.ga_data, echo_attr);
2805 out_flush();
2806 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002807 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2808 {
2809 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2810 TRUE);
2811 ui_write((char_u *)"\r\n", 2, TRUE);
2812 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002813 else
2814 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002815 SOURCING_LNUM = iptr->isn_lnum;
2816 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002817 }
2818 }
2819 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002820 ga_clear(&ga);
2821 }
2822 break;
2823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 // load local variable or argument
2825 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002826 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002827 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002829 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 break;
2831
2832 // load v: variable
2833 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002834 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002835 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002837 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 break;
2839
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002840 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 case ISN_LOADSCRIPT:
2842 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002843 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 svar_T *sv;
2845
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002846 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002847 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002848 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002849 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002850 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002851 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002853 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 }
2855 break;
2856
2857 // load s: variable in old script
2858 case ISN_LOADS:
2859 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002860 hashtab_T *ht = &SCRIPT_VARS(
2861 iptr->isn_arg.loadstore.ls_sid);
2862 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 if (di == NULL)
2866 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002867 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002868 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002869 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 }
2871 else
2872 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002873 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002874 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002876 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 }
2878 }
2879 break;
2880
Bram Moolenaard3aac292020-04-19 14:32:17 +02002881 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002883 case ISN_LOADB:
2884 case ISN_LOADW:
2885 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002887 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002888
Bram Moolenaar06b77222022-01-25 15:51:56 +00002889 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002890 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002891 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002892 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 break;
2896
Bram Moolenaar03290b82020-12-19 16:30:44 +01002897 // load autoload variable
2898 case ISN_LOADAUTO:
2899 {
2900 char_u *name = iptr->isn_arg.string;
2901
Bram Moolenaar35578162021-08-02 19:10:38 +02002902 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002903 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002904 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002905 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002906 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002907 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002908 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002909 }
2910 break;
2911
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002912 // load g:/b:/w:/t: namespace
2913 case ISN_LOADGDICT:
2914 case ISN_LOADBDICT:
2915 case ISN_LOADWDICT:
2916 case ISN_LOADTDICT:
2917 {
2918 dict_T *d = NULL;
2919
2920 switch (iptr->isn_type)
2921 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002922 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2923 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2924 case ISN_LOADWDICT: d = curwin->w_vars; break;
2925 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002926 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002927 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002928 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002929 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002930 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002931 tv = STACK_TV_BOT(0);
2932 tv->v_type = VAR_DICT;
2933 tv->v_lock = 0;
2934 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002935 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002937 }
2938 break;
2939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 // load &option
2941 case ISN_LOADOPT:
2942 {
2943 typval_T optval;
2944 char_u *name = iptr->isn_arg.string;
2945
Bram Moolenaara8c17702020-04-01 21:17:24 +02002946 // This is not expected to fail, name is checked during
2947 // compilation: don't set SOURCING_LNUM.
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 Moolenaar9a78e6d2020-07-01 18:29:55 +02002950 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002951 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002953 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 }
2955 break;
2956
2957 // load $ENV
2958 case ISN_LOADENV:
2959 {
2960 typval_T optval;
2961 char_u *name = iptr->isn_arg.string;
2962
Bram Moolenaar35578162021-08-02 19:10:38 +02002963 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002964 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002965 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002966 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002968 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 }
2970 break;
2971
2972 // load @register
2973 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002974 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002975 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 tv = STACK_TV_BOT(0);
2977 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002978 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002979 // This may result in NULL, which should be equivalent to an
2980 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 tv->vval.v_string = get_reg_contents(
2982 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002983 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 break;
2985
2986 // store local variable
2987 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002988 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 tv = STACK_TV_VAR(iptr->isn_arg.number);
2990 clear_tv(tv);
2991 *tv = *STACK_TV_BOT(0);
2992 break;
2993
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002994 // store s: variable in old script
2995 case ISN_STORES:
2996 {
2997 hashtab_T *ht = &SCRIPT_VARS(
2998 iptr->isn_arg.loadstore.ls_sid);
2999 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003000 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003001
Bram Moolenaar4c137212021-04-19 16:48:48 +02003002 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003003 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003004 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003005 else
3006 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003007 SOURCING_LNUM = iptr->isn_lnum;
3008 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003009 {
3010 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003011 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003012 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003013 clear_tv(&di->di_tv);
3014 di->di_tv = *STACK_TV_BOT(0);
3015 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003016 }
3017 break;
3018
3019 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 case ISN_STORESCRIPT:
3021 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003022 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3023 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003025 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003026 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003027 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003028 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003029
3030 // "const" and "final" are checked at compile time, locking
3031 // the value needs to be checked here.
3032 SOURCING_LNUM = iptr->isn_lnum;
3033 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003034 {
3035 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003036 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003037 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 clear_tv(sv->sv_tv);
3040 *sv->sv_tv = *STACK_TV_BOT(0);
3041 }
3042 break;
3043
3044 // store option
3045 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003046 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003048 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3049 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 long n = 0;
3051 char_u *s = NULL;
3052 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003053 char_u numbuf[NUMBUFLEN];
3054 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055
Bram Moolenaar4c137212021-04-19 16:48:48 +02003056 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 tv = STACK_TV_BOT(0);
3058 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003059 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003061 if (s == NULL)
3062 s = (char_u *)"";
3063 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003064 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3065 {
3066 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003067 // If the option can be set to a function reference or
3068 // a lambda and the passed value is a function
3069 // reference, then convert it to the name (string) of
3070 // the function reference.
3071 s = tv2string(tv, &tofree, numbuf, 0);
3072 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003073 {
3074 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003075 goto on_error;
3076 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003079 // must be VAR_NUMBER, CHECKTYPE makes sure
3080 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003081 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003082 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003083 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 if (msg != NULL)
3085 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003086 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003088 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 }
3091 break;
3092
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003093 // store $ENV
3094 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003095 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003096 tv = STACK_TV_BOT(0);
3097 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3098 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003099 break;
3100
3101 // store @r
3102 case ISN_STOREREG:
3103 {
3104 int reg = iptr->isn_arg.number;
3105
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003107 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003108 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003109 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003110 }
3111 break;
3112
3113 // store v: variable
3114 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003115 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003116 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3117 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003118 // should not happen, type is checked when compiling
3119 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003120 break;
3121
Bram Moolenaard3aac292020-04-19 14:32:17 +02003122 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003124 case ISN_STOREB:
3125 case ISN_STOREW:
3126 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003128 dictitem_T *di;
3129 hashtab_T *ht;
3130 char_u *name = iptr->isn_arg.string + 2;
3131
Bram Moolenaard3aac292020-04-19 14:32:17 +02003132 switch (iptr->isn_type)
3133 {
3134 case ISN_STOREG:
3135 ht = get_globvar_ht();
3136 break;
3137 case ISN_STOREB:
3138 ht = &curbuf->b_vars->dv_hashtab;
3139 break;
3140 case ISN_STOREW:
3141 ht = &curwin->w_vars->dv_hashtab;
3142 break;
3143 case ISN_STORET:
3144 ht = &curtab->tp_vars->dv_hashtab;
3145 break;
3146 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003147 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149
Bram Moolenaar4c137212021-04-19 16:48:48 +02003150 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003151 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003153 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 else
3155 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003156 SOURCING_LNUM = iptr->isn_lnum;
3157 if (var_check_permission(di, name) == FAIL)
3158 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 clear_tv(&di->di_tv);
3160 di->di_tv = *STACK_TV_BOT(0);
3161 }
3162 }
3163 break;
3164
Bram Moolenaar03290b82020-12-19 16:30:44 +01003165 // store an autoload variable
3166 case ISN_STOREAUTO:
3167 SOURCING_LNUM = iptr->isn_lnum;
3168 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3169 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003170 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003171 break;
3172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 // store number in local variable
3174 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003175 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 clear_tv(tv);
3177 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003178 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 break;
3180
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003181 // store value in list or dict variable
3182 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003183 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003184 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003185
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003186 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003187 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003188 if (res == NOTDONE)
3189 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003190 }
3191 break;
3192
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003193 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003194 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003195 if (execute_storerange(iptr, ectx) == FAIL)
3196 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003197 break;
3198
Bram Moolenaar0186e582021-01-10 18:33:11 +01003199 // load or store variable or argument from outer scope
3200 case ISN_LOADOUTER:
3201 case ISN_STOREOUTER:
3202 {
3203 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003204 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3205 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003206
3207 while (depth > 1 && outer != NULL)
3208 {
3209 outer = outer->out_up;
3210 --depth;
3211 }
3212 if (outer == NULL)
3213 {
3214 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003215 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3216 || ectx->ec_outer_ref == NULL)
3217 // Possibly :def function called from legacy
3218 // context.
3219 emsg(_(e_closure_called_from_invalid_context));
3220 else
3221 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003222 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003223 }
3224 tv = ((typval_T *)outer->out_stack->ga_data)
3225 + outer->out_frame_idx + STACK_FRAME_SIZE
3226 + iptr->isn_arg.outer.outer_idx;
3227 if (iptr->isn_type == ISN_LOADOUTER)
3228 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003229 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003230 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003231 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003232 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003233 }
3234 else
3235 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003236 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003237 clear_tv(tv);
3238 *tv = *STACK_TV_BOT(0);
3239 }
3240 }
3241 break;
3242
Bram Moolenaar752fc692021-01-04 21:57:11 +01003243 // unlet item in list or dict variable
3244 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003245 if (execute_unletindex(iptr, ectx) == FAIL)
3246 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003247 break;
3248
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003249 // unlet range of items in list variable
3250 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003251 if (execute_unletrange(iptr, ectx) == FAIL)
3252 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003253 break;
3254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 // push constant
3256 case ISN_PUSHNR:
3257 case ISN_PUSHBOOL:
3258 case ISN_PUSHSPEC:
3259 case ISN_PUSHF:
3260 case ISN_PUSHS:
3261 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003262 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003263 case ISN_PUSHCHANNEL:
3264 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003265 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003266 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003268 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003269 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 switch (iptr->isn_type)
3271 {
3272 case ISN_PUSHNR:
3273 tv->v_type = VAR_NUMBER;
3274 tv->vval.v_number = iptr->isn_arg.number;
3275 break;
3276 case ISN_PUSHBOOL:
3277 tv->v_type = VAR_BOOL;
3278 tv->vval.v_number = iptr->isn_arg.number;
3279 break;
3280 case ISN_PUSHSPEC:
3281 tv->v_type = VAR_SPECIAL;
3282 tv->vval.v_number = iptr->isn_arg.number;
3283 break;
3284#ifdef FEAT_FLOAT
3285 case ISN_PUSHF:
3286 tv->v_type = VAR_FLOAT;
3287 tv->vval.v_float = iptr->isn_arg.fnumber;
3288 break;
3289#endif
3290 case ISN_PUSHBLOB:
3291 blob_copy(iptr->isn_arg.blob, tv);
3292 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003293 case ISN_PUSHFUNC:
3294 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003295 if (iptr->isn_arg.string == NULL)
3296 tv->vval.v_string = NULL;
3297 else
3298 tv->vval.v_string =
3299 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003300 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003301 case ISN_PUSHCHANNEL:
3302#ifdef FEAT_JOB_CHANNEL
3303 tv->v_type = VAR_CHANNEL;
3304 tv->vval.v_channel = iptr->isn_arg.channel;
3305 if (tv->vval.v_channel != NULL)
3306 ++tv->vval.v_channel->ch_refcount;
3307#endif
3308 break;
3309 case ISN_PUSHJOB:
3310#ifdef FEAT_JOB_CHANNEL
3311 tv->v_type = VAR_JOB;
3312 tv->vval.v_job = iptr->isn_arg.job;
3313 if (tv->vval.v_job != NULL)
3314 ++tv->vval.v_job->jv_refcount;
3315#endif
3316 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 default:
3318 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003319 tv->vval.v_string = iptr->isn_arg.string == NULL
3320 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 }
3322 break;
3323
Bram Moolenaar06b77222022-01-25 15:51:56 +00003324 case ISN_AUTOLOAD:
3325 {
3326 char_u *name = iptr->isn_arg.string;
3327
3328 (void)script_autoload(name, FALSE);
3329 if (find_func(name, TRUE))
3330 {
3331 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3332 goto theend;
3333 tv = STACK_TV_BOT(0);
3334 tv->v_lock = 0;
3335 ++ectx->ec_stack.ga_len;
3336 tv->v_type = VAR_FUNC;
3337 tv->vval.v_string = vim_strsave(name);
3338 }
3339 else
3340 {
3341 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3342
3343 if (res == NOTDONE)
3344 goto theend;
3345 if (res == FAIL)
3346 goto on_error;
3347 }
3348 }
3349 break;
3350
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003351 case ISN_UNLET:
3352 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3353 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003354 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003355 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003356 case ISN_UNLETENV:
3357 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3358 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003359
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003360 case ISN_LOCKUNLOCK:
3361 {
3362 typval_T *lval_root_save = lval_root;
3363 int res;
3364
3365 // Stack has the local variable, argument the whole :lock
3366 // or :unlock command, like ISN_EXEC.
3367 --ectx->ec_stack.ga_len;
3368 lval_root = STACK_TV_BOT(0);
3369 res = exec_command(iptr);
3370 clear_tv(lval_root);
3371 lval_root = lval_root_save;
3372 if (res == FAIL)
3373 goto on_error;
3374 }
3375 break;
3376
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003377 case ISN_LOCKCONST:
3378 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3379 break;
3380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 // create a list from items on the stack; uses a single allocation
3382 // for the list header and the items
3383 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003384 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003385 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 break;
3387
3388 // create a dict from items on the stack
3389 case ISN_NEWDICT:
3390 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003391 int count = iptr->isn_arg.number;
3392 dict_T *dict = dict_alloc();
3393 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003394 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003395 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003397 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003398 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 for (idx = 0; idx < count; ++idx)
3400 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003401 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003403 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003404 key = tv->vval.v_string == NULL
3405 ? (char_u *)"" : tv->vval.v_string;
3406 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003407 if (item != NULL)
3408 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003409 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar74409f62022-01-01 15:58:22 +00003410 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003411 dict_unref(dict);
3412 goto on_error;
3413 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003414 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003416 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003417 {
3418 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003419 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3422 item->di_tv.v_lock = 0;
3423 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003424 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003425 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003426 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003427 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 }
3430
3431 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003432 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003433 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003434 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003436 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 tv = STACK_TV_BOT(-1);
3438 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003439 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 tv->vval.v_dict = dict;
3441 ++dict->dv_refcount;
3442 }
3443 break;
3444
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003445 // create a partial with NULL value
3446 case ISN_NEWPARTIAL:
3447 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3448 goto theend;
3449 ++ectx->ec_stack.ga_len;
3450 tv = STACK_TV_BOT(-1);
3451 tv->v_type = VAR_PARTIAL;
3452 tv->v_lock = 0;
3453 tv->vval.v_partial = NULL;
3454 break;
3455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 // call a :def function
3457 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003458 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003459 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3460 NULL,
3461 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003462 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003463 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 break;
3465
3466 // call a builtin function
3467 case ISN_BCALL:
3468 SOURCING_LNUM = iptr->isn_lnum;
3469 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3470 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003471 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003472 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 break;
3474
3475 // call a funcref or partial
3476 case ISN_PCALL:
3477 {
3478 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3479 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003480 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481
3482 SOURCING_LNUM = iptr->isn_lnum;
3483 if (pfunc->cpf_top)
3484 {
3485 // funcref is above the arguments
3486 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3487 }
3488 else
3489 {
3490 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003491 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003492 partial_tv = *STACK_TV_BOT(0);
3493 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003495 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003496 if (tv == &partial_tv)
3497 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003499 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 }
3501 break;
3502
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003503 case ISN_PCALL_END:
3504 // PCALL finished, arguments have been consumed and replaced by
3505 // the return value. Now clear the funcref from the stack,
3506 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003507 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003508 clear_tv(STACK_TV_BOT(-1));
3509 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3510 break;
3511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 // call a user defined function or funcref/partial
3513 case ISN_UCALL:
3514 {
3515 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3516
3517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003518 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003519 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003520 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 }
3522 break;
3523
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003524 // return from a :def function call without a value
3525 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003526 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003527 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003528 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003529 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003530 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003531 tv->vval.v_number = 0;
3532 tv->v_lock = 0;
3533 // FALLTHROUGH
3534
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003535 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 case ISN_RETURN:
3537 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003538 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003539 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003540
3541 if (trystack->ga_len > 0)
3542 trycmd = ((trycmd_T *)trystack->ga_data)
3543 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003544 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003545 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003547 // jump to ":finally" or ":endtry"
3548 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003549 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003550 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003551 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 trycmd->tcd_return = TRUE;
3553 }
3554 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003555 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 }
3557 break;
3558
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003559 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 case ISN_FUNCREF:
3561 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003562 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003563 ufunc_T *ufunc;
3564 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003567 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003568 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003569 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003570 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003571 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003572 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003573 if (funcref->fr_func_name == NULL)
3574 {
3575 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3576 + funcref->fr_dfunc_idx;
3577
3578 ufunc = pt_dfunc->df_ufunc;
3579 }
3580 else
3581 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003582 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003583 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003584 if (ufunc == NULL)
3585 {
3586 SOURCING_LNUM = iptr->isn_lnum;
3587 iemsg("ufunc unexpectedly NULL for FUNCREF");
3588 goto theend;
3589 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003590 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003591 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003593 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 tv->vval.v_partial = pt;
3595 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003596 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 }
3598 break;
3599
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003600 // Create a global function from a lambda.
3601 case ISN_NEWFUNC:
3602 {
3603 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3604
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003605 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003606 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003607 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003608 }
3609 break;
3610
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003611 // List functions
3612 case ISN_DEF:
3613 if (iptr->isn_arg.string == NULL)
3614 list_functions(NULL);
3615 else
3616 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003617 exarg_T ea;
3618 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003619
3620 CLEAR_FIELD(ea);
3621 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003622 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3623 define_function(&ea, NULL, &lines_to_free);
3624 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003625 }
3626 break;
3627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 // jump if a condition is met
3629 case ISN_JUMP:
3630 {
3631 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003632 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 int jump = TRUE;
3634
3635 if (when != JUMP_ALWAYS)
3636 {
3637 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003638 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003639 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003640 || when == JUMP_IF_COND_TRUE)
3641 {
3642 SOURCING_LNUM = iptr->isn_lnum;
3643 jump = tv_get_bool_chk(tv, &error);
3644 if (error)
3645 goto on_error;
3646 }
3647 else
3648 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003650 || when == JUMP_AND_KEEP_IF_FALSE
3651 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003653 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 {
3655 // drop the value from the stack
3656 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003657 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 }
3659 }
3660 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003661 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 }
3663 break;
3664
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003665 // Jump if an argument with a default value was already set and not
3666 // v:none.
3667 case ISN_JUMP_IF_ARG_SET:
3668 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3669 if (tv->v_type != VAR_UNKNOWN
3670 && !(tv->v_type == VAR_SPECIAL
3671 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003672 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003673 break;
3674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 // top of a for loop
3676 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003677 if (execute_for(iptr, ectx) == FAIL)
3678 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 break;
3680
3681 // start of ":try" block
3682 case ISN_TRY:
3683 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003684 trycmd_T *trycmd = NULL;
3685
Bram Moolenaar35578162021-08-02 19:10:38 +02003686 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003687 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003688 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3689 + ectx->ec_trystack.ga_len;
3690 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003692 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003693 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3694 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003695 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003696 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003697 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003698 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003699 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003700 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 }
3702 break;
3703
3704 case ISN_PUSHEXC:
3705 if (current_exception == NULL)
3706 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003707 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003709 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003711 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003712 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003714 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003716 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 tv->vval.v_string = vim_strsave(
3718 (char_u *)current_exception->value);
3719 break;
3720
3721 case ISN_CATCH:
3722 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003723 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
Bram Moolenaar4c137212021-04-19 16:48:48 +02003725 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 if (trystack->ga_len > 0)
3727 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003728 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 + trystack->ga_len - 1;
3730 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003731 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 }
3733 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003734 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 catch_exception(current_exception);
3736 }
3737 break;
3738
Bram Moolenaarc150c092021-02-13 15:02:46 +01003739 case ISN_TRYCONT:
3740 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003741 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003742 trycont_T *trycont = &iptr->isn_arg.trycont;
3743 int i;
3744 trycmd_T *trycmd;
3745 int iidx = trycont->tct_where;
3746
3747 if (trystack->ga_len < trycont->tct_levels)
3748 {
3749 siemsg("TRYCONT: expected %d levels, found %d",
3750 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003751 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003752 }
3753 // Make :endtry jump to any outer try block and the last
3754 // :endtry inside the loop to the loop start.
3755 for (i = trycont->tct_levels; i > 0; --i)
3756 {
3757 trycmd = ((trycmd_T *)trystack->ga_data)
3758 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003759 // Add one to tcd_cont to be able to jump to
3760 // instruction with index zero.
3761 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003762 iidx = trycmd->tcd_finally_idx == 0
3763 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003764 }
3765 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003766 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003767 }
3768 break;
3769
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003770 case ISN_FINALLY:
3771 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003772 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003773 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3774 + trystack->ga_len - 1;
3775
3776 // Reset the index to avoid a return statement jumps here
3777 // again.
3778 trycmd->tcd_finally_idx = 0;
3779 break;
3780 }
3781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 // end of ":try" block
3783 case ISN_ENDTRY:
3784 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003785 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786
3787 if (trystack->ga_len > 0)
3788 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003789 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 --trystack->ga_len;
3792 --trylevel;
3793 trycmd = ((trycmd_T *)trystack->ga_data)
3794 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003795 if (trycmd->tcd_did_throw)
3796 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003797 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 {
3799 // discard the exception
3800 if (caught_stack == current_exception)
3801 caught_stack = caught_stack->caught;
3802 discard_current_exception();
3803 }
3804
3805 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003806 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003807
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003809 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003810 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003811 clear_tv(STACK_TV_BOT(0));
3812 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003813 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003814 // handling :continue: jump to outer try block or
3815 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003816 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 }
3818 }
3819 break;
3820
3821 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003822 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003823 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003824
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003825 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3826 {
3827 // throwing an exception while using "silent!" causes
3828 // the function to abort but not display an error.
3829 tv = STACK_TV_BOT(-1);
3830 clear_tv(tv);
3831 tv->v_type = VAR_NUMBER;
3832 tv->vval.v_number = 0;
3833 goto done;
3834 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003835 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003836 tv = STACK_TV_BOT(0);
3837 if (tv->vval.v_string == NULL
3838 || *skipwhite(tv->vval.v_string) == NUL)
3839 {
3840 vim_free(tv->vval.v_string);
3841 SOURCING_LNUM = iptr->isn_lnum;
3842 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003843 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003844 }
3845
3846 // Inside a "catch" we need to first discard the caught
3847 // exception.
3848 if (trystack->ga_len > 0)
3849 {
3850 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3851 + trystack->ga_len - 1;
3852 if (trycmd->tcd_caught && current_exception != NULL)
3853 {
3854 // discard the exception
3855 if (caught_stack == current_exception)
3856 caught_stack = caught_stack->caught;
3857 discard_current_exception();
3858 trycmd->tcd_caught = FALSE;
3859 }
3860 }
3861
Bram Moolenaar90a57162022-02-12 14:23:17 +00003862 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003863 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3864 == FAIL)
3865 {
3866 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003867 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003868 }
3869 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 break;
3872
3873 // compare with special values
3874 case ISN_COMPAREBOOL:
3875 case ISN_COMPARESPECIAL:
3876 {
3877 typval_T *tv1 = STACK_TV_BOT(-2);
3878 typval_T *tv2 = STACK_TV_BOT(-1);
3879 varnumber_T arg1 = tv1->vval.v_number;
3880 varnumber_T arg2 = tv2->vval.v_number;
3881 int res;
3882
3883 switch (iptr->isn_arg.op.op_type)
3884 {
3885 case EXPR_EQUAL: res = arg1 == arg2; break;
3886 case EXPR_NEQUAL: res = arg1 != arg2; break;
3887 default: res = 0; break;
3888 }
3889
Bram Moolenaar4c137212021-04-19 16:48:48 +02003890 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 tv1->v_type = VAR_BOOL;
3892 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3893 }
3894 break;
3895
Bram Moolenaar7a222242022-03-01 19:23:24 +00003896 case ISN_COMPARENULL:
3897 {
3898 typval_T *tv1 = STACK_TV_BOT(-2);
3899 typval_T *tv2 = STACK_TV_BOT(-1);
3900 int res;
3901
3902 res = typval_compare_null(tv1, tv2);
3903 if (res == MAYBE)
3904 goto on_error;
3905 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3906 res = !res;
3907 clear_tv(tv1);
3908 clear_tv(tv2);
3909 --ectx->ec_stack.ga_len;
3910 tv1->v_type = VAR_BOOL;
3911 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3912 }
3913 break;
3914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 // Operation with two number arguments
3916 case ISN_OPNR:
3917 case ISN_COMPARENR:
3918 {
3919 typval_T *tv1 = STACK_TV_BOT(-2);
3920 typval_T *tv2 = STACK_TV_BOT(-1);
3921 varnumber_T arg1 = tv1->vval.v_number;
3922 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003923 varnumber_T res = 0;
3924 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925
3926 switch (iptr->isn_arg.op.op_type)
3927 {
3928 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003929 case EXPR_DIV: if (arg2 == 0)
3930 div_zero = TRUE;
3931 else
3932 res = arg1 / arg2;
3933 break;
3934 case EXPR_REM: if (arg2 == 0)
3935 div_zero = TRUE;
3936 else
3937 res = arg1 % arg2;
3938 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 case EXPR_SUB: res = arg1 - arg2; break;
3940 case EXPR_ADD: res = arg1 + arg2; break;
3941
3942 case EXPR_EQUAL: res = arg1 == arg2; break;
3943 case EXPR_NEQUAL: res = arg1 != arg2; break;
3944 case EXPR_GREATER: res = arg1 > arg2; break;
3945 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3946 case EXPR_SMALLER: res = arg1 < arg2; break;
3947 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003948 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 }
3950
Bram Moolenaar4c137212021-04-19 16:48:48 +02003951 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 if (iptr->isn_type == ISN_COMPARENR)
3953 {
3954 tv1->v_type = VAR_BOOL;
3955 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3956 }
3957 else
3958 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003959 if (div_zero)
3960 {
3961 SOURCING_LNUM = iptr->isn_lnum;
3962 emsg(_(e_divide_by_zero));
3963 goto on_error;
3964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 }
3966 break;
3967
3968 // Computation with two float arguments
3969 case ISN_OPFLOAT:
3970 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003971#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 {
3973 typval_T *tv1 = STACK_TV_BOT(-2);
3974 typval_T *tv2 = STACK_TV_BOT(-1);
3975 float_T arg1 = tv1->vval.v_float;
3976 float_T arg2 = tv2->vval.v_float;
3977 float_T res = 0;
3978 int cmp = FALSE;
3979
3980 switch (iptr->isn_arg.op.op_type)
3981 {
3982 case EXPR_MULT: res = arg1 * arg2; break;
3983 case EXPR_DIV: res = arg1 / arg2; break;
3984 case EXPR_SUB: res = arg1 - arg2; break;
3985 case EXPR_ADD: res = arg1 + arg2; break;
3986
3987 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3988 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3989 case EXPR_GREATER: cmp = arg1 > arg2; break;
3990 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3991 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3992 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3993 default: cmp = 0; break;
3994 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003995 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 if (iptr->isn_type == ISN_COMPAREFLOAT)
3997 {
3998 tv1->v_type = VAR_BOOL;
3999 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4000 }
4001 else
4002 tv1->vval.v_float = res;
4003 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004004#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 break;
4006
4007 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004008 case ISN_COMPAREDICT:
4009 case ISN_COMPAREFUNC:
4010 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 case ISN_COMPAREBLOB:
4012 {
4013 typval_T *tv1 = STACK_TV_BOT(-2);
4014 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004015 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4016 int ic = iptr->isn_arg.op.op_ic;
4017 int res = FALSE;
4018 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019
Bram Moolenaar265f8112021-12-19 12:33:05 +00004020 SOURCING_LNUM = iptr->isn_lnum;
4021 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004023 status = typval_compare_list(tv1, tv2,
4024 exprtype, ic, &res);
4025 }
4026 else if (iptr->isn_type == ISN_COMPAREDICT)
4027 {
4028 status = typval_compare_dict(tv1, tv2,
4029 exprtype, ic, &res);
4030 }
4031 else if (iptr->isn_type == ISN_COMPAREFUNC)
4032 {
4033 status = typval_compare_func(tv1, tv2,
4034 exprtype, ic, &res);
4035 }
4036 else if (iptr->isn_type == ISN_COMPARESTRING)
4037 {
4038 status = typval_compare_string(tv1, tv2,
4039 exprtype, ic, &res);
4040 }
4041 else
4042 {
4043 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004045 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 clear_tv(tv1);
4047 clear_tv(tv2);
4048 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004049 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4050 if (status == FAIL)
4051 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 }
4053 break;
4054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 case ISN_COMPAREANY:
4056 {
4057 typval_T *tv1 = STACK_TV_BOT(-2);
4058 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004059 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004061 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062
Bram Moolenaareb26f432020-09-14 16:50:05 +02004063 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004064 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004066 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004067 if (status == FAIL)
4068 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 }
4070 break;
4071
4072 case ISN_ADDLIST:
4073 case ISN_ADDBLOB:
4074 {
4075 typval_T *tv1 = STACK_TV_BOT(-2);
4076 typval_T *tv2 = STACK_TV_BOT(-1);
4077
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004078 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004080 {
4081 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4082 && tv1->vval.v_list != NULL)
4083 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4084 NULL);
4085 else
4086 eval_addlist(tv1, tv2);
4087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 else
4089 eval_addblob(tv1, tv2);
4090 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004091 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 }
4093 break;
4094
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004095 case ISN_LISTAPPEND:
4096 {
4097 typval_T *tv1 = STACK_TV_BOT(-2);
4098 typval_T *tv2 = STACK_TV_BOT(-1);
4099 list_T *l = tv1->vval.v_list;
4100
4101 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004102 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004103 if (l == NULL)
4104 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004105 emsg(_(e_cannot_add_to_null_list));
4106 goto on_error;
4107 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004108 if (value_check_lock(l->lv_lock, NULL, FALSE))
4109 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004110 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004111 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004112 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004113 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004114 }
4115 break;
4116
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004117 case ISN_BLOBAPPEND:
4118 {
4119 typval_T *tv1 = STACK_TV_BOT(-2);
4120 typval_T *tv2 = STACK_TV_BOT(-1);
4121 blob_T *b = tv1->vval.v_blob;
4122 int error = FALSE;
4123 varnumber_T n;
4124
4125 // add a number to a blob
4126 if (b == NULL)
4127 {
4128 SOURCING_LNUM = iptr->isn_lnum;
4129 emsg(_(e_cannot_add_to_null_blob));
4130 goto on_error;
4131 }
4132 n = tv_get_number_chk(tv2, &error);
4133 if (error)
4134 goto on_error;
4135 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004136 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004137 }
4138 break;
4139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 // Computation with two arguments of unknown type
4141 case ISN_OPANY:
4142 {
4143 typval_T *tv1 = STACK_TV_BOT(-2);
4144 typval_T *tv2 = STACK_TV_BOT(-1);
4145 varnumber_T n1, n2;
4146#ifdef FEAT_FLOAT
4147 float_T f1 = 0, f2 = 0;
4148#endif
4149 int error = FALSE;
4150
4151 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4152 {
4153 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4154 {
4155 eval_addlist(tv1, tv2);
4156 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004157 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 break;
4159 }
4160 else if (tv1->v_type == VAR_BLOB
4161 && tv2->v_type == VAR_BLOB)
4162 {
4163 eval_addblob(tv1, tv2);
4164 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004165 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 break;
4167 }
4168 }
4169#ifdef FEAT_FLOAT
4170 if (tv1->v_type == VAR_FLOAT)
4171 {
4172 f1 = tv1->vval.v_float;
4173 n1 = 0;
4174 }
4175 else
4176#endif
4177 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004178 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 n1 = tv_get_number_chk(tv1, &error);
4180 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004181 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182#ifdef FEAT_FLOAT
4183 if (tv2->v_type == VAR_FLOAT)
4184 f1 = n1;
4185#endif
4186 }
4187#ifdef FEAT_FLOAT
4188 if (tv2->v_type == VAR_FLOAT)
4189 {
4190 f2 = tv2->vval.v_float;
4191 n2 = 0;
4192 }
4193 else
4194#endif
4195 {
4196 n2 = tv_get_number_chk(tv2, &error);
4197 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004198 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199#ifdef FEAT_FLOAT
4200 if (tv1->v_type == VAR_FLOAT)
4201 f2 = n2;
4202#endif
4203 }
4204#ifdef FEAT_FLOAT
4205 // if there is a float on either side the result is a float
4206 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4207 {
4208 switch (iptr->isn_arg.op.op_type)
4209 {
4210 case EXPR_MULT: f1 = f1 * f2; break;
4211 case EXPR_DIV: f1 = f1 / f2; break;
4212 case EXPR_SUB: f1 = f1 - f2; break;
4213 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004214 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004215 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004216 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 }
4218 clear_tv(tv1);
4219 clear_tv(tv2);
4220 tv1->v_type = VAR_FLOAT;
4221 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004222 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 }
4224 else
4225#endif
4226 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004227 int failed = FALSE;
4228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 switch (iptr->isn_arg.op.op_type)
4230 {
4231 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004232 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4233 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004234 goto on_error;
4235 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 case EXPR_SUB: n1 = n1 - n2; break;
4237 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004238 default: n1 = num_modulus(n1, n2, &failed);
4239 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004240 goto on_error;
4241 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 }
4243 clear_tv(tv1);
4244 clear_tv(tv2);
4245 tv1->v_type = VAR_NUMBER;
4246 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004247 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 }
4249 }
4250 break;
4251
4252 case ISN_CONCAT:
4253 {
4254 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4255 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4256 char_u *res;
4257
4258 res = concat_str(str1, str2);
4259 clear_tv(STACK_TV_BOT(-2));
4260 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004261 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 STACK_TV_BOT(-1)->vval.v_string = res;
4263 }
4264 break;
4265
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004266 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004267 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004268 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004269 int is_slice = iptr->isn_type == ISN_STRSLICE;
4270 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004271 char_u *res;
4272
4273 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004274 // string slice: string is at stack-3, first index at
4275 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004276 if (is_slice)
4277 {
4278 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004279 n1 = tv->vval.v_number;
4280 }
4281
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004282 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004283 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004284
Bram Moolenaar4c137212021-04-19 16:48:48 +02004285 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004286 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004287 if (is_slice)
4288 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004289 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004290 else
4291 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004292 // single character (including composing characters).
4293 // If the index is too big or negative the result is
4294 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004295 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004296 vim_free(tv->vval.v_string);
4297 tv->vval.v_string = res;
4298 }
4299 break;
4300
4301 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004302 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004303 case ISN_BLOBINDEX:
4304 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004306 int is_slice = iptr->isn_type == ISN_LISTSLICE
4307 || iptr->isn_type == ISN_BLOBSLICE;
4308 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4309 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004310 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004311 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312
4313 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004314 // list slice: list is at stack-3, indexes at stack-2 and
4315 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004316 // Same for blob.
4317 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318
4319 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004320 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004322
4323 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 {
Bram Moolenaared591872020-08-15 22:14:53 +02004325 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004326 n1 = tv->vval.v_number;
4327 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 }
Bram Moolenaared591872020-08-15 22:14:53 +02004329
Bram Moolenaar4c137212021-04-19 16:48:48 +02004330 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004331 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004333 if (is_blob)
4334 {
4335 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4336 n1, n2, FALSE, tv) == FAIL)
4337 goto on_error;
4338 }
4339 else
4340 {
4341 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4342 n1, n2, FALSE, tv, TRUE) == FAIL)
4343 goto on_error;
4344 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 }
4346 break;
4347
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004348 case ISN_ANYINDEX:
4349 case ISN_ANYSLICE:
4350 {
4351 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4352 typval_T *var1, *var2;
4353 int res;
4354
4355 // index: composite is at stack-2, index at stack-1
4356 // slice: composite is at stack-3, indexes at stack-2 and
4357 // stack-1
4358 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004359 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004360 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4361 goto on_error;
4362 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4363 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004364 res = eval_index_inner(tv, is_slice, var1, var2,
4365 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004366 clear_tv(var1);
4367 if (is_slice)
4368 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004369 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004370 if (res == FAIL)
4371 goto on_error;
4372 }
4373 break;
4374
Bram Moolenaar9af78762020-06-16 11:34:42 +02004375 case ISN_SLICE:
4376 {
4377 list_T *list;
4378 int count = iptr->isn_arg.number;
4379
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004380 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004381 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004382 list = tv->vval.v_list;
4383
4384 // no error for short list, expect it to be checked earlier
4385 if (list != NULL && list->lv_len >= count)
4386 {
4387 list_T *newlist = list_slice(list,
4388 count, list->lv_len - 1);
4389
4390 if (newlist != NULL)
4391 {
4392 list_unref(list);
4393 tv->vval.v_list = newlist;
4394 ++newlist->lv_refcount;
4395 }
4396 }
4397 }
4398 break;
4399
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004400 case ISN_GETITEM:
4401 {
4402 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004403 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004404
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004405 // Get list item: list is at stack-1, push item.
4406 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004407 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4408 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004409
Bram Moolenaar35578162021-08-02 19:10:38 +02004410 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004411 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004412 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004413 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004414
4415 // Useful when used in unpack assignment. Reset at
4416 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004417 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004418 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004419 }
4420 break;
4421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 case ISN_MEMBER:
4423 {
4424 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004425 char_u *key;
4426 dictitem_T *di;
4427
4428 // dict member: dict is at stack-2, key at stack-1
4429 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004430 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004431 dict = tv->vval.v_dict;
4432
4433 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004434 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004435 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004436 if (key == NULL)
4437 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004438
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004439 if ((di = dict_find(dict, key, -1)) == NULL)
4440 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004441 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004442 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004443
4444 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004445 // stack contents makes sense and the dict stack is
4446 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004447 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004448 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004449 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004450 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004451 tv->v_type = VAR_NUMBER;
4452 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004453 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004454 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004455 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004456 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004457 // Put the dict used on the dict stack, it might be used by
4458 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004459 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004460 if (dict_stack_save(tv) == FAIL)
4461 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004462 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004463 }
4464 break;
4465
4466 // dict member with string key
4467 case ISN_STRINGMEMBER:
4468 {
4469 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 dictitem_T *di;
4471
4472 tv = STACK_TV_BOT(-1);
4473 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4474 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004475 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004476 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004477 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 }
4479 dict = tv->vval.v_dict;
4480
4481 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4482 == NULL)
4483 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004485 semsg(_(e_key_not_present_in_dictionary),
4486 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004487 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004489 // Put the dict used on the dict stack, it might be used by
4490 // a dict function later.
4491 if (dict_stack_save(tv) == FAIL)
4492 goto on_fatal_error;
4493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004495 }
4496 break;
4497
4498 case ISN_CLEARDICT:
4499 dict_stack_drop();
4500 break;
4501
4502 case ISN_USEDICT:
4503 {
4504 typval_T *dict_tv = dict_stack_get_tv();
4505
4506 // Turn "dict.Func" into a partial for "Func" bound to
4507 // "dict". Don't do this when "Func" is already a partial
4508 // that was bound explicitly (pt_auto is FALSE).
4509 tv = STACK_TV_BOT(-1);
4510 if (dict_tv != NULL
4511 && dict_tv->v_type == VAR_DICT
4512 && dict_tv->vval.v_dict != NULL
4513 && (tv->v_type == VAR_FUNC
4514 || (tv->v_type == VAR_PARTIAL
4515 && (tv->vval.v_partial->pt_auto
4516 || tv->vval.v_partial->pt_dict == NULL))))
4517 dict_tv->vval.v_dict =
4518 make_partial(dict_tv->vval.v_dict, tv);
4519 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 }
4521 break;
4522
4523 case ISN_NEGATENR:
4524 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004525 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004526#ifdef FEAT_FLOAT
4527 if (tv->v_type == VAR_FLOAT)
4528 tv->vval.v_float = -tv->vval.v_float;
4529 else
4530#endif
4531 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 break;
4533
4534 case ISN_CHECKNR:
4535 {
4536 int error = FALSE;
4537
4538 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004539 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004541 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 (void)tv_get_number_chk(tv, &error);
4543 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004544 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 }
4546 break;
4547
4548 case ISN_CHECKTYPE:
4549 {
4550 checktype_T *ct = &iptr->isn_arg.type;
4551
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004552 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004553 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004554 if (!ectx->ec_where.wt_variable)
4555 ectx->ec_where.wt_index = ct->ct_arg_idx;
4556 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4557 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004558 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004559 if (!ectx->ec_where.wt_variable)
4560 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004561
4562 // number 0 is FALSE, number 1 is TRUE
4563 if (tv->v_type == VAR_NUMBER
4564 && ct->ct_type->tt_type == VAR_BOOL
4565 && (tv->vval.v_number == 0
4566 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004568 tv->v_type = VAR_BOOL;
4569 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004570 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 }
4572 }
4573 break;
4574
Bram Moolenaar9af78762020-06-16 11:34:42 +02004575 case ISN_CHECKLEN:
4576 {
4577 int min_len = iptr->isn_arg.checklen.cl_min_len;
4578 list_T *list = NULL;
4579
4580 tv = STACK_TV_BOT(-1);
4581 if (tv->v_type == VAR_LIST)
4582 list = tv->vval.v_list;
4583 if (list == NULL || list->lv_len < min_len
4584 || (list->lv_len > min_len
4585 && !iptr->isn_arg.checklen.cl_more_OK))
4586 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004587 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004588 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004589 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004590 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004591 }
4592 }
4593 break;
4594
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004595 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004596 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004597 break;
4598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004600 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 {
4602 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004603 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004605 if (iptr->isn_type == ISN_2BOOL)
4606 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004607 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004608 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004609 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004610 n = !n;
4611 }
4612 else
4613 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004614 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004615 SOURCING_LNUM = iptr->isn_lnum;
4616 n = tv_get_bool_chk(tv, &error);
4617 if (error)
4618 goto on_error;
4619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 clear_tv(tv);
4621 tv->v_type = VAR_BOOL;
4622 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4623 }
4624 break;
4625
4626 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004627 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004628 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004629 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4630 iptr->isn_type == ISN_2STRING_ANY,
4631 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004632 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 break;
4634
Bram Moolenaar08597872020-12-10 19:43:40 +01004635 case ISN_RANGE:
4636 {
4637 exarg_T ea;
4638 char *errormsg;
4639
Bram Moolenaarece0b872021-01-08 20:40:45 +01004640 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004641 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004642 ea.addr_type = ADDR_LINES;
4643 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004644 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004645 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004646 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004647
Bram Moolenaar35578162021-08-02 19:10:38 +02004648 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004649 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004650 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004651 tv = STACK_TV_BOT(-1);
4652 tv->v_type = VAR_NUMBER;
4653 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004654 if (ea.addr_count == 0)
4655 tv->vval.v_number = curwin->w_cursor.lnum;
4656 else
4657 tv->vval.v_number = ea.line2;
4658 }
4659 break;
4660
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004661 case ISN_PUT:
4662 {
4663 int regname = iptr->isn_arg.put.put_regname;
4664 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4665 char_u *expr = NULL;
4666 int dir = FORWARD;
4667
Bram Moolenaar08597872020-12-10 19:43:40 +01004668 if (lnum < -2)
4669 {
4670 // line number was put on the stack by ISN_RANGE
4671 tv = STACK_TV_BOT(-1);
4672 curwin->w_cursor.lnum = tv->vval.v_number;
4673 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4674 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004675 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004676 }
4677 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004678 // :put! above cursor
4679 dir = BACKWARD;
4680 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004681 {
4682 curwin->w_cursor.lnum = lnum;
4683 if (lnum == 0)
4684 // check_cursor() below will move to line 1
4685 dir = BACKWARD;
4686 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004687
4688 if (regname == '=')
4689 {
4690 tv = STACK_TV_BOT(-1);
4691 if (tv->v_type == VAR_STRING)
4692 expr = tv->vval.v_string;
4693 else
4694 {
4695 expr = typval2string(tv, TRUE); // allocates value
4696 clear_tv(tv);
4697 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004698 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004699 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004700 check_cursor();
4701 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4702 vim_free(expr);
4703 }
4704 break;
4705
Bram Moolenaar02194d22020-10-24 23:08:38 +02004706 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004707 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4708 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4709 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4710 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004711 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4712 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004713 break;
4714
Bram Moolenaar02194d22020-10-24 23:08:38 +02004715 case ISN_CMDMOD_REV:
4716 // filter regprog is owned by the instruction, don't free it
4717 cmdmod.cmod_filter_regmatch.regprog = NULL;
4718 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004719 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4720 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004721 break;
4722
Bram Moolenaar792f7862020-11-23 08:31:18 +01004723 case ISN_UNPACK:
4724 {
4725 int count = iptr->isn_arg.unpack.unp_count;
4726 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4727 list_T *l;
4728 listitem_T *li;
4729 int i;
4730
4731 // Check there is a valid list to unpack.
4732 tv = STACK_TV_BOT(-1);
4733 if (tv->v_type != VAR_LIST)
4734 {
4735 SOURCING_LNUM = iptr->isn_lnum;
4736 emsg(_(e_for_argument_must_be_sequence_of_lists));
4737 goto on_error;
4738 }
4739 l = tv->vval.v_list;
4740 if (l == NULL
4741 || l->lv_len < (semicolon ? count - 1 : count))
4742 {
4743 SOURCING_LNUM = iptr->isn_lnum;
4744 emsg(_(e_list_value_does_not_have_enough_items));
4745 goto on_error;
4746 }
4747 else if (!semicolon && l->lv_len > count)
4748 {
4749 SOURCING_LNUM = iptr->isn_lnum;
4750 emsg(_(e_list_value_has_more_items_than_targets));
4751 goto on_error;
4752 }
4753
4754 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004755 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004756 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004757 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004758
4759 // Variable after semicolon gets a list with the remaining
4760 // items.
4761 if (semicolon)
4762 {
4763 list_T *rem_list =
4764 list_alloc_with_items(l->lv_len - count + 1);
4765
4766 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004767 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004768 tv = STACK_TV_BOT(-count);
4769 tv->vval.v_list = rem_list;
4770 ++rem_list->lv_refcount;
4771 tv->v_lock = 0;
4772 li = l->lv_first;
4773 for (i = 0; i < count - 1; ++i)
4774 li = li->li_next;
4775 for (i = 0; li != NULL; ++i)
4776 {
4777 list_set_item(rem_list, i, &li->li_tv);
4778 li = li->li_next;
4779 }
4780 --count;
4781 }
4782
4783 // Produce the values in reverse order, first item last.
4784 li = l->lv_first;
4785 for (i = 0; i < count; ++i)
4786 {
4787 tv = STACK_TV_BOT(-i - 1);
4788 copy_tv(&li->li_tv, tv);
4789 li = li->li_next;
4790 }
4791
4792 list_unref(l);
4793 }
4794 break;
4795
Bram Moolenaarb2049902021-01-24 12:53:53 +01004796 case ISN_PROF_START:
4797 case ISN_PROF_END:
4798 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004799#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004800 funccall_T cookie;
4801 ufunc_T *cur_ufunc =
4802 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004803 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004804
4805 cookie.func = cur_ufunc;
4806 if (iptr->isn_type == ISN_PROF_START)
4807 {
4808 func_line_start(&cookie, iptr->isn_lnum);
4809 // if we get here the instruction is executed
4810 func_line_exec(&cookie);
4811 }
4812 else
4813 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004814#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004815 }
4816 break;
4817
Bram Moolenaare99d4222021-06-13 14:01:26 +02004818 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004819 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004820 break;
4821
Bram Moolenaar389df252020-07-09 21:20:47 +02004822 case ISN_SHUFFLE:
4823 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004824 typval_T tmp_tv;
4825 int item = iptr->isn_arg.shuffle.shfl_item;
4826 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004827
4828 tmp_tv = *STACK_TV_BOT(-item);
4829 for ( ; up > 0 && item > 1; --up)
4830 {
4831 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4832 --item;
4833 }
4834 *STACK_TV_BOT(-item) = tmp_tv;
4835 }
4836 break;
4837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004839 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004841 ectx->ec_where.wt_index = 0;
4842 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 break;
4844 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004845 continue;
4846
Bram Moolenaard032f342020-07-18 18:13:02 +02004847func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004848 // Restore previous function. If the frame pointer is where we started
4849 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004851 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004852
Bram Moolenaar4c137212021-04-19 16:48:48 +02004853 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004854 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004855 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004856 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004857
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004858on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004859 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004860 // If "emsg_silent" is set then ignore the error, unless it was set
4861 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004862 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004863 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004864 {
4865 // If a sequence of instructions causes an error while ":silent!"
4866 // was used, restore the stack length and jump ahead to restoring
4867 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004868 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004869 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004870 while (ectx->ec_stack.ga_len
4871 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004872 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004873 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004874 clear_tv(STACK_TV_BOT(0));
4875 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004876 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4877 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004878 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004879 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004880 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004881on_fatal_error:
4882 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004883 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004884 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004885 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 }
4887
4888done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004889 ret = OK;
4890theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004891 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004892 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4893 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004894}
4895
4896/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004897 * Execute the instructions from a VAR_INSTR typeval and put the result in
4898 * "rettv".
4899 * Return OK or FAIL.
4900 */
4901 int
4902exe_typval_instr(typval_T *tv, typval_T *rettv)
4903{
4904 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4905 isn_T *save_instr = ectx->ec_instr;
4906 int save_iidx = ectx->ec_iidx;
4907 int res;
4908
4909 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4910 res = exec_instructions(ectx);
4911 if (res == OK)
4912 {
4913 *rettv = *STACK_TV_BOT(-1);
4914 --ectx->ec_stack.ga_len;
4915 }
4916
4917 ectx->ec_instr = save_instr;
4918 ectx->ec_iidx = save_iidx;
4919
4920 return res;
4921}
4922
4923/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004924 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4925 * "substitute_instr".
4926 */
4927 char_u *
4928exe_substitute_instr(void)
4929{
4930 ectx_T *ectx = substitute_instr->subs_ectx;
4931 isn_T *save_instr = ectx->ec_instr;
4932 int save_iidx = ectx->ec_iidx;
4933 char_u *res;
4934
4935 ectx->ec_instr = substitute_instr->subs_instr;
4936 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004937 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004938 typval_T *tv = STACK_TV_BOT(-1);
4939
Bram Moolenaar27523602021-06-05 21:36:19 +02004940 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004941 --ectx->ec_stack.ga_len;
4942 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004943 }
4944 else
4945 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004946 substitute_instr->subs_status = FAIL;
4947 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949
Bram Moolenaar4c137212021-04-19 16:48:48 +02004950 ectx->ec_instr = save_instr;
4951 ectx->ec_iidx = save_iidx;
4952
4953 return res;
4954}
4955
4956/*
4957 * Call a "def" function from old Vim script.
4958 * Return OK or FAIL.
4959 */
4960 int
4961call_def_function(
4962 ufunc_T *ufunc,
4963 int argc_arg, // nr of arguments
4964 typval_T *argv, // arguments
4965 partial_T *partial, // optional partial for context
4966 typval_T *rettv) // return value
4967{
4968 ectx_T ectx; // execution context
4969 int argc = argc_arg;
4970 typval_T *tv;
4971 int idx;
4972 int ret = FAIL;
4973 int defcount = ufunc->uf_args.ga_len - argc;
4974 sctx_T save_current_sctx = current_sctx;
4975 int did_emsg_before = did_emsg_cumul + did_emsg;
4976 int save_suppress_errthrow = suppress_errthrow;
4977 msglist_T **saved_msg_list = NULL;
4978 msglist_T *private_msg_list = NULL;
4979 int save_emsg_silent_def = emsg_silent_def;
4980 int save_did_emsg_def = did_emsg_def;
4981 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004982 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004983
4984// Get pointer to item in the stack.
4985#undef STACK_TV
4986#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4987
4988// Get pointer to item at the bottom of the stack, -1 is the bottom.
4989#undef STACK_TV_BOT
4990#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4991
4992// Get pointer to a local variable on the stack. Negative for arguments.
4993#undef STACK_TV_VAR
4994#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4995
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004996 // Update uf_has_breakpoint if needed.
4997 update_has_breakpoint(ufunc);
4998
Bram Moolenaar4c137212021-04-19 16:48:48 +02004999 if (ufunc->uf_def_status == UF_NOT_COMPILED
5000 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02005001 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
5002 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005003 == FAIL))
5004 {
5005 if (did_emsg_cumul + did_emsg == did_emsg_before)
5006 semsg(_(e_function_is_not_compiled_str),
5007 printable_func_name(ufunc));
5008 return FAIL;
5009 }
5010
5011 {
5012 // Check the function was really compiled.
5013 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5014 + ufunc->uf_dfunc_idx;
5015 if (INSTRUCTIONS(dfunc) == NULL)
5016 {
5017 iemsg("using call_def_function() on not compiled function");
5018 return FAIL;
5019 }
5020 }
5021
5022 // If depth of calling is getting too high, don't execute the function.
5023 orig_funcdepth = funcdepth_get();
5024 if (funcdepth_increment() == FAIL)
5025 return FAIL;
5026
5027 CLEAR_FIELD(ectx);
5028 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5029 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005030 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005031 {
5032 funcdepth_decrement();
5033 return FAIL;
5034 }
5035 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5036 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5037 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005038 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005039
5040 idx = argc - ufunc->uf_args.ga_len;
5041 if (idx > 0 && ufunc->uf_va_name == NULL)
5042 {
5043 if (idx == 1)
5044 emsg(_(e_one_argument_too_many));
5045 else
5046 semsg(_(e_nr_arguments_too_many), idx);
5047 goto failed_early;
5048 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005049 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5050 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005051 {
5052 if (idx == -1)
5053 emsg(_(e_one_argument_too_few));
5054 else
5055 semsg(_(e_nr_arguments_too_few), -idx);
5056 goto failed_early;
5057 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005058
5059 // Put arguments on the stack, but no more than what the function expects.
5060 // A lambda can be called with more arguments than it uses.
5061 for (idx = 0; idx < argc
5062 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5063 ++idx)
5064 {
5065 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5066 && argv[idx].v_type == VAR_SPECIAL
5067 && argv[idx].vval.v_number == VVAL_NONE)
5068 {
5069 // Use the default value.
5070 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5071 }
5072 else
5073 {
5074 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5075 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005076 ufunc->uf_arg_types[idx], &argv[idx],
5077 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005078 goto failed_early;
5079 copy_tv(&argv[idx], STACK_TV_BOT(0));
5080 }
5081 ++ectx.ec_stack.ga_len;
5082 }
5083
5084 // Turn varargs into a list. Empty list if no args.
5085 if (ufunc->uf_va_name != NULL)
5086 {
5087 int vararg_count = argc - ufunc->uf_args.ga_len;
5088
5089 if (vararg_count < 0)
5090 vararg_count = 0;
5091 else
5092 argc -= vararg_count;
5093 if (exe_newlist(vararg_count, &ectx) == FAIL)
5094 goto failed_early;
5095
5096 // Check the type of the list items.
5097 tv = STACK_TV_BOT(-1);
5098 if (ufunc->uf_va_type != NULL
5099 && ufunc->uf_va_type != &t_list_any
5100 && ufunc->uf_va_type->tt_member != &t_any
5101 && tv->vval.v_list != NULL)
5102 {
5103 type_T *expected = ufunc->uf_va_type->tt_member;
5104 listitem_T *li = tv->vval.v_list->lv_first;
5105
5106 for (idx = 0; idx < vararg_count; ++idx)
5107 {
5108 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005109 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005110 goto failed_early;
5111 li = li->li_next;
5112 }
5113 }
5114
5115 if (defcount > 0)
5116 // Move varargs list to below missing default arguments.
5117 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5118 --ectx.ec_stack.ga_len;
5119 }
5120
5121 // Make space for omitted arguments, will store default value below.
5122 // Any varargs list goes after them.
5123 if (defcount > 0)
5124 for (idx = 0; idx < defcount; ++idx)
5125 {
5126 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5127 ++ectx.ec_stack.ga_len;
5128 }
5129 if (ufunc->uf_va_name != NULL)
5130 ++ectx.ec_stack.ga_len;
5131
5132 // Frame pointer points to just after arguments.
5133 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5134 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5135
5136 {
5137 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5138 + ufunc->uf_dfunc_idx;
5139 ufunc_T *base_ufunc = dfunc->df_ufunc;
5140
5141 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5142 // by copy_func().
5143 if (partial != NULL || base_ufunc->uf_partial != NULL)
5144 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005145 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5146 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005147 goto failed_early;
5148 if (partial != NULL)
5149 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005150 outer_T *outer = get_pt_outer(partial);
5151
5152 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005153 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005154 if (current_ectx != NULL)
5155 {
5156 if (current_ectx->ec_outer_ref != NULL
5157 && current_ectx->ec_outer_ref->or_outer != NULL)
5158 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005159 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005160 }
5161 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005162 }
5163 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005164 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005165 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005166 ++partial->pt_refcount;
5167 ectx.ec_outer_ref->or_partial = partial;
5168 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005169 }
5170 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005171 {
5172 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5173 ++base_ufunc->uf_partial->pt_refcount;
5174 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5175 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005176 }
5177 }
5178
5179 // dummy frame entries
5180 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5181 {
5182 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5183 ++ectx.ec_stack.ga_len;
5184 }
5185
5186 {
5187 // Reserve space for local variables and any closure reference count.
5188 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5189 + ufunc->uf_dfunc_idx;
5190
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005191 // Initialize variables to zero. That avoids having to generate
5192 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005193 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005194 {
5195 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5196 STACK_TV_VAR(idx)->vval.v_number = 0;
5197 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005198 ectx.ec_stack.ga_len += dfunc->df_varcount;
5199 if (dfunc->df_has_closure)
5200 {
5201 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5202 STACK_TV_VAR(idx)->vval.v_number = 0;
5203 ++ectx.ec_stack.ga_len;
5204 }
5205
5206 ectx.ec_instr = INSTRUCTIONS(dfunc);
5207 }
5208
5209 // Following errors are in the function, not the caller.
5210 // Commands behave like vim9script.
5211 estack_push_ufunc(ufunc, 1);
5212 current_sctx = ufunc->uf_script_ctx;
5213 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5214
5215 // Use a specific location for storing error messages to be converted to an
5216 // exception.
5217 saved_msg_list = msg_list;
5218 msg_list = &private_msg_list;
5219
5220 // Do turn errors into exceptions.
5221 suppress_errthrow = FALSE;
5222
5223 // Do not delete the function while executing it.
5224 ++ufunc->uf_calls;
5225
5226 // When ":silent!" was used before calling then we still abort the
5227 // function. If ":silent!" is used in the function then we don't.
5228 emsg_silent_def = emsg_silent;
5229 did_emsg_def = 0;
5230
5231 ectx.ec_where.wt_index = 0;
5232 ectx.ec_where.wt_variable = FALSE;
5233
5234 // Execute the instructions until done.
5235 ret = exec_instructions(&ectx);
5236 if (ret == OK)
5237 {
5238 // function finished, get result from the stack.
5239 if (ufunc->uf_ret_type == &t_void)
5240 {
5241 rettv->v_type = VAR_VOID;
5242 }
5243 else
5244 {
5245 tv = STACK_TV_BOT(-1);
5246 *rettv = *tv;
5247 tv->v_type = VAR_UNKNOWN;
5248 }
5249 }
5250
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005251 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005252 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5253 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005254
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005255 // Deal with any remaining closures, they may be in use somewhere.
5256 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005257 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005258 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005259 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005260 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005261
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005262 estack_pop();
5263 current_sctx = save_current_sctx;
5264
Bram Moolenaar2336c372021-12-06 15:06:54 +00005265 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5266 // Function was unreferenced while being used, free it now.
5267 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005268
Bram Moolenaar352134b2020-10-17 22:04:08 +02005269 if (*msg_list != NULL && saved_msg_list != NULL)
5270 {
5271 msglist_T **plist = saved_msg_list;
5272
5273 // Append entries from the current msg_list (uncaught exceptions) to
5274 // the saved msg_list.
5275 while (*plist != NULL)
5276 plist = &(*plist)->next;
5277
5278 *plist = *msg_list;
5279 }
5280 msg_list = saved_msg_list;
5281
Bram Moolenaar4c137212021-04-19 16:48:48 +02005282 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005283 {
5284 cmdmod.cmod_filter_regmatch.regprog = NULL;
5285 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005286 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005287 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005288 emsg_silent_def = save_emsg_silent_def;
5289 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005290
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005291failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005292 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5294 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005295 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005298 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005299 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005300 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005301 if (ectx.ec_outer_ref->or_outer_allocated)
5302 vim_free(ectx.ec_outer_ref->or_outer);
5303 partial_unref(ectx.ec_outer_ref->or_partial);
5304 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005305 }
5306
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005307 // Not sure if this is necessary.
5308 suppress_errthrow = save_suppress_errthrow;
5309
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005310 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5311 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005312 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005313 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005314 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315 return ret;
5316}
5317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005319 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5320 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5321 * "pfx" is prefixed to every line.
5322 */
5323 static void
5324list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5325{
5326 int line_idx = 0;
5327 int prev_current = 0;
5328 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005329 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005330
5331 for (current = 0; current < instr_count; ++current)
5332 {
5333 isn_T *iptr = &instr[current];
5334 char *line;
5335
5336 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005337 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005338 while (line_idx < iptr->isn_lnum
5339 && line_idx < ufunc->uf_lines.ga_len)
5340 {
5341 if (current > prev_current)
5342 {
5343 msg_puts("\n\n");
5344 prev_current = current;
5345 }
5346 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5347 if (line != NULL)
5348 msg(line);
5349 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005350 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5351 {
5352 int first_def_arg = ufunc->uf_args.ga_len
5353 - ufunc->uf_def_args.ga_len;
5354
5355 if (def_arg_idx > 0)
5356 msg_puts("\n\n");
5357 msg_start();
5358 msg_puts(" ");
5359 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5360 first_def_arg + def_arg_idx]);
5361 msg_puts(" = ");
5362 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5363 msg_clr_eos();
5364 msg_end();
5365 }
5366 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005367
5368 switch (iptr->isn_type)
5369 {
5370 case ISN_EXEC:
5371 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5372 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005373 case ISN_EXEC_SPLIT:
5374 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5375 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005376 case ISN_EXECRANGE:
5377 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5378 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005379 case ISN_LEGACY_EVAL:
5380 smsg("%s%4d EVAL legacy %s", pfx, current,
5381 iptr->isn_arg.string);
5382 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005383 case ISN_REDIRSTART:
5384 smsg("%s%4d REDIR", pfx, current);
5385 break;
5386 case ISN_REDIREND:
5387 smsg("%s%4d REDIR END%s", pfx, current,
5388 iptr->isn_arg.number ? " append" : "");
5389 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005390 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005391#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005392 smsg("%s%4d CEXPR pre %s", pfx, current,
5393 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005394#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005395 break;
5396 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005397#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005398 {
5399 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5400
5401 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5402 cexpr_get_auname(cer->cer_cmdidx),
5403 cer->cer_forceit ? "!" : "",
5404 cer->cer_cmdline);
5405 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005406#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005407 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005408 case ISN_INSTR:
5409 {
5410 smsg("%s%4d INSTR", pfx, current);
5411 list_instructions(" ", iptr->isn_arg.instr,
5412 INT_MAX, NULL);
5413 msg(" -------------");
5414 }
5415 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005416 case ISN_SUBSTITUTE:
5417 {
5418 subs_T *subs = &iptr->isn_arg.subs;
5419
5420 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5421 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5422 msg(" -------------");
5423 }
5424 break;
5425 case ISN_EXECCONCAT:
5426 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5427 (varnumber_T)iptr->isn_arg.number);
5428 break;
5429 case ISN_ECHO:
5430 {
5431 echo_T *echo = &iptr->isn_arg.echo;
5432
5433 smsg("%s%4d %s %d", pfx, current,
5434 echo->echo_with_white ? "ECHO" : "ECHON",
5435 echo->echo_count);
5436 }
5437 break;
5438 case ISN_EXECUTE:
5439 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005440 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005441 break;
5442 case ISN_ECHOMSG:
5443 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005444 (varnumber_T)(iptr->isn_arg.number));
5445 break;
5446 case ISN_ECHOCONSOLE:
5447 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5448 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005449 break;
5450 case ISN_ECHOERR:
5451 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005452 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005453 break;
5454 case ISN_LOAD:
5455 {
5456 if (iptr->isn_arg.number < 0)
5457 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5458 (varnumber_T)(iptr->isn_arg.number
5459 + STACK_FRAME_SIZE));
5460 else
5461 smsg("%s%4d LOAD $%lld", pfx, current,
5462 (varnumber_T)(iptr->isn_arg.number));
5463 }
5464 break;
5465 case ISN_LOADOUTER:
5466 {
5467 if (iptr->isn_arg.number < 0)
5468 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5469 iptr->isn_arg.outer.outer_depth,
5470 iptr->isn_arg.outer.outer_idx
5471 + STACK_FRAME_SIZE);
5472 else
5473 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5474 iptr->isn_arg.outer.outer_depth,
5475 iptr->isn_arg.outer.outer_idx);
5476 }
5477 break;
5478 case ISN_LOADV:
5479 smsg("%s%4d LOADV v:%s", pfx, current,
5480 get_vim_var_name(iptr->isn_arg.number));
5481 break;
5482 case ISN_LOADSCRIPT:
5483 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005484 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5485 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5486 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005487
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005488 sv = get_script_svar(sref, -1);
5489 if (sv == NULL)
5490 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5491 pfx, current, si->sn_name);
5492 else
5493 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005494 sv->sv_name,
5495 sref->sref_idx,
5496 si->sn_name);
5497 }
5498 break;
5499 case ISN_LOADS:
5500 {
5501 scriptitem_T *si = SCRIPT_ITEM(
5502 iptr->isn_arg.loadstore.ls_sid);
5503
5504 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5505 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5506 }
5507 break;
5508 case ISN_LOADAUTO:
5509 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5510 break;
5511 case ISN_LOADG:
5512 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5513 break;
5514 case ISN_LOADB:
5515 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5516 break;
5517 case ISN_LOADW:
5518 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5519 break;
5520 case ISN_LOADT:
5521 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5522 break;
5523 case ISN_LOADGDICT:
5524 smsg("%s%4d LOAD g:", pfx, current);
5525 break;
5526 case ISN_LOADBDICT:
5527 smsg("%s%4d LOAD b:", pfx, current);
5528 break;
5529 case ISN_LOADWDICT:
5530 smsg("%s%4d LOAD w:", pfx, current);
5531 break;
5532 case ISN_LOADTDICT:
5533 smsg("%s%4d LOAD t:", pfx, current);
5534 break;
5535 case ISN_LOADOPT:
5536 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5537 break;
5538 case ISN_LOADENV:
5539 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5540 break;
5541 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005542 smsg("%s%4d LOADREG @%c", pfx, current,
5543 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005544 break;
5545
5546 case ISN_STORE:
5547 if (iptr->isn_arg.number < 0)
5548 smsg("%s%4d STORE arg[%lld]", pfx, current,
5549 iptr->isn_arg.number + STACK_FRAME_SIZE);
5550 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005551 smsg("%s%4d STORE $%lld", pfx, current,
5552 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005553 break;
5554 case ISN_STOREOUTER:
5555 {
5556 if (iptr->isn_arg.number < 0)
5557 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5558 iptr->isn_arg.outer.outer_depth,
5559 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5560 else
5561 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5562 iptr->isn_arg.outer.outer_depth,
5563 iptr->isn_arg.outer.outer_idx);
5564 }
5565 break;
5566 case ISN_STOREV:
5567 smsg("%s%4d STOREV v:%s", pfx, current,
5568 get_vim_var_name(iptr->isn_arg.number));
5569 break;
5570 case ISN_STOREAUTO:
5571 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5572 break;
5573 case ISN_STOREG:
5574 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5575 break;
5576 case ISN_STOREB:
5577 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5578 break;
5579 case ISN_STOREW:
5580 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5581 break;
5582 case ISN_STORET:
5583 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5584 break;
5585 case ISN_STORES:
5586 {
5587 scriptitem_T *si = SCRIPT_ITEM(
5588 iptr->isn_arg.loadstore.ls_sid);
5589
5590 smsg("%s%4d STORES %s in %s", pfx, current,
5591 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5592 }
5593 break;
5594 case ISN_STORESCRIPT:
5595 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005596 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5597 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5598 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005599
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005600 sv = get_script_svar(sref, -1);
5601 if (sv == NULL)
5602 smsg("%s%4d STORESCRIPT [deleted] in %s",
5603 pfx, current, si->sn_name);
5604 else
5605 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005606 sv->sv_name,
5607 sref->sref_idx,
5608 si->sn_name);
5609 }
5610 break;
5611 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005612 case ISN_STOREFUNCOPT:
5613 smsg("%s%4d %s &%s", pfx, current,
5614 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005615 iptr->isn_arg.storeopt.so_name);
5616 break;
5617 case ISN_STOREENV:
5618 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5619 break;
5620 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005621 smsg("%s%4d STOREREG @%c", pfx, current,
5622 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005623 break;
5624 case ISN_STORENR:
5625 smsg("%s%4d STORE %lld in $%d", pfx, current,
5626 iptr->isn_arg.storenr.stnr_val,
5627 iptr->isn_arg.storenr.stnr_idx);
5628 break;
5629
5630 case ISN_STOREINDEX:
5631 smsg("%s%4d STOREINDEX %s", pfx, current,
5632 vartype_name(iptr->isn_arg.vartype));
5633 break;
5634
5635 case ISN_STORERANGE:
5636 smsg("%s%4d STORERANGE", pfx, current);
5637 break;
5638
5639 // constants
5640 case ISN_PUSHNR:
5641 smsg("%s%4d PUSHNR %lld", pfx, current,
5642 (varnumber_T)(iptr->isn_arg.number));
5643 break;
5644 case ISN_PUSHBOOL:
5645 case ISN_PUSHSPEC:
5646 smsg("%s%4d PUSH %s", pfx, current,
5647 get_var_special_name(iptr->isn_arg.number));
5648 break;
5649 case ISN_PUSHF:
5650#ifdef FEAT_FLOAT
5651 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5652#endif
5653 break;
5654 case ISN_PUSHS:
5655 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5656 break;
5657 case ISN_PUSHBLOB:
5658 {
5659 char_u *r;
5660 char_u numbuf[NUMBUFLEN];
5661 char_u *tofree;
5662
5663 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5664 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5665 vim_free(tofree);
5666 }
5667 break;
5668 case ISN_PUSHFUNC:
5669 {
5670 char *name = (char *)iptr->isn_arg.string;
5671
5672 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5673 name == NULL ? "[none]" : name);
5674 }
5675 break;
5676 case ISN_PUSHCHANNEL:
5677#ifdef FEAT_JOB_CHANNEL
5678 {
5679 channel_T *channel = iptr->isn_arg.channel;
5680
5681 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5682 channel == NULL ? 0 : channel->ch_id);
5683 }
5684#endif
5685 break;
5686 case ISN_PUSHJOB:
5687#ifdef FEAT_JOB_CHANNEL
5688 {
5689 typval_T tv;
5690 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005691 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005692
5693 tv.v_type = VAR_JOB;
5694 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005695 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005696 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5697 }
5698#endif
5699 break;
5700 case ISN_PUSHEXC:
5701 smsg("%s%4d PUSH v:exception", pfx, current);
5702 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005703 case ISN_AUTOLOAD:
5704 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5705 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005706 case ISN_UNLET:
5707 smsg("%s%4d UNLET%s %s", pfx, current,
5708 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5709 iptr->isn_arg.unlet.ul_name);
5710 break;
5711 case ISN_UNLETENV:
5712 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5713 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5714 iptr->isn_arg.unlet.ul_name);
5715 break;
5716 case ISN_UNLETINDEX:
5717 smsg("%s%4d UNLETINDEX", pfx, current);
5718 break;
5719 case ISN_UNLETRANGE:
5720 smsg("%s%4d UNLETRANGE", pfx, current);
5721 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005722 case ISN_LOCKUNLOCK:
5723 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5724 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005725 case ISN_LOCKCONST:
5726 smsg("%s%4d LOCKCONST", pfx, current);
5727 break;
5728 case ISN_NEWLIST:
5729 smsg("%s%4d NEWLIST size %lld", pfx, current,
5730 (varnumber_T)(iptr->isn_arg.number));
5731 break;
5732 case ISN_NEWDICT:
5733 smsg("%s%4d NEWDICT size %lld", pfx, current,
5734 (varnumber_T)(iptr->isn_arg.number));
5735 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005736 case ISN_NEWPARTIAL:
5737 smsg("%s%4d NEWPARTIAL", pfx, current);
5738 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005739
5740 // function call
5741 case ISN_BCALL:
5742 {
5743 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5744
5745 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5746 internal_func_name(cbfunc->cbf_idx),
5747 cbfunc->cbf_argcount);
5748 }
5749 break;
5750 case ISN_DCALL:
5751 {
5752 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5753 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5754 + cdfunc->cdf_idx;
5755
5756 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005757 printable_func_name(df->df_ufunc),
5758 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005759 }
5760 break;
5761 case ISN_UCALL:
5762 {
5763 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5764
5765 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5766 cufunc->cuf_name, cufunc->cuf_argcount);
5767 }
5768 break;
5769 case ISN_PCALL:
5770 {
5771 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5772
5773 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5774 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5775 }
5776 break;
5777 case ISN_PCALL_END:
5778 smsg("%s%4d PCALL end", pfx, current);
5779 break;
5780 case ISN_RETURN:
5781 smsg("%s%4d RETURN", pfx, current);
5782 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005783 case ISN_RETURN_VOID:
5784 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005785 break;
5786 case ISN_FUNCREF:
5787 {
5788 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005789 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005790
Bram Moolenaar38453522021-11-28 22:00:12 +00005791 if (funcref->fr_func_name == NULL)
5792 {
5793 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5794 + funcref->fr_dfunc_idx;
5795 name = df->df_ufunc->uf_name;
5796 }
5797 else
5798 name = funcref->fr_func_name;
5799 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005800 }
5801 break;
5802
5803 case ISN_NEWFUNC:
5804 {
5805 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5806
5807 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5808 newfunc->nf_lambda, newfunc->nf_global);
5809 }
5810 break;
5811
5812 case ISN_DEF:
5813 {
5814 char_u *name = iptr->isn_arg.string;
5815
5816 smsg("%s%4d DEF %s", pfx, current,
5817 name == NULL ? (char_u *)"" : name);
5818 }
5819 break;
5820
5821 case ISN_JUMP:
5822 {
5823 char *when = "?";
5824
5825 switch (iptr->isn_arg.jump.jump_when)
5826 {
5827 case JUMP_ALWAYS:
5828 when = "JUMP";
5829 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005830 case JUMP_NEVER:
5831 iemsg("JUMP_NEVER should not be used");
5832 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005833 case JUMP_AND_KEEP_IF_TRUE:
5834 when = "JUMP_AND_KEEP_IF_TRUE";
5835 break;
5836 case JUMP_IF_FALSE:
5837 when = "JUMP_IF_FALSE";
5838 break;
5839 case JUMP_AND_KEEP_IF_FALSE:
5840 when = "JUMP_AND_KEEP_IF_FALSE";
5841 break;
5842 case JUMP_IF_COND_FALSE:
5843 when = "JUMP_IF_COND_FALSE";
5844 break;
5845 case JUMP_IF_COND_TRUE:
5846 when = "JUMP_IF_COND_TRUE";
5847 break;
5848 }
5849 smsg("%s%4d %s -> %d", pfx, current, when,
5850 iptr->isn_arg.jump.jump_where);
5851 }
5852 break;
5853
5854 case ISN_JUMP_IF_ARG_SET:
5855 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5856 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5857 iptr->isn_arg.jump.jump_where);
5858 break;
5859
5860 case ISN_FOR:
5861 {
5862 forloop_T *forloop = &iptr->isn_arg.forloop;
5863
5864 smsg("%s%4d FOR $%d -> %d", pfx, current,
5865 forloop->for_idx, forloop->for_end);
5866 }
5867 break;
5868
5869 case ISN_TRY:
5870 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005871 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005872
5873 if (try->try_ref->try_finally == 0)
5874 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5875 pfx, current,
5876 try->try_ref->try_catch,
5877 try->try_ref->try_endtry);
5878 else
5879 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5880 pfx, current,
5881 try->try_ref->try_catch,
5882 try->try_ref->try_finally,
5883 try->try_ref->try_endtry);
5884 }
5885 break;
5886 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005887 smsg("%s%4d CATCH", pfx, current);
5888 break;
5889 case ISN_TRYCONT:
5890 {
5891 trycont_T *trycont = &iptr->isn_arg.trycont;
5892
5893 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5894 trycont->tct_levels,
5895 trycont->tct_levels == 1 ? "" : "s",
5896 trycont->tct_where);
5897 }
5898 break;
5899 case ISN_FINALLY:
5900 smsg("%s%4d FINALLY", pfx, current);
5901 break;
5902 case ISN_ENDTRY:
5903 smsg("%s%4d ENDTRY", pfx, current);
5904 break;
5905 case ISN_THROW:
5906 smsg("%s%4d THROW", pfx, current);
5907 break;
5908
5909 // expression operations on number
5910 case ISN_OPNR:
5911 case ISN_OPFLOAT:
5912 case ISN_OPANY:
5913 {
5914 char *what;
5915 char *ins;
5916
5917 switch (iptr->isn_arg.op.op_type)
5918 {
5919 case EXPR_MULT: what = "*"; break;
5920 case EXPR_DIV: what = "/"; break;
5921 case EXPR_REM: what = "%"; break;
5922 case EXPR_SUB: what = "-"; break;
5923 case EXPR_ADD: what = "+"; break;
5924 default: what = "???"; break;
5925 }
5926 switch (iptr->isn_type)
5927 {
5928 case ISN_OPNR: ins = "OPNR"; break;
5929 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5930 case ISN_OPANY: ins = "OPANY"; break;
5931 default: ins = "???"; break;
5932 }
5933 smsg("%s%4d %s %s", pfx, current, ins, what);
5934 }
5935 break;
5936
5937 case ISN_COMPAREBOOL:
5938 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00005939 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005940 case ISN_COMPARENR:
5941 case ISN_COMPAREFLOAT:
5942 case ISN_COMPARESTRING:
5943 case ISN_COMPAREBLOB:
5944 case ISN_COMPARELIST:
5945 case ISN_COMPAREDICT:
5946 case ISN_COMPAREFUNC:
5947 case ISN_COMPAREANY:
5948 {
5949 char *p;
5950 char buf[10];
5951 char *type;
5952
5953 switch (iptr->isn_arg.op.op_type)
5954 {
5955 case EXPR_EQUAL: p = "=="; break;
5956 case EXPR_NEQUAL: p = "!="; break;
5957 case EXPR_GREATER: p = ">"; break;
5958 case EXPR_GEQUAL: p = ">="; break;
5959 case EXPR_SMALLER: p = "<"; break;
5960 case EXPR_SEQUAL: p = "<="; break;
5961 case EXPR_MATCH: p = "=~"; break;
5962 case EXPR_IS: p = "is"; break;
5963 case EXPR_ISNOT: p = "isnot"; break;
5964 case EXPR_NOMATCH: p = "!~"; break;
5965 default: p = "???"; break;
5966 }
5967 STRCPY(buf, p);
5968 if (iptr->isn_arg.op.op_ic == TRUE)
5969 strcat(buf, "?");
5970 switch(iptr->isn_type)
5971 {
5972 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5973 case ISN_COMPARESPECIAL:
5974 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00005975 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005976 case ISN_COMPARENR: type = "COMPARENR"; break;
5977 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5978 case ISN_COMPARESTRING:
5979 type = "COMPARESTRING"; break;
5980 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5981 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5982 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5983 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5984 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5985 default: type = "???"; break;
5986 }
5987
5988 smsg("%s%4d %s %s", pfx, current, type, buf);
5989 }
5990 break;
5991
5992 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5993 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5994
5995 // expression operations
5996 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5997 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5998 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5999 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6000 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6001 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6002 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6003 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6004 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6005 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6006 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6007 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006008 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006009 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6010 iptr->isn_arg.getitem.gi_index,
6011 iptr->isn_arg.getitem.gi_with_op ?
6012 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006013 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6014 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6015 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006016 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6017 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6018
Bram Moolenaar4c137212021-04-19 16:48:48 +02006019 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6020
6021 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6022 case ISN_CHECKTYPE:
6023 {
6024 checktype_T *ct = &iptr->isn_arg.type;
6025 char *tofree;
6026
6027 if (ct->ct_arg_idx == 0)
6028 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6029 type_name(ct->ct_type, &tofree),
6030 (int)ct->ct_off);
6031 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006032 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6033 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006034 type_name(ct->ct_type, &tofree),
6035 (int)ct->ct_off,
6036 (int)ct->ct_arg_idx);
6037 vim_free(tofree);
6038 break;
6039 }
6040 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6041 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6042 iptr->isn_arg.checklen.cl_min_len);
6043 break;
6044 case ISN_SETTYPE:
6045 {
6046 char *tofree;
6047
6048 smsg("%s%4d SETTYPE %s", pfx, current,
6049 type_name(iptr->isn_arg.type.ct_type, &tofree));
6050 vim_free(tofree);
6051 break;
6052 }
6053 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006054 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6055 smsg("%s%4d INVERT %d (!val)", pfx, current,
6056 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006057 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006058 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6059 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006060 break;
6061 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006062 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006063 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006064 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6065 pfx, current,
6066 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006067 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006068 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6069 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006070 break;
6071 case ISN_PUT:
6072 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6073 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006074 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006075 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6076 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006077 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006078 else
6079 smsg("%s%4d PUT %c %ld", pfx, current,
6080 iptr->isn_arg.put.put_regname,
6081 (long)iptr->isn_arg.put.put_lnum);
6082 break;
6083
Bram Moolenaar4c137212021-04-19 16:48:48 +02006084 case ISN_CMDMOD:
6085 {
6086 char_u *buf;
6087 size_t len = produce_cmdmods(
6088 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6089
6090 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006091 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006092 {
6093 (void)produce_cmdmods(
6094 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6095 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6096 vim_free(buf);
6097 }
6098 break;
6099 }
6100 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6101
6102 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006103 smsg("%s%4d PROFILE START line %d", pfx, current,
6104 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006105 break;
6106
6107 case ISN_PROF_END:
6108 smsg("%s%4d PROFILE END", pfx, current);
6109 break;
6110
Bram Moolenaare99d4222021-06-13 14:01:26 +02006111 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006112 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6113 iptr->isn_arg.debug.dbg_break_lnum + 1,
6114 iptr->isn_lnum,
6115 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006116 break;
6117
Bram Moolenaar4c137212021-04-19 16:48:48 +02006118 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6119 iptr->isn_arg.unpack.unp_count,
6120 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6121 break;
6122 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6123 iptr->isn_arg.shuffle.shfl_item,
6124 iptr->isn_arg.shuffle.shfl_up);
6125 break;
6126 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6127
6128 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6129 return;
6130 }
6131
6132 out_flush(); // output one line at a time
6133 ui_breakcheck();
6134 if (got_int)
6135 break;
6136 }
6137}
6138
6139/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006140 * Handle command line completion for the :disassemble command.
6141 */
6142 void
6143set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6144{
6145 char_u *p;
6146
6147 // Default: expand user functions, "debug" and "profile"
6148 xp->xp_context = EXPAND_DISASSEMBLE;
6149 xp->xp_pattern = arg;
6150
6151 // first argument already typed: only user function names
6152 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6153 {
6154 xp->xp_context = EXPAND_USER_FUNC;
6155 xp->xp_pattern = skipwhite(p);
6156 }
6157}
6158
6159/*
6160 * Function given to ExpandGeneric() to obtain the list of :disassemble
6161 * arguments.
6162 */
6163 char_u *
6164get_disassemble_argument(expand_T *xp, int idx)
6165{
6166 if (idx == 0)
6167 return (char_u *)"debug";
6168 if (idx == 1)
6169 return (char_u *)"profile";
6170 return get_user_func_name(xp, idx - 2);
6171}
6172
6173/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006174 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006175 * We don't really need this at runtime, but we do have tests that require it,
6176 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 */
6178 void
6179ex_disassemble(exarg_T *eap)
6180{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006181 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006182 char_u *fname;
6183 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184 dfunc_T *dfunc;
6185 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006186 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006187 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006188 compiletype_T compile_type = CT_NONE;
6189
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006190 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006191 {
6192 compile_type = CT_PROFILE;
6193 arg = skipwhite(arg + 7);
6194 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006195 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006196 {
6197 compile_type = CT_DEBUG;
6198 arg = skipwhite(arg + 5);
6199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006201 if (STRNCMP(arg, "<lambda>", 8) == 0)
6202 {
6203 arg += 8;
6204 (void)getdigits(&arg);
6205 fname = vim_strnsave(eap->arg, arg - eap->arg);
6206 }
6207 else
6208 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006209 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006210 if (fname == NULL)
6211 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006212 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006213 return;
6214 }
6215
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006216 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006217 if (ufunc == NULL)
6218 {
6219 char_u *p = untrans_function_name(fname);
6220
6221 if (p != NULL)
6222 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006223 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006224 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006225 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226 if (ufunc == NULL)
6227 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006228 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229 return;
6230 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006231 if (func_needs_compiling(ufunc, compile_type)
6232 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006233 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006234 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006236 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237 return;
6238 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006239 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240
6241 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006242 switch (compile_type)
6243 {
6244 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006245#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006246 instr = dfunc->df_instr_prof;
6247 instr_count = dfunc->df_instr_prof_count;
6248 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006249#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006250 // FALLTHROUGH
6251 case CT_NONE:
6252 instr = dfunc->df_instr;
6253 instr_count = dfunc->df_instr_count;
6254 break;
6255 case CT_DEBUG:
6256 instr = dfunc->df_instr_debug;
6257 instr_count = dfunc->df_instr_debug_count;
6258 break;
6259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260
Bram Moolenaar4c137212021-04-19 16:48:48 +02006261 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262}
6263
6264/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006265 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266 * list, etc. Mostly like what JavaScript does, except that empty list and
6267 * empty dictionary are FALSE.
6268 */
6269 int
6270tv2bool(typval_T *tv)
6271{
6272 switch (tv->v_type)
6273 {
6274 case VAR_NUMBER:
6275 return tv->vval.v_number != 0;
6276 case VAR_FLOAT:
6277#ifdef FEAT_FLOAT
6278 return tv->vval.v_float != 0.0;
6279#else
6280 break;
6281#endif
6282 case VAR_PARTIAL:
6283 return tv->vval.v_partial != NULL;
6284 case VAR_FUNC:
6285 case VAR_STRING:
6286 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6287 case VAR_LIST:
6288 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6289 case VAR_DICT:
6290 return tv->vval.v_dict != NULL
6291 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6292 case VAR_BOOL:
6293 case VAR_SPECIAL:
6294 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6295 case VAR_JOB:
6296#ifdef FEAT_JOB_CHANNEL
6297 return tv->vval.v_job != NULL;
6298#else
6299 break;
6300#endif
6301 case VAR_CHANNEL:
6302#ifdef FEAT_JOB_CHANNEL
6303 return tv->vval.v_channel != NULL;
6304#else
6305 break;
6306#endif
6307 case VAR_BLOB:
6308 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6309 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006310 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006312 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 break;
6314 }
6315 return FALSE;
6316}
6317
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006318 void
6319emsg_using_string_as(typval_T *tv, int as_number)
6320{
6321 semsg(_(as_number ? e_using_string_as_number_str
6322 : e_using_string_as_bool_str),
6323 tv->vval.v_string == NULL
6324 ? (char_u *)"" : tv->vval.v_string);
6325}
6326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327/*
6328 * If "tv" is a string give an error and return FAIL.
6329 */
6330 int
6331check_not_string(typval_T *tv)
6332{
6333 if (tv->v_type == VAR_STRING)
6334 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006335 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336 clear_tv(tv);
6337 return FAIL;
6338 }
6339 return OK;
6340}
6341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006342
6343#endif // FEAT_EVAL