blob: d908e64314ca5ed82518f3882ec2ba7f466fdef5 [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 */
155 static void
156update_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 Moolenaar6bc30b02021-06-16 19:19:55 +02001625 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001626 return STACK_TV_VAR(idx);
1627 }
1628
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001629 // Go through argument names.
1630 ufunc = dfunc->df_ufunc;
1631 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1632 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1633 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1634 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1635 - varargs_off + idx);
1636 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1637 return STACK_TV(ectx->ec_frame_idx - 1);
1638
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001639 return NULL;
1640}
1641
Bram Moolenaar26a44842021-09-02 18:49:06 +02001642/*
1643 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1644 * breakpoint was set in that function or when there is any expression.
1645 */
1646 int
1647may_break_in_function(ufunc_T *ufunc)
1648{
1649 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1650}
1651
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001652 static void
1653handle_debug(isn_T *iptr, ectx_T *ectx)
1654{
1655 char_u *line;
1656 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1657 + ectx->ec_dfunc_idx)->df_ufunc;
1658 isn_T *ni;
1659 int end_lnum = iptr->isn_lnum;
1660 garray_T ga;
1661 int lnum;
1662
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001663 if (ex_nesting_level > debug_break_level)
1664 {
1665 linenr_T breakpoint;
1666
Bram Moolenaar26a44842021-09-02 18:49:06 +02001667 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001668 return;
1669
1670 // check for the next breakpoint if needed
1671 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001672 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001673 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1674 return;
1675 }
1676
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001677 SOURCING_LNUM = iptr->isn_lnum;
1678 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001679 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001680
1681 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1682 if (ni->isn_type == ISN_DEBUG
1683 || ni->isn_type == ISN_RETURN
1684 || ni->isn_type == ISN_RETURN_VOID)
1685 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001686 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001687 break;
1688 }
1689
1690 if (end_lnum > iptr->isn_lnum)
1691 {
1692 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001693 for (lnum = iptr->isn_lnum; lnum < end_lnum
1694 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001695 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001696 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001697
Bram Moolenaar303215d2021-07-07 20:10:43 +02001698 if (p == NULL)
1699 continue; // left over from continuation line
1700 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001701 if (*p == '#')
1702 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001703 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001704 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1705 if (STRNCMP(p, "def ", 4) == 0)
1706 break;
1707 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001708 line = ga_concat_strings(&ga, " ");
1709 vim_free(ga.ga_data);
1710 }
1711 else
1712 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001713
Dominique Pelle6e969552021-06-17 13:53:41 +02001714 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001715 debug_context = NULL;
1716
1717 if (end_lnum > iptr->isn_lnum)
1718 vim_free(line);
1719}
1720
Bram Moolenaar4c137212021-04-19 16:48:48 +02001721/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001722 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001723 * Returns OK, FAIL or NOTDONE (uncatchable error).
1724 */
1725 static int
1726execute_storeindex(isn_T *iptr, ectx_T *ectx)
1727{
1728 vartype_T dest_type = iptr->isn_arg.vartype;
1729 typval_T *tv;
1730 typval_T *tv_idx = STACK_TV_BOT(-2);
1731 typval_T *tv_dest = STACK_TV_BOT(-1);
1732 int status = OK;
1733
1734 // Stack contains:
1735 // -3 value to be stored
1736 // -2 index
1737 // -1 dict or list
1738 tv = STACK_TV_BOT(-3);
1739 SOURCING_LNUM = iptr->isn_lnum;
1740 if (dest_type == VAR_ANY)
1741 {
1742 dest_type = tv_dest->v_type;
1743 if (dest_type == VAR_DICT)
1744 status = do_2string(tv_idx, TRUE, FALSE);
1745 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1746 {
1747 emsg(_(e_number_expected));
1748 status = FAIL;
1749 }
1750 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001751
1752 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001753 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001754 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001755 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001756 long lidx = (long)tv_idx->vval.v_number;
1757 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001758
Bram Moolenaare08be092022-02-17 13:08:26 +00001759 if (list == NULL)
1760 {
1761 emsg(_(e_list_not_set));
1762 return FAIL;
1763 }
1764 if (lidx < 0 && list->lv_len + lidx >= 0)
1765 // negative index is relative to the end
1766 lidx = list->lv_len + lidx;
1767 if (lidx < 0 || lidx > list->lv_len)
1768 {
1769 semsg(_(e_list_index_out_of_range_nr), lidx);
1770 return FAIL;
1771 }
1772 if (lidx < list->lv_len)
1773 {
1774 listitem_T *li = list_find(list, lidx);
1775
1776 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001777 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001778 return FAIL;
1779 // overwrite existing list item
1780 clear_tv(&li->li_tv);
1781 li->li_tv = *tv;
1782 }
1783 else
1784 {
1785 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1786 return FAIL;
1787 // append to list, only fails when out of memory
1788 if (list_append_tv(list, tv) == FAIL)
1789 return NOTDONE;
1790 clear_tv(tv);
1791 }
1792 }
1793 else if (dest_type == VAR_DICT)
1794 {
1795 char_u *key = tv_idx->vval.v_string;
1796 dict_T *dict = tv_dest->vval.v_dict;
1797 dictitem_T *di;
1798
1799 SOURCING_LNUM = iptr->isn_lnum;
1800 if (dict == NULL)
1801 {
1802 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001803 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001804 }
1805 if (key == NULL)
1806 key = (char_u *)"";
1807 di = dict_find(dict, key, -1);
1808 if (di != NULL)
1809 {
1810 if (error_if_locked(di->di_tv.v_lock,
1811 e_cannot_change_dict_item))
1812 return FAIL;
1813 // overwrite existing value
1814 clear_tv(&di->di_tv);
1815 di->di_tv = *tv;
1816 }
1817 else
1818 {
1819 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1820 return FAIL;
1821 // add to dict, only fails when out of memory
1822 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1823 return NOTDONE;
1824 clear_tv(tv);
1825 }
1826 }
1827 else if (dest_type == VAR_BLOB)
1828 {
1829 long lidx = (long)tv_idx->vval.v_number;
1830 blob_T *blob = tv_dest->vval.v_blob;
1831 varnumber_T nr;
1832 int error = FALSE;
1833 int len;
1834
1835 if (blob == NULL)
1836 {
1837 emsg(_(e_blob_not_set));
1838 return FAIL;
1839 }
1840 len = blob_len(blob);
1841 if (lidx < 0 && len + lidx >= 0)
1842 // negative index is relative to the end
1843 lidx = len + lidx;
1844
1845 // Can add one byte at the end.
1846 if (lidx < 0 || lidx > len)
1847 {
1848 semsg(_(e_blob_index_out_of_range_nr), lidx);
1849 return FAIL;
1850 }
1851 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1852 return FAIL;
1853 nr = tv_get_number_chk(tv, &error);
1854 if (error)
1855 return FAIL;
1856 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001857 }
1858 else
1859 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001860 status = FAIL;
1861 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001862 }
1863 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001864
1865 clear_tv(tv_idx);
1866 clear_tv(tv_dest);
1867 ectx->ec_stack.ga_len -= 3;
1868 if (status == FAIL)
1869 {
1870 clear_tv(tv);
1871 return FAIL;
1872 }
1873 return OK;
1874}
1875
1876/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001877 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001878 */
1879 static int
1880execute_storerange(isn_T *iptr, ectx_T *ectx)
1881{
1882 typval_T *tv;
1883 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1884 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1885 typval_T *tv_dest = STACK_TV_BOT(-1);
1886 int status = OK;
1887
1888 // Stack contains:
1889 // -4 value to be stored
1890 // -3 first index or "none"
1891 // -2 second index or "none"
1892 // -1 destination list or blob
1893 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001894 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001895 if (tv_dest->v_type == VAR_LIST)
1896 {
1897 long n1;
1898 long n2;
1899 int error = FALSE;
1900
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001901 n1 = (long)tv_get_number_chk(tv_idx1, &error);
1902 if (error)
1903 status = FAIL;
1904 else
1905 {
1906 if (tv_idx2->v_type == VAR_SPECIAL
1907 && tv_idx2->vval.v_number == VVAL_NONE)
1908 n2 = list_len(tv_dest->vval.v_list) - 1;
1909 else
1910 n2 = (long)tv_get_number_chk(tv_idx2, &error);
1911 if (error)
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001912 status = FAIL; // cannot happen?
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001913 else
1914 {
1915 listitem_T *li1 = check_range_index_one(
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001916 tv_dest->vval.v_list, &n1, FALSE);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001917
1918 if (li1 == NULL)
1919 status = FAIL;
1920 else
1921 {
1922 status = check_range_index_two(
1923 tv_dest->vval.v_list,
1924 &n1, li1, &n2, FALSE);
1925 if (status != FAIL)
1926 status = list_assign_range(
1927 tv_dest->vval.v_list,
1928 tv->vval.v_list,
1929 n1,
1930 n2,
1931 tv_idx2->v_type == VAR_SPECIAL,
1932 (char_u *)"=",
1933 (char_u *)"[unknown]");
1934 }
1935 }
1936 }
1937 }
1938 else if (tv_dest->v_type == VAR_BLOB)
1939 {
1940 varnumber_T n1;
1941 varnumber_T n2;
1942 int error = FALSE;
1943
1944 n1 = tv_get_number_chk(tv_idx1, &error);
1945 if (error)
1946 status = FAIL;
1947 else
1948 {
1949 if (tv_idx2->v_type == VAR_SPECIAL
1950 && tv_idx2->vval.v_number == VVAL_NONE)
1951 n2 = blob_len(tv_dest->vval.v_blob) - 1;
1952 else
1953 n2 = tv_get_number_chk(tv_idx2, &error);
1954 if (error)
1955 status = FAIL;
1956 else
1957 {
1958 long bloblen = blob_len(tv_dest->vval.v_blob);
1959
1960 if (check_blob_index(bloblen,
1961 n1, FALSE) == FAIL
1962 || check_blob_range(bloblen,
1963 n1, n2, FALSE) == FAIL)
1964 status = FAIL;
1965 else
1966 status = blob_set_range(
1967 tv_dest->vval.v_blob, n1, n2, tv);
1968 }
1969 }
1970 }
1971 else
1972 {
1973 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001974 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001975 }
1976
1977 clear_tv(tv_idx1);
1978 clear_tv(tv_idx2);
1979 clear_tv(tv_dest);
1980 ectx->ec_stack.ga_len -= 4;
1981 clear_tv(tv);
1982
1983 return status;
1984}
1985
1986/*
1987 * Unlet item in list or dict variable.
1988 */
1989 static int
1990execute_unletindex(isn_T *iptr, ectx_T *ectx)
1991{
1992 typval_T *tv_idx = STACK_TV_BOT(-2);
1993 typval_T *tv_dest = STACK_TV_BOT(-1);
1994 int status = OK;
1995
1996 // Stack contains:
1997 // -2 index
1998 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00001999 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002000 if (tv_dest->v_type == VAR_DICT)
2001 {
2002 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002003 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002004 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002005 semsg(_(e_expected_str_but_got_str),
2006 vartype_name(VAR_STRING),
2007 vartype_name(tv_idx->v_type));
2008 status = FAIL;
2009 }
2010 else
2011 {
2012 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002013 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002014 dictitem_T *di = NULL;
2015
2016 if (d != NULL && value_check_lock(
2017 d->dv_lock, NULL, FALSE))
2018 status = FAIL;
2019 else
2020 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002021 if (tv_idx->v_type == VAR_STRING)
2022 {
2023 key = tv_idx->vval.v_string;
2024 if (key == NULL)
2025 key = (char_u *)"";
2026 }
2027 else
2028 {
2029 key = tv_get_string(tv_idx);
2030 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002031 if (d != NULL)
2032 di = dict_find(d, key, (int)STRLEN(key));
2033 if (di == NULL)
2034 {
2035 // NULL dict is equivalent to empty dict
2036 semsg(_(e_key_not_present_in_dictionary),
2037 key);
2038 status = FAIL;
2039 }
2040 else if (var_check_fixed(di->di_flags,
2041 NULL, FALSE)
2042 || var_check_ro(di->di_flags,
2043 NULL, FALSE))
2044 status = FAIL;
2045 else
2046 dictitem_remove(d, di);
2047 }
2048 }
2049 }
2050 else if (tv_dest->v_type == VAR_LIST)
2051 {
2052 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002053 if (check_for_number(tv_idx) == FAIL)
2054 {
2055 status = FAIL;
2056 }
2057 else
2058 {
2059 list_T *l = tv_dest->vval.v_list;
2060 long n = (long)tv_idx->vval.v_number;
2061
2062 if (l != NULL && value_check_lock(
2063 l->lv_lock, NULL, FALSE))
2064 status = FAIL;
2065 else
2066 {
2067 listitem_T *li = list_find(l, n);
2068
2069 if (li == NULL)
2070 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002071 semsg(_(e_list_index_out_of_range_nr), n);
2072 status = FAIL;
2073 }
2074 else if (value_check_lock(li->li_tv.v_lock,
2075 NULL, FALSE))
2076 status = FAIL;
2077 else
2078 listitem_remove(l, li);
2079 }
2080 }
2081 }
2082 else
2083 {
2084 status = FAIL;
2085 semsg(_(e_cannot_index_str),
2086 vartype_name(tv_dest->v_type));
2087 }
2088
2089 clear_tv(tv_idx);
2090 clear_tv(tv_dest);
2091 ectx->ec_stack.ga_len -= 2;
2092
2093 return status;
2094}
2095
2096/*
2097 * Unlet a range of items in a list variable.
2098 */
2099 static int
2100execute_unletrange(isn_T *iptr, ectx_T *ectx)
2101{
2102 // Stack contains:
2103 // -3 index1
2104 // -2 index2
2105 // -1 dict or list
2106 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2107 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2108 typval_T *tv_dest = STACK_TV_BOT(-1);
2109 int status = OK;
2110
2111 if (tv_dest->v_type == VAR_LIST)
2112 {
2113 // indexes must be a number
2114 SOURCING_LNUM = iptr->isn_lnum;
2115 if (check_for_number(tv_idx1) == FAIL
2116 || (tv_idx2->v_type != VAR_SPECIAL
2117 && check_for_number(tv_idx2) == FAIL))
2118 {
2119 status = FAIL;
2120 }
2121 else
2122 {
2123 list_T *l = tv_dest->vval.v_list;
2124 long n1 = (long)tv_idx1->vval.v_number;
2125 long n2 = tv_idx2->v_type == VAR_SPECIAL
2126 ? 0 : (long)tv_idx2->vval.v_number;
2127 listitem_T *li;
2128
2129 li = list_find_index(l, &n1);
2130 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002131 {
2132 semsg(_(e_list_index_out_of_range_nr),
2133 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002134 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002135 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002136 else
2137 {
2138 if (n1 < 0)
2139 n1 = list_idx_of_item(l, li);
2140 if (n2 < 0)
2141 {
2142 listitem_T *li2 = list_find(l, n2);
2143
2144 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002145 {
2146 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002147 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002148 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002149 else
2150 n2 = list_idx_of_item(l, li2);
2151 }
2152 if (status != FAIL
2153 && tv_idx2->v_type != VAR_SPECIAL
2154 && n2 < n1)
2155 {
2156 semsg(_(e_list_index_out_of_range_nr), n2);
2157 status = FAIL;
2158 }
2159 if (status != FAIL
2160 && list_unlet_range(l, li, NULL, n1,
2161 tv_idx2->v_type != VAR_SPECIAL, n2)
2162 == FAIL)
2163 status = FAIL;
2164 }
2165 }
2166 }
2167 else
2168 {
2169 status = FAIL;
2170 SOURCING_LNUM = iptr->isn_lnum;
2171 semsg(_(e_cannot_index_str),
2172 vartype_name(tv_dest->v_type));
2173 }
2174
2175 clear_tv(tv_idx1);
2176 clear_tv(tv_idx2);
2177 clear_tv(tv_dest);
2178 ectx->ec_stack.ga_len -= 3;
2179
2180 return status;
2181}
2182
2183/*
2184 * Top of a for loop.
2185 */
2186 static int
2187execute_for(isn_T *iptr, ectx_T *ectx)
2188{
2189 typval_T *tv;
2190 typval_T *ltv = STACK_TV_BOT(-1);
2191 typval_T *idxtv =
2192 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2193
2194 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2195 return FAIL;
2196 if (ltv->v_type == VAR_LIST)
2197 {
2198 list_T *list = ltv->vval.v_list;
2199
2200 // push the next item from the list
2201 ++idxtv->vval.v_number;
2202 if (list == NULL
2203 || idxtv->vval.v_number >= list->lv_len)
2204 {
2205 // past the end of the list, jump to "endfor"
2206 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2207 may_restore_cmdmod(&ectx->ec_funclocal);
2208 }
2209 else if (list->lv_first == &range_list_item)
2210 {
2211 // non-materialized range() list
2212 tv = STACK_TV_BOT(0);
2213 tv->v_type = VAR_NUMBER;
2214 tv->v_lock = 0;
2215 tv->vval.v_number = list_find_nr(
2216 list, idxtv->vval.v_number, NULL);
2217 ++ectx->ec_stack.ga_len;
2218 }
2219 else
2220 {
2221 listitem_T *li = list_find(list,
2222 idxtv->vval.v_number);
2223
2224 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2225 ++ectx->ec_stack.ga_len;
2226 }
2227 }
2228 else if (ltv->v_type == VAR_STRING)
2229 {
2230 char_u *str = ltv->vval.v_string;
2231
2232 // The index is for the last byte of the previous
2233 // character.
2234 ++idxtv->vval.v_number;
2235 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2236 {
2237 // past the end of the string, jump to "endfor"
2238 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2239 may_restore_cmdmod(&ectx->ec_funclocal);
2240 }
2241 else
2242 {
2243 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2244
2245 // Push the next character from the string.
2246 tv = STACK_TV_BOT(0);
2247 tv->v_type = VAR_STRING;
2248 tv->vval.v_string = vim_strnsave(
2249 str + idxtv->vval.v_number, clen);
2250 ++ectx->ec_stack.ga_len;
2251 idxtv->vval.v_number += clen - 1;
2252 }
2253 }
2254 else if (ltv->v_type == VAR_BLOB)
2255 {
2256 blob_T *blob = ltv->vval.v_blob;
2257
2258 // When we get here the first time make a copy of the
2259 // blob, so that the iteration still works when it is
2260 // changed.
2261 if (idxtv->vval.v_number == -1 && blob != NULL)
2262 {
2263 blob_copy(blob, ltv);
2264 blob_unref(blob);
2265 blob = ltv->vval.v_blob;
2266 }
2267
2268 // The index is for the previous byte.
2269 ++idxtv->vval.v_number;
2270 if (blob == NULL
2271 || idxtv->vval.v_number >= blob_len(blob))
2272 {
2273 // past the end of the blob, jump to "endfor"
2274 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2275 may_restore_cmdmod(&ectx->ec_funclocal);
2276 }
2277 else
2278 {
2279 // Push the next byte from the blob.
2280 tv = STACK_TV_BOT(0);
2281 tv->v_type = VAR_NUMBER;
2282 tv->vval.v_number = blob_get(blob,
2283 idxtv->vval.v_number);
2284 ++ectx->ec_stack.ga_len;
2285 }
2286 }
2287 else
2288 {
2289 semsg(_(e_for_loop_on_str_not_supported),
2290 vartype_name(ltv->v_type));
2291 return FAIL;
2292 }
2293 return OK;
2294}
2295
2296/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002297 * Load instruction for w:/b:/g:/t: variable.
2298 * "isn_type" is used instead of "iptr->isn_type".
2299 */
2300 static int
2301load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2302{
2303 dictitem_T *di = NULL;
2304 hashtab_T *ht = NULL;
2305 char namespace;
2306
2307 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2308 return NOTDONE;
2309 switch (isn_type)
2310 {
2311 case ISN_LOADG:
2312 ht = get_globvar_ht();
2313 namespace = 'g';
2314 break;
2315 case ISN_LOADB:
2316 ht = &curbuf->b_vars->dv_hashtab;
2317 namespace = 'b';
2318 break;
2319 case ISN_LOADW:
2320 ht = &curwin->w_vars->dv_hashtab;
2321 namespace = 'w';
2322 break;
2323 case ISN_LOADT:
2324 ht = &curtab->tp_vars->dv_hashtab;
2325 namespace = 't';
2326 break;
2327 default: // Cannot reach here
2328 return NOTDONE;
2329 }
2330 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2331
Bram Moolenaar06b77222022-01-25 15:51:56 +00002332 if (di == NULL)
2333 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002334 if (isn_type == ISN_LOADG)
2335 {
2336 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2337
2338 // g:Something could be a function
2339 if (ufunc != NULL)
2340 {
2341 typval_T *tv = STACK_TV_BOT(0);
2342
2343 ++ectx->ec_stack.ga_len;
2344 tv->v_type = VAR_FUNC;
2345 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2346 if (tv->vval.v_string == NULL)
2347 return FAIL;
2348 STRCPY(tv->vval.v_string, "g:");
2349 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2350 return OK;
2351 }
2352 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002353 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002354 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002355 // no check if the item exists in the script but
2356 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002357 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002358 else
2359 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002360 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002361 return FAIL;
2362 }
2363 else
2364 {
2365 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2366 ++ectx->ec_stack.ga_len;
2367 }
2368 return OK;
2369}
2370
2371/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002372 * Execute instructions in execution context "ectx".
2373 * Return OK or FAIL;
2374 */
2375 static int
2376exec_instructions(ectx_T *ectx)
2377{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002378 int ret = FAIL;
2379 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002380 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002381
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002382 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002383 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002384
Bram Moolenaarff652882021-05-16 15:24:49 +02002385 // Only catch exceptions in this instruction list.
2386 ectx->ec_trylevel_at_start = trylevel;
2387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 for (;;)
2389 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002390 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002392 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002393
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002394 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002395 {
2396 line_breakcheck();
2397 breakcheck_count = 0;
2398 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002399 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002400 {
2401 // Turn CTRL-C into an exception.
2402 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002403 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002404 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002405 did_throw = TRUE;
2406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002408 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002409 {
2410 // Turn an error message into an exception.
2411 did_emsg = FALSE;
2412 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002413 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002414 did_throw = TRUE;
2415 *msg_list = NULL;
2416 }
2417
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002418 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002420 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002421 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002422 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423
2424 // An exception jumps to the first catch, finally, or returns from
2425 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002426 while (index > 0)
2427 {
2428 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002429 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002430 break;
2431 // In the catch and finally block of this try we have to go up
2432 // one level.
2433 --index;
2434 trycmd = NULL;
2435 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002436 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002438 if (trycmd->tcd_in_catch)
2439 {
2440 // exception inside ":catch", jump to ":finally" once
2441 ectx->ec_iidx = trycmd->tcd_finally_idx;
2442 trycmd->tcd_finally_idx = 0;
2443 }
2444 else
2445 // jump to first ":catch"
2446 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002447 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002448 did_throw = FALSE; // don't come back here until :endtry
2449 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 }
2451 else
2452 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002453 // Not inside try or need to return from current functions.
2454 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002455 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002456 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002457 tv = STACK_TV_BOT(0);
2458 tv->v_type = VAR_NUMBER;
2459 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002460 ++ectx->ec_stack.ga_len;
2461 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002463 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002464 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002465 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002466 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 goto done;
2468 }
2469
Bram Moolenaar4c137212021-04-19 16:48:48 +02002470 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002471 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 }
2473 continue;
2474 }
2475
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002476 /*
2477 * Big switch on the instruction. Most compilers will be turning this
2478 * into an efficient lookup table, since the "case" values are an enum
2479 * with sequential numbers. It may look ugly, but it should be the
2480 * most efficient way.
2481 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002482 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 switch (iptr->isn_type)
2484 {
2485 // execute Ex command line
2486 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002487 if (exec_command(iptr) == FAIL)
2488 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 break;
2490
Bram Moolenaar20677332021-06-06 17:02:53 +02002491 // execute Ex command line split at NL characters.
2492 case ISN_EXEC_SPLIT:
2493 {
2494 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002495 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002496
2497 SOURCING_LNUM = iptr->isn_lnum;
2498 CLEAR_FIELD(cookie);
2499 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2500 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002501 line = get_split_sourceline(0, &cookie, 0, 0);
2502 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002503 get_split_sourceline, &cookie,
2504 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2505 == FAIL
2506 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002507 {
2508 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002509 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002510 }
2511 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002512 }
2513 break;
2514
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002515 // execute Ex command line that is only a range
2516 case ISN_EXECRANGE:
2517 {
2518 exarg_T ea;
2519 char *error = NULL;
2520
2521 CLEAR_FIELD(ea);
2522 ea.cmdidx = CMD_SIZE;
2523 ea.addr_type = ADDR_LINES;
2524 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002525 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002526 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002527 if (ea.cmd == NULL)
2528 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002529 // error is always NULL when using ADDR_LINES
2530 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002531 if (error != NULL)
2532 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002533 emsg(error);
2534 goto on_error;
2535 }
2536 }
2537 break;
2538
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002539 // Evaluate an expression with legacy syntax, push it onto the
2540 // stack.
2541 case ISN_LEGACY_EVAL:
2542 {
2543 char_u *arg = iptr->isn_arg.string;
2544 int res;
2545 int save_flags = cmdmod.cmod_flags;
2546
Bram Moolenaar35578162021-08-02 19:10:38 +02002547 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002548 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002549 tv = STACK_TV_BOT(0);
2550 init_tv(tv);
2551 cmdmod.cmod_flags |= CMOD_LEGACY;
2552 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2553 cmdmod.cmod_flags = save_flags;
2554 if (res == FAIL)
2555 goto on_error;
2556 ++ectx->ec_stack.ga_len;
2557 }
2558 break;
2559
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002560 // push typeval VAR_INSTR with instructions to be executed
2561 case ISN_INSTR:
2562 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002563 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002564 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002565 tv = STACK_TV_BOT(0);
2566 tv->vval.v_instr = ALLOC_ONE(instr_T);
2567 if (tv->vval.v_instr == NULL)
2568 goto on_error;
2569 ++ectx->ec_stack.ga_len;
2570
2571 tv->v_type = VAR_INSTR;
2572 tv->vval.v_instr->instr_ectx = ectx;
2573 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2574 }
2575 break;
2576
Bram Moolenaar4c137212021-04-19 16:48:48 +02002577 // execute :substitute with an expression
2578 case ISN_SUBSTITUTE:
2579 {
2580 subs_T *subs = &iptr->isn_arg.subs;
2581 source_cookie_T cookie;
2582 struct subs_expr_S *save_instr = substitute_instr;
2583 struct subs_expr_S subs_instr;
2584 int res;
2585
2586 subs_instr.subs_ectx = ectx;
2587 subs_instr.subs_instr = subs->subs_instr;
2588 subs_instr.subs_status = OK;
2589 substitute_instr = &subs_instr;
2590
2591 SOURCING_LNUM = iptr->isn_lnum;
2592 // This is very much like ISN_EXEC
2593 CLEAR_FIELD(cookie);
2594 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2595 res = do_cmdline(subs->subs_cmd,
2596 getsourceline, &cookie,
2597 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2598 substitute_instr = save_instr;
2599
2600 if (res == FAIL || did_emsg
2601 || subs_instr.subs_status == FAIL)
2602 goto on_error;
2603 }
2604 break;
2605
2606 case ISN_FINISH:
2607 goto done;
2608
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002609 case ISN_REDIRSTART:
2610 // create a dummy entry for var_redir_str()
2611 if (alloc_redir_lval() == FAIL)
2612 goto on_error;
2613
2614 // The output is stored in growarray "redir_ga" until
2615 // redirection ends.
2616 init_redir_ga();
2617 redir_vname = 1;
2618 break;
2619
2620 case ISN_REDIREND:
2621 {
2622 char_u *res = get_clear_redir_ga();
2623
2624 // End redirection, put redirected text on the stack.
2625 clear_redir_lval();
2626 redir_vname = 0;
2627
Bram Moolenaar35578162021-08-02 19:10:38 +02002628 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002629 {
2630 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002631 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002632 }
2633 tv = STACK_TV_BOT(0);
2634 tv->v_type = VAR_STRING;
2635 tv->vval.v_string = res;
2636 ++ectx->ec_stack.ga_len;
2637 }
2638 break;
2639
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002640 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002641#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002642 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2643 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002644#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002645 break;
2646
2647 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002648#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002649 {
2650 exarg_T ea;
2651 int res;
2652
2653 CLEAR_FIELD(ea);
2654 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2655 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2656 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2657 --ectx->ec_stack.ga_len;
2658 tv = STACK_TV_BOT(0);
2659 res = cexpr_core(&ea, tv);
2660 clear_tv(tv);
2661 if (res == FAIL)
2662 goto on_error;
2663 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002664#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002665 break;
2666
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002667 // execute Ex command from pieces on the stack
2668 case ISN_EXECCONCAT:
2669 {
2670 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002671 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002672 int pass;
2673 int i;
2674 char_u *cmd = NULL;
2675 char_u *str;
2676
2677 for (pass = 1; pass <= 2; ++pass)
2678 {
2679 for (i = 0; i < count; ++i)
2680 {
2681 tv = STACK_TV_BOT(i - count);
2682 str = tv->vval.v_string;
2683 if (str != NULL && *str != NUL)
2684 {
2685 if (pass == 2)
2686 STRCPY(cmd + len, str);
2687 len += STRLEN(str);
2688 }
2689 if (pass == 2)
2690 clear_tv(tv);
2691 }
2692 if (pass == 1)
2693 {
2694 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002695 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002696 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002697 len = 0;
2698 }
2699 }
2700
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002701 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002702 do_cmdline_cmd(cmd);
2703 vim_free(cmd);
2704 }
2705 break;
2706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 // execute :echo {string} ...
2708 case ISN_ECHO:
2709 {
2710 int count = iptr->isn_arg.echo.echo_count;
2711 int atstart = TRUE;
2712 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002713 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714
2715 for (idx = 0; idx < count; ++idx)
2716 {
2717 tv = STACK_TV_BOT(idx - count);
2718 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2719 &atstart, &needclr);
2720 clear_tv(tv);
2721 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002722 if (needclr)
2723 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002724 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 }
2726 break;
2727
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002728 // :execute {string} ...
2729 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002730 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002731 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002732 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002733 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002734 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002735 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002736 {
2737 int count = iptr->isn_arg.number;
2738 garray_T ga;
2739 char_u buf[NUMBUFLEN];
2740 char_u *p;
2741 int len;
2742 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002743 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002744
2745 ga_init2(&ga, 1, 80);
2746 for (idx = 0; idx < count; ++idx)
2747 {
2748 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002749 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002750 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002751 if (tv->v_type == VAR_CHANNEL
2752 || tv->v_type == VAR_JOB)
2753 {
2754 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002755 semsg(_(e_using_invalid_value_as_string_str),
2756 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002757 break;
2758 }
2759 else
2760 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002761 }
2762 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002763 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002764
2765 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002766 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002767 failed = TRUE;
2768 else
2769 {
2770 if (ga.ga_len > 0)
2771 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2772 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2773 ga.ga_len += len;
2774 }
2775 clear_tv(tv);
2776 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002777 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002778 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002779 {
2780 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002781 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002782 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002783
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002784 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002785 {
2786 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002787 {
2788 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002789 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002790 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002791 {
2792 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002793 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002794 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002795 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002796 else
2797 {
2798 msg_sb_eol();
2799 if (iptr->isn_type == ISN_ECHOMSG)
2800 {
2801 msg_attr(ga.ga_data, echo_attr);
2802 out_flush();
2803 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002804 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2805 {
2806 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2807 TRUE);
2808 ui_write((char_u *)"\r\n", 2, TRUE);
2809 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002810 else
2811 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002812 SOURCING_LNUM = iptr->isn_lnum;
2813 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002814 }
2815 }
2816 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002817 ga_clear(&ga);
2818 }
2819 break;
2820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 // load local variable or argument
2822 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002823 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002824 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002826 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 break;
2828
2829 // load v: variable
2830 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002831 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002832 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002834 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 break;
2836
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002837 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 case ISN_LOADSCRIPT:
2839 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002840 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 svar_T *sv;
2842
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002843 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002844 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002845 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002846 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002847 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002848 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002850 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 }
2852 break;
2853
2854 // load s: variable in old script
2855 case ISN_LOADS:
2856 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002857 hashtab_T *ht = &SCRIPT_VARS(
2858 iptr->isn_arg.loadstore.ls_sid);
2859 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 if (di == NULL)
2863 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002864 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002865 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002866 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 }
2868 else
2869 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002870 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002871 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002873 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 }
2875 }
2876 break;
2877
Bram Moolenaard3aac292020-04-19 14:32:17 +02002878 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002880 case ISN_LOADB:
2881 case ISN_LOADW:
2882 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002884 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002885
Bram Moolenaar06b77222022-01-25 15:51:56 +00002886 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002887 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002888 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002889 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 break;
2893
Bram Moolenaar03290b82020-12-19 16:30:44 +01002894 // load autoload variable
2895 case ISN_LOADAUTO:
2896 {
2897 char_u *name = iptr->isn_arg.string;
2898
Bram Moolenaar35578162021-08-02 19:10:38 +02002899 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002900 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002901 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002902 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002903 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002904 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002905 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002906 }
2907 break;
2908
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002909 // load g:/b:/w:/t: namespace
2910 case ISN_LOADGDICT:
2911 case ISN_LOADBDICT:
2912 case ISN_LOADWDICT:
2913 case ISN_LOADTDICT:
2914 {
2915 dict_T *d = NULL;
2916
2917 switch (iptr->isn_type)
2918 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002919 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2920 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2921 case ISN_LOADWDICT: d = curwin->w_vars; break;
2922 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002923 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002924 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002925 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002926 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002927 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002928 tv = STACK_TV_BOT(0);
2929 tv->v_type = VAR_DICT;
2930 tv->v_lock = 0;
2931 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002932 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002933 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002934 }
2935 break;
2936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 // load &option
2938 case ISN_LOADOPT:
2939 {
2940 typval_T optval;
2941 char_u *name = iptr->isn_arg.string;
2942
Bram Moolenaara8c17702020-04-01 21:17:24 +02002943 // This is not expected to fail, name is checked during
2944 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002945 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002946 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002947 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002948 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002950 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 }
2952 break;
2953
2954 // load $ENV
2955 case ISN_LOADENV:
2956 {
2957 typval_T optval;
2958 char_u *name = iptr->isn_arg.string;
2959
Bram Moolenaar35578162021-08-02 19:10:38 +02002960 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002961 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002962 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002963 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 }
2967 break;
2968
2969 // load @register
2970 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002971 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002972 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 tv = STACK_TV_BOT(0);
2974 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002975 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002976 // This may result in NULL, which should be equivalent to an
2977 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 tv->vval.v_string = get_reg_contents(
2979 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002980 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 break;
2982
2983 // store local variable
2984 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002985 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 tv = STACK_TV_VAR(iptr->isn_arg.number);
2987 clear_tv(tv);
2988 *tv = *STACK_TV_BOT(0);
2989 break;
2990
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002991 // store s: variable in old script
2992 case ISN_STORES:
2993 {
2994 hashtab_T *ht = &SCRIPT_VARS(
2995 iptr->isn_arg.loadstore.ls_sid);
2996 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002997 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002998
Bram Moolenaar4c137212021-04-19 16:48:48 +02002999 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003000 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003001 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003002 else
3003 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003004 SOURCING_LNUM = iptr->isn_lnum;
3005 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003006 {
3007 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003008 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003009 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003010 clear_tv(&di->di_tv);
3011 di->di_tv = *STACK_TV_BOT(0);
3012 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003013 }
3014 break;
3015
3016 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 case ISN_STORESCRIPT:
3018 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003019 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3020 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003022 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003023 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003024 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003025 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003026
3027 // "const" and "final" are checked at compile time, locking
3028 // the value needs to be checked here.
3029 SOURCING_LNUM = iptr->isn_lnum;
3030 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003031 {
3032 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003033 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003034 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 clear_tv(sv->sv_tv);
3037 *sv->sv_tv = *STACK_TV_BOT(0);
3038 }
3039 break;
3040
3041 // store option
3042 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003043 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003045 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3046 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 long n = 0;
3048 char_u *s = NULL;
3049 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003050 char_u numbuf[NUMBUFLEN];
3051 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052
Bram Moolenaar4c137212021-04-19 16:48:48 +02003053 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 tv = STACK_TV_BOT(0);
3055 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003056 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003058 if (s == NULL)
3059 s = (char_u *)"";
3060 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003061 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3062 {
3063 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003064 // If the option can be set to a function reference or
3065 // a lambda and the passed value is a function
3066 // reference, then convert it to the name (string) of
3067 // the function reference.
3068 s = tv2string(tv, &tofree, numbuf, 0);
3069 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003070 {
3071 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003072 goto on_error;
3073 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003076 // must be VAR_NUMBER, CHECKTYPE makes sure
3077 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003078 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003079 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003080 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 if (msg != NULL)
3082 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003083 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003085 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 }
3088 break;
3089
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003090 // store $ENV
3091 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003092 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003093 tv = STACK_TV_BOT(0);
3094 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3095 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003096 break;
3097
3098 // store @r
3099 case ISN_STOREREG:
3100 {
3101 int reg = iptr->isn_arg.number;
3102
Bram Moolenaar4c137212021-04-19 16:48:48 +02003103 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003104 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003105 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003106 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003107 }
3108 break;
3109
3110 // store v: variable
3111 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003112 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003113 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3114 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003115 // should not happen, type is checked when compiling
3116 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003117 break;
3118
Bram Moolenaard3aac292020-04-19 14:32:17 +02003119 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003121 case ISN_STOREB:
3122 case ISN_STOREW:
3123 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003125 dictitem_T *di;
3126 hashtab_T *ht;
3127 char_u *name = iptr->isn_arg.string + 2;
3128
Bram Moolenaard3aac292020-04-19 14:32:17 +02003129 switch (iptr->isn_type)
3130 {
3131 case ISN_STOREG:
3132 ht = get_globvar_ht();
3133 break;
3134 case ISN_STOREB:
3135 ht = &curbuf->b_vars->dv_hashtab;
3136 break;
3137 case ISN_STOREW:
3138 ht = &curwin->w_vars->dv_hashtab;
3139 break;
3140 case ISN_STORET:
3141 ht = &curtab->tp_vars->dv_hashtab;
3142 break;
3143 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003144 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146
Bram Moolenaar4c137212021-04-19 16:48:48 +02003147 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003148 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003150 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 else
3152 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003153 SOURCING_LNUM = iptr->isn_lnum;
3154 if (var_check_permission(di, name) == FAIL)
3155 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 clear_tv(&di->di_tv);
3157 di->di_tv = *STACK_TV_BOT(0);
3158 }
3159 }
3160 break;
3161
Bram Moolenaar03290b82020-12-19 16:30:44 +01003162 // store an autoload variable
3163 case ISN_STOREAUTO:
3164 SOURCING_LNUM = iptr->isn_lnum;
3165 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3166 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003167 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003168 break;
3169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 // store number in local variable
3171 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003172 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 clear_tv(tv);
3174 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003175 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 break;
3177
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003178 // store value in list or dict variable
3179 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003180 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003181 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003182
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003183 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003184 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003185 if (res == NOTDONE)
3186 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003187 }
3188 break;
3189
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003190 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003191 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003192 if (execute_storerange(iptr, ectx) == FAIL)
3193 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003194 break;
3195
Bram Moolenaar0186e582021-01-10 18:33:11 +01003196 // load or store variable or argument from outer scope
3197 case ISN_LOADOUTER:
3198 case ISN_STOREOUTER:
3199 {
3200 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003201 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3202 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003203
3204 while (depth > 1 && outer != NULL)
3205 {
3206 outer = outer->out_up;
3207 --depth;
3208 }
3209 if (outer == NULL)
3210 {
3211 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003212 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3213 || ectx->ec_outer_ref == NULL)
3214 // Possibly :def function called from legacy
3215 // context.
3216 emsg(_(e_closure_called_from_invalid_context));
3217 else
3218 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003219 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003220 }
3221 tv = ((typval_T *)outer->out_stack->ga_data)
3222 + outer->out_frame_idx + STACK_FRAME_SIZE
3223 + iptr->isn_arg.outer.outer_idx;
3224 if (iptr->isn_type == ISN_LOADOUTER)
3225 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003226 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003227 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003228 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003229 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003230 }
3231 else
3232 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003233 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003234 clear_tv(tv);
3235 *tv = *STACK_TV_BOT(0);
3236 }
3237 }
3238 break;
3239
Bram Moolenaar752fc692021-01-04 21:57:11 +01003240 // unlet item in list or dict variable
3241 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003242 if (execute_unletindex(iptr, ectx) == FAIL)
3243 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003244 break;
3245
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003246 // unlet range of items in list variable
3247 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003248 if (execute_unletrange(iptr, ectx) == FAIL)
3249 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003250 break;
3251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 // push constant
3253 case ISN_PUSHNR:
3254 case ISN_PUSHBOOL:
3255 case ISN_PUSHSPEC:
3256 case ISN_PUSHF:
3257 case ISN_PUSHS:
3258 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003259 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003260 case ISN_PUSHCHANNEL:
3261 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003262 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003263 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003265 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003266 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 switch (iptr->isn_type)
3268 {
3269 case ISN_PUSHNR:
3270 tv->v_type = VAR_NUMBER;
3271 tv->vval.v_number = iptr->isn_arg.number;
3272 break;
3273 case ISN_PUSHBOOL:
3274 tv->v_type = VAR_BOOL;
3275 tv->vval.v_number = iptr->isn_arg.number;
3276 break;
3277 case ISN_PUSHSPEC:
3278 tv->v_type = VAR_SPECIAL;
3279 tv->vval.v_number = iptr->isn_arg.number;
3280 break;
3281#ifdef FEAT_FLOAT
3282 case ISN_PUSHF:
3283 tv->v_type = VAR_FLOAT;
3284 tv->vval.v_float = iptr->isn_arg.fnumber;
3285 break;
3286#endif
3287 case ISN_PUSHBLOB:
3288 blob_copy(iptr->isn_arg.blob, tv);
3289 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003290 case ISN_PUSHFUNC:
3291 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003292 if (iptr->isn_arg.string == NULL)
3293 tv->vval.v_string = NULL;
3294 else
3295 tv->vval.v_string =
3296 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003297 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003298 case ISN_PUSHCHANNEL:
3299#ifdef FEAT_JOB_CHANNEL
3300 tv->v_type = VAR_CHANNEL;
3301 tv->vval.v_channel = iptr->isn_arg.channel;
3302 if (tv->vval.v_channel != NULL)
3303 ++tv->vval.v_channel->ch_refcount;
3304#endif
3305 break;
3306 case ISN_PUSHJOB:
3307#ifdef FEAT_JOB_CHANNEL
3308 tv->v_type = VAR_JOB;
3309 tv->vval.v_job = iptr->isn_arg.job;
3310 if (tv->vval.v_job != NULL)
3311 ++tv->vval.v_job->jv_refcount;
3312#endif
3313 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 default:
3315 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003316 tv->vval.v_string = iptr->isn_arg.string == NULL
3317 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 }
3319 break;
3320
Bram Moolenaar06b77222022-01-25 15:51:56 +00003321 case ISN_AUTOLOAD:
3322 {
3323 char_u *name = iptr->isn_arg.string;
3324
3325 (void)script_autoload(name, FALSE);
3326 if (find_func(name, TRUE))
3327 {
3328 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3329 goto theend;
3330 tv = STACK_TV_BOT(0);
3331 tv->v_lock = 0;
3332 ++ectx->ec_stack.ga_len;
3333 tv->v_type = VAR_FUNC;
3334 tv->vval.v_string = vim_strsave(name);
3335 }
3336 else
3337 {
3338 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3339
3340 if (res == NOTDONE)
3341 goto theend;
3342 if (res == FAIL)
3343 goto on_error;
3344 }
3345 }
3346 break;
3347
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003348 case ISN_UNLET:
3349 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3350 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003351 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003352 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003353 case ISN_UNLETENV:
3354 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3355 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003356
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003357 case ISN_LOCKUNLOCK:
3358 {
3359 typval_T *lval_root_save = lval_root;
3360 int res;
3361
3362 // Stack has the local variable, argument the whole :lock
3363 // or :unlock command, like ISN_EXEC.
3364 --ectx->ec_stack.ga_len;
3365 lval_root = STACK_TV_BOT(0);
3366 res = exec_command(iptr);
3367 clear_tv(lval_root);
3368 lval_root = lval_root_save;
3369 if (res == FAIL)
3370 goto on_error;
3371 }
3372 break;
3373
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003374 case ISN_LOCKCONST:
3375 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3376 break;
3377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 // create a list from items on the stack; uses a single allocation
3379 // for the list header and the items
3380 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003381 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003382 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 break;
3384
3385 // create a dict from items on the stack
3386 case ISN_NEWDICT:
3387 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003388 int count = iptr->isn_arg.number;
3389 dict_T *dict = dict_alloc();
3390 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003391 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003392 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003394 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003395 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 for (idx = 0; idx < count; ++idx)
3397 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003398 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003400 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003401 key = tv->vval.v_string == NULL
3402 ? (char_u *)"" : tv->vval.v_string;
3403 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003404 if (item != NULL)
3405 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003406 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar74409f62022-01-01 15:58:22 +00003407 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003408 dict_unref(dict);
3409 goto on_error;
3410 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003411 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003413 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003414 {
3415 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003416 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3419 item->di_tv.v_lock = 0;
3420 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003421 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003422 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003423 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003424 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 }
3427
3428 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003429 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003430 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003431 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003433 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 tv = STACK_TV_BOT(-1);
3435 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003436 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 tv->vval.v_dict = dict;
3438 ++dict->dv_refcount;
3439 }
3440 break;
3441
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003442 // create a partial with NULL value
3443 case ISN_NEWPARTIAL:
3444 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3445 goto theend;
3446 ++ectx->ec_stack.ga_len;
3447 tv = STACK_TV_BOT(-1);
3448 tv->v_type = VAR_PARTIAL;
3449 tv->v_lock = 0;
3450 tv->vval.v_partial = NULL;
3451 break;
3452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 // call a :def function
3454 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003455 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003456 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3457 NULL,
3458 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003459 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003460 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 break;
3462
3463 // call a builtin function
3464 case ISN_BCALL:
3465 SOURCING_LNUM = iptr->isn_lnum;
3466 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3467 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003468 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003469 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 break;
3471
3472 // call a funcref or partial
3473 case ISN_PCALL:
3474 {
3475 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3476 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003477 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478
3479 SOURCING_LNUM = iptr->isn_lnum;
3480 if (pfunc->cpf_top)
3481 {
3482 // funcref is above the arguments
3483 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3484 }
3485 else
3486 {
3487 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003488 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003489 partial_tv = *STACK_TV_BOT(0);
3490 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003492 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003493 if (tv == &partial_tv)
3494 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003496 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 }
3498 break;
3499
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003500 case ISN_PCALL_END:
3501 // PCALL finished, arguments have been consumed and replaced by
3502 // the return value. Now clear the funcref from the stack,
3503 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003504 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003505 clear_tv(STACK_TV_BOT(-1));
3506 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3507 break;
3508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 // call a user defined function or funcref/partial
3510 case ISN_UCALL:
3511 {
3512 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3513
3514 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003515 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003516 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003517 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 }
3519 break;
3520
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003521 // return from a :def function call without a value
3522 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003523 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003524 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003525 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003526 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003527 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003528 tv->vval.v_number = 0;
3529 tv->v_lock = 0;
3530 // FALLTHROUGH
3531
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003532 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 case ISN_RETURN:
3534 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003535 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003536 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003537
3538 if (trystack->ga_len > 0)
3539 trycmd = ((trycmd_T *)trystack->ga_data)
3540 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003541 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003542 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003544 // jump to ":finally" or ":endtry"
3545 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003546 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003547 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003548 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 trycmd->tcd_return = TRUE;
3550 }
3551 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003552 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 }
3554 break;
3555
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003556 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 case ISN_FUNCREF:
3558 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003559 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003560 ufunc_T *ufunc;
3561 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003564 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003565 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003566 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003567 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003568 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003569 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003570 if (funcref->fr_func_name == NULL)
3571 {
3572 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3573 + funcref->fr_dfunc_idx;
3574
3575 ufunc = pt_dfunc->df_ufunc;
3576 }
3577 else
3578 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003579 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003580 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003581 if (ufunc == NULL)
3582 {
3583 SOURCING_LNUM = iptr->isn_lnum;
3584 iemsg("ufunc unexpectedly NULL for FUNCREF");
3585 goto theend;
3586 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003587 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003588 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003590 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 tv->vval.v_partial = pt;
3592 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003593 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 }
3595 break;
3596
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003597 // Create a global function from a lambda.
3598 case ISN_NEWFUNC:
3599 {
3600 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3601
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003602 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003603 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003604 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003605 }
3606 break;
3607
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003608 // List functions
3609 case ISN_DEF:
3610 if (iptr->isn_arg.string == NULL)
3611 list_functions(NULL);
3612 else
3613 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003614 exarg_T ea;
3615 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003616
3617 CLEAR_FIELD(ea);
3618 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003619 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3620 define_function(&ea, NULL, &lines_to_free);
3621 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003622 }
3623 break;
3624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 // jump if a condition is met
3626 case ISN_JUMP:
3627 {
3628 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003629 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 int jump = TRUE;
3631
3632 if (when != JUMP_ALWAYS)
3633 {
3634 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003635 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003636 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003637 || when == JUMP_IF_COND_TRUE)
3638 {
3639 SOURCING_LNUM = iptr->isn_lnum;
3640 jump = tv_get_bool_chk(tv, &error);
3641 if (error)
3642 goto on_error;
3643 }
3644 else
3645 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003647 || when == JUMP_AND_KEEP_IF_FALSE
3648 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003650 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 {
3652 // drop the value from the stack
3653 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003654 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 }
3656 }
3657 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003658 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 }
3660 break;
3661
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003662 // Jump if an argument with a default value was already set and not
3663 // v:none.
3664 case ISN_JUMP_IF_ARG_SET:
3665 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3666 if (tv->v_type != VAR_UNKNOWN
3667 && !(tv->v_type == VAR_SPECIAL
3668 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003669 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003670 break;
3671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 // top of a for loop
3673 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003674 if (execute_for(iptr, ectx) == FAIL)
3675 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 break;
3677
3678 // start of ":try" block
3679 case ISN_TRY:
3680 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003681 trycmd_T *trycmd = NULL;
3682
Bram Moolenaar35578162021-08-02 19:10:38 +02003683 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003684 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003685 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3686 + ectx->ec_trystack.ga_len;
3687 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003689 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3691 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003692 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003693 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003694 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003695 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003696 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003697 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 }
3699 break;
3700
3701 case ISN_PUSHEXC:
3702 if (current_exception == NULL)
3703 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003704 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003706 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003708 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003709 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003711 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003713 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 tv->vval.v_string = vim_strsave(
3715 (char_u *)current_exception->value);
3716 break;
3717
3718 case ISN_CATCH:
3719 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003720 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721
Bram Moolenaar4c137212021-04-19 16:48:48 +02003722 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 if (trystack->ga_len > 0)
3724 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003725 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 + trystack->ga_len - 1;
3727 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003728 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 }
3730 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003731 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 catch_exception(current_exception);
3733 }
3734 break;
3735
Bram Moolenaarc150c092021-02-13 15:02:46 +01003736 case ISN_TRYCONT:
3737 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003738 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003739 trycont_T *trycont = &iptr->isn_arg.trycont;
3740 int i;
3741 trycmd_T *trycmd;
3742 int iidx = trycont->tct_where;
3743
3744 if (trystack->ga_len < trycont->tct_levels)
3745 {
3746 siemsg("TRYCONT: expected %d levels, found %d",
3747 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003748 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003749 }
3750 // Make :endtry jump to any outer try block and the last
3751 // :endtry inside the loop to the loop start.
3752 for (i = trycont->tct_levels; i > 0; --i)
3753 {
3754 trycmd = ((trycmd_T *)trystack->ga_data)
3755 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003756 // Add one to tcd_cont to be able to jump to
3757 // instruction with index zero.
3758 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003759 iidx = trycmd->tcd_finally_idx == 0
3760 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003761 }
3762 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003763 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003764 }
3765 break;
3766
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003767 case ISN_FINALLY:
3768 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003769 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003770 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3771 + trystack->ga_len - 1;
3772
3773 // Reset the index to avoid a return statement jumps here
3774 // again.
3775 trycmd->tcd_finally_idx = 0;
3776 break;
3777 }
3778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 // end of ":try" block
3780 case ISN_ENDTRY:
3781 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003782 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783
3784 if (trystack->ga_len > 0)
3785 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003786 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 --trystack->ga_len;
3789 --trylevel;
3790 trycmd = ((trycmd_T *)trystack->ga_data)
3791 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003792 if (trycmd->tcd_did_throw)
3793 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003794 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 {
3796 // discard the exception
3797 if (caught_stack == current_exception)
3798 caught_stack = caught_stack->caught;
3799 discard_current_exception();
3800 }
3801
3802 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003803 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003804
Bram Moolenaar4c137212021-04-19 16:48:48 +02003805 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003806 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003807 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003808 clear_tv(STACK_TV_BOT(0));
3809 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003810 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003811 // handling :continue: jump to outer try block or
3812 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003813 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 }
3815 }
3816 break;
3817
3818 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003819 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003820 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003821
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003822 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3823 {
3824 // throwing an exception while using "silent!" causes
3825 // the function to abort but not display an error.
3826 tv = STACK_TV_BOT(-1);
3827 clear_tv(tv);
3828 tv->v_type = VAR_NUMBER;
3829 tv->vval.v_number = 0;
3830 goto done;
3831 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003832 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003833 tv = STACK_TV_BOT(0);
3834 if (tv->vval.v_string == NULL
3835 || *skipwhite(tv->vval.v_string) == NUL)
3836 {
3837 vim_free(tv->vval.v_string);
3838 SOURCING_LNUM = iptr->isn_lnum;
3839 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003840 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003841 }
3842
3843 // Inside a "catch" we need to first discard the caught
3844 // exception.
3845 if (trystack->ga_len > 0)
3846 {
3847 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3848 + trystack->ga_len - 1;
3849 if (trycmd->tcd_caught && current_exception != NULL)
3850 {
3851 // discard the exception
3852 if (caught_stack == current_exception)
3853 caught_stack = caught_stack->caught;
3854 discard_current_exception();
3855 trycmd->tcd_caught = FALSE;
3856 }
3857 }
3858
Bram Moolenaar90a57162022-02-12 14:23:17 +00003859 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003860 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3861 == FAIL)
3862 {
3863 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003864 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003865 }
3866 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 break;
3869
3870 // compare with special values
3871 case ISN_COMPAREBOOL:
3872 case ISN_COMPARESPECIAL:
3873 {
3874 typval_T *tv1 = STACK_TV_BOT(-2);
3875 typval_T *tv2 = STACK_TV_BOT(-1);
3876 varnumber_T arg1 = tv1->vval.v_number;
3877 varnumber_T arg2 = tv2->vval.v_number;
3878 int res;
3879
3880 switch (iptr->isn_arg.op.op_type)
3881 {
3882 case EXPR_EQUAL: res = arg1 == arg2; break;
3883 case EXPR_NEQUAL: res = arg1 != arg2; break;
3884 default: res = 0; break;
3885 }
3886
Bram Moolenaar4c137212021-04-19 16:48:48 +02003887 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 tv1->v_type = VAR_BOOL;
3889 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3890 }
3891 break;
3892
Bram Moolenaar7a222242022-03-01 19:23:24 +00003893 case ISN_COMPARENULL:
3894 {
3895 typval_T *tv1 = STACK_TV_BOT(-2);
3896 typval_T *tv2 = STACK_TV_BOT(-1);
3897 int res;
3898
3899 res = typval_compare_null(tv1, tv2);
3900 if (res == MAYBE)
3901 goto on_error;
3902 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3903 res = !res;
3904 clear_tv(tv1);
3905 clear_tv(tv2);
3906 --ectx->ec_stack.ga_len;
3907 tv1->v_type = VAR_BOOL;
3908 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3909 }
3910 break;
3911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 // Operation with two number arguments
3913 case ISN_OPNR:
3914 case ISN_COMPARENR:
3915 {
3916 typval_T *tv1 = STACK_TV_BOT(-2);
3917 typval_T *tv2 = STACK_TV_BOT(-1);
3918 varnumber_T arg1 = tv1->vval.v_number;
3919 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003920 varnumber_T res = 0;
3921 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922
3923 switch (iptr->isn_arg.op.op_type)
3924 {
3925 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003926 case EXPR_DIV: if (arg2 == 0)
3927 div_zero = TRUE;
3928 else
3929 res = arg1 / arg2;
3930 break;
3931 case EXPR_REM: if (arg2 == 0)
3932 div_zero = TRUE;
3933 else
3934 res = arg1 % arg2;
3935 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 case EXPR_SUB: res = arg1 - arg2; break;
3937 case EXPR_ADD: res = arg1 + arg2; break;
3938
3939 case EXPR_EQUAL: res = arg1 == arg2; break;
3940 case EXPR_NEQUAL: res = arg1 != arg2; break;
3941 case EXPR_GREATER: res = arg1 > arg2; break;
3942 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3943 case EXPR_SMALLER: res = arg1 < arg2; break;
3944 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003945 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 }
3947
Bram Moolenaar4c137212021-04-19 16:48:48 +02003948 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 if (iptr->isn_type == ISN_COMPARENR)
3950 {
3951 tv1->v_type = VAR_BOOL;
3952 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3953 }
3954 else
3955 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003956 if (div_zero)
3957 {
3958 SOURCING_LNUM = iptr->isn_lnum;
3959 emsg(_(e_divide_by_zero));
3960 goto on_error;
3961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 }
3963 break;
3964
3965 // Computation with two float arguments
3966 case ISN_OPFLOAT:
3967 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003968#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 {
3970 typval_T *tv1 = STACK_TV_BOT(-2);
3971 typval_T *tv2 = STACK_TV_BOT(-1);
3972 float_T arg1 = tv1->vval.v_float;
3973 float_T arg2 = tv2->vval.v_float;
3974 float_T res = 0;
3975 int cmp = FALSE;
3976
3977 switch (iptr->isn_arg.op.op_type)
3978 {
3979 case EXPR_MULT: res = arg1 * arg2; break;
3980 case EXPR_DIV: res = arg1 / arg2; break;
3981 case EXPR_SUB: res = arg1 - arg2; break;
3982 case EXPR_ADD: res = arg1 + arg2; break;
3983
3984 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3985 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3986 case EXPR_GREATER: cmp = arg1 > arg2; break;
3987 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3988 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3989 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3990 default: cmp = 0; break;
3991 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003992 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 if (iptr->isn_type == ISN_COMPAREFLOAT)
3994 {
3995 tv1->v_type = VAR_BOOL;
3996 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3997 }
3998 else
3999 tv1->vval.v_float = res;
4000 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004001#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 break;
4003
4004 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004005 case ISN_COMPAREDICT:
4006 case ISN_COMPAREFUNC:
4007 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 case ISN_COMPAREBLOB:
4009 {
4010 typval_T *tv1 = STACK_TV_BOT(-2);
4011 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004012 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4013 int ic = iptr->isn_arg.op.op_ic;
4014 int res = FALSE;
4015 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016
Bram Moolenaar265f8112021-12-19 12:33:05 +00004017 SOURCING_LNUM = iptr->isn_lnum;
4018 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004020 status = typval_compare_list(tv1, tv2,
4021 exprtype, ic, &res);
4022 }
4023 else if (iptr->isn_type == ISN_COMPAREDICT)
4024 {
4025 status = typval_compare_dict(tv1, tv2,
4026 exprtype, ic, &res);
4027 }
4028 else if (iptr->isn_type == ISN_COMPAREFUNC)
4029 {
4030 status = typval_compare_func(tv1, tv2,
4031 exprtype, ic, &res);
4032 }
4033 else if (iptr->isn_type == ISN_COMPARESTRING)
4034 {
4035 status = typval_compare_string(tv1, tv2,
4036 exprtype, ic, &res);
4037 }
4038 else
4039 {
4040 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004042 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 clear_tv(tv1);
4044 clear_tv(tv2);
4045 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004046 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4047 if (status == FAIL)
4048 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 }
4050 break;
4051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 case ISN_COMPAREANY:
4053 {
4054 typval_T *tv1 = STACK_TV_BOT(-2);
4055 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004056 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004058 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059
Bram Moolenaareb26f432020-09-14 16:50:05 +02004060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004061 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004063 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004064 if (status == FAIL)
4065 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 }
4067 break;
4068
4069 case ISN_ADDLIST:
4070 case ISN_ADDBLOB:
4071 {
4072 typval_T *tv1 = STACK_TV_BOT(-2);
4073 typval_T *tv2 = STACK_TV_BOT(-1);
4074
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004075 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004077 {
4078 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4079 && tv1->vval.v_list != NULL)
4080 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4081 NULL);
4082 else
4083 eval_addlist(tv1, tv2);
4084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 else
4086 eval_addblob(tv1, tv2);
4087 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004088 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 }
4090 break;
4091
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004092 case ISN_LISTAPPEND:
4093 {
4094 typval_T *tv1 = STACK_TV_BOT(-2);
4095 typval_T *tv2 = STACK_TV_BOT(-1);
4096 list_T *l = tv1->vval.v_list;
4097
4098 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004099 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004100 if (l == NULL)
4101 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004102 emsg(_(e_cannot_add_to_null_list));
4103 goto on_error;
4104 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004105 if (value_check_lock(l->lv_lock, NULL, FALSE))
4106 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004107 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004108 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004109 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004110 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004111 }
4112 break;
4113
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004114 case ISN_BLOBAPPEND:
4115 {
4116 typval_T *tv1 = STACK_TV_BOT(-2);
4117 typval_T *tv2 = STACK_TV_BOT(-1);
4118 blob_T *b = tv1->vval.v_blob;
4119 int error = FALSE;
4120 varnumber_T n;
4121
4122 // add a number to a blob
4123 if (b == NULL)
4124 {
4125 SOURCING_LNUM = iptr->isn_lnum;
4126 emsg(_(e_cannot_add_to_null_blob));
4127 goto on_error;
4128 }
4129 n = tv_get_number_chk(tv2, &error);
4130 if (error)
4131 goto on_error;
4132 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004133 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004134 }
4135 break;
4136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 // Computation with two arguments of unknown type
4138 case ISN_OPANY:
4139 {
4140 typval_T *tv1 = STACK_TV_BOT(-2);
4141 typval_T *tv2 = STACK_TV_BOT(-1);
4142 varnumber_T n1, n2;
4143#ifdef FEAT_FLOAT
4144 float_T f1 = 0, f2 = 0;
4145#endif
4146 int error = FALSE;
4147
4148 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4149 {
4150 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4151 {
4152 eval_addlist(tv1, tv2);
4153 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004154 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 break;
4156 }
4157 else if (tv1->v_type == VAR_BLOB
4158 && tv2->v_type == VAR_BLOB)
4159 {
4160 eval_addblob(tv1, tv2);
4161 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004162 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 break;
4164 }
4165 }
4166#ifdef FEAT_FLOAT
4167 if (tv1->v_type == VAR_FLOAT)
4168 {
4169 f1 = tv1->vval.v_float;
4170 n1 = 0;
4171 }
4172 else
4173#endif
4174 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004175 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 n1 = tv_get_number_chk(tv1, &error);
4177 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004178 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179#ifdef FEAT_FLOAT
4180 if (tv2->v_type == VAR_FLOAT)
4181 f1 = n1;
4182#endif
4183 }
4184#ifdef FEAT_FLOAT
4185 if (tv2->v_type == VAR_FLOAT)
4186 {
4187 f2 = tv2->vval.v_float;
4188 n2 = 0;
4189 }
4190 else
4191#endif
4192 {
4193 n2 = tv_get_number_chk(tv2, &error);
4194 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004195 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196#ifdef FEAT_FLOAT
4197 if (tv1->v_type == VAR_FLOAT)
4198 f2 = n2;
4199#endif
4200 }
4201#ifdef FEAT_FLOAT
4202 // if there is a float on either side the result is a float
4203 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4204 {
4205 switch (iptr->isn_arg.op.op_type)
4206 {
4207 case EXPR_MULT: f1 = f1 * f2; break;
4208 case EXPR_DIV: f1 = f1 / f2; break;
4209 case EXPR_SUB: f1 = f1 - f2; break;
4210 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004211 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004212 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004213 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 }
4215 clear_tv(tv1);
4216 clear_tv(tv2);
4217 tv1->v_type = VAR_FLOAT;
4218 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004219 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 }
4221 else
4222#endif
4223 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004224 int failed = FALSE;
4225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 switch (iptr->isn_arg.op.op_type)
4227 {
4228 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004229 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4230 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004231 goto on_error;
4232 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 case EXPR_SUB: n1 = n1 - n2; break;
4234 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004235 default: n1 = num_modulus(n1, n2, &failed);
4236 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004237 goto on_error;
4238 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 }
4240 clear_tv(tv1);
4241 clear_tv(tv2);
4242 tv1->v_type = VAR_NUMBER;
4243 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004244 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 }
4246 }
4247 break;
4248
4249 case ISN_CONCAT:
4250 {
4251 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4252 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4253 char_u *res;
4254
4255 res = concat_str(str1, str2);
4256 clear_tv(STACK_TV_BOT(-2));
4257 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004258 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 STACK_TV_BOT(-1)->vval.v_string = res;
4260 }
4261 break;
4262
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004263 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004264 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004265 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004266 int is_slice = iptr->isn_type == ISN_STRSLICE;
4267 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004268 char_u *res;
4269
4270 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004271 // string slice: string is at stack-3, first index at
4272 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004273 if (is_slice)
4274 {
4275 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004276 n1 = tv->vval.v_number;
4277 }
4278
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004279 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004280 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004281
Bram Moolenaar4c137212021-04-19 16:48:48 +02004282 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004283 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004284 if (is_slice)
4285 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004286 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004287 else
4288 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004289 // single character (including composing characters).
4290 // If the index is too big or negative the result is
4291 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004292 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004293 vim_free(tv->vval.v_string);
4294 tv->vval.v_string = res;
4295 }
4296 break;
4297
4298 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004299 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004300 case ISN_BLOBINDEX:
4301 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004303 int is_slice = iptr->isn_type == ISN_LISTSLICE
4304 || iptr->isn_type == ISN_BLOBSLICE;
4305 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4306 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004307 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004308 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309
4310 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004311 // list slice: list is at stack-3, indexes at stack-2 and
4312 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004313 // Same for blob.
4314 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315
4316 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004317 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004319
4320 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 {
Bram Moolenaared591872020-08-15 22:14:53 +02004322 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004323 n1 = tv->vval.v_number;
4324 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 }
Bram Moolenaared591872020-08-15 22:14:53 +02004326
Bram Moolenaar4c137212021-04-19 16:48:48 +02004327 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004328 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004329 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004330 if (is_blob)
4331 {
4332 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4333 n1, n2, FALSE, tv) == FAIL)
4334 goto on_error;
4335 }
4336 else
4337 {
4338 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4339 n1, n2, FALSE, tv, TRUE) == FAIL)
4340 goto on_error;
4341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 }
4343 break;
4344
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004345 case ISN_ANYINDEX:
4346 case ISN_ANYSLICE:
4347 {
4348 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4349 typval_T *var1, *var2;
4350 int res;
4351
4352 // index: composite is at stack-2, index at stack-1
4353 // slice: composite is at stack-3, indexes at stack-2 and
4354 // stack-1
4355 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004356 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004357 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4358 goto on_error;
4359 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4360 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004361 res = eval_index_inner(tv, is_slice, var1, var2,
4362 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004363 clear_tv(var1);
4364 if (is_slice)
4365 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004366 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004367 if (res == FAIL)
4368 goto on_error;
4369 }
4370 break;
4371
Bram Moolenaar9af78762020-06-16 11:34:42 +02004372 case ISN_SLICE:
4373 {
4374 list_T *list;
4375 int count = iptr->isn_arg.number;
4376
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004377 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004378 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004379 list = tv->vval.v_list;
4380
4381 // no error for short list, expect it to be checked earlier
4382 if (list != NULL && list->lv_len >= count)
4383 {
4384 list_T *newlist = list_slice(list,
4385 count, list->lv_len - 1);
4386
4387 if (newlist != NULL)
4388 {
4389 list_unref(list);
4390 tv->vval.v_list = newlist;
4391 ++newlist->lv_refcount;
4392 }
4393 }
4394 }
4395 break;
4396
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004397 case ISN_GETITEM:
4398 {
4399 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004400 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004401
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004402 // Get list item: list is at stack-1, push item.
4403 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004404 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4405 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004406
Bram Moolenaar35578162021-08-02 19:10:38 +02004407 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004408 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004409 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004410 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004411
4412 // Useful when used in unpack assignment. Reset at
4413 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004414 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004415 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004416 }
4417 break;
4418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 case ISN_MEMBER:
4420 {
4421 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004422 char_u *key;
4423 dictitem_T *di;
4424
4425 // dict member: dict is at stack-2, key at stack-1
4426 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004427 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004428 dict = tv->vval.v_dict;
4429
4430 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004431 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004432 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004433 if (key == NULL)
4434 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004435
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004436 if ((di = dict_find(dict, key, -1)) == NULL)
4437 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004438 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004439 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004440
4441 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004442 // stack contents makes sense and the dict stack is
4443 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004444 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004445 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004446 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004447 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004448 tv->v_type = VAR_NUMBER;
4449 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004450 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004451 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004452 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004453 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004454 // Put the dict used on the dict stack, it might be used by
4455 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004456 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004457 if (dict_stack_save(tv) == FAIL)
4458 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004459 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004460 }
4461 break;
4462
4463 // dict member with string key
4464 case ISN_STRINGMEMBER:
4465 {
4466 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 dictitem_T *di;
4468
4469 tv = STACK_TV_BOT(-1);
4470 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4471 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004472 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004473 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004474 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 }
4476 dict = tv->vval.v_dict;
4477
4478 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4479 == NULL)
4480 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004481 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004482 semsg(_(e_key_not_present_in_dictionary),
4483 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004484 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004486 // Put the dict used on the dict stack, it might be used by
4487 // a dict function later.
4488 if (dict_stack_save(tv) == FAIL)
4489 goto on_fatal_error;
4490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004492 }
4493 break;
4494
4495 case ISN_CLEARDICT:
4496 dict_stack_drop();
4497 break;
4498
4499 case ISN_USEDICT:
4500 {
4501 typval_T *dict_tv = dict_stack_get_tv();
4502
4503 // Turn "dict.Func" into a partial for "Func" bound to
4504 // "dict". Don't do this when "Func" is already a partial
4505 // that was bound explicitly (pt_auto is FALSE).
4506 tv = STACK_TV_BOT(-1);
4507 if (dict_tv != NULL
4508 && dict_tv->v_type == VAR_DICT
4509 && dict_tv->vval.v_dict != NULL
4510 && (tv->v_type == VAR_FUNC
4511 || (tv->v_type == VAR_PARTIAL
4512 && (tv->vval.v_partial->pt_auto
4513 || tv->vval.v_partial->pt_dict == NULL))))
4514 dict_tv->vval.v_dict =
4515 make_partial(dict_tv->vval.v_dict, tv);
4516 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 }
4518 break;
4519
4520 case ISN_NEGATENR:
4521 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004522 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004523#ifdef FEAT_FLOAT
4524 if (tv->v_type == VAR_FLOAT)
4525 tv->vval.v_float = -tv->vval.v_float;
4526 else
4527#endif
4528 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 break;
4530
4531 case ISN_CHECKNR:
4532 {
4533 int error = FALSE;
4534
4535 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004536 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004538 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 (void)tv_get_number_chk(tv, &error);
4540 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004541 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 }
4543 break;
4544
4545 case ISN_CHECKTYPE:
4546 {
4547 checktype_T *ct = &iptr->isn_arg.type;
4548
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004549 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004550 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004551 if (!ectx->ec_where.wt_variable)
4552 ectx->ec_where.wt_index = ct->ct_arg_idx;
4553 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4554 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004555 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004556 if (!ectx->ec_where.wt_variable)
4557 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004558
4559 // number 0 is FALSE, number 1 is TRUE
4560 if (tv->v_type == VAR_NUMBER
4561 && ct->ct_type->tt_type == VAR_BOOL
4562 && (tv->vval.v_number == 0
4563 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004565 tv->v_type = VAR_BOOL;
4566 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004567 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 }
4569 }
4570 break;
4571
Bram Moolenaar9af78762020-06-16 11:34:42 +02004572 case ISN_CHECKLEN:
4573 {
4574 int min_len = iptr->isn_arg.checklen.cl_min_len;
4575 list_T *list = NULL;
4576
4577 tv = STACK_TV_BOT(-1);
4578 if (tv->v_type == VAR_LIST)
4579 list = tv->vval.v_list;
4580 if (list == NULL || list->lv_len < min_len
4581 || (list->lv_len > min_len
4582 && !iptr->isn_arg.checklen.cl_more_OK))
4583 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004584 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004585 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004586 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004587 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004588 }
4589 }
4590 break;
4591
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004592 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004593 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004594 break;
4595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004597 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 {
4599 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004600 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004602 if (iptr->isn_type == ISN_2BOOL)
4603 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004604 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004605 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004606 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004607 n = !n;
4608 }
4609 else
4610 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004611 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004612 SOURCING_LNUM = iptr->isn_lnum;
4613 n = tv_get_bool_chk(tv, &error);
4614 if (error)
4615 goto on_error;
4616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 clear_tv(tv);
4618 tv->v_type = VAR_BOOL;
4619 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4620 }
4621 break;
4622
4623 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004624 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004625 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004626 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4627 iptr->isn_type == ISN_2STRING_ANY,
4628 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004629 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 break;
4631
Bram Moolenaar08597872020-12-10 19:43:40 +01004632 case ISN_RANGE:
4633 {
4634 exarg_T ea;
4635 char *errormsg;
4636
Bram Moolenaarece0b872021-01-08 20:40:45 +01004637 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004638 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004639 ea.addr_type = ADDR_LINES;
4640 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004641 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004642 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004643 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004644
Bram Moolenaar35578162021-08-02 19:10:38 +02004645 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004646 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004647 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004648 tv = STACK_TV_BOT(-1);
4649 tv->v_type = VAR_NUMBER;
4650 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004651 if (ea.addr_count == 0)
4652 tv->vval.v_number = curwin->w_cursor.lnum;
4653 else
4654 tv->vval.v_number = ea.line2;
4655 }
4656 break;
4657
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004658 case ISN_PUT:
4659 {
4660 int regname = iptr->isn_arg.put.put_regname;
4661 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4662 char_u *expr = NULL;
4663 int dir = FORWARD;
4664
Bram Moolenaar08597872020-12-10 19:43:40 +01004665 if (lnum < -2)
4666 {
4667 // line number was put on the stack by ISN_RANGE
4668 tv = STACK_TV_BOT(-1);
4669 curwin->w_cursor.lnum = tv->vval.v_number;
4670 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4671 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004672 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004673 }
4674 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004675 // :put! above cursor
4676 dir = BACKWARD;
4677 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004678 {
4679 curwin->w_cursor.lnum = lnum;
4680 if (lnum == 0)
4681 // check_cursor() below will move to line 1
4682 dir = BACKWARD;
4683 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004684
4685 if (regname == '=')
4686 {
4687 tv = STACK_TV_BOT(-1);
4688 if (tv->v_type == VAR_STRING)
4689 expr = tv->vval.v_string;
4690 else
4691 {
4692 expr = typval2string(tv, TRUE); // allocates value
4693 clear_tv(tv);
4694 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004695 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004696 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004697 check_cursor();
4698 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4699 vim_free(expr);
4700 }
4701 break;
4702
Bram Moolenaar02194d22020-10-24 23:08:38 +02004703 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004704 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4705 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4706 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4707 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004708 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4709 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004710 break;
4711
Bram Moolenaar02194d22020-10-24 23:08:38 +02004712 case ISN_CMDMOD_REV:
4713 // filter regprog is owned by the instruction, don't free it
4714 cmdmod.cmod_filter_regmatch.regprog = NULL;
4715 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004716 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4717 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004718 break;
4719
Bram Moolenaar792f7862020-11-23 08:31:18 +01004720 case ISN_UNPACK:
4721 {
4722 int count = iptr->isn_arg.unpack.unp_count;
4723 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4724 list_T *l;
4725 listitem_T *li;
4726 int i;
4727
4728 // Check there is a valid list to unpack.
4729 tv = STACK_TV_BOT(-1);
4730 if (tv->v_type != VAR_LIST)
4731 {
4732 SOURCING_LNUM = iptr->isn_lnum;
4733 emsg(_(e_for_argument_must_be_sequence_of_lists));
4734 goto on_error;
4735 }
4736 l = tv->vval.v_list;
4737 if (l == NULL
4738 || l->lv_len < (semicolon ? count - 1 : count))
4739 {
4740 SOURCING_LNUM = iptr->isn_lnum;
4741 emsg(_(e_list_value_does_not_have_enough_items));
4742 goto on_error;
4743 }
4744 else if (!semicolon && l->lv_len > count)
4745 {
4746 SOURCING_LNUM = iptr->isn_lnum;
4747 emsg(_(e_list_value_has_more_items_than_targets));
4748 goto on_error;
4749 }
4750
4751 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004752 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004753 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004754 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004755
4756 // Variable after semicolon gets a list with the remaining
4757 // items.
4758 if (semicolon)
4759 {
4760 list_T *rem_list =
4761 list_alloc_with_items(l->lv_len - count + 1);
4762
4763 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004764 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004765 tv = STACK_TV_BOT(-count);
4766 tv->vval.v_list = rem_list;
4767 ++rem_list->lv_refcount;
4768 tv->v_lock = 0;
4769 li = l->lv_first;
4770 for (i = 0; i < count - 1; ++i)
4771 li = li->li_next;
4772 for (i = 0; li != NULL; ++i)
4773 {
4774 list_set_item(rem_list, i, &li->li_tv);
4775 li = li->li_next;
4776 }
4777 --count;
4778 }
4779
4780 // Produce the values in reverse order, first item last.
4781 li = l->lv_first;
4782 for (i = 0; i < count; ++i)
4783 {
4784 tv = STACK_TV_BOT(-i - 1);
4785 copy_tv(&li->li_tv, tv);
4786 li = li->li_next;
4787 }
4788
4789 list_unref(l);
4790 }
4791 break;
4792
Bram Moolenaarb2049902021-01-24 12:53:53 +01004793 case ISN_PROF_START:
4794 case ISN_PROF_END:
4795 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004796#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004797 funccall_T cookie;
4798 ufunc_T *cur_ufunc =
4799 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004800 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004801
4802 cookie.func = cur_ufunc;
4803 if (iptr->isn_type == ISN_PROF_START)
4804 {
4805 func_line_start(&cookie, iptr->isn_lnum);
4806 // if we get here the instruction is executed
4807 func_line_exec(&cookie);
4808 }
4809 else
4810 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004811#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004812 }
4813 break;
4814
Bram Moolenaare99d4222021-06-13 14:01:26 +02004815 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004816 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004817 break;
4818
Bram Moolenaar389df252020-07-09 21:20:47 +02004819 case ISN_SHUFFLE:
4820 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004821 typval_T tmp_tv;
4822 int item = iptr->isn_arg.shuffle.shfl_item;
4823 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004824
4825 tmp_tv = *STACK_TV_BOT(-item);
4826 for ( ; up > 0 && item > 1; --up)
4827 {
4828 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4829 --item;
4830 }
4831 *STACK_TV_BOT(-item) = tmp_tv;
4832 }
4833 break;
4834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004836 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004838 ectx->ec_where.wt_index = 0;
4839 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 break;
4841 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004842 continue;
4843
Bram Moolenaard032f342020-07-18 18:13:02 +02004844func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004845 // Restore previous function. If the frame pointer is where we started
4846 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004847 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004848 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004849
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004851 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004852 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004853 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004854
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004855on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004856 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004857 // If "emsg_silent" is set then ignore the error, unless it was set
4858 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004859 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004860 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004861 {
4862 // If a sequence of instructions causes an error while ":silent!"
4863 // was used, restore the stack length and jump ahead to restoring
4864 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004865 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004866 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004867 while (ectx->ec_stack.ga_len
4868 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004869 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004870 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004871 clear_tv(STACK_TV_BOT(0));
4872 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004873 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4874 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004875 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004876 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004877 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004878on_fatal_error:
4879 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004880 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004881 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004882 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 }
4884
4885done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004886 ret = OK;
4887theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004888 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004889 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4890 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004891}
4892
4893/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004894 * Execute the instructions from a VAR_INSTR typeval and put the result in
4895 * "rettv".
4896 * Return OK or FAIL.
4897 */
4898 int
4899exe_typval_instr(typval_T *tv, typval_T *rettv)
4900{
4901 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4902 isn_T *save_instr = ectx->ec_instr;
4903 int save_iidx = ectx->ec_iidx;
4904 int res;
4905
4906 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4907 res = exec_instructions(ectx);
4908 if (res == OK)
4909 {
4910 *rettv = *STACK_TV_BOT(-1);
4911 --ectx->ec_stack.ga_len;
4912 }
4913
4914 ectx->ec_instr = save_instr;
4915 ectx->ec_iidx = save_iidx;
4916
4917 return res;
4918}
4919
4920/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004921 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4922 * "substitute_instr".
4923 */
4924 char_u *
4925exe_substitute_instr(void)
4926{
4927 ectx_T *ectx = substitute_instr->subs_ectx;
4928 isn_T *save_instr = ectx->ec_instr;
4929 int save_iidx = ectx->ec_iidx;
4930 char_u *res;
4931
4932 ectx->ec_instr = substitute_instr->subs_instr;
4933 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004934 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004935 typval_T *tv = STACK_TV_BOT(-1);
4936
Bram Moolenaar27523602021-06-05 21:36:19 +02004937 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004938 --ectx->ec_stack.ga_len;
4939 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004940 }
4941 else
4942 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004943 substitute_instr->subs_status = FAIL;
4944 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 ectx->ec_instr = save_instr;
4948 ectx->ec_iidx = save_iidx;
4949
4950 return res;
4951}
4952
4953/*
4954 * Call a "def" function from old Vim script.
4955 * Return OK or FAIL.
4956 */
4957 int
4958call_def_function(
4959 ufunc_T *ufunc,
4960 int argc_arg, // nr of arguments
4961 typval_T *argv, // arguments
4962 partial_T *partial, // optional partial for context
4963 typval_T *rettv) // return value
4964{
4965 ectx_T ectx; // execution context
4966 int argc = argc_arg;
4967 typval_T *tv;
4968 int idx;
4969 int ret = FAIL;
4970 int defcount = ufunc->uf_args.ga_len - argc;
4971 sctx_T save_current_sctx = current_sctx;
4972 int did_emsg_before = did_emsg_cumul + did_emsg;
4973 int save_suppress_errthrow = suppress_errthrow;
4974 msglist_T **saved_msg_list = NULL;
4975 msglist_T *private_msg_list = NULL;
4976 int save_emsg_silent_def = emsg_silent_def;
4977 int save_did_emsg_def = did_emsg_def;
4978 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004979 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004980
4981// Get pointer to item in the stack.
4982#undef STACK_TV
4983#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4984
4985// Get pointer to item at the bottom of the stack, -1 is the bottom.
4986#undef STACK_TV_BOT
4987#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4988
4989// Get pointer to a local variable on the stack. Negative for arguments.
4990#undef STACK_TV_VAR
4991#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4992
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004993 // Update uf_has_breakpoint if needed.
4994 update_has_breakpoint(ufunc);
4995
Bram Moolenaar4c137212021-04-19 16:48:48 +02004996 if (ufunc->uf_def_status == UF_NOT_COMPILED
4997 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004998 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4999 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005000 == FAIL))
5001 {
5002 if (did_emsg_cumul + did_emsg == did_emsg_before)
5003 semsg(_(e_function_is_not_compiled_str),
5004 printable_func_name(ufunc));
5005 return FAIL;
5006 }
5007
5008 {
5009 // Check the function was really compiled.
5010 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5011 + ufunc->uf_dfunc_idx;
5012 if (INSTRUCTIONS(dfunc) == NULL)
5013 {
5014 iemsg("using call_def_function() on not compiled function");
5015 return FAIL;
5016 }
5017 }
5018
5019 // If depth of calling is getting too high, don't execute the function.
5020 orig_funcdepth = funcdepth_get();
5021 if (funcdepth_increment() == FAIL)
5022 return FAIL;
5023
5024 CLEAR_FIELD(ectx);
5025 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5026 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005027 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005028 {
5029 funcdepth_decrement();
5030 return FAIL;
5031 }
5032 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5033 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5034 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005035 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005036
5037 idx = argc - ufunc->uf_args.ga_len;
5038 if (idx > 0 && ufunc->uf_va_name == NULL)
5039 {
5040 if (idx == 1)
5041 emsg(_(e_one_argument_too_many));
5042 else
5043 semsg(_(e_nr_arguments_too_many), idx);
5044 goto failed_early;
5045 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005046 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5047 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005048 {
5049 if (idx == -1)
5050 emsg(_(e_one_argument_too_few));
5051 else
5052 semsg(_(e_nr_arguments_too_few), -idx);
5053 goto failed_early;
5054 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005055
5056 // Put arguments on the stack, but no more than what the function expects.
5057 // A lambda can be called with more arguments than it uses.
5058 for (idx = 0; idx < argc
5059 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5060 ++idx)
5061 {
5062 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5063 && argv[idx].v_type == VAR_SPECIAL
5064 && argv[idx].vval.v_number == VVAL_NONE)
5065 {
5066 // Use the default value.
5067 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5068 }
5069 else
5070 {
5071 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5072 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005073 ufunc->uf_arg_types[idx], &argv[idx],
5074 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005075 goto failed_early;
5076 copy_tv(&argv[idx], STACK_TV_BOT(0));
5077 }
5078 ++ectx.ec_stack.ga_len;
5079 }
5080
5081 // Turn varargs into a list. Empty list if no args.
5082 if (ufunc->uf_va_name != NULL)
5083 {
5084 int vararg_count = argc - ufunc->uf_args.ga_len;
5085
5086 if (vararg_count < 0)
5087 vararg_count = 0;
5088 else
5089 argc -= vararg_count;
5090 if (exe_newlist(vararg_count, &ectx) == FAIL)
5091 goto failed_early;
5092
5093 // Check the type of the list items.
5094 tv = STACK_TV_BOT(-1);
5095 if (ufunc->uf_va_type != NULL
5096 && ufunc->uf_va_type != &t_list_any
5097 && ufunc->uf_va_type->tt_member != &t_any
5098 && tv->vval.v_list != NULL)
5099 {
5100 type_T *expected = ufunc->uf_va_type->tt_member;
5101 listitem_T *li = tv->vval.v_list->lv_first;
5102
5103 for (idx = 0; idx < vararg_count; ++idx)
5104 {
5105 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005106 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005107 goto failed_early;
5108 li = li->li_next;
5109 }
5110 }
5111
5112 if (defcount > 0)
5113 // Move varargs list to below missing default arguments.
5114 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5115 --ectx.ec_stack.ga_len;
5116 }
5117
5118 // Make space for omitted arguments, will store default value below.
5119 // Any varargs list goes after them.
5120 if (defcount > 0)
5121 for (idx = 0; idx < defcount; ++idx)
5122 {
5123 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5124 ++ectx.ec_stack.ga_len;
5125 }
5126 if (ufunc->uf_va_name != NULL)
5127 ++ectx.ec_stack.ga_len;
5128
5129 // Frame pointer points to just after arguments.
5130 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5131 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5132
5133 {
5134 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5135 + ufunc->uf_dfunc_idx;
5136 ufunc_T *base_ufunc = dfunc->df_ufunc;
5137
5138 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5139 // by copy_func().
5140 if (partial != NULL || base_ufunc->uf_partial != NULL)
5141 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005142 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5143 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005144 goto failed_early;
5145 if (partial != NULL)
5146 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005147 outer_T *outer = get_pt_outer(partial);
5148
5149 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005150 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005151 if (current_ectx != NULL)
5152 {
5153 if (current_ectx->ec_outer_ref != NULL
5154 && current_ectx->ec_outer_ref->or_outer != NULL)
5155 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005156 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005157 }
5158 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005159 }
5160 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005161 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005162 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005163 ++partial->pt_refcount;
5164 ectx.ec_outer_ref->or_partial = partial;
5165 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005166 }
5167 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005168 {
5169 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5170 ++base_ufunc->uf_partial->pt_refcount;
5171 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5172 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005173 }
5174 }
5175
5176 // dummy frame entries
5177 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5178 {
5179 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5180 ++ectx.ec_stack.ga_len;
5181 }
5182
5183 {
5184 // Reserve space for local variables and any closure reference count.
5185 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5186 + ufunc->uf_dfunc_idx;
5187
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005188 // Initialize variables to zero. That avoids having to generate
5189 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005190 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005191 {
5192 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5193 STACK_TV_VAR(idx)->vval.v_number = 0;
5194 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005195 ectx.ec_stack.ga_len += dfunc->df_varcount;
5196 if (dfunc->df_has_closure)
5197 {
5198 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5199 STACK_TV_VAR(idx)->vval.v_number = 0;
5200 ++ectx.ec_stack.ga_len;
5201 }
5202
5203 ectx.ec_instr = INSTRUCTIONS(dfunc);
5204 }
5205
5206 // Following errors are in the function, not the caller.
5207 // Commands behave like vim9script.
5208 estack_push_ufunc(ufunc, 1);
5209 current_sctx = ufunc->uf_script_ctx;
5210 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5211
5212 // Use a specific location for storing error messages to be converted to an
5213 // exception.
5214 saved_msg_list = msg_list;
5215 msg_list = &private_msg_list;
5216
5217 // Do turn errors into exceptions.
5218 suppress_errthrow = FALSE;
5219
5220 // Do not delete the function while executing it.
5221 ++ufunc->uf_calls;
5222
5223 // When ":silent!" was used before calling then we still abort the
5224 // function. If ":silent!" is used in the function then we don't.
5225 emsg_silent_def = emsg_silent;
5226 did_emsg_def = 0;
5227
5228 ectx.ec_where.wt_index = 0;
5229 ectx.ec_where.wt_variable = FALSE;
5230
5231 // Execute the instructions until done.
5232 ret = exec_instructions(&ectx);
5233 if (ret == OK)
5234 {
5235 // function finished, get result from the stack.
5236 if (ufunc->uf_ret_type == &t_void)
5237 {
5238 rettv->v_type = VAR_VOID;
5239 }
5240 else
5241 {
5242 tv = STACK_TV_BOT(-1);
5243 *rettv = *tv;
5244 tv->v_type = VAR_UNKNOWN;
5245 }
5246 }
5247
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005248 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005249 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5250 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005251
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005252 // Deal with any remaining closures, they may be in use somewhere.
5253 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005254 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005255 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005256 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005257 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005258
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005259 estack_pop();
5260 current_sctx = save_current_sctx;
5261
Bram Moolenaar2336c372021-12-06 15:06:54 +00005262 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5263 // Function was unreferenced while being used, free it now.
5264 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005265
Bram Moolenaar352134b2020-10-17 22:04:08 +02005266 if (*msg_list != NULL && saved_msg_list != NULL)
5267 {
5268 msglist_T **plist = saved_msg_list;
5269
5270 // Append entries from the current msg_list (uncaught exceptions) to
5271 // the saved msg_list.
5272 while (*plist != NULL)
5273 plist = &(*plist)->next;
5274
5275 *plist = *msg_list;
5276 }
5277 msg_list = saved_msg_list;
5278
Bram Moolenaar4c137212021-04-19 16:48:48 +02005279 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005280 {
5281 cmdmod.cmod_filter_regmatch.regprog = NULL;
5282 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005283 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005284 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005285 emsg_silent_def = save_emsg_silent_def;
5286 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005287
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005288failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005289 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5291 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005292 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005295 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005296 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005297 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005298 if (ectx.ec_outer_ref->or_outer_allocated)
5299 vim_free(ectx.ec_outer_ref->or_outer);
5300 partial_unref(ectx.ec_outer_ref->or_partial);
5301 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005302 }
5303
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005304 // Not sure if this is necessary.
5305 suppress_errthrow = save_suppress_errthrow;
5306
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005307 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5308 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005309 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005310 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005311 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312 return ret;
5313}
5314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005316 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5317 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5318 * "pfx" is prefixed to every line.
5319 */
5320 static void
5321list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5322{
5323 int line_idx = 0;
5324 int prev_current = 0;
5325 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005326 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005327
5328 for (current = 0; current < instr_count; ++current)
5329 {
5330 isn_T *iptr = &instr[current];
5331 char *line;
5332
5333 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005334 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005335 while (line_idx < iptr->isn_lnum
5336 && line_idx < ufunc->uf_lines.ga_len)
5337 {
5338 if (current > prev_current)
5339 {
5340 msg_puts("\n\n");
5341 prev_current = current;
5342 }
5343 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5344 if (line != NULL)
5345 msg(line);
5346 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005347 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5348 {
5349 int first_def_arg = ufunc->uf_args.ga_len
5350 - ufunc->uf_def_args.ga_len;
5351
5352 if (def_arg_idx > 0)
5353 msg_puts("\n\n");
5354 msg_start();
5355 msg_puts(" ");
5356 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5357 first_def_arg + def_arg_idx]);
5358 msg_puts(" = ");
5359 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5360 msg_clr_eos();
5361 msg_end();
5362 }
5363 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005364
5365 switch (iptr->isn_type)
5366 {
5367 case ISN_EXEC:
5368 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5369 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005370 case ISN_EXEC_SPLIT:
5371 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5372 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005373 case ISN_EXECRANGE:
5374 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5375 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005376 case ISN_LEGACY_EVAL:
5377 smsg("%s%4d EVAL legacy %s", pfx, current,
5378 iptr->isn_arg.string);
5379 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005380 case ISN_REDIRSTART:
5381 smsg("%s%4d REDIR", pfx, current);
5382 break;
5383 case ISN_REDIREND:
5384 smsg("%s%4d REDIR END%s", pfx, current,
5385 iptr->isn_arg.number ? " append" : "");
5386 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005387 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005388#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005389 smsg("%s%4d CEXPR pre %s", pfx, current,
5390 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005391#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005392 break;
5393 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005394#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005395 {
5396 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5397
5398 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5399 cexpr_get_auname(cer->cer_cmdidx),
5400 cer->cer_forceit ? "!" : "",
5401 cer->cer_cmdline);
5402 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005403#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005404 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005405 case ISN_INSTR:
5406 {
5407 smsg("%s%4d INSTR", pfx, current);
5408 list_instructions(" ", iptr->isn_arg.instr,
5409 INT_MAX, NULL);
5410 msg(" -------------");
5411 }
5412 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005413 case ISN_SUBSTITUTE:
5414 {
5415 subs_T *subs = &iptr->isn_arg.subs;
5416
5417 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5418 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5419 msg(" -------------");
5420 }
5421 break;
5422 case ISN_EXECCONCAT:
5423 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5424 (varnumber_T)iptr->isn_arg.number);
5425 break;
5426 case ISN_ECHO:
5427 {
5428 echo_T *echo = &iptr->isn_arg.echo;
5429
5430 smsg("%s%4d %s %d", pfx, current,
5431 echo->echo_with_white ? "ECHO" : "ECHON",
5432 echo->echo_count);
5433 }
5434 break;
5435 case ISN_EXECUTE:
5436 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005437 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005438 break;
5439 case ISN_ECHOMSG:
5440 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005441 (varnumber_T)(iptr->isn_arg.number));
5442 break;
5443 case ISN_ECHOCONSOLE:
5444 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5445 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005446 break;
5447 case ISN_ECHOERR:
5448 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005449 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005450 break;
5451 case ISN_LOAD:
5452 {
5453 if (iptr->isn_arg.number < 0)
5454 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5455 (varnumber_T)(iptr->isn_arg.number
5456 + STACK_FRAME_SIZE));
5457 else
5458 smsg("%s%4d LOAD $%lld", pfx, current,
5459 (varnumber_T)(iptr->isn_arg.number));
5460 }
5461 break;
5462 case ISN_LOADOUTER:
5463 {
5464 if (iptr->isn_arg.number < 0)
5465 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5466 iptr->isn_arg.outer.outer_depth,
5467 iptr->isn_arg.outer.outer_idx
5468 + STACK_FRAME_SIZE);
5469 else
5470 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5471 iptr->isn_arg.outer.outer_depth,
5472 iptr->isn_arg.outer.outer_idx);
5473 }
5474 break;
5475 case ISN_LOADV:
5476 smsg("%s%4d LOADV v:%s", pfx, current,
5477 get_vim_var_name(iptr->isn_arg.number));
5478 break;
5479 case ISN_LOADSCRIPT:
5480 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005481 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5482 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5483 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005484
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005485 sv = get_script_svar(sref, -1);
5486 if (sv == NULL)
5487 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5488 pfx, current, si->sn_name);
5489 else
5490 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005491 sv->sv_name,
5492 sref->sref_idx,
5493 si->sn_name);
5494 }
5495 break;
5496 case ISN_LOADS:
5497 {
5498 scriptitem_T *si = SCRIPT_ITEM(
5499 iptr->isn_arg.loadstore.ls_sid);
5500
5501 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5502 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5503 }
5504 break;
5505 case ISN_LOADAUTO:
5506 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5507 break;
5508 case ISN_LOADG:
5509 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5510 break;
5511 case ISN_LOADB:
5512 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5513 break;
5514 case ISN_LOADW:
5515 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5516 break;
5517 case ISN_LOADT:
5518 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5519 break;
5520 case ISN_LOADGDICT:
5521 smsg("%s%4d LOAD g:", pfx, current);
5522 break;
5523 case ISN_LOADBDICT:
5524 smsg("%s%4d LOAD b:", pfx, current);
5525 break;
5526 case ISN_LOADWDICT:
5527 smsg("%s%4d LOAD w:", pfx, current);
5528 break;
5529 case ISN_LOADTDICT:
5530 smsg("%s%4d LOAD t:", pfx, current);
5531 break;
5532 case ISN_LOADOPT:
5533 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5534 break;
5535 case ISN_LOADENV:
5536 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5537 break;
5538 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005539 smsg("%s%4d LOADREG @%c", pfx, current,
5540 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005541 break;
5542
5543 case ISN_STORE:
5544 if (iptr->isn_arg.number < 0)
5545 smsg("%s%4d STORE arg[%lld]", pfx, current,
5546 iptr->isn_arg.number + STACK_FRAME_SIZE);
5547 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005548 smsg("%s%4d STORE $%lld", pfx, current,
5549 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005550 break;
5551 case ISN_STOREOUTER:
5552 {
5553 if (iptr->isn_arg.number < 0)
5554 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5555 iptr->isn_arg.outer.outer_depth,
5556 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5557 else
5558 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5559 iptr->isn_arg.outer.outer_depth,
5560 iptr->isn_arg.outer.outer_idx);
5561 }
5562 break;
5563 case ISN_STOREV:
5564 smsg("%s%4d STOREV v:%s", pfx, current,
5565 get_vim_var_name(iptr->isn_arg.number));
5566 break;
5567 case ISN_STOREAUTO:
5568 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5569 break;
5570 case ISN_STOREG:
5571 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5572 break;
5573 case ISN_STOREB:
5574 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5575 break;
5576 case ISN_STOREW:
5577 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5578 break;
5579 case ISN_STORET:
5580 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5581 break;
5582 case ISN_STORES:
5583 {
5584 scriptitem_T *si = SCRIPT_ITEM(
5585 iptr->isn_arg.loadstore.ls_sid);
5586
5587 smsg("%s%4d STORES %s in %s", pfx, current,
5588 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5589 }
5590 break;
5591 case ISN_STORESCRIPT:
5592 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005593 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5594 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5595 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005596
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005597 sv = get_script_svar(sref, -1);
5598 if (sv == NULL)
5599 smsg("%s%4d STORESCRIPT [deleted] in %s",
5600 pfx, current, si->sn_name);
5601 else
5602 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005603 sv->sv_name,
5604 sref->sref_idx,
5605 si->sn_name);
5606 }
5607 break;
5608 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005609 case ISN_STOREFUNCOPT:
5610 smsg("%s%4d %s &%s", pfx, current,
5611 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005612 iptr->isn_arg.storeopt.so_name);
5613 break;
5614 case ISN_STOREENV:
5615 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5616 break;
5617 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005618 smsg("%s%4d STOREREG @%c", pfx, current,
5619 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005620 break;
5621 case ISN_STORENR:
5622 smsg("%s%4d STORE %lld in $%d", pfx, current,
5623 iptr->isn_arg.storenr.stnr_val,
5624 iptr->isn_arg.storenr.stnr_idx);
5625 break;
5626
5627 case ISN_STOREINDEX:
5628 smsg("%s%4d STOREINDEX %s", pfx, current,
5629 vartype_name(iptr->isn_arg.vartype));
5630 break;
5631
5632 case ISN_STORERANGE:
5633 smsg("%s%4d STORERANGE", pfx, current);
5634 break;
5635
5636 // constants
5637 case ISN_PUSHNR:
5638 smsg("%s%4d PUSHNR %lld", pfx, current,
5639 (varnumber_T)(iptr->isn_arg.number));
5640 break;
5641 case ISN_PUSHBOOL:
5642 case ISN_PUSHSPEC:
5643 smsg("%s%4d PUSH %s", pfx, current,
5644 get_var_special_name(iptr->isn_arg.number));
5645 break;
5646 case ISN_PUSHF:
5647#ifdef FEAT_FLOAT
5648 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5649#endif
5650 break;
5651 case ISN_PUSHS:
5652 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5653 break;
5654 case ISN_PUSHBLOB:
5655 {
5656 char_u *r;
5657 char_u numbuf[NUMBUFLEN];
5658 char_u *tofree;
5659
5660 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5661 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5662 vim_free(tofree);
5663 }
5664 break;
5665 case ISN_PUSHFUNC:
5666 {
5667 char *name = (char *)iptr->isn_arg.string;
5668
5669 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5670 name == NULL ? "[none]" : name);
5671 }
5672 break;
5673 case ISN_PUSHCHANNEL:
5674#ifdef FEAT_JOB_CHANNEL
5675 {
5676 channel_T *channel = iptr->isn_arg.channel;
5677
5678 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5679 channel == NULL ? 0 : channel->ch_id);
5680 }
5681#endif
5682 break;
5683 case ISN_PUSHJOB:
5684#ifdef FEAT_JOB_CHANNEL
5685 {
5686 typval_T tv;
5687 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005688 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005689
5690 tv.v_type = VAR_JOB;
5691 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005692 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005693 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5694 }
5695#endif
5696 break;
5697 case ISN_PUSHEXC:
5698 smsg("%s%4d PUSH v:exception", pfx, current);
5699 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005700 case ISN_AUTOLOAD:
5701 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5702 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005703 case ISN_UNLET:
5704 smsg("%s%4d UNLET%s %s", pfx, current,
5705 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5706 iptr->isn_arg.unlet.ul_name);
5707 break;
5708 case ISN_UNLETENV:
5709 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5710 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5711 iptr->isn_arg.unlet.ul_name);
5712 break;
5713 case ISN_UNLETINDEX:
5714 smsg("%s%4d UNLETINDEX", pfx, current);
5715 break;
5716 case ISN_UNLETRANGE:
5717 smsg("%s%4d UNLETRANGE", pfx, current);
5718 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005719 case ISN_LOCKUNLOCK:
5720 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5721 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005722 case ISN_LOCKCONST:
5723 smsg("%s%4d LOCKCONST", pfx, current);
5724 break;
5725 case ISN_NEWLIST:
5726 smsg("%s%4d NEWLIST size %lld", pfx, current,
5727 (varnumber_T)(iptr->isn_arg.number));
5728 break;
5729 case ISN_NEWDICT:
5730 smsg("%s%4d NEWDICT size %lld", pfx, current,
5731 (varnumber_T)(iptr->isn_arg.number));
5732 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005733 case ISN_NEWPARTIAL:
5734 smsg("%s%4d NEWPARTIAL", pfx, current);
5735 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005736
5737 // function call
5738 case ISN_BCALL:
5739 {
5740 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5741
5742 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5743 internal_func_name(cbfunc->cbf_idx),
5744 cbfunc->cbf_argcount);
5745 }
5746 break;
5747 case ISN_DCALL:
5748 {
5749 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5750 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5751 + cdfunc->cdf_idx;
5752
5753 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005754 printable_func_name(df->df_ufunc),
5755 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005756 }
5757 break;
5758 case ISN_UCALL:
5759 {
5760 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5761
5762 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5763 cufunc->cuf_name, cufunc->cuf_argcount);
5764 }
5765 break;
5766 case ISN_PCALL:
5767 {
5768 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5769
5770 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5771 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5772 }
5773 break;
5774 case ISN_PCALL_END:
5775 smsg("%s%4d PCALL end", pfx, current);
5776 break;
5777 case ISN_RETURN:
5778 smsg("%s%4d RETURN", pfx, current);
5779 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005780 case ISN_RETURN_VOID:
5781 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005782 break;
5783 case ISN_FUNCREF:
5784 {
5785 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005786 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005787
Bram Moolenaar38453522021-11-28 22:00:12 +00005788 if (funcref->fr_func_name == NULL)
5789 {
5790 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5791 + funcref->fr_dfunc_idx;
5792 name = df->df_ufunc->uf_name;
5793 }
5794 else
5795 name = funcref->fr_func_name;
5796 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005797 }
5798 break;
5799
5800 case ISN_NEWFUNC:
5801 {
5802 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5803
5804 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5805 newfunc->nf_lambda, newfunc->nf_global);
5806 }
5807 break;
5808
5809 case ISN_DEF:
5810 {
5811 char_u *name = iptr->isn_arg.string;
5812
5813 smsg("%s%4d DEF %s", pfx, current,
5814 name == NULL ? (char_u *)"" : name);
5815 }
5816 break;
5817
5818 case ISN_JUMP:
5819 {
5820 char *when = "?";
5821
5822 switch (iptr->isn_arg.jump.jump_when)
5823 {
5824 case JUMP_ALWAYS:
5825 when = "JUMP";
5826 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005827 case JUMP_NEVER:
5828 iemsg("JUMP_NEVER should not be used");
5829 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005830 case JUMP_AND_KEEP_IF_TRUE:
5831 when = "JUMP_AND_KEEP_IF_TRUE";
5832 break;
5833 case JUMP_IF_FALSE:
5834 when = "JUMP_IF_FALSE";
5835 break;
5836 case JUMP_AND_KEEP_IF_FALSE:
5837 when = "JUMP_AND_KEEP_IF_FALSE";
5838 break;
5839 case JUMP_IF_COND_FALSE:
5840 when = "JUMP_IF_COND_FALSE";
5841 break;
5842 case JUMP_IF_COND_TRUE:
5843 when = "JUMP_IF_COND_TRUE";
5844 break;
5845 }
5846 smsg("%s%4d %s -> %d", pfx, current, when,
5847 iptr->isn_arg.jump.jump_where);
5848 }
5849 break;
5850
5851 case ISN_JUMP_IF_ARG_SET:
5852 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5853 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5854 iptr->isn_arg.jump.jump_where);
5855 break;
5856
5857 case ISN_FOR:
5858 {
5859 forloop_T *forloop = &iptr->isn_arg.forloop;
5860
5861 smsg("%s%4d FOR $%d -> %d", pfx, current,
5862 forloop->for_idx, forloop->for_end);
5863 }
5864 break;
5865
5866 case ISN_TRY:
5867 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005868 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005869
5870 if (try->try_ref->try_finally == 0)
5871 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5872 pfx, current,
5873 try->try_ref->try_catch,
5874 try->try_ref->try_endtry);
5875 else
5876 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5877 pfx, current,
5878 try->try_ref->try_catch,
5879 try->try_ref->try_finally,
5880 try->try_ref->try_endtry);
5881 }
5882 break;
5883 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005884 smsg("%s%4d CATCH", pfx, current);
5885 break;
5886 case ISN_TRYCONT:
5887 {
5888 trycont_T *trycont = &iptr->isn_arg.trycont;
5889
5890 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5891 trycont->tct_levels,
5892 trycont->tct_levels == 1 ? "" : "s",
5893 trycont->tct_where);
5894 }
5895 break;
5896 case ISN_FINALLY:
5897 smsg("%s%4d FINALLY", pfx, current);
5898 break;
5899 case ISN_ENDTRY:
5900 smsg("%s%4d ENDTRY", pfx, current);
5901 break;
5902 case ISN_THROW:
5903 smsg("%s%4d THROW", pfx, current);
5904 break;
5905
5906 // expression operations on number
5907 case ISN_OPNR:
5908 case ISN_OPFLOAT:
5909 case ISN_OPANY:
5910 {
5911 char *what;
5912 char *ins;
5913
5914 switch (iptr->isn_arg.op.op_type)
5915 {
5916 case EXPR_MULT: what = "*"; break;
5917 case EXPR_DIV: what = "/"; break;
5918 case EXPR_REM: what = "%"; break;
5919 case EXPR_SUB: what = "-"; break;
5920 case EXPR_ADD: what = "+"; break;
5921 default: what = "???"; break;
5922 }
5923 switch (iptr->isn_type)
5924 {
5925 case ISN_OPNR: ins = "OPNR"; break;
5926 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5927 case ISN_OPANY: ins = "OPANY"; break;
5928 default: ins = "???"; break;
5929 }
5930 smsg("%s%4d %s %s", pfx, current, ins, what);
5931 }
5932 break;
5933
5934 case ISN_COMPAREBOOL:
5935 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00005936 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005937 case ISN_COMPARENR:
5938 case ISN_COMPAREFLOAT:
5939 case ISN_COMPARESTRING:
5940 case ISN_COMPAREBLOB:
5941 case ISN_COMPARELIST:
5942 case ISN_COMPAREDICT:
5943 case ISN_COMPAREFUNC:
5944 case ISN_COMPAREANY:
5945 {
5946 char *p;
5947 char buf[10];
5948 char *type;
5949
5950 switch (iptr->isn_arg.op.op_type)
5951 {
5952 case EXPR_EQUAL: p = "=="; break;
5953 case EXPR_NEQUAL: p = "!="; break;
5954 case EXPR_GREATER: p = ">"; break;
5955 case EXPR_GEQUAL: p = ">="; break;
5956 case EXPR_SMALLER: p = "<"; break;
5957 case EXPR_SEQUAL: p = "<="; break;
5958 case EXPR_MATCH: p = "=~"; break;
5959 case EXPR_IS: p = "is"; break;
5960 case EXPR_ISNOT: p = "isnot"; break;
5961 case EXPR_NOMATCH: p = "!~"; break;
5962 default: p = "???"; break;
5963 }
5964 STRCPY(buf, p);
5965 if (iptr->isn_arg.op.op_ic == TRUE)
5966 strcat(buf, "?");
5967 switch(iptr->isn_type)
5968 {
5969 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5970 case ISN_COMPARESPECIAL:
5971 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00005972 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005973 case ISN_COMPARENR: type = "COMPARENR"; break;
5974 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5975 case ISN_COMPARESTRING:
5976 type = "COMPARESTRING"; break;
5977 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5978 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5979 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5980 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5981 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5982 default: type = "???"; break;
5983 }
5984
5985 smsg("%s%4d %s %s", pfx, current, type, buf);
5986 }
5987 break;
5988
5989 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5990 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5991
5992 // expression operations
5993 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5994 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5995 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5996 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5997 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5998 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5999 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6000 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6001 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6002 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6003 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6004 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006005 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006006 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6007 iptr->isn_arg.getitem.gi_index,
6008 iptr->isn_arg.getitem.gi_with_op ?
6009 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006010 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6011 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6012 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006013 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6014 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6015
Bram Moolenaar4c137212021-04-19 16:48:48 +02006016 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6017
6018 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6019 case ISN_CHECKTYPE:
6020 {
6021 checktype_T *ct = &iptr->isn_arg.type;
6022 char *tofree;
6023
6024 if (ct->ct_arg_idx == 0)
6025 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6026 type_name(ct->ct_type, &tofree),
6027 (int)ct->ct_off);
6028 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006029 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6030 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006031 type_name(ct->ct_type, &tofree),
6032 (int)ct->ct_off,
6033 (int)ct->ct_arg_idx);
6034 vim_free(tofree);
6035 break;
6036 }
6037 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6038 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6039 iptr->isn_arg.checklen.cl_min_len);
6040 break;
6041 case ISN_SETTYPE:
6042 {
6043 char *tofree;
6044
6045 smsg("%s%4d SETTYPE %s", pfx, current,
6046 type_name(iptr->isn_arg.type.ct_type, &tofree));
6047 vim_free(tofree);
6048 break;
6049 }
6050 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006051 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6052 smsg("%s%4d INVERT %d (!val)", pfx, current,
6053 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006054 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006055 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6056 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006057 break;
6058 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006059 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006060 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006061 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6062 pfx, current,
6063 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006064 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006065 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6066 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006067 break;
6068 case ISN_PUT:
6069 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6070 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006071 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006072 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6073 smsg("%s%4d PUT %c 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
6076 smsg("%s%4d PUT %c %ld", pfx, current,
6077 iptr->isn_arg.put.put_regname,
6078 (long)iptr->isn_arg.put.put_lnum);
6079 break;
6080
Bram Moolenaar4c137212021-04-19 16:48:48 +02006081 case ISN_CMDMOD:
6082 {
6083 char_u *buf;
6084 size_t len = produce_cmdmods(
6085 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6086
6087 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006088 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006089 {
6090 (void)produce_cmdmods(
6091 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6092 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6093 vim_free(buf);
6094 }
6095 break;
6096 }
6097 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6098
6099 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006100 smsg("%s%4d PROFILE START line %d", pfx, current,
6101 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006102 break;
6103
6104 case ISN_PROF_END:
6105 smsg("%s%4d PROFILE END", pfx, current);
6106 break;
6107
Bram Moolenaare99d4222021-06-13 14:01:26 +02006108 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006109 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6110 iptr->isn_arg.debug.dbg_break_lnum + 1,
6111 iptr->isn_lnum,
6112 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006113 break;
6114
Bram Moolenaar4c137212021-04-19 16:48:48 +02006115 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6116 iptr->isn_arg.unpack.unp_count,
6117 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6118 break;
6119 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6120 iptr->isn_arg.shuffle.shfl_item,
6121 iptr->isn_arg.shuffle.shfl_up);
6122 break;
6123 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6124
6125 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6126 return;
6127 }
6128
6129 out_flush(); // output one line at a time
6130 ui_breakcheck();
6131 if (got_int)
6132 break;
6133 }
6134}
6135
6136/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006137 * Handle command line completion for the :disassemble command.
6138 */
6139 void
6140set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6141{
6142 char_u *p;
6143
6144 // Default: expand user functions, "debug" and "profile"
6145 xp->xp_context = EXPAND_DISASSEMBLE;
6146 xp->xp_pattern = arg;
6147
6148 // first argument already typed: only user function names
6149 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6150 {
6151 xp->xp_context = EXPAND_USER_FUNC;
6152 xp->xp_pattern = skipwhite(p);
6153 }
6154}
6155
6156/*
6157 * Function given to ExpandGeneric() to obtain the list of :disassemble
6158 * arguments.
6159 */
6160 char_u *
6161get_disassemble_argument(expand_T *xp, int idx)
6162{
6163 if (idx == 0)
6164 return (char_u *)"debug";
6165 if (idx == 1)
6166 return (char_u *)"profile";
6167 return get_user_func_name(xp, idx - 2);
6168}
6169
6170/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006171 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006172 * We don't really need this at runtime, but we do have tests that require it,
6173 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 */
6175 void
6176ex_disassemble(exarg_T *eap)
6177{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006178 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006179 char_u *fname;
6180 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181 dfunc_T *dfunc;
6182 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006183 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006184 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006185 compiletype_T compile_type = CT_NONE;
6186
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006187 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006188 {
6189 compile_type = CT_PROFILE;
6190 arg = skipwhite(arg + 7);
6191 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006192 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006193 {
6194 compile_type = CT_DEBUG;
6195 arg = skipwhite(arg + 5);
6196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006198 if (STRNCMP(arg, "<lambda>", 8) == 0)
6199 {
6200 arg += 8;
6201 (void)getdigits(&arg);
6202 fname = vim_strnsave(eap->arg, arg - eap->arg);
6203 }
6204 else
6205 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006206 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006207 if (fname == NULL)
6208 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006209 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006210 return;
6211 }
6212
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006213 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006214 if (ufunc == NULL)
6215 {
6216 char_u *p = untrans_function_name(fname);
6217
6218 if (p != NULL)
6219 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006220 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006221 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006222 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223 if (ufunc == NULL)
6224 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006225 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226 return;
6227 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006228 if (func_needs_compiling(ufunc, compile_type)
6229 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006230 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006231 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006233 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234 return;
6235 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006236 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237
6238 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006239 switch (compile_type)
6240 {
6241 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006242#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006243 instr = dfunc->df_instr_prof;
6244 instr_count = dfunc->df_instr_prof_count;
6245 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006246#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006247 // FALLTHROUGH
6248 case CT_NONE:
6249 instr = dfunc->df_instr;
6250 instr_count = dfunc->df_instr_count;
6251 break;
6252 case CT_DEBUG:
6253 instr = dfunc->df_instr_debug;
6254 instr_count = dfunc->df_instr_debug_count;
6255 break;
6256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257
Bram Moolenaar4c137212021-04-19 16:48:48 +02006258 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259}
6260
6261/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006262 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 * list, etc. Mostly like what JavaScript does, except that empty list and
6264 * empty dictionary are FALSE.
6265 */
6266 int
6267tv2bool(typval_T *tv)
6268{
6269 switch (tv->v_type)
6270 {
6271 case VAR_NUMBER:
6272 return tv->vval.v_number != 0;
6273 case VAR_FLOAT:
6274#ifdef FEAT_FLOAT
6275 return tv->vval.v_float != 0.0;
6276#else
6277 break;
6278#endif
6279 case VAR_PARTIAL:
6280 return tv->vval.v_partial != NULL;
6281 case VAR_FUNC:
6282 case VAR_STRING:
6283 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6284 case VAR_LIST:
6285 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6286 case VAR_DICT:
6287 return tv->vval.v_dict != NULL
6288 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6289 case VAR_BOOL:
6290 case VAR_SPECIAL:
6291 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6292 case VAR_JOB:
6293#ifdef FEAT_JOB_CHANNEL
6294 return tv->vval.v_job != NULL;
6295#else
6296 break;
6297#endif
6298 case VAR_CHANNEL:
6299#ifdef FEAT_JOB_CHANNEL
6300 return tv->vval.v_channel != NULL;
6301#else
6302 break;
6303#endif
6304 case VAR_BLOB:
6305 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6306 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006307 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006309 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 break;
6311 }
6312 return FALSE;
6313}
6314
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006315 void
6316emsg_using_string_as(typval_T *tv, int as_number)
6317{
6318 semsg(_(as_number ? e_using_string_as_number_str
6319 : e_using_string_as_bool_str),
6320 tv->vval.v_string == NULL
6321 ? (char_u *)"" : tv->vval.v_string);
6322}
6323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324/*
6325 * If "tv" is a string give an error and return FAIL.
6326 */
6327 int
6328check_not_string(typval_T *tv)
6329{
6330 if (tv->v_type == VAR_STRING)
6331 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006332 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333 clear_tv(tv);
6334 return FAIL;
6335 }
6336 return OK;
6337}
6338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339
6340#endif // FEAT_EVAL