blob: 96bd24509367bc2ad5a17a294c40eddb643fcbdb [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
2818 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002819 case ISN_RETURN_ZERO:
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 Moolenaar299f3032021-01-08 20:53:09 +01002824 tv->v_type = VAR_NUMBER;
2825 tv->vval.v_number = 0;
2826 tv->v_lock = 0;
2827 // FALLTHROUGH
2828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 case ISN_RETURN:
2830 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002831 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002832 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002833
2834 if (trystack->ga_len > 0)
2835 trycmd = ((trycmd_T *)trystack->ga_data)
2836 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002837 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002838 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002840 // jump to ":finally" or ":endtry"
2841 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002842 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002843 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002844 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 trycmd->tcd_return = TRUE;
2846 }
2847 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002848 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 }
2850 break;
2851
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002852 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 case ISN_FUNCREF:
2854 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002855 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2856 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2857 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002860 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002861 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002862 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002863 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002864 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002865 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002866 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002867 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002868 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002870 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 tv->vval.v_partial = pt;
2872 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002873 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 }
2875 break;
2876
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002877 // Create a global function from a lambda.
2878 case ISN_NEWFUNC:
2879 {
2880 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2881
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002882 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002883 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002884 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002885 }
2886 break;
2887
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002888 // List functions
2889 case ISN_DEF:
2890 if (iptr->isn_arg.string == NULL)
2891 list_functions(NULL);
2892 else
2893 {
2894 exarg_T ea;
2895
2896 CLEAR_FIELD(ea);
2897 ea.cmd = ea.arg = iptr->isn_arg.string;
2898 define_function(&ea, NULL);
2899 }
2900 break;
2901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 // jump if a condition is met
2903 case ISN_JUMP:
2904 {
2905 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002906 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 int jump = TRUE;
2908
2909 if (when != JUMP_ALWAYS)
2910 {
2911 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002912 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002913 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002914 || when == JUMP_IF_COND_TRUE)
2915 {
2916 SOURCING_LNUM = iptr->isn_lnum;
2917 jump = tv_get_bool_chk(tv, &error);
2918 if (error)
2919 goto on_error;
2920 }
2921 else
2922 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002924 || when == JUMP_AND_KEEP_IF_FALSE
2925 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002927 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 {
2929 // drop the value from the stack
2930 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002931 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 }
2933 }
2934 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002935 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 }
2937 break;
2938
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002939 // Jump if an argument with a default value was already set and not
2940 // v:none.
2941 case ISN_JUMP_IF_ARG_SET:
2942 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2943 if (tv->v_type != VAR_UNKNOWN
2944 && !(tv->v_type == VAR_SPECIAL
2945 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002946 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002947 break;
2948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 // top of a for loop
2950 case ISN_FOR:
2951 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002952 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 typval_T *idxtv =
2954 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2955
Bram Moolenaar4c137212021-04-19 16:48:48 +02002956 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002957 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002958 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002959 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002960 list_T *list = ltv->vval.v_list;
2961
2962 // push the next item from the list
2963 ++idxtv->vval.v_number;
2964 if (list == NULL
2965 || idxtv->vval.v_number >= list->lv_len)
2966 {
2967 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002968 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2969 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002970 }
2971 else if (list->lv_first == &range_list_item)
2972 {
2973 // non-materialized range() list
2974 tv = STACK_TV_BOT(0);
2975 tv->v_type = VAR_NUMBER;
2976 tv->v_lock = 0;
2977 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002979 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002980 }
2981 else
2982 {
2983 listitem_T *li = list_find(list,
2984 idxtv->vval.v_number);
2985
2986 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002987 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002988 }
2989 }
2990 else if (ltv->v_type == VAR_STRING)
2991 {
2992 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002993
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002994 // The index is for the last byte of the previous
2995 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002996 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002997 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002998 {
2999 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003000 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3001 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003002 }
3003 else
3004 {
3005 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3006
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003007 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003008 tv = STACK_TV_BOT(0);
3009 tv->v_type = VAR_STRING;
3010 tv->vval.v_string = vim_strnsave(
3011 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003012 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003013 idxtv->vval.v_number += clen - 1;
3014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003016 else if (ltv->v_type == VAR_BLOB)
3017 {
3018 blob_T *blob = ltv->vval.v_blob;
3019
3020 // When we get here the first time make a copy of the
3021 // blob, so that the iteration still works when it is
3022 // changed.
3023 if (idxtv->vval.v_number == -1 && blob != NULL)
3024 {
3025 blob_copy(blob, ltv);
3026 blob_unref(blob);
3027 blob = ltv->vval.v_blob;
3028 }
3029
3030 // The index is for the previous byte.
3031 ++idxtv->vval.v_number;
3032 if (blob == NULL
3033 || idxtv->vval.v_number >= blob_len(blob))
3034 {
3035 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003036 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3037 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003038 }
3039 else
3040 {
3041 // Push the next byte from the blob.
3042 tv = STACK_TV_BOT(0);
3043 tv->v_type = VAR_NUMBER;
3044 tv->vval.v_number = blob_get(blob,
3045 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003046 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003047 }
3048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 else
3050 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003051 semsg(_(e_for_loop_on_str_not_supported),
3052 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003053 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 }
3055 }
3056 break;
3057
3058 // start of ":try" block
3059 case ISN_TRY:
3060 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003061 trycmd_T *trycmd = NULL;
3062
Bram Moolenaar4c137212021-04-19 16:48:48 +02003063 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003064 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003065 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3066 + ectx->ec_trystack.ga_len;
3067 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003069 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003070 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3071 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003072 trycmd->tcd_catch_idx =
3073 iptr->isn_arg.try.try_ref->try_catch;
3074 trycmd->tcd_finally_idx =
3075 iptr->isn_arg.try.try_ref->try_finally;
3076 trycmd->tcd_endtry_idx =
3077 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
3079 break;
3080
3081 case ISN_PUSHEXC:
3082 if (current_exception == NULL)
3083 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003084 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003086 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003088 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003089 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003091 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003093 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 tv->vval.v_string = vim_strsave(
3095 (char_u *)current_exception->value);
3096 break;
3097
3098 case ISN_CATCH:
3099 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003100 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101
Bram Moolenaar4c137212021-04-19 16:48:48 +02003102 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 if (trystack->ga_len > 0)
3104 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003105 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 + trystack->ga_len - 1;
3107 trycmd->tcd_caught = TRUE;
3108 }
3109 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003110 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 catch_exception(current_exception);
3112 }
3113 break;
3114
Bram Moolenaarc150c092021-02-13 15:02:46 +01003115 case ISN_TRYCONT:
3116 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003117 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003118 trycont_T *trycont = &iptr->isn_arg.trycont;
3119 int i;
3120 trycmd_T *trycmd;
3121 int iidx = trycont->tct_where;
3122
3123 if (trystack->ga_len < trycont->tct_levels)
3124 {
3125 siemsg("TRYCONT: expected %d levels, found %d",
3126 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003127 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003128 }
3129 // Make :endtry jump to any outer try block and the last
3130 // :endtry inside the loop to the loop start.
3131 for (i = trycont->tct_levels; i > 0; --i)
3132 {
3133 trycmd = ((trycmd_T *)trystack->ga_data)
3134 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003135 // Add one to tcd_cont to be able to jump to
3136 // instruction with index zero.
3137 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003138 iidx = trycmd->tcd_finally_idx == 0
3139 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003140 }
3141 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003142 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003143 }
3144 break;
3145
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003146 case ISN_FINALLY:
3147 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003148 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003149 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3150 + trystack->ga_len - 1;
3151
3152 // Reset the index to avoid a return statement jumps here
3153 // again.
3154 trycmd->tcd_finally_idx = 0;
3155 break;
3156 }
3157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 // end of ":try" block
3159 case ISN_ENDTRY:
3160 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003161 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162
3163 if (trystack->ga_len > 0)
3164 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003165 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 --trystack->ga_len;
3168 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003169 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 trycmd = ((trycmd_T *)trystack->ga_data)
3171 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003172 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 {
3174 // discard the exception
3175 if (caught_stack == current_exception)
3176 caught_stack = caught_stack->caught;
3177 discard_current_exception();
3178 }
3179
3180 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003181 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003182
Bram Moolenaar4c137212021-04-19 16:48:48 +02003183 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003184 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003185 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003186 clear_tv(STACK_TV_BOT(0));
3187 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003188 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003189 // handling :continue: jump to outer try block or
3190 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003191 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 }
3193 }
3194 break;
3195
3196 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003197 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003198 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003199
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003200 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3201 {
3202 // throwing an exception while using "silent!" causes
3203 // the function to abort but not display an error.
3204 tv = STACK_TV_BOT(-1);
3205 clear_tv(tv);
3206 tv->v_type = VAR_NUMBER;
3207 tv->vval.v_number = 0;
3208 goto done;
3209 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003210 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003211 tv = STACK_TV_BOT(0);
3212 if (tv->vval.v_string == NULL
3213 || *skipwhite(tv->vval.v_string) == NUL)
3214 {
3215 vim_free(tv->vval.v_string);
3216 SOURCING_LNUM = iptr->isn_lnum;
3217 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003218 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003219 }
3220
3221 // Inside a "catch" we need to first discard the caught
3222 // exception.
3223 if (trystack->ga_len > 0)
3224 {
3225 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3226 + trystack->ga_len - 1;
3227 if (trycmd->tcd_caught && current_exception != NULL)
3228 {
3229 // discard the exception
3230 if (caught_stack == current_exception)
3231 caught_stack = caught_stack->caught;
3232 discard_current_exception();
3233 trycmd->tcd_caught = FALSE;
3234 }
3235 }
3236
3237 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3238 == FAIL)
3239 {
3240 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003241 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003242 }
3243 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 break;
3246
3247 // compare with special values
3248 case ISN_COMPAREBOOL:
3249 case ISN_COMPARESPECIAL:
3250 {
3251 typval_T *tv1 = STACK_TV_BOT(-2);
3252 typval_T *tv2 = STACK_TV_BOT(-1);
3253 varnumber_T arg1 = tv1->vval.v_number;
3254 varnumber_T arg2 = tv2->vval.v_number;
3255 int res;
3256
3257 switch (iptr->isn_arg.op.op_type)
3258 {
3259 case EXPR_EQUAL: res = arg1 == arg2; break;
3260 case EXPR_NEQUAL: res = arg1 != arg2; break;
3261 default: res = 0; break;
3262 }
3263
Bram Moolenaar4c137212021-04-19 16:48:48 +02003264 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 tv1->v_type = VAR_BOOL;
3266 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3267 }
3268 break;
3269
3270 // Operation with two number arguments
3271 case ISN_OPNR:
3272 case ISN_COMPARENR:
3273 {
3274 typval_T *tv1 = STACK_TV_BOT(-2);
3275 typval_T *tv2 = STACK_TV_BOT(-1);
3276 varnumber_T arg1 = tv1->vval.v_number;
3277 varnumber_T arg2 = tv2->vval.v_number;
3278 varnumber_T res;
3279
3280 switch (iptr->isn_arg.op.op_type)
3281 {
3282 case EXPR_MULT: res = arg1 * arg2; break;
3283 case EXPR_DIV: res = arg1 / arg2; break;
3284 case EXPR_REM: res = arg1 % arg2; break;
3285 case EXPR_SUB: res = arg1 - arg2; break;
3286 case EXPR_ADD: res = arg1 + arg2; break;
3287
3288 case EXPR_EQUAL: res = arg1 == arg2; break;
3289 case EXPR_NEQUAL: res = arg1 != arg2; break;
3290 case EXPR_GREATER: res = arg1 > arg2; break;
3291 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3292 case EXPR_SMALLER: res = arg1 < arg2; break;
3293 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3294 default: res = 0; break;
3295 }
3296
Bram Moolenaar4c137212021-04-19 16:48:48 +02003297 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 if (iptr->isn_type == ISN_COMPARENR)
3299 {
3300 tv1->v_type = VAR_BOOL;
3301 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3302 }
3303 else
3304 tv1->vval.v_number = res;
3305 }
3306 break;
3307
3308 // Computation with two float arguments
3309 case ISN_OPFLOAT:
3310 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003311#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 {
3313 typval_T *tv1 = STACK_TV_BOT(-2);
3314 typval_T *tv2 = STACK_TV_BOT(-1);
3315 float_T arg1 = tv1->vval.v_float;
3316 float_T arg2 = tv2->vval.v_float;
3317 float_T res = 0;
3318 int cmp = FALSE;
3319
3320 switch (iptr->isn_arg.op.op_type)
3321 {
3322 case EXPR_MULT: res = arg1 * arg2; break;
3323 case EXPR_DIV: res = arg1 / arg2; break;
3324 case EXPR_SUB: res = arg1 - arg2; break;
3325 case EXPR_ADD: res = arg1 + arg2; break;
3326
3327 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3328 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3329 case EXPR_GREATER: cmp = arg1 > arg2; break;
3330 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3331 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3332 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3333 default: cmp = 0; break;
3334 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003335 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 if (iptr->isn_type == ISN_COMPAREFLOAT)
3337 {
3338 tv1->v_type = VAR_BOOL;
3339 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3340 }
3341 else
3342 tv1->vval.v_float = res;
3343 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003344#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 break;
3346
3347 case ISN_COMPARELIST:
3348 {
3349 typval_T *tv1 = STACK_TV_BOT(-2);
3350 typval_T *tv2 = STACK_TV_BOT(-1);
3351 list_T *arg1 = tv1->vval.v_list;
3352 list_T *arg2 = tv2->vval.v_list;
3353 int cmp = FALSE;
3354 int ic = iptr->isn_arg.op.op_ic;
3355
3356 switch (iptr->isn_arg.op.op_type)
3357 {
3358 case EXPR_EQUAL: cmp =
3359 list_equal(arg1, arg2, ic, FALSE); break;
3360 case EXPR_NEQUAL: cmp =
3361 !list_equal(arg1, arg2, ic, FALSE); break;
3362 case EXPR_IS: cmp = arg1 == arg2; break;
3363 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3364 default: cmp = 0; break;
3365 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003366 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 clear_tv(tv1);
3368 clear_tv(tv2);
3369 tv1->v_type = VAR_BOOL;
3370 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3371 }
3372 break;
3373
3374 case ISN_COMPAREBLOB:
3375 {
3376 typval_T *tv1 = STACK_TV_BOT(-2);
3377 typval_T *tv2 = STACK_TV_BOT(-1);
3378 blob_T *arg1 = tv1->vval.v_blob;
3379 blob_T *arg2 = tv2->vval.v_blob;
3380 int cmp = FALSE;
3381
3382 switch (iptr->isn_arg.op.op_type)
3383 {
3384 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3385 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3386 case EXPR_IS: cmp = arg1 == arg2; break;
3387 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3388 default: cmp = 0; break;
3389 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003390 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 clear_tv(tv1);
3392 clear_tv(tv2);
3393 tv1->v_type = VAR_BOOL;
3394 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3395 }
3396 break;
3397
3398 // TODO: handle separately
3399 case ISN_COMPARESTRING:
3400 case ISN_COMPAREDICT:
3401 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 case ISN_COMPAREANY:
3403 {
3404 typval_T *tv1 = STACK_TV_BOT(-2);
3405 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003406 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 int ic = iptr->isn_arg.op.op_ic;
3408
Bram Moolenaareb26f432020-09-14 16:50:05 +02003409 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003410 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003412 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 }
3414 break;
3415
3416 case ISN_ADDLIST:
3417 case ISN_ADDBLOB:
3418 {
3419 typval_T *tv1 = STACK_TV_BOT(-2);
3420 typval_T *tv2 = STACK_TV_BOT(-1);
3421
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003422 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 if (iptr->isn_type == ISN_ADDLIST)
3424 eval_addlist(tv1, tv2);
3425 else
3426 eval_addblob(tv1, tv2);
3427 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003428 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 }
3430 break;
3431
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003432 case ISN_LISTAPPEND:
3433 {
3434 typval_T *tv1 = STACK_TV_BOT(-2);
3435 typval_T *tv2 = STACK_TV_BOT(-1);
3436 list_T *l = tv1->vval.v_list;
3437
3438 // add an item to a list
3439 if (l == NULL)
3440 {
3441 SOURCING_LNUM = iptr->isn_lnum;
3442 emsg(_(e_cannot_add_to_null_list));
3443 goto on_error;
3444 }
3445 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003446 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003447 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003448 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003449 }
3450 break;
3451
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003452 case ISN_BLOBAPPEND:
3453 {
3454 typval_T *tv1 = STACK_TV_BOT(-2);
3455 typval_T *tv2 = STACK_TV_BOT(-1);
3456 blob_T *b = tv1->vval.v_blob;
3457 int error = FALSE;
3458 varnumber_T n;
3459
3460 // add a number to a blob
3461 if (b == NULL)
3462 {
3463 SOURCING_LNUM = iptr->isn_lnum;
3464 emsg(_(e_cannot_add_to_null_blob));
3465 goto on_error;
3466 }
3467 n = tv_get_number_chk(tv2, &error);
3468 if (error)
3469 goto on_error;
3470 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003471 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003472 }
3473 break;
3474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 // Computation with two arguments of unknown type
3476 case ISN_OPANY:
3477 {
3478 typval_T *tv1 = STACK_TV_BOT(-2);
3479 typval_T *tv2 = STACK_TV_BOT(-1);
3480 varnumber_T n1, n2;
3481#ifdef FEAT_FLOAT
3482 float_T f1 = 0, f2 = 0;
3483#endif
3484 int error = FALSE;
3485
3486 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3487 {
3488 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3489 {
3490 eval_addlist(tv1, tv2);
3491 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003492 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 break;
3494 }
3495 else if (tv1->v_type == VAR_BLOB
3496 && tv2->v_type == VAR_BLOB)
3497 {
3498 eval_addblob(tv1, tv2);
3499 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003500 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 break;
3502 }
3503 }
3504#ifdef FEAT_FLOAT
3505 if (tv1->v_type == VAR_FLOAT)
3506 {
3507 f1 = tv1->vval.v_float;
3508 n1 = 0;
3509 }
3510 else
3511#endif
3512 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003513 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 n1 = tv_get_number_chk(tv1, &error);
3515 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003516 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517#ifdef FEAT_FLOAT
3518 if (tv2->v_type == VAR_FLOAT)
3519 f1 = n1;
3520#endif
3521 }
3522#ifdef FEAT_FLOAT
3523 if (tv2->v_type == VAR_FLOAT)
3524 {
3525 f2 = tv2->vval.v_float;
3526 n2 = 0;
3527 }
3528 else
3529#endif
3530 {
3531 n2 = tv_get_number_chk(tv2, &error);
3532 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003533 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534#ifdef FEAT_FLOAT
3535 if (tv1->v_type == VAR_FLOAT)
3536 f2 = n2;
3537#endif
3538 }
3539#ifdef FEAT_FLOAT
3540 // if there is a float on either side the result is a float
3541 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3542 {
3543 switch (iptr->isn_arg.op.op_type)
3544 {
3545 case EXPR_MULT: f1 = f1 * f2; break;
3546 case EXPR_DIV: f1 = f1 / f2; break;
3547 case EXPR_SUB: f1 = f1 - f2; break;
3548 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003549 default: SOURCING_LNUM = iptr->isn_lnum;
3550 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003551 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 }
3553 clear_tv(tv1);
3554 clear_tv(tv2);
3555 tv1->v_type = VAR_FLOAT;
3556 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003557 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 }
3559 else
3560#endif
3561 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003562 int failed = FALSE;
3563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 switch (iptr->isn_arg.op.op_type)
3565 {
3566 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003567 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3568 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003569 goto on_error;
3570 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 case EXPR_SUB: n1 = n1 - n2; break;
3572 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003573 default: n1 = num_modulus(n1, n2, &failed);
3574 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003575 goto on_error;
3576 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 }
3578 clear_tv(tv1);
3579 clear_tv(tv2);
3580 tv1->v_type = VAR_NUMBER;
3581 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003582 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 }
3584 }
3585 break;
3586
3587 case ISN_CONCAT:
3588 {
3589 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3590 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3591 char_u *res;
3592
3593 res = concat_str(str1, str2);
3594 clear_tv(STACK_TV_BOT(-2));
3595 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003596 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 STACK_TV_BOT(-1)->vval.v_string = res;
3598 }
3599 break;
3600
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003601 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003602 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003603 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003604 int is_slice = iptr->isn_type == ISN_STRSLICE;
3605 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003606 char_u *res;
3607
3608 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003609 // string slice: string is at stack-3, first index at
3610 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003611 if (is_slice)
3612 {
3613 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003614 n1 = tv->vval.v_number;
3615 }
3616
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003617 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003618 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003619
Bram Moolenaar4c137212021-04-19 16:48:48 +02003620 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003621 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003622 if (is_slice)
3623 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003624 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003625 else
3626 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003627 // single character (including composing characters).
3628 // If the index is too big or negative the result is
3629 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003630 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003631 vim_free(tv->vval.v_string);
3632 tv->vval.v_string = res;
3633 }
3634 break;
3635
3636 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003637 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003638 case ISN_BLOBINDEX:
3639 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003641 int is_slice = iptr->isn_type == ISN_LISTSLICE
3642 || iptr->isn_type == ISN_BLOBSLICE;
3643 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3644 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003645 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003646 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647
3648 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003649 // list slice: list is at stack-3, indexes at stack-2 and
3650 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003651 // Same for blob.
3652 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653
3654 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003655 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003657
3658 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 {
Bram Moolenaared591872020-08-15 22:14:53 +02003660 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003661 n1 = tv->vval.v_number;
3662 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
Bram Moolenaared591872020-08-15 22:14:53 +02003664
Bram Moolenaar4c137212021-04-19 16:48:48 +02003665 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003666 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003667 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003668 if (is_blob)
3669 {
3670 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3671 n1, n2, FALSE, tv) == FAIL)
3672 goto on_error;
3673 }
3674 else
3675 {
3676 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3677 n1, n2, FALSE, tv, TRUE) == FAIL)
3678 goto on_error;
3679 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 }
3681 break;
3682
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003683 case ISN_ANYINDEX:
3684 case ISN_ANYSLICE:
3685 {
3686 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3687 typval_T *var1, *var2;
3688 int res;
3689
3690 // index: composite is at stack-2, index at stack-1
3691 // slice: composite is at stack-3, indexes at stack-2 and
3692 // stack-1
3693 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003694 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003695 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3696 goto on_error;
3697 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3698 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003699 res = eval_index_inner(tv, is_slice, var1, var2,
3700 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003701 clear_tv(var1);
3702 if (is_slice)
3703 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003704 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003705 if (res == FAIL)
3706 goto on_error;
3707 }
3708 break;
3709
Bram Moolenaar9af78762020-06-16 11:34:42 +02003710 case ISN_SLICE:
3711 {
3712 list_T *list;
3713 int count = iptr->isn_arg.number;
3714
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003715 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003716 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003717 list = tv->vval.v_list;
3718
3719 // no error for short list, expect it to be checked earlier
3720 if (list != NULL && list->lv_len >= count)
3721 {
3722 list_T *newlist = list_slice(list,
3723 count, list->lv_len - 1);
3724
3725 if (newlist != NULL)
3726 {
3727 list_unref(list);
3728 tv->vval.v_list = newlist;
3729 ++newlist->lv_refcount;
3730 }
3731 }
3732 }
3733 break;
3734
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003735 case ISN_GETITEM:
3736 {
3737 listitem_T *li;
3738 int index = iptr->isn_arg.number;
3739
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003740 // Get list item: list is at stack-1, push item.
3741 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003742 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003743 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003744
Bram Moolenaar4c137212021-04-19 16:48:48 +02003745 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003746 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003747 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003748 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003749
3750 // Useful when used in unpack assignment. Reset at
3751 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003752 ectx->ec_where.wt_index = index + 1;
3753 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003754 }
3755 break;
3756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 case ISN_MEMBER:
3758 {
3759 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003760 char_u *key;
3761 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003762 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003763
3764 // dict member: dict is at stack-2, key at stack-1
3765 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003766 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003767 dict = tv->vval.v_dict;
3768
3769 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003770 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003771 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003772 if (key == NULL)
3773 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003774
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003775 if ((di = dict_find(dict, key, -1)) == NULL)
3776 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003777 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003778 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003779
3780 // If :silent! is used we will continue, make sure the
3781 // stack contents makes sense.
3782 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003783 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003784 tv = STACK_TV_BOT(-1);
3785 clear_tv(tv);
3786 tv->v_type = VAR_NUMBER;
3787 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003788 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003789 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003790 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003791 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003792 // Clear the dict only after getting the item, to avoid
3793 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003794 tv = STACK_TV_BOT(-1);
3795 temp_tv = *tv;
3796 copy_tv(&di->di_tv, tv);
3797 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003798 }
3799 break;
3800
3801 // dict member with string key
3802 case ISN_STRINGMEMBER:
3803 {
3804 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003806 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 tv = STACK_TV_BOT(-1);
3809 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3810 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003811 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003813 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 }
3815 dict = tv->vval.v_dict;
3816
3817 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3818 == NULL)
3819 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003820 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003822 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003824 // Clear the dict after getting the item, to avoid that it
3825 // make the item invalid.
3826 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003828 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 }
3830 break;
3831
3832 case ISN_NEGATENR:
3833 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003834 if (tv->v_type != VAR_NUMBER
3835#ifdef FEAT_FLOAT
3836 && tv->v_type != VAR_FLOAT
3837#endif
3838 )
3839 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003840 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003841 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003842 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003843 }
3844#ifdef FEAT_FLOAT
3845 if (tv->v_type == VAR_FLOAT)
3846 tv->vval.v_float = -tv->vval.v_float;
3847 else
3848#endif
3849 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 break;
3851
3852 case ISN_CHECKNR:
3853 {
3854 int error = FALSE;
3855
3856 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003857 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003859 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 (void)tv_get_number_chk(tv, &error);
3861 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003862 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 }
3864 break;
3865
3866 case ISN_CHECKTYPE:
3867 {
3868 checktype_T *ct = &iptr->isn_arg.type;
3869
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003870 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003871 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003872 if (!ectx->ec_where.wt_variable)
3873 ectx->ec_where.wt_index = ct->ct_arg_idx;
3874 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3875 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003876 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003877 if (!ectx->ec_where.wt_variable)
3878 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003879
3880 // number 0 is FALSE, number 1 is TRUE
3881 if (tv->v_type == VAR_NUMBER
3882 && ct->ct_type->tt_type == VAR_BOOL
3883 && (tv->vval.v_number == 0
3884 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003886 tv->v_type = VAR_BOOL;
3887 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003888 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 }
3890 }
3891 break;
3892
Bram Moolenaar9af78762020-06-16 11:34:42 +02003893 case ISN_CHECKLEN:
3894 {
3895 int min_len = iptr->isn_arg.checklen.cl_min_len;
3896 list_T *list = NULL;
3897
3898 tv = STACK_TV_BOT(-1);
3899 if (tv->v_type == VAR_LIST)
3900 list = tv->vval.v_list;
3901 if (list == NULL || list->lv_len < min_len
3902 || (list->lv_len > min_len
3903 && !iptr->isn_arg.checklen.cl_more_OK))
3904 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003905 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003906 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003907 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003908 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003909 }
3910 }
3911 break;
3912
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003913 case ISN_SETTYPE:
3914 {
3915 checktype_T *ct = &iptr->isn_arg.type;
3916
3917 tv = STACK_TV_BOT(-1);
3918 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3919 {
3920 free_type(tv->vval.v_dict->dv_type);
3921 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3922 }
3923 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3924 {
3925 free_type(tv->vval.v_list->lv_type);
3926 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3927 }
3928 }
3929 break;
3930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003932 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 {
3934 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003935 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003937 if (iptr->isn_type == ISN_2BOOL)
3938 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003939 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003940 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003941 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003942 n = !n;
3943 }
3944 else
3945 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003946 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003947 SOURCING_LNUM = iptr->isn_lnum;
3948 n = tv_get_bool_chk(tv, &error);
3949 if (error)
3950 goto on_error;
3951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 clear_tv(tv);
3953 tv->v_type = VAR_BOOL;
3954 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3955 }
3956 break;
3957
3958 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003959 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003960 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003961 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3962 iptr->isn_type == ISN_2STRING_ANY,
3963 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003964 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 break;
3966
Bram Moolenaar08597872020-12-10 19:43:40 +01003967 case ISN_RANGE:
3968 {
3969 exarg_T ea;
3970 char *errormsg;
3971
Bram Moolenaarece0b872021-01-08 20:40:45 +01003972 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003973 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003974 ea.addr_type = ADDR_LINES;
3975 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003976 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003977 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003978 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003979
Bram Moolenaar4c137212021-04-19 16:48:48 +02003980 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003981 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003982 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003983 tv = STACK_TV_BOT(-1);
3984 tv->v_type = VAR_NUMBER;
3985 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003986 if (ea.addr_count == 0)
3987 tv->vval.v_number = curwin->w_cursor.lnum;
3988 else
3989 tv->vval.v_number = ea.line2;
3990 }
3991 break;
3992
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003993 case ISN_PUT:
3994 {
3995 int regname = iptr->isn_arg.put.put_regname;
3996 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3997 char_u *expr = NULL;
3998 int dir = FORWARD;
3999
Bram Moolenaar08597872020-12-10 19:43:40 +01004000 if (lnum < -2)
4001 {
4002 // line number was put on the stack by ISN_RANGE
4003 tv = STACK_TV_BOT(-1);
4004 curwin->w_cursor.lnum = tv->vval.v_number;
4005 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4006 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004007 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004008 }
4009 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004010 // :put! above cursor
4011 dir = BACKWARD;
4012 else if (lnum >= 0)
4013 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004014
4015 if (regname == '=')
4016 {
4017 tv = STACK_TV_BOT(-1);
4018 if (tv->v_type == VAR_STRING)
4019 expr = tv->vval.v_string;
4020 else
4021 {
4022 expr = typval2string(tv, TRUE); // allocates value
4023 clear_tv(tv);
4024 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004025 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004026 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004027 check_cursor();
4028 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4029 vim_free(expr);
4030 }
4031 break;
4032
Bram Moolenaar02194d22020-10-24 23:08:38 +02004033 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004034 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4035 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4036 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4037 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004038 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4039 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004040 break;
4041
Bram Moolenaar02194d22020-10-24 23:08:38 +02004042 case ISN_CMDMOD_REV:
4043 // filter regprog is owned by the instruction, don't free it
4044 cmdmod.cmod_filter_regmatch.regprog = NULL;
4045 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004046 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4047 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004048 break;
4049
Bram Moolenaar792f7862020-11-23 08:31:18 +01004050 case ISN_UNPACK:
4051 {
4052 int count = iptr->isn_arg.unpack.unp_count;
4053 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4054 list_T *l;
4055 listitem_T *li;
4056 int i;
4057
4058 // Check there is a valid list to unpack.
4059 tv = STACK_TV_BOT(-1);
4060 if (tv->v_type != VAR_LIST)
4061 {
4062 SOURCING_LNUM = iptr->isn_lnum;
4063 emsg(_(e_for_argument_must_be_sequence_of_lists));
4064 goto on_error;
4065 }
4066 l = tv->vval.v_list;
4067 if (l == NULL
4068 || l->lv_len < (semicolon ? count - 1 : count))
4069 {
4070 SOURCING_LNUM = iptr->isn_lnum;
4071 emsg(_(e_list_value_does_not_have_enough_items));
4072 goto on_error;
4073 }
4074 else if (!semicolon && l->lv_len > count)
4075 {
4076 SOURCING_LNUM = iptr->isn_lnum;
4077 emsg(_(e_list_value_has_more_items_than_targets));
4078 goto on_error;
4079 }
4080
4081 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004082 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004083 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004084 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004085
4086 // Variable after semicolon gets a list with the remaining
4087 // items.
4088 if (semicolon)
4089 {
4090 list_T *rem_list =
4091 list_alloc_with_items(l->lv_len - count + 1);
4092
4093 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004094 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004095 tv = STACK_TV_BOT(-count);
4096 tv->vval.v_list = rem_list;
4097 ++rem_list->lv_refcount;
4098 tv->v_lock = 0;
4099 li = l->lv_first;
4100 for (i = 0; i < count - 1; ++i)
4101 li = li->li_next;
4102 for (i = 0; li != NULL; ++i)
4103 {
4104 list_set_item(rem_list, i, &li->li_tv);
4105 li = li->li_next;
4106 }
4107 --count;
4108 }
4109
4110 // Produce the values in reverse order, first item last.
4111 li = l->lv_first;
4112 for (i = 0; i < count; ++i)
4113 {
4114 tv = STACK_TV_BOT(-i - 1);
4115 copy_tv(&li->li_tv, tv);
4116 li = li->li_next;
4117 }
4118
4119 list_unref(l);
4120 }
4121 break;
4122
Bram Moolenaarb2049902021-01-24 12:53:53 +01004123 case ISN_PROF_START:
4124 case ISN_PROF_END:
4125 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004126#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004127 funccall_T cookie;
4128 ufunc_T *cur_ufunc =
4129 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004130 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004131
4132 cookie.func = cur_ufunc;
4133 if (iptr->isn_type == ISN_PROF_START)
4134 {
4135 func_line_start(&cookie, iptr->isn_lnum);
4136 // if we get here the instruction is executed
4137 func_line_exec(&cookie);
4138 }
4139 else
4140 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004141#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004142 }
4143 break;
4144
Bram Moolenaare99d4222021-06-13 14:01:26 +02004145 case ISN_DEBUG:
4146 if (ex_nesting_level <= debug_break_level)
4147 {
4148 char_u *line;
4149 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
4150 + ectx->ec_dfunc_idx)->df_ufunc;
4151
4152 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004153 debug_context = ectx;
4154 debug_arg_count = iptr->isn_arg.number;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004155 line = ((char_u **)ufunc->uf_lines.ga_data)[
4156 iptr->isn_lnum - 1];
4157 if (line == NULL)
4158 line = (char_u *)"[empty]";
4159 do_debug(line);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004160 debug_context = NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004161 }
4162 break;
4163
Bram Moolenaar389df252020-07-09 21:20:47 +02004164 case ISN_SHUFFLE:
4165 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004166 typval_T tmp_tv;
4167 int item = iptr->isn_arg.shuffle.shfl_item;
4168 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004169
4170 tmp_tv = *STACK_TV_BOT(-item);
4171 for ( ; up > 0 && item > 1; --up)
4172 {
4173 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4174 --item;
4175 }
4176 *STACK_TV_BOT(-item) = tmp_tv;
4177 }
4178 break;
4179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004181 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004183 ectx->ec_where.wt_index = 0;
4184 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 break;
4186 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004187 continue;
4188
Bram Moolenaard032f342020-07-18 18:13:02 +02004189func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004190 // Restore previous function. If the frame pointer is where we started
4191 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004192 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004193 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004194
Bram Moolenaar4c137212021-04-19 16:48:48 +02004195 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004196 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004197 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004198 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004199
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004200on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004201 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004202 // If "emsg_silent" is set then ignore the error, unless it was set
4203 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004204 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004205 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004206 {
4207 // If a sequence of instructions causes an error while ":silent!"
4208 // was used, restore the stack length and jump ahead to restoring
4209 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004210 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004211 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004212 while (ectx->ec_stack.ga_len
4213 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004214 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004215 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004216 clear_tv(STACK_TV_BOT(0));
4217 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004218 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4219 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004220 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004221 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004222 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004223on_fatal_error:
4224 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004225 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004226 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004227 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 }
4229
4230done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004231 ret = OK;
4232theend:
4233 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4234 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004235}
4236
4237/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004238 * Execute the instructions from a VAR_INSTR typeval and put the result in
4239 * "rettv".
4240 * Return OK or FAIL.
4241 */
4242 int
4243exe_typval_instr(typval_T *tv, typval_T *rettv)
4244{
4245 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4246 isn_T *save_instr = ectx->ec_instr;
4247 int save_iidx = ectx->ec_iidx;
4248 int res;
4249
4250 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4251 res = exec_instructions(ectx);
4252 if (res == OK)
4253 {
4254 *rettv = *STACK_TV_BOT(-1);
4255 --ectx->ec_stack.ga_len;
4256 }
4257
4258 ectx->ec_instr = save_instr;
4259 ectx->ec_iidx = save_iidx;
4260
4261 return res;
4262}
4263
4264/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004265 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4266 * "substitute_instr".
4267 */
4268 char_u *
4269exe_substitute_instr(void)
4270{
4271 ectx_T *ectx = substitute_instr->subs_ectx;
4272 isn_T *save_instr = ectx->ec_instr;
4273 int save_iidx = ectx->ec_iidx;
4274 char_u *res;
4275
4276 ectx->ec_instr = substitute_instr->subs_instr;
4277 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004278 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004279 typval_T *tv = STACK_TV_BOT(-1);
4280
Bram Moolenaar27523602021-06-05 21:36:19 +02004281 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004282 --ectx->ec_stack.ga_len;
4283 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004284 }
4285 else
4286 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004287 substitute_instr->subs_status = FAIL;
4288 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290
Bram Moolenaar4c137212021-04-19 16:48:48 +02004291 ectx->ec_instr = save_instr;
4292 ectx->ec_iidx = save_iidx;
4293
4294 return res;
4295}
4296
4297/*
4298 * Call a "def" function from old Vim script.
4299 * Return OK or FAIL.
4300 */
4301 int
4302call_def_function(
4303 ufunc_T *ufunc,
4304 int argc_arg, // nr of arguments
4305 typval_T *argv, // arguments
4306 partial_T *partial, // optional partial for context
4307 typval_T *rettv) // return value
4308{
4309 ectx_T ectx; // execution context
4310 int argc = argc_arg;
4311 typval_T *tv;
4312 int idx;
4313 int ret = FAIL;
4314 int defcount = ufunc->uf_args.ga_len - argc;
4315 sctx_T save_current_sctx = current_sctx;
4316 int did_emsg_before = did_emsg_cumul + did_emsg;
4317 int save_suppress_errthrow = suppress_errthrow;
4318 msglist_T **saved_msg_list = NULL;
4319 msglist_T *private_msg_list = NULL;
4320 int save_emsg_silent_def = emsg_silent_def;
4321 int save_did_emsg_def = did_emsg_def;
4322 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004323 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004324
4325// Get pointer to item in the stack.
4326#undef STACK_TV
4327#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4328
4329// Get pointer to item at the bottom of the stack, -1 is the bottom.
4330#undef STACK_TV_BOT
4331#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4332
4333// Get pointer to a local variable on the stack. Negative for arguments.
4334#undef STACK_TV_VAR
4335#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4336
4337 if (ufunc->uf_def_status == UF_NOT_COMPILED
4338 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004339 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4340 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004341 == FAIL))
4342 {
4343 if (did_emsg_cumul + did_emsg == did_emsg_before)
4344 semsg(_(e_function_is_not_compiled_str),
4345 printable_func_name(ufunc));
4346 return FAIL;
4347 }
4348
4349 {
4350 // Check the function was really compiled.
4351 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4352 + ufunc->uf_dfunc_idx;
4353 if (INSTRUCTIONS(dfunc) == NULL)
4354 {
4355 iemsg("using call_def_function() on not compiled function");
4356 return FAIL;
4357 }
4358 }
4359
4360 // If depth of calling is getting too high, don't execute the function.
4361 orig_funcdepth = funcdepth_get();
4362 if (funcdepth_increment() == FAIL)
4363 return FAIL;
4364
4365 CLEAR_FIELD(ectx);
4366 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4367 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4368 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4369 {
4370 funcdepth_decrement();
4371 return FAIL;
4372 }
4373 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4374 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4375 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004376 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004377
4378 idx = argc - ufunc->uf_args.ga_len;
4379 if (idx > 0 && ufunc->uf_va_name == NULL)
4380 {
4381 if (idx == 1)
4382 emsg(_(e_one_argument_too_many));
4383 else
4384 semsg(_(e_nr_arguments_too_many), idx);
4385 goto failed_early;
4386 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004387 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4388 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004389 {
4390 if (idx == -1)
4391 emsg(_(e_one_argument_too_few));
4392 else
4393 semsg(_(e_nr_arguments_too_few), -idx);
4394 goto failed_early;
4395 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004396
4397 // Put arguments on the stack, but no more than what the function expects.
4398 // A lambda can be called with more arguments than it uses.
4399 for (idx = 0; idx < argc
4400 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4401 ++idx)
4402 {
4403 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4404 && argv[idx].v_type == VAR_SPECIAL
4405 && argv[idx].vval.v_number == VVAL_NONE)
4406 {
4407 // Use the default value.
4408 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4409 }
4410 else
4411 {
4412 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4413 && check_typval_arg_type(
4414 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4415 goto failed_early;
4416 copy_tv(&argv[idx], STACK_TV_BOT(0));
4417 }
4418 ++ectx.ec_stack.ga_len;
4419 }
4420
4421 // Turn varargs into a list. Empty list if no args.
4422 if (ufunc->uf_va_name != NULL)
4423 {
4424 int vararg_count = argc - ufunc->uf_args.ga_len;
4425
4426 if (vararg_count < 0)
4427 vararg_count = 0;
4428 else
4429 argc -= vararg_count;
4430 if (exe_newlist(vararg_count, &ectx) == FAIL)
4431 goto failed_early;
4432
4433 // Check the type of the list items.
4434 tv = STACK_TV_BOT(-1);
4435 if (ufunc->uf_va_type != NULL
4436 && ufunc->uf_va_type != &t_list_any
4437 && ufunc->uf_va_type->tt_member != &t_any
4438 && tv->vval.v_list != NULL)
4439 {
4440 type_T *expected = ufunc->uf_va_type->tt_member;
4441 listitem_T *li = tv->vval.v_list->lv_first;
4442
4443 for (idx = 0; idx < vararg_count; ++idx)
4444 {
4445 if (check_typval_arg_type(expected, &li->li_tv,
4446 argc + idx + 1) == FAIL)
4447 goto failed_early;
4448 li = li->li_next;
4449 }
4450 }
4451
4452 if (defcount > 0)
4453 // Move varargs list to below missing default arguments.
4454 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4455 --ectx.ec_stack.ga_len;
4456 }
4457
4458 // Make space for omitted arguments, will store default value below.
4459 // Any varargs list goes after them.
4460 if (defcount > 0)
4461 for (idx = 0; idx < defcount; ++idx)
4462 {
4463 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4464 ++ectx.ec_stack.ga_len;
4465 }
4466 if (ufunc->uf_va_name != NULL)
4467 ++ectx.ec_stack.ga_len;
4468
4469 // Frame pointer points to just after arguments.
4470 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4471 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4472
4473 {
4474 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4475 + ufunc->uf_dfunc_idx;
4476 ufunc_T *base_ufunc = dfunc->df_ufunc;
4477
4478 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4479 // by copy_func().
4480 if (partial != NULL || base_ufunc->uf_partial != NULL)
4481 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004482 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4483 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004484 goto failed_early;
4485 if (partial != NULL)
4486 {
4487 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4488 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004489 if (current_ectx->ec_outer_ref != NULL
4490 && current_ectx->ec_outer_ref->or_outer != NULL)
4491 ectx.ec_outer_ref->or_outer =
4492 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004493 }
4494 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004495 {
4496 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4497 ++partial->pt_refcount;
4498 ectx.ec_outer_ref->or_partial = partial;
4499 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004500 }
4501 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004502 {
4503 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4504 ++base_ufunc->uf_partial->pt_refcount;
4505 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4506 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004507 }
4508 }
4509
4510 // dummy frame entries
4511 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4512 {
4513 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4514 ++ectx.ec_stack.ga_len;
4515 }
4516
4517 {
4518 // Reserve space for local variables and any closure reference count.
4519 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4520 + ufunc->uf_dfunc_idx;
4521
4522 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4523 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4524 ectx.ec_stack.ga_len += dfunc->df_varcount;
4525 if (dfunc->df_has_closure)
4526 {
4527 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4528 STACK_TV_VAR(idx)->vval.v_number = 0;
4529 ++ectx.ec_stack.ga_len;
4530 }
4531
4532 ectx.ec_instr = INSTRUCTIONS(dfunc);
4533 }
4534
4535 // Following errors are in the function, not the caller.
4536 // Commands behave like vim9script.
4537 estack_push_ufunc(ufunc, 1);
4538 current_sctx = ufunc->uf_script_ctx;
4539 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4540
4541 // Use a specific location for storing error messages to be converted to an
4542 // exception.
4543 saved_msg_list = msg_list;
4544 msg_list = &private_msg_list;
4545
4546 // Do turn errors into exceptions.
4547 suppress_errthrow = FALSE;
4548
4549 // Do not delete the function while executing it.
4550 ++ufunc->uf_calls;
4551
4552 // When ":silent!" was used before calling then we still abort the
4553 // function. If ":silent!" is used in the function then we don't.
4554 emsg_silent_def = emsg_silent;
4555 did_emsg_def = 0;
4556
4557 ectx.ec_where.wt_index = 0;
4558 ectx.ec_where.wt_variable = FALSE;
4559
4560 // Execute the instructions until done.
4561 ret = exec_instructions(&ectx);
4562 if (ret == OK)
4563 {
4564 // function finished, get result from the stack.
4565 if (ufunc->uf_ret_type == &t_void)
4566 {
4567 rettv->v_type = VAR_VOID;
4568 }
4569 else
4570 {
4571 tv = STACK_TV_BOT(-1);
4572 *rettv = *tv;
4573 tv->v_type = VAR_UNKNOWN;
4574 }
4575 }
4576
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004577 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004578 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4579 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004580
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004581 // Deal with any remaining closures, they may be in use somewhere.
4582 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004583 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004584 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004585 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4586 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004587
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004588 estack_pop();
4589 current_sctx = save_current_sctx;
4590
Bram Moolenaarc970e422021-03-17 15:03:04 +01004591 // TODO: when is it safe to delete the function if it is no longer used?
4592 --ufunc->uf_calls;
4593
Bram Moolenaar352134b2020-10-17 22:04:08 +02004594 if (*msg_list != NULL && saved_msg_list != NULL)
4595 {
4596 msglist_T **plist = saved_msg_list;
4597
4598 // Append entries from the current msg_list (uncaught exceptions) to
4599 // the saved msg_list.
4600 while (*plist != NULL)
4601 plist = &(*plist)->next;
4602
4603 *plist = *msg_list;
4604 }
4605 msg_list = saved_msg_list;
4606
Bram Moolenaar4c137212021-04-19 16:48:48 +02004607 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004608 {
4609 cmdmod.cmod_filter_regmatch.regprog = NULL;
4610 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004611 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004612 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004613 emsg_silent_def = save_emsg_silent_def;
4614 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004615
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004616failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004617 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4619 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004620 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004623 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004624 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004625 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004626 if (ectx.ec_outer_ref->or_outer_allocated)
4627 vim_free(ectx.ec_outer_ref->or_outer);
4628 partial_unref(ectx.ec_outer_ref->or_partial);
4629 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004630 }
4631
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004632 // Not sure if this is necessary.
4633 suppress_errthrow = save_suppress_errthrow;
4634
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004635 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004636 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004637 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004638 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 return ret;
4640}
4641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004643 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4644 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4645 * "pfx" is prefixed to every line.
4646 */
4647 static void
4648list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4649{
4650 int line_idx = 0;
4651 int prev_current = 0;
4652 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004653 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004654
4655 for (current = 0; current < instr_count; ++current)
4656 {
4657 isn_T *iptr = &instr[current];
4658 char *line;
4659
4660 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004661 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004662 while (line_idx < iptr->isn_lnum
4663 && line_idx < ufunc->uf_lines.ga_len)
4664 {
4665 if (current > prev_current)
4666 {
4667 msg_puts("\n\n");
4668 prev_current = current;
4669 }
4670 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4671 if (line != NULL)
4672 msg(line);
4673 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004674 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4675 {
4676 int first_def_arg = ufunc->uf_args.ga_len
4677 - ufunc->uf_def_args.ga_len;
4678
4679 if (def_arg_idx > 0)
4680 msg_puts("\n\n");
4681 msg_start();
4682 msg_puts(" ");
4683 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4684 first_def_arg + def_arg_idx]);
4685 msg_puts(" = ");
4686 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4687 msg_clr_eos();
4688 msg_end();
4689 }
4690 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004691
4692 switch (iptr->isn_type)
4693 {
4694 case ISN_EXEC:
4695 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4696 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004697 case ISN_EXEC_SPLIT:
4698 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4699 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004700 case ISN_LEGACY_EVAL:
4701 smsg("%s%4d EVAL legacy %s", pfx, current,
4702 iptr->isn_arg.string);
4703 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004704 case ISN_REDIRSTART:
4705 smsg("%s%4d REDIR", pfx, current);
4706 break;
4707 case ISN_REDIREND:
4708 smsg("%s%4d REDIR END%s", pfx, current,
4709 iptr->isn_arg.number ? " append" : "");
4710 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004711 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004712#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004713 smsg("%s%4d CEXPR pre %s", pfx, current,
4714 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004715#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004716 break;
4717 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004718#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004719 {
4720 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4721
4722 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4723 cexpr_get_auname(cer->cer_cmdidx),
4724 cer->cer_forceit ? "!" : "",
4725 cer->cer_cmdline);
4726 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004727#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004728 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004729 case ISN_INSTR:
4730 {
4731 smsg("%s%4d INSTR", pfx, current);
4732 list_instructions(" ", iptr->isn_arg.instr,
4733 INT_MAX, NULL);
4734 msg(" -------------");
4735 }
4736 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004737 case ISN_SUBSTITUTE:
4738 {
4739 subs_T *subs = &iptr->isn_arg.subs;
4740
4741 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4742 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4743 msg(" -------------");
4744 }
4745 break;
4746 case ISN_EXECCONCAT:
4747 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4748 (varnumber_T)iptr->isn_arg.number);
4749 break;
4750 case ISN_ECHO:
4751 {
4752 echo_T *echo = &iptr->isn_arg.echo;
4753
4754 smsg("%s%4d %s %d", pfx, current,
4755 echo->echo_with_white ? "ECHO" : "ECHON",
4756 echo->echo_count);
4757 }
4758 break;
4759 case ISN_EXECUTE:
4760 smsg("%s%4d EXECUTE %lld", pfx, current,
4761 (varnumber_T)(iptr->isn_arg.number));
4762 break;
4763 case ISN_ECHOMSG:
4764 smsg("%s%4d ECHOMSG %lld", pfx, current,
4765 (varnumber_T)(iptr->isn_arg.number));
4766 break;
4767 case ISN_ECHOERR:
4768 smsg("%s%4d ECHOERR %lld", pfx, current,
4769 (varnumber_T)(iptr->isn_arg.number));
4770 break;
4771 case ISN_LOAD:
4772 {
4773 if (iptr->isn_arg.number < 0)
4774 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4775 (varnumber_T)(iptr->isn_arg.number
4776 + STACK_FRAME_SIZE));
4777 else
4778 smsg("%s%4d LOAD $%lld", pfx, current,
4779 (varnumber_T)(iptr->isn_arg.number));
4780 }
4781 break;
4782 case ISN_LOADOUTER:
4783 {
4784 if (iptr->isn_arg.number < 0)
4785 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4786 iptr->isn_arg.outer.outer_depth,
4787 iptr->isn_arg.outer.outer_idx
4788 + STACK_FRAME_SIZE);
4789 else
4790 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4791 iptr->isn_arg.outer.outer_depth,
4792 iptr->isn_arg.outer.outer_idx);
4793 }
4794 break;
4795 case ISN_LOADV:
4796 smsg("%s%4d LOADV v:%s", pfx, current,
4797 get_vim_var_name(iptr->isn_arg.number));
4798 break;
4799 case ISN_LOADSCRIPT:
4800 {
4801 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4802 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4803 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4804 + sref->sref_idx;
4805
4806 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4807 sv->sv_name,
4808 sref->sref_idx,
4809 si->sn_name);
4810 }
4811 break;
4812 case ISN_LOADS:
4813 {
4814 scriptitem_T *si = SCRIPT_ITEM(
4815 iptr->isn_arg.loadstore.ls_sid);
4816
4817 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4818 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4819 }
4820 break;
4821 case ISN_LOADAUTO:
4822 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4823 break;
4824 case ISN_LOADG:
4825 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4826 break;
4827 case ISN_LOADB:
4828 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4829 break;
4830 case ISN_LOADW:
4831 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4832 break;
4833 case ISN_LOADT:
4834 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4835 break;
4836 case ISN_LOADGDICT:
4837 smsg("%s%4d LOAD g:", pfx, current);
4838 break;
4839 case ISN_LOADBDICT:
4840 smsg("%s%4d LOAD b:", pfx, current);
4841 break;
4842 case ISN_LOADWDICT:
4843 smsg("%s%4d LOAD w:", pfx, current);
4844 break;
4845 case ISN_LOADTDICT:
4846 smsg("%s%4d LOAD t:", pfx, current);
4847 break;
4848 case ISN_LOADOPT:
4849 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4850 break;
4851 case ISN_LOADENV:
4852 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4853 break;
4854 case ISN_LOADREG:
4855 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4856 break;
4857
4858 case ISN_STORE:
4859 if (iptr->isn_arg.number < 0)
4860 smsg("%s%4d STORE arg[%lld]", pfx, current,
4861 iptr->isn_arg.number + STACK_FRAME_SIZE);
4862 else
4863 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4864 break;
4865 case ISN_STOREOUTER:
4866 {
4867 if (iptr->isn_arg.number < 0)
4868 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4869 iptr->isn_arg.outer.outer_depth,
4870 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4871 else
4872 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4873 iptr->isn_arg.outer.outer_depth,
4874 iptr->isn_arg.outer.outer_idx);
4875 }
4876 break;
4877 case ISN_STOREV:
4878 smsg("%s%4d STOREV v:%s", pfx, current,
4879 get_vim_var_name(iptr->isn_arg.number));
4880 break;
4881 case ISN_STOREAUTO:
4882 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4883 break;
4884 case ISN_STOREG:
4885 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4886 break;
4887 case ISN_STOREB:
4888 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4889 break;
4890 case ISN_STOREW:
4891 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4892 break;
4893 case ISN_STORET:
4894 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4895 break;
4896 case ISN_STORES:
4897 {
4898 scriptitem_T *si = SCRIPT_ITEM(
4899 iptr->isn_arg.loadstore.ls_sid);
4900
4901 smsg("%s%4d STORES %s in %s", pfx, current,
4902 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4903 }
4904 break;
4905 case ISN_STORESCRIPT:
4906 {
4907 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4908 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4909 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4910 + sref->sref_idx;
4911
4912 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4913 sv->sv_name,
4914 sref->sref_idx,
4915 si->sn_name);
4916 }
4917 break;
4918 case ISN_STOREOPT:
4919 smsg("%s%4d STOREOPT &%s", pfx, current,
4920 iptr->isn_arg.storeopt.so_name);
4921 break;
4922 case ISN_STOREENV:
4923 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4924 break;
4925 case ISN_STOREREG:
4926 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4927 break;
4928 case ISN_STORENR:
4929 smsg("%s%4d STORE %lld in $%d", pfx, current,
4930 iptr->isn_arg.storenr.stnr_val,
4931 iptr->isn_arg.storenr.stnr_idx);
4932 break;
4933
4934 case ISN_STOREINDEX:
4935 smsg("%s%4d STOREINDEX %s", pfx, current,
4936 vartype_name(iptr->isn_arg.vartype));
4937 break;
4938
4939 case ISN_STORERANGE:
4940 smsg("%s%4d STORERANGE", pfx, current);
4941 break;
4942
4943 // constants
4944 case ISN_PUSHNR:
4945 smsg("%s%4d PUSHNR %lld", pfx, current,
4946 (varnumber_T)(iptr->isn_arg.number));
4947 break;
4948 case ISN_PUSHBOOL:
4949 case ISN_PUSHSPEC:
4950 smsg("%s%4d PUSH %s", pfx, current,
4951 get_var_special_name(iptr->isn_arg.number));
4952 break;
4953 case ISN_PUSHF:
4954#ifdef FEAT_FLOAT
4955 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4956#endif
4957 break;
4958 case ISN_PUSHS:
4959 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4960 break;
4961 case ISN_PUSHBLOB:
4962 {
4963 char_u *r;
4964 char_u numbuf[NUMBUFLEN];
4965 char_u *tofree;
4966
4967 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4968 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4969 vim_free(tofree);
4970 }
4971 break;
4972 case ISN_PUSHFUNC:
4973 {
4974 char *name = (char *)iptr->isn_arg.string;
4975
4976 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4977 name == NULL ? "[none]" : name);
4978 }
4979 break;
4980 case ISN_PUSHCHANNEL:
4981#ifdef FEAT_JOB_CHANNEL
4982 {
4983 channel_T *channel = iptr->isn_arg.channel;
4984
4985 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4986 channel == NULL ? 0 : channel->ch_id);
4987 }
4988#endif
4989 break;
4990 case ISN_PUSHJOB:
4991#ifdef FEAT_JOB_CHANNEL
4992 {
4993 typval_T tv;
4994 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004995 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02004996
4997 tv.v_type = VAR_JOB;
4998 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004999 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005000 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5001 }
5002#endif
5003 break;
5004 case ISN_PUSHEXC:
5005 smsg("%s%4d PUSH v:exception", pfx, current);
5006 break;
5007 case ISN_UNLET:
5008 smsg("%s%4d UNLET%s %s", pfx, current,
5009 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5010 iptr->isn_arg.unlet.ul_name);
5011 break;
5012 case ISN_UNLETENV:
5013 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5014 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5015 iptr->isn_arg.unlet.ul_name);
5016 break;
5017 case ISN_UNLETINDEX:
5018 smsg("%s%4d UNLETINDEX", pfx, current);
5019 break;
5020 case ISN_UNLETRANGE:
5021 smsg("%s%4d UNLETRANGE", pfx, current);
5022 break;
5023 case ISN_LOCKCONST:
5024 smsg("%s%4d LOCKCONST", pfx, current);
5025 break;
5026 case ISN_NEWLIST:
5027 smsg("%s%4d NEWLIST size %lld", pfx, current,
5028 (varnumber_T)(iptr->isn_arg.number));
5029 break;
5030 case ISN_NEWDICT:
5031 smsg("%s%4d NEWDICT size %lld", pfx, current,
5032 (varnumber_T)(iptr->isn_arg.number));
5033 break;
5034
5035 // function call
5036 case ISN_BCALL:
5037 {
5038 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5039
5040 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5041 internal_func_name(cbfunc->cbf_idx),
5042 cbfunc->cbf_argcount);
5043 }
5044 break;
5045 case ISN_DCALL:
5046 {
5047 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5048 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5049 + cdfunc->cdf_idx;
5050
5051 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5052 df->df_ufunc->uf_name_exp != NULL
5053 ? df->df_ufunc->uf_name_exp
5054 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5055 }
5056 break;
5057 case ISN_UCALL:
5058 {
5059 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5060
5061 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5062 cufunc->cuf_name, cufunc->cuf_argcount);
5063 }
5064 break;
5065 case ISN_PCALL:
5066 {
5067 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5068
5069 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5070 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5071 }
5072 break;
5073 case ISN_PCALL_END:
5074 smsg("%s%4d PCALL end", pfx, current);
5075 break;
5076 case ISN_RETURN:
5077 smsg("%s%4d RETURN", pfx, current);
5078 break;
5079 case ISN_RETURN_ZERO:
5080 smsg("%s%4d RETURN 0", pfx, current);
5081 break;
5082 case ISN_FUNCREF:
5083 {
5084 funcref_T *funcref = &iptr->isn_arg.funcref;
5085 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5086 + funcref->fr_func;
5087
5088 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5089 }
5090 break;
5091
5092 case ISN_NEWFUNC:
5093 {
5094 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5095
5096 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5097 newfunc->nf_lambda, newfunc->nf_global);
5098 }
5099 break;
5100
5101 case ISN_DEF:
5102 {
5103 char_u *name = iptr->isn_arg.string;
5104
5105 smsg("%s%4d DEF %s", pfx, current,
5106 name == NULL ? (char_u *)"" : name);
5107 }
5108 break;
5109
5110 case ISN_JUMP:
5111 {
5112 char *when = "?";
5113
5114 switch (iptr->isn_arg.jump.jump_when)
5115 {
5116 case JUMP_ALWAYS:
5117 when = "JUMP";
5118 break;
5119 case JUMP_AND_KEEP_IF_TRUE:
5120 when = "JUMP_AND_KEEP_IF_TRUE";
5121 break;
5122 case JUMP_IF_FALSE:
5123 when = "JUMP_IF_FALSE";
5124 break;
5125 case JUMP_AND_KEEP_IF_FALSE:
5126 when = "JUMP_AND_KEEP_IF_FALSE";
5127 break;
5128 case JUMP_IF_COND_FALSE:
5129 when = "JUMP_IF_COND_FALSE";
5130 break;
5131 case JUMP_IF_COND_TRUE:
5132 when = "JUMP_IF_COND_TRUE";
5133 break;
5134 }
5135 smsg("%s%4d %s -> %d", pfx, current, when,
5136 iptr->isn_arg.jump.jump_where);
5137 }
5138 break;
5139
5140 case ISN_JUMP_IF_ARG_SET:
5141 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5142 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5143 iptr->isn_arg.jump.jump_where);
5144 break;
5145
5146 case ISN_FOR:
5147 {
5148 forloop_T *forloop = &iptr->isn_arg.forloop;
5149
5150 smsg("%s%4d FOR $%d -> %d", pfx, current,
5151 forloop->for_idx, forloop->for_end);
5152 }
5153 break;
5154
5155 case ISN_TRY:
5156 {
5157 try_T *try = &iptr->isn_arg.try;
5158
5159 if (try->try_ref->try_finally == 0)
5160 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5161 pfx, current,
5162 try->try_ref->try_catch,
5163 try->try_ref->try_endtry);
5164 else
5165 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5166 pfx, current,
5167 try->try_ref->try_catch,
5168 try->try_ref->try_finally,
5169 try->try_ref->try_endtry);
5170 }
5171 break;
5172 case ISN_CATCH:
5173 // TODO
5174 smsg("%s%4d CATCH", pfx, current);
5175 break;
5176 case ISN_TRYCONT:
5177 {
5178 trycont_T *trycont = &iptr->isn_arg.trycont;
5179
5180 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5181 trycont->tct_levels,
5182 trycont->tct_levels == 1 ? "" : "s",
5183 trycont->tct_where);
5184 }
5185 break;
5186 case ISN_FINALLY:
5187 smsg("%s%4d FINALLY", pfx, current);
5188 break;
5189 case ISN_ENDTRY:
5190 smsg("%s%4d ENDTRY", pfx, current);
5191 break;
5192 case ISN_THROW:
5193 smsg("%s%4d THROW", pfx, current);
5194 break;
5195
5196 // expression operations on number
5197 case ISN_OPNR:
5198 case ISN_OPFLOAT:
5199 case ISN_OPANY:
5200 {
5201 char *what;
5202 char *ins;
5203
5204 switch (iptr->isn_arg.op.op_type)
5205 {
5206 case EXPR_MULT: what = "*"; break;
5207 case EXPR_DIV: what = "/"; break;
5208 case EXPR_REM: what = "%"; break;
5209 case EXPR_SUB: what = "-"; break;
5210 case EXPR_ADD: what = "+"; break;
5211 default: what = "???"; break;
5212 }
5213 switch (iptr->isn_type)
5214 {
5215 case ISN_OPNR: ins = "OPNR"; break;
5216 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5217 case ISN_OPANY: ins = "OPANY"; break;
5218 default: ins = "???"; break;
5219 }
5220 smsg("%s%4d %s %s", pfx, current, ins, what);
5221 }
5222 break;
5223
5224 case ISN_COMPAREBOOL:
5225 case ISN_COMPARESPECIAL:
5226 case ISN_COMPARENR:
5227 case ISN_COMPAREFLOAT:
5228 case ISN_COMPARESTRING:
5229 case ISN_COMPAREBLOB:
5230 case ISN_COMPARELIST:
5231 case ISN_COMPAREDICT:
5232 case ISN_COMPAREFUNC:
5233 case ISN_COMPAREANY:
5234 {
5235 char *p;
5236 char buf[10];
5237 char *type;
5238
5239 switch (iptr->isn_arg.op.op_type)
5240 {
5241 case EXPR_EQUAL: p = "=="; break;
5242 case EXPR_NEQUAL: p = "!="; break;
5243 case EXPR_GREATER: p = ">"; break;
5244 case EXPR_GEQUAL: p = ">="; break;
5245 case EXPR_SMALLER: p = "<"; break;
5246 case EXPR_SEQUAL: p = "<="; break;
5247 case EXPR_MATCH: p = "=~"; break;
5248 case EXPR_IS: p = "is"; break;
5249 case EXPR_ISNOT: p = "isnot"; break;
5250 case EXPR_NOMATCH: p = "!~"; break;
5251 default: p = "???"; break;
5252 }
5253 STRCPY(buf, p);
5254 if (iptr->isn_arg.op.op_ic == TRUE)
5255 strcat(buf, "?");
5256 switch(iptr->isn_type)
5257 {
5258 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5259 case ISN_COMPARESPECIAL:
5260 type = "COMPARESPECIAL"; break;
5261 case ISN_COMPARENR: type = "COMPARENR"; break;
5262 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5263 case ISN_COMPARESTRING:
5264 type = "COMPARESTRING"; break;
5265 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5266 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5267 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5268 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5269 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5270 default: type = "???"; break;
5271 }
5272
5273 smsg("%s%4d %s %s", pfx, current, type, buf);
5274 }
5275 break;
5276
5277 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5278 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5279
5280 // expression operations
5281 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5282 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5283 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5284 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5285 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5286 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5287 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5288 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5289 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5290 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5291 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5292 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5293 pfx, current, iptr->isn_arg.number); break;
5294 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5295 pfx, current, iptr->isn_arg.number); break;
5296 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5297 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5298 iptr->isn_arg.string); break;
5299 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5300
5301 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5302 case ISN_CHECKTYPE:
5303 {
5304 checktype_T *ct = &iptr->isn_arg.type;
5305 char *tofree;
5306
5307 if (ct->ct_arg_idx == 0)
5308 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5309 type_name(ct->ct_type, &tofree),
5310 (int)ct->ct_off);
5311 else
5312 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5313 type_name(ct->ct_type, &tofree),
5314 (int)ct->ct_off,
5315 (int)ct->ct_arg_idx);
5316 vim_free(tofree);
5317 break;
5318 }
5319 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5320 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5321 iptr->isn_arg.checklen.cl_min_len);
5322 break;
5323 case ISN_SETTYPE:
5324 {
5325 char *tofree;
5326
5327 smsg("%s%4d SETTYPE %s", pfx, current,
5328 type_name(iptr->isn_arg.type.ct_type, &tofree));
5329 vim_free(tofree);
5330 break;
5331 }
5332 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005333 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5334 smsg("%s%4d INVERT %d (!val)", pfx, current,
5335 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005336 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005337 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5338 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005339 break;
5340 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005341 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005342 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005343 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5344 pfx, current,
5345 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005346 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005347 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5348 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005349 break;
5350 case ISN_PUT:
5351 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5352 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005353 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005354 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5355 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005356 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005357 else
5358 smsg("%s%4d PUT %c %ld", pfx, current,
5359 iptr->isn_arg.put.put_regname,
5360 (long)iptr->isn_arg.put.put_lnum);
5361 break;
5362
5363 // TODO: summarize modifiers
5364 case ISN_CMDMOD:
5365 {
5366 char_u *buf;
5367 size_t len = produce_cmdmods(
5368 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5369
5370 buf = alloc(len + 1);
5371 if (buf != NULL)
5372 {
5373 (void)produce_cmdmods(
5374 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5375 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5376 vim_free(buf);
5377 }
5378 break;
5379 }
5380 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5381
5382 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005383 smsg("%s%4d PROFILE START line %d", pfx, current,
5384 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005385 break;
5386
5387 case ISN_PROF_END:
5388 smsg("%s%4d PROFILE END", pfx, current);
5389 break;
5390
Bram Moolenaare99d4222021-06-13 14:01:26 +02005391 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005392 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5393 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005394 break;
5395
Bram Moolenaar4c137212021-04-19 16:48:48 +02005396 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5397 iptr->isn_arg.unpack.unp_count,
5398 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5399 break;
5400 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5401 iptr->isn_arg.shuffle.shfl_item,
5402 iptr->isn_arg.shuffle.shfl_up);
5403 break;
5404 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5405
5406 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5407 return;
5408 }
5409
5410 out_flush(); // output one line at a time
5411 ui_breakcheck();
5412 if (got_int)
5413 break;
5414 }
5415}
5416
5417/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005418 * Handle command line completion for the :disassemble command.
5419 */
5420 void
5421set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5422{
5423 char_u *p;
5424
5425 // Default: expand user functions, "debug" and "profile"
5426 xp->xp_context = EXPAND_DISASSEMBLE;
5427 xp->xp_pattern = arg;
5428
5429 // first argument already typed: only user function names
5430 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5431 {
5432 xp->xp_context = EXPAND_USER_FUNC;
5433 xp->xp_pattern = skipwhite(p);
5434 }
5435}
5436
5437/*
5438 * Function given to ExpandGeneric() to obtain the list of :disassemble
5439 * arguments.
5440 */
5441 char_u *
5442get_disassemble_argument(expand_T *xp, int idx)
5443{
5444 if (idx == 0)
5445 return (char_u *)"debug";
5446 if (idx == 1)
5447 return (char_u *)"profile";
5448 return get_user_func_name(xp, idx - 2);
5449}
5450
5451/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005452 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005453 * We don't really need this at runtime, but we do have tests that require it,
5454 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455 */
5456 void
5457ex_disassemble(exarg_T *eap)
5458{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005459 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005460 char_u *fname;
5461 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462 dfunc_T *dfunc;
5463 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005464 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005465 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005466 compiletype_T compile_type = CT_NONE;
5467
5468 if (STRNCMP(arg, "profile", 7) == 0)
5469 {
5470 compile_type = CT_PROFILE;
5471 arg = skipwhite(arg + 7);
5472 }
5473 else if (STRNCMP(arg, "debug", 5) == 0)
5474 {
5475 compile_type = CT_DEBUG;
5476 arg = skipwhite(arg + 5);
5477 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005478
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005479 if (STRNCMP(arg, "<lambda>", 8) == 0)
5480 {
5481 arg += 8;
5482 (void)getdigits(&arg);
5483 fname = vim_strnsave(eap->arg, arg - eap->arg);
5484 }
5485 else
5486 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005487 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005488 if (fname == NULL)
5489 {
5490 semsg(_(e_invarg2), eap->arg);
5491 return;
5492 }
5493
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005494 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005495 if (ufunc == NULL)
5496 {
5497 char_u *p = untrans_function_name(fname);
5498
5499 if (p != NULL)
5500 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005501 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005502 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005503 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504 if (ufunc == NULL)
5505 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005506 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005507 return;
5508 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005509 if (func_needs_compiling(ufunc, compile_type)
5510 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005511 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005512 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005514 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515 return;
5516 }
5517 if (ufunc->uf_name_exp != NULL)
5518 msg((char *)ufunc->uf_name_exp);
5519 else
5520 msg((char *)ufunc->uf_name);
5521
5522 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005523 switch (compile_type)
5524 {
5525 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005526#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005527 instr = dfunc->df_instr_prof;
5528 instr_count = dfunc->df_instr_prof_count;
5529 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005530#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005531 // FALLTHROUGH
5532 case CT_NONE:
5533 instr = dfunc->df_instr;
5534 instr_count = dfunc->df_instr_count;
5535 break;
5536 case CT_DEBUG:
5537 instr = dfunc->df_instr_debug;
5538 instr_count = dfunc->df_instr_debug_count;
5539 break;
5540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541
Bram Moolenaar4c137212021-04-19 16:48:48 +02005542 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543}
5544
5545/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005546 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547 * list, etc. Mostly like what JavaScript does, except that empty list and
5548 * empty dictionary are FALSE.
5549 */
5550 int
5551tv2bool(typval_T *tv)
5552{
5553 switch (tv->v_type)
5554 {
5555 case VAR_NUMBER:
5556 return tv->vval.v_number != 0;
5557 case VAR_FLOAT:
5558#ifdef FEAT_FLOAT
5559 return tv->vval.v_float != 0.0;
5560#else
5561 break;
5562#endif
5563 case VAR_PARTIAL:
5564 return tv->vval.v_partial != NULL;
5565 case VAR_FUNC:
5566 case VAR_STRING:
5567 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5568 case VAR_LIST:
5569 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5570 case VAR_DICT:
5571 return tv->vval.v_dict != NULL
5572 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5573 case VAR_BOOL:
5574 case VAR_SPECIAL:
5575 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5576 case VAR_JOB:
5577#ifdef FEAT_JOB_CHANNEL
5578 return tv->vval.v_job != NULL;
5579#else
5580 break;
5581#endif
5582 case VAR_CHANNEL:
5583#ifdef FEAT_JOB_CHANNEL
5584 return tv->vval.v_channel != NULL;
5585#else
5586 break;
5587#endif
5588 case VAR_BLOB:
5589 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5590 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005591 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005593 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005594 break;
5595 }
5596 return FALSE;
5597}
5598
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005599 void
5600emsg_using_string_as(typval_T *tv, int as_number)
5601{
5602 semsg(_(as_number ? e_using_string_as_number_str
5603 : e_using_string_as_bool_str),
5604 tv->vval.v_string == NULL
5605 ? (char_u *)"" : tv->vval.v_string);
5606}
5607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005608/*
5609 * If "tv" is a string give an error and return FAIL.
5610 */
5611 int
5612check_not_string(typval_T *tv)
5613{
5614 if (tv->v_type == VAR_STRING)
5615 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005616 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005617 clear_tv(tv);
5618 return FAIL;
5619 }
5620 return OK;
5621}
5622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005623
5624#endif // FEAT_EVAL