blob: 8b817e4505f4d4fdd48d807abc3fe29556b90ba2 [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 Moolenaarb69c6fb2021-06-14 20:40:37 +0200232 // When debugging and using "cont" switches to the not-debugged
233 // instructions, may need to still compile them.
234 if ((func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
235 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
236 == FAIL)
237 || INSTRUCTIONS(dfunc) == NULL)
238 {
239 if (did_emsg_cumul + did_emsg == did_emsg_before)
240 semsg(_(e_function_is_not_compiled_str),
241 printable_func_name(ufunc));
242 return FAIL;
243 }
244
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200245 if (ufunc->uf_va_name != NULL)
246 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200247 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200248 // Stack at time of call with 2 varargs:
249 // normal_arg
250 // optional_arg
251 // vararg_1
252 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200253 // After creating the list:
254 // normal_arg
255 // optional_arg
256 // vararg-list
257 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200258 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200259 // After creating the list
260 // normal_arg
261 // (space for optional_arg)
262 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200263 vararg_count = argcount - ufunc->uf_args.ga_len;
264 if (vararg_count < 0)
265 vararg_count = 0;
266 else
267 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200268 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200269 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200270
271 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200272 }
273
Bram Moolenaarfe270812020-04-11 22:31:27 +0200274 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200275 if (arg_to_add < 0)
276 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200277 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200278 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200279 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200280 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200281 return FAIL;
282 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200283
284 // Reserve space for:
285 // - missing arguments
286 // - stack frame
287 // - local variables
288 // - if needed: a counter for number of closures created in
289 // ectx->ec_funcrefs.
290 varcount = dfunc->df_varcount + dfunc->df_has_closure;
291 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
292 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 return FAIL;
294
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100295 // If depth of calling is getting too high, don't execute the function.
296 if (funcdepth_increment() == FAIL)
297 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200298 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100299
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100300 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200301 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100302 {
303 floc = ALLOC_ONE(funclocal_T);
304 if (floc == NULL)
305 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200306 *floc = ectx->ec_funclocal;
307 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100308 }
309
Bram Moolenaarfe270812020-04-11 22:31:27 +0200310 // Move the vararg-list to below the missing optional arguments.
311 if (vararg_count > 0 && arg_to_add > 0)
312 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100313
314 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200315 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200316 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200317 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318
319 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100320 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
321 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200322 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200323 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
324 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100325 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100326 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200327 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328
329 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200330 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200332 if (dfunc->df_has_closure)
333 {
334 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
335
336 tv->v_type = VAR_NUMBER;
337 tv->vval.v_number = 0;
338 }
339 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100341 if (pt != NULL || ufunc->uf_partial != NULL
342 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100343 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200344 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100345
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200346 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100347 return FAIL;
348 if (pt != NULL)
349 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200350 ref->or_outer = &pt->pt_outer;
351 ++pt->pt_refcount;
352 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100353 }
354 else if (ufunc->uf_partial != NULL)
355 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200356 ref->or_outer = &ufunc->uf_partial->pt_outer;
357 ++ufunc->uf_partial->pt_refcount;
358 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100359 }
360 else
361 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200362 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
363 if (ref->or_outer == NULL)
364 {
365 vim_free(ref);
366 return FAIL;
367 }
368 ref->or_outer_allocated = TRUE;
369 ref->or_outer->out_stack = &ectx->ec_stack;
370 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
371 if (ectx->ec_outer_ref != NULL)
372 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100373 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200374 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100375 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100376 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200377 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100378
Bram Moolenaarc970e422021-03-17 15:03:04 +0100379 ++ufunc->uf_calls;
380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100381 // Set execution state to the start of the called function.
382 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100383 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100384 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200385 if (entry != NULL)
386 {
387 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200388 // Save the current context so it can be restored on return.
389 entry->es_save_sctx = current_sctx;
390 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200391 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100392
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200393 // Start execution at the first instruction.
394 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395
396 return OK;
397}
398
399// Get pointer to item in the stack.
400#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
401
402/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 * Used when returning from a function: Check if any closure is still
404 * referenced. If so then move the arguments and variables to a separate piece
405 * of stack to be used when the closure is called.
406 * When "free_arguments" is TRUE the arguments are to be freed.
407 * Returns FAIL when out of memory.
408 */
409 static int
410handle_closure_in_use(ectx_T *ectx, int free_arguments)
411{
412 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
413 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200414 int argcount;
415 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200416 int idx;
417 typval_T *tv;
418 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200419 garray_T *gap = &ectx->ec_funcrefs;
420 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200421
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200422 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200423 return OK; // function was freed
424 if (dfunc->df_has_closure == 0)
425 return OK; // no closures
426 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
427 closure_count = tv->vval.v_number;
428 if (closure_count == 0)
429 return OK; // no funcrefs created
430
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200431 argcount = ufunc_argcount(dfunc->df_ufunc);
432 top = ectx->ec_frame_idx - argcount;
433
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200434 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200435 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200436 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200437 partial_T *pt;
438 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200439
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200440 if (off < 0)
441 continue; // count is off or already done
442 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200443 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200444 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200445 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200446 int i;
447
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200448 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200449 // unreferenced on return.
450 for (i = 0; i < dfunc->df_varcount; ++i)
451 {
452 typval_T *stv = STACK_TV(ectx->ec_frame_idx
453 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200454 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200455 --refcount;
456 }
457 if (refcount > 1)
458 {
459 closure_in_use = TRUE;
460 break;
461 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200462 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200463 }
464
465 if (closure_in_use)
466 {
467 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
468 typval_T *stack;
469
470 // A closure is using the arguments and/or local variables.
471 // Move them to the called function.
472 if (funcstack == NULL)
473 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200474 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
475 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200476 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
477 funcstack->fs_ga.ga_data = stack;
478 if (stack == NULL)
479 {
480 vim_free(funcstack);
481 return FAIL;
482 }
483
484 // Move or copy the arguments.
485 for (idx = 0; idx < argcount; ++idx)
486 {
487 tv = STACK_TV(top + idx);
488 if (free_arguments)
489 {
490 *(stack + idx) = *tv;
491 tv->v_type = VAR_UNKNOWN;
492 }
493 else
494 copy_tv(tv, stack + idx);
495 }
496 // Move the local variables.
497 for (idx = 0; idx < dfunc->df_varcount; ++idx)
498 {
499 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200500
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200501 // A partial created for a local function, that is also used as a
502 // local variable, has a reference count for the variable, thus
503 // will never go down to zero. When all these refcounts are one
504 // then the funcstack is unused. We need to count how many we have
505 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200506 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
507 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200508 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200509
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200510 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200511 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
512 gap->ga_len - closure_count + i])
513 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200514 }
515
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200516 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200517 tv->v_type = VAR_UNKNOWN;
518 }
519
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200520 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200521 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200522 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
523 - closure_count + idx];
524 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200525 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200526 ++funcstack->fs_refcount;
527 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100528 pt->pt_outer.out_stack = &funcstack->fs_ga;
529 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 }
531 }
532 }
533
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534 for (idx = 0; idx < closure_count; ++idx)
535 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
536 - closure_count + idx]);
537 gap->ga_len -= closure_count;
538 if (gap->ga_len == 0)
539 ga_clear(gap);
540
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200541 return OK;
542}
543
544/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200545 * Called when a partial is freed or its reference count goes down to one. The
546 * funcstack may be the only reference to the partials in the local variables.
547 * Go over all of them, the funcref and can be freed if all partials
548 * referencing the funcstack have a reference count of one.
549 */
550 void
551funcstack_check_refcount(funcstack_T *funcstack)
552{
553 int i;
554 garray_T *gap = &funcstack->fs_ga;
555 int done = 0;
556
557 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
558 return;
559 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
560 {
561 typval_T *tv = ((typval_T *)gap->ga_data) + i;
562
563 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
564 && tv->vval.v_partial->pt_funcstack == funcstack
565 && tv->vval.v_partial->pt_refcount == 1)
566 ++done;
567 }
568 if (done == funcstack->fs_min_refcount)
569 {
570 typval_T *stack = gap->ga_data;
571
572 // All partials referencing the funcstack have a reference count of
573 // one, thus the funcstack is no longer of use.
574 for (i = 0; i < gap->ga_len; ++i)
575 clear_tv(stack + i);
576 vim_free(stack);
577 vim_free(funcstack);
578 }
579}
580
581/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 * Return from the current function.
583 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200584 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200585func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100588 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200589 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
590 + ectx->ec_dfunc_idx;
591 int argcount = ufunc_argcount(dfunc->df_ufunc);
592 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200593 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100594 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
595 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200596 funclocal_T *floc;
597#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100598 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
599 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600
Bram Moolenaar12d26532021-02-19 19:13:21 +0100601 if (do_profiling == PROF_YES)
602 {
603 ufunc_T *caller = prev_dfunc->df_ufunc;
604
605 if (dfunc->df_ufunc->uf_profiling
606 || (caller != NULL && caller->uf_profiling))
607 {
608 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
609 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
610 --profile_info_ga.ga_len;
611 }
612 }
613#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100614 // TODO: when is it safe to delete the function when it is no longer used?
615 --dfunc->df_ufunc->uf_calls;
616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200618 entry = estack_pop();
619 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200620 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622 if (handle_closure_in_use(ectx, TRUE) == FAIL)
623 return FAIL;
624
625 // Clear the arguments.
626 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
627 clear_tv(STACK_TV(idx));
628
629 // Clear local variables and temp values, but not the return value.
630 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100631 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100633
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100634 // The return value should be on top of the stack. However, when aborting
635 // it may not be there and ec_frame_idx is the top of the stack.
636 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100637 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100638 ret_idx = 0;
639
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200640 if (ectx->ec_outer_ref != NULL)
641 {
642 if (ectx->ec_outer_ref->or_outer_allocated)
643 vim_free(ectx->ec_outer_ref->or_outer);
644 partial_unref(ectx->ec_outer_ref->or_partial);
645 vim_free(ectx->ec_outer_ref);
646 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100647
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100648 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100649 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100650 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
651 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200652 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
653 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200654 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100655 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100656 floc = (void *)STACK_TV(ectx->ec_frame_idx
657 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200658 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100659 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
660 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200661
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100662 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200663 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100664 else
665 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200666 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100667 vim_free(floc);
668 }
669
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100670 if (ret_idx > 0)
671 {
672 // Reset the stack to the position before the call, with a spot for the
673 // return value, moved there from above the frame.
674 ectx->ec_stack.ga_len = top + 1;
675 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
676 }
677 else
678 // Reset the stack to the position before the call.
679 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200680
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100681 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200682 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200683 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684}
685
686#undef STACK_TV
687
688/*
689 * Prepare arguments and rettv for calling a builtin or user function.
690 */
691 static int
692call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
693{
694 int idx;
695 typval_T *tv;
696
697 // Move arguments from bottom of the stack to argvars[] and add terminator.
698 for (idx = 0; idx < argcount; ++idx)
699 argvars[idx] = *STACK_TV_BOT(idx - argcount);
700 argvars[argcount].v_type = VAR_UNKNOWN;
701
702 // Result replaces the arguments on the stack.
703 if (argcount > 0)
704 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200705 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 return FAIL;
707 else
708 ++ectx->ec_stack.ga_len;
709
710 // Default return value is zero.
711 tv = STACK_TV_BOT(-1);
712 tv->v_type = VAR_NUMBER;
713 tv->vval.v_number = 0;
714
715 return OK;
716}
717
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200718// Ugly global to avoid passing the execution context around through many
719// layers.
720static ectx_T *current_ectx = NULL;
721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722/*
723 * Call a builtin function by index.
724 */
725 static int
726call_bfunc(int func_idx, int argcount, ectx_T *ectx)
727{
728 typval_T argvars[MAX_FUNC_ARGS];
729 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100730 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200731 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732
733 if (call_prepare(argcount, argvars, ectx) == FAIL)
734 return FAIL;
735
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200736 // Call the builtin function. Set "current_ectx" so that when it
737 // recursively invokes call_def_function() a closure context can be set.
738 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200740 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741
742 // Clear the arguments.
743 for (idx = 0; idx < argcount; ++idx)
744 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200745
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100746 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200747 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 return OK;
749}
750
751/*
752 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100753 * If the function is compiled this will add a stack frame and set the
754 * instruction pointer at the start of the function.
755 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200756 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100757 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 */
759 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100760call_ufunc(
761 ufunc_T *ufunc,
762 partial_T *pt,
763 int argcount,
764 ectx_T *ectx,
765 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766{
767 typval_T argvars[MAX_FUNC_ARGS];
768 funcexe_T funcexe;
769 int error;
770 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100771 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200772 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773
Bram Moolenaare99d4222021-06-13 14:01:26 +0200774 if (func_needs_compiling(ufunc, compile_type)
775 && compile_def_function(ufunc, FALSE, compile_type, NULL)
776 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200777 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200778 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100779 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100780 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100781 if (error != FCERR_UNKNOWN)
782 {
783 if (error == FCERR_TOOMANY)
784 semsg(_(e_toomanyarg), ufunc->uf_name);
785 else
786 semsg(_(e_toofewarg), ufunc->uf_name);
787 return FAIL;
788 }
789
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100790 // The function has been compiled, can call it quickly. For a function
791 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100792 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100793 if (iptr != NULL)
794 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100795 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100796 iptr->isn_type = ISN_DCALL;
797 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
798 iptr->isn_arg.dfunc.cdf_argcount = argcount;
799 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200800 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802
803 if (call_prepare(argcount, argvars, ectx) == FAIL)
804 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200805 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 funcexe.evaluate = TRUE;
807
808 // Call the user function. Result goes in last position on the stack.
809 // TODO: add selfdict if there is one
810 error = call_user_func_check(ufunc, argcount, argvars,
811 STACK_TV_BOT(-1), &funcexe, NULL);
812
813 // Clear the arguments.
814 for (idx = 0; idx < argcount; ++idx)
815 clear_tv(&argvars[idx]);
816
817 if (error != FCERR_NONE)
818 {
819 user_func_error(error, ufunc->uf_name);
820 return FAIL;
821 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100822 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200823 // Error other than from calling the function itself.
824 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 return OK;
826}
827
828/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100829 * If command modifiers were applied restore them.
830 */
831 static void
832may_restore_cmdmod(funclocal_T *funclocal)
833{
834 if (funclocal->floc_restore_cmdmod)
835 {
836 cmdmod.cmod_filter_regmatch.regprog = NULL;
837 undo_cmdmod(&cmdmod);
838 cmdmod = funclocal->floc_save_cmdmod;
839 funclocal->floc_restore_cmdmod = FALSE;
840 }
841}
842
843/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200844 * Return TRUE if an error was given or CTRL-C was pressed.
845 */
846 static int
847vim9_aborting(int prev_called_emsg)
848{
849 return called_emsg > prev_called_emsg || got_int || did_throw;
850}
851
852/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 * Execute a function by "name".
854 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100855 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 * Returns FAIL if not found without an error message.
857 */
858 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100859call_by_name(
860 char_u *name,
861 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100862 ectx_T *ectx,
863 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864{
865 ufunc_T *ufunc;
866
867 if (builtin_function(name, -1))
868 {
869 int func_idx = find_internal_func(name);
870
871 if (func_idx < 0)
872 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200873 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 return FAIL;
875 return call_bfunc(func_idx, argcount, ectx);
876 }
877
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200878 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200879
880 if (ufunc == NULL)
881 {
882 int called_emsg_before = called_emsg;
883
884 if (script_autoload(name, TRUE))
885 // loaded a package, search for the function again
886 ufunc = find_func(name, FALSE, NULL);
887 if (vim9_aborting(called_emsg_before))
888 return FAIL; // bail out if loading the script caused an error
889 }
890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100892 {
893 if (ufunc->uf_arg_types != NULL)
894 {
895 int i;
896 typval_T *argv = STACK_TV_BOT(0) - argcount;
897
898 // The function can change at runtime, check that the argument
899 // types are correct.
900 for (i = 0; i < argcount; ++i)
901 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100902 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100903
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100904 if (i < ufunc->uf_args.ga_len)
905 type = ufunc->uf_arg_types[i];
906 else if (ufunc->uf_va_type != NULL)
907 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100908 if (type != NULL && check_typval_arg_type(type,
909 &argv[i], i + 1) == FAIL)
910 return FAIL;
911 }
912 }
913
Bram Moolenaar4c137212021-04-19 16:48:48 +0200914 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916
917 return FAIL;
918}
919
920 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100921call_partial(
922 typval_T *tv,
923 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100924 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200926 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200927 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100929 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930
931 if (tv->v_type == VAR_PARTIAL)
932 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200933 partial_T *pt = tv->vval.v_partial;
934 int i;
935
936 if (pt->pt_argc > 0)
937 {
938 // Make space for arguments from the partial, shift the "argcount"
939 // arguments up.
940 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
941 return FAIL;
942 for (i = 1; i <= argcount; ++i)
943 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
944 ectx->ec_stack.ga_len += pt->pt_argc;
945 argcount += pt->pt_argc;
946
947 // copy the arguments from the partial onto the stack
948 for (i = 0; i < pt->pt_argc; ++i)
949 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951
952 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200953 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 name = pt->pt_name;
956 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200957 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200959 if (name != NULL)
960 {
961 char_u fname_buf[FLEN_FIXED + 1];
962 char_u *tofree = NULL;
963 int error = FCERR_NONE;
964 char_u *fname;
965
966 // May need to translate <SNR>123_ to K_SNR.
967 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
968 if (error != FCERR_NONE)
969 res = FAIL;
970 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200971 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200972 vim_free(tofree);
973 }
974
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100975 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 {
977 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200978 semsg(_(e_unknownfunc),
979 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 return FAIL;
981 }
982 return OK;
983}
984
985/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200986 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
987 * TRUE.
988 */
989 static int
990error_if_locked(int lock, char *error)
991{
992 if (lock & (VAR_LOCKED | VAR_FIXED))
993 {
994 emsg(_(error));
995 return TRUE;
996 }
997 return FALSE;
998}
999
1000/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001001 * Give an error if "tv" is not a number and return FAIL.
1002 */
1003 static int
1004check_for_number(typval_T *tv)
1005{
1006 if (tv->v_type != VAR_NUMBER)
1007 {
1008 semsg(_(e_expected_str_but_got_str),
1009 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1010 return FAIL;
1011 }
1012 return OK;
1013}
1014
1015/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001016 * Store "tv" in variable "name".
1017 * This is for s: and g: variables.
1018 */
1019 static void
1020store_var(char_u *name, typval_T *tv)
1021{
1022 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001023 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001024
Bram Moolenaard877a572021-04-01 19:42:48 +02001025 if (tv->v_lock)
1026 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001027 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001028 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001029 restore_funccal();
1030}
1031
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001032/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001033 * Convert "tv" to a string.
1034 * Return FAIL if not allowed.
1035 */
1036 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001037do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001038{
1039 if (tv->v_type != VAR_STRING)
1040 {
1041 char_u *str;
1042
1043 if (is_2string_any)
1044 {
1045 switch (tv->v_type)
1046 {
1047 case VAR_SPECIAL:
1048 case VAR_BOOL:
1049 case VAR_NUMBER:
1050 case VAR_FLOAT:
1051 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001052
1053 case VAR_LIST:
1054 if (tolerant)
1055 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001056 char_u *s, *e, *p;
1057 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001058
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001059 ga_init2(&ga, sizeof(char_u *), 1);
1060
1061 // Convert to NL separated items, then
1062 // escape the items and replace the NL with
1063 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001064 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001065 if (str == NULL)
1066 return FAIL;
1067 s = str;
1068 while ((e = vim_strchr(s, '\n')) != NULL)
1069 {
1070 *e = NUL;
1071 p = vim_strsave_fnameescape(s, FALSE);
1072 if (p != NULL)
1073 {
1074 ga_concat(&ga, p);
1075 ga_concat(&ga, (char_u *)" ");
1076 vim_free(p);
1077 }
1078 s = e + 1;
1079 }
1080 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001081 clear_tv(tv);
1082 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001083 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001084 return OK;
1085 }
1086 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001087 default: to_string_error(tv->v_type);
1088 return FAIL;
1089 }
1090 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001091 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001092 clear_tv(tv);
1093 tv->v_type = VAR_STRING;
1094 tv->vval.v_string = str;
1095 }
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001100 * When the value of "sv" is a null list of dict, allocate it.
1101 */
1102 static void
1103allocate_if_null(typval_T *tv)
1104{
1105 switch (tv->v_type)
1106 {
1107 case VAR_LIST:
1108 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001109 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001110 break;
1111 case VAR_DICT:
1112 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001113 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001114 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001115 case VAR_BLOB:
1116 if (tv->vval.v_blob == NULL)
1117 (void)rettv_blob_alloc(tv);
1118 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001119 default:
1120 break;
1121 }
1122}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001123
Bram Moolenaare7525c52021-01-09 13:20:37 +01001124/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001125 * Return the character "str[index]" where "index" is the character index,
1126 * including composing characters.
1127 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001128 */
1129 char_u *
1130char_from_string(char_u *str, varnumber_T index)
1131{
1132 size_t nbyte = 0;
1133 varnumber_T nchar = index;
1134 size_t slen;
1135
1136 if (str == NULL)
1137 return NULL;
1138 slen = STRLEN(str);
1139
Bram Moolenaarff871402021-03-26 13:34:05 +01001140 // Do the same as for a list: a negative index counts from the end.
1141 // Optimization to check the first byte to be below 0x80 (and no composing
1142 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001143 if (index < 0)
1144 {
1145 int clen = 0;
1146
1147 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001148 {
1149 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1150 ++nbyte;
1151 else if (enc_utf8)
1152 nbyte += utfc_ptr2len(str + nbyte);
1153 else
1154 nbyte += mb_ptr2len(str + nbyte);
1155 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001156 nchar = clen + index;
1157 if (nchar < 0)
1158 // unlike list: index out of range results in empty string
1159 return NULL;
1160 }
1161
1162 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001163 {
1164 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1165 ++nbyte;
1166 else if (enc_utf8)
1167 nbyte += utfc_ptr2len(str + nbyte);
1168 else
1169 nbyte += mb_ptr2len(str + nbyte);
1170 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001171 if (nbyte >= slen)
1172 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001173 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001174}
1175
1176/*
1177 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001178 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001179 * If going over the end return "str_len".
1180 * If "idx" is negative count from the end, -1 is the last character.
1181 * When going over the start return -1.
1182 */
1183 static long
1184char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1185{
1186 varnumber_T nchar = idx;
1187 size_t nbyte = 0;
1188
1189 if (nchar >= 0)
1190 {
1191 while (nchar > 0 && nbyte < str_len)
1192 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001193 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001194 --nchar;
1195 }
1196 }
1197 else
1198 {
1199 nbyte = str_len;
1200 while (nchar < 0 && nbyte > 0)
1201 {
1202 --nbyte;
1203 nbyte -= mb_head_off(str, str + nbyte);
1204 ++nchar;
1205 }
1206 if (nchar < 0)
1207 return -1;
1208 }
1209 return (long)nbyte;
1210}
1211
1212/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001213 * Return the slice "str[first : last]" using character indexes. Composing
1214 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001215 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001216 * Return NULL when the result is empty.
1217 */
1218 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001219string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001220{
1221 long start_byte, end_byte;
1222 size_t slen;
1223
1224 if (str == NULL)
1225 return NULL;
1226 slen = STRLEN(str);
1227 start_byte = char_idx2byte(str, slen, first);
1228 if (start_byte < 0)
1229 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001230 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001231 end_byte = (long)slen;
1232 else
1233 {
1234 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001235 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001236 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001237 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001238 }
1239
1240 if (start_byte >= (long)slen || end_byte <= start_byte)
1241 return NULL;
1242 return vim_strnsave(str + start_byte, end_byte - start_byte);
1243}
1244
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001245 static svar_T *
1246get_script_svar(scriptref_T *sref, ectx_T *ectx)
1247{
1248 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1249 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1250 + ectx->ec_dfunc_idx;
1251 svar_T *sv;
1252
1253 if (sref->sref_seq != si->sn_script_seq)
1254 {
1255 // The script was reloaded after the function was
1256 // compiled, the script_idx may not be valid.
1257 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1258 dfunc->df_ufunc->uf_name_exp);
1259 return NULL;
1260 }
1261 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1262 if (!equal_type(sv->sv_type, sref->sref_type))
1263 {
1264 emsg(_(e_script_variable_type_changed));
1265 return NULL;
1266 }
1267 return sv;
1268}
1269
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001270/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001271 * Function passed to do_cmdline() for splitting a script joined by NL
1272 * characters.
1273 */
1274 static char_u *
1275get_split_sourceline(
1276 int c UNUSED,
1277 void *cookie,
1278 int indent UNUSED,
1279 getline_opt_T options UNUSED)
1280{
1281 source_cookie_T *sp = (source_cookie_T *)cookie;
1282 char_u *p;
1283 char_u *line;
1284
1285 if (*sp->nextline == NUL)
1286 return NULL;
1287 p = vim_strchr(sp->nextline, '\n');
1288 if (p == NULL)
1289 {
1290 line = vim_strsave(sp->nextline);
1291 sp->nextline += STRLEN(sp->nextline);
1292 }
1293 else
1294 {
1295 line = vim_strnsave(sp->nextline, p - sp->nextline);
1296 sp->nextline = p + 1;
1297 }
1298 return line;
1299}
1300
1301/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 * Execute a function by "name".
1303 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001304 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 */
1306 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001307call_eval_func(
1308 char_u *name,
1309 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001310 ectx_T *ectx,
1311 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312{
Bram Moolenaared677f52020-08-12 16:38:10 +02001313 int called_emsg_before = called_emsg;
1314 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315
Bram Moolenaar4c137212021-04-19 16:48:48 +02001316 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001317 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001319 dictitem_T *v;
1320
1321 v = find_var(name, NULL, FALSE);
1322 if (v == NULL)
1323 {
1324 semsg(_(e_unknownfunc), name);
1325 return FAIL;
1326 }
1327 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1328 {
1329 semsg(_(e_unknownfunc), name);
1330 return FAIL;
1331 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001332 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001334 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335}
1336
1337/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001338 * When a function reference is used, fill a partial with the information
1339 * needed, especially when it is used as a closure.
1340 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001341 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001342fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1343{
1344 pt->pt_func = ufunc;
1345 pt->pt_refcount = 1;
1346
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001347 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001348 {
1349 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1350 + ectx->ec_dfunc_idx;
1351
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001352 // The closure may need to find arguments and local variables in the
1353 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001354 pt->pt_outer.out_stack = &ectx->ec_stack;
1355 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001356 if (ectx->ec_outer_ref != NULL)
1357 {
1358 // The current context already has a context, link to that one.
1359 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1360 if (ectx->ec_outer_ref->or_partial != NULL)
1361 {
1362 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1363 ++pt->pt_outer.out_up_partial->pt_refcount;
1364 }
1365 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001366
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001367 // If this function returns and the closure is still being used, we
1368 // need to make a copy of the context (arguments and local variables).
1369 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001370 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1371 {
1372 vim_free(pt);
1373 return FAIL;
1374 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001375 // Extra variable keeps the count of closures created in the current
1376 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001377 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1378 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1379
1380 ((partial_T **)ectx->ec_funcrefs.ga_data)
1381 [ectx->ec_funcrefs.ga_len] = pt;
1382 ++pt->pt_refcount;
1383 ++ectx->ec_funcrefs.ga_len;
1384 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001385 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001386 return OK;
1387}
1388
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001389// used for v_instr of typval of VAR_INSTR
1390struct instr_S {
1391 ectx_T *instr_ectx;
1392 isn_T *instr_instr;
1393};
1394
Bram Moolenaar4c137212021-04-19 16:48:48 +02001395// used for substitute_instr
1396typedef struct subs_expr_S {
1397 ectx_T *subs_ectx;
1398 isn_T *subs_instr;
1399 int subs_status;
1400} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401
1402// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001403#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404
1405// Get pointer to item at the bottom of the stack, -1 is the bottom.
1406#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001407#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 +01001408
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001409// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001410#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 +01001411
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001412// Set when calling do_debug().
1413static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001414static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001415
1416/*
1417 * When debugging lookup "name" and return the typeval.
1418 * When not found return NULL.
1419 */
1420 typval_T *
1421lookup_debug_var(char_u *name)
1422{
1423 int idx;
1424 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001425 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001426 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001427 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001428
1429 if (ectx == NULL)
1430 return NULL;
1431 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1432
1433 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001434 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001435 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001436 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001437 return STACK_TV_VAR(idx);
1438 }
1439
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001440 // Go through argument names.
1441 ufunc = dfunc->df_ufunc;
1442 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1443 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1444 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1445 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1446 - varargs_off + idx);
1447 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1448 return STACK_TV(ectx->ec_frame_idx - 1);
1449
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001450 return NULL;
1451}
1452
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001453 static void
1454handle_debug(isn_T *iptr, ectx_T *ectx)
1455{
1456 char_u *line;
1457 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1458 + ectx->ec_dfunc_idx)->df_ufunc;
1459 isn_T *ni;
1460 int end_lnum = iptr->isn_lnum;
1461 garray_T ga;
1462 int lnum;
1463
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001464 if (ex_nesting_level > debug_break_level)
1465 {
1466 linenr_T breakpoint;
1467
1468 if (!ufunc->uf_has_breakpoint)
1469 return;
1470
1471 // check for the next breakpoint if needed
1472 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
1473 iptr->isn_lnum - 1);
1474 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1475 return;
1476 }
1477
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001478 SOURCING_LNUM = iptr->isn_lnum;
1479 debug_context = ectx;
1480 debug_var_count = iptr->isn_arg.number;
1481
1482 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1483 if (ni->isn_type == ISN_DEBUG
1484 || ni->isn_type == ISN_RETURN
1485 || ni->isn_type == ISN_RETURN_VOID)
1486 {
1487 end_lnum = ni->isn_lnum;
1488 break;
1489 }
1490
1491 if (end_lnum > iptr->isn_lnum)
1492 {
1493 ga_init2(&ga, sizeof(char_u *), 10);
1494 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001495 {
1496 char_u *p = skipwhite(
1497 ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1]);
1498
1499 if (*p == '#')
1500 break;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001501 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001502 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1503 if (STRNCMP(p, "def ", 4) == 0)
1504 break;
1505 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001506 line = ga_concat_strings(&ga, " ");
1507 vim_free(ga.ga_data);
1508 }
1509 else
1510 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001511
Dominique Pelle6e969552021-06-17 13:53:41 +02001512 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001513 debug_context = NULL;
1514
1515 if (end_lnum > iptr->isn_lnum)
1516 vim_free(line);
1517}
1518
Bram Moolenaar4c137212021-04-19 16:48:48 +02001519/*
1520 * Execute instructions in execution context "ectx".
1521 * Return OK or FAIL;
1522 */
1523 static int
1524exec_instructions(ectx_T *ectx)
1525{
1526 int breakcheck_count = 0;
1527 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001528 int ret = FAIL;
1529 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001530
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001531 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001532 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001533
Bram Moolenaarff652882021-05-16 15:24:49 +02001534 // Only catch exceptions in this instruction list.
1535 ectx->ec_trylevel_at_start = trylevel;
1536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 for (;;)
1538 {
1539 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001540
Bram Moolenaar270d0382020-05-15 21:42:53 +02001541 if (++breakcheck_count >= 100)
1542 {
1543 line_breakcheck();
1544 breakcheck_count = 0;
1545 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001546 if (got_int)
1547 {
1548 // Turn CTRL-C into an exception.
1549 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001550 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001551 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001552 did_throw = TRUE;
1553 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554
Bram Moolenaara26b9702020-04-18 19:53:28 +02001555 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1556 {
1557 // Turn an error message into an exception.
1558 did_emsg = FALSE;
1559 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001560 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001561 did_throw = TRUE;
1562 *msg_list = NULL;
1563 }
1564
Bram Moolenaar4c137212021-04-19 16:48:48 +02001565 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001567 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001568 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569
1570 // An exception jumps to the first catch, finally, or returns from
1571 // the current function.
1572 if (trystack->ga_len > 0)
1573 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001574 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 {
1576 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001577 ectx->ec_in_catch = TRUE;
1578 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 }
1580 else
1581 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001582 // Not inside try or need to return from current functions.
1583 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001584 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001585 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001586 tv = STACK_TV_BOT(0);
1587 tv->v_type = VAR_NUMBER;
1588 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001589 ++ectx->ec_stack.ga_len;
1590 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001592 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001593 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001594 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001595 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 goto done;
1597 }
1598
Bram Moolenaar4c137212021-04-19 16:48:48 +02001599 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001600 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 }
1602 continue;
1603 }
1604
Bram Moolenaar4c137212021-04-19 16:48:48 +02001605 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 switch (iptr->isn_type)
1607 {
1608 // execute Ex command line
1609 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001610 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001611 source_cookie_T cookie;
1612
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001613 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001614 // Pass getsourceline to get an error for a missing ":end"
1615 // command.
1616 CLEAR_FIELD(cookie);
1617 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1618 if (do_cmdline(iptr->isn_arg.string,
1619 getsourceline, &cookie,
1620 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1621 == FAIL
1622 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001623 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 break;
1626
Bram Moolenaar20677332021-06-06 17:02:53 +02001627 // execute Ex command line split at NL characters.
1628 case ISN_EXEC_SPLIT:
1629 {
1630 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001631 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001632
1633 SOURCING_LNUM = iptr->isn_lnum;
1634 CLEAR_FIELD(cookie);
1635 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1636 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001637 line = get_split_sourceline(0, &cookie, 0, 0);
1638 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001639 get_split_sourceline, &cookie,
1640 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1641 == FAIL
1642 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001643 {
1644 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001645 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001646 }
1647 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001648 }
1649 break;
1650
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001651 // Evaluate an expression with legacy syntax, push it onto the
1652 // stack.
1653 case ISN_LEGACY_EVAL:
1654 {
1655 char_u *arg = iptr->isn_arg.string;
1656 int res;
1657 int save_flags = cmdmod.cmod_flags;
1658
1659 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001660 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001661 tv = STACK_TV_BOT(0);
1662 init_tv(tv);
1663 cmdmod.cmod_flags |= CMOD_LEGACY;
1664 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1665 cmdmod.cmod_flags = save_flags;
1666 if (res == FAIL)
1667 goto on_error;
1668 ++ectx->ec_stack.ga_len;
1669 }
1670 break;
1671
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001672 // push typeval VAR_INSTR with instructions to be executed
1673 case ISN_INSTR:
1674 {
1675 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001676 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001677 tv = STACK_TV_BOT(0);
1678 tv->vval.v_instr = ALLOC_ONE(instr_T);
1679 if (tv->vval.v_instr == NULL)
1680 goto on_error;
1681 ++ectx->ec_stack.ga_len;
1682
1683 tv->v_type = VAR_INSTR;
1684 tv->vval.v_instr->instr_ectx = ectx;
1685 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1686 }
1687 break;
1688
Bram Moolenaar4c137212021-04-19 16:48:48 +02001689 // execute :substitute with an expression
1690 case ISN_SUBSTITUTE:
1691 {
1692 subs_T *subs = &iptr->isn_arg.subs;
1693 source_cookie_T cookie;
1694 struct subs_expr_S *save_instr = substitute_instr;
1695 struct subs_expr_S subs_instr;
1696 int res;
1697
1698 subs_instr.subs_ectx = ectx;
1699 subs_instr.subs_instr = subs->subs_instr;
1700 subs_instr.subs_status = OK;
1701 substitute_instr = &subs_instr;
1702
1703 SOURCING_LNUM = iptr->isn_lnum;
1704 // This is very much like ISN_EXEC
1705 CLEAR_FIELD(cookie);
1706 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1707 res = do_cmdline(subs->subs_cmd,
1708 getsourceline, &cookie,
1709 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1710 substitute_instr = save_instr;
1711
1712 if (res == FAIL || did_emsg
1713 || subs_instr.subs_status == FAIL)
1714 goto on_error;
1715 }
1716 break;
1717
1718 case ISN_FINISH:
1719 goto done;
1720
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001721 case ISN_REDIRSTART:
1722 // create a dummy entry for var_redir_str()
1723 if (alloc_redir_lval() == FAIL)
1724 goto on_error;
1725
1726 // The output is stored in growarray "redir_ga" until
1727 // redirection ends.
1728 init_redir_ga();
1729 redir_vname = 1;
1730 break;
1731
1732 case ISN_REDIREND:
1733 {
1734 char_u *res = get_clear_redir_ga();
1735
1736 // End redirection, put redirected text on the stack.
1737 clear_redir_lval();
1738 redir_vname = 0;
1739
1740 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1741 {
1742 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001743 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001744 }
1745 tv = STACK_TV_BOT(0);
1746 tv->v_type = VAR_STRING;
1747 tv->vval.v_string = res;
1748 ++ectx->ec_stack.ga_len;
1749 }
1750 break;
1751
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001752 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001753#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001754 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1755 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001756#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001757 break;
1758
1759 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001760#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001761 {
1762 exarg_T ea;
1763 int res;
1764
1765 CLEAR_FIELD(ea);
1766 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1767 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1768 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1769 --ectx->ec_stack.ga_len;
1770 tv = STACK_TV_BOT(0);
1771 res = cexpr_core(&ea, tv);
1772 clear_tv(tv);
1773 if (res == FAIL)
1774 goto on_error;
1775 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001776#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001777 break;
1778
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001779 // execute Ex command from pieces on the stack
1780 case ISN_EXECCONCAT:
1781 {
1782 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001783 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001784 int pass;
1785 int i;
1786 char_u *cmd = NULL;
1787 char_u *str;
1788
1789 for (pass = 1; pass <= 2; ++pass)
1790 {
1791 for (i = 0; i < count; ++i)
1792 {
1793 tv = STACK_TV_BOT(i - count);
1794 str = tv->vval.v_string;
1795 if (str != NULL && *str != NUL)
1796 {
1797 if (pass == 2)
1798 STRCPY(cmd + len, str);
1799 len += STRLEN(str);
1800 }
1801 if (pass == 2)
1802 clear_tv(tv);
1803 }
1804 if (pass == 1)
1805 {
1806 cmd = alloc(len + 1);
1807 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001808 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001809 len = 0;
1810 }
1811 }
1812
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001813 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001814 do_cmdline_cmd(cmd);
1815 vim_free(cmd);
1816 }
1817 break;
1818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 // execute :echo {string} ...
1820 case ISN_ECHO:
1821 {
1822 int count = iptr->isn_arg.echo.echo_count;
1823 int atstart = TRUE;
1824 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001825 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826
1827 for (idx = 0; idx < count; ++idx)
1828 {
1829 tv = STACK_TV_BOT(idx - count);
1830 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1831 &atstart, &needclr);
1832 clear_tv(tv);
1833 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001834 if (needclr)
1835 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001836 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 }
1838 break;
1839
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001840 // :execute {string} ...
1841 // :echomsg {string} ...
1842 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001843 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001844 case ISN_ECHOMSG:
1845 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001846 {
1847 int count = iptr->isn_arg.number;
1848 garray_T ga;
1849 char_u buf[NUMBUFLEN];
1850 char_u *p;
1851 int len;
1852 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001853 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001854
1855 ga_init2(&ga, 1, 80);
1856 for (idx = 0; idx < count; ++idx)
1857 {
1858 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001859 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001860 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001861 if (tv->v_type == VAR_CHANNEL
1862 || tv->v_type == VAR_JOB)
1863 {
1864 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001865 semsg(_(e_using_invalid_value_as_string_str),
1866 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001867 break;
1868 }
1869 else
1870 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001871 }
1872 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001873 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001874
1875 len = (int)STRLEN(p);
1876 if (ga_grow(&ga, len + 2) == FAIL)
1877 failed = TRUE;
1878 else
1879 {
1880 if (ga.ga_len > 0)
1881 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1882 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1883 ga.ga_len += len;
1884 }
1885 clear_tv(tv);
1886 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001887 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001888 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001889 {
1890 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001891 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001892 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001893
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001894 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001895 {
1896 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001897 {
1898 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001899 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001900 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001901 {
1902 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001903 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001904 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001905 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001906 else
1907 {
1908 msg_sb_eol();
1909 if (iptr->isn_type == ISN_ECHOMSG)
1910 {
1911 msg_attr(ga.ga_data, echo_attr);
1912 out_flush();
1913 }
1914 else
1915 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001916 SOURCING_LNUM = iptr->isn_lnum;
1917 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001918 }
1919 }
1920 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001921 ga_clear(&ga);
1922 }
1923 break;
1924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 // load local variable or argument
1926 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001927 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001928 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001930 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 break;
1932
1933 // load v: variable
1934 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001935 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001936 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001938 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 break;
1940
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001941 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 case ISN_LOADSCRIPT:
1943 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001944 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 svar_T *sv;
1946
Bram Moolenaar4c137212021-04-19 16:48:48 +02001947 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001948 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001949 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001950 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001951 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001952 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001954 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 }
1956 break;
1957
1958 // load s: variable in old script
1959 case ISN_LOADS:
1960 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001961 hashtab_T *ht = &SCRIPT_VARS(
1962 iptr->isn_arg.loadstore.ls_sid);
1963 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 if (di == NULL)
1967 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001968 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001969 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001970 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 }
1972 else
1973 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001974 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001975 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001977 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 }
1979 }
1980 break;
1981
Bram Moolenaard3aac292020-04-19 14:32:17 +02001982 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001984 case ISN_LOADB:
1985 case ISN_LOADW:
1986 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001988 dictitem_T *di = NULL;
1989 hashtab_T *ht = NULL;
1990 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001991
Bram Moolenaard3aac292020-04-19 14:32:17 +02001992 switch (iptr->isn_type)
1993 {
1994 case ISN_LOADG:
1995 ht = get_globvar_ht();
1996 namespace = 'g';
1997 break;
1998 case ISN_LOADB:
1999 ht = &curbuf->b_vars->dv_hashtab;
2000 namespace = 'b';
2001 break;
2002 case ISN_LOADW:
2003 ht = &curwin->w_vars->dv_hashtab;
2004 namespace = 'w';
2005 break;
2006 case ISN_LOADT:
2007 ht = &curtab->tp_vars->dv_hashtab;
2008 namespace = 't';
2009 break;
2010 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002011 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002012 }
2013 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 if (di == NULL)
2016 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002017 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002018 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002019 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002020 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 }
2022 else
2023 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002024 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002025 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002027 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 }
2029 }
2030 break;
2031
Bram Moolenaar03290b82020-12-19 16:30:44 +01002032 // load autoload variable
2033 case ISN_LOADAUTO:
2034 {
2035 char_u *name = iptr->isn_arg.string;
2036
Bram Moolenaar4c137212021-04-19 16:48:48 +02002037 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002038 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002039 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002040 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002041 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002042 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002043 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002044 }
2045 break;
2046
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002047 // load g:/b:/w:/t: namespace
2048 case ISN_LOADGDICT:
2049 case ISN_LOADBDICT:
2050 case ISN_LOADWDICT:
2051 case ISN_LOADTDICT:
2052 {
2053 dict_T *d = NULL;
2054
2055 switch (iptr->isn_type)
2056 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002057 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2058 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2059 case ISN_LOADWDICT: d = curwin->w_vars; break;
2060 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002061 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002062 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002063 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002064 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002065 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002066 tv = STACK_TV_BOT(0);
2067 tv->v_type = VAR_DICT;
2068 tv->v_lock = 0;
2069 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002070 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002071 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002072 }
2073 break;
2074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 // load &option
2076 case ISN_LOADOPT:
2077 {
2078 typval_T optval;
2079 char_u *name = iptr->isn_arg.string;
2080
Bram Moolenaara8c17702020-04-01 21:17:24 +02002081 // This is not expected to fail, name is checked during
2082 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002083 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002084 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002085 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002086 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002088 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 }
2090 break;
2091
2092 // load $ENV
2093 case ISN_LOADENV:
2094 {
2095 typval_T optval;
2096 char_u *name = iptr->isn_arg.string;
2097
Bram Moolenaar4c137212021-04-19 16:48:48 +02002098 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002099 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002100 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002101 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002103 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 }
2105 break;
2106
2107 // load @register
2108 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002109 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002110 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 tv = STACK_TV_BOT(0);
2112 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002113 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002114 // This may result in NULL, which should be equivalent to an
2115 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 tv->vval.v_string = get_reg_contents(
2117 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002118 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 break;
2120
2121 // store local variable
2122 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002123 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 tv = STACK_TV_VAR(iptr->isn_arg.number);
2125 clear_tv(tv);
2126 *tv = *STACK_TV_BOT(0);
2127 break;
2128
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002129 // store s: variable in old script
2130 case ISN_STORES:
2131 {
2132 hashtab_T *ht = &SCRIPT_VARS(
2133 iptr->isn_arg.loadstore.ls_sid);
2134 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002135 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002136
Bram Moolenaar4c137212021-04-19 16:48:48 +02002137 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002138 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002139 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002140 else
2141 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002142 SOURCING_LNUM = iptr->isn_lnum;
2143 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002144 {
2145 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002146 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002147 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002148 clear_tv(&di->di_tv);
2149 di->di_tv = *STACK_TV_BOT(0);
2150 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002151 }
2152 break;
2153
2154 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 case ISN_STORESCRIPT:
2156 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002157 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2158 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159
Bram Moolenaar4c137212021-04-19 16:48:48 +02002160 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002161 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002162 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002163 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002164
2165 // "const" and "final" are checked at compile time, locking
2166 // the value needs to be checked here.
2167 SOURCING_LNUM = iptr->isn_lnum;
2168 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002169 {
2170 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002171 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002172 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 clear_tv(sv->sv_tv);
2175 *sv->sv_tv = *STACK_TV_BOT(0);
2176 }
2177 break;
2178
2179 // store option
2180 case ISN_STOREOPT:
2181 {
2182 long n = 0;
2183 char_u *s = NULL;
2184 char *msg;
2185
Bram Moolenaar4c137212021-04-19 16:48:48 +02002186 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 tv = STACK_TV_BOT(0);
2188 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002189 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002191 if (s == NULL)
2192 s = (char_u *)"";
2193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002195 // must be VAR_NUMBER, CHECKTYPE makes sure
2196 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2198 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002199 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 if (msg != NULL)
2201 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002202 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002204 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 }
2207 break;
2208
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002209 // store $ENV
2210 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002211 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002212 tv = STACK_TV_BOT(0);
2213 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2214 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002215 break;
2216
2217 // store @r
2218 case ISN_STOREREG:
2219 {
2220 int reg = iptr->isn_arg.number;
2221
Bram Moolenaar4c137212021-04-19 16:48:48 +02002222 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002223 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002224 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002225 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002226 }
2227 break;
2228
2229 // store v: variable
2230 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002231 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002232 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2233 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002234 // should not happen, type is checked when compiling
2235 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002236 break;
2237
Bram Moolenaard3aac292020-04-19 14:32:17 +02002238 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002240 case ISN_STOREB:
2241 case ISN_STOREW:
2242 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002244 dictitem_T *di;
2245 hashtab_T *ht;
2246 char_u *name = iptr->isn_arg.string + 2;
2247
Bram Moolenaard3aac292020-04-19 14:32:17 +02002248 switch (iptr->isn_type)
2249 {
2250 case ISN_STOREG:
2251 ht = get_globvar_ht();
2252 break;
2253 case ISN_STOREB:
2254 ht = &curbuf->b_vars->dv_hashtab;
2255 break;
2256 case ISN_STOREW:
2257 ht = &curwin->w_vars->dv_hashtab;
2258 break;
2259 case ISN_STORET:
2260 ht = &curtab->tp_vars->dv_hashtab;
2261 break;
2262 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002263 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265
Bram Moolenaar4c137212021-04-19 16:48:48 +02002266 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002267 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002269 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 else
2271 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002272 SOURCING_LNUM = iptr->isn_lnum;
2273 if (var_check_permission(di, name) == FAIL)
2274 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 clear_tv(&di->di_tv);
2276 di->di_tv = *STACK_TV_BOT(0);
2277 }
2278 }
2279 break;
2280
Bram Moolenaar03290b82020-12-19 16:30:44 +01002281 // store an autoload variable
2282 case ISN_STOREAUTO:
2283 SOURCING_LNUM = iptr->isn_lnum;
2284 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2285 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002286 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002287 break;
2288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 // store number in local variable
2290 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002291 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 clear_tv(tv);
2293 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002294 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 break;
2296
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002297 // store value in list or dict variable
2298 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002299 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002300 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002301 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002302 typval_T *tv_dest = STACK_TV_BOT(-1);
2303 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002304
Bram Moolenaar752fc692021-01-04 21:57:11 +01002305 // Stack contains:
2306 // -3 value to be stored
2307 // -2 index
2308 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002309 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002310 SOURCING_LNUM = iptr->isn_lnum;
2311 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002312 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002313 dest_type = tv_dest->v_type;
2314 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002315 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002316 else if (dest_type == VAR_LIST
2317 && tv_idx->v_type != VAR_NUMBER)
2318 {
2319 emsg(_(e_number_exp));
2320 status = FAIL;
2321 }
2322 }
2323 else if (dest_type != tv_dest->v_type)
2324 {
2325 // just in case, should be OK
2326 semsg(_(e_expected_str_but_got_str),
2327 vartype_name(dest_type),
2328 vartype_name(tv_dest->v_type));
2329 status = FAIL;
2330 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002331
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002332 if (status == OK && dest_type == VAR_LIST)
2333 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002334 long lidx = (long)tv_idx->vval.v_number;
2335 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002336
2337 if (list == NULL)
2338 {
2339 emsg(_(e_list_not_set));
2340 goto on_error;
2341 }
2342 if (lidx < 0 && list->lv_len + lidx >= 0)
2343 // negative index is relative to the end
2344 lidx = list->lv_len + lidx;
2345 if (lidx < 0 || lidx > list->lv_len)
2346 {
2347 semsg(_(e_listidx), lidx);
2348 goto on_error;
2349 }
2350 if (lidx < list->lv_len)
2351 {
2352 listitem_T *li = list_find(list, lidx);
2353
2354 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002355 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002356 goto on_error;
2357 // overwrite existing list item
2358 clear_tv(&li->li_tv);
2359 li->li_tv = *tv;
2360 }
2361 else
2362 {
2363 if (error_if_locked(list->lv_lock,
2364 e_cannot_change_list))
2365 goto on_error;
2366 // append to list, only fails when out of memory
2367 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002368 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002369 clear_tv(tv);
2370 }
2371 }
2372 else if (status == OK && dest_type == VAR_DICT)
2373 {
2374 char_u *key = tv_idx->vval.v_string;
2375 dict_T *dict = tv_dest->vval.v_dict;
2376 dictitem_T *di;
2377
2378 SOURCING_LNUM = iptr->isn_lnum;
2379 if (dict == NULL)
2380 {
2381 emsg(_(e_dictionary_not_set));
2382 goto on_error;
2383 }
2384 if (key == NULL)
2385 key = (char_u *)"";
2386 di = dict_find(dict, key, -1);
2387 if (di != NULL)
2388 {
2389 if (error_if_locked(di->di_tv.v_lock,
2390 e_cannot_change_dict_item))
2391 goto on_error;
2392 // overwrite existing value
2393 clear_tv(&di->di_tv);
2394 di->di_tv = *tv;
2395 }
2396 else
2397 {
2398 if (error_if_locked(dict->dv_lock,
2399 e_cannot_change_dict))
2400 goto on_error;
2401 // add to dict, only fails when out of memory
2402 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002403 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002404 clear_tv(tv);
2405 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002406 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002407 else if (status == OK && dest_type == VAR_BLOB)
2408 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002409 long lidx = (long)tv_idx->vval.v_number;
2410 blob_T *blob = tv_dest->vval.v_blob;
2411 varnumber_T nr;
2412 int error = FALSE;
2413 int len;
2414
2415 if (blob == NULL)
2416 {
2417 emsg(_(e_blob_not_set));
2418 goto on_error;
2419 }
2420 len = blob_len(blob);
2421 if (lidx < 0 && len + lidx >= 0)
2422 // negative index is relative to the end
2423 lidx = len + lidx;
2424
2425 // Can add one byte at the end.
2426 if (lidx < 0 || lidx > len)
2427 {
2428 semsg(_(e_blobidx), lidx);
2429 goto on_error;
2430 }
2431 if (value_check_lock(blob->bv_lock,
2432 (char_u *)"blob", FALSE))
2433 goto on_error;
2434 nr = tv_get_number_chk(tv, &error);
2435 if (error)
2436 goto on_error;
2437 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002438 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002439 else
2440 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002441 status = FAIL;
2442 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002443 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002444
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002445 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002446 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002447 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002448 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002449 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002450 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002451 goto on_error;
2452 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002453 }
2454 break;
2455
Bram Moolenaar68452172021-04-12 21:21:02 +02002456 // store value in blob range
2457 case ISN_STORERANGE:
2458 {
2459 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2460 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2461 typval_T *tv_dest = STACK_TV_BOT(-1);
2462 int status = OK;
2463
2464 // Stack contains:
2465 // -4 value to be stored
2466 // -3 first index or "none"
2467 // -2 second index or "none"
2468 // -1 destination blob
2469 tv = STACK_TV_BOT(-4);
2470 if (tv_dest->v_type != VAR_BLOB)
2471 {
2472 status = FAIL;
2473 emsg(_(e_blob_required));
2474 }
2475 else
2476 {
2477 varnumber_T n1;
2478 varnumber_T n2;
2479 int error = FALSE;
2480
2481 n1 = tv_get_number_chk(tv_idx1, &error);
2482 if (error)
2483 status = FAIL;
2484 else
2485 {
2486 if (tv_idx2->v_type == VAR_SPECIAL
2487 && tv_idx2->vval.v_number == VVAL_NONE)
2488 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2489 else
2490 n2 = tv_get_number_chk(tv_idx2, &error);
2491 if (error)
2492 status = FAIL;
2493 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002494 {
2495 long bloblen = blob_len(tv_dest->vval.v_blob);
2496
2497 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002498 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002499 || check_blob_range(bloblen,
2500 n1, n2, FALSE) == FAIL)
2501 status = FAIL;
2502 else
2503 status = blob_set_range(
2504 tv_dest->vval.v_blob, n1, n2, tv);
2505 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002506 }
2507 }
2508
2509 clear_tv(tv_idx1);
2510 clear_tv(tv_idx2);
2511 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002512 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002513 clear_tv(tv);
2514
2515 if (status == FAIL)
2516 goto on_error;
2517 }
2518 break;
2519
Bram Moolenaar0186e582021-01-10 18:33:11 +01002520 // load or store variable or argument from outer scope
2521 case ISN_LOADOUTER:
2522 case ISN_STOREOUTER:
2523 {
2524 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002525 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2526 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002527
2528 while (depth > 1 && outer != NULL)
2529 {
2530 outer = outer->out_up;
2531 --depth;
2532 }
2533 if (outer == NULL)
2534 {
2535 SOURCING_LNUM = iptr->isn_lnum;
2536 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002537 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002538 }
2539 tv = ((typval_T *)outer->out_stack->ga_data)
2540 + outer->out_frame_idx + STACK_FRAME_SIZE
2541 + iptr->isn_arg.outer.outer_idx;
2542 if (iptr->isn_type == ISN_LOADOUTER)
2543 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002544 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002545 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002546 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002547 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002548 }
2549 else
2550 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002551 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002552 clear_tv(tv);
2553 *tv = *STACK_TV_BOT(0);
2554 }
2555 }
2556 break;
2557
Bram Moolenaar752fc692021-01-04 21:57:11 +01002558 // unlet item in list or dict variable
2559 case ISN_UNLETINDEX:
2560 {
2561 typval_T *tv_idx = STACK_TV_BOT(-2);
2562 typval_T *tv_dest = STACK_TV_BOT(-1);
2563 int status = OK;
2564
2565 // Stack contains:
2566 // -2 index
2567 // -1 dict or list
2568 if (tv_dest->v_type == VAR_DICT)
2569 {
2570 // unlet a dict item, index must be a string
2571 if (tv_idx->v_type != VAR_STRING)
2572 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002573 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002574 semsg(_(e_expected_str_but_got_str),
2575 vartype_name(VAR_STRING),
2576 vartype_name(tv_idx->v_type));
2577 status = FAIL;
2578 }
2579 else
2580 {
2581 dict_T *d = tv_dest->vval.v_dict;
2582 char_u *key = tv_idx->vval.v_string;
2583 dictitem_T *di = NULL;
2584
2585 if (key == NULL)
2586 key = (char_u *)"";
2587 if (d != NULL)
2588 di = dict_find(d, key, (int)STRLEN(key));
2589 if (di == NULL)
2590 {
2591 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002592 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002593 semsg(_(e_dictkey), key);
2594 status = FAIL;
2595 }
2596 else
2597 {
2598 // TODO: check for dict or item locked
2599 dictitem_remove(d, di);
2600 }
2601 }
2602 }
2603 else if (tv_dest->v_type == VAR_LIST)
2604 {
2605 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002606 SOURCING_LNUM = iptr->isn_lnum;
2607 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002608 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002609 status = FAIL;
2610 }
2611 else
2612 {
2613 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002614 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002615 listitem_T *li = NULL;
2616
2617 li = list_find(l, n);
2618 if (li == NULL)
2619 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002620 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002621 semsg(_(e_listidx), n);
2622 status = FAIL;
2623 }
2624 else
2625 // TODO: check for list or item locked
2626 listitem_remove(l, li);
2627 }
2628 }
2629 else
2630 {
2631 status = FAIL;
2632 semsg(_(e_cannot_index_str),
2633 vartype_name(tv_dest->v_type));
2634 }
2635
2636 clear_tv(tv_idx);
2637 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002638 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002639 if (status == FAIL)
2640 goto on_error;
2641 }
2642 break;
2643
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002644 // unlet range of items in list variable
2645 case ISN_UNLETRANGE:
2646 {
2647 // Stack contains:
2648 // -3 index1
2649 // -2 index2
2650 // -1 dict or list
2651 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2652 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2653 typval_T *tv_dest = STACK_TV_BOT(-1);
2654 int status = OK;
2655
2656 if (tv_dest->v_type == VAR_LIST)
2657 {
2658 // indexes must be a number
2659 SOURCING_LNUM = iptr->isn_lnum;
2660 if (check_for_number(tv_idx1) == FAIL
2661 || check_for_number(tv_idx2) == FAIL)
2662 {
2663 status = FAIL;
2664 }
2665 else
2666 {
2667 list_T *l = tv_dest->vval.v_list;
2668 long n1 = (long)tv_idx1->vval.v_number;
2669 long n2 = (long)tv_idx2->vval.v_number;
2670 listitem_T *li;
2671
2672 li = list_find_index(l, &n1);
2673 if (li == NULL
2674 || list_unlet_range(l, li, NULL, n1,
2675 TRUE, n2) == FAIL)
2676 status = FAIL;
2677 }
2678 }
2679 else
2680 {
2681 status = FAIL;
2682 SOURCING_LNUM = iptr->isn_lnum;
2683 semsg(_(e_cannot_index_str),
2684 vartype_name(tv_dest->v_type));
2685 }
2686
2687 clear_tv(tv_idx1);
2688 clear_tv(tv_idx2);
2689 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002690 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002691 if (status == FAIL)
2692 goto on_error;
2693 }
2694 break;
2695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 // push constant
2697 case ISN_PUSHNR:
2698 case ISN_PUSHBOOL:
2699 case ISN_PUSHSPEC:
2700 case ISN_PUSHF:
2701 case ISN_PUSHS:
2702 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002703 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002704 case ISN_PUSHCHANNEL:
2705 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002706 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002707 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002709 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002710 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 switch (iptr->isn_type)
2712 {
2713 case ISN_PUSHNR:
2714 tv->v_type = VAR_NUMBER;
2715 tv->vval.v_number = iptr->isn_arg.number;
2716 break;
2717 case ISN_PUSHBOOL:
2718 tv->v_type = VAR_BOOL;
2719 tv->vval.v_number = iptr->isn_arg.number;
2720 break;
2721 case ISN_PUSHSPEC:
2722 tv->v_type = VAR_SPECIAL;
2723 tv->vval.v_number = iptr->isn_arg.number;
2724 break;
2725#ifdef FEAT_FLOAT
2726 case ISN_PUSHF:
2727 tv->v_type = VAR_FLOAT;
2728 tv->vval.v_float = iptr->isn_arg.fnumber;
2729 break;
2730#endif
2731 case ISN_PUSHBLOB:
2732 blob_copy(iptr->isn_arg.blob, tv);
2733 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002734 case ISN_PUSHFUNC:
2735 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002736 if (iptr->isn_arg.string == NULL)
2737 tv->vval.v_string = NULL;
2738 else
2739 tv->vval.v_string =
2740 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002741 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002742 case ISN_PUSHCHANNEL:
2743#ifdef FEAT_JOB_CHANNEL
2744 tv->v_type = VAR_CHANNEL;
2745 tv->vval.v_channel = iptr->isn_arg.channel;
2746 if (tv->vval.v_channel != NULL)
2747 ++tv->vval.v_channel->ch_refcount;
2748#endif
2749 break;
2750 case ISN_PUSHJOB:
2751#ifdef FEAT_JOB_CHANNEL
2752 tv->v_type = VAR_JOB;
2753 tv->vval.v_job = iptr->isn_arg.job;
2754 if (tv->vval.v_job != NULL)
2755 ++tv->vval.v_job->jv_refcount;
2756#endif
2757 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 default:
2759 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002760 tv->vval.v_string = vim_strsave(
2761 iptr->isn_arg.string == NULL
2762 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 }
2764 break;
2765
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002766 case ISN_UNLET:
2767 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2768 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002769 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002770 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002771 case ISN_UNLETENV:
2772 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2773 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002774
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002775 case ISN_LOCKCONST:
2776 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2777 break;
2778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 // create a list from items on the stack; uses a single allocation
2780 // for the list header and the items
2781 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002782 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002783 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 break;
2785
2786 // create a dict from items on the stack
2787 case ISN_NEWDICT:
2788 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002789 int count = iptr->isn_arg.number;
2790 dict_T *dict = dict_alloc();
2791 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002792 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002793 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794
2795 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002796 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 for (idx = 0; idx < count; ++idx)
2798 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002799 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002801 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002802 key = tv->vval.v_string == NULL
2803 ? (char_u *)"" : tv->vval.v_string;
2804 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002805 if (item != NULL)
2806 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002807 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002808 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002809 dict_unref(dict);
2810 goto on_error;
2811 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002812 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 clear_tv(tv);
2814 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002815 {
2816 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002817 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2820 item->di_tv.v_lock = 0;
2821 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002822 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002823 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002824 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002825 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 }
2828
2829 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002830 ectx->ec_stack.ga_len -= 2 * count - 1;
2831 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002832 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002834 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 tv = STACK_TV_BOT(-1);
2836 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002837 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 tv->vval.v_dict = dict;
2839 ++dict->dv_refcount;
2840 }
2841 break;
2842
2843 // call a :def function
2844 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002845 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002846 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2847 NULL,
2848 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002849 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002850 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 break;
2852
2853 // call a builtin function
2854 case ISN_BCALL:
2855 SOURCING_LNUM = iptr->isn_lnum;
2856 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2857 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002858 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002859 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 break;
2861
2862 // call a funcref or partial
2863 case ISN_PCALL:
2864 {
2865 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2866 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002867 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868
2869 SOURCING_LNUM = iptr->isn_lnum;
2870 if (pfunc->cpf_top)
2871 {
2872 // funcref is above the arguments
2873 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2874 }
2875 else
2876 {
2877 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002878 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002879 partial_tv = *STACK_TV_BOT(0);
2880 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002882 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002883 if (tv == &partial_tv)
2884 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002886 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 }
2888 break;
2889
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002890 case ISN_PCALL_END:
2891 // PCALL finished, arguments have been consumed and replaced by
2892 // the return value. Now clear the funcref from the stack,
2893 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002894 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002895 clear_tv(STACK_TV_BOT(-1));
2896 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2897 break;
2898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 // call a user defined function or funcref/partial
2900 case ISN_UCALL:
2901 {
2902 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2903
2904 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002905 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002906 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002907 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 }
2909 break;
2910
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002911 // return from a :def function call without a value
2912 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002913 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002914 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002915 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002916 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002917 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002918 tv->vval.v_number = 0;
2919 tv->v_lock = 0;
2920 // FALLTHROUGH
2921
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002922 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 case ISN_RETURN:
2924 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002925 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002926 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002927
2928 if (trystack->ga_len > 0)
2929 trycmd = ((trycmd_T *)trystack->ga_data)
2930 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002931 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002932 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002934 // jump to ":finally" or ":endtry"
2935 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002937 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002938 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 trycmd->tcd_return = TRUE;
2940 }
2941 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002942 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 }
2944 break;
2945
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002946 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 case ISN_FUNCREF:
2948 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002949 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2950 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2951 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002954 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002955 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002956 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002957 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002958 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002959 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002960 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002961 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002962 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002964 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 tv->vval.v_partial = pt;
2966 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002967 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 }
2969 break;
2970
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002971 // Create a global function from a lambda.
2972 case ISN_NEWFUNC:
2973 {
2974 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2975
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002976 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002977 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002978 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002979 }
2980 break;
2981
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002982 // List functions
2983 case ISN_DEF:
2984 if (iptr->isn_arg.string == NULL)
2985 list_functions(NULL);
2986 else
2987 {
2988 exarg_T ea;
2989
2990 CLEAR_FIELD(ea);
2991 ea.cmd = ea.arg = iptr->isn_arg.string;
2992 define_function(&ea, NULL);
2993 }
2994 break;
2995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 // jump if a condition is met
2997 case ISN_JUMP:
2998 {
2999 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003000 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 int jump = TRUE;
3002
3003 if (when != JUMP_ALWAYS)
3004 {
3005 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003006 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003007 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003008 || when == JUMP_IF_COND_TRUE)
3009 {
3010 SOURCING_LNUM = iptr->isn_lnum;
3011 jump = tv_get_bool_chk(tv, &error);
3012 if (error)
3013 goto on_error;
3014 }
3015 else
3016 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003018 || when == JUMP_AND_KEEP_IF_FALSE
3019 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003021 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 {
3023 // drop the value from the stack
3024 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003025 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
3027 }
3028 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003029 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 }
3031 break;
3032
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003033 // Jump if an argument with a default value was already set and not
3034 // v:none.
3035 case ISN_JUMP_IF_ARG_SET:
3036 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3037 if (tv->v_type != VAR_UNKNOWN
3038 && !(tv->v_type == VAR_SPECIAL
3039 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003040 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003041 break;
3042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 // top of a for loop
3044 case ISN_FOR:
3045 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003046 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 typval_T *idxtv =
3048 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3049
Bram Moolenaar4c137212021-04-19 16:48:48 +02003050 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003051 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003052 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003053 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003054 list_T *list = ltv->vval.v_list;
3055
3056 // push the next item from the list
3057 ++idxtv->vval.v_number;
3058 if (list == NULL
3059 || idxtv->vval.v_number >= list->lv_len)
3060 {
3061 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003062 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3063 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003064 }
3065 else if (list->lv_first == &range_list_item)
3066 {
3067 // non-materialized range() list
3068 tv = STACK_TV_BOT(0);
3069 tv->v_type = VAR_NUMBER;
3070 tv->v_lock = 0;
3071 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003073 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003074 }
3075 else
3076 {
3077 listitem_T *li = list_find(list,
3078 idxtv->vval.v_number);
3079
3080 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003081 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003082 }
3083 }
3084 else if (ltv->v_type == VAR_STRING)
3085 {
3086 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003087
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003088 // The index is for the last byte of the previous
3089 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003090 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003091 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003092 {
3093 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003094 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3095 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003096 }
3097 else
3098 {
3099 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3100
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003101 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003102 tv = STACK_TV_BOT(0);
3103 tv->v_type = VAR_STRING;
3104 tv->vval.v_string = vim_strnsave(
3105 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003107 idxtv->vval.v_number += clen - 1;
3108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003110 else if (ltv->v_type == VAR_BLOB)
3111 {
3112 blob_T *blob = ltv->vval.v_blob;
3113
3114 // When we get here the first time make a copy of the
3115 // blob, so that the iteration still works when it is
3116 // changed.
3117 if (idxtv->vval.v_number == -1 && blob != NULL)
3118 {
3119 blob_copy(blob, ltv);
3120 blob_unref(blob);
3121 blob = ltv->vval.v_blob;
3122 }
3123
3124 // The index is for the previous byte.
3125 ++idxtv->vval.v_number;
3126 if (blob == NULL
3127 || idxtv->vval.v_number >= blob_len(blob))
3128 {
3129 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3131 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003132 }
3133 else
3134 {
3135 // Push the next byte from the blob.
3136 tv = STACK_TV_BOT(0);
3137 tv->v_type = VAR_NUMBER;
3138 tv->vval.v_number = blob_get(blob,
3139 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003140 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003141 }
3142 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 else
3144 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003145 semsg(_(e_for_loop_on_str_not_supported),
3146 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003147 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 }
3149 }
3150 break;
3151
3152 // start of ":try" block
3153 case ISN_TRY:
3154 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003155 trycmd_T *trycmd = NULL;
3156
Bram Moolenaar4c137212021-04-19 16:48:48 +02003157 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003158 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003159 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3160 + ectx->ec_trystack.ga_len;
3161 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003163 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003164 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3165 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003166 trycmd->tcd_catch_idx =
3167 iptr->isn_arg.try.try_ref->try_catch;
3168 trycmd->tcd_finally_idx =
3169 iptr->isn_arg.try.try_ref->try_finally;
3170 trycmd->tcd_endtry_idx =
3171 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 }
3173 break;
3174
3175 case ISN_PUSHEXC:
3176 if (current_exception == NULL)
3177 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003178 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003180 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003182 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003183 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003185 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003187 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 tv->vval.v_string = vim_strsave(
3189 (char_u *)current_exception->value);
3190 break;
3191
3192 case ISN_CATCH:
3193 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003194 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195
Bram Moolenaar4c137212021-04-19 16:48:48 +02003196 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 if (trystack->ga_len > 0)
3198 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003199 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 + trystack->ga_len - 1;
3201 trycmd->tcd_caught = TRUE;
3202 }
3203 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003204 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 catch_exception(current_exception);
3206 }
3207 break;
3208
Bram Moolenaarc150c092021-02-13 15:02:46 +01003209 case ISN_TRYCONT:
3210 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003211 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003212 trycont_T *trycont = &iptr->isn_arg.trycont;
3213 int i;
3214 trycmd_T *trycmd;
3215 int iidx = trycont->tct_where;
3216
3217 if (trystack->ga_len < trycont->tct_levels)
3218 {
3219 siemsg("TRYCONT: expected %d levels, found %d",
3220 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003221 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003222 }
3223 // Make :endtry jump to any outer try block and the last
3224 // :endtry inside the loop to the loop start.
3225 for (i = trycont->tct_levels; i > 0; --i)
3226 {
3227 trycmd = ((trycmd_T *)trystack->ga_data)
3228 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003229 // Add one to tcd_cont to be able to jump to
3230 // instruction with index zero.
3231 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003232 iidx = trycmd->tcd_finally_idx == 0
3233 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003234 }
3235 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003236 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003237 }
3238 break;
3239
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003240 case ISN_FINALLY:
3241 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003242 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003243 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3244 + trystack->ga_len - 1;
3245
3246 // Reset the index to avoid a return statement jumps here
3247 // again.
3248 trycmd->tcd_finally_idx = 0;
3249 break;
3250 }
3251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 // end of ":try" block
3253 case ISN_ENDTRY:
3254 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003255 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256
3257 if (trystack->ga_len > 0)
3258 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003259 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 --trystack->ga_len;
3262 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003263 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 trycmd = ((trycmd_T *)trystack->ga_data)
3265 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003266 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 {
3268 // discard the exception
3269 if (caught_stack == current_exception)
3270 caught_stack = caught_stack->caught;
3271 discard_current_exception();
3272 }
3273
3274 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003275 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003276
Bram Moolenaar4c137212021-04-19 16:48:48 +02003277 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003278 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003279 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003280 clear_tv(STACK_TV_BOT(0));
3281 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003282 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003283 // handling :continue: jump to outer try block or
3284 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003285 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 }
3287 }
3288 break;
3289
3290 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003291 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003292 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003293
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003294 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3295 {
3296 // throwing an exception while using "silent!" causes
3297 // the function to abort but not display an error.
3298 tv = STACK_TV_BOT(-1);
3299 clear_tv(tv);
3300 tv->v_type = VAR_NUMBER;
3301 tv->vval.v_number = 0;
3302 goto done;
3303 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003304 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003305 tv = STACK_TV_BOT(0);
3306 if (tv->vval.v_string == NULL
3307 || *skipwhite(tv->vval.v_string) == NUL)
3308 {
3309 vim_free(tv->vval.v_string);
3310 SOURCING_LNUM = iptr->isn_lnum;
3311 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003312 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003313 }
3314
3315 // Inside a "catch" we need to first discard the caught
3316 // exception.
3317 if (trystack->ga_len > 0)
3318 {
3319 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3320 + trystack->ga_len - 1;
3321 if (trycmd->tcd_caught && current_exception != NULL)
3322 {
3323 // discard the exception
3324 if (caught_stack == current_exception)
3325 caught_stack = caught_stack->caught;
3326 discard_current_exception();
3327 trycmd->tcd_caught = FALSE;
3328 }
3329 }
3330
3331 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3332 == FAIL)
3333 {
3334 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003335 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003336 }
3337 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 break;
3340
3341 // compare with special values
3342 case ISN_COMPAREBOOL:
3343 case ISN_COMPARESPECIAL:
3344 {
3345 typval_T *tv1 = STACK_TV_BOT(-2);
3346 typval_T *tv2 = STACK_TV_BOT(-1);
3347 varnumber_T arg1 = tv1->vval.v_number;
3348 varnumber_T arg2 = tv2->vval.v_number;
3349 int res;
3350
3351 switch (iptr->isn_arg.op.op_type)
3352 {
3353 case EXPR_EQUAL: res = arg1 == arg2; break;
3354 case EXPR_NEQUAL: res = arg1 != arg2; break;
3355 default: res = 0; break;
3356 }
3357
Bram Moolenaar4c137212021-04-19 16:48:48 +02003358 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 tv1->v_type = VAR_BOOL;
3360 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3361 }
3362 break;
3363
3364 // Operation with two number arguments
3365 case ISN_OPNR:
3366 case ISN_COMPARENR:
3367 {
3368 typval_T *tv1 = STACK_TV_BOT(-2);
3369 typval_T *tv2 = STACK_TV_BOT(-1);
3370 varnumber_T arg1 = tv1->vval.v_number;
3371 varnumber_T arg2 = tv2->vval.v_number;
3372 varnumber_T res;
3373
3374 switch (iptr->isn_arg.op.op_type)
3375 {
3376 case EXPR_MULT: res = arg1 * arg2; break;
3377 case EXPR_DIV: res = arg1 / arg2; break;
3378 case EXPR_REM: res = arg1 % arg2; break;
3379 case EXPR_SUB: res = arg1 - arg2; break;
3380 case EXPR_ADD: res = arg1 + arg2; break;
3381
3382 case EXPR_EQUAL: res = arg1 == arg2; break;
3383 case EXPR_NEQUAL: res = arg1 != arg2; break;
3384 case EXPR_GREATER: res = arg1 > arg2; break;
3385 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3386 case EXPR_SMALLER: res = arg1 < arg2; break;
3387 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3388 default: res = 0; break;
3389 }
3390
Bram Moolenaar4c137212021-04-19 16:48:48 +02003391 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 if (iptr->isn_type == ISN_COMPARENR)
3393 {
3394 tv1->v_type = VAR_BOOL;
3395 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3396 }
3397 else
3398 tv1->vval.v_number = res;
3399 }
3400 break;
3401
3402 // Computation with two float arguments
3403 case ISN_OPFLOAT:
3404 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003405#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 {
3407 typval_T *tv1 = STACK_TV_BOT(-2);
3408 typval_T *tv2 = STACK_TV_BOT(-1);
3409 float_T arg1 = tv1->vval.v_float;
3410 float_T arg2 = tv2->vval.v_float;
3411 float_T res = 0;
3412 int cmp = FALSE;
3413
3414 switch (iptr->isn_arg.op.op_type)
3415 {
3416 case EXPR_MULT: res = arg1 * arg2; break;
3417 case EXPR_DIV: res = arg1 / arg2; break;
3418 case EXPR_SUB: res = arg1 - arg2; break;
3419 case EXPR_ADD: res = arg1 + arg2; break;
3420
3421 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3422 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3423 case EXPR_GREATER: cmp = arg1 > arg2; break;
3424 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3425 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3426 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3427 default: cmp = 0; break;
3428 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003429 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 if (iptr->isn_type == ISN_COMPAREFLOAT)
3431 {
3432 tv1->v_type = VAR_BOOL;
3433 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3434 }
3435 else
3436 tv1->vval.v_float = res;
3437 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003438#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 break;
3440
3441 case ISN_COMPARELIST:
3442 {
3443 typval_T *tv1 = STACK_TV_BOT(-2);
3444 typval_T *tv2 = STACK_TV_BOT(-1);
3445 list_T *arg1 = tv1->vval.v_list;
3446 list_T *arg2 = tv2->vval.v_list;
3447 int cmp = FALSE;
3448 int ic = iptr->isn_arg.op.op_ic;
3449
3450 switch (iptr->isn_arg.op.op_type)
3451 {
3452 case EXPR_EQUAL: cmp =
3453 list_equal(arg1, arg2, ic, FALSE); break;
3454 case EXPR_NEQUAL: cmp =
3455 !list_equal(arg1, arg2, ic, FALSE); break;
3456 case EXPR_IS: cmp = arg1 == arg2; break;
3457 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3458 default: cmp = 0; break;
3459 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003460 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 clear_tv(tv1);
3462 clear_tv(tv2);
3463 tv1->v_type = VAR_BOOL;
3464 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3465 }
3466 break;
3467
3468 case ISN_COMPAREBLOB:
3469 {
3470 typval_T *tv1 = STACK_TV_BOT(-2);
3471 typval_T *tv2 = STACK_TV_BOT(-1);
3472 blob_T *arg1 = tv1->vval.v_blob;
3473 blob_T *arg2 = tv2->vval.v_blob;
3474 int cmp = FALSE;
3475
3476 switch (iptr->isn_arg.op.op_type)
3477 {
3478 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3479 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3480 case EXPR_IS: cmp = arg1 == arg2; break;
3481 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3482 default: cmp = 0; break;
3483 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003484 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 clear_tv(tv1);
3486 clear_tv(tv2);
3487 tv1->v_type = VAR_BOOL;
3488 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3489 }
3490 break;
3491
3492 // TODO: handle separately
3493 case ISN_COMPARESTRING:
3494 case ISN_COMPAREDICT:
3495 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 case ISN_COMPAREANY:
3497 {
3498 typval_T *tv1 = STACK_TV_BOT(-2);
3499 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003500 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 int ic = iptr->isn_arg.op.op_ic;
3502
Bram Moolenaareb26f432020-09-14 16:50:05 +02003503 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003504 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003506 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 }
3508 break;
3509
3510 case ISN_ADDLIST:
3511 case ISN_ADDBLOB:
3512 {
3513 typval_T *tv1 = STACK_TV_BOT(-2);
3514 typval_T *tv2 = STACK_TV_BOT(-1);
3515
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003516 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 if (iptr->isn_type == ISN_ADDLIST)
3518 eval_addlist(tv1, tv2);
3519 else
3520 eval_addblob(tv1, tv2);
3521 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003522 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 }
3524 break;
3525
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003526 case ISN_LISTAPPEND:
3527 {
3528 typval_T *tv1 = STACK_TV_BOT(-2);
3529 typval_T *tv2 = STACK_TV_BOT(-1);
3530 list_T *l = tv1->vval.v_list;
3531
3532 // add an item to a list
3533 if (l == NULL)
3534 {
3535 SOURCING_LNUM = iptr->isn_lnum;
3536 emsg(_(e_cannot_add_to_null_list));
3537 goto on_error;
3538 }
3539 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003540 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003541 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003542 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003543 }
3544 break;
3545
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003546 case ISN_BLOBAPPEND:
3547 {
3548 typval_T *tv1 = STACK_TV_BOT(-2);
3549 typval_T *tv2 = STACK_TV_BOT(-1);
3550 blob_T *b = tv1->vval.v_blob;
3551 int error = FALSE;
3552 varnumber_T n;
3553
3554 // add a number to a blob
3555 if (b == NULL)
3556 {
3557 SOURCING_LNUM = iptr->isn_lnum;
3558 emsg(_(e_cannot_add_to_null_blob));
3559 goto on_error;
3560 }
3561 n = tv_get_number_chk(tv2, &error);
3562 if (error)
3563 goto on_error;
3564 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003565 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003566 }
3567 break;
3568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 // Computation with two arguments of unknown type
3570 case ISN_OPANY:
3571 {
3572 typval_T *tv1 = STACK_TV_BOT(-2);
3573 typval_T *tv2 = STACK_TV_BOT(-1);
3574 varnumber_T n1, n2;
3575#ifdef FEAT_FLOAT
3576 float_T f1 = 0, f2 = 0;
3577#endif
3578 int error = FALSE;
3579
3580 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3581 {
3582 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3583 {
3584 eval_addlist(tv1, tv2);
3585 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003586 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 break;
3588 }
3589 else if (tv1->v_type == VAR_BLOB
3590 && tv2->v_type == VAR_BLOB)
3591 {
3592 eval_addblob(tv1, tv2);
3593 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003594 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 break;
3596 }
3597 }
3598#ifdef FEAT_FLOAT
3599 if (tv1->v_type == VAR_FLOAT)
3600 {
3601 f1 = tv1->vval.v_float;
3602 n1 = 0;
3603 }
3604 else
3605#endif
3606 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003607 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 n1 = tv_get_number_chk(tv1, &error);
3609 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003610 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611#ifdef FEAT_FLOAT
3612 if (tv2->v_type == VAR_FLOAT)
3613 f1 = n1;
3614#endif
3615 }
3616#ifdef FEAT_FLOAT
3617 if (tv2->v_type == VAR_FLOAT)
3618 {
3619 f2 = tv2->vval.v_float;
3620 n2 = 0;
3621 }
3622 else
3623#endif
3624 {
3625 n2 = tv_get_number_chk(tv2, &error);
3626 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003627 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628#ifdef FEAT_FLOAT
3629 if (tv1->v_type == VAR_FLOAT)
3630 f2 = n2;
3631#endif
3632 }
3633#ifdef FEAT_FLOAT
3634 // if there is a float on either side the result is a float
3635 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3636 {
3637 switch (iptr->isn_arg.op.op_type)
3638 {
3639 case EXPR_MULT: f1 = f1 * f2; break;
3640 case EXPR_DIV: f1 = f1 / f2; break;
3641 case EXPR_SUB: f1 = f1 - f2; break;
3642 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003643 default: SOURCING_LNUM = iptr->isn_lnum;
3644 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003645 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 }
3647 clear_tv(tv1);
3648 clear_tv(tv2);
3649 tv1->v_type = VAR_FLOAT;
3650 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003651 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 }
3653 else
3654#endif
3655 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003656 int failed = FALSE;
3657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 switch (iptr->isn_arg.op.op_type)
3659 {
3660 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003661 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3662 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003663 goto on_error;
3664 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 case EXPR_SUB: n1 = n1 - n2; break;
3666 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003667 default: n1 = num_modulus(n1, n2, &failed);
3668 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003669 goto on_error;
3670 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 }
3672 clear_tv(tv1);
3673 clear_tv(tv2);
3674 tv1->v_type = VAR_NUMBER;
3675 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003676 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 }
3678 }
3679 break;
3680
3681 case ISN_CONCAT:
3682 {
3683 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3684 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3685 char_u *res;
3686
3687 res = concat_str(str1, str2);
3688 clear_tv(STACK_TV_BOT(-2));
3689 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 STACK_TV_BOT(-1)->vval.v_string = res;
3692 }
3693 break;
3694
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003695 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003696 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003697 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003698 int is_slice = iptr->isn_type == ISN_STRSLICE;
3699 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003700 char_u *res;
3701
3702 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003703 // string slice: string is at stack-3, first index at
3704 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003705 if (is_slice)
3706 {
3707 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003708 n1 = tv->vval.v_number;
3709 }
3710
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003711 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003712 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003713
Bram Moolenaar4c137212021-04-19 16:48:48 +02003714 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003715 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003716 if (is_slice)
3717 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003718 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003719 else
3720 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003721 // single character (including composing characters).
3722 // If the index is too big or negative the result is
3723 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003724 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003725 vim_free(tv->vval.v_string);
3726 tv->vval.v_string = res;
3727 }
3728 break;
3729
3730 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003731 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003732 case ISN_BLOBINDEX:
3733 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003735 int is_slice = iptr->isn_type == ISN_LISTSLICE
3736 || iptr->isn_type == ISN_BLOBSLICE;
3737 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3738 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003739 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003740 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741
3742 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003743 // list slice: list is at stack-3, indexes at stack-2 and
3744 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003745 // Same for blob.
3746 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747
3748 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003749 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003751
3752 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 {
Bram Moolenaared591872020-08-15 22:14:53 +02003754 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003755 n1 = tv->vval.v_number;
3756 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 }
Bram Moolenaared591872020-08-15 22:14:53 +02003758
Bram Moolenaar4c137212021-04-19 16:48:48 +02003759 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003760 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003761 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003762 if (is_blob)
3763 {
3764 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3765 n1, n2, FALSE, tv) == FAIL)
3766 goto on_error;
3767 }
3768 else
3769 {
3770 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3771 n1, n2, FALSE, tv, TRUE) == FAIL)
3772 goto on_error;
3773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 }
3775 break;
3776
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003777 case ISN_ANYINDEX:
3778 case ISN_ANYSLICE:
3779 {
3780 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3781 typval_T *var1, *var2;
3782 int res;
3783
3784 // index: composite is at stack-2, index at stack-1
3785 // slice: composite is at stack-3, indexes at stack-2 and
3786 // stack-1
3787 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003788 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003789 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3790 goto on_error;
3791 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3792 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003793 res = eval_index_inner(tv, is_slice, var1, var2,
3794 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003795 clear_tv(var1);
3796 if (is_slice)
3797 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003798 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003799 if (res == FAIL)
3800 goto on_error;
3801 }
3802 break;
3803
Bram Moolenaar9af78762020-06-16 11:34:42 +02003804 case ISN_SLICE:
3805 {
3806 list_T *list;
3807 int count = iptr->isn_arg.number;
3808
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003809 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003810 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003811 list = tv->vval.v_list;
3812
3813 // no error for short list, expect it to be checked earlier
3814 if (list != NULL && list->lv_len >= count)
3815 {
3816 list_T *newlist = list_slice(list,
3817 count, list->lv_len - 1);
3818
3819 if (newlist != NULL)
3820 {
3821 list_unref(list);
3822 tv->vval.v_list = newlist;
3823 ++newlist->lv_refcount;
3824 }
3825 }
3826 }
3827 break;
3828
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003829 case ISN_GETITEM:
3830 {
3831 listitem_T *li;
3832 int index = iptr->isn_arg.number;
3833
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003834 // Get list item: list is at stack-1, push item.
3835 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003836 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003837 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003838
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003840 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003841 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003842 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003843
3844 // Useful when used in unpack assignment. Reset at
3845 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003846 ectx->ec_where.wt_index = index + 1;
3847 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003848 }
3849 break;
3850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 case ISN_MEMBER:
3852 {
3853 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003854 char_u *key;
3855 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003856 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003857
3858 // dict member: dict is at stack-2, key at stack-1
3859 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003860 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003861 dict = tv->vval.v_dict;
3862
3863 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003864 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003865 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003866 if (key == NULL)
3867 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003868
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003869 if ((di = dict_find(dict, key, -1)) == NULL)
3870 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003871 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003872 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003873
3874 // If :silent! is used we will continue, make sure the
3875 // stack contents makes sense.
3876 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003877 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003878 tv = STACK_TV_BOT(-1);
3879 clear_tv(tv);
3880 tv->v_type = VAR_NUMBER;
3881 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003882 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003883 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003884 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003885 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003886 // Clear the dict only after getting the item, to avoid
3887 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003888 tv = STACK_TV_BOT(-1);
3889 temp_tv = *tv;
3890 copy_tv(&di->di_tv, tv);
3891 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003892 }
3893 break;
3894
3895 // dict member with string key
3896 case ISN_STRINGMEMBER:
3897 {
3898 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003900 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901
3902 tv = STACK_TV_BOT(-1);
3903 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3904 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003905 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003907 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 }
3909 dict = tv->vval.v_dict;
3910
3911 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3912 == NULL)
3913 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003914 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003916 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003918 // Clear the dict after getting the item, to avoid that it
3919 // make the item invalid.
3920 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003922 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 }
3924 break;
3925
3926 case ISN_NEGATENR:
3927 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003928 if (tv->v_type != VAR_NUMBER
3929#ifdef FEAT_FLOAT
3930 && tv->v_type != VAR_FLOAT
3931#endif
3932 )
3933 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003934 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003935 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003936 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003937 }
3938#ifdef FEAT_FLOAT
3939 if (tv->v_type == VAR_FLOAT)
3940 tv->vval.v_float = -tv->vval.v_float;
3941 else
3942#endif
3943 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 break;
3945
3946 case ISN_CHECKNR:
3947 {
3948 int error = FALSE;
3949
3950 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003951 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003953 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 (void)tv_get_number_chk(tv, &error);
3955 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003956 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 }
3958 break;
3959
3960 case ISN_CHECKTYPE:
3961 {
3962 checktype_T *ct = &iptr->isn_arg.type;
3963
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003964 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003965 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003966 if (!ectx->ec_where.wt_variable)
3967 ectx->ec_where.wt_index = ct->ct_arg_idx;
3968 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3969 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003970 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003971 if (!ectx->ec_where.wt_variable)
3972 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003973
3974 // number 0 is FALSE, number 1 is TRUE
3975 if (tv->v_type == VAR_NUMBER
3976 && ct->ct_type->tt_type == VAR_BOOL
3977 && (tv->vval.v_number == 0
3978 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003980 tv->v_type = VAR_BOOL;
3981 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003982 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 }
3984 }
3985 break;
3986
Bram Moolenaar9af78762020-06-16 11:34:42 +02003987 case ISN_CHECKLEN:
3988 {
3989 int min_len = iptr->isn_arg.checklen.cl_min_len;
3990 list_T *list = NULL;
3991
3992 tv = STACK_TV_BOT(-1);
3993 if (tv->v_type == VAR_LIST)
3994 list = tv->vval.v_list;
3995 if (list == NULL || list->lv_len < min_len
3996 || (list->lv_len > min_len
3997 && !iptr->isn_arg.checklen.cl_more_OK))
3998 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003999 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004000 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004001 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004002 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004003 }
4004 }
4005 break;
4006
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004007 case ISN_SETTYPE:
4008 {
4009 checktype_T *ct = &iptr->isn_arg.type;
4010
4011 tv = STACK_TV_BOT(-1);
4012 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4013 {
4014 free_type(tv->vval.v_dict->dv_type);
4015 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4016 }
4017 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4018 {
4019 free_type(tv->vval.v_list->lv_type);
4020 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4021 }
4022 }
4023 break;
4024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004026 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 {
4028 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004029 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004031 if (iptr->isn_type == ISN_2BOOL)
4032 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004033 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004034 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004035 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004036 n = !n;
4037 }
4038 else
4039 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004040 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004041 SOURCING_LNUM = iptr->isn_lnum;
4042 n = tv_get_bool_chk(tv, &error);
4043 if (error)
4044 goto on_error;
4045 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 clear_tv(tv);
4047 tv->v_type = VAR_BOOL;
4048 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4049 }
4050 break;
4051
4052 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004053 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004054 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004055 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4056 iptr->isn_type == ISN_2STRING_ANY,
4057 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004058 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 break;
4060
Bram Moolenaar08597872020-12-10 19:43:40 +01004061 case ISN_RANGE:
4062 {
4063 exarg_T ea;
4064 char *errormsg;
4065
Bram Moolenaarece0b872021-01-08 20:40:45 +01004066 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004067 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004068 ea.addr_type = ADDR_LINES;
4069 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004070 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004071 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004072 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004073
Bram Moolenaar4c137212021-04-19 16:48:48 +02004074 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004075 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004076 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004077 tv = STACK_TV_BOT(-1);
4078 tv->v_type = VAR_NUMBER;
4079 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004080 if (ea.addr_count == 0)
4081 tv->vval.v_number = curwin->w_cursor.lnum;
4082 else
4083 tv->vval.v_number = ea.line2;
4084 }
4085 break;
4086
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004087 case ISN_PUT:
4088 {
4089 int regname = iptr->isn_arg.put.put_regname;
4090 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4091 char_u *expr = NULL;
4092 int dir = FORWARD;
4093
Bram Moolenaar08597872020-12-10 19:43:40 +01004094 if (lnum < -2)
4095 {
4096 // line number was put on the stack by ISN_RANGE
4097 tv = STACK_TV_BOT(-1);
4098 curwin->w_cursor.lnum = tv->vval.v_number;
4099 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4100 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004101 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004102 }
4103 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004104 // :put! above cursor
4105 dir = BACKWARD;
4106 else if (lnum >= 0)
4107 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004108
4109 if (regname == '=')
4110 {
4111 tv = STACK_TV_BOT(-1);
4112 if (tv->v_type == VAR_STRING)
4113 expr = tv->vval.v_string;
4114 else
4115 {
4116 expr = typval2string(tv, TRUE); // allocates value
4117 clear_tv(tv);
4118 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004119 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004120 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004121 check_cursor();
4122 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4123 vim_free(expr);
4124 }
4125 break;
4126
Bram Moolenaar02194d22020-10-24 23:08:38 +02004127 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004128 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4129 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4130 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4131 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004132 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4133 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004134 break;
4135
Bram Moolenaar02194d22020-10-24 23:08:38 +02004136 case ISN_CMDMOD_REV:
4137 // filter regprog is owned by the instruction, don't free it
4138 cmdmod.cmod_filter_regmatch.regprog = NULL;
4139 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004140 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4141 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004142 break;
4143
Bram Moolenaar792f7862020-11-23 08:31:18 +01004144 case ISN_UNPACK:
4145 {
4146 int count = iptr->isn_arg.unpack.unp_count;
4147 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4148 list_T *l;
4149 listitem_T *li;
4150 int i;
4151
4152 // Check there is a valid list to unpack.
4153 tv = STACK_TV_BOT(-1);
4154 if (tv->v_type != VAR_LIST)
4155 {
4156 SOURCING_LNUM = iptr->isn_lnum;
4157 emsg(_(e_for_argument_must_be_sequence_of_lists));
4158 goto on_error;
4159 }
4160 l = tv->vval.v_list;
4161 if (l == NULL
4162 || l->lv_len < (semicolon ? count - 1 : count))
4163 {
4164 SOURCING_LNUM = iptr->isn_lnum;
4165 emsg(_(e_list_value_does_not_have_enough_items));
4166 goto on_error;
4167 }
4168 else if (!semicolon && l->lv_len > count)
4169 {
4170 SOURCING_LNUM = iptr->isn_lnum;
4171 emsg(_(e_list_value_has_more_items_than_targets));
4172 goto on_error;
4173 }
4174
4175 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004176 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004177 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004178 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004179
4180 // Variable after semicolon gets a list with the remaining
4181 // items.
4182 if (semicolon)
4183 {
4184 list_T *rem_list =
4185 list_alloc_with_items(l->lv_len - count + 1);
4186
4187 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004188 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004189 tv = STACK_TV_BOT(-count);
4190 tv->vval.v_list = rem_list;
4191 ++rem_list->lv_refcount;
4192 tv->v_lock = 0;
4193 li = l->lv_first;
4194 for (i = 0; i < count - 1; ++i)
4195 li = li->li_next;
4196 for (i = 0; li != NULL; ++i)
4197 {
4198 list_set_item(rem_list, i, &li->li_tv);
4199 li = li->li_next;
4200 }
4201 --count;
4202 }
4203
4204 // Produce the values in reverse order, first item last.
4205 li = l->lv_first;
4206 for (i = 0; i < count; ++i)
4207 {
4208 tv = STACK_TV_BOT(-i - 1);
4209 copy_tv(&li->li_tv, tv);
4210 li = li->li_next;
4211 }
4212
4213 list_unref(l);
4214 }
4215 break;
4216
Bram Moolenaarb2049902021-01-24 12:53:53 +01004217 case ISN_PROF_START:
4218 case ISN_PROF_END:
4219 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004220#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004221 funccall_T cookie;
4222 ufunc_T *cur_ufunc =
4223 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004224 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004225
4226 cookie.func = cur_ufunc;
4227 if (iptr->isn_type == ISN_PROF_START)
4228 {
4229 func_line_start(&cookie, iptr->isn_lnum);
4230 // if we get here the instruction is executed
4231 func_line_exec(&cookie);
4232 }
4233 else
4234 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004235#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004236 }
4237 break;
4238
Bram Moolenaare99d4222021-06-13 14:01:26 +02004239 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004240 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004241 break;
4242
Bram Moolenaar389df252020-07-09 21:20:47 +02004243 case ISN_SHUFFLE:
4244 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004245 typval_T tmp_tv;
4246 int item = iptr->isn_arg.shuffle.shfl_item;
4247 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004248
4249 tmp_tv = *STACK_TV_BOT(-item);
4250 for ( ; up > 0 && item > 1; --up)
4251 {
4252 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4253 --item;
4254 }
4255 *STACK_TV_BOT(-item) = tmp_tv;
4256 }
4257 break;
4258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004260 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004262 ectx->ec_where.wt_index = 0;
4263 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 break;
4265 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004266 continue;
4267
Bram Moolenaard032f342020-07-18 18:13:02 +02004268func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004269 // Restore previous function. If the frame pointer is where we started
4270 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004271 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004272 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004273
Bram Moolenaar4c137212021-04-19 16:48:48 +02004274 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004275 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004276 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004277 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004278
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004279on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004280 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004281 // If "emsg_silent" is set then ignore the error, unless it was set
4282 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004283 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004284 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004285 {
4286 // If a sequence of instructions causes an error while ":silent!"
4287 // was used, restore the stack length and jump ahead to restoring
4288 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004289 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004290 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004291 while (ectx->ec_stack.ga_len
4292 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004293 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004294 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004295 clear_tv(STACK_TV_BOT(0));
4296 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004297 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4298 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004299 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004300 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004301 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004302on_fatal_error:
4303 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004304 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004305 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004306 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 }
4308
4309done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004310 ret = OK;
4311theend:
4312 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4313 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004314}
4315
4316/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004317 * Execute the instructions from a VAR_INSTR typeval and put the result in
4318 * "rettv".
4319 * Return OK or FAIL.
4320 */
4321 int
4322exe_typval_instr(typval_T *tv, typval_T *rettv)
4323{
4324 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4325 isn_T *save_instr = ectx->ec_instr;
4326 int save_iidx = ectx->ec_iidx;
4327 int res;
4328
4329 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4330 res = exec_instructions(ectx);
4331 if (res == OK)
4332 {
4333 *rettv = *STACK_TV_BOT(-1);
4334 --ectx->ec_stack.ga_len;
4335 }
4336
4337 ectx->ec_instr = save_instr;
4338 ectx->ec_iidx = save_iidx;
4339
4340 return res;
4341}
4342
4343/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004344 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4345 * "substitute_instr".
4346 */
4347 char_u *
4348exe_substitute_instr(void)
4349{
4350 ectx_T *ectx = substitute_instr->subs_ectx;
4351 isn_T *save_instr = ectx->ec_instr;
4352 int save_iidx = ectx->ec_iidx;
4353 char_u *res;
4354
4355 ectx->ec_instr = substitute_instr->subs_instr;
4356 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004357 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004358 typval_T *tv = STACK_TV_BOT(-1);
4359
Bram Moolenaar27523602021-06-05 21:36:19 +02004360 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004361 --ectx->ec_stack.ga_len;
4362 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004363 }
4364 else
4365 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004366 substitute_instr->subs_status = FAIL;
4367 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369
Bram Moolenaar4c137212021-04-19 16:48:48 +02004370 ectx->ec_instr = save_instr;
4371 ectx->ec_iidx = save_iidx;
4372
4373 return res;
4374}
4375
4376/*
4377 * Call a "def" function from old Vim script.
4378 * Return OK or FAIL.
4379 */
4380 int
4381call_def_function(
4382 ufunc_T *ufunc,
4383 int argc_arg, // nr of arguments
4384 typval_T *argv, // arguments
4385 partial_T *partial, // optional partial for context
4386 typval_T *rettv) // return value
4387{
4388 ectx_T ectx; // execution context
4389 int argc = argc_arg;
4390 typval_T *tv;
4391 int idx;
4392 int ret = FAIL;
4393 int defcount = ufunc->uf_args.ga_len - argc;
4394 sctx_T save_current_sctx = current_sctx;
4395 int did_emsg_before = did_emsg_cumul + did_emsg;
4396 int save_suppress_errthrow = suppress_errthrow;
4397 msglist_T **saved_msg_list = NULL;
4398 msglist_T *private_msg_list = NULL;
4399 int save_emsg_silent_def = emsg_silent_def;
4400 int save_did_emsg_def = did_emsg_def;
4401 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004402 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004403
4404// Get pointer to item in the stack.
4405#undef STACK_TV
4406#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4407
4408// Get pointer to item at the bottom of the stack, -1 is the bottom.
4409#undef STACK_TV_BOT
4410#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4411
4412// Get pointer to a local variable on the stack. Negative for arguments.
4413#undef STACK_TV_VAR
4414#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4415
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004416 // Update uf_has_breakpoint if needed.
4417 update_has_breakpoint(ufunc);
4418
Bram Moolenaar4c137212021-04-19 16:48:48 +02004419 if (ufunc->uf_def_status == UF_NOT_COMPILED
4420 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004421 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4422 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004423 == FAIL))
4424 {
4425 if (did_emsg_cumul + did_emsg == did_emsg_before)
4426 semsg(_(e_function_is_not_compiled_str),
4427 printable_func_name(ufunc));
4428 return FAIL;
4429 }
4430
4431 {
4432 // Check the function was really compiled.
4433 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4434 + ufunc->uf_dfunc_idx;
4435 if (INSTRUCTIONS(dfunc) == NULL)
4436 {
4437 iemsg("using call_def_function() on not compiled function");
4438 return FAIL;
4439 }
4440 }
4441
4442 // If depth of calling is getting too high, don't execute the function.
4443 orig_funcdepth = funcdepth_get();
4444 if (funcdepth_increment() == FAIL)
4445 return FAIL;
4446
4447 CLEAR_FIELD(ectx);
4448 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4449 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4450 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4451 {
4452 funcdepth_decrement();
4453 return FAIL;
4454 }
4455 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4456 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4457 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004458 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004459
4460 idx = argc - ufunc->uf_args.ga_len;
4461 if (idx > 0 && ufunc->uf_va_name == NULL)
4462 {
4463 if (idx == 1)
4464 emsg(_(e_one_argument_too_many));
4465 else
4466 semsg(_(e_nr_arguments_too_many), idx);
4467 goto failed_early;
4468 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004469 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4470 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004471 {
4472 if (idx == -1)
4473 emsg(_(e_one_argument_too_few));
4474 else
4475 semsg(_(e_nr_arguments_too_few), -idx);
4476 goto failed_early;
4477 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004478
4479 // Put arguments on the stack, but no more than what the function expects.
4480 // A lambda can be called with more arguments than it uses.
4481 for (idx = 0; idx < argc
4482 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4483 ++idx)
4484 {
4485 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4486 && argv[idx].v_type == VAR_SPECIAL
4487 && argv[idx].vval.v_number == VVAL_NONE)
4488 {
4489 // Use the default value.
4490 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4491 }
4492 else
4493 {
4494 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4495 && check_typval_arg_type(
4496 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4497 goto failed_early;
4498 copy_tv(&argv[idx], STACK_TV_BOT(0));
4499 }
4500 ++ectx.ec_stack.ga_len;
4501 }
4502
4503 // Turn varargs into a list. Empty list if no args.
4504 if (ufunc->uf_va_name != NULL)
4505 {
4506 int vararg_count = argc - ufunc->uf_args.ga_len;
4507
4508 if (vararg_count < 0)
4509 vararg_count = 0;
4510 else
4511 argc -= vararg_count;
4512 if (exe_newlist(vararg_count, &ectx) == FAIL)
4513 goto failed_early;
4514
4515 // Check the type of the list items.
4516 tv = STACK_TV_BOT(-1);
4517 if (ufunc->uf_va_type != NULL
4518 && ufunc->uf_va_type != &t_list_any
4519 && ufunc->uf_va_type->tt_member != &t_any
4520 && tv->vval.v_list != NULL)
4521 {
4522 type_T *expected = ufunc->uf_va_type->tt_member;
4523 listitem_T *li = tv->vval.v_list->lv_first;
4524
4525 for (idx = 0; idx < vararg_count; ++idx)
4526 {
4527 if (check_typval_arg_type(expected, &li->li_tv,
4528 argc + idx + 1) == FAIL)
4529 goto failed_early;
4530 li = li->li_next;
4531 }
4532 }
4533
4534 if (defcount > 0)
4535 // Move varargs list to below missing default arguments.
4536 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4537 --ectx.ec_stack.ga_len;
4538 }
4539
4540 // Make space for omitted arguments, will store default value below.
4541 // Any varargs list goes after them.
4542 if (defcount > 0)
4543 for (idx = 0; idx < defcount; ++idx)
4544 {
4545 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4546 ++ectx.ec_stack.ga_len;
4547 }
4548 if (ufunc->uf_va_name != NULL)
4549 ++ectx.ec_stack.ga_len;
4550
4551 // Frame pointer points to just after arguments.
4552 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4553 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4554
4555 {
4556 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4557 + ufunc->uf_dfunc_idx;
4558 ufunc_T *base_ufunc = dfunc->df_ufunc;
4559
4560 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4561 // by copy_func().
4562 if (partial != NULL || base_ufunc->uf_partial != NULL)
4563 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004564 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4565 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004566 goto failed_early;
4567 if (partial != NULL)
4568 {
4569 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4570 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004571 if (current_ectx->ec_outer_ref != NULL
4572 && current_ectx->ec_outer_ref->or_outer != NULL)
4573 ectx.ec_outer_ref->or_outer =
4574 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004575 }
4576 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004577 {
4578 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4579 ++partial->pt_refcount;
4580 ectx.ec_outer_ref->or_partial = partial;
4581 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004582 }
4583 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004584 {
4585 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4586 ++base_ufunc->uf_partial->pt_refcount;
4587 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4588 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004589 }
4590 }
4591
4592 // dummy frame entries
4593 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4594 {
4595 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4596 ++ectx.ec_stack.ga_len;
4597 }
4598
4599 {
4600 // Reserve space for local variables and any closure reference count.
4601 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4602 + ufunc->uf_dfunc_idx;
4603
4604 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4605 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4606 ectx.ec_stack.ga_len += dfunc->df_varcount;
4607 if (dfunc->df_has_closure)
4608 {
4609 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4610 STACK_TV_VAR(idx)->vval.v_number = 0;
4611 ++ectx.ec_stack.ga_len;
4612 }
4613
4614 ectx.ec_instr = INSTRUCTIONS(dfunc);
4615 }
4616
4617 // Following errors are in the function, not the caller.
4618 // Commands behave like vim9script.
4619 estack_push_ufunc(ufunc, 1);
4620 current_sctx = ufunc->uf_script_ctx;
4621 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4622
4623 // Use a specific location for storing error messages to be converted to an
4624 // exception.
4625 saved_msg_list = msg_list;
4626 msg_list = &private_msg_list;
4627
4628 // Do turn errors into exceptions.
4629 suppress_errthrow = FALSE;
4630
4631 // Do not delete the function while executing it.
4632 ++ufunc->uf_calls;
4633
4634 // When ":silent!" was used before calling then we still abort the
4635 // function. If ":silent!" is used in the function then we don't.
4636 emsg_silent_def = emsg_silent;
4637 did_emsg_def = 0;
4638
4639 ectx.ec_where.wt_index = 0;
4640 ectx.ec_where.wt_variable = FALSE;
4641
4642 // Execute the instructions until done.
4643 ret = exec_instructions(&ectx);
4644 if (ret == OK)
4645 {
4646 // function finished, get result from the stack.
4647 if (ufunc->uf_ret_type == &t_void)
4648 {
4649 rettv->v_type = VAR_VOID;
4650 }
4651 else
4652 {
4653 tv = STACK_TV_BOT(-1);
4654 *rettv = *tv;
4655 tv->v_type = VAR_UNKNOWN;
4656 }
4657 }
4658
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004659 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004660 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4661 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004662
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004663 // Deal with any remaining closures, they may be in use somewhere.
4664 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004665 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004666 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004667 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4668 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004669
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004670 estack_pop();
4671 current_sctx = save_current_sctx;
4672
Bram Moolenaarc970e422021-03-17 15:03:04 +01004673 // TODO: when is it safe to delete the function if it is no longer used?
4674 --ufunc->uf_calls;
4675
Bram Moolenaar352134b2020-10-17 22:04:08 +02004676 if (*msg_list != NULL && saved_msg_list != NULL)
4677 {
4678 msglist_T **plist = saved_msg_list;
4679
4680 // Append entries from the current msg_list (uncaught exceptions) to
4681 // the saved msg_list.
4682 while (*plist != NULL)
4683 plist = &(*plist)->next;
4684
4685 *plist = *msg_list;
4686 }
4687 msg_list = saved_msg_list;
4688
Bram Moolenaar4c137212021-04-19 16:48:48 +02004689 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004690 {
4691 cmdmod.cmod_filter_regmatch.regprog = NULL;
4692 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004693 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004694 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004695 emsg_silent_def = save_emsg_silent_def;
4696 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004697
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004698failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004699 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4701 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004702 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004705 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004706 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004707 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004708 if (ectx.ec_outer_ref->or_outer_allocated)
4709 vim_free(ectx.ec_outer_ref->or_outer);
4710 partial_unref(ectx.ec_outer_ref->or_partial);
4711 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004712 }
4713
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004714 // Not sure if this is necessary.
4715 suppress_errthrow = save_suppress_errthrow;
4716
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004717 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004718 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004719 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004720 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 return ret;
4722}
4723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004725 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4726 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4727 * "pfx" is prefixed to every line.
4728 */
4729 static void
4730list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4731{
4732 int line_idx = 0;
4733 int prev_current = 0;
4734 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004735 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004736
4737 for (current = 0; current < instr_count; ++current)
4738 {
4739 isn_T *iptr = &instr[current];
4740 char *line;
4741
4742 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004743 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004744 while (line_idx < iptr->isn_lnum
4745 && line_idx < ufunc->uf_lines.ga_len)
4746 {
4747 if (current > prev_current)
4748 {
4749 msg_puts("\n\n");
4750 prev_current = current;
4751 }
4752 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4753 if (line != NULL)
4754 msg(line);
4755 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004756 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4757 {
4758 int first_def_arg = ufunc->uf_args.ga_len
4759 - ufunc->uf_def_args.ga_len;
4760
4761 if (def_arg_idx > 0)
4762 msg_puts("\n\n");
4763 msg_start();
4764 msg_puts(" ");
4765 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4766 first_def_arg + def_arg_idx]);
4767 msg_puts(" = ");
4768 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4769 msg_clr_eos();
4770 msg_end();
4771 }
4772 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004773
4774 switch (iptr->isn_type)
4775 {
4776 case ISN_EXEC:
4777 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4778 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004779 case ISN_EXEC_SPLIT:
4780 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4781 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004782 case ISN_LEGACY_EVAL:
4783 smsg("%s%4d EVAL legacy %s", pfx, current,
4784 iptr->isn_arg.string);
4785 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004786 case ISN_REDIRSTART:
4787 smsg("%s%4d REDIR", pfx, current);
4788 break;
4789 case ISN_REDIREND:
4790 smsg("%s%4d REDIR END%s", pfx, current,
4791 iptr->isn_arg.number ? " append" : "");
4792 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004793 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004794#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004795 smsg("%s%4d CEXPR pre %s", pfx, current,
4796 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004797#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004798 break;
4799 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004800#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004801 {
4802 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4803
4804 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4805 cexpr_get_auname(cer->cer_cmdidx),
4806 cer->cer_forceit ? "!" : "",
4807 cer->cer_cmdline);
4808 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004809#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004810 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004811 case ISN_INSTR:
4812 {
4813 smsg("%s%4d INSTR", pfx, current);
4814 list_instructions(" ", iptr->isn_arg.instr,
4815 INT_MAX, NULL);
4816 msg(" -------------");
4817 }
4818 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004819 case ISN_SUBSTITUTE:
4820 {
4821 subs_T *subs = &iptr->isn_arg.subs;
4822
4823 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4824 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4825 msg(" -------------");
4826 }
4827 break;
4828 case ISN_EXECCONCAT:
4829 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4830 (varnumber_T)iptr->isn_arg.number);
4831 break;
4832 case ISN_ECHO:
4833 {
4834 echo_T *echo = &iptr->isn_arg.echo;
4835
4836 smsg("%s%4d %s %d", pfx, current,
4837 echo->echo_with_white ? "ECHO" : "ECHON",
4838 echo->echo_count);
4839 }
4840 break;
4841 case ISN_EXECUTE:
4842 smsg("%s%4d EXECUTE %lld", pfx, current,
4843 (varnumber_T)(iptr->isn_arg.number));
4844 break;
4845 case ISN_ECHOMSG:
4846 smsg("%s%4d ECHOMSG %lld", pfx, current,
4847 (varnumber_T)(iptr->isn_arg.number));
4848 break;
4849 case ISN_ECHOERR:
4850 smsg("%s%4d ECHOERR %lld", pfx, current,
4851 (varnumber_T)(iptr->isn_arg.number));
4852 break;
4853 case ISN_LOAD:
4854 {
4855 if (iptr->isn_arg.number < 0)
4856 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4857 (varnumber_T)(iptr->isn_arg.number
4858 + STACK_FRAME_SIZE));
4859 else
4860 smsg("%s%4d LOAD $%lld", pfx, current,
4861 (varnumber_T)(iptr->isn_arg.number));
4862 }
4863 break;
4864 case ISN_LOADOUTER:
4865 {
4866 if (iptr->isn_arg.number < 0)
4867 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4868 iptr->isn_arg.outer.outer_depth,
4869 iptr->isn_arg.outer.outer_idx
4870 + STACK_FRAME_SIZE);
4871 else
4872 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4873 iptr->isn_arg.outer.outer_depth,
4874 iptr->isn_arg.outer.outer_idx);
4875 }
4876 break;
4877 case ISN_LOADV:
4878 smsg("%s%4d LOADV v:%s", pfx, current,
4879 get_vim_var_name(iptr->isn_arg.number));
4880 break;
4881 case ISN_LOADSCRIPT:
4882 {
4883 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4884 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4885 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4886 + sref->sref_idx;
4887
4888 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4889 sv->sv_name,
4890 sref->sref_idx,
4891 si->sn_name);
4892 }
4893 break;
4894 case ISN_LOADS:
4895 {
4896 scriptitem_T *si = SCRIPT_ITEM(
4897 iptr->isn_arg.loadstore.ls_sid);
4898
4899 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4900 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4901 }
4902 break;
4903 case ISN_LOADAUTO:
4904 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4905 break;
4906 case ISN_LOADG:
4907 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4908 break;
4909 case ISN_LOADB:
4910 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4911 break;
4912 case ISN_LOADW:
4913 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4914 break;
4915 case ISN_LOADT:
4916 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4917 break;
4918 case ISN_LOADGDICT:
4919 smsg("%s%4d LOAD g:", pfx, current);
4920 break;
4921 case ISN_LOADBDICT:
4922 smsg("%s%4d LOAD b:", pfx, current);
4923 break;
4924 case ISN_LOADWDICT:
4925 smsg("%s%4d LOAD w:", pfx, current);
4926 break;
4927 case ISN_LOADTDICT:
4928 smsg("%s%4d LOAD t:", pfx, current);
4929 break;
4930 case ISN_LOADOPT:
4931 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4932 break;
4933 case ISN_LOADENV:
4934 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4935 break;
4936 case ISN_LOADREG:
4937 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4938 break;
4939
4940 case ISN_STORE:
4941 if (iptr->isn_arg.number < 0)
4942 smsg("%s%4d STORE arg[%lld]", pfx, current,
4943 iptr->isn_arg.number + STACK_FRAME_SIZE);
4944 else
4945 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4946 break;
4947 case ISN_STOREOUTER:
4948 {
4949 if (iptr->isn_arg.number < 0)
4950 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4951 iptr->isn_arg.outer.outer_depth,
4952 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4953 else
4954 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4955 iptr->isn_arg.outer.outer_depth,
4956 iptr->isn_arg.outer.outer_idx);
4957 }
4958 break;
4959 case ISN_STOREV:
4960 smsg("%s%4d STOREV v:%s", pfx, current,
4961 get_vim_var_name(iptr->isn_arg.number));
4962 break;
4963 case ISN_STOREAUTO:
4964 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4965 break;
4966 case ISN_STOREG:
4967 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4968 break;
4969 case ISN_STOREB:
4970 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4971 break;
4972 case ISN_STOREW:
4973 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4974 break;
4975 case ISN_STORET:
4976 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4977 break;
4978 case ISN_STORES:
4979 {
4980 scriptitem_T *si = SCRIPT_ITEM(
4981 iptr->isn_arg.loadstore.ls_sid);
4982
4983 smsg("%s%4d STORES %s in %s", pfx, current,
4984 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4985 }
4986 break;
4987 case ISN_STORESCRIPT:
4988 {
4989 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4990 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4991 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4992 + sref->sref_idx;
4993
4994 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4995 sv->sv_name,
4996 sref->sref_idx,
4997 si->sn_name);
4998 }
4999 break;
5000 case ISN_STOREOPT:
5001 smsg("%s%4d STOREOPT &%s", pfx, current,
5002 iptr->isn_arg.storeopt.so_name);
5003 break;
5004 case ISN_STOREENV:
5005 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5006 break;
5007 case ISN_STOREREG:
5008 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
5009 break;
5010 case ISN_STORENR:
5011 smsg("%s%4d STORE %lld in $%d", pfx, current,
5012 iptr->isn_arg.storenr.stnr_val,
5013 iptr->isn_arg.storenr.stnr_idx);
5014 break;
5015
5016 case ISN_STOREINDEX:
5017 smsg("%s%4d STOREINDEX %s", pfx, current,
5018 vartype_name(iptr->isn_arg.vartype));
5019 break;
5020
5021 case ISN_STORERANGE:
5022 smsg("%s%4d STORERANGE", pfx, current);
5023 break;
5024
5025 // constants
5026 case ISN_PUSHNR:
5027 smsg("%s%4d PUSHNR %lld", pfx, current,
5028 (varnumber_T)(iptr->isn_arg.number));
5029 break;
5030 case ISN_PUSHBOOL:
5031 case ISN_PUSHSPEC:
5032 smsg("%s%4d PUSH %s", pfx, current,
5033 get_var_special_name(iptr->isn_arg.number));
5034 break;
5035 case ISN_PUSHF:
5036#ifdef FEAT_FLOAT
5037 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5038#endif
5039 break;
5040 case ISN_PUSHS:
5041 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5042 break;
5043 case ISN_PUSHBLOB:
5044 {
5045 char_u *r;
5046 char_u numbuf[NUMBUFLEN];
5047 char_u *tofree;
5048
5049 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5050 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5051 vim_free(tofree);
5052 }
5053 break;
5054 case ISN_PUSHFUNC:
5055 {
5056 char *name = (char *)iptr->isn_arg.string;
5057
5058 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5059 name == NULL ? "[none]" : name);
5060 }
5061 break;
5062 case ISN_PUSHCHANNEL:
5063#ifdef FEAT_JOB_CHANNEL
5064 {
5065 channel_T *channel = iptr->isn_arg.channel;
5066
5067 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5068 channel == NULL ? 0 : channel->ch_id);
5069 }
5070#endif
5071 break;
5072 case ISN_PUSHJOB:
5073#ifdef FEAT_JOB_CHANNEL
5074 {
5075 typval_T tv;
5076 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005077 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005078
5079 tv.v_type = VAR_JOB;
5080 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005081 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005082 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5083 }
5084#endif
5085 break;
5086 case ISN_PUSHEXC:
5087 smsg("%s%4d PUSH v:exception", pfx, current);
5088 break;
5089 case ISN_UNLET:
5090 smsg("%s%4d UNLET%s %s", pfx, current,
5091 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5092 iptr->isn_arg.unlet.ul_name);
5093 break;
5094 case ISN_UNLETENV:
5095 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5096 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5097 iptr->isn_arg.unlet.ul_name);
5098 break;
5099 case ISN_UNLETINDEX:
5100 smsg("%s%4d UNLETINDEX", pfx, current);
5101 break;
5102 case ISN_UNLETRANGE:
5103 smsg("%s%4d UNLETRANGE", pfx, current);
5104 break;
5105 case ISN_LOCKCONST:
5106 smsg("%s%4d LOCKCONST", pfx, current);
5107 break;
5108 case ISN_NEWLIST:
5109 smsg("%s%4d NEWLIST size %lld", pfx, current,
5110 (varnumber_T)(iptr->isn_arg.number));
5111 break;
5112 case ISN_NEWDICT:
5113 smsg("%s%4d NEWDICT size %lld", pfx, current,
5114 (varnumber_T)(iptr->isn_arg.number));
5115 break;
5116
5117 // function call
5118 case ISN_BCALL:
5119 {
5120 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5121
5122 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5123 internal_func_name(cbfunc->cbf_idx),
5124 cbfunc->cbf_argcount);
5125 }
5126 break;
5127 case ISN_DCALL:
5128 {
5129 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5130 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5131 + cdfunc->cdf_idx;
5132
5133 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5134 df->df_ufunc->uf_name_exp != NULL
5135 ? df->df_ufunc->uf_name_exp
5136 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5137 }
5138 break;
5139 case ISN_UCALL:
5140 {
5141 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5142
5143 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5144 cufunc->cuf_name, cufunc->cuf_argcount);
5145 }
5146 break;
5147 case ISN_PCALL:
5148 {
5149 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5150
5151 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5152 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5153 }
5154 break;
5155 case ISN_PCALL_END:
5156 smsg("%s%4d PCALL end", pfx, current);
5157 break;
5158 case ISN_RETURN:
5159 smsg("%s%4d RETURN", pfx, current);
5160 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005161 case ISN_RETURN_VOID:
5162 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005163 break;
5164 case ISN_FUNCREF:
5165 {
5166 funcref_T *funcref = &iptr->isn_arg.funcref;
5167 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5168 + funcref->fr_func;
5169
5170 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5171 }
5172 break;
5173
5174 case ISN_NEWFUNC:
5175 {
5176 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5177
5178 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5179 newfunc->nf_lambda, newfunc->nf_global);
5180 }
5181 break;
5182
5183 case ISN_DEF:
5184 {
5185 char_u *name = iptr->isn_arg.string;
5186
5187 smsg("%s%4d DEF %s", pfx, current,
5188 name == NULL ? (char_u *)"" : name);
5189 }
5190 break;
5191
5192 case ISN_JUMP:
5193 {
5194 char *when = "?";
5195
5196 switch (iptr->isn_arg.jump.jump_when)
5197 {
5198 case JUMP_ALWAYS:
5199 when = "JUMP";
5200 break;
5201 case JUMP_AND_KEEP_IF_TRUE:
5202 when = "JUMP_AND_KEEP_IF_TRUE";
5203 break;
5204 case JUMP_IF_FALSE:
5205 when = "JUMP_IF_FALSE";
5206 break;
5207 case JUMP_AND_KEEP_IF_FALSE:
5208 when = "JUMP_AND_KEEP_IF_FALSE";
5209 break;
5210 case JUMP_IF_COND_FALSE:
5211 when = "JUMP_IF_COND_FALSE";
5212 break;
5213 case JUMP_IF_COND_TRUE:
5214 when = "JUMP_IF_COND_TRUE";
5215 break;
5216 }
5217 smsg("%s%4d %s -> %d", pfx, current, when,
5218 iptr->isn_arg.jump.jump_where);
5219 }
5220 break;
5221
5222 case ISN_JUMP_IF_ARG_SET:
5223 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5224 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5225 iptr->isn_arg.jump.jump_where);
5226 break;
5227
5228 case ISN_FOR:
5229 {
5230 forloop_T *forloop = &iptr->isn_arg.forloop;
5231
5232 smsg("%s%4d FOR $%d -> %d", pfx, current,
5233 forloop->for_idx, forloop->for_end);
5234 }
5235 break;
5236
5237 case ISN_TRY:
5238 {
5239 try_T *try = &iptr->isn_arg.try;
5240
5241 if (try->try_ref->try_finally == 0)
5242 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5243 pfx, current,
5244 try->try_ref->try_catch,
5245 try->try_ref->try_endtry);
5246 else
5247 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5248 pfx, current,
5249 try->try_ref->try_catch,
5250 try->try_ref->try_finally,
5251 try->try_ref->try_endtry);
5252 }
5253 break;
5254 case ISN_CATCH:
5255 // TODO
5256 smsg("%s%4d CATCH", pfx, current);
5257 break;
5258 case ISN_TRYCONT:
5259 {
5260 trycont_T *trycont = &iptr->isn_arg.trycont;
5261
5262 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5263 trycont->tct_levels,
5264 trycont->tct_levels == 1 ? "" : "s",
5265 trycont->tct_where);
5266 }
5267 break;
5268 case ISN_FINALLY:
5269 smsg("%s%4d FINALLY", pfx, current);
5270 break;
5271 case ISN_ENDTRY:
5272 smsg("%s%4d ENDTRY", pfx, current);
5273 break;
5274 case ISN_THROW:
5275 smsg("%s%4d THROW", pfx, current);
5276 break;
5277
5278 // expression operations on number
5279 case ISN_OPNR:
5280 case ISN_OPFLOAT:
5281 case ISN_OPANY:
5282 {
5283 char *what;
5284 char *ins;
5285
5286 switch (iptr->isn_arg.op.op_type)
5287 {
5288 case EXPR_MULT: what = "*"; break;
5289 case EXPR_DIV: what = "/"; break;
5290 case EXPR_REM: what = "%"; break;
5291 case EXPR_SUB: what = "-"; break;
5292 case EXPR_ADD: what = "+"; break;
5293 default: what = "???"; break;
5294 }
5295 switch (iptr->isn_type)
5296 {
5297 case ISN_OPNR: ins = "OPNR"; break;
5298 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5299 case ISN_OPANY: ins = "OPANY"; break;
5300 default: ins = "???"; break;
5301 }
5302 smsg("%s%4d %s %s", pfx, current, ins, what);
5303 }
5304 break;
5305
5306 case ISN_COMPAREBOOL:
5307 case ISN_COMPARESPECIAL:
5308 case ISN_COMPARENR:
5309 case ISN_COMPAREFLOAT:
5310 case ISN_COMPARESTRING:
5311 case ISN_COMPAREBLOB:
5312 case ISN_COMPARELIST:
5313 case ISN_COMPAREDICT:
5314 case ISN_COMPAREFUNC:
5315 case ISN_COMPAREANY:
5316 {
5317 char *p;
5318 char buf[10];
5319 char *type;
5320
5321 switch (iptr->isn_arg.op.op_type)
5322 {
5323 case EXPR_EQUAL: p = "=="; break;
5324 case EXPR_NEQUAL: p = "!="; break;
5325 case EXPR_GREATER: p = ">"; break;
5326 case EXPR_GEQUAL: p = ">="; break;
5327 case EXPR_SMALLER: p = "<"; break;
5328 case EXPR_SEQUAL: p = "<="; break;
5329 case EXPR_MATCH: p = "=~"; break;
5330 case EXPR_IS: p = "is"; break;
5331 case EXPR_ISNOT: p = "isnot"; break;
5332 case EXPR_NOMATCH: p = "!~"; break;
5333 default: p = "???"; break;
5334 }
5335 STRCPY(buf, p);
5336 if (iptr->isn_arg.op.op_ic == TRUE)
5337 strcat(buf, "?");
5338 switch(iptr->isn_type)
5339 {
5340 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5341 case ISN_COMPARESPECIAL:
5342 type = "COMPARESPECIAL"; break;
5343 case ISN_COMPARENR: type = "COMPARENR"; break;
5344 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5345 case ISN_COMPARESTRING:
5346 type = "COMPARESTRING"; break;
5347 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5348 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5349 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5350 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5351 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5352 default: type = "???"; break;
5353 }
5354
5355 smsg("%s%4d %s %s", pfx, current, type, buf);
5356 }
5357 break;
5358
5359 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5360 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5361
5362 // expression operations
5363 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5364 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5365 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5366 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5367 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5368 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5369 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5370 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5371 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5372 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5373 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5374 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5375 pfx, current, iptr->isn_arg.number); break;
5376 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5377 pfx, current, iptr->isn_arg.number); break;
5378 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5379 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5380 iptr->isn_arg.string); break;
5381 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5382
5383 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5384 case ISN_CHECKTYPE:
5385 {
5386 checktype_T *ct = &iptr->isn_arg.type;
5387 char *tofree;
5388
5389 if (ct->ct_arg_idx == 0)
5390 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5391 type_name(ct->ct_type, &tofree),
5392 (int)ct->ct_off);
5393 else
5394 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5395 type_name(ct->ct_type, &tofree),
5396 (int)ct->ct_off,
5397 (int)ct->ct_arg_idx);
5398 vim_free(tofree);
5399 break;
5400 }
5401 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5402 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5403 iptr->isn_arg.checklen.cl_min_len);
5404 break;
5405 case ISN_SETTYPE:
5406 {
5407 char *tofree;
5408
5409 smsg("%s%4d SETTYPE %s", pfx, current,
5410 type_name(iptr->isn_arg.type.ct_type, &tofree));
5411 vim_free(tofree);
5412 break;
5413 }
5414 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005415 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5416 smsg("%s%4d INVERT %d (!val)", pfx, current,
5417 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005418 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005419 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5420 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005421 break;
5422 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005423 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005424 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005425 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5426 pfx, current,
5427 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005428 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005429 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5430 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005431 break;
5432 case ISN_PUT:
5433 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5434 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005435 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005436 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5437 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005438 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005439 else
5440 smsg("%s%4d PUT %c %ld", pfx, current,
5441 iptr->isn_arg.put.put_regname,
5442 (long)iptr->isn_arg.put.put_lnum);
5443 break;
5444
5445 // TODO: summarize modifiers
5446 case ISN_CMDMOD:
5447 {
5448 char_u *buf;
5449 size_t len = produce_cmdmods(
5450 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5451
5452 buf = alloc(len + 1);
5453 if (buf != NULL)
5454 {
5455 (void)produce_cmdmods(
5456 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5457 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5458 vim_free(buf);
5459 }
5460 break;
5461 }
5462 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5463
5464 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005465 smsg("%s%4d PROFILE START line %d", pfx, current,
5466 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005467 break;
5468
5469 case ISN_PROF_END:
5470 smsg("%s%4d PROFILE END", pfx, current);
5471 break;
5472
Bram Moolenaare99d4222021-06-13 14:01:26 +02005473 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005474 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5475 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005476 break;
5477
Bram Moolenaar4c137212021-04-19 16:48:48 +02005478 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5479 iptr->isn_arg.unpack.unp_count,
5480 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5481 break;
5482 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5483 iptr->isn_arg.shuffle.shfl_item,
5484 iptr->isn_arg.shuffle.shfl_up);
5485 break;
5486 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5487
5488 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5489 return;
5490 }
5491
5492 out_flush(); // output one line at a time
5493 ui_breakcheck();
5494 if (got_int)
5495 break;
5496 }
5497}
5498
5499/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005500 * Handle command line completion for the :disassemble command.
5501 */
5502 void
5503set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5504{
5505 char_u *p;
5506
5507 // Default: expand user functions, "debug" and "profile"
5508 xp->xp_context = EXPAND_DISASSEMBLE;
5509 xp->xp_pattern = arg;
5510
5511 // first argument already typed: only user function names
5512 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5513 {
5514 xp->xp_context = EXPAND_USER_FUNC;
5515 xp->xp_pattern = skipwhite(p);
5516 }
5517}
5518
5519/*
5520 * Function given to ExpandGeneric() to obtain the list of :disassemble
5521 * arguments.
5522 */
5523 char_u *
5524get_disassemble_argument(expand_T *xp, int idx)
5525{
5526 if (idx == 0)
5527 return (char_u *)"debug";
5528 if (idx == 1)
5529 return (char_u *)"profile";
5530 return get_user_func_name(xp, idx - 2);
5531}
5532
5533/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005534 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005535 * We don't really need this at runtime, but we do have tests that require it,
5536 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005537 */
5538 void
5539ex_disassemble(exarg_T *eap)
5540{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005541 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005542 char_u *fname;
5543 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 dfunc_T *dfunc;
5545 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005546 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005547 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005548 compiletype_T compile_type = CT_NONE;
5549
5550 if (STRNCMP(arg, "profile", 7) == 0)
5551 {
5552 compile_type = CT_PROFILE;
5553 arg = skipwhite(arg + 7);
5554 }
5555 else if (STRNCMP(arg, "debug", 5) == 0)
5556 {
5557 compile_type = CT_DEBUG;
5558 arg = skipwhite(arg + 5);
5559 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005560
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005561 if (STRNCMP(arg, "<lambda>", 8) == 0)
5562 {
5563 arg += 8;
5564 (void)getdigits(&arg);
5565 fname = vim_strnsave(eap->arg, arg - eap->arg);
5566 }
5567 else
5568 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005569 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005570 if (fname == NULL)
5571 {
5572 semsg(_(e_invarg2), eap->arg);
5573 return;
5574 }
5575
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005576 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005577 if (ufunc == NULL)
5578 {
5579 char_u *p = untrans_function_name(fname);
5580
5581 if (p != NULL)
5582 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005583 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005584 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005585 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005586 if (ufunc == NULL)
5587 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005588 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005589 return;
5590 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005591 if (func_needs_compiling(ufunc, compile_type)
5592 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005593 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005594 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005596 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597 return;
5598 }
5599 if (ufunc->uf_name_exp != NULL)
5600 msg((char *)ufunc->uf_name_exp);
5601 else
5602 msg((char *)ufunc->uf_name);
5603
5604 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005605 switch (compile_type)
5606 {
5607 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005608#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005609 instr = dfunc->df_instr_prof;
5610 instr_count = dfunc->df_instr_prof_count;
5611 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005612#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005613 // FALLTHROUGH
5614 case CT_NONE:
5615 instr = dfunc->df_instr;
5616 instr_count = dfunc->df_instr_count;
5617 break;
5618 case CT_DEBUG:
5619 instr = dfunc->df_instr_debug;
5620 instr_count = dfunc->df_instr_debug_count;
5621 break;
5622 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005623
Bram Moolenaar4c137212021-04-19 16:48:48 +02005624 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625}
5626
5627/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005628 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629 * list, etc. Mostly like what JavaScript does, except that empty list and
5630 * empty dictionary are FALSE.
5631 */
5632 int
5633tv2bool(typval_T *tv)
5634{
5635 switch (tv->v_type)
5636 {
5637 case VAR_NUMBER:
5638 return tv->vval.v_number != 0;
5639 case VAR_FLOAT:
5640#ifdef FEAT_FLOAT
5641 return tv->vval.v_float != 0.0;
5642#else
5643 break;
5644#endif
5645 case VAR_PARTIAL:
5646 return tv->vval.v_partial != NULL;
5647 case VAR_FUNC:
5648 case VAR_STRING:
5649 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5650 case VAR_LIST:
5651 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5652 case VAR_DICT:
5653 return tv->vval.v_dict != NULL
5654 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5655 case VAR_BOOL:
5656 case VAR_SPECIAL:
5657 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5658 case VAR_JOB:
5659#ifdef FEAT_JOB_CHANNEL
5660 return tv->vval.v_job != NULL;
5661#else
5662 break;
5663#endif
5664 case VAR_CHANNEL:
5665#ifdef FEAT_JOB_CHANNEL
5666 return tv->vval.v_channel != NULL;
5667#else
5668 break;
5669#endif
5670 case VAR_BLOB:
5671 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5672 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005673 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005675 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676 break;
5677 }
5678 return FALSE;
5679}
5680
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005681 void
5682emsg_using_string_as(typval_T *tv, int as_number)
5683{
5684 semsg(_(as_number ? e_using_string_as_number_str
5685 : e_using_string_as_bool_str),
5686 tv->vval.v_string == NULL
5687 ? (char_u *)"" : tv->vval.v_string);
5688}
5689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690/*
5691 * If "tv" is a string give an error and return FAIL.
5692 */
5693 int
5694check_not_string(typval_T *tv)
5695{
5696 if (tv->v_type == VAR_STRING)
5697 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005698 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699 clear_tv(tv);
5700 return FAIL;
5701 }
5702 return OK;
5703}
5704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005705
5706#endif // FEAT_EVAL