blob: 4562a08bf4e130a49cdd5bced5814dd86b433873 [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
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
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
168/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100170 * This adds a stack frame and sets the instruction pointer to the start of the
171 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200172 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 *
174 * Stack has:
175 * - current arguments (already there)
176 * - omitted optional argument (default values) added here
177 * - stack frame:
178 * - pointer to calling function
179 * - Index of next instruction in calling function
180 * - previous frame pointer
181 * - reserved space for local variables
182 */
183 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100184call_dfunc(
185 int cdf_idx,
186 partial_T *pt,
187 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100188 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100190 int argcount = argcount_arg;
191 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
192 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200193 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100194 int arg_to_add;
195 int vararg_count = 0;
196 int varcount;
197 int idx;
198 estack_T *entry;
199 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200200 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100201
202 if (dfunc->df_deleted)
203 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100204 // don't use ufunc->uf_name, it may have been freed
205 emsg_funcname(e_func_deleted,
206 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 return FAIL;
208 }
209
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100210#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100211 if (do_profiling == PROF_YES)
212 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200213 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100214 {
215 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
216 + profile_info_ga.ga_len;
217 ++profile_info_ga.ga_len;
218 CLEAR_POINTER(info);
219 profile_may_start_func(info, ufunc,
220 (((dfunc_T *)def_functions.ga_data)
221 + ectx->ec_dfunc_idx)->df_ufunc);
222 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100223 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100224#endif
225
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200226 // Update uf_has_breakpoint if needed.
227 update_has_breakpoint(ufunc);
228
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200229 // When debugging and using "cont" switches to the not-debugged
230 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200231 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
232 {
233 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
234
235 // compile_def_function() may cause def_functions.ga_data to change
236 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
237 }
238 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200239 {
240 if (did_emsg_cumul + did_emsg == did_emsg_before)
241 semsg(_(e_function_is_not_compiled_str),
242 printable_func_name(ufunc));
243 return FAIL;
244 }
245
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200246 if (ufunc->uf_va_name != NULL)
247 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200248 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200249 // Stack at time of call with 2 varargs:
250 // normal_arg
251 // optional_arg
252 // vararg_1
253 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200254 // After creating the list:
255 // normal_arg
256 // optional_arg
257 // vararg-list
258 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200259 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200260 // After creating the list
261 // normal_arg
262 // (space for optional_arg)
263 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200264 vararg_count = argcount - ufunc->uf_args.ga_len;
265 if (vararg_count < 0)
266 vararg_count = 0;
267 else
268 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200269 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200270 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200271
272 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200273 }
274
Bram Moolenaarfe270812020-04-11 22:31:27 +0200275 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200276 if (arg_to_add < 0)
277 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200278 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200279 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200280 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200281 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200282 return FAIL;
283 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200284
285 // Reserve space for:
286 // - missing arguments
287 // - stack frame
288 // - local variables
289 // - if needed: a counter for number of closures created in
290 // ectx->ec_funcrefs.
291 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200292 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 return FAIL;
294
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100295 // If depth of calling is getting too high, don't execute the function.
296 if (funcdepth_increment() == FAIL)
297 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200298 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100299
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100300 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200301 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100302 {
303 floc = ALLOC_ONE(funclocal_T);
304 if (floc == NULL)
305 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200306 *floc = ectx->ec_funclocal;
307 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100308 }
309
Bram Moolenaarfe270812020-04-11 22:31:27 +0200310 // Move the vararg-list to below the missing optional arguments.
311 if (vararg_count > 0 && arg_to_add > 0)
312 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100313
314 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200315 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200316 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200317 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318
319 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100320 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
321 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200322 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200323 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
324 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100325 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100326 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200327 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328
329 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200330 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200332 if (dfunc->df_has_closure)
333 {
334 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
335
336 tv->v_type = VAR_NUMBER;
337 tv->vval.v_number = 0;
338 }
339 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100341 if (pt != NULL || ufunc->uf_partial != NULL
342 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100343 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200344 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100345
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200346 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100347 return FAIL;
348 if (pt != NULL)
349 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200350 ref->or_outer = &pt->pt_outer;
351 ++pt->pt_refcount;
352 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100353 }
354 else if (ufunc->uf_partial != NULL)
355 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200356 ref->or_outer = &ufunc->uf_partial->pt_outer;
357 ++ufunc->uf_partial->pt_refcount;
358 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100359 }
360 else
361 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200362 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200363 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200364 {
365 vim_free(ref);
366 return FAIL;
367 }
368 ref->or_outer_allocated = TRUE;
369 ref->or_outer->out_stack = &ectx->ec_stack;
370 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
371 if (ectx->ec_outer_ref != NULL)
372 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100373 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200374 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100375 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100376 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200377 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100378
Bram Moolenaarc970e422021-03-17 15:03:04 +0100379 ++ufunc->uf_calls;
380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100381 // Set execution state to the start of the called function.
382 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100383 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100384 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200385 if (entry != NULL)
386 {
387 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200388 // Save the current context so it can be restored on return.
389 entry->es_save_sctx = current_sctx;
390 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200391 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100392
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200393 // Start execution at the first instruction.
394 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395
396 return OK;
397}
398
399// Get pointer to item in the stack.
400#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
401
402/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 * Used when returning from a function: Check if any closure is still
404 * referenced. If so then move the arguments and variables to a separate piece
405 * of stack to be used when the closure is called.
406 * When "free_arguments" is TRUE the arguments are to be freed.
407 * Returns FAIL when out of memory.
408 */
409 static int
410handle_closure_in_use(ectx_T *ectx, int free_arguments)
411{
412 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
413 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200414 int argcount;
415 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200416 int idx;
417 typval_T *tv;
418 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200419 garray_T *gap = &ectx->ec_funcrefs;
420 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200421
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200422 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200423 return OK; // function was freed
424 if (dfunc->df_has_closure == 0)
425 return OK; // no closures
426 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
427 closure_count = tv->vval.v_number;
428 if (closure_count == 0)
429 return OK; // no funcrefs created
430
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200431 argcount = ufunc_argcount(dfunc->df_ufunc);
432 top = ectx->ec_frame_idx - argcount;
433
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200434 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200435 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200436 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200437 partial_T *pt;
438 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200439
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200440 if (off < 0)
441 continue; // count is off or already done
442 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200443 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200444 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200445 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200446 int i;
447
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200448 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200449 // unreferenced on return.
450 for (i = 0; i < dfunc->df_varcount; ++i)
451 {
452 typval_T *stv = STACK_TV(ectx->ec_frame_idx
453 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200454 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200455 --refcount;
456 }
457 if (refcount > 1)
458 {
459 closure_in_use = TRUE;
460 break;
461 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200462 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200463 }
464
465 if (closure_in_use)
466 {
467 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
468 typval_T *stack;
469
470 // A closure is using the arguments and/or local variables.
471 // Move them to the called function.
472 if (funcstack == NULL)
473 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200474 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
475 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200476 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
477 funcstack->fs_ga.ga_data = stack;
478 if (stack == NULL)
479 {
480 vim_free(funcstack);
481 return FAIL;
482 }
483
484 // Move or copy the arguments.
485 for (idx = 0; idx < argcount; ++idx)
486 {
487 tv = STACK_TV(top + idx);
488 if (free_arguments)
489 {
490 *(stack + idx) = *tv;
491 tv->v_type = VAR_UNKNOWN;
492 }
493 else
494 copy_tv(tv, stack + idx);
495 }
496 // Move the local variables.
497 for (idx = 0; idx < dfunc->df_varcount; ++idx)
498 {
499 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200500
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200501 // A partial created for a local function, that is also used as a
502 // local variable, has a reference count for the variable, thus
503 // will never go down to zero. When all these refcounts are one
504 // then the funcstack is unused. We need to count how many we have
505 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200506 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
507 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200508 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200509
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200510 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200511 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
512 gap->ga_len - closure_count + i])
513 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200514 }
515
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200516 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200517 tv->v_type = VAR_UNKNOWN;
518 }
519
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200520 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200521 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200522 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
523 - closure_count + idx];
524 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200525 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200526 ++funcstack->fs_refcount;
527 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100528 pt->pt_outer.out_stack = &funcstack->fs_ga;
529 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 }
531 }
532 }
533
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534 for (idx = 0; idx < closure_count; ++idx)
535 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
536 - closure_count + idx]);
537 gap->ga_len -= closure_count;
538 if (gap->ga_len == 0)
539 ga_clear(gap);
540
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200541 return OK;
542}
543
544/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200545 * Called when a partial is freed or its reference count goes down to one. The
546 * funcstack may be the only reference to the partials in the local variables.
547 * Go over all of them, the funcref and can be freed if all partials
548 * referencing the funcstack have a reference count of one.
549 */
550 void
551funcstack_check_refcount(funcstack_T *funcstack)
552{
553 int i;
554 garray_T *gap = &funcstack->fs_ga;
555 int done = 0;
556
557 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
558 return;
559 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
560 {
561 typval_T *tv = ((typval_T *)gap->ga_data) + i;
562
563 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
564 && tv->vval.v_partial->pt_funcstack == funcstack
565 && tv->vval.v_partial->pt_refcount == 1)
566 ++done;
567 }
568 if (done == funcstack->fs_min_refcount)
569 {
570 typval_T *stack = gap->ga_data;
571
572 // All partials referencing the funcstack have a reference count of
573 // one, thus the funcstack is no longer of use.
574 for (i = 0; i < gap->ga_len; ++i)
575 clear_tv(stack + i);
576 vim_free(stack);
577 vim_free(funcstack);
578 }
579}
580
581/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 * Return from the current function.
583 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200584 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200585func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100588 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200589 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
590 + ectx->ec_dfunc_idx;
591 int argcount = ufunc_argcount(dfunc->df_ufunc);
592 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200593 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100594 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
595 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200596 funclocal_T *floc;
597#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100598 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
599 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600
Bram Moolenaar12d26532021-02-19 19:13:21 +0100601 if (do_profiling == PROF_YES)
602 {
603 ufunc_T *caller = prev_dfunc->df_ufunc;
604
605 if (dfunc->df_ufunc->uf_profiling
606 || (caller != NULL && caller->uf_profiling))
607 {
608 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
609 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
610 --profile_info_ga.ga_len;
611 }
612 }
613#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100614 // TODO: when is it safe to delete the function when it is no longer used?
615 --dfunc->df_ufunc->uf_calls;
616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200618 entry = estack_pop();
619 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200620 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622 if (handle_closure_in_use(ectx, TRUE) == FAIL)
623 return FAIL;
624
625 // Clear the arguments.
626 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
627 clear_tv(STACK_TV(idx));
628
629 // Clear local variables and temp values, but not the return value.
630 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100631 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100633
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100634 // The return value should be on top of the stack. However, when aborting
635 // it may not be there and ec_frame_idx is the top of the stack.
636 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100637 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100638 ret_idx = 0;
639
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200640 if (ectx->ec_outer_ref != NULL)
641 {
642 if (ectx->ec_outer_ref->or_outer_allocated)
643 vim_free(ectx->ec_outer_ref->or_outer);
644 partial_unref(ectx->ec_outer_ref->or_partial);
645 vim_free(ectx->ec_outer_ref);
646 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100647
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100648 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100649 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100650 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
651 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200652 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
653 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200654 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100655 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100656 floc = (void *)STACK_TV(ectx->ec_frame_idx
657 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200658 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100659 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
660 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200661
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100662 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200663 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100664 else
665 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200666 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100667 vim_free(floc);
668 }
669
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100670 if (ret_idx > 0)
671 {
672 // Reset the stack to the position before the call, with a spot for the
673 // return value, moved there from above the frame.
674 ectx->ec_stack.ga_len = top + 1;
675 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
676 }
677 else
678 // Reset the stack to the position before the call.
679 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200680
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100681 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200682 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200683 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684}
685
686#undef STACK_TV
687
688/*
689 * Prepare arguments and rettv for calling a builtin or user function.
690 */
691 static int
692call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
693{
694 int idx;
695 typval_T *tv;
696
697 // Move arguments from bottom of the stack to argvars[] and add terminator.
698 for (idx = 0; idx < argcount; ++idx)
699 argvars[idx] = *STACK_TV_BOT(idx - argcount);
700 argvars[argcount].v_type = VAR_UNKNOWN;
701
702 // Result replaces the arguments on the stack.
703 if (argcount > 0)
704 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200705 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 return FAIL;
707 else
708 ++ectx->ec_stack.ga_len;
709
710 // Default return value is zero.
711 tv = STACK_TV_BOT(-1);
712 tv->v_type = VAR_NUMBER;
713 tv->vval.v_number = 0;
714
715 return OK;
716}
717
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200718// Ugly global to avoid passing the execution context around through many
719// layers.
720static ectx_T *current_ectx = NULL;
721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722/*
723 * Call a builtin function by index.
724 */
725 static int
726call_bfunc(int func_idx, int argcount, ectx_T *ectx)
727{
728 typval_T argvars[MAX_FUNC_ARGS];
729 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100730 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200731 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200732 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733
734 if (call_prepare(argcount, argvars, ectx) == FAIL)
735 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200736 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200738 // Call the builtin function. Set "current_ectx" so that when it
739 // recursively invokes call_def_function() a closure context can be set.
740 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200742 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200743 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744
745 // Clear the arguments.
746 for (idx = 0; idx < argcount; ++idx)
747 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200748
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100749 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200750 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 return OK;
752}
753
754/*
755 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100756 * If the function is compiled this will add a stack frame and set the
757 * instruction pointer at the start of the function.
758 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200759 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100760 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 */
762 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100763call_ufunc(
764 ufunc_T *ufunc,
765 partial_T *pt,
766 int argcount,
767 ectx_T *ectx,
768 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769{
770 typval_T argvars[MAX_FUNC_ARGS];
771 funcexe_T funcexe;
772 int error;
773 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100774 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200775 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776
Bram Moolenaare99d4222021-06-13 14:01:26 +0200777 if (func_needs_compiling(ufunc, compile_type)
778 && compile_def_function(ufunc, FALSE, compile_type, NULL)
779 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200780 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200781 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100782 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100783 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100784 if (error != FCERR_UNKNOWN)
785 {
786 if (error == FCERR_TOOMANY)
787 semsg(_(e_toomanyarg), ufunc->uf_name);
788 else
789 semsg(_(e_toofewarg), ufunc->uf_name);
790 return FAIL;
791 }
792
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100793 // The function has been compiled, can call it quickly. For a function
794 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100795 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100796 if (iptr != NULL)
797 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100798 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100799 iptr->isn_type = ISN_DCALL;
800 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
801 iptr->isn_arg.dfunc.cdf_argcount = argcount;
802 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200803 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805
806 if (call_prepare(argcount, argvars, ectx) == FAIL)
807 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200808 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 funcexe.evaluate = TRUE;
810
811 // Call the user function. Result goes in last position on the stack.
812 // TODO: add selfdict if there is one
813 error = call_user_func_check(ufunc, argcount, argvars,
814 STACK_TV_BOT(-1), &funcexe, NULL);
815
816 // Clear the arguments.
817 for (idx = 0; idx < argcount; ++idx)
818 clear_tv(&argvars[idx]);
819
820 if (error != FCERR_NONE)
821 {
822 user_func_error(error, ufunc->uf_name);
823 return FAIL;
824 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100825 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200826 // Error other than from calling the function itself.
827 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828 return OK;
829}
830
831/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100832 * If command modifiers were applied restore them.
833 */
834 static void
835may_restore_cmdmod(funclocal_T *funclocal)
836{
837 if (funclocal->floc_restore_cmdmod)
838 {
839 cmdmod.cmod_filter_regmatch.regprog = NULL;
840 undo_cmdmod(&cmdmod);
841 cmdmod = funclocal->floc_save_cmdmod;
842 funclocal->floc_restore_cmdmod = FALSE;
843 }
844}
845
846/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200847 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
848 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200849 */
850 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200851vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200852{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200853 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200854}
855
856/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 * Execute a function by "name".
858 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100859 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 * Returns FAIL if not found without an error message.
861 */
862 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100863call_by_name(
864 char_u *name,
865 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100866 ectx_T *ectx,
867 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868{
869 ufunc_T *ufunc;
870
871 if (builtin_function(name, -1))
872 {
873 int func_idx = find_internal_func(name);
874
875 if (func_idx < 0)
876 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200877 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 return FAIL;
879 return call_bfunc(func_idx, argcount, ectx);
880 }
881
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200882 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200883
884 if (ufunc == NULL)
885 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200886 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +0200887
888 if (script_autoload(name, TRUE))
889 // loaded a package, search for the function again
890 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200891
892 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +0200893 return FAIL; // bail out if loading the script caused an error
894 }
895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100897 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200898 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100899 {
900 int i;
901 typval_T *argv = STACK_TV_BOT(0) - argcount;
902
903 // The function can change at runtime, check that the argument
904 // types are correct.
905 for (i = 0; i < argcount; ++i)
906 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100907 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100908
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200909 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100910 type = ufunc->uf_arg_types[i];
911 else if (ufunc->uf_va_type != NULL)
912 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100913 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200914 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100915 return FAIL;
916 }
917 }
918
Bram Moolenaar4c137212021-04-19 16:48:48 +0200919 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921
922 return FAIL;
923}
924
925 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100926call_partial(
927 typval_T *tv,
928 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100929 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200931 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200932 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100934 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935
936 if (tv->v_type == VAR_PARTIAL)
937 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200938 partial_T *pt = tv->vval.v_partial;
939 int i;
940
941 if (pt->pt_argc > 0)
942 {
943 // Make space for arguments from the partial, shift the "argcount"
944 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +0200945 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +0200946 return FAIL;
947 for (i = 1; i <= argcount; ++i)
948 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
949 ectx->ec_stack.ga_len += pt->pt_argc;
950 argcount += pt->pt_argc;
951
952 // copy the arguments from the partial onto the stack
953 for (i = 0; i < pt->pt_argc; ++i)
954 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956
957 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200958 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 name = pt->pt_name;
961 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200962 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200964 if (name != NULL)
965 {
966 char_u fname_buf[FLEN_FIXED + 1];
967 char_u *tofree = NULL;
968 int error = FCERR_NONE;
969 char_u *fname;
970
971 // May need to translate <SNR>123_ to K_SNR.
972 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
973 if (error != FCERR_NONE)
974 res = FAIL;
975 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200976 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200977 vim_free(tofree);
978 }
979
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100980 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 {
982 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200983 semsg(_(e_unknownfunc),
984 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 return FAIL;
986 }
987 return OK;
988}
989
990/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200991 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
992 * TRUE.
993 */
994 static int
995error_if_locked(int lock, char *error)
996{
997 if (lock & (VAR_LOCKED | VAR_FIXED))
998 {
999 emsg(_(error));
1000 return TRUE;
1001 }
1002 return FALSE;
1003}
1004
1005/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001006 * Give an error if "tv" is not a number and return FAIL.
1007 */
1008 static int
1009check_for_number(typval_T *tv)
1010{
1011 if (tv->v_type != VAR_NUMBER)
1012 {
1013 semsg(_(e_expected_str_but_got_str),
1014 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1015 return FAIL;
1016 }
1017 return OK;
1018}
1019
1020/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001021 * Store "tv" in variable "name".
1022 * This is for s: and g: variables.
1023 */
1024 static void
1025store_var(char_u *name, typval_T *tv)
1026{
1027 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001028 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001029
Bram Moolenaard877a572021-04-01 19:42:48 +02001030 if (tv->v_lock)
1031 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001032 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001033 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001034 restore_funccal();
1035}
1036
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001037/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001038 * Convert "tv" to a string.
1039 * Return FAIL if not allowed.
1040 */
1041 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001042do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001043{
1044 if (tv->v_type != VAR_STRING)
1045 {
1046 char_u *str;
1047
1048 if (is_2string_any)
1049 {
1050 switch (tv->v_type)
1051 {
1052 case VAR_SPECIAL:
1053 case VAR_BOOL:
1054 case VAR_NUMBER:
1055 case VAR_FLOAT:
1056 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001057
1058 case VAR_LIST:
1059 if (tolerant)
1060 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001061 char_u *s, *e, *p;
1062 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001063
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001064 ga_init2(&ga, sizeof(char_u *), 1);
1065
1066 // Convert to NL separated items, then
1067 // escape the items and replace the NL with
1068 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001069 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001070 if (str == NULL)
1071 return FAIL;
1072 s = str;
1073 while ((e = vim_strchr(s, '\n')) != NULL)
1074 {
1075 *e = NUL;
1076 p = vim_strsave_fnameescape(s, FALSE);
1077 if (p != NULL)
1078 {
1079 ga_concat(&ga, p);
1080 ga_concat(&ga, (char_u *)" ");
1081 vim_free(p);
1082 }
1083 s = e + 1;
1084 }
1085 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001086 clear_tv(tv);
1087 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001088 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001089 return OK;
1090 }
1091 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001092 default: to_string_error(tv->v_type);
1093 return FAIL;
1094 }
1095 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001096 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001097 clear_tv(tv);
1098 tv->v_type = VAR_STRING;
1099 tv->vval.v_string = str;
1100 }
1101 return OK;
1102}
1103
1104/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001105 * When the value of "sv" is a null list of dict, allocate it.
1106 */
1107 static void
1108allocate_if_null(typval_T *tv)
1109{
1110 switch (tv->v_type)
1111 {
1112 case VAR_LIST:
1113 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001114 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001115 break;
1116 case VAR_DICT:
1117 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001118 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001119 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001120 case VAR_BLOB:
1121 if (tv->vval.v_blob == NULL)
1122 (void)rettv_blob_alloc(tv);
1123 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001124 default:
1125 break;
1126 }
1127}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001128
Bram Moolenaare7525c52021-01-09 13:20:37 +01001129/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001130 * Return the character "str[index]" where "index" is the character index,
1131 * including composing characters.
1132 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001133 */
1134 char_u *
1135char_from_string(char_u *str, varnumber_T index)
1136{
1137 size_t nbyte = 0;
1138 varnumber_T nchar = index;
1139 size_t slen;
1140
1141 if (str == NULL)
1142 return NULL;
1143 slen = STRLEN(str);
1144
Bram Moolenaarff871402021-03-26 13:34:05 +01001145 // Do the same as for a list: a negative index counts from the end.
1146 // Optimization to check the first byte to be below 0x80 (and no composing
1147 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001148 if (index < 0)
1149 {
1150 int clen = 0;
1151
1152 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001153 {
1154 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1155 ++nbyte;
1156 else if (enc_utf8)
1157 nbyte += utfc_ptr2len(str + nbyte);
1158 else
1159 nbyte += mb_ptr2len(str + nbyte);
1160 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001161 nchar = clen + index;
1162 if (nchar < 0)
1163 // unlike list: index out of range results in empty string
1164 return NULL;
1165 }
1166
1167 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001168 {
1169 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1170 ++nbyte;
1171 else if (enc_utf8)
1172 nbyte += utfc_ptr2len(str + nbyte);
1173 else
1174 nbyte += mb_ptr2len(str + nbyte);
1175 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001176 if (nbyte >= slen)
1177 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001178 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001179}
1180
1181/*
1182 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001183 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001184 * If going over the end return "str_len".
1185 * If "idx" is negative count from the end, -1 is the last character.
1186 * When going over the start return -1.
1187 */
1188 static long
1189char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1190{
1191 varnumber_T nchar = idx;
1192 size_t nbyte = 0;
1193
1194 if (nchar >= 0)
1195 {
1196 while (nchar > 0 && nbyte < str_len)
1197 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001198 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001199 --nchar;
1200 }
1201 }
1202 else
1203 {
1204 nbyte = str_len;
1205 while (nchar < 0 && nbyte > 0)
1206 {
1207 --nbyte;
1208 nbyte -= mb_head_off(str, str + nbyte);
1209 ++nchar;
1210 }
1211 if (nchar < 0)
1212 return -1;
1213 }
1214 return (long)nbyte;
1215}
1216
1217/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001218 * Return the slice "str[first : last]" using character indexes. Composing
1219 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001220 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001221 * Return NULL when the result is empty.
1222 */
1223 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001224string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001225{
1226 long start_byte, end_byte;
1227 size_t slen;
1228
1229 if (str == NULL)
1230 return NULL;
1231 slen = STRLEN(str);
1232 start_byte = char_idx2byte(str, slen, first);
1233 if (start_byte < 0)
1234 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001235 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001236 end_byte = (long)slen;
1237 else
1238 {
1239 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001240 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001241 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001242 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001243 }
1244
1245 if (start_byte >= (long)slen || end_byte <= start_byte)
1246 return NULL;
1247 return vim_strnsave(str + start_byte, end_byte - start_byte);
1248}
1249
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001250/*
1251 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1252 * When "dfunc_idx" is negative don't give an error.
1253 * Returns NULL for an error.
1254 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001255 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001256get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001257{
1258 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001259 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1260 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001261 svar_T *sv;
1262
1263 if (sref->sref_seq != si->sn_script_seq)
1264 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001265 // The script was reloaded after the function was compiled, the
1266 // script_idx may not be valid.
1267 if (dfunc != NULL)
1268 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1269 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001270 return NULL;
1271 }
1272 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001273 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001274 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001275 if (dfunc != NULL)
1276 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001277 return NULL;
1278 }
1279 return sv;
1280}
1281
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001282/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001283 * Function passed to do_cmdline() for splitting a script joined by NL
1284 * characters.
1285 */
1286 static char_u *
1287get_split_sourceline(
1288 int c UNUSED,
1289 void *cookie,
1290 int indent UNUSED,
1291 getline_opt_T options UNUSED)
1292{
1293 source_cookie_T *sp = (source_cookie_T *)cookie;
1294 char_u *p;
1295 char_u *line;
1296
1297 if (*sp->nextline == NUL)
1298 return NULL;
1299 p = vim_strchr(sp->nextline, '\n');
1300 if (p == NULL)
1301 {
1302 line = vim_strsave(sp->nextline);
1303 sp->nextline += STRLEN(sp->nextline);
1304 }
1305 else
1306 {
1307 line = vim_strnsave(sp->nextline, p - sp->nextline);
1308 sp->nextline = p + 1;
1309 }
1310 return line;
1311}
1312
1313/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 * Execute a function by "name".
1315 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001316 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 */
1318 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001319call_eval_func(
1320 char_u *name,
1321 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001322 ectx_T *ectx,
1323 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324{
Bram Moolenaared677f52020-08-12 16:38:10 +02001325 int called_emsg_before = called_emsg;
1326 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
Bram Moolenaar4c137212021-04-19 16:48:48 +02001328 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001329 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001331 dictitem_T *v;
1332
1333 v = find_var(name, NULL, FALSE);
1334 if (v == NULL)
1335 {
1336 semsg(_(e_unknownfunc), name);
1337 return FAIL;
1338 }
1339 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1340 {
1341 semsg(_(e_unknownfunc), name);
1342 return FAIL;
1343 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001344 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001346 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347}
1348
1349/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001350 * When a function reference is used, fill a partial with the information
1351 * needed, especially when it is used as a closure.
1352 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001353 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001354fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1355{
1356 pt->pt_func = ufunc;
1357 pt->pt_refcount = 1;
1358
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001359 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001360 {
1361 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1362 + ectx->ec_dfunc_idx;
1363
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001364 // The closure may need to find arguments and local variables in the
1365 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001366 pt->pt_outer.out_stack = &ectx->ec_stack;
1367 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001368 if (ectx->ec_outer_ref != NULL)
1369 {
1370 // The current context already has a context, link to that one.
1371 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1372 if (ectx->ec_outer_ref->or_partial != NULL)
1373 {
1374 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1375 ++pt->pt_outer.out_up_partial->pt_refcount;
1376 }
1377 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001378
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001379 // If this function returns and the closure is still being used, we
1380 // need to make a copy of the context (arguments and local variables).
1381 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001382 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001383 {
1384 vim_free(pt);
1385 return FAIL;
1386 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001387 // Extra variable keeps the count of closures created in the current
1388 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001389 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1390 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1391
1392 ((partial_T **)ectx->ec_funcrefs.ga_data)
1393 [ectx->ec_funcrefs.ga_len] = pt;
1394 ++pt->pt_refcount;
1395 ++ectx->ec_funcrefs.ga_len;
1396 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001397 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001398 return OK;
1399}
1400
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001401/*
1402 * Execute iptr->isn_arg.string as an Ex command.
1403 */
1404 static int
1405exec_command(isn_T *iptr)
1406{
1407 source_cookie_T cookie;
1408
1409 SOURCING_LNUM = iptr->isn_lnum;
1410 // Pass getsourceline to get an error for a missing ":end"
1411 // command.
1412 CLEAR_FIELD(cookie);
1413 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1414 if (do_cmdline(iptr->isn_arg.string,
1415 getsourceline, &cookie,
1416 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1417 || did_emsg)
1418 return FAIL;
1419 return OK;
1420}
1421
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001422// used for v_instr of typval of VAR_INSTR
1423struct instr_S {
1424 ectx_T *instr_ectx;
1425 isn_T *instr_instr;
1426};
1427
Bram Moolenaar4c137212021-04-19 16:48:48 +02001428// used for substitute_instr
1429typedef struct subs_expr_S {
1430 ectx_T *subs_ectx;
1431 isn_T *subs_instr;
1432 int subs_status;
1433} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434
1435// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001436#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437
1438// Get pointer to item at the bottom of the stack, -1 is the bottom.
1439#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001440#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 +01001441
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001442// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001443#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 +01001444
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001445// Set when calling do_debug().
1446static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001447static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001448
1449/*
1450 * When debugging lookup "name" and return the typeval.
1451 * When not found return NULL.
1452 */
1453 typval_T *
1454lookup_debug_var(char_u *name)
1455{
1456 int idx;
1457 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001458 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001459 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001460 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001461
1462 if (ectx == NULL)
1463 return NULL;
1464 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1465
1466 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001467 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001468 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001469 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001470 return STACK_TV_VAR(idx);
1471 }
1472
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001473 // Go through argument names.
1474 ufunc = dfunc->df_ufunc;
1475 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1476 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1477 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1478 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1479 - varargs_off + idx);
1480 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1481 return STACK_TV(ectx->ec_frame_idx - 1);
1482
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001483 return NULL;
1484}
1485
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001486 static void
1487handle_debug(isn_T *iptr, ectx_T *ectx)
1488{
1489 char_u *line;
1490 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1491 + ectx->ec_dfunc_idx)->df_ufunc;
1492 isn_T *ni;
1493 int end_lnum = iptr->isn_lnum;
1494 garray_T ga;
1495 int lnum;
1496
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001497 if (ex_nesting_level > debug_break_level)
1498 {
1499 linenr_T breakpoint;
1500
1501 if (!ufunc->uf_has_breakpoint)
1502 return;
1503
1504 // check for the next breakpoint if needed
1505 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001506 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001507 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1508 return;
1509 }
1510
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001511 SOURCING_LNUM = iptr->isn_lnum;
1512 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001513 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001514
1515 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1516 if (ni->isn_type == ISN_DEBUG
1517 || ni->isn_type == ISN_RETURN
1518 || ni->isn_type == ISN_RETURN_VOID)
1519 {
1520 end_lnum = ni->isn_lnum;
1521 break;
1522 }
1523
1524 if (end_lnum > iptr->isn_lnum)
1525 {
1526 ga_init2(&ga, sizeof(char_u *), 10);
1527 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001528 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001529 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001530
Bram Moolenaar303215d2021-07-07 20:10:43 +02001531 if (p == NULL)
1532 continue; // left over from continuation line
1533 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001534 if (*p == '#')
1535 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001536 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001537 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1538 if (STRNCMP(p, "def ", 4) == 0)
1539 break;
1540 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001541 line = ga_concat_strings(&ga, " ");
1542 vim_free(ga.ga_data);
1543 }
1544 else
1545 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001546
Dominique Pelle6e969552021-06-17 13:53:41 +02001547 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001548 debug_context = NULL;
1549
1550 if (end_lnum > iptr->isn_lnum)
1551 vim_free(line);
1552}
1553
Bram Moolenaar4c137212021-04-19 16:48:48 +02001554/*
1555 * Execute instructions in execution context "ectx".
1556 * Return OK or FAIL;
1557 */
1558 static int
1559exec_instructions(ectx_T *ectx)
1560{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001561 int ret = FAIL;
1562 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001563
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001564 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001565 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001566
Bram Moolenaarff652882021-05-16 15:24:49 +02001567 // Only catch exceptions in this instruction list.
1568 ectx->ec_trylevel_at_start = trylevel;
1569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 for (;;)
1571 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001572 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001574 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001575
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001576 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001577 {
1578 line_breakcheck();
1579 breakcheck_count = 0;
1580 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001581 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001582 {
1583 // Turn CTRL-C into an exception.
1584 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001585 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001586 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001587 did_throw = TRUE;
1588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001590 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001591 {
1592 // Turn an error message into an exception.
1593 did_emsg = FALSE;
1594 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001595 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001596 did_throw = TRUE;
1597 *msg_list = NULL;
1598 }
1599
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001600 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001602 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001603 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001604 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605
1606 // An exception jumps to the first catch, finally, or returns from
1607 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001608 while (index > 0)
1609 {
1610 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001611 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001612 break;
1613 // In the catch and finally block of this try we have to go up
1614 // one level.
1615 --index;
1616 trycmd = NULL;
1617 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001618 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001620 if (trycmd->tcd_in_catch)
1621 {
1622 // exception inside ":catch", jump to ":finally" once
1623 ectx->ec_iidx = trycmd->tcd_finally_idx;
1624 trycmd->tcd_finally_idx = 0;
1625 }
1626 else
1627 // jump to first ":catch"
1628 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001629 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001630 did_throw = FALSE; // don't come back here until :endtry
1631 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 }
1633 else
1634 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001635 // Not inside try or need to return from current functions.
1636 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001637 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001638 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001639 tv = STACK_TV_BOT(0);
1640 tv->v_type = VAR_NUMBER;
1641 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001642 ++ectx->ec_stack.ga_len;
1643 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001645 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001646 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001647 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001648 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 goto done;
1650 }
1651
Bram Moolenaar4c137212021-04-19 16:48:48 +02001652 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001653 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 }
1655 continue;
1656 }
1657
Bram Moolenaar4c137212021-04-19 16:48:48 +02001658 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 switch (iptr->isn_type)
1660 {
1661 // execute Ex command line
1662 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001663 if (exec_command(iptr) == FAIL)
1664 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 break;
1666
Bram Moolenaar20677332021-06-06 17:02:53 +02001667 // execute Ex command line split at NL characters.
1668 case ISN_EXEC_SPLIT:
1669 {
1670 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001671 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001672
1673 SOURCING_LNUM = iptr->isn_lnum;
1674 CLEAR_FIELD(cookie);
1675 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1676 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001677 line = get_split_sourceline(0, &cookie, 0, 0);
1678 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001679 get_split_sourceline, &cookie,
1680 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1681 == FAIL
1682 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001683 {
1684 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001685 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001686 }
1687 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001688 }
1689 break;
1690
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001691 // Evaluate an expression with legacy syntax, push it onto the
1692 // stack.
1693 case ISN_LEGACY_EVAL:
1694 {
1695 char_u *arg = iptr->isn_arg.string;
1696 int res;
1697 int save_flags = cmdmod.cmod_flags;
1698
Bram Moolenaar35578162021-08-02 19:10:38 +02001699 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001700 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001701 tv = STACK_TV_BOT(0);
1702 init_tv(tv);
1703 cmdmod.cmod_flags |= CMOD_LEGACY;
1704 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1705 cmdmod.cmod_flags = save_flags;
1706 if (res == FAIL)
1707 goto on_error;
1708 ++ectx->ec_stack.ga_len;
1709 }
1710 break;
1711
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001712 // push typeval VAR_INSTR with instructions to be executed
1713 case ISN_INSTR:
1714 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001715 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001716 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001717 tv = STACK_TV_BOT(0);
1718 tv->vval.v_instr = ALLOC_ONE(instr_T);
1719 if (tv->vval.v_instr == NULL)
1720 goto on_error;
1721 ++ectx->ec_stack.ga_len;
1722
1723 tv->v_type = VAR_INSTR;
1724 tv->vval.v_instr->instr_ectx = ectx;
1725 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1726 }
1727 break;
1728
Bram Moolenaar4c137212021-04-19 16:48:48 +02001729 // execute :substitute with an expression
1730 case ISN_SUBSTITUTE:
1731 {
1732 subs_T *subs = &iptr->isn_arg.subs;
1733 source_cookie_T cookie;
1734 struct subs_expr_S *save_instr = substitute_instr;
1735 struct subs_expr_S subs_instr;
1736 int res;
1737
1738 subs_instr.subs_ectx = ectx;
1739 subs_instr.subs_instr = subs->subs_instr;
1740 subs_instr.subs_status = OK;
1741 substitute_instr = &subs_instr;
1742
1743 SOURCING_LNUM = iptr->isn_lnum;
1744 // This is very much like ISN_EXEC
1745 CLEAR_FIELD(cookie);
1746 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1747 res = do_cmdline(subs->subs_cmd,
1748 getsourceline, &cookie,
1749 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1750 substitute_instr = save_instr;
1751
1752 if (res == FAIL || did_emsg
1753 || subs_instr.subs_status == FAIL)
1754 goto on_error;
1755 }
1756 break;
1757
1758 case ISN_FINISH:
1759 goto done;
1760
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001761 case ISN_REDIRSTART:
1762 // create a dummy entry for var_redir_str()
1763 if (alloc_redir_lval() == FAIL)
1764 goto on_error;
1765
1766 // The output is stored in growarray "redir_ga" until
1767 // redirection ends.
1768 init_redir_ga();
1769 redir_vname = 1;
1770 break;
1771
1772 case ISN_REDIREND:
1773 {
1774 char_u *res = get_clear_redir_ga();
1775
1776 // End redirection, put redirected text on the stack.
1777 clear_redir_lval();
1778 redir_vname = 0;
1779
Bram Moolenaar35578162021-08-02 19:10:38 +02001780 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001781 {
1782 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001783 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001784 }
1785 tv = STACK_TV_BOT(0);
1786 tv->v_type = VAR_STRING;
1787 tv->vval.v_string = res;
1788 ++ectx->ec_stack.ga_len;
1789 }
1790 break;
1791
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001792 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001793#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001794 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1795 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001796#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001797 break;
1798
1799 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001800#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001801 {
1802 exarg_T ea;
1803 int res;
1804
1805 CLEAR_FIELD(ea);
1806 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1807 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1808 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1809 --ectx->ec_stack.ga_len;
1810 tv = STACK_TV_BOT(0);
1811 res = cexpr_core(&ea, tv);
1812 clear_tv(tv);
1813 if (res == FAIL)
1814 goto on_error;
1815 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001816#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001817 break;
1818
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001819 // execute Ex command from pieces on the stack
1820 case ISN_EXECCONCAT:
1821 {
1822 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001823 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001824 int pass;
1825 int i;
1826 char_u *cmd = NULL;
1827 char_u *str;
1828
1829 for (pass = 1; pass <= 2; ++pass)
1830 {
1831 for (i = 0; i < count; ++i)
1832 {
1833 tv = STACK_TV_BOT(i - count);
1834 str = tv->vval.v_string;
1835 if (str != NULL && *str != NUL)
1836 {
1837 if (pass == 2)
1838 STRCPY(cmd + len, str);
1839 len += STRLEN(str);
1840 }
1841 if (pass == 2)
1842 clear_tv(tv);
1843 }
1844 if (pass == 1)
1845 {
1846 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001847 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001848 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001849 len = 0;
1850 }
1851 }
1852
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001853 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001854 do_cmdline_cmd(cmd);
1855 vim_free(cmd);
1856 }
1857 break;
1858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 // execute :echo {string} ...
1860 case ISN_ECHO:
1861 {
1862 int count = iptr->isn_arg.echo.echo_count;
1863 int atstart = TRUE;
1864 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001865 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866
1867 for (idx = 0; idx < count; ++idx)
1868 {
1869 tv = STACK_TV_BOT(idx - count);
1870 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1871 &atstart, &needclr);
1872 clear_tv(tv);
1873 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001874 if (needclr)
1875 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001876 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 }
1878 break;
1879
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001880 // :execute {string} ...
1881 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02001882 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001883 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001884 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001885 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02001886 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001887 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001888 {
1889 int count = iptr->isn_arg.number;
1890 garray_T ga;
1891 char_u buf[NUMBUFLEN];
1892 char_u *p;
1893 int len;
1894 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001895 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001896
1897 ga_init2(&ga, 1, 80);
1898 for (idx = 0; idx < count; ++idx)
1899 {
1900 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001901 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001902 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001903 if (tv->v_type == VAR_CHANNEL
1904 || tv->v_type == VAR_JOB)
1905 {
1906 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001907 semsg(_(e_using_invalid_value_as_string_str),
1908 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001909 break;
1910 }
1911 else
1912 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001913 }
1914 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001915 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001916
1917 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02001918 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01001919 failed = TRUE;
1920 else
1921 {
1922 if (ga.ga_len > 0)
1923 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1924 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1925 ga.ga_len += len;
1926 }
1927 clear_tv(tv);
1928 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001929 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001930 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001931 {
1932 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001933 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001934 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001935
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001936 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001937 {
1938 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001939 {
1940 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001941 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001942 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001943 {
1944 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001945 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001946 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001947 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001948 else
1949 {
1950 msg_sb_eol();
1951 if (iptr->isn_type == ISN_ECHOMSG)
1952 {
1953 msg_attr(ga.ga_data, echo_attr);
1954 out_flush();
1955 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02001956 else if (iptr->isn_type == ISN_ECHOCONSOLE)
1957 {
1958 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
1959 TRUE);
1960 ui_write((char_u *)"\r\n", 2, TRUE);
1961 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001962 else
1963 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001964 SOURCING_LNUM = iptr->isn_lnum;
1965 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001966 }
1967 }
1968 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001969 ga_clear(&ga);
1970 }
1971 break;
1972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 // load local variable or argument
1974 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02001975 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001976 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001978 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 break;
1980
1981 // load v: variable
1982 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02001983 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001984 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001986 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 break;
1988
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001989 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 case ISN_LOADSCRIPT:
1991 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001992 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 svar_T *sv;
1994
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001995 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001996 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001997 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001998 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02001999 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002000 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002002 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 }
2004 break;
2005
2006 // load s: variable in old script
2007 case ISN_LOADS:
2008 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002009 hashtab_T *ht = &SCRIPT_VARS(
2010 iptr->isn_arg.loadstore.ls_sid);
2011 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 if (di == NULL)
2015 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002016 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002017 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002018 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 }
2020 else
2021 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002022 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002023 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002025 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 }
2027 }
2028 break;
2029
Bram Moolenaard3aac292020-04-19 14:32:17 +02002030 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002032 case ISN_LOADB:
2033 case ISN_LOADW:
2034 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002036 dictitem_T *di = NULL;
2037 hashtab_T *ht = NULL;
2038 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002039
Bram Moolenaard3aac292020-04-19 14:32:17 +02002040 switch (iptr->isn_type)
2041 {
2042 case ISN_LOADG:
2043 ht = get_globvar_ht();
2044 namespace = 'g';
2045 break;
2046 case ISN_LOADB:
2047 ht = &curbuf->b_vars->dv_hashtab;
2048 namespace = 'b';
2049 break;
2050 case ISN_LOADW:
2051 ht = &curwin->w_vars->dv_hashtab;
2052 namespace = 'w';
2053 break;
2054 case ISN_LOADT:
2055 ht = &curtab->tp_vars->dv_hashtab;
2056 namespace = 't';
2057 break;
2058 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002059 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002060 }
2061 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 if (di == NULL)
2064 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002065 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002066 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002067 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002068 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 }
2070 else
2071 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002072 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002073 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002075 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 }
2077 }
2078 break;
2079
Bram Moolenaar03290b82020-12-19 16:30:44 +01002080 // load autoload variable
2081 case ISN_LOADAUTO:
2082 {
2083 char_u *name = iptr->isn_arg.string;
2084
Bram Moolenaar35578162021-08-02 19:10:38 +02002085 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002086 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002087 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002088 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002089 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002090 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002091 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002092 }
2093 break;
2094
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002095 // load g:/b:/w:/t: namespace
2096 case ISN_LOADGDICT:
2097 case ISN_LOADBDICT:
2098 case ISN_LOADWDICT:
2099 case ISN_LOADTDICT:
2100 {
2101 dict_T *d = NULL;
2102
2103 switch (iptr->isn_type)
2104 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002105 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2106 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2107 case ISN_LOADWDICT: d = curwin->w_vars; break;
2108 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002109 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002110 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002111 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002112 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002113 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002114 tv = STACK_TV_BOT(0);
2115 tv->v_type = VAR_DICT;
2116 tv->v_lock = 0;
2117 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002118 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002119 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002120 }
2121 break;
2122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 // load &option
2124 case ISN_LOADOPT:
2125 {
2126 typval_T optval;
2127 char_u *name = iptr->isn_arg.string;
2128
Bram Moolenaara8c17702020-04-01 21:17:24 +02002129 // This is not expected to fail, name is checked during
2130 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002131 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002132 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002133 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002134 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002136 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 }
2138 break;
2139
2140 // load $ENV
2141 case ISN_LOADENV:
2142 {
2143 typval_T optval;
2144 char_u *name = iptr->isn_arg.string;
2145
Bram Moolenaar35578162021-08-02 19:10:38 +02002146 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002147 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002148 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002149 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002151 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152 }
2153 break;
2154
2155 // load @register
2156 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002157 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002158 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 tv = STACK_TV_BOT(0);
2160 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002161 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002162 // This may result in NULL, which should be equivalent to an
2163 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 tv->vval.v_string = get_reg_contents(
2165 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002166 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 break;
2168
2169 // store local variable
2170 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002171 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 tv = STACK_TV_VAR(iptr->isn_arg.number);
2173 clear_tv(tv);
2174 *tv = *STACK_TV_BOT(0);
2175 break;
2176
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002177 // store s: variable in old script
2178 case ISN_STORES:
2179 {
2180 hashtab_T *ht = &SCRIPT_VARS(
2181 iptr->isn_arg.loadstore.ls_sid);
2182 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002183 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002184
Bram Moolenaar4c137212021-04-19 16:48:48 +02002185 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002186 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002187 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002188 else
2189 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002190 SOURCING_LNUM = iptr->isn_lnum;
2191 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002192 {
2193 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002194 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002195 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002196 clear_tv(&di->di_tv);
2197 di->di_tv = *STACK_TV_BOT(0);
2198 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002199 }
2200 break;
2201
2202 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 case ISN_STORESCRIPT:
2204 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002205 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2206 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002208 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002209 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002210 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002211 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002212
2213 // "const" and "final" are checked at compile time, locking
2214 // the value needs to be checked here.
2215 SOURCING_LNUM = iptr->isn_lnum;
2216 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002217 {
2218 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002219 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002220 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 clear_tv(sv->sv_tv);
2223 *sv->sv_tv = *STACK_TV_BOT(0);
2224 }
2225 break;
2226
2227 // store option
2228 case ISN_STOREOPT:
2229 {
2230 long n = 0;
2231 char_u *s = NULL;
2232 char *msg;
2233
Bram Moolenaar4c137212021-04-19 16:48:48 +02002234 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 tv = STACK_TV_BOT(0);
2236 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002237 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002239 if (s == NULL)
2240 s = (char_u *)"";
2241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002243 // must be VAR_NUMBER, CHECKTYPE makes sure
2244 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2246 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002247 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 if (msg != NULL)
2249 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002250 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002252 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 }
2255 break;
2256
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002257 // store $ENV
2258 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002259 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002260 tv = STACK_TV_BOT(0);
2261 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2262 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002263 break;
2264
2265 // store @r
2266 case ISN_STOREREG:
2267 {
2268 int reg = iptr->isn_arg.number;
2269
Bram Moolenaar4c137212021-04-19 16:48:48 +02002270 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002271 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002272 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002273 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002274 }
2275 break;
2276
2277 // store v: variable
2278 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002279 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002280 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2281 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002282 // should not happen, type is checked when compiling
2283 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002284 break;
2285
Bram Moolenaard3aac292020-04-19 14:32:17 +02002286 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002288 case ISN_STOREB:
2289 case ISN_STOREW:
2290 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002292 dictitem_T *di;
2293 hashtab_T *ht;
2294 char_u *name = iptr->isn_arg.string + 2;
2295
Bram Moolenaard3aac292020-04-19 14:32:17 +02002296 switch (iptr->isn_type)
2297 {
2298 case ISN_STOREG:
2299 ht = get_globvar_ht();
2300 break;
2301 case ISN_STOREB:
2302 ht = &curbuf->b_vars->dv_hashtab;
2303 break;
2304 case ISN_STOREW:
2305 ht = &curwin->w_vars->dv_hashtab;
2306 break;
2307 case ISN_STORET:
2308 ht = &curtab->tp_vars->dv_hashtab;
2309 break;
2310 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002311 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313
Bram Moolenaar4c137212021-04-19 16:48:48 +02002314 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002315 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002317 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 else
2319 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002320 SOURCING_LNUM = iptr->isn_lnum;
2321 if (var_check_permission(di, name) == FAIL)
2322 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 clear_tv(&di->di_tv);
2324 di->di_tv = *STACK_TV_BOT(0);
2325 }
2326 }
2327 break;
2328
Bram Moolenaar03290b82020-12-19 16:30:44 +01002329 // store an autoload variable
2330 case ISN_STOREAUTO:
2331 SOURCING_LNUM = iptr->isn_lnum;
2332 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2333 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002334 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002335 break;
2336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 // store number in local variable
2338 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002339 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 clear_tv(tv);
2341 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002342 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 break;
2344
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002345 // store value in list or dict variable
2346 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002347 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002348 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002349 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002350 typval_T *tv_dest = STACK_TV_BOT(-1);
2351 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002352
Bram Moolenaar752fc692021-01-04 21:57:11 +01002353 // Stack contains:
2354 // -3 value to be stored
2355 // -2 index
2356 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002357 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002358 SOURCING_LNUM = iptr->isn_lnum;
2359 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002360 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002361 dest_type = tv_dest->v_type;
2362 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002363 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002364 else if (dest_type == VAR_LIST
2365 && tv_idx->v_type != VAR_NUMBER)
2366 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002367 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002368 status = FAIL;
2369 }
2370 }
2371 else if (dest_type != tv_dest->v_type)
2372 {
2373 // just in case, should be OK
2374 semsg(_(e_expected_str_but_got_str),
2375 vartype_name(dest_type),
2376 vartype_name(tv_dest->v_type));
2377 status = FAIL;
2378 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002379
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002380 if (status == OK && dest_type == VAR_LIST)
2381 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002382 long lidx = (long)tv_idx->vval.v_number;
2383 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002384
2385 if (list == NULL)
2386 {
2387 emsg(_(e_list_not_set));
2388 goto on_error;
2389 }
2390 if (lidx < 0 && list->lv_len + lidx >= 0)
2391 // negative index is relative to the end
2392 lidx = list->lv_len + lidx;
2393 if (lidx < 0 || lidx > list->lv_len)
2394 {
2395 semsg(_(e_listidx), lidx);
2396 goto on_error;
2397 }
2398 if (lidx < list->lv_len)
2399 {
2400 listitem_T *li = list_find(list, lidx);
2401
2402 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002403 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002404 goto on_error;
2405 // overwrite existing list item
2406 clear_tv(&li->li_tv);
2407 li->li_tv = *tv;
2408 }
2409 else
2410 {
2411 if (error_if_locked(list->lv_lock,
2412 e_cannot_change_list))
2413 goto on_error;
2414 // append to list, only fails when out of memory
2415 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002416 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002417 clear_tv(tv);
2418 }
2419 }
2420 else if (status == OK && dest_type == VAR_DICT)
2421 {
2422 char_u *key = tv_idx->vval.v_string;
2423 dict_T *dict = tv_dest->vval.v_dict;
2424 dictitem_T *di;
2425
2426 SOURCING_LNUM = iptr->isn_lnum;
2427 if (dict == NULL)
2428 {
2429 emsg(_(e_dictionary_not_set));
2430 goto on_error;
2431 }
2432 if (key == NULL)
2433 key = (char_u *)"";
2434 di = dict_find(dict, key, -1);
2435 if (di != NULL)
2436 {
2437 if (error_if_locked(di->di_tv.v_lock,
2438 e_cannot_change_dict_item))
2439 goto on_error;
2440 // overwrite existing value
2441 clear_tv(&di->di_tv);
2442 di->di_tv = *tv;
2443 }
2444 else
2445 {
2446 if (error_if_locked(dict->dv_lock,
2447 e_cannot_change_dict))
2448 goto on_error;
2449 // add to dict, only fails when out of memory
2450 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002451 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002452 clear_tv(tv);
2453 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002454 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002455 else if (status == OK && dest_type == VAR_BLOB)
2456 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002457 long lidx = (long)tv_idx->vval.v_number;
2458 blob_T *blob = tv_dest->vval.v_blob;
2459 varnumber_T nr;
2460 int error = FALSE;
2461 int len;
2462
2463 if (blob == NULL)
2464 {
2465 emsg(_(e_blob_not_set));
2466 goto on_error;
2467 }
2468 len = blob_len(blob);
2469 if (lidx < 0 && len + lidx >= 0)
2470 // negative index is relative to the end
2471 lidx = len + lidx;
2472
2473 // Can add one byte at the end.
2474 if (lidx < 0 || lidx > len)
2475 {
2476 semsg(_(e_blobidx), lidx);
2477 goto on_error;
2478 }
2479 if (value_check_lock(blob->bv_lock,
2480 (char_u *)"blob", FALSE))
2481 goto on_error;
2482 nr = tv_get_number_chk(tv, &error);
2483 if (error)
2484 goto on_error;
2485 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002486 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002487 else
2488 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002489 status = FAIL;
2490 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002491 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002492
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002493 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002494 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002495 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002496 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002497 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002498 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002499 goto on_error;
2500 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002501 }
2502 break;
2503
Bram Moolenaar68452172021-04-12 21:21:02 +02002504 // store value in blob range
2505 case ISN_STORERANGE:
2506 {
2507 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2508 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2509 typval_T *tv_dest = STACK_TV_BOT(-1);
2510 int status = OK;
2511
2512 // Stack contains:
2513 // -4 value to be stored
2514 // -3 first index or "none"
2515 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002516 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002517 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002518 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002519 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002520 long n1;
2521 long n2;
2522 int error = FALSE;
2523
2524 SOURCING_LNUM = iptr->isn_lnum;
2525 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2526 if (error)
2527 status = FAIL;
2528 else
2529 {
2530 if (tv_idx2->v_type == VAR_SPECIAL
2531 && tv_idx2->vval.v_number == VVAL_NONE)
2532 n2 = list_len(tv_dest->vval.v_list) - 1;
2533 else
2534 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2535 if (error)
2536 status = FAIL;
2537 else
2538 {
2539 listitem_T *li1 = check_range_index_one(
2540 tv_dest->vval.v_list, &n1, FALSE);
2541
2542 if (li1 == NULL)
2543 status = FAIL;
2544 else
2545 {
2546 status = check_range_index_two(
2547 tv_dest->vval.v_list,
2548 &n1, li1, &n2, FALSE);
2549 if (status != FAIL)
2550 status = list_assign_range(
2551 tv_dest->vval.v_list,
2552 tv->vval.v_list,
2553 n1,
2554 n2,
2555 tv_idx2->v_type == VAR_SPECIAL,
2556 (char_u *)"=",
2557 (char_u *)"[unknown]");
2558 }
2559 }
2560 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002561 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002562 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002563 {
2564 varnumber_T n1;
2565 varnumber_T n2;
2566 int error = FALSE;
2567
2568 n1 = tv_get_number_chk(tv_idx1, &error);
2569 if (error)
2570 status = FAIL;
2571 else
2572 {
2573 if (tv_idx2->v_type == VAR_SPECIAL
2574 && tv_idx2->vval.v_number == VVAL_NONE)
2575 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2576 else
2577 n2 = tv_get_number_chk(tv_idx2, &error);
2578 if (error)
2579 status = FAIL;
2580 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002581 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002582 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002583
2584 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002585 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002586 || check_blob_range(bloblen,
2587 n1, n2, FALSE) == FAIL)
2588 status = FAIL;
2589 else
2590 status = blob_set_range(
2591 tv_dest->vval.v_blob, n1, n2, tv);
2592 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002593 }
2594 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002595 else
2596 {
2597 status = FAIL;
2598 emsg(_(e_blob_required));
2599 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002600
2601 clear_tv(tv_idx1);
2602 clear_tv(tv_idx2);
2603 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002604 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002605 clear_tv(tv);
2606
2607 if (status == FAIL)
2608 goto on_error;
2609 }
2610 break;
2611
Bram Moolenaar0186e582021-01-10 18:33:11 +01002612 // load or store variable or argument from outer scope
2613 case ISN_LOADOUTER:
2614 case ISN_STOREOUTER:
2615 {
2616 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002617 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2618 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002619
2620 while (depth > 1 && outer != NULL)
2621 {
2622 outer = outer->out_up;
2623 --depth;
2624 }
2625 if (outer == NULL)
2626 {
2627 SOURCING_LNUM = iptr->isn_lnum;
2628 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002629 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002630 }
2631 tv = ((typval_T *)outer->out_stack->ga_data)
2632 + outer->out_frame_idx + STACK_FRAME_SIZE
2633 + iptr->isn_arg.outer.outer_idx;
2634 if (iptr->isn_type == ISN_LOADOUTER)
2635 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002636 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002637 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002638 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002639 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002640 }
2641 else
2642 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002643 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002644 clear_tv(tv);
2645 *tv = *STACK_TV_BOT(0);
2646 }
2647 }
2648 break;
2649
Bram Moolenaar752fc692021-01-04 21:57:11 +01002650 // unlet item in list or dict variable
2651 case ISN_UNLETINDEX:
2652 {
2653 typval_T *tv_idx = STACK_TV_BOT(-2);
2654 typval_T *tv_dest = STACK_TV_BOT(-1);
2655 int status = OK;
2656
2657 // Stack contains:
2658 // -2 index
2659 // -1 dict or list
2660 if (tv_dest->v_type == VAR_DICT)
2661 {
2662 // unlet a dict item, index must be a string
2663 if (tv_idx->v_type != VAR_STRING)
2664 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002665 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002666 semsg(_(e_expected_str_but_got_str),
2667 vartype_name(VAR_STRING),
2668 vartype_name(tv_idx->v_type));
2669 status = FAIL;
2670 }
2671 else
2672 {
2673 dict_T *d = tv_dest->vval.v_dict;
2674 char_u *key = tv_idx->vval.v_string;
2675 dictitem_T *di = NULL;
2676
2677 if (key == NULL)
2678 key = (char_u *)"";
2679 if (d != NULL)
2680 di = dict_find(d, key, (int)STRLEN(key));
2681 if (di == NULL)
2682 {
2683 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002684 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002685 semsg(_(e_dictkey), key);
2686 status = FAIL;
2687 }
2688 else
2689 {
2690 // TODO: check for dict or item locked
2691 dictitem_remove(d, di);
2692 }
2693 }
2694 }
2695 else if (tv_dest->v_type == VAR_LIST)
2696 {
2697 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002698 SOURCING_LNUM = iptr->isn_lnum;
2699 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002700 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002701 status = FAIL;
2702 }
2703 else
2704 {
2705 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002706 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002707 listitem_T *li = NULL;
2708
2709 li = list_find(l, n);
2710 if (li == NULL)
2711 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002712 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002713 semsg(_(e_listidx), n);
2714 status = FAIL;
2715 }
2716 else
2717 // TODO: check for list or item locked
2718 listitem_remove(l, li);
2719 }
2720 }
2721 else
2722 {
2723 status = FAIL;
2724 semsg(_(e_cannot_index_str),
2725 vartype_name(tv_dest->v_type));
2726 }
2727
2728 clear_tv(tv_idx);
2729 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002730 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002731 if (status == FAIL)
2732 goto on_error;
2733 }
2734 break;
2735
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002736 // unlet range of items in list variable
2737 case ISN_UNLETRANGE:
2738 {
2739 // Stack contains:
2740 // -3 index1
2741 // -2 index2
2742 // -1 dict or list
2743 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2744 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2745 typval_T *tv_dest = STACK_TV_BOT(-1);
2746 int status = OK;
2747
2748 if (tv_dest->v_type == VAR_LIST)
2749 {
2750 // indexes must be a number
2751 SOURCING_LNUM = iptr->isn_lnum;
2752 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002753 || (tv_idx2->v_type != VAR_SPECIAL
2754 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002755 {
2756 status = FAIL;
2757 }
2758 else
2759 {
2760 list_T *l = tv_dest->vval.v_list;
2761 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002762 long n2 = tv_idx2->v_type == VAR_SPECIAL
2763 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002764 listitem_T *li;
2765
2766 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002767 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002768 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002769 else
2770 {
2771 if (n1 < 0)
2772 n1 = list_idx_of_item(l, li);
2773 if (n2 < 0)
2774 {
2775 listitem_T *li2 = list_find(l, n2);
2776
2777 if (li2 == NULL)
2778 status = FAIL;
2779 else
2780 n2 = list_idx_of_item(l, li2);
2781 }
2782 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002783 && tv_idx2->v_type != VAR_SPECIAL
2784 && n2 < n1)
2785 {
2786 semsg(_(e_listidx), n2);
2787 status = FAIL;
2788 }
2789 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002790 && list_unlet_range(l, li, NULL, n1,
2791 tv_idx2->v_type != VAR_SPECIAL, n2)
2792 == FAIL)
2793 status = FAIL;
2794 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002795 }
2796 }
2797 else
2798 {
2799 status = FAIL;
2800 SOURCING_LNUM = iptr->isn_lnum;
2801 semsg(_(e_cannot_index_str),
2802 vartype_name(tv_dest->v_type));
2803 }
2804
2805 clear_tv(tv_idx1);
2806 clear_tv(tv_idx2);
2807 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002808 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002809 if (status == FAIL)
2810 goto on_error;
2811 }
2812 break;
2813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 // push constant
2815 case ISN_PUSHNR:
2816 case ISN_PUSHBOOL:
2817 case ISN_PUSHSPEC:
2818 case ISN_PUSHF:
2819 case ISN_PUSHS:
2820 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002821 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002822 case ISN_PUSHCHANNEL:
2823 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02002824 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002825 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002827 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002828 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 switch (iptr->isn_type)
2830 {
2831 case ISN_PUSHNR:
2832 tv->v_type = VAR_NUMBER;
2833 tv->vval.v_number = iptr->isn_arg.number;
2834 break;
2835 case ISN_PUSHBOOL:
2836 tv->v_type = VAR_BOOL;
2837 tv->vval.v_number = iptr->isn_arg.number;
2838 break;
2839 case ISN_PUSHSPEC:
2840 tv->v_type = VAR_SPECIAL;
2841 tv->vval.v_number = iptr->isn_arg.number;
2842 break;
2843#ifdef FEAT_FLOAT
2844 case ISN_PUSHF:
2845 tv->v_type = VAR_FLOAT;
2846 tv->vval.v_float = iptr->isn_arg.fnumber;
2847 break;
2848#endif
2849 case ISN_PUSHBLOB:
2850 blob_copy(iptr->isn_arg.blob, tv);
2851 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002852 case ISN_PUSHFUNC:
2853 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002854 if (iptr->isn_arg.string == NULL)
2855 tv->vval.v_string = NULL;
2856 else
2857 tv->vval.v_string =
2858 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002859 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002860 case ISN_PUSHCHANNEL:
2861#ifdef FEAT_JOB_CHANNEL
2862 tv->v_type = VAR_CHANNEL;
2863 tv->vval.v_channel = iptr->isn_arg.channel;
2864 if (tv->vval.v_channel != NULL)
2865 ++tv->vval.v_channel->ch_refcount;
2866#endif
2867 break;
2868 case ISN_PUSHJOB:
2869#ifdef FEAT_JOB_CHANNEL
2870 tv->v_type = VAR_JOB;
2871 tv->vval.v_job = iptr->isn_arg.job;
2872 if (tv->vval.v_job != NULL)
2873 ++tv->vval.v_job->jv_refcount;
2874#endif
2875 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 default:
2877 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002878 tv->vval.v_string = vim_strsave(
2879 iptr->isn_arg.string == NULL
2880 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 }
2882 break;
2883
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002884 case ISN_UNLET:
2885 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2886 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002887 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002888 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002889 case ISN_UNLETENV:
2890 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2891 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002892
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002893 case ISN_LOCKUNLOCK:
2894 {
2895 typval_T *lval_root_save = lval_root;
2896 int res;
2897
2898 // Stack has the local variable, argument the whole :lock
2899 // or :unlock command, like ISN_EXEC.
2900 --ectx->ec_stack.ga_len;
2901 lval_root = STACK_TV_BOT(0);
2902 res = exec_command(iptr);
2903 clear_tv(lval_root);
2904 lval_root = lval_root_save;
2905 if (res == FAIL)
2906 goto on_error;
2907 }
2908 break;
2909
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002910 case ISN_LOCKCONST:
2911 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2912 break;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 // create a list from items on the stack; uses a single allocation
2915 // for the list header and the items
2916 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002917 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002918 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 break;
2920
2921 // create a dict from items on the stack
2922 case ISN_NEWDICT:
2923 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002924 int count = iptr->isn_arg.number;
2925 dict_T *dict = dict_alloc();
2926 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002927 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002928 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002930 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002931 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 for (idx = 0; idx < count; ++idx)
2933 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002934 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002936 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002937 key = tv->vval.v_string == NULL
2938 ? (char_u *)"" : tv->vval.v_string;
2939 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002940 if (item != NULL)
2941 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002942 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002943 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002944 dict_unref(dict);
2945 goto on_error;
2946 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002947 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002949 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02002950 {
2951 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002952 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2955 item->di_tv.v_lock = 0;
2956 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002957 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002958 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002959 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002960 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 }
2963
2964 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02002966 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002967 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002969 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 tv = STACK_TV_BOT(-1);
2971 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002972 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 tv->vval.v_dict = dict;
2974 ++dict->dv_refcount;
2975 }
2976 break;
2977
2978 // call a :def function
2979 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002980 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002981 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2982 NULL,
2983 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002984 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002985 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 break;
2987
2988 // call a builtin function
2989 case ISN_BCALL:
2990 SOURCING_LNUM = iptr->isn_lnum;
2991 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2992 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002993 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002994 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 break;
2996
2997 // call a funcref or partial
2998 case ISN_PCALL:
2999 {
3000 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3001 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003002 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003
3004 SOURCING_LNUM = iptr->isn_lnum;
3005 if (pfunc->cpf_top)
3006 {
3007 // funcref is above the arguments
3008 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3009 }
3010 else
3011 {
3012 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003013 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003014 partial_tv = *STACK_TV_BOT(0);
3015 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003017 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003018 if (tv == &partial_tv)
3019 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003021 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 }
3023 break;
3024
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003025 case ISN_PCALL_END:
3026 // PCALL finished, arguments have been consumed and replaced by
3027 // the return value. Now clear the funcref from the stack,
3028 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003029 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003030 clear_tv(STACK_TV_BOT(-1));
3031 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3032 break;
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 // call a user defined function or funcref/partial
3035 case ISN_UCALL:
3036 {
3037 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3038
3039 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003040 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003041 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003042 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 }
3044 break;
3045
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003046 // return from a :def function call without a value
3047 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003048 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003049 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003050 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003051 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003052 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003053 tv->vval.v_number = 0;
3054 tv->v_lock = 0;
3055 // FALLTHROUGH
3056
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003057 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 case ISN_RETURN:
3059 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003060 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003061 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003062
3063 if (trystack->ga_len > 0)
3064 trycmd = ((trycmd_T *)trystack->ga_data)
3065 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003066 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003067 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003069 // jump to ":finally" or ":endtry"
3070 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003071 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003072 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003073 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 trycmd->tcd_return = TRUE;
3075 }
3076 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003077 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
3079 break;
3080
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003081 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 case ISN_FUNCREF:
3083 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003084 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
3085 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3086 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003089 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003090 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003091 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003092 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003093 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003094 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01003095 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003096 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003097 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003099 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 tv->vval.v_partial = pt;
3101 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003102 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 }
3104 break;
3105
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003106 // Create a global function from a lambda.
3107 case ISN_NEWFUNC:
3108 {
3109 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3110
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003111 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003112 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003113 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003114 }
3115 break;
3116
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003117 // List functions
3118 case ISN_DEF:
3119 if (iptr->isn_arg.string == NULL)
3120 list_functions(NULL);
3121 else
3122 {
3123 exarg_T ea;
3124
3125 CLEAR_FIELD(ea);
3126 ea.cmd = ea.arg = iptr->isn_arg.string;
3127 define_function(&ea, NULL);
3128 }
3129 break;
3130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 // jump if a condition is met
3132 case ISN_JUMP:
3133 {
3134 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003135 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 int jump = TRUE;
3137
3138 if (when != JUMP_ALWAYS)
3139 {
3140 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003141 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003142 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003143 || when == JUMP_IF_COND_TRUE)
3144 {
3145 SOURCING_LNUM = iptr->isn_lnum;
3146 jump = tv_get_bool_chk(tv, &error);
3147 if (error)
3148 goto on_error;
3149 }
3150 else
3151 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003153 || when == JUMP_AND_KEEP_IF_FALSE
3154 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003156 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 {
3158 // drop the value from the stack
3159 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003160 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 }
3162 }
3163 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003164 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 }
3166 break;
3167
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003168 // Jump if an argument with a default value was already set and not
3169 // v:none.
3170 case ISN_JUMP_IF_ARG_SET:
3171 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3172 if (tv->v_type != VAR_UNKNOWN
3173 && !(tv->v_type == VAR_SPECIAL
3174 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003175 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003176 break;
3177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 // top of a for loop
3179 case ISN_FOR:
3180 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003181 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 typval_T *idxtv =
3183 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3184
Bram Moolenaar35578162021-08-02 19:10:38 +02003185 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003186 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003187 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003188 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003189 list_T *list = ltv->vval.v_list;
3190
3191 // push the next item from the list
3192 ++idxtv->vval.v_number;
3193 if (list == NULL
3194 || idxtv->vval.v_number >= list->lv_len)
3195 {
3196 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003197 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3198 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003199 }
3200 else if (list->lv_first == &range_list_item)
3201 {
3202 // non-materialized range() list
3203 tv = STACK_TV_BOT(0);
3204 tv->v_type = VAR_NUMBER;
3205 tv->v_lock = 0;
3206 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003208 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003209 }
3210 else
3211 {
3212 listitem_T *li = list_find(list,
3213 idxtv->vval.v_number);
3214
3215 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003216 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003217 }
3218 }
3219 else if (ltv->v_type == VAR_STRING)
3220 {
3221 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003222
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003223 // The index is for the last byte of the previous
3224 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003225 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003226 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003227 {
3228 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003229 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3230 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003231 }
3232 else
3233 {
3234 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3235
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003236 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003237 tv = STACK_TV_BOT(0);
3238 tv->v_type = VAR_STRING;
3239 tv->vval.v_string = vim_strnsave(
3240 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003241 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003242 idxtv->vval.v_number += clen - 1;
3243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003245 else if (ltv->v_type == VAR_BLOB)
3246 {
3247 blob_T *blob = ltv->vval.v_blob;
3248
3249 // When we get here the first time make a copy of the
3250 // blob, so that the iteration still works when it is
3251 // changed.
3252 if (idxtv->vval.v_number == -1 && blob != NULL)
3253 {
3254 blob_copy(blob, ltv);
3255 blob_unref(blob);
3256 blob = ltv->vval.v_blob;
3257 }
3258
3259 // The index is for the previous byte.
3260 ++idxtv->vval.v_number;
3261 if (blob == NULL
3262 || idxtv->vval.v_number >= blob_len(blob))
3263 {
3264 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003265 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3266 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003267 }
3268 else
3269 {
3270 // Push the next byte from the blob.
3271 tv = STACK_TV_BOT(0);
3272 tv->v_type = VAR_NUMBER;
3273 tv->vval.v_number = blob_get(blob,
3274 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003275 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003276 }
3277 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 else
3279 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003280 semsg(_(e_for_loop_on_str_not_supported),
3281 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003282 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 }
3284 }
3285 break;
3286
3287 // start of ":try" block
3288 case ISN_TRY:
3289 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003290 trycmd_T *trycmd = NULL;
3291
Bram Moolenaar35578162021-08-02 19:10:38 +02003292 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003293 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003294 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3295 + ectx->ec_trystack.ga_len;
3296 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003298 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003299 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3300 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003301 trycmd->tcd_catch_idx =
3302 iptr->isn_arg.try.try_ref->try_catch;
3303 trycmd->tcd_finally_idx =
3304 iptr->isn_arg.try.try_ref->try_finally;
3305 trycmd->tcd_endtry_idx =
3306 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 }
3308 break;
3309
3310 case ISN_PUSHEXC:
3311 if (current_exception == NULL)
3312 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003313 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003315 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003317 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003318 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003320 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003322 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 tv->vval.v_string = vim_strsave(
3324 (char_u *)current_exception->value);
3325 break;
3326
3327 case ISN_CATCH:
3328 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003329 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330
Bram Moolenaar4c137212021-04-19 16:48:48 +02003331 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 if (trystack->ga_len > 0)
3333 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003334 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 + trystack->ga_len - 1;
3336 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003337 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 }
3339 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003340 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 catch_exception(current_exception);
3342 }
3343 break;
3344
Bram Moolenaarc150c092021-02-13 15:02:46 +01003345 case ISN_TRYCONT:
3346 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003347 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003348 trycont_T *trycont = &iptr->isn_arg.trycont;
3349 int i;
3350 trycmd_T *trycmd;
3351 int iidx = trycont->tct_where;
3352
3353 if (trystack->ga_len < trycont->tct_levels)
3354 {
3355 siemsg("TRYCONT: expected %d levels, found %d",
3356 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003357 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003358 }
3359 // Make :endtry jump to any outer try block and the last
3360 // :endtry inside the loop to the loop start.
3361 for (i = trycont->tct_levels; i > 0; --i)
3362 {
3363 trycmd = ((trycmd_T *)trystack->ga_data)
3364 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003365 // Add one to tcd_cont to be able to jump to
3366 // instruction with index zero.
3367 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003368 iidx = trycmd->tcd_finally_idx == 0
3369 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003370 }
3371 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003372 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003373 }
3374 break;
3375
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003376 case ISN_FINALLY:
3377 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003378 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003379 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3380 + trystack->ga_len - 1;
3381
3382 // Reset the index to avoid a return statement jumps here
3383 // again.
3384 trycmd->tcd_finally_idx = 0;
3385 break;
3386 }
3387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 // end of ":try" block
3389 case ISN_ENDTRY:
3390 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003391 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392
3393 if (trystack->ga_len > 0)
3394 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003395 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 --trystack->ga_len;
3398 --trylevel;
3399 trycmd = ((trycmd_T *)trystack->ga_data)
3400 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003401 if (trycmd->tcd_did_throw)
3402 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003403 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 {
3405 // discard the exception
3406 if (caught_stack == current_exception)
3407 caught_stack = caught_stack->caught;
3408 discard_current_exception();
3409 }
3410
3411 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003412 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003413
Bram Moolenaar4c137212021-04-19 16:48:48 +02003414 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003415 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003417 clear_tv(STACK_TV_BOT(0));
3418 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003419 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003420 // handling :continue: jump to outer try block or
3421 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003422 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 }
3424 }
3425 break;
3426
3427 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003428 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003429 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003430
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003431 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3432 {
3433 // throwing an exception while using "silent!" causes
3434 // the function to abort but not display an error.
3435 tv = STACK_TV_BOT(-1);
3436 clear_tv(tv);
3437 tv->v_type = VAR_NUMBER;
3438 tv->vval.v_number = 0;
3439 goto done;
3440 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003441 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003442 tv = STACK_TV_BOT(0);
3443 if (tv->vval.v_string == NULL
3444 || *skipwhite(tv->vval.v_string) == NUL)
3445 {
3446 vim_free(tv->vval.v_string);
3447 SOURCING_LNUM = iptr->isn_lnum;
3448 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003449 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003450 }
3451
3452 // Inside a "catch" we need to first discard the caught
3453 // exception.
3454 if (trystack->ga_len > 0)
3455 {
3456 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3457 + trystack->ga_len - 1;
3458 if (trycmd->tcd_caught && current_exception != NULL)
3459 {
3460 // discard the exception
3461 if (caught_stack == current_exception)
3462 caught_stack = caught_stack->caught;
3463 discard_current_exception();
3464 trycmd->tcd_caught = FALSE;
3465 }
3466 }
3467
3468 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3469 == FAIL)
3470 {
3471 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003472 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003473 }
3474 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 break;
3477
3478 // compare with special values
3479 case ISN_COMPAREBOOL:
3480 case ISN_COMPARESPECIAL:
3481 {
3482 typval_T *tv1 = STACK_TV_BOT(-2);
3483 typval_T *tv2 = STACK_TV_BOT(-1);
3484 varnumber_T arg1 = tv1->vval.v_number;
3485 varnumber_T arg2 = tv2->vval.v_number;
3486 int res;
3487
3488 switch (iptr->isn_arg.op.op_type)
3489 {
3490 case EXPR_EQUAL: res = arg1 == arg2; break;
3491 case EXPR_NEQUAL: res = arg1 != arg2; break;
3492 default: res = 0; break;
3493 }
3494
Bram Moolenaar4c137212021-04-19 16:48:48 +02003495 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 tv1->v_type = VAR_BOOL;
3497 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3498 }
3499 break;
3500
3501 // Operation with two number arguments
3502 case ISN_OPNR:
3503 case ISN_COMPARENR:
3504 {
3505 typval_T *tv1 = STACK_TV_BOT(-2);
3506 typval_T *tv2 = STACK_TV_BOT(-1);
3507 varnumber_T arg1 = tv1->vval.v_number;
3508 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003509 varnumber_T res = 0;
3510 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511
3512 switch (iptr->isn_arg.op.op_type)
3513 {
3514 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003515 case EXPR_DIV: if (arg2 == 0)
3516 div_zero = TRUE;
3517 else
3518 res = arg1 / arg2;
3519 break;
3520 case EXPR_REM: if (arg2 == 0)
3521 div_zero = TRUE;
3522 else
3523 res = arg1 % arg2;
3524 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 case EXPR_SUB: res = arg1 - arg2; break;
3526 case EXPR_ADD: res = arg1 + arg2; break;
3527
3528 case EXPR_EQUAL: res = arg1 == arg2; break;
3529 case EXPR_NEQUAL: res = arg1 != arg2; break;
3530 case EXPR_GREATER: res = arg1 > arg2; break;
3531 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3532 case EXPR_SMALLER: res = arg1 < arg2; break;
3533 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003534 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 }
3536
Bram Moolenaar4c137212021-04-19 16:48:48 +02003537 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 if (iptr->isn_type == ISN_COMPARENR)
3539 {
3540 tv1->v_type = VAR_BOOL;
3541 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3542 }
3543 else
3544 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003545 if (div_zero)
3546 {
3547 SOURCING_LNUM = iptr->isn_lnum;
3548 emsg(_(e_divide_by_zero));
3549 goto on_error;
3550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 }
3552 break;
3553
3554 // Computation with two float arguments
3555 case ISN_OPFLOAT:
3556 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003557#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 {
3559 typval_T *tv1 = STACK_TV_BOT(-2);
3560 typval_T *tv2 = STACK_TV_BOT(-1);
3561 float_T arg1 = tv1->vval.v_float;
3562 float_T arg2 = tv2->vval.v_float;
3563 float_T res = 0;
3564 int cmp = FALSE;
3565
3566 switch (iptr->isn_arg.op.op_type)
3567 {
3568 case EXPR_MULT: res = arg1 * arg2; break;
3569 case EXPR_DIV: res = arg1 / arg2; break;
3570 case EXPR_SUB: res = arg1 - arg2; break;
3571 case EXPR_ADD: res = arg1 + arg2; break;
3572
3573 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3574 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3575 case EXPR_GREATER: cmp = arg1 > arg2; break;
3576 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3577 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3578 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3579 default: cmp = 0; break;
3580 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003581 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 if (iptr->isn_type == ISN_COMPAREFLOAT)
3583 {
3584 tv1->v_type = VAR_BOOL;
3585 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3586 }
3587 else
3588 tv1->vval.v_float = res;
3589 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003590#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 break;
3592
3593 case ISN_COMPARELIST:
3594 {
3595 typval_T *tv1 = STACK_TV_BOT(-2);
3596 typval_T *tv2 = STACK_TV_BOT(-1);
3597 list_T *arg1 = tv1->vval.v_list;
3598 list_T *arg2 = tv2->vval.v_list;
3599 int cmp = FALSE;
3600 int ic = iptr->isn_arg.op.op_ic;
3601
3602 switch (iptr->isn_arg.op.op_type)
3603 {
3604 case EXPR_EQUAL: cmp =
3605 list_equal(arg1, arg2, ic, FALSE); break;
3606 case EXPR_NEQUAL: cmp =
3607 !list_equal(arg1, arg2, ic, FALSE); break;
3608 case EXPR_IS: cmp = arg1 == arg2; break;
3609 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3610 default: cmp = 0; break;
3611 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003612 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 clear_tv(tv1);
3614 clear_tv(tv2);
3615 tv1->v_type = VAR_BOOL;
3616 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3617 }
3618 break;
3619
3620 case ISN_COMPAREBLOB:
3621 {
3622 typval_T *tv1 = STACK_TV_BOT(-2);
3623 typval_T *tv2 = STACK_TV_BOT(-1);
3624 blob_T *arg1 = tv1->vval.v_blob;
3625 blob_T *arg2 = tv2->vval.v_blob;
3626 int cmp = FALSE;
3627
3628 switch (iptr->isn_arg.op.op_type)
3629 {
3630 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3631 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3632 case EXPR_IS: cmp = arg1 == arg2; break;
3633 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3634 default: cmp = 0; break;
3635 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003636 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 clear_tv(tv1);
3638 clear_tv(tv2);
3639 tv1->v_type = VAR_BOOL;
3640 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3641 }
3642 break;
3643
3644 // TODO: handle separately
3645 case ISN_COMPARESTRING:
3646 case ISN_COMPAREDICT:
3647 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 case ISN_COMPAREANY:
3649 {
3650 typval_T *tv1 = STACK_TV_BOT(-2);
3651 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003652 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 int ic = iptr->isn_arg.op.op_ic;
3654
Bram Moolenaareb26f432020-09-14 16:50:05 +02003655 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003656 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003658 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 }
3660 break;
3661
3662 case ISN_ADDLIST:
3663 case ISN_ADDBLOB:
3664 {
3665 typval_T *tv1 = STACK_TV_BOT(-2);
3666 typval_T *tv2 = STACK_TV_BOT(-1);
3667
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003668 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 if (iptr->isn_type == ISN_ADDLIST)
3670 eval_addlist(tv1, tv2);
3671 else
3672 eval_addblob(tv1, tv2);
3673 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003674 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 }
3676 break;
3677
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003678 case ISN_LISTAPPEND:
3679 {
3680 typval_T *tv1 = STACK_TV_BOT(-2);
3681 typval_T *tv2 = STACK_TV_BOT(-1);
3682 list_T *l = tv1->vval.v_list;
3683
3684 // add an item to a list
3685 if (l == NULL)
3686 {
3687 SOURCING_LNUM = iptr->isn_lnum;
3688 emsg(_(e_cannot_add_to_null_list));
3689 goto on_error;
3690 }
3691 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003692 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003693 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003694 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003695 }
3696 break;
3697
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003698 case ISN_BLOBAPPEND:
3699 {
3700 typval_T *tv1 = STACK_TV_BOT(-2);
3701 typval_T *tv2 = STACK_TV_BOT(-1);
3702 blob_T *b = tv1->vval.v_blob;
3703 int error = FALSE;
3704 varnumber_T n;
3705
3706 // add a number to a blob
3707 if (b == NULL)
3708 {
3709 SOURCING_LNUM = iptr->isn_lnum;
3710 emsg(_(e_cannot_add_to_null_blob));
3711 goto on_error;
3712 }
3713 n = tv_get_number_chk(tv2, &error);
3714 if (error)
3715 goto on_error;
3716 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003717 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003718 }
3719 break;
3720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 // Computation with two arguments of unknown type
3722 case ISN_OPANY:
3723 {
3724 typval_T *tv1 = STACK_TV_BOT(-2);
3725 typval_T *tv2 = STACK_TV_BOT(-1);
3726 varnumber_T n1, n2;
3727#ifdef FEAT_FLOAT
3728 float_T f1 = 0, f2 = 0;
3729#endif
3730 int error = FALSE;
3731
3732 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3733 {
3734 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3735 {
3736 eval_addlist(tv1, tv2);
3737 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003738 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 break;
3740 }
3741 else if (tv1->v_type == VAR_BLOB
3742 && tv2->v_type == VAR_BLOB)
3743 {
3744 eval_addblob(tv1, tv2);
3745 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003746 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 break;
3748 }
3749 }
3750#ifdef FEAT_FLOAT
3751 if (tv1->v_type == VAR_FLOAT)
3752 {
3753 f1 = tv1->vval.v_float;
3754 n1 = 0;
3755 }
3756 else
3757#endif
3758 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003759 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 n1 = tv_get_number_chk(tv1, &error);
3761 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003762 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763#ifdef FEAT_FLOAT
3764 if (tv2->v_type == VAR_FLOAT)
3765 f1 = n1;
3766#endif
3767 }
3768#ifdef FEAT_FLOAT
3769 if (tv2->v_type == VAR_FLOAT)
3770 {
3771 f2 = tv2->vval.v_float;
3772 n2 = 0;
3773 }
3774 else
3775#endif
3776 {
3777 n2 = tv_get_number_chk(tv2, &error);
3778 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003779 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780#ifdef FEAT_FLOAT
3781 if (tv1->v_type == VAR_FLOAT)
3782 f2 = n2;
3783#endif
3784 }
3785#ifdef FEAT_FLOAT
3786 // if there is a float on either side the result is a float
3787 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3788 {
3789 switch (iptr->isn_arg.op.op_type)
3790 {
3791 case EXPR_MULT: f1 = f1 * f2; break;
3792 case EXPR_DIV: f1 = f1 / f2; break;
3793 case EXPR_SUB: f1 = f1 - f2; break;
3794 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003795 default: SOURCING_LNUM = iptr->isn_lnum;
3796 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003797 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
3799 clear_tv(tv1);
3800 clear_tv(tv2);
3801 tv1->v_type = VAR_FLOAT;
3802 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003803 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 }
3805 else
3806#endif
3807 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003808 int failed = FALSE;
3809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 switch (iptr->isn_arg.op.op_type)
3811 {
3812 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003813 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3814 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003815 goto on_error;
3816 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 case EXPR_SUB: n1 = n1 - n2; break;
3818 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003819 default: n1 = num_modulus(n1, n2, &failed);
3820 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003821 goto on_error;
3822 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 }
3824 clear_tv(tv1);
3825 clear_tv(tv2);
3826 tv1->v_type = VAR_NUMBER;
3827 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003828 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 }
3830 }
3831 break;
3832
3833 case ISN_CONCAT:
3834 {
3835 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3836 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3837 char_u *res;
3838
3839 res = concat_str(str1, str2);
3840 clear_tv(STACK_TV_BOT(-2));
3841 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003842 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 STACK_TV_BOT(-1)->vval.v_string = res;
3844 }
3845 break;
3846
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003847 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003848 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003849 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003850 int is_slice = iptr->isn_type == ISN_STRSLICE;
3851 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003852 char_u *res;
3853
3854 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003855 // string slice: string is at stack-3, first index at
3856 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003857 if (is_slice)
3858 {
3859 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003860 n1 = tv->vval.v_number;
3861 }
3862
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003863 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003864 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003865
Bram Moolenaar4c137212021-04-19 16:48:48 +02003866 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003867 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003868 if (is_slice)
3869 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003870 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003871 else
3872 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003873 // single character (including composing characters).
3874 // If the index is too big or negative the result is
3875 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003876 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003877 vim_free(tv->vval.v_string);
3878 tv->vval.v_string = res;
3879 }
3880 break;
3881
3882 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003883 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003884 case ISN_BLOBINDEX:
3885 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003887 int is_slice = iptr->isn_type == ISN_LISTSLICE
3888 || iptr->isn_type == ISN_BLOBSLICE;
3889 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3890 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003891 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003892 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893
3894 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003895 // list slice: list is at stack-3, indexes at stack-2 and
3896 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003897 // Same for blob.
3898 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899
3900 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003901 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003903
3904 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 {
Bram Moolenaared591872020-08-15 22:14:53 +02003906 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003907 n1 = tv->vval.v_number;
3908 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 }
Bram Moolenaared591872020-08-15 22:14:53 +02003910
Bram Moolenaar4c137212021-04-19 16:48:48 +02003911 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003912 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003913 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003914 if (is_blob)
3915 {
3916 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3917 n1, n2, FALSE, tv) == FAIL)
3918 goto on_error;
3919 }
3920 else
3921 {
3922 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3923 n1, n2, FALSE, tv, TRUE) == FAIL)
3924 goto on_error;
3925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 }
3927 break;
3928
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003929 case ISN_ANYINDEX:
3930 case ISN_ANYSLICE:
3931 {
3932 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3933 typval_T *var1, *var2;
3934 int res;
3935
3936 // index: composite is at stack-2, index at stack-1
3937 // slice: composite is at stack-3, indexes at stack-2 and
3938 // stack-1
3939 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003940 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003941 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3942 goto on_error;
3943 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3944 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003945 res = eval_index_inner(tv, is_slice, var1, var2,
3946 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003947 clear_tv(var1);
3948 if (is_slice)
3949 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003950 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003951 if (res == FAIL)
3952 goto on_error;
3953 }
3954 break;
3955
Bram Moolenaar9af78762020-06-16 11:34:42 +02003956 case ISN_SLICE:
3957 {
3958 list_T *list;
3959 int count = iptr->isn_arg.number;
3960
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003961 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003962 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003963 list = tv->vval.v_list;
3964
3965 // no error for short list, expect it to be checked earlier
3966 if (list != NULL && list->lv_len >= count)
3967 {
3968 list_T *newlist = list_slice(list,
3969 count, list->lv_len - 1);
3970
3971 if (newlist != NULL)
3972 {
3973 list_unref(list);
3974 tv->vval.v_list = newlist;
3975 ++newlist->lv_refcount;
3976 }
3977 }
3978 }
3979 break;
3980
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003981 case ISN_GETITEM:
3982 {
3983 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003984 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003985
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003986 // Get list item: list is at stack-1, push item.
3987 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003988 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3989 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003990
Bram Moolenaar35578162021-08-02 19:10:38 +02003991 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003992 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003993 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003994 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003995
3996 // Useful when used in unpack assignment. Reset at
3997 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003998 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003999 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004000 }
4001 break;
4002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 case ISN_MEMBER:
4004 {
4005 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004006 char_u *key;
4007 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004008 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004009
4010 // dict member: dict is at stack-2, key at stack-1
4011 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004012 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004013 dict = tv->vval.v_dict;
4014
4015 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004016 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004017 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004018 if (key == NULL)
4019 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004020
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004021 if ((di = dict_find(dict, key, -1)) == NULL)
4022 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004023 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004024 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004025
4026 // If :silent! is used we will continue, make sure the
4027 // stack contents makes sense.
4028 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004029 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004030 tv = STACK_TV_BOT(-1);
4031 clear_tv(tv);
4032 tv->v_type = VAR_NUMBER;
4033 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004034 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004035 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004036 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004037 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004038 // Clear the dict only after getting the item, to avoid
4039 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004040 tv = STACK_TV_BOT(-1);
4041 temp_tv = *tv;
4042 copy_tv(&di->di_tv, tv);
4043 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004044 }
4045 break;
4046
4047 // dict member with string key
4048 case ISN_STRINGMEMBER:
4049 {
4050 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004052 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
4054 tv = STACK_TV_BOT(-1);
4055 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4056 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004057 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004059 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 }
4061 dict = tv->vval.v_dict;
4062
4063 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4064 == NULL)
4065 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004066 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004068 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004070 // Clear the dict after getting the item, to avoid that it
4071 // make the item invalid.
4072 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004074 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 }
4076 break;
4077
4078 case ISN_NEGATENR:
4079 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004080 if (tv->v_type != VAR_NUMBER
4081#ifdef FEAT_FLOAT
4082 && tv->v_type != VAR_FLOAT
4083#endif
4084 )
4085 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004086 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004087 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004088 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004089 }
4090#ifdef FEAT_FLOAT
4091 if (tv->v_type == VAR_FLOAT)
4092 tv->vval.v_float = -tv->vval.v_float;
4093 else
4094#endif
4095 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 break;
4097
4098 case ISN_CHECKNR:
4099 {
4100 int error = FALSE;
4101
4102 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004103 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004105 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 (void)tv_get_number_chk(tv, &error);
4107 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004108 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 }
4110 break;
4111
4112 case ISN_CHECKTYPE:
4113 {
4114 checktype_T *ct = &iptr->isn_arg.type;
4115
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004116 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004117 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004118 if (!ectx->ec_where.wt_variable)
4119 ectx->ec_where.wt_index = ct->ct_arg_idx;
4120 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4121 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004122 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004123 if (!ectx->ec_where.wt_variable)
4124 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004125
4126 // number 0 is FALSE, number 1 is TRUE
4127 if (tv->v_type == VAR_NUMBER
4128 && ct->ct_type->tt_type == VAR_BOOL
4129 && (tv->vval.v_number == 0
4130 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004132 tv->v_type = VAR_BOOL;
4133 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004134 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 }
4136 }
4137 break;
4138
Bram Moolenaar9af78762020-06-16 11:34:42 +02004139 case ISN_CHECKLEN:
4140 {
4141 int min_len = iptr->isn_arg.checklen.cl_min_len;
4142 list_T *list = NULL;
4143
4144 tv = STACK_TV_BOT(-1);
4145 if (tv->v_type == VAR_LIST)
4146 list = tv->vval.v_list;
4147 if (list == NULL || list->lv_len < min_len
4148 || (list->lv_len > min_len
4149 && !iptr->isn_arg.checklen.cl_more_OK))
4150 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004151 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004152 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004153 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004154 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004155 }
4156 }
4157 break;
4158
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004159 case ISN_SETTYPE:
4160 {
4161 checktype_T *ct = &iptr->isn_arg.type;
4162
4163 tv = STACK_TV_BOT(-1);
4164 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4165 {
4166 free_type(tv->vval.v_dict->dv_type);
4167 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4168 }
4169 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4170 {
4171 free_type(tv->vval.v_list->lv_type);
4172 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4173 }
4174 }
4175 break;
4176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004178 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 {
4180 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004181 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004183 if (iptr->isn_type == ISN_2BOOL)
4184 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004185 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004186 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004187 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004188 n = !n;
4189 }
4190 else
4191 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004192 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004193 SOURCING_LNUM = iptr->isn_lnum;
4194 n = tv_get_bool_chk(tv, &error);
4195 if (error)
4196 goto on_error;
4197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 clear_tv(tv);
4199 tv->v_type = VAR_BOOL;
4200 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4201 }
4202 break;
4203
4204 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004205 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004206 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004207 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4208 iptr->isn_type == ISN_2STRING_ANY,
4209 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004210 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 break;
4212
Bram Moolenaar08597872020-12-10 19:43:40 +01004213 case ISN_RANGE:
4214 {
4215 exarg_T ea;
4216 char *errormsg;
4217
Bram Moolenaarece0b872021-01-08 20:40:45 +01004218 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004219 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004220 ea.addr_type = ADDR_LINES;
4221 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004222 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004223 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004224 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004225
Bram Moolenaar35578162021-08-02 19:10:38 +02004226 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004227 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004228 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004229 tv = STACK_TV_BOT(-1);
4230 tv->v_type = VAR_NUMBER;
4231 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004232 if (ea.addr_count == 0)
4233 tv->vval.v_number = curwin->w_cursor.lnum;
4234 else
4235 tv->vval.v_number = ea.line2;
4236 }
4237 break;
4238
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004239 case ISN_PUT:
4240 {
4241 int regname = iptr->isn_arg.put.put_regname;
4242 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4243 char_u *expr = NULL;
4244 int dir = FORWARD;
4245
Bram Moolenaar08597872020-12-10 19:43:40 +01004246 if (lnum < -2)
4247 {
4248 // line number was put on the stack by ISN_RANGE
4249 tv = STACK_TV_BOT(-1);
4250 curwin->w_cursor.lnum = tv->vval.v_number;
4251 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4252 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004253 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004254 }
4255 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004256 // :put! above cursor
4257 dir = BACKWARD;
4258 else if (lnum >= 0)
4259 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004260
4261 if (regname == '=')
4262 {
4263 tv = STACK_TV_BOT(-1);
4264 if (tv->v_type == VAR_STRING)
4265 expr = tv->vval.v_string;
4266 else
4267 {
4268 expr = typval2string(tv, TRUE); // allocates value
4269 clear_tv(tv);
4270 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004271 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004272 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004273 check_cursor();
4274 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4275 vim_free(expr);
4276 }
4277 break;
4278
Bram Moolenaar02194d22020-10-24 23:08:38 +02004279 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004280 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4281 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4282 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4283 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004284 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4285 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004286 break;
4287
Bram Moolenaar02194d22020-10-24 23:08:38 +02004288 case ISN_CMDMOD_REV:
4289 // filter regprog is owned by the instruction, don't free it
4290 cmdmod.cmod_filter_regmatch.regprog = NULL;
4291 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004292 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4293 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004294 break;
4295
Bram Moolenaar792f7862020-11-23 08:31:18 +01004296 case ISN_UNPACK:
4297 {
4298 int count = iptr->isn_arg.unpack.unp_count;
4299 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4300 list_T *l;
4301 listitem_T *li;
4302 int i;
4303
4304 // Check there is a valid list to unpack.
4305 tv = STACK_TV_BOT(-1);
4306 if (tv->v_type != VAR_LIST)
4307 {
4308 SOURCING_LNUM = iptr->isn_lnum;
4309 emsg(_(e_for_argument_must_be_sequence_of_lists));
4310 goto on_error;
4311 }
4312 l = tv->vval.v_list;
4313 if (l == NULL
4314 || l->lv_len < (semicolon ? count - 1 : count))
4315 {
4316 SOURCING_LNUM = iptr->isn_lnum;
4317 emsg(_(e_list_value_does_not_have_enough_items));
4318 goto on_error;
4319 }
4320 else if (!semicolon && l->lv_len > count)
4321 {
4322 SOURCING_LNUM = iptr->isn_lnum;
4323 emsg(_(e_list_value_has_more_items_than_targets));
4324 goto on_error;
4325 }
4326
4327 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004328 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004329 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004330 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004331
4332 // Variable after semicolon gets a list with the remaining
4333 // items.
4334 if (semicolon)
4335 {
4336 list_T *rem_list =
4337 list_alloc_with_items(l->lv_len - count + 1);
4338
4339 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004340 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004341 tv = STACK_TV_BOT(-count);
4342 tv->vval.v_list = rem_list;
4343 ++rem_list->lv_refcount;
4344 tv->v_lock = 0;
4345 li = l->lv_first;
4346 for (i = 0; i < count - 1; ++i)
4347 li = li->li_next;
4348 for (i = 0; li != NULL; ++i)
4349 {
4350 list_set_item(rem_list, i, &li->li_tv);
4351 li = li->li_next;
4352 }
4353 --count;
4354 }
4355
4356 // Produce the values in reverse order, first item last.
4357 li = l->lv_first;
4358 for (i = 0; i < count; ++i)
4359 {
4360 tv = STACK_TV_BOT(-i - 1);
4361 copy_tv(&li->li_tv, tv);
4362 li = li->li_next;
4363 }
4364
4365 list_unref(l);
4366 }
4367 break;
4368
Bram Moolenaarb2049902021-01-24 12:53:53 +01004369 case ISN_PROF_START:
4370 case ISN_PROF_END:
4371 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004372#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004373 funccall_T cookie;
4374 ufunc_T *cur_ufunc =
4375 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004376 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004377
4378 cookie.func = cur_ufunc;
4379 if (iptr->isn_type == ISN_PROF_START)
4380 {
4381 func_line_start(&cookie, iptr->isn_lnum);
4382 // if we get here the instruction is executed
4383 func_line_exec(&cookie);
4384 }
4385 else
4386 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004387#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004388 }
4389 break;
4390
Bram Moolenaare99d4222021-06-13 14:01:26 +02004391 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004392 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004393 break;
4394
Bram Moolenaar389df252020-07-09 21:20:47 +02004395 case ISN_SHUFFLE:
4396 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004397 typval_T tmp_tv;
4398 int item = iptr->isn_arg.shuffle.shfl_item;
4399 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004400
4401 tmp_tv = *STACK_TV_BOT(-item);
4402 for ( ; up > 0 && item > 1; --up)
4403 {
4404 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4405 --item;
4406 }
4407 *STACK_TV_BOT(-item) = tmp_tv;
4408 }
4409 break;
4410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004412 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004414 ectx->ec_where.wt_index = 0;
4415 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 break;
4417 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004418 continue;
4419
Bram Moolenaard032f342020-07-18 18:13:02 +02004420func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004421 // Restore previous function. If the frame pointer is where we started
4422 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004423 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004424 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004425
Bram Moolenaar4c137212021-04-19 16:48:48 +02004426 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004427 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004428 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004429 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004430
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004431on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004432 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004433 // If "emsg_silent" is set then ignore the error, unless it was set
4434 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004435 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004436 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004437 {
4438 // If a sequence of instructions causes an error while ":silent!"
4439 // was used, restore the stack length and jump ahead to restoring
4440 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004441 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004442 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004443 while (ectx->ec_stack.ga_len
4444 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004445 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004446 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004447 clear_tv(STACK_TV_BOT(0));
4448 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004449 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4450 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004451 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004452 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004453 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004454on_fatal_error:
4455 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004456 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004457 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004458 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 }
4460
4461done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004462 ret = OK;
4463theend:
4464 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4465 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004466}
4467
4468/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004469 * Execute the instructions from a VAR_INSTR typeval and put the result in
4470 * "rettv".
4471 * Return OK or FAIL.
4472 */
4473 int
4474exe_typval_instr(typval_T *tv, typval_T *rettv)
4475{
4476 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4477 isn_T *save_instr = ectx->ec_instr;
4478 int save_iidx = ectx->ec_iidx;
4479 int res;
4480
4481 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4482 res = exec_instructions(ectx);
4483 if (res == OK)
4484 {
4485 *rettv = *STACK_TV_BOT(-1);
4486 --ectx->ec_stack.ga_len;
4487 }
4488
4489 ectx->ec_instr = save_instr;
4490 ectx->ec_iidx = save_iidx;
4491
4492 return res;
4493}
4494
4495/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004496 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4497 * "substitute_instr".
4498 */
4499 char_u *
4500exe_substitute_instr(void)
4501{
4502 ectx_T *ectx = substitute_instr->subs_ectx;
4503 isn_T *save_instr = ectx->ec_instr;
4504 int save_iidx = ectx->ec_iidx;
4505 char_u *res;
4506
4507 ectx->ec_instr = substitute_instr->subs_instr;
4508 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004509 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004510 typval_T *tv = STACK_TV_BOT(-1);
4511
Bram Moolenaar27523602021-06-05 21:36:19 +02004512 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004513 --ectx->ec_stack.ga_len;
4514 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004515 }
4516 else
4517 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004518 substitute_instr->subs_status = FAIL;
4519 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521
Bram Moolenaar4c137212021-04-19 16:48:48 +02004522 ectx->ec_instr = save_instr;
4523 ectx->ec_iidx = save_iidx;
4524
4525 return res;
4526}
4527
4528/*
4529 * Call a "def" function from old Vim script.
4530 * Return OK or FAIL.
4531 */
4532 int
4533call_def_function(
4534 ufunc_T *ufunc,
4535 int argc_arg, // nr of arguments
4536 typval_T *argv, // arguments
4537 partial_T *partial, // optional partial for context
4538 typval_T *rettv) // return value
4539{
4540 ectx_T ectx; // execution context
4541 int argc = argc_arg;
4542 typval_T *tv;
4543 int idx;
4544 int ret = FAIL;
4545 int defcount = ufunc->uf_args.ga_len - argc;
4546 sctx_T save_current_sctx = current_sctx;
4547 int did_emsg_before = did_emsg_cumul + did_emsg;
4548 int save_suppress_errthrow = suppress_errthrow;
4549 msglist_T **saved_msg_list = NULL;
4550 msglist_T *private_msg_list = NULL;
4551 int save_emsg_silent_def = emsg_silent_def;
4552 int save_did_emsg_def = did_emsg_def;
4553 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004554 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004555
4556// Get pointer to item in the stack.
4557#undef STACK_TV
4558#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4559
4560// Get pointer to item at the bottom of the stack, -1 is the bottom.
4561#undef STACK_TV_BOT
4562#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4563
4564// Get pointer to a local variable on the stack. Negative for arguments.
4565#undef STACK_TV_VAR
4566#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4567
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004568 // Update uf_has_breakpoint if needed.
4569 update_has_breakpoint(ufunc);
4570
Bram Moolenaar4c137212021-04-19 16:48:48 +02004571 if (ufunc->uf_def_status == UF_NOT_COMPILED
4572 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004573 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4574 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004575 == FAIL))
4576 {
4577 if (did_emsg_cumul + did_emsg == did_emsg_before)
4578 semsg(_(e_function_is_not_compiled_str),
4579 printable_func_name(ufunc));
4580 return FAIL;
4581 }
4582
4583 {
4584 // Check the function was really compiled.
4585 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4586 + ufunc->uf_dfunc_idx;
4587 if (INSTRUCTIONS(dfunc) == NULL)
4588 {
4589 iemsg("using call_def_function() on not compiled function");
4590 return FAIL;
4591 }
4592 }
4593
4594 // If depth of calling is getting too high, don't execute the function.
4595 orig_funcdepth = funcdepth_get();
4596 if (funcdepth_increment() == FAIL)
4597 return FAIL;
4598
4599 CLEAR_FIELD(ectx);
4600 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4601 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004602 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004603 {
4604 funcdepth_decrement();
4605 return FAIL;
4606 }
4607 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4608 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4609 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004610 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004611
4612 idx = argc - ufunc->uf_args.ga_len;
4613 if (idx > 0 && ufunc->uf_va_name == NULL)
4614 {
4615 if (idx == 1)
4616 emsg(_(e_one_argument_too_many));
4617 else
4618 semsg(_(e_nr_arguments_too_many), idx);
4619 goto failed_early;
4620 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004621 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4622 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004623 {
4624 if (idx == -1)
4625 emsg(_(e_one_argument_too_few));
4626 else
4627 semsg(_(e_nr_arguments_too_few), -idx);
4628 goto failed_early;
4629 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004630
4631 // Put arguments on the stack, but no more than what the function expects.
4632 // A lambda can be called with more arguments than it uses.
4633 for (idx = 0; idx < argc
4634 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4635 ++idx)
4636 {
4637 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4638 && argv[idx].v_type == VAR_SPECIAL
4639 && argv[idx].vval.v_number == VVAL_NONE)
4640 {
4641 // Use the default value.
4642 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4643 }
4644 else
4645 {
4646 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4647 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004648 ufunc->uf_arg_types[idx], &argv[idx],
4649 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004650 goto failed_early;
4651 copy_tv(&argv[idx], STACK_TV_BOT(0));
4652 }
4653 ++ectx.ec_stack.ga_len;
4654 }
4655
4656 // Turn varargs into a list. Empty list if no args.
4657 if (ufunc->uf_va_name != NULL)
4658 {
4659 int vararg_count = argc - ufunc->uf_args.ga_len;
4660
4661 if (vararg_count < 0)
4662 vararg_count = 0;
4663 else
4664 argc -= vararg_count;
4665 if (exe_newlist(vararg_count, &ectx) == FAIL)
4666 goto failed_early;
4667
4668 // Check the type of the list items.
4669 tv = STACK_TV_BOT(-1);
4670 if (ufunc->uf_va_type != NULL
4671 && ufunc->uf_va_type != &t_list_any
4672 && ufunc->uf_va_type->tt_member != &t_any
4673 && tv->vval.v_list != NULL)
4674 {
4675 type_T *expected = ufunc->uf_va_type->tt_member;
4676 listitem_T *li = tv->vval.v_list->lv_first;
4677
4678 for (idx = 0; idx < vararg_count; ++idx)
4679 {
4680 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004681 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004682 goto failed_early;
4683 li = li->li_next;
4684 }
4685 }
4686
4687 if (defcount > 0)
4688 // Move varargs list to below missing default arguments.
4689 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4690 --ectx.ec_stack.ga_len;
4691 }
4692
4693 // Make space for omitted arguments, will store default value below.
4694 // Any varargs list goes after them.
4695 if (defcount > 0)
4696 for (idx = 0; idx < defcount; ++idx)
4697 {
4698 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4699 ++ectx.ec_stack.ga_len;
4700 }
4701 if (ufunc->uf_va_name != NULL)
4702 ++ectx.ec_stack.ga_len;
4703
4704 // Frame pointer points to just after arguments.
4705 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4706 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4707
4708 {
4709 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4710 + ufunc->uf_dfunc_idx;
4711 ufunc_T *base_ufunc = dfunc->df_ufunc;
4712
4713 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4714 // by copy_func().
4715 if (partial != NULL || base_ufunc->uf_partial != NULL)
4716 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004717 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4718 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004719 goto failed_early;
4720 if (partial != NULL)
4721 {
4722 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4723 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004724 if (current_ectx->ec_outer_ref != NULL
4725 && current_ectx->ec_outer_ref->or_outer != NULL)
4726 ectx.ec_outer_ref->or_outer =
4727 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004728 }
4729 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004730 {
4731 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4732 ++partial->pt_refcount;
4733 ectx.ec_outer_ref->or_partial = partial;
4734 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004735 }
4736 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004737 {
4738 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4739 ++base_ufunc->uf_partial->pt_refcount;
4740 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4741 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004742 }
4743 }
4744
4745 // dummy frame entries
4746 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4747 {
4748 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4749 ++ectx.ec_stack.ga_len;
4750 }
4751
4752 {
4753 // Reserve space for local variables and any closure reference count.
4754 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4755 + ufunc->uf_dfunc_idx;
4756
4757 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4758 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4759 ectx.ec_stack.ga_len += dfunc->df_varcount;
4760 if (dfunc->df_has_closure)
4761 {
4762 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4763 STACK_TV_VAR(idx)->vval.v_number = 0;
4764 ++ectx.ec_stack.ga_len;
4765 }
4766
4767 ectx.ec_instr = INSTRUCTIONS(dfunc);
4768 }
4769
4770 // Following errors are in the function, not the caller.
4771 // Commands behave like vim9script.
4772 estack_push_ufunc(ufunc, 1);
4773 current_sctx = ufunc->uf_script_ctx;
4774 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4775
4776 // Use a specific location for storing error messages to be converted to an
4777 // exception.
4778 saved_msg_list = msg_list;
4779 msg_list = &private_msg_list;
4780
4781 // Do turn errors into exceptions.
4782 suppress_errthrow = FALSE;
4783
4784 // Do not delete the function while executing it.
4785 ++ufunc->uf_calls;
4786
4787 // When ":silent!" was used before calling then we still abort the
4788 // function. If ":silent!" is used in the function then we don't.
4789 emsg_silent_def = emsg_silent;
4790 did_emsg_def = 0;
4791
4792 ectx.ec_where.wt_index = 0;
4793 ectx.ec_where.wt_variable = FALSE;
4794
4795 // Execute the instructions until done.
4796 ret = exec_instructions(&ectx);
4797 if (ret == OK)
4798 {
4799 // function finished, get result from the stack.
4800 if (ufunc->uf_ret_type == &t_void)
4801 {
4802 rettv->v_type = VAR_VOID;
4803 }
4804 else
4805 {
4806 tv = STACK_TV_BOT(-1);
4807 *rettv = *tv;
4808 tv->v_type = VAR_UNKNOWN;
4809 }
4810 }
4811
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004812 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004813 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4814 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004815
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004816 // Deal with any remaining closures, they may be in use somewhere.
4817 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004818 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004819 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004820 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4821 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004822
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004823 estack_pop();
4824 current_sctx = save_current_sctx;
4825
Bram Moolenaarc970e422021-03-17 15:03:04 +01004826 // TODO: when is it safe to delete the function if it is no longer used?
4827 --ufunc->uf_calls;
4828
Bram Moolenaar352134b2020-10-17 22:04:08 +02004829 if (*msg_list != NULL && saved_msg_list != NULL)
4830 {
4831 msglist_T **plist = saved_msg_list;
4832
4833 // Append entries from the current msg_list (uncaught exceptions) to
4834 // the saved msg_list.
4835 while (*plist != NULL)
4836 plist = &(*plist)->next;
4837
4838 *plist = *msg_list;
4839 }
4840 msg_list = saved_msg_list;
4841
Bram Moolenaar4c137212021-04-19 16:48:48 +02004842 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004843 {
4844 cmdmod.cmod_filter_regmatch.regprog = NULL;
4845 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004846 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004847 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004848 emsg_silent_def = save_emsg_silent_def;
4849 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004850
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004851failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004852 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4854 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004855 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004858 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004859 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004860 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004861 if (ectx.ec_outer_ref->or_outer_allocated)
4862 vim_free(ectx.ec_outer_ref->or_outer);
4863 partial_unref(ectx.ec_outer_ref->or_partial);
4864 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004865 }
4866
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004867 // Not sure if this is necessary.
4868 suppress_errthrow = save_suppress_errthrow;
4869
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004870 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
4871 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004872 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004873 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004874 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 return ret;
4876}
4877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004879 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4880 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4881 * "pfx" is prefixed to every line.
4882 */
4883 static void
4884list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4885{
4886 int line_idx = 0;
4887 int prev_current = 0;
4888 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004889 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004890
4891 for (current = 0; current < instr_count; ++current)
4892 {
4893 isn_T *iptr = &instr[current];
4894 char *line;
4895
4896 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004897 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004898 while (line_idx < iptr->isn_lnum
4899 && line_idx < ufunc->uf_lines.ga_len)
4900 {
4901 if (current > prev_current)
4902 {
4903 msg_puts("\n\n");
4904 prev_current = current;
4905 }
4906 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4907 if (line != NULL)
4908 msg(line);
4909 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004910 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4911 {
4912 int first_def_arg = ufunc->uf_args.ga_len
4913 - ufunc->uf_def_args.ga_len;
4914
4915 if (def_arg_idx > 0)
4916 msg_puts("\n\n");
4917 msg_start();
4918 msg_puts(" ");
4919 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4920 first_def_arg + def_arg_idx]);
4921 msg_puts(" = ");
4922 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4923 msg_clr_eos();
4924 msg_end();
4925 }
4926 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004927
4928 switch (iptr->isn_type)
4929 {
4930 case ISN_EXEC:
4931 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4932 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004933 case ISN_EXEC_SPLIT:
4934 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4935 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004936 case ISN_LEGACY_EVAL:
4937 smsg("%s%4d EVAL legacy %s", pfx, current,
4938 iptr->isn_arg.string);
4939 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004940 case ISN_REDIRSTART:
4941 smsg("%s%4d REDIR", pfx, current);
4942 break;
4943 case ISN_REDIREND:
4944 smsg("%s%4d REDIR END%s", pfx, current,
4945 iptr->isn_arg.number ? " append" : "");
4946 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004947 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004948#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004949 smsg("%s%4d CEXPR pre %s", pfx, current,
4950 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004951#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004952 break;
4953 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004954#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004955 {
4956 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4957
4958 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4959 cexpr_get_auname(cer->cer_cmdidx),
4960 cer->cer_forceit ? "!" : "",
4961 cer->cer_cmdline);
4962 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004963#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004964 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004965 case ISN_INSTR:
4966 {
4967 smsg("%s%4d INSTR", pfx, current);
4968 list_instructions(" ", iptr->isn_arg.instr,
4969 INT_MAX, NULL);
4970 msg(" -------------");
4971 }
4972 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004973 case ISN_SUBSTITUTE:
4974 {
4975 subs_T *subs = &iptr->isn_arg.subs;
4976
4977 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4978 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4979 msg(" -------------");
4980 }
4981 break;
4982 case ISN_EXECCONCAT:
4983 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4984 (varnumber_T)iptr->isn_arg.number);
4985 break;
4986 case ISN_ECHO:
4987 {
4988 echo_T *echo = &iptr->isn_arg.echo;
4989
4990 smsg("%s%4d %s %d", pfx, current,
4991 echo->echo_with_white ? "ECHO" : "ECHON",
4992 echo->echo_count);
4993 }
4994 break;
4995 case ISN_EXECUTE:
4996 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02004997 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004998 break;
4999 case ISN_ECHOMSG:
5000 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005001 (varnumber_T)(iptr->isn_arg.number));
5002 break;
5003 case ISN_ECHOCONSOLE:
5004 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5005 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005006 break;
5007 case ISN_ECHOERR:
5008 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005009 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005010 break;
5011 case ISN_LOAD:
5012 {
5013 if (iptr->isn_arg.number < 0)
5014 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5015 (varnumber_T)(iptr->isn_arg.number
5016 + STACK_FRAME_SIZE));
5017 else
5018 smsg("%s%4d LOAD $%lld", pfx, current,
5019 (varnumber_T)(iptr->isn_arg.number));
5020 }
5021 break;
5022 case ISN_LOADOUTER:
5023 {
5024 if (iptr->isn_arg.number < 0)
5025 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5026 iptr->isn_arg.outer.outer_depth,
5027 iptr->isn_arg.outer.outer_idx
5028 + STACK_FRAME_SIZE);
5029 else
5030 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5031 iptr->isn_arg.outer.outer_depth,
5032 iptr->isn_arg.outer.outer_idx);
5033 }
5034 break;
5035 case ISN_LOADV:
5036 smsg("%s%4d LOADV v:%s", pfx, current,
5037 get_vim_var_name(iptr->isn_arg.number));
5038 break;
5039 case ISN_LOADSCRIPT:
5040 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005041 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5042 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5043 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005044
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005045 sv = get_script_svar(sref, -1);
5046 if (sv == NULL)
5047 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5048 pfx, current, si->sn_name);
5049 else
5050 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005051 sv->sv_name,
5052 sref->sref_idx,
5053 si->sn_name);
5054 }
5055 break;
5056 case ISN_LOADS:
5057 {
5058 scriptitem_T *si = SCRIPT_ITEM(
5059 iptr->isn_arg.loadstore.ls_sid);
5060
5061 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5062 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5063 }
5064 break;
5065 case ISN_LOADAUTO:
5066 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5067 break;
5068 case ISN_LOADG:
5069 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5070 break;
5071 case ISN_LOADB:
5072 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5073 break;
5074 case ISN_LOADW:
5075 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5076 break;
5077 case ISN_LOADT:
5078 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5079 break;
5080 case ISN_LOADGDICT:
5081 smsg("%s%4d LOAD g:", pfx, current);
5082 break;
5083 case ISN_LOADBDICT:
5084 smsg("%s%4d LOAD b:", pfx, current);
5085 break;
5086 case ISN_LOADWDICT:
5087 smsg("%s%4d LOAD w:", pfx, current);
5088 break;
5089 case ISN_LOADTDICT:
5090 smsg("%s%4d LOAD t:", pfx, current);
5091 break;
5092 case ISN_LOADOPT:
5093 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5094 break;
5095 case ISN_LOADENV:
5096 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5097 break;
5098 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005099 smsg("%s%4d LOADREG @%c", pfx, current,
5100 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005101 break;
5102
5103 case ISN_STORE:
5104 if (iptr->isn_arg.number < 0)
5105 smsg("%s%4d STORE arg[%lld]", pfx, current,
5106 iptr->isn_arg.number + STACK_FRAME_SIZE);
5107 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005108 smsg("%s%4d STORE $%lld", pfx, current,
5109 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005110 break;
5111 case ISN_STOREOUTER:
5112 {
5113 if (iptr->isn_arg.number < 0)
5114 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5115 iptr->isn_arg.outer.outer_depth,
5116 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5117 else
5118 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5119 iptr->isn_arg.outer.outer_depth,
5120 iptr->isn_arg.outer.outer_idx);
5121 }
5122 break;
5123 case ISN_STOREV:
5124 smsg("%s%4d STOREV v:%s", pfx, current,
5125 get_vim_var_name(iptr->isn_arg.number));
5126 break;
5127 case ISN_STOREAUTO:
5128 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5129 break;
5130 case ISN_STOREG:
5131 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5132 break;
5133 case ISN_STOREB:
5134 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5135 break;
5136 case ISN_STOREW:
5137 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5138 break;
5139 case ISN_STORET:
5140 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5141 break;
5142 case ISN_STORES:
5143 {
5144 scriptitem_T *si = SCRIPT_ITEM(
5145 iptr->isn_arg.loadstore.ls_sid);
5146
5147 smsg("%s%4d STORES %s in %s", pfx, current,
5148 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5149 }
5150 break;
5151 case ISN_STORESCRIPT:
5152 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005153 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5154 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5155 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005156
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005157 sv = get_script_svar(sref, -1);
5158 if (sv == NULL)
5159 smsg("%s%4d STORESCRIPT [deleted] in %s",
5160 pfx, current, si->sn_name);
5161 else
5162 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005163 sv->sv_name,
5164 sref->sref_idx,
5165 si->sn_name);
5166 }
5167 break;
5168 case ISN_STOREOPT:
5169 smsg("%s%4d STOREOPT &%s", pfx, current,
5170 iptr->isn_arg.storeopt.so_name);
5171 break;
5172 case ISN_STOREENV:
5173 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5174 break;
5175 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005176 smsg("%s%4d STOREREG @%c", pfx, current,
5177 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005178 break;
5179 case ISN_STORENR:
5180 smsg("%s%4d STORE %lld in $%d", pfx, current,
5181 iptr->isn_arg.storenr.stnr_val,
5182 iptr->isn_arg.storenr.stnr_idx);
5183 break;
5184
5185 case ISN_STOREINDEX:
5186 smsg("%s%4d STOREINDEX %s", pfx, current,
5187 vartype_name(iptr->isn_arg.vartype));
5188 break;
5189
5190 case ISN_STORERANGE:
5191 smsg("%s%4d STORERANGE", pfx, current);
5192 break;
5193
5194 // constants
5195 case ISN_PUSHNR:
5196 smsg("%s%4d PUSHNR %lld", pfx, current,
5197 (varnumber_T)(iptr->isn_arg.number));
5198 break;
5199 case ISN_PUSHBOOL:
5200 case ISN_PUSHSPEC:
5201 smsg("%s%4d PUSH %s", pfx, current,
5202 get_var_special_name(iptr->isn_arg.number));
5203 break;
5204 case ISN_PUSHF:
5205#ifdef FEAT_FLOAT
5206 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5207#endif
5208 break;
5209 case ISN_PUSHS:
5210 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5211 break;
5212 case ISN_PUSHBLOB:
5213 {
5214 char_u *r;
5215 char_u numbuf[NUMBUFLEN];
5216 char_u *tofree;
5217
5218 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5219 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5220 vim_free(tofree);
5221 }
5222 break;
5223 case ISN_PUSHFUNC:
5224 {
5225 char *name = (char *)iptr->isn_arg.string;
5226
5227 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5228 name == NULL ? "[none]" : name);
5229 }
5230 break;
5231 case ISN_PUSHCHANNEL:
5232#ifdef FEAT_JOB_CHANNEL
5233 {
5234 channel_T *channel = iptr->isn_arg.channel;
5235
5236 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5237 channel == NULL ? 0 : channel->ch_id);
5238 }
5239#endif
5240 break;
5241 case ISN_PUSHJOB:
5242#ifdef FEAT_JOB_CHANNEL
5243 {
5244 typval_T tv;
5245 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005246 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005247
5248 tv.v_type = VAR_JOB;
5249 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005250 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005251 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5252 }
5253#endif
5254 break;
5255 case ISN_PUSHEXC:
5256 smsg("%s%4d PUSH v:exception", pfx, current);
5257 break;
5258 case ISN_UNLET:
5259 smsg("%s%4d UNLET%s %s", pfx, current,
5260 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5261 iptr->isn_arg.unlet.ul_name);
5262 break;
5263 case ISN_UNLETENV:
5264 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5265 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5266 iptr->isn_arg.unlet.ul_name);
5267 break;
5268 case ISN_UNLETINDEX:
5269 smsg("%s%4d UNLETINDEX", pfx, current);
5270 break;
5271 case ISN_UNLETRANGE:
5272 smsg("%s%4d UNLETRANGE", pfx, current);
5273 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005274 case ISN_LOCKUNLOCK:
5275 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5276 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005277 case ISN_LOCKCONST:
5278 smsg("%s%4d LOCKCONST", pfx, current);
5279 break;
5280 case ISN_NEWLIST:
5281 smsg("%s%4d NEWLIST size %lld", pfx, current,
5282 (varnumber_T)(iptr->isn_arg.number));
5283 break;
5284 case ISN_NEWDICT:
5285 smsg("%s%4d NEWDICT size %lld", pfx, current,
5286 (varnumber_T)(iptr->isn_arg.number));
5287 break;
5288
5289 // function call
5290 case ISN_BCALL:
5291 {
5292 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5293
5294 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5295 internal_func_name(cbfunc->cbf_idx),
5296 cbfunc->cbf_argcount);
5297 }
5298 break;
5299 case ISN_DCALL:
5300 {
5301 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5302 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5303 + cdfunc->cdf_idx;
5304
5305 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005306 printable_func_name(df->df_ufunc),
5307 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005308 }
5309 break;
5310 case ISN_UCALL:
5311 {
5312 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5313
5314 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5315 cufunc->cuf_name, cufunc->cuf_argcount);
5316 }
5317 break;
5318 case ISN_PCALL:
5319 {
5320 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5321
5322 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5323 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5324 }
5325 break;
5326 case ISN_PCALL_END:
5327 smsg("%s%4d PCALL end", pfx, current);
5328 break;
5329 case ISN_RETURN:
5330 smsg("%s%4d RETURN", pfx, current);
5331 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005332 case ISN_RETURN_VOID:
5333 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005334 break;
5335 case ISN_FUNCREF:
5336 {
5337 funcref_T *funcref = &iptr->isn_arg.funcref;
5338 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5339 + funcref->fr_func;
5340
5341 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5342 }
5343 break;
5344
5345 case ISN_NEWFUNC:
5346 {
5347 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5348
5349 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5350 newfunc->nf_lambda, newfunc->nf_global);
5351 }
5352 break;
5353
5354 case ISN_DEF:
5355 {
5356 char_u *name = iptr->isn_arg.string;
5357
5358 smsg("%s%4d DEF %s", pfx, current,
5359 name == NULL ? (char_u *)"" : name);
5360 }
5361 break;
5362
5363 case ISN_JUMP:
5364 {
5365 char *when = "?";
5366
5367 switch (iptr->isn_arg.jump.jump_when)
5368 {
5369 case JUMP_ALWAYS:
5370 when = "JUMP";
5371 break;
5372 case JUMP_AND_KEEP_IF_TRUE:
5373 when = "JUMP_AND_KEEP_IF_TRUE";
5374 break;
5375 case JUMP_IF_FALSE:
5376 when = "JUMP_IF_FALSE";
5377 break;
5378 case JUMP_AND_KEEP_IF_FALSE:
5379 when = "JUMP_AND_KEEP_IF_FALSE";
5380 break;
5381 case JUMP_IF_COND_FALSE:
5382 when = "JUMP_IF_COND_FALSE";
5383 break;
5384 case JUMP_IF_COND_TRUE:
5385 when = "JUMP_IF_COND_TRUE";
5386 break;
5387 }
5388 smsg("%s%4d %s -> %d", pfx, current, when,
5389 iptr->isn_arg.jump.jump_where);
5390 }
5391 break;
5392
5393 case ISN_JUMP_IF_ARG_SET:
5394 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5395 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5396 iptr->isn_arg.jump.jump_where);
5397 break;
5398
5399 case ISN_FOR:
5400 {
5401 forloop_T *forloop = &iptr->isn_arg.forloop;
5402
5403 smsg("%s%4d FOR $%d -> %d", pfx, current,
5404 forloop->for_idx, forloop->for_end);
5405 }
5406 break;
5407
5408 case ISN_TRY:
5409 {
5410 try_T *try = &iptr->isn_arg.try;
5411
5412 if (try->try_ref->try_finally == 0)
5413 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5414 pfx, current,
5415 try->try_ref->try_catch,
5416 try->try_ref->try_endtry);
5417 else
5418 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5419 pfx, current,
5420 try->try_ref->try_catch,
5421 try->try_ref->try_finally,
5422 try->try_ref->try_endtry);
5423 }
5424 break;
5425 case ISN_CATCH:
5426 // TODO
5427 smsg("%s%4d CATCH", pfx, current);
5428 break;
5429 case ISN_TRYCONT:
5430 {
5431 trycont_T *trycont = &iptr->isn_arg.trycont;
5432
5433 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5434 trycont->tct_levels,
5435 trycont->tct_levels == 1 ? "" : "s",
5436 trycont->tct_where);
5437 }
5438 break;
5439 case ISN_FINALLY:
5440 smsg("%s%4d FINALLY", pfx, current);
5441 break;
5442 case ISN_ENDTRY:
5443 smsg("%s%4d ENDTRY", pfx, current);
5444 break;
5445 case ISN_THROW:
5446 smsg("%s%4d THROW", pfx, current);
5447 break;
5448
5449 // expression operations on number
5450 case ISN_OPNR:
5451 case ISN_OPFLOAT:
5452 case ISN_OPANY:
5453 {
5454 char *what;
5455 char *ins;
5456
5457 switch (iptr->isn_arg.op.op_type)
5458 {
5459 case EXPR_MULT: what = "*"; break;
5460 case EXPR_DIV: what = "/"; break;
5461 case EXPR_REM: what = "%"; break;
5462 case EXPR_SUB: what = "-"; break;
5463 case EXPR_ADD: what = "+"; break;
5464 default: what = "???"; break;
5465 }
5466 switch (iptr->isn_type)
5467 {
5468 case ISN_OPNR: ins = "OPNR"; break;
5469 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5470 case ISN_OPANY: ins = "OPANY"; break;
5471 default: ins = "???"; break;
5472 }
5473 smsg("%s%4d %s %s", pfx, current, ins, what);
5474 }
5475 break;
5476
5477 case ISN_COMPAREBOOL:
5478 case ISN_COMPARESPECIAL:
5479 case ISN_COMPARENR:
5480 case ISN_COMPAREFLOAT:
5481 case ISN_COMPARESTRING:
5482 case ISN_COMPAREBLOB:
5483 case ISN_COMPARELIST:
5484 case ISN_COMPAREDICT:
5485 case ISN_COMPAREFUNC:
5486 case ISN_COMPAREANY:
5487 {
5488 char *p;
5489 char buf[10];
5490 char *type;
5491
5492 switch (iptr->isn_arg.op.op_type)
5493 {
5494 case EXPR_EQUAL: p = "=="; break;
5495 case EXPR_NEQUAL: p = "!="; break;
5496 case EXPR_GREATER: p = ">"; break;
5497 case EXPR_GEQUAL: p = ">="; break;
5498 case EXPR_SMALLER: p = "<"; break;
5499 case EXPR_SEQUAL: p = "<="; break;
5500 case EXPR_MATCH: p = "=~"; break;
5501 case EXPR_IS: p = "is"; break;
5502 case EXPR_ISNOT: p = "isnot"; break;
5503 case EXPR_NOMATCH: p = "!~"; break;
5504 default: p = "???"; break;
5505 }
5506 STRCPY(buf, p);
5507 if (iptr->isn_arg.op.op_ic == TRUE)
5508 strcat(buf, "?");
5509 switch(iptr->isn_type)
5510 {
5511 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5512 case ISN_COMPARESPECIAL:
5513 type = "COMPARESPECIAL"; break;
5514 case ISN_COMPARENR: type = "COMPARENR"; break;
5515 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5516 case ISN_COMPARESTRING:
5517 type = "COMPARESTRING"; break;
5518 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5519 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5520 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5521 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5522 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5523 default: type = "???"; break;
5524 }
5525
5526 smsg("%s%4d %s %s", pfx, current, type, buf);
5527 }
5528 break;
5529
5530 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5531 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5532
5533 // expression operations
5534 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5535 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5536 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5537 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5538 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5539 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5540 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5541 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5542 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5543 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5544 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5545 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005546 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005547 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5548 iptr->isn_arg.getitem.gi_index,
5549 iptr->isn_arg.getitem.gi_with_op ?
5550 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005551 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5552 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5553 iptr->isn_arg.string); break;
5554 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5555
5556 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5557 case ISN_CHECKTYPE:
5558 {
5559 checktype_T *ct = &iptr->isn_arg.type;
5560 char *tofree;
5561
5562 if (ct->ct_arg_idx == 0)
5563 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5564 type_name(ct->ct_type, &tofree),
5565 (int)ct->ct_off);
5566 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005567 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5568 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005569 type_name(ct->ct_type, &tofree),
5570 (int)ct->ct_off,
5571 (int)ct->ct_arg_idx);
5572 vim_free(tofree);
5573 break;
5574 }
5575 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5576 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5577 iptr->isn_arg.checklen.cl_min_len);
5578 break;
5579 case ISN_SETTYPE:
5580 {
5581 char *tofree;
5582
5583 smsg("%s%4d SETTYPE %s", pfx, current,
5584 type_name(iptr->isn_arg.type.ct_type, &tofree));
5585 vim_free(tofree);
5586 break;
5587 }
5588 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005589 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5590 smsg("%s%4d INVERT %d (!val)", pfx, current,
5591 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005592 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005593 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5594 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005595 break;
5596 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005597 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005598 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005599 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5600 pfx, current,
5601 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005602 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005603 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5604 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005605 break;
5606 case ISN_PUT:
5607 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5608 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005609 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005610 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5611 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005612 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005613 else
5614 smsg("%s%4d PUT %c %ld", pfx, current,
5615 iptr->isn_arg.put.put_regname,
5616 (long)iptr->isn_arg.put.put_lnum);
5617 break;
5618
5619 // TODO: summarize modifiers
5620 case ISN_CMDMOD:
5621 {
5622 char_u *buf;
5623 size_t len = produce_cmdmods(
5624 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5625
5626 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005627 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 {
5629 (void)produce_cmdmods(
5630 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5631 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5632 vim_free(buf);
5633 }
5634 break;
5635 }
5636 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5637
5638 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005639 smsg("%s%4d PROFILE START line %d", pfx, current,
5640 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005641 break;
5642
5643 case ISN_PROF_END:
5644 smsg("%s%4d PROFILE END", pfx, current);
5645 break;
5646
Bram Moolenaare99d4222021-06-13 14:01:26 +02005647 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005648 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5649 iptr->isn_arg.debug.dbg_break_lnum + 1,
5650 iptr->isn_lnum,
5651 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005652 break;
5653
Bram Moolenaar4c137212021-04-19 16:48:48 +02005654 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5655 iptr->isn_arg.unpack.unp_count,
5656 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5657 break;
5658 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5659 iptr->isn_arg.shuffle.shfl_item,
5660 iptr->isn_arg.shuffle.shfl_up);
5661 break;
5662 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5663
5664 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5665 return;
5666 }
5667
5668 out_flush(); // output one line at a time
5669 ui_breakcheck();
5670 if (got_int)
5671 break;
5672 }
5673}
5674
5675/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005676 * Handle command line completion for the :disassemble command.
5677 */
5678 void
5679set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5680{
5681 char_u *p;
5682
5683 // Default: expand user functions, "debug" and "profile"
5684 xp->xp_context = EXPAND_DISASSEMBLE;
5685 xp->xp_pattern = arg;
5686
5687 // first argument already typed: only user function names
5688 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5689 {
5690 xp->xp_context = EXPAND_USER_FUNC;
5691 xp->xp_pattern = skipwhite(p);
5692 }
5693}
5694
5695/*
5696 * Function given to ExpandGeneric() to obtain the list of :disassemble
5697 * arguments.
5698 */
5699 char_u *
5700get_disassemble_argument(expand_T *xp, int idx)
5701{
5702 if (idx == 0)
5703 return (char_u *)"debug";
5704 if (idx == 1)
5705 return (char_u *)"profile";
5706 return get_user_func_name(xp, idx - 2);
5707}
5708
5709/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005710 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005711 * We don't really need this at runtime, but we do have tests that require it,
5712 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005713 */
5714 void
5715ex_disassemble(exarg_T *eap)
5716{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005717 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005718 char_u *fname;
5719 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005720 dfunc_T *dfunc;
5721 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005722 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005723 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005724 compiletype_T compile_type = CT_NONE;
5725
5726 if (STRNCMP(arg, "profile", 7) == 0)
5727 {
5728 compile_type = CT_PROFILE;
5729 arg = skipwhite(arg + 7);
5730 }
5731 else if (STRNCMP(arg, "debug", 5) == 0)
5732 {
5733 compile_type = CT_DEBUG;
5734 arg = skipwhite(arg + 5);
5735 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005736
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005737 if (STRNCMP(arg, "<lambda>", 8) == 0)
5738 {
5739 arg += 8;
5740 (void)getdigits(&arg);
5741 fname = vim_strnsave(eap->arg, arg - eap->arg);
5742 }
5743 else
5744 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005745 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005746 if (fname == NULL)
5747 {
5748 semsg(_(e_invarg2), eap->arg);
5749 return;
5750 }
5751
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005752 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005753 if (ufunc == NULL)
5754 {
5755 char_u *p = untrans_function_name(fname);
5756
5757 if (p != NULL)
5758 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005759 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005760 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005761 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005762 if (ufunc == NULL)
5763 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005764 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765 return;
5766 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005767 if (func_needs_compiling(ufunc, compile_type)
5768 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005769 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005770 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005772 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773 return;
5774 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005775 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005776
5777 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005778 switch (compile_type)
5779 {
5780 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005781#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005782 instr = dfunc->df_instr_prof;
5783 instr_count = dfunc->df_instr_prof_count;
5784 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005785#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005786 // FALLTHROUGH
5787 case CT_NONE:
5788 instr = dfunc->df_instr;
5789 instr_count = dfunc->df_instr_count;
5790 break;
5791 case CT_DEBUG:
5792 instr = dfunc->df_instr_debug;
5793 instr_count = dfunc->df_instr_debug_count;
5794 break;
5795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005796
Bram Moolenaar4c137212021-04-19 16:48:48 +02005797 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005798}
5799
5800/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005801 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005802 * list, etc. Mostly like what JavaScript does, except that empty list and
5803 * empty dictionary are FALSE.
5804 */
5805 int
5806tv2bool(typval_T *tv)
5807{
5808 switch (tv->v_type)
5809 {
5810 case VAR_NUMBER:
5811 return tv->vval.v_number != 0;
5812 case VAR_FLOAT:
5813#ifdef FEAT_FLOAT
5814 return tv->vval.v_float != 0.0;
5815#else
5816 break;
5817#endif
5818 case VAR_PARTIAL:
5819 return tv->vval.v_partial != NULL;
5820 case VAR_FUNC:
5821 case VAR_STRING:
5822 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5823 case VAR_LIST:
5824 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5825 case VAR_DICT:
5826 return tv->vval.v_dict != NULL
5827 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5828 case VAR_BOOL:
5829 case VAR_SPECIAL:
5830 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5831 case VAR_JOB:
5832#ifdef FEAT_JOB_CHANNEL
5833 return tv->vval.v_job != NULL;
5834#else
5835 break;
5836#endif
5837 case VAR_CHANNEL:
5838#ifdef FEAT_JOB_CHANNEL
5839 return tv->vval.v_channel != NULL;
5840#else
5841 break;
5842#endif
5843 case VAR_BLOB:
5844 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5845 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005846 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005847 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005848 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005849 break;
5850 }
5851 return FALSE;
5852}
5853
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005854 void
5855emsg_using_string_as(typval_T *tv, int as_number)
5856{
5857 semsg(_(as_number ? e_using_string_as_number_str
5858 : e_using_string_as_bool_str),
5859 tv->vval.v_string == NULL
5860 ? (char_u *)"" : tv->vval.v_string);
5861}
5862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863/*
5864 * If "tv" is a string give an error and return FAIL.
5865 */
5866 int
5867check_not_string(typval_T *tv)
5868{
5869 if (tv->v_type == VAR_STRING)
5870 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005871 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872 clear_tv(tv);
5873 return FAIL;
5874 }
5875 return OK;
5876}
5877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878
5879#endif // FEAT_EVAL