blob: 84de4fee5a3e891275d79e06c819cf4d254d3848 [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 Moolenaarf18332f2021-05-07 17:55:55 +02001399// used for v_instr of typval of VAR_INSTR
1400struct instr_S {
1401 ectx_T *instr_ectx;
1402 isn_T *instr_instr;
1403};
1404
Bram Moolenaar4c137212021-04-19 16:48:48 +02001405// used for substitute_instr
1406typedef struct subs_expr_S {
1407 ectx_T *subs_ectx;
1408 isn_T *subs_instr;
1409 int subs_status;
1410} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411
1412// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001413#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414
1415// Get pointer to item at the bottom of the stack, -1 is the bottom.
1416#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001417#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 +01001418
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001419// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001420#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 +01001421
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001422// Set when calling do_debug().
1423static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001424static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001425
1426/*
1427 * When debugging lookup "name" and return the typeval.
1428 * When not found return NULL.
1429 */
1430 typval_T *
1431lookup_debug_var(char_u *name)
1432{
1433 int idx;
1434 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001435 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001436 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001437 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001438
1439 if (ectx == NULL)
1440 return NULL;
1441 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1442
1443 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001444 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001445 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001446 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001447 return STACK_TV_VAR(idx);
1448 }
1449
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001450 // Go through argument names.
1451 ufunc = dfunc->df_ufunc;
1452 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1453 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1454 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1455 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1456 - varargs_off + idx);
1457 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1458 return STACK_TV(ectx->ec_frame_idx - 1);
1459
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001460 return NULL;
1461}
1462
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001463 static void
1464handle_debug(isn_T *iptr, ectx_T *ectx)
1465{
1466 char_u *line;
1467 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1468 + ectx->ec_dfunc_idx)->df_ufunc;
1469 isn_T *ni;
1470 int end_lnum = iptr->isn_lnum;
1471 garray_T ga;
1472 int lnum;
1473
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001474 if (ex_nesting_level > debug_break_level)
1475 {
1476 linenr_T breakpoint;
1477
1478 if (!ufunc->uf_has_breakpoint)
1479 return;
1480
1481 // check for the next breakpoint if needed
1482 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001483 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001484 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1485 return;
1486 }
1487
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001488 SOURCING_LNUM = iptr->isn_lnum;
1489 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001490 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001491
1492 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1493 if (ni->isn_type == ISN_DEBUG
1494 || ni->isn_type == ISN_RETURN
1495 || ni->isn_type == ISN_RETURN_VOID)
1496 {
1497 end_lnum = ni->isn_lnum;
1498 break;
1499 }
1500
1501 if (end_lnum > iptr->isn_lnum)
1502 {
1503 ga_init2(&ga, sizeof(char_u *), 10);
1504 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001505 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001506 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001507
Bram Moolenaar303215d2021-07-07 20:10:43 +02001508 if (p == NULL)
1509 continue; // left over from continuation line
1510 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001511 if (*p == '#')
1512 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001513 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001514 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1515 if (STRNCMP(p, "def ", 4) == 0)
1516 break;
1517 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001518 line = ga_concat_strings(&ga, " ");
1519 vim_free(ga.ga_data);
1520 }
1521 else
1522 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001523
Dominique Pelle6e969552021-06-17 13:53:41 +02001524 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001525 debug_context = NULL;
1526
1527 if (end_lnum > iptr->isn_lnum)
1528 vim_free(line);
1529}
1530
Bram Moolenaar4c137212021-04-19 16:48:48 +02001531/*
1532 * Execute instructions in execution context "ectx".
1533 * Return OK or FAIL;
1534 */
1535 static int
1536exec_instructions(ectx_T *ectx)
1537{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001538 int ret = FAIL;
1539 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001540
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001541 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001542 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001543
Bram Moolenaarff652882021-05-16 15:24:49 +02001544 // Only catch exceptions in this instruction list.
1545 ectx->ec_trylevel_at_start = trylevel;
1546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 for (;;)
1548 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001549 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001551 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001552
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001553 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001554 {
1555 line_breakcheck();
1556 breakcheck_count = 0;
1557 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001558 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001559 {
1560 // Turn CTRL-C into an exception.
1561 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001562 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001563 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001564 did_throw = TRUE;
1565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001567 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001568 {
1569 // Turn an error message into an exception.
1570 did_emsg = FALSE;
1571 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001572 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001573 did_throw = TRUE;
1574 *msg_list = NULL;
1575 }
1576
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001577 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001579 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001580 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001581 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582
1583 // An exception jumps to the first catch, finally, or returns from
1584 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001585 while (index > 0)
1586 {
1587 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001588 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001589 break;
1590 // In the catch and finally block of this try we have to go up
1591 // one level.
1592 --index;
1593 trycmd = NULL;
1594 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001595 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001597 if (trycmd->tcd_in_catch)
1598 {
1599 // exception inside ":catch", jump to ":finally" once
1600 ectx->ec_iidx = trycmd->tcd_finally_idx;
1601 trycmd->tcd_finally_idx = 0;
1602 }
1603 else
1604 // jump to first ":catch"
1605 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001606 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001607 did_throw = FALSE; // don't come back here until :endtry
1608 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 }
1610 else
1611 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001612 // Not inside try or need to return from current functions.
1613 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001614 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001615 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001616 tv = STACK_TV_BOT(0);
1617 tv->v_type = VAR_NUMBER;
1618 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001619 ++ectx->ec_stack.ga_len;
1620 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001622 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001623 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001624 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001625 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 goto done;
1627 }
1628
Bram Moolenaar4c137212021-04-19 16:48:48 +02001629 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001630 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 }
1632 continue;
1633 }
1634
Bram Moolenaar4c137212021-04-19 16:48:48 +02001635 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 switch (iptr->isn_type)
1637 {
1638 // execute Ex command line
1639 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001640 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001641 source_cookie_T cookie;
1642
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001643 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001644 // Pass getsourceline to get an error for a missing ":end"
1645 // command.
1646 CLEAR_FIELD(cookie);
1647 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1648 if (do_cmdline(iptr->isn_arg.string,
1649 getsourceline, &cookie,
1650 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1651 == FAIL
1652 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001653 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 break;
1656
Bram Moolenaar20677332021-06-06 17:02:53 +02001657 // execute Ex command line split at NL characters.
1658 case ISN_EXEC_SPLIT:
1659 {
1660 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001661 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001662
1663 SOURCING_LNUM = iptr->isn_lnum;
1664 CLEAR_FIELD(cookie);
1665 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1666 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001667 line = get_split_sourceline(0, &cookie, 0, 0);
1668 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001669 get_split_sourceline, &cookie,
1670 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1671 == FAIL
1672 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001673 {
1674 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001675 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001676 }
1677 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001678 }
1679 break;
1680
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001681 // Evaluate an expression with legacy syntax, push it onto the
1682 // stack.
1683 case ISN_LEGACY_EVAL:
1684 {
1685 char_u *arg = iptr->isn_arg.string;
1686 int res;
1687 int save_flags = cmdmod.cmod_flags;
1688
Bram Moolenaar35578162021-08-02 19:10:38 +02001689 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001690 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001691 tv = STACK_TV_BOT(0);
1692 init_tv(tv);
1693 cmdmod.cmod_flags |= CMOD_LEGACY;
1694 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1695 cmdmod.cmod_flags = save_flags;
1696 if (res == FAIL)
1697 goto on_error;
1698 ++ectx->ec_stack.ga_len;
1699 }
1700 break;
1701
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001702 // push typeval VAR_INSTR with instructions to be executed
1703 case ISN_INSTR:
1704 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001705 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001706 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001707 tv = STACK_TV_BOT(0);
1708 tv->vval.v_instr = ALLOC_ONE(instr_T);
1709 if (tv->vval.v_instr == NULL)
1710 goto on_error;
1711 ++ectx->ec_stack.ga_len;
1712
1713 tv->v_type = VAR_INSTR;
1714 tv->vval.v_instr->instr_ectx = ectx;
1715 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1716 }
1717 break;
1718
Bram Moolenaar4c137212021-04-19 16:48:48 +02001719 // execute :substitute with an expression
1720 case ISN_SUBSTITUTE:
1721 {
1722 subs_T *subs = &iptr->isn_arg.subs;
1723 source_cookie_T cookie;
1724 struct subs_expr_S *save_instr = substitute_instr;
1725 struct subs_expr_S subs_instr;
1726 int res;
1727
1728 subs_instr.subs_ectx = ectx;
1729 subs_instr.subs_instr = subs->subs_instr;
1730 subs_instr.subs_status = OK;
1731 substitute_instr = &subs_instr;
1732
1733 SOURCING_LNUM = iptr->isn_lnum;
1734 // This is very much like ISN_EXEC
1735 CLEAR_FIELD(cookie);
1736 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1737 res = do_cmdline(subs->subs_cmd,
1738 getsourceline, &cookie,
1739 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1740 substitute_instr = save_instr;
1741
1742 if (res == FAIL || did_emsg
1743 || subs_instr.subs_status == FAIL)
1744 goto on_error;
1745 }
1746 break;
1747
1748 case ISN_FINISH:
1749 goto done;
1750
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001751 case ISN_REDIRSTART:
1752 // create a dummy entry for var_redir_str()
1753 if (alloc_redir_lval() == FAIL)
1754 goto on_error;
1755
1756 // The output is stored in growarray "redir_ga" until
1757 // redirection ends.
1758 init_redir_ga();
1759 redir_vname = 1;
1760 break;
1761
1762 case ISN_REDIREND:
1763 {
1764 char_u *res = get_clear_redir_ga();
1765
1766 // End redirection, put redirected text on the stack.
1767 clear_redir_lval();
1768 redir_vname = 0;
1769
Bram Moolenaar35578162021-08-02 19:10:38 +02001770 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001771 {
1772 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001773 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001774 }
1775 tv = STACK_TV_BOT(0);
1776 tv->v_type = VAR_STRING;
1777 tv->vval.v_string = res;
1778 ++ectx->ec_stack.ga_len;
1779 }
1780 break;
1781
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001782 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001783#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001784 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1785 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001786#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001787 break;
1788
1789 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001790#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001791 {
1792 exarg_T ea;
1793 int res;
1794
1795 CLEAR_FIELD(ea);
1796 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1797 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1798 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1799 --ectx->ec_stack.ga_len;
1800 tv = STACK_TV_BOT(0);
1801 res = cexpr_core(&ea, tv);
1802 clear_tv(tv);
1803 if (res == FAIL)
1804 goto on_error;
1805 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001806#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001807 break;
1808
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001809 // execute Ex command from pieces on the stack
1810 case ISN_EXECCONCAT:
1811 {
1812 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001813 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001814 int pass;
1815 int i;
1816 char_u *cmd = NULL;
1817 char_u *str;
1818
1819 for (pass = 1; pass <= 2; ++pass)
1820 {
1821 for (i = 0; i < count; ++i)
1822 {
1823 tv = STACK_TV_BOT(i - count);
1824 str = tv->vval.v_string;
1825 if (str != NULL && *str != NUL)
1826 {
1827 if (pass == 2)
1828 STRCPY(cmd + len, str);
1829 len += STRLEN(str);
1830 }
1831 if (pass == 2)
1832 clear_tv(tv);
1833 }
1834 if (pass == 1)
1835 {
1836 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001837 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001838 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001839 len = 0;
1840 }
1841 }
1842
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001843 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001844 do_cmdline_cmd(cmd);
1845 vim_free(cmd);
1846 }
1847 break;
1848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 // execute :echo {string} ...
1850 case ISN_ECHO:
1851 {
1852 int count = iptr->isn_arg.echo.echo_count;
1853 int atstart = TRUE;
1854 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001855 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856
1857 for (idx = 0; idx < count; ++idx)
1858 {
1859 tv = STACK_TV_BOT(idx - count);
1860 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1861 &atstart, &needclr);
1862 clear_tv(tv);
1863 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001864 if (needclr)
1865 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001866 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 }
1868 break;
1869
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001870 // :execute {string} ...
1871 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02001872 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001873 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001874 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001875 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02001876 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001877 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001878 {
1879 int count = iptr->isn_arg.number;
1880 garray_T ga;
1881 char_u buf[NUMBUFLEN];
1882 char_u *p;
1883 int len;
1884 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001885 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001886
1887 ga_init2(&ga, 1, 80);
1888 for (idx = 0; idx < count; ++idx)
1889 {
1890 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001891 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001892 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001893 if (tv->v_type == VAR_CHANNEL
1894 || tv->v_type == VAR_JOB)
1895 {
1896 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001897 semsg(_(e_using_invalid_value_as_string_str),
1898 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001899 break;
1900 }
1901 else
1902 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001903 }
1904 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001905 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001906
1907 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02001908 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01001909 failed = TRUE;
1910 else
1911 {
1912 if (ga.ga_len > 0)
1913 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1914 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1915 ga.ga_len += len;
1916 }
1917 clear_tv(tv);
1918 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001919 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001920 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001921 {
1922 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001923 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001924 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001925
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001926 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001927 {
1928 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001929 {
1930 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001931 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001932 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001933 {
1934 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001935 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001936 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001937 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001938 else
1939 {
1940 msg_sb_eol();
1941 if (iptr->isn_type == ISN_ECHOMSG)
1942 {
1943 msg_attr(ga.ga_data, echo_attr);
1944 out_flush();
1945 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02001946 else if (iptr->isn_type == ISN_ECHOCONSOLE)
1947 {
1948 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
1949 TRUE);
1950 ui_write((char_u *)"\r\n", 2, TRUE);
1951 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001952 else
1953 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001954 SOURCING_LNUM = iptr->isn_lnum;
1955 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001956 }
1957 }
1958 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001959 ga_clear(&ga);
1960 }
1961 break;
1962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 // load local variable or argument
1964 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02001965 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001966 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001968 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 break;
1970
1971 // load v: variable
1972 case ISN_LOADV:
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(get_vim_var_tv(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
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001979 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 case ISN_LOADSCRIPT:
1981 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001982 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 svar_T *sv;
1984
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001985 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001986 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001987 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001988 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02001989 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001990 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001992 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 }
1994 break;
1995
1996 // load s: variable in old script
1997 case ISN_LOADS:
1998 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001999 hashtab_T *ht = &SCRIPT_VARS(
2000 iptr->isn_arg.loadstore.ls_sid);
2001 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 if (di == NULL)
2005 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002006 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002007 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002008 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 }
2010 else
2011 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002012 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002013 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002015 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 }
2017 }
2018 break;
2019
Bram Moolenaard3aac292020-04-19 14:32:17 +02002020 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002022 case ISN_LOADB:
2023 case ISN_LOADW:
2024 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002026 dictitem_T *di = NULL;
2027 hashtab_T *ht = NULL;
2028 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002029
Bram Moolenaard3aac292020-04-19 14:32:17 +02002030 switch (iptr->isn_type)
2031 {
2032 case ISN_LOADG:
2033 ht = get_globvar_ht();
2034 namespace = 'g';
2035 break;
2036 case ISN_LOADB:
2037 ht = &curbuf->b_vars->dv_hashtab;
2038 namespace = 'b';
2039 break;
2040 case ISN_LOADW:
2041 ht = &curwin->w_vars->dv_hashtab;
2042 namespace = 'w';
2043 break;
2044 case ISN_LOADT:
2045 ht = &curtab->tp_vars->dv_hashtab;
2046 namespace = 't';
2047 break;
2048 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002049 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002050 }
2051 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 if (di == NULL)
2054 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002055 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002056 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002057 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002058 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 }
2060 else
2061 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002062 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002063 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002065 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 }
2067 }
2068 break;
2069
Bram Moolenaar03290b82020-12-19 16:30:44 +01002070 // load autoload variable
2071 case ISN_LOADAUTO:
2072 {
2073 char_u *name = iptr->isn_arg.string;
2074
Bram Moolenaar35578162021-08-02 19:10:38 +02002075 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002076 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002077 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002078 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002079 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002080 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002081 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002082 }
2083 break;
2084
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002085 // load g:/b:/w:/t: namespace
2086 case ISN_LOADGDICT:
2087 case ISN_LOADBDICT:
2088 case ISN_LOADWDICT:
2089 case ISN_LOADTDICT:
2090 {
2091 dict_T *d = NULL;
2092
2093 switch (iptr->isn_type)
2094 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002095 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2096 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2097 case ISN_LOADWDICT: d = curwin->w_vars; break;
2098 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002099 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002100 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002101 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002102 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002103 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002104 tv = STACK_TV_BOT(0);
2105 tv->v_type = VAR_DICT;
2106 tv->v_lock = 0;
2107 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002108 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002109 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002110 }
2111 break;
2112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 // load &option
2114 case ISN_LOADOPT:
2115 {
2116 typval_T optval;
2117 char_u *name = iptr->isn_arg.string;
2118
Bram Moolenaara8c17702020-04-01 21:17:24 +02002119 // This is not expected to fail, name is checked during
2120 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002121 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002122 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002123 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002124 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002126 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 }
2128 break;
2129
2130 // load $ENV
2131 case ISN_LOADENV:
2132 {
2133 typval_T optval;
2134 char_u *name = iptr->isn_arg.string;
2135
Bram Moolenaar35578162021-08-02 19:10:38 +02002136 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002137 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002138 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002139 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002141 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 }
2143 break;
2144
2145 // load @register
2146 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002147 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002148 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 tv = STACK_TV_BOT(0);
2150 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002151 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002152 // This may result in NULL, which should be equivalent to an
2153 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 tv->vval.v_string = get_reg_contents(
2155 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002156 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 break;
2158
2159 // store local variable
2160 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002161 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 tv = STACK_TV_VAR(iptr->isn_arg.number);
2163 clear_tv(tv);
2164 *tv = *STACK_TV_BOT(0);
2165 break;
2166
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002167 // store s: variable in old script
2168 case ISN_STORES:
2169 {
2170 hashtab_T *ht = &SCRIPT_VARS(
2171 iptr->isn_arg.loadstore.ls_sid);
2172 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002173 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002174
Bram Moolenaar4c137212021-04-19 16:48:48 +02002175 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002176 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002177 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002178 else
2179 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002180 SOURCING_LNUM = iptr->isn_lnum;
2181 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002182 {
2183 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002184 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002185 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002186 clear_tv(&di->di_tv);
2187 di->di_tv = *STACK_TV_BOT(0);
2188 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002189 }
2190 break;
2191
2192 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 case ISN_STORESCRIPT:
2194 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002195 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2196 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002198 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002199 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002200 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002201 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002202
2203 // "const" and "final" are checked at compile time, locking
2204 // the value needs to be checked here.
2205 SOURCING_LNUM = iptr->isn_lnum;
2206 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002207 {
2208 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002209 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002210 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 clear_tv(sv->sv_tv);
2213 *sv->sv_tv = *STACK_TV_BOT(0);
2214 }
2215 break;
2216
2217 // store option
2218 case ISN_STOREOPT:
2219 {
2220 long n = 0;
2221 char_u *s = NULL;
2222 char *msg;
2223
Bram Moolenaar4c137212021-04-19 16:48:48 +02002224 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 tv = STACK_TV_BOT(0);
2226 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002227 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002229 if (s == NULL)
2230 s = (char_u *)"";
2231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002233 // must be VAR_NUMBER, CHECKTYPE makes sure
2234 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2236 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002237 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 if (msg != NULL)
2239 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002240 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002242 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 }
2245 break;
2246
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002247 // store $ENV
2248 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002249 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002250 tv = STACK_TV_BOT(0);
2251 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2252 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002253 break;
2254
2255 // store @r
2256 case ISN_STOREREG:
2257 {
2258 int reg = iptr->isn_arg.number;
2259
Bram Moolenaar4c137212021-04-19 16:48:48 +02002260 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002261 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002262 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002263 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002264 }
2265 break;
2266
2267 // store v: variable
2268 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002269 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002270 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2271 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002272 // should not happen, type is checked when compiling
2273 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002274 break;
2275
Bram Moolenaard3aac292020-04-19 14:32:17 +02002276 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002278 case ISN_STOREB:
2279 case ISN_STOREW:
2280 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002282 dictitem_T *di;
2283 hashtab_T *ht;
2284 char_u *name = iptr->isn_arg.string + 2;
2285
Bram Moolenaard3aac292020-04-19 14:32:17 +02002286 switch (iptr->isn_type)
2287 {
2288 case ISN_STOREG:
2289 ht = get_globvar_ht();
2290 break;
2291 case ISN_STOREB:
2292 ht = &curbuf->b_vars->dv_hashtab;
2293 break;
2294 case ISN_STOREW:
2295 ht = &curwin->w_vars->dv_hashtab;
2296 break;
2297 case ISN_STORET:
2298 ht = &curtab->tp_vars->dv_hashtab;
2299 break;
2300 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002301 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303
Bram Moolenaar4c137212021-04-19 16:48:48 +02002304 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002305 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002307 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 else
2309 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002310 SOURCING_LNUM = iptr->isn_lnum;
2311 if (var_check_permission(di, name) == FAIL)
2312 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 clear_tv(&di->di_tv);
2314 di->di_tv = *STACK_TV_BOT(0);
2315 }
2316 }
2317 break;
2318
Bram Moolenaar03290b82020-12-19 16:30:44 +01002319 // store an autoload variable
2320 case ISN_STOREAUTO:
2321 SOURCING_LNUM = iptr->isn_lnum;
2322 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2323 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002324 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002325 break;
2326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 // store number in local variable
2328 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002329 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 clear_tv(tv);
2331 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002332 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 break;
2334
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002335 // store value in list or dict variable
2336 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002337 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002338 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002339 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002340 typval_T *tv_dest = STACK_TV_BOT(-1);
2341 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002342
Bram Moolenaar752fc692021-01-04 21:57:11 +01002343 // Stack contains:
2344 // -3 value to be stored
2345 // -2 index
2346 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002347 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002348 SOURCING_LNUM = iptr->isn_lnum;
2349 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002350 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002351 dest_type = tv_dest->v_type;
2352 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002353 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002354 else if (dest_type == VAR_LIST
2355 && tv_idx->v_type != VAR_NUMBER)
2356 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002357 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002358 status = FAIL;
2359 }
2360 }
2361 else if (dest_type != tv_dest->v_type)
2362 {
2363 // just in case, should be OK
2364 semsg(_(e_expected_str_but_got_str),
2365 vartype_name(dest_type),
2366 vartype_name(tv_dest->v_type));
2367 status = FAIL;
2368 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002369
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002370 if (status == OK && dest_type == VAR_LIST)
2371 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002372 long lidx = (long)tv_idx->vval.v_number;
2373 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002374
2375 if (list == NULL)
2376 {
2377 emsg(_(e_list_not_set));
2378 goto on_error;
2379 }
2380 if (lidx < 0 && list->lv_len + lidx >= 0)
2381 // negative index is relative to the end
2382 lidx = list->lv_len + lidx;
2383 if (lidx < 0 || lidx > list->lv_len)
2384 {
2385 semsg(_(e_listidx), lidx);
2386 goto on_error;
2387 }
2388 if (lidx < list->lv_len)
2389 {
2390 listitem_T *li = list_find(list, lidx);
2391
2392 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002393 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002394 goto on_error;
2395 // overwrite existing list item
2396 clear_tv(&li->li_tv);
2397 li->li_tv = *tv;
2398 }
2399 else
2400 {
2401 if (error_if_locked(list->lv_lock,
2402 e_cannot_change_list))
2403 goto on_error;
2404 // append to list, only fails when out of memory
2405 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002406 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002407 clear_tv(tv);
2408 }
2409 }
2410 else if (status == OK && dest_type == VAR_DICT)
2411 {
2412 char_u *key = tv_idx->vval.v_string;
2413 dict_T *dict = tv_dest->vval.v_dict;
2414 dictitem_T *di;
2415
2416 SOURCING_LNUM = iptr->isn_lnum;
2417 if (dict == NULL)
2418 {
2419 emsg(_(e_dictionary_not_set));
2420 goto on_error;
2421 }
2422 if (key == NULL)
2423 key = (char_u *)"";
2424 di = dict_find(dict, key, -1);
2425 if (di != NULL)
2426 {
2427 if (error_if_locked(di->di_tv.v_lock,
2428 e_cannot_change_dict_item))
2429 goto on_error;
2430 // overwrite existing value
2431 clear_tv(&di->di_tv);
2432 di->di_tv = *tv;
2433 }
2434 else
2435 {
2436 if (error_if_locked(dict->dv_lock,
2437 e_cannot_change_dict))
2438 goto on_error;
2439 // add to dict, only fails when out of memory
2440 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002441 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002442 clear_tv(tv);
2443 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002444 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002445 else if (status == OK && dest_type == VAR_BLOB)
2446 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002447 long lidx = (long)tv_idx->vval.v_number;
2448 blob_T *blob = tv_dest->vval.v_blob;
2449 varnumber_T nr;
2450 int error = FALSE;
2451 int len;
2452
2453 if (blob == NULL)
2454 {
2455 emsg(_(e_blob_not_set));
2456 goto on_error;
2457 }
2458 len = blob_len(blob);
2459 if (lidx < 0 && len + lidx >= 0)
2460 // negative index is relative to the end
2461 lidx = len + lidx;
2462
2463 // Can add one byte at the end.
2464 if (lidx < 0 || lidx > len)
2465 {
2466 semsg(_(e_blobidx), lidx);
2467 goto on_error;
2468 }
2469 if (value_check_lock(blob->bv_lock,
2470 (char_u *)"blob", FALSE))
2471 goto on_error;
2472 nr = tv_get_number_chk(tv, &error);
2473 if (error)
2474 goto on_error;
2475 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002476 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002477 else
2478 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002479 status = FAIL;
2480 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002481 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002482
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002483 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002484 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002485 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002486 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002487 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002488 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002489 goto on_error;
2490 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002491 }
2492 break;
2493
Bram Moolenaar68452172021-04-12 21:21:02 +02002494 // store value in blob range
2495 case ISN_STORERANGE:
2496 {
2497 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2498 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2499 typval_T *tv_dest = STACK_TV_BOT(-1);
2500 int status = OK;
2501
2502 // Stack contains:
2503 // -4 value to be stored
2504 // -3 first index or "none"
2505 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002506 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002507 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002508 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002509 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002510 long n1;
2511 long n2;
2512 int error = FALSE;
2513
2514 SOURCING_LNUM = iptr->isn_lnum;
2515 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2516 if (error)
2517 status = FAIL;
2518 else
2519 {
2520 if (tv_idx2->v_type == VAR_SPECIAL
2521 && tv_idx2->vval.v_number == VVAL_NONE)
2522 n2 = list_len(tv_dest->vval.v_list) - 1;
2523 else
2524 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2525 if (error)
2526 status = FAIL;
2527 else
2528 {
2529 listitem_T *li1 = check_range_index_one(
2530 tv_dest->vval.v_list, &n1, FALSE);
2531
2532 if (li1 == NULL)
2533 status = FAIL;
2534 else
2535 {
2536 status = check_range_index_two(
2537 tv_dest->vval.v_list,
2538 &n1, li1, &n2, FALSE);
2539 if (status != FAIL)
2540 status = list_assign_range(
2541 tv_dest->vval.v_list,
2542 tv->vval.v_list,
2543 n1,
2544 n2,
2545 tv_idx2->v_type == VAR_SPECIAL,
2546 (char_u *)"=",
2547 (char_u *)"[unknown]");
2548 }
2549 }
2550 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002551 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002552 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002553 {
2554 varnumber_T n1;
2555 varnumber_T n2;
2556 int error = FALSE;
2557
2558 n1 = tv_get_number_chk(tv_idx1, &error);
2559 if (error)
2560 status = FAIL;
2561 else
2562 {
2563 if (tv_idx2->v_type == VAR_SPECIAL
2564 && tv_idx2->vval.v_number == VVAL_NONE)
2565 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2566 else
2567 n2 = tv_get_number_chk(tv_idx2, &error);
2568 if (error)
2569 status = FAIL;
2570 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002571 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002572 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002573
2574 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002575 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002576 || check_blob_range(bloblen,
2577 n1, n2, FALSE) == FAIL)
2578 status = FAIL;
2579 else
2580 status = blob_set_range(
2581 tv_dest->vval.v_blob, n1, n2, tv);
2582 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002583 }
2584 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002585 else
2586 {
2587 status = FAIL;
2588 emsg(_(e_blob_required));
2589 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002590
2591 clear_tv(tv_idx1);
2592 clear_tv(tv_idx2);
2593 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002594 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002595 clear_tv(tv);
2596
2597 if (status == FAIL)
2598 goto on_error;
2599 }
2600 break;
2601
Bram Moolenaar0186e582021-01-10 18:33:11 +01002602 // load or store variable or argument from outer scope
2603 case ISN_LOADOUTER:
2604 case ISN_STOREOUTER:
2605 {
2606 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002607 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2608 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002609
2610 while (depth > 1 && outer != NULL)
2611 {
2612 outer = outer->out_up;
2613 --depth;
2614 }
2615 if (outer == NULL)
2616 {
2617 SOURCING_LNUM = iptr->isn_lnum;
2618 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002619 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002620 }
2621 tv = ((typval_T *)outer->out_stack->ga_data)
2622 + outer->out_frame_idx + STACK_FRAME_SIZE
2623 + iptr->isn_arg.outer.outer_idx;
2624 if (iptr->isn_type == ISN_LOADOUTER)
2625 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002626 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002627 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002628 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002629 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002630 }
2631 else
2632 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002633 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002634 clear_tv(tv);
2635 *tv = *STACK_TV_BOT(0);
2636 }
2637 }
2638 break;
2639
Bram Moolenaar752fc692021-01-04 21:57:11 +01002640 // unlet item in list or dict variable
2641 case ISN_UNLETINDEX:
2642 {
2643 typval_T *tv_idx = STACK_TV_BOT(-2);
2644 typval_T *tv_dest = STACK_TV_BOT(-1);
2645 int status = OK;
2646
2647 // Stack contains:
2648 // -2 index
2649 // -1 dict or list
2650 if (tv_dest->v_type == VAR_DICT)
2651 {
2652 // unlet a dict item, index must be a string
2653 if (tv_idx->v_type != VAR_STRING)
2654 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002655 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002656 semsg(_(e_expected_str_but_got_str),
2657 vartype_name(VAR_STRING),
2658 vartype_name(tv_idx->v_type));
2659 status = FAIL;
2660 }
2661 else
2662 {
2663 dict_T *d = tv_dest->vval.v_dict;
2664 char_u *key = tv_idx->vval.v_string;
2665 dictitem_T *di = NULL;
2666
2667 if (key == NULL)
2668 key = (char_u *)"";
2669 if (d != NULL)
2670 di = dict_find(d, key, (int)STRLEN(key));
2671 if (di == NULL)
2672 {
2673 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002674 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002675 semsg(_(e_dictkey), key);
2676 status = FAIL;
2677 }
2678 else
2679 {
2680 // TODO: check for dict or item locked
2681 dictitem_remove(d, di);
2682 }
2683 }
2684 }
2685 else if (tv_dest->v_type == VAR_LIST)
2686 {
2687 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002688 SOURCING_LNUM = iptr->isn_lnum;
2689 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002690 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002691 status = FAIL;
2692 }
2693 else
2694 {
2695 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002696 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002697 listitem_T *li = NULL;
2698
2699 li = list_find(l, n);
2700 if (li == NULL)
2701 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002702 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002703 semsg(_(e_listidx), n);
2704 status = FAIL;
2705 }
2706 else
2707 // TODO: check for list or item locked
2708 listitem_remove(l, li);
2709 }
2710 }
2711 else
2712 {
2713 status = FAIL;
2714 semsg(_(e_cannot_index_str),
2715 vartype_name(tv_dest->v_type));
2716 }
2717
2718 clear_tv(tv_idx);
2719 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002720 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002721 if (status == FAIL)
2722 goto on_error;
2723 }
2724 break;
2725
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002726 // unlet range of items in list variable
2727 case ISN_UNLETRANGE:
2728 {
2729 // Stack contains:
2730 // -3 index1
2731 // -2 index2
2732 // -1 dict or list
2733 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2734 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2735 typval_T *tv_dest = STACK_TV_BOT(-1);
2736 int status = OK;
2737
2738 if (tv_dest->v_type == VAR_LIST)
2739 {
2740 // indexes must be a number
2741 SOURCING_LNUM = iptr->isn_lnum;
2742 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002743 || (tv_idx2->v_type != VAR_SPECIAL
2744 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002745 {
2746 status = FAIL;
2747 }
2748 else
2749 {
2750 list_T *l = tv_dest->vval.v_list;
2751 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002752 long n2 = tv_idx2->v_type == VAR_SPECIAL
2753 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002754 listitem_T *li;
2755
2756 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002757 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002758 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002759 else
2760 {
2761 if (n1 < 0)
2762 n1 = list_idx_of_item(l, li);
2763 if (n2 < 0)
2764 {
2765 listitem_T *li2 = list_find(l, n2);
2766
2767 if (li2 == NULL)
2768 status = FAIL;
2769 else
2770 n2 = list_idx_of_item(l, li2);
2771 }
2772 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002773 && tv_idx2->v_type != VAR_SPECIAL
2774 && n2 < n1)
2775 {
2776 semsg(_(e_listidx), n2);
2777 status = FAIL;
2778 }
2779 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002780 && list_unlet_range(l, li, NULL, n1,
2781 tv_idx2->v_type != VAR_SPECIAL, n2)
2782 == FAIL)
2783 status = FAIL;
2784 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002785 }
2786 }
2787 else
2788 {
2789 status = FAIL;
2790 SOURCING_LNUM = iptr->isn_lnum;
2791 semsg(_(e_cannot_index_str),
2792 vartype_name(tv_dest->v_type));
2793 }
2794
2795 clear_tv(tv_idx1);
2796 clear_tv(tv_idx2);
2797 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002798 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002799 if (status == FAIL)
2800 goto on_error;
2801 }
2802 break;
2803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 // push constant
2805 case ISN_PUSHNR:
2806 case ISN_PUSHBOOL:
2807 case ISN_PUSHSPEC:
2808 case ISN_PUSHF:
2809 case ISN_PUSHS:
2810 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002811 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002812 case ISN_PUSHCHANNEL:
2813 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02002814 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002815 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002817 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002818 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 switch (iptr->isn_type)
2820 {
2821 case ISN_PUSHNR:
2822 tv->v_type = VAR_NUMBER;
2823 tv->vval.v_number = iptr->isn_arg.number;
2824 break;
2825 case ISN_PUSHBOOL:
2826 tv->v_type = VAR_BOOL;
2827 tv->vval.v_number = iptr->isn_arg.number;
2828 break;
2829 case ISN_PUSHSPEC:
2830 tv->v_type = VAR_SPECIAL;
2831 tv->vval.v_number = iptr->isn_arg.number;
2832 break;
2833#ifdef FEAT_FLOAT
2834 case ISN_PUSHF:
2835 tv->v_type = VAR_FLOAT;
2836 tv->vval.v_float = iptr->isn_arg.fnumber;
2837 break;
2838#endif
2839 case ISN_PUSHBLOB:
2840 blob_copy(iptr->isn_arg.blob, tv);
2841 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002842 case ISN_PUSHFUNC:
2843 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002844 if (iptr->isn_arg.string == NULL)
2845 tv->vval.v_string = NULL;
2846 else
2847 tv->vval.v_string =
2848 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002849 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002850 case ISN_PUSHCHANNEL:
2851#ifdef FEAT_JOB_CHANNEL
2852 tv->v_type = VAR_CHANNEL;
2853 tv->vval.v_channel = iptr->isn_arg.channel;
2854 if (tv->vval.v_channel != NULL)
2855 ++tv->vval.v_channel->ch_refcount;
2856#endif
2857 break;
2858 case ISN_PUSHJOB:
2859#ifdef FEAT_JOB_CHANNEL
2860 tv->v_type = VAR_JOB;
2861 tv->vval.v_job = iptr->isn_arg.job;
2862 if (tv->vval.v_job != NULL)
2863 ++tv->vval.v_job->jv_refcount;
2864#endif
2865 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 default:
2867 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002868 tv->vval.v_string = vim_strsave(
2869 iptr->isn_arg.string == NULL
2870 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 }
2872 break;
2873
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002874 case ISN_UNLET:
2875 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2876 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002877 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002878 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002879 case ISN_UNLETENV:
2880 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2881 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002882
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002883 case ISN_LOCKCONST:
2884 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2885 break;
2886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 // create a list from items on the stack; uses a single allocation
2888 // for the list header and the items
2889 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002890 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002891 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 break;
2893
2894 // create a dict from items on the stack
2895 case ISN_NEWDICT:
2896 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002897 int count = iptr->isn_arg.number;
2898 dict_T *dict = dict_alloc();
2899 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002900 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002901 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002903 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002904 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 for (idx = 0; idx < count; ++idx)
2906 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002907 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002909 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002910 key = tv->vval.v_string == NULL
2911 ? (char_u *)"" : tv->vval.v_string;
2912 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002913 if (item != NULL)
2914 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002915 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002916 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002917 dict_unref(dict);
2918 goto on_error;
2919 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002920 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002922 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02002923 {
2924 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002925 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2928 item->di_tv.v_lock = 0;
2929 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002930 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002931 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002932 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002933 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 }
2936
2937 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002938 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02002939 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002940 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002942 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 tv = STACK_TV_BOT(-1);
2944 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002945 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 tv->vval.v_dict = dict;
2947 ++dict->dv_refcount;
2948 }
2949 break;
2950
2951 // call a :def function
2952 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002953 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002954 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2955 NULL,
2956 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002957 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002958 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 break;
2960
2961 // call a builtin function
2962 case ISN_BCALL:
2963 SOURCING_LNUM = iptr->isn_lnum;
2964 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2965 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002966 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002967 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 break;
2969
2970 // call a funcref or partial
2971 case ISN_PCALL:
2972 {
2973 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2974 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002975 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976
2977 SOURCING_LNUM = iptr->isn_lnum;
2978 if (pfunc->cpf_top)
2979 {
2980 // funcref is above the arguments
2981 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2982 }
2983 else
2984 {
2985 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002986 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002987 partial_tv = *STACK_TV_BOT(0);
2988 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002990 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002991 if (tv == &partial_tv)
2992 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002994 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 }
2996 break;
2997
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002998 case ISN_PCALL_END:
2999 // PCALL finished, arguments have been consumed and replaced by
3000 // the return value. Now clear the funcref from the stack,
3001 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003002 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003003 clear_tv(STACK_TV_BOT(-1));
3004 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3005 break;
3006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 // call a user defined function or funcref/partial
3008 case ISN_UCALL:
3009 {
3010 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3011
3012 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003013 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003014 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003015 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 }
3017 break;
3018
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003019 // return from a :def function call without a value
3020 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003021 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003022 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003023 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003024 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003025 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003026 tv->vval.v_number = 0;
3027 tv->v_lock = 0;
3028 // FALLTHROUGH
3029
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003030 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 case ISN_RETURN:
3032 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003033 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003034 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003035
3036 if (trystack->ga_len > 0)
3037 trycmd = ((trycmd_T *)trystack->ga_data)
3038 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003039 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003040 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003042 // jump to ":finally" or ":endtry"
3043 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003044 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003045 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003046 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 trycmd->tcd_return = TRUE;
3048 }
3049 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003050 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 }
3052 break;
3053
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003054 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 case ISN_FUNCREF:
3056 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003057 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
3058 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3059 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003062 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003063 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003064 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003065 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003066 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003067 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01003068 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003069 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003070 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003072 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 tv->vval.v_partial = pt;
3074 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003075 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 }
3077 break;
3078
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003079 // Create a global function from a lambda.
3080 case ISN_NEWFUNC:
3081 {
3082 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3083
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003084 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003085 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003086 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003087 }
3088 break;
3089
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003090 // List functions
3091 case ISN_DEF:
3092 if (iptr->isn_arg.string == NULL)
3093 list_functions(NULL);
3094 else
3095 {
3096 exarg_T ea;
3097
3098 CLEAR_FIELD(ea);
3099 ea.cmd = ea.arg = iptr->isn_arg.string;
3100 define_function(&ea, NULL);
3101 }
3102 break;
3103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 // jump if a condition is met
3105 case ISN_JUMP:
3106 {
3107 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003108 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 int jump = TRUE;
3110
3111 if (when != JUMP_ALWAYS)
3112 {
3113 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003114 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003115 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003116 || when == JUMP_IF_COND_TRUE)
3117 {
3118 SOURCING_LNUM = iptr->isn_lnum;
3119 jump = tv_get_bool_chk(tv, &error);
3120 if (error)
3121 goto on_error;
3122 }
3123 else
3124 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003126 || when == JUMP_AND_KEEP_IF_FALSE
3127 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003129 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 {
3131 // drop the value from the stack
3132 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003133 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 }
3135 }
3136 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003137 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 }
3139 break;
3140
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003141 // Jump if an argument with a default value was already set and not
3142 // v:none.
3143 case ISN_JUMP_IF_ARG_SET:
3144 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3145 if (tv->v_type != VAR_UNKNOWN
3146 && !(tv->v_type == VAR_SPECIAL
3147 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003148 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003149 break;
3150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 // top of a for loop
3152 case ISN_FOR:
3153 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003154 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 typval_T *idxtv =
3156 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3157
Bram Moolenaar35578162021-08-02 19:10:38 +02003158 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003159 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003160 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003161 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003162 list_T *list = ltv->vval.v_list;
3163
3164 // push the next item from the list
3165 ++idxtv->vval.v_number;
3166 if (list == NULL
3167 || idxtv->vval.v_number >= list->lv_len)
3168 {
3169 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003170 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3171 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003172 }
3173 else if (list->lv_first == &range_list_item)
3174 {
3175 // non-materialized range() list
3176 tv = STACK_TV_BOT(0);
3177 tv->v_type = VAR_NUMBER;
3178 tv->v_lock = 0;
3179 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003181 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003182 }
3183 else
3184 {
3185 listitem_T *li = list_find(list,
3186 idxtv->vval.v_number);
3187
3188 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003189 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003190 }
3191 }
3192 else if (ltv->v_type == VAR_STRING)
3193 {
3194 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003195
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003196 // The index is for the last byte of the previous
3197 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003198 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003199 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003200 {
3201 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003202 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3203 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003204 }
3205 else
3206 {
3207 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3208
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003209 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003210 tv = STACK_TV_BOT(0);
3211 tv->v_type = VAR_STRING;
3212 tv->vval.v_string = vim_strnsave(
3213 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003214 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003215 idxtv->vval.v_number += clen - 1;
3216 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003218 else if (ltv->v_type == VAR_BLOB)
3219 {
3220 blob_T *blob = ltv->vval.v_blob;
3221
3222 // When we get here the first time make a copy of the
3223 // blob, so that the iteration still works when it is
3224 // changed.
3225 if (idxtv->vval.v_number == -1 && blob != NULL)
3226 {
3227 blob_copy(blob, ltv);
3228 blob_unref(blob);
3229 blob = ltv->vval.v_blob;
3230 }
3231
3232 // The index is for the previous byte.
3233 ++idxtv->vval.v_number;
3234 if (blob == NULL
3235 || idxtv->vval.v_number >= blob_len(blob))
3236 {
3237 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003238 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3239 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003240 }
3241 else
3242 {
3243 // Push the next byte from the blob.
3244 tv = STACK_TV_BOT(0);
3245 tv->v_type = VAR_NUMBER;
3246 tv->vval.v_number = blob_get(blob,
3247 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003248 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003249 }
3250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 else
3252 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003253 semsg(_(e_for_loop_on_str_not_supported),
3254 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003255 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 }
3257 }
3258 break;
3259
3260 // start of ":try" block
3261 case ISN_TRY:
3262 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003263 trycmd_T *trycmd = NULL;
3264
Bram Moolenaar35578162021-08-02 19:10:38 +02003265 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003266 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003267 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3268 + ectx->ec_trystack.ga_len;
3269 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003271 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003272 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3273 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003274 trycmd->tcd_catch_idx =
3275 iptr->isn_arg.try.try_ref->try_catch;
3276 trycmd->tcd_finally_idx =
3277 iptr->isn_arg.try.try_ref->try_finally;
3278 trycmd->tcd_endtry_idx =
3279 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 }
3281 break;
3282
3283 case ISN_PUSHEXC:
3284 if (current_exception == NULL)
3285 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003286 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003288 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003290 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003291 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003293 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003295 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 tv->vval.v_string = vim_strsave(
3297 (char_u *)current_exception->value);
3298 break;
3299
3300 case ISN_CATCH:
3301 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003302 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303
Bram Moolenaar4c137212021-04-19 16:48:48 +02003304 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 if (trystack->ga_len > 0)
3306 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003307 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 + trystack->ga_len - 1;
3309 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003310 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 }
3312 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003313 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 catch_exception(current_exception);
3315 }
3316 break;
3317
Bram Moolenaarc150c092021-02-13 15:02:46 +01003318 case ISN_TRYCONT:
3319 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003320 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003321 trycont_T *trycont = &iptr->isn_arg.trycont;
3322 int i;
3323 trycmd_T *trycmd;
3324 int iidx = trycont->tct_where;
3325
3326 if (trystack->ga_len < trycont->tct_levels)
3327 {
3328 siemsg("TRYCONT: expected %d levels, found %d",
3329 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003330 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003331 }
3332 // Make :endtry jump to any outer try block and the last
3333 // :endtry inside the loop to the loop start.
3334 for (i = trycont->tct_levels; i > 0; --i)
3335 {
3336 trycmd = ((trycmd_T *)trystack->ga_data)
3337 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003338 // Add one to tcd_cont to be able to jump to
3339 // instruction with index zero.
3340 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003341 iidx = trycmd->tcd_finally_idx == 0
3342 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003343 }
3344 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003345 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003346 }
3347 break;
3348
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003349 case ISN_FINALLY:
3350 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003351 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003352 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3353 + trystack->ga_len - 1;
3354
3355 // Reset the index to avoid a return statement jumps here
3356 // again.
3357 trycmd->tcd_finally_idx = 0;
3358 break;
3359 }
3360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 // end of ":try" block
3362 case ISN_ENDTRY:
3363 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003364 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365
3366 if (trystack->ga_len > 0)
3367 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003368 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 --trystack->ga_len;
3371 --trylevel;
3372 trycmd = ((trycmd_T *)trystack->ga_data)
3373 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003374 if (trycmd->tcd_did_throw)
3375 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003376 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 {
3378 // discard the exception
3379 if (caught_stack == current_exception)
3380 caught_stack = caught_stack->caught;
3381 discard_current_exception();
3382 }
3383
3384 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003385 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003386
Bram Moolenaar4c137212021-04-19 16:48:48 +02003387 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003388 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003389 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003390 clear_tv(STACK_TV_BOT(0));
3391 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003392 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003393 // handling :continue: jump to outer try block or
3394 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003395 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 }
3397 }
3398 break;
3399
3400 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003401 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003402 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003403
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003404 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3405 {
3406 // throwing an exception while using "silent!" causes
3407 // the function to abort but not display an error.
3408 tv = STACK_TV_BOT(-1);
3409 clear_tv(tv);
3410 tv->v_type = VAR_NUMBER;
3411 tv->vval.v_number = 0;
3412 goto done;
3413 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003414 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003415 tv = STACK_TV_BOT(0);
3416 if (tv->vval.v_string == NULL
3417 || *skipwhite(tv->vval.v_string) == NUL)
3418 {
3419 vim_free(tv->vval.v_string);
3420 SOURCING_LNUM = iptr->isn_lnum;
3421 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003422 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003423 }
3424
3425 // Inside a "catch" we need to first discard the caught
3426 // exception.
3427 if (trystack->ga_len > 0)
3428 {
3429 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3430 + trystack->ga_len - 1;
3431 if (trycmd->tcd_caught && current_exception != NULL)
3432 {
3433 // discard the exception
3434 if (caught_stack == current_exception)
3435 caught_stack = caught_stack->caught;
3436 discard_current_exception();
3437 trycmd->tcd_caught = FALSE;
3438 }
3439 }
3440
3441 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3442 == FAIL)
3443 {
3444 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003445 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003446 }
3447 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 break;
3450
3451 // compare with special values
3452 case ISN_COMPAREBOOL:
3453 case ISN_COMPARESPECIAL:
3454 {
3455 typval_T *tv1 = STACK_TV_BOT(-2);
3456 typval_T *tv2 = STACK_TV_BOT(-1);
3457 varnumber_T arg1 = tv1->vval.v_number;
3458 varnumber_T arg2 = tv2->vval.v_number;
3459 int res;
3460
3461 switch (iptr->isn_arg.op.op_type)
3462 {
3463 case EXPR_EQUAL: res = arg1 == arg2; break;
3464 case EXPR_NEQUAL: res = arg1 != arg2; break;
3465 default: res = 0; break;
3466 }
3467
Bram Moolenaar4c137212021-04-19 16:48:48 +02003468 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 tv1->v_type = VAR_BOOL;
3470 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3471 }
3472 break;
3473
3474 // Operation with two number arguments
3475 case ISN_OPNR:
3476 case ISN_COMPARENR:
3477 {
3478 typval_T *tv1 = STACK_TV_BOT(-2);
3479 typval_T *tv2 = STACK_TV_BOT(-1);
3480 varnumber_T arg1 = tv1->vval.v_number;
3481 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003482 varnumber_T res = 0;
3483 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484
3485 switch (iptr->isn_arg.op.op_type)
3486 {
3487 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003488 case EXPR_DIV: if (arg2 == 0)
3489 div_zero = TRUE;
3490 else
3491 res = arg1 / arg2;
3492 break;
3493 case EXPR_REM: if (arg2 == 0)
3494 div_zero = TRUE;
3495 else
3496 res = arg1 % arg2;
3497 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 case EXPR_SUB: res = arg1 - arg2; break;
3499 case EXPR_ADD: res = arg1 + arg2; break;
3500
3501 case EXPR_EQUAL: res = arg1 == arg2; break;
3502 case EXPR_NEQUAL: res = arg1 != arg2; break;
3503 case EXPR_GREATER: res = arg1 > arg2; break;
3504 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3505 case EXPR_SMALLER: res = arg1 < arg2; break;
3506 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003507 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 }
3509
Bram Moolenaar4c137212021-04-19 16:48:48 +02003510 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 if (iptr->isn_type == ISN_COMPARENR)
3512 {
3513 tv1->v_type = VAR_BOOL;
3514 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3515 }
3516 else
3517 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003518 if (div_zero)
3519 {
3520 SOURCING_LNUM = iptr->isn_lnum;
3521 emsg(_(e_divide_by_zero));
3522 goto on_error;
3523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 }
3525 break;
3526
3527 // Computation with two float arguments
3528 case ISN_OPFLOAT:
3529 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003530#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 {
3532 typval_T *tv1 = STACK_TV_BOT(-2);
3533 typval_T *tv2 = STACK_TV_BOT(-1);
3534 float_T arg1 = tv1->vval.v_float;
3535 float_T arg2 = tv2->vval.v_float;
3536 float_T res = 0;
3537 int cmp = FALSE;
3538
3539 switch (iptr->isn_arg.op.op_type)
3540 {
3541 case EXPR_MULT: res = arg1 * arg2; break;
3542 case EXPR_DIV: res = arg1 / arg2; break;
3543 case EXPR_SUB: res = arg1 - arg2; break;
3544 case EXPR_ADD: res = arg1 + arg2; break;
3545
3546 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3547 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3548 case EXPR_GREATER: cmp = arg1 > arg2; break;
3549 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3550 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3551 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3552 default: cmp = 0; break;
3553 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003554 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 if (iptr->isn_type == ISN_COMPAREFLOAT)
3556 {
3557 tv1->v_type = VAR_BOOL;
3558 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3559 }
3560 else
3561 tv1->vval.v_float = res;
3562 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003563#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 break;
3565
3566 case ISN_COMPARELIST:
3567 {
3568 typval_T *tv1 = STACK_TV_BOT(-2);
3569 typval_T *tv2 = STACK_TV_BOT(-1);
3570 list_T *arg1 = tv1->vval.v_list;
3571 list_T *arg2 = tv2->vval.v_list;
3572 int cmp = FALSE;
3573 int ic = iptr->isn_arg.op.op_ic;
3574
3575 switch (iptr->isn_arg.op.op_type)
3576 {
3577 case EXPR_EQUAL: cmp =
3578 list_equal(arg1, arg2, ic, FALSE); break;
3579 case EXPR_NEQUAL: cmp =
3580 !list_equal(arg1, arg2, ic, FALSE); break;
3581 case EXPR_IS: cmp = arg1 == arg2; break;
3582 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3583 default: cmp = 0; break;
3584 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003585 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 clear_tv(tv1);
3587 clear_tv(tv2);
3588 tv1->v_type = VAR_BOOL;
3589 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3590 }
3591 break;
3592
3593 case ISN_COMPAREBLOB:
3594 {
3595 typval_T *tv1 = STACK_TV_BOT(-2);
3596 typval_T *tv2 = STACK_TV_BOT(-1);
3597 blob_T *arg1 = tv1->vval.v_blob;
3598 blob_T *arg2 = tv2->vval.v_blob;
3599 int cmp = FALSE;
3600
3601 switch (iptr->isn_arg.op.op_type)
3602 {
3603 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3604 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3605 case EXPR_IS: cmp = arg1 == arg2; break;
3606 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3607 default: cmp = 0; break;
3608 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 clear_tv(tv1);
3611 clear_tv(tv2);
3612 tv1->v_type = VAR_BOOL;
3613 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3614 }
3615 break;
3616
3617 // TODO: handle separately
3618 case ISN_COMPARESTRING:
3619 case ISN_COMPAREDICT:
3620 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 case ISN_COMPAREANY:
3622 {
3623 typval_T *tv1 = STACK_TV_BOT(-2);
3624 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003625 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 int ic = iptr->isn_arg.op.op_ic;
3627
Bram Moolenaareb26f432020-09-14 16:50:05 +02003628 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003629 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003631 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 }
3633 break;
3634
3635 case ISN_ADDLIST:
3636 case ISN_ADDBLOB:
3637 {
3638 typval_T *tv1 = STACK_TV_BOT(-2);
3639 typval_T *tv2 = STACK_TV_BOT(-1);
3640
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003641 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 if (iptr->isn_type == ISN_ADDLIST)
3643 eval_addlist(tv1, tv2);
3644 else
3645 eval_addblob(tv1, tv2);
3646 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003647 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 }
3649 break;
3650
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003651 case ISN_LISTAPPEND:
3652 {
3653 typval_T *tv1 = STACK_TV_BOT(-2);
3654 typval_T *tv2 = STACK_TV_BOT(-1);
3655 list_T *l = tv1->vval.v_list;
3656
3657 // add an item to a list
3658 if (l == NULL)
3659 {
3660 SOURCING_LNUM = iptr->isn_lnum;
3661 emsg(_(e_cannot_add_to_null_list));
3662 goto on_error;
3663 }
3664 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003665 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003666 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003667 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003668 }
3669 break;
3670
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003671 case ISN_BLOBAPPEND:
3672 {
3673 typval_T *tv1 = STACK_TV_BOT(-2);
3674 typval_T *tv2 = STACK_TV_BOT(-1);
3675 blob_T *b = tv1->vval.v_blob;
3676 int error = FALSE;
3677 varnumber_T n;
3678
3679 // add a number to a blob
3680 if (b == NULL)
3681 {
3682 SOURCING_LNUM = iptr->isn_lnum;
3683 emsg(_(e_cannot_add_to_null_blob));
3684 goto on_error;
3685 }
3686 n = tv_get_number_chk(tv2, &error);
3687 if (error)
3688 goto on_error;
3689 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003691 }
3692 break;
3693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 // Computation with two arguments of unknown type
3695 case ISN_OPANY:
3696 {
3697 typval_T *tv1 = STACK_TV_BOT(-2);
3698 typval_T *tv2 = STACK_TV_BOT(-1);
3699 varnumber_T n1, n2;
3700#ifdef FEAT_FLOAT
3701 float_T f1 = 0, f2 = 0;
3702#endif
3703 int error = FALSE;
3704
3705 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3706 {
3707 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3708 {
3709 eval_addlist(tv1, tv2);
3710 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003711 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 break;
3713 }
3714 else if (tv1->v_type == VAR_BLOB
3715 && tv2->v_type == VAR_BLOB)
3716 {
3717 eval_addblob(tv1, tv2);
3718 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003719 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 break;
3721 }
3722 }
3723#ifdef FEAT_FLOAT
3724 if (tv1->v_type == VAR_FLOAT)
3725 {
3726 f1 = tv1->vval.v_float;
3727 n1 = 0;
3728 }
3729 else
3730#endif
3731 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003732 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 n1 = tv_get_number_chk(tv1, &error);
3734 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003735 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736#ifdef FEAT_FLOAT
3737 if (tv2->v_type == VAR_FLOAT)
3738 f1 = n1;
3739#endif
3740 }
3741#ifdef FEAT_FLOAT
3742 if (tv2->v_type == VAR_FLOAT)
3743 {
3744 f2 = tv2->vval.v_float;
3745 n2 = 0;
3746 }
3747 else
3748#endif
3749 {
3750 n2 = tv_get_number_chk(tv2, &error);
3751 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003752 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753#ifdef FEAT_FLOAT
3754 if (tv1->v_type == VAR_FLOAT)
3755 f2 = n2;
3756#endif
3757 }
3758#ifdef FEAT_FLOAT
3759 // if there is a float on either side the result is a float
3760 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3761 {
3762 switch (iptr->isn_arg.op.op_type)
3763 {
3764 case EXPR_MULT: f1 = f1 * f2; break;
3765 case EXPR_DIV: f1 = f1 / f2; break;
3766 case EXPR_SUB: f1 = f1 - f2; break;
3767 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003768 default: SOURCING_LNUM = iptr->isn_lnum;
3769 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003770 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 }
3772 clear_tv(tv1);
3773 clear_tv(tv2);
3774 tv1->v_type = VAR_FLOAT;
3775 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003776 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 }
3778 else
3779#endif
3780 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003781 int failed = FALSE;
3782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 switch (iptr->isn_arg.op.op_type)
3784 {
3785 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003786 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3787 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003788 goto on_error;
3789 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 case EXPR_SUB: n1 = n1 - n2; break;
3791 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003792 default: n1 = num_modulus(n1, n2, &failed);
3793 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003794 goto on_error;
3795 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 }
3797 clear_tv(tv1);
3798 clear_tv(tv2);
3799 tv1->v_type = VAR_NUMBER;
3800 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003801 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 }
3803 }
3804 break;
3805
3806 case ISN_CONCAT:
3807 {
3808 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3809 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3810 char_u *res;
3811
3812 res = concat_str(str1, str2);
3813 clear_tv(STACK_TV_BOT(-2));
3814 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003815 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 STACK_TV_BOT(-1)->vval.v_string = res;
3817 }
3818 break;
3819
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003820 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003821 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003822 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003823 int is_slice = iptr->isn_type == ISN_STRSLICE;
3824 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003825 char_u *res;
3826
3827 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003828 // string slice: string is at stack-3, first index at
3829 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003830 if (is_slice)
3831 {
3832 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003833 n1 = tv->vval.v_number;
3834 }
3835
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003836 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003837 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003838
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003840 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003841 if (is_slice)
3842 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003843 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003844 else
3845 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003846 // single character (including composing characters).
3847 // If the index is too big or negative the result is
3848 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003849 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003850 vim_free(tv->vval.v_string);
3851 tv->vval.v_string = res;
3852 }
3853 break;
3854
3855 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003856 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003857 case ISN_BLOBINDEX:
3858 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003860 int is_slice = iptr->isn_type == ISN_LISTSLICE
3861 || iptr->isn_type == ISN_BLOBSLICE;
3862 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3863 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003864 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003865 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866
3867 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003868 // list slice: list is at stack-3, indexes at stack-2 and
3869 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003870 // Same for blob.
3871 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872
3873 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003874 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003876
3877 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 {
Bram Moolenaared591872020-08-15 22:14:53 +02003879 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003880 n1 = tv->vval.v_number;
3881 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 }
Bram Moolenaared591872020-08-15 22:14:53 +02003883
Bram Moolenaar4c137212021-04-19 16:48:48 +02003884 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003885 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003886 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003887 if (is_blob)
3888 {
3889 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3890 n1, n2, FALSE, tv) == FAIL)
3891 goto on_error;
3892 }
3893 else
3894 {
3895 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3896 n1, n2, FALSE, tv, TRUE) == FAIL)
3897 goto on_error;
3898 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 }
3900 break;
3901
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003902 case ISN_ANYINDEX:
3903 case ISN_ANYSLICE:
3904 {
3905 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3906 typval_T *var1, *var2;
3907 int res;
3908
3909 // index: composite is at stack-2, index at stack-1
3910 // slice: composite is at stack-3, indexes at stack-2 and
3911 // stack-1
3912 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003913 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003914 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3915 goto on_error;
3916 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3917 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003918 res = eval_index_inner(tv, is_slice, var1, var2,
3919 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003920 clear_tv(var1);
3921 if (is_slice)
3922 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003923 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003924 if (res == FAIL)
3925 goto on_error;
3926 }
3927 break;
3928
Bram Moolenaar9af78762020-06-16 11:34:42 +02003929 case ISN_SLICE:
3930 {
3931 list_T *list;
3932 int count = iptr->isn_arg.number;
3933
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003934 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003935 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003936 list = tv->vval.v_list;
3937
3938 // no error for short list, expect it to be checked earlier
3939 if (list != NULL && list->lv_len >= count)
3940 {
3941 list_T *newlist = list_slice(list,
3942 count, list->lv_len - 1);
3943
3944 if (newlist != NULL)
3945 {
3946 list_unref(list);
3947 tv->vval.v_list = newlist;
3948 ++newlist->lv_refcount;
3949 }
3950 }
3951 }
3952 break;
3953
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003954 case ISN_GETITEM:
3955 {
3956 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003957 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003958
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003959 // Get list item: list is at stack-1, push item.
3960 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003961 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3962 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003963
Bram Moolenaar35578162021-08-02 19:10:38 +02003964 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003965 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003966 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003967 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003968
3969 // Useful when used in unpack assignment. Reset at
3970 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003971 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003972 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003973 }
3974 break;
3975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 case ISN_MEMBER:
3977 {
3978 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003979 char_u *key;
3980 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003981 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003982
3983 // dict member: dict is at stack-2, key at stack-1
3984 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003985 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003986 dict = tv->vval.v_dict;
3987
3988 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003989 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003990 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003991 if (key == NULL)
3992 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003993
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003994 if ((di = dict_find(dict, key, -1)) == NULL)
3995 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003996 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003997 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003998
3999 // If :silent! is used we will continue, make sure the
4000 // stack contents makes sense.
4001 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004002 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004003 tv = STACK_TV_BOT(-1);
4004 clear_tv(tv);
4005 tv->v_type = VAR_NUMBER;
4006 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004007 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004008 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004009 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004010 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004011 // Clear the dict only after getting the item, to avoid
4012 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004013 tv = STACK_TV_BOT(-1);
4014 temp_tv = *tv;
4015 copy_tv(&di->di_tv, tv);
4016 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004017 }
4018 break;
4019
4020 // dict member with string key
4021 case ISN_STRINGMEMBER:
4022 {
4023 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004025 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026
4027 tv = STACK_TV_BOT(-1);
4028 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4029 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004030 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004032 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 }
4034 dict = tv->vval.v_dict;
4035
4036 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4037 == NULL)
4038 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004039 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004041 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004043 // Clear the dict after getting the item, to avoid that it
4044 // make the item invalid.
4045 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004047 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 }
4049 break;
4050
4051 case ISN_NEGATENR:
4052 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004053 if (tv->v_type != VAR_NUMBER
4054#ifdef FEAT_FLOAT
4055 && tv->v_type != VAR_FLOAT
4056#endif
4057 )
4058 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004059 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004060 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004061 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004062 }
4063#ifdef FEAT_FLOAT
4064 if (tv->v_type == VAR_FLOAT)
4065 tv->vval.v_float = -tv->vval.v_float;
4066 else
4067#endif
4068 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 break;
4070
4071 case ISN_CHECKNR:
4072 {
4073 int error = FALSE;
4074
4075 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004076 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004078 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 (void)tv_get_number_chk(tv, &error);
4080 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004081 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 }
4083 break;
4084
4085 case ISN_CHECKTYPE:
4086 {
4087 checktype_T *ct = &iptr->isn_arg.type;
4088
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004089 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004090 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004091 if (!ectx->ec_where.wt_variable)
4092 ectx->ec_where.wt_index = ct->ct_arg_idx;
4093 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4094 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004095 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004096 if (!ectx->ec_where.wt_variable)
4097 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004098
4099 // number 0 is FALSE, number 1 is TRUE
4100 if (tv->v_type == VAR_NUMBER
4101 && ct->ct_type->tt_type == VAR_BOOL
4102 && (tv->vval.v_number == 0
4103 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004105 tv->v_type = VAR_BOOL;
4106 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004107 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 }
4109 }
4110 break;
4111
Bram Moolenaar9af78762020-06-16 11:34:42 +02004112 case ISN_CHECKLEN:
4113 {
4114 int min_len = iptr->isn_arg.checklen.cl_min_len;
4115 list_T *list = NULL;
4116
4117 tv = STACK_TV_BOT(-1);
4118 if (tv->v_type == VAR_LIST)
4119 list = tv->vval.v_list;
4120 if (list == NULL || list->lv_len < min_len
4121 || (list->lv_len > min_len
4122 && !iptr->isn_arg.checklen.cl_more_OK))
4123 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004124 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004125 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004126 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004127 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004128 }
4129 }
4130 break;
4131
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004132 case ISN_SETTYPE:
4133 {
4134 checktype_T *ct = &iptr->isn_arg.type;
4135
4136 tv = STACK_TV_BOT(-1);
4137 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4138 {
4139 free_type(tv->vval.v_dict->dv_type);
4140 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4141 }
4142 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4143 {
4144 free_type(tv->vval.v_list->lv_type);
4145 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4146 }
4147 }
4148 break;
4149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004151 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 {
4153 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004154 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004156 if (iptr->isn_type == ISN_2BOOL)
4157 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004158 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004159 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004160 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004161 n = !n;
4162 }
4163 else
4164 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004165 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004166 SOURCING_LNUM = iptr->isn_lnum;
4167 n = tv_get_bool_chk(tv, &error);
4168 if (error)
4169 goto on_error;
4170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 clear_tv(tv);
4172 tv->v_type = VAR_BOOL;
4173 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4174 }
4175 break;
4176
4177 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004178 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004179 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004180 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4181 iptr->isn_type == ISN_2STRING_ANY,
4182 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004183 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 break;
4185
Bram Moolenaar08597872020-12-10 19:43:40 +01004186 case ISN_RANGE:
4187 {
4188 exarg_T ea;
4189 char *errormsg;
4190
Bram Moolenaarece0b872021-01-08 20:40:45 +01004191 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004192 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004193 ea.addr_type = ADDR_LINES;
4194 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004195 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004196 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004197 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004198
Bram Moolenaar35578162021-08-02 19:10:38 +02004199 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004200 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004201 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004202 tv = STACK_TV_BOT(-1);
4203 tv->v_type = VAR_NUMBER;
4204 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004205 if (ea.addr_count == 0)
4206 tv->vval.v_number = curwin->w_cursor.lnum;
4207 else
4208 tv->vval.v_number = ea.line2;
4209 }
4210 break;
4211
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004212 case ISN_PUT:
4213 {
4214 int regname = iptr->isn_arg.put.put_regname;
4215 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4216 char_u *expr = NULL;
4217 int dir = FORWARD;
4218
Bram Moolenaar08597872020-12-10 19:43:40 +01004219 if (lnum < -2)
4220 {
4221 // line number was put on the stack by ISN_RANGE
4222 tv = STACK_TV_BOT(-1);
4223 curwin->w_cursor.lnum = tv->vval.v_number;
4224 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4225 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004226 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004227 }
4228 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004229 // :put! above cursor
4230 dir = BACKWARD;
4231 else if (lnum >= 0)
4232 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004233
4234 if (regname == '=')
4235 {
4236 tv = STACK_TV_BOT(-1);
4237 if (tv->v_type == VAR_STRING)
4238 expr = tv->vval.v_string;
4239 else
4240 {
4241 expr = typval2string(tv, TRUE); // allocates value
4242 clear_tv(tv);
4243 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004244 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004245 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004246 check_cursor();
4247 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4248 vim_free(expr);
4249 }
4250 break;
4251
Bram Moolenaar02194d22020-10-24 23:08:38 +02004252 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004253 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4254 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4255 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4256 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004257 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4258 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004259 break;
4260
Bram Moolenaar02194d22020-10-24 23:08:38 +02004261 case ISN_CMDMOD_REV:
4262 // filter regprog is owned by the instruction, don't free it
4263 cmdmod.cmod_filter_regmatch.regprog = NULL;
4264 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004265 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4266 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004267 break;
4268
Bram Moolenaar792f7862020-11-23 08:31:18 +01004269 case ISN_UNPACK:
4270 {
4271 int count = iptr->isn_arg.unpack.unp_count;
4272 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4273 list_T *l;
4274 listitem_T *li;
4275 int i;
4276
4277 // Check there is a valid list to unpack.
4278 tv = STACK_TV_BOT(-1);
4279 if (tv->v_type != VAR_LIST)
4280 {
4281 SOURCING_LNUM = iptr->isn_lnum;
4282 emsg(_(e_for_argument_must_be_sequence_of_lists));
4283 goto on_error;
4284 }
4285 l = tv->vval.v_list;
4286 if (l == NULL
4287 || l->lv_len < (semicolon ? count - 1 : count))
4288 {
4289 SOURCING_LNUM = iptr->isn_lnum;
4290 emsg(_(e_list_value_does_not_have_enough_items));
4291 goto on_error;
4292 }
4293 else if (!semicolon && l->lv_len > count)
4294 {
4295 SOURCING_LNUM = iptr->isn_lnum;
4296 emsg(_(e_list_value_has_more_items_than_targets));
4297 goto on_error;
4298 }
4299
4300 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004301 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004302 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004303 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004304
4305 // Variable after semicolon gets a list with the remaining
4306 // items.
4307 if (semicolon)
4308 {
4309 list_T *rem_list =
4310 list_alloc_with_items(l->lv_len - count + 1);
4311
4312 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004313 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004314 tv = STACK_TV_BOT(-count);
4315 tv->vval.v_list = rem_list;
4316 ++rem_list->lv_refcount;
4317 tv->v_lock = 0;
4318 li = l->lv_first;
4319 for (i = 0; i < count - 1; ++i)
4320 li = li->li_next;
4321 for (i = 0; li != NULL; ++i)
4322 {
4323 list_set_item(rem_list, i, &li->li_tv);
4324 li = li->li_next;
4325 }
4326 --count;
4327 }
4328
4329 // Produce the values in reverse order, first item last.
4330 li = l->lv_first;
4331 for (i = 0; i < count; ++i)
4332 {
4333 tv = STACK_TV_BOT(-i - 1);
4334 copy_tv(&li->li_tv, tv);
4335 li = li->li_next;
4336 }
4337
4338 list_unref(l);
4339 }
4340 break;
4341
Bram Moolenaarb2049902021-01-24 12:53:53 +01004342 case ISN_PROF_START:
4343 case ISN_PROF_END:
4344 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004345#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004346 funccall_T cookie;
4347 ufunc_T *cur_ufunc =
4348 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004349 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004350
4351 cookie.func = cur_ufunc;
4352 if (iptr->isn_type == ISN_PROF_START)
4353 {
4354 func_line_start(&cookie, iptr->isn_lnum);
4355 // if we get here the instruction is executed
4356 func_line_exec(&cookie);
4357 }
4358 else
4359 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004360#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004361 }
4362 break;
4363
Bram Moolenaare99d4222021-06-13 14:01:26 +02004364 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004365 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004366 break;
4367
Bram Moolenaar389df252020-07-09 21:20:47 +02004368 case ISN_SHUFFLE:
4369 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004370 typval_T tmp_tv;
4371 int item = iptr->isn_arg.shuffle.shfl_item;
4372 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004373
4374 tmp_tv = *STACK_TV_BOT(-item);
4375 for ( ; up > 0 && item > 1; --up)
4376 {
4377 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4378 --item;
4379 }
4380 *STACK_TV_BOT(-item) = tmp_tv;
4381 }
4382 break;
4383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004387 ectx->ec_where.wt_index = 0;
4388 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 break;
4390 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004391 continue;
4392
Bram Moolenaard032f342020-07-18 18:13:02 +02004393func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004394 // Restore previous function. If the frame pointer is where we started
4395 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004396 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004397 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004398
Bram Moolenaar4c137212021-04-19 16:48:48 +02004399 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004400 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004401 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004402 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004403
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004404on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004405 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004406 // If "emsg_silent" is set then ignore the error, unless it was set
4407 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004408 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004409 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004410 {
4411 // If a sequence of instructions causes an error while ":silent!"
4412 // was used, restore the stack length and jump ahead to restoring
4413 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004414 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004415 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004416 while (ectx->ec_stack.ga_len
4417 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004418 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004419 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004420 clear_tv(STACK_TV_BOT(0));
4421 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004422 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4423 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004424 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004425 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004426 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004427on_fatal_error:
4428 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004429 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004430 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004431 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 }
4433
4434done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004435 ret = OK;
4436theend:
4437 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4438 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004439}
4440
4441/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004442 * Execute the instructions from a VAR_INSTR typeval and put the result in
4443 * "rettv".
4444 * Return OK or FAIL.
4445 */
4446 int
4447exe_typval_instr(typval_T *tv, typval_T *rettv)
4448{
4449 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4450 isn_T *save_instr = ectx->ec_instr;
4451 int save_iidx = ectx->ec_iidx;
4452 int res;
4453
4454 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4455 res = exec_instructions(ectx);
4456 if (res == OK)
4457 {
4458 *rettv = *STACK_TV_BOT(-1);
4459 --ectx->ec_stack.ga_len;
4460 }
4461
4462 ectx->ec_instr = save_instr;
4463 ectx->ec_iidx = save_iidx;
4464
4465 return res;
4466}
4467
4468/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004469 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4470 * "substitute_instr".
4471 */
4472 char_u *
4473exe_substitute_instr(void)
4474{
4475 ectx_T *ectx = substitute_instr->subs_ectx;
4476 isn_T *save_instr = ectx->ec_instr;
4477 int save_iidx = ectx->ec_iidx;
4478 char_u *res;
4479
4480 ectx->ec_instr = substitute_instr->subs_instr;
4481 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004482 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004483 typval_T *tv = STACK_TV_BOT(-1);
4484
Bram Moolenaar27523602021-06-05 21:36:19 +02004485 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004486 --ectx->ec_stack.ga_len;
4487 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004488 }
4489 else
4490 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004491 substitute_instr->subs_status = FAIL;
4492 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494
Bram Moolenaar4c137212021-04-19 16:48:48 +02004495 ectx->ec_instr = save_instr;
4496 ectx->ec_iidx = save_iidx;
4497
4498 return res;
4499}
4500
4501/*
4502 * Call a "def" function from old Vim script.
4503 * Return OK or FAIL.
4504 */
4505 int
4506call_def_function(
4507 ufunc_T *ufunc,
4508 int argc_arg, // nr of arguments
4509 typval_T *argv, // arguments
4510 partial_T *partial, // optional partial for context
4511 typval_T *rettv) // return value
4512{
4513 ectx_T ectx; // execution context
4514 int argc = argc_arg;
4515 typval_T *tv;
4516 int idx;
4517 int ret = FAIL;
4518 int defcount = ufunc->uf_args.ga_len - argc;
4519 sctx_T save_current_sctx = current_sctx;
4520 int did_emsg_before = did_emsg_cumul + did_emsg;
4521 int save_suppress_errthrow = suppress_errthrow;
4522 msglist_T **saved_msg_list = NULL;
4523 msglist_T *private_msg_list = NULL;
4524 int save_emsg_silent_def = emsg_silent_def;
4525 int save_did_emsg_def = did_emsg_def;
4526 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004527 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004528
4529// Get pointer to item in the stack.
4530#undef STACK_TV
4531#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4532
4533// Get pointer to item at the bottom of the stack, -1 is the bottom.
4534#undef STACK_TV_BOT
4535#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4536
4537// Get pointer to a local variable on the stack. Negative for arguments.
4538#undef STACK_TV_VAR
4539#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4540
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004541 // Update uf_has_breakpoint if needed.
4542 update_has_breakpoint(ufunc);
4543
Bram Moolenaar4c137212021-04-19 16:48:48 +02004544 if (ufunc->uf_def_status == UF_NOT_COMPILED
4545 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004546 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4547 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004548 == FAIL))
4549 {
4550 if (did_emsg_cumul + did_emsg == did_emsg_before)
4551 semsg(_(e_function_is_not_compiled_str),
4552 printable_func_name(ufunc));
4553 return FAIL;
4554 }
4555
4556 {
4557 // Check the function was really compiled.
4558 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4559 + ufunc->uf_dfunc_idx;
4560 if (INSTRUCTIONS(dfunc) == NULL)
4561 {
4562 iemsg("using call_def_function() on not compiled function");
4563 return FAIL;
4564 }
4565 }
4566
4567 // If depth of calling is getting too high, don't execute the function.
4568 orig_funcdepth = funcdepth_get();
4569 if (funcdepth_increment() == FAIL)
4570 return FAIL;
4571
4572 CLEAR_FIELD(ectx);
4573 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4574 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004575 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004576 {
4577 funcdepth_decrement();
4578 return FAIL;
4579 }
4580 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4581 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4582 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004583 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004584
4585 idx = argc - ufunc->uf_args.ga_len;
4586 if (idx > 0 && ufunc->uf_va_name == NULL)
4587 {
4588 if (idx == 1)
4589 emsg(_(e_one_argument_too_many));
4590 else
4591 semsg(_(e_nr_arguments_too_many), idx);
4592 goto failed_early;
4593 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004594 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4595 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004596 {
4597 if (idx == -1)
4598 emsg(_(e_one_argument_too_few));
4599 else
4600 semsg(_(e_nr_arguments_too_few), -idx);
4601 goto failed_early;
4602 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004603
4604 // Put arguments on the stack, but no more than what the function expects.
4605 // A lambda can be called with more arguments than it uses.
4606 for (idx = 0; idx < argc
4607 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4608 ++idx)
4609 {
4610 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4611 && argv[idx].v_type == VAR_SPECIAL
4612 && argv[idx].vval.v_number == VVAL_NONE)
4613 {
4614 // Use the default value.
4615 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4616 }
4617 else
4618 {
4619 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4620 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004621 ufunc->uf_arg_types[idx], &argv[idx],
4622 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004623 goto failed_early;
4624 copy_tv(&argv[idx], STACK_TV_BOT(0));
4625 }
4626 ++ectx.ec_stack.ga_len;
4627 }
4628
4629 // Turn varargs into a list. Empty list if no args.
4630 if (ufunc->uf_va_name != NULL)
4631 {
4632 int vararg_count = argc - ufunc->uf_args.ga_len;
4633
4634 if (vararg_count < 0)
4635 vararg_count = 0;
4636 else
4637 argc -= vararg_count;
4638 if (exe_newlist(vararg_count, &ectx) == FAIL)
4639 goto failed_early;
4640
4641 // Check the type of the list items.
4642 tv = STACK_TV_BOT(-1);
4643 if (ufunc->uf_va_type != NULL
4644 && ufunc->uf_va_type != &t_list_any
4645 && ufunc->uf_va_type->tt_member != &t_any
4646 && tv->vval.v_list != NULL)
4647 {
4648 type_T *expected = ufunc->uf_va_type->tt_member;
4649 listitem_T *li = tv->vval.v_list->lv_first;
4650
4651 for (idx = 0; idx < vararg_count; ++idx)
4652 {
4653 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004654 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004655 goto failed_early;
4656 li = li->li_next;
4657 }
4658 }
4659
4660 if (defcount > 0)
4661 // Move varargs list to below missing default arguments.
4662 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4663 --ectx.ec_stack.ga_len;
4664 }
4665
4666 // Make space for omitted arguments, will store default value below.
4667 // Any varargs list goes after them.
4668 if (defcount > 0)
4669 for (idx = 0; idx < defcount; ++idx)
4670 {
4671 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4672 ++ectx.ec_stack.ga_len;
4673 }
4674 if (ufunc->uf_va_name != NULL)
4675 ++ectx.ec_stack.ga_len;
4676
4677 // Frame pointer points to just after arguments.
4678 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4679 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4680
4681 {
4682 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4683 + ufunc->uf_dfunc_idx;
4684 ufunc_T *base_ufunc = dfunc->df_ufunc;
4685
4686 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4687 // by copy_func().
4688 if (partial != NULL || base_ufunc->uf_partial != NULL)
4689 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004690 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4691 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004692 goto failed_early;
4693 if (partial != NULL)
4694 {
4695 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4696 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004697 if (current_ectx->ec_outer_ref != NULL
4698 && current_ectx->ec_outer_ref->or_outer != NULL)
4699 ectx.ec_outer_ref->or_outer =
4700 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004701 }
4702 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004703 {
4704 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4705 ++partial->pt_refcount;
4706 ectx.ec_outer_ref->or_partial = partial;
4707 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004708 }
4709 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004710 {
4711 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4712 ++base_ufunc->uf_partial->pt_refcount;
4713 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4714 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004715 }
4716 }
4717
4718 // dummy frame entries
4719 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4720 {
4721 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4722 ++ectx.ec_stack.ga_len;
4723 }
4724
4725 {
4726 // Reserve space for local variables and any closure reference count.
4727 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4728 + ufunc->uf_dfunc_idx;
4729
4730 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4731 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4732 ectx.ec_stack.ga_len += dfunc->df_varcount;
4733 if (dfunc->df_has_closure)
4734 {
4735 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4736 STACK_TV_VAR(idx)->vval.v_number = 0;
4737 ++ectx.ec_stack.ga_len;
4738 }
4739
4740 ectx.ec_instr = INSTRUCTIONS(dfunc);
4741 }
4742
4743 // Following errors are in the function, not the caller.
4744 // Commands behave like vim9script.
4745 estack_push_ufunc(ufunc, 1);
4746 current_sctx = ufunc->uf_script_ctx;
4747 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4748
4749 // Use a specific location for storing error messages to be converted to an
4750 // exception.
4751 saved_msg_list = msg_list;
4752 msg_list = &private_msg_list;
4753
4754 // Do turn errors into exceptions.
4755 suppress_errthrow = FALSE;
4756
4757 // Do not delete the function while executing it.
4758 ++ufunc->uf_calls;
4759
4760 // When ":silent!" was used before calling then we still abort the
4761 // function. If ":silent!" is used in the function then we don't.
4762 emsg_silent_def = emsg_silent;
4763 did_emsg_def = 0;
4764
4765 ectx.ec_where.wt_index = 0;
4766 ectx.ec_where.wt_variable = FALSE;
4767
4768 // Execute the instructions until done.
4769 ret = exec_instructions(&ectx);
4770 if (ret == OK)
4771 {
4772 // function finished, get result from the stack.
4773 if (ufunc->uf_ret_type == &t_void)
4774 {
4775 rettv->v_type = VAR_VOID;
4776 }
4777 else
4778 {
4779 tv = STACK_TV_BOT(-1);
4780 *rettv = *tv;
4781 tv->v_type = VAR_UNKNOWN;
4782 }
4783 }
4784
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004785 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004786 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4787 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004788
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004789 // Deal with any remaining closures, they may be in use somewhere.
4790 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004791 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004792 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004793 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4794 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004795
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004796 estack_pop();
4797 current_sctx = save_current_sctx;
4798
Bram Moolenaarc970e422021-03-17 15:03:04 +01004799 // TODO: when is it safe to delete the function if it is no longer used?
4800 --ufunc->uf_calls;
4801
Bram Moolenaar352134b2020-10-17 22:04:08 +02004802 if (*msg_list != NULL && saved_msg_list != NULL)
4803 {
4804 msglist_T **plist = saved_msg_list;
4805
4806 // Append entries from the current msg_list (uncaught exceptions) to
4807 // the saved msg_list.
4808 while (*plist != NULL)
4809 plist = &(*plist)->next;
4810
4811 *plist = *msg_list;
4812 }
4813 msg_list = saved_msg_list;
4814
Bram Moolenaar4c137212021-04-19 16:48:48 +02004815 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004816 {
4817 cmdmod.cmod_filter_regmatch.regprog = NULL;
4818 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004819 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004820 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004821 emsg_silent_def = save_emsg_silent_def;
4822 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004823
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004824failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004825 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4827 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004828 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004831 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004832 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004833 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004834 if (ectx.ec_outer_ref->or_outer_allocated)
4835 vim_free(ectx.ec_outer_ref->or_outer);
4836 partial_unref(ectx.ec_outer_ref->or_partial);
4837 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004838 }
4839
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004840 // Not sure if this is necessary.
4841 suppress_errthrow = save_suppress_errthrow;
4842
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004843 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
4844 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004845 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004846 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004847 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 return ret;
4849}
4850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004852 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4853 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4854 * "pfx" is prefixed to every line.
4855 */
4856 static void
4857list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4858{
4859 int line_idx = 0;
4860 int prev_current = 0;
4861 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004862 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004863
4864 for (current = 0; current < instr_count; ++current)
4865 {
4866 isn_T *iptr = &instr[current];
4867 char *line;
4868
4869 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004870 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004871 while (line_idx < iptr->isn_lnum
4872 && line_idx < ufunc->uf_lines.ga_len)
4873 {
4874 if (current > prev_current)
4875 {
4876 msg_puts("\n\n");
4877 prev_current = current;
4878 }
4879 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4880 if (line != NULL)
4881 msg(line);
4882 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004883 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4884 {
4885 int first_def_arg = ufunc->uf_args.ga_len
4886 - ufunc->uf_def_args.ga_len;
4887
4888 if (def_arg_idx > 0)
4889 msg_puts("\n\n");
4890 msg_start();
4891 msg_puts(" ");
4892 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4893 first_def_arg + def_arg_idx]);
4894 msg_puts(" = ");
4895 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4896 msg_clr_eos();
4897 msg_end();
4898 }
4899 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004900
4901 switch (iptr->isn_type)
4902 {
4903 case ISN_EXEC:
4904 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4905 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004906 case ISN_EXEC_SPLIT:
4907 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4908 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004909 case ISN_LEGACY_EVAL:
4910 smsg("%s%4d EVAL legacy %s", pfx, current,
4911 iptr->isn_arg.string);
4912 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004913 case ISN_REDIRSTART:
4914 smsg("%s%4d REDIR", pfx, current);
4915 break;
4916 case ISN_REDIREND:
4917 smsg("%s%4d REDIR END%s", pfx, current,
4918 iptr->isn_arg.number ? " append" : "");
4919 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004920 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004921#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004922 smsg("%s%4d CEXPR pre %s", pfx, current,
4923 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004924#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004925 break;
4926 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004927#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004928 {
4929 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4930
4931 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4932 cexpr_get_auname(cer->cer_cmdidx),
4933 cer->cer_forceit ? "!" : "",
4934 cer->cer_cmdline);
4935 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004936#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004937 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004938 case ISN_INSTR:
4939 {
4940 smsg("%s%4d INSTR", pfx, current);
4941 list_instructions(" ", iptr->isn_arg.instr,
4942 INT_MAX, NULL);
4943 msg(" -------------");
4944 }
4945 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004946 case ISN_SUBSTITUTE:
4947 {
4948 subs_T *subs = &iptr->isn_arg.subs;
4949
4950 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4951 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4952 msg(" -------------");
4953 }
4954 break;
4955 case ISN_EXECCONCAT:
4956 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4957 (varnumber_T)iptr->isn_arg.number);
4958 break;
4959 case ISN_ECHO:
4960 {
4961 echo_T *echo = &iptr->isn_arg.echo;
4962
4963 smsg("%s%4d %s %d", pfx, current,
4964 echo->echo_with_white ? "ECHO" : "ECHON",
4965 echo->echo_count);
4966 }
4967 break;
4968 case ISN_EXECUTE:
4969 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02004970 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004971 break;
4972 case ISN_ECHOMSG:
4973 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02004974 (varnumber_T)(iptr->isn_arg.number));
4975 break;
4976 case ISN_ECHOCONSOLE:
4977 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
4978 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004979 break;
4980 case ISN_ECHOERR:
4981 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02004982 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004983 break;
4984 case ISN_LOAD:
4985 {
4986 if (iptr->isn_arg.number < 0)
4987 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4988 (varnumber_T)(iptr->isn_arg.number
4989 + STACK_FRAME_SIZE));
4990 else
4991 smsg("%s%4d LOAD $%lld", pfx, current,
4992 (varnumber_T)(iptr->isn_arg.number));
4993 }
4994 break;
4995 case ISN_LOADOUTER:
4996 {
4997 if (iptr->isn_arg.number < 0)
4998 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4999 iptr->isn_arg.outer.outer_depth,
5000 iptr->isn_arg.outer.outer_idx
5001 + STACK_FRAME_SIZE);
5002 else
5003 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5004 iptr->isn_arg.outer.outer_depth,
5005 iptr->isn_arg.outer.outer_idx);
5006 }
5007 break;
5008 case ISN_LOADV:
5009 smsg("%s%4d LOADV v:%s", pfx, current,
5010 get_vim_var_name(iptr->isn_arg.number));
5011 break;
5012 case ISN_LOADSCRIPT:
5013 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005014 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5015 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5016 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005017
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005018 sv = get_script_svar(sref, -1);
5019 if (sv == NULL)
5020 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5021 pfx, current, si->sn_name);
5022 else
5023 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005024 sv->sv_name,
5025 sref->sref_idx,
5026 si->sn_name);
5027 }
5028 break;
5029 case ISN_LOADS:
5030 {
5031 scriptitem_T *si = SCRIPT_ITEM(
5032 iptr->isn_arg.loadstore.ls_sid);
5033
5034 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5035 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5036 }
5037 break;
5038 case ISN_LOADAUTO:
5039 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5040 break;
5041 case ISN_LOADG:
5042 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5043 break;
5044 case ISN_LOADB:
5045 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5046 break;
5047 case ISN_LOADW:
5048 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5049 break;
5050 case ISN_LOADT:
5051 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5052 break;
5053 case ISN_LOADGDICT:
5054 smsg("%s%4d LOAD g:", pfx, current);
5055 break;
5056 case ISN_LOADBDICT:
5057 smsg("%s%4d LOAD b:", pfx, current);
5058 break;
5059 case ISN_LOADWDICT:
5060 smsg("%s%4d LOAD w:", pfx, current);
5061 break;
5062 case ISN_LOADTDICT:
5063 smsg("%s%4d LOAD t:", pfx, current);
5064 break;
5065 case ISN_LOADOPT:
5066 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5067 break;
5068 case ISN_LOADENV:
5069 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5070 break;
5071 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005072 smsg("%s%4d LOADREG @%c", pfx, current,
5073 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005074 break;
5075
5076 case ISN_STORE:
5077 if (iptr->isn_arg.number < 0)
5078 smsg("%s%4d STORE arg[%lld]", pfx, current,
5079 iptr->isn_arg.number + STACK_FRAME_SIZE);
5080 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005081 smsg("%s%4d STORE $%lld", pfx, current,
5082 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005083 break;
5084 case ISN_STOREOUTER:
5085 {
5086 if (iptr->isn_arg.number < 0)
5087 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5088 iptr->isn_arg.outer.outer_depth,
5089 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5090 else
5091 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5092 iptr->isn_arg.outer.outer_depth,
5093 iptr->isn_arg.outer.outer_idx);
5094 }
5095 break;
5096 case ISN_STOREV:
5097 smsg("%s%4d STOREV v:%s", pfx, current,
5098 get_vim_var_name(iptr->isn_arg.number));
5099 break;
5100 case ISN_STOREAUTO:
5101 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5102 break;
5103 case ISN_STOREG:
5104 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5105 break;
5106 case ISN_STOREB:
5107 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5108 break;
5109 case ISN_STOREW:
5110 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5111 break;
5112 case ISN_STORET:
5113 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5114 break;
5115 case ISN_STORES:
5116 {
5117 scriptitem_T *si = SCRIPT_ITEM(
5118 iptr->isn_arg.loadstore.ls_sid);
5119
5120 smsg("%s%4d STORES %s in %s", pfx, current,
5121 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5122 }
5123 break;
5124 case ISN_STORESCRIPT:
5125 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005126 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5127 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5128 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005129
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005130 sv = get_script_svar(sref, -1);
5131 if (sv == NULL)
5132 smsg("%s%4d STORESCRIPT [deleted] in %s",
5133 pfx, current, si->sn_name);
5134 else
5135 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005136 sv->sv_name,
5137 sref->sref_idx,
5138 si->sn_name);
5139 }
5140 break;
5141 case ISN_STOREOPT:
5142 smsg("%s%4d STOREOPT &%s", pfx, current,
5143 iptr->isn_arg.storeopt.so_name);
5144 break;
5145 case ISN_STOREENV:
5146 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5147 break;
5148 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005149 smsg("%s%4d STOREREG @%c", pfx, current,
5150 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005151 break;
5152 case ISN_STORENR:
5153 smsg("%s%4d STORE %lld in $%d", pfx, current,
5154 iptr->isn_arg.storenr.stnr_val,
5155 iptr->isn_arg.storenr.stnr_idx);
5156 break;
5157
5158 case ISN_STOREINDEX:
5159 smsg("%s%4d STOREINDEX %s", pfx, current,
5160 vartype_name(iptr->isn_arg.vartype));
5161 break;
5162
5163 case ISN_STORERANGE:
5164 smsg("%s%4d STORERANGE", pfx, current);
5165 break;
5166
5167 // constants
5168 case ISN_PUSHNR:
5169 smsg("%s%4d PUSHNR %lld", pfx, current,
5170 (varnumber_T)(iptr->isn_arg.number));
5171 break;
5172 case ISN_PUSHBOOL:
5173 case ISN_PUSHSPEC:
5174 smsg("%s%4d PUSH %s", pfx, current,
5175 get_var_special_name(iptr->isn_arg.number));
5176 break;
5177 case ISN_PUSHF:
5178#ifdef FEAT_FLOAT
5179 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5180#endif
5181 break;
5182 case ISN_PUSHS:
5183 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5184 break;
5185 case ISN_PUSHBLOB:
5186 {
5187 char_u *r;
5188 char_u numbuf[NUMBUFLEN];
5189 char_u *tofree;
5190
5191 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5192 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5193 vim_free(tofree);
5194 }
5195 break;
5196 case ISN_PUSHFUNC:
5197 {
5198 char *name = (char *)iptr->isn_arg.string;
5199
5200 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5201 name == NULL ? "[none]" : name);
5202 }
5203 break;
5204 case ISN_PUSHCHANNEL:
5205#ifdef FEAT_JOB_CHANNEL
5206 {
5207 channel_T *channel = iptr->isn_arg.channel;
5208
5209 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5210 channel == NULL ? 0 : channel->ch_id);
5211 }
5212#endif
5213 break;
5214 case ISN_PUSHJOB:
5215#ifdef FEAT_JOB_CHANNEL
5216 {
5217 typval_T tv;
5218 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005219 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005220
5221 tv.v_type = VAR_JOB;
5222 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005223 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005224 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5225 }
5226#endif
5227 break;
5228 case ISN_PUSHEXC:
5229 smsg("%s%4d PUSH v:exception", pfx, current);
5230 break;
5231 case ISN_UNLET:
5232 smsg("%s%4d UNLET%s %s", pfx, current,
5233 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5234 iptr->isn_arg.unlet.ul_name);
5235 break;
5236 case ISN_UNLETENV:
5237 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5238 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5239 iptr->isn_arg.unlet.ul_name);
5240 break;
5241 case ISN_UNLETINDEX:
5242 smsg("%s%4d UNLETINDEX", pfx, current);
5243 break;
5244 case ISN_UNLETRANGE:
5245 smsg("%s%4d UNLETRANGE", pfx, current);
5246 break;
5247 case ISN_LOCKCONST:
5248 smsg("%s%4d LOCKCONST", pfx, current);
5249 break;
5250 case ISN_NEWLIST:
5251 smsg("%s%4d NEWLIST size %lld", pfx, current,
5252 (varnumber_T)(iptr->isn_arg.number));
5253 break;
5254 case ISN_NEWDICT:
5255 smsg("%s%4d NEWDICT size %lld", pfx, current,
5256 (varnumber_T)(iptr->isn_arg.number));
5257 break;
5258
5259 // function call
5260 case ISN_BCALL:
5261 {
5262 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5263
5264 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5265 internal_func_name(cbfunc->cbf_idx),
5266 cbfunc->cbf_argcount);
5267 }
5268 break;
5269 case ISN_DCALL:
5270 {
5271 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5272 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5273 + cdfunc->cdf_idx;
5274
5275 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005276 printable_func_name(df->df_ufunc),
5277 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005278 }
5279 break;
5280 case ISN_UCALL:
5281 {
5282 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5283
5284 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5285 cufunc->cuf_name, cufunc->cuf_argcount);
5286 }
5287 break;
5288 case ISN_PCALL:
5289 {
5290 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5291
5292 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5293 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5294 }
5295 break;
5296 case ISN_PCALL_END:
5297 smsg("%s%4d PCALL end", pfx, current);
5298 break;
5299 case ISN_RETURN:
5300 smsg("%s%4d RETURN", pfx, current);
5301 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005302 case ISN_RETURN_VOID:
5303 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005304 break;
5305 case ISN_FUNCREF:
5306 {
5307 funcref_T *funcref = &iptr->isn_arg.funcref;
5308 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5309 + funcref->fr_func;
5310
5311 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5312 }
5313 break;
5314
5315 case ISN_NEWFUNC:
5316 {
5317 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5318
5319 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5320 newfunc->nf_lambda, newfunc->nf_global);
5321 }
5322 break;
5323
5324 case ISN_DEF:
5325 {
5326 char_u *name = iptr->isn_arg.string;
5327
5328 smsg("%s%4d DEF %s", pfx, current,
5329 name == NULL ? (char_u *)"" : name);
5330 }
5331 break;
5332
5333 case ISN_JUMP:
5334 {
5335 char *when = "?";
5336
5337 switch (iptr->isn_arg.jump.jump_when)
5338 {
5339 case JUMP_ALWAYS:
5340 when = "JUMP";
5341 break;
5342 case JUMP_AND_KEEP_IF_TRUE:
5343 when = "JUMP_AND_KEEP_IF_TRUE";
5344 break;
5345 case JUMP_IF_FALSE:
5346 when = "JUMP_IF_FALSE";
5347 break;
5348 case JUMP_AND_KEEP_IF_FALSE:
5349 when = "JUMP_AND_KEEP_IF_FALSE";
5350 break;
5351 case JUMP_IF_COND_FALSE:
5352 when = "JUMP_IF_COND_FALSE";
5353 break;
5354 case JUMP_IF_COND_TRUE:
5355 when = "JUMP_IF_COND_TRUE";
5356 break;
5357 }
5358 smsg("%s%4d %s -> %d", pfx, current, when,
5359 iptr->isn_arg.jump.jump_where);
5360 }
5361 break;
5362
5363 case ISN_JUMP_IF_ARG_SET:
5364 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5365 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5366 iptr->isn_arg.jump.jump_where);
5367 break;
5368
5369 case ISN_FOR:
5370 {
5371 forloop_T *forloop = &iptr->isn_arg.forloop;
5372
5373 smsg("%s%4d FOR $%d -> %d", pfx, current,
5374 forloop->for_idx, forloop->for_end);
5375 }
5376 break;
5377
5378 case ISN_TRY:
5379 {
5380 try_T *try = &iptr->isn_arg.try;
5381
5382 if (try->try_ref->try_finally == 0)
5383 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5384 pfx, current,
5385 try->try_ref->try_catch,
5386 try->try_ref->try_endtry);
5387 else
5388 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5389 pfx, current,
5390 try->try_ref->try_catch,
5391 try->try_ref->try_finally,
5392 try->try_ref->try_endtry);
5393 }
5394 break;
5395 case ISN_CATCH:
5396 // TODO
5397 smsg("%s%4d CATCH", pfx, current);
5398 break;
5399 case ISN_TRYCONT:
5400 {
5401 trycont_T *trycont = &iptr->isn_arg.trycont;
5402
5403 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5404 trycont->tct_levels,
5405 trycont->tct_levels == 1 ? "" : "s",
5406 trycont->tct_where);
5407 }
5408 break;
5409 case ISN_FINALLY:
5410 smsg("%s%4d FINALLY", pfx, current);
5411 break;
5412 case ISN_ENDTRY:
5413 smsg("%s%4d ENDTRY", pfx, current);
5414 break;
5415 case ISN_THROW:
5416 smsg("%s%4d THROW", pfx, current);
5417 break;
5418
5419 // expression operations on number
5420 case ISN_OPNR:
5421 case ISN_OPFLOAT:
5422 case ISN_OPANY:
5423 {
5424 char *what;
5425 char *ins;
5426
5427 switch (iptr->isn_arg.op.op_type)
5428 {
5429 case EXPR_MULT: what = "*"; break;
5430 case EXPR_DIV: what = "/"; break;
5431 case EXPR_REM: what = "%"; break;
5432 case EXPR_SUB: what = "-"; break;
5433 case EXPR_ADD: what = "+"; break;
5434 default: what = "???"; break;
5435 }
5436 switch (iptr->isn_type)
5437 {
5438 case ISN_OPNR: ins = "OPNR"; break;
5439 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5440 case ISN_OPANY: ins = "OPANY"; break;
5441 default: ins = "???"; break;
5442 }
5443 smsg("%s%4d %s %s", pfx, current, ins, what);
5444 }
5445 break;
5446
5447 case ISN_COMPAREBOOL:
5448 case ISN_COMPARESPECIAL:
5449 case ISN_COMPARENR:
5450 case ISN_COMPAREFLOAT:
5451 case ISN_COMPARESTRING:
5452 case ISN_COMPAREBLOB:
5453 case ISN_COMPARELIST:
5454 case ISN_COMPAREDICT:
5455 case ISN_COMPAREFUNC:
5456 case ISN_COMPAREANY:
5457 {
5458 char *p;
5459 char buf[10];
5460 char *type;
5461
5462 switch (iptr->isn_arg.op.op_type)
5463 {
5464 case EXPR_EQUAL: p = "=="; break;
5465 case EXPR_NEQUAL: p = "!="; break;
5466 case EXPR_GREATER: p = ">"; break;
5467 case EXPR_GEQUAL: p = ">="; break;
5468 case EXPR_SMALLER: p = "<"; break;
5469 case EXPR_SEQUAL: p = "<="; break;
5470 case EXPR_MATCH: p = "=~"; break;
5471 case EXPR_IS: p = "is"; break;
5472 case EXPR_ISNOT: p = "isnot"; break;
5473 case EXPR_NOMATCH: p = "!~"; break;
5474 default: p = "???"; break;
5475 }
5476 STRCPY(buf, p);
5477 if (iptr->isn_arg.op.op_ic == TRUE)
5478 strcat(buf, "?");
5479 switch(iptr->isn_type)
5480 {
5481 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5482 case ISN_COMPARESPECIAL:
5483 type = "COMPARESPECIAL"; break;
5484 case ISN_COMPARENR: type = "COMPARENR"; break;
5485 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5486 case ISN_COMPARESTRING:
5487 type = "COMPARESTRING"; break;
5488 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5489 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5490 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5491 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5492 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5493 default: type = "???"; break;
5494 }
5495
5496 smsg("%s%4d %s %s", pfx, current, type, buf);
5497 }
5498 break;
5499
5500 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5501 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5502
5503 // expression operations
5504 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5505 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5506 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5507 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5508 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5509 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5510 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5511 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5512 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5513 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5514 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5515 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005516 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005517 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5518 iptr->isn_arg.getitem.gi_index,
5519 iptr->isn_arg.getitem.gi_with_op ?
5520 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005521 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5522 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5523 iptr->isn_arg.string); break;
5524 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5525
5526 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5527 case ISN_CHECKTYPE:
5528 {
5529 checktype_T *ct = &iptr->isn_arg.type;
5530 char *tofree;
5531
5532 if (ct->ct_arg_idx == 0)
5533 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5534 type_name(ct->ct_type, &tofree),
5535 (int)ct->ct_off);
5536 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005537 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5538 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005539 type_name(ct->ct_type, &tofree),
5540 (int)ct->ct_off,
5541 (int)ct->ct_arg_idx);
5542 vim_free(tofree);
5543 break;
5544 }
5545 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5546 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5547 iptr->isn_arg.checklen.cl_min_len);
5548 break;
5549 case ISN_SETTYPE:
5550 {
5551 char *tofree;
5552
5553 smsg("%s%4d SETTYPE %s", pfx, current,
5554 type_name(iptr->isn_arg.type.ct_type, &tofree));
5555 vim_free(tofree);
5556 break;
5557 }
5558 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005559 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5560 smsg("%s%4d INVERT %d (!val)", pfx, current,
5561 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005562 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005563 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5564 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005565 break;
5566 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005567 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005568 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005569 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5570 pfx, current,
5571 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005572 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005573 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5574 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005575 break;
5576 case ISN_PUT:
5577 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5578 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005579 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005580 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5581 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005582 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005583 else
5584 smsg("%s%4d PUT %c %ld", pfx, current,
5585 iptr->isn_arg.put.put_regname,
5586 (long)iptr->isn_arg.put.put_lnum);
5587 break;
5588
5589 // TODO: summarize modifiers
5590 case ISN_CMDMOD:
5591 {
5592 char_u *buf;
5593 size_t len = produce_cmdmods(
5594 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5595
5596 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005597 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005598 {
5599 (void)produce_cmdmods(
5600 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5601 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5602 vim_free(buf);
5603 }
5604 break;
5605 }
5606 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5607
5608 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005609 smsg("%s%4d PROFILE START line %d", pfx, current,
5610 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005611 break;
5612
5613 case ISN_PROF_END:
5614 smsg("%s%4d PROFILE END", pfx, current);
5615 break;
5616
Bram Moolenaare99d4222021-06-13 14:01:26 +02005617 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005618 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5619 iptr->isn_arg.debug.dbg_break_lnum + 1,
5620 iptr->isn_lnum,
5621 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005622 break;
5623
Bram Moolenaar4c137212021-04-19 16:48:48 +02005624 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5625 iptr->isn_arg.unpack.unp_count,
5626 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5627 break;
5628 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5629 iptr->isn_arg.shuffle.shfl_item,
5630 iptr->isn_arg.shuffle.shfl_up);
5631 break;
5632 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5633
5634 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5635 return;
5636 }
5637
5638 out_flush(); // output one line at a time
5639 ui_breakcheck();
5640 if (got_int)
5641 break;
5642 }
5643}
5644
5645/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005646 * Handle command line completion for the :disassemble command.
5647 */
5648 void
5649set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5650{
5651 char_u *p;
5652
5653 // Default: expand user functions, "debug" and "profile"
5654 xp->xp_context = EXPAND_DISASSEMBLE;
5655 xp->xp_pattern = arg;
5656
5657 // first argument already typed: only user function names
5658 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5659 {
5660 xp->xp_context = EXPAND_USER_FUNC;
5661 xp->xp_pattern = skipwhite(p);
5662 }
5663}
5664
5665/*
5666 * Function given to ExpandGeneric() to obtain the list of :disassemble
5667 * arguments.
5668 */
5669 char_u *
5670get_disassemble_argument(expand_T *xp, int idx)
5671{
5672 if (idx == 0)
5673 return (char_u *)"debug";
5674 if (idx == 1)
5675 return (char_u *)"profile";
5676 return get_user_func_name(xp, idx - 2);
5677}
5678
5679/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005680 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005681 * We don't really need this at runtime, but we do have tests that require it,
5682 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683 */
5684 void
5685ex_disassemble(exarg_T *eap)
5686{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005687 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005688 char_u *fname;
5689 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690 dfunc_T *dfunc;
5691 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005692 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005693 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005694 compiletype_T compile_type = CT_NONE;
5695
5696 if (STRNCMP(arg, "profile", 7) == 0)
5697 {
5698 compile_type = CT_PROFILE;
5699 arg = skipwhite(arg + 7);
5700 }
5701 else if (STRNCMP(arg, "debug", 5) == 0)
5702 {
5703 compile_type = CT_DEBUG;
5704 arg = skipwhite(arg + 5);
5705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005707 if (STRNCMP(arg, "<lambda>", 8) == 0)
5708 {
5709 arg += 8;
5710 (void)getdigits(&arg);
5711 fname = vim_strnsave(eap->arg, arg - eap->arg);
5712 }
5713 else
5714 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005715 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005716 if (fname == NULL)
5717 {
5718 semsg(_(e_invarg2), eap->arg);
5719 return;
5720 }
5721
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005722 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005723 if (ufunc == NULL)
5724 {
5725 char_u *p = untrans_function_name(fname);
5726
5727 if (p != NULL)
5728 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005729 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005730 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005731 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005732 if (ufunc == NULL)
5733 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005734 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005735 return;
5736 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005737 if (func_needs_compiling(ufunc, compile_type)
5738 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005739 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005740 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005742 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743 return;
5744 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005745 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746
5747 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005748 switch (compile_type)
5749 {
5750 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005751#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005752 instr = dfunc->df_instr_prof;
5753 instr_count = dfunc->df_instr_prof_count;
5754 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005755#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005756 // FALLTHROUGH
5757 case CT_NONE:
5758 instr = dfunc->df_instr;
5759 instr_count = dfunc->df_instr_count;
5760 break;
5761 case CT_DEBUG:
5762 instr = dfunc->df_instr_debug;
5763 instr_count = dfunc->df_instr_debug_count;
5764 break;
5765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766
Bram Moolenaar4c137212021-04-19 16:48:48 +02005767 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005768}
5769
5770/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005771 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005772 * list, etc. Mostly like what JavaScript does, except that empty list and
5773 * empty dictionary are FALSE.
5774 */
5775 int
5776tv2bool(typval_T *tv)
5777{
5778 switch (tv->v_type)
5779 {
5780 case VAR_NUMBER:
5781 return tv->vval.v_number != 0;
5782 case VAR_FLOAT:
5783#ifdef FEAT_FLOAT
5784 return tv->vval.v_float != 0.0;
5785#else
5786 break;
5787#endif
5788 case VAR_PARTIAL:
5789 return tv->vval.v_partial != NULL;
5790 case VAR_FUNC:
5791 case VAR_STRING:
5792 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5793 case VAR_LIST:
5794 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5795 case VAR_DICT:
5796 return tv->vval.v_dict != NULL
5797 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5798 case VAR_BOOL:
5799 case VAR_SPECIAL:
5800 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5801 case VAR_JOB:
5802#ifdef FEAT_JOB_CHANNEL
5803 return tv->vval.v_job != NULL;
5804#else
5805 break;
5806#endif
5807 case VAR_CHANNEL:
5808#ifdef FEAT_JOB_CHANNEL
5809 return tv->vval.v_channel != NULL;
5810#else
5811 break;
5812#endif
5813 case VAR_BLOB:
5814 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5815 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005816 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005817 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005818 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005819 break;
5820 }
5821 return FALSE;
5822}
5823
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005824 void
5825emsg_using_string_as(typval_T *tv, int as_number)
5826{
5827 semsg(_(as_number ? e_using_string_as_number_str
5828 : e_using_string_as_bool_str),
5829 tv->vval.v_string == NULL
5830 ? (char_u *)"" : tv->vval.v_string);
5831}
5832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833/*
5834 * If "tv" is a string give an error and return FAIL.
5835 */
5836 int
5837check_not_string(typval_T *tv)
5838{
5839 if (tv->v_type == VAR_STRING)
5840 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005841 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 clear_tv(tv);
5843 return FAIL;
5844 }
5845 return OK;
5846}
5847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848
5849#endif // FEAT_EVAL