blob: 9fc942d3796b46cb96cd61fb243dca6c51bee339 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200140 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
155 static void
156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
168/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100170 * This adds a stack frame and sets the instruction pointer to the start of the
171 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200172 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 *
174 * Stack has:
175 * - current arguments (already there)
176 * - omitted optional argument (default values) added here
177 * - stack frame:
178 * - pointer to calling function
179 * - Index of next instruction in calling function
180 * - previous frame pointer
181 * - reserved space for local variables
182 */
183 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100184call_dfunc(
185 int cdf_idx,
186 partial_T *pt,
187 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100188 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100190 int argcount = argcount_arg;
191 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
192 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200193 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100194 int arg_to_add;
195 int vararg_count = 0;
196 int varcount;
197 int idx;
198 estack_T *entry;
199 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200200 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100201
202 if (dfunc->df_deleted)
203 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100204 // don't use ufunc->uf_name, it may have been freed
205 emsg_funcname(e_func_deleted,
206 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 return FAIL;
208 }
209
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100210#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100211 if (do_profiling == PROF_YES)
212 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200213 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100214 {
215 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
216 + profile_info_ga.ga_len;
217 ++profile_info_ga.ga_len;
218 CLEAR_POINTER(info);
219 profile_may_start_func(info, ufunc,
220 (((dfunc_T *)def_functions.ga_data)
221 + ectx->ec_dfunc_idx)->df_ufunc);
222 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100223 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100224#endif
225
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200226 // Update uf_has_breakpoint if needed.
227 update_has_breakpoint(ufunc);
228
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200229 // When debugging and using "cont" switches to the not-debugged
230 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200231 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
232 {
233 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
234
235 // compile_def_function() may cause def_functions.ga_data to change
236 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
237 }
238 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200239 {
240 if (did_emsg_cumul + did_emsg == did_emsg_before)
241 semsg(_(e_function_is_not_compiled_str),
242 printable_func_name(ufunc));
243 return FAIL;
244 }
245
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200246 if (ufunc->uf_va_name != NULL)
247 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200248 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200249 // Stack at time of call with 2 varargs:
250 // normal_arg
251 // optional_arg
252 // vararg_1
253 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200254 // After creating the list:
255 // normal_arg
256 // optional_arg
257 // vararg-list
258 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200259 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200260 // After creating the list
261 // normal_arg
262 // (space for optional_arg)
263 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200264 vararg_count = argcount - ufunc->uf_args.ga_len;
265 if (vararg_count < 0)
266 vararg_count = 0;
267 else
268 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200269 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200270 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200271
272 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200273 }
274
Bram Moolenaarfe270812020-04-11 22:31:27 +0200275 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200276 if (arg_to_add < 0)
277 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200278 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200279 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200280 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200281 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200282 return FAIL;
283 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200284
285 // Reserve space for:
286 // - missing arguments
287 // - stack frame
288 // - local variables
289 // - if needed: a counter for number of closures created in
290 // ectx->ec_funcrefs.
291 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200292 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 return FAIL;
294
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100295 // If depth of calling is getting too high, don't execute the function.
296 if (funcdepth_increment() == FAIL)
297 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200298 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100299
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100300 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200301 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100302 {
303 floc = ALLOC_ONE(funclocal_T);
304 if (floc == NULL)
305 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200306 *floc = ectx->ec_funclocal;
307 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100308 }
309
Bram Moolenaarfe270812020-04-11 22:31:27 +0200310 // Move the vararg-list to below the missing optional arguments.
311 if (vararg_count > 0 && arg_to_add > 0)
312 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100313
314 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200315 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200316 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200317 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318
319 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100320 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
321 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200322 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200323 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
324 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100325 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100326 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200327 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328
329 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200330 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200332 if (dfunc->df_has_closure)
333 {
334 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
335
336 tv->v_type = VAR_NUMBER;
337 tv->vval.v_number = 0;
338 }
339 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100341 if (pt != NULL || ufunc->uf_partial != NULL
342 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100343 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200344 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100345
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200346 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100347 return FAIL;
348 if (pt != NULL)
349 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200350 ref->or_outer = &pt->pt_outer;
351 ++pt->pt_refcount;
352 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100353 }
354 else if (ufunc->uf_partial != NULL)
355 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200356 ref->or_outer = &ufunc->uf_partial->pt_outer;
357 ++ufunc->uf_partial->pt_refcount;
358 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100359 }
360 else
361 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200362 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200363 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200364 {
365 vim_free(ref);
366 return FAIL;
367 }
368 ref->or_outer_allocated = TRUE;
369 ref->or_outer->out_stack = &ectx->ec_stack;
370 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
371 if (ectx->ec_outer_ref != NULL)
372 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100373 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200374 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100375 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100376 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200377 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100378
Bram Moolenaarc970e422021-03-17 15:03:04 +0100379 ++ufunc->uf_calls;
380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100381 // Set execution state to the start of the called function.
382 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100383 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100384 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200385 if (entry != NULL)
386 {
387 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200388 // Save the current context so it can be restored on return.
389 entry->es_save_sctx = current_sctx;
390 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200391 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100392
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200393 // Start execution at the first instruction.
394 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395
396 return OK;
397}
398
399// Get pointer to item in the stack.
400#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
401
402/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 * Used when returning from a function: Check if any closure is still
404 * referenced. If so then move the arguments and variables to a separate piece
405 * of stack to be used when the closure is called.
406 * When "free_arguments" is TRUE the arguments are to be freed.
407 * Returns FAIL when out of memory.
408 */
409 static int
410handle_closure_in_use(ectx_T *ectx, int free_arguments)
411{
412 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
413 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200414 int argcount;
415 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200416 int idx;
417 typval_T *tv;
418 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200419 garray_T *gap = &ectx->ec_funcrefs;
420 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200421
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200422 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200423 return OK; // function was freed
424 if (dfunc->df_has_closure == 0)
425 return OK; // no closures
426 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
427 closure_count = tv->vval.v_number;
428 if (closure_count == 0)
429 return OK; // no funcrefs created
430
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200431 argcount = ufunc_argcount(dfunc->df_ufunc);
432 top = ectx->ec_frame_idx - argcount;
433
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200434 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200435 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200436 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200437 partial_T *pt;
438 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200439
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200440 if (off < 0)
441 continue; // count is off or already done
442 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200443 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200444 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200445 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200446 int i;
447
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200448 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200449 // unreferenced on return.
450 for (i = 0; i < dfunc->df_varcount; ++i)
451 {
452 typval_T *stv = STACK_TV(ectx->ec_frame_idx
453 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200454 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200455 --refcount;
456 }
457 if (refcount > 1)
458 {
459 closure_in_use = TRUE;
460 break;
461 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200462 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200463 }
464
465 if (closure_in_use)
466 {
467 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
468 typval_T *stack;
469
470 // A closure is using the arguments and/or local variables.
471 // Move them to the called function.
472 if (funcstack == NULL)
473 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200474 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
475 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200476 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
477 funcstack->fs_ga.ga_data = stack;
478 if (stack == NULL)
479 {
480 vim_free(funcstack);
481 return FAIL;
482 }
483
484 // Move or copy the arguments.
485 for (idx = 0; idx < argcount; ++idx)
486 {
487 tv = STACK_TV(top + idx);
488 if (free_arguments)
489 {
490 *(stack + idx) = *tv;
491 tv->v_type = VAR_UNKNOWN;
492 }
493 else
494 copy_tv(tv, stack + idx);
495 }
496 // Move the local variables.
497 for (idx = 0; idx < dfunc->df_varcount; ++idx)
498 {
499 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200500
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200501 // A partial created for a local function, that is also used as a
502 // local variable, has a reference count for the variable, thus
503 // will never go down to zero. When all these refcounts are one
504 // then the funcstack is unused. We need to count how many we have
505 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200506 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
507 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200508 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200509
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200510 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200511 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
512 gap->ga_len - closure_count + i])
513 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200514 }
515
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200516 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200517 tv->v_type = VAR_UNKNOWN;
518 }
519
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200520 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200521 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200522 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
523 - closure_count + idx];
524 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200525 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200526 ++funcstack->fs_refcount;
527 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100528 pt->pt_outer.out_stack = &funcstack->fs_ga;
529 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 }
531 }
532 }
533
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534 for (idx = 0; idx < closure_count; ++idx)
535 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
536 - closure_count + idx]);
537 gap->ga_len -= closure_count;
538 if (gap->ga_len == 0)
539 ga_clear(gap);
540
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200541 return OK;
542}
543
544/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200545 * Called when a partial is freed or its reference count goes down to one. The
546 * funcstack may be the only reference to the partials in the local variables.
547 * Go over all of them, the funcref and can be freed if all partials
548 * referencing the funcstack have a reference count of one.
549 */
550 void
551funcstack_check_refcount(funcstack_T *funcstack)
552{
553 int i;
554 garray_T *gap = &funcstack->fs_ga;
555 int done = 0;
556
557 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
558 return;
559 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
560 {
561 typval_T *tv = ((typval_T *)gap->ga_data) + i;
562
563 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
564 && tv->vval.v_partial->pt_funcstack == funcstack
565 && tv->vval.v_partial->pt_refcount == 1)
566 ++done;
567 }
568 if (done == funcstack->fs_min_refcount)
569 {
570 typval_T *stack = gap->ga_data;
571
572 // All partials referencing the funcstack have a reference count of
573 // one, thus the funcstack is no longer of use.
574 for (i = 0; i < gap->ga_len; ++i)
575 clear_tv(stack + i);
576 vim_free(stack);
577 vim_free(funcstack);
578 }
579}
580
581/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 * Return from the current function.
583 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200584 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200585func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100588 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200589 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
590 + ectx->ec_dfunc_idx;
591 int argcount = ufunc_argcount(dfunc->df_ufunc);
592 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200593 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100594 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
595 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200596 funclocal_T *floc;
597#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100598 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
599 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600
Bram Moolenaar12d26532021-02-19 19:13:21 +0100601 if (do_profiling == PROF_YES)
602 {
603 ufunc_T *caller = prev_dfunc->df_ufunc;
604
605 if (dfunc->df_ufunc->uf_profiling
606 || (caller != NULL && caller->uf_profiling))
607 {
608 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
609 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
610 --profile_info_ga.ga_len;
611 }
612 }
613#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100614 // TODO: when is it safe to delete the function when it is no longer used?
615 --dfunc->df_ufunc->uf_calls;
616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200618 entry = estack_pop();
619 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200620 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622 if (handle_closure_in_use(ectx, TRUE) == FAIL)
623 return FAIL;
624
625 // Clear the arguments.
626 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
627 clear_tv(STACK_TV(idx));
628
629 // Clear local variables and temp values, but not the return value.
630 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100631 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100633
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100634 // The return value should be on top of the stack. However, when aborting
635 // it may not be there and ec_frame_idx is the top of the stack.
636 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100637 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100638 ret_idx = 0;
639
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200640 if (ectx->ec_outer_ref != NULL)
641 {
642 if (ectx->ec_outer_ref->or_outer_allocated)
643 vim_free(ectx->ec_outer_ref->or_outer);
644 partial_unref(ectx->ec_outer_ref->or_partial);
645 vim_free(ectx->ec_outer_ref);
646 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100647
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100648 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100649 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100650 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
651 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200652 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
653 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200654 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100655 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100656 floc = (void *)STACK_TV(ectx->ec_frame_idx
657 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200658 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100659 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
660 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200661
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100662 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200663 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100664 else
665 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200666 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100667 vim_free(floc);
668 }
669
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100670 if (ret_idx > 0)
671 {
672 // Reset the stack to the position before the call, with a spot for the
673 // return value, moved there from above the frame.
674 ectx->ec_stack.ga_len = top + 1;
675 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
676 }
677 else
678 // Reset the stack to the position before the call.
679 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200680
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100681 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200682 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200683 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684}
685
686#undef STACK_TV
687
688/*
689 * Prepare arguments and rettv for calling a builtin or user function.
690 */
691 static int
692call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
693{
694 int idx;
695 typval_T *tv;
696
697 // Move arguments from bottom of the stack to argvars[] and add terminator.
698 for (idx = 0; idx < argcount; ++idx)
699 argvars[idx] = *STACK_TV_BOT(idx - argcount);
700 argvars[argcount].v_type = VAR_UNKNOWN;
701
702 // Result replaces the arguments on the stack.
703 if (argcount > 0)
704 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200705 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 return FAIL;
707 else
708 ++ectx->ec_stack.ga_len;
709
710 // Default return value is zero.
711 tv = STACK_TV_BOT(-1);
712 tv->v_type = VAR_NUMBER;
713 tv->vval.v_number = 0;
714
715 return OK;
716}
717
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200718// Ugly global to avoid passing the execution context around through many
719// layers.
720static ectx_T *current_ectx = NULL;
721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722/*
723 * Call a builtin function by index.
724 */
725 static int
726call_bfunc(int func_idx, int argcount, ectx_T *ectx)
727{
728 typval_T argvars[MAX_FUNC_ARGS];
729 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100730 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200731 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200732 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733
734 if (call_prepare(argcount, argvars, ectx) == FAIL)
735 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200736 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200738 // Call the builtin function. Set "current_ectx" so that when it
739 // recursively invokes call_def_function() a closure context can be set.
740 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200742 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200743 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744
745 // Clear the arguments.
746 for (idx = 0; idx < argcount; ++idx)
747 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200748
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100749 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200750 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 return OK;
752}
753
754/*
755 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100756 * If the function is compiled this will add a stack frame and set the
757 * instruction pointer at the start of the function.
758 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200759 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100760 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 */
762 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100763call_ufunc(
764 ufunc_T *ufunc,
765 partial_T *pt,
766 int argcount,
767 ectx_T *ectx,
768 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769{
770 typval_T argvars[MAX_FUNC_ARGS];
771 funcexe_T funcexe;
772 int error;
773 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100774 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200775 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776
Bram Moolenaare99d4222021-06-13 14:01:26 +0200777 if (func_needs_compiling(ufunc, compile_type)
778 && compile_def_function(ufunc, FALSE, compile_type, NULL)
779 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200780 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200781 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100782 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100783 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100784 if (error != FCERR_UNKNOWN)
785 {
786 if (error == FCERR_TOOMANY)
787 semsg(_(e_toomanyarg), ufunc->uf_name);
788 else
789 semsg(_(e_toofewarg), ufunc->uf_name);
790 return FAIL;
791 }
792
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100793 // The function has been compiled, can call it quickly. For a function
794 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100795 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100796 if (iptr != NULL)
797 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100798 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100799 iptr->isn_type = ISN_DCALL;
800 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
801 iptr->isn_arg.dfunc.cdf_argcount = argcount;
802 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200803 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805
806 if (call_prepare(argcount, argvars, ectx) == FAIL)
807 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200808 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 funcexe.evaluate = TRUE;
810
811 // Call the user function. Result goes in last position on the stack.
812 // TODO: add selfdict if there is one
813 error = call_user_func_check(ufunc, argcount, argvars,
814 STACK_TV_BOT(-1), &funcexe, NULL);
815
816 // Clear the arguments.
817 for (idx = 0; idx < argcount; ++idx)
818 clear_tv(&argvars[idx]);
819
820 if (error != FCERR_NONE)
821 {
822 user_func_error(error, ufunc->uf_name);
823 return FAIL;
824 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100825 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200826 // Error other than from calling the function itself.
827 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828 return OK;
829}
830
831/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100832 * If command modifiers were applied restore them.
833 */
834 static void
835may_restore_cmdmod(funclocal_T *funclocal)
836{
837 if (funclocal->floc_restore_cmdmod)
838 {
839 cmdmod.cmod_filter_regmatch.regprog = NULL;
840 undo_cmdmod(&cmdmod);
841 cmdmod = funclocal->floc_save_cmdmod;
842 funclocal->floc_restore_cmdmod = FALSE;
843 }
844}
845
846/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200847 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
848 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200849 */
850 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200851vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200852{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200853 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200854}
855
856/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 * Execute a function by "name".
858 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100859 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 * Returns FAIL if not found without an error message.
861 */
862 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100863call_by_name(
864 char_u *name,
865 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100866 ectx_T *ectx,
867 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868{
869 ufunc_T *ufunc;
870
871 if (builtin_function(name, -1))
872 {
873 int func_idx = find_internal_func(name);
874
875 if (func_idx < 0)
876 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200877 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 return FAIL;
879 return call_bfunc(func_idx, argcount, ectx);
880 }
881
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200882 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200883
884 if (ufunc == NULL)
885 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200886 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +0200887
888 if (script_autoload(name, TRUE))
889 // loaded a package, search for the function again
890 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200891
892 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +0200893 return FAIL; // bail out if loading the script caused an error
894 }
895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100897 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200898 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100899 {
900 int i;
901 typval_T *argv = STACK_TV_BOT(0) - argcount;
902
903 // The function can change at runtime, check that the argument
904 // types are correct.
905 for (i = 0; i < argcount; ++i)
906 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100907 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100908
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200909 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100910 type = ufunc->uf_arg_types[i];
911 else if (ufunc->uf_va_type != NULL)
912 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100913 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200914 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100915 return FAIL;
916 }
917 }
918
Bram Moolenaar4c137212021-04-19 16:48:48 +0200919 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921
922 return FAIL;
923}
924
925 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100926call_partial(
927 typval_T *tv,
928 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100929 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200931 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200932 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100934 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935
936 if (tv->v_type == VAR_PARTIAL)
937 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200938 partial_T *pt = tv->vval.v_partial;
939 int i;
940
941 if (pt->pt_argc > 0)
942 {
943 // Make space for arguments from the partial, shift the "argcount"
944 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +0200945 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +0200946 return FAIL;
947 for (i = 1; i <= argcount; ++i)
948 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
949 ectx->ec_stack.ga_len += pt->pt_argc;
950 argcount += pt->pt_argc;
951
952 // copy the arguments from the partial onto the stack
953 for (i = 0; i < pt->pt_argc; ++i)
954 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956
957 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200958 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 name = pt->pt_name;
961 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200962 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200964 if (name != NULL)
965 {
966 char_u fname_buf[FLEN_FIXED + 1];
967 char_u *tofree = NULL;
968 int error = FCERR_NONE;
969 char_u *fname;
970
971 // May need to translate <SNR>123_ to K_SNR.
972 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
973 if (error != FCERR_NONE)
974 res = FAIL;
975 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200976 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200977 vim_free(tofree);
978 }
979
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100980 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 {
982 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200983 semsg(_(e_unknownfunc),
984 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 return FAIL;
986 }
987 return OK;
988}
989
990/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200991 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
992 * TRUE.
993 */
994 static int
995error_if_locked(int lock, char *error)
996{
997 if (lock & (VAR_LOCKED | VAR_FIXED))
998 {
999 emsg(_(error));
1000 return TRUE;
1001 }
1002 return FALSE;
1003}
1004
1005/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001006 * Give an error if "tv" is not a number and return FAIL.
1007 */
1008 static int
1009check_for_number(typval_T *tv)
1010{
1011 if (tv->v_type != VAR_NUMBER)
1012 {
1013 semsg(_(e_expected_str_but_got_str),
1014 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1015 return FAIL;
1016 }
1017 return OK;
1018}
1019
1020/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001021 * Store "tv" in variable "name".
1022 * This is for s: and g: variables.
1023 */
1024 static void
1025store_var(char_u *name, typval_T *tv)
1026{
1027 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001028 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001029
Bram Moolenaard877a572021-04-01 19:42:48 +02001030 if (tv->v_lock)
1031 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001032 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001033 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001034 restore_funccal();
1035}
1036
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001037/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001038 * Convert "tv" to a string.
1039 * Return FAIL if not allowed.
1040 */
1041 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001042do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001043{
1044 if (tv->v_type != VAR_STRING)
1045 {
1046 char_u *str;
1047
1048 if (is_2string_any)
1049 {
1050 switch (tv->v_type)
1051 {
1052 case VAR_SPECIAL:
1053 case VAR_BOOL:
1054 case VAR_NUMBER:
1055 case VAR_FLOAT:
1056 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001057
1058 case VAR_LIST:
1059 if (tolerant)
1060 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001061 char_u *s, *e, *p;
1062 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001063
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001064 ga_init2(&ga, sizeof(char_u *), 1);
1065
1066 // Convert to NL separated items, then
1067 // escape the items and replace the NL with
1068 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001069 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001070 if (str == NULL)
1071 return FAIL;
1072 s = str;
1073 while ((e = vim_strchr(s, '\n')) != NULL)
1074 {
1075 *e = NUL;
1076 p = vim_strsave_fnameescape(s, FALSE);
1077 if (p != NULL)
1078 {
1079 ga_concat(&ga, p);
1080 ga_concat(&ga, (char_u *)" ");
1081 vim_free(p);
1082 }
1083 s = e + 1;
1084 }
1085 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001086 clear_tv(tv);
1087 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001088 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001089 return OK;
1090 }
1091 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001092 default: to_string_error(tv->v_type);
1093 return FAIL;
1094 }
1095 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001096 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001097 clear_tv(tv);
1098 tv->v_type = VAR_STRING;
1099 tv->vval.v_string = str;
1100 }
1101 return OK;
1102}
1103
1104/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001105 * When the value of "sv" is a null list of dict, allocate it.
1106 */
1107 static void
1108allocate_if_null(typval_T *tv)
1109{
1110 switch (tv->v_type)
1111 {
1112 case VAR_LIST:
1113 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001114 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001115 break;
1116 case VAR_DICT:
1117 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001118 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001119 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001120 case VAR_BLOB:
1121 if (tv->vval.v_blob == NULL)
1122 (void)rettv_blob_alloc(tv);
1123 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001124 default:
1125 break;
1126 }
1127}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001128
Bram Moolenaare7525c52021-01-09 13:20:37 +01001129/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001130 * Return the character "str[index]" where "index" is the character index,
1131 * including composing characters.
1132 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001133 */
1134 char_u *
1135char_from_string(char_u *str, varnumber_T index)
1136{
1137 size_t nbyte = 0;
1138 varnumber_T nchar = index;
1139 size_t slen;
1140
1141 if (str == NULL)
1142 return NULL;
1143 slen = STRLEN(str);
1144
Bram Moolenaarff871402021-03-26 13:34:05 +01001145 // Do the same as for a list: a negative index counts from the end.
1146 // Optimization to check the first byte to be below 0x80 (and no composing
1147 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001148 if (index < 0)
1149 {
1150 int clen = 0;
1151
1152 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001153 {
1154 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1155 ++nbyte;
1156 else if (enc_utf8)
1157 nbyte += utfc_ptr2len(str + nbyte);
1158 else
1159 nbyte += mb_ptr2len(str + nbyte);
1160 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001161 nchar = clen + index;
1162 if (nchar < 0)
1163 // unlike list: index out of range results in empty string
1164 return NULL;
1165 }
1166
1167 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001168 {
1169 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1170 ++nbyte;
1171 else if (enc_utf8)
1172 nbyte += utfc_ptr2len(str + nbyte);
1173 else
1174 nbyte += mb_ptr2len(str + nbyte);
1175 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001176 if (nbyte >= slen)
1177 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001178 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001179}
1180
1181/*
1182 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001183 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001184 * If going over the end return "str_len".
1185 * If "idx" is negative count from the end, -1 is the last character.
1186 * When going over the start return -1.
1187 */
1188 static long
1189char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1190{
1191 varnumber_T nchar = idx;
1192 size_t nbyte = 0;
1193
1194 if (nchar >= 0)
1195 {
1196 while (nchar > 0 && nbyte < str_len)
1197 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001198 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001199 --nchar;
1200 }
1201 }
1202 else
1203 {
1204 nbyte = str_len;
1205 while (nchar < 0 && nbyte > 0)
1206 {
1207 --nbyte;
1208 nbyte -= mb_head_off(str, str + nbyte);
1209 ++nchar;
1210 }
1211 if (nchar < 0)
1212 return -1;
1213 }
1214 return (long)nbyte;
1215}
1216
1217/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001218 * Return the slice "str[first : last]" using character indexes. Composing
1219 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001220 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001221 * Return NULL when the result is empty.
1222 */
1223 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001224string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001225{
1226 long start_byte, end_byte;
1227 size_t slen;
1228
1229 if (str == NULL)
1230 return NULL;
1231 slen = STRLEN(str);
1232 start_byte = char_idx2byte(str, slen, first);
1233 if (start_byte < 0)
1234 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001235 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001236 end_byte = (long)slen;
1237 else
1238 {
1239 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001240 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001241 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001242 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001243 }
1244
1245 if (start_byte >= (long)slen || end_byte <= start_byte)
1246 return NULL;
1247 return vim_strnsave(str + start_byte, end_byte - start_byte);
1248}
1249
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001250/*
1251 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1252 * When "dfunc_idx" is negative don't give an error.
1253 * Returns NULL for an error.
1254 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001255 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001256get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001257{
1258 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001259 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1260 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001261 svar_T *sv;
1262
1263 if (sref->sref_seq != si->sn_script_seq)
1264 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001265 // The script was reloaded after the function was compiled, the
1266 // script_idx may not be valid.
1267 if (dfunc != NULL)
1268 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1269 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001270 return NULL;
1271 }
1272 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001273 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001274 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001275 if (dfunc != NULL)
1276 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001277 return NULL;
1278 }
1279 return sv;
1280}
1281
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001282/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001283 * Function passed to do_cmdline() for splitting a script joined by NL
1284 * characters.
1285 */
1286 static char_u *
1287get_split_sourceline(
1288 int c UNUSED,
1289 void *cookie,
1290 int indent UNUSED,
1291 getline_opt_T options UNUSED)
1292{
1293 source_cookie_T *sp = (source_cookie_T *)cookie;
1294 char_u *p;
1295 char_u *line;
1296
1297 if (*sp->nextline == NUL)
1298 return NULL;
1299 p = vim_strchr(sp->nextline, '\n');
1300 if (p == NULL)
1301 {
1302 line = vim_strsave(sp->nextline);
1303 sp->nextline += STRLEN(sp->nextline);
1304 }
1305 else
1306 {
1307 line = vim_strnsave(sp->nextline, p - sp->nextline);
1308 sp->nextline = p + 1;
1309 }
1310 return line;
1311}
1312
1313/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 * Execute a function by "name".
1315 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001316 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 */
1318 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001319call_eval_func(
1320 char_u *name,
1321 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001322 ectx_T *ectx,
1323 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324{
Bram Moolenaared677f52020-08-12 16:38:10 +02001325 int called_emsg_before = called_emsg;
1326 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
Bram Moolenaar4c137212021-04-19 16:48:48 +02001328 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001329 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001331 dictitem_T *v;
1332
1333 v = find_var(name, NULL, FALSE);
1334 if (v == NULL)
1335 {
1336 semsg(_(e_unknownfunc), name);
1337 return FAIL;
1338 }
1339 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1340 {
1341 semsg(_(e_unknownfunc), name);
1342 return FAIL;
1343 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001344 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001346 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347}
1348
1349/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001350 * When a function reference is used, fill a partial with the information
1351 * needed, especially when it is used as a closure.
1352 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001353 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001354fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1355{
1356 pt->pt_func = ufunc;
1357 pt->pt_refcount = 1;
1358
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001359 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001360 {
1361 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1362 + ectx->ec_dfunc_idx;
1363
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001364 // The closure may need to find arguments and local variables in the
1365 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001366 pt->pt_outer.out_stack = &ectx->ec_stack;
1367 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001368 if (ectx->ec_outer_ref != NULL)
1369 {
1370 // The current context already has a context, link to that one.
1371 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1372 if (ectx->ec_outer_ref->or_partial != NULL)
1373 {
1374 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1375 ++pt->pt_outer.out_up_partial->pt_refcount;
1376 }
1377 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001378
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001379 // If this function returns and the closure is still being used, we
1380 // need to make a copy of the context (arguments and local variables).
1381 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001382 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001383 {
1384 vim_free(pt);
1385 return FAIL;
1386 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001387 // Extra variable keeps the count of closures created in the current
1388 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001389 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1390 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1391
1392 ((partial_T **)ectx->ec_funcrefs.ga_data)
1393 [ectx->ec_funcrefs.ga_len] = pt;
1394 ++pt->pt_refcount;
1395 ++ectx->ec_funcrefs.ga_len;
1396 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001397 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001398 return OK;
1399}
1400
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001401/*
1402 * Execute iptr->isn_arg.string as an Ex command.
1403 */
1404 static int
1405exec_command(isn_T *iptr)
1406{
1407 source_cookie_T cookie;
1408
1409 SOURCING_LNUM = iptr->isn_lnum;
1410 // Pass getsourceline to get an error for a missing ":end"
1411 // command.
1412 CLEAR_FIELD(cookie);
1413 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1414 if (do_cmdline(iptr->isn_arg.string,
1415 getsourceline, &cookie,
1416 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1417 || did_emsg)
1418 return FAIL;
1419 return OK;
1420}
1421
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001422// used for v_instr of typval of VAR_INSTR
1423struct instr_S {
1424 ectx_T *instr_ectx;
1425 isn_T *instr_instr;
1426};
1427
Bram Moolenaar4c137212021-04-19 16:48:48 +02001428// used for substitute_instr
1429typedef struct subs_expr_S {
1430 ectx_T *subs_ectx;
1431 isn_T *subs_instr;
1432 int subs_status;
1433} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434
1435// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001436#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437
1438// Get pointer to item at the bottom of the stack, -1 is the bottom.
1439#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001440#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001442// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001443#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001445// Set when calling do_debug().
1446static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001447static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001448
1449/*
1450 * When debugging lookup "name" and return the typeval.
1451 * When not found return NULL.
1452 */
1453 typval_T *
1454lookup_debug_var(char_u *name)
1455{
1456 int idx;
1457 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001458 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001459 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001460 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001461
1462 if (ectx == NULL)
1463 return NULL;
1464 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1465
1466 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001467 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001468 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001469 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001470 return STACK_TV_VAR(idx);
1471 }
1472
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001473 // Go through argument names.
1474 ufunc = dfunc->df_ufunc;
1475 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1476 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1477 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1478 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1479 - varargs_off + idx);
1480 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1481 return STACK_TV(ectx->ec_frame_idx - 1);
1482
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001483 return NULL;
1484}
1485
Bram Moolenaar26a44842021-09-02 18:49:06 +02001486/*
1487 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1488 * breakpoint was set in that function or when there is any expression.
1489 */
1490 int
1491may_break_in_function(ufunc_T *ufunc)
1492{
1493 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1494}
1495
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001496 static void
1497handle_debug(isn_T *iptr, ectx_T *ectx)
1498{
1499 char_u *line;
1500 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1501 + ectx->ec_dfunc_idx)->df_ufunc;
1502 isn_T *ni;
1503 int end_lnum = iptr->isn_lnum;
1504 garray_T ga;
1505 int lnum;
1506
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001507 if (ex_nesting_level > debug_break_level)
1508 {
1509 linenr_T breakpoint;
1510
Bram Moolenaar26a44842021-09-02 18:49:06 +02001511 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001512 return;
1513
1514 // check for the next breakpoint if needed
1515 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001516 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001517 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1518 return;
1519 }
1520
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001521 SOURCING_LNUM = iptr->isn_lnum;
1522 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001523 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001524
1525 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1526 if (ni->isn_type == ISN_DEBUG
1527 || ni->isn_type == ISN_RETURN
1528 || ni->isn_type == ISN_RETURN_VOID)
1529 {
1530 end_lnum = ni->isn_lnum;
1531 break;
1532 }
1533
1534 if (end_lnum > iptr->isn_lnum)
1535 {
1536 ga_init2(&ga, sizeof(char_u *), 10);
1537 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001538 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001539 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001540
Bram Moolenaar303215d2021-07-07 20:10:43 +02001541 if (p == NULL)
1542 continue; // left over from continuation line
1543 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001544 if (*p == '#')
1545 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001546 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001547 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1548 if (STRNCMP(p, "def ", 4) == 0)
1549 break;
1550 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001551 line = ga_concat_strings(&ga, " ");
1552 vim_free(ga.ga_data);
1553 }
1554 else
1555 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001556
Dominique Pelle6e969552021-06-17 13:53:41 +02001557 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001558 debug_context = NULL;
1559
1560 if (end_lnum > iptr->isn_lnum)
1561 vim_free(line);
1562}
1563
Bram Moolenaar4c137212021-04-19 16:48:48 +02001564/*
1565 * Execute instructions in execution context "ectx".
1566 * Return OK or FAIL;
1567 */
1568 static int
1569exec_instructions(ectx_T *ectx)
1570{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001571 int ret = FAIL;
1572 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001573
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001574 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001575 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001576
Bram Moolenaarff652882021-05-16 15:24:49 +02001577 // Only catch exceptions in this instruction list.
1578 ectx->ec_trylevel_at_start = trylevel;
1579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 for (;;)
1581 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001582 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001584 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001585
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001586 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001587 {
1588 line_breakcheck();
1589 breakcheck_count = 0;
1590 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001591 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001592 {
1593 // Turn CTRL-C into an exception.
1594 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001595 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001596 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001597 did_throw = TRUE;
1598 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001600 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001601 {
1602 // Turn an error message into an exception.
1603 did_emsg = FALSE;
1604 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001605 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001606 did_throw = TRUE;
1607 *msg_list = NULL;
1608 }
1609
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001610 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001612 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001613 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001614 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615
1616 // An exception jumps to the first catch, finally, or returns from
1617 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001618 while (index > 0)
1619 {
1620 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001621 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001622 break;
1623 // In the catch and finally block of this try we have to go up
1624 // one level.
1625 --index;
1626 trycmd = NULL;
1627 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001628 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001630 if (trycmd->tcd_in_catch)
1631 {
1632 // exception inside ":catch", jump to ":finally" once
1633 ectx->ec_iidx = trycmd->tcd_finally_idx;
1634 trycmd->tcd_finally_idx = 0;
1635 }
1636 else
1637 // jump to first ":catch"
1638 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001639 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001640 did_throw = FALSE; // don't come back here until :endtry
1641 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 }
1643 else
1644 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001645 // Not inside try or need to return from current functions.
1646 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001647 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001648 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001649 tv = STACK_TV_BOT(0);
1650 tv->v_type = VAR_NUMBER;
1651 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001652 ++ectx->ec_stack.ga_len;
1653 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001655 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001656 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001657 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001658 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 goto done;
1660 }
1661
Bram Moolenaar4c137212021-04-19 16:48:48 +02001662 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001663 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 }
1665 continue;
1666 }
1667
Bram Moolenaar4c137212021-04-19 16:48:48 +02001668 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 switch (iptr->isn_type)
1670 {
1671 // execute Ex command line
1672 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001673 if (exec_command(iptr) == FAIL)
1674 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 break;
1676
Bram Moolenaar20677332021-06-06 17:02:53 +02001677 // execute Ex command line split at NL characters.
1678 case ISN_EXEC_SPLIT:
1679 {
1680 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001681 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001682
1683 SOURCING_LNUM = iptr->isn_lnum;
1684 CLEAR_FIELD(cookie);
1685 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1686 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001687 line = get_split_sourceline(0, &cookie, 0, 0);
1688 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001689 get_split_sourceline, &cookie,
1690 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1691 == FAIL
1692 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001693 {
1694 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001695 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001696 }
1697 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001698 }
1699 break;
1700
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001701 // Evaluate an expression with legacy syntax, push it onto the
1702 // stack.
1703 case ISN_LEGACY_EVAL:
1704 {
1705 char_u *arg = iptr->isn_arg.string;
1706 int res;
1707 int save_flags = cmdmod.cmod_flags;
1708
Bram Moolenaar35578162021-08-02 19:10:38 +02001709 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001710 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001711 tv = STACK_TV_BOT(0);
1712 init_tv(tv);
1713 cmdmod.cmod_flags |= CMOD_LEGACY;
1714 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1715 cmdmod.cmod_flags = save_flags;
1716 if (res == FAIL)
1717 goto on_error;
1718 ++ectx->ec_stack.ga_len;
1719 }
1720 break;
1721
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001722 // push typeval VAR_INSTR with instructions to be executed
1723 case ISN_INSTR:
1724 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001725 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001726 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001727 tv = STACK_TV_BOT(0);
1728 tv->vval.v_instr = ALLOC_ONE(instr_T);
1729 if (tv->vval.v_instr == NULL)
1730 goto on_error;
1731 ++ectx->ec_stack.ga_len;
1732
1733 tv->v_type = VAR_INSTR;
1734 tv->vval.v_instr->instr_ectx = ectx;
1735 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1736 }
1737 break;
1738
Bram Moolenaar4c137212021-04-19 16:48:48 +02001739 // execute :substitute with an expression
1740 case ISN_SUBSTITUTE:
1741 {
1742 subs_T *subs = &iptr->isn_arg.subs;
1743 source_cookie_T cookie;
1744 struct subs_expr_S *save_instr = substitute_instr;
1745 struct subs_expr_S subs_instr;
1746 int res;
1747
1748 subs_instr.subs_ectx = ectx;
1749 subs_instr.subs_instr = subs->subs_instr;
1750 subs_instr.subs_status = OK;
1751 substitute_instr = &subs_instr;
1752
1753 SOURCING_LNUM = iptr->isn_lnum;
1754 // This is very much like ISN_EXEC
1755 CLEAR_FIELD(cookie);
1756 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1757 res = do_cmdline(subs->subs_cmd,
1758 getsourceline, &cookie,
1759 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1760 substitute_instr = save_instr;
1761
1762 if (res == FAIL || did_emsg
1763 || subs_instr.subs_status == FAIL)
1764 goto on_error;
1765 }
1766 break;
1767
1768 case ISN_FINISH:
1769 goto done;
1770
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001771 case ISN_REDIRSTART:
1772 // create a dummy entry for var_redir_str()
1773 if (alloc_redir_lval() == FAIL)
1774 goto on_error;
1775
1776 // The output is stored in growarray "redir_ga" until
1777 // redirection ends.
1778 init_redir_ga();
1779 redir_vname = 1;
1780 break;
1781
1782 case ISN_REDIREND:
1783 {
1784 char_u *res = get_clear_redir_ga();
1785
1786 // End redirection, put redirected text on the stack.
1787 clear_redir_lval();
1788 redir_vname = 0;
1789
Bram Moolenaar35578162021-08-02 19:10:38 +02001790 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001791 {
1792 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001793 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001794 }
1795 tv = STACK_TV_BOT(0);
1796 tv->v_type = VAR_STRING;
1797 tv->vval.v_string = res;
1798 ++ectx->ec_stack.ga_len;
1799 }
1800 break;
1801
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001802 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001803#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001804 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1805 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001806#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001807 break;
1808
1809 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001810#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001811 {
1812 exarg_T ea;
1813 int res;
1814
1815 CLEAR_FIELD(ea);
1816 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1817 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1818 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1819 --ectx->ec_stack.ga_len;
1820 tv = STACK_TV_BOT(0);
1821 res = cexpr_core(&ea, tv);
1822 clear_tv(tv);
1823 if (res == FAIL)
1824 goto on_error;
1825 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001826#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001827 break;
1828
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001829 // execute Ex command from pieces on the stack
1830 case ISN_EXECCONCAT:
1831 {
1832 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001833 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001834 int pass;
1835 int i;
1836 char_u *cmd = NULL;
1837 char_u *str;
1838
1839 for (pass = 1; pass <= 2; ++pass)
1840 {
1841 for (i = 0; i < count; ++i)
1842 {
1843 tv = STACK_TV_BOT(i - count);
1844 str = tv->vval.v_string;
1845 if (str != NULL && *str != NUL)
1846 {
1847 if (pass == 2)
1848 STRCPY(cmd + len, str);
1849 len += STRLEN(str);
1850 }
1851 if (pass == 2)
1852 clear_tv(tv);
1853 }
1854 if (pass == 1)
1855 {
1856 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001857 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001858 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001859 len = 0;
1860 }
1861 }
1862
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001863 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001864 do_cmdline_cmd(cmd);
1865 vim_free(cmd);
1866 }
1867 break;
1868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 // execute :echo {string} ...
1870 case ISN_ECHO:
1871 {
1872 int count = iptr->isn_arg.echo.echo_count;
1873 int atstart = TRUE;
1874 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001875 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876
1877 for (idx = 0; idx < count; ++idx)
1878 {
1879 tv = STACK_TV_BOT(idx - count);
1880 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1881 &atstart, &needclr);
1882 clear_tv(tv);
1883 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001884 if (needclr)
1885 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001886 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 }
1888 break;
1889
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001890 // :execute {string} ...
1891 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02001892 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001893 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001894 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001895 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02001896 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001897 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001898 {
1899 int count = iptr->isn_arg.number;
1900 garray_T ga;
1901 char_u buf[NUMBUFLEN];
1902 char_u *p;
1903 int len;
1904 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001905 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001906
1907 ga_init2(&ga, 1, 80);
1908 for (idx = 0; idx < count; ++idx)
1909 {
1910 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001911 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001912 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001913 if (tv->v_type == VAR_CHANNEL
1914 || tv->v_type == VAR_JOB)
1915 {
1916 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001917 semsg(_(e_using_invalid_value_as_string_str),
1918 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001919 break;
1920 }
1921 else
1922 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001923 }
1924 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001925 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001926
1927 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02001928 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01001929 failed = TRUE;
1930 else
1931 {
1932 if (ga.ga_len > 0)
1933 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1934 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1935 ga.ga_len += len;
1936 }
1937 clear_tv(tv);
1938 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001939 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001940 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001941 {
1942 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001943 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001944 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001945
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001946 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001947 {
1948 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001949 {
1950 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001951 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001952 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001953 {
1954 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001955 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001956 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001957 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001958 else
1959 {
1960 msg_sb_eol();
1961 if (iptr->isn_type == ISN_ECHOMSG)
1962 {
1963 msg_attr(ga.ga_data, echo_attr);
1964 out_flush();
1965 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02001966 else if (iptr->isn_type == ISN_ECHOCONSOLE)
1967 {
1968 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
1969 TRUE);
1970 ui_write((char_u *)"\r\n", 2, TRUE);
1971 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001972 else
1973 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001974 SOURCING_LNUM = iptr->isn_lnum;
1975 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001976 }
1977 }
1978 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001979 ga_clear(&ga);
1980 }
1981 break;
1982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 // load local variable or argument
1984 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02001985 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001986 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001988 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 break;
1990
1991 // load v: variable
1992 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02001993 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001994 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001996 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 break;
1998
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001999 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 case ISN_LOADSCRIPT:
2001 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002002 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 svar_T *sv;
2004
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002005 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002006 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002007 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002008 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002009 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002010 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002012 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 }
2014 break;
2015
2016 // load s: variable in old script
2017 case ISN_LOADS:
2018 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002019 hashtab_T *ht = &SCRIPT_VARS(
2020 iptr->isn_arg.loadstore.ls_sid);
2021 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 if (di == NULL)
2025 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002026 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002027 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002028 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 }
2030 else
2031 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002032 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002033 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002035 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 }
2037 }
2038 break;
2039
Bram Moolenaard3aac292020-04-19 14:32:17 +02002040 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002042 case ISN_LOADB:
2043 case ISN_LOADW:
2044 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002046 dictitem_T *di = NULL;
2047 hashtab_T *ht = NULL;
2048 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002049
Bram Moolenaard3aac292020-04-19 14:32:17 +02002050 switch (iptr->isn_type)
2051 {
2052 case ISN_LOADG:
2053 ht = get_globvar_ht();
2054 namespace = 'g';
2055 break;
2056 case ISN_LOADB:
2057 ht = &curbuf->b_vars->dv_hashtab;
2058 namespace = 'b';
2059 break;
2060 case ISN_LOADW:
2061 ht = &curwin->w_vars->dv_hashtab;
2062 namespace = 'w';
2063 break;
2064 case ISN_LOADT:
2065 ht = &curtab->tp_vars->dv_hashtab;
2066 namespace = 't';
2067 break;
2068 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002069 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002070 }
2071 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 if (di == NULL)
2074 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002075 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002076 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002077 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002078 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 }
2080 else
2081 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002082 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002083 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002085 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 }
2087 }
2088 break;
2089
Bram Moolenaar03290b82020-12-19 16:30:44 +01002090 // load autoload variable
2091 case ISN_LOADAUTO:
2092 {
2093 char_u *name = iptr->isn_arg.string;
2094
Bram Moolenaar35578162021-08-02 19:10:38 +02002095 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002096 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002097 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002098 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002099 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002100 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002101 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002102 }
2103 break;
2104
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002105 // load g:/b:/w:/t: namespace
2106 case ISN_LOADGDICT:
2107 case ISN_LOADBDICT:
2108 case ISN_LOADWDICT:
2109 case ISN_LOADTDICT:
2110 {
2111 dict_T *d = NULL;
2112
2113 switch (iptr->isn_type)
2114 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002115 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2116 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2117 case ISN_LOADWDICT: d = curwin->w_vars; break;
2118 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002119 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002120 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002121 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002122 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002123 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002124 tv = STACK_TV_BOT(0);
2125 tv->v_type = VAR_DICT;
2126 tv->v_lock = 0;
2127 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002128 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002129 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002130 }
2131 break;
2132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 // load &option
2134 case ISN_LOADOPT:
2135 {
2136 typval_T optval;
2137 char_u *name = iptr->isn_arg.string;
2138
Bram Moolenaara8c17702020-04-01 21:17:24 +02002139 // This is not expected to fail, name is checked during
2140 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002141 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002142 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002143 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002144 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002146 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 }
2148 break;
2149
2150 // load $ENV
2151 case ISN_LOADENV:
2152 {
2153 typval_T optval;
2154 char_u *name = iptr->isn_arg.string;
2155
Bram Moolenaar35578162021-08-02 19:10:38 +02002156 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002157 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002158 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002159 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002161 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 }
2163 break;
2164
2165 // load @register
2166 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002167 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002168 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 tv = STACK_TV_BOT(0);
2170 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002171 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002172 // This may result in NULL, which should be equivalent to an
2173 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 tv->vval.v_string = get_reg_contents(
2175 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002176 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 break;
2178
2179 // store local variable
2180 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002181 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 tv = STACK_TV_VAR(iptr->isn_arg.number);
2183 clear_tv(tv);
2184 *tv = *STACK_TV_BOT(0);
2185 break;
2186
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002187 // store s: variable in old script
2188 case ISN_STORES:
2189 {
2190 hashtab_T *ht = &SCRIPT_VARS(
2191 iptr->isn_arg.loadstore.ls_sid);
2192 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002193 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002194
Bram Moolenaar4c137212021-04-19 16:48:48 +02002195 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002196 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002197 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002198 else
2199 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002200 SOURCING_LNUM = iptr->isn_lnum;
2201 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002202 {
2203 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002204 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002205 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002206 clear_tv(&di->di_tv);
2207 di->di_tv = *STACK_TV_BOT(0);
2208 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002209 }
2210 break;
2211
2212 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 case ISN_STORESCRIPT:
2214 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002215 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2216 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002218 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002219 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002220 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002221 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002222
2223 // "const" and "final" are checked at compile time, locking
2224 // the value needs to be checked here.
2225 SOURCING_LNUM = iptr->isn_lnum;
2226 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002227 {
2228 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002229 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002230 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 clear_tv(sv->sv_tv);
2233 *sv->sv_tv = *STACK_TV_BOT(0);
2234 }
2235 break;
2236
2237 // store option
2238 case ISN_STOREOPT:
2239 {
2240 long n = 0;
2241 char_u *s = NULL;
2242 char *msg;
2243
Bram Moolenaar4c137212021-04-19 16:48:48 +02002244 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 tv = STACK_TV_BOT(0);
2246 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002247 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002249 if (s == NULL)
2250 s = (char_u *)"";
2251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002253 // must be VAR_NUMBER, CHECKTYPE makes sure
2254 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2256 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002257 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 if (msg != NULL)
2259 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002260 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002262 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 }
2265 break;
2266
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002267 // store $ENV
2268 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002269 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002270 tv = STACK_TV_BOT(0);
2271 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2272 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002273 break;
2274
2275 // store @r
2276 case ISN_STOREREG:
2277 {
2278 int reg = iptr->isn_arg.number;
2279
Bram Moolenaar4c137212021-04-19 16:48:48 +02002280 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002281 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002282 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002283 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002284 }
2285 break;
2286
2287 // store v: variable
2288 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002289 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002290 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2291 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002292 // should not happen, type is checked when compiling
2293 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002294 break;
2295
Bram Moolenaard3aac292020-04-19 14:32:17 +02002296 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002298 case ISN_STOREB:
2299 case ISN_STOREW:
2300 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002302 dictitem_T *di;
2303 hashtab_T *ht;
2304 char_u *name = iptr->isn_arg.string + 2;
2305
Bram Moolenaard3aac292020-04-19 14:32:17 +02002306 switch (iptr->isn_type)
2307 {
2308 case ISN_STOREG:
2309 ht = get_globvar_ht();
2310 break;
2311 case ISN_STOREB:
2312 ht = &curbuf->b_vars->dv_hashtab;
2313 break;
2314 case ISN_STOREW:
2315 ht = &curwin->w_vars->dv_hashtab;
2316 break;
2317 case ISN_STORET:
2318 ht = &curtab->tp_vars->dv_hashtab;
2319 break;
2320 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002321 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002322 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323
Bram Moolenaar4c137212021-04-19 16:48:48 +02002324 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002325 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002327 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 else
2329 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002330 SOURCING_LNUM = iptr->isn_lnum;
2331 if (var_check_permission(di, name) == FAIL)
2332 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 clear_tv(&di->di_tv);
2334 di->di_tv = *STACK_TV_BOT(0);
2335 }
2336 }
2337 break;
2338
Bram Moolenaar03290b82020-12-19 16:30:44 +01002339 // store an autoload variable
2340 case ISN_STOREAUTO:
2341 SOURCING_LNUM = iptr->isn_lnum;
2342 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2343 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002344 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002345 break;
2346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 // store number in local variable
2348 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002349 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 clear_tv(tv);
2351 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002352 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 break;
2354
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002355 // store value in list or dict variable
2356 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002357 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002358 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002359 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002360 typval_T *tv_dest = STACK_TV_BOT(-1);
2361 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002362
Bram Moolenaar752fc692021-01-04 21:57:11 +01002363 // Stack contains:
2364 // -3 value to be stored
2365 // -2 index
2366 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002367 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002368 SOURCING_LNUM = iptr->isn_lnum;
2369 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002370 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002371 dest_type = tv_dest->v_type;
2372 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002373 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002374 else if (dest_type == VAR_LIST
2375 && tv_idx->v_type != VAR_NUMBER)
2376 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002377 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002378 status = FAIL;
2379 }
2380 }
2381 else if (dest_type != tv_dest->v_type)
2382 {
2383 // just in case, should be OK
2384 semsg(_(e_expected_str_but_got_str),
2385 vartype_name(dest_type),
2386 vartype_name(tv_dest->v_type));
2387 status = FAIL;
2388 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002389
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002390 if (status == OK && dest_type == VAR_LIST)
2391 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002392 long lidx = (long)tv_idx->vval.v_number;
2393 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002394
2395 if (list == NULL)
2396 {
2397 emsg(_(e_list_not_set));
2398 goto on_error;
2399 }
2400 if (lidx < 0 && list->lv_len + lidx >= 0)
2401 // negative index is relative to the end
2402 lidx = list->lv_len + lidx;
2403 if (lidx < 0 || lidx > list->lv_len)
2404 {
2405 semsg(_(e_listidx), lidx);
2406 goto on_error;
2407 }
2408 if (lidx < list->lv_len)
2409 {
2410 listitem_T *li = list_find(list, lidx);
2411
2412 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002413 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002414 goto on_error;
2415 // overwrite existing list item
2416 clear_tv(&li->li_tv);
2417 li->li_tv = *tv;
2418 }
2419 else
2420 {
2421 if (error_if_locked(list->lv_lock,
2422 e_cannot_change_list))
2423 goto on_error;
2424 // append to list, only fails when out of memory
2425 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002426 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002427 clear_tv(tv);
2428 }
2429 }
2430 else if (status == OK && dest_type == VAR_DICT)
2431 {
2432 char_u *key = tv_idx->vval.v_string;
2433 dict_T *dict = tv_dest->vval.v_dict;
2434 dictitem_T *di;
2435
2436 SOURCING_LNUM = iptr->isn_lnum;
2437 if (dict == NULL)
2438 {
2439 emsg(_(e_dictionary_not_set));
2440 goto on_error;
2441 }
2442 if (key == NULL)
2443 key = (char_u *)"";
2444 di = dict_find(dict, key, -1);
2445 if (di != NULL)
2446 {
2447 if (error_if_locked(di->di_tv.v_lock,
2448 e_cannot_change_dict_item))
2449 goto on_error;
2450 // overwrite existing value
2451 clear_tv(&di->di_tv);
2452 di->di_tv = *tv;
2453 }
2454 else
2455 {
2456 if (error_if_locked(dict->dv_lock,
2457 e_cannot_change_dict))
2458 goto on_error;
2459 // add to dict, only fails when out of memory
2460 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002461 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002462 clear_tv(tv);
2463 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002464 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002465 else if (status == OK && dest_type == VAR_BLOB)
2466 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002467 long lidx = (long)tv_idx->vval.v_number;
2468 blob_T *blob = tv_dest->vval.v_blob;
2469 varnumber_T nr;
2470 int error = FALSE;
2471 int len;
2472
2473 if (blob == NULL)
2474 {
2475 emsg(_(e_blob_not_set));
2476 goto on_error;
2477 }
2478 len = blob_len(blob);
2479 if (lidx < 0 && len + lidx >= 0)
2480 // negative index is relative to the end
2481 lidx = len + lidx;
2482
2483 // Can add one byte at the end.
2484 if (lidx < 0 || lidx > len)
2485 {
2486 semsg(_(e_blobidx), lidx);
2487 goto on_error;
2488 }
2489 if (value_check_lock(blob->bv_lock,
2490 (char_u *)"blob", FALSE))
2491 goto on_error;
2492 nr = tv_get_number_chk(tv, &error);
2493 if (error)
2494 goto on_error;
2495 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002496 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002497 else
2498 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002499 status = FAIL;
2500 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002501 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002502
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002503 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002504 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002505 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002506 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002507 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002508 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002509 goto on_error;
2510 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002511 }
2512 break;
2513
Bram Moolenaar68452172021-04-12 21:21:02 +02002514 // store value in blob range
2515 case ISN_STORERANGE:
2516 {
2517 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2518 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2519 typval_T *tv_dest = STACK_TV_BOT(-1);
2520 int status = OK;
2521
2522 // Stack contains:
2523 // -4 value to be stored
2524 // -3 first index or "none"
2525 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002526 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002527 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002528 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002529 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002530 long n1;
2531 long n2;
2532 int error = FALSE;
2533
2534 SOURCING_LNUM = iptr->isn_lnum;
2535 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2536 if (error)
2537 status = FAIL;
2538 else
2539 {
2540 if (tv_idx2->v_type == VAR_SPECIAL
2541 && tv_idx2->vval.v_number == VVAL_NONE)
2542 n2 = list_len(tv_dest->vval.v_list) - 1;
2543 else
2544 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2545 if (error)
2546 status = FAIL;
2547 else
2548 {
2549 listitem_T *li1 = check_range_index_one(
2550 tv_dest->vval.v_list, &n1, FALSE);
2551
2552 if (li1 == NULL)
2553 status = FAIL;
2554 else
2555 {
2556 status = check_range_index_two(
2557 tv_dest->vval.v_list,
2558 &n1, li1, &n2, FALSE);
2559 if (status != FAIL)
2560 status = list_assign_range(
2561 tv_dest->vval.v_list,
2562 tv->vval.v_list,
2563 n1,
2564 n2,
2565 tv_idx2->v_type == VAR_SPECIAL,
2566 (char_u *)"=",
2567 (char_u *)"[unknown]");
2568 }
2569 }
2570 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002571 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002572 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002573 {
2574 varnumber_T n1;
2575 varnumber_T n2;
2576 int error = FALSE;
2577
2578 n1 = tv_get_number_chk(tv_idx1, &error);
2579 if (error)
2580 status = FAIL;
2581 else
2582 {
2583 if (tv_idx2->v_type == VAR_SPECIAL
2584 && tv_idx2->vval.v_number == VVAL_NONE)
2585 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2586 else
2587 n2 = tv_get_number_chk(tv_idx2, &error);
2588 if (error)
2589 status = FAIL;
2590 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002591 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002592 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002593
2594 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002595 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002596 || check_blob_range(bloblen,
2597 n1, n2, FALSE) == FAIL)
2598 status = FAIL;
2599 else
2600 status = blob_set_range(
2601 tv_dest->vval.v_blob, n1, n2, tv);
2602 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002603 }
2604 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002605 else
2606 {
2607 status = FAIL;
2608 emsg(_(e_blob_required));
2609 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002610
2611 clear_tv(tv_idx1);
2612 clear_tv(tv_idx2);
2613 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002614 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002615 clear_tv(tv);
2616
2617 if (status == FAIL)
2618 goto on_error;
2619 }
2620 break;
2621
Bram Moolenaar0186e582021-01-10 18:33:11 +01002622 // load or store variable or argument from outer scope
2623 case ISN_LOADOUTER:
2624 case ISN_STOREOUTER:
2625 {
2626 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002627 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2628 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002629
2630 while (depth > 1 && outer != NULL)
2631 {
2632 outer = outer->out_up;
2633 --depth;
2634 }
2635 if (outer == NULL)
2636 {
2637 SOURCING_LNUM = iptr->isn_lnum;
2638 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002639 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002640 }
2641 tv = ((typval_T *)outer->out_stack->ga_data)
2642 + outer->out_frame_idx + STACK_FRAME_SIZE
2643 + iptr->isn_arg.outer.outer_idx;
2644 if (iptr->isn_type == ISN_LOADOUTER)
2645 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002646 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002647 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002648 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002649 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002650 }
2651 else
2652 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002653 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002654 clear_tv(tv);
2655 *tv = *STACK_TV_BOT(0);
2656 }
2657 }
2658 break;
2659
Bram Moolenaar752fc692021-01-04 21:57:11 +01002660 // unlet item in list or dict variable
2661 case ISN_UNLETINDEX:
2662 {
2663 typval_T *tv_idx = STACK_TV_BOT(-2);
2664 typval_T *tv_dest = STACK_TV_BOT(-1);
2665 int status = OK;
2666
2667 // Stack contains:
2668 // -2 index
2669 // -1 dict or list
2670 if (tv_dest->v_type == VAR_DICT)
2671 {
2672 // unlet a dict item, index must be a string
2673 if (tv_idx->v_type != VAR_STRING)
2674 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002675 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002676 semsg(_(e_expected_str_but_got_str),
2677 vartype_name(VAR_STRING),
2678 vartype_name(tv_idx->v_type));
2679 status = FAIL;
2680 }
2681 else
2682 {
2683 dict_T *d = tv_dest->vval.v_dict;
2684 char_u *key = tv_idx->vval.v_string;
2685 dictitem_T *di = NULL;
2686
2687 if (key == NULL)
2688 key = (char_u *)"";
2689 if (d != NULL)
2690 di = dict_find(d, key, (int)STRLEN(key));
2691 if (di == NULL)
2692 {
2693 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002694 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002695 semsg(_(e_dictkey), key);
2696 status = FAIL;
2697 }
2698 else
2699 {
2700 // TODO: check for dict or item locked
2701 dictitem_remove(d, di);
2702 }
2703 }
2704 }
2705 else if (tv_dest->v_type == VAR_LIST)
2706 {
2707 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002708 SOURCING_LNUM = iptr->isn_lnum;
2709 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002710 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002711 status = FAIL;
2712 }
2713 else
2714 {
2715 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002716 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002717 listitem_T *li = NULL;
2718
2719 li = list_find(l, n);
2720 if (li == NULL)
2721 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002722 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002723 semsg(_(e_listidx), n);
2724 status = FAIL;
2725 }
2726 else
2727 // TODO: check for list or item locked
2728 listitem_remove(l, li);
2729 }
2730 }
2731 else
2732 {
2733 status = FAIL;
2734 semsg(_(e_cannot_index_str),
2735 vartype_name(tv_dest->v_type));
2736 }
2737
2738 clear_tv(tv_idx);
2739 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002740 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002741 if (status == FAIL)
2742 goto on_error;
2743 }
2744 break;
2745
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002746 // unlet range of items in list variable
2747 case ISN_UNLETRANGE:
2748 {
2749 // Stack contains:
2750 // -3 index1
2751 // -2 index2
2752 // -1 dict or list
2753 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2754 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2755 typval_T *tv_dest = STACK_TV_BOT(-1);
2756 int status = OK;
2757
2758 if (tv_dest->v_type == VAR_LIST)
2759 {
2760 // indexes must be a number
2761 SOURCING_LNUM = iptr->isn_lnum;
2762 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002763 || (tv_idx2->v_type != VAR_SPECIAL
2764 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002765 {
2766 status = FAIL;
2767 }
2768 else
2769 {
2770 list_T *l = tv_dest->vval.v_list;
2771 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002772 long n2 = tv_idx2->v_type == VAR_SPECIAL
2773 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002774 listitem_T *li;
2775
2776 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002777 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002778 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002779 else
2780 {
2781 if (n1 < 0)
2782 n1 = list_idx_of_item(l, li);
2783 if (n2 < 0)
2784 {
2785 listitem_T *li2 = list_find(l, n2);
2786
2787 if (li2 == NULL)
2788 status = FAIL;
2789 else
2790 n2 = list_idx_of_item(l, li2);
2791 }
2792 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002793 && tv_idx2->v_type != VAR_SPECIAL
2794 && n2 < n1)
2795 {
2796 semsg(_(e_listidx), n2);
2797 status = FAIL;
2798 }
2799 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002800 && list_unlet_range(l, li, NULL, n1,
2801 tv_idx2->v_type != VAR_SPECIAL, n2)
2802 == FAIL)
2803 status = FAIL;
2804 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002805 }
2806 }
2807 else
2808 {
2809 status = FAIL;
2810 SOURCING_LNUM = iptr->isn_lnum;
2811 semsg(_(e_cannot_index_str),
2812 vartype_name(tv_dest->v_type));
2813 }
2814
2815 clear_tv(tv_idx1);
2816 clear_tv(tv_idx2);
2817 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002818 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002819 if (status == FAIL)
2820 goto on_error;
2821 }
2822 break;
2823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 // push constant
2825 case ISN_PUSHNR:
2826 case ISN_PUSHBOOL:
2827 case ISN_PUSHSPEC:
2828 case ISN_PUSHF:
2829 case ISN_PUSHS:
2830 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002831 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002832 case ISN_PUSHCHANNEL:
2833 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02002834 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002835 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002837 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002838 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 switch (iptr->isn_type)
2840 {
2841 case ISN_PUSHNR:
2842 tv->v_type = VAR_NUMBER;
2843 tv->vval.v_number = iptr->isn_arg.number;
2844 break;
2845 case ISN_PUSHBOOL:
2846 tv->v_type = VAR_BOOL;
2847 tv->vval.v_number = iptr->isn_arg.number;
2848 break;
2849 case ISN_PUSHSPEC:
2850 tv->v_type = VAR_SPECIAL;
2851 tv->vval.v_number = iptr->isn_arg.number;
2852 break;
2853#ifdef FEAT_FLOAT
2854 case ISN_PUSHF:
2855 tv->v_type = VAR_FLOAT;
2856 tv->vval.v_float = iptr->isn_arg.fnumber;
2857 break;
2858#endif
2859 case ISN_PUSHBLOB:
2860 blob_copy(iptr->isn_arg.blob, tv);
2861 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002862 case ISN_PUSHFUNC:
2863 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002864 if (iptr->isn_arg.string == NULL)
2865 tv->vval.v_string = NULL;
2866 else
2867 tv->vval.v_string =
2868 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002869 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002870 case ISN_PUSHCHANNEL:
2871#ifdef FEAT_JOB_CHANNEL
2872 tv->v_type = VAR_CHANNEL;
2873 tv->vval.v_channel = iptr->isn_arg.channel;
2874 if (tv->vval.v_channel != NULL)
2875 ++tv->vval.v_channel->ch_refcount;
2876#endif
2877 break;
2878 case ISN_PUSHJOB:
2879#ifdef FEAT_JOB_CHANNEL
2880 tv->v_type = VAR_JOB;
2881 tv->vval.v_job = iptr->isn_arg.job;
2882 if (tv->vval.v_job != NULL)
2883 ++tv->vval.v_job->jv_refcount;
2884#endif
2885 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 default:
2887 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002888 tv->vval.v_string = vim_strsave(
2889 iptr->isn_arg.string == NULL
2890 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 }
2892 break;
2893
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002894 case ISN_UNLET:
2895 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2896 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002897 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002898 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002899 case ISN_UNLETENV:
2900 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2901 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002902
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002903 case ISN_LOCKUNLOCK:
2904 {
2905 typval_T *lval_root_save = lval_root;
2906 int res;
2907
2908 // Stack has the local variable, argument the whole :lock
2909 // or :unlock command, like ISN_EXEC.
2910 --ectx->ec_stack.ga_len;
2911 lval_root = STACK_TV_BOT(0);
2912 res = exec_command(iptr);
2913 clear_tv(lval_root);
2914 lval_root = lval_root_save;
2915 if (res == FAIL)
2916 goto on_error;
2917 }
2918 break;
2919
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002920 case ISN_LOCKCONST:
2921 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2922 break;
2923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 // create a list from items on the stack; uses a single allocation
2925 // for the list header and the items
2926 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002927 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002928 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 break;
2930
2931 // create a dict from items on the stack
2932 case ISN_NEWDICT:
2933 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002934 int count = iptr->isn_arg.number;
2935 dict_T *dict = dict_alloc();
2936 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002937 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002938 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002940 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002941 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 for (idx = 0; idx < count; ++idx)
2943 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002944 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002946 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002947 key = tv->vval.v_string == NULL
2948 ? (char_u *)"" : tv->vval.v_string;
2949 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002950 if (item != NULL)
2951 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002952 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002953 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002954 dict_unref(dict);
2955 goto on_error;
2956 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002957 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002959 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02002960 {
2961 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002962 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2965 item->di_tv.v_lock = 0;
2966 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002967 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002968 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002969 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002970 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 }
2973
2974 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002975 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02002976 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002977 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002979 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 tv = STACK_TV_BOT(-1);
2981 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002982 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 tv->vval.v_dict = dict;
2984 ++dict->dv_refcount;
2985 }
2986 break;
2987
2988 // call a :def function
2989 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002990 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002991 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2992 NULL,
2993 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002994 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002995 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 break;
2997
2998 // call a builtin function
2999 case ISN_BCALL:
3000 SOURCING_LNUM = iptr->isn_lnum;
3001 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3002 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003003 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003004 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 break;
3006
3007 // call a funcref or partial
3008 case ISN_PCALL:
3009 {
3010 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3011 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003012 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013
3014 SOURCING_LNUM = iptr->isn_lnum;
3015 if (pfunc->cpf_top)
3016 {
3017 // funcref is above the arguments
3018 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3019 }
3020 else
3021 {
3022 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003023 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003024 partial_tv = *STACK_TV_BOT(0);
3025 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003028 if (tv == &partial_tv)
3029 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003031 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 }
3033 break;
3034
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003035 case ISN_PCALL_END:
3036 // PCALL finished, arguments have been consumed and replaced by
3037 // the return value. Now clear the funcref from the stack,
3038 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003039 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003040 clear_tv(STACK_TV_BOT(-1));
3041 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3042 break;
3043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 // call a user defined function or funcref/partial
3045 case ISN_UCALL:
3046 {
3047 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3048
3049 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003050 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003051 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003052 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 }
3054 break;
3055
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003056 // return from a :def function call without a value
3057 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003058 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003059 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003060 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003061 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003062 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003063 tv->vval.v_number = 0;
3064 tv->v_lock = 0;
3065 // FALLTHROUGH
3066
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003067 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 case ISN_RETURN:
3069 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003070 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003071 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003072
3073 if (trystack->ga_len > 0)
3074 trycmd = ((trycmd_T *)trystack->ga_data)
3075 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003076 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003077 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003079 // jump to ":finally" or ":endtry"
3080 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003081 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003082 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003083 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 trycmd->tcd_return = TRUE;
3085 }
3086 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003087 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 }
3089 break;
3090
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003091 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 case ISN_FUNCREF:
3093 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003094 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
3095 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3096 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003099 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003100 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003101 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003102 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003103 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003104 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01003105 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003107 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003109 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 tv->vval.v_partial = pt;
3111 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003112 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 }
3114 break;
3115
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003116 // Create a global function from a lambda.
3117 case ISN_NEWFUNC:
3118 {
3119 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3120
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003121 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003122 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003123 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003124 }
3125 break;
3126
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003127 // List functions
3128 case ISN_DEF:
3129 if (iptr->isn_arg.string == NULL)
3130 list_functions(NULL);
3131 else
3132 {
3133 exarg_T ea;
3134
3135 CLEAR_FIELD(ea);
3136 ea.cmd = ea.arg = iptr->isn_arg.string;
3137 define_function(&ea, NULL);
3138 }
3139 break;
3140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 // jump if a condition is met
3142 case ISN_JUMP:
3143 {
3144 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003145 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 int jump = TRUE;
3147
3148 if (when != JUMP_ALWAYS)
3149 {
3150 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003151 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003152 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003153 || when == JUMP_IF_COND_TRUE)
3154 {
3155 SOURCING_LNUM = iptr->isn_lnum;
3156 jump = tv_get_bool_chk(tv, &error);
3157 if (error)
3158 goto on_error;
3159 }
3160 else
3161 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003163 || when == JUMP_AND_KEEP_IF_FALSE
3164 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003166 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 {
3168 // drop the value from the stack
3169 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003170 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 }
3172 }
3173 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003174 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 }
3176 break;
3177
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003178 // Jump if an argument with a default value was already set and not
3179 // v:none.
3180 case ISN_JUMP_IF_ARG_SET:
3181 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3182 if (tv->v_type != VAR_UNKNOWN
3183 && !(tv->v_type == VAR_SPECIAL
3184 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003185 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003186 break;
3187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 // top of a for loop
3189 case ISN_FOR:
3190 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003191 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 typval_T *idxtv =
3193 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3194
Bram Moolenaar35578162021-08-02 19:10:38 +02003195 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003196 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003197 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003198 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003199 list_T *list = ltv->vval.v_list;
3200
3201 // push the next item from the list
3202 ++idxtv->vval.v_number;
3203 if (list == NULL
3204 || idxtv->vval.v_number >= list->lv_len)
3205 {
3206 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003207 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3208 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003209 }
3210 else if (list->lv_first == &range_list_item)
3211 {
3212 // non-materialized range() list
3213 tv = STACK_TV_BOT(0);
3214 tv->v_type = VAR_NUMBER;
3215 tv->v_lock = 0;
3216 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003218 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003219 }
3220 else
3221 {
3222 listitem_T *li = list_find(list,
3223 idxtv->vval.v_number);
3224
3225 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003226 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003227 }
3228 }
3229 else if (ltv->v_type == VAR_STRING)
3230 {
3231 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003232
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003233 // The index is for the last byte of the previous
3234 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003235 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003236 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003237 {
3238 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003239 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3240 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003241 }
3242 else
3243 {
3244 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3245
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003246 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003247 tv = STACK_TV_BOT(0);
3248 tv->v_type = VAR_STRING;
3249 tv->vval.v_string = vim_strnsave(
3250 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003251 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003252 idxtv->vval.v_number += clen - 1;
3253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003255 else if (ltv->v_type == VAR_BLOB)
3256 {
3257 blob_T *blob = ltv->vval.v_blob;
3258
3259 // When we get here the first time make a copy of the
3260 // blob, so that the iteration still works when it is
3261 // changed.
3262 if (idxtv->vval.v_number == -1 && blob != NULL)
3263 {
3264 blob_copy(blob, ltv);
3265 blob_unref(blob);
3266 blob = ltv->vval.v_blob;
3267 }
3268
3269 // The index is for the previous byte.
3270 ++idxtv->vval.v_number;
3271 if (blob == NULL
3272 || idxtv->vval.v_number >= blob_len(blob))
3273 {
3274 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003275 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3276 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003277 }
3278 else
3279 {
3280 // Push the next byte from the blob.
3281 tv = STACK_TV_BOT(0);
3282 tv->v_type = VAR_NUMBER;
3283 tv->vval.v_number = blob_get(blob,
3284 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003285 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003286 }
3287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 else
3289 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003290 semsg(_(e_for_loop_on_str_not_supported),
3291 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003292 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 }
3294 }
3295 break;
3296
3297 // start of ":try" block
3298 case ISN_TRY:
3299 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003300 trycmd_T *trycmd = NULL;
3301
Bram Moolenaar35578162021-08-02 19:10:38 +02003302 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003303 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003304 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3305 + ectx->ec_trystack.ga_len;
3306 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003308 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003309 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3310 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003311 trycmd->tcd_catch_idx =
3312 iptr->isn_arg.try.try_ref->try_catch;
3313 trycmd->tcd_finally_idx =
3314 iptr->isn_arg.try.try_ref->try_finally;
3315 trycmd->tcd_endtry_idx =
3316 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 }
3318 break;
3319
3320 case ISN_PUSHEXC:
3321 if (current_exception == NULL)
3322 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003323 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003325 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003327 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003328 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003330 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003332 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 tv->vval.v_string = vim_strsave(
3334 (char_u *)current_exception->value);
3335 break;
3336
3337 case ISN_CATCH:
3338 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003339 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340
Bram Moolenaar4c137212021-04-19 16:48:48 +02003341 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 if (trystack->ga_len > 0)
3343 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003344 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 + trystack->ga_len - 1;
3346 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003347 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 }
3349 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003350 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 catch_exception(current_exception);
3352 }
3353 break;
3354
Bram Moolenaarc150c092021-02-13 15:02:46 +01003355 case ISN_TRYCONT:
3356 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003357 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003358 trycont_T *trycont = &iptr->isn_arg.trycont;
3359 int i;
3360 trycmd_T *trycmd;
3361 int iidx = trycont->tct_where;
3362
3363 if (trystack->ga_len < trycont->tct_levels)
3364 {
3365 siemsg("TRYCONT: expected %d levels, found %d",
3366 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003367 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003368 }
3369 // Make :endtry jump to any outer try block and the last
3370 // :endtry inside the loop to the loop start.
3371 for (i = trycont->tct_levels; i > 0; --i)
3372 {
3373 trycmd = ((trycmd_T *)trystack->ga_data)
3374 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003375 // Add one to tcd_cont to be able to jump to
3376 // instruction with index zero.
3377 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003378 iidx = trycmd->tcd_finally_idx == 0
3379 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003380 }
3381 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003382 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003383 }
3384 break;
3385
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003386 case ISN_FINALLY:
3387 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003388 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003389 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3390 + trystack->ga_len - 1;
3391
3392 // Reset the index to avoid a return statement jumps here
3393 // again.
3394 trycmd->tcd_finally_idx = 0;
3395 break;
3396 }
3397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 // end of ":try" block
3399 case ISN_ENDTRY:
3400 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003401 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402
3403 if (trystack->ga_len > 0)
3404 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003405 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 --trystack->ga_len;
3408 --trylevel;
3409 trycmd = ((trycmd_T *)trystack->ga_data)
3410 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003411 if (trycmd->tcd_did_throw)
3412 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003413 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 {
3415 // discard the exception
3416 if (caught_stack == current_exception)
3417 caught_stack = caught_stack->caught;
3418 discard_current_exception();
3419 }
3420
3421 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003422 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003423
Bram Moolenaar4c137212021-04-19 16:48:48 +02003424 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003425 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003426 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003427 clear_tv(STACK_TV_BOT(0));
3428 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003429 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003430 // handling :continue: jump to outer try block or
3431 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003432 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 }
3434 }
3435 break;
3436
3437 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003438 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003439 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003440
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003441 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3442 {
3443 // throwing an exception while using "silent!" causes
3444 // the function to abort but not display an error.
3445 tv = STACK_TV_BOT(-1);
3446 clear_tv(tv);
3447 tv->v_type = VAR_NUMBER;
3448 tv->vval.v_number = 0;
3449 goto done;
3450 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003451 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003452 tv = STACK_TV_BOT(0);
3453 if (tv->vval.v_string == NULL
3454 || *skipwhite(tv->vval.v_string) == NUL)
3455 {
3456 vim_free(tv->vval.v_string);
3457 SOURCING_LNUM = iptr->isn_lnum;
3458 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003459 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003460 }
3461
3462 // Inside a "catch" we need to first discard the caught
3463 // exception.
3464 if (trystack->ga_len > 0)
3465 {
3466 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3467 + trystack->ga_len - 1;
3468 if (trycmd->tcd_caught && current_exception != NULL)
3469 {
3470 // discard the exception
3471 if (caught_stack == current_exception)
3472 caught_stack = caught_stack->caught;
3473 discard_current_exception();
3474 trycmd->tcd_caught = FALSE;
3475 }
3476 }
3477
3478 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3479 == FAIL)
3480 {
3481 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003482 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003483 }
3484 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 break;
3487
3488 // compare with special values
3489 case ISN_COMPAREBOOL:
3490 case ISN_COMPARESPECIAL:
3491 {
3492 typval_T *tv1 = STACK_TV_BOT(-2);
3493 typval_T *tv2 = STACK_TV_BOT(-1);
3494 varnumber_T arg1 = tv1->vval.v_number;
3495 varnumber_T arg2 = tv2->vval.v_number;
3496 int res;
3497
3498 switch (iptr->isn_arg.op.op_type)
3499 {
3500 case EXPR_EQUAL: res = arg1 == arg2; break;
3501 case EXPR_NEQUAL: res = arg1 != arg2; break;
3502 default: res = 0; break;
3503 }
3504
Bram Moolenaar4c137212021-04-19 16:48:48 +02003505 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 tv1->v_type = VAR_BOOL;
3507 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3508 }
3509 break;
3510
3511 // Operation with two number arguments
3512 case ISN_OPNR:
3513 case ISN_COMPARENR:
3514 {
3515 typval_T *tv1 = STACK_TV_BOT(-2);
3516 typval_T *tv2 = STACK_TV_BOT(-1);
3517 varnumber_T arg1 = tv1->vval.v_number;
3518 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003519 varnumber_T res = 0;
3520 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521
3522 switch (iptr->isn_arg.op.op_type)
3523 {
3524 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003525 case EXPR_DIV: if (arg2 == 0)
3526 div_zero = TRUE;
3527 else
3528 res = arg1 / arg2;
3529 break;
3530 case EXPR_REM: if (arg2 == 0)
3531 div_zero = TRUE;
3532 else
3533 res = arg1 % arg2;
3534 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 case EXPR_SUB: res = arg1 - arg2; break;
3536 case EXPR_ADD: res = arg1 + arg2; break;
3537
3538 case EXPR_EQUAL: res = arg1 == arg2; break;
3539 case EXPR_NEQUAL: res = arg1 != arg2; break;
3540 case EXPR_GREATER: res = arg1 > arg2; break;
3541 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3542 case EXPR_SMALLER: res = arg1 < arg2; break;
3543 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003544 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 }
3546
Bram Moolenaar4c137212021-04-19 16:48:48 +02003547 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 if (iptr->isn_type == ISN_COMPARENR)
3549 {
3550 tv1->v_type = VAR_BOOL;
3551 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3552 }
3553 else
3554 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003555 if (div_zero)
3556 {
3557 SOURCING_LNUM = iptr->isn_lnum;
3558 emsg(_(e_divide_by_zero));
3559 goto on_error;
3560 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 }
3562 break;
3563
3564 // Computation with two float arguments
3565 case ISN_OPFLOAT:
3566 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003567#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 {
3569 typval_T *tv1 = STACK_TV_BOT(-2);
3570 typval_T *tv2 = STACK_TV_BOT(-1);
3571 float_T arg1 = tv1->vval.v_float;
3572 float_T arg2 = tv2->vval.v_float;
3573 float_T res = 0;
3574 int cmp = FALSE;
3575
3576 switch (iptr->isn_arg.op.op_type)
3577 {
3578 case EXPR_MULT: res = arg1 * arg2; break;
3579 case EXPR_DIV: res = arg1 / arg2; break;
3580 case EXPR_SUB: res = arg1 - arg2; break;
3581 case EXPR_ADD: res = arg1 + arg2; break;
3582
3583 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3584 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3585 case EXPR_GREATER: cmp = arg1 > arg2; break;
3586 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3587 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3588 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3589 default: cmp = 0; break;
3590 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003591 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 if (iptr->isn_type == ISN_COMPAREFLOAT)
3593 {
3594 tv1->v_type = VAR_BOOL;
3595 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3596 }
3597 else
3598 tv1->vval.v_float = res;
3599 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003600#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 break;
3602
3603 case ISN_COMPARELIST:
3604 {
3605 typval_T *tv1 = STACK_TV_BOT(-2);
3606 typval_T *tv2 = STACK_TV_BOT(-1);
3607 list_T *arg1 = tv1->vval.v_list;
3608 list_T *arg2 = tv2->vval.v_list;
3609 int cmp = FALSE;
3610 int ic = iptr->isn_arg.op.op_ic;
3611
3612 switch (iptr->isn_arg.op.op_type)
3613 {
3614 case EXPR_EQUAL: cmp =
3615 list_equal(arg1, arg2, ic, FALSE); break;
3616 case EXPR_NEQUAL: cmp =
3617 !list_equal(arg1, arg2, ic, FALSE); break;
3618 case EXPR_IS: cmp = arg1 == arg2; break;
3619 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3620 default: cmp = 0; break;
3621 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003622 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 clear_tv(tv1);
3624 clear_tv(tv2);
3625 tv1->v_type = VAR_BOOL;
3626 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3627 }
3628 break;
3629
3630 case ISN_COMPAREBLOB:
3631 {
3632 typval_T *tv1 = STACK_TV_BOT(-2);
3633 typval_T *tv2 = STACK_TV_BOT(-1);
3634 blob_T *arg1 = tv1->vval.v_blob;
3635 blob_T *arg2 = tv2->vval.v_blob;
3636 int cmp = FALSE;
3637
3638 switch (iptr->isn_arg.op.op_type)
3639 {
3640 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3641 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3642 case EXPR_IS: cmp = arg1 == arg2; break;
3643 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3644 default: cmp = 0; break;
3645 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003646 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 clear_tv(tv1);
3648 clear_tv(tv2);
3649 tv1->v_type = VAR_BOOL;
3650 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3651 }
3652 break;
3653
3654 // TODO: handle separately
3655 case ISN_COMPARESTRING:
3656 case ISN_COMPAREDICT:
3657 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 case ISN_COMPAREANY:
3659 {
3660 typval_T *tv1 = STACK_TV_BOT(-2);
3661 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003662 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 int ic = iptr->isn_arg.op.op_ic;
3664
Bram Moolenaareb26f432020-09-14 16:50:05 +02003665 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003666 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003668 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 }
3670 break;
3671
3672 case ISN_ADDLIST:
3673 case ISN_ADDBLOB:
3674 {
3675 typval_T *tv1 = STACK_TV_BOT(-2);
3676 typval_T *tv2 = STACK_TV_BOT(-1);
3677
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003678 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003680 {
3681 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3682 && tv1->vval.v_list != NULL)
3683 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3684 NULL);
3685 else
3686 eval_addlist(tv1, tv2);
3687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 else
3689 eval_addblob(tv1, tv2);
3690 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003691 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 }
3693 break;
3694
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003695 case ISN_LISTAPPEND:
3696 {
3697 typval_T *tv1 = STACK_TV_BOT(-2);
3698 typval_T *tv2 = STACK_TV_BOT(-1);
3699 list_T *l = tv1->vval.v_list;
3700
3701 // add an item to a list
3702 if (l == NULL)
3703 {
3704 SOURCING_LNUM = iptr->isn_lnum;
3705 emsg(_(e_cannot_add_to_null_list));
3706 goto on_error;
3707 }
3708 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003709 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003710 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003711 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003712 }
3713 break;
3714
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003715 case ISN_BLOBAPPEND:
3716 {
3717 typval_T *tv1 = STACK_TV_BOT(-2);
3718 typval_T *tv2 = STACK_TV_BOT(-1);
3719 blob_T *b = tv1->vval.v_blob;
3720 int error = FALSE;
3721 varnumber_T n;
3722
3723 // add a number to a blob
3724 if (b == NULL)
3725 {
3726 SOURCING_LNUM = iptr->isn_lnum;
3727 emsg(_(e_cannot_add_to_null_blob));
3728 goto on_error;
3729 }
3730 n = tv_get_number_chk(tv2, &error);
3731 if (error)
3732 goto on_error;
3733 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003734 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003735 }
3736 break;
3737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 // Computation with two arguments of unknown type
3739 case ISN_OPANY:
3740 {
3741 typval_T *tv1 = STACK_TV_BOT(-2);
3742 typval_T *tv2 = STACK_TV_BOT(-1);
3743 varnumber_T n1, n2;
3744#ifdef FEAT_FLOAT
3745 float_T f1 = 0, f2 = 0;
3746#endif
3747 int error = FALSE;
3748
3749 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3750 {
3751 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3752 {
3753 eval_addlist(tv1, tv2);
3754 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003755 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 break;
3757 }
3758 else if (tv1->v_type == VAR_BLOB
3759 && tv2->v_type == VAR_BLOB)
3760 {
3761 eval_addblob(tv1, tv2);
3762 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003763 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 break;
3765 }
3766 }
3767#ifdef FEAT_FLOAT
3768 if (tv1->v_type == VAR_FLOAT)
3769 {
3770 f1 = tv1->vval.v_float;
3771 n1 = 0;
3772 }
3773 else
3774#endif
3775 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 n1 = tv_get_number_chk(tv1, &error);
3778 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003779 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780#ifdef FEAT_FLOAT
3781 if (tv2->v_type == VAR_FLOAT)
3782 f1 = n1;
3783#endif
3784 }
3785#ifdef FEAT_FLOAT
3786 if (tv2->v_type == VAR_FLOAT)
3787 {
3788 f2 = tv2->vval.v_float;
3789 n2 = 0;
3790 }
3791 else
3792#endif
3793 {
3794 n2 = tv_get_number_chk(tv2, &error);
3795 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003796 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797#ifdef FEAT_FLOAT
3798 if (tv1->v_type == VAR_FLOAT)
3799 f2 = n2;
3800#endif
3801 }
3802#ifdef FEAT_FLOAT
3803 // if there is a float on either side the result is a float
3804 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3805 {
3806 switch (iptr->isn_arg.op.op_type)
3807 {
3808 case EXPR_MULT: f1 = f1 * f2; break;
3809 case EXPR_DIV: f1 = f1 / f2; break;
3810 case EXPR_SUB: f1 = f1 - f2; break;
3811 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003812 default: SOURCING_LNUM = iptr->isn_lnum;
3813 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003814 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 }
3816 clear_tv(tv1);
3817 clear_tv(tv2);
3818 tv1->v_type = VAR_FLOAT;
3819 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003820 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 }
3822 else
3823#endif
3824 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003825 int failed = FALSE;
3826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 switch (iptr->isn_arg.op.op_type)
3828 {
3829 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003830 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3831 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003832 goto on_error;
3833 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 case EXPR_SUB: n1 = n1 - n2; break;
3835 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003836 default: n1 = num_modulus(n1, n2, &failed);
3837 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003838 goto on_error;
3839 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 }
3841 clear_tv(tv1);
3842 clear_tv(tv2);
3843 tv1->v_type = VAR_NUMBER;
3844 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003845 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 }
3847 }
3848 break;
3849
3850 case ISN_CONCAT:
3851 {
3852 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3853 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3854 char_u *res;
3855
3856 res = concat_str(str1, str2);
3857 clear_tv(STACK_TV_BOT(-2));
3858 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003859 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 STACK_TV_BOT(-1)->vval.v_string = res;
3861 }
3862 break;
3863
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003864 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003865 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003866 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003867 int is_slice = iptr->isn_type == ISN_STRSLICE;
3868 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003869 char_u *res;
3870
3871 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003872 // string slice: string is at stack-3, first index at
3873 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003874 if (is_slice)
3875 {
3876 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003877 n1 = tv->vval.v_number;
3878 }
3879
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003880 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003881 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003882
Bram Moolenaar4c137212021-04-19 16:48:48 +02003883 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003884 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003885 if (is_slice)
3886 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003887 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003888 else
3889 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003890 // single character (including composing characters).
3891 // If the index is too big or negative the result is
3892 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003893 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003894 vim_free(tv->vval.v_string);
3895 tv->vval.v_string = res;
3896 }
3897 break;
3898
3899 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003900 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003901 case ISN_BLOBINDEX:
3902 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003904 int is_slice = iptr->isn_type == ISN_LISTSLICE
3905 || iptr->isn_type == ISN_BLOBSLICE;
3906 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3907 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003908 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003909 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910
3911 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003912 // list slice: list is at stack-3, indexes at stack-2 and
3913 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003914 // Same for blob.
3915 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916
3917 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003918 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003920
3921 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 {
Bram Moolenaared591872020-08-15 22:14:53 +02003923 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003924 n1 = tv->vval.v_number;
3925 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 }
Bram Moolenaared591872020-08-15 22:14:53 +02003927
Bram Moolenaar4c137212021-04-19 16:48:48 +02003928 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003929 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003930 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003931 if (is_blob)
3932 {
3933 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3934 n1, n2, FALSE, tv) == FAIL)
3935 goto on_error;
3936 }
3937 else
3938 {
3939 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3940 n1, n2, FALSE, tv, TRUE) == FAIL)
3941 goto on_error;
3942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 }
3944 break;
3945
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003946 case ISN_ANYINDEX:
3947 case ISN_ANYSLICE:
3948 {
3949 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3950 typval_T *var1, *var2;
3951 int res;
3952
3953 // index: composite is at stack-2, index at stack-1
3954 // slice: composite is at stack-3, indexes at stack-2 and
3955 // stack-1
3956 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003957 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003958 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3959 goto on_error;
3960 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3961 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003962 res = eval_index_inner(tv, is_slice, var1, var2,
3963 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003964 clear_tv(var1);
3965 if (is_slice)
3966 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003967 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003968 if (res == FAIL)
3969 goto on_error;
3970 }
3971 break;
3972
Bram Moolenaar9af78762020-06-16 11:34:42 +02003973 case ISN_SLICE:
3974 {
3975 list_T *list;
3976 int count = iptr->isn_arg.number;
3977
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003978 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003979 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003980 list = tv->vval.v_list;
3981
3982 // no error for short list, expect it to be checked earlier
3983 if (list != NULL && list->lv_len >= count)
3984 {
3985 list_T *newlist = list_slice(list,
3986 count, list->lv_len - 1);
3987
3988 if (newlist != NULL)
3989 {
3990 list_unref(list);
3991 tv->vval.v_list = newlist;
3992 ++newlist->lv_refcount;
3993 }
3994 }
3995 }
3996 break;
3997
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003998 case ISN_GETITEM:
3999 {
4000 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004001 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004002
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004003 // Get list item: list is at stack-1, push item.
4004 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004005 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4006 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004007
Bram Moolenaar35578162021-08-02 19:10:38 +02004008 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004009 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004010 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004011 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004012
4013 // Useful when used in unpack assignment. Reset at
4014 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004015 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004016 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004017 }
4018 break;
4019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 case ISN_MEMBER:
4021 {
4022 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004023 char_u *key;
4024 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004025 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004026
4027 // dict member: dict is at stack-2, key at stack-1
4028 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004029 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004030 dict = tv->vval.v_dict;
4031
4032 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004033 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004034 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004035 if (key == NULL)
4036 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004037
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004038 if ((di = dict_find(dict, key, -1)) == NULL)
4039 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004040 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004041 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004042
4043 // If :silent! is used we will continue, make sure the
4044 // stack contents makes sense.
4045 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004046 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004047 tv = STACK_TV_BOT(-1);
4048 clear_tv(tv);
4049 tv->v_type = VAR_NUMBER;
4050 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004051 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004052 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004053 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004054 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004055 // Clear the dict only after getting the item, to avoid
4056 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004057 tv = STACK_TV_BOT(-1);
4058 temp_tv = *tv;
4059 copy_tv(&di->di_tv, tv);
4060 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004061 }
4062 break;
4063
4064 // dict member with string key
4065 case ISN_STRINGMEMBER:
4066 {
4067 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004069 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070
4071 tv = STACK_TV_BOT(-1);
4072 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4073 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004074 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004076 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 }
4078 dict = tv->vval.v_dict;
4079
4080 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4081 == NULL)
4082 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004083 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004085 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004087 // Clear the dict after getting the item, to avoid that it
4088 // make the item invalid.
4089 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02004091 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 }
4093 break;
4094
4095 case ISN_NEGATENR:
4096 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004097 if (tv->v_type != VAR_NUMBER
4098#ifdef FEAT_FLOAT
4099 && tv->v_type != VAR_FLOAT
4100#endif
4101 )
4102 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004103 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004104 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004105 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004106 }
4107#ifdef FEAT_FLOAT
4108 if (tv->v_type == VAR_FLOAT)
4109 tv->vval.v_float = -tv->vval.v_float;
4110 else
4111#endif
4112 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 break;
4114
4115 case ISN_CHECKNR:
4116 {
4117 int error = FALSE;
4118
4119 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004120 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004122 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 (void)tv_get_number_chk(tv, &error);
4124 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004125 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 }
4127 break;
4128
4129 case ISN_CHECKTYPE:
4130 {
4131 checktype_T *ct = &iptr->isn_arg.type;
4132
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004133 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004134 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004135 if (!ectx->ec_where.wt_variable)
4136 ectx->ec_where.wt_index = ct->ct_arg_idx;
4137 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4138 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004139 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004140 if (!ectx->ec_where.wt_variable)
4141 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004142
4143 // number 0 is FALSE, number 1 is TRUE
4144 if (tv->v_type == VAR_NUMBER
4145 && ct->ct_type->tt_type == VAR_BOOL
4146 && (tv->vval.v_number == 0
4147 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004149 tv->v_type = VAR_BOOL;
4150 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004151 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 }
4153 }
4154 break;
4155
Bram Moolenaar9af78762020-06-16 11:34:42 +02004156 case ISN_CHECKLEN:
4157 {
4158 int min_len = iptr->isn_arg.checklen.cl_min_len;
4159 list_T *list = NULL;
4160
4161 tv = STACK_TV_BOT(-1);
4162 if (tv->v_type == VAR_LIST)
4163 list = tv->vval.v_list;
4164 if (list == NULL || list->lv_len < min_len
4165 || (list->lv_len > min_len
4166 && !iptr->isn_arg.checklen.cl_more_OK))
4167 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004168 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004169 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004170 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004171 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004172 }
4173 }
4174 break;
4175
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004176 case ISN_SETTYPE:
4177 {
4178 checktype_T *ct = &iptr->isn_arg.type;
4179
4180 tv = STACK_TV_BOT(-1);
4181 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4182 {
4183 free_type(tv->vval.v_dict->dv_type);
4184 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4185 }
4186 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4187 {
4188 free_type(tv->vval.v_list->lv_type);
4189 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4190 }
4191 }
4192 break;
4193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004195 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 {
4197 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004198 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004200 if (iptr->isn_type == ISN_2BOOL)
4201 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004202 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004203 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004204 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004205 n = !n;
4206 }
4207 else
4208 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004209 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004210 SOURCING_LNUM = iptr->isn_lnum;
4211 n = tv_get_bool_chk(tv, &error);
4212 if (error)
4213 goto on_error;
4214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 clear_tv(tv);
4216 tv->v_type = VAR_BOOL;
4217 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4218 }
4219 break;
4220
4221 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004222 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004224 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4225 iptr->isn_type == ISN_2STRING_ANY,
4226 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004227 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 break;
4229
Bram Moolenaar08597872020-12-10 19:43:40 +01004230 case ISN_RANGE:
4231 {
4232 exarg_T ea;
4233 char *errormsg;
4234
Bram Moolenaarece0b872021-01-08 20:40:45 +01004235 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004236 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004237 ea.addr_type = ADDR_LINES;
4238 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004239 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004240 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004241 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004242
Bram Moolenaar35578162021-08-02 19:10:38 +02004243 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004244 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004245 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004246 tv = STACK_TV_BOT(-1);
4247 tv->v_type = VAR_NUMBER;
4248 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004249 if (ea.addr_count == 0)
4250 tv->vval.v_number = curwin->w_cursor.lnum;
4251 else
4252 tv->vval.v_number = ea.line2;
4253 }
4254 break;
4255
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004256 case ISN_PUT:
4257 {
4258 int regname = iptr->isn_arg.put.put_regname;
4259 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4260 char_u *expr = NULL;
4261 int dir = FORWARD;
4262
Bram Moolenaar08597872020-12-10 19:43:40 +01004263 if (lnum < -2)
4264 {
4265 // line number was put on the stack by ISN_RANGE
4266 tv = STACK_TV_BOT(-1);
4267 curwin->w_cursor.lnum = tv->vval.v_number;
4268 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4269 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004270 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004271 }
4272 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004273 // :put! above cursor
4274 dir = BACKWARD;
4275 else if (lnum >= 0)
4276 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004277
4278 if (regname == '=')
4279 {
4280 tv = STACK_TV_BOT(-1);
4281 if (tv->v_type == VAR_STRING)
4282 expr = tv->vval.v_string;
4283 else
4284 {
4285 expr = typval2string(tv, TRUE); // allocates value
4286 clear_tv(tv);
4287 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004288 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004289 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004290 check_cursor();
4291 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4292 vim_free(expr);
4293 }
4294 break;
4295
Bram Moolenaar02194d22020-10-24 23:08:38 +02004296 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004297 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4298 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4299 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4300 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004301 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4302 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004303 break;
4304
Bram Moolenaar02194d22020-10-24 23:08:38 +02004305 case ISN_CMDMOD_REV:
4306 // filter regprog is owned by the instruction, don't free it
4307 cmdmod.cmod_filter_regmatch.regprog = NULL;
4308 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004309 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4310 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004311 break;
4312
Bram Moolenaar792f7862020-11-23 08:31:18 +01004313 case ISN_UNPACK:
4314 {
4315 int count = iptr->isn_arg.unpack.unp_count;
4316 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4317 list_T *l;
4318 listitem_T *li;
4319 int i;
4320
4321 // Check there is a valid list to unpack.
4322 tv = STACK_TV_BOT(-1);
4323 if (tv->v_type != VAR_LIST)
4324 {
4325 SOURCING_LNUM = iptr->isn_lnum;
4326 emsg(_(e_for_argument_must_be_sequence_of_lists));
4327 goto on_error;
4328 }
4329 l = tv->vval.v_list;
4330 if (l == NULL
4331 || l->lv_len < (semicolon ? count - 1 : count))
4332 {
4333 SOURCING_LNUM = iptr->isn_lnum;
4334 emsg(_(e_list_value_does_not_have_enough_items));
4335 goto on_error;
4336 }
4337 else if (!semicolon && l->lv_len > count)
4338 {
4339 SOURCING_LNUM = iptr->isn_lnum;
4340 emsg(_(e_list_value_has_more_items_than_targets));
4341 goto on_error;
4342 }
4343
4344 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004345 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004346 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004347 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004348
4349 // Variable after semicolon gets a list with the remaining
4350 // items.
4351 if (semicolon)
4352 {
4353 list_T *rem_list =
4354 list_alloc_with_items(l->lv_len - count + 1);
4355
4356 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004357 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004358 tv = STACK_TV_BOT(-count);
4359 tv->vval.v_list = rem_list;
4360 ++rem_list->lv_refcount;
4361 tv->v_lock = 0;
4362 li = l->lv_first;
4363 for (i = 0; i < count - 1; ++i)
4364 li = li->li_next;
4365 for (i = 0; li != NULL; ++i)
4366 {
4367 list_set_item(rem_list, i, &li->li_tv);
4368 li = li->li_next;
4369 }
4370 --count;
4371 }
4372
4373 // Produce the values in reverse order, first item last.
4374 li = l->lv_first;
4375 for (i = 0; i < count; ++i)
4376 {
4377 tv = STACK_TV_BOT(-i - 1);
4378 copy_tv(&li->li_tv, tv);
4379 li = li->li_next;
4380 }
4381
4382 list_unref(l);
4383 }
4384 break;
4385
Bram Moolenaarb2049902021-01-24 12:53:53 +01004386 case ISN_PROF_START:
4387 case ISN_PROF_END:
4388 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004389#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004390 funccall_T cookie;
4391 ufunc_T *cur_ufunc =
4392 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004393 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004394
4395 cookie.func = cur_ufunc;
4396 if (iptr->isn_type == ISN_PROF_START)
4397 {
4398 func_line_start(&cookie, iptr->isn_lnum);
4399 // if we get here the instruction is executed
4400 func_line_exec(&cookie);
4401 }
4402 else
4403 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004404#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004405 }
4406 break;
4407
Bram Moolenaare99d4222021-06-13 14:01:26 +02004408 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004409 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004410 break;
4411
Bram Moolenaar389df252020-07-09 21:20:47 +02004412 case ISN_SHUFFLE:
4413 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004414 typval_T tmp_tv;
4415 int item = iptr->isn_arg.shuffle.shfl_item;
4416 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004417
4418 tmp_tv = *STACK_TV_BOT(-item);
4419 for ( ; up > 0 && item > 1; --up)
4420 {
4421 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4422 --item;
4423 }
4424 *STACK_TV_BOT(-item) = tmp_tv;
4425 }
4426 break;
4427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004429 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004431 ectx->ec_where.wt_index = 0;
4432 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 break;
4434 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004435 continue;
4436
Bram Moolenaard032f342020-07-18 18:13:02 +02004437func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004438 // Restore previous function. If the frame pointer is where we started
4439 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004440 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004441 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004442
Bram Moolenaar4c137212021-04-19 16:48:48 +02004443 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004444 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004445 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004446 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004447
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004448on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004449 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004450 // If "emsg_silent" is set then ignore the error, unless it was set
4451 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004452 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004453 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004454 {
4455 // If a sequence of instructions causes an error while ":silent!"
4456 // was used, restore the stack length and jump ahead to restoring
4457 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004458 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004459 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004460 while (ectx->ec_stack.ga_len
4461 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004462 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004463 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004464 clear_tv(STACK_TV_BOT(0));
4465 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004466 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4467 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004468 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004469 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004470 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004471on_fatal_error:
4472 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004473 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004474 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004475 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 }
4477
4478done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004479 ret = OK;
4480theend:
4481 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4482 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004483}
4484
4485/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004486 * Execute the instructions from a VAR_INSTR typeval and put the result in
4487 * "rettv".
4488 * Return OK or FAIL.
4489 */
4490 int
4491exe_typval_instr(typval_T *tv, typval_T *rettv)
4492{
4493 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4494 isn_T *save_instr = ectx->ec_instr;
4495 int save_iidx = ectx->ec_iidx;
4496 int res;
4497
4498 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4499 res = exec_instructions(ectx);
4500 if (res == OK)
4501 {
4502 *rettv = *STACK_TV_BOT(-1);
4503 --ectx->ec_stack.ga_len;
4504 }
4505
4506 ectx->ec_instr = save_instr;
4507 ectx->ec_iidx = save_iidx;
4508
4509 return res;
4510}
4511
4512/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004513 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4514 * "substitute_instr".
4515 */
4516 char_u *
4517exe_substitute_instr(void)
4518{
4519 ectx_T *ectx = substitute_instr->subs_ectx;
4520 isn_T *save_instr = ectx->ec_instr;
4521 int save_iidx = ectx->ec_iidx;
4522 char_u *res;
4523
4524 ectx->ec_instr = substitute_instr->subs_instr;
4525 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004526 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004527 typval_T *tv = STACK_TV_BOT(-1);
4528
Bram Moolenaar27523602021-06-05 21:36:19 +02004529 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004530 --ectx->ec_stack.ga_len;
4531 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004532 }
4533 else
4534 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004535 substitute_instr->subs_status = FAIL;
4536 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538
Bram Moolenaar4c137212021-04-19 16:48:48 +02004539 ectx->ec_instr = save_instr;
4540 ectx->ec_iidx = save_iidx;
4541
4542 return res;
4543}
4544
4545/*
4546 * Call a "def" function from old Vim script.
4547 * Return OK or FAIL.
4548 */
4549 int
4550call_def_function(
4551 ufunc_T *ufunc,
4552 int argc_arg, // nr of arguments
4553 typval_T *argv, // arguments
4554 partial_T *partial, // optional partial for context
4555 typval_T *rettv) // return value
4556{
4557 ectx_T ectx; // execution context
4558 int argc = argc_arg;
4559 typval_T *tv;
4560 int idx;
4561 int ret = FAIL;
4562 int defcount = ufunc->uf_args.ga_len - argc;
4563 sctx_T save_current_sctx = current_sctx;
4564 int did_emsg_before = did_emsg_cumul + did_emsg;
4565 int save_suppress_errthrow = suppress_errthrow;
4566 msglist_T **saved_msg_list = NULL;
4567 msglist_T *private_msg_list = NULL;
4568 int save_emsg_silent_def = emsg_silent_def;
4569 int save_did_emsg_def = did_emsg_def;
4570 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004571 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004572
4573// Get pointer to item in the stack.
4574#undef STACK_TV
4575#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4576
4577// Get pointer to item at the bottom of the stack, -1 is the bottom.
4578#undef STACK_TV_BOT
4579#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4580
4581// Get pointer to a local variable on the stack. Negative for arguments.
4582#undef STACK_TV_VAR
4583#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4584
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004585 // Update uf_has_breakpoint if needed.
4586 update_has_breakpoint(ufunc);
4587
Bram Moolenaar4c137212021-04-19 16:48:48 +02004588 if (ufunc->uf_def_status == UF_NOT_COMPILED
4589 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004590 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4591 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004592 == FAIL))
4593 {
4594 if (did_emsg_cumul + did_emsg == did_emsg_before)
4595 semsg(_(e_function_is_not_compiled_str),
4596 printable_func_name(ufunc));
4597 return FAIL;
4598 }
4599
4600 {
4601 // Check the function was really compiled.
4602 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4603 + ufunc->uf_dfunc_idx;
4604 if (INSTRUCTIONS(dfunc) == NULL)
4605 {
4606 iemsg("using call_def_function() on not compiled function");
4607 return FAIL;
4608 }
4609 }
4610
4611 // If depth of calling is getting too high, don't execute the function.
4612 orig_funcdepth = funcdepth_get();
4613 if (funcdepth_increment() == FAIL)
4614 return FAIL;
4615
4616 CLEAR_FIELD(ectx);
4617 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4618 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004619 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004620 {
4621 funcdepth_decrement();
4622 return FAIL;
4623 }
4624 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4625 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4626 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004627 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004628
4629 idx = argc - ufunc->uf_args.ga_len;
4630 if (idx > 0 && ufunc->uf_va_name == NULL)
4631 {
4632 if (idx == 1)
4633 emsg(_(e_one_argument_too_many));
4634 else
4635 semsg(_(e_nr_arguments_too_many), idx);
4636 goto failed_early;
4637 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004638 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4639 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004640 {
4641 if (idx == -1)
4642 emsg(_(e_one_argument_too_few));
4643 else
4644 semsg(_(e_nr_arguments_too_few), -idx);
4645 goto failed_early;
4646 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004647
4648 // Put arguments on the stack, but no more than what the function expects.
4649 // A lambda can be called with more arguments than it uses.
4650 for (idx = 0; idx < argc
4651 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4652 ++idx)
4653 {
4654 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4655 && argv[idx].v_type == VAR_SPECIAL
4656 && argv[idx].vval.v_number == VVAL_NONE)
4657 {
4658 // Use the default value.
4659 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4660 }
4661 else
4662 {
4663 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4664 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004665 ufunc->uf_arg_types[idx], &argv[idx],
4666 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004667 goto failed_early;
4668 copy_tv(&argv[idx], STACK_TV_BOT(0));
4669 }
4670 ++ectx.ec_stack.ga_len;
4671 }
4672
4673 // Turn varargs into a list. Empty list if no args.
4674 if (ufunc->uf_va_name != NULL)
4675 {
4676 int vararg_count = argc - ufunc->uf_args.ga_len;
4677
4678 if (vararg_count < 0)
4679 vararg_count = 0;
4680 else
4681 argc -= vararg_count;
4682 if (exe_newlist(vararg_count, &ectx) == FAIL)
4683 goto failed_early;
4684
4685 // Check the type of the list items.
4686 tv = STACK_TV_BOT(-1);
4687 if (ufunc->uf_va_type != NULL
4688 && ufunc->uf_va_type != &t_list_any
4689 && ufunc->uf_va_type->tt_member != &t_any
4690 && tv->vval.v_list != NULL)
4691 {
4692 type_T *expected = ufunc->uf_va_type->tt_member;
4693 listitem_T *li = tv->vval.v_list->lv_first;
4694
4695 for (idx = 0; idx < vararg_count; ++idx)
4696 {
4697 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004698 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004699 goto failed_early;
4700 li = li->li_next;
4701 }
4702 }
4703
4704 if (defcount > 0)
4705 // Move varargs list to below missing default arguments.
4706 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4707 --ectx.ec_stack.ga_len;
4708 }
4709
4710 // Make space for omitted arguments, will store default value below.
4711 // Any varargs list goes after them.
4712 if (defcount > 0)
4713 for (idx = 0; idx < defcount; ++idx)
4714 {
4715 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4716 ++ectx.ec_stack.ga_len;
4717 }
4718 if (ufunc->uf_va_name != NULL)
4719 ++ectx.ec_stack.ga_len;
4720
4721 // Frame pointer points to just after arguments.
4722 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4723 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4724
4725 {
4726 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4727 + ufunc->uf_dfunc_idx;
4728 ufunc_T *base_ufunc = dfunc->df_ufunc;
4729
4730 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4731 // by copy_func().
4732 if (partial != NULL || base_ufunc->uf_partial != NULL)
4733 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004734 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4735 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004736 goto failed_early;
4737 if (partial != NULL)
4738 {
4739 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4740 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004741 if (current_ectx->ec_outer_ref != NULL
4742 && current_ectx->ec_outer_ref->or_outer != NULL)
4743 ectx.ec_outer_ref->or_outer =
4744 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004745 }
4746 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004747 {
4748 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4749 ++partial->pt_refcount;
4750 ectx.ec_outer_ref->or_partial = partial;
4751 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004752 }
4753 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004754 {
4755 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4756 ++base_ufunc->uf_partial->pt_refcount;
4757 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4758 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004759 }
4760 }
4761
4762 // dummy frame entries
4763 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4764 {
4765 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4766 ++ectx.ec_stack.ga_len;
4767 }
4768
4769 {
4770 // Reserve space for local variables and any closure reference count.
4771 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4772 + ufunc->uf_dfunc_idx;
4773
4774 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4775 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4776 ectx.ec_stack.ga_len += dfunc->df_varcount;
4777 if (dfunc->df_has_closure)
4778 {
4779 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4780 STACK_TV_VAR(idx)->vval.v_number = 0;
4781 ++ectx.ec_stack.ga_len;
4782 }
4783
4784 ectx.ec_instr = INSTRUCTIONS(dfunc);
4785 }
4786
4787 // Following errors are in the function, not the caller.
4788 // Commands behave like vim9script.
4789 estack_push_ufunc(ufunc, 1);
4790 current_sctx = ufunc->uf_script_ctx;
4791 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4792
4793 // Use a specific location for storing error messages to be converted to an
4794 // exception.
4795 saved_msg_list = msg_list;
4796 msg_list = &private_msg_list;
4797
4798 // Do turn errors into exceptions.
4799 suppress_errthrow = FALSE;
4800
4801 // Do not delete the function while executing it.
4802 ++ufunc->uf_calls;
4803
4804 // When ":silent!" was used before calling then we still abort the
4805 // function. If ":silent!" is used in the function then we don't.
4806 emsg_silent_def = emsg_silent;
4807 did_emsg_def = 0;
4808
4809 ectx.ec_where.wt_index = 0;
4810 ectx.ec_where.wt_variable = FALSE;
4811
4812 // Execute the instructions until done.
4813 ret = exec_instructions(&ectx);
4814 if (ret == OK)
4815 {
4816 // function finished, get result from the stack.
4817 if (ufunc->uf_ret_type == &t_void)
4818 {
4819 rettv->v_type = VAR_VOID;
4820 }
4821 else
4822 {
4823 tv = STACK_TV_BOT(-1);
4824 *rettv = *tv;
4825 tv->v_type = VAR_UNKNOWN;
4826 }
4827 }
4828
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004829 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004830 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4831 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004832
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004833 // Deal with any remaining closures, they may be in use somewhere.
4834 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004835 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004836 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004837 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4838 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004839
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004840 estack_pop();
4841 current_sctx = save_current_sctx;
4842
Bram Moolenaarc970e422021-03-17 15:03:04 +01004843 // TODO: when is it safe to delete the function if it is no longer used?
4844 --ufunc->uf_calls;
4845
Bram Moolenaar352134b2020-10-17 22:04:08 +02004846 if (*msg_list != NULL && saved_msg_list != NULL)
4847 {
4848 msglist_T **plist = saved_msg_list;
4849
4850 // Append entries from the current msg_list (uncaught exceptions) to
4851 // the saved msg_list.
4852 while (*plist != NULL)
4853 plist = &(*plist)->next;
4854
4855 *plist = *msg_list;
4856 }
4857 msg_list = saved_msg_list;
4858
Bram Moolenaar4c137212021-04-19 16:48:48 +02004859 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004860 {
4861 cmdmod.cmod_filter_regmatch.regprog = NULL;
4862 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004863 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004864 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004865 emsg_silent_def = save_emsg_silent_def;
4866 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004867
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004868failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004869 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4871 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004872 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004875 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004876 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004877 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004878 if (ectx.ec_outer_ref->or_outer_allocated)
4879 vim_free(ectx.ec_outer_ref->or_outer);
4880 partial_unref(ectx.ec_outer_ref->or_partial);
4881 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004882 }
4883
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004884 // Not sure if this is necessary.
4885 suppress_errthrow = save_suppress_errthrow;
4886
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004887 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
4888 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004889 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004890 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004891 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 return ret;
4893}
4894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004896 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4897 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4898 * "pfx" is prefixed to every line.
4899 */
4900 static void
4901list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4902{
4903 int line_idx = 0;
4904 int prev_current = 0;
4905 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004906 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004907
4908 for (current = 0; current < instr_count; ++current)
4909 {
4910 isn_T *iptr = &instr[current];
4911 char *line;
4912
4913 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004914 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004915 while (line_idx < iptr->isn_lnum
4916 && line_idx < ufunc->uf_lines.ga_len)
4917 {
4918 if (current > prev_current)
4919 {
4920 msg_puts("\n\n");
4921 prev_current = current;
4922 }
4923 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4924 if (line != NULL)
4925 msg(line);
4926 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004927 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4928 {
4929 int first_def_arg = ufunc->uf_args.ga_len
4930 - ufunc->uf_def_args.ga_len;
4931
4932 if (def_arg_idx > 0)
4933 msg_puts("\n\n");
4934 msg_start();
4935 msg_puts(" ");
4936 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4937 first_def_arg + def_arg_idx]);
4938 msg_puts(" = ");
4939 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4940 msg_clr_eos();
4941 msg_end();
4942 }
4943 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004944
4945 switch (iptr->isn_type)
4946 {
4947 case ISN_EXEC:
4948 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4949 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004950 case ISN_EXEC_SPLIT:
4951 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4952 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004953 case ISN_LEGACY_EVAL:
4954 smsg("%s%4d EVAL legacy %s", pfx, current,
4955 iptr->isn_arg.string);
4956 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004957 case ISN_REDIRSTART:
4958 smsg("%s%4d REDIR", pfx, current);
4959 break;
4960 case ISN_REDIREND:
4961 smsg("%s%4d REDIR END%s", pfx, current,
4962 iptr->isn_arg.number ? " append" : "");
4963 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004964 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004965#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004966 smsg("%s%4d CEXPR pre %s", pfx, current,
4967 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004968#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004969 break;
4970 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004971#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004972 {
4973 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4974
4975 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4976 cexpr_get_auname(cer->cer_cmdidx),
4977 cer->cer_forceit ? "!" : "",
4978 cer->cer_cmdline);
4979 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004980#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004981 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004982 case ISN_INSTR:
4983 {
4984 smsg("%s%4d INSTR", pfx, current);
4985 list_instructions(" ", iptr->isn_arg.instr,
4986 INT_MAX, NULL);
4987 msg(" -------------");
4988 }
4989 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004990 case ISN_SUBSTITUTE:
4991 {
4992 subs_T *subs = &iptr->isn_arg.subs;
4993
4994 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4995 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4996 msg(" -------------");
4997 }
4998 break;
4999 case ISN_EXECCONCAT:
5000 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5001 (varnumber_T)iptr->isn_arg.number);
5002 break;
5003 case ISN_ECHO:
5004 {
5005 echo_T *echo = &iptr->isn_arg.echo;
5006
5007 smsg("%s%4d %s %d", pfx, current,
5008 echo->echo_with_white ? "ECHO" : "ECHON",
5009 echo->echo_count);
5010 }
5011 break;
5012 case ISN_EXECUTE:
5013 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005014 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005015 break;
5016 case ISN_ECHOMSG:
5017 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005018 (varnumber_T)(iptr->isn_arg.number));
5019 break;
5020 case ISN_ECHOCONSOLE:
5021 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5022 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005023 break;
5024 case ISN_ECHOERR:
5025 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005026 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005027 break;
5028 case ISN_LOAD:
5029 {
5030 if (iptr->isn_arg.number < 0)
5031 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5032 (varnumber_T)(iptr->isn_arg.number
5033 + STACK_FRAME_SIZE));
5034 else
5035 smsg("%s%4d LOAD $%lld", pfx, current,
5036 (varnumber_T)(iptr->isn_arg.number));
5037 }
5038 break;
5039 case ISN_LOADOUTER:
5040 {
5041 if (iptr->isn_arg.number < 0)
5042 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5043 iptr->isn_arg.outer.outer_depth,
5044 iptr->isn_arg.outer.outer_idx
5045 + STACK_FRAME_SIZE);
5046 else
5047 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5048 iptr->isn_arg.outer.outer_depth,
5049 iptr->isn_arg.outer.outer_idx);
5050 }
5051 break;
5052 case ISN_LOADV:
5053 smsg("%s%4d LOADV v:%s", pfx, current,
5054 get_vim_var_name(iptr->isn_arg.number));
5055 break;
5056 case ISN_LOADSCRIPT:
5057 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005058 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5059 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5060 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005061
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005062 sv = get_script_svar(sref, -1);
5063 if (sv == NULL)
5064 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5065 pfx, current, si->sn_name);
5066 else
5067 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005068 sv->sv_name,
5069 sref->sref_idx,
5070 si->sn_name);
5071 }
5072 break;
5073 case ISN_LOADS:
5074 {
5075 scriptitem_T *si = SCRIPT_ITEM(
5076 iptr->isn_arg.loadstore.ls_sid);
5077
5078 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5079 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5080 }
5081 break;
5082 case ISN_LOADAUTO:
5083 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5084 break;
5085 case ISN_LOADG:
5086 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5087 break;
5088 case ISN_LOADB:
5089 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5090 break;
5091 case ISN_LOADW:
5092 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5093 break;
5094 case ISN_LOADT:
5095 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5096 break;
5097 case ISN_LOADGDICT:
5098 smsg("%s%4d LOAD g:", pfx, current);
5099 break;
5100 case ISN_LOADBDICT:
5101 smsg("%s%4d LOAD b:", pfx, current);
5102 break;
5103 case ISN_LOADWDICT:
5104 smsg("%s%4d LOAD w:", pfx, current);
5105 break;
5106 case ISN_LOADTDICT:
5107 smsg("%s%4d LOAD t:", pfx, current);
5108 break;
5109 case ISN_LOADOPT:
5110 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5111 break;
5112 case ISN_LOADENV:
5113 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5114 break;
5115 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005116 smsg("%s%4d LOADREG @%c", pfx, current,
5117 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005118 break;
5119
5120 case ISN_STORE:
5121 if (iptr->isn_arg.number < 0)
5122 smsg("%s%4d STORE arg[%lld]", pfx, current,
5123 iptr->isn_arg.number + STACK_FRAME_SIZE);
5124 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005125 smsg("%s%4d STORE $%lld", pfx, current,
5126 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005127 break;
5128 case ISN_STOREOUTER:
5129 {
5130 if (iptr->isn_arg.number < 0)
5131 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5132 iptr->isn_arg.outer.outer_depth,
5133 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5134 else
5135 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5136 iptr->isn_arg.outer.outer_depth,
5137 iptr->isn_arg.outer.outer_idx);
5138 }
5139 break;
5140 case ISN_STOREV:
5141 smsg("%s%4d STOREV v:%s", pfx, current,
5142 get_vim_var_name(iptr->isn_arg.number));
5143 break;
5144 case ISN_STOREAUTO:
5145 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5146 break;
5147 case ISN_STOREG:
5148 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5149 break;
5150 case ISN_STOREB:
5151 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5152 break;
5153 case ISN_STOREW:
5154 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5155 break;
5156 case ISN_STORET:
5157 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5158 break;
5159 case ISN_STORES:
5160 {
5161 scriptitem_T *si = SCRIPT_ITEM(
5162 iptr->isn_arg.loadstore.ls_sid);
5163
5164 smsg("%s%4d STORES %s in %s", pfx, current,
5165 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5166 }
5167 break;
5168 case ISN_STORESCRIPT:
5169 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005170 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5171 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5172 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005173
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005174 sv = get_script_svar(sref, -1);
5175 if (sv == NULL)
5176 smsg("%s%4d STORESCRIPT [deleted] in %s",
5177 pfx, current, si->sn_name);
5178 else
5179 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005180 sv->sv_name,
5181 sref->sref_idx,
5182 si->sn_name);
5183 }
5184 break;
5185 case ISN_STOREOPT:
5186 smsg("%s%4d STOREOPT &%s", pfx, current,
5187 iptr->isn_arg.storeopt.so_name);
5188 break;
5189 case ISN_STOREENV:
5190 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5191 break;
5192 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005193 smsg("%s%4d STOREREG @%c", pfx, current,
5194 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005195 break;
5196 case ISN_STORENR:
5197 smsg("%s%4d STORE %lld in $%d", pfx, current,
5198 iptr->isn_arg.storenr.stnr_val,
5199 iptr->isn_arg.storenr.stnr_idx);
5200 break;
5201
5202 case ISN_STOREINDEX:
5203 smsg("%s%4d STOREINDEX %s", pfx, current,
5204 vartype_name(iptr->isn_arg.vartype));
5205 break;
5206
5207 case ISN_STORERANGE:
5208 smsg("%s%4d STORERANGE", pfx, current);
5209 break;
5210
5211 // constants
5212 case ISN_PUSHNR:
5213 smsg("%s%4d PUSHNR %lld", pfx, current,
5214 (varnumber_T)(iptr->isn_arg.number));
5215 break;
5216 case ISN_PUSHBOOL:
5217 case ISN_PUSHSPEC:
5218 smsg("%s%4d PUSH %s", pfx, current,
5219 get_var_special_name(iptr->isn_arg.number));
5220 break;
5221 case ISN_PUSHF:
5222#ifdef FEAT_FLOAT
5223 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5224#endif
5225 break;
5226 case ISN_PUSHS:
5227 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5228 break;
5229 case ISN_PUSHBLOB:
5230 {
5231 char_u *r;
5232 char_u numbuf[NUMBUFLEN];
5233 char_u *tofree;
5234
5235 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5236 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5237 vim_free(tofree);
5238 }
5239 break;
5240 case ISN_PUSHFUNC:
5241 {
5242 char *name = (char *)iptr->isn_arg.string;
5243
5244 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5245 name == NULL ? "[none]" : name);
5246 }
5247 break;
5248 case ISN_PUSHCHANNEL:
5249#ifdef FEAT_JOB_CHANNEL
5250 {
5251 channel_T *channel = iptr->isn_arg.channel;
5252
5253 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5254 channel == NULL ? 0 : channel->ch_id);
5255 }
5256#endif
5257 break;
5258 case ISN_PUSHJOB:
5259#ifdef FEAT_JOB_CHANNEL
5260 {
5261 typval_T tv;
5262 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005263 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005264
5265 tv.v_type = VAR_JOB;
5266 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005267 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005268 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5269 }
5270#endif
5271 break;
5272 case ISN_PUSHEXC:
5273 smsg("%s%4d PUSH v:exception", pfx, current);
5274 break;
5275 case ISN_UNLET:
5276 smsg("%s%4d UNLET%s %s", pfx, current,
5277 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5278 iptr->isn_arg.unlet.ul_name);
5279 break;
5280 case ISN_UNLETENV:
5281 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5282 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5283 iptr->isn_arg.unlet.ul_name);
5284 break;
5285 case ISN_UNLETINDEX:
5286 smsg("%s%4d UNLETINDEX", pfx, current);
5287 break;
5288 case ISN_UNLETRANGE:
5289 smsg("%s%4d UNLETRANGE", pfx, current);
5290 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005291 case ISN_LOCKUNLOCK:
5292 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5293 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005294 case ISN_LOCKCONST:
5295 smsg("%s%4d LOCKCONST", pfx, current);
5296 break;
5297 case ISN_NEWLIST:
5298 smsg("%s%4d NEWLIST size %lld", pfx, current,
5299 (varnumber_T)(iptr->isn_arg.number));
5300 break;
5301 case ISN_NEWDICT:
5302 smsg("%s%4d NEWDICT size %lld", pfx, current,
5303 (varnumber_T)(iptr->isn_arg.number));
5304 break;
5305
5306 // function call
5307 case ISN_BCALL:
5308 {
5309 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5310
5311 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5312 internal_func_name(cbfunc->cbf_idx),
5313 cbfunc->cbf_argcount);
5314 }
5315 break;
5316 case ISN_DCALL:
5317 {
5318 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5319 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5320 + cdfunc->cdf_idx;
5321
5322 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005323 printable_func_name(df->df_ufunc),
5324 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005325 }
5326 break;
5327 case ISN_UCALL:
5328 {
5329 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5330
5331 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5332 cufunc->cuf_name, cufunc->cuf_argcount);
5333 }
5334 break;
5335 case ISN_PCALL:
5336 {
5337 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5338
5339 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5340 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5341 }
5342 break;
5343 case ISN_PCALL_END:
5344 smsg("%s%4d PCALL end", pfx, current);
5345 break;
5346 case ISN_RETURN:
5347 smsg("%s%4d RETURN", pfx, current);
5348 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005349 case ISN_RETURN_VOID:
5350 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005351 break;
5352 case ISN_FUNCREF:
5353 {
5354 funcref_T *funcref = &iptr->isn_arg.funcref;
5355 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5356 + funcref->fr_func;
5357
5358 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5359 }
5360 break;
5361
5362 case ISN_NEWFUNC:
5363 {
5364 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5365
5366 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5367 newfunc->nf_lambda, newfunc->nf_global);
5368 }
5369 break;
5370
5371 case ISN_DEF:
5372 {
5373 char_u *name = iptr->isn_arg.string;
5374
5375 smsg("%s%4d DEF %s", pfx, current,
5376 name == NULL ? (char_u *)"" : name);
5377 }
5378 break;
5379
5380 case ISN_JUMP:
5381 {
5382 char *when = "?";
5383
5384 switch (iptr->isn_arg.jump.jump_when)
5385 {
5386 case JUMP_ALWAYS:
5387 when = "JUMP";
5388 break;
5389 case JUMP_AND_KEEP_IF_TRUE:
5390 when = "JUMP_AND_KEEP_IF_TRUE";
5391 break;
5392 case JUMP_IF_FALSE:
5393 when = "JUMP_IF_FALSE";
5394 break;
5395 case JUMP_AND_KEEP_IF_FALSE:
5396 when = "JUMP_AND_KEEP_IF_FALSE";
5397 break;
5398 case JUMP_IF_COND_FALSE:
5399 when = "JUMP_IF_COND_FALSE";
5400 break;
5401 case JUMP_IF_COND_TRUE:
5402 when = "JUMP_IF_COND_TRUE";
5403 break;
5404 }
5405 smsg("%s%4d %s -> %d", pfx, current, when,
5406 iptr->isn_arg.jump.jump_where);
5407 }
5408 break;
5409
5410 case ISN_JUMP_IF_ARG_SET:
5411 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5412 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5413 iptr->isn_arg.jump.jump_where);
5414 break;
5415
5416 case ISN_FOR:
5417 {
5418 forloop_T *forloop = &iptr->isn_arg.forloop;
5419
5420 smsg("%s%4d FOR $%d -> %d", pfx, current,
5421 forloop->for_idx, forloop->for_end);
5422 }
5423 break;
5424
5425 case ISN_TRY:
5426 {
5427 try_T *try = &iptr->isn_arg.try;
5428
5429 if (try->try_ref->try_finally == 0)
5430 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5431 pfx, current,
5432 try->try_ref->try_catch,
5433 try->try_ref->try_endtry);
5434 else
5435 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5436 pfx, current,
5437 try->try_ref->try_catch,
5438 try->try_ref->try_finally,
5439 try->try_ref->try_endtry);
5440 }
5441 break;
5442 case ISN_CATCH:
5443 // TODO
5444 smsg("%s%4d CATCH", pfx, current);
5445 break;
5446 case ISN_TRYCONT:
5447 {
5448 trycont_T *trycont = &iptr->isn_arg.trycont;
5449
5450 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5451 trycont->tct_levels,
5452 trycont->tct_levels == 1 ? "" : "s",
5453 trycont->tct_where);
5454 }
5455 break;
5456 case ISN_FINALLY:
5457 smsg("%s%4d FINALLY", pfx, current);
5458 break;
5459 case ISN_ENDTRY:
5460 smsg("%s%4d ENDTRY", pfx, current);
5461 break;
5462 case ISN_THROW:
5463 smsg("%s%4d THROW", pfx, current);
5464 break;
5465
5466 // expression operations on number
5467 case ISN_OPNR:
5468 case ISN_OPFLOAT:
5469 case ISN_OPANY:
5470 {
5471 char *what;
5472 char *ins;
5473
5474 switch (iptr->isn_arg.op.op_type)
5475 {
5476 case EXPR_MULT: what = "*"; break;
5477 case EXPR_DIV: what = "/"; break;
5478 case EXPR_REM: what = "%"; break;
5479 case EXPR_SUB: what = "-"; break;
5480 case EXPR_ADD: what = "+"; break;
5481 default: what = "???"; break;
5482 }
5483 switch (iptr->isn_type)
5484 {
5485 case ISN_OPNR: ins = "OPNR"; break;
5486 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5487 case ISN_OPANY: ins = "OPANY"; break;
5488 default: ins = "???"; break;
5489 }
5490 smsg("%s%4d %s %s", pfx, current, ins, what);
5491 }
5492 break;
5493
5494 case ISN_COMPAREBOOL:
5495 case ISN_COMPARESPECIAL:
5496 case ISN_COMPARENR:
5497 case ISN_COMPAREFLOAT:
5498 case ISN_COMPARESTRING:
5499 case ISN_COMPAREBLOB:
5500 case ISN_COMPARELIST:
5501 case ISN_COMPAREDICT:
5502 case ISN_COMPAREFUNC:
5503 case ISN_COMPAREANY:
5504 {
5505 char *p;
5506 char buf[10];
5507 char *type;
5508
5509 switch (iptr->isn_arg.op.op_type)
5510 {
5511 case EXPR_EQUAL: p = "=="; break;
5512 case EXPR_NEQUAL: p = "!="; break;
5513 case EXPR_GREATER: p = ">"; break;
5514 case EXPR_GEQUAL: p = ">="; break;
5515 case EXPR_SMALLER: p = "<"; break;
5516 case EXPR_SEQUAL: p = "<="; break;
5517 case EXPR_MATCH: p = "=~"; break;
5518 case EXPR_IS: p = "is"; break;
5519 case EXPR_ISNOT: p = "isnot"; break;
5520 case EXPR_NOMATCH: p = "!~"; break;
5521 default: p = "???"; break;
5522 }
5523 STRCPY(buf, p);
5524 if (iptr->isn_arg.op.op_ic == TRUE)
5525 strcat(buf, "?");
5526 switch(iptr->isn_type)
5527 {
5528 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5529 case ISN_COMPARESPECIAL:
5530 type = "COMPARESPECIAL"; break;
5531 case ISN_COMPARENR: type = "COMPARENR"; break;
5532 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5533 case ISN_COMPARESTRING:
5534 type = "COMPARESTRING"; break;
5535 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5536 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5537 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5538 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5539 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5540 default: type = "???"; break;
5541 }
5542
5543 smsg("%s%4d %s %s", pfx, current, type, buf);
5544 }
5545 break;
5546
5547 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5548 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5549
5550 // expression operations
5551 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5552 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5553 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5554 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5555 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5556 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5557 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5558 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5559 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5560 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5561 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5562 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005563 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005564 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5565 iptr->isn_arg.getitem.gi_index,
5566 iptr->isn_arg.getitem.gi_with_op ?
5567 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005568 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5569 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5570 iptr->isn_arg.string); break;
5571 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5572
5573 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5574 case ISN_CHECKTYPE:
5575 {
5576 checktype_T *ct = &iptr->isn_arg.type;
5577 char *tofree;
5578
5579 if (ct->ct_arg_idx == 0)
5580 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5581 type_name(ct->ct_type, &tofree),
5582 (int)ct->ct_off);
5583 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005584 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5585 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005586 type_name(ct->ct_type, &tofree),
5587 (int)ct->ct_off,
5588 (int)ct->ct_arg_idx);
5589 vim_free(tofree);
5590 break;
5591 }
5592 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5593 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5594 iptr->isn_arg.checklen.cl_min_len);
5595 break;
5596 case ISN_SETTYPE:
5597 {
5598 char *tofree;
5599
5600 smsg("%s%4d SETTYPE %s", pfx, current,
5601 type_name(iptr->isn_arg.type.ct_type, &tofree));
5602 vim_free(tofree);
5603 break;
5604 }
5605 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005606 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5607 smsg("%s%4d INVERT %d (!val)", pfx, current,
5608 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005609 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005610 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5611 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005612 break;
5613 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005614 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005615 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005616 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5617 pfx, current,
5618 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005619 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005620 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5621 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005622 break;
5623 case ISN_PUT:
5624 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5625 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005626 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005627 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5628 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005629 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005630 else
5631 smsg("%s%4d PUT %c %ld", pfx, current,
5632 iptr->isn_arg.put.put_regname,
5633 (long)iptr->isn_arg.put.put_lnum);
5634 break;
5635
5636 // TODO: summarize modifiers
5637 case ISN_CMDMOD:
5638 {
5639 char_u *buf;
5640 size_t len = produce_cmdmods(
5641 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5642
5643 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005644 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005645 {
5646 (void)produce_cmdmods(
5647 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5648 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5649 vim_free(buf);
5650 }
5651 break;
5652 }
5653 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5654
5655 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005656 smsg("%s%4d PROFILE START line %d", pfx, current,
5657 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005658 break;
5659
5660 case ISN_PROF_END:
5661 smsg("%s%4d PROFILE END", pfx, current);
5662 break;
5663
Bram Moolenaare99d4222021-06-13 14:01:26 +02005664 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005665 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5666 iptr->isn_arg.debug.dbg_break_lnum + 1,
5667 iptr->isn_lnum,
5668 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005669 break;
5670
Bram Moolenaar4c137212021-04-19 16:48:48 +02005671 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5672 iptr->isn_arg.unpack.unp_count,
5673 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5674 break;
5675 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5676 iptr->isn_arg.shuffle.shfl_item,
5677 iptr->isn_arg.shuffle.shfl_up);
5678 break;
5679 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5680
5681 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5682 return;
5683 }
5684
5685 out_flush(); // output one line at a time
5686 ui_breakcheck();
5687 if (got_int)
5688 break;
5689 }
5690}
5691
5692/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005693 * Handle command line completion for the :disassemble command.
5694 */
5695 void
5696set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5697{
5698 char_u *p;
5699
5700 // Default: expand user functions, "debug" and "profile"
5701 xp->xp_context = EXPAND_DISASSEMBLE;
5702 xp->xp_pattern = arg;
5703
5704 // first argument already typed: only user function names
5705 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5706 {
5707 xp->xp_context = EXPAND_USER_FUNC;
5708 xp->xp_pattern = skipwhite(p);
5709 }
5710}
5711
5712/*
5713 * Function given to ExpandGeneric() to obtain the list of :disassemble
5714 * arguments.
5715 */
5716 char_u *
5717get_disassemble_argument(expand_T *xp, int idx)
5718{
5719 if (idx == 0)
5720 return (char_u *)"debug";
5721 if (idx == 1)
5722 return (char_u *)"profile";
5723 return get_user_func_name(xp, idx - 2);
5724}
5725
5726/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005727 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005728 * We don't really need this at runtime, but we do have tests that require it,
5729 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005730 */
5731 void
5732ex_disassemble(exarg_T *eap)
5733{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005734 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005735 char_u *fname;
5736 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737 dfunc_T *dfunc;
5738 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005739 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005740 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005741 compiletype_T compile_type = CT_NONE;
5742
5743 if (STRNCMP(arg, "profile", 7) == 0)
5744 {
5745 compile_type = CT_PROFILE;
5746 arg = skipwhite(arg + 7);
5747 }
5748 else if (STRNCMP(arg, "debug", 5) == 0)
5749 {
5750 compile_type = CT_DEBUG;
5751 arg = skipwhite(arg + 5);
5752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005753
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005754 if (STRNCMP(arg, "<lambda>", 8) == 0)
5755 {
5756 arg += 8;
5757 (void)getdigits(&arg);
5758 fname = vim_strnsave(eap->arg, arg - eap->arg);
5759 }
5760 else
5761 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005762 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005763 if (fname == NULL)
5764 {
5765 semsg(_(e_invarg2), eap->arg);
5766 return;
5767 }
5768
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005769 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005770 if (ufunc == NULL)
5771 {
5772 char_u *p = untrans_function_name(fname);
5773
5774 if (p != NULL)
5775 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005776 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005777 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005778 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779 if (ufunc == NULL)
5780 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005781 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005782 return;
5783 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005784 if (func_needs_compiling(ufunc, compile_type)
5785 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005786 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005787 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005788 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005789 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005790 return;
5791 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005792 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005793
5794 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005795 switch (compile_type)
5796 {
5797 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005798#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005799 instr = dfunc->df_instr_prof;
5800 instr_count = dfunc->df_instr_prof_count;
5801 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005802#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005803 // FALLTHROUGH
5804 case CT_NONE:
5805 instr = dfunc->df_instr;
5806 instr_count = dfunc->df_instr_count;
5807 break;
5808 case CT_DEBUG:
5809 instr = dfunc->df_instr_debug;
5810 instr_count = dfunc->df_instr_debug_count;
5811 break;
5812 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813
Bram Moolenaar4c137212021-04-19 16:48:48 +02005814 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005815}
5816
5817/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005818 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005819 * list, etc. Mostly like what JavaScript does, except that empty list and
5820 * empty dictionary are FALSE.
5821 */
5822 int
5823tv2bool(typval_T *tv)
5824{
5825 switch (tv->v_type)
5826 {
5827 case VAR_NUMBER:
5828 return tv->vval.v_number != 0;
5829 case VAR_FLOAT:
5830#ifdef FEAT_FLOAT
5831 return tv->vval.v_float != 0.0;
5832#else
5833 break;
5834#endif
5835 case VAR_PARTIAL:
5836 return tv->vval.v_partial != NULL;
5837 case VAR_FUNC:
5838 case VAR_STRING:
5839 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5840 case VAR_LIST:
5841 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5842 case VAR_DICT:
5843 return tv->vval.v_dict != NULL
5844 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5845 case VAR_BOOL:
5846 case VAR_SPECIAL:
5847 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5848 case VAR_JOB:
5849#ifdef FEAT_JOB_CHANNEL
5850 return tv->vval.v_job != NULL;
5851#else
5852 break;
5853#endif
5854 case VAR_CHANNEL:
5855#ifdef FEAT_JOB_CHANNEL
5856 return tv->vval.v_channel != NULL;
5857#else
5858 break;
5859#endif
5860 case VAR_BLOB:
5861 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5862 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005863 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005865 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866 break;
5867 }
5868 return FALSE;
5869}
5870
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005871 void
5872emsg_using_string_as(typval_T *tv, int as_number)
5873{
5874 semsg(_(as_number ? e_using_string_as_number_str
5875 : e_using_string_as_bool_str),
5876 tv->vval.v_string == NULL
5877 ? (char_u *)"" : tv->vval.v_string);
5878}
5879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005880/*
5881 * If "tv" is a string give an error and return FAIL.
5882 */
5883 int
5884check_not_string(typval_T *tv)
5885{
5886 if (tv->v_type == VAR_STRING)
5887 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005888 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889 clear_tv(tv);
5890 return FAIL;
5891 }
5892 return OK;
5893}
5894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895
5896#endif // FEAT_EVAL