blob: 04d5e5f27cea3007ee8fa6b38ac756851d65eae5 [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 Moolenaara1773442020-08-12 15:21:22 +0200847 * Return TRUE if an error was given or CTRL-C was pressed.
848 */
849 static int
850vim9_aborting(int prev_called_emsg)
851{
852 return called_emsg > prev_called_emsg || got_int || did_throw;
853}
854
855/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 * Execute a function by "name".
857 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100858 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 * Returns FAIL if not found without an error message.
860 */
861 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100862call_by_name(
863 char_u *name,
864 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100865 ectx_T *ectx,
866 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867{
868 ufunc_T *ufunc;
869
870 if (builtin_function(name, -1))
871 {
872 int func_idx = find_internal_func(name);
873
874 if (func_idx < 0)
875 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200876 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 return FAIL;
878 return call_bfunc(func_idx, argcount, ectx);
879 }
880
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200881 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200882
883 if (ufunc == NULL)
884 {
885 int called_emsg_before = called_emsg;
886
887 if (script_autoload(name, TRUE))
888 // loaded a package, search for the function again
889 ufunc = find_func(name, FALSE, NULL);
890 if (vim9_aborting(called_emsg_before))
891 return FAIL; // bail out if loading the script caused an error
892 }
893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100895 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200896 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100897 {
898 int i;
899 typval_T *argv = STACK_TV_BOT(0) - argcount;
900
901 // The function can change at runtime, check that the argument
902 // types are correct.
903 for (i = 0; i < argcount; ++i)
904 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100905 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100906
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200907 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100908 type = ufunc->uf_arg_types[i];
909 else if (ufunc->uf_va_type != NULL)
910 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100911 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200912 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100913 return FAIL;
914 }
915 }
916
Bram Moolenaar4c137212021-04-19 16:48:48 +0200917 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919
920 return FAIL;
921}
922
923 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100924call_partial(
925 typval_T *tv,
926 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100927 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200929 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200930 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100932 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
934 if (tv->v_type == VAR_PARTIAL)
935 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200936 partial_T *pt = tv->vval.v_partial;
937 int i;
938
939 if (pt->pt_argc > 0)
940 {
941 // Make space for arguments from the partial, shift the "argcount"
942 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +0200943 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +0200944 return FAIL;
945 for (i = 1; i <= argcount; ++i)
946 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
947 ectx->ec_stack.ga_len += pt->pt_argc;
948 argcount += pt->pt_argc;
949
950 // copy the arguments from the partial onto the stack
951 for (i = 0; i < pt->pt_argc; ++i)
952 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954
955 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200956 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 name = pt->pt_name;
959 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200960 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200962 if (name != NULL)
963 {
964 char_u fname_buf[FLEN_FIXED + 1];
965 char_u *tofree = NULL;
966 int error = FCERR_NONE;
967 char_u *fname;
968
969 // May need to translate <SNR>123_ to K_SNR.
970 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
971 if (error != FCERR_NONE)
972 res = FAIL;
973 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200974 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200975 vim_free(tofree);
976 }
977
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100978 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 {
980 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200981 semsg(_(e_unknownfunc),
982 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 return FAIL;
984 }
985 return OK;
986}
987
988/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200989 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
990 * TRUE.
991 */
992 static int
993error_if_locked(int lock, char *error)
994{
995 if (lock & (VAR_LOCKED | VAR_FIXED))
996 {
997 emsg(_(error));
998 return TRUE;
999 }
1000 return FALSE;
1001}
1002
1003/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001004 * Give an error if "tv" is not a number and return FAIL.
1005 */
1006 static int
1007check_for_number(typval_T *tv)
1008{
1009 if (tv->v_type != VAR_NUMBER)
1010 {
1011 semsg(_(e_expected_str_but_got_str),
1012 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1013 return FAIL;
1014 }
1015 return OK;
1016}
1017
1018/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001019 * Store "tv" in variable "name".
1020 * This is for s: and g: variables.
1021 */
1022 static void
1023store_var(char_u *name, typval_T *tv)
1024{
1025 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001026 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001027
Bram Moolenaard877a572021-04-01 19:42:48 +02001028 if (tv->v_lock)
1029 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001030 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001031 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001032 restore_funccal();
1033}
1034
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001035/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001036 * Convert "tv" to a string.
1037 * Return FAIL if not allowed.
1038 */
1039 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001040do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001041{
1042 if (tv->v_type != VAR_STRING)
1043 {
1044 char_u *str;
1045
1046 if (is_2string_any)
1047 {
1048 switch (tv->v_type)
1049 {
1050 case VAR_SPECIAL:
1051 case VAR_BOOL:
1052 case VAR_NUMBER:
1053 case VAR_FLOAT:
1054 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001055
1056 case VAR_LIST:
1057 if (tolerant)
1058 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001059 char_u *s, *e, *p;
1060 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001061
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001062 ga_init2(&ga, sizeof(char_u *), 1);
1063
1064 // Convert to NL separated items, then
1065 // escape the items and replace the NL with
1066 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001067 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001068 if (str == NULL)
1069 return FAIL;
1070 s = str;
1071 while ((e = vim_strchr(s, '\n')) != NULL)
1072 {
1073 *e = NUL;
1074 p = vim_strsave_fnameescape(s, FALSE);
1075 if (p != NULL)
1076 {
1077 ga_concat(&ga, p);
1078 ga_concat(&ga, (char_u *)" ");
1079 vim_free(p);
1080 }
1081 s = e + 1;
1082 }
1083 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001084 clear_tv(tv);
1085 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001086 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001087 return OK;
1088 }
1089 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001090 default: to_string_error(tv->v_type);
1091 return FAIL;
1092 }
1093 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001094 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001095 clear_tv(tv);
1096 tv->v_type = VAR_STRING;
1097 tv->vval.v_string = str;
1098 }
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001103 * When the value of "sv" is a null list of dict, allocate it.
1104 */
1105 static void
1106allocate_if_null(typval_T *tv)
1107{
1108 switch (tv->v_type)
1109 {
1110 case VAR_LIST:
1111 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001112 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001113 break;
1114 case VAR_DICT:
1115 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001116 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001117 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001118 case VAR_BLOB:
1119 if (tv->vval.v_blob == NULL)
1120 (void)rettv_blob_alloc(tv);
1121 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001122 default:
1123 break;
1124 }
1125}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001126
Bram Moolenaare7525c52021-01-09 13:20:37 +01001127/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001128 * Return the character "str[index]" where "index" is the character index,
1129 * including composing characters.
1130 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001131 */
1132 char_u *
1133char_from_string(char_u *str, varnumber_T index)
1134{
1135 size_t nbyte = 0;
1136 varnumber_T nchar = index;
1137 size_t slen;
1138
1139 if (str == NULL)
1140 return NULL;
1141 slen = STRLEN(str);
1142
Bram Moolenaarff871402021-03-26 13:34:05 +01001143 // Do the same as for a list: a negative index counts from the end.
1144 // Optimization to check the first byte to be below 0x80 (and no composing
1145 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001146 if (index < 0)
1147 {
1148 int clen = 0;
1149
1150 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001151 {
1152 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1153 ++nbyte;
1154 else if (enc_utf8)
1155 nbyte += utfc_ptr2len(str + nbyte);
1156 else
1157 nbyte += mb_ptr2len(str + nbyte);
1158 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001159 nchar = clen + index;
1160 if (nchar < 0)
1161 // unlike list: index out of range results in empty string
1162 return NULL;
1163 }
1164
1165 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001166 {
1167 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1168 ++nbyte;
1169 else if (enc_utf8)
1170 nbyte += utfc_ptr2len(str + nbyte);
1171 else
1172 nbyte += mb_ptr2len(str + nbyte);
1173 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001174 if (nbyte >= slen)
1175 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001176 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001177}
1178
1179/*
1180 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001181 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001182 * If going over the end return "str_len".
1183 * If "idx" is negative count from the end, -1 is the last character.
1184 * When going over the start return -1.
1185 */
1186 static long
1187char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1188{
1189 varnumber_T nchar = idx;
1190 size_t nbyte = 0;
1191
1192 if (nchar >= 0)
1193 {
1194 while (nchar > 0 && nbyte < str_len)
1195 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001196 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001197 --nchar;
1198 }
1199 }
1200 else
1201 {
1202 nbyte = str_len;
1203 while (nchar < 0 && nbyte > 0)
1204 {
1205 --nbyte;
1206 nbyte -= mb_head_off(str, str + nbyte);
1207 ++nchar;
1208 }
1209 if (nchar < 0)
1210 return -1;
1211 }
1212 return (long)nbyte;
1213}
1214
1215/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001216 * Return the slice "str[first : last]" using character indexes. Composing
1217 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001218 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001219 * Return NULL when the result is empty.
1220 */
1221 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001222string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001223{
1224 long start_byte, end_byte;
1225 size_t slen;
1226
1227 if (str == NULL)
1228 return NULL;
1229 slen = STRLEN(str);
1230 start_byte = char_idx2byte(str, slen, first);
1231 if (start_byte < 0)
1232 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001233 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001234 end_byte = (long)slen;
1235 else
1236 {
1237 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001238 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001239 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001240 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001241 }
1242
1243 if (start_byte >= (long)slen || end_byte <= start_byte)
1244 return NULL;
1245 return vim_strnsave(str + start_byte, end_byte - start_byte);
1246}
1247
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001248/*
1249 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1250 * When "dfunc_idx" is negative don't give an error.
1251 * Returns NULL for an error.
1252 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001253 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001254get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001255{
1256 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001257 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1258 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001259 svar_T *sv;
1260
1261 if (sref->sref_seq != si->sn_script_seq)
1262 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001263 // The script was reloaded after the function was compiled, the
1264 // script_idx may not be valid.
1265 if (dfunc != NULL)
1266 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1267 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001268 return NULL;
1269 }
1270 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001271 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001272 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001273 if (dfunc != NULL)
1274 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001275 return NULL;
1276 }
1277 return sv;
1278}
1279
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001280/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001281 * Function passed to do_cmdline() for splitting a script joined by NL
1282 * characters.
1283 */
1284 static char_u *
1285get_split_sourceline(
1286 int c UNUSED,
1287 void *cookie,
1288 int indent UNUSED,
1289 getline_opt_T options UNUSED)
1290{
1291 source_cookie_T *sp = (source_cookie_T *)cookie;
1292 char_u *p;
1293 char_u *line;
1294
1295 if (*sp->nextline == NUL)
1296 return NULL;
1297 p = vim_strchr(sp->nextline, '\n');
1298 if (p == NULL)
1299 {
1300 line = vim_strsave(sp->nextline);
1301 sp->nextline += STRLEN(sp->nextline);
1302 }
1303 else
1304 {
1305 line = vim_strnsave(sp->nextline, p - sp->nextline);
1306 sp->nextline = p + 1;
1307 }
1308 return line;
1309}
1310
1311/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 * Execute a function by "name".
1313 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001314 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 */
1316 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001317call_eval_func(
1318 char_u *name,
1319 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001320 ectx_T *ectx,
1321 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322{
Bram Moolenaared677f52020-08-12 16:38:10 +02001323 int called_emsg_before = called_emsg;
1324 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325
Bram Moolenaar4c137212021-04-19 16:48:48 +02001326 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001327 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001329 dictitem_T *v;
1330
1331 v = find_var(name, NULL, FALSE);
1332 if (v == NULL)
1333 {
1334 semsg(_(e_unknownfunc), name);
1335 return FAIL;
1336 }
1337 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1338 {
1339 semsg(_(e_unknownfunc), name);
1340 return FAIL;
1341 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001342 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001344 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345}
1346
1347/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001348 * When a function reference is used, fill a partial with the information
1349 * needed, especially when it is used as a closure.
1350 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001351 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001352fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1353{
1354 pt->pt_func = ufunc;
1355 pt->pt_refcount = 1;
1356
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001357 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001358 {
1359 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1360 + ectx->ec_dfunc_idx;
1361
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001362 // The closure may need to find arguments and local variables in the
1363 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001364 pt->pt_outer.out_stack = &ectx->ec_stack;
1365 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001366 if (ectx->ec_outer_ref != NULL)
1367 {
1368 // The current context already has a context, link to that one.
1369 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1370 if (ectx->ec_outer_ref->or_partial != NULL)
1371 {
1372 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1373 ++pt->pt_outer.out_up_partial->pt_refcount;
1374 }
1375 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001376
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001377 // If this function returns and the closure is still being used, we
1378 // need to make a copy of the context (arguments and local variables).
1379 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001380 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001381 {
1382 vim_free(pt);
1383 return FAIL;
1384 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001385 // Extra variable keeps the count of closures created in the current
1386 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001387 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1388 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1389
1390 ((partial_T **)ectx->ec_funcrefs.ga_data)
1391 [ectx->ec_funcrefs.ga_len] = pt;
1392 ++pt->pt_refcount;
1393 ++ectx->ec_funcrefs.ga_len;
1394 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001395 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001396 return OK;
1397}
1398
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001399/*
1400 * Execute iptr->isn_arg.string as an Ex command.
1401 */
1402 static int
1403exec_command(isn_T *iptr)
1404{
1405 source_cookie_T cookie;
1406
1407 SOURCING_LNUM = iptr->isn_lnum;
1408 // Pass getsourceline to get an error for a missing ":end"
1409 // command.
1410 CLEAR_FIELD(cookie);
1411 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1412 if (do_cmdline(iptr->isn_arg.string,
1413 getsourceline, &cookie,
1414 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1415 || did_emsg)
1416 return FAIL;
1417 return OK;
1418}
1419
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001420// used for v_instr of typval of VAR_INSTR
1421struct instr_S {
1422 ectx_T *instr_ectx;
1423 isn_T *instr_instr;
1424};
1425
Bram Moolenaar4c137212021-04-19 16:48:48 +02001426// used for substitute_instr
1427typedef struct subs_expr_S {
1428 ectx_T *subs_ectx;
1429 isn_T *subs_instr;
1430 int subs_status;
1431} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432
1433// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001434#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435
1436// Get pointer to item at the bottom of the stack, -1 is the bottom.
1437#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001438#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 +01001439
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001440// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001441#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 +01001442
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001443// Set when calling do_debug().
1444static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001445static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001446
1447/*
1448 * When debugging lookup "name" and return the typeval.
1449 * When not found return NULL.
1450 */
1451 typval_T *
1452lookup_debug_var(char_u *name)
1453{
1454 int idx;
1455 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001456 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001457 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001458 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001459
1460 if (ectx == NULL)
1461 return NULL;
1462 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1463
1464 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001465 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001466 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001467 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001468 return STACK_TV_VAR(idx);
1469 }
1470
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001471 // Go through argument names.
1472 ufunc = dfunc->df_ufunc;
1473 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1474 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1475 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1476 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1477 - varargs_off + idx);
1478 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1479 return STACK_TV(ectx->ec_frame_idx - 1);
1480
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001481 return NULL;
1482}
1483
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001484 static void
1485handle_debug(isn_T *iptr, ectx_T *ectx)
1486{
1487 char_u *line;
1488 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1489 + ectx->ec_dfunc_idx)->df_ufunc;
1490 isn_T *ni;
1491 int end_lnum = iptr->isn_lnum;
1492 garray_T ga;
1493 int lnum;
1494
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001495 if (ex_nesting_level > debug_break_level)
1496 {
1497 linenr_T breakpoint;
1498
1499 if (!ufunc->uf_has_breakpoint)
1500 return;
1501
1502 // check for the next breakpoint if needed
1503 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001504 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001505 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1506 return;
1507 }
1508
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001509 SOURCING_LNUM = iptr->isn_lnum;
1510 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001511 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001512
1513 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1514 if (ni->isn_type == ISN_DEBUG
1515 || ni->isn_type == ISN_RETURN
1516 || ni->isn_type == ISN_RETURN_VOID)
1517 {
1518 end_lnum = ni->isn_lnum;
1519 break;
1520 }
1521
1522 if (end_lnum > iptr->isn_lnum)
1523 {
1524 ga_init2(&ga, sizeof(char_u *), 10);
1525 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001526 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001527 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001528
Bram Moolenaar303215d2021-07-07 20:10:43 +02001529 if (p == NULL)
1530 continue; // left over from continuation line
1531 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001532 if (*p == '#')
1533 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001534 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001535 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1536 if (STRNCMP(p, "def ", 4) == 0)
1537 break;
1538 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001539 line = ga_concat_strings(&ga, " ");
1540 vim_free(ga.ga_data);
1541 }
1542 else
1543 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001544
Dominique Pelle6e969552021-06-17 13:53:41 +02001545 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001546 debug_context = NULL;
1547
1548 if (end_lnum > iptr->isn_lnum)
1549 vim_free(line);
1550}
1551
Bram Moolenaar4c137212021-04-19 16:48:48 +02001552/*
1553 * Execute instructions in execution context "ectx".
1554 * Return OK or FAIL;
1555 */
1556 static int
1557exec_instructions(ectx_T *ectx)
1558{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001559 int ret = FAIL;
1560 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001561
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001562 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001563 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001564
Bram Moolenaarff652882021-05-16 15:24:49 +02001565 // Only catch exceptions in this instruction list.
1566 ectx->ec_trylevel_at_start = trylevel;
1567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 for (;;)
1569 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001570 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001572 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001573
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001574 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001575 {
1576 line_breakcheck();
1577 breakcheck_count = 0;
1578 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001579 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001580 {
1581 // Turn CTRL-C into an exception.
1582 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001583 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001584 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001585 did_throw = TRUE;
1586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001588 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001589 {
1590 // Turn an error message into an exception.
1591 did_emsg = FALSE;
1592 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001593 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001594 did_throw = TRUE;
1595 *msg_list = NULL;
1596 }
1597
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001598 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001600 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001601 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001602 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603
1604 // An exception jumps to the first catch, finally, or returns from
1605 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001606 while (index > 0)
1607 {
1608 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001609 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001610 break;
1611 // In the catch and finally block of this try we have to go up
1612 // one level.
1613 --index;
1614 trycmd = NULL;
1615 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001616 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001618 if (trycmd->tcd_in_catch)
1619 {
1620 // exception inside ":catch", jump to ":finally" once
1621 ectx->ec_iidx = trycmd->tcd_finally_idx;
1622 trycmd->tcd_finally_idx = 0;
1623 }
1624 else
1625 // jump to first ":catch"
1626 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001627 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001628 did_throw = FALSE; // don't come back here until :endtry
1629 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 }
1631 else
1632 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001633 // Not inside try or need to return from current functions.
1634 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001635 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001636 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001637 tv = STACK_TV_BOT(0);
1638 tv->v_type = VAR_NUMBER;
1639 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001640 ++ectx->ec_stack.ga_len;
1641 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001643 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001644 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001645 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001646 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 goto done;
1648 }
1649
Bram Moolenaar4c137212021-04-19 16:48:48 +02001650 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001651 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 }
1653 continue;
1654 }
1655
Bram Moolenaar4c137212021-04-19 16:48:48 +02001656 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 switch (iptr->isn_type)
1658 {
1659 // execute Ex command line
1660 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001661 if (exec_command(iptr) == FAIL)
1662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 break;
1664
Bram Moolenaar20677332021-06-06 17:02:53 +02001665 // execute Ex command line split at NL characters.
1666 case ISN_EXEC_SPLIT:
1667 {
1668 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001669 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001670
1671 SOURCING_LNUM = iptr->isn_lnum;
1672 CLEAR_FIELD(cookie);
1673 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1674 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001675 line = get_split_sourceline(0, &cookie, 0, 0);
1676 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001677 get_split_sourceline, &cookie,
1678 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1679 == FAIL
1680 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001681 {
1682 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001683 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001684 }
1685 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001686 }
1687 break;
1688
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001689 // Evaluate an expression with legacy syntax, push it onto the
1690 // stack.
1691 case ISN_LEGACY_EVAL:
1692 {
1693 char_u *arg = iptr->isn_arg.string;
1694 int res;
1695 int save_flags = cmdmod.cmod_flags;
1696
Bram Moolenaar35578162021-08-02 19:10:38 +02001697 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001698 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001699 tv = STACK_TV_BOT(0);
1700 init_tv(tv);
1701 cmdmod.cmod_flags |= CMOD_LEGACY;
1702 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1703 cmdmod.cmod_flags = save_flags;
1704 if (res == FAIL)
1705 goto on_error;
1706 ++ectx->ec_stack.ga_len;
1707 }
1708 break;
1709
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001710 // push typeval VAR_INSTR with instructions to be executed
1711 case ISN_INSTR:
1712 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001713 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001714 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001715 tv = STACK_TV_BOT(0);
1716 tv->vval.v_instr = ALLOC_ONE(instr_T);
1717 if (tv->vval.v_instr == NULL)
1718 goto on_error;
1719 ++ectx->ec_stack.ga_len;
1720
1721 tv->v_type = VAR_INSTR;
1722 tv->vval.v_instr->instr_ectx = ectx;
1723 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1724 }
1725 break;
1726
Bram Moolenaar4c137212021-04-19 16:48:48 +02001727 // execute :substitute with an expression
1728 case ISN_SUBSTITUTE:
1729 {
1730 subs_T *subs = &iptr->isn_arg.subs;
1731 source_cookie_T cookie;
1732 struct subs_expr_S *save_instr = substitute_instr;
1733 struct subs_expr_S subs_instr;
1734 int res;
1735
1736 subs_instr.subs_ectx = ectx;
1737 subs_instr.subs_instr = subs->subs_instr;
1738 subs_instr.subs_status = OK;
1739 substitute_instr = &subs_instr;
1740
1741 SOURCING_LNUM = iptr->isn_lnum;
1742 // This is very much like ISN_EXEC
1743 CLEAR_FIELD(cookie);
1744 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1745 res = do_cmdline(subs->subs_cmd,
1746 getsourceline, &cookie,
1747 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1748 substitute_instr = save_instr;
1749
1750 if (res == FAIL || did_emsg
1751 || subs_instr.subs_status == FAIL)
1752 goto on_error;
1753 }
1754 break;
1755
1756 case ISN_FINISH:
1757 goto done;
1758
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001759 case ISN_REDIRSTART:
1760 // create a dummy entry for var_redir_str()
1761 if (alloc_redir_lval() == FAIL)
1762 goto on_error;
1763
1764 // The output is stored in growarray "redir_ga" until
1765 // redirection ends.
1766 init_redir_ga();
1767 redir_vname = 1;
1768 break;
1769
1770 case ISN_REDIREND:
1771 {
1772 char_u *res = get_clear_redir_ga();
1773
1774 // End redirection, put redirected text on the stack.
1775 clear_redir_lval();
1776 redir_vname = 0;
1777
Bram Moolenaar35578162021-08-02 19:10:38 +02001778 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001779 {
1780 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001781 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001782 }
1783 tv = STACK_TV_BOT(0);
1784 tv->v_type = VAR_STRING;
1785 tv->vval.v_string = res;
1786 ++ectx->ec_stack.ga_len;
1787 }
1788 break;
1789
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001790 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001791#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001792 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1793 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001794#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001795 break;
1796
1797 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001798#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001799 {
1800 exarg_T ea;
1801 int res;
1802
1803 CLEAR_FIELD(ea);
1804 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1805 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1806 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1807 --ectx->ec_stack.ga_len;
1808 tv = STACK_TV_BOT(0);
1809 res = cexpr_core(&ea, tv);
1810 clear_tv(tv);
1811 if (res == FAIL)
1812 goto on_error;
1813 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001814#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001815 break;
1816
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001817 // execute Ex command from pieces on the stack
1818 case ISN_EXECCONCAT:
1819 {
1820 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001821 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001822 int pass;
1823 int i;
1824 char_u *cmd = NULL;
1825 char_u *str;
1826
1827 for (pass = 1; pass <= 2; ++pass)
1828 {
1829 for (i = 0; i < count; ++i)
1830 {
1831 tv = STACK_TV_BOT(i - count);
1832 str = tv->vval.v_string;
1833 if (str != NULL && *str != NUL)
1834 {
1835 if (pass == 2)
1836 STRCPY(cmd + len, str);
1837 len += STRLEN(str);
1838 }
1839 if (pass == 2)
1840 clear_tv(tv);
1841 }
1842 if (pass == 1)
1843 {
1844 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001845 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001846 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001847 len = 0;
1848 }
1849 }
1850
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001851 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001852 do_cmdline_cmd(cmd);
1853 vim_free(cmd);
1854 }
1855 break;
1856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 // execute :echo {string} ...
1858 case ISN_ECHO:
1859 {
1860 int count = iptr->isn_arg.echo.echo_count;
1861 int atstart = TRUE;
1862 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001863 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864
1865 for (idx = 0; idx < count; ++idx)
1866 {
1867 tv = STACK_TV_BOT(idx - count);
1868 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1869 &atstart, &needclr);
1870 clear_tv(tv);
1871 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001872 if (needclr)
1873 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001874 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 }
1876 break;
1877
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001878 // :execute {string} ...
1879 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02001880 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001881 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001882 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001883 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02001884 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001885 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001886 {
1887 int count = iptr->isn_arg.number;
1888 garray_T ga;
1889 char_u buf[NUMBUFLEN];
1890 char_u *p;
1891 int len;
1892 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001893 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001894
1895 ga_init2(&ga, 1, 80);
1896 for (idx = 0; idx < count; ++idx)
1897 {
1898 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001899 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001900 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001901 if (tv->v_type == VAR_CHANNEL
1902 || tv->v_type == VAR_JOB)
1903 {
1904 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001905 semsg(_(e_using_invalid_value_as_string_str),
1906 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001907 break;
1908 }
1909 else
1910 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001911 }
1912 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001913 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001914
1915 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02001916 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01001917 failed = TRUE;
1918 else
1919 {
1920 if (ga.ga_len > 0)
1921 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1922 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1923 ga.ga_len += len;
1924 }
1925 clear_tv(tv);
1926 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001927 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001928 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001929 {
1930 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001931 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001932 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001933
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001934 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001935 {
1936 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001937 {
1938 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001939 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001940 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001941 {
1942 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001943 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001944 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001945 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001946 else
1947 {
1948 msg_sb_eol();
1949 if (iptr->isn_type == ISN_ECHOMSG)
1950 {
1951 msg_attr(ga.ga_data, echo_attr);
1952 out_flush();
1953 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02001954 else if (iptr->isn_type == ISN_ECHOCONSOLE)
1955 {
1956 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
1957 TRUE);
1958 ui_write((char_u *)"\r\n", 2, TRUE);
1959 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001960 else
1961 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001962 SOURCING_LNUM = iptr->isn_lnum;
1963 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001964 }
1965 }
1966 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001967 ga_clear(&ga);
1968 }
1969 break;
1970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 // load local variable or argument
1972 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02001973 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001974 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001976 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 break;
1978
1979 // load v: variable
1980 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02001981 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001982 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001984 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 break;
1986
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001987 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 case ISN_LOADSCRIPT:
1989 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001990 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 svar_T *sv;
1992
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001993 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001994 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001995 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001996 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02001997 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001998 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002000 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 }
2002 break;
2003
2004 // load s: variable in old script
2005 case ISN_LOADS:
2006 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002007 hashtab_T *ht = &SCRIPT_VARS(
2008 iptr->isn_arg.loadstore.ls_sid);
2009 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 if (di == NULL)
2013 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002014 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002015 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002016 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 }
2018 else
2019 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002020 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002021 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002023 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 }
2025 }
2026 break;
2027
Bram Moolenaard3aac292020-04-19 14:32:17 +02002028 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002030 case ISN_LOADB:
2031 case ISN_LOADW:
2032 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002034 dictitem_T *di = NULL;
2035 hashtab_T *ht = NULL;
2036 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002037
Bram Moolenaard3aac292020-04-19 14:32:17 +02002038 switch (iptr->isn_type)
2039 {
2040 case ISN_LOADG:
2041 ht = get_globvar_ht();
2042 namespace = 'g';
2043 break;
2044 case ISN_LOADB:
2045 ht = &curbuf->b_vars->dv_hashtab;
2046 namespace = 'b';
2047 break;
2048 case ISN_LOADW:
2049 ht = &curwin->w_vars->dv_hashtab;
2050 namespace = 'w';
2051 break;
2052 case ISN_LOADT:
2053 ht = &curtab->tp_vars->dv_hashtab;
2054 namespace = 't';
2055 break;
2056 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002057 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002058 }
2059 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 if (di == NULL)
2062 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002063 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002064 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002065 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002066 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 }
2068 else
2069 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002070 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002071 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002073 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 }
2075 }
2076 break;
2077
Bram Moolenaar03290b82020-12-19 16:30:44 +01002078 // load autoload variable
2079 case ISN_LOADAUTO:
2080 {
2081 char_u *name = iptr->isn_arg.string;
2082
Bram Moolenaar35578162021-08-02 19:10:38 +02002083 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002084 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002085 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002086 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002087 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002088 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002089 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002090 }
2091 break;
2092
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002093 // load g:/b:/w:/t: namespace
2094 case ISN_LOADGDICT:
2095 case ISN_LOADBDICT:
2096 case ISN_LOADWDICT:
2097 case ISN_LOADTDICT:
2098 {
2099 dict_T *d = NULL;
2100
2101 switch (iptr->isn_type)
2102 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002103 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2104 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2105 case ISN_LOADWDICT: d = curwin->w_vars; break;
2106 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002107 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002108 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002109 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002110 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002111 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002112 tv = STACK_TV_BOT(0);
2113 tv->v_type = VAR_DICT;
2114 tv->v_lock = 0;
2115 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002116 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002117 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002118 }
2119 break;
2120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 // load &option
2122 case ISN_LOADOPT:
2123 {
2124 typval_T optval;
2125 char_u *name = iptr->isn_arg.string;
2126
Bram Moolenaara8c17702020-04-01 21:17:24 +02002127 // This is not expected to fail, name is checked during
2128 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002129 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002130 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002131 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002132 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002134 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 }
2136 break;
2137
2138 // load $ENV
2139 case ISN_LOADENV:
2140 {
2141 typval_T optval;
2142 char_u *name = iptr->isn_arg.string;
2143
Bram Moolenaar35578162021-08-02 19:10:38 +02002144 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002145 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002146 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002147 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002149 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 }
2151 break;
2152
2153 // load @register
2154 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002155 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002156 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 tv = STACK_TV_BOT(0);
2158 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002159 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002160 // This may result in NULL, which should be equivalent to an
2161 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 tv->vval.v_string = get_reg_contents(
2163 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002164 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 break;
2166
2167 // store local variable
2168 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002169 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 tv = STACK_TV_VAR(iptr->isn_arg.number);
2171 clear_tv(tv);
2172 *tv = *STACK_TV_BOT(0);
2173 break;
2174
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002175 // store s: variable in old script
2176 case ISN_STORES:
2177 {
2178 hashtab_T *ht = &SCRIPT_VARS(
2179 iptr->isn_arg.loadstore.ls_sid);
2180 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002181 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002182
Bram Moolenaar4c137212021-04-19 16:48:48 +02002183 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002184 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002185 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002186 else
2187 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002188 SOURCING_LNUM = iptr->isn_lnum;
2189 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002190 {
2191 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002192 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002193 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002194 clear_tv(&di->di_tv);
2195 di->di_tv = *STACK_TV_BOT(0);
2196 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002197 }
2198 break;
2199
2200 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 case ISN_STORESCRIPT:
2202 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002203 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2204 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002206 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002207 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002208 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002209 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002210
2211 // "const" and "final" are checked at compile time, locking
2212 // the value needs to be checked here.
2213 SOURCING_LNUM = iptr->isn_lnum;
2214 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002215 {
2216 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002217 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002218 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 clear_tv(sv->sv_tv);
2221 *sv->sv_tv = *STACK_TV_BOT(0);
2222 }
2223 break;
2224
2225 // store option
2226 case ISN_STOREOPT:
2227 {
2228 long n = 0;
2229 char_u *s = NULL;
2230 char *msg;
2231
Bram Moolenaar4c137212021-04-19 16:48:48 +02002232 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 tv = STACK_TV_BOT(0);
2234 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002235 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002237 if (s == NULL)
2238 s = (char_u *)"";
2239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002241 // must be VAR_NUMBER, CHECKTYPE makes sure
2242 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2244 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002245 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 if (msg != NULL)
2247 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002250 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 }
2253 break;
2254
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002255 // store $ENV
2256 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002257 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002258 tv = STACK_TV_BOT(0);
2259 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2260 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002261 break;
2262
2263 // store @r
2264 case ISN_STOREREG:
2265 {
2266 int reg = iptr->isn_arg.number;
2267
Bram Moolenaar4c137212021-04-19 16:48:48 +02002268 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002269 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002270 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002271 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002272 }
2273 break;
2274
2275 // store v: variable
2276 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002277 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002278 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2279 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002280 // should not happen, type is checked when compiling
2281 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002282 break;
2283
Bram Moolenaard3aac292020-04-19 14:32:17 +02002284 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002286 case ISN_STOREB:
2287 case ISN_STOREW:
2288 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002290 dictitem_T *di;
2291 hashtab_T *ht;
2292 char_u *name = iptr->isn_arg.string + 2;
2293
Bram Moolenaard3aac292020-04-19 14:32:17 +02002294 switch (iptr->isn_type)
2295 {
2296 case ISN_STOREG:
2297 ht = get_globvar_ht();
2298 break;
2299 case ISN_STOREB:
2300 ht = &curbuf->b_vars->dv_hashtab;
2301 break;
2302 case ISN_STOREW:
2303 ht = &curwin->w_vars->dv_hashtab;
2304 break;
2305 case ISN_STORET:
2306 ht = &curtab->tp_vars->dv_hashtab;
2307 break;
2308 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002309 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311
Bram Moolenaar4c137212021-04-19 16:48:48 +02002312 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002313 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002315 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 else
2317 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002318 SOURCING_LNUM = iptr->isn_lnum;
2319 if (var_check_permission(di, name) == FAIL)
2320 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 clear_tv(&di->di_tv);
2322 di->di_tv = *STACK_TV_BOT(0);
2323 }
2324 }
2325 break;
2326
Bram Moolenaar03290b82020-12-19 16:30:44 +01002327 // store an autoload variable
2328 case ISN_STOREAUTO:
2329 SOURCING_LNUM = iptr->isn_lnum;
2330 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2331 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002332 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002333 break;
2334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 // store number in local variable
2336 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002337 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 clear_tv(tv);
2339 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002340 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 break;
2342
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002343 // store value in list or dict variable
2344 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002345 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002346 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002347 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002348 typval_T *tv_dest = STACK_TV_BOT(-1);
2349 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002350
Bram Moolenaar752fc692021-01-04 21:57:11 +01002351 // Stack contains:
2352 // -3 value to be stored
2353 // -2 index
2354 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002355 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002356 SOURCING_LNUM = iptr->isn_lnum;
2357 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002358 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002359 dest_type = tv_dest->v_type;
2360 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002361 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002362 else if (dest_type == VAR_LIST
2363 && tv_idx->v_type != VAR_NUMBER)
2364 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002365 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002366 status = FAIL;
2367 }
2368 }
2369 else if (dest_type != tv_dest->v_type)
2370 {
2371 // just in case, should be OK
2372 semsg(_(e_expected_str_but_got_str),
2373 vartype_name(dest_type),
2374 vartype_name(tv_dest->v_type));
2375 status = FAIL;
2376 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002377
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002378 if (status == OK && dest_type == VAR_LIST)
2379 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002380 long lidx = (long)tv_idx->vval.v_number;
2381 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002382
2383 if (list == NULL)
2384 {
2385 emsg(_(e_list_not_set));
2386 goto on_error;
2387 }
2388 if (lidx < 0 && list->lv_len + lidx >= 0)
2389 // negative index is relative to the end
2390 lidx = list->lv_len + lidx;
2391 if (lidx < 0 || lidx > list->lv_len)
2392 {
2393 semsg(_(e_listidx), lidx);
2394 goto on_error;
2395 }
2396 if (lidx < list->lv_len)
2397 {
2398 listitem_T *li = list_find(list, lidx);
2399
2400 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002401 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002402 goto on_error;
2403 // overwrite existing list item
2404 clear_tv(&li->li_tv);
2405 li->li_tv = *tv;
2406 }
2407 else
2408 {
2409 if (error_if_locked(list->lv_lock,
2410 e_cannot_change_list))
2411 goto on_error;
2412 // append to list, only fails when out of memory
2413 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002414 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002415 clear_tv(tv);
2416 }
2417 }
2418 else if (status == OK && dest_type == VAR_DICT)
2419 {
2420 char_u *key = tv_idx->vval.v_string;
2421 dict_T *dict = tv_dest->vval.v_dict;
2422 dictitem_T *di;
2423
2424 SOURCING_LNUM = iptr->isn_lnum;
2425 if (dict == NULL)
2426 {
2427 emsg(_(e_dictionary_not_set));
2428 goto on_error;
2429 }
2430 if (key == NULL)
2431 key = (char_u *)"";
2432 di = dict_find(dict, key, -1);
2433 if (di != NULL)
2434 {
2435 if (error_if_locked(di->di_tv.v_lock,
2436 e_cannot_change_dict_item))
2437 goto on_error;
2438 // overwrite existing value
2439 clear_tv(&di->di_tv);
2440 di->di_tv = *tv;
2441 }
2442 else
2443 {
2444 if (error_if_locked(dict->dv_lock,
2445 e_cannot_change_dict))
2446 goto on_error;
2447 // add to dict, only fails when out of memory
2448 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002449 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002450 clear_tv(tv);
2451 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002452 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002453 else if (status == OK && dest_type == VAR_BLOB)
2454 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002455 long lidx = (long)tv_idx->vval.v_number;
2456 blob_T *blob = tv_dest->vval.v_blob;
2457 varnumber_T nr;
2458 int error = FALSE;
2459 int len;
2460
2461 if (blob == NULL)
2462 {
2463 emsg(_(e_blob_not_set));
2464 goto on_error;
2465 }
2466 len = blob_len(blob);
2467 if (lidx < 0 && len + lidx >= 0)
2468 // negative index is relative to the end
2469 lidx = len + lidx;
2470
2471 // Can add one byte at the end.
2472 if (lidx < 0 || lidx > len)
2473 {
2474 semsg(_(e_blobidx), lidx);
2475 goto on_error;
2476 }
2477 if (value_check_lock(blob->bv_lock,
2478 (char_u *)"blob", FALSE))
2479 goto on_error;
2480 nr = tv_get_number_chk(tv, &error);
2481 if (error)
2482 goto on_error;
2483 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002484 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002485 else
2486 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002487 status = FAIL;
2488 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002489 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002490
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002491 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002492 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002493 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002494 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002495 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002496 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002497 goto on_error;
2498 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002499 }
2500 break;
2501
Bram Moolenaar68452172021-04-12 21:21:02 +02002502 // store value in blob range
2503 case ISN_STORERANGE:
2504 {
2505 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2506 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2507 typval_T *tv_dest = STACK_TV_BOT(-1);
2508 int status = OK;
2509
2510 // Stack contains:
2511 // -4 value to be stored
2512 // -3 first index or "none"
2513 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002514 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002515 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002516 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002517 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002518 long n1;
2519 long n2;
2520 int error = FALSE;
2521
2522 SOURCING_LNUM = iptr->isn_lnum;
2523 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2524 if (error)
2525 status = FAIL;
2526 else
2527 {
2528 if (tv_idx2->v_type == VAR_SPECIAL
2529 && tv_idx2->vval.v_number == VVAL_NONE)
2530 n2 = list_len(tv_dest->vval.v_list) - 1;
2531 else
2532 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2533 if (error)
2534 status = FAIL;
2535 else
2536 {
2537 listitem_T *li1 = check_range_index_one(
2538 tv_dest->vval.v_list, &n1, FALSE);
2539
2540 if (li1 == NULL)
2541 status = FAIL;
2542 else
2543 {
2544 status = check_range_index_two(
2545 tv_dest->vval.v_list,
2546 &n1, li1, &n2, FALSE);
2547 if (status != FAIL)
2548 status = list_assign_range(
2549 tv_dest->vval.v_list,
2550 tv->vval.v_list,
2551 n1,
2552 n2,
2553 tv_idx2->v_type == VAR_SPECIAL,
2554 (char_u *)"=",
2555 (char_u *)"[unknown]");
2556 }
2557 }
2558 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002559 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002560 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002561 {
2562 varnumber_T n1;
2563 varnumber_T n2;
2564 int error = FALSE;
2565
2566 n1 = tv_get_number_chk(tv_idx1, &error);
2567 if (error)
2568 status = FAIL;
2569 else
2570 {
2571 if (tv_idx2->v_type == VAR_SPECIAL
2572 && tv_idx2->vval.v_number == VVAL_NONE)
2573 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2574 else
2575 n2 = tv_get_number_chk(tv_idx2, &error);
2576 if (error)
2577 status = FAIL;
2578 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002579 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002580 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002581
2582 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002583 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002584 || check_blob_range(bloblen,
2585 n1, n2, FALSE) == FAIL)
2586 status = FAIL;
2587 else
2588 status = blob_set_range(
2589 tv_dest->vval.v_blob, n1, n2, tv);
2590 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002591 }
2592 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002593 else
2594 {
2595 status = FAIL;
2596 emsg(_(e_blob_required));
2597 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002598
2599 clear_tv(tv_idx1);
2600 clear_tv(tv_idx2);
2601 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002602 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002603 clear_tv(tv);
2604
2605 if (status == FAIL)
2606 goto on_error;
2607 }
2608 break;
2609
Bram Moolenaar0186e582021-01-10 18:33:11 +01002610 // load or store variable or argument from outer scope
2611 case ISN_LOADOUTER:
2612 case ISN_STOREOUTER:
2613 {
2614 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002615 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2616 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002617
2618 while (depth > 1 && outer != NULL)
2619 {
2620 outer = outer->out_up;
2621 --depth;
2622 }
2623 if (outer == NULL)
2624 {
2625 SOURCING_LNUM = iptr->isn_lnum;
2626 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002627 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002628 }
2629 tv = ((typval_T *)outer->out_stack->ga_data)
2630 + outer->out_frame_idx + STACK_FRAME_SIZE
2631 + iptr->isn_arg.outer.outer_idx;
2632 if (iptr->isn_type == ISN_LOADOUTER)
2633 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002634 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002635 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002636 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002637 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002638 }
2639 else
2640 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002641 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002642 clear_tv(tv);
2643 *tv = *STACK_TV_BOT(0);
2644 }
2645 }
2646 break;
2647
Bram Moolenaar752fc692021-01-04 21:57:11 +01002648 // unlet item in list or dict variable
2649 case ISN_UNLETINDEX:
2650 {
2651 typval_T *tv_idx = STACK_TV_BOT(-2);
2652 typval_T *tv_dest = STACK_TV_BOT(-1);
2653 int status = OK;
2654
2655 // Stack contains:
2656 // -2 index
2657 // -1 dict or list
2658 if (tv_dest->v_type == VAR_DICT)
2659 {
2660 // unlet a dict item, index must be a string
2661 if (tv_idx->v_type != VAR_STRING)
2662 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002663 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002664 semsg(_(e_expected_str_but_got_str),
2665 vartype_name(VAR_STRING),
2666 vartype_name(tv_idx->v_type));
2667 status = FAIL;
2668 }
2669 else
2670 {
2671 dict_T *d = tv_dest->vval.v_dict;
2672 char_u *key = tv_idx->vval.v_string;
2673 dictitem_T *di = NULL;
2674
2675 if (key == NULL)
2676 key = (char_u *)"";
2677 if (d != NULL)
2678 di = dict_find(d, key, (int)STRLEN(key));
2679 if (di == NULL)
2680 {
2681 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002682 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002683 semsg(_(e_dictkey), key);
2684 status = FAIL;
2685 }
2686 else
2687 {
2688 // TODO: check for dict or item locked
2689 dictitem_remove(d, di);
2690 }
2691 }
2692 }
2693 else if (tv_dest->v_type == VAR_LIST)
2694 {
2695 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002696 SOURCING_LNUM = iptr->isn_lnum;
2697 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002698 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002699 status = FAIL;
2700 }
2701 else
2702 {
2703 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002704 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002705 listitem_T *li = NULL;
2706
2707 li = list_find(l, n);
2708 if (li == NULL)
2709 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002710 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002711 semsg(_(e_listidx), n);
2712 status = FAIL;
2713 }
2714 else
2715 // TODO: check for list or item locked
2716 listitem_remove(l, li);
2717 }
2718 }
2719 else
2720 {
2721 status = FAIL;
2722 semsg(_(e_cannot_index_str),
2723 vartype_name(tv_dest->v_type));
2724 }
2725
2726 clear_tv(tv_idx);
2727 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002728 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002729 if (status == FAIL)
2730 goto on_error;
2731 }
2732 break;
2733
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002734 // unlet range of items in list variable
2735 case ISN_UNLETRANGE:
2736 {
2737 // Stack contains:
2738 // -3 index1
2739 // -2 index2
2740 // -1 dict or list
2741 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2742 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2743 typval_T *tv_dest = STACK_TV_BOT(-1);
2744 int status = OK;
2745
2746 if (tv_dest->v_type == VAR_LIST)
2747 {
2748 // indexes must be a number
2749 SOURCING_LNUM = iptr->isn_lnum;
2750 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002751 || (tv_idx2->v_type != VAR_SPECIAL
2752 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002753 {
2754 status = FAIL;
2755 }
2756 else
2757 {
2758 list_T *l = tv_dest->vval.v_list;
2759 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002760 long n2 = tv_idx2->v_type == VAR_SPECIAL
2761 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002762 listitem_T *li;
2763
2764 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002765 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002766 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002767 else
2768 {
2769 if (n1 < 0)
2770 n1 = list_idx_of_item(l, li);
2771 if (n2 < 0)
2772 {
2773 listitem_T *li2 = list_find(l, n2);
2774
2775 if (li2 == NULL)
2776 status = FAIL;
2777 else
2778 n2 = list_idx_of_item(l, li2);
2779 }
2780 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002781 && tv_idx2->v_type != VAR_SPECIAL
2782 && n2 < n1)
2783 {
2784 semsg(_(e_listidx), n2);
2785 status = FAIL;
2786 }
2787 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002788 && list_unlet_range(l, li, NULL, n1,
2789 tv_idx2->v_type != VAR_SPECIAL, n2)
2790 == FAIL)
2791 status = FAIL;
2792 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002793 }
2794 }
2795 else
2796 {
2797 status = FAIL;
2798 SOURCING_LNUM = iptr->isn_lnum;
2799 semsg(_(e_cannot_index_str),
2800 vartype_name(tv_dest->v_type));
2801 }
2802
2803 clear_tv(tv_idx1);
2804 clear_tv(tv_idx2);
2805 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002806 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002807 if (status == FAIL)
2808 goto on_error;
2809 }
2810 break;
2811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 // push constant
2813 case ISN_PUSHNR:
2814 case ISN_PUSHBOOL:
2815 case ISN_PUSHSPEC:
2816 case ISN_PUSHF:
2817 case ISN_PUSHS:
2818 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002819 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002820 case ISN_PUSHCHANNEL:
2821 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02002822 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002823 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002825 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002826 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 switch (iptr->isn_type)
2828 {
2829 case ISN_PUSHNR:
2830 tv->v_type = VAR_NUMBER;
2831 tv->vval.v_number = iptr->isn_arg.number;
2832 break;
2833 case ISN_PUSHBOOL:
2834 tv->v_type = VAR_BOOL;
2835 tv->vval.v_number = iptr->isn_arg.number;
2836 break;
2837 case ISN_PUSHSPEC:
2838 tv->v_type = VAR_SPECIAL;
2839 tv->vval.v_number = iptr->isn_arg.number;
2840 break;
2841#ifdef FEAT_FLOAT
2842 case ISN_PUSHF:
2843 tv->v_type = VAR_FLOAT;
2844 tv->vval.v_float = iptr->isn_arg.fnumber;
2845 break;
2846#endif
2847 case ISN_PUSHBLOB:
2848 blob_copy(iptr->isn_arg.blob, tv);
2849 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002850 case ISN_PUSHFUNC:
2851 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002852 if (iptr->isn_arg.string == NULL)
2853 tv->vval.v_string = NULL;
2854 else
2855 tv->vval.v_string =
2856 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002857 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002858 case ISN_PUSHCHANNEL:
2859#ifdef FEAT_JOB_CHANNEL
2860 tv->v_type = VAR_CHANNEL;
2861 tv->vval.v_channel = iptr->isn_arg.channel;
2862 if (tv->vval.v_channel != NULL)
2863 ++tv->vval.v_channel->ch_refcount;
2864#endif
2865 break;
2866 case ISN_PUSHJOB:
2867#ifdef FEAT_JOB_CHANNEL
2868 tv->v_type = VAR_JOB;
2869 tv->vval.v_job = iptr->isn_arg.job;
2870 if (tv->vval.v_job != NULL)
2871 ++tv->vval.v_job->jv_refcount;
2872#endif
2873 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 default:
2875 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002876 tv->vval.v_string = vim_strsave(
2877 iptr->isn_arg.string == NULL
2878 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 }
2880 break;
2881
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002882 case ISN_UNLET:
2883 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2884 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002885 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002886 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002887 case ISN_UNLETENV:
2888 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2889 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002890
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002891 case ISN_LOCKUNLOCK:
2892 {
2893 typval_T *lval_root_save = lval_root;
2894 int res;
2895
2896 // Stack has the local variable, argument the whole :lock
2897 // or :unlock command, like ISN_EXEC.
2898 --ectx->ec_stack.ga_len;
2899 lval_root = STACK_TV_BOT(0);
2900 res = exec_command(iptr);
2901 clear_tv(lval_root);
2902 lval_root = lval_root_save;
2903 if (res == FAIL)
2904 goto on_error;
2905 }
2906 break;
2907
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002908 case ISN_LOCKCONST:
2909 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2910 break;
2911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 // create a list from items on the stack; uses a single allocation
2913 // for the list header and the items
2914 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002915 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002916 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 break;
2918
2919 // create a dict from items on the stack
2920 case ISN_NEWDICT:
2921 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002922 int count = iptr->isn_arg.number;
2923 dict_T *dict = dict_alloc();
2924 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002925 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002926 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002928 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002929 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 for (idx = 0; idx < count; ++idx)
2931 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002932 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002934 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002935 key = tv->vval.v_string == NULL
2936 ? (char_u *)"" : tv->vval.v_string;
2937 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002938 if (item != NULL)
2939 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002940 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002941 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002942 dict_unref(dict);
2943 goto on_error;
2944 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002945 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002947 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02002948 {
2949 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002950 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2953 item->di_tv.v_lock = 0;
2954 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002955 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002956 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002957 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002958 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002959 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 }
2961
2962 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002963 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02002964 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002965 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002967 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 tv = STACK_TV_BOT(-1);
2969 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002970 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 tv->vval.v_dict = dict;
2972 ++dict->dv_refcount;
2973 }
2974 break;
2975
2976 // call a :def function
2977 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002978 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002979 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2980 NULL,
2981 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002982 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002983 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 break;
2985
2986 // call a builtin function
2987 case ISN_BCALL:
2988 SOURCING_LNUM = iptr->isn_lnum;
2989 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2990 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002991 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002992 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 break;
2994
2995 // call a funcref or partial
2996 case ISN_PCALL:
2997 {
2998 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2999 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003000 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001
3002 SOURCING_LNUM = iptr->isn_lnum;
3003 if (pfunc->cpf_top)
3004 {
3005 // funcref is above the arguments
3006 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3007 }
3008 else
3009 {
3010 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003011 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003012 partial_tv = *STACK_TV_BOT(0);
3013 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003015 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003016 if (tv == &partial_tv)
3017 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003019 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 }
3021 break;
3022
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003023 case ISN_PCALL_END:
3024 // PCALL finished, arguments have been consumed and replaced by
3025 // the return value. Now clear the funcref from the stack,
3026 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003028 clear_tv(STACK_TV_BOT(-1));
3029 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3030 break;
3031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 // call a user defined function or funcref/partial
3033 case ISN_UCALL:
3034 {
3035 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3036
3037 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003038 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003039 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003040 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 }
3042 break;
3043
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003044 // return from a :def function call without a value
3045 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003046 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003047 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003048 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003049 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003050 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003051 tv->vval.v_number = 0;
3052 tv->v_lock = 0;
3053 // FALLTHROUGH
3054
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003055 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 case ISN_RETURN:
3057 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003058 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003059 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003060
3061 if (trystack->ga_len > 0)
3062 trycmd = ((trycmd_T *)trystack->ga_data)
3063 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003064 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003065 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003067 // jump to ":finally" or ":endtry"
3068 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003069 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003070 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003071 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 trycmd->tcd_return = TRUE;
3073 }
3074 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003075 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 }
3077 break;
3078
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003079 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 case ISN_FUNCREF:
3081 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003082 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
3083 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3084 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003087 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003088 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003089 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003090 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003091 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003092 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01003093 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003094 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003095 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003097 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 tv->vval.v_partial = pt;
3099 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003100 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 }
3102 break;
3103
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003104 // Create a global function from a lambda.
3105 case ISN_NEWFUNC:
3106 {
3107 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3108
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003109 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003110 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003111 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003112 }
3113 break;
3114
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003115 // List functions
3116 case ISN_DEF:
3117 if (iptr->isn_arg.string == NULL)
3118 list_functions(NULL);
3119 else
3120 {
3121 exarg_T ea;
3122
3123 CLEAR_FIELD(ea);
3124 ea.cmd = ea.arg = iptr->isn_arg.string;
3125 define_function(&ea, NULL);
3126 }
3127 break;
3128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 // jump if a condition is met
3130 case ISN_JUMP:
3131 {
3132 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003133 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 int jump = TRUE;
3135
3136 if (when != JUMP_ALWAYS)
3137 {
3138 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003139 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003140 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003141 || when == JUMP_IF_COND_TRUE)
3142 {
3143 SOURCING_LNUM = iptr->isn_lnum;
3144 jump = tv_get_bool_chk(tv, &error);
3145 if (error)
3146 goto on_error;
3147 }
3148 else
3149 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003151 || when == JUMP_AND_KEEP_IF_FALSE
3152 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003154 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 {
3156 // drop the value from the stack
3157 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003158 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 }
3160 }
3161 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003162 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 }
3164 break;
3165
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003166 // Jump if an argument with a default value was already set and not
3167 // v:none.
3168 case ISN_JUMP_IF_ARG_SET:
3169 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3170 if (tv->v_type != VAR_UNKNOWN
3171 && !(tv->v_type == VAR_SPECIAL
3172 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003173 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003174 break;
3175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 // top of a for loop
3177 case ISN_FOR:
3178 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003179 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 typval_T *idxtv =
3181 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3182
Bram Moolenaar35578162021-08-02 19:10:38 +02003183 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003184 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003185 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003186 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003187 list_T *list = ltv->vval.v_list;
3188
3189 // push the next item from the list
3190 ++idxtv->vval.v_number;
3191 if (list == NULL
3192 || idxtv->vval.v_number >= list->lv_len)
3193 {
3194 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3196 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003197 }
3198 else if (list->lv_first == &range_list_item)
3199 {
3200 // non-materialized range() list
3201 tv = STACK_TV_BOT(0);
3202 tv->v_type = VAR_NUMBER;
3203 tv->v_lock = 0;
3204 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003206 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003207 }
3208 else
3209 {
3210 listitem_T *li = list_find(list,
3211 idxtv->vval.v_number);
3212
3213 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003214 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003215 }
3216 }
3217 else if (ltv->v_type == VAR_STRING)
3218 {
3219 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003220
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003221 // The index is for the last byte of the previous
3222 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003223 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003224 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003225 {
3226 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003227 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3228 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003229 }
3230 else
3231 {
3232 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3233
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003234 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003235 tv = STACK_TV_BOT(0);
3236 tv->v_type = VAR_STRING;
3237 tv->vval.v_string = vim_strnsave(
3238 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003239 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003240 idxtv->vval.v_number += clen - 1;
3241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003243 else if (ltv->v_type == VAR_BLOB)
3244 {
3245 blob_T *blob = ltv->vval.v_blob;
3246
3247 // When we get here the first time make a copy of the
3248 // blob, so that the iteration still works when it is
3249 // changed.
3250 if (idxtv->vval.v_number == -1 && blob != NULL)
3251 {
3252 blob_copy(blob, ltv);
3253 blob_unref(blob);
3254 blob = ltv->vval.v_blob;
3255 }
3256
3257 // The index is for the previous byte.
3258 ++idxtv->vval.v_number;
3259 if (blob == NULL
3260 || idxtv->vval.v_number >= blob_len(blob))
3261 {
3262 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003263 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3264 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003265 }
3266 else
3267 {
3268 // Push the next byte from the blob.
3269 tv = STACK_TV_BOT(0);
3270 tv->v_type = VAR_NUMBER;
3271 tv->vval.v_number = blob_get(blob,
3272 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003273 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003274 }
3275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 else
3277 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003278 semsg(_(e_for_loop_on_str_not_supported),
3279 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003280 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 }
3282 }
3283 break;
3284
3285 // start of ":try" block
3286 case ISN_TRY:
3287 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003288 trycmd_T *trycmd = NULL;
3289
Bram Moolenaar35578162021-08-02 19:10:38 +02003290 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003291 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003292 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3293 + ectx->ec_trystack.ga_len;
3294 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003296 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003297 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3298 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003299 trycmd->tcd_catch_idx =
3300 iptr->isn_arg.try.try_ref->try_catch;
3301 trycmd->tcd_finally_idx =
3302 iptr->isn_arg.try.try_ref->try_finally;
3303 trycmd->tcd_endtry_idx =
3304 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 }
3306 break;
3307
3308 case ISN_PUSHEXC:
3309 if (current_exception == NULL)
3310 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003311 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003313 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003315 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003316 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003318 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003320 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 tv->vval.v_string = vim_strsave(
3322 (char_u *)current_exception->value);
3323 break;
3324
3325 case ISN_CATCH:
3326 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003327 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328
Bram Moolenaar4c137212021-04-19 16:48:48 +02003329 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 if (trystack->ga_len > 0)
3331 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003332 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 + trystack->ga_len - 1;
3334 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003335 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 }
3337 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003338 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 catch_exception(current_exception);
3340 }
3341 break;
3342
Bram Moolenaarc150c092021-02-13 15:02:46 +01003343 case ISN_TRYCONT:
3344 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003345 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003346 trycont_T *trycont = &iptr->isn_arg.trycont;
3347 int i;
3348 trycmd_T *trycmd;
3349 int iidx = trycont->tct_where;
3350
3351 if (trystack->ga_len < trycont->tct_levels)
3352 {
3353 siemsg("TRYCONT: expected %d levels, found %d",
3354 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003355 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003356 }
3357 // Make :endtry jump to any outer try block and the last
3358 // :endtry inside the loop to the loop start.
3359 for (i = trycont->tct_levels; i > 0; --i)
3360 {
3361 trycmd = ((trycmd_T *)trystack->ga_data)
3362 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003363 // Add one to tcd_cont to be able to jump to
3364 // instruction with index zero.
3365 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003366 iidx = trycmd->tcd_finally_idx == 0
3367 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003368 }
3369 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003370 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003371 }
3372 break;
3373
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003374 case ISN_FINALLY:
3375 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003376 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003377 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3378 + trystack->ga_len - 1;
3379
3380 // Reset the index to avoid a return statement jumps here
3381 // again.
3382 trycmd->tcd_finally_idx = 0;
3383 break;
3384 }
3385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 // end of ":try" block
3387 case ISN_ENDTRY:
3388 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003389 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390
3391 if (trystack->ga_len > 0)
3392 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003393 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 --trystack->ga_len;
3396 --trylevel;
3397 trycmd = ((trycmd_T *)trystack->ga_data)
3398 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003399 if (trycmd->tcd_did_throw)
3400 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003401 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 {
3403 // discard the exception
3404 if (caught_stack == current_exception)
3405 caught_stack = caught_stack->caught;
3406 discard_current_exception();
3407 }
3408
3409 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003410 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003411
Bram Moolenaar4c137212021-04-19 16:48:48 +02003412 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003413 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003414 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003415 clear_tv(STACK_TV_BOT(0));
3416 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003417 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003418 // handling :continue: jump to outer try block or
3419 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003420 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 }
3422 }
3423 break;
3424
3425 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003426 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003427 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003428
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003429 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3430 {
3431 // throwing an exception while using "silent!" causes
3432 // the function to abort but not display an error.
3433 tv = STACK_TV_BOT(-1);
3434 clear_tv(tv);
3435 tv->v_type = VAR_NUMBER;
3436 tv->vval.v_number = 0;
3437 goto done;
3438 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003439 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003440 tv = STACK_TV_BOT(0);
3441 if (tv->vval.v_string == NULL
3442 || *skipwhite(tv->vval.v_string) == NUL)
3443 {
3444 vim_free(tv->vval.v_string);
3445 SOURCING_LNUM = iptr->isn_lnum;
3446 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003447 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003448 }
3449
3450 // Inside a "catch" we need to first discard the caught
3451 // exception.
3452 if (trystack->ga_len > 0)
3453 {
3454 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3455 + trystack->ga_len - 1;
3456 if (trycmd->tcd_caught && current_exception != NULL)
3457 {
3458 // discard the exception
3459 if (caught_stack == current_exception)
3460 caught_stack = caught_stack->caught;
3461 discard_current_exception();
3462 trycmd->tcd_caught = FALSE;
3463 }
3464 }
3465
3466 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3467 == FAIL)
3468 {
3469 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003470 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003471 }
3472 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 break;
3475
3476 // compare with special values
3477 case ISN_COMPAREBOOL:
3478 case ISN_COMPARESPECIAL:
3479 {
3480 typval_T *tv1 = STACK_TV_BOT(-2);
3481 typval_T *tv2 = STACK_TV_BOT(-1);
3482 varnumber_T arg1 = tv1->vval.v_number;
3483 varnumber_T arg2 = tv2->vval.v_number;
3484 int res;
3485
3486 switch (iptr->isn_arg.op.op_type)
3487 {
3488 case EXPR_EQUAL: res = arg1 == arg2; break;
3489 case EXPR_NEQUAL: res = arg1 != arg2; break;
3490 default: res = 0; break;
3491 }
3492
Bram Moolenaar4c137212021-04-19 16:48:48 +02003493 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 tv1->v_type = VAR_BOOL;
3495 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3496 }
3497 break;
3498
3499 // Operation with two number arguments
3500 case ISN_OPNR:
3501 case ISN_COMPARENR:
3502 {
3503 typval_T *tv1 = STACK_TV_BOT(-2);
3504 typval_T *tv2 = STACK_TV_BOT(-1);
3505 varnumber_T arg1 = tv1->vval.v_number;
3506 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003507 varnumber_T res = 0;
3508 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509
3510 switch (iptr->isn_arg.op.op_type)
3511 {
3512 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003513 case EXPR_DIV: if (arg2 == 0)
3514 div_zero = TRUE;
3515 else
3516 res = arg1 / arg2;
3517 break;
3518 case EXPR_REM: if (arg2 == 0)
3519 div_zero = TRUE;
3520 else
3521 res = arg1 % arg2;
3522 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 case EXPR_SUB: res = arg1 - arg2; break;
3524 case EXPR_ADD: res = arg1 + arg2; break;
3525
3526 case EXPR_EQUAL: res = arg1 == arg2; break;
3527 case EXPR_NEQUAL: res = arg1 != arg2; break;
3528 case EXPR_GREATER: res = arg1 > arg2; break;
3529 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3530 case EXPR_SMALLER: res = arg1 < arg2; break;
3531 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003532 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 }
3534
Bram Moolenaar4c137212021-04-19 16:48:48 +02003535 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 if (iptr->isn_type == ISN_COMPARENR)
3537 {
3538 tv1->v_type = VAR_BOOL;
3539 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3540 }
3541 else
3542 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003543 if (div_zero)
3544 {
3545 SOURCING_LNUM = iptr->isn_lnum;
3546 emsg(_(e_divide_by_zero));
3547 goto on_error;
3548 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 }
3550 break;
3551
3552 // Computation with two float arguments
3553 case ISN_OPFLOAT:
3554 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003555#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 {
3557 typval_T *tv1 = STACK_TV_BOT(-2);
3558 typval_T *tv2 = STACK_TV_BOT(-1);
3559 float_T arg1 = tv1->vval.v_float;
3560 float_T arg2 = tv2->vval.v_float;
3561 float_T res = 0;
3562 int cmp = FALSE;
3563
3564 switch (iptr->isn_arg.op.op_type)
3565 {
3566 case EXPR_MULT: res = arg1 * arg2; break;
3567 case EXPR_DIV: res = arg1 / arg2; break;
3568 case EXPR_SUB: res = arg1 - arg2; break;
3569 case EXPR_ADD: res = arg1 + arg2; break;
3570
3571 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3572 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3573 case EXPR_GREATER: cmp = arg1 > arg2; break;
3574 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3575 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3576 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3577 default: cmp = 0; break;
3578 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003579 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 if (iptr->isn_type == ISN_COMPAREFLOAT)
3581 {
3582 tv1->v_type = VAR_BOOL;
3583 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3584 }
3585 else
3586 tv1->vval.v_float = res;
3587 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003588#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 break;
3590
3591 case ISN_COMPARELIST:
3592 {
3593 typval_T *tv1 = STACK_TV_BOT(-2);
3594 typval_T *tv2 = STACK_TV_BOT(-1);
3595 list_T *arg1 = tv1->vval.v_list;
3596 list_T *arg2 = tv2->vval.v_list;
3597 int cmp = FALSE;
3598 int ic = iptr->isn_arg.op.op_ic;
3599
3600 switch (iptr->isn_arg.op.op_type)
3601 {
3602 case EXPR_EQUAL: cmp =
3603 list_equal(arg1, arg2, ic, FALSE); break;
3604 case EXPR_NEQUAL: cmp =
3605 !list_equal(arg1, arg2, ic, FALSE); break;
3606 case EXPR_IS: cmp = arg1 == arg2; break;
3607 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3608 default: cmp = 0; break;
3609 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003610 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 clear_tv(tv1);
3612 clear_tv(tv2);
3613 tv1->v_type = VAR_BOOL;
3614 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3615 }
3616 break;
3617
3618 case ISN_COMPAREBLOB:
3619 {
3620 typval_T *tv1 = STACK_TV_BOT(-2);
3621 typval_T *tv2 = STACK_TV_BOT(-1);
3622 blob_T *arg1 = tv1->vval.v_blob;
3623 blob_T *arg2 = tv2->vval.v_blob;
3624 int cmp = FALSE;
3625
3626 switch (iptr->isn_arg.op.op_type)
3627 {
3628 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3629 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3630 case EXPR_IS: cmp = arg1 == arg2; break;
3631 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3632 default: cmp = 0; break;
3633 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003634 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 clear_tv(tv1);
3636 clear_tv(tv2);
3637 tv1->v_type = VAR_BOOL;
3638 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3639 }
3640 break;
3641
3642 // TODO: handle separately
3643 case ISN_COMPARESTRING:
3644 case ISN_COMPAREDICT:
3645 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 case ISN_COMPAREANY:
3647 {
3648 typval_T *tv1 = STACK_TV_BOT(-2);
3649 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003650 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 int ic = iptr->isn_arg.op.op_ic;
3652
Bram Moolenaareb26f432020-09-14 16:50:05 +02003653 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003654 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003656 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 }
3658 break;
3659
3660 case ISN_ADDLIST:
3661 case ISN_ADDBLOB:
3662 {
3663 typval_T *tv1 = STACK_TV_BOT(-2);
3664 typval_T *tv2 = STACK_TV_BOT(-1);
3665
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003666 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 if (iptr->isn_type == ISN_ADDLIST)
3668 eval_addlist(tv1, tv2);
3669 else
3670 eval_addblob(tv1, tv2);
3671 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003672 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 }
3674 break;
3675
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003676 case ISN_LISTAPPEND:
3677 {
3678 typval_T *tv1 = STACK_TV_BOT(-2);
3679 typval_T *tv2 = STACK_TV_BOT(-1);
3680 list_T *l = tv1->vval.v_list;
3681
3682 // add an item to a list
3683 if (l == NULL)
3684 {
3685 SOURCING_LNUM = iptr->isn_lnum;
3686 emsg(_(e_cannot_add_to_null_list));
3687 goto on_error;
3688 }
3689 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003690 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003691 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003692 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003693 }
3694 break;
3695
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003696 case ISN_BLOBAPPEND:
3697 {
3698 typval_T *tv1 = STACK_TV_BOT(-2);
3699 typval_T *tv2 = STACK_TV_BOT(-1);
3700 blob_T *b = tv1->vval.v_blob;
3701 int error = FALSE;
3702 varnumber_T n;
3703
3704 // add a number to a blob
3705 if (b == NULL)
3706 {
3707 SOURCING_LNUM = iptr->isn_lnum;
3708 emsg(_(e_cannot_add_to_null_blob));
3709 goto on_error;
3710 }
3711 n = tv_get_number_chk(tv2, &error);
3712 if (error)
3713 goto on_error;
3714 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003715 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003716 }
3717 break;
3718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 // Computation with two arguments of unknown type
3720 case ISN_OPANY:
3721 {
3722 typval_T *tv1 = STACK_TV_BOT(-2);
3723 typval_T *tv2 = STACK_TV_BOT(-1);
3724 varnumber_T n1, n2;
3725#ifdef FEAT_FLOAT
3726 float_T f1 = 0, f2 = 0;
3727#endif
3728 int error = FALSE;
3729
3730 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3731 {
3732 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3733 {
3734 eval_addlist(tv1, tv2);
3735 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003736 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 break;
3738 }
3739 else if (tv1->v_type == VAR_BLOB
3740 && tv2->v_type == VAR_BLOB)
3741 {
3742 eval_addblob(tv1, tv2);
3743 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003744 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 break;
3746 }
3747 }
3748#ifdef FEAT_FLOAT
3749 if (tv1->v_type == VAR_FLOAT)
3750 {
3751 f1 = tv1->vval.v_float;
3752 n1 = 0;
3753 }
3754 else
3755#endif
3756 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003757 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 n1 = tv_get_number_chk(tv1, &error);
3759 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003760 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761#ifdef FEAT_FLOAT
3762 if (tv2->v_type == VAR_FLOAT)
3763 f1 = n1;
3764#endif
3765 }
3766#ifdef FEAT_FLOAT
3767 if (tv2->v_type == VAR_FLOAT)
3768 {
3769 f2 = tv2->vval.v_float;
3770 n2 = 0;
3771 }
3772 else
3773#endif
3774 {
3775 n2 = tv_get_number_chk(tv2, &error);
3776 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003777 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778#ifdef FEAT_FLOAT
3779 if (tv1->v_type == VAR_FLOAT)
3780 f2 = n2;
3781#endif
3782 }
3783#ifdef FEAT_FLOAT
3784 // if there is a float on either side the result is a float
3785 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3786 {
3787 switch (iptr->isn_arg.op.op_type)
3788 {
3789 case EXPR_MULT: f1 = f1 * f2; break;
3790 case EXPR_DIV: f1 = f1 / f2; break;
3791 case EXPR_SUB: f1 = f1 - f2; break;
3792 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003793 default: SOURCING_LNUM = iptr->isn_lnum;
3794 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003795 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 }
3797 clear_tv(tv1);
3798 clear_tv(tv2);
3799 tv1->v_type = VAR_FLOAT;
3800 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003801 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 }
3803 else
3804#endif
3805 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003806 int failed = FALSE;
3807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 switch (iptr->isn_arg.op.op_type)
3809 {
3810 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003811 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3812 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003813 goto on_error;
3814 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 case EXPR_SUB: n1 = n1 - n2; break;
3816 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003817 default: n1 = num_modulus(n1, n2, &failed);
3818 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003819 goto on_error;
3820 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 }
3822 clear_tv(tv1);
3823 clear_tv(tv2);
3824 tv1->v_type = VAR_NUMBER;
3825 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003826 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 }
3828 }
3829 break;
3830
3831 case ISN_CONCAT:
3832 {
3833 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3834 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3835 char_u *res;
3836
3837 res = concat_str(str1, str2);
3838 clear_tv(STACK_TV_BOT(-2));
3839 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003840 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 STACK_TV_BOT(-1)->vval.v_string = res;
3842 }
3843 break;
3844
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003845 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003846 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003847 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003848 int is_slice = iptr->isn_type == ISN_STRSLICE;
3849 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003850 char_u *res;
3851
3852 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003853 // string slice: string is at stack-3, first index at
3854 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003855 if (is_slice)
3856 {
3857 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003858 n1 = tv->vval.v_number;
3859 }
3860
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003861 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003862 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003863
Bram Moolenaar4c137212021-04-19 16:48:48 +02003864 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003865 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003866 if (is_slice)
3867 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003868 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003869 else
3870 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003871 // single character (including composing characters).
3872 // If the index is too big or negative the result is
3873 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003874 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003875 vim_free(tv->vval.v_string);
3876 tv->vval.v_string = res;
3877 }
3878 break;
3879
3880 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003881 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003882 case ISN_BLOBINDEX:
3883 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003885 int is_slice = iptr->isn_type == ISN_LISTSLICE
3886 || iptr->isn_type == ISN_BLOBSLICE;
3887 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3888 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003889 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003890 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891
3892 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003893 // list slice: list is at stack-3, indexes at stack-2 and
3894 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003895 // Same for blob.
3896 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897
3898 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003899 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003901
3902 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 {
Bram Moolenaared591872020-08-15 22:14:53 +02003904 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003905 n1 = tv->vval.v_number;
3906 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 }
Bram Moolenaared591872020-08-15 22:14:53 +02003908
Bram Moolenaar4c137212021-04-19 16:48:48 +02003909 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003910 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003911 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003912 if (is_blob)
3913 {
3914 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3915 n1, n2, FALSE, tv) == FAIL)
3916 goto on_error;
3917 }
3918 else
3919 {
3920 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3921 n1, n2, FALSE, tv, TRUE) == FAIL)
3922 goto on_error;
3923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 }
3925 break;
3926
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003927 case ISN_ANYINDEX:
3928 case ISN_ANYSLICE:
3929 {
3930 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3931 typval_T *var1, *var2;
3932 int res;
3933
3934 // index: composite is at stack-2, index at stack-1
3935 // slice: composite is at stack-3, indexes at stack-2 and
3936 // stack-1
3937 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003938 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003939 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3940 goto on_error;
3941 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3942 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003943 res = eval_index_inner(tv, is_slice, var1, var2,
3944 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003945 clear_tv(var1);
3946 if (is_slice)
3947 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003948 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003949 if (res == FAIL)
3950 goto on_error;
3951 }
3952 break;
3953
Bram Moolenaar9af78762020-06-16 11:34:42 +02003954 case ISN_SLICE:
3955 {
3956 list_T *list;
3957 int count = iptr->isn_arg.number;
3958
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003959 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003960 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003961 list = tv->vval.v_list;
3962
3963 // no error for short list, expect it to be checked earlier
3964 if (list != NULL && list->lv_len >= count)
3965 {
3966 list_T *newlist = list_slice(list,
3967 count, list->lv_len - 1);
3968
3969 if (newlist != NULL)
3970 {
3971 list_unref(list);
3972 tv->vval.v_list = newlist;
3973 ++newlist->lv_refcount;
3974 }
3975 }
3976 }
3977 break;
3978
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003979 case ISN_GETITEM:
3980 {
3981 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003982 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003983
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003984 // Get list item: list is at stack-1, push item.
3985 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003986 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3987 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003988
Bram Moolenaar35578162021-08-02 19:10:38 +02003989 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003990 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003991 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003992 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003993
3994 // Useful when used in unpack assignment. Reset at
3995 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003996 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003997 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003998 }
3999 break;
4000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 case ISN_MEMBER:
4002 {
4003 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004004 char_u *key;
4005 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004006 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004007
4008 // dict member: dict is at stack-2, key at stack-1
4009 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004010 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004011 dict = tv->vval.v_dict;
4012
4013 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004014 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004015 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004016 if (key == NULL)
4017 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004018
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004019 if ((di = dict_find(dict, key, -1)) == NULL)
4020 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004021 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004022 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004023
4024 // If :silent! is used we will continue, make sure the
4025 // stack contents makes sense.
4026 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004027 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004028 tv = STACK_TV_BOT(-1);
4029 clear_tv(tv);
4030 tv->v_type = VAR_NUMBER;
4031 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004032 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004033 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004034 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004035 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004036 // Clear the dict only after getting the item, to avoid
4037 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004038 tv = STACK_TV_BOT(-1);
4039 temp_tv = *tv;
4040 copy_tv(&di->di_tv, tv);
4041 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004042 }
4043 break;
4044
4045 // dict member with string key
4046 case ISN_STRINGMEMBER:
4047 {
4048 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004050 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051
4052 tv = STACK_TV_BOT(-1);
4053 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4054 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004055 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004057 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 }
4059 dict = tv->vval.v_dict;
4060
4061 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4062 == NULL)
4063 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004064 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004066 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004068 // Clear the dict after getting the item, to avoid that it
4069 // make the item invalid.
4070 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004072 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 }
4074 break;
4075
4076 case ISN_NEGATENR:
4077 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004078 if (tv->v_type != VAR_NUMBER
4079#ifdef FEAT_FLOAT
4080 && tv->v_type != VAR_FLOAT
4081#endif
4082 )
4083 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004084 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004085 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004086 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004087 }
4088#ifdef FEAT_FLOAT
4089 if (tv->v_type == VAR_FLOAT)
4090 tv->vval.v_float = -tv->vval.v_float;
4091 else
4092#endif
4093 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 break;
4095
4096 case ISN_CHECKNR:
4097 {
4098 int error = FALSE;
4099
4100 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004101 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004103 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 (void)tv_get_number_chk(tv, &error);
4105 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004106 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 }
4108 break;
4109
4110 case ISN_CHECKTYPE:
4111 {
4112 checktype_T *ct = &iptr->isn_arg.type;
4113
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004114 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004115 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004116 if (!ectx->ec_where.wt_variable)
4117 ectx->ec_where.wt_index = ct->ct_arg_idx;
4118 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4119 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004120 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004121 if (!ectx->ec_where.wt_variable)
4122 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004123
4124 // number 0 is FALSE, number 1 is TRUE
4125 if (tv->v_type == VAR_NUMBER
4126 && ct->ct_type->tt_type == VAR_BOOL
4127 && (tv->vval.v_number == 0
4128 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004130 tv->v_type = VAR_BOOL;
4131 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004132 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 }
4134 }
4135 break;
4136
Bram Moolenaar9af78762020-06-16 11:34:42 +02004137 case ISN_CHECKLEN:
4138 {
4139 int min_len = iptr->isn_arg.checklen.cl_min_len;
4140 list_T *list = NULL;
4141
4142 tv = STACK_TV_BOT(-1);
4143 if (tv->v_type == VAR_LIST)
4144 list = tv->vval.v_list;
4145 if (list == NULL || list->lv_len < min_len
4146 || (list->lv_len > min_len
4147 && !iptr->isn_arg.checklen.cl_more_OK))
4148 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004149 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004150 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004151 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004152 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004153 }
4154 }
4155 break;
4156
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004157 case ISN_SETTYPE:
4158 {
4159 checktype_T *ct = &iptr->isn_arg.type;
4160
4161 tv = STACK_TV_BOT(-1);
4162 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4163 {
4164 free_type(tv->vval.v_dict->dv_type);
4165 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4166 }
4167 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4168 {
4169 free_type(tv->vval.v_list->lv_type);
4170 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4171 }
4172 }
4173 break;
4174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004176 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 {
4178 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004179 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004181 if (iptr->isn_type == ISN_2BOOL)
4182 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004183 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004184 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004185 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004186 n = !n;
4187 }
4188 else
4189 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004190 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004191 SOURCING_LNUM = iptr->isn_lnum;
4192 n = tv_get_bool_chk(tv, &error);
4193 if (error)
4194 goto on_error;
4195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 clear_tv(tv);
4197 tv->v_type = VAR_BOOL;
4198 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4199 }
4200 break;
4201
4202 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004203 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004204 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004205 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4206 iptr->isn_type == ISN_2STRING_ANY,
4207 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004208 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 break;
4210
Bram Moolenaar08597872020-12-10 19:43:40 +01004211 case ISN_RANGE:
4212 {
4213 exarg_T ea;
4214 char *errormsg;
4215
Bram Moolenaarece0b872021-01-08 20:40:45 +01004216 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004217 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004218 ea.addr_type = ADDR_LINES;
4219 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004220 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004221 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004222 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004223
Bram Moolenaar35578162021-08-02 19:10:38 +02004224 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004225 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004226 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004227 tv = STACK_TV_BOT(-1);
4228 tv->v_type = VAR_NUMBER;
4229 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004230 if (ea.addr_count == 0)
4231 tv->vval.v_number = curwin->w_cursor.lnum;
4232 else
4233 tv->vval.v_number = ea.line2;
4234 }
4235 break;
4236
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004237 case ISN_PUT:
4238 {
4239 int regname = iptr->isn_arg.put.put_regname;
4240 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4241 char_u *expr = NULL;
4242 int dir = FORWARD;
4243
Bram Moolenaar08597872020-12-10 19:43:40 +01004244 if (lnum < -2)
4245 {
4246 // line number was put on the stack by ISN_RANGE
4247 tv = STACK_TV_BOT(-1);
4248 curwin->w_cursor.lnum = tv->vval.v_number;
4249 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4250 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004251 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004252 }
4253 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004254 // :put! above cursor
4255 dir = BACKWARD;
4256 else if (lnum >= 0)
4257 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004258
4259 if (regname == '=')
4260 {
4261 tv = STACK_TV_BOT(-1);
4262 if (tv->v_type == VAR_STRING)
4263 expr = tv->vval.v_string;
4264 else
4265 {
4266 expr = typval2string(tv, TRUE); // allocates value
4267 clear_tv(tv);
4268 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004269 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004270 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004271 check_cursor();
4272 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4273 vim_free(expr);
4274 }
4275 break;
4276
Bram Moolenaar02194d22020-10-24 23:08:38 +02004277 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004278 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4279 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4280 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4281 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004282 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4283 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004284 break;
4285
Bram Moolenaar02194d22020-10-24 23:08:38 +02004286 case ISN_CMDMOD_REV:
4287 // filter regprog is owned by the instruction, don't free it
4288 cmdmod.cmod_filter_regmatch.regprog = NULL;
4289 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004290 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4291 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004292 break;
4293
Bram Moolenaar792f7862020-11-23 08:31:18 +01004294 case ISN_UNPACK:
4295 {
4296 int count = iptr->isn_arg.unpack.unp_count;
4297 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4298 list_T *l;
4299 listitem_T *li;
4300 int i;
4301
4302 // Check there is a valid list to unpack.
4303 tv = STACK_TV_BOT(-1);
4304 if (tv->v_type != VAR_LIST)
4305 {
4306 SOURCING_LNUM = iptr->isn_lnum;
4307 emsg(_(e_for_argument_must_be_sequence_of_lists));
4308 goto on_error;
4309 }
4310 l = tv->vval.v_list;
4311 if (l == NULL
4312 || l->lv_len < (semicolon ? count - 1 : count))
4313 {
4314 SOURCING_LNUM = iptr->isn_lnum;
4315 emsg(_(e_list_value_does_not_have_enough_items));
4316 goto on_error;
4317 }
4318 else if (!semicolon && l->lv_len > count)
4319 {
4320 SOURCING_LNUM = iptr->isn_lnum;
4321 emsg(_(e_list_value_has_more_items_than_targets));
4322 goto on_error;
4323 }
4324
4325 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004326 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004327 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004328 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004329
4330 // Variable after semicolon gets a list with the remaining
4331 // items.
4332 if (semicolon)
4333 {
4334 list_T *rem_list =
4335 list_alloc_with_items(l->lv_len - count + 1);
4336
4337 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004338 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004339 tv = STACK_TV_BOT(-count);
4340 tv->vval.v_list = rem_list;
4341 ++rem_list->lv_refcount;
4342 tv->v_lock = 0;
4343 li = l->lv_first;
4344 for (i = 0; i < count - 1; ++i)
4345 li = li->li_next;
4346 for (i = 0; li != NULL; ++i)
4347 {
4348 list_set_item(rem_list, i, &li->li_tv);
4349 li = li->li_next;
4350 }
4351 --count;
4352 }
4353
4354 // Produce the values in reverse order, first item last.
4355 li = l->lv_first;
4356 for (i = 0; i < count; ++i)
4357 {
4358 tv = STACK_TV_BOT(-i - 1);
4359 copy_tv(&li->li_tv, tv);
4360 li = li->li_next;
4361 }
4362
4363 list_unref(l);
4364 }
4365 break;
4366
Bram Moolenaarb2049902021-01-24 12:53:53 +01004367 case ISN_PROF_START:
4368 case ISN_PROF_END:
4369 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004370#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004371 funccall_T cookie;
4372 ufunc_T *cur_ufunc =
4373 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004374 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004375
4376 cookie.func = cur_ufunc;
4377 if (iptr->isn_type == ISN_PROF_START)
4378 {
4379 func_line_start(&cookie, iptr->isn_lnum);
4380 // if we get here the instruction is executed
4381 func_line_exec(&cookie);
4382 }
4383 else
4384 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004385#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004386 }
4387 break;
4388
Bram Moolenaare99d4222021-06-13 14:01:26 +02004389 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004390 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004391 break;
4392
Bram Moolenaar389df252020-07-09 21:20:47 +02004393 case ISN_SHUFFLE:
4394 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004395 typval_T tmp_tv;
4396 int item = iptr->isn_arg.shuffle.shfl_item;
4397 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004398
4399 tmp_tv = *STACK_TV_BOT(-item);
4400 for ( ; up > 0 && item > 1; --up)
4401 {
4402 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4403 --item;
4404 }
4405 *STACK_TV_BOT(-item) = tmp_tv;
4406 }
4407 break;
4408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004410 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004412 ectx->ec_where.wt_index = 0;
4413 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 break;
4415 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004416 continue;
4417
Bram Moolenaard032f342020-07-18 18:13:02 +02004418func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004419 // Restore previous function. If the frame pointer is where we started
4420 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004421 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004422 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004423
Bram Moolenaar4c137212021-04-19 16:48:48 +02004424 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004425 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004426 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004427 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004428
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004429on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004430 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004431 // If "emsg_silent" is set then ignore the error, unless it was set
4432 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004433 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004434 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004435 {
4436 // If a sequence of instructions causes an error while ":silent!"
4437 // was used, restore the stack length and jump ahead to restoring
4438 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004439 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004440 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004441 while (ectx->ec_stack.ga_len
4442 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004443 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004444 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004445 clear_tv(STACK_TV_BOT(0));
4446 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004447 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4448 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004449 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004450 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004451 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004452on_fatal_error:
4453 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004454 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004455 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004456 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 }
4458
4459done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004460 ret = OK;
4461theend:
4462 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4463 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004464}
4465
4466/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004467 * Execute the instructions from a VAR_INSTR typeval and put the result in
4468 * "rettv".
4469 * Return OK or FAIL.
4470 */
4471 int
4472exe_typval_instr(typval_T *tv, typval_T *rettv)
4473{
4474 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4475 isn_T *save_instr = ectx->ec_instr;
4476 int save_iidx = ectx->ec_iidx;
4477 int res;
4478
4479 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4480 res = exec_instructions(ectx);
4481 if (res == OK)
4482 {
4483 *rettv = *STACK_TV_BOT(-1);
4484 --ectx->ec_stack.ga_len;
4485 }
4486
4487 ectx->ec_instr = save_instr;
4488 ectx->ec_iidx = save_iidx;
4489
4490 return res;
4491}
4492
4493/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004494 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4495 * "substitute_instr".
4496 */
4497 char_u *
4498exe_substitute_instr(void)
4499{
4500 ectx_T *ectx = substitute_instr->subs_ectx;
4501 isn_T *save_instr = ectx->ec_instr;
4502 int save_iidx = ectx->ec_iidx;
4503 char_u *res;
4504
4505 ectx->ec_instr = substitute_instr->subs_instr;
4506 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004507 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004508 typval_T *tv = STACK_TV_BOT(-1);
4509
Bram Moolenaar27523602021-06-05 21:36:19 +02004510 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004511 --ectx->ec_stack.ga_len;
4512 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004513 }
4514 else
4515 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004516 substitute_instr->subs_status = FAIL;
4517 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519
Bram Moolenaar4c137212021-04-19 16:48:48 +02004520 ectx->ec_instr = save_instr;
4521 ectx->ec_iidx = save_iidx;
4522
4523 return res;
4524}
4525
4526/*
4527 * Call a "def" function from old Vim script.
4528 * Return OK or FAIL.
4529 */
4530 int
4531call_def_function(
4532 ufunc_T *ufunc,
4533 int argc_arg, // nr of arguments
4534 typval_T *argv, // arguments
4535 partial_T *partial, // optional partial for context
4536 typval_T *rettv) // return value
4537{
4538 ectx_T ectx; // execution context
4539 int argc = argc_arg;
4540 typval_T *tv;
4541 int idx;
4542 int ret = FAIL;
4543 int defcount = ufunc->uf_args.ga_len - argc;
4544 sctx_T save_current_sctx = current_sctx;
4545 int did_emsg_before = did_emsg_cumul + did_emsg;
4546 int save_suppress_errthrow = suppress_errthrow;
4547 msglist_T **saved_msg_list = NULL;
4548 msglist_T *private_msg_list = NULL;
4549 int save_emsg_silent_def = emsg_silent_def;
4550 int save_did_emsg_def = did_emsg_def;
4551 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004552 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004553
4554// Get pointer to item in the stack.
4555#undef STACK_TV
4556#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4557
4558// Get pointer to item at the bottom of the stack, -1 is the bottom.
4559#undef STACK_TV_BOT
4560#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4561
4562// Get pointer to a local variable on the stack. Negative for arguments.
4563#undef STACK_TV_VAR
4564#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4565
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004566 // Update uf_has_breakpoint if needed.
4567 update_has_breakpoint(ufunc);
4568
Bram Moolenaar4c137212021-04-19 16:48:48 +02004569 if (ufunc->uf_def_status == UF_NOT_COMPILED
4570 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004571 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4572 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004573 == FAIL))
4574 {
4575 if (did_emsg_cumul + did_emsg == did_emsg_before)
4576 semsg(_(e_function_is_not_compiled_str),
4577 printable_func_name(ufunc));
4578 return FAIL;
4579 }
4580
4581 {
4582 // Check the function was really compiled.
4583 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4584 + ufunc->uf_dfunc_idx;
4585 if (INSTRUCTIONS(dfunc) == NULL)
4586 {
4587 iemsg("using call_def_function() on not compiled function");
4588 return FAIL;
4589 }
4590 }
4591
4592 // If depth of calling is getting too high, don't execute the function.
4593 orig_funcdepth = funcdepth_get();
4594 if (funcdepth_increment() == FAIL)
4595 return FAIL;
4596
4597 CLEAR_FIELD(ectx);
4598 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4599 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004600 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004601 {
4602 funcdepth_decrement();
4603 return FAIL;
4604 }
4605 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4606 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4607 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004608 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004609
4610 idx = argc - ufunc->uf_args.ga_len;
4611 if (idx > 0 && ufunc->uf_va_name == NULL)
4612 {
4613 if (idx == 1)
4614 emsg(_(e_one_argument_too_many));
4615 else
4616 semsg(_(e_nr_arguments_too_many), idx);
4617 goto failed_early;
4618 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004619 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4620 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004621 {
4622 if (idx == -1)
4623 emsg(_(e_one_argument_too_few));
4624 else
4625 semsg(_(e_nr_arguments_too_few), -idx);
4626 goto failed_early;
4627 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004628
4629 // Put arguments on the stack, but no more than what the function expects.
4630 // A lambda can be called with more arguments than it uses.
4631 for (idx = 0; idx < argc
4632 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4633 ++idx)
4634 {
4635 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4636 && argv[idx].v_type == VAR_SPECIAL
4637 && argv[idx].vval.v_number == VVAL_NONE)
4638 {
4639 // Use the default value.
4640 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4641 }
4642 else
4643 {
4644 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4645 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004646 ufunc->uf_arg_types[idx], &argv[idx],
4647 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004648 goto failed_early;
4649 copy_tv(&argv[idx], STACK_TV_BOT(0));
4650 }
4651 ++ectx.ec_stack.ga_len;
4652 }
4653
4654 // Turn varargs into a list. Empty list if no args.
4655 if (ufunc->uf_va_name != NULL)
4656 {
4657 int vararg_count = argc - ufunc->uf_args.ga_len;
4658
4659 if (vararg_count < 0)
4660 vararg_count = 0;
4661 else
4662 argc -= vararg_count;
4663 if (exe_newlist(vararg_count, &ectx) == FAIL)
4664 goto failed_early;
4665
4666 // Check the type of the list items.
4667 tv = STACK_TV_BOT(-1);
4668 if (ufunc->uf_va_type != NULL
4669 && ufunc->uf_va_type != &t_list_any
4670 && ufunc->uf_va_type->tt_member != &t_any
4671 && tv->vval.v_list != NULL)
4672 {
4673 type_T *expected = ufunc->uf_va_type->tt_member;
4674 listitem_T *li = tv->vval.v_list->lv_first;
4675
4676 for (idx = 0; idx < vararg_count; ++idx)
4677 {
4678 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004679 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004680 goto failed_early;
4681 li = li->li_next;
4682 }
4683 }
4684
4685 if (defcount > 0)
4686 // Move varargs list to below missing default arguments.
4687 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4688 --ectx.ec_stack.ga_len;
4689 }
4690
4691 // Make space for omitted arguments, will store default value below.
4692 // Any varargs list goes after them.
4693 if (defcount > 0)
4694 for (idx = 0; idx < defcount; ++idx)
4695 {
4696 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4697 ++ectx.ec_stack.ga_len;
4698 }
4699 if (ufunc->uf_va_name != NULL)
4700 ++ectx.ec_stack.ga_len;
4701
4702 // Frame pointer points to just after arguments.
4703 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4704 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4705
4706 {
4707 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4708 + ufunc->uf_dfunc_idx;
4709 ufunc_T *base_ufunc = dfunc->df_ufunc;
4710
4711 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4712 // by copy_func().
4713 if (partial != NULL || base_ufunc->uf_partial != NULL)
4714 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004715 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4716 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004717 goto failed_early;
4718 if (partial != NULL)
4719 {
4720 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4721 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004722 if (current_ectx->ec_outer_ref != NULL
4723 && current_ectx->ec_outer_ref->or_outer != NULL)
4724 ectx.ec_outer_ref->or_outer =
4725 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004726 }
4727 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004728 {
4729 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4730 ++partial->pt_refcount;
4731 ectx.ec_outer_ref->or_partial = partial;
4732 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004733 }
4734 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004735 {
4736 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4737 ++base_ufunc->uf_partial->pt_refcount;
4738 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4739 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004740 }
4741 }
4742
4743 // dummy frame entries
4744 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4745 {
4746 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4747 ++ectx.ec_stack.ga_len;
4748 }
4749
4750 {
4751 // Reserve space for local variables and any closure reference count.
4752 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4753 + ufunc->uf_dfunc_idx;
4754
4755 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4756 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4757 ectx.ec_stack.ga_len += dfunc->df_varcount;
4758 if (dfunc->df_has_closure)
4759 {
4760 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4761 STACK_TV_VAR(idx)->vval.v_number = 0;
4762 ++ectx.ec_stack.ga_len;
4763 }
4764
4765 ectx.ec_instr = INSTRUCTIONS(dfunc);
4766 }
4767
4768 // Following errors are in the function, not the caller.
4769 // Commands behave like vim9script.
4770 estack_push_ufunc(ufunc, 1);
4771 current_sctx = ufunc->uf_script_ctx;
4772 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4773
4774 // Use a specific location for storing error messages to be converted to an
4775 // exception.
4776 saved_msg_list = msg_list;
4777 msg_list = &private_msg_list;
4778
4779 // Do turn errors into exceptions.
4780 suppress_errthrow = FALSE;
4781
4782 // Do not delete the function while executing it.
4783 ++ufunc->uf_calls;
4784
4785 // When ":silent!" was used before calling then we still abort the
4786 // function. If ":silent!" is used in the function then we don't.
4787 emsg_silent_def = emsg_silent;
4788 did_emsg_def = 0;
4789
4790 ectx.ec_where.wt_index = 0;
4791 ectx.ec_where.wt_variable = FALSE;
4792
4793 // Execute the instructions until done.
4794 ret = exec_instructions(&ectx);
4795 if (ret == OK)
4796 {
4797 // function finished, get result from the stack.
4798 if (ufunc->uf_ret_type == &t_void)
4799 {
4800 rettv->v_type = VAR_VOID;
4801 }
4802 else
4803 {
4804 tv = STACK_TV_BOT(-1);
4805 *rettv = *tv;
4806 tv->v_type = VAR_UNKNOWN;
4807 }
4808 }
4809
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004810 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004811 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4812 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004813
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004814 // Deal with any remaining closures, they may be in use somewhere.
4815 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004816 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004817 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004818 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4819 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004820
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004821 estack_pop();
4822 current_sctx = save_current_sctx;
4823
Bram Moolenaarc970e422021-03-17 15:03:04 +01004824 // TODO: when is it safe to delete the function if it is no longer used?
4825 --ufunc->uf_calls;
4826
Bram Moolenaar352134b2020-10-17 22:04:08 +02004827 if (*msg_list != NULL && saved_msg_list != NULL)
4828 {
4829 msglist_T **plist = saved_msg_list;
4830
4831 // Append entries from the current msg_list (uncaught exceptions) to
4832 // the saved msg_list.
4833 while (*plist != NULL)
4834 plist = &(*plist)->next;
4835
4836 *plist = *msg_list;
4837 }
4838 msg_list = saved_msg_list;
4839
Bram Moolenaar4c137212021-04-19 16:48:48 +02004840 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004841 {
4842 cmdmod.cmod_filter_regmatch.regprog = NULL;
4843 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004844 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004845 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004846 emsg_silent_def = save_emsg_silent_def;
4847 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004848
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004849failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004850 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4852 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004853 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004856 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004857 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004858 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004859 if (ectx.ec_outer_ref->or_outer_allocated)
4860 vim_free(ectx.ec_outer_ref->or_outer);
4861 partial_unref(ectx.ec_outer_ref->or_partial);
4862 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004863 }
4864
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004865 // Not sure if this is necessary.
4866 suppress_errthrow = save_suppress_errthrow;
4867
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004868 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
4869 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004870 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004871 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004872 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 return ret;
4874}
4875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004877 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4878 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4879 * "pfx" is prefixed to every line.
4880 */
4881 static void
4882list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4883{
4884 int line_idx = 0;
4885 int prev_current = 0;
4886 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004887 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004888
4889 for (current = 0; current < instr_count; ++current)
4890 {
4891 isn_T *iptr = &instr[current];
4892 char *line;
4893
4894 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004895 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004896 while (line_idx < iptr->isn_lnum
4897 && line_idx < ufunc->uf_lines.ga_len)
4898 {
4899 if (current > prev_current)
4900 {
4901 msg_puts("\n\n");
4902 prev_current = current;
4903 }
4904 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4905 if (line != NULL)
4906 msg(line);
4907 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004908 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4909 {
4910 int first_def_arg = ufunc->uf_args.ga_len
4911 - ufunc->uf_def_args.ga_len;
4912
4913 if (def_arg_idx > 0)
4914 msg_puts("\n\n");
4915 msg_start();
4916 msg_puts(" ");
4917 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4918 first_def_arg + def_arg_idx]);
4919 msg_puts(" = ");
4920 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4921 msg_clr_eos();
4922 msg_end();
4923 }
4924 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004925
4926 switch (iptr->isn_type)
4927 {
4928 case ISN_EXEC:
4929 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4930 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004931 case ISN_EXEC_SPLIT:
4932 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4933 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004934 case ISN_LEGACY_EVAL:
4935 smsg("%s%4d EVAL legacy %s", pfx, current,
4936 iptr->isn_arg.string);
4937 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004938 case ISN_REDIRSTART:
4939 smsg("%s%4d REDIR", pfx, current);
4940 break;
4941 case ISN_REDIREND:
4942 smsg("%s%4d REDIR END%s", pfx, current,
4943 iptr->isn_arg.number ? " append" : "");
4944 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004945 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004946#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004947 smsg("%s%4d CEXPR pre %s", pfx, current,
4948 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004949#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004950 break;
4951 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004952#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004953 {
4954 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4955
4956 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4957 cexpr_get_auname(cer->cer_cmdidx),
4958 cer->cer_forceit ? "!" : "",
4959 cer->cer_cmdline);
4960 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004961#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004962 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004963 case ISN_INSTR:
4964 {
4965 smsg("%s%4d INSTR", pfx, current);
4966 list_instructions(" ", iptr->isn_arg.instr,
4967 INT_MAX, NULL);
4968 msg(" -------------");
4969 }
4970 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004971 case ISN_SUBSTITUTE:
4972 {
4973 subs_T *subs = &iptr->isn_arg.subs;
4974
4975 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4976 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4977 msg(" -------------");
4978 }
4979 break;
4980 case ISN_EXECCONCAT:
4981 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4982 (varnumber_T)iptr->isn_arg.number);
4983 break;
4984 case ISN_ECHO:
4985 {
4986 echo_T *echo = &iptr->isn_arg.echo;
4987
4988 smsg("%s%4d %s %d", pfx, current,
4989 echo->echo_with_white ? "ECHO" : "ECHON",
4990 echo->echo_count);
4991 }
4992 break;
4993 case ISN_EXECUTE:
4994 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02004995 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004996 break;
4997 case ISN_ECHOMSG:
4998 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02004999 (varnumber_T)(iptr->isn_arg.number));
5000 break;
5001 case ISN_ECHOCONSOLE:
5002 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5003 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005004 break;
5005 case ISN_ECHOERR:
5006 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005007 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005008 break;
5009 case ISN_LOAD:
5010 {
5011 if (iptr->isn_arg.number < 0)
5012 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5013 (varnumber_T)(iptr->isn_arg.number
5014 + STACK_FRAME_SIZE));
5015 else
5016 smsg("%s%4d LOAD $%lld", pfx, current,
5017 (varnumber_T)(iptr->isn_arg.number));
5018 }
5019 break;
5020 case ISN_LOADOUTER:
5021 {
5022 if (iptr->isn_arg.number < 0)
5023 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5024 iptr->isn_arg.outer.outer_depth,
5025 iptr->isn_arg.outer.outer_idx
5026 + STACK_FRAME_SIZE);
5027 else
5028 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5029 iptr->isn_arg.outer.outer_depth,
5030 iptr->isn_arg.outer.outer_idx);
5031 }
5032 break;
5033 case ISN_LOADV:
5034 smsg("%s%4d LOADV v:%s", pfx, current,
5035 get_vim_var_name(iptr->isn_arg.number));
5036 break;
5037 case ISN_LOADSCRIPT:
5038 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005039 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5040 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5041 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005042
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005043 sv = get_script_svar(sref, -1);
5044 if (sv == NULL)
5045 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5046 pfx, current, si->sn_name);
5047 else
5048 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005049 sv->sv_name,
5050 sref->sref_idx,
5051 si->sn_name);
5052 }
5053 break;
5054 case ISN_LOADS:
5055 {
5056 scriptitem_T *si = SCRIPT_ITEM(
5057 iptr->isn_arg.loadstore.ls_sid);
5058
5059 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5060 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5061 }
5062 break;
5063 case ISN_LOADAUTO:
5064 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5065 break;
5066 case ISN_LOADG:
5067 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5068 break;
5069 case ISN_LOADB:
5070 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5071 break;
5072 case ISN_LOADW:
5073 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5074 break;
5075 case ISN_LOADT:
5076 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5077 break;
5078 case ISN_LOADGDICT:
5079 smsg("%s%4d LOAD g:", pfx, current);
5080 break;
5081 case ISN_LOADBDICT:
5082 smsg("%s%4d LOAD b:", pfx, current);
5083 break;
5084 case ISN_LOADWDICT:
5085 smsg("%s%4d LOAD w:", pfx, current);
5086 break;
5087 case ISN_LOADTDICT:
5088 smsg("%s%4d LOAD t:", pfx, current);
5089 break;
5090 case ISN_LOADOPT:
5091 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5092 break;
5093 case ISN_LOADENV:
5094 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5095 break;
5096 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005097 smsg("%s%4d LOADREG @%c", pfx, current,
5098 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005099 break;
5100
5101 case ISN_STORE:
5102 if (iptr->isn_arg.number < 0)
5103 smsg("%s%4d STORE arg[%lld]", pfx, current,
5104 iptr->isn_arg.number + STACK_FRAME_SIZE);
5105 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005106 smsg("%s%4d STORE $%lld", pfx, current,
5107 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005108 break;
5109 case ISN_STOREOUTER:
5110 {
5111 if (iptr->isn_arg.number < 0)
5112 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5113 iptr->isn_arg.outer.outer_depth,
5114 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5115 else
5116 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5117 iptr->isn_arg.outer.outer_depth,
5118 iptr->isn_arg.outer.outer_idx);
5119 }
5120 break;
5121 case ISN_STOREV:
5122 smsg("%s%4d STOREV v:%s", pfx, current,
5123 get_vim_var_name(iptr->isn_arg.number));
5124 break;
5125 case ISN_STOREAUTO:
5126 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5127 break;
5128 case ISN_STOREG:
5129 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5130 break;
5131 case ISN_STOREB:
5132 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5133 break;
5134 case ISN_STOREW:
5135 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5136 break;
5137 case ISN_STORET:
5138 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5139 break;
5140 case ISN_STORES:
5141 {
5142 scriptitem_T *si = SCRIPT_ITEM(
5143 iptr->isn_arg.loadstore.ls_sid);
5144
5145 smsg("%s%4d STORES %s in %s", pfx, current,
5146 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5147 }
5148 break;
5149 case ISN_STORESCRIPT:
5150 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005151 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5152 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5153 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005154
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005155 sv = get_script_svar(sref, -1);
5156 if (sv == NULL)
5157 smsg("%s%4d STORESCRIPT [deleted] in %s",
5158 pfx, current, si->sn_name);
5159 else
5160 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005161 sv->sv_name,
5162 sref->sref_idx,
5163 si->sn_name);
5164 }
5165 break;
5166 case ISN_STOREOPT:
5167 smsg("%s%4d STOREOPT &%s", pfx, current,
5168 iptr->isn_arg.storeopt.so_name);
5169 break;
5170 case ISN_STOREENV:
5171 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5172 break;
5173 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005174 smsg("%s%4d STOREREG @%c", pfx, current,
5175 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005176 break;
5177 case ISN_STORENR:
5178 smsg("%s%4d STORE %lld in $%d", pfx, current,
5179 iptr->isn_arg.storenr.stnr_val,
5180 iptr->isn_arg.storenr.stnr_idx);
5181 break;
5182
5183 case ISN_STOREINDEX:
5184 smsg("%s%4d STOREINDEX %s", pfx, current,
5185 vartype_name(iptr->isn_arg.vartype));
5186 break;
5187
5188 case ISN_STORERANGE:
5189 smsg("%s%4d STORERANGE", pfx, current);
5190 break;
5191
5192 // constants
5193 case ISN_PUSHNR:
5194 smsg("%s%4d PUSHNR %lld", pfx, current,
5195 (varnumber_T)(iptr->isn_arg.number));
5196 break;
5197 case ISN_PUSHBOOL:
5198 case ISN_PUSHSPEC:
5199 smsg("%s%4d PUSH %s", pfx, current,
5200 get_var_special_name(iptr->isn_arg.number));
5201 break;
5202 case ISN_PUSHF:
5203#ifdef FEAT_FLOAT
5204 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5205#endif
5206 break;
5207 case ISN_PUSHS:
5208 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5209 break;
5210 case ISN_PUSHBLOB:
5211 {
5212 char_u *r;
5213 char_u numbuf[NUMBUFLEN];
5214 char_u *tofree;
5215
5216 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5217 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5218 vim_free(tofree);
5219 }
5220 break;
5221 case ISN_PUSHFUNC:
5222 {
5223 char *name = (char *)iptr->isn_arg.string;
5224
5225 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5226 name == NULL ? "[none]" : name);
5227 }
5228 break;
5229 case ISN_PUSHCHANNEL:
5230#ifdef FEAT_JOB_CHANNEL
5231 {
5232 channel_T *channel = iptr->isn_arg.channel;
5233
5234 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5235 channel == NULL ? 0 : channel->ch_id);
5236 }
5237#endif
5238 break;
5239 case ISN_PUSHJOB:
5240#ifdef FEAT_JOB_CHANNEL
5241 {
5242 typval_T tv;
5243 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005244 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005245
5246 tv.v_type = VAR_JOB;
5247 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005248 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005249 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5250 }
5251#endif
5252 break;
5253 case ISN_PUSHEXC:
5254 smsg("%s%4d PUSH v:exception", pfx, current);
5255 break;
5256 case ISN_UNLET:
5257 smsg("%s%4d UNLET%s %s", pfx, current,
5258 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5259 iptr->isn_arg.unlet.ul_name);
5260 break;
5261 case ISN_UNLETENV:
5262 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5263 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5264 iptr->isn_arg.unlet.ul_name);
5265 break;
5266 case ISN_UNLETINDEX:
5267 smsg("%s%4d UNLETINDEX", pfx, current);
5268 break;
5269 case ISN_UNLETRANGE:
5270 smsg("%s%4d UNLETRANGE", pfx, current);
5271 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005272 case ISN_LOCKUNLOCK:
5273 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5274 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005275 case ISN_LOCKCONST:
5276 smsg("%s%4d LOCKCONST", pfx, current);
5277 break;
5278 case ISN_NEWLIST:
5279 smsg("%s%4d NEWLIST size %lld", pfx, current,
5280 (varnumber_T)(iptr->isn_arg.number));
5281 break;
5282 case ISN_NEWDICT:
5283 smsg("%s%4d NEWDICT size %lld", pfx, current,
5284 (varnumber_T)(iptr->isn_arg.number));
5285 break;
5286
5287 // function call
5288 case ISN_BCALL:
5289 {
5290 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5291
5292 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5293 internal_func_name(cbfunc->cbf_idx),
5294 cbfunc->cbf_argcount);
5295 }
5296 break;
5297 case ISN_DCALL:
5298 {
5299 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5300 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5301 + cdfunc->cdf_idx;
5302
5303 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005304 printable_func_name(df->df_ufunc),
5305 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005306 }
5307 break;
5308 case ISN_UCALL:
5309 {
5310 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5311
5312 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5313 cufunc->cuf_name, cufunc->cuf_argcount);
5314 }
5315 break;
5316 case ISN_PCALL:
5317 {
5318 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5319
5320 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5321 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5322 }
5323 break;
5324 case ISN_PCALL_END:
5325 smsg("%s%4d PCALL end", pfx, current);
5326 break;
5327 case ISN_RETURN:
5328 smsg("%s%4d RETURN", pfx, current);
5329 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005330 case ISN_RETURN_VOID:
5331 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005332 break;
5333 case ISN_FUNCREF:
5334 {
5335 funcref_T *funcref = &iptr->isn_arg.funcref;
5336 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5337 + funcref->fr_func;
5338
5339 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5340 }
5341 break;
5342
5343 case ISN_NEWFUNC:
5344 {
5345 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5346
5347 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5348 newfunc->nf_lambda, newfunc->nf_global);
5349 }
5350 break;
5351
5352 case ISN_DEF:
5353 {
5354 char_u *name = iptr->isn_arg.string;
5355
5356 smsg("%s%4d DEF %s", pfx, current,
5357 name == NULL ? (char_u *)"" : name);
5358 }
5359 break;
5360
5361 case ISN_JUMP:
5362 {
5363 char *when = "?";
5364
5365 switch (iptr->isn_arg.jump.jump_when)
5366 {
5367 case JUMP_ALWAYS:
5368 when = "JUMP";
5369 break;
5370 case JUMP_AND_KEEP_IF_TRUE:
5371 when = "JUMP_AND_KEEP_IF_TRUE";
5372 break;
5373 case JUMP_IF_FALSE:
5374 when = "JUMP_IF_FALSE";
5375 break;
5376 case JUMP_AND_KEEP_IF_FALSE:
5377 when = "JUMP_AND_KEEP_IF_FALSE";
5378 break;
5379 case JUMP_IF_COND_FALSE:
5380 when = "JUMP_IF_COND_FALSE";
5381 break;
5382 case JUMP_IF_COND_TRUE:
5383 when = "JUMP_IF_COND_TRUE";
5384 break;
5385 }
5386 smsg("%s%4d %s -> %d", pfx, current, when,
5387 iptr->isn_arg.jump.jump_where);
5388 }
5389 break;
5390
5391 case ISN_JUMP_IF_ARG_SET:
5392 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5393 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5394 iptr->isn_arg.jump.jump_where);
5395 break;
5396
5397 case ISN_FOR:
5398 {
5399 forloop_T *forloop = &iptr->isn_arg.forloop;
5400
5401 smsg("%s%4d FOR $%d -> %d", pfx, current,
5402 forloop->for_idx, forloop->for_end);
5403 }
5404 break;
5405
5406 case ISN_TRY:
5407 {
5408 try_T *try = &iptr->isn_arg.try;
5409
5410 if (try->try_ref->try_finally == 0)
5411 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5412 pfx, current,
5413 try->try_ref->try_catch,
5414 try->try_ref->try_endtry);
5415 else
5416 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5417 pfx, current,
5418 try->try_ref->try_catch,
5419 try->try_ref->try_finally,
5420 try->try_ref->try_endtry);
5421 }
5422 break;
5423 case ISN_CATCH:
5424 // TODO
5425 smsg("%s%4d CATCH", pfx, current);
5426 break;
5427 case ISN_TRYCONT:
5428 {
5429 trycont_T *trycont = &iptr->isn_arg.trycont;
5430
5431 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5432 trycont->tct_levels,
5433 trycont->tct_levels == 1 ? "" : "s",
5434 trycont->tct_where);
5435 }
5436 break;
5437 case ISN_FINALLY:
5438 smsg("%s%4d FINALLY", pfx, current);
5439 break;
5440 case ISN_ENDTRY:
5441 smsg("%s%4d ENDTRY", pfx, current);
5442 break;
5443 case ISN_THROW:
5444 smsg("%s%4d THROW", pfx, current);
5445 break;
5446
5447 // expression operations on number
5448 case ISN_OPNR:
5449 case ISN_OPFLOAT:
5450 case ISN_OPANY:
5451 {
5452 char *what;
5453 char *ins;
5454
5455 switch (iptr->isn_arg.op.op_type)
5456 {
5457 case EXPR_MULT: what = "*"; break;
5458 case EXPR_DIV: what = "/"; break;
5459 case EXPR_REM: what = "%"; break;
5460 case EXPR_SUB: what = "-"; break;
5461 case EXPR_ADD: what = "+"; break;
5462 default: what = "???"; break;
5463 }
5464 switch (iptr->isn_type)
5465 {
5466 case ISN_OPNR: ins = "OPNR"; break;
5467 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5468 case ISN_OPANY: ins = "OPANY"; break;
5469 default: ins = "???"; break;
5470 }
5471 smsg("%s%4d %s %s", pfx, current, ins, what);
5472 }
5473 break;
5474
5475 case ISN_COMPAREBOOL:
5476 case ISN_COMPARESPECIAL:
5477 case ISN_COMPARENR:
5478 case ISN_COMPAREFLOAT:
5479 case ISN_COMPARESTRING:
5480 case ISN_COMPAREBLOB:
5481 case ISN_COMPARELIST:
5482 case ISN_COMPAREDICT:
5483 case ISN_COMPAREFUNC:
5484 case ISN_COMPAREANY:
5485 {
5486 char *p;
5487 char buf[10];
5488 char *type;
5489
5490 switch (iptr->isn_arg.op.op_type)
5491 {
5492 case EXPR_EQUAL: p = "=="; break;
5493 case EXPR_NEQUAL: p = "!="; break;
5494 case EXPR_GREATER: p = ">"; break;
5495 case EXPR_GEQUAL: p = ">="; break;
5496 case EXPR_SMALLER: p = "<"; break;
5497 case EXPR_SEQUAL: p = "<="; break;
5498 case EXPR_MATCH: p = "=~"; break;
5499 case EXPR_IS: p = "is"; break;
5500 case EXPR_ISNOT: p = "isnot"; break;
5501 case EXPR_NOMATCH: p = "!~"; break;
5502 default: p = "???"; break;
5503 }
5504 STRCPY(buf, p);
5505 if (iptr->isn_arg.op.op_ic == TRUE)
5506 strcat(buf, "?");
5507 switch(iptr->isn_type)
5508 {
5509 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5510 case ISN_COMPARESPECIAL:
5511 type = "COMPARESPECIAL"; break;
5512 case ISN_COMPARENR: type = "COMPARENR"; break;
5513 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5514 case ISN_COMPARESTRING:
5515 type = "COMPARESTRING"; break;
5516 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5517 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5518 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5519 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5520 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5521 default: type = "???"; break;
5522 }
5523
5524 smsg("%s%4d %s %s", pfx, current, type, buf);
5525 }
5526 break;
5527
5528 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5529 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5530
5531 // expression operations
5532 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5533 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5534 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5535 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5536 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5537 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5538 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5539 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5540 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5541 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5542 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5543 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005544 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005545 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5546 iptr->isn_arg.getitem.gi_index,
5547 iptr->isn_arg.getitem.gi_with_op ?
5548 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005549 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5550 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5551 iptr->isn_arg.string); break;
5552 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5553
5554 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5555 case ISN_CHECKTYPE:
5556 {
5557 checktype_T *ct = &iptr->isn_arg.type;
5558 char *tofree;
5559
5560 if (ct->ct_arg_idx == 0)
5561 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5562 type_name(ct->ct_type, &tofree),
5563 (int)ct->ct_off);
5564 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005565 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5566 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005567 type_name(ct->ct_type, &tofree),
5568 (int)ct->ct_off,
5569 (int)ct->ct_arg_idx);
5570 vim_free(tofree);
5571 break;
5572 }
5573 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5574 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5575 iptr->isn_arg.checklen.cl_min_len);
5576 break;
5577 case ISN_SETTYPE:
5578 {
5579 char *tofree;
5580
5581 smsg("%s%4d SETTYPE %s", pfx, current,
5582 type_name(iptr->isn_arg.type.ct_type, &tofree));
5583 vim_free(tofree);
5584 break;
5585 }
5586 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005587 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5588 smsg("%s%4d INVERT %d (!val)", pfx, current,
5589 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005590 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005591 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5592 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005593 break;
5594 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005595 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005596 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005597 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5598 pfx, current,
5599 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005600 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005601 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5602 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005603 break;
5604 case ISN_PUT:
5605 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5606 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005607 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005608 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5609 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005610 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005611 else
5612 smsg("%s%4d PUT %c %ld", pfx, current,
5613 iptr->isn_arg.put.put_regname,
5614 (long)iptr->isn_arg.put.put_lnum);
5615 break;
5616
5617 // TODO: summarize modifiers
5618 case ISN_CMDMOD:
5619 {
5620 char_u *buf;
5621 size_t len = produce_cmdmods(
5622 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5623
5624 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005625 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005626 {
5627 (void)produce_cmdmods(
5628 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5629 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5630 vim_free(buf);
5631 }
5632 break;
5633 }
5634 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5635
5636 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005637 smsg("%s%4d PROFILE START line %d", pfx, current,
5638 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005639 break;
5640
5641 case ISN_PROF_END:
5642 smsg("%s%4d PROFILE END", pfx, current);
5643 break;
5644
Bram Moolenaare99d4222021-06-13 14:01:26 +02005645 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005646 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5647 iptr->isn_arg.debug.dbg_break_lnum + 1,
5648 iptr->isn_lnum,
5649 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005650 break;
5651
Bram Moolenaar4c137212021-04-19 16:48:48 +02005652 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5653 iptr->isn_arg.unpack.unp_count,
5654 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5655 break;
5656 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5657 iptr->isn_arg.shuffle.shfl_item,
5658 iptr->isn_arg.shuffle.shfl_up);
5659 break;
5660 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5661
5662 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5663 return;
5664 }
5665
5666 out_flush(); // output one line at a time
5667 ui_breakcheck();
5668 if (got_int)
5669 break;
5670 }
5671}
5672
5673/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005674 * Handle command line completion for the :disassemble command.
5675 */
5676 void
5677set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5678{
5679 char_u *p;
5680
5681 // Default: expand user functions, "debug" and "profile"
5682 xp->xp_context = EXPAND_DISASSEMBLE;
5683 xp->xp_pattern = arg;
5684
5685 // first argument already typed: only user function names
5686 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5687 {
5688 xp->xp_context = EXPAND_USER_FUNC;
5689 xp->xp_pattern = skipwhite(p);
5690 }
5691}
5692
5693/*
5694 * Function given to ExpandGeneric() to obtain the list of :disassemble
5695 * arguments.
5696 */
5697 char_u *
5698get_disassemble_argument(expand_T *xp, int idx)
5699{
5700 if (idx == 0)
5701 return (char_u *)"debug";
5702 if (idx == 1)
5703 return (char_u *)"profile";
5704 return get_user_func_name(xp, idx - 2);
5705}
5706
5707/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005708 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005709 * We don't really need this at runtime, but we do have tests that require it,
5710 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711 */
5712 void
5713ex_disassemble(exarg_T *eap)
5714{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005715 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005716 char_u *fname;
5717 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005718 dfunc_T *dfunc;
5719 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005720 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005721 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005722 compiletype_T compile_type = CT_NONE;
5723
5724 if (STRNCMP(arg, "profile", 7) == 0)
5725 {
5726 compile_type = CT_PROFILE;
5727 arg = skipwhite(arg + 7);
5728 }
5729 else if (STRNCMP(arg, "debug", 5) == 0)
5730 {
5731 compile_type = CT_DEBUG;
5732 arg = skipwhite(arg + 5);
5733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005735 if (STRNCMP(arg, "<lambda>", 8) == 0)
5736 {
5737 arg += 8;
5738 (void)getdigits(&arg);
5739 fname = vim_strnsave(eap->arg, arg - eap->arg);
5740 }
5741 else
5742 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005743 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005744 if (fname == NULL)
5745 {
5746 semsg(_(e_invarg2), eap->arg);
5747 return;
5748 }
5749
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005750 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005751 if (ufunc == NULL)
5752 {
5753 char_u *p = untrans_function_name(fname);
5754
5755 if (p != NULL)
5756 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005757 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005758 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005759 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005760 if (ufunc == NULL)
5761 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005762 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 return;
5764 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005765 if (func_needs_compiling(ufunc, compile_type)
5766 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005767 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005768 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005770 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771 return;
5772 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005773 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005774
5775 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005776 switch (compile_type)
5777 {
5778 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005779#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005780 instr = dfunc->df_instr_prof;
5781 instr_count = dfunc->df_instr_prof_count;
5782 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005783#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005784 // FALLTHROUGH
5785 case CT_NONE:
5786 instr = dfunc->df_instr;
5787 instr_count = dfunc->df_instr_count;
5788 break;
5789 case CT_DEBUG:
5790 instr = dfunc->df_instr_debug;
5791 instr_count = dfunc->df_instr_debug_count;
5792 break;
5793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794
Bram Moolenaar4c137212021-04-19 16:48:48 +02005795 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005796}
5797
5798/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005799 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800 * list, etc. Mostly like what JavaScript does, except that empty list and
5801 * empty dictionary are FALSE.
5802 */
5803 int
5804tv2bool(typval_T *tv)
5805{
5806 switch (tv->v_type)
5807 {
5808 case VAR_NUMBER:
5809 return tv->vval.v_number != 0;
5810 case VAR_FLOAT:
5811#ifdef FEAT_FLOAT
5812 return tv->vval.v_float != 0.0;
5813#else
5814 break;
5815#endif
5816 case VAR_PARTIAL:
5817 return tv->vval.v_partial != NULL;
5818 case VAR_FUNC:
5819 case VAR_STRING:
5820 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5821 case VAR_LIST:
5822 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5823 case VAR_DICT:
5824 return tv->vval.v_dict != NULL
5825 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5826 case VAR_BOOL:
5827 case VAR_SPECIAL:
5828 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5829 case VAR_JOB:
5830#ifdef FEAT_JOB_CHANNEL
5831 return tv->vval.v_job != NULL;
5832#else
5833 break;
5834#endif
5835 case VAR_CHANNEL:
5836#ifdef FEAT_JOB_CHANNEL
5837 return tv->vval.v_channel != NULL;
5838#else
5839 break;
5840#endif
5841 case VAR_BLOB:
5842 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5843 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005844 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005845 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005846 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005847 break;
5848 }
5849 return FALSE;
5850}
5851
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005852 void
5853emsg_using_string_as(typval_T *tv, int as_number)
5854{
5855 semsg(_(as_number ? e_using_string_as_number_str
5856 : e_using_string_as_bool_str),
5857 tv->vval.v_string == NULL
5858 ? (char_u *)"" : tv->vval.v_string);
5859}
5860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861/*
5862 * If "tv" is a string give an error and return FAIL.
5863 */
5864 int
5865check_not_string(typval_T *tv)
5866{
5867 if (tv->v_type == VAR_STRING)
5868 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005869 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870 clear_tv(tv);
5871 return FAIL;
5872 }
5873 return OK;
5874}
5875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876
5877#endif // FEAT_EVAL