blob: 6d0da617977e1a792d7621a8cfe94c1d6decbb5b [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 Moolenaar7e82c5f2021-02-21 21:32:45 +010029 int tcd_catch_idx; // instruction of the first :catch or :finally
30 int tcd_finally_idx; // instruction of the :finally block or zero
31 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010032 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010033 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_return; // when TRUE return from end of :finally
35} trycmd_T;
36
Bram Moolenaar4c137212021-04-19 16:48:48 +020037// Data local to a function.
38// On a function call, if not empty, is saved on the stack and restored when
39// returning.
40typedef struct {
41 int floc_restore_cmdmod;
42 cmdmod_T floc_save_cmdmod;
43 int floc_restore_cmdmod_stacklen;
44} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010045
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020046// Structure to hold a reference to an outer_T, with information of whether it
47// was allocated.
48typedef struct {
49 outer_T *or_outer;
50 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
51 int or_outer_allocated; // free "or_outer" later
52} outer_ref_T;
53
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010054// A stack is used to store:
55// - arguments passed to a :def function
56// - info about the calling function, to use when returning
57// - local variables
58// - temporary values
59//
60// In detail (FP == Frame Pointer):
61// arg1 first argument from caller (if present)
62// arg2 second argument from caller (if present)
63// extra_arg1 any missing optional argument default value
64// FP -> cur_func calling function
65// current previous instruction pointer
66// frame_ptr previous Frame Pointer
67// var1 space for local variable
68// var2 space for local variable
69// .... fixed space for max. number of local variables
70// temp temporary values
71// .... flexible space for temporary values (can grow big)
72
73/*
74 * Execution context.
75 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010076struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020078 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020079 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020081 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020082 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 garray_T ec_trystack; // stack of trycmd_T values
85 int ec_in_catch; // when TRUE in catch or finally block
86
87 int ec_dfunc_idx; // current function index
88 isn_T *ec_instr; // array with instructions
89 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020090
91 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020092
93 int ec_did_emsg_before;
94 int ec_trylevel_at_start;
95 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010096};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097
Bram Moolenaar12d26532021-02-19 19:13:21 +010098#ifdef FEAT_PROFILE
99// stack of profinfo_T used when profiling.
100static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
101#endif
102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200104#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 +0100105
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200106 void
107to_string_error(vartype_T vartype)
108{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200109 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200110}
111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100113 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 */
115 static int
116ufunc_argcount(ufunc_T *ufunc)
117{
118 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200151 * If debug_tick changed check if "ufunc" has a breakpoint and update
152 * "uf_has_breakpoint".
153 */
154 static void
155update_has_breakpoint(ufunc_T *ufunc)
156{
157 if (ufunc->uf_debug_tick != debug_tick)
158 {
159 linenr_T breakpoint;
160
161 ufunc->uf_debug_tick = debug_tick;
162 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
163 ufunc->uf_has_breakpoint = breakpoint > 0;
164 }
165}
166
167/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100169 * This adds a stack frame and sets the instruction pointer to the start of the
170 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200171 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 *
173 * Stack has:
174 * - current arguments (already there)
175 * - omitted optional argument (default values) added here
176 * - stack frame:
177 * - pointer to calling function
178 * - Index of next instruction in calling function
179 * - previous frame pointer
180 * - reserved space for local variables
181 */
182 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100183call_dfunc(
184 int cdf_idx,
185 partial_T *pt,
186 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100187 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100189 int argcount = argcount_arg;
190 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
191 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200192 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100193 int arg_to_add;
194 int vararg_count = 0;
195 int varcount;
196 int idx;
197 estack_T *entry;
198 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199
200 if (dfunc->df_deleted)
201 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100202 // don't use ufunc->uf_name, it may have been freed
203 emsg_funcname(e_func_deleted,
204 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205 return FAIL;
206 }
207
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100208#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100209 if (do_profiling == PROF_YES)
210 {
211 if (ga_grow(&profile_info_ga, 1) == OK)
212 {
213 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
214 + profile_info_ga.ga_len;
215 ++profile_info_ga.ga_len;
216 CLEAR_POINTER(info);
217 profile_may_start_func(info, ufunc,
218 (((dfunc_T *)def_functions.ga_data)
219 + ectx->ec_dfunc_idx)->df_ufunc);
220 }
221
222 // Profiling might be enabled/disabled along the way. This should not
223 // fail, since the function was compiled before and toggling profiling
224 // doesn't change any errors.
Bram Moolenaare99d4222021-06-13 14:01:26 +0200225 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
226 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100227 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100228 return FAIL;
229 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100230#endif
231
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200232 // Update uf_has_breakpoint if needed.
233 update_has_breakpoint(ufunc);
234
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200235 // When debugging and using "cont" switches to the not-debugged
236 // instructions, may need to still compile them.
237 if ((func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
238 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
239 == FAIL)
240 || INSTRUCTIONS(dfunc) == NULL)
241 {
242 if (did_emsg_cumul + did_emsg == did_emsg_before)
243 semsg(_(e_function_is_not_compiled_str),
244 printable_func_name(ufunc));
245 return FAIL;
246 }
247
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200248 if (ufunc->uf_va_name != NULL)
249 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200250 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200251 // Stack at time of call with 2 varargs:
252 // normal_arg
253 // optional_arg
254 // vararg_1
255 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200256 // After creating the list:
257 // normal_arg
258 // optional_arg
259 // vararg-list
260 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200261 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200262 // After creating the list
263 // normal_arg
264 // (space for optional_arg)
265 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200266 vararg_count = argcount - ufunc->uf_args.ga_len;
267 if (vararg_count < 0)
268 vararg_count = 0;
269 else
270 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200271 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200272 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200273
274 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200275 }
276
Bram Moolenaarfe270812020-04-11 22:31:27 +0200277 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200278 if (arg_to_add < 0)
279 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200280 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200281 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200282 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200283 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200284 return FAIL;
285 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200286
287 // Reserve space for:
288 // - missing arguments
289 // - stack frame
290 // - local variables
291 // - if needed: a counter for number of closures created in
292 // ectx->ec_funcrefs.
293 varcount = dfunc->df_varcount + dfunc->df_has_closure;
294 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
295 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 return FAIL;
297
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100298 // If depth of calling is getting too high, don't execute the function.
299 if (funcdepth_increment() == FAIL)
300 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200301 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100302
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100303 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200304 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100305 {
306 floc = ALLOC_ONE(funclocal_T);
307 if (floc == NULL)
308 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200309 *floc = ectx->ec_funclocal;
310 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100311 }
312
Bram Moolenaarfe270812020-04-11 22:31:27 +0200313 // Move the vararg-list to below the missing optional arguments.
314 if (vararg_count > 0 && arg_to_add > 0)
315 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100316
317 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200318 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200319 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200320 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321
322 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100323 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
324 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200325 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200326 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
327 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100328 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100329 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200330 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331
332 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200333 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200335 if (dfunc->df_has_closure)
336 {
337 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
338
339 tv->v_type = VAR_NUMBER;
340 tv->vval.v_number = 0;
341 }
342 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100344 if (pt != NULL || ufunc->uf_partial != NULL
345 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100346 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200347 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100348
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200349 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100350 return FAIL;
351 if (pt != NULL)
352 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200353 ref->or_outer = &pt->pt_outer;
354 ++pt->pt_refcount;
355 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100356 }
357 else if (ufunc->uf_partial != NULL)
358 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200359 ref->or_outer = &ufunc->uf_partial->pt_outer;
360 ++ufunc->uf_partial->pt_refcount;
361 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100362 }
363 else
364 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200365 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
366 if (ref->or_outer == NULL)
367 {
368 vim_free(ref);
369 return FAIL;
370 }
371 ref->or_outer_allocated = TRUE;
372 ref->or_outer->out_stack = &ectx->ec_stack;
373 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
374 if (ectx->ec_outer_ref != NULL)
375 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100376 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200377 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100378 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100379 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200380 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100381
Bram Moolenaarc970e422021-03-17 15:03:04 +0100382 ++ufunc->uf_calls;
383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 // Set execution state to the start of the called function.
385 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100386 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100387 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200388 if (entry != NULL)
389 {
390 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200391 // Save the current context so it can be restored on return.
392 entry->es_save_sctx = current_sctx;
393 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200394 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100395
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200396 // Start execution at the first instruction.
397 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398
399 return OK;
400}
401
402// Get pointer to item in the stack.
403#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
404
405/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200406 * Used when returning from a function: Check if any closure is still
407 * referenced. If so then move the arguments and variables to a separate piece
408 * of stack to be used when the closure is called.
409 * When "free_arguments" is TRUE the arguments are to be freed.
410 * Returns FAIL when out of memory.
411 */
412 static int
413handle_closure_in_use(ectx_T *ectx, int free_arguments)
414{
415 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
416 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200417 int argcount;
418 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200419 int idx;
420 typval_T *tv;
421 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200422 garray_T *gap = &ectx->ec_funcrefs;
423 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200424
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200425 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200426 return OK; // function was freed
427 if (dfunc->df_has_closure == 0)
428 return OK; // no closures
429 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
430 closure_count = tv->vval.v_number;
431 if (closure_count == 0)
432 return OK; // no funcrefs created
433
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200434 argcount = ufunc_argcount(dfunc->df_ufunc);
435 top = ectx->ec_frame_idx - argcount;
436
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200437 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200438 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200439 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200440 partial_T *pt;
441 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200442
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200443 if (off < 0)
444 continue; // count is off or already done
445 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200446 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200447 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200448 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200449 int i;
450
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200451 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200452 // unreferenced on return.
453 for (i = 0; i < dfunc->df_varcount; ++i)
454 {
455 typval_T *stv = STACK_TV(ectx->ec_frame_idx
456 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200457 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200458 --refcount;
459 }
460 if (refcount > 1)
461 {
462 closure_in_use = TRUE;
463 break;
464 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200465 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 }
467
468 if (closure_in_use)
469 {
470 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
471 typval_T *stack;
472
473 // A closure is using the arguments and/or local variables.
474 // Move them to the called function.
475 if (funcstack == NULL)
476 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200477 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
478 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200479 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
480 funcstack->fs_ga.ga_data = stack;
481 if (stack == NULL)
482 {
483 vim_free(funcstack);
484 return FAIL;
485 }
486
487 // Move or copy the arguments.
488 for (idx = 0; idx < argcount; ++idx)
489 {
490 tv = STACK_TV(top + idx);
491 if (free_arguments)
492 {
493 *(stack + idx) = *tv;
494 tv->v_type = VAR_UNKNOWN;
495 }
496 else
497 copy_tv(tv, stack + idx);
498 }
499 // Move the local variables.
500 for (idx = 0; idx < dfunc->df_varcount; ++idx)
501 {
502 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200503
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200504 // A partial created for a local function, that is also used as a
505 // local variable, has a reference count for the variable, thus
506 // will never go down to zero. When all these refcounts are one
507 // then the funcstack is unused. We need to count how many we have
508 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200509 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
510 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200511 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200512
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200513 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200514 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
515 gap->ga_len - closure_count + i])
516 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200517 }
518
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200519 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200520 tv->v_type = VAR_UNKNOWN;
521 }
522
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200523 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200524 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200525 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
526 - closure_count + idx];
527 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200528 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200529 ++funcstack->fs_refcount;
530 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100531 pt->pt_outer.out_stack = &funcstack->fs_ga;
532 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200533 }
534 }
535 }
536
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200537 for (idx = 0; idx < closure_count; ++idx)
538 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
539 - closure_count + idx]);
540 gap->ga_len -= closure_count;
541 if (gap->ga_len == 0)
542 ga_clear(gap);
543
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200544 return OK;
545}
546
547/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200548 * Called when a partial is freed or its reference count goes down to one. The
549 * funcstack may be the only reference to the partials in the local variables.
550 * Go over all of them, the funcref and can be freed if all partials
551 * referencing the funcstack have a reference count of one.
552 */
553 void
554funcstack_check_refcount(funcstack_T *funcstack)
555{
556 int i;
557 garray_T *gap = &funcstack->fs_ga;
558 int done = 0;
559
560 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
561 return;
562 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
563 {
564 typval_T *tv = ((typval_T *)gap->ga_data) + i;
565
566 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
567 && tv->vval.v_partial->pt_funcstack == funcstack
568 && tv->vval.v_partial->pt_refcount == 1)
569 ++done;
570 }
571 if (done == funcstack->fs_min_refcount)
572 {
573 typval_T *stack = gap->ga_data;
574
575 // All partials referencing the funcstack have a reference count of
576 // one, thus the funcstack is no longer of use.
577 for (i = 0; i < gap->ga_len; ++i)
578 clear_tv(stack + i);
579 vim_free(stack);
580 vim_free(funcstack);
581 }
582}
583
584/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 * Return from the current function.
586 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200587 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200588func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100591 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200592 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
593 + ectx->ec_dfunc_idx;
594 int argcount = ufunc_argcount(dfunc->df_ufunc);
595 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200596 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100597 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
598 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200599 funclocal_T *floc;
600#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100601 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
602 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
Bram Moolenaar12d26532021-02-19 19:13:21 +0100604 if (do_profiling == PROF_YES)
605 {
606 ufunc_T *caller = prev_dfunc->df_ufunc;
607
608 if (dfunc->df_ufunc->uf_profiling
609 || (caller != NULL && caller->uf_profiling))
610 {
611 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
612 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
613 --profile_info_ga.ga_len;
614 }
615 }
616#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100617 // TODO: when is it safe to delete the function when it is no longer used?
618 --dfunc->df_ufunc->uf_calls;
619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200621 entry = estack_pop();
622 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200623 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200625 if (handle_closure_in_use(ectx, TRUE) == FAIL)
626 return FAIL;
627
628 // Clear the arguments.
629 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
630 clear_tv(STACK_TV(idx));
631
632 // Clear local variables and temp values, but not the return value.
633 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100634 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100636
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100637 // The return value should be on top of the stack. However, when aborting
638 // it may not be there and ec_frame_idx is the top of the stack.
639 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100640 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100641 ret_idx = 0;
642
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200643 if (ectx->ec_outer_ref != NULL)
644 {
645 if (ectx->ec_outer_ref->or_outer_allocated)
646 vim_free(ectx->ec_outer_ref->or_outer);
647 partial_unref(ectx->ec_outer_ref->or_partial);
648 vim_free(ectx->ec_outer_ref);
649 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100650
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100651 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100652 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100653 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
654 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200655 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
656 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200657 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100658 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100659 floc = (void *)STACK_TV(ectx->ec_frame_idx
660 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200661 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100662 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
663 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200664
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100665 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200666 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100667 else
668 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200669 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100670 vim_free(floc);
671 }
672
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100673 if (ret_idx > 0)
674 {
675 // Reset the stack to the position before the call, with a spot for the
676 // return value, moved there from above the frame.
677 ectx->ec_stack.ga_len = top + 1;
678 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
679 }
680 else
681 // Reset the stack to the position before the call.
682 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200683
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100684 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200685 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200686 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100687}
688
689#undef STACK_TV
690
691/*
692 * Prepare arguments and rettv for calling a builtin or user function.
693 */
694 static int
695call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
696{
697 int idx;
698 typval_T *tv;
699
700 // Move arguments from bottom of the stack to argvars[] and add terminator.
701 for (idx = 0; idx < argcount; ++idx)
702 argvars[idx] = *STACK_TV_BOT(idx - argcount);
703 argvars[argcount].v_type = VAR_UNKNOWN;
704
705 // Result replaces the arguments on the stack.
706 if (argcount > 0)
707 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200708 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 return FAIL;
710 else
711 ++ectx->ec_stack.ga_len;
712
713 // Default return value is zero.
714 tv = STACK_TV_BOT(-1);
715 tv->v_type = VAR_NUMBER;
716 tv->vval.v_number = 0;
717
718 return OK;
719}
720
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200721// Ugly global to avoid passing the execution context around through many
722// layers.
723static ectx_T *current_ectx = NULL;
724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725/*
726 * Call a builtin function by index.
727 */
728 static int
729call_bfunc(int func_idx, int argcount, ectx_T *ectx)
730{
731 typval_T argvars[MAX_FUNC_ARGS];
732 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100733 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200734 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735
736 if (call_prepare(argcount, argvars, ectx) == FAIL)
737 return FAIL;
738
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 Moolenaar8a7d6542020-01-26 15:56:19 +0100744
745 // Clear the arguments.
746 for (idx = 0; idx < argcount; ++idx)
747 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200748
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100749 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200750 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 return OK;
752}
753
754/*
755 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100756 * If the function is compiled this will add a stack frame and set the
757 * instruction pointer at the start of the function.
758 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200759 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100760 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 */
762 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100763call_ufunc(
764 ufunc_T *ufunc,
765 partial_T *pt,
766 int argcount,
767 ectx_T *ectx,
768 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769{
770 typval_T argvars[MAX_FUNC_ARGS];
771 funcexe_T funcexe;
772 int error;
773 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100774 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200775 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776
Bram Moolenaare99d4222021-06-13 14:01:26 +0200777 if (func_needs_compiling(ufunc, compile_type)
778 && compile_def_function(ufunc, FALSE, compile_type, NULL)
779 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200780 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200781 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100782 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100783 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100784 if (error != FCERR_UNKNOWN)
785 {
786 if (error == FCERR_TOOMANY)
787 semsg(_(e_toomanyarg), ufunc->uf_name);
788 else
789 semsg(_(e_toofewarg), ufunc->uf_name);
790 return FAIL;
791 }
792
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100793 // The function has been compiled, can call it quickly. For a function
794 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100795 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100796 if (iptr != NULL)
797 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100798 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100799 iptr->isn_type = ISN_DCALL;
800 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
801 iptr->isn_arg.dfunc.cdf_argcount = argcount;
802 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200803 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805
806 if (call_prepare(argcount, argvars, ectx) == FAIL)
807 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200808 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 funcexe.evaluate = TRUE;
810
811 // Call the user function. Result goes in last position on the stack.
812 // TODO: add selfdict if there is one
813 error = call_user_func_check(ufunc, argcount, argvars,
814 STACK_TV_BOT(-1), &funcexe, NULL);
815
816 // Clear the arguments.
817 for (idx = 0; idx < argcount; ++idx)
818 clear_tv(&argvars[idx]);
819
820 if (error != FCERR_NONE)
821 {
822 user_func_error(error, ufunc->uf_name);
823 return FAIL;
824 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100825 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200826 // Error other than from calling the function itself.
827 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828 return OK;
829}
830
831/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100832 * If command modifiers were applied restore them.
833 */
834 static void
835may_restore_cmdmod(funclocal_T *funclocal)
836{
837 if (funclocal->floc_restore_cmdmod)
838 {
839 cmdmod.cmod_filter_regmatch.regprog = NULL;
840 undo_cmdmod(&cmdmod);
841 cmdmod = funclocal->floc_save_cmdmod;
842 funclocal->floc_restore_cmdmod = FALSE;
843 }
844}
845
846/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200847 * Return TRUE if an error was given or CTRL-C was pressed.
848 */
849 static int
850vim9_aborting(int prev_called_emsg)
851{
852 return called_emsg > prev_called_emsg || got_int || did_throw;
853}
854
855/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 * Execute a function by "name".
857 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100858 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 * Returns FAIL if not found without an error message.
860 */
861 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100862call_by_name(
863 char_u *name,
864 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100865 ectx_T *ectx,
866 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867{
868 ufunc_T *ufunc;
869
870 if (builtin_function(name, -1))
871 {
872 int func_idx = find_internal_func(name);
873
874 if (func_idx < 0)
875 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200876 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 return FAIL;
878 return call_bfunc(func_idx, argcount, ectx);
879 }
880
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200881 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200882
883 if (ufunc == NULL)
884 {
885 int called_emsg_before = called_emsg;
886
887 if (script_autoload(name, TRUE))
888 // loaded a package, search for the function again
889 ufunc = find_func(name, FALSE, NULL);
890 if (vim9_aborting(called_emsg_before))
891 return FAIL; // bail out if loading the script caused an error
892 }
893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100895 {
896 if (ufunc->uf_arg_types != NULL)
897 {
898 int i;
899 typval_T *argv = STACK_TV_BOT(0) - argcount;
900
901 // The function can change at runtime, check that the argument
902 // types are correct.
903 for (i = 0; i < argcount; ++i)
904 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100905 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100906
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100907 if (i < ufunc->uf_args.ga_len)
908 type = ufunc->uf_arg_types[i];
909 else if (ufunc->uf_va_type != NULL)
910 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100911 if (type != NULL && check_typval_arg_type(type,
912 &argv[i], i + 1) == FAIL)
913 return FAIL;
914 }
915 }
916
Bram Moolenaar4c137212021-04-19 16:48:48 +0200917 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919
920 return FAIL;
921}
922
923 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100924call_partial(
925 typval_T *tv,
926 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100927 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200929 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200930 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100932 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
934 if (tv->v_type == VAR_PARTIAL)
935 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200936 partial_T *pt = tv->vval.v_partial;
937 int i;
938
939 if (pt->pt_argc > 0)
940 {
941 // Make space for arguments from the partial, shift the "argcount"
942 // arguments up.
943 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
944 return FAIL;
945 for (i = 1; i <= argcount; ++i)
946 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
947 ectx->ec_stack.ga_len += pt->pt_argc;
948 argcount += pt->pt_argc;
949
950 // copy the arguments from the partial onto the stack
951 for (i = 0; i < pt->pt_argc; ++i)
952 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954
955 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200956 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 name = pt->pt_name;
959 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200960 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200962 if (name != NULL)
963 {
964 char_u fname_buf[FLEN_FIXED + 1];
965 char_u *tofree = NULL;
966 int error = FCERR_NONE;
967 char_u *fname;
968
969 // May need to translate <SNR>123_ to K_SNR.
970 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
971 if (error != FCERR_NONE)
972 res = FAIL;
973 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200974 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200975 vim_free(tofree);
976 }
977
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100978 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 {
980 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200981 semsg(_(e_unknownfunc),
982 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 return FAIL;
984 }
985 return OK;
986}
987
988/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200989 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
990 * TRUE.
991 */
992 static int
993error_if_locked(int lock, char *error)
994{
995 if (lock & (VAR_LOCKED | VAR_FIXED))
996 {
997 emsg(_(error));
998 return TRUE;
999 }
1000 return FALSE;
1001}
1002
1003/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001004 * Give an error if "tv" is not a number and return FAIL.
1005 */
1006 static int
1007check_for_number(typval_T *tv)
1008{
1009 if (tv->v_type != VAR_NUMBER)
1010 {
1011 semsg(_(e_expected_str_but_got_str),
1012 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1013 return FAIL;
1014 }
1015 return OK;
1016}
1017
1018/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001019 * Store "tv" in variable "name".
1020 * This is for s: and g: variables.
1021 */
1022 static void
1023store_var(char_u *name, typval_T *tv)
1024{
1025 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001026 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001027
Bram Moolenaard877a572021-04-01 19:42:48 +02001028 if (tv->v_lock)
1029 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001030 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001031 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001032 restore_funccal();
1033}
1034
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001035/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001036 * Convert "tv" to a string.
1037 * Return FAIL if not allowed.
1038 */
1039 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001040do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001041{
1042 if (tv->v_type != VAR_STRING)
1043 {
1044 char_u *str;
1045
1046 if (is_2string_any)
1047 {
1048 switch (tv->v_type)
1049 {
1050 case VAR_SPECIAL:
1051 case VAR_BOOL:
1052 case VAR_NUMBER:
1053 case VAR_FLOAT:
1054 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001055
1056 case VAR_LIST:
1057 if (tolerant)
1058 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001059 char_u *s, *e, *p;
1060 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001061
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001062 ga_init2(&ga, sizeof(char_u *), 1);
1063
1064 // Convert to NL separated items, then
1065 // escape the items and replace the NL with
1066 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001067 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001068 if (str == NULL)
1069 return FAIL;
1070 s = str;
1071 while ((e = vim_strchr(s, '\n')) != NULL)
1072 {
1073 *e = NUL;
1074 p = vim_strsave_fnameescape(s, FALSE);
1075 if (p != NULL)
1076 {
1077 ga_concat(&ga, p);
1078 ga_concat(&ga, (char_u *)" ");
1079 vim_free(p);
1080 }
1081 s = e + 1;
1082 }
1083 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001084 clear_tv(tv);
1085 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001086 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001087 return OK;
1088 }
1089 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001090 default: to_string_error(tv->v_type);
1091 return FAIL;
1092 }
1093 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001094 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001095 clear_tv(tv);
1096 tv->v_type = VAR_STRING;
1097 tv->vval.v_string = str;
1098 }
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001103 * When the value of "sv" is a null list of dict, allocate it.
1104 */
1105 static void
1106allocate_if_null(typval_T *tv)
1107{
1108 switch (tv->v_type)
1109 {
1110 case VAR_LIST:
1111 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001112 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001113 break;
1114 case VAR_DICT:
1115 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001116 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001117 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001118 case VAR_BLOB:
1119 if (tv->vval.v_blob == NULL)
1120 (void)rettv_blob_alloc(tv);
1121 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001122 default:
1123 break;
1124 }
1125}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001126
Bram Moolenaare7525c52021-01-09 13:20:37 +01001127/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001128 * Return the character "str[index]" where "index" is the character index,
1129 * including composing characters.
1130 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001131 */
1132 char_u *
1133char_from_string(char_u *str, varnumber_T index)
1134{
1135 size_t nbyte = 0;
1136 varnumber_T nchar = index;
1137 size_t slen;
1138
1139 if (str == NULL)
1140 return NULL;
1141 slen = STRLEN(str);
1142
Bram Moolenaarff871402021-03-26 13:34:05 +01001143 // Do the same as for a list: a negative index counts from the end.
1144 // Optimization to check the first byte to be below 0x80 (and no composing
1145 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001146 if (index < 0)
1147 {
1148 int clen = 0;
1149
1150 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001151 {
1152 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1153 ++nbyte;
1154 else if (enc_utf8)
1155 nbyte += utfc_ptr2len(str + nbyte);
1156 else
1157 nbyte += mb_ptr2len(str + nbyte);
1158 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001159 nchar = clen + index;
1160 if (nchar < 0)
1161 // unlike list: index out of range results in empty string
1162 return NULL;
1163 }
1164
1165 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001166 {
1167 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1168 ++nbyte;
1169 else if (enc_utf8)
1170 nbyte += utfc_ptr2len(str + nbyte);
1171 else
1172 nbyte += mb_ptr2len(str + nbyte);
1173 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001174 if (nbyte >= slen)
1175 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001176 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001177}
1178
1179/*
1180 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001181 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001182 * If going over the end return "str_len".
1183 * If "idx" is negative count from the end, -1 is the last character.
1184 * When going over the start return -1.
1185 */
1186 static long
1187char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1188{
1189 varnumber_T nchar = idx;
1190 size_t nbyte = 0;
1191
1192 if (nchar >= 0)
1193 {
1194 while (nchar > 0 && nbyte < str_len)
1195 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001196 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001197 --nchar;
1198 }
1199 }
1200 else
1201 {
1202 nbyte = str_len;
1203 while (nchar < 0 && nbyte > 0)
1204 {
1205 --nbyte;
1206 nbyte -= mb_head_off(str, str + nbyte);
1207 ++nchar;
1208 }
1209 if (nchar < 0)
1210 return -1;
1211 }
1212 return (long)nbyte;
1213}
1214
1215/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001216 * Return the slice "str[first : last]" using character indexes. Composing
1217 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001218 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001219 * Return NULL when the result is empty.
1220 */
1221 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001222string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001223{
1224 long start_byte, end_byte;
1225 size_t slen;
1226
1227 if (str == NULL)
1228 return NULL;
1229 slen = STRLEN(str);
1230 start_byte = char_idx2byte(str, slen, first);
1231 if (start_byte < 0)
1232 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001233 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001234 end_byte = (long)slen;
1235 else
1236 {
1237 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001238 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001239 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001240 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001241 }
1242
1243 if (start_byte >= (long)slen || end_byte <= start_byte)
1244 return NULL;
1245 return vim_strnsave(str + start_byte, end_byte - start_byte);
1246}
1247
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001248 static svar_T *
1249get_script_svar(scriptref_T *sref, ectx_T *ectx)
1250{
1251 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1252 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1253 + ectx->ec_dfunc_idx;
1254 svar_T *sv;
1255
1256 if (sref->sref_seq != si->sn_script_seq)
1257 {
1258 // The script was reloaded after the function was
1259 // compiled, the script_idx may not be valid.
1260 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1261 dfunc->df_ufunc->uf_name_exp);
1262 return NULL;
1263 }
1264 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1265 if (!equal_type(sv->sv_type, sref->sref_type))
1266 {
1267 emsg(_(e_script_variable_type_changed));
1268 return NULL;
1269 }
1270 return sv;
1271}
1272
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001273/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001274 * Function passed to do_cmdline() for splitting a script joined by NL
1275 * characters.
1276 */
1277 static char_u *
1278get_split_sourceline(
1279 int c UNUSED,
1280 void *cookie,
1281 int indent UNUSED,
1282 getline_opt_T options UNUSED)
1283{
1284 source_cookie_T *sp = (source_cookie_T *)cookie;
1285 char_u *p;
1286 char_u *line;
1287
1288 if (*sp->nextline == NUL)
1289 return NULL;
1290 p = vim_strchr(sp->nextline, '\n');
1291 if (p == NULL)
1292 {
1293 line = vim_strsave(sp->nextline);
1294 sp->nextline += STRLEN(sp->nextline);
1295 }
1296 else
1297 {
1298 line = vim_strnsave(sp->nextline, p - sp->nextline);
1299 sp->nextline = p + 1;
1300 }
1301 return line;
1302}
1303
1304/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 * Execute a function by "name".
1306 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001307 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 */
1309 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001310call_eval_func(
1311 char_u *name,
1312 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001313 ectx_T *ectx,
1314 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315{
Bram Moolenaared677f52020-08-12 16:38:10 +02001316 int called_emsg_before = called_emsg;
1317 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318
Bram Moolenaar4c137212021-04-19 16:48:48 +02001319 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001320 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001322 dictitem_T *v;
1323
1324 v = find_var(name, NULL, FALSE);
1325 if (v == NULL)
1326 {
1327 semsg(_(e_unknownfunc), name);
1328 return FAIL;
1329 }
1330 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1331 {
1332 semsg(_(e_unknownfunc), name);
1333 return FAIL;
1334 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001335 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001337 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338}
1339
1340/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001341 * When a function reference is used, fill a partial with the information
1342 * needed, especially when it is used as a closure.
1343 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001344 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001345fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1346{
1347 pt->pt_func = ufunc;
1348 pt->pt_refcount = 1;
1349
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001350 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001351 {
1352 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1353 + ectx->ec_dfunc_idx;
1354
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001355 // The closure may need to find arguments and local variables in the
1356 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001357 pt->pt_outer.out_stack = &ectx->ec_stack;
1358 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001359 if (ectx->ec_outer_ref != NULL)
1360 {
1361 // The current context already has a context, link to that one.
1362 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1363 if (ectx->ec_outer_ref->or_partial != NULL)
1364 {
1365 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1366 ++pt->pt_outer.out_up_partial->pt_refcount;
1367 }
1368 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001369
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001370 // If this function returns and the closure is still being used, we
1371 // need to make a copy of the context (arguments and local variables).
1372 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001373 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1374 {
1375 vim_free(pt);
1376 return FAIL;
1377 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001378 // Extra variable keeps the count of closures created in the current
1379 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001380 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1381 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1382
1383 ((partial_T **)ectx->ec_funcrefs.ga_data)
1384 [ectx->ec_funcrefs.ga_len] = pt;
1385 ++pt->pt_refcount;
1386 ++ectx->ec_funcrefs.ga_len;
1387 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001388 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001389 return OK;
1390}
1391
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001392// used for v_instr of typval of VAR_INSTR
1393struct instr_S {
1394 ectx_T *instr_ectx;
1395 isn_T *instr_instr;
1396};
1397
Bram Moolenaar4c137212021-04-19 16:48:48 +02001398// used for substitute_instr
1399typedef struct subs_expr_S {
1400 ectx_T *subs_ectx;
1401 isn_T *subs_instr;
1402 int subs_status;
1403} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404
1405// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001406#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407
1408// Get pointer to item at the bottom of the stack, -1 is the bottom.
1409#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001410#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 +01001411
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001412// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001413#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 +01001414
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001415// Set when calling do_debug().
1416static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001417static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001418
1419/*
1420 * When debugging lookup "name" and return the typeval.
1421 * When not found return NULL.
1422 */
1423 typval_T *
1424lookup_debug_var(char_u *name)
1425{
1426 int idx;
1427 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001428 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001429 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001430 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001431
1432 if (ectx == NULL)
1433 return NULL;
1434 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1435
1436 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001437 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001438 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001439 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001440 return STACK_TV_VAR(idx);
1441 }
1442
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001443 // Go through argument names.
1444 ufunc = dfunc->df_ufunc;
1445 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1446 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1447 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1448 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1449 - varargs_off + idx);
1450 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1451 return STACK_TV(ectx->ec_frame_idx - 1);
1452
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001453 return NULL;
1454}
1455
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001456 static void
1457handle_debug(isn_T *iptr, ectx_T *ectx)
1458{
1459 char_u *line;
1460 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1461 + ectx->ec_dfunc_idx)->df_ufunc;
1462 isn_T *ni;
1463 int end_lnum = iptr->isn_lnum;
1464 garray_T ga;
1465 int lnum;
1466
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001467 if (ex_nesting_level > debug_break_level)
1468 {
1469 linenr_T breakpoint;
1470
1471 if (!ufunc->uf_has_breakpoint)
1472 return;
1473
1474 // check for the next breakpoint if needed
1475 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
1476 iptr->isn_lnum - 1);
1477 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1478 return;
1479 }
1480
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001481 SOURCING_LNUM = iptr->isn_lnum;
1482 debug_context = ectx;
1483 debug_var_count = iptr->isn_arg.number;
1484
1485 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1486 if (ni->isn_type == ISN_DEBUG
1487 || ni->isn_type == ISN_RETURN
1488 || ni->isn_type == ISN_RETURN_VOID)
1489 {
1490 end_lnum = ni->isn_lnum;
1491 break;
1492 }
1493
1494 if (end_lnum > iptr->isn_lnum)
1495 {
1496 ga_init2(&ga, sizeof(char_u *), 10);
1497 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001498 {
1499 char_u *p = skipwhite(
1500 ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1]);
1501
1502 if (*p == '#')
1503 break;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001504 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001505 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1506 if (STRNCMP(p, "def ", 4) == 0)
1507 break;
1508 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001509 line = ga_concat_strings(&ga, " ");
1510 vim_free(ga.ga_data);
1511 }
1512 else
1513 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001514
Dominique Pelle6e969552021-06-17 13:53:41 +02001515 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001516 debug_context = NULL;
1517
1518 if (end_lnum > iptr->isn_lnum)
1519 vim_free(line);
1520}
1521
Bram Moolenaar4c137212021-04-19 16:48:48 +02001522/*
1523 * Execute instructions in execution context "ectx".
1524 * Return OK or FAIL;
1525 */
1526 static int
1527exec_instructions(ectx_T *ectx)
1528{
1529 int breakcheck_count = 0;
1530 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001531 int ret = FAIL;
1532 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001533
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001534 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001535 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001536
Bram Moolenaarff652882021-05-16 15:24:49 +02001537 // Only catch exceptions in this instruction list.
1538 ectx->ec_trylevel_at_start = trylevel;
1539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 for (;;)
1541 {
1542 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001543
Bram Moolenaar270d0382020-05-15 21:42:53 +02001544 if (++breakcheck_count >= 100)
1545 {
1546 line_breakcheck();
1547 breakcheck_count = 0;
1548 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001549 if (got_int)
1550 {
1551 // Turn CTRL-C into an exception.
1552 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001553 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001554 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001555 did_throw = TRUE;
1556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557
Bram Moolenaara26b9702020-04-18 19:53:28 +02001558 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1559 {
1560 // Turn an error message into an exception.
1561 did_emsg = FALSE;
1562 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001563 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001564 did_throw = TRUE;
1565 *msg_list = NULL;
1566 }
1567
Bram Moolenaar4c137212021-04-19 16:48:48 +02001568 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001570 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001571 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572
1573 // An exception jumps to the first catch, finally, or returns from
1574 // the current function.
1575 if (trystack->ga_len > 0)
1576 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001577 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 {
1579 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001580 ectx->ec_in_catch = TRUE;
1581 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 }
1583 else
1584 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001585 // Not inside try or need to return from current functions.
1586 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001587 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001588 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001589 tv = STACK_TV_BOT(0);
1590 tv->v_type = VAR_NUMBER;
1591 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001592 ++ectx->ec_stack.ga_len;
1593 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001595 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001596 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001597 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001598 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 goto done;
1600 }
1601
Bram Moolenaar4c137212021-04-19 16:48:48 +02001602 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001603 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 }
1605 continue;
1606 }
1607
Bram Moolenaar4c137212021-04-19 16:48:48 +02001608 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 switch (iptr->isn_type)
1610 {
1611 // execute Ex command line
1612 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001613 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001614 source_cookie_T cookie;
1615
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001616 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001617 // Pass getsourceline to get an error for a missing ":end"
1618 // command.
1619 CLEAR_FIELD(cookie);
1620 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1621 if (do_cmdline(iptr->isn_arg.string,
1622 getsourceline, &cookie,
1623 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1624 == FAIL
1625 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001626 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 break;
1629
Bram Moolenaar20677332021-06-06 17:02:53 +02001630 // execute Ex command line split at NL characters.
1631 case ISN_EXEC_SPLIT:
1632 {
1633 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001634 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001635
1636 SOURCING_LNUM = iptr->isn_lnum;
1637 CLEAR_FIELD(cookie);
1638 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1639 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001640 line = get_split_sourceline(0, &cookie, 0, 0);
1641 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001642 get_split_sourceline, &cookie,
1643 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1644 == FAIL
1645 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001646 {
1647 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001648 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001649 }
1650 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001651 }
1652 break;
1653
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001654 // Evaluate an expression with legacy syntax, push it onto the
1655 // stack.
1656 case ISN_LEGACY_EVAL:
1657 {
1658 char_u *arg = iptr->isn_arg.string;
1659 int res;
1660 int save_flags = cmdmod.cmod_flags;
1661
1662 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001663 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001664 tv = STACK_TV_BOT(0);
1665 init_tv(tv);
1666 cmdmod.cmod_flags |= CMOD_LEGACY;
1667 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1668 cmdmod.cmod_flags = save_flags;
1669 if (res == FAIL)
1670 goto on_error;
1671 ++ectx->ec_stack.ga_len;
1672 }
1673 break;
1674
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001675 // push typeval VAR_INSTR with instructions to be executed
1676 case ISN_INSTR:
1677 {
1678 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001679 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001680 tv = STACK_TV_BOT(0);
1681 tv->vval.v_instr = ALLOC_ONE(instr_T);
1682 if (tv->vval.v_instr == NULL)
1683 goto on_error;
1684 ++ectx->ec_stack.ga_len;
1685
1686 tv->v_type = VAR_INSTR;
1687 tv->vval.v_instr->instr_ectx = ectx;
1688 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1689 }
1690 break;
1691
Bram Moolenaar4c137212021-04-19 16:48:48 +02001692 // execute :substitute with an expression
1693 case ISN_SUBSTITUTE:
1694 {
1695 subs_T *subs = &iptr->isn_arg.subs;
1696 source_cookie_T cookie;
1697 struct subs_expr_S *save_instr = substitute_instr;
1698 struct subs_expr_S subs_instr;
1699 int res;
1700
1701 subs_instr.subs_ectx = ectx;
1702 subs_instr.subs_instr = subs->subs_instr;
1703 subs_instr.subs_status = OK;
1704 substitute_instr = &subs_instr;
1705
1706 SOURCING_LNUM = iptr->isn_lnum;
1707 // This is very much like ISN_EXEC
1708 CLEAR_FIELD(cookie);
1709 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1710 res = do_cmdline(subs->subs_cmd,
1711 getsourceline, &cookie,
1712 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1713 substitute_instr = save_instr;
1714
1715 if (res == FAIL || did_emsg
1716 || subs_instr.subs_status == FAIL)
1717 goto on_error;
1718 }
1719 break;
1720
1721 case ISN_FINISH:
1722 goto done;
1723
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001724 case ISN_REDIRSTART:
1725 // create a dummy entry for var_redir_str()
1726 if (alloc_redir_lval() == FAIL)
1727 goto on_error;
1728
1729 // The output is stored in growarray "redir_ga" until
1730 // redirection ends.
1731 init_redir_ga();
1732 redir_vname = 1;
1733 break;
1734
1735 case ISN_REDIREND:
1736 {
1737 char_u *res = get_clear_redir_ga();
1738
1739 // End redirection, put redirected text on the stack.
1740 clear_redir_lval();
1741 redir_vname = 0;
1742
1743 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1744 {
1745 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001746 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001747 }
1748 tv = STACK_TV_BOT(0);
1749 tv->v_type = VAR_STRING;
1750 tv->vval.v_string = res;
1751 ++ectx->ec_stack.ga_len;
1752 }
1753 break;
1754
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001755 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001756#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001757 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1758 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001759#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001760 break;
1761
1762 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001763#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001764 {
1765 exarg_T ea;
1766 int res;
1767
1768 CLEAR_FIELD(ea);
1769 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1770 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1771 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1772 --ectx->ec_stack.ga_len;
1773 tv = STACK_TV_BOT(0);
1774 res = cexpr_core(&ea, tv);
1775 clear_tv(tv);
1776 if (res == FAIL)
1777 goto on_error;
1778 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001779#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001780 break;
1781
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001782 // execute Ex command from pieces on the stack
1783 case ISN_EXECCONCAT:
1784 {
1785 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001786 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001787 int pass;
1788 int i;
1789 char_u *cmd = NULL;
1790 char_u *str;
1791
1792 for (pass = 1; pass <= 2; ++pass)
1793 {
1794 for (i = 0; i < count; ++i)
1795 {
1796 tv = STACK_TV_BOT(i - count);
1797 str = tv->vval.v_string;
1798 if (str != NULL && *str != NUL)
1799 {
1800 if (pass == 2)
1801 STRCPY(cmd + len, str);
1802 len += STRLEN(str);
1803 }
1804 if (pass == 2)
1805 clear_tv(tv);
1806 }
1807 if (pass == 1)
1808 {
1809 cmd = alloc(len + 1);
1810 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001811 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001812 len = 0;
1813 }
1814 }
1815
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001816 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001817 do_cmdline_cmd(cmd);
1818 vim_free(cmd);
1819 }
1820 break;
1821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 // execute :echo {string} ...
1823 case ISN_ECHO:
1824 {
1825 int count = iptr->isn_arg.echo.echo_count;
1826 int atstart = TRUE;
1827 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001828 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
1830 for (idx = 0; idx < count; ++idx)
1831 {
1832 tv = STACK_TV_BOT(idx - count);
1833 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1834 &atstart, &needclr);
1835 clear_tv(tv);
1836 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001837 if (needclr)
1838 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001839 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 }
1841 break;
1842
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001843 // :execute {string} ...
1844 // :echomsg {string} ...
1845 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001846 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001847 case ISN_ECHOMSG:
1848 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001849 {
1850 int count = iptr->isn_arg.number;
1851 garray_T ga;
1852 char_u buf[NUMBUFLEN];
1853 char_u *p;
1854 int len;
1855 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001856 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001857
1858 ga_init2(&ga, 1, 80);
1859 for (idx = 0; idx < count; ++idx)
1860 {
1861 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001862 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001863 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001864 if (tv->v_type == VAR_CHANNEL
1865 || tv->v_type == VAR_JOB)
1866 {
1867 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001868 semsg(_(e_using_invalid_value_as_string_str),
1869 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001870 break;
1871 }
1872 else
1873 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001874 }
1875 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001876 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001877
1878 len = (int)STRLEN(p);
1879 if (ga_grow(&ga, len + 2) == FAIL)
1880 failed = TRUE;
1881 else
1882 {
1883 if (ga.ga_len > 0)
1884 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1885 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1886 ga.ga_len += len;
1887 }
1888 clear_tv(tv);
1889 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001890 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001891 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001892 {
1893 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001894 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001895 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001896
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001897 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001898 {
1899 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001900 {
1901 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001902 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001903 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001904 {
1905 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001906 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001907 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001908 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001909 else
1910 {
1911 msg_sb_eol();
1912 if (iptr->isn_type == ISN_ECHOMSG)
1913 {
1914 msg_attr(ga.ga_data, echo_attr);
1915 out_flush();
1916 }
1917 else
1918 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001919 SOURCING_LNUM = iptr->isn_lnum;
1920 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001921 }
1922 }
1923 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001924 ga_clear(&ga);
1925 }
1926 break;
1927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 // load local variable or argument
1929 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001930 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001931 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001933 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 break;
1935
1936 // load v: variable
1937 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001938 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001939 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001941 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 break;
1943
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001944 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 case ISN_LOADSCRIPT:
1946 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001947 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 svar_T *sv;
1949
Bram Moolenaar4c137212021-04-19 16:48:48 +02001950 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001951 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001952 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001953 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001954 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001955 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001957 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 }
1959 break;
1960
1961 // load s: variable in old script
1962 case ISN_LOADS:
1963 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001964 hashtab_T *ht = &SCRIPT_VARS(
1965 iptr->isn_arg.loadstore.ls_sid);
1966 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 if (di == NULL)
1970 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001971 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001972 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001973 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 }
1975 else
1976 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001977 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001978 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001980 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 }
1982 }
1983 break;
1984
Bram Moolenaard3aac292020-04-19 14:32:17 +02001985 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001987 case ISN_LOADB:
1988 case ISN_LOADW:
1989 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001991 dictitem_T *di = NULL;
1992 hashtab_T *ht = NULL;
1993 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001994
Bram Moolenaard3aac292020-04-19 14:32:17 +02001995 switch (iptr->isn_type)
1996 {
1997 case ISN_LOADG:
1998 ht = get_globvar_ht();
1999 namespace = 'g';
2000 break;
2001 case ISN_LOADB:
2002 ht = &curbuf->b_vars->dv_hashtab;
2003 namespace = 'b';
2004 break;
2005 case ISN_LOADW:
2006 ht = &curwin->w_vars->dv_hashtab;
2007 namespace = 'w';
2008 break;
2009 case ISN_LOADT:
2010 ht = &curtab->tp_vars->dv_hashtab;
2011 namespace = 't';
2012 break;
2013 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002014 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002015 }
2016 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 if (di == NULL)
2019 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002020 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002021 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002022 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002023 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 }
2025 else
2026 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002027 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002028 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002030 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 }
2032 }
2033 break;
2034
Bram Moolenaar03290b82020-12-19 16:30:44 +01002035 // load autoload variable
2036 case ISN_LOADAUTO:
2037 {
2038 char_u *name = iptr->isn_arg.string;
2039
Bram Moolenaar4c137212021-04-19 16:48:48 +02002040 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002041 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002042 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002043 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002044 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002045 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002046 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002047 }
2048 break;
2049
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002050 // load g:/b:/w:/t: namespace
2051 case ISN_LOADGDICT:
2052 case ISN_LOADBDICT:
2053 case ISN_LOADWDICT:
2054 case ISN_LOADTDICT:
2055 {
2056 dict_T *d = NULL;
2057
2058 switch (iptr->isn_type)
2059 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002060 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2061 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2062 case ISN_LOADWDICT: d = curwin->w_vars; break;
2063 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002064 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002065 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002066 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002067 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002068 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002069 tv = STACK_TV_BOT(0);
2070 tv->v_type = VAR_DICT;
2071 tv->v_lock = 0;
2072 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002073 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002074 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002075 }
2076 break;
2077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 // load &option
2079 case ISN_LOADOPT:
2080 {
2081 typval_T optval;
2082 char_u *name = iptr->isn_arg.string;
2083
Bram Moolenaara8c17702020-04-01 21:17:24 +02002084 // This is not expected to fail, name is checked during
2085 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002086 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002087 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002088 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002089 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002091 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 }
2093 break;
2094
2095 // load $ENV
2096 case ISN_LOADENV:
2097 {
2098 typval_T optval;
2099 char_u *name = iptr->isn_arg.string;
2100
Bram Moolenaar4c137212021-04-19 16:48:48 +02002101 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002102 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002103 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002104 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002106 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 }
2108 break;
2109
2110 // load @register
2111 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002112 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002113 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 tv = STACK_TV_BOT(0);
2115 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002116 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002117 // This may result in NULL, which should be equivalent to an
2118 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 tv->vval.v_string = get_reg_contents(
2120 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002121 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 break;
2123
2124 // store local variable
2125 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002126 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 tv = STACK_TV_VAR(iptr->isn_arg.number);
2128 clear_tv(tv);
2129 *tv = *STACK_TV_BOT(0);
2130 break;
2131
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002132 // store s: variable in old script
2133 case ISN_STORES:
2134 {
2135 hashtab_T *ht = &SCRIPT_VARS(
2136 iptr->isn_arg.loadstore.ls_sid);
2137 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002138 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002139
Bram Moolenaar4c137212021-04-19 16:48:48 +02002140 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002141 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002142 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002143 else
2144 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002145 SOURCING_LNUM = iptr->isn_lnum;
2146 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002147 {
2148 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002149 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002150 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002151 clear_tv(&di->di_tv);
2152 di->di_tv = *STACK_TV_BOT(0);
2153 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002154 }
2155 break;
2156
2157 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 case ISN_STORESCRIPT:
2159 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002160 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2161 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162
Bram Moolenaar4c137212021-04-19 16:48:48 +02002163 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002164 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002165 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002166 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002167
2168 // "const" and "final" are checked at compile time, locking
2169 // the value needs to be checked here.
2170 SOURCING_LNUM = iptr->isn_lnum;
2171 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002172 {
2173 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002174 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002175 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 clear_tv(sv->sv_tv);
2178 *sv->sv_tv = *STACK_TV_BOT(0);
2179 }
2180 break;
2181
2182 // store option
2183 case ISN_STOREOPT:
2184 {
2185 long n = 0;
2186 char_u *s = NULL;
2187 char *msg;
2188
Bram Moolenaar4c137212021-04-19 16:48:48 +02002189 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 tv = STACK_TV_BOT(0);
2191 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002192 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002194 if (s == NULL)
2195 s = (char_u *)"";
2196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002198 // must be VAR_NUMBER, CHECKTYPE makes sure
2199 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2201 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002202 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 if (msg != NULL)
2204 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002207 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 }
2210 break;
2211
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002212 // store $ENV
2213 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002214 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002215 tv = STACK_TV_BOT(0);
2216 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2217 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002218 break;
2219
2220 // store @r
2221 case ISN_STOREREG:
2222 {
2223 int reg = iptr->isn_arg.number;
2224
Bram Moolenaar4c137212021-04-19 16:48:48 +02002225 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002226 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002227 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002228 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002229 }
2230 break;
2231
2232 // store v: variable
2233 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002234 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002235 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2236 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002237 // should not happen, type is checked when compiling
2238 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002239 break;
2240
Bram Moolenaard3aac292020-04-19 14:32:17 +02002241 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002243 case ISN_STOREB:
2244 case ISN_STOREW:
2245 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002247 dictitem_T *di;
2248 hashtab_T *ht;
2249 char_u *name = iptr->isn_arg.string + 2;
2250
Bram Moolenaard3aac292020-04-19 14:32:17 +02002251 switch (iptr->isn_type)
2252 {
2253 case ISN_STOREG:
2254 ht = get_globvar_ht();
2255 break;
2256 case ISN_STOREB:
2257 ht = &curbuf->b_vars->dv_hashtab;
2258 break;
2259 case ISN_STOREW:
2260 ht = &curwin->w_vars->dv_hashtab;
2261 break;
2262 case ISN_STORET:
2263 ht = &curtab->tp_vars->dv_hashtab;
2264 break;
2265 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002266 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268
Bram Moolenaar4c137212021-04-19 16:48:48 +02002269 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002270 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002272 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 else
2274 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002275 SOURCING_LNUM = iptr->isn_lnum;
2276 if (var_check_permission(di, name) == FAIL)
2277 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 clear_tv(&di->di_tv);
2279 di->di_tv = *STACK_TV_BOT(0);
2280 }
2281 }
2282 break;
2283
Bram Moolenaar03290b82020-12-19 16:30:44 +01002284 // store an autoload variable
2285 case ISN_STOREAUTO:
2286 SOURCING_LNUM = iptr->isn_lnum;
2287 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2288 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002289 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002290 break;
2291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 // store number in local variable
2293 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002294 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 clear_tv(tv);
2296 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002297 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 break;
2299
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002300 // store value in list or dict variable
2301 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002302 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002303 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002304 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002305 typval_T *tv_dest = STACK_TV_BOT(-1);
2306 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002307
Bram Moolenaar752fc692021-01-04 21:57:11 +01002308 // Stack contains:
2309 // -3 value to be stored
2310 // -2 index
2311 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002312 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002313 SOURCING_LNUM = iptr->isn_lnum;
2314 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002315 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002316 dest_type = tv_dest->v_type;
2317 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002318 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002319 else if (dest_type == VAR_LIST
2320 && tv_idx->v_type != VAR_NUMBER)
2321 {
2322 emsg(_(e_number_exp));
2323 status = FAIL;
2324 }
2325 }
2326 else if (dest_type != tv_dest->v_type)
2327 {
2328 // just in case, should be OK
2329 semsg(_(e_expected_str_but_got_str),
2330 vartype_name(dest_type),
2331 vartype_name(tv_dest->v_type));
2332 status = FAIL;
2333 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002334
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002335 if (status == OK && dest_type == VAR_LIST)
2336 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002337 long lidx = (long)tv_idx->vval.v_number;
2338 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002339
2340 if (list == NULL)
2341 {
2342 emsg(_(e_list_not_set));
2343 goto on_error;
2344 }
2345 if (lidx < 0 && list->lv_len + lidx >= 0)
2346 // negative index is relative to the end
2347 lidx = list->lv_len + lidx;
2348 if (lidx < 0 || lidx > list->lv_len)
2349 {
2350 semsg(_(e_listidx), lidx);
2351 goto on_error;
2352 }
2353 if (lidx < list->lv_len)
2354 {
2355 listitem_T *li = list_find(list, lidx);
2356
2357 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002358 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002359 goto on_error;
2360 // overwrite existing list item
2361 clear_tv(&li->li_tv);
2362 li->li_tv = *tv;
2363 }
2364 else
2365 {
2366 if (error_if_locked(list->lv_lock,
2367 e_cannot_change_list))
2368 goto on_error;
2369 // append to list, only fails when out of memory
2370 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002371 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002372 clear_tv(tv);
2373 }
2374 }
2375 else if (status == OK && dest_type == VAR_DICT)
2376 {
2377 char_u *key = tv_idx->vval.v_string;
2378 dict_T *dict = tv_dest->vval.v_dict;
2379 dictitem_T *di;
2380
2381 SOURCING_LNUM = iptr->isn_lnum;
2382 if (dict == NULL)
2383 {
2384 emsg(_(e_dictionary_not_set));
2385 goto on_error;
2386 }
2387 if (key == NULL)
2388 key = (char_u *)"";
2389 di = dict_find(dict, key, -1);
2390 if (di != NULL)
2391 {
2392 if (error_if_locked(di->di_tv.v_lock,
2393 e_cannot_change_dict_item))
2394 goto on_error;
2395 // overwrite existing value
2396 clear_tv(&di->di_tv);
2397 di->di_tv = *tv;
2398 }
2399 else
2400 {
2401 if (error_if_locked(dict->dv_lock,
2402 e_cannot_change_dict))
2403 goto on_error;
2404 // add to dict, only fails when out of memory
2405 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002406 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002407 clear_tv(tv);
2408 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002409 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002410 else if (status == OK && dest_type == VAR_BLOB)
2411 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002412 long lidx = (long)tv_idx->vval.v_number;
2413 blob_T *blob = tv_dest->vval.v_blob;
2414 varnumber_T nr;
2415 int error = FALSE;
2416 int len;
2417
2418 if (blob == NULL)
2419 {
2420 emsg(_(e_blob_not_set));
2421 goto on_error;
2422 }
2423 len = blob_len(blob);
2424 if (lidx < 0 && len + lidx >= 0)
2425 // negative index is relative to the end
2426 lidx = len + lidx;
2427
2428 // Can add one byte at the end.
2429 if (lidx < 0 || lidx > len)
2430 {
2431 semsg(_(e_blobidx), lidx);
2432 goto on_error;
2433 }
2434 if (value_check_lock(blob->bv_lock,
2435 (char_u *)"blob", FALSE))
2436 goto on_error;
2437 nr = tv_get_number_chk(tv, &error);
2438 if (error)
2439 goto on_error;
2440 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002441 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002442 else
2443 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002444 status = FAIL;
2445 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002446 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002447
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002448 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002449 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002450 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002451 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002452 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002453 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002454 goto on_error;
2455 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002456 }
2457 break;
2458
Bram Moolenaar68452172021-04-12 21:21:02 +02002459 // store value in blob range
2460 case ISN_STORERANGE:
2461 {
2462 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2463 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2464 typval_T *tv_dest = STACK_TV_BOT(-1);
2465 int status = OK;
2466
2467 // Stack contains:
2468 // -4 value to be stored
2469 // -3 first index or "none"
2470 // -2 second index or "none"
2471 // -1 destination blob
2472 tv = STACK_TV_BOT(-4);
2473 if (tv_dest->v_type != VAR_BLOB)
2474 {
2475 status = FAIL;
2476 emsg(_(e_blob_required));
2477 }
2478 else
2479 {
2480 varnumber_T n1;
2481 varnumber_T n2;
2482 int error = FALSE;
2483
2484 n1 = tv_get_number_chk(tv_idx1, &error);
2485 if (error)
2486 status = FAIL;
2487 else
2488 {
2489 if (tv_idx2->v_type == VAR_SPECIAL
2490 && tv_idx2->vval.v_number == VVAL_NONE)
2491 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2492 else
2493 n2 = tv_get_number_chk(tv_idx2, &error);
2494 if (error)
2495 status = FAIL;
2496 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002497 {
2498 long bloblen = blob_len(tv_dest->vval.v_blob);
2499
2500 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002501 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002502 || check_blob_range(bloblen,
2503 n1, n2, FALSE) == FAIL)
2504 status = FAIL;
2505 else
2506 status = blob_set_range(
2507 tv_dest->vval.v_blob, n1, n2, tv);
2508 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002509 }
2510 }
2511
2512 clear_tv(tv_idx1);
2513 clear_tv(tv_idx2);
2514 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002515 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002516 clear_tv(tv);
2517
2518 if (status == FAIL)
2519 goto on_error;
2520 }
2521 break;
2522
Bram Moolenaar0186e582021-01-10 18:33:11 +01002523 // load or store variable or argument from outer scope
2524 case ISN_LOADOUTER:
2525 case ISN_STOREOUTER:
2526 {
2527 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002528 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2529 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002530
2531 while (depth > 1 && outer != NULL)
2532 {
2533 outer = outer->out_up;
2534 --depth;
2535 }
2536 if (outer == NULL)
2537 {
2538 SOURCING_LNUM = iptr->isn_lnum;
2539 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002540 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002541 }
2542 tv = ((typval_T *)outer->out_stack->ga_data)
2543 + outer->out_frame_idx + STACK_FRAME_SIZE
2544 + iptr->isn_arg.outer.outer_idx;
2545 if (iptr->isn_type == ISN_LOADOUTER)
2546 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002547 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002548 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002549 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002550 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002551 }
2552 else
2553 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002554 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002555 clear_tv(tv);
2556 *tv = *STACK_TV_BOT(0);
2557 }
2558 }
2559 break;
2560
Bram Moolenaar752fc692021-01-04 21:57:11 +01002561 // unlet item in list or dict variable
2562 case ISN_UNLETINDEX:
2563 {
2564 typval_T *tv_idx = STACK_TV_BOT(-2);
2565 typval_T *tv_dest = STACK_TV_BOT(-1);
2566 int status = OK;
2567
2568 // Stack contains:
2569 // -2 index
2570 // -1 dict or list
2571 if (tv_dest->v_type == VAR_DICT)
2572 {
2573 // unlet a dict item, index must be a string
2574 if (tv_idx->v_type != VAR_STRING)
2575 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002576 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002577 semsg(_(e_expected_str_but_got_str),
2578 vartype_name(VAR_STRING),
2579 vartype_name(tv_idx->v_type));
2580 status = FAIL;
2581 }
2582 else
2583 {
2584 dict_T *d = tv_dest->vval.v_dict;
2585 char_u *key = tv_idx->vval.v_string;
2586 dictitem_T *di = NULL;
2587
2588 if (key == NULL)
2589 key = (char_u *)"";
2590 if (d != NULL)
2591 di = dict_find(d, key, (int)STRLEN(key));
2592 if (di == NULL)
2593 {
2594 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002595 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002596 semsg(_(e_dictkey), key);
2597 status = FAIL;
2598 }
2599 else
2600 {
2601 // TODO: check for dict or item locked
2602 dictitem_remove(d, di);
2603 }
2604 }
2605 }
2606 else if (tv_dest->v_type == VAR_LIST)
2607 {
2608 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002609 SOURCING_LNUM = iptr->isn_lnum;
2610 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002611 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002612 status = FAIL;
2613 }
2614 else
2615 {
2616 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002617 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002618 listitem_T *li = NULL;
2619
2620 li = list_find(l, n);
2621 if (li == NULL)
2622 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002623 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002624 semsg(_(e_listidx), n);
2625 status = FAIL;
2626 }
2627 else
2628 // TODO: check for list or item locked
2629 listitem_remove(l, li);
2630 }
2631 }
2632 else
2633 {
2634 status = FAIL;
2635 semsg(_(e_cannot_index_str),
2636 vartype_name(tv_dest->v_type));
2637 }
2638
2639 clear_tv(tv_idx);
2640 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002641 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002642 if (status == FAIL)
2643 goto on_error;
2644 }
2645 break;
2646
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002647 // unlet range of items in list variable
2648 case ISN_UNLETRANGE:
2649 {
2650 // Stack contains:
2651 // -3 index1
2652 // -2 index2
2653 // -1 dict or list
2654 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2655 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2656 typval_T *tv_dest = STACK_TV_BOT(-1);
2657 int status = OK;
2658
2659 if (tv_dest->v_type == VAR_LIST)
2660 {
2661 // indexes must be a number
2662 SOURCING_LNUM = iptr->isn_lnum;
2663 if (check_for_number(tv_idx1) == FAIL
2664 || check_for_number(tv_idx2) == FAIL)
2665 {
2666 status = FAIL;
2667 }
2668 else
2669 {
2670 list_T *l = tv_dest->vval.v_list;
2671 long n1 = (long)tv_idx1->vval.v_number;
2672 long n2 = (long)tv_idx2->vval.v_number;
2673 listitem_T *li;
2674
2675 li = list_find_index(l, &n1);
2676 if (li == NULL
2677 || list_unlet_range(l, li, NULL, n1,
2678 TRUE, n2) == FAIL)
2679 status = FAIL;
2680 }
2681 }
2682 else
2683 {
2684 status = FAIL;
2685 SOURCING_LNUM = iptr->isn_lnum;
2686 semsg(_(e_cannot_index_str),
2687 vartype_name(tv_dest->v_type));
2688 }
2689
2690 clear_tv(tv_idx1);
2691 clear_tv(tv_idx2);
2692 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002693 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002694 if (status == FAIL)
2695 goto on_error;
2696 }
2697 break;
2698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 // push constant
2700 case ISN_PUSHNR:
2701 case ISN_PUSHBOOL:
2702 case ISN_PUSHSPEC:
2703 case ISN_PUSHF:
2704 case ISN_PUSHS:
2705 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002706 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002707 case ISN_PUSHCHANNEL:
2708 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002709 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002710 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002712 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002713 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 switch (iptr->isn_type)
2715 {
2716 case ISN_PUSHNR:
2717 tv->v_type = VAR_NUMBER;
2718 tv->vval.v_number = iptr->isn_arg.number;
2719 break;
2720 case ISN_PUSHBOOL:
2721 tv->v_type = VAR_BOOL;
2722 tv->vval.v_number = iptr->isn_arg.number;
2723 break;
2724 case ISN_PUSHSPEC:
2725 tv->v_type = VAR_SPECIAL;
2726 tv->vval.v_number = iptr->isn_arg.number;
2727 break;
2728#ifdef FEAT_FLOAT
2729 case ISN_PUSHF:
2730 tv->v_type = VAR_FLOAT;
2731 tv->vval.v_float = iptr->isn_arg.fnumber;
2732 break;
2733#endif
2734 case ISN_PUSHBLOB:
2735 blob_copy(iptr->isn_arg.blob, tv);
2736 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002737 case ISN_PUSHFUNC:
2738 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002739 if (iptr->isn_arg.string == NULL)
2740 tv->vval.v_string = NULL;
2741 else
2742 tv->vval.v_string =
2743 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002744 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002745 case ISN_PUSHCHANNEL:
2746#ifdef FEAT_JOB_CHANNEL
2747 tv->v_type = VAR_CHANNEL;
2748 tv->vval.v_channel = iptr->isn_arg.channel;
2749 if (tv->vval.v_channel != NULL)
2750 ++tv->vval.v_channel->ch_refcount;
2751#endif
2752 break;
2753 case ISN_PUSHJOB:
2754#ifdef FEAT_JOB_CHANNEL
2755 tv->v_type = VAR_JOB;
2756 tv->vval.v_job = iptr->isn_arg.job;
2757 if (tv->vval.v_job != NULL)
2758 ++tv->vval.v_job->jv_refcount;
2759#endif
2760 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 default:
2762 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002763 tv->vval.v_string = vim_strsave(
2764 iptr->isn_arg.string == NULL
2765 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 }
2767 break;
2768
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002769 case ISN_UNLET:
2770 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2771 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002772 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002773 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002774 case ISN_UNLETENV:
2775 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2776 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002777
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002778 case ISN_LOCKCONST:
2779 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2780 break;
2781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 // create a list from items on the stack; uses a single allocation
2783 // for the list header and the items
2784 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002785 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002786 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 break;
2788
2789 // create a dict from items on the stack
2790 case ISN_NEWDICT:
2791 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002792 int count = iptr->isn_arg.number;
2793 dict_T *dict = dict_alloc();
2794 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002795 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002796 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797
2798 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002799 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 for (idx = 0; idx < count; ++idx)
2801 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002802 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002804 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002805 key = tv->vval.v_string == NULL
2806 ? (char_u *)"" : tv->vval.v_string;
2807 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002808 if (item != NULL)
2809 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002810 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002811 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002812 dict_unref(dict);
2813 goto on_error;
2814 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002815 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 clear_tv(tv);
2817 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002818 {
2819 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002820 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2823 item->di_tv.v_lock = 0;
2824 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002825 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002826 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002827 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002828 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002829 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 }
2831
2832 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002833 ectx->ec_stack.ga_len -= 2 * count - 1;
2834 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002835 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002837 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 tv = STACK_TV_BOT(-1);
2839 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002840 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 tv->vval.v_dict = dict;
2842 ++dict->dv_refcount;
2843 }
2844 break;
2845
2846 // call a :def function
2847 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002848 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002849 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2850 NULL,
2851 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002852 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002853 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 break;
2855
2856 // call a builtin function
2857 case ISN_BCALL:
2858 SOURCING_LNUM = iptr->isn_lnum;
2859 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2860 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002861 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002862 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 break;
2864
2865 // call a funcref or partial
2866 case ISN_PCALL:
2867 {
2868 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2869 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002870 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871
2872 SOURCING_LNUM = iptr->isn_lnum;
2873 if (pfunc->cpf_top)
2874 {
2875 // funcref is above the arguments
2876 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2877 }
2878 else
2879 {
2880 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002881 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002882 partial_tv = *STACK_TV_BOT(0);
2883 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002885 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002886 if (tv == &partial_tv)
2887 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002889 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 }
2891 break;
2892
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002893 case ISN_PCALL_END:
2894 // PCALL finished, arguments have been consumed and replaced by
2895 // the return value. Now clear the funcref from the stack,
2896 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002897 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002898 clear_tv(STACK_TV_BOT(-1));
2899 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2900 break;
2901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 // call a user defined function or funcref/partial
2903 case ISN_UCALL:
2904 {
2905 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2906
2907 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002908 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002909 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002910 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 }
2912 break;
2913
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002914 // return from a :def function call without a value
2915 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002916 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002917 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002918 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002919 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002920 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002921 tv->vval.v_number = 0;
2922 tv->v_lock = 0;
2923 // FALLTHROUGH
2924
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002925 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 case ISN_RETURN:
2927 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002928 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002929 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002930
2931 if (trystack->ga_len > 0)
2932 trycmd = ((trycmd_T *)trystack->ga_data)
2933 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002934 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002935 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002937 // jump to ":finally" or ":endtry"
2938 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002939 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002940 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002941 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 trycmd->tcd_return = TRUE;
2943 }
2944 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002945 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 }
2947 break;
2948
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002949 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 case ISN_FUNCREF:
2951 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002952 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2953 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2954 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002957 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002958 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002959 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002960 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002961 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002962 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002963 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002964 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002965 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002967 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 tv->vval.v_partial = pt;
2969 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002970 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 }
2972 break;
2973
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002974 // Create a global function from a lambda.
2975 case ISN_NEWFUNC:
2976 {
2977 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2978
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002979 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002980 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002981 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002982 }
2983 break;
2984
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002985 // List functions
2986 case ISN_DEF:
2987 if (iptr->isn_arg.string == NULL)
2988 list_functions(NULL);
2989 else
2990 {
2991 exarg_T ea;
2992
2993 CLEAR_FIELD(ea);
2994 ea.cmd = ea.arg = iptr->isn_arg.string;
2995 define_function(&ea, NULL);
2996 }
2997 break;
2998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 // jump if a condition is met
3000 case ISN_JUMP:
3001 {
3002 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003003 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 int jump = TRUE;
3005
3006 if (when != JUMP_ALWAYS)
3007 {
3008 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003009 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003010 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003011 || when == JUMP_IF_COND_TRUE)
3012 {
3013 SOURCING_LNUM = iptr->isn_lnum;
3014 jump = tv_get_bool_chk(tv, &error);
3015 if (error)
3016 goto on_error;
3017 }
3018 else
3019 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003021 || when == JUMP_AND_KEEP_IF_FALSE
3022 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003024 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 {
3026 // drop the value from the stack
3027 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003028 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 }
3030 }
3031 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003032 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 }
3034 break;
3035
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003036 // Jump if an argument with a default value was already set and not
3037 // v:none.
3038 case ISN_JUMP_IF_ARG_SET:
3039 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3040 if (tv->v_type != VAR_UNKNOWN
3041 && !(tv->v_type == VAR_SPECIAL
3042 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003043 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003044 break;
3045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 // top of a for loop
3047 case ISN_FOR:
3048 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003049 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 typval_T *idxtv =
3051 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3052
Bram Moolenaar4c137212021-04-19 16:48:48 +02003053 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003054 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003055 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003056 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003057 list_T *list = ltv->vval.v_list;
3058
3059 // push the next item from the list
3060 ++idxtv->vval.v_number;
3061 if (list == NULL
3062 || idxtv->vval.v_number >= list->lv_len)
3063 {
3064 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003065 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3066 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003067 }
3068 else if (list->lv_first == &range_list_item)
3069 {
3070 // non-materialized range() list
3071 tv = STACK_TV_BOT(0);
3072 tv->v_type = VAR_NUMBER;
3073 tv->v_lock = 0;
3074 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003076 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003077 }
3078 else
3079 {
3080 listitem_T *li = list_find(list,
3081 idxtv->vval.v_number);
3082
3083 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003084 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003085 }
3086 }
3087 else if (ltv->v_type == VAR_STRING)
3088 {
3089 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003090
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003091 // The index is for the last byte of the previous
3092 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003093 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003094 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003095 {
3096 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003097 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3098 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003099 }
3100 else
3101 {
3102 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3103
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003104 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003105 tv = STACK_TV_BOT(0);
3106 tv->v_type = VAR_STRING;
3107 tv->vval.v_string = vim_strnsave(
3108 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003109 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003110 idxtv->vval.v_number += clen - 1;
3111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003113 else if (ltv->v_type == VAR_BLOB)
3114 {
3115 blob_T *blob = ltv->vval.v_blob;
3116
3117 // When we get here the first time make a copy of the
3118 // blob, so that the iteration still works when it is
3119 // changed.
3120 if (idxtv->vval.v_number == -1 && blob != NULL)
3121 {
3122 blob_copy(blob, ltv);
3123 blob_unref(blob);
3124 blob = ltv->vval.v_blob;
3125 }
3126
3127 // The index is for the previous byte.
3128 ++idxtv->vval.v_number;
3129 if (blob == NULL
3130 || idxtv->vval.v_number >= blob_len(blob))
3131 {
3132 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003133 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3134 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003135 }
3136 else
3137 {
3138 // Push the next byte from the blob.
3139 tv = STACK_TV_BOT(0);
3140 tv->v_type = VAR_NUMBER;
3141 tv->vval.v_number = blob_get(blob,
3142 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003143 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003144 }
3145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 else
3147 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003148 semsg(_(e_for_loop_on_str_not_supported),
3149 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003150 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 }
3152 }
3153 break;
3154
3155 // start of ":try" block
3156 case ISN_TRY:
3157 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003158 trycmd_T *trycmd = NULL;
3159
Bram Moolenaar4c137212021-04-19 16:48:48 +02003160 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003161 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003162 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3163 + ectx->ec_trystack.ga_len;
3164 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003166 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003167 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3168 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003169 trycmd->tcd_catch_idx =
3170 iptr->isn_arg.try.try_ref->try_catch;
3171 trycmd->tcd_finally_idx =
3172 iptr->isn_arg.try.try_ref->try_finally;
3173 trycmd->tcd_endtry_idx =
3174 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 }
3176 break;
3177
3178 case ISN_PUSHEXC:
3179 if (current_exception == NULL)
3180 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003181 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003183 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003185 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003186 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003188 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003190 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 tv->vval.v_string = vim_strsave(
3192 (char_u *)current_exception->value);
3193 break;
3194
3195 case ISN_CATCH:
3196 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003197 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198
Bram Moolenaar4c137212021-04-19 16:48:48 +02003199 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 if (trystack->ga_len > 0)
3201 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003202 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 + trystack->ga_len - 1;
3204 trycmd->tcd_caught = TRUE;
3205 }
3206 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003207 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 catch_exception(current_exception);
3209 }
3210 break;
3211
Bram Moolenaarc150c092021-02-13 15:02:46 +01003212 case ISN_TRYCONT:
3213 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003214 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003215 trycont_T *trycont = &iptr->isn_arg.trycont;
3216 int i;
3217 trycmd_T *trycmd;
3218 int iidx = trycont->tct_where;
3219
3220 if (trystack->ga_len < trycont->tct_levels)
3221 {
3222 siemsg("TRYCONT: expected %d levels, found %d",
3223 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003224 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003225 }
3226 // Make :endtry jump to any outer try block and the last
3227 // :endtry inside the loop to the loop start.
3228 for (i = trycont->tct_levels; i > 0; --i)
3229 {
3230 trycmd = ((trycmd_T *)trystack->ga_data)
3231 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003232 // Add one to tcd_cont to be able to jump to
3233 // instruction with index zero.
3234 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003235 iidx = trycmd->tcd_finally_idx == 0
3236 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003237 }
3238 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003239 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003240 }
3241 break;
3242
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003243 case ISN_FINALLY:
3244 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003245 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003246 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3247 + trystack->ga_len - 1;
3248
3249 // Reset the index to avoid a return statement jumps here
3250 // again.
3251 trycmd->tcd_finally_idx = 0;
3252 break;
3253 }
3254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 // end of ":try" block
3256 case ISN_ENDTRY:
3257 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003258 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259
3260 if (trystack->ga_len > 0)
3261 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003262 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 --trystack->ga_len;
3265 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003266 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 trycmd = ((trycmd_T *)trystack->ga_data)
3268 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003269 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 {
3271 // discard the exception
3272 if (caught_stack == current_exception)
3273 caught_stack = caught_stack->caught;
3274 discard_current_exception();
3275 }
3276
3277 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003278 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003279
Bram Moolenaar4c137212021-04-19 16:48:48 +02003280 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003281 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003282 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003283 clear_tv(STACK_TV_BOT(0));
3284 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003285 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003286 // handling :continue: jump to outer try block or
3287 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003288 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 }
3290 }
3291 break;
3292
3293 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003294 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003295 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003296
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003297 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3298 {
3299 // throwing an exception while using "silent!" causes
3300 // the function to abort but not display an error.
3301 tv = STACK_TV_BOT(-1);
3302 clear_tv(tv);
3303 tv->v_type = VAR_NUMBER;
3304 tv->vval.v_number = 0;
3305 goto done;
3306 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003307 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003308 tv = STACK_TV_BOT(0);
3309 if (tv->vval.v_string == NULL
3310 || *skipwhite(tv->vval.v_string) == NUL)
3311 {
3312 vim_free(tv->vval.v_string);
3313 SOURCING_LNUM = iptr->isn_lnum;
3314 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003315 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003316 }
3317
3318 // Inside a "catch" we need to first discard the caught
3319 // exception.
3320 if (trystack->ga_len > 0)
3321 {
3322 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3323 + trystack->ga_len - 1;
3324 if (trycmd->tcd_caught && current_exception != NULL)
3325 {
3326 // discard the exception
3327 if (caught_stack == current_exception)
3328 caught_stack = caught_stack->caught;
3329 discard_current_exception();
3330 trycmd->tcd_caught = FALSE;
3331 }
3332 }
3333
3334 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3335 == FAIL)
3336 {
3337 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003338 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003339 }
3340 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 break;
3343
3344 // compare with special values
3345 case ISN_COMPAREBOOL:
3346 case ISN_COMPARESPECIAL:
3347 {
3348 typval_T *tv1 = STACK_TV_BOT(-2);
3349 typval_T *tv2 = STACK_TV_BOT(-1);
3350 varnumber_T arg1 = tv1->vval.v_number;
3351 varnumber_T arg2 = tv2->vval.v_number;
3352 int res;
3353
3354 switch (iptr->isn_arg.op.op_type)
3355 {
3356 case EXPR_EQUAL: res = arg1 == arg2; break;
3357 case EXPR_NEQUAL: res = arg1 != arg2; break;
3358 default: res = 0; break;
3359 }
3360
Bram Moolenaar4c137212021-04-19 16:48:48 +02003361 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 tv1->v_type = VAR_BOOL;
3363 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3364 }
3365 break;
3366
3367 // Operation with two number arguments
3368 case ISN_OPNR:
3369 case ISN_COMPARENR:
3370 {
3371 typval_T *tv1 = STACK_TV_BOT(-2);
3372 typval_T *tv2 = STACK_TV_BOT(-1);
3373 varnumber_T arg1 = tv1->vval.v_number;
3374 varnumber_T arg2 = tv2->vval.v_number;
3375 varnumber_T res;
3376
3377 switch (iptr->isn_arg.op.op_type)
3378 {
3379 case EXPR_MULT: res = arg1 * arg2; break;
3380 case EXPR_DIV: res = arg1 / arg2; break;
3381 case EXPR_REM: res = arg1 % arg2; break;
3382 case EXPR_SUB: res = arg1 - arg2; break;
3383 case EXPR_ADD: res = arg1 + arg2; break;
3384
3385 case EXPR_EQUAL: res = arg1 == arg2; break;
3386 case EXPR_NEQUAL: res = arg1 != arg2; break;
3387 case EXPR_GREATER: res = arg1 > arg2; break;
3388 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3389 case EXPR_SMALLER: res = arg1 < arg2; break;
3390 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3391 default: res = 0; break;
3392 }
3393
Bram Moolenaar4c137212021-04-19 16:48:48 +02003394 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 if (iptr->isn_type == ISN_COMPARENR)
3396 {
3397 tv1->v_type = VAR_BOOL;
3398 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3399 }
3400 else
3401 tv1->vval.v_number = res;
3402 }
3403 break;
3404
3405 // Computation with two float arguments
3406 case ISN_OPFLOAT:
3407 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003408#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 {
3410 typval_T *tv1 = STACK_TV_BOT(-2);
3411 typval_T *tv2 = STACK_TV_BOT(-1);
3412 float_T arg1 = tv1->vval.v_float;
3413 float_T arg2 = tv2->vval.v_float;
3414 float_T res = 0;
3415 int cmp = FALSE;
3416
3417 switch (iptr->isn_arg.op.op_type)
3418 {
3419 case EXPR_MULT: res = arg1 * arg2; break;
3420 case EXPR_DIV: res = arg1 / arg2; break;
3421 case EXPR_SUB: res = arg1 - arg2; break;
3422 case EXPR_ADD: res = arg1 + arg2; break;
3423
3424 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3425 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3426 case EXPR_GREATER: cmp = arg1 > arg2; break;
3427 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3428 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3429 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3430 default: cmp = 0; break;
3431 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003432 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 if (iptr->isn_type == ISN_COMPAREFLOAT)
3434 {
3435 tv1->v_type = VAR_BOOL;
3436 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3437 }
3438 else
3439 tv1->vval.v_float = res;
3440 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003441#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 break;
3443
3444 case ISN_COMPARELIST:
3445 {
3446 typval_T *tv1 = STACK_TV_BOT(-2);
3447 typval_T *tv2 = STACK_TV_BOT(-1);
3448 list_T *arg1 = tv1->vval.v_list;
3449 list_T *arg2 = tv2->vval.v_list;
3450 int cmp = FALSE;
3451 int ic = iptr->isn_arg.op.op_ic;
3452
3453 switch (iptr->isn_arg.op.op_type)
3454 {
3455 case EXPR_EQUAL: cmp =
3456 list_equal(arg1, arg2, ic, FALSE); break;
3457 case EXPR_NEQUAL: cmp =
3458 !list_equal(arg1, arg2, ic, FALSE); break;
3459 case EXPR_IS: cmp = arg1 == arg2; break;
3460 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3461 default: cmp = 0; break;
3462 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003463 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 clear_tv(tv1);
3465 clear_tv(tv2);
3466 tv1->v_type = VAR_BOOL;
3467 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3468 }
3469 break;
3470
3471 case ISN_COMPAREBLOB:
3472 {
3473 typval_T *tv1 = STACK_TV_BOT(-2);
3474 typval_T *tv2 = STACK_TV_BOT(-1);
3475 blob_T *arg1 = tv1->vval.v_blob;
3476 blob_T *arg2 = tv2->vval.v_blob;
3477 int cmp = FALSE;
3478
3479 switch (iptr->isn_arg.op.op_type)
3480 {
3481 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3482 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3483 case EXPR_IS: cmp = arg1 == arg2; break;
3484 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3485 default: cmp = 0; break;
3486 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003487 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 clear_tv(tv1);
3489 clear_tv(tv2);
3490 tv1->v_type = VAR_BOOL;
3491 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3492 }
3493 break;
3494
3495 // TODO: handle separately
3496 case ISN_COMPARESTRING:
3497 case ISN_COMPAREDICT:
3498 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 case ISN_COMPAREANY:
3500 {
3501 typval_T *tv1 = STACK_TV_BOT(-2);
3502 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003503 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 int ic = iptr->isn_arg.op.op_ic;
3505
Bram Moolenaareb26f432020-09-14 16:50:05 +02003506 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003507 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003509 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 }
3511 break;
3512
3513 case ISN_ADDLIST:
3514 case ISN_ADDBLOB:
3515 {
3516 typval_T *tv1 = STACK_TV_BOT(-2);
3517 typval_T *tv2 = STACK_TV_BOT(-1);
3518
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003519 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 if (iptr->isn_type == ISN_ADDLIST)
3521 eval_addlist(tv1, tv2);
3522 else
3523 eval_addblob(tv1, tv2);
3524 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003525 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 }
3527 break;
3528
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003529 case ISN_LISTAPPEND:
3530 {
3531 typval_T *tv1 = STACK_TV_BOT(-2);
3532 typval_T *tv2 = STACK_TV_BOT(-1);
3533 list_T *l = tv1->vval.v_list;
3534
3535 // add an item to a list
3536 if (l == NULL)
3537 {
3538 SOURCING_LNUM = iptr->isn_lnum;
3539 emsg(_(e_cannot_add_to_null_list));
3540 goto on_error;
3541 }
3542 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003543 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003544 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003545 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003546 }
3547 break;
3548
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003549 case ISN_BLOBAPPEND:
3550 {
3551 typval_T *tv1 = STACK_TV_BOT(-2);
3552 typval_T *tv2 = STACK_TV_BOT(-1);
3553 blob_T *b = tv1->vval.v_blob;
3554 int error = FALSE;
3555 varnumber_T n;
3556
3557 // add a number to a blob
3558 if (b == NULL)
3559 {
3560 SOURCING_LNUM = iptr->isn_lnum;
3561 emsg(_(e_cannot_add_to_null_blob));
3562 goto on_error;
3563 }
3564 n = tv_get_number_chk(tv2, &error);
3565 if (error)
3566 goto on_error;
3567 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003568 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003569 }
3570 break;
3571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 // Computation with two arguments of unknown type
3573 case ISN_OPANY:
3574 {
3575 typval_T *tv1 = STACK_TV_BOT(-2);
3576 typval_T *tv2 = STACK_TV_BOT(-1);
3577 varnumber_T n1, n2;
3578#ifdef FEAT_FLOAT
3579 float_T f1 = 0, f2 = 0;
3580#endif
3581 int error = FALSE;
3582
3583 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3584 {
3585 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3586 {
3587 eval_addlist(tv1, tv2);
3588 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003589 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 break;
3591 }
3592 else if (tv1->v_type == VAR_BLOB
3593 && tv2->v_type == VAR_BLOB)
3594 {
3595 eval_addblob(tv1, tv2);
3596 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003597 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 break;
3599 }
3600 }
3601#ifdef FEAT_FLOAT
3602 if (tv1->v_type == VAR_FLOAT)
3603 {
3604 f1 = tv1->vval.v_float;
3605 n1 = 0;
3606 }
3607 else
3608#endif
3609 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003610 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 n1 = tv_get_number_chk(tv1, &error);
3612 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003613 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614#ifdef FEAT_FLOAT
3615 if (tv2->v_type == VAR_FLOAT)
3616 f1 = n1;
3617#endif
3618 }
3619#ifdef FEAT_FLOAT
3620 if (tv2->v_type == VAR_FLOAT)
3621 {
3622 f2 = tv2->vval.v_float;
3623 n2 = 0;
3624 }
3625 else
3626#endif
3627 {
3628 n2 = tv_get_number_chk(tv2, &error);
3629 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003630 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631#ifdef FEAT_FLOAT
3632 if (tv1->v_type == VAR_FLOAT)
3633 f2 = n2;
3634#endif
3635 }
3636#ifdef FEAT_FLOAT
3637 // if there is a float on either side the result is a float
3638 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3639 {
3640 switch (iptr->isn_arg.op.op_type)
3641 {
3642 case EXPR_MULT: f1 = f1 * f2; break;
3643 case EXPR_DIV: f1 = f1 / f2; break;
3644 case EXPR_SUB: f1 = f1 - f2; break;
3645 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003646 default: SOURCING_LNUM = iptr->isn_lnum;
3647 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003648 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 }
3650 clear_tv(tv1);
3651 clear_tv(tv2);
3652 tv1->v_type = VAR_FLOAT;
3653 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003654 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 }
3656 else
3657#endif
3658 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003659 int failed = FALSE;
3660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 switch (iptr->isn_arg.op.op_type)
3662 {
3663 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003664 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3665 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003666 goto on_error;
3667 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 case EXPR_SUB: n1 = n1 - n2; break;
3669 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003670 default: n1 = num_modulus(n1, n2, &failed);
3671 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003672 goto on_error;
3673 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 }
3675 clear_tv(tv1);
3676 clear_tv(tv2);
3677 tv1->v_type = VAR_NUMBER;
3678 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003679 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 }
3681 }
3682 break;
3683
3684 case ISN_CONCAT:
3685 {
3686 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3687 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3688 char_u *res;
3689
3690 res = concat_str(str1, str2);
3691 clear_tv(STACK_TV_BOT(-2));
3692 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003693 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 STACK_TV_BOT(-1)->vval.v_string = res;
3695 }
3696 break;
3697
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003698 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003699 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003700 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003701 int is_slice = iptr->isn_type == ISN_STRSLICE;
3702 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003703 char_u *res;
3704
3705 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003706 // string slice: string is at stack-3, first index at
3707 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003708 if (is_slice)
3709 {
3710 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003711 n1 = tv->vval.v_number;
3712 }
3713
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003714 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003715 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003716
Bram Moolenaar4c137212021-04-19 16:48:48 +02003717 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003718 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003719 if (is_slice)
3720 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003721 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003722 else
3723 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003724 // single character (including composing characters).
3725 // If the index is too big or negative the result is
3726 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003727 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003728 vim_free(tv->vval.v_string);
3729 tv->vval.v_string = res;
3730 }
3731 break;
3732
3733 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003734 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003735 case ISN_BLOBINDEX:
3736 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003738 int is_slice = iptr->isn_type == ISN_LISTSLICE
3739 || iptr->isn_type == ISN_BLOBSLICE;
3740 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3741 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003742 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003743 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744
3745 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003746 // list slice: list is at stack-3, indexes at stack-2 and
3747 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003748 // Same for blob.
3749 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750
3751 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003752 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003754
3755 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 {
Bram Moolenaared591872020-08-15 22:14:53 +02003757 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003758 n1 = tv->vval.v_number;
3759 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 }
Bram Moolenaared591872020-08-15 22:14:53 +02003761
Bram Moolenaar4c137212021-04-19 16:48:48 +02003762 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003763 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003764 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003765 if (is_blob)
3766 {
3767 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3768 n1, n2, FALSE, tv) == FAIL)
3769 goto on_error;
3770 }
3771 else
3772 {
3773 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3774 n1, n2, FALSE, tv, TRUE) == FAIL)
3775 goto on_error;
3776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 }
3778 break;
3779
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003780 case ISN_ANYINDEX:
3781 case ISN_ANYSLICE:
3782 {
3783 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3784 typval_T *var1, *var2;
3785 int res;
3786
3787 // index: composite is at stack-2, index at stack-1
3788 // slice: composite is at stack-3, indexes at stack-2 and
3789 // stack-1
3790 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003791 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003792 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3793 goto on_error;
3794 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3795 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003796 res = eval_index_inner(tv, is_slice, var1, var2,
3797 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003798 clear_tv(var1);
3799 if (is_slice)
3800 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003801 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003802 if (res == FAIL)
3803 goto on_error;
3804 }
3805 break;
3806
Bram Moolenaar9af78762020-06-16 11:34:42 +02003807 case ISN_SLICE:
3808 {
3809 list_T *list;
3810 int count = iptr->isn_arg.number;
3811
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003812 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003813 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003814 list = tv->vval.v_list;
3815
3816 // no error for short list, expect it to be checked earlier
3817 if (list != NULL && list->lv_len >= count)
3818 {
3819 list_T *newlist = list_slice(list,
3820 count, list->lv_len - 1);
3821
3822 if (newlist != NULL)
3823 {
3824 list_unref(list);
3825 tv->vval.v_list = newlist;
3826 ++newlist->lv_refcount;
3827 }
3828 }
3829 }
3830 break;
3831
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003832 case ISN_GETITEM:
3833 {
3834 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003835 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003836
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003837 // Get list item: list is at stack-1, push item.
3838 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003839 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
3840 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003841
Bram Moolenaar4c137212021-04-19 16:48:48 +02003842 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003843 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003844 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003845 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003846
3847 // Useful when used in unpack assignment. Reset at
3848 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003849 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003850 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003851 }
3852 break;
3853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 case ISN_MEMBER:
3855 {
3856 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003857 char_u *key;
3858 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003859 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003860
3861 // dict member: dict is at stack-2, key at stack-1
3862 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003863 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003864 dict = tv->vval.v_dict;
3865
3866 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003867 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003868 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003869 if (key == NULL)
3870 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003871
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003872 if ((di = dict_find(dict, key, -1)) == NULL)
3873 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003874 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003875 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003876
3877 // If :silent! is used we will continue, make sure the
3878 // stack contents makes sense.
3879 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003880 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003881 tv = STACK_TV_BOT(-1);
3882 clear_tv(tv);
3883 tv->v_type = VAR_NUMBER;
3884 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003885 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003886 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003887 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003888 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003889 // Clear the dict only after getting the item, to avoid
3890 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003891 tv = STACK_TV_BOT(-1);
3892 temp_tv = *tv;
3893 copy_tv(&di->di_tv, tv);
3894 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003895 }
3896 break;
3897
3898 // dict member with string key
3899 case ISN_STRINGMEMBER:
3900 {
3901 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003903 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904
3905 tv = STACK_TV_BOT(-1);
3906 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3907 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003908 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003910 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 }
3912 dict = tv->vval.v_dict;
3913
3914 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3915 == NULL)
3916 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003917 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003919 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003921 // Clear the dict after getting the item, to avoid that it
3922 // make the item invalid.
3923 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003925 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 }
3927 break;
3928
3929 case ISN_NEGATENR:
3930 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003931 if (tv->v_type != VAR_NUMBER
3932#ifdef FEAT_FLOAT
3933 && tv->v_type != VAR_FLOAT
3934#endif
3935 )
3936 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003937 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003938 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003939 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003940 }
3941#ifdef FEAT_FLOAT
3942 if (tv->v_type == VAR_FLOAT)
3943 tv->vval.v_float = -tv->vval.v_float;
3944 else
3945#endif
3946 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 break;
3948
3949 case ISN_CHECKNR:
3950 {
3951 int error = FALSE;
3952
3953 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003954 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003956 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 (void)tv_get_number_chk(tv, &error);
3958 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003959 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 }
3961 break;
3962
3963 case ISN_CHECKTYPE:
3964 {
3965 checktype_T *ct = &iptr->isn_arg.type;
3966
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003967 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003968 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003969 if (!ectx->ec_where.wt_variable)
3970 ectx->ec_where.wt_index = ct->ct_arg_idx;
3971 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3972 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003973 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003974 if (!ectx->ec_where.wt_variable)
3975 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003976
3977 // number 0 is FALSE, number 1 is TRUE
3978 if (tv->v_type == VAR_NUMBER
3979 && ct->ct_type->tt_type == VAR_BOOL
3980 && (tv->vval.v_number == 0
3981 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003983 tv->v_type = VAR_BOOL;
3984 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003985 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 }
3987 }
3988 break;
3989
Bram Moolenaar9af78762020-06-16 11:34:42 +02003990 case ISN_CHECKLEN:
3991 {
3992 int min_len = iptr->isn_arg.checklen.cl_min_len;
3993 list_T *list = NULL;
3994
3995 tv = STACK_TV_BOT(-1);
3996 if (tv->v_type == VAR_LIST)
3997 list = tv->vval.v_list;
3998 if (list == NULL || list->lv_len < min_len
3999 || (list->lv_len > min_len
4000 && !iptr->isn_arg.checklen.cl_more_OK))
4001 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004002 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004003 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004004 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004005 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004006 }
4007 }
4008 break;
4009
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004010 case ISN_SETTYPE:
4011 {
4012 checktype_T *ct = &iptr->isn_arg.type;
4013
4014 tv = STACK_TV_BOT(-1);
4015 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4016 {
4017 free_type(tv->vval.v_dict->dv_type);
4018 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4019 }
4020 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4021 {
4022 free_type(tv->vval.v_list->lv_type);
4023 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4024 }
4025 }
4026 break;
4027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004029 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 {
4031 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004032 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004034 if (iptr->isn_type == ISN_2BOOL)
4035 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004036 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004037 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004038 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004039 n = !n;
4040 }
4041 else
4042 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004043 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004044 SOURCING_LNUM = iptr->isn_lnum;
4045 n = tv_get_bool_chk(tv, &error);
4046 if (error)
4047 goto on_error;
4048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 clear_tv(tv);
4050 tv->v_type = VAR_BOOL;
4051 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4052 }
4053 break;
4054
4055 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004056 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004057 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004058 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4059 iptr->isn_type == ISN_2STRING_ANY,
4060 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004061 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 break;
4063
Bram Moolenaar08597872020-12-10 19:43:40 +01004064 case ISN_RANGE:
4065 {
4066 exarg_T ea;
4067 char *errormsg;
4068
Bram Moolenaarece0b872021-01-08 20:40:45 +01004069 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004070 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004071 ea.addr_type = ADDR_LINES;
4072 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004073 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004074 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004075 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004076
Bram Moolenaar4c137212021-04-19 16:48:48 +02004077 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004078 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004079 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004080 tv = STACK_TV_BOT(-1);
4081 tv->v_type = VAR_NUMBER;
4082 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004083 if (ea.addr_count == 0)
4084 tv->vval.v_number = curwin->w_cursor.lnum;
4085 else
4086 tv->vval.v_number = ea.line2;
4087 }
4088 break;
4089
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004090 case ISN_PUT:
4091 {
4092 int regname = iptr->isn_arg.put.put_regname;
4093 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4094 char_u *expr = NULL;
4095 int dir = FORWARD;
4096
Bram Moolenaar08597872020-12-10 19:43:40 +01004097 if (lnum < -2)
4098 {
4099 // line number was put on the stack by ISN_RANGE
4100 tv = STACK_TV_BOT(-1);
4101 curwin->w_cursor.lnum = tv->vval.v_number;
4102 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4103 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004104 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004105 }
4106 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004107 // :put! above cursor
4108 dir = BACKWARD;
4109 else if (lnum >= 0)
4110 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004111
4112 if (regname == '=')
4113 {
4114 tv = STACK_TV_BOT(-1);
4115 if (tv->v_type == VAR_STRING)
4116 expr = tv->vval.v_string;
4117 else
4118 {
4119 expr = typval2string(tv, TRUE); // allocates value
4120 clear_tv(tv);
4121 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004122 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004123 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004124 check_cursor();
4125 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4126 vim_free(expr);
4127 }
4128 break;
4129
Bram Moolenaar02194d22020-10-24 23:08:38 +02004130 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004131 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4132 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4133 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4134 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004135 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4136 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004137 break;
4138
Bram Moolenaar02194d22020-10-24 23:08:38 +02004139 case ISN_CMDMOD_REV:
4140 // filter regprog is owned by the instruction, don't free it
4141 cmdmod.cmod_filter_regmatch.regprog = NULL;
4142 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004143 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4144 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004145 break;
4146
Bram Moolenaar792f7862020-11-23 08:31:18 +01004147 case ISN_UNPACK:
4148 {
4149 int count = iptr->isn_arg.unpack.unp_count;
4150 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4151 list_T *l;
4152 listitem_T *li;
4153 int i;
4154
4155 // Check there is a valid list to unpack.
4156 tv = STACK_TV_BOT(-1);
4157 if (tv->v_type != VAR_LIST)
4158 {
4159 SOURCING_LNUM = iptr->isn_lnum;
4160 emsg(_(e_for_argument_must_be_sequence_of_lists));
4161 goto on_error;
4162 }
4163 l = tv->vval.v_list;
4164 if (l == NULL
4165 || l->lv_len < (semicolon ? count - 1 : count))
4166 {
4167 SOURCING_LNUM = iptr->isn_lnum;
4168 emsg(_(e_list_value_does_not_have_enough_items));
4169 goto on_error;
4170 }
4171 else if (!semicolon && l->lv_len > count)
4172 {
4173 SOURCING_LNUM = iptr->isn_lnum;
4174 emsg(_(e_list_value_has_more_items_than_targets));
4175 goto on_error;
4176 }
4177
4178 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004179 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004180 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004181 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004182
4183 // Variable after semicolon gets a list with the remaining
4184 // items.
4185 if (semicolon)
4186 {
4187 list_T *rem_list =
4188 list_alloc_with_items(l->lv_len - count + 1);
4189
4190 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004191 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004192 tv = STACK_TV_BOT(-count);
4193 tv->vval.v_list = rem_list;
4194 ++rem_list->lv_refcount;
4195 tv->v_lock = 0;
4196 li = l->lv_first;
4197 for (i = 0; i < count - 1; ++i)
4198 li = li->li_next;
4199 for (i = 0; li != NULL; ++i)
4200 {
4201 list_set_item(rem_list, i, &li->li_tv);
4202 li = li->li_next;
4203 }
4204 --count;
4205 }
4206
4207 // Produce the values in reverse order, first item last.
4208 li = l->lv_first;
4209 for (i = 0; i < count; ++i)
4210 {
4211 tv = STACK_TV_BOT(-i - 1);
4212 copy_tv(&li->li_tv, tv);
4213 li = li->li_next;
4214 }
4215
4216 list_unref(l);
4217 }
4218 break;
4219
Bram Moolenaarb2049902021-01-24 12:53:53 +01004220 case ISN_PROF_START:
4221 case ISN_PROF_END:
4222 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004223#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004224 funccall_T cookie;
4225 ufunc_T *cur_ufunc =
4226 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004227 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004228
4229 cookie.func = cur_ufunc;
4230 if (iptr->isn_type == ISN_PROF_START)
4231 {
4232 func_line_start(&cookie, iptr->isn_lnum);
4233 // if we get here the instruction is executed
4234 func_line_exec(&cookie);
4235 }
4236 else
4237 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004238#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004239 }
4240 break;
4241
Bram Moolenaare99d4222021-06-13 14:01:26 +02004242 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004243 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004244 break;
4245
Bram Moolenaar389df252020-07-09 21:20:47 +02004246 case ISN_SHUFFLE:
4247 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004248 typval_T tmp_tv;
4249 int item = iptr->isn_arg.shuffle.shfl_item;
4250 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004251
4252 tmp_tv = *STACK_TV_BOT(-item);
4253 for ( ; up > 0 && item > 1; --up)
4254 {
4255 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4256 --item;
4257 }
4258 *STACK_TV_BOT(-item) = tmp_tv;
4259 }
4260 break;
4261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004263 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004265 ectx->ec_where.wt_index = 0;
4266 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 break;
4268 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004269 continue;
4270
Bram Moolenaard032f342020-07-18 18:13:02 +02004271func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004272 // Restore previous function. If the frame pointer is where we started
4273 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004274 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004275 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004276
Bram Moolenaar4c137212021-04-19 16:48:48 +02004277 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004278 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004279 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004280 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004281
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004282on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004283 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004284 // If "emsg_silent" is set then ignore the error, unless it was set
4285 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004286 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004287 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004288 {
4289 // If a sequence of instructions causes an error while ":silent!"
4290 // was used, restore the stack length and jump ahead to restoring
4291 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004292 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004293 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004294 while (ectx->ec_stack.ga_len
4295 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004296 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004297 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004298 clear_tv(STACK_TV_BOT(0));
4299 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004300 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4301 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004302 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004303 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004304 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004305on_fatal_error:
4306 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004307 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004308 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004309 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 }
4311
4312done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004313 ret = OK;
4314theend:
4315 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4316 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004317}
4318
4319/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004320 * Execute the instructions from a VAR_INSTR typeval and put the result in
4321 * "rettv".
4322 * Return OK or FAIL.
4323 */
4324 int
4325exe_typval_instr(typval_T *tv, typval_T *rettv)
4326{
4327 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4328 isn_T *save_instr = ectx->ec_instr;
4329 int save_iidx = ectx->ec_iidx;
4330 int res;
4331
4332 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4333 res = exec_instructions(ectx);
4334 if (res == OK)
4335 {
4336 *rettv = *STACK_TV_BOT(-1);
4337 --ectx->ec_stack.ga_len;
4338 }
4339
4340 ectx->ec_instr = save_instr;
4341 ectx->ec_iidx = save_iidx;
4342
4343 return res;
4344}
4345
4346/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004347 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4348 * "substitute_instr".
4349 */
4350 char_u *
4351exe_substitute_instr(void)
4352{
4353 ectx_T *ectx = substitute_instr->subs_ectx;
4354 isn_T *save_instr = ectx->ec_instr;
4355 int save_iidx = ectx->ec_iidx;
4356 char_u *res;
4357
4358 ectx->ec_instr = substitute_instr->subs_instr;
4359 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004360 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004361 typval_T *tv = STACK_TV_BOT(-1);
4362
Bram Moolenaar27523602021-06-05 21:36:19 +02004363 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004364 --ectx->ec_stack.ga_len;
4365 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004366 }
4367 else
4368 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004369 substitute_instr->subs_status = FAIL;
4370 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004371 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372
Bram Moolenaar4c137212021-04-19 16:48:48 +02004373 ectx->ec_instr = save_instr;
4374 ectx->ec_iidx = save_iidx;
4375
4376 return res;
4377}
4378
4379/*
4380 * Call a "def" function from old Vim script.
4381 * Return OK or FAIL.
4382 */
4383 int
4384call_def_function(
4385 ufunc_T *ufunc,
4386 int argc_arg, // nr of arguments
4387 typval_T *argv, // arguments
4388 partial_T *partial, // optional partial for context
4389 typval_T *rettv) // return value
4390{
4391 ectx_T ectx; // execution context
4392 int argc = argc_arg;
4393 typval_T *tv;
4394 int idx;
4395 int ret = FAIL;
4396 int defcount = ufunc->uf_args.ga_len - argc;
4397 sctx_T save_current_sctx = current_sctx;
4398 int did_emsg_before = did_emsg_cumul + did_emsg;
4399 int save_suppress_errthrow = suppress_errthrow;
4400 msglist_T **saved_msg_list = NULL;
4401 msglist_T *private_msg_list = NULL;
4402 int save_emsg_silent_def = emsg_silent_def;
4403 int save_did_emsg_def = did_emsg_def;
4404 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004405 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004406
4407// Get pointer to item in the stack.
4408#undef STACK_TV
4409#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4410
4411// Get pointer to item at the bottom of the stack, -1 is the bottom.
4412#undef STACK_TV_BOT
4413#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4414
4415// Get pointer to a local variable on the stack. Negative for arguments.
4416#undef STACK_TV_VAR
4417#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4418
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004419 // Update uf_has_breakpoint if needed.
4420 update_has_breakpoint(ufunc);
4421
Bram Moolenaar4c137212021-04-19 16:48:48 +02004422 if (ufunc->uf_def_status == UF_NOT_COMPILED
4423 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004424 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4425 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004426 == FAIL))
4427 {
4428 if (did_emsg_cumul + did_emsg == did_emsg_before)
4429 semsg(_(e_function_is_not_compiled_str),
4430 printable_func_name(ufunc));
4431 return FAIL;
4432 }
4433
4434 {
4435 // Check the function was really compiled.
4436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4437 + ufunc->uf_dfunc_idx;
4438 if (INSTRUCTIONS(dfunc) == NULL)
4439 {
4440 iemsg("using call_def_function() on not compiled function");
4441 return FAIL;
4442 }
4443 }
4444
4445 // If depth of calling is getting too high, don't execute the function.
4446 orig_funcdepth = funcdepth_get();
4447 if (funcdepth_increment() == FAIL)
4448 return FAIL;
4449
4450 CLEAR_FIELD(ectx);
4451 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4452 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4453 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4454 {
4455 funcdepth_decrement();
4456 return FAIL;
4457 }
4458 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4459 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4460 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004461 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004462
4463 idx = argc - ufunc->uf_args.ga_len;
4464 if (idx > 0 && ufunc->uf_va_name == NULL)
4465 {
4466 if (idx == 1)
4467 emsg(_(e_one_argument_too_many));
4468 else
4469 semsg(_(e_nr_arguments_too_many), idx);
4470 goto failed_early;
4471 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004472 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4473 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004474 {
4475 if (idx == -1)
4476 emsg(_(e_one_argument_too_few));
4477 else
4478 semsg(_(e_nr_arguments_too_few), -idx);
4479 goto failed_early;
4480 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004481
4482 // Put arguments on the stack, but no more than what the function expects.
4483 // A lambda can be called with more arguments than it uses.
4484 for (idx = 0; idx < argc
4485 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4486 ++idx)
4487 {
4488 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4489 && argv[idx].v_type == VAR_SPECIAL
4490 && argv[idx].vval.v_number == VVAL_NONE)
4491 {
4492 // Use the default value.
4493 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4494 }
4495 else
4496 {
4497 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4498 && check_typval_arg_type(
4499 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4500 goto failed_early;
4501 copy_tv(&argv[idx], STACK_TV_BOT(0));
4502 }
4503 ++ectx.ec_stack.ga_len;
4504 }
4505
4506 // Turn varargs into a list. Empty list if no args.
4507 if (ufunc->uf_va_name != NULL)
4508 {
4509 int vararg_count = argc - ufunc->uf_args.ga_len;
4510
4511 if (vararg_count < 0)
4512 vararg_count = 0;
4513 else
4514 argc -= vararg_count;
4515 if (exe_newlist(vararg_count, &ectx) == FAIL)
4516 goto failed_early;
4517
4518 // Check the type of the list items.
4519 tv = STACK_TV_BOT(-1);
4520 if (ufunc->uf_va_type != NULL
4521 && ufunc->uf_va_type != &t_list_any
4522 && ufunc->uf_va_type->tt_member != &t_any
4523 && tv->vval.v_list != NULL)
4524 {
4525 type_T *expected = ufunc->uf_va_type->tt_member;
4526 listitem_T *li = tv->vval.v_list->lv_first;
4527
4528 for (idx = 0; idx < vararg_count; ++idx)
4529 {
4530 if (check_typval_arg_type(expected, &li->li_tv,
4531 argc + idx + 1) == FAIL)
4532 goto failed_early;
4533 li = li->li_next;
4534 }
4535 }
4536
4537 if (defcount > 0)
4538 // Move varargs list to below missing default arguments.
4539 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4540 --ectx.ec_stack.ga_len;
4541 }
4542
4543 // Make space for omitted arguments, will store default value below.
4544 // Any varargs list goes after them.
4545 if (defcount > 0)
4546 for (idx = 0; idx < defcount; ++idx)
4547 {
4548 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4549 ++ectx.ec_stack.ga_len;
4550 }
4551 if (ufunc->uf_va_name != NULL)
4552 ++ectx.ec_stack.ga_len;
4553
4554 // Frame pointer points to just after arguments.
4555 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4556 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4557
4558 {
4559 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4560 + ufunc->uf_dfunc_idx;
4561 ufunc_T *base_ufunc = dfunc->df_ufunc;
4562
4563 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4564 // by copy_func().
4565 if (partial != NULL || base_ufunc->uf_partial != NULL)
4566 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004567 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4568 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004569 goto failed_early;
4570 if (partial != NULL)
4571 {
4572 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4573 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004574 if (current_ectx->ec_outer_ref != NULL
4575 && current_ectx->ec_outer_ref->or_outer != NULL)
4576 ectx.ec_outer_ref->or_outer =
4577 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004578 }
4579 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004580 {
4581 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4582 ++partial->pt_refcount;
4583 ectx.ec_outer_ref->or_partial = partial;
4584 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004585 }
4586 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004587 {
4588 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4589 ++base_ufunc->uf_partial->pt_refcount;
4590 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4591 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004592 }
4593 }
4594
4595 // dummy frame entries
4596 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4597 {
4598 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4599 ++ectx.ec_stack.ga_len;
4600 }
4601
4602 {
4603 // Reserve space for local variables and any closure reference count.
4604 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4605 + ufunc->uf_dfunc_idx;
4606
4607 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4608 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4609 ectx.ec_stack.ga_len += dfunc->df_varcount;
4610 if (dfunc->df_has_closure)
4611 {
4612 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4613 STACK_TV_VAR(idx)->vval.v_number = 0;
4614 ++ectx.ec_stack.ga_len;
4615 }
4616
4617 ectx.ec_instr = INSTRUCTIONS(dfunc);
4618 }
4619
4620 // Following errors are in the function, not the caller.
4621 // Commands behave like vim9script.
4622 estack_push_ufunc(ufunc, 1);
4623 current_sctx = ufunc->uf_script_ctx;
4624 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4625
4626 // Use a specific location for storing error messages to be converted to an
4627 // exception.
4628 saved_msg_list = msg_list;
4629 msg_list = &private_msg_list;
4630
4631 // Do turn errors into exceptions.
4632 suppress_errthrow = FALSE;
4633
4634 // Do not delete the function while executing it.
4635 ++ufunc->uf_calls;
4636
4637 // When ":silent!" was used before calling then we still abort the
4638 // function. If ":silent!" is used in the function then we don't.
4639 emsg_silent_def = emsg_silent;
4640 did_emsg_def = 0;
4641
4642 ectx.ec_where.wt_index = 0;
4643 ectx.ec_where.wt_variable = FALSE;
4644
4645 // Execute the instructions until done.
4646 ret = exec_instructions(&ectx);
4647 if (ret == OK)
4648 {
4649 // function finished, get result from the stack.
4650 if (ufunc->uf_ret_type == &t_void)
4651 {
4652 rettv->v_type = VAR_VOID;
4653 }
4654 else
4655 {
4656 tv = STACK_TV_BOT(-1);
4657 *rettv = *tv;
4658 tv->v_type = VAR_UNKNOWN;
4659 }
4660 }
4661
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004662 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004663 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4664 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004665
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004666 // Deal with any remaining closures, they may be in use somewhere.
4667 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004668 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004669 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004670 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4671 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004672
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004673 estack_pop();
4674 current_sctx = save_current_sctx;
4675
Bram Moolenaarc970e422021-03-17 15:03:04 +01004676 // TODO: when is it safe to delete the function if it is no longer used?
4677 --ufunc->uf_calls;
4678
Bram Moolenaar352134b2020-10-17 22:04:08 +02004679 if (*msg_list != NULL && saved_msg_list != NULL)
4680 {
4681 msglist_T **plist = saved_msg_list;
4682
4683 // Append entries from the current msg_list (uncaught exceptions) to
4684 // the saved msg_list.
4685 while (*plist != NULL)
4686 plist = &(*plist)->next;
4687
4688 *plist = *msg_list;
4689 }
4690 msg_list = saved_msg_list;
4691
Bram Moolenaar4c137212021-04-19 16:48:48 +02004692 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004693 {
4694 cmdmod.cmod_filter_regmatch.regprog = NULL;
4695 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004696 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004697 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004698 emsg_silent_def = save_emsg_silent_def;
4699 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004700
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004701failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004702 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4704 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004705 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004708 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004709 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004710 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004711 if (ectx.ec_outer_ref->or_outer_allocated)
4712 vim_free(ectx.ec_outer_ref->or_outer);
4713 partial_unref(ectx.ec_outer_ref->or_partial);
4714 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004715 }
4716
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004717 // Not sure if this is necessary.
4718 suppress_errthrow = save_suppress_errthrow;
4719
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004720 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004721 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004722 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004723 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 return ret;
4725}
4726
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004728 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4729 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4730 * "pfx" is prefixed to every line.
4731 */
4732 static void
4733list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4734{
4735 int line_idx = 0;
4736 int prev_current = 0;
4737 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004738 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004739
4740 for (current = 0; current < instr_count; ++current)
4741 {
4742 isn_T *iptr = &instr[current];
4743 char *line;
4744
4745 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004746 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004747 while (line_idx < iptr->isn_lnum
4748 && line_idx < ufunc->uf_lines.ga_len)
4749 {
4750 if (current > prev_current)
4751 {
4752 msg_puts("\n\n");
4753 prev_current = current;
4754 }
4755 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4756 if (line != NULL)
4757 msg(line);
4758 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004759 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4760 {
4761 int first_def_arg = ufunc->uf_args.ga_len
4762 - ufunc->uf_def_args.ga_len;
4763
4764 if (def_arg_idx > 0)
4765 msg_puts("\n\n");
4766 msg_start();
4767 msg_puts(" ");
4768 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4769 first_def_arg + def_arg_idx]);
4770 msg_puts(" = ");
4771 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4772 msg_clr_eos();
4773 msg_end();
4774 }
4775 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004776
4777 switch (iptr->isn_type)
4778 {
4779 case ISN_EXEC:
4780 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4781 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004782 case ISN_EXEC_SPLIT:
4783 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4784 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004785 case ISN_LEGACY_EVAL:
4786 smsg("%s%4d EVAL legacy %s", pfx, current,
4787 iptr->isn_arg.string);
4788 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004789 case ISN_REDIRSTART:
4790 smsg("%s%4d REDIR", pfx, current);
4791 break;
4792 case ISN_REDIREND:
4793 smsg("%s%4d REDIR END%s", pfx, current,
4794 iptr->isn_arg.number ? " append" : "");
4795 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004796 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004797#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004798 smsg("%s%4d CEXPR pre %s", pfx, current,
4799 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004800#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004801 break;
4802 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004803#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004804 {
4805 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4806
4807 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4808 cexpr_get_auname(cer->cer_cmdidx),
4809 cer->cer_forceit ? "!" : "",
4810 cer->cer_cmdline);
4811 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004812#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004813 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004814 case ISN_INSTR:
4815 {
4816 smsg("%s%4d INSTR", pfx, current);
4817 list_instructions(" ", iptr->isn_arg.instr,
4818 INT_MAX, NULL);
4819 msg(" -------------");
4820 }
4821 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004822 case ISN_SUBSTITUTE:
4823 {
4824 subs_T *subs = &iptr->isn_arg.subs;
4825
4826 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4827 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4828 msg(" -------------");
4829 }
4830 break;
4831 case ISN_EXECCONCAT:
4832 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4833 (varnumber_T)iptr->isn_arg.number);
4834 break;
4835 case ISN_ECHO:
4836 {
4837 echo_T *echo = &iptr->isn_arg.echo;
4838
4839 smsg("%s%4d %s %d", pfx, current,
4840 echo->echo_with_white ? "ECHO" : "ECHON",
4841 echo->echo_count);
4842 }
4843 break;
4844 case ISN_EXECUTE:
4845 smsg("%s%4d EXECUTE %lld", pfx, current,
4846 (varnumber_T)(iptr->isn_arg.number));
4847 break;
4848 case ISN_ECHOMSG:
4849 smsg("%s%4d ECHOMSG %lld", pfx, current,
4850 (varnumber_T)(iptr->isn_arg.number));
4851 break;
4852 case ISN_ECHOERR:
4853 smsg("%s%4d ECHOERR %lld", pfx, current,
4854 (varnumber_T)(iptr->isn_arg.number));
4855 break;
4856 case ISN_LOAD:
4857 {
4858 if (iptr->isn_arg.number < 0)
4859 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4860 (varnumber_T)(iptr->isn_arg.number
4861 + STACK_FRAME_SIZE));
4862 else
4863 smsg("%s%4d LOAD $%lld", pfx, current,
4864 (varnumber_T)(iptr->isn_arg.number));
4865 }
4866 break;
4867 case ISN_LOADOUTER:
4868 {
4869 if (iptr->isn_arg.number < 0)
4870 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4871 iptr->isn_arg.outer.outer_depth,
4872 iptr->isn_arg.outer.outer_idx
4873 + STACK_FRAME_SIZE);
4874 else
4875 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4876 iptr->isn_arg.outer.outer_depth,
4877 iptr->isn_arg.outer.outer_idx);
4878 }
4879 break;
4880 case ISN_LOADV:
4881 smsg("%s%4d LOADV v:%s", pfx, current,
4882 get_vim_var_name(iptr->isn_arg.number));
4883 break;
4884 case ISN_LOADSCRIPT:
4885 {
4886 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4887 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4888 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4889 + sref->sref_idx;
4890
4891 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4892 sv->sv_name,
4893 sref->sref_idx,
4894 si->sn_name);
4895 }
4896 break;
4897 case ISN_LOADS:
4898 {
4899 scriptitem_T *si = SCRIPT_ITEM(
4900 iptr->isn_arg.loadstore.ls_sid);
4901
4902 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4903 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4904 }
4905 break;
4906 case ISN_LOADAUTO:
4907 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4908 break;
4909 case ISN_LOADG:
4910 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4911 break;
4912 case ISN_LOADB:
4913 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4914 break;
4915 case ISN_LOADW:
4916 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4917 break;
4918 case ISN_LOADT:
4919 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4920 break;
4921 case ISN_LOADGDICT:
4922 smsg("%s%4d LOAD g:", pfx, current);
4923 break;
4924 case ISN_LOADBDICT:
4925 smsg("%s%4d LOAD b:", pfx, current);
4926 break;
4927 case ISN_LOADWDICT:
4928 smsg("%s%4d LOAD w:", pfx, current);
4929 break;
4930 case ISN_LOADTDICT:
4931 smsg("%s%4d LOAD t:", pfx, current);
4932 break;
4933 case ISN_LOADOPT:
4934 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4935 break;
4936 case ISN_LOADENV:
4937 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4938 break;
4939 case ISN_LOADREG:
4940 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4941 break;
4942
4943 case ISN_STORE:
4944 if (iptr->isn_arg.number < 0)
4945 smsg("%s%4d STORE arg[%lld]", pfx, current,
4946 iptr->isn_arg.number + STACK_FRAME_SIZE);
4947 else
4948 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4949 break;
4950 case ISN_STOREOUTER:
4951 {
4952 if (iptr->isn_arg.number < 0)
4953 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4954 iptr->isn_arg.outer.outer_depth,
4955 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4956 else
4957 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4958 iptr->isn_arg.outer.outer_depth,
4959 iptr->isn_arg.outer.outer_idx);
4960 }
4961 break;
4962 case ISN_STOREV:
4963 smsg("%s%4d STOREV v:%s", pfx, current,
4964 get_vim_var_name(iptr->isn_arg.number));
4965 break;
4966 case ISN_STOREAUTO:
4967 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4968 break;
4969 case ISN_STOREG:
4970 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4971 break;
4972 case ISN_STOREB:
4973 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4974 break;
4975 case ISN_STOREW:
4976 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4977 break;
4978 case ISN_STORET:
4979 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4980 break;
4981 case ISN_STORES:
4982 {
4983 scriptitem_T *si = SCRIPT_ITEM(
4984 iptr->isn_arg.loadstore.ls_sid);
4985
4986 smsg("%s%4d STORES %s in %s", pfx, current,
4987 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4988 }
4989 break;
4990 case ISN_STORESCRIPT:
4991 {
4992 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4993 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4994 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4995 + sref->sref_idx;
4996
4997 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4998 sv->sv_name,
4999 sref->sref_idx,
5000 si->sn_name);
5001 }
5002 break;
5003 case ISN_STOREOPT:
5004 smsg("%s%4d STOREOPT &%s", pfx, current,
5005 iptr->isn_arg.storeopt.so_name);
5006 break;
5007 case ISN_STOREENV:
5008 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5009 break;
5010 case ISN_STOREREG:
5011 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
5012 break;
5013 case ISN_STORENR:
5014 smsg("%s%4d STORE %lld in $%d", pfx, current,
5015 iptr->isn_arg.storenr.stnr_val,
5016 iptr->isn_arg.storenr.stnr_idx);
5017 break;
5018
5019 case ISN_STOREINDEX:
5020 smsg("%s%4d STOREINDEX %s", pfx, current,
5021 vartype_name(iptr->isn_arg.vartype));
5022 break;
5023
5024 case ISN_STORERANGE:
5025 smsg("%s%4d STORERANGE", pfx, current);
5026 break;
5027
5028 // constants
5029 case ISN_PUSHNR:
5030 smsg("%s%4d PUSHNR %lld", pfx, current,
5031 (varnumber_T)(iptr->isn_arg.number));
5032 break;
5033 case ISN_PUSHBOOL:
5034 case ISN_PUSHSPEC:
5035 smsg("%s%4d PUSH %s", pfx, current,
5036 get_var_special_name(iptr->isn_arg.number));
5037 break;
5038 case ISN_PUSHF:
5039#ifdef FEAT_FLOAT
5040 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5041#endif
5042 break;
5043 case ISN_PUSHS:
5044 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5045 break;
5046 case ISN_PUSHBLOB:
5047 {
5048 char_u *r;
5049 char_u numbuf[NUMBUFLEN];
5050 char_u *tofree;
5051
5052 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5053 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5054 vim_free(tofree);
5055 }
5056 break;
5057 case ISN_PUSHFUNC:
5058 {
5059 char *name = (char *)iptr->isn_arg.string;
5060
5061 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5062 name == NULL ? "[none]" : name);
5063 }
5064 break;
5065 case ISN_PUSHCHANNEL:
5066#ifdef FEAT_JOB_CHANNEL
5067 {
5068 channel_T *channel = iptr->isn_arg.channel;
5069
5070 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5071 channel == NULL ? 0 : channel->ch_id);
5072 }
5073#endif
5074 break;
5075 case ISN_PUSHJOB:
5076#ifdef FEAT_JOB_CHANNEL
5077 {
5078 typval_T tv;
5079 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005080 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005081
5082 tv.v_type = VAR_JOB;
5083 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005084 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005085 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5086 }
5087#endif
5088 break;
5089 case ISN_PUSHEXC:
5090 smsg("%s%4d PUSH v:exception", pfx, current);
5091 break;
5092 case ISN_UNLET:
5093 smsg("%s%4d UNLET%s %s", pfx, current,
5094 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5095 iptr->isn_arg.unlet.ul_name);
5096 break;
5097 case ISN_UNLETENV:
5098 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5099 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5100 iptr->isn_arg.unlet.ul_name);
5101 break;
5102 case ISN_UNLETINDEX:
5103 smsg("%s%4d UNLETINDEX", pfx, current);
5104 break;
5105 case ISN_UNLETRANGE:
5106 smsg("%s%4d UNLETRANGE", pfx, current);
5107 break;
5108 case ISN_LOCKCONST:
5109 smsg("%s%4d LOCKCONST", pfx, current);
5110 break;
5111 case ISN_NEWLIST:
5112 smsg("%s%4d NEWLIST size %lld", pfx, current,
5113 (varnumber_T)(iptr->isn_arg.number));
5114 break;
5115 case ISN_NEWDICT:
5116 smsg("%s%4d NEWDICT size %lld", pfx, current,
5117 (varnumber_T)(iptr->isn_arg.number));
5118 break;
5119
5120 // function call
5121 case ISN_BCALL:
5122 {
5123 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5124
5125 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5126 internal_func_name(cbfunc->cbf_idx),
5127 cbfunc->cbf_argcount);
5128 }
5129 break;
5130 case ISN_DCALL:
5131 {
5132 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5133 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5134 + cdfunc->cdf_idx;
5135
5136 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5137 df->df_ufunc->uf_name_exp != NULL
5138 ? df->df_ufunc->uf_name_exp
5139 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5140 }
5141 break;
5142 case ISN_UCALL:
5143 {
5144 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5145
5146 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5147 cufunc->cuf_name, cufunc->cuf_argcount);
5148 }
5149 break;
5150 case ISN_PCALL:
5151 {
5152 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5153
5154 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5155 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5156 }
5157 break;
5158 case ISN_PCALL_END:
5159 smsg("%s%4d PCALL end", pfx, current);
5160 break;
5161 case ISN_RETURN:
5162 smsg("%s%4d RETURN", pfx, current);
5163 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005164 case ISN_RETURN_VOID:
5165 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005166 break;
5167 case ISN_FUNCREF:
5168 {
5169 funcref_T *funcref = &iptr->isn_arg.funcref;
5170 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5171 + funcref->fr_func;
5172
5173 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5174 }
5175 break;
5176
5177 case ISN_NEWFUNC:
5178 {
5179 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5180
5181 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5182 newfunc->nf_lambda, newfunc->nf_global);
5183 }
5184 break;
5185
5186 case ISN_DEF:
5187 {
5188 char_u *name = iptr->isn_arg.string;
5189
5190 smsg("%s%4d DEF %s", pfx, current,
5191 name == NULL ? (char_u *)"" : name);
5192 }
5193 break;
5194
5195 case ISN_JUMP:
5196 {
5197 char *when = "?";
5198
5199 switch (iptr->isn_arg.jump.jump_when)
5200 {
5201 case JUMP_ALWAYS:
5202 when = "JUMP";
5203 break;
5204 case JUMP_AND_KEEP_IF_TRUE:
5205 when = "JUMP_AND_KEEP_IF_TRUE";
5206 break;
5207 case JUMP_IF_FALSE:
5208 when = "JUMP_IF_FALSE";
5209 break;
5210 case JUMP_AND_KEEP_IF_FALSE:
5211 when = "JUMP_AND_KEEP_IF_FALSE";
5212 break;
5213 case JUMP_IF_COND_FALSE:
5214 when = "JUMP_IF_COND_FALSE";
5215 break;
5216 case JUMP_IF_COND_TRUE:
5217 when = "JUMP_IF_COND_TRUE";
5218 break;
5219 }
5220 smsg("%s%4d %s -> %d", pfx, current, when,
5221 iptr->isn_arg.jump.jump_where);
5222 }
5223 break;
5224
5225 case ISN_JUMP_IF_ARG_SET:
5226 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5227 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5228 iptr->isn_arg.jump.jump_where);
5229 break;
5230
5231 case ISN_FOR:
5232 {
5233 forloop_T *forloop = &iptr->isn_arg.forloop;
5234
5235 smsg("%s%4d FOR $%d -> %d", pfx, current,
5236 forloop->for_idx, forloop->for_end);
5237 }
5238 break;
5239
5240 case ISN_TRY:
5241 {
5242 try_T *try = &iptr->isn_arg.try;
5243
5244 if (try->try_ref->try_finally == 0)
5245 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5246 pfx, current,
5247 try->try_ref->try_catch,
5248 try->try_ref->try_endtry);
5249 else
5250 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5251 pfx, current,
5252 try->try_ref->try_catch,
5253 try->try_ref->try_finally,
5254 try->try_ref->try_endtry);
5255 }
5256 break;
5257 case ISN_CATCH:
5258 // TODO
5259 smsg("%s%4d CATCH", pfx, current);
5260 break;
5261 case ISN_TRYCONT:
5262 {
5263 trycont_T *trycont = &iptr->isn_arg.trycont;
5264
5265 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5266 trycont->tct_levels,
5267 trycont->tct_levels == 1 ? "" : "s",
5268 trycont->tct_where);
5269 }
5270 break;
5271 case ISN_FINALLY:
5272 smsg("%s%4d FINALLY", pfx, current);
5273 break;
5274 case ISN_ENDTRY:
5275 smsg("%s%4d ENDTRY", pfx, current);
5276 break;
5277 case ISN_THROW:
5278 smsg("%s%4d THROW", pfx, current);
5279 break;
5280
5281 // expression operations on number
5282 case ISN_OPNR:
5283 case ISN_OPFLOAT:
5284 case ISN_OPANY:
5285 {
5286 char *what;
5287 char *ins;
5288
5289 switch (iptr->isn_arg.op.op_type)
5290 {
5291 case EXPR_MULT: what = "*"; break;
5292 case EXPR_DIV: what = "/"; break;
5293 case EXPR_REM: what = "%"; break;
5294 case EXPR_SUB: what = "-"; break;
5295 case EXPR_ADD: what = "+"; break;
5296 default: what = "???"; break;
5297 }
5298 switch (iptr->isn_type)
5299 {
5300 case ISN_OPNR: ins = "OPNR"; break;
5301 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5302 case ISN_OPANY: ins = "OPANY"; break;
5303 default: ins = "???"; break;
5304 }
5305 smsg("%s%4d %s %s", pfx, current, ins, what);
5306 }
5307 break;
5308
5309 case ISN_COMPAREBOOL:
5310 case ISN_COMPARESPECIAL:
5311 case ISN_COMPARENR:
5312 case ISN_COMPAREFLOAT:
5313 case ISN_COMPARESTRING:
5314 case ISN_COMPAREBLOB:
5315 case ISN_COMPARELIST:
5316 case ISN_COMPAREDICT:
5317 case ISN_COMPAREFUNC:
5318 case ISN_COMPAREANY:
5319 {
5320 char *p;
5321 char buf[10];
5322 char *type;
5323
5324 switch (iptr->isn_arg.op.op_type)
5325 {
5326 case EXPR_EQUAL: p = "=="; break;
5327 case EXPR_NEQUAL: p = "!="; break;
5328 case EXPR_GREATER: p = ">"; break;
5329 case EXPR_GEQUAL: p = ">="; break;
5330 case EXPR_SMALLER: p = "<"; break;
5331 case EXPR_SEQUAL: p = "<="; break;
5332 case EXPR_MATCH: p = "=~"; break;
5333 case EXPR_IS: p = "is"; break;
5334 case EXPR_ISNOT: p = "isnot"; break;
5335 case EXPR_NOMATCH: p = "!~"; break;
5336 default: p = "???"; break;
5337 }
5338 STRCPY(buf, p);
5339 if (iptr->isn_arg.op.op_ic == TRUE)
5340 strcat(buf, "?");
5341 switch(iptr->isn_type)
5342 {
5343 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5344 case ISN_COMPARESPECIAL:
5345 type = "COMPARESPECIAL"; break;
5346 case ISN_COMPARENR: type = "COMPARENR"; break;
5347 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5348 case ISN_COMPARESTRING:
5349 type = "COMPARESTRING"; break;
5350 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5351 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5352 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5353 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5354 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5355 default: type = "???"; break;
5356 }
5357
5358 smsg("%s%4d %s %s", pfx, current, type, buf);
5359 }
5360 break;
5361
5362 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5363 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5364
5365 // expression operations
5366 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5367 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5368 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5369 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5370 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5371 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5372 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5373 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5374 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5375 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5376 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5377 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5378 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005379 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5380 iptr->isn_arg.getitem.gi_index,
5381 iptr->isn_arg.getitem.gi_with_op ?
5382 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005383 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5384 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5385 iptr->isn_arg.string); break;
5386 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5387
5388 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5389 case ISN_CHECKTYPE:
5390 {
5391 checktype_T *ct = &iptr->isn_arg.type;
5392 char *tofree;
5393
5394 if (ct->ct_arg_idx == 0)
5395 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5396 type_name(ct->ct_type, &tofree),
5397 (int)ct->ct_off);
5398 else
5399 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5400 type_name(ct->ct_type, &tofree),
5401 (int)ct->ct_off,
5402 (int)ct->ct_arg_idx);
5403 vim_free(tofree);
5404 break;
5405 }
5406 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5407 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5408 iptr->isn_arg.checklen.cl_min_len);
5409 break;
5410 case ISN_SETTYPE:
5411 {
5412 char *tofree;
5413
5414 smsg("%s%4d SETTYPE %s", pfx, current,
5415 type_name(iptr->isn_arg.type.ct_type, &tofree));
5416 vim_free(tofree);
5417 break;
5418 }
5419 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005420 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5421 smsg("%s%4d INVERT %d (!val)", pfx, current,
5422 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005423 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005424 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5425 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005426 break;
5427 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005428 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005429 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005430 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5431 pfx, current,
5432 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005433 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005434 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5435 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005436 break;
5437 case ISN_PUT:
5438 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5439 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005440 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005441 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5442 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005443 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005444 else
5445 smsg("%s%4d PUT %c %ld", pfx, current,
5446 iptr->isn_arg.put.put_regname,
5447 (long)iptr->isn_arg.put.put_lnum);
5448 break;
5449
5450 // TODO: summarize modifiers
5451 case ISN_CMDMOD:
5452 {
5453 char_u *buf;
5454 size_t len = produce_cmdmods(
5455 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5456
5457 buf = alloc(len + 1);
5458 if (buf != NULL)
5459 {
5460 (void)produce_cmdmods(
5461 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5462 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5463 vim_free(buf);
5464 }
5465 break;
5466 }
5467 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5468
5469 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005470 smsg("%s%4d PROFILE START line %d", pfx, current,
5471 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005472 break;
5473
5474 case ISN_PROF_END:
5475 smsg("%s%4d PROFILE END", pfx, current);
5476 break;
5477
Bram Moolenaare99d4222021-06-13 14:01:26 +02005478 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005479 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5480 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005481 break;
5482
Bram Moolenaar4c137212021-04-19 16:48:48 +02005483 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5484 iptr->isn_arg.unpack.unp_count,
5485 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5486 break;
5487 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5488 iptr->isn_arg.shuffle.shfl_item,
5489 iptr->isn_arg.shuffle.shfl_up);
5490 break;
5491 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5492
5493 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5494 return;
5495 }
5496
5497 out_flush(); // output one line at a time
5498 ui_breakcheck();
5499 if (got_int)
5500 break;
5501 }
5502}
5503
5504/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005505 * Handle command line completion for the :disassemble command.
5506 */
5507 void
5508set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5509{
5510 char_u *p;
5511
5512 // Default: expand user functions, "debug" and "profile"
5513 xp->xp_context = EXPAND_DISASSEMBLE;
5514 xp->xp_pattern = arg;
5515
5516 // first argument already typed: only user function names
5517 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5518 {
5519 xp->xp_context = EXPAND_USER_FUNC;
5520 xp->xp_pattern = skipwhite(p);
5521 }
5522}
5523
5524/*
5525 * Function given to ExpandGeneric() to obtain the list of :disassemble
5526 * arguments.
5527 */
5528 char_u *
5529get_disassemble_argument(expand_T *xp, int idx)
5530{
5531 if (idx == 0)
5532 return (char_u *)"debug";
5533 if (idx == 1)
5534 return (char_u *)"profile";
5535 return get_user_func_name(xp, idx - 2);
5536}
5537
5538/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005539 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005540 * We don't really need this at runtime, but we do have tests that require it,
5541 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542 */
5543 void
5544ex_disassemble(exarg_T *eap)
5545{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005546 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005547 char_u *fname;
5548 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005549 dfunc_T *dfunc;
5550 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005551 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005552 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005553 compiletype_T compile_type = CT_NONE;
5554
5555 if (STRNCMP(arg, "profile", 7) == 0)
5556 {
5557 compile_type = CT_PROFILE;
5558 arg = skipwhite(arg + 7);
5559 }
5560 else if (STRNCMP(arg, "debug", 5) == 0)
5561 {
5562 compile_type = CT_DEBUG;
5563 arg = skipwhite(arg + 5);
5564 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005566 if (STRNCMP(arg, "<lambda>", 8) == 0)
5567 {
5568 arg += 8;
5569 (void)getdigits(&arg);
5570 fname = vim_strnsave(eap->arg, arg - eap->arg);
5571 }
5572 else
5573 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005574 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005575 if (fname == NULL)
5576 {
5577 semsg(_(e_invarg2), eap->arg);
5578 return;
5579 }
5580
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005581 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005582 if (ufunc == NULL)
5583 {
5584 char_u *p = untrans_function_name(fname);
5585
5586 if (p != NULL)
5587 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005588 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005589 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005590 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005591 if (ufunc == NULL)
5592 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005593 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005594 return;
5595 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005596 if (func_needs_compiling(ufunc, compile_type)
5597 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005598 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005599 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005600 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005601 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602 return;
5603 }
5604 if (ufunc->uf_name_exp != NULL)
5605 msg((char *)ufunc->uf_name_exp);
5606 else
5607 msg((char *)ufunc->uf_name);
5608
5609 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005610 switch (compile_type)
5611 {
5612 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005613#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005614 instr = dfunc->df_instr_prof;
5615 instr_count = dfunc->df_instr_prof_count;
5616 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005617#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005618 // FALLTHROUGH
5619 case CT_NONE:
5620 instr = dfunc->df_instr;
5621 instr_count = dfunc->df_instr_count;
5622 break;
5623 case CT_DEBUG:
5624 instr = dfunc->df_instr_debug;
5625 instr_count = dfunc->df_instr_debug_count;
5626 break;
5627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005628
Bram Moolenaar4c137212021-04-19 16:48:48 +02005629 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630}
5631
5632/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005633 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634 * list, etc. Mostly like what JavaScript does, except that empty list and
5635 * empty dictionary are FALSE.
5636 */
5637 int
5638tv2bool(typval_T *tv)
5639{
5640 switch (tv->v_type)
5641 {
5642 case VAR_NUMBER:
5643 return tv->vval.v_number != 0;
5644 case VAR_FLOAT:
5645#ifdef FEAT_FLOAT
5646 return tv->vval.v_float != 0.0;
5647#else
5648 break;
5649#endif
5650 case VAR_PARTIAL:
5651 return tv->vval.v_partial != NULL;
5652 case VAR_FUNC:
5653 case VAR_STRING:
5654 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5655 case VAR_LIST:
5656 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5657 case VAR_DICT:
5658 return tv->vval.v_dict != NULL
5659 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5660 case VAR_BOOL:
5661 case VAR_SPECIAL:
5662 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5663 case VAR_JOB:
5664#ifdef FEAT_JOB_CHANNEL
5665 return tv->vval.v_job != NULL;
5666#else
5667 break;
5668#endif
5669 case VAR_CHANNEL:
5670#ifdef FEAT_JOB_CHANNEL
5671 return tv->vval.v_channel != NULL;
5672#else
5673 break;
5674#endif
5675 case VAR_BLOB:
5676 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5677 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005678 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005679 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005680 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 break;
5682 }
5683 return FALSE;
5684}
5685
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005686 void
5687emsg_using_string_as(typval_T *tv, int as_number)
5688{
5689 semsg(_(as_number ? e_using_string_as_number_str
5690 : e_using_string_as_bool_str),
5691 tv->vval.v_string == NULL
5692 ? (char_u *)"" : tv->vval.v_string);
5693}
5694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695/*
5696 * If "tv" is a string give an error and return FAIL.
5697 */
5698 int
5699check_not_string(typval_T *tv)
5700{
5701 if (tv->v_type == VAR_STRING)
5702 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005703 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704 clear_tv(tv);
5705 return FAIL;
5706 }
5707 return OK;
5708}
5709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005710
5711#endif // FEAT_EVAL