blob: 16b1bdf38db4fe9f113534b5f2b50b8680097b7a [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
88 int ec_dfunc_idx; // current function index
89 isn_T *ec_instr; // array with instructions
90 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 Moolenaar270d0382020-05-15 21:42:53 +0200140 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
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 {
213 if (ga_grow(&profile_info_ga, 1) == OK)
214 {
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;
292 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
293 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 return FAIL;
295
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100296 // If depth of calling is getting too high, don't execute the function.
297 if (funcdepth_increment() == FAIL)
298 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200299 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100300
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100301 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200302 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100303 {
304 floc = ALLOC_ONE(funclocal_T);
305 if (floc == NULL)
306 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200307 *floc = ectx->ec_funclocal;
308 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100309 }
310
Bram Moolenaarfe270812020-04-11 22:31:27 +0200311 // Move the vararg-list to below the missing optional arguments.
312 if (vararg_count > 0 && arg_to_add > 0)
313 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100314
315 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200316 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200317 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200318 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319
320 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100321 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
322 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200323 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200324 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
325 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100326 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100327 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200328 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329
330 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200331 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200333 if (dfunc->df_has_closure)
334 {
335 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
336
337 tv->v_type = VAR_NUMBER;
338 tv->vval.v_number = 0;
339 }
340 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100341
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100342 if (pt != NULL || ufunc->uf_partial != NULL
343 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100344 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200345 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100346
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200347 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100348 return FAIL;
349 if (pt != NULL)
350 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200351 ref->or_outer = &pt->pt_outer;
352 ++pt->pt_refcount;
353 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100354 }
355 else if (ufunc->uf_partial != NULL)
356 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200357 ref->or_outer = &ufunc->uf_partial->pt_outer;
358 ++ufunc->uf_partial->pt_refcount;
359 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100360 }
361 else
362 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200363 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
364 if (ref->or_outer == NULL)
365 {
366 vim_free(ref);
367 return FAIL;
368 }
369 ref->or_outer_allocated = TRUE;
370 ref->or_outer->out_stack = &ectx->ec_stack;
371 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
372 if (ectx->ec_outer_ref != NULL)
373 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100374 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200375 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100376 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100377 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200378 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100379
Bram Moolenaarc970e422021-03-17 15:03:04 +0100380 ++ufunc->uf_calls;
381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382 // Set execution state to the start of the called function.
383 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100384 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100385 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200386 if (entry != NULL)
387 {
388 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200389 // Save the current context so it can be restored on return.
390 entry->es_save_sctx = current_sctx;
391 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200392 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100393
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200394 // Start execution at the first instruction.
395 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396
397 return OK;
398}
399
400// Get pointer to item in the stack.
401#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
402
403/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 * Used when returning from a function: Check if any closure is still
405 * referenced. If so then move the arguments and variables to a separate piece
406 * of stack to be used when the closure is called.
407 * When "free_arguments" is TRUE the arguments are to be freed.
408 * Returns FAIL when out of memory.
409 */
410 static int
411handle_closure_in_use(ectx_T *ectx, int free_arguments)
412{
413 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
414 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200415 int argcount;
416 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417 int idx;
418 typval_T *tv;
419 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200420 garray_T *gap = &ectx->ec_funcrefs;
421 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200422
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200423 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200424 return OK; // function was freed
425 if (dfunc->df_has_closure == 0)
426 return OK; // no closures
427 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
428 closure_count = tv->vval.v_number;
429 if (closure_count == 0)
430 return OK; // no funcrefs created
431
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200432 argcount = ufunc_argcount(dfunc->df_ufunc);
433 top = ectx->ec_frame_idx - argcount;
434
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200435 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200436 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200437 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200438 partial_T *pt;
439 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200440
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200441 if (off < 0)
442 continue; // count is off or already done
443 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200444 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200445 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200446 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200447 int i;
448
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200449 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200450 // unreferenced on return.
451 for (i = 0; i < dfunc->df_varcount; ++i)
452 {
453 typval_T *stv = STACK_TV(ectx->ec_frame_idx
454 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200455 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200456 --refcount;
457 }
458 if (refcount > 1)
459 {
460 closure_in_use = TRUE;
461 break;
462 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200463 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200464 }
465
466 if (closure_in_use)
467 {
468 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
469 typval_T *stack;
470
471 // A closure is using the arguments and/or local variables.
472 // Move them to the called function.
473 if (funcstack == NULL)
474 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200475 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
476 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200477 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
478 funcstack->fs_ga.ga_data = stack;
479 if (stack == NULL)
480 {
481 vim_free(funcstack);
482 return FAIL;
483 }
484
485 // Move or copy the arguments.
486 for (idx = 0; idx < argcount; ++idx)
487 {
488 tv = STACK_TV(top + idx);
489 if (free_arguments)
490 {
491 *(stack + idx) = *tv;
492 tv->v_type = VAR_UNKNOWN;
493 }
494 else
495 copy_tv(tv, stack + idx);
496 }
497 // Move the local variables.
498 for (idx = 0; idx < dfunc->df_varcount; ++idx)
499 {
500 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200501
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200502 // A partial created for a local function, that is also used as a
503 // local variable, has a reference count for the variable, thus
504 // will never go down to zero. When all these refcounts are one
505 // then the funcstack is unused. We need to count how many we have
506 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200507 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
508 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200509 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200510
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200511 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200512 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
513 gap->ga_len - closure_count + i])
514 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200515 }
516
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200517 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200518 tv->v_type = VAR_UNKNOWN;
519 }
520
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200521 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200522 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200523 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
524 - closure_count + idx];
525 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200526 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200527 ++funcstack->fs_refcount;
528 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100529 pt->pt_outer.out_stack = &funcstack->fs_ga;
530 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200531 }
532 }
533 }
534
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200535 for (idx = 0; idx < closure_count; ++idx)
536 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
537 - closure_count + idx]);
538 gap->ga_len -= closure_count;
539 if (gap->ga_len == 0)
540 ga_clear(gap);
541
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200542 return OK;
543}
544
545/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200546 * Called when a partial is freed or its reference count goes down to one. The
547 * funcstack may be the only reference to the partials in the local variables.
548 * Go over all of them, the funcref and can be freed if all partials
549 * referencing the funcstack have a reference count of one.
550 */
551 void
552funcstack_check_refcount(funcstack_T *funcstack)
553{
554 int i;
555 garray_T *gap = &funcstack->fs_ga;
556 int done = 0;
557
558 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
559 return;
560 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
561 {
562 typval_T *tv = ((typval_T *)gap->ga_data) + i;
563
564 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
565 && tv->vval.v_partial->pt_funcstack == funcstack
566 && tv->vval.v_partial->pt_refcount == 1)
567 ++done;
568 }
569 if (done == funcstack->fs_min_refcount)
570 {
571 typval_T *stack = gap->ga_data;
572
573 // All partials referencing the funcstack have a reference count of
574 // one, thus the funcstack is no longer of use.
575 for (i = 0; i < gap->ga_len; ++i)
576 clear_tv(stack + i);
577 vim_free(stack);
578 vim_free(funcstack);
579 }
580}
581
582/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 * Return from the current function.
584 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200585 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200586func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100589 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200590 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
591 + ectx->ec_dfunc_idx;
592 int argcount = ufunc_argcount(dfunc->df_ufunc);
593 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200594 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100595 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
596 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200597 funclocal_T *floc;
598#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100599 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
600 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601
Bram Moolenaar12d26532021-02-19 19:13:21 +0100602 if (do_profiling == PROF_YES)
603 {
604 ufunc_T *caller = prev_dfunc->df_ufunc;
605
606 if (dfunc->df_ufunc->uf_profiling
607 || (caller != NULL && caller->uf_profiling))
608 {
609 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
610 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
611 --profile_info_ga.ga_len;
612 }
613 }
614#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100615 // TODO: when is it safe to delete the function when it is no longer used?
616 --dfunc->df_ufunc->uf_calls;
617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200619 entry = estack_pop();
620 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200621 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200623 if (handle_closure_in_use(ectx, TRUE) == FAIL)
624 return FAIL;
625
626 // Clear the arguments.
627 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
628 clear_tv(STACK_TV(idx));
629
630 // Clear local variables and temp values, but not the return value.
631 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100632 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100634
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100635 // The return value should be on top of the stack. However, when aborting
636 // it may not be there and ec_frame_idx is the top of the stack.
637 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100638 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100639 ret_idx = 0;
640
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200641 if (ectx->ec_outer_ref != NULL)
642 {
643 if (ectx->ec_outer_ref->or_outer_allocated)
644 vim_free(ectx->ec_outer_ref->or_outer);
645 partial_unref(ectx->ec_outer_ref->or_partial);
646 vim_free(ectx->ec_outer_ref);
647 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100648
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100649 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100650 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100651 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
652 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200653 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
654 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200655 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100656 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100657 floc = (void *)STACK_TV(ectx->ec_frame_idx
658 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200659 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100660 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
661 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200662
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100663 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200664 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100665 else
666 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200667 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100668 vim_free(floc);
669 }
670
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100671 if (ret_idx > 0)
672 {
673 // Reset the stack to the position before the call, with a spot for the
674 // return value, moved there from above the frame.
675 ectx->ec_stack.ga_len = top + 1;
676 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
677 }
678 else
679 // Reset the stack to the position before the call.
680 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200681
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100682 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200683 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200684 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685}
686
687#undef STACK_TV
688
689/*
690 * Prepare arguments and rettv for calling a builtin or user function.
691 */
692 static int
693call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
694{
695 int idx;
696 typval_T *tv;
697
698 // Move arguments from bottom of the stack to argvars[] and add terminator.
699 for (idx = 0; idx < argcount; ++idx)
700 argvars[idx] = *STACK_TV_BOT(idx - argcount);
701 argvars[argcount].v_type = VAR_UNKNOWN;
702
703 // Result replaces the arguments on the stack.
704 if (argcount > 0)
705 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200706 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 return FAIL;
708 else
709 ++ectx->ec_stack.ga_len;
710
711 // Default return value is zero.
712 tv = STACK_TV_BOT(-1);
713 tv->v_type = VAR_NUMBER;
714 tv->vval.v_number = 0;
715
716 return OK;
717}
718
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200719// Ugly global to avoid passing the execution context around through many
720// layers.
721static ectx_T *current_ectx = NULL;
722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723/*
724 * Call a builtin function by index.
725 */
726 static int
727call_bfunc(int func_idx, int argcount, ectx_T *ectx)
728{
729 typval_T argvars[MAX_FUNC_ARGS];
730 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100731 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200732 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200733 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734
735 if (call_prepare(argcount, argvars, ectx) == FAIL)
736 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200737 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200739 // Call the builtin function. Set "current_ectx" so that when it
740 // recursively invokes call_def_function() a closure context can be set.
741 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200743 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200744 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745
746 // Clear the arguments.
747 for (idx = 0; idx < argcount; ++idx)
748 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200749
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100750 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200751 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 return OK;
753}
754
755/*
756 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100757 * If the function is compiled this will add a stack frame and set the
758 * instruction pointer at the start of the function.
759 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200760 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100761 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 */
763 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100764call_ufunc(
765 ufunc_T *ufunc,
766 partial_T *pt,
767 int argcount,
768 ectx_T *ectx,
769 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770{
771 typval_T argvars[MAX_FUNC_ARGS];
772 funcexe_T funcexe;
773 int error;
774 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100775 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200776 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777
Bram Moolenaare99d4222021-06-13 14:01:26 +0200778 if (func_needs_compiling(ufunc, compile_type)
779 && compile_def_function(ufunc, FALSE, compile_type, NULL)
780 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200781 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200782 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100783 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100784 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100785 if (error != FCERR_UNKNOWN)
786 {
787 if (error == FCERR_TOOMANY)
788 semsg(_(e_toomanyarg), ufunc->uf_name);
789 else
790 semsg(_(e_toofewarg), ufunc->uf_name);
791 return FAIL;
792 }
793
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100794 // The function has been compiled, can call it quickly. For a function
795 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100796 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100797 if (iptr != NULL)
798 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100799 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100800 iptr->isn_type = ISN_DCALL;
801 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
802 iptr->isn_arg.dfunc.cdf_argcount = argcount;
803 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200804 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806
807 if (call_prepare(argcount, argvars, ectx) == FAIL)
808 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200809 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 funcexe.evaluate = TRUE;
811
812 // Call the user function. Result goes in last position on the stack.
813 // TODO: add selfdict if there is one
814 error = call_user_func_check(ufunc, argcount, argvars,
815 STACK_TV_BOT(-1), &funcexe, NULL);
816
817 // Clear the arguments.
818 for (idx = 0; idx < argcount; ++idx)
819 clear_tv(&argvars[idx]);
820
821 if (error != FCERR_NONE)
822 {
823 user_func_error(error, ufunc->uf_name);
824 return FAIL;
825 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100826 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200827 // Error other than from calling the function itself.
828 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 return OK;
830}
831
832/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100833 * If command modifiers were applied restore them.
834 */
835 static void
836may_restore_cmdmod(funclocal_T *funclocal)
837{
838 if (funclocal->floc_restore_cmdmod)
839 {
840 cmdmod.cmod_filter_regmatch.regprog = NULL;
841 undo_cmdmod(&cmdmod);
842 cmdmod = funclocal->floc_save_cmdmod;
843 funclocal->floc_restore_cmdmod = FALSE;
844 }
845}
846
847/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200848 * Return TRUE if an error was given or CTRL-C was pressed.
849 */
850 static int
851vim9_aborting(int prev_called_emsg)
852{
853 return called_emsg > prev_called_emsg || got_int || did_throw;
854}
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 {
886 int called_emsg_before = called_emsg;
887
888 if (script_autoload(name, TRUE))
889 // loaded a package, search for the function again
890 ufunc = find_func(name, FALSE, NULL);
891 if (vim9_aborting(called_emsg_before))
892 return FAIL; // bail out if loading the script caused an error
893 }
894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100896 {
897 if (ufunc->uf_arg_types != NULL)
898 {
899 int i;
900 typval_T *argv = STACK_TV_BOT(0) - argcount;
901
902 // The function can change at runtime, check that the argument
903 // types are correct.
904 for (i = 0; i < argcount; ++i)
905 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100906 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100907
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100908 if (i < ufunc->uf_args.ga_len)
909 type = ufunc->uf_arg_types[i];
910 else if (ufunc->uf_va_type != NULL)
911 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100912 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200913 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100914 return FAIL;
915 }
916 }
917
Bram Moolenaar4c137212021-04-19 16:48:48 +0200918 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920
921 return FAIL;
922}
923
924 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100925call_partial(
926 typval_T *tv,
927 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100928 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200930 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200931 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100933 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935 if (tv->v_type == VAR_PARTIAL)
936 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200937 partial_T *pt = tv->vval.v_partial;
938 int i;
939
940 if (pt->pt_argc > 0)
941 {
942 // Make space for arguments from the partial, shift the "argcount"
943 // arguments up.
944 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
945 return FAIL;
946 for (i = 1; i <= argcount; ++i)
947 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
948 ectx->ec_stack.ga_len += pt->pt_argc;
949 argcount += pt->pt_argc;
950
951 // copy the arguments from the partial onto the stack
952 for (i = 0; i < pt->pt_argc; ++i)
953 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955
956 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200957 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 name = pt->pt_name;
960 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200961 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200963 if (name != NULL)
964 {
965 char_u fname_buf[FLEN_FIXED + 1];
966 char_u *tofree = NULL;
967 int error = FCERR_NONE;
968 char_u *fname;
969
970 // May need to translate <SNR>123_ to K_SNR.
971 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
972 if (error != FCERR_NONE)
973 res = FAIL;
974 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200975 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200976 vim_free(tofree);
977 }
978
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100979 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 {
981 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200982 semsg(_(e_unknownfunc),
983 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 return FAIL;
985 }
986 return OK;
987}
988
989/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200990 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
991 * TRUE.
992 */
993 static int
994error_if_locked(int lock, char *error)
995{
996 if (lock & (VAR_LOCKED | VAR_FIXED))
997 {
998 emsg(_(error));
999 return TRUE;
1000 }
1001 return FALSE;
1002}
1003
1004/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001005 * Give an error if "tv" is not a number and return FAIL.
1006 */
1007 static int
1008check_for_number(typval_T *tv)
1009{
1010 if (tv->v_type != VAR_NUMBER)
1011 {
1012 semsg(_(e_expected_str_but_got_str),
1013 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1014 return FAIL;
1015 }
1016 return OK;
1017}
1018
1019/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001020 * Store "tv" in variable "name".
1021 * This is for s: and g: variables.
1022 */
1023 static void
1024store_var(char_u *name, typval_T *tv)
1025{
1026 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001027 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001028
Bram Moolenaard877a572021-04-01 19:42:48 +02001029 if (tv->v_lock)
1030 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001031 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001032 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001033 restore_funccal();
1034}
1035
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001036/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001037 * Convert "tv" to a string.
1038 * Return FAIL if not allowed.
1039 */
1040 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001041do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001042{
1043 if (tv->v_type != VAR_STRING)
1044 {
1045 char_u *str;
1046
1047 if (is_2string_any)
1048 {
1049 switch (tv->v_type)
1050 {
1051 case VAR_SPECIAL:
1052 case VAR_BOOL:
1053 case VAR_NUMBER:
1054 case VAR_FLOAT:
1055 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001056
1057 case VAR_LIST:
1058 if (tolerant)
1059 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001060 char_u *s, *e, *p;
1061 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001062
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001063 ga_init2(&ga, sizeof(char_u *), 1);
1064
1065 // Convert to NL separated items, then
1066 // escape the items and replace the NL with
1067 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001068 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001069 if (str == NULL)
1070 return FAIL;
1071 s = str;
1072 while ((e = vim_strchr(s, '\n')) != NULL)
1073 {
1074 *e = NUL;
1075 p = vim_strsave_fnameescape(s, FALSE);
1076 if (p != NULL)
1077 {
1078 ga_concat(&ga, p);
1079 ga_concat(&ga, (char_u *)" ");
1080 vim_free(p);
1081 }
1082 s = e + 1;
1083 }
1084 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001085 clear_tv(tv);
1086 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001087 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001088 return OK;
1089 }
1090 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001091 default: to_string_error(tv->v_type);
1092 return FAIL;
1093 }
1094 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001095 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001096 clear_tv(tv);
1097 tv->v_type = VAR_STRING;
1098 tv->vval.v_string = str;
1099 }
1100 return OK;
1101}
1102
1103/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001104 * When the value of "sv" is a null list of dict, allocate it.
1105 */
1106 static void
1107allocate_if_null(typval_T *tv)
1108{
1109 switch (tv->v_type)
1110 {
1111 case VAR_LIST:
1112 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001113 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001114 break;
1115 case VAR_DICT:
1116 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001117 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001118 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001119 case VAR_BLOB:
1120 if (tv->vval.v_blob == NULL)
1121 (void)rettv_blob_alloc(tv);
1122 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001123 default:
1124 break;
1125 }
1126}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001127
Bram Moolenaare7525c52021-01-09 13:20:37 +01001128/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001129 * Return the character "str[index]" where "index" is the character index,
1130 * including composing characters.
1131 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001132 */
1133 char_u *
1134char_from_string(char_u *str, varnumber_T index)
1135{
1136 size_t nbyte = 0;
1137 varnumber_T nchar = index;
1138 size_t slen;
1139
1140 if (str == NULL)
1141 return NULL;
1142 slen = STRLEN(str);
1143
Bram Moolenaarff871402021-03-26 13:34:05 +01001144 // Do the same as for a list: a negative index counts from the end.
1145 // Optimization to check the first byte to be below 0x80 (and no composing
1146 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001147 if (index < 0)
1148 {
1149 int clen = 0;
1150
1151 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001152 {
1153 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1154 ++nbyte;
1155 else if (enc_utf8)
1156 nbyte += utfc_ptr2len(str + nbyte);
1157 else
1158 nbyte += mb_ptr2len(str + nbyte);
1159 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001160 nchar = clen + index;
1161 if (nchar < 0)
1162 // unlike list: index out of range results in empty string
1163 return NULL;
1164 }
1165
1166 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001167 {
1168 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1169 ++nbyte;
1170 else if (enc_utf8)
1171 nbyte += utfc_ptr2len(str + nbyte);
1172 else
1173 nbyte += mb_ptr2len(str + nbyte);
1174 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001175 if (nbyte >= slen)
1176 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001177 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001178}
1179
1180/*
1181 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001182 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001183 * If going over the end return "str_len".
1184 * If "idx" is negative count from the end, -1 is the last character.
1185 * When going over the start return -1.
1186 */
1187 static long
1188char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1189{
1190 varnumber_T nchar = idx;
1191 size_t nbyte = 0;
1192
1193 if (nchar >= 0)
1194 {
1195 while (nchar > 0 && nbyte < str_len)
1196 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001197 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001198 --nchar;
1199 }
1200 }
1201 else
1202 {
1203 nbyte = str_len;
1204 while (nchar < 0 && nbyte > 0)
1205 {
1206 --nbyte;
1207 nbyte -= mb_head_off(str, str + nbyte);
1208 ++nchar;
1209 }
1210 if (nchar < 0)
1211 return -1;
1212 }
1213 return (long)nbyte;
1214}
1215
1216/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001217 * Return the slice "str[first : last]" using character indexes. Composing
1218 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001219 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001220 * Return NULL when the result is empty.
1221 */
1222 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001223string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001224{
1225 long start_byte, end_byte;
1226 size_t slen;
1227
1228 if (str == NULL)
1229 return NULL;
1230 slen = STRLEN(str);
1231 start_byte = char_idx2byte(str, slen, first);
1232 if (start_byte < 0)
1233 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001234 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001235 end_byte = (long)slen;
1236 else
1237 {
1238 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001239 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001240 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001241 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001242 }
1243
1244 if (start_byte >= (long)slen || end_byte <= start_byte)
1245 return NULL;
1246 return vim_strnsave(str + start_byte, end_byte - start_byte);
1247}
1248
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001249 static svar_T *
1250get_script_svar(scriptref_T *sref, ectx_T *ectx)
1251{
1252 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1253 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1254 + ectx->ec_dfunc_idx;
1255 svar_T *sv;
1256
1257 if (sref->sref_seq != si->sn_script_seq)
1258 {
1259 // The script was reloaded after the function was
1260 // compiled, the script_idx may not be valid.
1261 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1262 dfunc->df_ufunc->uf_name_exp);
1263 return NULL;
1264 }
1265 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1266 if (!equal_type(sv->sv_type, sref->sref_type))
1267 {
1268 emsg(_(e_script_variable_type_changed));
1269 return NULL;
1270 }
1271 return sv;
1272}
1273
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001274/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001275 * Function passed to do_cmdline() for splitting a script joined by NL
1276 * characters.
1277 */
1278 static char_u *
1279get_split_sourceline(
1280 int c UNUSED,
1281 void *cookie,
1282 int indent UNUSED,
1283 getline_opt_T options UNUSED)
1284{
1285 source_cookie_T *sp = (source_cookie_T *)cookie;
1286 char_u *p;
1287 char_u *line;
1288
1289 if (*sp->nextline == NUL)
1290 return NULL;
1291 p = vim_strchr(sp->nextline, '\n');
1292 if (p == NULL)
1293 {
1294 line = vim_strsave(sp->nextline);
1295 sp->nextline += STRLEN(sp->nextline);
1296 }
1297 else
1298 {
1299 line = vim_strnsave(sp->nextline, p - sp->nextline);
1300 sp->nextline = p + 1;
1301 }
1302 return line;
1303}
1304
1305/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 * Execute a function by "name".
1307 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001308 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 */
1310 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001311call_eval_func(
1312 char_u *name,
1313 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001314 ectx_T *ectx,
1315 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316{
Bram Moolenaared677f52020-08-12 16:38:10 +02001317 int called_emsg_before = called_emsg;
1318 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319
Bram Moolenaar4c137212021-04-19 16:48:48 +02001320 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001321 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001323 dictitem_T *v;
1324
1325 v = find_var(name, NULL, FALSE);
1326 if (v == NULL)
1327 {
1328 semsg(_(e_unknownfunc), name);
1329 return FAIL;
1330 }
1331 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1332 {
1333 semsg(_(e_unknownfunc), name);
1334 return FAIL;
1335 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001336 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001338 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339}
1340
1341/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001342 * When a function reference is used, fill a partial with the information
1343 * needed, especially when it is used as a closure.
1344 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001345 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001346fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1347{
1348 pt->pt_func = ufunc;
1349 pt->pt_refcount = 1;
1350
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001351 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001352 {
1353 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1354 + ectx->ec_dfunc_idx;
1355
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001356 // The closure may need to find arguments and local variables in the
1357 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001358 pt->pt_outer.out_stack = &ectx->ec_stack;
1359 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001360 if (ectx->ec_outer_ref != NULL)
1361 {
1362 // The current context already has a context, link to that one.
1363 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1364 if (ectx->ec_outer_ref->or_partial != NULL)
1365 {
1366 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1367 ++pt->pt_outer.out_up_partial->pt_refcount;
1368 }
1369 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001370
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001371 // If this function returns and the closure is still being used, we
1372 // need to make a copy of the context (arguments and local variables).
1373 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001374 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1375 {
1376 vim_free(pt);
1377 return FAIL;
1378 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001379 // Extra variable keeps the count of closures created in the current
1380 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001381 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1382 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1383
1384 ((partial_T **)ectx->ec_funcrefs.ga_data)
1385 [ectx->ec_funcrefs.ga_len] = pt;
1386 ++pt->pt_refcount;
1387 ++ectx->ec_funcrefs.ga_len;
1388 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001389 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001390 return OK;
1391}
1392
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001393// used for v_instr of typval of VAR_INSTR
1394struct instr_S {
1395 ectx_T *instr_ectx;
1396 isn_T *instr_instr;
1397};
1398
Bram Moolenaar4c137212021-04-19 16:48:48 +02001399// used for substitute_instr
1400typedef struct subs_expr_S {
1401 ectx_T *subs_ectx;
1402 isn_T *subs_instr;
1403 int subs_status;
1404} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405
1406// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001407#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
1409// Get pointer to item at the bottom of the stack, -1 is the bottom.
1410#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001411#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 +01001412
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001413// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001414#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 +01001415
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001416// Set when calling do_debug().
1417static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001418static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001419
1420/*
1421 * When debugging lookup "name" and return the typeval.
1422 * When not found return NULL.
1423 */
1424 typval_T *
1425lookup_debug_var(char_u *name)
1426{
1427 int idx;
1428 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001429 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001430 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001431 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001432
1433 if (ectx == NULL)
1434 return NULL;
1435 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1436
1437 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001438 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001439 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001440 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001441 return STACK_TV_VAR(idx);
1442 }
1443
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001444 // Go through argument names.
1445 ufunc = dfunc->df_ufunc;
1446 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1447 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1448 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1449 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1450 - varargs_off + idx);
1451 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1452 return STACK_TV(ectx->ec_frame_idx - 1);
1453
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001454 return NULL;
1455}
1456
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001457 static void
1458handle_debug(isn_T *iptr, ectx_T *ectx)
1459{
1460 char_u *line;
1461 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1462 + ectx->ec_dfunc_idx)->df_ufunc;
1463 isn_T *ni;
1464 int end_lnum = iptr->isn_lnum;
1465 garray_T ga;
1466 int lnum;
1467
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001468 if (ex_nesting_level > debug_break_level)
1469 {
1470 linenr_T breakpoint;
1471
1472 if (!ufunc->uf_has_breakpoint)
1473 return;
1474
1475 // check for the next breakpoint if needed
1476 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001477 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001478 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1479 return;
1480 }
1481
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001482 SOURCING_LNUM = iptr->isn_lnum;
1483 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001484 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001485
1486 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1487 if (ni->isn_type == ISN_DEBUG
1488 || ni->isn_type == ISN_RETURN
1489 || ni->isn_type == ISN_RETURN_VOID)
1490 {
1491 end_lnum = ni->isn_lnum;
1492 break;
1493 }
1494
1495 if (end_lnum > iptr->isn_lnum)
1496 {
1497 ga_init2(&ga, sizeof(char_u *), 10);
1498 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001499 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001500 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001501
Bram Moolenaar303215d2021-07-07 20:10:43 +02001502 if (p == NULL)
1503 continue; // left over from continuation line
1504 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001505 if (*p == '#')
1506 break;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001507 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001508 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1509 if (STRNCMP(p, "def ", 4) == 0)
1510 break;
1511 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001512 line = ga_concat_strings(&ga, " ");
1513 vim_free(ga.ga_data);
1514 }
1515 else
1516 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001517
Dominique Pelle6e969552021-06-17 13:53:41 +02001518 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001519 debug_context = NULL;
1520
1521 if (end_lnum > iptr->isn_lnum)
1522 vim_free(line);
1523}
1524
Bram Moolenaar4c137212021-04-19 16:48:48 +02001525/*
1526 * Execute instructions in execution context "ectx".
1527 * Return OK or FAIL;
1528 */
1529 static int
1530exec_instructions(ectx_T *ectx)
1531{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001532 int ret = FAIL;
1533 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001534
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001535 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001536 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001537
Bram Moolenaarff652882021-05-16 15:24:49 +02001538 // Only catch exceptions in this instruction list.
1539 ectx->ec_trylevel_at_start = trylevel;
1540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 for (;;)
1542 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001543 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001545 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001546
Bram Moolenaar270d0382020-05-15 21:42:53 +02001547 if (++breakcheck_count >= 100)
1548 {
1549 line_breakcheck();
1550 breakcheck_count = 0;
1551 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001552 if (got_int)
1553 {
1554 // Turn CTRL-C into an exception.
1555 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001556 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001557 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001558 did_throw = TRUE;
1559 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560
Bram Moolenaara26b9702020-04-18 19:53:28 +02001561 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1562 {
1563 // Turn an error message into an exception.
1564 did_emsg = FALSE;
1565 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001566 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001567 did_throw = TRUE;
1568 *msg_list = NULL;
1569 }
1570
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001571 if (did_throw)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001573 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001574 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001575 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576
1577 // An exception jumps to the first catch, finally, or returns from
1578 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001579 while (index > 0)
1580 {
1581 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001582 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001583 break;
1584 // In the catch and finally block of this try we have to go up
1585 // one level.
1586 --index;
1587 trycmd = NULL;
1588 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001589 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001591 if (trycmd->tcd_in_catch)
1592 {
1593 // exception inside ":catch", jump to ":finally" once
1594 ectx->ec_iidx = trycmd->tcd_finally_idx;
1595 trycmd->tcd_finally_idx = 0;
1596 }
1597 else
1598 // jump to first ":catch"
1599 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001600 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001601 did_throw = FALSE; // don't come back here until :endtry
1602 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 }
1604 else
1605 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001606 // Not inside try or need to return from current functions.
1607 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001608 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001609 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001610 tv = STACK_TV_BOT(0);
1611 tv->v_type = VAR_NUMBER;
1612 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001613 ++ectx->ec_stack.ga_len;
1614 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001616 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001617 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001618 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001619 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620 goto done;
1621 }
1622
Bram Moolenaar4c137212021-04-19 16:48:48 +02001623 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001624 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 }
1626 continue;
1627 }
1628
Bram Moolenaar4c137212021-04-19 16:48:48 +02001629 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 switch (iptr->isn_type)
1631 {
1632 // execute Ex command line
1633 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001634 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001635 source_cookie_T cookie;
1636
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001637 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001638 // Pass getsourceline to get an error for a missing ":end"
1639 // command.
1640 CLEAR_FIELD(cookie);
1641 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1642 if (do_cmdline(iptr->isn_arg.string,
1643 getsourceline, &cookie,
1644 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1645 == FAIL
1646 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001647 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 break;
1650
Bram Moolenaar20677332021-06-06 17:02:53 +02001651 // execute Ex command line split at NL characters.
1652 case ISN_EXEC_SPLIT:
1653 {
1654 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001655 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001656
1657 SOURCING_LNUM = iptr->isn_lnum;
1658 CLEAR_FIELD(cookie);
1659 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1660 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001661 line = get_split_sourceline(0, &cookie, 0, 0);
1662 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001663 get_split_sourceline, &cookie,
1664 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1665 == FAIL
1666 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001667 {
1668 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001669 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001670 }
1671 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001672 }
1673 break;
1674
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001675 // Evaluate an expression with legacy syntax, push it onto the
1676 // stack.
1677 case ISN_LEGACY_EVAL:
1678 {
1679 char_u *arg = iptr->isn_arg.string;
1680 int res;
1681 int save_flags = cmdmod.cmod_flags;
1682
1683 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001684 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001685 tv = STACK_TV_BOT(0);
1686 init_tv(tv);
1687 cmdmod.cmod_flags |= CMOD_LEGACY;
1688 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1689 cmdmod.cmod_flags = save_flags;
1690 if (res == FAIL)
1691 goto on_error;
1692 ++ectx->ec_stack.ga_len;
1693 }
1694 break;
1695
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001696 // push typeval VAR_INSTR with instructions to be executed
1697 case ISN_INSTR:
1698 {
1699 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001700 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001701 tv = STACK_TV_BOT(0);
1702 tv->vval.v_instr = ALLOC_ONE(instr_T);
1703 if (tv->vval.v_instr == NULL)
1704 goto on_error;
1705 ++ectx->ec_stack.ga_len;
1706
1707 tv->v_type = VAR_INSTR;
1708 tv->vval.v_instr->instr_ectx = ectx;
1709 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1710 }
1711 break;
1712
Bram Moolenaar4c137212021-04-19 16:48:48 +02001713 // execute :substitute with an expression
1714 case ISN_SUBSTITUTE:
1715 {
1716 subs_T *subs = &iptr->isn_arg.subs;
1717 source_cookie_T cookie;
1718 struct subs_expr_S *save_instr = substitute_instr;
1719 struct subs_expr_S subs_instr;
1720 int res;
1721
1722 subs_instr.subs_ectx = ectx;
1723 subs_instr.subs_instr = subs->subs_instr;
1724 subs_instr.subs_status = OK;
1725 substitute_instr = &subs_instr;
1726
1727 SOURCING_LNUM = iptr->isn_lnum;
1728 // This is very much like ISN_EXEC
1729 CLEAR_FIELD(cookie);
1730 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1731 res = do_cmdline(subs->subs_cmd,
1732 getsourceline, &cookie,
1733 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1734 substitute_instr = save_instr;
1735
1736 if (res == FAIL || did_emsg
1737 || subs_instr.subs_status == FAIL)
1738 goto on_error;
1739 }
1740 break;
1741
1742 case ISN_FINISH:
1743 goto done;
1744
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001745 case ISN_REDIRSTART:
1746 // create a dummy entry for var_redir_str()
1747 if (alloc_redir_lval() == FAIL)
1748 goto on_error;
1749
1750 // The output is stored in growarray "redir_ga" until
1751 // redirection ends.
1752 init_redir_ga();
1753 redir_vname = 1;
1754 break;
1755
1756 case ISN_REDIREND:
1757 {
1758 char_u *res = get_clear_redir_ga();
1759
1760 // End redirection, put redirected text on the stack.
1761 clear_redir_lval();
1762 redir_vname = 0;
1763
1764 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1765 {
1766 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001767 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001768 }
1769 tv = STACK_TV_BOT(0);
1770 tv->v_type = VAR_STRING;
1771 tv->vval.v_string = res;
1772 ++ectx->ec_stack.ga_len;
1773 }
1774 break;
1775
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001776 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001777#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001778 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1779 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001780#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001781 break;
1782
1783 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001784#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001785 {
1786 exarg_T ea;
1787 int res;
1788
1789 CLEAR_FIELD(ea);
1790 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1791 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1792 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1793 --ectx->ec_stack.ga_len;
1794 tv = STACK_TV_BOT(0);
1795 res = cexpr_core(&ea, tv);
1796 clear_tv(tv);
1797 if (res == FAIL)
1798 goto on_error;
1799 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001800#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001801 break;
1802
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001803 // execute Ex command from pieces on the stack
1804 case ISN_EXECCONCAT:
1805 {
1806 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001807 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001808 int pass;
1809 int i;
1810 char_u *cmd = NULL;
1811 char_u *str;
1812
1813 for (pass = 1; pass <= 2; ++pass)
1814 {
1815 for (i = 0; i < count; ++i)
1816 {
1817 tv = STACK_TV_BOT(i - count);
1818 str = tv->vval.v_string;
1819 if (str != NULL && *str != NUL)
1820 {
1821 if (pass == 2)
1822 STRCPY(cmd + len, str);
1823 len += STRLEN(str);
1824 }
1825 if (pass == 2)
1826 clear_tv(tv);
1827 }
1828 if (pass == 1)
1829 {
1830 cmd = alloc(len + 1);
1831 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001832 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001833 len = 0;
1834 }
1835 }
1836
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001837 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001838 do_cmdline_cmd(cmd);
1839 vim_free(cmd);
1840 }
1841 break;
1842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 // execute :echo {string} ...
1844 case ISN_ECHO:
1845 {
1846 int count = iptr->isn_arg.echo.echo_count;
1847 int atstart = TRUE;
1848 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001849 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850
1851 for (idx = 0; idx < count; ++idx)
1852 {
1853 tv = STACK_TV_BOT(idx - count);
1854 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1855 &atstart, &needclr);
1856 clear_tv(tv);
1857 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001858 if (needclr)
1859 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001860 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 }
1862 break;
1863
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001864 // :execute {string} ...
1865 // :echomsg {string} ...
1866 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001867 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001868 case ISN_ECHOMSG:
1869 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001870 {
1871 int count = iptr->isn_arg.number;
1872 garray_T ga;
1873 char_u buf[NUMBUFLEN];
1874 char_u *p;
1875 int len;
1876 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001877 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001878
1879 ga_init2(&ga, 1, 80);
1880 for (idx = 0; idx < count; ++idx)
1881 {
1882 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001883 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001884 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001885 if (tv->v_type == VAR_CHANNEL
1886 || tv->v_type == VAR_JOB)
1887 {
1888 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001889 semsg(_(e_using_invalid_value_as_string_str),
1890 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001891 break;
1892 }
1893 else
1894 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001895 }
1896 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001897 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001898
1899 len = (int)STRLEN(p);
1900 if (ga_grow(&ga, len + 2) == FAIL)
1901 failed = TRUE;
1902 else
1903 {
1904 if (ga.ga_len > 0)
1905 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1906 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1907 ga.ga_len += len;
1908 }
1909 clear_tv(tv);
1910 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001911 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001912 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001913 {
1914 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001915 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001916 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001917
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001918 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001919 {
1920 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001921 {
1922 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001923 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001924 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001925 {
1926 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001927 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001928 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001929 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001930 else
1931 {
1932 msg_sb_eol();
1933 if (iptr->isn_type == ISN_ECHOMSG)
1934 {
1935 msg_attr(ga.ga_data, echo_attr);
1936 out_flush();
1937 }
1938 else
1939 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001940 SOURCING_LNUM = iptr->isn_lnum;
1941 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001942 }
1943 }
1944 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001945 ga_clear(&ga);
1946 }
1947 break;
1948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 // load local variable or argument
1950 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001951 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001952 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001954 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 break;
1956
1957 // load v: variable
1958 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001959 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001960 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001962 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 break;
1964
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001965 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 case ISN_LOADSCRIPT:
1967 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001968 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 svar_T *sv;
1970
Bram Moolenaar4c137212021-04-19 16:48:48 +02001971 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001972 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001973 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001974 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001975 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001976 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001978 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 }
1980 break;
1981
1982 // load s: variable in old script
1983 case ISN_LOADS:
1984 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001985 hashtab_T *ht = &SCRIPT_VARS(
1986 iptr->isn_arg.loadstore.ls_sid);
1987 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 if (di == NULL)
1991 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001992 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001993 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001994 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 }
1996 else
1997 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001998 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001999 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002001 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 }
2003 }
2004 break;
2005
Bram Moolenaard3aac292020-04-19 14:32:17 +02002006 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002008 case ISN_LOADB:
2009 case ISN_LOADW:
2010 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002012 dictitem_T *di = NULL;
2013 hashtab_T *ht = NULL;
2014 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002015
Bram Moolenaard3aac292020-04-19 14:32:17 +02002016 switch (iptr->isn_type)
2017 {
2018 case ISN_LOADG:
2019 ht = get_globvar_ht();
2020 namespace = 'g';
2021 break;
2022 case ISN_LOADB:
2023 ht = &curbuf->b_vars->dv_hashtab;
2024 namespace = 'b';
2025 break;
2026 case ISN_LOADW:
2027 ht = &curwin->w_vars->dv_hashtab;
2028 namespace = 'w';
2029 break;
2030 case ISN_LOADT:
2031 ht = &curtab->tp_vars->dv_hashtab;
2032 namespace = 't';
2033 break;
2034 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002035 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002036 }
2037 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 if (di == NULL)
2040 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002041 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002042 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002043 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002044 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 }
2046 else
2047 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002048 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002049 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002051 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 }
2053 }
2054 break;
2055
Bram Moolenaar03290b82020-12-19 16:30:44 +01002056 // load autoload variable
2057 case ISN_LOADAUTO:
2058 {
2059 char_u *name = iptr->isn_arg.string;
2060
Bram Moolenaar4c137212021-04-19 16:48:48 +02002061 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002062 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002063 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002064 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002065 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002066 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002067 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002068 }
2069 break;
2070
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002071 // load g:/b:/w:/t: namespace
2072 case ISN_LOADGDICT:
2073 case ISN_LOADBDICT:
2074 case ISN_LOADWDICT:
2075 case ISN_LOADTDICT:
2076 {
2077 dict_T *d = NULL;
2078
2079 switch (iptr->isn_type)
2080 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002081 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2082 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2083 case ISN_LOADWDICT: d = curwin->w_vars; break;
2084 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002085 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002086 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002087 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002088 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002089 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002090 tv = STACK_TV_BOT(0);
2091 tv->v_type = VAR_DICT;
2092 tv->v_lock = 0;
2093 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002094 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002095 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002096 }
2097 break;
2098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 // load &option
2100 case ISN_LOADOPT:
2101 {
2102 typval_T optval;
2103 char_u *name = iptr->isn_arg.string;
2104
Bram Moolenaara8c17702020-04-01 21:17:24 +02002105 // This is not expected to fail, name is checked during
2106 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002107 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002108 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002109 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002110 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002112 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 }
2114 break;
2115
2116 // load $ENV
2117 case ISN_LOADENV:
2118 {
2119 typval_T optval;
2120 char_u *name = iptr->isn_arg.string;
2121
Bram Moolenaar4c137212021-04-19 16:48:48 +02002122 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002123 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002124 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002125 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002127 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 }
2129 break;
2130
2131 // load @register
2132 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002133 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002134 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 tv = STACK_TV_BOT(0);
2136 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002137 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002138 // This may result in NULL, which should be equivalent to an
2139 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 tv->vval.v_string = get_reg_contents(
2141 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002142 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 break;
2144
2145 // store local variable
2146 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002147 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 tv = STACK_TV_VAR(iptr->isn_arg.number);
2149 clear_tv(tv);
2150 *tv = *STACK_TV_BOT(0);
2151 break;
2152
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002153 // store s: variable in old script
2154 case ISN_STORES:
2155 {
2156 hashtab_T *ht = &SCRIPT_VARS(
2157 iptr->isn_arg.loadstore.ls_sid);
2158 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002159 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002160
Bram Moolenaar4c137212021-04-19 16:48:48 +02002161 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002162 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002163 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002164 else
2165 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002166 SOURCING_LNUM = iptr->isn_lnum;
2167 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002168 {
2169 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002170 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002171 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002172 clear_tv(&di->di_tv);
2173 di->di_tv = *STACK_TV_BOT(0);
2174 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002175 }
2176 break;
2177
2178 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 case ISN_STORESCRIPT:
2180 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002181 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2182 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183
Bram Moolenaar4c137212021-04-19 16:48:48 +02002184 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002185 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002186 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002187 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002188
2189 // "const" and "final" are checked at compile time, locking
2190 // the value needs to be checked here.
2191 SOURCING_LNUM = iptr->isn_lnum;
2192 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002193 {
2194 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002195 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002196 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 clear_tv(sv->sv_tv);
2199 *sv->sv_tv = *STACK_TV_BOT(0);
2200 }
2201 break;
2202
2203 // store option
2204 case ISN_STOREOPT:
2205 {
2206 long n = 0;
2207 char_u *s = NULL;
2208 char *msg;
2209
Bram Moolenaar4c137212021-04-19 16:48:48 +02002210 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 tv = STACK_TV_BOT(0);
2212 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002213 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002215 if (s == NULL)
2216 s = (char_u *)"";
2217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002219 // must be VAR_NUMBER, CHECKTYPE makes sure
2220 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2222 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002223 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 if (msg != NULL)
2225 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002226 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002228 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 }
2231 break;
2232
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002233 // store $ENV
2234 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002235 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002236 tv = STACK_TV_BOT(0);
2237 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2238 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002239 break;
2240
2241 // store @r
2242 case ISN_STOREREG:
2243 {
2244 int reg = iptr->isn_arg.number;
2245
Bram Moolenaar4c137212021-04-19 16:48:48 +02002246 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002247 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002248 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002249 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002250 }
2251 break;
2252
2253 // store v: variable
2254 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002255 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002256 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2257 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002258 // should not happen, type is checked when compiling
2259 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002260 break;
2261
Bram Moolenaard3aac292020-04-19 14:32:17 +02002262 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002264 case ISN_STOREB:
2265 case ISN_STOREW:
2266 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002268 dictitem_T *di;
2269 hashtab_T *ht;
2270 char_u *name = iptr->isn_arg.string + 2;
2271
Bram Moolenaard3aac292020-04-19 14:32:17 +02002272 switch (iptr->isn_type)
2273 {
2274 case ISN_STOREG:
2275 ht = get_globvar_ht();
2276 break;
2277 case ISN_STOREB:
2278 ht = &curbuf->b_vars->dv_hashtab;
2279 break;
2280 case ISN_STOREW:
2281 ht = &curwin->w_vars->dv_hashtab;
2282 break;
2283 case ISN_STORET:
2284 ht = &curtab->tp_vars->dv_hashtab;
2285 break;
2286 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002287 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289
Bram Moolenaar4c137212021-04-19 16:48:48 +02002290 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002291 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002293 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 else
2295 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002296 SOURCING_LNUM = iptr->isn_lnum;
2297 if (var_check_permission(di, name) == FAIL)
2298 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 clear_tv(&di->di_tv);
2300 di->di_tv = *STACK_TV_BOT(0);
2301 }
2302 }
2303 break;
2304
Bram Moolenaar03290b82020-12-19 16:30:44 +01002305 // store an autoload variable
2306 case ISN_STOREAUTO:
2307 SOURCING_LNUM = iptr->isn_lnum;
2308 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2309 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002310 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002311 break;
2312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 // store number in local variable
2314 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002315 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 clear_tv(tv);
2317 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002318 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 break;
2320
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002321 // store value in list or dict variable
2322 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002323 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002324 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002325 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002326 typval_T *tv_dest = STACK_TV_BOT(-1);
2327 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002328
Bram Moolenaar752fc692021-01-04 21:57:11 +01002329 // Stack contains:
2330 // -3 value to be stored
2331 // -2 index
2332 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002333 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002334 SOURCING_LNUM = iptr->isn_lnum;
2335 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002336 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002337 dest_type = tv_dest->v_type;
2338 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002339 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002340 else if (dest_type == VAR_LIST
2341 && tv_idx->v_type != VAR_NUMBER)
2342 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002343 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002344 status = FAIL;
2345 }
2346 }
2347 else if (dest_type != tv_dest->v_type)
2348 {
2349 // just in case, should be OK
2350 semsg(_(e_expected_str_but_got_str),
2351 vartype_name(dest_type),
2352 vartype_name(tv_dest->v_type));
2353 status = FAIL;
2354 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002355
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002356 if (status == OK && dest_type == VAR_LIST)
2357 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002358 long lidx = (long)tv_idx->vval.v_number;
2359 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002360
2361 if (list == NULL)
2362 {
2363 emsg(_(e_list_not_set));
2364 goto on_error;
2365 }
2366 if (lidx < 0 && list->lv_len + lidx >= 0)
2367 // negative index is relative to the end
2368 lidx = list->lv_len + lidx;
2369 if (lidx < 0 || lidx > list->lv_len)
2370 {
2371 semsg(_(e_listidx), lidx);
2372 goto on_error;
2373 }
2374 if (lidx < list->lv_len)
2375 {
2376 listitem_T *li = list_find(list, lidx);
2377
2378 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002379 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002380 goto on_error;
2381 // overwrite existing list item
2382 clear_tv(&li->li_tv);
2383 li->li_tv = *tv;
2384 }
2385 else
2386 {
2387 if (error_if_locked(list->lv_lock,
2388 e_cannot_change_list))
2389 goto on_error;
2390 // append to list, only fails when out of memory
2391 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002392 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002393 clear_tv(tv);
2394 }
2395 }
2396 else if (status == OK && dest_type == VAR_DICT)
2397 {
2398 char_u *key = tv_idx->vval.v_string;
2399 dict_T *dict = tv_dest->vval.v_dict;
2400 dictitem_T *di;
2401
2402 SOURCING_LNUM = iptr->isn_lnum;
2403 if (dict == NULL)
2404 {
2405 emsg(_(e_dictionary_not_set));
2406 goto on_error;
2407 }
2408 if (key == NULL)
2409 key = (char_u *)"";
2410 di = dict_find(dict, key, -1);
2411 if (di != NULL)
2412 {
2413 if (error_if_locked(di->di_tv.v_lock,
2414 e_cannot_change_dict_item))
2415 goto on_error;
2416 // overwrite existing value
2417 clear_tv(&di->di_tv);
2418 di->di_tv = *tv;
2419 }
2420 else
2421 {
2422 if (error_if_locked(dict->dv_lock,
2423 e_cannot_change_dict))
2424 goto on_error;
2425 // add to dict, only fails when out of memory
2426 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002427 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002428 clear_tv(tv);
2429 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002430 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002431 else if (status == OK && dest_type == VAR_BLOB)
2432 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002433 long lidx = (long)tv_idx->vval.v_number;
2434 blob_T *blob = tv_dest->vval.v_blob;
2435 varnumber_T nr;
2436 int error = FALSE;
2437 int len;
2438
2439 if (blob == NULL)
2440 {
2441 emsg(_(e_blob_not_set));
2442 goto on_error;
2443 }
2444 len = blob_len(blob);
2445 if (lidx < 0 && len + lidx >= 0)
2446 // negative index is relative to the end
2447 lidx = len + lidx;
2448
2449 // Can add one byte at the end.
2450 if (lidx < 0 || lidx > len)
2451 {
2452 semsg(_(e_blobidx), lidx);
2453 goto on_error;
2454 }
2455 if (value_check_lock(blob->bv_lock,
2456 (char_u *)"blob", FALSE))
2457 goto on_error;
2458 nr = tv_get_number_chk(tv, &error);
2459 if (error)
2460 goto on_error;
2461 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002462 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002463 else
2464 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002465 status = FAIL;
2466 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002467 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002468
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002469 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002470 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002471 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002472 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002473 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002474 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002475 goto on_error;
2476 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002477 }
2478 break;
2479
Bram Moolenaar68452172021-04-12 21:21:02 +02002480 // store value in blob range
2481 case ISN_STORERANGE:
2482 {
2483 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2484 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2485 typval_T *tv_dest = STACK_TV_BOT(-1);
2486 int status = OK;
2487
2488 // Stack contains:
2489 // -4 value to be stored
2490 // -3 first index or "none"
2491 // -2 second index or "none"
2492 // -1 destination blob
2493 tv = STACK_TV_BOT(-4);
2494 if (tv_dest->v_type != VAR_BLOB)
2495 {
2496 status = FAIL;
2497 emsg(_(e_blob_required));
2498 }
2499 else
2500 {
2501 varnumber_T n1;
2502 varnumber_T n2;
2503 int error = FALSE;
2504
2505 n1 = tv_get_number_chk(tv_idx1, &error);
2506 if (error)
2507 status = FAIL;
2508 else
2509 {
2510 if (tv_idx2->v_type == VAR_SPECIAL
2511 && tv_idx2->vval.v_number == VVAL_NONE)
2512 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2513 else
2514 n2 = tv_get_number_chk(tv_idx2, &error);
2515 if (error)
2516 status = FAIL;
2517 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002518 {
2519 long bloblen = blob_len(tv_dest->vval.v_blob);
2520
2521 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002522 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002523 || check_blob_range(bloblen,
2524 n1, n2, FALSE) == FAIL)
2525 status = FAIL;
2526 else
2527 status = blob_set_range(
2528 tv_dest->vval.v_blob, n1, n2, tv);
2529 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002530 }
2531 }
2532
2533 clear_tv(tv_idx1);
2534 clear_tv(tv_idx2);
2535 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002536 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002537 clear_tv(tv);
2538
2539 if (status == FAIL)
2540 goto on_error;
2541 }
2542 break;
2543
Bram Moolenaar0186e582021-01-10 18:33:11 +01002544 // load or store variable or argument from outer scope
2545 case ISN_LOADOUTER:
2546 case ISN_STOREOUTER:
2547 {
2548 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002549 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2550 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002551
2552 while (depth > 1 && outer != NULL)
2553 {
2554 outer = outer->out_up;
2555 --depth;
2556 }
2557 if (outer == NULL)
2558 {
2559 SOURCING_LNUM = iptr->isn_lnum;
2560 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002561 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002562 }
2563 tv = ((typval_T *)outer->out_stack->ga_data)
2564 + outer->out_frame_idx + STACK_FRAME_SIZE
2565 + iptr->isn_arg.outer.outer_idx;
2566 if (iptr->isn_type == ISN_LOADOUTER)
2567 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002568 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002569 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002570 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002571 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002572 }
2573 else
2574 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002575 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002576 clear_tv(tv);
2577 *tv = *STACK_TV_BOT(0);
2578 }
2579 }
2580 break;
2581
Bram Moolenaar752fc692021-01-04 21:57:11 +01002582 // unlet item in list or dict variable
2583 case ISN_UNLETINDEX:
2584 {
2585 typval_T *tv_idx = STACK_TV_BOT(-2);
2586 typval_T *tv_dest = STACK_TV_BOT(-1);
2587 int status = OK;
2588
2589 // Stack contains:
2590 // -2 index
2591 // -1 dict or list
2592 if (tv_dest->v_type == VAR_DICT)
2593 {
2594 // unlet a dict item, index must be a string
2595 if (tv_idx->v_type != VAR_STRING)
2596 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002597 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002598 semsg(_(e_expected_str_but_got_str),
2599 vartype_name(VAR_STRING),
2600 vartype_name(tv_idx->v_type));
2601 status = FAIL;
2602 }
2603 else
2604 {
2605 dict_T *d = tv_dest->vval.v_dict;
2606 char_u *key = tv_idx->vval.v_string;
2607 dictitem_T *di = NULL;
2608
2609 if (key == NULL)
2610 key = (char_u *)"";
2611 if (d != NULL)
2612 di = dict_find(d, key, (int)STRLEN(key));
2613 if (di == NULL)
2614 {
2615 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002616 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002617 semsg(_(e_dictkey), key);
2618 status = FAIL;
2619 }
2620 else
2621 {
2622 // TODO: check for dict or item locked
2623 dictitem_remove(d, di);
2624 }
2625 }
2626 }
2627 else if (tv_dest->v_type == VAR_LIST)
2628 {
2629 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002630 SOURCING_LNUM = iptr->isn_lnum;
2631 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002632 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002633 status = FAIL;
2634 }
2635 else
2636 {
2637 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002638 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002639 listitem_T *li = NULL;
2640
2641 li = list_find(l, n);
2642 if (li == NULL)
2643 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002644 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002645 semsg(_(e_listidx), n);
2646 status = FAIL;
2647 }
2648 else
2649 // TODO: check for list or item locked
2650 listitem_remove(l, li);
2651 }
2652 }
2653 else
2654 {
2655 status = FAIL;
2656 semsg(_(e_cannot_index_str),
2657 vartype_name(tv_dest->v_type));
2658 }
2659
2660 clear_tv(tv_idx);
2661 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002662 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002663 if (status == FAIL)
2664 goto on_error;
2665 }
2666 break;
2667
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002668 // unlet range of items in list variable
2669 case ISN_UNLETRANGE:
2670 {
2671 // Stack contains:
2672 // -3 index1
2673 // -2 index2
2674 // -1 dict or list
2675 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2676 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2677 typval_T *tv_dest = STACK_TV_BOT(-1);
2678 int status = OK;
2679
2680 if (tv_dest->v_type == VAR_LIST)
2681 {
2682 // indexes must be a number
2683 SOURCING_LNUM = iptr->isn_lnum;
2684 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002685 || (tv_idx2->v_type != VAR_SPECIAL
2686 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002687 {
2688 status = FAIL;
2689 }
2690 else
2691 {
2692 list_T *l = tv_dest->vval.v_list;
2693 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002694 long n2 = tv_idx2->v_type == VAR_SPECIAL
2695 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002696 listitem_T *li;
2697
2698 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002699 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002700 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002701 else
2702 {
2703 if (n1 < 0)
2704 n1 = list_idx_of_item(l, li);
2705 if (n2 < 0)
2706 {
2707 listitem_T *li2 = list_find(l, n2);
2708
2709 if (li2 == NULL)
2710 status = FAIL;
2711 else
2712 n2 = list_idx_of_item(l, li2);
2713 }
2714 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002715 && tv_idx2->v_type != VAR_SPECIAL
2716 && n2 < n1)
2717 {
2718 semsg(_(e_listidx), n2);
2719 status = FAIL;
2720 }
2721 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002722 && list_unlet_range(l, li, NULL, n1,
2723 tv_idx2->v_type != VAR_SPECIAL, n2)
2724 == FAIL)
2725 status = FAIL;
2726 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002727 }
2728 }
2729 else
2730 {
2731 status = FAIL;
2732 SOURCING_LNUM = iptr->isn_lnum;
2733 semsg(_(e_cannot_index_str),
2734 vartype_name(tv_dest->v_type));
2735 }
2736
2737 clear_tv(tv_idx1);
2738 clear_tv(tv_idx2);
2739 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002740 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002741 if (status == FAIL)
2742 goto on_error;
2743 }
2744 break;
2745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 // push constant
2747 case ISN_PUSHNR:
2748 case ISN_PUSHBOOL:
2749 case ISN_PUSHSPEC:
2750 case ISN_PUSHF:
2751 case ISN_PUSHS:
2752 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002753 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002754 case ISN_PUSHCHANNEL:
2755 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002756 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002757 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002759 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002760 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 switch (iptr->isn_type)
2762 {
2763 case ISN_PUSHNR:
2764 tv->v_type = VAR_NUMBER;
2765 tv->vval.v_number = iptr->isn_arg.number;
2766 break;
2767 case ISN_PUSHBOOL:
2768 tv->v_type = VAR_BOOL;
2769 tv->vval.v_number = iptr->isn_arg.number;
2770 break;
2771 case ISN_PUSHSPEC:
2772 tv->v_type = VAR_SPECIAL;
2773 tv->vval.v_number = iptr->isn_arg.number;
2774 break;
2775#ifdef FEAT_FLOAT
2776 case ISN_PUSHF:
2777 tv->v_type = VAR_FLOAT;
2778 tv->vval.v_float = iptr->isn_arg.fnumber;
2779 break;
2780#endif
2781 case ISN_PUSHBLOB:
2782 blob_copy(iptr->isn_arg.blob, tv);
2783 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002784 case ISN_PUSHFUNC:
2785 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002786 if (iptr->isn_arg.string == NULL)
2787 tv->vval.v_string = NULL;
2788 else
2789 tv->vval.v_string =
2790 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002791 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002792 case ISN_PUSHCHANNEL:
2793#ifdef FEAT_JOB_CHANNEL
2794 tv->v_type = VAR_CHANNEL;
2795 tv->vval.v_channel = iptr->isn_arg.channel;
2796 if (tv->vval.v_channel != NULL)
2797 ++tv->vval.v_channel->ch_refcount;
2798#endif
2799 break;
2800 case ISN_PUSHJOB:
2801#ifdef FEAT_JOB_CHANNEL
2802 tv->v_type = VAR_JOB;
2803 tv->vval.v_job = iptr->isn_arg.job;
2804 if (tv->vval.v_job != NULL)
2805 ++tv->vval.v_job->jv_refcount;
2806#endif
2807 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 default:
2809 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002810 tv->vval.v_string = vim_strsave(
2811 iptr->isn_arg.string == NULL
2812 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 }
2814 break;
2815
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002816 case ISN_UNLET:
2817 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2818 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002819 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002820 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002821 case ISN_UNLETENV:
2822 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2823 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002824
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002825 case ISN_LOCKCONST:
2826 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2827 break;
2828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 // create a list from items on the stack; uses a single allocation
2830 // for the list header and the items
2831 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002832 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002833 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 break;
2835
2836 // create a dict from items on the stack
2837 case ISN_NEWDICT:
2838 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002839 int count = iptr->isn_arg.number;
2840 dict_T *dict = dict_alloc();
2841 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002842 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002843 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844
2845 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002846 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 for (idx = 0; idx < count; ++idx)
2848 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002849 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002851 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002852 key = tv->vval.v_string == NULL
2853 ? (char_u *)"" : tv->vval.v_string;
2854 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002855 if (item != NULL)
2856 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002857 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002858 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002859 dict_unref(dict);
2860 goto on_error;
2861 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002862 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 clear_tv(tv);
2864 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002865 {
2866 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002867 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2870 item->di_tv.v_lock = 0;
2871 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002872 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002873 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002874 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002875 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 }
2878
2879 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002880 ectx->ec_stack.ga_len -= 2 * count - 1;
2881 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002882 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002884 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 tv = STACK_TV_BOT(-1);
2886 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002887 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 tv->vval.v_dict = dict;
2889 ++dict->dv_refcount;
2890 }
2891 break;
2892
2893 // call a :def function
2894 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002895 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002896 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2897 NULL,
2898 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002899 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002900 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 break;
2902
2903 // call a builtin function
2904 case ISN_BCALL:
2905 SOURCING_LNUM = iptr->isn_lnum;
2906 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2907 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002908 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002909 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 break;
2911
2912 // call a funcref or partial
2913 case ISN_PCALL:
2914 {
2915 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2916 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002917 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918
2919 SOURCING_LNUM = iptr->isn_lnum;
2920 if (pfunc->cpf_top)
2921 {
2922 // funcref is above the arguments
2923 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2924 }
2925 else
2926 {
2927 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002928 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002929 partial_tv = *STACK_TV_BOT(0);
2930 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002932 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002933 if (tv == &partial_tv)
2934 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002936 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 }
2938 break;
2939
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002940 case ISN_PCALL_END:
2941 // PCALL finished, arguments have been consumed and replaced by
2942 // the return value. Now clear the funcref from the stack,
2943 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002944 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002945 clear_tv(STACK_TV_BOT(-1));
2946 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2947 break;
2948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 // call a user defined function or funcref/partial
2950 case ISN_UCALL:
2951 {
2952 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2953
2954 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002955 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002956 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002957 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 }
2959 break;
2960
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002961 // return from a :def function call without a value
2962 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002963 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002964 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002965 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002966 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002967 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002968 tv->vval.v_number = 0;
2969 tv->v_lock = 0;
2970 // FALLTHROUGH
2971
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002972 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 case ISN_RETURN:
2974 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002975 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002976 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002977
2978 if (trystack->ga_len > 0)
2979 trycmd = ((trycmd_T *)trystack->ga_data)
2980 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002981 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002982 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002984 // jump to ":finally" or ":endtry"
2985 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002986 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002987 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002988 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 trycmd->tcd_return = TRUE;
2990 }
2991 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002992 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 }
2994 break;
2995
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002996 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 case ISN_FUNCREF:
2998 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002999 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
3000 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3001 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003004 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003005 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003006 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003007 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003008 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003009 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01003010 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003011 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003012 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003014 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 tv->vval.v_partial = pt;
3016 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003017 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 }
3019 break;
3020
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003021 // Create a global function from a lambda.
3022 case ISN_NEWFUNC:
3023 {
3024 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3025
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003026 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003028 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003029 }
3030 break;
3031
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003032 // List functions
3033 case ISN_DEF:
3034 if (iptr->isn_arg.string == NULL)
3035 list_functions(NULL);
3036 else
3037 {
3038 exarg_T ea;
3039
3040 CLEAR_FIELD(ea);
3041 ea.cmd = ea.arg = iptr->isn_arg.string;
3042 define_function(&ea, NULL);
3043 }
3044 break;
3045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 // jump if a condition is met
3047 case ISN_JUMP:
3048 {
3049 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003050 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 int jump = TRUE;
3052
3053 if (when != JUMP_ALWAYS)
3054 {
3055 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003056 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003057 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003058 || when == JUMP_IF_COND_TRUE)
3059 {
3060 SOURCING_LNUM = iptr->isn_lnum;
3061 jump = tv_get_bool_chk(tv, &error);
3062 if (error)
3063 goto on_error;
3064 }
3065 else
3066 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003068 || when == JUMP_AND_KEEP_IF_FALSE
3069 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003071 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 {
3073 // drop the value from the stack
3074 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003075 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 }
3077 }
3078 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003079 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 }
3081 break;
3082
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003083 // Jump if an argument with a default value was already set and not
3084 // v:none.
3085 case ISN_JUMP_IF_ARG_SET:
3086 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3087 if (tv->v_type != VAR_UNKNOWN
3088 && !(tv->v_type == VAR_SPECIAL
3089 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003090 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003091 break;
3092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 // top of a for loop
3094 case ISN_FOR:
3095 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003096 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 typval_T *idxtv =
3098 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3099
Bram Moolenaar4c137212021-04-19 16:48:48 +02003100 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003101 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003102 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003103 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003104 list_T *list = ltv->vval.v_list;
3105
3106 // push the next item from the list
3107 ++idxtv->vval.v_number;
3108 if (list == NULL
3109 || idxtv->vval.v_number >= list->lv_len)
3110 {
3111 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003112 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3113 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003114 }
3115 else if (list->lv_first == &range_list_item)
3116 {
3117 // non-materialized range() list
3118 tv = STACK_TV_BOT(0);
3119 tv->v_type = VAR_NUMBER;
3120 tv->v_lock = 0;
3121 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003123 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003124 }
3125 else
3126 {
3127 listitem_T *li = list_find(list,
3128 idxtv->vval.v_number);
3129
3130 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003131 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003132 }
3133 }
3134 else if (ltv->v_type == VAR_STRING)
3135 {
3136 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003137
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003138 // The index is for the last byte of the previous
3139 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003140 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003141 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003142 {
3143 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3145 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003146 }
3147 else
3148 {
3149 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3150
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003151 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003152 tv = STACK_TV_BOT(0);
3153 tv->v_type = VAR_STRING;
3154 tv->vval.v_string = vim_strnsave(
3155 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003156 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003157 idxtv->vval.v_number += clen - 1;
3158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003160 else if (ltv->v_type == VAR_BLOB)
3161 {
3162 blob_T *blob = ltv->vval.v_blob;
3163
3164 // When we get here the first time make a copy of the
3165 // blob, so that the iteration still works when it is
3166 // changed.
3167 if (idxtv->vval.v_number == -1 && blob != NULL)
3168 {
3169 blob_copy(blob, ltv);
3170 blob_unref(blob);
3171 blob = ltv->vval.v_blob;
3172 }
3173
3174 // The index is for the previous byte.
3175 ++idxtv->vval.v_number;
3176 if (blob == NULL
3177 || idxtv->vval.v_number >= blob_len(blob))
3178 {
3179 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003180 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3181 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003182 }
3183 else
3184 {
3185 // Push the next byte from the blob.
3186 tv = STACK_TV_BOT(0);
3187 tv->v_type = VAR_NUMBER;
3188 tv->vval.v_number = blob_get(blob,
3189 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003190 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003191 }
3192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 else
3194 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003195 semsg(_(e_for_loop_on_str_not_supported),
3196 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003197 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 }
3199 }
3200 break;
3201
3202 // start of ":try" block
3203 case ISN_TRY:
3204 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003205 trycmd_T *trycmd = NULL;
3206
Bram Moolenaar4c137212021-04-19 16:48:48 +02003207 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003208 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003209 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3210 + ectx->ec_trystack.ga_len;
3211 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003213 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003214 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3215 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003216 trycmd->tcd_catch_idx =
3217 iptr->isn_arg.try.try_ref->try_catch;
3218 trycmd->tcd_finally_idx =
3219 iptr->isn_arg.try.try_ref->try_finally;
3220 trycmd->tcd_endtry_idx =
3221 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 }
3223 break;
3224
3225 case ISN_PUSHEXC:
3226 if (current_exception == NULL)
3227 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003228 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003230 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003232 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003233 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003235 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003237 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 tv->vval.v_string = vim_strsave(
3239 (char_u *)current_exception->value);
3240 break;
3241
3242 case ISN_CATCH:
3243 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003244 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245
Bram Moolenaar4c137212021-04-19 16:48:48 +02003246 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 if (trystack->ga_len > 0)
3248 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003249 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 + trystack->ga_len - 1;
3251 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003252 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 }
3254 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003255 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 catch_exception(current_exception);
3257 }
3258 break;
3259
Bram Moolenaarc150c092021-02-13 15:02:46 +01003260 case ISN_TRYCONT:
3261 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003262 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003263 trycont_T *trycont = &iptr->isn_arg.trycont;
3264 int i;
3265 trycmd_T *trycmd;
3266 int iidx = trycont->tct_where;
3267
3268 if (trystack->ga_len < trycont->tct_levels)
3269 {
3270 siemsg("TRYCONT: expected %d levels, found %d",
3271 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003272 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003273 }
3274 // Make :endtry jump to any outer try block and the last
3275 // :endtry inside the loop to the loop start.
3276 for (i = trycont->tct_levels; i > 0; --i)
3277 {
3278 trycmd = ((trycmd_T *)trystack->ga_data)
3279 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003280 // Add one to tcd_cont to be able to jump to
3281 // instruction with index zero.
3282 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003283 iidx = trycmd->tcd_finally_idx == 0
3284 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003285 }
3286 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003287 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003288 }
3289 break;
3290
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003291 case ISN_FINALLY:
3292 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003293 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003294 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3295 + trystack->ga_len - 1;
3296
3297 // Reset the index to avoid a return statement jumps here
3298 // again.
3299 trycmd->tcd_finally_idx = 0;
3300 break;
3301 }
3302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 // end of ":try" block
3304 case ISN_ENDTRY:
3305 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003306 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307
3308 if (trystack->ga_len > 0)
3309 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003310 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 --trystack->ga_len;
3313 --trylevel;
3314 trycmd = ((trycmd_T *)trystack->ga_data)
3315 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003316 if (trycmd->tcd_did_throw)
3317 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003318 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 {
3320 // discard the exception
3321 if (caught_stack == current_exception)
3322 caught_stack = caught_stack->caught;
3323 discard_current_exception();
3324 }
3325
3326 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003327 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003328
Bram Moolenaar4c137212021-04-19 16:48:48 +02003329 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003330 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003331 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003332 clear_tv(STACK_TV_BOT(0));
3333 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003334 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003335 // handling :continue: jump to outer try block or
3336 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003337 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 }
3339 }
3340 break;
3341
3342 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003343 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003344 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003345
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003346 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3347 {
3348 // throwing an exception while using "silent!" causes
3349 // the function to abort but not display an error.
3350 tv = STACK_TV_BOT(-1);
3351 clear_tv(tv);
3352 tv->v_type = VAR_NUMBER;
3353 tv->vval.v_number = 0;
3354 goto done;
3355 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003356 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003357 tv = STACK_TV_BOT(0);
3358 if (tv->vval.v_string == NULL
3359 || *skipwhite(tv->vval.v_string) == NUL)
3360 {
3361 vim_free(tv->vval.v_string);
3362 SOURCING_LNUM = iptr->isn_lnum;
3363 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003364 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003365 }
3366
3367 // Inside a "catch" we need to first discard the caught
3368 // exception.
3369 if (trystack->ga_len > 0)
3370 {
3371 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3372 + trystack->ga_len - 1;
3373 if (trycmd->tcd_caught && current_exception != NULL)
3374 {
3375 // discard the exception
3376 if (caught_stack == current_exception)
3377 caught_stack = caught_stack->caught;
3378 discard_current_exception();
3379 trycmd->tcd_caught = FALSE;
3380 }
3381 }
3382
3383 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3384 == FAIL)
3385 {
3386 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003387 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003388 }
3389 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 break;
3392
3393 // compare with special values
3394 case ISN_COMPAREBOOL:
3395 case ISN_COMPARESPECIAL:
3396 {
3397 typval_T *tv1 = STACK_TV_BOT(-2);
3398 typval_T *tv2 = STACK_TV_BOT(-1);
3399 varnumber_T arg1 = tv1->vval.v_number;
3400 varnumber_T arg2 = tv2->vval.v_number;
3401 int res;
3402
3403 switch (iptr->isn_arg.op.op_type)
3404 {
3405 case EXPR_EQUAL: res = arg1 == arg2; break;
3406 case EXPR_NEQUAL: res = arg1 != arg2; break;
3407 default: res = 0; break;
3408 }
3409
Bram Moolenaar4c137212021-04-19 16:48:48 +02003410 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 tv1->v_type = VAR_BOOL;
3412 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3413 }
3414 break;
3415
3416 // Operation with two number arguments
3417 case ISN_OPNR:
3418 case ISN_COMPARENR:
3419 {
3420 typval_T *tv1 = STACK_TV_BOT(-2);
3421 typval_T *tv2 = STACK_TV_BOT(-1);
3422 varnumber_T arg1 = tv1->vval.v_number;
3423 varnumber_T arg2 = tv2->vval.v_number;
3424 varnumber_T res;
3425
3426 switch (iptr->isn_arg.op.op_type)
3427 {
3428 case EXPR_MULT: res = arg1 * arg2; break;
3429 case EXPR_DIV: res = arg1 / arg2; break;
3430 case EXPR_REM: res = arg1 % arg2; break;
3431 case EXPR_SUB: res = arg1 - arg2; break;
3432 case EXPR_ADD: res = arg1 + arg2; break;
3433
3434 case EXPR_EQUAL: res = arg1 == arg2; break;
3435 case EXPR_NEQUAL: res = arg1 != arg2; break;
3436 case EXPR_GREATER: res = arg1 > arg2; break;
3437 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3438 case EXPR_SMALLER: res = arg1 < arg2; break;
3439 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3440 default: res = 0; break;
3441 }
3442
Bram Moolenaar4c137212021-04-19 16:48:48 +02003443 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 if (iptr->isn_type == ISN_COMPARENR)
3445 {
3446 tv1->v_type = VAR_BOOL;
3447 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3448 }
3449 else
3450 tv1->vval.v_number = res;
3451 }
3452 break;
3453
3454 // Computation with two float arguments
3455 case ISN_OPFLOAT:
3456 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003457#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 {
3459 typval_T *tv1 = STACK_TV_BOT(-2);
3460 typval_T *tv2 = STACK_TV_BOT(-1);
3461 float_T arg1 = tv1->vval.v_float;
3462 float_T arg2 = tv2->vval.v_float;
3463 float_T res = 0;
3464 int cmp = FALSE;
3465
3466 switch (iptr->isn_arg.op.op_type)
3467 {
3468 case EXPR_MULT: res = arg1 * arg2; break;
3469 case EXPR_DIV: res = arg1 / arg2; break;
3470 case EXPR_SUB: res = arg1 - arg2; break;
3471 case EXPR_ADD: res = arg1 + arg2; break;
3472
3473 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3474 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3475 case EXPR_GREATER: cmp = arg1 > arg2; break;
3476 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3477 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3478 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3479 default: cmp = 0; break;
3480 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003481 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 if (iptr->isn_type == ISN_COMPAREFLOAT)
3483 {
3484 tv1->v_type = VAR_BOOL;
3485 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3486 }
3487 else
3488 tv1->vval.v_float = res;
3489 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003490#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 break;
3492
3493 case ISN_COMPARELIST:
3494 {
3495 typval_T *tv1 = STACK_TV_BOT(-2);
3496 typval_T *tv2 = STACK_TV_BOT(-1);
3497 list_T *arg1 = tv1->vval.v_list;
3498 list_T *arg2 = tv2->vval.v_list;
3499 int cmp = FALSE;
3500 int ic = iptr->isn_arg.op.op_ic;
3501
3502 switch (iptr->isn_arg.op.op_type)
3503 {
3504 case EXPR_EQUAL: cmp =
3505 list_equal(arg1, arg2, ic, FALSE); break;
3506 case EXPR_NEQUAL: cmp =
3507 !list_equal(arg1, arg2, ic, FALSE); break;
3508 case EXPR_IS: cmp = arg1 == arg2; break;
3509 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3510 default: cmp = 0; break;
3511 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 clear_tv(tv1);
3514 clear_tv(tv2);
3515 tv1->v_type = VAR_BOOL;
3516 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3517 }
3518 break;
3519
3520 case ISN_COMPAREBLOB:
3521 {
3522 typval_T *tv1 = STACK_TV_BOT(-2);
3523 typval_T *tv2 = STACK_TV_BOT(-1);
3524 blob_T *arg1 = tv1->vval.v_blob;
3525 blob_T *arg2 = tv2->vval.v_blob;
3526 int cmp = FALSE;
3527
3528 switch (iptr->isn_arg.op.op_type)
3529 {
3530 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3531 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3532 case EXPR_IS: cmp = arg1 == arg2; break;
3533 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3534 default: cmp = 0; break;
3535 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003536 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 clear_tv(tv1);
3538 clear_tv(tv2);
3539 tv1->v_type = VAR_BOOL;
3540 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3541 }
3542 break;
3543
3544 // TODO: handle separately
3545 case ISN_COMPARESTRING:
3546 case ISN_COMPAREDICT:
3547 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 case ISN_COMPAREANY:
3549 {
3550 typval_T *tv1 = STACK_TV_BOT(-2);
3551 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003552 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 int ic = iptr->isn_arg.op.op_ic;
3554
Bram Moolenaareb26f432020-09-14 16:50:05 +02003555 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003556 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003558 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 }
3560 break;
3561
3562 case ISN_ADDLIST:
3563 case ISN_ADDBLOB:
3564 {
3565 typval_T *tv1 = STACK_TV_BOT(-2);
3566 typval_T *tv2 = STACK_TV_BOT(-1);
3567
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003568 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 if (iptr->isn_type == ISN_ADDLIST)
3570 eval_addlist(tv1, tv2);
3571 else
3572 eval_addblob(tv1, tv2);
3573 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003574 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 }
3576 break;
3577
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003578 case ISN_LISTAPPEND:
3579 {
3580 typval_T *tv1 = STACK_TV_BOT(-2);
3581 typval_T *tv2 = STACK_TV_BOT(-1);
3582 list_T *l = tv1->vval.v_list;
3583
3584 // add an item to a list
3585 if (l == NULL)
3586 {
3587 SOURCING_LNUM = iptr->isn_lnum;
3588 emsg(_(e_cannot_add_to_null_list));
3589 goto on_error;
3590 }
3591 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003592 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003593 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003594 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003595 }
3596 break;
3597
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003598 case ISN_BLOBAPPEND:
3599 {
3600 typval_T *tv1 = STACK_TV_BOT(-2);
3601 typval_T *tv2 = STACK_TV_BOT(-1);
3602 blob_T *b = tv1->vval.v_blob;
3603 int error = FALSE;
3604 varnumber_T n;
3605
3606 // add a number to a blob
3607 if (b == NULL)
3608 {
3609 SOURCING_LNUM = iptr->isn_lnum;
3610 emsg(_(e_cannot_add_to_null_blob));
3611 goto on_error;
3612 }
3613 n = tv_get_number_chk(tv2, &error);
3614 if (error)
3615 goto on_error;
3616 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003617 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003618 }
3619 break;
3620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 // Computation with two arguments of unknown type
3622 case ISN_OPANY:
3623 {
3624 typval_T *tv1 = STACK_TV_BOT(-2);
3625 typval_T *tv2 = STACK_TV_BOT(-1);
3626 varnumber_T n1, n2;
3627#ifdef FEAT_FLOAT
3628 float_T f1 = 0, f2 = 0;
3629#endif
3630 int error = FALSE;
3631
3632 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3633 {
3634 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3635 {
3636 eval_addlist(tv1, tv2);
3637 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003638 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 break;
3640 }
3641 else if (tv1->v_type == VAR_BLOB
3642 && tv2->v_type == VAR_BLOB)
3643 {
3644 eval_addblob(tv1, tv2);
3645 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003646 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 break;
3648 }
3649 }
3650#ifdef FEAT_FLOAT
3651 if (tv1->v_type == VAR_FLOAT)
3652 {
3653 f1 = tv1->vval.v_float;
3654 n1 = 0;
3655 }
3656 else
3657#endif
3658 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 n1 = tv_get_number_chk(tv1, &error);
3661 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663#ifdef FEAT_FLOAT
3664 if (tv2->v_type == VAR_FLOAT)
3665 f1 = n1;
3666#endif
3667 }
3668#ifdef FEAT_FLOAT
3669 if (tv2->v_type == VAR_FLOAT)
3670 {
3671 f2 = tv2->vval.v_float;
3672 n2 = 0;
3673 }
3674 else
3675#endif
3676 {
3677 n2 = tv_get_number_chk(tv2, &error);
3678 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003679 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680#ifdef FEAT_FLOAT
3681 if (tv1->v_type == VAR_FLOAT)
3682 f2 = n2;
3683#endif
3684 }
3685#ifdef FEAT_FLOAT
3686 // if there is a float on either side the result is a float
3687 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3688 {
3689 switch (iptr->isn_arg.op.op_type)
3690 {
3691 case EXPR_MULT: f1 = f1 * f2; break;
3692 case EXPR_DIV: f1 = f1 / f2; break;
3693 case EXPR_SUB: f1 = f1 - f2; break;
3694 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003695 default: SOURCING_LNUM = iptr->isn_lnum;
3696 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003697 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 }
3699 clear_tv(tv1);
3700 clear_tv(tv2);
3701 tv1->v_type = VAR_FLOAT;
3702 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003703 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 }
3705 else
3706#endif
3707 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003708 int failed = FALSE;
3709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 switch (iptr->isn_arg.op.op_type)
3711 {
3712 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003713 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3714 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003715 goto on_error;
3716 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 case EXPR_SUB: n1 = n1 - n2; break;
3718 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003719 default: n1 = num_modulus(n1, n2, &failed);
3720 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003721 goto on_error;
3722 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 }
3724 clear_tv(tv1);
3725 clear_tv(tv2);
3726 tv1->v_type = VAR_NUMBER;
3727 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003728 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 }
3730 }
3731 break;
3732
3733 case ISN_CONCAT:
3734 {
3735 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3736 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3737 char_u *res;
3738
3739 res = concat_str(str1, str2);
3740 clear_tv(STACK_TV_BOT(-2));
3741 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003742 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 STACK_TV_BOT(-1)->vval.v_string = res;
3744 }
3745 break;
3746
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003747 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003748 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003749 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003750 int is_slice = iptr->isn_type == ISN_STRSLICE;
3751 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003752 char_u *res;
3753
3754 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003755 // string slice: string is at stack-3, first index at
3756 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003757 if (is_slice)
3758 {
3759 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003760 n1 = tv->vval.v_number;
3761 }
3762
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003763 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003764 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003765
Bram Moolenaar4c137212021-04-19 16:48:48 +02003766 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003767 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003768 if (is_slice)
3769 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003770 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003771 else
3772 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003773 // single character (including composing characters).
3774 // If the index is too big or negative the result is
3775 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003776 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003777 vim_free(tv->vval.v_string);
3778 tv->vval.v_string = res;
3779 }
3780 break;
3781
3782 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003783 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003784 case ISN_BLOBINDEX:
3785 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003787 int is_slice = iptr->isn_type == ISN_LISTSLICE
3788 || iptr->isn_type == ISN_BLOBSLICE;
3789 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3790 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003791 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003792 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793
3794 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003795 // list slice: list is at stack-3, indexes at stack-2 and
3796 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003797 // Same for blob.
3798 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799
3800 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003801 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003803
3804 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 {
Bram Moolenaared591872020-08-15 22:14:53 +02003806 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003807 n1 = tv->vval.v_number;
3808 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 }
Bram Moolenaared591872020-08-15 22:14:53 +02003810
Bram Moolenaar4c137212021-04-19 16:48:48 +02003811 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003812 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003813 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003814 if (is_blob)
3815 {
3816 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3817 n1, n2, FALSE, tv) == FAIL)
3818 goto on_error;
3819 }
3820 else
3821 {
3822 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3823 n1, n2, FALSE, tv, TRUE) == FAIL)
3824 goto on_error;
3825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 }
3827 break;
3828
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003829 case ISN_ANYINDEX:
3830 case ISN_ANYSLICE:
3831 {
3832 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3833 typval_T *var1, *var2;
3834 int res;
3835
3836 // index: composite is at stack-2, index at stack-1
3837 // slice: composite is at stack-3, indexes at stack-2 and
3838 // stack-1
3839 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003840 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003841 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3842 goto on_error;
3843 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3844 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003845 res = eval_index_inner(tv, is_slice, var1, var2,
3846 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003847 clear_tv(var1);
3848 if (is_slice)
3849 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003850 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003851 if (res == FAIL)
3852 goto on_error;
3853 }
3854 break;
3855
Bram Moolenaar9af78762020-06-16 11:34:42 +02003856 case ISN_SLICE:
3857 {
3858 list_T *list;
3859 int count = iptr->isn_arg.number;
3860
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003861 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003862 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003863 list = tv->vval.v_list;
3864
3865 // no error for short list, expect it to be checked earlier
3866 if (list != NULL && list->lv_len >= count)
3867 {
3868 list_T *newlist = list_slice(list,
3869 count, list->lv_len - 1);
3870
3871 if (newlist != NULL)
3872 {
3873 list_unref(list);
3874 tv->vval.v_list = newlist;
3875 ++newlist->lv_refcount;
3876 }
3877 }
3878 }
3879 break;
3880
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003881 case ISN_GETITEM:
3882 {
3883 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003884 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003885
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003886 // Get list item: list is at stack-1, push item.
3887 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003888 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3889 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003890
Bram Moolenaar4c137212021-04-19 16:48:48 +02003891 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003892 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003893 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003894 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003895
3896 // Useful when used in unpack assignment. Reset at
3897 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003898 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003899 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003900 }
3901 break;
3902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 case ISN_MEMBER:
3904 {
3905 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003906 char_u *key;
3907 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003908 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003909
3910 // dict member: dict is at stack-2, key at stack-1
3911 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003912 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003913 dict = tv->vval.v_dict;
3914
3915 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003916 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003917 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003918 if (key == NULL)
3919 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003920
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003921 if ((di = dict_find(dict, key, -1)) == NULL)
3922 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003923 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003924 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003925
3926 // If :silent! is used we will continue, make sure the
3927 // stack contents makes sense.
3928 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003929 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003930 tv = STACK_TV_BOT(-1);
3931 clear_tv(tv);
3932 tv->v_type = VAR_NUMBER;
3933 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003934 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003935 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003936 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003937 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003938 // Clear the dict only after getting the item, to avoid
3939 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003940 tv = STACK_TV_BOT(-1);
3941 temp_tv = *tv;
3942 copy_tv(&di->di_tv, tv);
3943 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003944 }
3945 break;
3946
3947 // dict member with string key
3948 case ISN_STRINGMEMBER:
3949 {
3950 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003952 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953
3954 tv = STACK_TV_BOT(-1);
3955 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3956 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003957 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003959 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 }
3961 dict = tv->vval.v_dict;
3962
3963 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3964 == NULL)
3965 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003966 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003968 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003970 // Clear the dict after getting the item, to avoid that it
3971 // make the item invalid.
3972 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003974 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 }
3976 break;
3977
3978 case ISN_NEGATENR:
3979 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003980 if (tv->v_type != VAR_NUMBER
3981#ifdef FEAT_FLOAT
3982 && tv->v_type != VAR_FLOAT
3983#endif
3984 )
3985 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003986 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003987 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003988 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003989 }
3990#ifdef FEAT_FLOAT
3991 if (tv->v_type == VAR_FLOAT)
3992 tv->vval.v_float = -tv->vval.v_float;
3993 else
3994#endif
3995 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 break;
3997
3998 case ISN_CHECKNR:
3999 {
4000 int error = FALSE;
4001
4002 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004003 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004005 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 (void)tv_get_number_chk(tv, &error);
4007 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004008 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 }
4010 break;
4011
4012 case ISN_CHECKTYPE:
4013 {
4014 checktype_T *ct = &iptr->isn_arg.type;
4015
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004016 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004017 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004018 if (!ectx->ec_where.wt_variable)
4019 ectx->ec_where.wt_index = ct->ct_arg_idx;
4020 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4021 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004022 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004023 if (!ectx->ec_where.wt_variable)
4024 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004025
4026 // number 0 is FALSE, number 1 is TRUE
4027 if (tv->v_type == VAR_NUMBER
4028 && ct->ct_type->tt_type == VAR_BOOL
4029 && (tv->vval.v_number == 0
4030 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004032 tv->v_type = VAR_BOOL;
4033 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004034 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 }
4036 }
4037 break;
4038
Bram Moolenaar9af78762020-06-16 11:34:42 +02004039 case ISN_CHECKLEN:
4040 {
4041 int min_len = iptr->isn_arg.checklen.cl_min_len;
4042 list_T *list = NULL;
4043
4044 tv = STACK_TV_BOT(-1);
4045 if (tv->v_type == VAR_LIST)
4046 list = tv->vval.v_list;
4047 if (list == NULL || list->lv_len < min_len
4048 || (list->lv_len > min_len
4049 && !iptr->isn_arg.checklen.cl_more_OK))
4050 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004051 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004052 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004053 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004054 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004055 }
4056 }
4057 break;
4058
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004059 case ISN_SETTYPE:
4060 {
4061 checktype_T *ct = &iptr->isn_arg.type;
4062
4063 tv = STACK_TV_BOT(-1);
4064 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4065 {
4066 free_type(tv->vval.v_dict->dv_type);
4067 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4068 }
4069 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4070 {
4071 free_type(tv->vval.v_list->lv_type);
4072 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4073 }
4074 }
4075 break;
4076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004078 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 {
4080 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004081 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004083 if (iptr->isn_type == ISN_2BOOL)
4084 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004085 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004086 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004087 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004088 n = !n;
4089 }
4090 else
4091 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004092 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004093 SOURCING_LNUM = iptr->isn_lnum;
4094 n = tv_get_bool_chk(tv, &error);
4095 if (error)
4096 goto on_error;
4097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 clear_tv(tv);
4099 tv->v_type = VAR_BOOL;
4100 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4101 }
4102 break;
4103
4104 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004105 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004106 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004107 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4108 iptr->isn_type == ISN_2STRING_ANY,
4109 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004110 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 break;
4112
Bram Moolenaar08597872020-12-10 19:43:40 +01004113 case ISN_RANGE:
4114 {
4115 exarg_T ea;
4116 char *errormsg;
4117
Bram Moolenaarece0b872021-01-08 20:40:45 +01004118 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004119 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004120 ea.addr_type = ADDR_LINES;
4121 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004122 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004123 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004124 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004125
Bram Moolenaar4c137212021-04-19 16:48:48 +02004126 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004127 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004128 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004129 tv = STACK_TV_BOT(-1);
4130 tv->v_type = VAR_NUMBER;
4131 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004132 if (ea.addr_count == 0)
4133 tv->vval.v_number = curwin->w_cursor.lnum;
4134 else
4135 tv->vval.v_number = ea.line2;
4136 }
4137 break;
4138
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004139 case ISN_PUT:
4140 {
4141 int regname = iptr->isn_arg.put.put_regname;
4142 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4143 char_u *expr = NULL;
4144 int dir = FORWARD;
4145
Bram Moolenaar08597872020-12-10 19:43:40 +01004146 if (lnum < -2)
4147 {
4148 // line number was put on the stack by ISN_RANGE
4149 tv = STACK_TV_BOT(-1);
4150 curwin->w_cursor.lnum = tv->vval.v_number;
4151 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4152 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004153 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004154 }
4155 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004156 // :put! above cursor
4157 dir = BACKWARD;
4158 else if (lnum >= 0)
4159 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004160
4161 if (regname == '=')
4162 {
4163 tv = STACK_TV_BOT(-1);
4164 if (tv->v_type == VAR_STRING)
4165 expr = tv->vval.v_string;
4166 else
4167 {
4168 expr = typval2string(tv, TRUE); // allocates value
4169 clear_tv(tv);
4170 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004171 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004172 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004173 check_cursor();
4174 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4175 vim_free(expr);
4176 }
4177 break;
4178
Bram Moolenaar02194d22020-10-24 23:08:38 +02004179 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004180 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4181 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4182 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4183 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004184 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4185 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004186 break;
4187
Bram Moolenaar02194d22020-10-24 23:08:38 +02004188 case ISN_CMDMOD_REV:
4189 // filter regprog is owned by the instruction, don't free it
4190 cmdmod.cmod_filter_regmatch.regprog = NULL;
4191 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004192 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4193 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004194 break;
4195
Bram Moolenaar792f7862020-11-23 08:31:18 +01004196 case ISN_UNPACK:
4197 {
4198 int count = iptr->isn_arg.unpack.unp_count;
4199 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4200 list_T *l;
4201 listitem_T *li;
4202 int i;
4203
4204 // Check there is a valid list to unpack.
4205 tv = STACK_TV_BOT(-1);
4206 if (tv->v_type != VAR_LIST)
4207 {
4208 SOURCING_LNUM = iptr->isn_lnum;
4209 emsg(_(e_for_argument_must_be_sequence_of_lists));
4210 goto on_error;
4211 }
4212 l = tv->vval.v_list;
4213 if (l == NULL
4214 || l->lv_len < (semicolon ? count - 1 : count))
4215 {
4216 SOURCING_LNUM = iptr->isn_lnum;
4217 emsg(_(e_list_value_does_not_have_enough_items));
4218 goto on_error;
4219 }
4220 else if (!semicolon && l->lv_len > count)
4221 {
4222 SOURCING_LNUM = iptr->isn_lnum;
4223 emsg(_(e_list_value_has_more_items_than_targets));
4224 goto on_error;
4225 }
4226
4227 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004228 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004229 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004230 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004231
4232 // Variable after semicolon gets a list with the remaining
4233 // items.
4234 if (semicolon)
4235 {
4236 list_T *rem_list =
4237 list_alloc_with_items(l->lv_len - count + 1);
4238
4239 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004240 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004241 tv = STACK_TV_BOT(-count);
4242 tv->vval.v_list = rem_list;
4243 ++rem_list->lv_refcount;
4244 tv->v_lock = 0;
4245 li = l->lv_first;
4246 for (i = 0; i < count - 1; ++i)
4247 li = li->li_next;
4248 for (i = 0; li != NULL; ++i)
4249 {
4250 list_set_item(rem_list, i, &li->li_tv);
4251 li = li->li_next;
4252 }
4253 --count;
4254 }
4255
4256 // Produce the values in reverse order, first item last.
4257 li = l->lv_first;
4258 for (i = 0; i < count; ++i)
4259 {
4260 tv = STACK_TV_BOT(-i - 1);
4261 copy_tv(&li->li_tv, tv);
4262 li = li->li_next;
4263 }
4264
4265 list_unref(l);
4266 }
4267 break;
4268
Bram Moolenaarb2049902021-01-24 12:53:53 +01004269 case ISN_PROF_START:
4270 case ISN_PROF_END:
4271 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004272#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004273 funccall_T cookie;
4274 ufunc_T *cur_ufunc =
4275 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004276 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004277
4278 cookie.func = cur_ufunc;
4279 if (iptr->isn_type == ISN_PROF_START)
4280 {
4281 func_line_start(&cookie, iptr->isn_lnum);
4282 // if we get here the instruction is executed
4283 func_line_exec(&cookie);
4284 }
4285 else
4286 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004287#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004288 }
4289 break;
4290
Bram Moolenaare99d4222021-06-13 14:01:26 +02004291 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004292 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004293 break;
4294
Bram Moolenaar389df252020-07-09 21:20:47 +02004295 case ISN_SHUFFLE:
4296 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004297 typval_T tmp_tv;
4298 int item = iptr->isn_arg.shuffle.shfl_item;
4299 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004300
4301 tmp_tv = *STACK_TV_BOT(-item);
4302 for ( ; up > 0 && item > 1; --up)
4303 {
4304 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4305 --item;
4306 }
4307 *STACK_TV_BOT(-item) = tmp_tv;
4308 }
4309 break;
4310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004312 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004314 ectx->ec_where.wt_index = 0;
4315 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 break;
4317 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004318 continue;
4319
Bram Moolenaard032f342020-07-18 18:13:02 +02004320func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004321 // Restore previous function. If the frame pointer is where we started
4322 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004323 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004324 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004325
Bram Moolenaar4c137212021-04-19 16:48:48 +02004326 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004327 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004328 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004329 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004330
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004331on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004332 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004333 // If "emsg_silent" is set then ignore the error, unless it was set
4334 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004335 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004336 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004337 {
4338 // If a sequence of instructions causes an error while ":silent!"
4339 // was used, restore the stack length and jump ahead to restoring
4340 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004341 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004342 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004343 while (ectx->ec_stack.ga_len
4344 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004345 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004346 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004347 clear_tv(STACK_TV_BOT(0));
4348 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004349 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4350 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004351 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004352 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004353 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004354on_fatal_error:
4355 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004356 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004357 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004358 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 }
4360
4361done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004362 ret = OK;
4363theend:
4364 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4365 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004366}
4367
4368/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004369 * Execute the instructions from a VAR_INSTR typeval and put the result in
4370 * "rettv".
4371 * Return OK or FAIL.
4372 */
4373 int
4374exe_typval_instr(typval_T *tv, typval_T *rettv)
4375{
4376 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4377 isn_T *save_instr = ectx->ec_instr;
4378 int save_iidx = ectx->ec_iidx;
4379 int res;
4380
4381 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4382 res = exec_instructions(ectx);
4383 if (res == OK)
4384 {
4385 *rettv = *STACK_TV_BOT(-1);
4386 --ectx->ec_stack.ga_len;
4387 }
4388
4389 ectx->ec_instr = save_instr;
4390 ectx->ec_iidx = save_iidx;
4391
4392 return res;
4393}
4394
4395/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004396 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4397 * "substitute_instr".
4398 */
4399 char_u *
4400exe_substitute_instr(void)
4401{
4402 ectx_T *ectx = substitute_instr->subs_ectx;
4403 isn_T *save_instr = ectx->ec_instr;
4404 int save_iidx = ectx->ec_iidx;
4405 char_u *res;
4406
4407 ectx->ec_instr = substitute_instr->subs_instr;
4408 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004409 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004410 typval_T *tv = STACK_TV_BOT(-1);
4411
Bram Moolenaar27523602021-06-05 21:36:19 +02004412 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004413 --ectx->ec_stack.ga_len;
4414 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004415 }
4416 else
4417 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004418 substitute_instr->subs_status = FAIL;
4419 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421
Bram Moolenaar4c137212021-04-19 16:48:48 +02004422 ectx->ec_instr = save_instr;
4423 ectx->ec_iidx = save_iidx;
4424
4425 return res;
4426}
4427
4428/*
4429 * Call a "def" function from old Vim script.
4430 * Return OK or FAIL.
4431 */
4432 int
4433call_def_function(
4434 ufunc_T *ufunc,
4435 int argc_arg, // nr of arguments
4436 typval_T *argv, // arguments
4437 partial_T *partial, // optional partial for context
4438 typval_T *rettv) // return value
4439{
4440 ectx_T ectx; // execution context
4441 int argc = argc_arg;
4442 typval_T *tv;
4443 int idx;
4444 int ret = FAIL;
4445 int defcount = ufunc->uf_args.ga_len - argc;
4446 sctx_T save_current_sctx = current_sctx;
4447 int did_emsg_before = did_emsg_cumul + did_emsg;
4448 int save_suppress_errthrow = suppress_errthrow;
4449 msglist_T **saved_msg_list = NULL;
4450 msglist_T *private_msg_list = NULL;
4451 int save_emsg_silent_def = emsg_silent_def;
4452 int save_did_emsg_def = did_emsg_def;
4453 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004454 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004455
4456// Get pointer to item in the stack.
4457#undef STACK_TV
4458#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4459
4460// Get pointer to item at the bottom of the stack, -1 is the bottom.
4461#undef STACK_TV_BOT
4462#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4463
4464// Get pointer to a local variable on the stack. Negative for arguments.
4465#undef STACK_TV_VAR
4466#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4467
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004468 // Update uf_has_breakpoint if needed.
4469 update_has_breakpoint(ufunc);
4470
Bram Moolenaar4c137212021-04-19 16:48:48 +02004471 if (ufunc->uf_def_status == UF_NOT_COMPILED
4472 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004473 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4474 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004475 == FAIL))
4476 {
4477 if (did_emsg_cumul + did_emsg == did_emsg_before)
4478 semsg(_(e_function_is_not_compiled_str),
4479 printable_func_name(ufunc));
4480 return FAIL;
4481 }
4482
4483 {
4484 // Check the function was really compiled.
4485 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4486 + ufunc->uf_dfunc_idx;
4487 if (INSTRUCTIONS(dfunc) == NULL)
4488 {
4489 iemsg("using call_def_function() on not compiled function");
4490 return FAIL;
4491 }
4492 }
4493
4494 // If depth of calling is getting too high, don't execute the function.
4495 orig_funcdepth = funcdepth_get();
4496 if (funcdepth_increment() == FAIL)
4497 return FAIL;
4498
4499 CLEAR_FIELD(ectx);
4500 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4501 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4502 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4503 {
4504 funcdepth_decrement();
4505 return FAIL;
4506 }
4507 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4508 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4509 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004510 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004511
4512 idx = argc - ufunc->uf_args.ga_len;
4513 if (idx > 0 && ufunc->uf_va_name == NULL)
4514 {
4515 if (idx == 1)
4516 emsg(_(e_one_argument_too_many));
4517 else
4518 semsg(_(e_nr_arguments_too_many), idx);
4519 goto failed_early;
4520 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004521 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4522 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004523 {
4524 if (idx == -1)
4525 emsg(_(e_one_argument_too_few));
4526 else
4527 semsg(_(e_nr_arguments_too_few), -idx);
4528 goto failed_early;
4529 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004530
4531 // Put arguments on the stack, but no more than what the function expects.
4532 // A lambda can be called with more arguments than it uses.
4533 for (idx = 0; idx < argc
4534 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4535 ++idx)
4536 {
4537 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4538 && argv[idx].v_type == VAR_SPECIAL
4539 && argv[idx].vval.v_number == VVAL_NONE)
4540 {
4541 // Use the default value.
4542 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4543 }
4544 else
4545 {
4546 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4547 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004548 ufunc->uf_arg_types[idx], &argv[idx],
4549 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004550 goto failed_early;
4551 copy_tv(&argv[idx], STACK_TV_BOT(0));
4552 }
4553 ++ectx.ec_stack.ga_len;
4554 }
4555
4556 // Turn varargs into a list. Empty list if no args.
4557 if (ufunc->uf_va_name != NULL)
4558 {
4559 int vararg_count = argc - ufunc->uf_args.ga_len;
4560
4561 if (vararg_count < 0)
4562 vararg_count = 0;
4563 else
4564 argc -= vararg_count;
4565 if (exe_newlist(vararg_count, &ectx) == FAIL)
4566 goto failed_early;
4567
4568 // Check the type of the list items.
4569 tv = STACK_TV_BOT(-1);
4570 if (ufunc->uf_va_type != NULL
4571 && ufunc->uf_va_type != &t_list_any
4572 && ufunc->uf_va_type->tt_member != &t_any
4573 && tv->vval.v_list != NULL)
4574 {
4575 type_T *expected = ufunc->uf_va_type->tt_member;
4576 listitem_T *li = tv->vval.v_list->lv_first;
4577
4578 for (idx = 0; idx < vararg_count; ++idx)
4579 {
4580 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004581 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004582 goto failed_early;
4583 li = li->li_next;
4584 }
4585 }
4586
4587 if (defcount > 0)
4588 // Move varargs list to below missing default arguments.
4589 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4590 --ectx.ec_stack.ga_len;
4591 }
4592
4593 // Make space for omitted arguments, will store default value below.
4594 // Any varargs list goes after them.
4595 if (defcount > 0)
4596 for (idx = 0; idx < defcount; ++idx)
4597 {
4598 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4599 ++ectx.ec_stack.ga_len;
4600 }
4601 if (ufunc->uf_va_name != NULL)
4602 ++ectx.ec_stack.ga_len;
4603
4604 // Frame pointer points to just after arguments.
4605 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4606 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4607
4608 {
4609 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4610 + ufunc->uf_dfunc_idx;
4611 ufunc_T *base_ufunc = dfunc->df_ufunc;
4612
4613 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4614 // by copy_func().
4615 if (partial != NULL || base_ufunc->uf_partial != NULL)
4616 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004617 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4618 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004619 goto failed_early;
4620 if (partial != NULL)
4621 {
4622 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4623 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004624 if (current_ectx->ec_outer_ref != NULL
4625 && current_ectx->ec_outer_ref->or_outer != NULL)
4626 ectx.ec_outer_ref->or_outer =
4627 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004628 }
4629 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004630 {
4631 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4632 ++partial->pt_refcount;
4633 ectx.ec_outer_ref->or_partial = partial;
4634 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004635 }
4636 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004637 {
4638 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4639 ++base_ufunc->uf_partial->pt_refcount;
4640 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4641 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004642 }
4643 }
4644
4645 // dummy frame entries
4646 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4647 {
4648 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4649 ++ectx.ec_stack.ga_len;
4650 }
4651
4652 {
4653 // Reserve space for local variables and any closure reference count.
4654 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4655 + ufunc->uf_dfunc_idx;
4656
4657 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4658 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4659 ectx.ec_stack.ga_len += dfunc->df_varcount;
4660 if (dfunc->df_has_closure)
4661 {
4662 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4663 STACK_TV_VAR(idx)->vval.v_number = 0;
4664 ++ectx.ec_stack.ga_len;
4665 }
4666
4667 ectx.ec_instr = INSTRUCTIONS(dfunc);
4668 }
4669
4670 // Following errors are in the function, not the caller.
4671 // Commands behave like vim9script.
4672 estack_push_ufunc(ufunc, 1);
4673 current_sctx = ufunc->uf_script_ctx;
4674 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4675
4676 // Use a specific location for storing error messages to be converted to an
4677 // exception.
4678 saved_msg_list = msg_list;
4679 msg_list = &private_msg_list;
4680
4681 // Do turn errors into exceptions.
4682 suppress_errthrow = FALSE;
4683
4684 // Do not delete the function while executing it.
4685 ++ufunc->uf_calls;
4686
4687 // When ":silent!" was used before calling then we still abort the
4688 // function. If ":silent!" is used in the function then we don't.
4689 emsg_silent_def = emsg_silent;
4690 did_emsg_def = 0;
4691
4692 ectx.ec_where.wt_index = 0;
4693 ectx.ec_where.wt_variable = FALSE;
4694
4695 // Execute the instructions until done.
4696 ret = exec_instructions(&ectx);
4697 if (ret == OK)
4698 {
4699 // function finished, get result from the stack.
4700 if (ufunc->uf_ret_type == &t_void)
4701 {
4702 rettv->v_type = VAR_VOID;
4703 }
4704 else
4705 {
4706 tv = STACK_TV_BOT(-1);
4707 *rettv = *tv;
4708 tv->v_type = VAR_UNKNOWN;
4709 }
4710 }
4711
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004712 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004713 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4714 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004715
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004716 // Deal with any remaining closures, they may be in use somewhere.
4717 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004718 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004719 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004720 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4721 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004722
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004723 estack_pop();
4724 current_sctx = save_current_sctx;
4725
Bram Moolenaarc970e422021-03-17 15:03:04 +01004726 // TODO: when is it safe to delete the function if it is no longer used?
4727 --ufunc->uf_calls;
4728
Bram Moolenaar352134b2020-10-17 22:04:08 +02004729 if (*msg_list != NULL && saved_msg_list != NULL)
4730 {
4731 msglist_T **plist = saved_msg_list;
4732
4733 // Append entries from the current msg_list (uncaught exceptions) to
4734 // the saved msg_list.
4735 while (*plist != NULL)
4736 plist = &(*plist)->next;
4737
4738 *plist = *msg_list;
4739 }
4740 msg_list = saved_msg_list;
4741
Bram Moolenaar4c137212021-04-19 16:48:48 +02004742 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004743 {
4744 cmdmod.cmod_filter_regmatch.regprog = NULL;
4745 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004746 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004747 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004748 emsg_silent_def = save_emsg_silent_def;
4749 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004750
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004751failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004752 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4754 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004755 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004758 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004759 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004760 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004761 if (ectx.ec_outer_ref->or_outer_allocated)
4762 vim_free(ectx.ec_outer_ref->or_outer);
4763 partial_unref(ectx.ec_outer_ref->or_partial);
4764 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004765 }
4766
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004767 // Not sure if this is necessary.
4768 suppress_errthrow = save_suppress_errthrow;
4769
Bram Moolenaarb5841b92021-07-15 18:09:53 +02004770 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
4771 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004772 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004773 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004774 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 return ret;
4776}
4777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004779 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4780 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4781 * "pfx" is prefixed to every line.
4782 */
4783 static void
4784list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4785{
4786 int line_idx = 0;
4787 int prev_current = 0;
4788 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004789 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004790
4791 for (current = 0; current < instr_count; ++current)
4792 {
4793 isn_T *iptr = &instr[current];
4794 char *line;
4795
4796 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004797 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004798 while (line_idx < iptr->isn_lnum
4799 && line_idx < ufunc->uf_lines.ga_len)
4800 {
4801 if (current > prev_current)
4802 {
4803 msg_puts("\n\n");
4804 prev_current = current;
4805 }
4806 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4807 if (line != NULL)
4808 msg(line);
4809 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004810 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4811 {
4812 int first_def_arg = ufunc->uf_args.ga_len
4813 - ufunc->uf_def_args.ga_len;
4814
4815 if (def_arg_idx > 0)
4816 msg_puts("\n\n");
4817 msg_start();
4818 msg_puts(" ");
4819 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4820 first_def_arg + def_arg_idx]);
4821 msg_puts(" = ");
4822 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4823 msg_clr_eos();
4824 msg_end();
4825 }
4826 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004827
4828 switch (iptr->isn_type)
4829 {
4830 case ISN_EXEC:
4831 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4832 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004833 case ISN_EXEC_SPLIT:
4834 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4835 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004836 case ISN_LEGACY_EVAL:
4837 smsg("%s%4d EVAL legacy %s", pfx, current,
4838 iptr->isn_arg.string);
4839 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004840 case ISN_REDIRSTART:
4841 smsg("%s%4d REDIR", pfx, current);
4842 break;
4843 case ISN_REDIREND:
4844 smsg("%s%4d REDIR END%s", pfx, current,
4845 iptr->isn_arg.number ? " append" : "");
4846 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004847 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004848#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004849 smsg("%s%4d CEXPR pre %s", pfx, current,
4850 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004851#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004852 break;
4853 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004854#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004855 {
4856 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4857
4858 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4859 cexpr_get_auname(cer->cer_cmdidx),
4860 cer->cer_forceit ? "!" : "",
4861 cer->cer_cmdline);
4862 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004863#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004864 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004865 case ISN_INSTR:
4866 {
4867 smsg("%s%4d INSTR", pfx, current);
4868 list_instructions(" ", iptr->isn_arg.instr,
4869 INT_MAX, NULL);
4870 msg(" -------------");
4871 }
4872 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004873 case ISN_SUBSTITUTE:
4874 {
4875 subs_T *subs = &iptr->isn_arg.subs;
4876
4877 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4878 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4879 msg(" -------------");
4880 }
4881 break;
4882 case ISN_EXECCONCAT:
4883 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4884 (varnumber_T)iptr->isn_arg.number);
4885 break;
4886 case ISN_ECHO:
4887 {
4888 echo_T *echo = &iptr->isn_arg.echo;
4889
4890 smsg("%s%4d %s %d", pfx, current,
4891 echo->echo_with_white ? "ECHO" : "ECHON",
4892 echo->echo_count);
4893 }
4894 break;
4895 case ISN_EXECUTE:
4896 smsg("%s%4d EXECUTE %lld", pfx, current,
4897 (varnumber_T)(iptr->isn_arg.number));
4898 break;
4899 case ISN_ECHOMSG:
4900 smsg("%s%4d ECHOMSG %lld", pfx, current,
4901 (varnumber_T)(iptr->isn_arg.number));
4902 break;
4903 case ISN_ECHOERR:
4904 smsg("%s%4d ECHOERR %lld", pfx, current,
4905 (varnumber_T)(iptr->isn_arg.number));
4906 break;
4907 case ISN_LOAD:
4908 {
4909 if (iptr->isn_arg.number < 0)
4910 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4911 (varnumber_T)(iptr->isn_arg.number
4912 + STACK_FRAME_SIZE));
4913 else
4914 smsg("%s%4d LOAD $%lld", pfx, current,
4915 (varnumber_T)(iptr->isn_arg.number));
4916 }
4917 break;
4918 case ISN_LOADOUTER:
4919 {
4920 if (iptr->isn_arg.number < 0)
4921 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4922 iptr->isn_arg.outer.outer_depth,
4923 iptr->isn_arg.outer.outer_idx
4924 + STACK_FRAME_SIZE);
4925 else
4926 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4927 iptr->isn_arg.outer.outer_depth,
4928 iptr->isn_arg.outer.outer_idx);
4929 }
4930 break;
4931 case ISN_LOADV:
4932 smsg("%s%4d LOADV v:%s", pfx, current,
4933 get_vim_var_name(iptr->isn_arg.number));
4934 break;
4935 case ISN_LOADSCRIPT:
4936 {
4937 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4938 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4939 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4940 + sref->sref_idx;
4941
4942 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4943 sv->sv_name,
4944 sref->sref_idx,
4945 si->sn_name);
4946 }
4947 break;
4948 case ISN_LOADS:
4949 {
4950 scriptitem_T *si = SCRIPT_ITEM(
4951 iptr->isn_arg.loadstore.ls_sid);
4952
4953 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4954 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4955 }
4956 break;
4957 case ISN_LOADAUTO:
4958 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4959 break;
4960 case ISN_LOADG:
4961 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4962 break;
4963 case ISN_LOADB:
4964 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4965 break;
4966 case ISN_LOADW:
4967 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4968 break;
4969 case ISN_LOADT:
4970 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4971 break;
4972 case ISN_LOADGDICT:
4973 smsg("%s%4d LOAD g:", pfx, current);
4974 break;
4975 case ISN_LOADBDICT:
4976 smsg("%s%4d LOAD b:", pfx, current);
4977 break;
4978 case ISN_LOADWDICT:
4979 smsg("%s%4d LOAD w:", pfx, current);
4980 break;
4981 case ISN_LOADTDICT:
4982 smsg("%s%4d LOAD t:", pfx, current);
4983 break;
4984 case ISN_LOADOPT:
4985 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4986 break;
4987 case ISN_LOADENV:
4988 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4989 break;
4990 case ISN_LOADREG:
4991 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4992 break;
4993
4994 case ISN_STORE:
4995 if (iptr->isn_arg.number < 0)
4996 smsg("%s%4d STORE arg[%lld]", pfx, current,
4997 iptr->isn_arg.number + STACK_FRAME_SIZE);
4998 else
4999 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
5000 break;
5001 case ISN_STOREOUTER:
5002 {
5003 if (iptr->isn_arg.number < 0)
5004 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5005 iptr->isn_arg.outer.outer_depth,
5006 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5007 else
5008 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5009 iptr->isn_arg.outer.outer_depth,
5010 iptr->isn_arg.outer.outer_idx);
5011 }
5012 break;
5013 case ISN_STOREV:
5014 smsg("%s%4d STOREV v:%s", pfx, current,
5015 get_vim_var_name(iptr->isn_arg.number));
5016 break;
5017 case ISN_STOREAUTO:
5018 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5019 break;
5020 case ISN_STOREG:
5021 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5022 break;
5023 case ISN_STOREB:
5024 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5025 break;
5026 case ISN_STOREW:
5027 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5028 break;
5029 case ISN_STORET:
5030 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5031 break;
5032 case ISN_STORES:
5033 {
5034 scriptitem_T *si = SCRIPT_ITEM(
5035 iptr->isn_arg.loadstore.ls_sid);
5036
5037 smsg("%s%4d STORES %s in %s", pfx, current,
5038 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5039 }
5040 break;
5041 case ISN_STORESCRIPT:
5042 {
5043 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5044 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5045 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
5046 + sref->sref_idx;
5047
5048 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
5049 sv->sv_name,
5050 sref->sref_idx,
5051 si->sn_name);
5052 }
5053 break;
5054 case ISN_STOREOPT:
5055 smsg("%s%4d STOREOPT &%s", pfx, current,
5056 iptr->isn_arg.storeopt.so_name);
5057 break;
5058 case ISN_STOREENV:
5059 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5060 break;
5061 case ISN_STOREREG:
5062 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
5063 break;
5064 case ISN_STORENR:
5065 smsg("%s%4d STORE %lld in $%d", pfx, current,
5066 iptr->isn_arg.storenr.stnr_val,
5067 iptr->isn_arg.storenr.stnr_idx);
5068 break;
5069
5070 case ISN_STOREINDEX:
5071 smsg("%s%4d STOREINDEX %s", pfx, current,
5072 vartype_name(iptr->isn_arg.vartype));
5073 break;
5074
5075 case ISN_STORERANGE:
5076 smsg("%s%4d STORERANGE", pfx, current);
5077 break;
5078
5079 // constants
5080 case ISN_PUSHNR:
5081 smsg("%s%4d PUSHNR %lld", pfx, current,
5082 (varnumber_T)(iptr->isn_arg.number));
5083 break;
5084 case ISN_PUSHBOOL:
5085 case ISN_PUSHSPEC:
5086 smsg("%s%4d PUSH %s", pfx, current,
5087 get_var_special_name(iptr->isn_arg.number));
5088 break;
5089 case ISN_PUSHF:
5090#ifdef FEAT_FLOAT
5091 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5092#endif
5093 break;
5094 case ISN_PUSHS:
5095 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5096 break;
5097 case ISN_PUSHBLOB:
5098 {
5099 char_u *r;
5100 char_u numbuf[NUMBUFLEN];
5101 char_u *tofree;
5102
5103 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5104 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5105 vim_free(tofree);
5106 }
5107 break;
5108 case ISN_PUSHFUNC:
5109 {
5110 char *name = (char *)iptr->isn_arg.string;
5111
5112 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5113 name == NULL ? "[none]" : name);
5114 }
5115 break;
5116 case ISN_PUSHCHANNEL:
5117#ifdef FEAT_JOB_CHANNEL
5118 {
5119 channel_T *channel = iptr->isn_arg.channel;
5120
5121 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5122 channel == NULL ? 0 : channel->ch_id);
5123 }
5124#endif
5125 break;
5126 case ISN_PUSHJOB:
5127#ifdef FEAT_JOB_CHANNEL
5128 {
5129 typval_T tv;
5130 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005131 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005132
5133 tv.v_type = VAR_JOB;
5134 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005135 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005136 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5137 }
5138#endif
5139 break;
5140 case ISN_PUSHEXC:
5141 smsg("%s%4d PUSH v:exception", pfx, current);
5142 break;
5143 case ISN_UNLET:
5144 smsg("%s%4d UNLET%s %s", pfx, current,
5145 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5146 iptr->isn_arg.unlet.ul_name);
5147 break;
5148 case ISN_UNLETENV:
5149 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5150 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5151 iptr->isn_arg.unlet.ul_name);
5152 break;
5153 case ISN_UNLETINDEX:
5154 smsg("%s%4d UNLETINDEX", pfx, current);
5155 break;
5156 case ISN_UNLETRANGE:
5157 smsg("%s%4d UNLETRANGE", pfx, current);
5158 break;
5159 case ISN_LOCKCONST:
5160 smsg("%s%4d LOCKCONST", pfx, current);
5161 break;
5162 case ISN_NEWLIST:
5163 smsg("%s%4d NEWLIST size %lld", pfx, current,
5164 (varnumber_T)(iptr->isn_arg.number));
5165 break;
5166 case ISN_NEWDICT:
5167 smsg("%s%4d NEWDICT size %lld", pfx, current,
5168 (varnumber_T)(iptr->isn_arg.number));
5169 break;
5170
5171 // function call
5172 case ISN_BCALL:
5173 {
5174 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5175
5176 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5177 internal_func_name(cbfunc->cbf_idx),
5178 cbfunc->cbf_argcount);
5179 }
5180 break;
5181 case ISN_DCALL:
5182 {
5183 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5184 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5185 + cdfunc->cdf_idx;
5186
5187 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5188 df->df_ufunc->uf_name_exp != NULL
5189 ? df->df_ufunc->uf_name_exp
5190 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5191 }
5192 break;
5193 case ISN_UCALL:
5194 {
5195 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5196
5197 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5198 cufunc->cuf_name, cufunc->cuf_argcount);
5199 }
5200 break;
5201 case ISN_PCALL:
5202 {
5203 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5204
5205 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5206 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5207 }
5208 break;
5209 case ISN_PCALL_END:
5210 smsg("%s%4d PCALL end", pfx, current);
5211 break;
5212 case ISN_RETURN:
5213 smsg("%s%4d RETURN", pfx, current);
5214 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005215 case ISN_RETURN_VOID:
5216 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005217 break;
5218 case ISN_FUNCREF:
5219 {
5220 funcref_T *funcref = &iptr->isn_arg.funcref;
5221 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5222 + funcref->fr_func;
5223
5224 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5225 }
5226 break;
5227
5228 case ISN_NEWFUNC:
5229 {
5230 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5231
5232 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5233 newfunc->nf_lambda, newfunc->nf_global);
5234 }
5235 break;
5236
5237 case ISN_DEF:
5238 {
5239 char_u *name = iptr->isn_arg.string;
5240
5241 smsg("%s%4d DEF %s", pfx, current,
5242 name == NULL ? (char_u *)"" : name);
5243 }
5244 break;
5245
5246 case ISN_JUMP:
5247 {
5248 char *when = "?";
5249
5250 switch (iptr->isn_arg.jump.jump_when)
5251 {
5252 case JUMP_ALWAYS:
5253 when = "JUMP";
5254 break;
5255 case JUMP_AND_KEEP_IF_TRUE:
5256 when = "JUMP_AND_KEEP_IF_TRUE";
5257 break;
5258 case JUMP_IF_FALSE:
5259 when = "JUMP_IF_FALSE";
5260 break;
5261 case JUMP_AND_KEEP_IF_FALSE:
5262 when = "JUMP_AND_KEEP_IF_FALSE";
5263 break;
5264 case JUMP_IF_COND_FALSE:
5265 when = "JUMP_IF_COND_FALSE";
5266 break;
5267 case JUMP_IF_COND_TRUE:
5268 when = "JUMP_IF_COND_TRUE";
5269 break;
5270 }
5271 smsg("%s%4d %s -> %d", pfx, current, when,
5272 iptr->isn_arg.jump.jump_where);
5273 }
5274 break;
5275
5276 case ISN_JUMP_IF_ARG_SET:
5277 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5278 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5279 iptr->isn_arg.jump.jump_where);
5280 break;
5281
5282 case ISN_FOR:
5283 {
5284 forloop_T *forloop = &iptr->isn_arg.forloop;
5285
5286 smsg("%s%4d FOR $%d -> %d", pfx, current,
5287 forloop->for_idx, forloop->for_end);
5288 }
5289 break;
5290
5291 case ISN_TRY:
5292 {
5293 try_T *try = &iptr->isn_arg.try;
5294
5295 if (try->try_ref->try_finally == 0)
5296 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5297 pfx, current,
5298 try->try_ref->try_catch,
5299 try->try_ref->try_endtry);
5300 else
5301 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5302 pfx, current,
5303 try->try_ref->try_catch,
5304 try->try_ref->try_finally,
5305 try->try_ref->try_endtry);
5306 }
5307 break;
5308 case ISN_CATCH:
5309 // TODO
5310 smsg("%s%4d CATCH", pfx, current);
5311 break;
5312 case ISN_TRYCONT:
5313 {
5314 trycont_T *trycont = &iptr->isn_arg.trycont;
5315
5316 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5317 trycont->tct_levels,
5318 trycont->tct_levels == 1 ? "" : "s",
5319 trycont->tct_where);
5320 }
5321 break;
5322 case ISN_FINALLY:
5323 smsg("%s%4d FINALLY", pfx, current);
5324 break;
5325 case ISN_ENDTRY:
5326 smsg("%s%4d ENDTRY", pfx, current);
5327 break;
5328 case ISN_THROW:
5329 smsg("%s%4d THROW", pfx, current);
5330 break;
5331
5332 // expression operations on number
5333 case ISN_OPNR:
5334 case ISN_OPFLOAT:
5335 case ISN_OPANY:
5336 {
5337 char *what;
5338 char *ins;
5339
5340 switch (iptr->isn_arg.op.op_type)
5341 {
5342 case EXPR_MULT: what = "*"; break;
5343 case EXPR_DIV: what = "/"; break;
5344 case EXPR_REM: what = "%"; break;
5345 case EXPR_SUB: what = "-"; break;
5346 case EXPR_ADD: what = "+"; break;
5347 default: what = "???"; break;
5348 }
5349 switch (iptr->isn_type)
5350 {
5351 case ISN_OPNR: ins = "OPNR"; break;
5352 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5353 case ISN_OPANY: ins = "OPANY"; break;
5354 default: ins = "???"; break;
5355 }
5356 smsg("%s%4d %s %s", pfx, current, ins, what);
5357 }
5358 break;
5359
5360 case ISN_COMPAREBOOL:
5361 case ISN_COMPARESPECIAL:
5362 case ISN_COMPARENR:
5363 case ISN_COMPAREFLOAT:
5364 case ISN_COMPARESTRING:
5365 case ISN_COMPAREBLOB:
5366 case ISN_COMPARELIST:
5367 case ISN_COMPAREDICT:
5368 case ISN_COMPAREFUNC:
5369 case ISN_COMPAREANY:
5370 {
5371 char *p;
5372 char buf[10];
5373 char *type;
5374
5375 switch (iptr->isn_arg.op.op_type)
5376 {
5377 case EXPR_EQUAL: p = "=="; break;
5378 case EXPR_NEQUAL: p = "!="; break;
5379 case EXPR_GREATER: p = ">"; break;
5380 case EXPR_GEQUAL: p = ">="; break;
5381 case EXPR_SMALLER: p = "<"; break;
5382 case EXPR_SEQUAL: p = "<="; break;
5383 case EXPR_MATCH: p = "=~"; break;
5384 case EXPR_IS: p = "is"; break;
5385 case EXPR_ISNOT: p = "isnot"; break;
5386 case EXPR_NOMATCH: p = "!~"; break;
5387 default: p = "???"; break;
5388 }
5389 STRCPY(buf, p);
5390 if (iptr->isn_arg.op.op_ic == TRUE)
5391 strcat(buf, "?");
5392 switch(iptr->isn_type)
5393 {
5394 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5395 case ISN_COMPARESPECIAL:
5396 type = "COMPARESPECIAL"; break;
5397 case ISN_COMPARENR: type = "COMPARENR"; break;
5398 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5399 case ISN_COMPARESTRING:
5400 type = "COMPARESTRING"; break;
5401 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5402 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5403 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5404 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5405 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5406 default: type = "???"; break;
5407 }
5408
5409 smsg("%s%4d %s %s", pfx, current, type, buf);
5410 }
5411 break;
5412
5413 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5414 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5415
5416 // expression operations
5417 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5418 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5419 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5420 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5421 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5422 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5423 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5424 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5425 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5426 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5427 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5428 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5429 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005430 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5431 iptr->isn_arg.getitem.gi_index,
5432 iptr->isn_arg.getitem.gi_with_op ?
5433 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005434 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5435 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5436 iptr->isn_arg.string); break;
5437 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5438
5439 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5440 case ISN_CHECKTYPE:
5441 {
5442 checktype_T *ct = &iptr->isn_arg.type;
5443 char *tofree;
5444
5445 if (ct->ct_arg_idx == 0)
5446 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5447 type_name(ct->ct_type, &tofree),
5448 (int)ct->ct_off);
5449 else
5450 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5451 type_name(ct->ct_type, &tofree),
5452 (int)ct->ct_off,
5453 (int)ct->ct_arg_idx);
5454 vim_free(tofree);
5455 break;
5456 }
5457 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5458 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5459 iptr->isn_arg.checklen.cl_min_len);
5460 break;
5461 case ISN_SETTYPE:
5462 {
5463 char *tofree;
5464
5465 smsg("%s%4d SETTYPE %s", pfx, current,
5466 type_name(iptr->isn_arg.type.ct_type, &tofree));
5467 vim_free(tofree);
5468 break;
5469 }
5470 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005471 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5472 smsg("%s%4d INVERT %d (!val)", pfx, current,
5473 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005474 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005475 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5476 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005477 break;
5478 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005479 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005480 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005481 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5482 pfx, current,
5483 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005484 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005485 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5486 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005487 break;
5488 case ISN_PUT:
5489 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5490 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005491 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005492 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5493 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005494 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005495 else
5496 smsg("%s%4d PUT %c %ld", pfx, current,
5497 iptr->isn_arg.put.put_regname,
5498 (long)iptr->isn_arg.put.put_lnum);
5499 break;
5500
5501 // TODO: summarize modifiers
5502 case ISN_CMDMOD:
5503 {
5504 char_u *buf;
5505 size_t len = produce_cmdmods(
5506 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5507
5508 buf = alloc(len + 1);
5509 if (buf != NULL)
5510 {
5511 (void)produce_cmdmods(
5512 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5513 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5514 vim_free(buf);
5515 }
5516 break;
5517 }
5518 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5519
5520 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005521 smsg("%s%4d PROFILE START line %d", pfx, current,
5522 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005523 break;
5524
5525 case ISN_PROF_END:
5526 smsg("%s%4d PROFILE END", pfx, current);
5527 break;
5528
Bram Moolenaare99d4222021-06-13 14:01:26 +02005529 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005530 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5531 iptr->isn_arg.debug.dbg_break_lnum + 1,
5532 iptr->isn_lnum,
5533 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005534 break;
5535
Bram Moolenaar4c137212021-04-19 16:48:48 +02005536 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5537 iptr->isn_arg.unpack.unp_count,
5538 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5539 break;
5540 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5541 iptr->isn_arg.shuffle.shfl_item,
5542 iptr->isn_arg.shuffle.shfl_up);
5543 break;
5544 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5545
5546 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5547 return;
5548 }
5549
5550 out_flush(); // output one line at a time
5551 ui_breakcheck();
5552 if (got_int)
5553 break;
5554 }
5555}
5556
5557/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005558 * Handle command line completion for the :disassemble command.
5559 */
5560 void
5561set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5562{
5563 char_u *p;
5564
5565 // Default: expand user functions, "debug" and "profile"
5566 xp->xp_context = EXPAND_DISASSEMBLE;
5567 xp->xp_pattern = arg;
5568
5569 // first argument already typed: only user function names
5570 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5571 {
5572 xp->xp_context = EXPAND_USER_FUNC;
5573 xp->xp_pattern = skipwhite(p);
5574 }
5575}
5576
5577/*
5578 * Function given to ExpandGeneric() to obtain the list of :disassemble
5579 * arguments.
5580 */
5581 char_u *
5582get_disassemble_argument(expand_T *xp, int idx)
5583{
5584 if (idx == 0)
5585 return (char_u *)"debug";
5586 if (idx == 1)
5587 return (char_u *)"profile";
5588 return get_user_func_name(xp, idx - 2);
5589}
5590
5591/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005592 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005593 * We don't really need this at runtime, but we do have tests that require it,
5594 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595 */
5596 void
5597ex_disassemble(exarg_T *eap)
5598{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005599 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005600 char_u *fname;
5601 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602 dfunc_T *dfunc;
5603 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005604 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005605 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005606 compiletype_T compile_type = CT_NONE;
5607
5608 if (STRNCMP(arg, "profile", 7) == 0)
5609 {
5610 compile_type = CT_PROFILE;
5611 arg = skipwhite(arg + 7);
5612 }
5613 else if (STRNCMP(arg, "debug", 5) == 0)
5614 {
5615 compile_type = CT_DEBUG;
5616 arg = skipwhite(arg + 5);
5617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005619 if (STRNCMP(arg, "<lambda>", 8) == 0)
5620 {
5621 arg += 8;
5622 (void)getdigits(&arg);
5623 fname = vim_strnsave(eap->arg, arg - eap->arg);
5624 }
5625 else
5626 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005627 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005628 if (fname == NULL)
5629 {
5630 semsg(_(e_invarg2), eap->arg);
5631 return;
5632 }
5633
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005634 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005635 if (ufunc == NULL)
5636 {
5637 char_u *p = untrans_function_name(fname);
5638
5639 if (p != NULL)
5640 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005641 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005642 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005643 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644 if (ufunc == NULL)
5645 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005646 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 return;
5648 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005649 if (func_needs_compiling(ufunc, compile_type)
5650 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005651 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005652 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005654 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005655 return;
5656 }
5657 if (ufunc->uf_name_exp != NULL)
5658 msg((char *)ufunc->uf_name_exp);
5659 else
5660 msg((char *)ufunc->uf_name);
5661
5662 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005663 switch (compile_type)
5664 {
5665 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005666#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005667 instr = dfunc->df_instr_prof;
5668 instr_count = dfunc->df_instr_prof_count;
5669 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005670#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005671 // FALLTHROUGH
5672 case CT_NONE:
5673 instr = dfunc->df_instr;
5674 instr_count = dfunc->df_instr_count;
5675 break;
5676 case CT_DEBUG:
5677 instr = dfunc->df_instr_debug;
5678 instr_count = dfunc->df_instr_debug_count;
5679 break;
5680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681
Bram Moolenaar4c137212021-04-19 16:48:48 +02005682 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683}
5684
5685/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005686 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687 * list, etc. Mostly like what JavaScript does, except that empty list and
5688 * empty dictionary are FALSE.
5689 */
5690 int
5691tv2bool(typval_T *tv)
5692{
5693 switch (tv->v_type)
5694 {
5695 case VAR_NUMBER:
5696 return tv->vval.v_number != 0;
5697 case VAR_FLOAT:
5698#ifdef FEAT_FLOAT
5699 return tv->vval.v_float != 0.0;
5700#else
5701 break;
5702#endif
5703 case VAR_PARTIAL:
5704 return tv->vval.v_partial != NULL;
5705 case VAR_FUNC:
5706 case VAR_STRING:
5707 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5708 case VAR_LIST:
5709 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5710 case VAR_DICT:
5711 return tv->vval.v_dict != NULL
5712 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5713 case VAR_BOOL:
5714 case VAR_SPECIAL:
5715 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5716 case VAR_JOB:
5717#ifdef FEAT_JOB_CHANNEL
5718 return tv->vval.v_job != NULL;
5719#else
5720 break;
5721#endif
5722 case VAR_CHANNEL:
5723#ifdef FEAT_JOB_CHANNEL
5724 return tv->vval.v_channel != NULL;
5725#else
5726 break;
5727#endif
5728 case VAR_BLOB:
5729 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5730 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005731 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005732 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005733 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734 break;
5735 }
5736 return FALSE;
5737}
5738
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005739 void
5740emsg_using_string_as(typval_T *tv, int as_number)
5741{
5742 semsg(_(as_number ? e_using_string_as_number_str
5743 : e_using_string_as_bool_str),
5744 tv->vval.v_string == NULL
5745 ? (char_u *)"" : tv->vval.v_string);
5746}
5747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005748/*
5749 * If "tv" is a string give an error and return FAIL.
5750 */
5751 int
5752check_not_string(typval_T *tv)
5753{
5754 if (tv->v_type == VAR_STRING)
5755 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005756 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005757 clear_tv(tv);
5758 return FAIL;
5759 }
5760 return OK;
5761}
5762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763
5764#endif // FEAT_EVAL