blob: e464732dda8da2b359e4fbd303a0fa60aab93063 [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100152 * This adds a stack frame and sets the instruction pointer to the start of the
153 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200154 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 *
156 * Stack has:
157 * - current arguments (already there)
158 * - omitted optional argument (default values) added here
159 * - stack frame:
160 * - pointer to calling function
161 * - Index of next instruction in calling function
162 * - previous frame pointer
163 * - reserved space for local variables
164 */
165 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100166call_dfunc(
167 int cdf_idx,
168 partial_T *pt,
169 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100170 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100172 int argcount = argcount_arg;
173 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
174 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200175 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100176 int arg_to_add;
177 int vararg_count = 0;
178 int varcount;
179 int idx;
180 estack_T *entry;
181 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182
183 if (dfunc->df_deleted)
184 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100185 // don't use ufunc->uf_name, it may have been freed
186 emsg_funcname(e_func_deleted,
187 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 return FAIL;
189 }
190
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100191#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100192 if (do_profiling == PROF_YES)
193 {
194 if (ga_grow(&profile_info_ga, 1) == OK)
195 {
196 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
197 + profile_info_ga.ga_len;
198 ++profile_info_ga.ga_len;
199 CLEAR_POINTER(info);
200 profile_may_start_func(info, ufunc,
201 (((dfunc_T *)def_functions.ga_data)
202 + ectx->ec_dfunc_idx)->df_ufunc);
203 }
204
205 // Profiling might be enabled/disabled along the way. This should not
206 // fail, since the function was compiled before and toggling profiling
207 // doesn't change any errors.
Bram Moolenaare99d4222021-06-13 14:01:26 +0200208 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
209 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100210 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100211 return FAIL;
212 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100213#endif
214
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200215 // When debugging and using "cont" switches to the not-debugged
216 // instructions, may need to still compile them.
217 if ((func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
218 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
219 == FAIL)
220 || INSTRUCTIONS(dfunc) == NULL)
221 {
222 if (did_emsg_cumul + did_emsg == did_emsg_before)
223 semsg(_(e_function_is_not_compiled_str),
224 printable_func_name(ufunc));
225 return FAIL;
226 }
227
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200228 if (ufunc->uf_va_name != NULL)
229 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200230 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200231 // Stack at time of call with 2 varargs:
232 // normal_arg
233 // optional_arg
234 // vararg_1
235 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200236 // After creating the list:
237 // normal_arg
238 // optional_arg
239 // vararg-list
240 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200241 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200242 // After creating the list
243 // normal_arg
244 // (space for optional_arg)
245 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200246 vararg_count = argcount - ufunc->uf_args.ga_len;
247 if (vararg_count < 0)
248 vararg_count = 0;
249 else
250 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200251 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200252 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200253
254 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200255 }
256
Bram Moolenaarfe270812020-04-11 22:31:27 +0200257 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200258 if (arg_to_add < 0)
259 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200260 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200261 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200262 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200263 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200264 return FAIL;
265 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200266
267 // Reserve space for:
268 // - missing arguments
269 // - stack frame
270 // - local variables
271 // - if needed: a counter for number of closures created in
272 // ectx->ec_funcrefs.
273 varcount = dfunc->df_varcount + dfunc->df_has_closure;
274 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
275 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 return FAIL;
277
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100278 // If depth of calling is getting too high, don't execute the function.
279 if (funcdepth_increment() == FAIL)
280 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200281 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100282
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100283 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200284 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100285 {
286 floc = ALLOC_ONE(funclocal_T);
287 if (floc == NULL)
288 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200289 *floc = ectx->ec_funclocal;
290 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100291 }
292
Bram Moolenaarfe270812020-04-11 22:31:27 +0200293 // Move the vararg-list to below the missing optional arguments.
294 if (vararg_count > 0 && arg_to_add > 0)
295 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100296
297 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200298 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200299 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200300 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301
302 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100303 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
304 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200305 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200306 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
307 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100308 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100309 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200310 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311
312 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200313 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200315 if (dfunc->df_has_closure)
316 {
317 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
318
319 tv->v_type = VAR_NUMBER;
320 tv->vval.v_number = 0;
321 }
322 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100324 if (pt != NULL || ufunc->uf_partial != NULL
325 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100326 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200327 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100328
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200329 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100330 return FAIL;
331 if (pt != NULL)
332 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200333 ref->or_outer = &pt->pt_outer;
334 ++pt->pt_refcount;
335 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100336 }
337 else if (ufunc->uf_partial != NULL)
338 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200339 ref->or_outer = &ufunc->uf_partial->pt_outer;
340 ++ufunc->uf_partial->pt_refcount;
341 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100342 }
343 else
344 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200345 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
346 if (ref->or_outer == NULL)
347 {
348 vim_free(ref);
349 return FAIL;
350 }
351 ref->or_outer_allocated = TRUE;
352 ref->or_outer->out_stack = &ectx->ec_stack;
353 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
354 if (ectx->ec_outer_ref != NULL)
355 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100356 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200357 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100358 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100359 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200360 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100361
Bram Moolenaarc970e422021-03-17 15:03:04 +0100362 ++ufunc->uf_calls;
363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364 // Set execution state to the start of the called function.
365 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100366 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100367 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200368 if (entry != NULL)
369 {
370 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200371 // Save the current context so it can be restored on return.
372 entry->es_save_sctx = current_sctx;
373 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200374 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100375
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200376 // Start execution at the first instruction.
377 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378
379 return OK;
380}
381
382// Get pointer to item in the stack.
383#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
384
385/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200386 * Used when returning from a function: Check if any closure is still
387 * referenced. If so then move the arguments and variables to a separate piece
388 * of stack to be used when the closure is called.
389 * When "free_arguments" is TRUE the arguments are to be freed.
390 * Returns FAIL when out of memory.
391 */
392 static int
393handle_closure_in_use(ectx_T *ectx, int free_arguments)
394{
395 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
396 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200397 int argcount;
398 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 int idx;
400 typval_T *tv;
401 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200402 garray_T *gap = &ectx->ec_funcrefs;
403 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200405 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200406 return OK; // function was freed
407 if (dfunc->df_has_closure == 0)
408 return OK; // no closures
409 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
410 closure_count = tv->vval.v_number;
411 if (closure_count == 0)
412 return OK; // no funcrefs created
413
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200414 argcount = ufunc_argcount(dfunc->df_ufunc);
415 top = ectx->ec_frame_idx - argcount;
416
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200418 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200419 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200420 partial_T *pt;
421 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200422
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200423 if (off < 0)
424 continue; // count is off or already done
425 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200426 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200427 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200428 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200429 int i;
430
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200431 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200432 // unreferenced on return.
433 for (i = 0; i < dfunc->df_varcount; ++i)
434 {
435 typval_T *stv = STACK_TV(ectx->ec_frame_idx
436 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200437 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200438 --refcount;
439 }
440 if (refcount > 1)
441 {
442 closure_in_use = TRUE;
443 break;
444 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200445 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200446 }
447
448 if (closure_in_use)
449 {
450 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
451 typval_T *stack;
452
453 // A closure is using the arguments and/or local variables.
454 // Move them to the called function.
455 if (funcstack == NULL)
456 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200457 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
458 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200459 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
460 funcstack->fs_ga.ga_data = stack;
461 if (stack == NULL)
462 {
463 vim_free(funcstack);
464 return FAIL;
465 }
466
467 // Move or copy the arguments.
468 for (idx = 0; idx < argcount; ++idx)
469 {
470 tv = STACK_TV(top + idx);
471 if (free_arguments)
472 {
473 *(stack + idx) = *tv;
474 tv->v_type = VAR_UNKNOWN;
475 }
476 else
477 copy_tv(tv, stack + idx);
478 }
479 // Move the local variables.
480 for (idx = 0; idx < dfunc->df_varcount; ++idx)
481 {
482 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200483
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200484 // A partial created for a local function, that is also used as a
485 // local variable, has a reference count for the variable, thus
486 // will never go down to zero. When all these refcounts are one
487 // then the funcstack is unused. We need to count how many we have
488 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200489 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
490 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200491 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200492
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200493 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200494 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
495 gap->ga_len - closure_count + i])
496 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200497 }
498
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200499 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200500 tv->v_type = VAR_UNKNOWN;
501 }
502
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200503 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200504 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200505 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
506 - closure_count + idx];
507 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200508 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200509 ++funcstack->fs_refcount;
510 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100511 pt->pt_outer.out_stack = &funcstack->fs_ga;
512 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200513 }
514 }
515 }
516
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200517 for (idx = 0; idx < closure_count; ++idx)
518 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
519 - closure_count + idx]);
520 gap->ga_len -= closure_count;
521 if (gap->ga_len == 0)
522 ga_clear(gap);
523
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200524 return OK;
525}
526
527/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200528 * Called when a partial is freed or its reference count goes down to one. The
529 * funcstack may be the only reference to the partials in the local variables.
530 * Go over all of them, the funcref and can be freed if all partials
531 * referencing the funcstack have a reference count of one.
532 */
533 void
534funcstack_check_refcount(funcstack_T *funcstack)
535{
536 int i;
537 garray_T *gap = &funcstack->fs_ga;
538 int done = 0;
539
540 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
541 return;
542 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
543 {
544 typval_T *tv = ((typval_T *)gap->ga_data) + i;
545
546 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
547 && tv->vval.v_partial->pt_funcstack == funcstack
548 && tv->vval.v_partial->pt_refcount == 1)
549 ++done;
550 }
551 if (done == funcstack->fs_min_refcount)
552 {
553 typval_T *stack = gap->ga_data;
554
555 // All partials referencing the funcstack have a reference count of
556 // one, thus the funcstack is no longer of use.
557 for (i = 0; i < gap->ga_len; ++i)
558 clear_tv(stack + i);
559 vim_free(stack);
560 vim_free(funcstack);
561 }
562}
563
564/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565 * Return from the current function.
566 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200567 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200568func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100571 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200572 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
573 + ectx->ec_dfunc_idx;
574 int argcount = ufunc_argcount(dfunc->df_ufunc);
575 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200576 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100577 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
578 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200579 funclocal_T *floc;
580#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100581 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
582 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583
Bram Moolenaar12d26532021-02-19 19:13:21 +0100584 if (do_profiling == PROF_YES)
585 {
586 ufunc_T *caller = prev_dfunc->df_ufunc;
587
588 if (dfunc->df_ufunc->uf_profiling
589 || (caller != NULL && caller->uf_profiling))
590 {
591 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
592 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
593 --profile_info_ga.ga_len;
594 }
595 }
596#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100597 // TODO: when is it safe to delete the function when it is no longer used?
598 --dfunc->df_ufunc->uf_calls;
599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200601 entry = estack_pop();
602 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200603 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200605 if (handle_closure_in_use(ectx, TRUE) == FAIL)
606 return FAIL;
607
608 // Clear the arguments.
609 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
610 clear_tv(STACK_TV(idx));
611
612 // Clear local variables and temp values, but not the return value.
613 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100614 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100616
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100617 // The return value should be on top of the stack. However, when aborting
618 // it may not be there and ec_frame_idx is the top of the stack.
619 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100620 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100621 ret_idx = 0;
622
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200623 if (ectx->ec_outer_ref != NULL)
624 {
625 if (ectx->ec_outer_ref->or_outer_allocated)
626 vim_free(ectx->ec_outer_ref->or_outer);
627 partial_unref(ectx->ec_outer_ref->or_partial);
628 vim_free(ectx->ec_outer_ref);
629 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100630
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100631 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100632 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100633 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
634 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200635 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
636 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200637 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100638 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100639 floc = (void *)STACK_TV(ectx->ec_frame_idx
640 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200641 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100642 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
643 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200644
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100645 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200646 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100647 else
648 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200649 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100650 vim_free(floc);
651 }
652
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100653 if (ret_idx > 0)
654 {
655 // Reset the stack to the position before the call, with a spot for the
656 // return value, moved there from above the frame.
657 ectx->ec_stack.ga_len = top + 1;
658 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
659 }
660 else
661 // Reset the stack to the position before the call.
662 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200663
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100664 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200665 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200666 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667}
668
669#undef STACK_TV
670
671/*
672 * Prepare arguments and rettv for calling a builtin or user function.
673 */
674 static int
675call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
676{
677 int idx;
678 typval_T *tv;
679
680 // Move arguments from bottom of the stack to argvars[] and add terminator.
681 for (idx = 0; idx < argcount; ++idx)
682 argvars[idx] = *STACK_TV_BOT(idx - argcount);
683 argvars[argcount].v_type = VAR_UNKNOWN;
684
685 // Result replaces the arguments on the stack.
686 if (argcount > 0)
687 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200688 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 return FAIL;
690 else
691 ++ectx->ec_stack.ga_len;
692
693 // Default return value is zero.
694 tv = STACK_TV_BOT(-1);
695 tv->v_type = VAR_NUMBER;
696 tv->vval.v_number = 0;
697
698 return OK;
699}
700
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200701// Ugly global to avoid passing the execution context around through many
702// layers.
703static ectx_T *current_ectx = NULL;
704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705/*
706 * Call a builtin function by index.
707 */
708 static int
709call_bfunc(int func_idx, int argcount, ectx_T *ectx)
710{
711 typval_T argvars[MAX_FUNC_ARGS];
712 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100713 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200714 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715
716 if (call_prepare(argcount, argvars, ectx) == FAIL)
717 return FAIL;
718
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200719 // Call the builtin function. Set "current_ectx" so that when it
720 // recursively invokes call_def_function() a closure context can be set.
721 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200723 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724
725 // Clear the arguments.
726 for (idx = 0; idx < argcount; ++idx)
727 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200728
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100729 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200730 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 return OK;
732}
733
734/*
735 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100736 * If the function is compiled this will add a stack frame and set the
737 * instruction pointer at the start of the function.
738 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200739 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100740 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 */
742 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100743call_ufunc(
744 ufunc_T *ufunc,
745 partial_T *pt,
746 int argcount,
747 ectx_T *ectx,
748 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749{
750 typval_T argvars[MAX_FUNC_ARGS];
751 funcexe_T funcexe;
752 int error;
753 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100754 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200755 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756
Bram Moolenaare99d4222021-06-13 14:01:26 +0200757 if (func_needs_compiling(ufunc, compile_type)
758 && compile_def_function(ufunc, FALSE, compile_type, NULL)
759 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200760 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200761 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100762 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100763 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100764 if (error != FCERR_UNKNOWN)
765 {
766 if (error == FCERR_TOOMANY)
767 semsg(_(e_toomanyarg), ufunc->uf_name);
768 else
769 semsg(_(e_toofewarg), ufunc->uf_name);
770 return FAIL;
771 }
772
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100773 // The function has been compiled, can call it quickly. For a function
774 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100775 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100776 if (iptr != NULL)
777 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100778 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100779 iptr->isn_type = ISN_DCALL;
780 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
781 iptr->isn_arg.dfunc.cdf_argcount = argcount;
782 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200783 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785
786 if (call_prepare(argcount, argvars, ectx) == FAIL)
787 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200788 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 funcexe.evaluate = TRUE;
790
791 // Call the user function. Result goes in last position on the stack.
792 // TODO: add selfdict if there is one
793 error = call_user_func_check(ufunc, argcount, argvars,
794 STACK_TV_BOT(-1), &funcexe, NULL);
795
796 // Clear the arguments.
797 for (idx = 0; idx < argcount; ++idx)
798 clear_tv(&argvars[idx]);
799
800 if (error != FCERR_NONE)
801 {
802 user_func_error(error, ufunc->uf_name);
803 return FAIL;
804 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100805 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200806 // Error other than from calling the function itself.
807 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 return OK;
809}
810
811/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100812 * If command modifiers were applied restore them.
813 */
814 static void
815may_restore_cmdmod(funclocal_T *funclocal)
816{
817 if (funclocal->floc_restore_cmdmod)
818 {
819 cmdmod.cmod_filter_regmatch.regprog = NULL;
820 undo_cmdmod(&cmdmod);
821 cmdmod = funclocal->floc_save_cmdmod;
822 funclocal->floc_restore_cmdmod = FALSE;
823 }
824}
825
826/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200827 * Return TRUE if an error was given or CTRL-C was pressed.
828 */
829 static int
830vim9_aborting(int prev_called_emsg)
831{
832 return called_emsg > prev_called_emsg || got_int || did_throw;
833}
834
835/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 * Execute a function by "name".
837 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100838 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 * Returns FAIL if not found without an error message.
840 */
841 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100842call_by_name(
843 char_u *name,
844 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100845 ectx_T *ectx,
846 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847{
848 ufunc_T *ufunc;
849
850 if (builtin_function(name, -1))
851 {
852 int func_idx = find_internal_func(name);
853
854 if (func_idx < 0)
855 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200856 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 return FAIL;
858 return call_bfunc(func_idx, argcount, ectx);
859 }
860
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200861 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200862
863 if (ufunc == NULL)
864 {
865 int called_emsg_before = called_emsg;
866
867 if (script_autoload(name, TRUE))
868 // loaded a package, search for the function again
869 ufunc = find_func(name, FALSE, NULL);
870 if (vim9_aborting(called_emsg_before))
871 return FAIL; // bail out if loading the script caused an error
872 }
873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100875 {
876 if (ufunc->uf_arg_types != NULL)
877 {
878 int i;
879 typval_T *argv = STACK_TV_BOT(0) - argcount;
880
881 // The function can change at runtime, check that the argument
882 // types are correct.
883 for (i = 0; i < argcount; ++i)
884 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100885 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100886
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100887 if (i < ufunc->uf_args.ga_len)
888 type = ufunc->uf_arg_types[i];
889 else if (ufunc->uf_va_type != NULL)
890 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100891 if (type != NULL && check_typval_arg_type(type,
892 &argv[i], i + 1) == FAIL)
893 return FAIL;
894 }
895 }
896
Bram Moolenaar4c137212021-04-19 16:48:48 +0200897 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100898 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899
900 return FAIL;
901}
902
903 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100904call_partial(
905 typval_T *tv,
906 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100907 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200909 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200910 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100912 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913
914 if (tv->v_type == VAR_PARTIAL)
915 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200916 partial_T *pt = tv->vval.v_partial;
917 int i;
918
919 if (pt->pt_argc > 0)
920 {
921 // Make space for arguments from the partial, shift the "argcount"
922 // arguments up.
923 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
924 return FAIL;
925 for (i = 1; i <= argcount; ++i)
926 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
927 ectx->ec_stack.ga_len += pt->pt_argc;
928 argcount += pt->pt_argc;
929
930 // copy the arguments from the partial onto the stack
931 for (i = 0; i < pt->pt_argc; ++i)
932 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200936 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 name = pt->pt_name;
939 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200940 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200942 if (name != NULL)
943 {
944 char_u fname_buf[FLEN_FIXED + 1];
945 char_u *tofree = NULL;
946 int error = FCERR_NONE;
947 char_u *fname;
948
949 // May need to translate <SNR>123_ to K_SNR.
950 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
951 if (error != FCERR_NONE)
952 res = FAIL;
953 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200954 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200955 vim_free(tofree);
956 }
957
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100958 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 {
960 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200961 semsg(_(e_unknownfunc),
962 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 return FAIL;
964 }
965 return OK;
966}
967
968/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200969 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
970 * TRUE.
971 */
972 static int
973error_if_locked(int lock, char *error)
974{
975 if (lock & (VAR_LOCKED | VAR_FIXED))
976 {
977 emsg(_(error));
978 return TRUE;
979 }
980 return FALSE;
981}
982
983/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100984 * Give an error if "tv" is not a number and return FAIL.
985 */
986 static int
987check_for_number(typval_T *tv)
988{
989 if (tv->v_type != VAR_NUMBER)
990 {
991 semsg(_(e_expected_str_but_got_str),
992 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
993 return FAIL;
994 }
995 return OK;
996}
997
998/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100999 * Store "tv" in variable "name".
1000 * This is for s: and g: variables.
1001 */
1002 static void
1003store_var(char_u *name, typval_T *tv)
1004{
1005 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001006 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001007
Bram Moolenaard877a572021-04-01 19:42:48 +02001008 if (tv->v_lock)
1009 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001010 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001011 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001012 restore_funccal();
1013}
1014
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001015/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001016 * Convert "tv" to a string.
1017 * Return FAIL if not allowed.
1018 */
1019 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001020do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001021{
1022 if (tv->v_type != VAR_STRING)
1023 {
1024 char_u *str;
1025
1026 if (is_2string_any)
1027 {
1028 switch (tv->v_type)
1029 {
1030 case VAR_SPECIAL:
1031 case VAR_BOOL:
1032 case VAR_NUMBER:
1033 case VAR_FLOAT:
1034 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001035
1036 case VAR_LIST:
1037 if (tolerant)
1038 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001039 char_u *s, *e, *p;
1040 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001041
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001042 ga_init2(&ga, sizeof(char_u *), 1);
1043
1044 // Convert to NL separated items, then
1045 // escape the items and replace the NL with
1046 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001047 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001048 if (str == NULL)
1049 return FAIL;
1050 s = str;
1051 while ((e = vim_strchr(s, '\n')) != NULL)
1052 {
1053 *e = NUL;
1054 p = vim_strsave_fnameescape(s, FALSE);
1055 if (p != NULL)
1056 {
1057 ga_concat(&ga, p);
1058 ga_concat(&ga, (char_u *)" ");
1059 vim_free(p);
1060 }
1061 s = e + 1;
1062 }
1063 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001064 clear_tv(tv);
1065 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001066 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001067 return OK;
1068 }
1069 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001070 default: to_string_error(tv->v_type);
1071 return FAIL;
1072 }
1073 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001074 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001075 clear_tv(tv);
1076 tv->v_type = VAR_STRING;
1077 tv->vval.v_string = str;
1078 }
1079 return OK;
1080}
1081
1082/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001083 * When the value of "sv" is a null list of dict, allocate it.
1084 */
1085 static void
1086allocate_if_null(typval_T *tv)
1087{
1088 switch (tv->v_type)
1089 {
1090 case VAR_LIST:
1091 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001092 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001093 break;
1094 case VAR_DICT:
1095 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001096 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001097 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001098 case VAR_BLOB:
1099 if (tv->vval.v_blob == NULL)
1100 (void)rettv_blob_alloc(tv);
1101 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001102 default:
1103 break;
1104 }
1105}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001106
Bram Moolenaare7525c52021-01-09 13:20:37 +01001107/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001108 * Return the character "str[index]" where "index" is the character index,
1109 * including composing characters.
1110 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001111 */
1112 char_u *
1113char_from_string(char_u *str, varnumber_T index)
1114{
1115 size_t nbyte = 0;
1116 varnumber_T nchar = index;
1117 size_t slen;
1118
1119 if (str == NULL)
1120 return NULL;
1121 slen = STRLEN(str);
1122
Bram Moolenaarff871402021-03-26 13:34:05 +01001123 // Do the same as for a list: a negative index counts from the end.
1124 // Optimization to check the first byte to be below 0x80 (and no composing
1125 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001126 if (index < 0)
1127 {
1128 int clen = 0;
1129
1130 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001131 {
1132 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1133 ++nbyte;
1134 else if (enc_utf8)
1135 nbyte += utfc_ptr2len(str + nbyte);
1136 else
1137 nbyte += mb_ptr2len(str + nbyte);
1138 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001139 nchar = clen + index;
1140 if (nchar < 0)
1141 // unlike list: index out of range results in empty string
1142 return NULL;
1143 }
1144
1145 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001146 {
1147 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1148 ++nbyte;
1149 else if (enc_utf8)
1150 nbyte += utfc_ptr2len(str + nbyte);
1151 else
1152 nbyte += mb_ptr2len(str + nbyte);
1153 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001154 if (nbyte >= slen)
1155 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001156 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001157}
1158
1159/*
1160 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001161 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001162 * If going over the end return "str_len".
1163 * If "idx" is negative count from the end, -1 is the last character.
1164 * When going over the start return -1.
1165 */
1166 static long
1167char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1168{
1169 varnumber_T nchar = idx;
1170 size_t nbyte = 0;
1171
1172 if (nchar >= 0)
1173 {
1174 while (nchar > 0 && nbyte < str_len)
1175 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001176 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001177 --nchar;
1178 }
1179 }
1180 else
1181 {
1182 nbyte = str_len;
1183 while (nchar < 0 && nbyte > 0)
1184 {
1185 --nbyte;
1186 nbyte -= mb_head_off(str, str + nbyte);
1187 ++nchar;
1188 }
1189 if (nchar < 0)
1190 return -1;
1191 }
1192 return (long)nbyte;
1193}
1194
1195/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001196 * Return the slice "str[first : last]" using character indexes. Composing
1197 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001198 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001199 * Return NULL when the result is empty.
1200 */
1201 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001202string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001203{
1204 long start_byte, end_byte;
1205 size_t slen;
1206
1207 if (str == NULL)
1208 return NULL;
1209 slen = STRLEN(str);
1210 start_byte = char_idx2byte(str, slen, first);
1211 if (start_byte < 0)
1212 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001213 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001214 end_byte = (long)slen;
1215 else
1216 {
1217 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001218 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001219 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001220 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001221 }
1222
1223 if (start_byte >= (long)slen || end_byte <= start_byte)
1224 return NULL;
1225 return vim_strnsave(str + start_byte, end_byte - start_byte);
1226}
1227
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001228 static svar_T *
1229get_script_svar(scriptref_T *sref, ectx_T *ectx)
1230{
1231 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1232 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1233 + ectx->ec_dfunc_idx;
1234 svar_T *sv;
1235
1236 if (sref->sref_seq != si->sn_script_seq)
1237 {
1238 // The script was reloaded after the function was
1239 // compiled, the script_idx may not be valid.
1240 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1241 dfunc->df_ufunc->uf_name_exp);
1242 return NULL;
1243 }
1244 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1245 if (!equal_type(sv->sv_type, sref->sref_type))
1246 {
1247 emsg(_(e_script_variable_type_changed));
1248 return NULL;
1249 }
1250 return sv;
1251}
1252
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001253/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001254 * Function passed to do_cmdline() for splitting a script joined by NL
1255 * characters.
1256 */
1257 static char_u *
1258get_split_sourceline(
1259 int c UNUSED,
1260 void *cookie,
1261 int indent UNUSED,
1262 getline_opt_T options UNUSED)
1263{
1264 source_cookie_T *sp = (source_cookie_T *)cookie;
1265 char_u *p;
1266 char_u *line;
1267
1268 if (*sp->nextline == NUL)
1269 return NULL;
1270 p = vim_strchr(sp->nextline, '\n');
1271 if (p == NULL)
1272 {
1273 line = vim_strsave(sp->nextline);
1274 sp->nextline += STRLEN(sp->nextline);
1275 }
1276 else
1277 {
1278 line = vim_strnsave(sp->nextline, p - sp->nextline);
1279 sp->nextline = p + 1;
1280 }
1281 return line;
1282}
1283
1284/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 * Execute a function by "name".
1286 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001287 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 */
1289 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001290call_eval_func(
1291 char_u *name,
1292 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001293 ectx_T *ectx,
1294 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295{
Bram Moolenaared677f52020-08-12 16:38:10 +02001296 int called_emsg_before = called_emsg;
1297 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298
Bram Moolenaar4c137212021-04-19 16:48:48 +02001299 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001300 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001302 dictitem_T *v;
1303
1304 v = find_var(name, NULL, FALSE);
1305 if (v == NULL)
1306 {
1307 semsg(_(e_unknownfunc), name);
1308 return FAIL;
1309 }
1310 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1311 {
1312 semsg(_(e_unknownfunc), name);
1313 return FAIL;
1314 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001315 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001317 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318}
1319
1320/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001321 * When a function reference is used, fill a partial with the information
1322 * needed, especially when it is used as a closure.
1323 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001324 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001325fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1326{
1327 pt->pt_func = ufunc;
1328 pt->pt_refcount = 1;
1329
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001330 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001331 {
1332 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1333 + ectx->ec_dfunc_idx;
1334
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001335 // The closure may need to find arguments and local variables in the
1336 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001337 pt->pt_outer.out_stack = &ectx->ec_stack;
1338 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001339 if (ectx->ec_outer_ref != NULL)
1340 {
1341 // The current context already has a context, link to that one.
1342 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1343 if (ectx->ec_outer_ref->or_partial != NULL)
1344 {
1345 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1346 ++pt->pt_outer.out_up_partial->pt_refcount;
1347 }
1348 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001349
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001350 // If this function returns and the closure is still being used, we
1351 // need to make a copy of the context (arguments and local variables).
1352 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001353 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1354 {
1355 vim_free(pt);
1356 return FAIL;
1357 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001358 // Extra variable keeps the count of closures created in the current
1359 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001360 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1361 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1362
1363 ((partial_T **)ectx->ec_funcrefs.ga_data)
1364 [ectx->ec_funcrefs.ga_len] = pt;
1365 ++pt->pt_refcount;
1366 ++ectx->ec_funcrefs.ga_len;
1367 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001368 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001369 return OK;
1370}
1371
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001372// used for v_instr of typval of VAR_INSTR
1373struct instr_S {
1374 ectx_T *instr_ectx;
1375 isn_T *instr_instr;
1376};
1377
Bram Moolenaar4c137212021-04-19 16:48:48 +02001378// used for substitute_instr
1379typedef struct subs_expr_S {
1380 ectx_T *subs_ectx;
1381 isn_T *subs_instr;
1382 int subs_status;
1383} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384
1385// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001386#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387
1388// Get pointer to item at the bottom of the stack, -1 is the bottom.
1389#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001390#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 +01001391
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001392// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001393#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 +01001394
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001395// Set when calling do_debug().
1396static ectx_T *debug_context = NULL;
1397static int debug_arg_count;
1398
1399/*
1400 * When debugging lookup "name" and return the typeval.
1401 * When not found return NULL.
1402 */
1403 typval_T *
1404lookup_debug_var(char_u *name)
1405{
1406 int idx;
1407 dfunc_T *dfunc;
1408 ectx_T *ectx = debug_context;
1409
1410 if (ectx == NULL)
1411 return NULL;
1412 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1413
1414 // Go through the local variable names, from last to first.
1415 for (idx = debug_arg_count - 1; idx >= 0; --idx)
1416 {
1417 char_u *s = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1418 if (STRCMP(s, name) == 0)
1419 return STACK_TV_VAR(idx);
1420 }
1421
1422 return NULL;
1423}
1424
Bram Moolenaar4c137212021-04-19 16:48:48 +02001425/*
1426 * Execute instructions in execution context "ectx".
1427 * Return OK or FAIL;
1428 */
1429 static int
1430exec_instructions(ectx_T *ectx)
1431{
1432 int breakcheck_count = 0;
1433 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001434 int ret = FAIL;
1435 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001436
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001437 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001438 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001439
Bram Moolenaarff652882021-05-16 15:24:49 +02001440 // Only catch exceptions in this instruction list.
1441 ectx->ec_trylevel_at_start = trylevel;
1442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 for (;;)
1444 {
1445 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001446
Bram Moolenaar270d0382020-05-15 21:42:53 +02001447 if (++breakcheck_count >= 100)
1448 {
1449 line_breakcheck();
1450 breakcheck_count = 0;
1451 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001452 if (got_int)
1453 {
1454 // Turn CTRL-C into an exception.
1455 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001456 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001457 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001458 did_throw = TRUE;
1459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460
Bram Moolenaara26b9702020-04-18 19:53:28 +02001461 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1462 {
1463 // Turn an error message into an exception.
1464 did_emsg = FALSE;
1465 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001466 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001467 did_throw = TRUE;
1468 *msg_list = NULL;
1469 }
1470
Bram Moolenaar4c137212021-04-19 16:48:48 +02001471 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001473 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001474 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475
1476 // An exception jumps to the first catch, finally, or returns from
1477 // the current function.
1478 if (trystack->ga_len > 0)
1479 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001480 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 {
1482 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001483 ectx->ec_in_catch = TRUE;
1484 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 }
1486 else
1487 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001488 // Not inside try or need to return from current functions.
1489 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001490 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001491 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001492 tv = STACK_TV_BOT(0);
1493 tv->v_type = VAR_NUMBER;
1494 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001495 ++ectx->ec_stack.ga_len;
1496 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001498 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001499 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001500 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001501 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502 goto done;
1503 }
1504
Bram Moolenaar4c137212021-04-19 16:48:48 +02001505 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001506 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 }
1508 continue;
1509 }
1510
Bram Moolenaar4c137212021-04-19 16:48:48 +02001511 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 switch (iptr->isn_type)
1513 {
1514 // execute Ex command line
1515 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001516 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001517 source_cookie_T cookie;
1518
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001519 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001520 // Pass getsourceline to get an error for a missing ":end"
1521 // command.
1522 CLEAR_FIELD(cookie);
1523 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1524 if (do_cmdline(iptr->isn_arg.string,
1525 getsourceline, &cookie,
1526 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1527 == FAIL
1528 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001529 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 break;
1532
Bram Moolenaar20677332021-06-06 17:02:53 +02001533 // execute Ex command line split at NL characters.
1534 case ISN_EXEC_SPLIT:
1535 {
1536 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001537 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001538
1539 SOURCING_LNUM = iptr->isn_lnum;
1540 CLEAR_FIELD(cookie);
1541 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1542 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001543 line = get_split_sourceline(0, &cookie, 0, 0);
1544 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001545 get_split_sourceline, &cookie,
1546 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1547 == FAIL
1548 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001549 {
1550 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001551 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001552 }
1553 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001554 }
1555 break;
1556
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001557 // Evaluate an expression with legacy syntax, push it onto the
1558 // stack.
1559 case ISN_LEGACY_EVAL:
1560 {
1561 char_u *arg = iptr->isn_arg.string;
1562 int res;
1563 int save_flags = cmdmod.cmod_flags;
1564
1565 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001566 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001567 tv = STACK_TV_BOT(0);
1568 init_tv(tv);
1569 cmdmod.cmod_flags |= CMOD_LEGACY;
1570 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1571 cmdmod.cmod_flags = save_flags;
1572 if (res == FAIL)
1573 goto on_error;
1574 ++ectx->ec_stack.ga_len;
1575 }
1576 break;
1577
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001578 // push typeval VAR_INSTR with instructions to be executed
1579 case ISN_INSTR:
1580 {
1581 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001582 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001583 tv = STACK_TV_BOT(0);
1584 tv->vval.v_instr = ALLOC_ONE(instr_T);
1585 if (tv->vval.v_instr == NULL)
1586 goto on_error;
1587 ++ectx->ec_stack.ga_len;
1588
1589 tv->v_type = VAR_INSTR;
1590 tv->vval.v_instr->instr_ectx = ectx;
1591 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1592 }
1593 break;
1594
Bram Moolenaar4c137212021-04-19 16:48:48 +02001595 // execute :substitute with an expression
1596 case ISN_SUBSTITUTE:
1597 {
1598 subs_T *subs = &iptr->isn_arg.subs;
1599 source_cookie_T cookie;
1600 struct subs_expr_S *save_instr = substitute_instr;
1601 struct subs_expr_S subs_instr;
1602 int res;
1603
1604 subs_instr.subs_ectx = ectx;
1605 subs_instr.subs_instr = subs->subs_instr;
1606 subs_instr.subs_status = OK;
1607 substitute_instr = &subs_instr;
1608
1609 SOURCING_LNUM = iptr->isn_lnum;
1610 // This is very much like ISN_EXEC
1611 CLEAR_FIELD(cookie);
1612 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1613 res = do_cmdline(subs->subs_cmd,
1614 getsourceline, &cookie,
1615 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1616 substitute_instr = save_instr;
1617
1618 if (res == FAIL || did_emsg
1619 || subs_instr.subs_status == FAIL)
1620 goto on_error;
1621 }
1622 break;
1623
1624 case ISN_FINISH:
1625 goto done;
1626
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001627 case ISN_REDIRSTART:
1628 // create a dummy entry for var_redir_str()
1629 if (alloc_redir_lval() == FAIL)
1630 goto on_error;
1631
1632 // The output is stored in growarray "redir_ga" until
1633 // redirection ends.
1634 init_redir_ga();
1635 redir_vname = 1;
1636 break;
1637
1638 case ISN_REDIREND:
1639 {
1640 char_u *res = get_clear_redir_ga();
1641
1642 // End redirection, put redirected text on the stack.
1643 clear_redir_lval();
1644 redir_vname = 0;
1645
1646 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1647 {
1648 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001649 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001650 }
1651 tv = STACK_TV_BOT(0);
1652 tv->v_type = VAR_STRING;
1653 tv->vval.v_string = res;
1654 ++ectx->ec_stack.ga_len;
1655 }
1656 break;
1657
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001658 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001659#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001660 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1661 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001662#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001663 break;
1664
1665 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001666#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001667 {
1668 exarg_T ea;
1669 int res;
1670
1671 CLEAR_FIELD(ea);
1672 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1673 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1674 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1675 --ectx->ec_stack.ga_len;
1676 tv = STACK_TV_BOT(0);
1677 res = cexpr_core(&ea, tv);
1678 clear_tv(tv);
1679 if (res == FAIL)
1680 goto on_error;
1681 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001682#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001683 break;
1684
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001685 // execute Ex command from pieces on the stack
1686 case ISN_EXECCONCAT:
1687 {
1688 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001689 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001690 int pass;
1691 int i;
1692 char_u *cmd = NULL;
1693 char_u *str;
1694
1695 for (pass = 1; pass <= 2; ++pass)
1696 {
1697 for (i = 0; i < count; ++i)
1698 {
1699 tv = STACK_TV_BOT(i - count);
1700 str = tv->vval.v_string;
1701 if (str != NULL && *str != NUL)
1702 {
1703 if (pass == 2)
1704 STRCPY(cmd + len, str);
1705 len += STRLEN(str);
1706 }
1707 if (pass == 2)
1708 clear_tv(tv);
1709 }
1710 if (pass == 1)
1711 {
1712 cmd = alloc(len + 1);
1713 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001714 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001715 len = 0;
1716 }
1717 }
1718
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001719 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001720 do_cmdline_cmd(cmd);
1721 vim_free(cmd);
1722 }
1723 break;
1724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 // execute :echo {string} ...
1726 case ISN_ECHO:
1727 {
1728 int count = iptr->isn_arg.echo.echo_count;
1729 int atstart = TRUE;
1730 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001731 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
1733 for (idx = 0; idx < count; ++idx)
1734 {
1735 tv = STACK_TV_BOT(idx - count);
1736 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1737 &atstart, &needclr);
1738 clear_tv(tv);
1739 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001740 if (needclr)
1741 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001742 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 }
1744 break;
1745
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001746 // :execute {string} ...
1747 // :echomsg {string} ...
1748 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001749 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001750 case ISN_ECHOMSG:
1751 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001752 {
1753 int count = iptr->isn_arg.number;
1754 garray_T ga;
1755 char_u buf[NUMBUFLEN];
1756 char_u *p;
1757 int len;
1758 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001759 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001760
1761 ga_init2(&ga, 1, 80);
1762 for (idx = 0; idx < count; ++idx)
1763 {
1764 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001765 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001766 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001767 if (tv->v_type == VAR_CHANNEL
1768 || tv->v_type == VAR_JOB)
1769 {
1770 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001771 semsg(_(e_using_invalid_value_as_string_str),
1772 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001773 break;
1774 }
1775 else
1776 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001777 }
1778 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001779 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001780
1781 len = (int)STRLEN(p);
1782 if (ga_grow(&ga, len + 2) == FAIL)
1783 failed = TRUE;
1784 else
1785 {
1786 if (ga.ga_len > 0)
1787 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1788 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1789 ga.ga_len += len;
1790 }
1791 clear_tv(tv);
1792 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001793 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001794 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001795 {
1796 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001797 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001798 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001799
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001800 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001801 {
1802 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001803 {
1804 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001805 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001806 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001807 {
1808 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001809 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001810 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001811 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001812 else
1813 {
1814 msg_sb_eol();
1815 if (iptr->isn_type == ISN_ECHOMSG)
1816 {
1817 msg_attr(ga.ga_data, echo_attr);
1818 out_flush();
1819 }
1820 else
1821 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001822 SOURCING_LNUM = iptr->isn_lnum;
1823 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001824 }
1825 }
1826 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001827 ga_clear(&ga);
1828 }
1829 break;
1830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 // load local variable or argument
1832 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001833 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001834 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001836 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 break;
1838
1839 // load v: variable
1840 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001841 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001842 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001844 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 break;
1846
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001847 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 case ISN_LOADSCRIPT:
1849 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001850 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 svar_T *sv;
1852
Bram Moolenaar4c137212021-04-19 16:48:48 +02001853 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001854 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001855 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001856 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001857 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001858 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001860 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 }
1862 break;
1863
1864 // load s: variable in old script
1865 case ISN_LOADS:
1866 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001867 hashtab_T *ht = &SCRIPT_VARS(
1868 iptr->isn_arg.loadstore.ls_sid);
1869 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 if (di == NULL)
1873 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001874 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001875 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001876 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 }
1878 else
1879 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001880 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001881 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001883 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 }
1885 }
1886 break;
1887
Bram Moolenaard3aac292020-04-19 14:32:17 +02001888 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001890 case ISN_LOADB:
1891 case ISN_LOADW:
1892 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001894 dictitem_T *di = NULL;
1895 hashtab_T *ht = NULL;
1896 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001897
Bram Moolenaard3aac292020-04-19 14:32:17 +02001898 switch (iptr->isn_type)
1899 {
1900 case ISN_LOADG:
1901 ht = get_globvar_ht();
1902 namespace = 'g';
1903 break;
1904 case ISN_LOADB:
1905 ht = &curbuf->b_vars->dv_hashtab;
1906 namespace = 'b';
1907 break;
1908 case ISN_LOADW:
1909 ht = &curwin->w_vars->dv_hashtab;
1910 namespace = 'w';
1911 break;
1912 case ISN_LOADT:
1913 ht = &curtab->tp_vars->dv_hashtab;
1914 namespace = 't';
1915 break;
1916 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001917 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001918 }
1919 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 if (di == NULL)
1922 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001923 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001924 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001925 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001926 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 }
1928 else
1929 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001930 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001931 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001933 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 }
1935 }
1936 break;
1937
Bram Moolenaar03290b82020-12-19 16:30:44 +01001938 // load autoload variable
1939 case ISN_LOADAUTO:
1940 {
1941 char_u *name = iptr->isn_arg.string;
1942
Bram Moolenaar4c137212021-04-19 16:48:48 +02001943 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001944 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001945 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001946 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001947 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001948 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001949 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001950 }
1951 break;
1952
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001953 // load g:/b:/w:/t: namespace
1954 case ISN_LOADGDICT:
1955 case ISN_LOADBDICT:
1956 case ISN_LOADWDICT:
1957 case ISN_LOADTDICT:
1958 {
1959 dict_T *d = NULL;
1960
1961 switch (iptr->isn_type)
1962 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001963 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1964 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1965 case ISN_LOADWDICT: d = curwin->w_vars; break;
1966 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001967 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001968 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001969 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001970 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001971 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001972 tv = STACK_TV_BOT(0);
1973 tv->v_type = VAR_DICT;
1974 tv->v_lock = 0;
1975 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001976 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001977 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001978 }
1979 break;
1980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 // load &option
1982 case ISN_LOADOPT:
1983 {
1984 typval_T optval;
1985 char_u *name = iptr->isn_arg.string;
1986
Bram Moolenaara8c17702020-04-01 21:17:24 +02001987 // This is not expected to fail, name is checked during
1988 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001989 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001990 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001991 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001992 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001994 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 }
1996 break;
1997
1998 // load $ENV
1999 case ISN_LOADENV:
2000 {
2001 typval_T optval;
2002 char_u *name = iptr->isn_arg.string;
2003
Bram Moolenaar4c137212021-04-19 16:48:48 +02002004 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002005 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002006 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002007 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002009 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 }
2011 break;
2012
2013 // load @register
2014 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002015 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002016 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 tv = STACK_TV_BOT(0);
2018 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002019 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002020 // This may result in NULL, which should be equivalent to an
2021 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 tv->vval.v_string = get_reg_contents(
2023 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002024 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 break;
2026
2027 // store local variable
2028 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002029 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 tv = STACK_TV_VAR(iptr->isn_arg.number);
2031 clear_tv(tv);
2032 *tv = *STACK_TV_BOT(0);
2033 break;
2034
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002035 // store s: variable in old script
2036 case ISN_STORES:
2037 {
2038 hashtab_T *ht = &SCRIPT_VARS(
2039 iptr->isn_arg.loadstore.ls_sid);
2040 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002041 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002042
Bram Moolenaar4c137212021-04-19 16:48:48 +02002043 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002044 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002045 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002046 else
2047 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002048 SOURCING_LNUM = iptr->isn_lnum;
2049 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002050 {
2051 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002052 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002053 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002054 clear_tv(&di->di_tv);
2055 di->di_tv = *STACK_TV_BOT(0);
2056 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002057 }
2058 break;
2059
2060 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 case ISN_STORESCRIPT:
2062 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002063 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2064 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065
Bram Moolenaar4c137212021-04-19 16:48:48 +02002066 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002067 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002068 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002069 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002070
2071 // "const" and "final" are checked at compile time, locking
2072 // the value needs to be checked here.
2073 SOURCING_LNUM = iptr->isn_lnum;
2074 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002075 {
2076 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002077 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002078 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 clear_tv(sv->sv_tv);
2081 *sv->sv_tv = *STACK_TV_BOT(0);
2082 }
2083 break;
2084
2085 // store option
2086 case ISN_STOREOPT:
2087 {
2088 long n = 0;
2089 char_u *s = NULL;
2090 char *msg;
2091
Bram Moolenaar4c137212021-04-19 16:48:48 +02002092 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 tv = STACK_TV_BOT(0);
2094 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002095 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002097 if (s == NULL)
2098 s = (char_u *)"";
2099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002101 // must be VAR_NUMBER, CHECKTYPE makes sure
2102 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2104 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002105 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 if (msg != NULL)
2107 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002108 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002110 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 }
2113 break;
2114
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002115 // store $ENV
2116 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002117 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002118 tv = STACK_TV_BOT(0);
2119 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2120 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002121 break;
2122
2123 // store @r
2124 case ISN_STOREREG:
2125 {
2126 int reg = iptr->isn_arg.number;
2127
Bram Moolenaar4c137212021-04-19 16:48:48 +02002128 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002129 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002130 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002131 tv_get_string(tv), -1, FALSE);
2132 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002133 }
2134 break;
2135
2136 // store v: variable
2137 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002138 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002139 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2140 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002141 // should not happen, type is checked when compiling
2142 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002143 break;
2144
Bram Moolenaard3aac292020-04-19 14:32:17 +02002145 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002147 case ISN_STOREB:
2148 case ISN_STOREW:
2149 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002151 dictitem_T *di;
2152 hashtab_T *ht;
2153 char_u *name = iptr->isn_arg.string + 2;
2154
Bram Moolenaard3aac292020-04-19 14:32:17 +02002155 switch (iptr->isn_type)
2156 {
2157 case ISN_STOREG:
2158 ht = get_globvar_ht();
2159 break;
2160 case ISN_STOREB:
2161 ht = &curbuf->b_vars->dv_hashtab;
2162 break;
2163 case ISN_STOREW:
2164 ht = &curwin->w_vars->dv_hashtab;
2165 break;
2166 case ISN_STORET:
2167 ht = &curtab->tp_vars->dv_hashtab;
2168 break;
2169 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002170 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172
Bram Moolenaar4c137212021-04-19 16:48:48 +02002173 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002174 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002176 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 else
2178 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002179 SOURCING_LNUM = iptr->isn_lnum;
2180 if (var_check_permission(di, name) == FAIL)
2181 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 clear_tv(&di->di_tv);
2183 di->di_tv = *STACK_TV_BOT(0);
2184 }
2185 }
2186 break;
2187
Bram Moolenaar03290b82020-12-19 16:30:44 +01002188 // store an autoload variable
2189 case ISN_STOREAUTO:
2190 SOURCING_LNUM = iptr->isn_lnum;
2191 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2192 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002193 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002194 break;
2195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 // store number in local variable
2197 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002198 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 clear_tv(tv);
2200 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002201 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 break;
2203
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002204 // store value in list or dict variable
2205 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002206 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002207 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002208 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002209 typval_T *tv_dest = STACK_TV_BOT(-1);
2210 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002211
Bram Moolenaar752fc692021-01-04 21:57:11 +01002212 // Stack contains:
2213 // -3 value to be stored
2214 // -2 index
2215 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002216 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002217 SOURCING_LNUM = iptr->isn_lnum;
2218 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002219 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002220 dest_type = tv_dest->v_type;
2221 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002222 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002223 else if (dest_type == VAR_LIST
2224 && tv_idx->v_type != VAR_NUMBER)
2225 {
2226 emsg(_(e_number_exp));
2227 status = FAIL;
2228 }
2229 }
2230 else if (dest_type != tv_dest->v_type)
2231 {
2232 // just in case, should be OK
2233 semsg(_(e_expected_str_but_got_str),
2234 vartype_name(dest_type),
2235 vartype_name(tv_dest->v_type));
2236 status = FAIL;
2237 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002238
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002239 if (status == OK && dest_type == VAR_LIST)
2240 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002241 long lidx = (long)tv_idx->vval.v_number;
2242 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002243
2244 if (list == NULL)
2245 {
2246 emsg(_(e_list_not_set));
2247 goto on_error;
2248 }
2249 if (lidx < 0 && list->lv_len + lidx >= 0)
2250 // negative index is relative to the end
2251 lidx = list->lv_len + lidx;
2252 if (lidx < 0 || lidx > list->lv_len)
2253 {
2254 semsg(_(e_listidx), lidx);
2255 goto on_error;
2256 }
2257 if (lidx < list->lv_len)
2258 {
2259 listitem_T *li = list_find(list, lidx);
2260
2261 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002262 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002263 goto on_error;
2264 // overwrite existing list item
2265 clear_tv(&li->li_tv);
2266 li->li_tv = *tv;
2267 }
2268 else
2269 {
2270 if (error_if_locked(list->lv_lock,
2271 e_cannot_change_list))
2272 goto on_error;
2273 // append to list, only fails when out of memory
2274 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002275 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002276 clear_tv(tv);
2277 }
2278 }
2279 else if (status == OK && dest_type == VAR_DICT)
2280 {
2281 char_u *key = tv_idx->vval.v_string;
2282 dict_T *dict = tv_dest->vval.v_dict;
2283 dictitem_T *di;
2284
2285 SOURCING_LNUM = iptr->isn_lnum;
2286 if (dict == NULL)
2287 {
2288 emsg(_(e_dictionary_not_set));
2289 goto on_error;
2290 }
2291 if (key == NULL)
2292 key = (char_u *)"";
2293 di = dict_find(dict, key, -1);
2294 if (di != NULL)
2295 {
2296 if (error_if_locked(di->di_tv.v_lock,
2297 e_cannot_change_dict_item))
2298 goto on_error;
2299 // overwrite existing value
2300 clear_tv(&di->di_tv);
2301 di->di_tv = *tv;
2302 }
2303 else
2304 {
2305 if (error_if_locked(dict->dv_lock,
2306 e_cannot_change_dict))
2307 goto on_error;
2308 // add to dict, only fails when out of memory
2309 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002310 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002311 clear_tv(tv);
2312 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002313 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002314 else if (status == OK && dest_type == VAR_BLOB)
2315 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002316 long lidx = (long)tv_idx->vval.v_number;
2317 blob_T *blob = tv_dest->vval.v_blob;
2318 varnumber_T nr;
2319 int error = FALSE;
2320 int len;
2321
2322 if (blob == NULL)
2323 {
2324 emsg(_(e_blob_not_set));
2325 goto on_error;
2326 }
2327 len = blob_len(blob);
2328 if (lidx < 0 && len + lidx >= 0)
2329 // negative index is relative to the end
2330 lidx = len + lidx;
2331
2332 // Can add one byte at the end.
2333 if (lidx < 0 || lidx > len)
2334 {
2335 semsg(_(e_blobidx), lidx);
2336 goto on_error;
2337 }
2338 if (value_check_lock(blob->bv_lock,
2339 (char_u *)"blob", FALSE))
2340 goto on_error;
2341 nr = tv_get_number_chk(tv, &error);
2342 if (error)
2343 goto on_error;
2344 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002345 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002346 else
2347 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002348 status = FAIL;
2349 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002350 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002351
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002352 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002353 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002354 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002355 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002356 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002357 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002358 goto on_error;
2359 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002360 }
2361 break;
2362
Bram Moolenaar68452172021-04-12 21:21:02 +02002363 // store value in blob range
2364 case ISN_STORERANGE:
2365 {
2366 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2367 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2368 typval_T *tv_dest = STACK_TV_BOT(-1);
2369 int status = OK;
2370
2371 // Stack contains:
2372 // -4 value to be stored
2373 // -3 first index or "none"
2374 // -2 second index or "none"
2375 // -1 destination blob
2376 tv = STACK_TV_BOT(-4);
2377 if (tv_dest->v_type != VAR_BLOB)
2378 {
2379 status = FAIL;
2380 emsg(_(e_blob_required));
2381 }
2382 else
2383 {
2384 varnumber_T n1;
2385 varnumber_T n2;
2386 int error = FALSE;
2387
2388 n1 = tv_get_number_chk(tv_idx1, &error);
2389 if (error)
2390 status = FAIL;
2391 else
2392 {
2393 if (tv_idx2->v_type == VAR_SPECIAL
2394 && tv_idx2->vval.v_number == VVAL_NONE)
2395 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2396 else
2397 n2 = tv_get_number_chk(tv_idx2, &error);
2398 if (error)
2399 status = FAIL;
2400 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002401 {
2402 long bloblen = blob_len(tv_dest->vval.v_blob);
2403
2404 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002405 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002406 || check_blob_range(bloblen,
2407 n1, n2, FALSE) == FAIL)
2408 status = FAIL;
2409 else
2410 status = blob_set_range(
2411 tv_dest->vval.v_blob, n1, n2, tv);
2412 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002413 }
2414 }
2415
2416 clear_tv(tv_idx1);
2417 clear_tv(tv_idx2);
2418 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002419 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002420 clear_tv(tv);
2421
2422 if (status == FAIL)
2423 goto on_error;
2424 }
2425 break;
2426
Bram Moolenaar0186e582021-01-10 18:33:11 +01002427 // load or store variable or argument from outer scope
2428 case ISN_LOADOUTER:
2429 case ISN_STOREOUTER:
2430 {
2431 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002432 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2433 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002434
2435 while (depth > 1 && outer != NULL)
2436 {
2437 outer = outer->out_up;
2438 --depth;
2439 }
2440 if (outer == NULL)
2441 {
2442 SOURCING_LNUM = iptr->isn_lnum;
2443 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002444 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002445 }
2446 tv = ((typval_T *)outer->out_stack->ga_data)
2447 + outer->out_frame_idx + STACK_FRAME_SIZE
2448 + iptr->isn_arg.outer.outer_idx;
2449 if (iptr->isn_type == ISN_LOADOUTER)
2450 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002451 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002452 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002453 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002454 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002455 }
2456 else
2457 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002458 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002459 clear_tv(tv);
2460 *tv = *STACK_TV_BOT(0);
2461 }
2462 }
2463 break;
2464
Bram Moolenaar752fc692021-01-04 21:57:11 +01002465 // unlet item in list or dict variable
2466 case ISN_UNLETINDEX:
2467 {
2468 typval_T *tv_idx = STACK_TV_BOT(-2);
2469 typval_T *tv_dest = STACK_TV_BOT(-1);
2470 int status = OK;
2471
2472 // Stack contains:
2473 // -2 index
2474 // -1 dict or list
2475 if (tv_dest->v_type == VAR_DICT)
2476 {
2477 // unlet a dict item, index must be a string
2478 if (tv_idx->v_type != VAR_STRING)
2479 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002480 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002481 semsg(_(e_expected_str_but_got_str),
2482 vartype_name(VAR_STRING),
2483 vartype_name(tv_idx->v_type));
2484 status = FAIL;
2485 }
2486 else
2487 {
2488 dict_T *d = tv_dest->vval.v_dict;
2489 char_u *key = tv_idx->vval.v_string;
2490 dictitem_T *di = NULL;
2491
2492 if (key == NULL)
2493 key = (char_u *)"";
2494 if (d != NULL)
2495 di = dict_find(d, key, (int)STRLEN(key));
2496 if (di == NULL)
2497 {
2498 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002499 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002500 semsg(_(e_dictkey), key);
2501 status = FAIL;
2502 }
2503 else
2504 {
2505 // TODO: check for dict or item locked
2506 dictitem_remove(d, di);
2507 }
2508 }
2509 }
2510 else if (tv_dest->v_type == VAR_LIST)
2511 {
2512 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002513 SOURCING_LNUM = iptr->isn_lnum;
2514 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002515 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002516 status = FAIL;
2517 }
2518 else
2519 {
2520 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002521 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002522 listitem_T *li = NULL;
2523
2524 li = list_find(l, n);
2525 if (li == NULL)
2526 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002527 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002528 semsg(_(e_listidx), n);
2529 status = FAIL;
2530 }
2531 else
2532 // TODO: check for list or item locked
2533 listitem_remove(l, li);
2534 }
2535 }
2536 else
2537 {
2538 status = FAIL;
2539 semsg(_(e_cannot_index_str),
2540 vartype_name(tv_dest->v_type));
2541 }
2542
2543 clear_tv(tv_idx);
2544 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002545 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002546 if (status == FAIL)
2547 goto on_error;
2548 }
2549 break;
2550
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002551 // unlet range of items in list variable
2552 case ISN_UNLETRANGE:
2553 {
2554 // Stack contains:
2555 // -3 index1
2556 // -2 index2
2557 // -1 dict or list
2558 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2559 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2560 typval_T *tv_dest = STACK_TV_BOT(-1);
2561 int status = OK;
2562
2563 if (tv_dest->v_type == VAR_LIST)
2564 {
2565 // indexes must be a number
2566 SOURCING_LNUM = iptr->isn_lnum;
2567 if (check_for_number(tv_idx1) == FAIL
2568 || check_for_number(tv_idx2) == FAIL)
2569 {
2570 status = FAIL;
2571 }
2572 else
2573 {
2574 list_T *l = tv_dest->vval.v_list;
2575 long n1 = (long)tv_idx1->vval.v_number;
2576 long n2 = (long)tv_idx2->vval.v_number;
2577 listitem_T *li;
2578
2579 li = list_find_index(l, &n1);
2580 if (li == NULL
2581 || list_unlet_range(l, li, NULL, n1,
2582 TRUE, n2) == FAIL)
2583 status = FAIL;
2584 }
2585 }
2586 else
2587 {
2588 status = FAIL;
2589 SOURCING_LNUM = iptr->isn_lnum;
2590 semsg(_(e_cannot_index_str),
2591 vartype_name(tv_dest->v_type));
2592 }
2593
2594 clear_tv(tv_idx1);
2595 clear_tv(tv_idx2);
2596 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002597 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002598 if (status == FAIL)
2599 goto on_error;
2600 }
2601 break;
2602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 // push constant
2604 case ISN_PUSHNR:
2605 case ISN_PUSHBOOL:
2606 case ISN_PUSHSPEC:
2607 case ISN_PUSHF:
2608 case ISN_PUSHS:
2609 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002610 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002611 case ISN_PUSHCHANNEL:
2612 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002613 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002614 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002616 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002617 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 switch (iptr->isn_type)
2619 {
2620 case ISN_PUSHNR:
2621 tv->v_type = VAR_NUMBER;
2622 tv->vval.v_number = iptr->isn_arg.number;
2623 break;
2624 case ISN_PUSHBOOL:
2625 tv->v_type = VAR_BOOL;
2626 tv->vval.v_number = iptr->isn_arg.number;
2627 break;
2628 case ISN_PUSHSPEC:
2629 tv->v_type = VAR_SPECIAL;
2630 tv->vval.v_number = iptr->isn_arg.number;
2631 break;
2632#ifdef FEAT_FLOAT
2633 case ISN_PUSHF:
2634 tv->v_type = VAR_FLOAT;
2635 tv->vval.v_float = iptr->isn_arg.fnumber;
2636 break;
2637#endif
2638 case ISN_PUSHBLOB:
2639 blob_copy(iptr->isn_arg.blob, tv);
2640 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002641 case ISN_PUSHFUNC:
2642 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002643 if (iptr->isn_arg.string == NULL)
2644 tv->vval.v_string = NULL;
2645 else
2646 tv->vval.v_string =
2647 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002648 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002649 case ISN_PUSHCHANNEL:
2650#ifdef FEAT_JOB_CHANNEL
2651 tv->v_type = VAR_CHANNEL;
2652 tv->vval.v_channel = iptr->isn_arg.channel;
2653 if (tv->vval.v_channel != NULL)
2654 ++tv->vval.v_channel->ch_refcount;
2655#endif
2656 break;
2657 case ISN_PUSHJOB:
2658#ifdef FEAT_JOB_CHANNEL
2659 tv->v_type = VAR_JOB;
2660 tv->vval.v_job = iptr->isn_arg.job;
2661 if (tv->vval.v_job != NULL)
2662 ++tv->vval.v_job->jv_refcount;
2663#endif
2664 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 default:
2666 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002667 tv->vval.v_string = vim_strsave(
2668 iptr->isn_arg.string == NULL
2669 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 }
2671 break;
2672
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002673 case ISN_UNLET:
2674 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2675 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002676 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002677 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002678 case ISN_UNLETENV:
2679 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2680 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002681
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002682 case ISN_LOCKCONST:
2683 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2684 break;
2685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 // create a list from items on the stack; uses a single allocation
2687 // for the list header and the items
2688 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002689 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002690 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 break;
2692
2693 // create a dict from items on the stack
2694 case ISN_NEWDICT:
2695 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002696 int count = iptr->isn_arg.number;
2697 dict_T *dict = dict_alloc();
2698 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002699 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002700 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701
2702 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002703 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 for (idx = 0; idx < count; ++idx)
2705 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002706 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002708 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002709 key = tv->vval.v_string == NULL
2710 ? (char_u *)"" : tv->vval.v_string;
2711 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002712 if (item != NULL)
2713 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002714 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002715 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002716 dict_unref(dict);
2717 goto on_error;
2718 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002719 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 clear_tv(tv);
2721 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002722 {
2723 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002724 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2727 item->di_tv.v_lock = 0;
2728 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002729 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002730 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002731 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002732 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 }
2735
2736 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002737 ectx->ec_stack.ga_len -= 2 * count - 1;
2738 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002739 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002741 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 tv = STACK_TV_BOT(-1);
2743 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002744 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 tv->vval.v_dict = dict;
2746 ++dict->dv_refcount;
2747 }
2748 break;
2749
2750 // call a :def function
2751 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002752 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002753 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2754 NULL,
2755 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002756 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002757 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 break;
2759
2760 // call a builtin function
2761 case ISN_BCALL:
2762 SOURCING_LNUM = iptr->isn_lnum;
2763 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2764 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002765 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002766 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 break;
2768
2769 // call a funcref or partial
2770 case ISN_PCALL:
2771 {
2772 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2773 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002774 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775
2776 SOURCING_LNUM = iptr->isn_lnum;
2777 if (pfunc->cpf_top)
2778 {
2779 // funcref is above the arguments
2780 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2781 }
2782 else
2783 {
2784 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002785 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002786 partial_tv = *STACK_TV_BOT(0);
2787 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002789 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002790 if (tv == &partial_tv)
2791 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002793 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 }
2795 break;
2796
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002797 case ISN_PCALL_END:
2798 // PCALL finished, arguments have been consumed and replaced by
2799 // the return value. Now clear the funcref from the stack,
2800 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002801 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002802 clear_tv(STACK_TV_BOT(-1));
2803 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2804 break;
2805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 // call a user defined function or funcref/partial
2807 case ISN_UCALL:
2808 {
2809 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2810
2811 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002812 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002813 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002814 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 }
2816 break;
2817
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002818 // return from a :def function call without a value
2819 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002820 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002821 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002822 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002823 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002824 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002825 tv->vval.v_number = 0;
2826 tv->v_lock = 0;
2827 // FALLTHROUGH
2828
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002829 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 case ISN_RETURN:
2831 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002832 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002833 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002834
2835 if (trystack->ga_len > 0)
2836 trycmd = ((trycmd_T *)trystack->ga_data)
2837 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002838 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002839 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002841 // jump to ":finally" or ":endtry"
2842 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002843 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002844 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002845 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 trycmd->tcd_return = TRUE;
2847 }
2848 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002849 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 }
2851 break;
2852
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002853 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 case ISN_FUNCREF:
2855 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002856 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2857 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2858 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002861 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002862 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002863 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002864 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002865 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002866 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002867 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002868 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002869 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002871 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 tv->vval.v_partial = pt;
2873 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002874 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 }
2876 break;
2877
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002878 // Create a global function from a lambda.
2879 case ISN_NEWFUNC:
2880 {
2881 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2882
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002883 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002884 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002885 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002886 }
2887 break;
2888
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002889 // List functions
2890 case ISN_DEF:
2891 if (iptr->isn_arg.string == NULL)
2892 list_functions(NULL);
2893 else
2894 {
2895 exarg_T ea;
2896
2897 CLEAR_FIELD(ea);
2898 ea.cmd = ea.arg = iptr->isn_arg.string;
2899 define_function(&ea, NULL);
2900 }
2901 break;
2902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 // jump if a condition is met
2904 case ISN_JUMP:
2905 {
2906 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002907 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 int jump = TRUE;
2909
2910 if (when != JUMP_ALWAYS)
2911 {
2912 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002913 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002914 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002915 || when == JUMP_IF_COND_TRUE)
2916 {
2917 SOURCING_LNUM = iptr->isn_lnum;
2918 jump = tv_get_bool_chk(tv, &error);
2919 if (error)
2920 goto on_error;
2921 }
2922 else
2923 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002925 || when == JUMP_AND_KEEP_IF_FALSE
2926 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002928 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 {
2930 // drop the value from the stack
2931 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002932 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 }
2934 }
2935 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 }
2938 break;
2939
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002940 // Jump if an argument with a default value was already set and not
2941 // v:none.
2942 case ISN_JUMP_IF_ARG_SET:
2943 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2944 if (tv->v_type != VAR_UNKNOWN
2945 && !(tv->v_type == VAR_SPECIAL
2946 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002947 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002948 break;
2949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 // top of a for loop
2951 case ISN_FOR:
2952 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002953 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 typval_T *idxtv =
2955 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2956
Bram Moolenaar4c137212021-04-19 16:48:48 +02002957 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002958 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002959 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002960 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002961 list_T *list = ltv->vval.v_list;
2962
2963 // push the next item from the list
2964 ++idxtv->vval.v_number;
2965 if (list == NULL
2966 || idxtv->vval.v_number >= list->lv_len)
2967 {
2968 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002969 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2970 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002971 }
2972 else if (list->lv_first == &range_list_item)
2973 {
2974 // non-materialized range() list
2975 tv = STACK_TV_BOT(0);
2976 tv->v_type = VAR_NUMBER;
2977 tv->v_lock = 0;
2978 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002980 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002981 }
2982 else
2983 {
2984 listitem_T *li = list_find(list,
2985 idxtv->vval.v_number);
2986
2987 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002988 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002989 }
2990 }
2991 else if (ltv->v_type == VAR_STRING)
2992 {
2993 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002994
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002995 // The index is for the last byte of the previous
2996 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002997 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002998 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002999 {
3000 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003001 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3002 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003003 }
3004 else
3005 {
3006 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3007
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003008 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003009 tv = STACK_TV_BOT(0);
3010 tv->v_type = VAR_STRING;
3011 tv->vval.v_string = vim_strnsave(
3012 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003013 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003014 idxtv->vval.v_number += clen - 1;
3015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003017 else if (ltv->v_type == VAR_BLOB)
3018 {
3019 blob_T *blob = ltv->vval.v_blob;
3020
3021 // When we get here the first time make a copy of the
3022 // blob, so that the iteration still works when it is
3023 // changed.
3024 if (idxtv->vval.v_number == -1 && blob != NULL)
3025 {
3026 blob_copy(blob, ltv);
3027 blob_unref(blob);
3028 blob = ltv->vval.v_blob;
3029 }
3030
3031 // The index is for the previous byte.
3032 ++idxtv->vval.v_number;
3033 if (blob == NULL
3034 || idxtv->vval.v_number >= blob_len(blob))
3035 {
3036 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003037 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3038 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003039 }
3040 else
3041 {
3042 // Push the next byte from the blob.
3043 tv = STACK_TV_BOT(0);
3044 tv->v_type = VAR_NUMBER;
3045 tv->vval.v_number = blob_get(blob,
3046 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003047 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003048 }
3049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 else
3051 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003052 semsg(_(e_for_loop_on_str_not_supported),
3053 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003054 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 }
3056 }
3057 break;
3058
3059 // start of ":try" block
3060 case ISN_TRY:
3061 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003062 trycmd_T *trycmd = NULL;
3063
Bram Moolenaar4c137212021-04-19 16:48:48 +02003064 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003065 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003066 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3067 + ectx->ec_trystack.ga_len;
3068 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003070 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003071 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3072 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003073 trycmd->tcd_catch_idx =
3074 iptr->isn_arg.try.try_ref->try_catch;
3075 trycmd->tcd_finally_idx =
3076 iptr->isn_arg.try.try_ref->try_finally;
3077 trycmd->tcd_endtry_idx =
3078 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 }
3080 break;
3081
3082 case ISN_PUSHEXC:
3083 if (current_exception == NULL)
3084 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003085 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003087 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003089 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003090 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003092 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003094 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 tv->vval.v_string = vim_strsave(
3096 (char_u *)current_exception->value);
3097 break;
3098
3099 case ISN_CATCH:
3100 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003101 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102
Bram Moolenaar4c137212021-04-19 16:48:48 +02003103 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 if (trystack->ga_len > 0)
3105 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003106 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 + trystack->ga_len - 1;
3108 trycmd->tcd_caught = TRUE;
3109 }
3110 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003111 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 catch_exception(current_exception);
3113 }
3114 break;
3115
Bram Moolenaarc150c092021-02-13 15:02:46 +01003116 case ISN_TRYCONT:
3117 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003118 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003119 trycont_T *trycont = &iptr->isn_arg.trycont;
3120 int i;
3121 trycmd_T *trycmd;
3122 int iidx = trycont->tct_where;
3123
3124 if (trystack->ga_len < trycont->tct_levels)
3125 {
3126 siemsg("TRYCONT: expected %d levels, found %d",
3127 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003128 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003129 }
3130 // Make :endtry jump to any outer try block and the last
3131 // :endtry inside the loop to the loop start.
3132 for (i = trycont->tct_levels; i > 0; --i)
3133 {
3134 trycmd = ((trycmd_T *)trystack->ga_data)
3135 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003136 // Add one to tcd_cont to be able to jump to
3137 // instruction with index zero.
3138 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003139 iidx = trycmd->tcd_finally_idx == 0
3140 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003141 }
3142 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003143 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003144 }
3145 break;
3146
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003147 case ISN_FINALLY:
3148 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003149 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003150 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3151 + trystack->ga_len - 1;
3152
3153 // Reset the index to avoid a return statement jumps here
3154 // again.
3155 trycmd->tcd_finally_idx = 0;
3156 break;
3157 }
3158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 // end of ":try" block
3160 case ISN_ENDTRY:
3161 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003162 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163
3164 if (trystack->ga_len > 0)
3165 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003166 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 --trystack->ga_len;
3169 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003170 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 trycmd = ((trycmd_T *)trystack->ga_data)
3172 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003173 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 {
3175 // discard the exception
3176 if (caught_stack == current_exception)
3177 caught_stack = caught_stack->caught;
3178 discard_current_exception();
3179 }
3180
3181 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003182 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003183
Bram Moolenaar4c137212021-04-19 16:48:48 +02003184 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003185 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003186 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003187 clear_tv(STACK_TV_BOT(0));
3188 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003189 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003190 // handling :continue: jump to outer try block or
3191 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003192 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 }
3194 }
3195 break;
3196
3197 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003198 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003199 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003200
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003201 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3202 {
3203 // throwing an exception while using "silent!" causes
3204 // the function to abort but not display an error.
3205 tv = STACK_TV_BOT(-1);
3206 clear_tv(tv);
3207 tv->v_type = VAR_NUMBER;
3208 tv->vval.v_number = 0;
3209 goto done;
3210 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003211 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003212 tv = STACK_TV_BOT(0);
3213 if (tv->vval.v_string == NULL
3214 || *skipwhite(tv->vval.v_string) == NUL)
3215 {
3216 vim_free(tv->vval.v_string);
3217 SOURCING_LNUM = iptr->isn_lnum;
3218 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003219 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003220 }
3221
3222 // Inside a "catch" we need to first discard the caught
3223 // exception.
3224 if (trystack->ga_len > 0)
3225 {
3226 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3227 + trystack->ga_len - 1;
3228 if (trycmd->tcd_caught && current_exception != NULL)
3229 {
3230 // discard the exception
3231 if (caught_stack == current_exception)
3232 caught_stack = caught_stack->caught;
3233 discard_current_exception();
3234 trycmd->tcd_caught = FALSE;
3235 }
3236 }
3237
3238 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3239 == FAIL)
3240 {
3241 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003242 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003243 }
3244 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 break;
3247
3248 // compare with special values
3249 case ISN_COMPAREBOOL:
3250 case ISN_COMPARESPECIAL:
3251 {
3252 typval_T *tv1 = STACK_TV_BOT(-2);
3253 typval_T *tv2 = STACK_TV_BOT(-1);
3254 varnumber_T arg1 = tv1->vval.v_number;
3255 varnumber_T arg2 = tv2->vval.v_number;
3256 int res;
3257
3258 switch (iptr->isn_arg.op.op_type)
3259 {
3260 case EXPR_EQUAL: res = arg1 == arg2; break;
3261 case EXPR_NEQUAL: res = arg1 != arg2; break;
3262 default: res = 0; break;
3263 }
3264
Bram Moolenaar4c137212021-04-19 16:48:48 +02003265 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 tv1->v_type = VAR_BOOL;
3267 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3268 }
3269 break;
3270
3271 // Operation with two number arguments
3272 case ISN_OPNR:
3273 case ISN_COMPARENR:
3274 {
3275 typval_T *tv1 = STACK_TV_BOT(-2);
3276 typval_T *tv2 = STACK_TV_BOT(-1);
3277 varnumber_T arg1 = tv1->vval.v_number;
3278 varnumber_T arg2 = tv2->vval.v_number;
3279 varnumber_T res;
3280
3281 switch (iptr->isn_arg.op.op_type)
3282 {
3283 case EXPR_MULT: res = arg1 * arg2; break;
3284 case EXPR_DIV: res = arg1 / arg2; break;
3285 case EXPR_REM: res = arg1 % arg2; break;
3286 case EXPR_SUB: res = arg1 - arg2; break;
3287 case EXPR_ADD: res = arg1 + arg2; break;
3288
3289 case EXPR_EQUAL: res = arg1 == arg2; break;
3290 case EXPR_NEQUAL: res = arg1 != arg2; break;
3291 case EXPR_GREATER: res = arg1 > arg2; break;
3292 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3293 case EXPR_SMALLER: res = arg1 < arg2; break;
3294 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3295 default: res = 0; break;
3296 }
3297
Bram Moolenaar4c137212021-04-19 16:48:48 +02003298 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 if (iptr->isn_type == ISN_COMPARENR)
3300 {
3301 tv1->v_type = VAR_BOOL;
3302 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3303 }
3304 else
3305 tv1->vval.v_number = res;
3306 }
3307 break;
3308
3309 // Computation with two float arguments
3310 case ISN_OPFLOAT:
3311 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003312#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 {
3314 typval_T *tv1 = STACK_TV_BOT(-2);
3315 typval_T *tv2 = STACK_TV_BOT(-1);
3316 float_T arg1 = tv1->vval.v_float;
3317 float_T arg2 = tv2->vval.v_float;
3318 float_T res = 0;
3319 int cmp = FALSE;
3320
3321 switch (iptr->isn_arg.op.op_type)
3322 {
3323 case EXPR_MULT: res = arg1 * arg2; break;
3324 case EXPR_DIV: res = arg1 / arg2; break;
3325 case EXPR_SUB: res = arg1 - arg2; break;
3326 case EXPR_ADD: res = arg1 + arg2; break;
3327
3328 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3329 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3330 case EXPR_GREATER: cmp = arg1 > arg2; break;
3331 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3332 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3333 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3334 default: cmp = 0; break;
3335 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003336 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 if (iptr->isn_type == ISN_COMPAREFLOAT)
3338 {
3339 tv1->v_type = VAR_BOOL;
3340 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3341 }
3342 else
3343 tv1->vval.v_float = res;
3344 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003345#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 break;
3347
3348 case ISN_COMPARELIST:
3349 {
3350 typval_T *tv1 = STACK_TV_BOT(-2);
3351 typval_T *tv2 = STACK_TV_BOT(-1);
3352 list_T *arg1 = tv1->vval.v_list;
3353 list_T *arg2 = tv2->vval.v_list;
3354 int cmp = FALSE;
3355 int ic = iptr->isn_arg.op.op_ic;
3356
3357 switch (iptr->isn_arg.op.op_type)
3358 {
3359 case EXPR_EQUAL: cmp =
3360 list_equal(arg1, arg2, ic, FALSE); break;
3361 case EXPR_NEQUAL: cmp =
3362 !list_equal(arg1, arg2, ic, FALSE); break;
3363 case EXPR_IS: cmp = arg1 == arg2; break;
3364 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3365 default: cmp = 0; break;
3366 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003367 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 clear_tv(tv1);
3369 clear_tv(tv2);
3370 tv1->v_type = VAR_BOOL;
3371 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3372 }
3373 break;
3374
3375 case ISN_COMPAREBLOB:
3376 {
3377 typval_T *tv1 = STACK_TV_BOT(-2);
3378 typval_T *tv2 = STACK_TV_BOT(-1);
3379 blob_T *arg1 = tv1->vval.v_blob;
3380 blob_T *arg2 = tv2->vval.v_blob;
3381 int cmp = FALSE;
3382
3383 switch (iptr->isn_arg.op.op_type)
3384 {
3385 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3386 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3387 case EXPR_IS: cmp = arg1 == arg2; break;
3388 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3389 default: cmp = 0; break;
3390 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003391 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 clear_tv(tv1);
3393 clear_tv(tv2);
3394 tv1->v_type = VAR_BOOL;
3395 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3396 }
3397 break;
3398
3399 // TODO: handle separately
3400 case ISN_COMPARESTRING:
3401 case ISN_COMPAREDICT:
3402 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 case ISN_COMPAREANY:
3404 {
3405 typval_T *tv1 = STACK_TV_BOT(-2);
3406 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003407 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 int ic = iptr->isn_arg.op.op_ic;
3409
Bram Moolenaareb26f432020-09-14 16:50:05 +02003410 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003411 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003413 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 }
3415 break;
3416
3417 case ISN_ADDLIST:
3418 case ISN_ADDBLOB:
3419 {
3420 typval_T *tv1 = STACK_TV_BOT(-2);
3421 typval_T *tv2 = STACK_TV_BOT(-1);
3422
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003423 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 if (iptr->isn_type == ISN_ADDLIST)
3425 eval_addlist(tv1, tv2);
3426 else
3427 eval_addblob(tv1, tv2);
3428 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003429 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 }
3431 break;
3432
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003433 case ISN_LISTAPPEND:
3434 {
3435 typval_T *tv1 = STACK_TV_BOT(-2);
3436 typval_T *tv2 = STACK_TV_BOT(-1);
3437 list_T *l = tv1->vval.v_list;
3438
3439 // add an item to a list
3440 if (l == NULL)
3441 {
3442 SOURCING_LNUM = iptr->isn_lnum;
3443 emsg(_(e_cannot_add_to_null_list));
3444 goto on_error;
3445 }
3446 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003447 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003448 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003449 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003450 }
3451 break;
3452
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003453 case ISN_BLOBAPPEND:
3454 {
3455 typval_T *tv1 = STACK_TV_BOT(-2);
3456 typval_T *tv2 = STACK_TV_BOT(-1);
3457 blob_T *b = tv1->vval.v_blob;
3458 int error = FALSE;
3459 varnumber_T n;
3460
3461 // add a number to a blob
3462 if (b == NULL)
3463 {
3464 SOURCING_LNUM = iptr->isn_lnum;
3465 emsg(_(e_cannot_add_to_null_blob));
3466 goto on_error;
3467 }
3468 n = tv_get_number_chk(tv2, &error);
3469 if (error)
3470 goto on_error;
3471 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003472 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003473 }
3474 break;
3475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 // Computation with two arguments of unknown type
3477 case ISN_OPANY:
3478 {
3479 typval_T *tv1 = STACK_TV_BOT(-2);
3480 typval_T *tv2 = STACK_TV_BOT(-1);
3481 varnumber_T n1, n2;
3482#ifdef FEAT_FLOAT
3483 float_T f1 = 0, f2 = 0;
3484#endif
3485 int error = FALSE;
3486
3487 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3488 {
3489 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3490 {
3491 eval_addlist(tv1, tv2);
3492 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003493 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 break;
3495 }
3496 else if (tv1->v_type == VAR_BLOB
3497 && tv2->v_type == VAR_BLOB)
3498 {
3499 eval_addblob(tv1, tv2);
3500 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003501 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 break;
3503 }
3504 }
3505#ifdef FEAT_FLOAT
3506 if (tv1->v_type == VAR_FLOAT)
3507 {
3508 f1 = tv1->vval.v_float;
3509 n1 = 0;
3510 }
3511 else
3512#endif
3513 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003514 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 n1 = tv_get_number_chk(tv1, &error);
3516 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003517 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518#ifdef FEAT_FLOAT
3519 if (tv2->v_type == VAR_FLOAT)
3520 f1 = n1;
3521#endif
3522 }
3523#ifdef FEAT_FLOAT
3524 if (tv2->v_type == VAR_FLOAT)
3525 {
3526 f2 = tv2->vval.v_float;
3527 n2 = 0;
3528 }
3529 else
3530#endif
3531 {
3532 n2 = tv_get_number_chk(tv2, &error);
3533 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003534 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535#ifdef FEAT_FLOAT
3536 if (tv1->v_type == VAR_FLOAT)
3537 f2 = n2;
3538#endif
3539 }
3540#ifdef FEAT_FLOAT
3541 // if there is a float on either side the result is a float
3542 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3543 {
3544 switch (iptr->isn_arg.op.op_type)
3545 {
3546 case EXPR_MULT: f1 = f1 * f2; break;
3547 case EXPR_DIV: f1 = f1 / f2; break;
3548 case EXPR_SUB: f1 = f1 - f2; break;
3549 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003550 default: SOURCING_LNUM = iptr->isn_lnum;
3551 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003552 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 }
3554 clear_tv(tv1);
3555 clear_tv(tv2);
3556 tv1->v_type = VAR_FLOAT;
3557 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003558 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 }
3560 else
3561#endif
3562 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003563 int failed = FALSE;
3564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 switch (iptr->isn_arg.op.op_type)
3566 {
3567 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003568 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3569 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003570 goto on_error;
3571 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 case EXPR_SUB: n1 = n1 - n2; break;
3573 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003574 default: n1 = num_modulus(n1, n2, &failed);
3575 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003576 goto on_error;
3577 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 }
3579 clear_tv(tv1);
3580 clear_tv(tv2);
3581 tv1->v_type = VAR_NUMBER;
3582 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003583 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 }
3585 }
3586 break;
3587
3588 case ISN_CONCAT:
3589 {
3590 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3591 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3592 char_u *res;
3593
3594 res = concat_str(str1, str2);
3595 clear_tv(STACK_TV_BOT(-2));
3596 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003597 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 STACK_TV_BOT(-1)->vval.v_string = res;
3599 }
3600 break;
3601
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003602 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003603 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003604 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003605 int is_slice = iptr->isn_type == ISN_STRSLICE;
3606 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003607 char_u *res;
3608
3609 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003610 // string slice: string is at stack-3, first index at
3611 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003612 if (is_slice)
3613 {
3614 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003615 n1 = tv->vval.v_number;
3616 }
3617
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003618 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003619 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003620
Bram Moolenaar4c137212021-04-19 16:48:48 +02003621 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003622 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003623 if (is_slice)
3624 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003625 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003626 else
3627 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003628 // single character (including composing characters).
3629 // If the index is too big or negative the result is
3630 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003631 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003632 vim_free(tv->vval.v_string);
3633 tv->vval.v_string = res;
3634 }
3635 break;
3636
3637 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003638 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003639 case ISN_BLOBINDEX:
3640 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003642 int is_slice = iptr->isn_type == ISN_LISTSLICE
3643 || iptr->isn_type == ISN_BLOBSLICE;
3644 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3645 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003646 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003647 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648
3649 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003650 // list slice: list is at stack-3, indexes at stack-2 and
3651 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003652 // Same for blob.
3653 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654
3655 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003656 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003658
3659 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 {
Bram Moolenaared591872020-08-15 22:14:53 +02003661 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003662 n1 = tv->vval.v_number;
3663 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 }
Bram Moolenaared591872020-08-15 22:14:53 +02003665
Bram Moolenaar4c137212021-04-19 16:48:48 +02003666 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003667 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003668 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003669 if (is_blob)
3670 {
3671 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3672 n1, n2, FALSE, tv) == FAIL)
3673 goto on_error;
3674 }
3675 else
3676 {
3677 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3678 n1, n2, FALSE, tv, TRUE) == FAIL)
3679 goto on_error;
3680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 }
3682 break;
3683
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003684 case ISN_ANYINDEX:
3685 case ISN_ANYSLICE:
3686 {
3687 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3688 typval_T *var1, *var2;
3689 int res;
3690
3691 // index: composite is at stack-2, index at stack-1
3692 // slice: composite is at stack-3, indexes at stack-2 and
3693 // stack-1
3694 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003695 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003696 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3697 goto on_error;
3698 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3699 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003700 res = eval_index_inner(tv, is_slice, var1, var2,
3701 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003702 clear_tv(var1);
3703 if (is_slice)
3704 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003705 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003706 if (res == FAIL)
3707 goto on_error;
3708 }
3709 break;
3710
Bram Moolenaar9af78762020-06-16 11:34:42 +02003711 case ISN_SLICE:
3712 {
3713 list_T *list;
3714 int count = iptr->isn_arg.number;
3715
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003716 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003717 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003718 list = tv->vval.v_list;
3719
3720 // no error for short list, expect it to be checked earlier
3721 if (list != NULL && list->lv_len >= count)
3722 {
3723 list_T *newlist = list_slice(list,
3724 count, list->lv_len - 1);
3725
3726 if (newlist != NULL)
3727 {
3728 list_unref(list);
3729 tv->vval.v_list = newlist;
3730 ++newlist->lv_refcount;
3731 }
3732 }
3733 }
3734 break;
3735
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003736 case ISN_GETITEM:
3737 {
3738 listitem_T *li;
3739 int index = iptr->isn_arg.number;
3740
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003741 // Get list item: list is at stack-1, push item.
3742 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003743 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003744 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003745
Bram Moolenaar4c137212021-04-19 16:48:48 +02003746 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003747 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003748 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003749 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003750
3751 // Useful when used in unpack assignment. Reset at
3752 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003753 ectx->ec_where.wt_index = index + 1;
3754 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003755 }
3756 break;
3757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 case ISN_MEMBER:
3759 {
3760 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003761 char_u *key;
3762 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003763 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003764
3765 // dict member: dict is at stack-2, key at stack-1
3766 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003767 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003768 dict = tv->vval.v_dict;
3769
3770 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003771 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003772 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003773 if (key == NULL)
3774 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003775
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003776 if ((di = dict_find(dict, key, -1)) == NULL)
3777 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003778 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003779 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003780
3781 // If :silent! is used we will continue, make sure the
3782 // stack contents makes sense.
3783 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003785 tv = STACK_TV_BOT(-1);
3786 clear_tv(tv);
3787 tv->v_type = VAR_NUMBER;
3788 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003789 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003790 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003791 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003792 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003793 // Clear the dict only after getting the item, to avoid
3794 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003795 tv = STACK_TV_BOT(-1);
3796 temp_tv = *tv;
3797 copy_tv(&di->di_tv, tv);
3798 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003799 }
3800 break;
3801
3802 // dict member with string key
3803 case ISN_STRINGMEMBER:
3804 {
3805 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003807 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808
3809 tv = STACK_TV_BOT(-1);
3810 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3811 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003812 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003814 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 }
3816 dict = tv->vval.v_dict;
3817
3818 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3819 == NULL)
3820 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003821 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003823 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003825 // Clear the dict after getting the item, to avoid that it
3826 // make the item invalid.
3827 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003829 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 }
3831 break;
3832
3833 case ISN_NEGATENR:
3834 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003835 if (tv->v_type != VAR_NUMBER
3836#ifdef FEAT_FLOAT
3837 && tv->v_type != VAR_FLOAT
3838#endif
3839 )
3840 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003841 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003842 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003843 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003844 }
3845#ifdef FEAT_FLOAT
3846 if (tv->v_type == VAR_FLOAT)
3847 tv->vval.v_float = -tv->vval.v_float;
3848 else
3849#endif
3850 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 break;
3852
3853 case ISN_CHECKNR:
3854 {
3855 int error = FALSE;
3856
3857 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003858 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003860 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 (void)tv_get_number_chk(tv, &error);
3862 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003863 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 }
3865 break;
3866
3867 case ISN_CHECKTYPE:
3868 {
3869 checktype_T *ct = &iptr->isn_arg.type;
3870
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003871 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003872 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003873 if (!ectx->ec_where.wt_variable)
3874 ectx->ec_where.wt_index = ct->ct_arg_idx;
3875 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3876 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003877 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003878 if (!ectx->ec_where.wt_variable)
3879 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003880
3881 // number 0 is FALSE, number 1 is TRUE
3882 if (tv->v_type == VAR_NUMBER
3883 && ct->ct_type->tt_type == VAR_BOOL
3884 && (tv->vval.v_number == 0
3885 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003887 tv->v_type = VAR_BOOL;
3888 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003889 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 }
3891 }
3892 break;
3893
Bram Moolenaar9af78762020-06-16 11:34:42 +02003894 case ISN_CHECKLEN:
3895 {
3896 int min_len = iptr->isn_arg.checklen.cl_min_len;
3897 list_T *list = NULL;
3898
3899 tv = STACK_TV_BOT(-1);
3900 if (tv->v_type == VAR_LIST)
3901 list = tv->vval.v_list;
3902 if (list == NULL || list->lv_len < min_len
3903 || (list->lv_len > min_len
3904 && !iptr->isn_arg.checklen.cl_more_OK))
3905 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003906 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003907 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003908 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003909 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003910 }
3911 }
3912 break;
3913
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003914 case ISN_SETTYPE:
3915 {
3916 checktype_T *ct = &iptr->isn_arg.type;
3917
3918 tv = STACK_TV_BOT(-1);
3919 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3920 {
3921 free_type(tv->vval.v_dict->dv_type);
3922 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3923 }
3924 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3925 {
3926 free_type(tv->vval.v_list->lv_type);
3927 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3928 }
3929 }
3930 break;
3931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003933 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 {
3935 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003936 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003938 if (iptr->isn_type == ISN_2BOOL)
3939 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003940 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003941 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003942 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003943 n = !n;
3944 }
3945 else
3946 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003947 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003948 SOURCING_LNUM = iptr->isn_lnum;
3949 n = tv_get_bool_chk(tv, &error);
3950 if (error)
3951 goto on_error;
3952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 clear_tv(tv);
3954 tv->v_type = VAR_BOOL;
3955 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3956 }
3957 break;
3958
3959 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003960 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003961 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003962 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3963 iptr->isn_type == ISN_2STRING_ANY,
3964 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003965 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 break;
3967
Bram Moolenaar08597872020-12-10 19:43:40 +01003968 case ISN_RANGE:
3969 {
3970 exarg_T ea;
3971 char *errormsg;
3972
Bram Moolenaarece0b872021-01-08 20:40:45 +01003973 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003974 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003975 ea.addr_type = ADDR_LINES;
3976 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003977 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003978 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003979 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003980
Bram Moolenaar4c137212021-04-19 16:48:48 +02003981 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003982 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003983 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003984 tv = STACK_TV_BOT(-1);
3985 tv->v_type = VAR_NUMBER;
3986 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003987 if (ea.addr_count == 0)
3988 tv->vval.v_number = curwin->w_cursor.lnum;
3989 else
3990 tv->vval.v_number = ea.line2;
3991 }
3992 break;
3993
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003994 case ISN_PUT:
3995 {
3996 int regname = iptr->isn_arg.put.put_regname;
3997 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3998 char_u *expr = NULL;
3999 int dir = FORWARD;
4000
Bram Moolenaar08597872020-12-10 19:43:40 +01004001 if (lnum < -2)
4002 {
4003 // line number was put on the stack by ISN_RANGE
4004 tv = STACK_TV_BOT(-1);
4005 curwin->w_cursor.lnum = tv->vval.v_number;
4006 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4007 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004008 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004009 }
4010 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004011 // :put! above cursor
4012 dir = BACKWARD;
4013 else if (lnum >= 0)
4014 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004015
4016 if (regname == '=')
4017 {
4018 tv = STACK_TV_BOT(-1);
4019 if (tv->v_type == VAR_STRING)
4020 expr = tv->vval.v_string;
4021 else
4022 {
4023 expr = typval2string(tv, TRUE); // allocates value
4024 clear_tv(tv);
4025 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004026 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004027 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004028 check_cursor();
4029 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4030 vim_free(expr);
4031 }
4032 break;
4033
Bram Moolenaar02194d22020-10-24 23:08:38 +02004034 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004035 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4036 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4037 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4038 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004039 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4040 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004041 break;
4042
Bram Moolenaar02194d22020-10-24 23:08:38 +02004043 case ISN_CMDMOD_REV:
4044 // filter regprog is owned by the instruction, don't free it
4045 cmdmod.cmod_filter_regmatch.regprog = NULL;
4046 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004047 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4048 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004049 break;
4050
Bram Moolenaar792f7862020-11-23 08:31:18 +01004051 case ISN_UNPACK:
4052 {
4053 int count = iptr->isn_arg.unpack.unp_count;
4054 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4055 list_T *l;
4056 listitem_T *li;
4057 int i;
4058
4059 // Check there is a valid list to unpack.
4060 tv = STACK_TV_BOT(-1);
4061 if (tv->v_type != VAR_LIST)
4062 {
4063 SOURCING_LNUM = iptr->isn_lnum;
4064 emsg(_(e_for_argument_must_be_sequence_of_lists));
4065 goto on_error;
4066 }
4067 l = tv->vval.v_list;
4068 if (l == NULL
4069 || l->lv_len < (semicolon ? count - 1 : count))
4070 {
4071 SOURCING_LNUM = iptr->isn_lnum;
4072 emsg(_(e_list_value_does_not_have_enough_items));
4073 goto on_error;
4074 }
4075 else if (!semicolon && l->lv_len > count)
4076 {
4077 SOURCING_LNUM = iptr->isn_lnum;
4078 emsg(_(e_list_value_has_more_items_than_targets));
4079 goto on_error;
4080 }
4081
4082 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004083 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004084 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004085 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004086
4087 // Variable after semicolon gets a list with the remaining
4088 // items.
4089 if (semicolon)
4090 {
4091 list_T *rem_list =
4092 list_alloc_with_items(l->lv_len - count + 1);
4093
4094 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004095 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004096 tv = STACK_TV_BOT(-count);
4097 tv->vval.v_list = rem_list;
4098 ++rem_list->lv_refcount;
4099 tv->v_lock = 0;
4100 li = l->lv_first;
4101 for (i = 0; i < count - 1; ++i)
4102 li = li->li_next;
4103 for (i = 0; li != NULL; ++i)
4104 {
4105 list_set_item(rem_list, i, &li->li_tv);
4106 li = li->li_next;
4107 }
4108 --count;
4109 }
4110
4111 // Produce the values in reverse order, first item last.
4112 li = l->lv_first;
4113 for (i = 0; i < count; ++i)
4114 {
4115 tv = STACK_TV_BOT(-i - 1);
4116 copy_tv(&li->li_tv, tv);
4117 li = li->li_next;
4118 }
4119
4120 list_unref(l);
4121 }
4122 break;
4123
Bram Moolenaarb2049902021-01-24 12:53:53 +01004124 case ISN_PROF_START:
4125 case ISN_PROF_END:
4126 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004127#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004128 funccall_T cookie;
4129 ufunc_T *cur_ufunc =
4130 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004131 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004132
4133 cookie.func = cur_ufunc;
4134 if (iptr->isn_type == ISN_PROF_START)
4135 {
4136 func_line_start(&cookie, iptr->isn_lnum);
4137 // if we get here the instruction is executed
4138 func_line_exec(&cookie);
4139 }
4140 else
4141 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004142#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004143 }
4144 break;
4145
Bram Moolenaare99d4222021-06-13 14:01:26 +02004146 case ISN_DEBUG:
4147 if (ex_nesting_level <= debug_break_level)
4148 {
4149 char_u *line;
4150 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
4151 + ectx->ec_dfunc_idx)->df_ufunc;
4152
4153 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004154 debug_context = ectx;
4155 debug_arg_count = iptr->isn_arg.number;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004156 line = ((char_u **)ufunc->uf_lines.ga_data)[
4157 iptr->isn_lnum - 1];
4158 if (line == NULL)
4159 line = (char_u *)"[empty]";
4160 do_debug(line);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004161 debug_context = NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004162 }
4163 break;
4164
Bram Moolenaar389df252020-07-09 21:20:47 +02004165 case ISN_SHUFFLE:
4166 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004167 typval_T tmp_tv;
4168 int item = iptr->isn_arg.shuffle.shfl_item;
4169 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004170
4171 tmp_tv = *STACK_TV_BOT(-item);
4172 for ( ; up > 0 && item > 1; --up)
4173 {
4174 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4175 --item;
4176 }
4177 *STACK_TV_BOT(-item) = tmp_tv;
4178 }
4179 break;
4180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004182 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004184 ectx->ec_where.wt_index = 0;
4185 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 break;
4187 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004188 continue;
4189
Bram Moolenaard032f342020-07-18 18:13:02 +02004190func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004191 // Restore previous function. If the frame pointer is where we started
4192 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004193 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004194 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004195
Bram Moolenaar4c137212021-04-19 16:48:48 +02004196 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004197 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004198 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004199 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004200
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004201on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004202 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004203 // If "emsg_silent" is set then ignore the error, unless it was set
4204 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004205 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004206 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004207 {
4208 // If a sequence of instructions causes an error while ":silent!"
4209 // was used, restore the stack length and jump ahead to restoring
4210 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004211 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004212 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004213 while (ectx->ec_stack.ga_len
4214 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004215 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004216 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004217 clear_tv(STACK_TV_BOT(0));
4218 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004219 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4220 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004221 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004222 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004223 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004224on_fatal_error:
4225 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004226 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004227 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004228 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 }
4230
4231done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004232 ret = OK;
4233theend:
4234 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4235 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004236}
4237
4238/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004239 * Execute the instructions from a VAR_INSTR typeval and put the result in
4240 * "rettv".
4241 * Return OK or FAIL.
4242 */
4243 int
4244exe_typval_instr(typval_T *tv, typval_T *rettv)
4245{
4246 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4247 isn_T *save_instr = ectx->ec_instr;
4248 int save_iidx = ectx->ec_iidx;
4249 int res;
4250
4251 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4252 res = exec_instructions(ectx);
4253 if (res == OK)
4254 {
4255 *rettv = *STACK_TV_BOT(-1);
4256 --ectx->ec_stack.ga_len;
4257 }
4258
4259 ectx->ec_instr = save_instr;
4260 ectx->ec_iidx = save_iidx;
4261
4262 return res;
4263}
4264
4265/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004266 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4267 * "substitute_instr".
4268 */
4269 char_u *
4270exe_substitute_instr(void)
4271{
4272 ectx_T *ectx = substitute_instr->subs_ectx;
4273 isn_T *save_instr = ectx->ec_instr;
4274 int save_iidx = ectx->ec_iidx;
4275 char_u *res;
4276
4277 ectx->ec_instr = substitute_instr->subs_instr;
4278 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004279 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004280 typval_T *tv = STACK_TV_BOT(-1);
4281
Bram Moolenaar27523602021-06-05 21:36:19 +02004282 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004283 --ectx->ec_stack.ga_len;
4284 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004285 }
4286 else
4287 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004288 substitute_instr->subs_status = FAIL;
4289 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291
Bram Moolenaar4c137212021-04-19 16:48:48 +02004292 ectx->ec_instr = save_instr;
4293 ectx->ec_iidx = save_iidx;
4294
4295 return res;
4296}
4297
4298/*
4299 * Call a "def" function from old Vim script.
4300 * Return OK or FAIL.
4301 */
4302 int
4303call_def_function(
4304 ufunc_T *ufunc,
4305 int argc_arg, // nr of arguments
4306 typval_T *argv, // arguments
4307 partial_T *partial, // optional partial for context
4308 typval_T *rettv) // return value
4309{
4310 ectx_T ectx; // execution context
4311 int argc = argc_arg;
4312 typval_T *tv;
4313 int idx;
4314 int ret = FAIL;
4315 int defcount = ufunc->uf_args.ga_len - argc;
4316 sctx_T save_current_sctx = current_sctx;
4317 int did_emsg_before = did_emsg_cumul + did_emsg;
4318 int save_suppress_errthrow = suppress_errthrow;
4319 msglist_T **saved_msg_list = NULL;
4320 msglist_T *private_msg_list = NULL;
4321 int save_emsg_silent_def = emsg_silent_def;
4322 int save_did_emsg_def = did_emsg_def;
4323 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004324 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004325
4326// Get pointer to item in the stack.
4327#undef STACK_TV
4328#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4329
4330// Get pointer to item at the bottom of the stack, -1 is the bottom.
4331#undef STACK_TV_BOT
4332#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4333
4334// Get pointer to a local variable on the stack. Negative for arguments.
4335#undef STACK_TV_VAR
4336#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4337
4338 if (ufunc->uf_def_status == UF_NOT_COMPILED
4339 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004340 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4341 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004342 == FAIL))
4343 {
4344 if (did_emsg_cumul + did_emsg == did_emsg_before)
4345 semsg(_(e_function_is_not_compiled_str),
4346 printable_func_name(ufunc));
4347 return FAIL;
4348 }
4349
4350 {
4351 // Check the function was really compiled.
4352 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4353 + ufunc->uf_dfunc_idx;
4354 if (INSTRUCTIONS(dfunc) == NULL)
4355 {
4356 iemsg("using call_def_function() on not compiled function");
4357 return FAIL;
4358 }
4359 }
4360
4361 // If depth of calling is getting too high, don't execute the function.
4362 orig_funcdepth = funcdepth_get();
4363 if (funcdepth_increment() == FAIL)
4364 return FAIL;
4365
4366 CLEAR_FIELD(ectx);
4367 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4368 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4369 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4370 {
4371 funcdepth_decrement();
4372 return FAIL;
4373 }
4374 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4375 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4376 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004377 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004378
4379 idx = argc - ufunc->uf_args.ga_len;
4380 if (idx > 0 && ufunc->uf_va_name == NULL)
4381 {
4382 if (idx == 1)
4383 emsg(_(e_one_argument_too_many));
4384 else
4385 semsg(_(e_nr_arguments_too_many), idx);
4386 goto failed_early;
4387 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004388 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4389 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004390 {
4391 if (idx == -1)
4392 emsg(_(e_one_argument_too_few));
4393 else
4394 semsg(_(e_nr_arguments_too_few), -idx);
4395 goto failed_early;
4396 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004397
4398 // Put arguments on the stack, but no more than what the function expects.
4399 // A lambda can be called with more arguments than it uses.
4400 for (idx = 0; idx < argc
4401 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4402 ++idx)
4403 {
4404 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4405 && argv[idx].v_type == VAR_SPECIAL
4406 && argv[idx].vval.v_number == VVAL_NONE)
4407 {
4408 // Use the default value.
4409 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4410 }
4411 else
4412 {
4413 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4414 && check_typval_arg_type(
4415 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4416 goto failed_early;
4417 copy_tv(&argv[idx], STACK_TV_BOT(0));
4418 }
4419 ++ectx.ec_stack.ga_len;
4420 }
4421
4422 // Turn varargs into a list. Empty list if no args.
4423 if (ufunc->uf_va_name != NULL)
4424 {
4425 int vararg_count = argc - ufunc->uf_args.ga_len;
4426
4427 if (vararg_count < 0)
4428 vararg_count = 0;
4429 else
4430 argc -= vararg_count;
4431 if (exe_newlist(vararg_count, &ectx) == FAIL)
4432 goto failed_early;
4433
4434 // Check the type of the list items.
4435 tv = STACK_TV_BOT(-1);
4436 if (ufunc->uf_va_type != NULL
4437 && ufunc->uf_va_type != &t_list_any
4438 && ufunc->uf_va_type->tt_member != &t_any
4439 && tv->vval.v_list != NULL)
4440 {
4441 type_T *expected = ufunc->uf_va_type->tt_member;
4442 listitem_T *li = tv->vval.v_list->lv_first;
4443
4444 for (idx = 0; idx < vararg_count; ++idx)
4445 {
4446 if (check_typval_arg_type(expected, &li->li_tv,
4447 argc + idx + 1) == FAIL)
4448 goto failed_early;
4449 li = li->li_next;
4450 }
4451 }
4452
4453 if (defcount > 0)
4454 // Move varargs list to below missing default arguments.
4455 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4456 --ectx.ec_stack.ga_len;
4457 }
4458
4459 // Make space for omitted arguments, will store default value below.
4460 // Any varargs list goes after them.
4461 if (defcount > 0)
4462 for (idx = 0; idx < defcount; ++idx)
4463 {
4464 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4465 ++ectx.ec_stack.ga_len;
4466 }
4467 if (ufunc->uf_va_name != NULL)
4468 ++ectx.ec_stack.ga_len;
4469
4470 // Frame pointer points to just after arguments.
4471 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4472 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4473
4474 {
4475 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4476 + ufunc->uf_dfunc_idx;
4477 ufunc_T *base_ufunc = dfunc->df_ufunc;
4478
4479 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4480 // by copy_func().
4481 if (partial != NULL || base_ufunc->uf_partial != NULL)
4482 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004483 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4484 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004485 goto failed_early;
4486 if (partial != NULL)
4487 {
4488 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4489 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004490 if (current_ectx->ec_outer_ref != NULL
4491 && current_ectx->ec_outer_ref->or_outer != NULL)
4492 ectx.ec_outer_ref->or_outer =
4493 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004494 }
4495 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004496 {
4497 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4498 ++partial->pt_refcount;
4499 ectx.ec_outer_ref->or_partial = partial;
4500 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004501 }
4502 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004503 {
4504 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4505 ++base_ufunc->uf_partial->pt_refcount;
4506 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4507 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004508 }
4509 }
4510
4511 // dummy frame entries
4512 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4513 {
4514 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4515 ++ectx.ec_stack.ga_len;
4516 }
4517
4518 {
4519 // Reserve space for local variables and any closure reference count.
4520 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4521 + ufunc->uf_dfunc_idx;
4522
4523 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4524 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4525 ectx.ec_stack.ga_len += dfunc->df_varcount;
4526 if (dfunc->df_has_closure)
4527 {
4528 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4529 STACK_TV_VAR(idx)->vval.v_number = 0;
4530 ++ectx.ec_stack.ga_len;
4531 }
4532
4533 ectx.ec_instr = INSTRUCTIONS(dfunc);
4534 }
4535
4536 // Following errors are in the function, not the caller.
4537 // Commands behave like vim9script.
4538 estack_push_ufunc(ufunc, 1);
4539 current_sctx = ufunc->uf_script_ctx;
4540 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4541
4542 // Use a specific location for storing error messages to be converted to an
4543 // exception.
4544 saved_msg_list = msg_list;
4545 msg_list = &private_msg_list;
4546
4547 // Do turn errors into exceptions.
4548 suppress_errthrow = FALSE;
4549
4550 // Do not delete the function while executing it.
4551 ++ufunc->uf_calls;
4552
4553 // When ":silent!" was used before calling then we still abort the
4554 // function. If ":silent!" is used in the function then we don't.
4555 emsg_silent_def = emsg_silent;
4556 did_emsg_def = 0;
4557
4558 ectx.ec_where.wt_index = 0;
4559 ectx.ec_where.wt_variable = FALSE;
4560
4561 // Execute the instructions until done.
4562 ret = exec_instructions(&ectx);
4563 if (ret == OK)
4564 {
4565 // function finished, get result from the stack.
4566 if (ufunc->uf_ret_type == &t_void)
4567 {
4568 rettv->v_type = VAR_VOID;
4569 }
4570 else
4571 {
4572 tv = STACK_TV_BOT(-1);
4573 *rettv = *tv;
4574 tv->v_type = VAR_UNKNOWN;
4575 }
4576 }
4577
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004578 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004579 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4580 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004581
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004582 // Deal with any remaining closures, they may be in use somewhere.
4583 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004584 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004585 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004586 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4587 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004588
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004589 estack_pop();
4590 current_sctx = save_current_sctx;
4591
Bram Moolenaarc970e422021-03-17 15:03:04 +01004592 // TODO: when is it safe to delete the function if it is no longer used?
4593 --ufunc->uf_calls;
4594
Bram Moolenaar352134b2020-10-17 22:04:08 +02004595 if (*msg_list != NULL && saved_msg_list != NULL)
4596 {
4597 msglist_T **plist = saved_msg_list;
4598
4599 // Append entries from the current msg_list (uncaught exceptions) to
4600 // the saved msg_list.
4601 while (*plist != NULL)
4602 plist = &(*plist)->next;
4603
4604 *plist = *msg_list;
4605 }
4606 msg_list = saved_msg_list;
4607
Bram Moolenaar4c137212021-04-19 16:48:48 +02004608 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004609 {
4610 cmdmod.cmod_filter_regmatch.regprog = NULL;
4611 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004612 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004613 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004614 emsg_silent_def = save_emsg_silent_def;
4615 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004616
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004617failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004618 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4620 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004621 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004624 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004625 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004626 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004627 if (ectx.ec_outer_ref->or_outer_allocated)
4628 vim_free(ectx.ec_outer_ref->or_outer);
4629 partial_unref(ectx.ec_outer_ref->or_partial);
4630 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004631 }
4632
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004633 // Not sure if this is necessary.
4634 suppress_errthrow = save_suppress_errthrow;
4635
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004636 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004637 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004638 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004639 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 return ret;
4641}
4642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004644 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4645 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4646 * "pfx" is prefixed to every line.
4647 */
4648 static void
4649list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4650{
4651 int line_idx = 0;
4652 int prev_current = 0;
4653 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004654 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004655
4656 for (current = 0; current < instr_count; ++current)
4657 {
4658 isn_T *iptr = &instr[current];
4659 char *line;
4660
4661 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004662 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004663 while (line_idx < iptr->isn_lnum
4664 && line_idx < ufunc->uf_lines.ga_len)
4665 {
4666 if (current > prev_current)
4667 {
4668 msg_puts("\n\n");
4669 prev_current = current;
4670 }
4671 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4672 if (line != NULL)
4673 msg(line);
4674 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004675 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4676 {
4677 int first_def_arg = ufunc->uf_args.ga_len
4678 - ufunc->uf_def_args.ga_len;
4679
4680 if (def_arg_idx > 0)
4681 msg_puts("\n\n");
4682 msg_start();
4683 msg_puts(" ");
4684 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4685 first_def_arg + def_arg_idx]);
4686 msg_puts(" = ");
4687 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4688 msg_clr_eos();
4689 msg_end();
4690 }
4691 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004692
4693 switch (iptr->isn_type)
4694 {
4695 case ISN_EXEC:
4696 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4697 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004698 case ISN_EXEC_SPLIT:
4699 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4700 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004701 case ISN_LEGACY_EVAL:
4702 smsg("%s%4d EVAL legacy %s", pfx, current,
4703 iptr->isn_arg.string);
4704 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004705 case ISN_REDIRSTART:
4706 smsg("%s%4d REDIR", pfx, current);
4707 break;
4708 case ISN_REDIREND:
4709 smsg("%s%4d REDIR END%s", pfx, current,
4710 iptr->isn_arg.number ? " append" : "");
4711 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004712 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004713#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004714 smsg("%s%4d CEXPR pre %s", pfx, current,
4715 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004716#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004717 break;
4718 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004719#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004720 {
4721 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4722
4723 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4724 cexpr_get_auname(cer->cer_cmdidx),
4725 cer->cer_forceit ? "!" : "",
4726 cer->cer_cmdline);
4727 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004728#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004729 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004730 case ISN_INSTR:
4731 {
4732 smsg("%s%4d INSTR", pfx, current);
4733 list_instructions(" ", iptr->isn_arg.instr,
4734 INT_MAX, NULL);
4735 msg(" -------------");
4736 }
4737 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004738 case ISN_SUBSTITUTE:
4739 {
4740 subs_T *subs = &iptr->isn_arg.subs;
4741
4742 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4743 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4744 msg(" -------------");
4745 }
4746 break;
4747 case ISN_EXECCONCAT:
4748 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4749 (varnumber_T)iptr->isn_arg.number);
4750 break;
4751 case ISN_ECHO:
4752 {
4753 echo_T *echo = &iptr->isn_arg.echo;
4754
4755 smsg("%s%4d %s %d", pfx, current,
4756 echo->echo_with_white ? "ECHO" : "ECHON",
4757 echo->echo_count);
4758 }
4759 break;
4760 case ISN_EXECUTE:
4761 smsg("%s%4d EXECUTE %lld", pfx, current,
4762 (varnumber_T)(iptr->isn_arg.number));
4763 break;
4764 case ISN_ECHOMSG:
4765 smsg("%s%4d ECHOMSG %lld", pfx, current,
4766 (varnumber_T)(iptr->isn_arg.number));
4767 break;
4768 case ISN_ECHOERR:
4769 smsg("%s%4d ECHOERR %lld", pfx, current,
4770 (varnumber_T)(iptr->isn_arg.number));
4771 break;
4772 case ISN_LOAD:
4773 {
4774 if (iptr->isn_arg.number < 0)
4775 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4776 (varnumber_T)(iptr->isn_arg.number
4777 + STACK_FRAME_SIZE));
4778 else
4779 smsg("%s%4d LOAD $%lld", pfx, current,
4780 (varnumber_T)(iptr->isn_arg.number));
4781 }
4782 break;
4783 case ISN_LOADOUTER:
4784 {
4785 if (iptr->isn_arg.number < 0)
4786 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4787 iptr->isn_arg.outer.outer_depth,
4788 iptr->isn_arg.outer.outer_idx
4789 + STACK_FRAME_SIZE);
4790 else
4791 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4792 iptr->isn_arg.outer.outer_depth,
4793 iptr->isn_arg.outer.outer_idx);
4794 }
4795 break;
4796 case ISN_LOADV:
4797 smsg("%s%4d LOADV v:%s", pfx, current,
4798 get_vim_var_name(iptr->isn_arg.number));
4799 break;
4800 case ISN_LOADSCRIPT:
4801 {
4802 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4803 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4804 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4805 + sref->sref_idx;
4806
4807 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4808 sv->sv_name,
4809 sref->sref_idx,
4810 si->sn_name);
4811 }
4812 break;
4813 case ISN_LOADS:
4814 {
4815 scriptitem_T *si = SCRIPT_ITEM(
4816 iptr->isn_arg.loadstore.ls_sid);
4817
4818 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4819 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4820 }
4821 break;
4822 case ISN_LOADAUTO:
4823 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4824 break;
4825 case ISN_LOADG:
4826 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4827 break;
4828 case ISN_LOADB:
4829 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4830 break;
4831 case ISN_LOADW:
4832 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4833 break;
4834 case ISN_LOADT:
4835 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4836 break;
4837 case ISN_LOADGDICT:
4838 smsg("%s%4d LOAD g:", pfx, current);
4839 break;
4840 case ISN_LOADBDICT:
4841 smsg("%s%4d LOAD b:", pfx, current);
4842 break;
4843 case ISN_LOADWDICT:
4844 smsg("%s%4d LOAD w:", pfx, current);
4845 break;
4846 case ISN_LOADTDICT:
4847 smsg("%s%4d LOAD t:", pfx, current);
4848 break;
4849 case ISN_LOADOPT:
4850 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4851 break;
4852 case ISN_LOADENV:
4853 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4854 break;
4855 case ISN_LOADREG:
4856 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4857 break;
4858
4859 case ISN_STORE:
4860 if (iptr->isn_arg.number < 0)
4861 smsg("%s%4d STORE arg[%lld]", pfx, current,
4862 iptr->isn_arg.number + STACK_FRAME_SIZE);
4863 else
4864 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4865 break;
4866 case ISN_STOREOUTER:
4867 {
4868 if (iptr->isn_arg.number < 0)
4869 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4870 iptr->isn_arg.outer.outer_depth,
4871 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4872 else
4873 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4874 iptr->isn_arg.outer.outer_depth,
4875 iptr->isn_arg.outer.outer_idx);
4876 }
4877 break;
4878 case ISN_STOREV:
4879 smsg("%s%4d STOREV v:%s", pfx, current,
4880 get_vim_var_name(iptr->isn_arg.number));
4881 break;
4882 case ISN_STOREAUTO:
4883 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4884 break;
4885 case ISN_STOREG:
4886 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4887 break;
4888 case ISN_STOREB:
4889 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4890 break;
4891 case ISN_STOREW:
4892 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4893 break;
4894 case ISN_STORET:
4895 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4896 break;
4897 case ISN_STORES:
4898 {
4899 scriptitem_T *si = SCRIPT_ITEM(
4900 iptr->isn_arg.loadstore.ls_sid);
4901
4902 smsg("%s%4d STORES %s in %s", pfx, current,
4903 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4904 }
4905 break;
4906 case ISN_STORESCRIPT:
4907 {
4908 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4909 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4910 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4911 + sref->sref_idx;
4912
4913 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4914 sv->sv_name,
4915 sref->sref_idx,
4916 si->sn_name);
4917 }
4918 break;
4919 case ISN_STOREOPT:
4920 smsg("%s%4d STOREOPT &%s", pfx, current,
4921 iptr->isn_arg.storeopt.so_name);
4922 break;
4923 case ISN_STOREENV:
4924 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4925 break;
4926 case ISN_STOREREG:
4927 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4928 break;
4929 case ISN_STORENR:
4930 smsg("%s%4d STORE %lld in $%d", pfx, current,
4931 iptr->isn_arg.storenr.stnr_val,
4932 iptr->isn_arg.storenr.stnr_idx);
4933 break;
4934
4935 case ISN_STOREINDEX:
4936 smsg("%s%4d STOREINDEX %s", pfx, current,
4937 vartype_name(iptr->isn_arg.vartype));
4938 break;
4939
4940 case ISN_STORERANGE:
4941 smsg("%s%4d STORERANGE", pfx, current);
4942 break;
4943
4944 // constants
4945 case ISN_PUSHNR:
4946 smsg("%s%4d PUSHNR %lld", pfx, current,
4947 (varnumber_T)(iptr->isn_arg.number));
4948 break;
4949 case ISN_PUSHBOOL:
4950 case ISN_PUSHSPEC:
4951 smsg("%s%4d PUSH %s", pfx, current,
4952 get_var_special_name(iptr->isn_arg.number));
4953 break;
4954 case ISN_PUSHF:
4955#ifdef FEAT_FLOAT
4956 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4957#endif
4958 break;
4959 case ISN_PUSHS:
4960 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4961 break;
4962 case ISN_PUSHBLOB:
4963 {
4964 char_u *r;
4965 char_u numbuf[NUMBUFLEN];
4966 char_u *tofree;
4967
4968 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4969 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4970 vim_free(tofree);
4971 }
4972 break;
4973 case ISN_PUSHFUNC:
4974 {
4975 char *name = (char *)iptr->isn_arg.string;
4976
4977 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4978 name == NULL ? "[none]" : name);
4979 }
4980 break;
4981 case ISN_PUSHCHANNEL:
4982#ifdef FEAT_JOB_CHANNEL
4983 {
4984 channel_T *channel = iptr->isn_arg.channel;
4985
4986 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4987 channel == NULL ? 0 : channel->ch_id);
4988 }
4989#endif
4990 break;
4991 case ISN_PUSHJOB:
4992#ifdef FEAT_JOB_CHANNEL
4993 {
4994 typval_T tv;
4995 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004996 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02004997
4998 tv.v_type = VAR_JOB;
4999 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005000 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005001 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5002 }
5003#endif
5004 break;
5005 case ISN_PUSHEXC:
5006 smsg("%s%4d PUSH v:exception", pfx, current);
5007 break;
5008 case ISN_UNLET:
5009 smsg("%s%4d UNLET%s %s", pfx, current,
5010 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5011 iptr->isn_arg.unlet.ul_name);
5012 break;
5013 case ISN_UNLETENV:
5014 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5015 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5016 iptr->isn_arg.unlet.ul_name);
5017 break;
5018 case ISN_UNLETINDEX:
5019 smsg("%s%4d UNLETINDEX", pfx, current);
5020 break;
5021 case ISN_UNLETRANGE:
5022 smsg("%s%4d UNLETRANGE", pfx, current);
5023 break;
5024 case ISN_LOCKCONST:
5025 smsg("%s%4d LOCKCONST", pfx, current);
5026 break;
5027 case ISN_NEWLIST:
5028 smsg("%s%4d NEWLIST size %lld", pfx, current,
5029 (varnumber_T)(iptr->isn_arg.number));
5030 break;
5031 case ISN_NEWDICT:
5032 smsg("%s%4d NEWDICT size %lld", pfx, current,
5033 (varnumber_T)(iptr->isn_arg.number));
5034 break;
5035
5036 // function call
5037 case ISN_BCALL:
5038 {
5039 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5040
5041 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5042 internal_func_name(cbfunc->cbf_idx),
5043 cbfunc->cbf_argcount);
5044 }
5045 break;
5046 case ISN_DCALL:
5047 {
5048 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5049 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5050 + cdfunc->cdf_idx;
5051
5052 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5053 df->df_ufunc->uf_name_exp != NULL
5054 ? df->df_ufunc->uf_name_exp
5055 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5056 }
5057 break;
5058 case ISN_UCALL:
5059 {
5060 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5061
5062 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5063 cufunc->cuf_name, cufunc->cuf_argcount);
5064 }
5065 break;
5066 case ISN_PCALL:
5067 {
5068 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5069
5070 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5071 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5072 }
5073 break;
5074 case ISN_PCALL_END:
5075 smsg("%s%4d PCALL end", pfx, current);
5076 break;
5077 case ISN_RETURN:
5078 smsg("%s%4d RETURN", pfx, current);
5079 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005080 case ISN_RETURN_VOID:
5081 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005082 break;
5083 case ISN_FUNCREF:
5084 {
5085 funcref_T *funcref = &iptr->isn_arg.funcref;
5086 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5087 + funcref->fr_func;
5088
5089 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5090 }
5091 break;
5092
5093 case ISN_NEWFUNC:
5094 {
5095 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5096
5097 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5098 newfunc->nf_lambda, newfunc->nf_global);
5099 }
5100 break;
5101
5102 case ISN_DEF:
5103 {
5104 char_u *name = iptr->isn_arg.string;
5105
5106 smsg("%s%4d DEF %s", pfx, current,
5107 name == NULL ? (char_u *)"" : name);
5108 }
5109 break;
5110
5111 case ISN_JUMP:
5112 {
5113 char *when = "?";
5114
5115 switch (iptr->isn_arg.jump.jump_when)
5116 {
5117 case JUMP_ALWAYS:
5118 when = "JUMP";
5119 break;
5120 case JUMP_AND_KEEP_IF_TRUE:
5121 when = "JUMP_AND_KEEP_IF_TRUE";
5122 break;
5123 case JUMP_IF_FALSE:
5124 when = "JUMP_IF_FALSE";
5125 break;
5126 case JUMP_AND_KEEP_IF_FALSE:
5127 when = "JUMP_AND_KEEP_IF_FALSE";
5128 break;
5129 case JUMP_IF_COND_FALSE:
5130 when = "JUMP_IF_COND_FALSE";
5131 break;
5132 case JUMP_IF_COND_TRUE:
5133 when = "JUMP_IF_COND_TRUE";
5134 break;
5135 }
5136 smsg("%s%4d %s -> %d", pfx, current, when,
5137 iptr->isn_arg.jump.jump_where);
5138 }
5139 break;
5140
5141 case ISN_JUMP_IF_ARG_SET:
5142 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5143 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5144 iptr->isn_arg.jump.jump_where);
5145 break;
5146
5147 case ISN_FOR:
5148 {
5149 forloop_T *forloop = &iptr->isn_arg.forloop;
5150
5151 smsg("%s%4d FOR $%d -> %d", pfx, current,
5152 forloop->for_idx, forloop->for_end);
5153 }
5154 break;
5155
5156 case ISN_TRY:
5157 {
5158 try_T *try = &iptr->isn_arg.try;
5159
5160 if (try->try_ref->try_finally == 0)
5161 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5162 pfx, current,
5163 try->try_ref->try_catch,
5164 try->try_ref->try_endtry);
5165 else
5166 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5167 pfx, current,
5168 try->try_ref->try_catch,
5169 try->try_ref->try_finally,
5170 try->try_ref->try_endtry);
5171 }
5172 break;
5173 case ISN_CATCH:
5174 // TODO
5175 smsg("%s%4d CATCH", pfx, current);
5176 break;
5177 case ISN_TRYCONT:
5178 {
5179 trycont_T *trycont = &iptr->isn_arg.trycont;
5180
5181 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5182 trycont->tct_levels,
5183 trycont->tct_levels == 1 ? "" : "s",
5184 trycont->tct_where);
5185 }
5186 break;
5187 case ISN_FINALLY:
5188 smsg("%s%4d FINALLY", pfx, current);
5189 break;
5190 case ISN_ENDTRY:
5191 smsg("%s%4d ENDTRY", pfx, current);
5192 break;
5193 case ISN_THROW:
5194 smsg("%s%4d THROW", pfx, current);
5195 break;
5196
5197 // expression operations on number
5198 case ISN_OPNR:
5199 case ISN_OPFLOAT:
5200 case ISN_OPANY:
5201 {
5202 char *what;
5203 char *ins;
5204
5205 switch (iptr->isn_arg.op.op_type)
5206 {
5207 case EXPR_MULT: what = "*"; break;
5208 case EXPR_DIV: what = "/"; break;
5209 case EXPR_REM: what = "%"; break;
5210 case EXPR_SUB: what = "-"; break;
5211 case EXPR_ADD: what = "+"; break;
5212 default: what = "???"; break;
5213 }
5214 switch (iptr->isn_type)
5215 {
5216 case ISN_OPNR: ins = "OPNR"; break;
5217 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5218 case ISN_OPANY: ins = "OPANY"; break;
5219 default: ins = "???"; break;
5220 }
5221 smsg("%s%4d %s %s", pfx, current, ins, what);
5222 }
5223 break;
5224
5225 case ISN_COMPAREBOOL:
5226 case ISN_COMPARESPECIAL:
5227 case ISN_COMPARENR:
5228 case ISN_COMPAREFLOAT:
5229 case ISN_COMPARESTRING:
5230 case ISN_COMPAREBLOB:
5231 case ISN_COMPARELIST:
5232 case ISN_COMPAREDICT:
5233 case ISN_COMPAREFUNC:
5234 case ISN_COMPAREANY:
5235 {
5236 char *p;
5237 char buf[10];
5238 char *type;
5239
5240 switch (iptr->isn_arg.op.op_type)
5241 {
5242 case EXPR_EQUAL: p = "=="; break;
5243 case EXPR_NEQUAL: p = "!="; break;
5244 case EXPR_GREATER: p = ">"; break;
5245 case EXPR_GEQUAL: p = ">="; break;
5246 case EXPR_SMALLER: p = "<"; break;
5247 case EXPR_SEQUAL: p = "<="; break;
5248 case EXPR_MATCH: p = "=~"; break;
5249 case EXPR_IS: p = "is"; break;
5250 case EXPR_ISNOT: p = "isnot"; break;
5251 case EXPR_NOMATCH: p = "!~"; break;
5252 default: p = "???"; break;
5253 }
5254 STRCPY(buf, p);
5255 if (iptr->isn_arg.op.op_ic == TRUE)
5256 strcat(buf, "?");
5257 switch(iptr->isn_type)
5258 {
5259 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5260 case ISN_COMPARESPECIAL:
5261 type = "COMPARESPECIAL"; break;
5262 case ISN_COMPARENR: type = "COMPARENR"; break;
5263 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5264 case ISN_COMPARESTRING:
5265 type = "COMPARESTRING"; break;
5266 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5267 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5268 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5269 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5270 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5271 default: type = "???"; break;
5272 }
5273
5274 smsg("%s%4d %s %s", pfx, current, type, buf);
5275 }
5276 break;
5277
5278 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5279 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5280
5281 // expression operations
5282 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5283 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5284 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5285 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5286 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5287 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5288 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5289 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5290 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5291 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5292 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5293 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5294 pfx, current, iptr->isn_arg.number); break;
5295 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5296 pfx, current, iptr->isn_arg.number); break;
5297 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5298 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5299 iptr->isn_arg.string); break;
5300 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5301
5302 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5303 case ISN_CHECKTYPE:
5304 {
5305 checktype_T *ct = &iptr->isn_arg.type;
5306 char *tofree;
5307
5308 if (ct->ct_arg_idx == 0)
5309 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5310 type_name(ct->ct_type, &tofree),
5311 (int)ct->ct_off);
5312 else
5313 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5314 type_name(ct->ct_type, &tofree),
5315 (int)ct->ct_off,
5316 (int)ct->ct_arg_idx);
5317 vim_free(tofree);
5318 break;
5319 }
5320 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5321 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5322 iptr->isn_arg.checklen.cl_min_len);
5323 break;
5324 case ISN_SETTYPE:
5325 {
5326 char *tofree;
5327
5328 smsg("%s%4d SETTYPE %s", pfx, current,
5329 type_name(iptr->isn_arg.type.ct_type, &tofree));
5330 vim_free(tofree);
5331 break;
5332 }
5333 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005334 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5335 smsg("%s%4d INVERT %d (!val)", pfx, current,
5336 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005337 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005338 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5339 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005340 break;
5341 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005342 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005343 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005344 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5345 pfx, current,
5346 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005347 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005348 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5349 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005350 break;
5351 case ISN_PUT:
5352 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5353 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005354 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005355 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5356 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005357 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005358 else
5359 smsg("%s%4d PUT %c %ld", pfx, current,
5360 iptr->isn_arg.put.put_regname,
5361 (long)iptr->isn_arg.put.put_lnum);
5362 break;
5363
5364 // TODO: summarize modifiers
5365 case ISN_CMDMOD:
5366 {
5367 char_u *buf;
5368 size_t len = produce_cmdmods(
5369 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5370
5371 buf = alloc(len + 1);
5372 if (buf != NULL)
5373 {
5374 (void)produce_cmdmods(
5375 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5376 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5377 vim_free(buf);
5378 }
5379 break;
5380 }
5381 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5382
5383 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005384 smsg("%s%4d PROFILE START line %d", pfx, current,
5385 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005386 break;
5387
5388 case ISN_PROF_END:
5389 smsg("%s%4d PROFILE END", pfx, current);
5390 break;
5391
Bram Moolenaare99d4222021-06-13 14:01:26 +02005392 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005393 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5394 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005395 break;
5396
Bram Moolenaar4c137212021-04-19 16:48:48 +02005397 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5398 iptr->isn_arg.unpack.unp_count,
5399 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5400 break;
5401 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5402 iptr->isn_arg.shuffle.shfl_item,
5403 iptr->isn_arg.shuffle.shfl_up);
5404 break;
5405 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5406
5407 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5408 return;
5409 }
5410
5411 out_flush(); // output one line at a time
5412 ui_breakcheck();
5413 if (got_int)
5414 break;
5415 }
5416}
5417
5418/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005419 * Handle command line completion for the :disassemble command.
5420 */
5421 void
5422set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5423{
5424 char_u *p;
5425
5426 // Default: expand user functions, "debug" and "profile"
5427 xp->xp_context = EXPAND_DISASSEMBLE;
5428 xp->xp_pattern = arg;
5429
5430 // first argument already typed: only user function names
5431 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5432 {
5433 xp->xp_context = EXPAND_USER_FUNC;
5434 xp->xp_pattern = skipwhite(p);
5435 }
5436}
5437
5438/*
5439 * Function given to ExpandGeneric() to obtain the list of :disassemble
5440 * arguments.
5441 */
5442 char_u *
5443get_disassemble_argument(expand_T *xp, int idx)
5444{
5445 if (idx == 0)
5446 return (char_u *)"debug";
5447 if (idx == 1)
5448 return (char_u *)"profile";
5449 return get_user_func_name(xp, idx - 2);
5450}
5451
5452/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005453 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005454 * We don't really need this at runtime, but we do have tests that require it,
5455 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456 */
5457 void
5458ex_disassemble(exarg_T *eap)
5459{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005460 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005461 char_u *fname;
5462 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463 dfunc_T *dfunc;
5464 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005465 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005466 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005467 compiletype_T compile_type = CT_NONE;
5468
5469 if (STRNCMP(arg, "profile", 7) == 0)
5470 {
5471 compile_type = CT_PROFILE;
5472 arg = skipwhite(arg + 7);
5473 }
5474 else if (STRNCMP(arg, "debug", 5) == 0)
5475 {
5476 compile_type = CT_DEBUG;
5477 arg = skipwhite(arg + 5);
5478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005480 if (STRNCMP(arg, "<lambda>", 8) == 0)
5481 {
5482 arg += 8;
5483 (void)getdigits(&arg);
5484 fname = vim_strnsave(eap->arg, arg - eap->arg);
5485 }
5486 else
5487 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005488 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005489 if (fname == NULL)
5490 {
5491 semsg(_(e_invarg2), eap->arg);
5492 return;
5493 }
5494
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005495 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005496 if (ufunc == NULL)
5497 {
5498 char_u *p = untrans_function_name(fname);
5499
5500 if (p != NULL)
5501 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005502 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005503 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005504 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505 if (ufunc == NULL)
5506 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005507 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508 return;
5509 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005510 if (func_needs_compiling(ufunc, compile_type)
5511 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005512 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005513 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005515 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516 return;
5517 }
5518 if (ufunc->uf_name_exp != NULL)
5519 msg((char *)ufunc->uf_name_exp);
5520 else
5521 msg((char *)ufunc->uf_name);
5522
5523 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005524 switch (compile_type)
5525 {
5526 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005527#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005528 instr = dfunc->df_instr_prof;
5529 instr_count = dfunc->df_instr_prof_count;
5530 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005531#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005532 // FALLTHROUGH
5533 case CT_NONE:
5534 instr = dfunc->df_instr;
5535 instr_count = dfunc->df_instr_count;
5536 break;
5537 case CT_DEBUG:
5538 instr = dfunc->df_instr_debug;
5539 instr_count = dfunc->df_instr_debug_count;
5540 break;
5541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542
Bram Moolenaar4c137212021-04-19 16:48:48 +02005543 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544}
5545
5546/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005547 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 * list, etc. Mostly like what JavaScript does, except that empty list and
5549 * empty dictionary are FALSE.
5550 */
5551 int
5552tv2bool(typval_T *tv)
5553{
5554 switch (tv->v_type)
5555 {
5556 case VAR_NUMBER:
5557 return tv->vval.v_number != 0;
5558 case VAR_FLOAT:
5559#ifdef FEAT_FLOAT
5560 return tv->vval.v_float != 0.0;
5561#else
5562 break;
5563#endif
5564 case VAR_PARTIAL:
5565 return tv->vval.v_partial != NULL;
5566 case VAR_FUNC:
5567 case VAR_STRING:
5568 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5569 case VAR_LIST:
5570 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5571 case VAR_DICT:
5572 return tv->vval.v_dict != NULL
5573 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5574 case VAR_BOOL:
5575 case VAR_SPECIAL:
5576 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5577 case VAR_JOB:
5578#ifdef FEAT_JOB_CHANNEL
5579 return tv->vval.v_job != NULL;
5580#else
5581 break;
5582#endif
5583 case VAR_CHANNEL:
5584#ifdef FEAT_JOB_CHANNEL
5585 return tv->vval.v_channel != NULL;
5586#else
5587 break;
5588#endif
5589 case VAR_BLOB:
5590 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5591 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005592 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005593 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005594 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595 break;
5596 }
5597 return FALSE;
5598}
5599
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005600 void
5601emsg_using_string_as(typval_T *tv, int as_number)
5602{
5603 semsg(_(as_number ? e_using_string_as_number_str
5604 : e_using_string_as_bool_str),
5605 tv->vval.v_string == NULL
5606 ? (char_u *)"" : tv->vval.v_string);
5607}
5608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609/*
5610 * If "tv" is a string give an error and return FAIL.
5611 */
5612 int
5613check_not_string(typval_T *tv)
5614{
5615 if (tv->v_type == VAR_STRING)
5616 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005617 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 clear_tv(tv);
5619 return FAIL;
5620 }
5621 return OK;
5622}
5623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005624
5625#endif // FEAT_EVAL