blob: b8ecf9b3784becd3e88e59f73517ab9a85b7caa3 [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 Moolenaar6d1792d2021-06-13 14:33:20 +0200755 compiletype_T compile_type = CT_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756
Bram Moolenaar6d1792d2021-06-13 14:33:20 +0200757#ifdef FEAT_PROFILE
758 if (do_profiling == PROF_YES && ufunc->uf_profiling)
759 compile_type = CT_PROFILE;
760#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +0200761 if (func_needs_compiling(ufunc, compile_type)
762 && compile_def_function(ufunc, FALSE, compile_type, NULL)
763 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200764 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200765 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100766 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100767 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100768 if (error != FCERR_UNKNOWN)
769 {
770 if (error == FCERR_TOOMANY)
771 semsg(_(e_toomanyarg), ufunc->uf_name);
772 else
773 semsg(_(e_toofewarg), ufunc->uf_name);
774 return FAIL;
775 }
776
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100777 // The function has been compiled, can call it quickly. For a function
778 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100779 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100780 if (iptr != NULL)
781 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100782 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100783 iptr->isn_type = ISN_DCALL;
784 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
785 iptr->isn_arg.dfunc.cdf_argcount = argcount;
786 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200787 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789
790 if (call_prepare(argcount, argvars, ectx) == FAIL)
791 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200792 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 funcexe.evaluate = TRUE;
794
795 // Call the user function. Result goes in last position on the stack.
796 // TODO: add selfdict if there is one
797 error = call_user_func_check(ufunc, argcount, argvars,
798 STACK_TV_BOT(-1), &funcexe, NULL);
799
800 // Clear the arguments.
801 for (idx = 0; idx < argcount; ++idx)
802 clear_tv(&argvars[idx]);
803
804 if (error != FCERR_NONE)
805 {
806 user_func_error(error, ufunc->uf_name);
807 return FAIL;
808 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100809 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200810 // Error other than from calling the function itself.
811 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 return OK;
813}
814
815/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100816 * If command modifiers were applied restore them.
817 */
818 static void
819may_restore_cmdmod(funclocal_T *funclocal)
820{
821 if (funclocal->floc_restore_cmdmod)
822 {
823 cmdmod.cmod_filter_regmatch.regprog = NULL;
824 undo_cmdmod(&cmdmod);
825 cmdmod = funclocal->floc_save_cmdmod;
826 funclocal->floc_restore_cmdmod = FALSE;
827 }
828}
829
830/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200831 * Return TRUE if an error was given or CTRL-C was pressed.
832 */
833 static int
834vim9_aborting(int prev_called_emsg)
835{
836 return called_emsg > prev_called_emsg || got_int || did_throw;
837}
838
839/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 * Execute a function by "name".
841 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100842 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 * Returns FAIL if not found without an error message.
844 */
845 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100846call_by_name(
847 char_u *name,
848 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100849 ectx_T *ectx,
850 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851{
852 ufunc_T *ufunc;
853
854 if (builtin_function(name, -1))
855 {
856 int func_idx = find_internal_func(name);
857
858 if (func_idx < 0)
859 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200860 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 return FAIL;
862 return call_bfunc(func_idx, argcount, ectx);
863 }
864
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200865 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200866
867 if (ufunc == NULL)
868 {
869 int called_emsg_before = called_emsg;
870
871 if (script_autoload(name, TRUE))
872 // loaded a package, search for the function again
873 ufunc = find_func(name, FALSE, NULL);
874 if (vim9_aborting(called_emsg_before))
875 return FAIL; // bail out if loading the script caused an error
876 }
877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100879 {
880 if (ufunc->uf_arg_types != NULL)
881 {
882 int i;
883 typval_T *argv = STACK_TV_BOT(0) - argcount;
884
885 // The function can change at runtime, check that the argument
886 // types are correct.
887 for (i = 0; i < argcount; ++i)
888 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100889 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100890
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100891 if (i < ufunc->uf_args.ga_len)
892 type = ufunc->uf_arg_types[i];
893 else if (ufunc->uf_va_type != NULL)
894 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100895 if (type != NULL && check_typval_arg_type(type,
896 &argv[i], i + 1) == FAIL)
897 return FAIL;
898 }
899 }
900
Bram Moolenaar4c137212021-04-19 16:48:48 +0200901 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903
904 return FAIL;
905}
906
907 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100908call_partial(
909 typval_T *tv,
910 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100911 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200913 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200914 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100916 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917
918 if (tv->v_type == VAR_PARTIAL)
919 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200920 partial_T *pt = tv->vval.v_partial;
921 int i;
922
923 if (pt->pt_argc > 0)
924 {
925 // Make space for arguments from the partial, shift the "argcount"
926 // arguments up.
927 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
928 return FAIL;
929 for (i = 1; i <= argcount; ++i)
930 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
931 ectx->ec_stack.ga_len += pt->pt_argc;
932 argcount += pt->pt_argc;
933
934 // copy the arguments from the partial onto the stack
935 for (i = 0; i < pt->pt_argc; ++i)
936 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
937 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938
939 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200940 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 name = pt->pt_name;
943 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200944 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200946 if (name != NULL)
947 {
948 char_u fname_buf[FLEN_FIXED + 1];
949 char_u *tofree = NULL;
950 int error = FCERR_NONE;
951 char_u *fname;
952
953 // May need to translate <SNR>123_ to K_SNR.
954 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
955 if (error != FCERR_NONE)
956 res = FAIL;
957 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200958 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200959 vim_free(tofree);
960 }
961
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100962 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 {
964 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200965 semsg(_(e_unknownfunc),
966 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 return FAIL;
968 }
969 return OK;
970}
971
972/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200973 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
974 * TRUE.
975 */
976 static int
977error_if_locked(int lock, char *error)
978{
979 if (lock & (VAR_LOCKED | VAR_FIXED))
980 {
981 emsg(_(error));
982 return TRUE;
983 }
984 return FALSE;
985}
986
987/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100988 * Give an error if "tv" is not a number and return FAIL.
989 */
990 static int
991check_for_number(typval_T *tv)
992{
993 if (tv->v_type != VAR_NUMBER)
994 {
995 semsg(_(e_expected_str_but_got_str),
996 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
997 return FAIL;
998 }
999 return OK;
1000}
1001
1002/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001003 * Store "tv" in variable "name".
1004 * This is for s: and g: variables.
1005 */
1006 static void
1007store_var(char_u *name, typval_T *tv)
1008{
1009 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001010 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001011
Bram Moolenaard877a572021-04-01 19:42:48 +02001012 if (tv->v_lock)
1013 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001014 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001015 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001016 restore_funccal();
1017}
1018
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001019/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001020 * Convert "tv" to a string.
1021 * Return FAIL if not allowed.
1022 */
1023 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001024do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001025{
1026 if (tv->v_type != VAR_STRING)
1027 {
1028 char_u *str;
1029
1030 if (is_2string_any)
1031 {
1032 switch (tv->v_type)
1033 {
1034 case VAR_SPECIAL:
1035 case VAR_BOOL:
1036 case VAR_NUMBER:
1037 case VAR_FLOAT:
1038 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001039
1040 case VAR_LIST:
1041 if (tolerant)
1042 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001043 char_u *s, *e, *p;
1044 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001045
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001046 ga_init2(&ga, sizeof(char_u *), 1);
1047
1048 // Convert to NL separated items, then
1049 // escape the items and replace the NL with
1050 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001051 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001052 if (str == NULL)
1053 return FAIL;
1054 s = str;
1055 while ((e = vim_strchr(s, '\n')) != NULL)
1056 {
1057 *e = NUL;
1058 p = vim_strsave_fnameescape(s, FALSE);
1059 if (p != NULL)
1060 {
1061 ga_concat(&ga, p);
1062 ga_concat(&ga, (char_u *)" ");
1063 vim_free(p);
1064 }
1065 s = e + 1;
1066 }
1067 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001068 clear_tv(tv);
1069 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001070 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001071 return OK;
1072 }
1073 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001074 default: to_string_error(tv->v_type);
1075 return FAIL;
1076 }
1077 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001078 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001079 clear_tv(tv);
1080 tv->v_type = VAR_STRING;
1081 tv->vval.v_string = str;
1082 }
1083 return OK;
1084}
1085
1086/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001087 * When the value of "sv" is a null list of dict, allocate it.
1088 */
1089 static void
1090allocate_if_null(typval_T *tv)
1091{
1092 switch (tv->v_type)
1093 {
1094 case VAR_LIST:
1095 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001096 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001097 break;
1098 case VAR_DICT:
1099 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001100 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001101 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001102 case VAR_BLOB:
1103 if (tv->vval.v_blob == NULL)
1104 (void)rettv_blob_alloc(tv);
1105 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001106 default:
1107 break;
1108 }
1109}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001110
Bram Moolenaare7525c52021-01-09 13:20:37 +01001111/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001112 * Return the character "str[index]" where "index" is the character index,
1113 * including composing characters.
1114 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001115 */
1116 char_u *
1117char_from_string(char_u *str, varnumber_T index)
1118{
1119 size_t nbyte = 0;
1120 varnumber_T nchar = index;
1121 size_t slen;
1122
1123 if (str == NULL)
1124 return NULL;
1125 slen = STRLEN(str);
1126
Bram Moolenaarff871402021-03-26 13:34:05 +01001127 // Do the same as for a list: a negative index counts from the end.
1128 // Optimization to check the first byte to be below 0x80 (and no composing
1129 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001130 if (index < 0)
1131 {
1132 int clen = 0;
1133
1134 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001135 {
1136 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1137 ++nbyte;
1138 else if (enc_utf8)
1139 nbyte += utfc_ptr2len(str + nbyte);
1140 else
1141 nbyte += mb_ptr2len(str + nbyte);
1142 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001143 nchar = clen + index;
1144 if (nchar < 0)
1145 // unlike list: index out of range results in empty string
1146 return NULL;
1147 }
1148
1149 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001150 {
1151 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1152 ++nbyte;
1153 else if (enc_utf8)
1154 nbyte += utfc_ptr2len(str + nbyte);
1155 else
1156 nbyte += mb_ptr2len(str + nbyte);
1157 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001158 if (nbyte >= slen)
1159 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001160 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001161}
1162
1163/*
1164 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001165 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001166 * If going over the end return "str_len".
1167 * If "idx" is negative count from the end, -1 is the last character.
1168 * When going over the start return -1.
1169 */
1170 static long
1171char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1172{
1173 varnumber_T nchar = idx;
1174 size_t nbyte = 0;
1175
1176 if (nchar >= 0)
1177 {
1178 while (nchar > 0 && nbyte < str_len)
1179 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001180 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001181 --nchar;
1182 }
1183 }
1184 else
1185 {
1186 nbyte = str_len;
1187 while (nchar < 0 && nbyte > 0)
1188 {
1189 --nbyte;
1190 nbyte -= mb_head_off(str, str + nbyte);
1191 ++nchar;
1192 }
1193 if (nchar < 0)
1194 return -1;
1195 }
1196 return (long)nbyte;
1197}
1198
1199/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001200 * Return the slice "str[first : last]" using character indexes. Composing
1201 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001202 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001203 * Return NULL when the result is empty.
1204 */
1205 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001206string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001207{
1208 long start_byte, end_byte;
1209 size_t slen;
1210
1211 if (str == NULL)
1212 return NULL;
1213 slen = STRLEN(str);
1214 start_byte = char_idx2byte(str, slen, first);
1215 if (start_byte < 0)
1216 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001217 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001218 end_byte = (long)slen;
1219 else
1220 {
1221 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001222 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001223 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001224 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001225 }
1226
1227 if (start_byte >= (long)slen || end_byte <= start_byte)
1228 return NULL;
1229 return vim_strnsave(str + start_byte, end_byte - start_byte);
1230}
1231
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001232 static svar_T *
1233get_script_svar(scriptref_T *sref, ectx_T *ectx)
1234{
1235 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1236 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1237 + ectx->ec_dfunc_idx;
1238 svar_T *sv;
1239
1240 if (sref->sref_seq != si->sn_script_seq)
1241 {
1242 // The script was reloaded after the function was
1243 // compiled, the script_idx may not be valid.
1244 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1245 dfunc->df_ufunc->uf_name_exp);
1246 return NULL;
1247 }
1248 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1249 if (!equal_type(sv->sv_type, sref->sref_type))
1250 {
1251 emsg(_(e_script_variable_type_changed));
1252 return NULL;
1253 }
1254 return sv;
1255}
1256
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001257/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001258 * Function passed to do_cmdline() for splitting a script joined by NL
1259 * characters.
1260 */
1261 static char_u *
1262get_split_sourceline(
1263 int c UNUSED,
1264 void *cookie,
1265 int indent UNUSED,
1266 getline_opt_T options UNUSED)
1267{
1268 source_cookie_T *sp = (source_cookie_T *)cookie;
1269 char_u *p;
1270 char_u *line;
1271
1272 if (*sp->nextline == NUL)
1273 return NULL;
1274 p = vim_strchr(sp->nextline, '\n');
1275 if (p == NULL)
1276 {
1277 line = vim_strsave(sp->nextline);
1278 sp->nextline += STRLEN(sp->nextline);
1279 }
1280 else
1281 {
1282 line = vim_strnsave(sp->nextline, p - sp->nextline);
1283 sp->nextline = p + 1;
1284 }
1285 return line;
1286}
1287
1288/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 * Execute a function by "name".
1290 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001291 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 */
1293 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001294call_eval_func(
1295 char_u *name,
1296 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001297 ectx_T *ectx,
1298 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299{
Bram Moolenaared677f52020-08-12 16:38:10 +02001300 int called_emsg_before = called_emsg;
1301 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302
Bram Moolenaar4c137212021-04-19 16:48:48 +02001303 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001304 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001306 dictitem_T *v;
1307
1308 v = find_var(name, NULL, FALSE);
1309 if (v == NULL)
1310 {
1311 semsg(_(e_unknownfunc), name);
1312 return FAIL;
1313 }
1314 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1315 {
1316 semsg(_(e_unknownfunc), name);
1317 return FAIL;
1318 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001319 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001321 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322}
1323
1324/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001325 * When a function reference is used, fill a partial with the information
1326 * needed, especially when it is used as a closure.
1327 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001328 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001329fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1330{
1331 pt->pt_func = ufunc;
1332 pt->pt_refcount = 1;
1333
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001334 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001335 {
1336 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1337 + ectx->ec_dfunc_idx;
1338
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001339 // The closure may need to find arguments and local variables in the
1340 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001341 pt->pt_outer.out_stack = &ectx->ec_stack;
1342 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001343 if (ectx->ec_outer_ref != NULL)
1344 {
1345 // The current context already has a context, link to that one.
1346 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1347 if (ectx->ec_outer_ref->or_partial != NULL)
1348 {
1349 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1350 ++pt->pt_outer.out_up_partial->pt_refcount;
1351 }
1352 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001353
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001354 // If this function returns and the closure is still being used, we
1355 // need to make a copy of the context (arguments and local variables).
1356 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001357 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1358 {
1359 vim_free(pt);
1360 return FAIL;
1361 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001362 // Extra variable keeps the count of closures created in the current
1363 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001364 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1365 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1366
1367 ((partial_T **)ectx->ec_funcrefs.ga_data)
1368 [ectx->ec_funcrefs.ga_len] = pt;
1369 ++pt->pt_refcount;
1370 ++ectx->ec_funcrefs.ga_len;
1371 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001372 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001373 return OK;
1374}
1375
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001376// used for v_instr of typval of VAR_INSTR
1377struct instr_S {
1378 ectx_T *instr_ectx;
1379 isn_T *instr_instr;
1380};
1381
Bram Moolenaar4c137212021-04-19 16:48:48 +02001382// used for substitute_instr
1383typedef struct subs_expr_S {
1384 ectx_T *subs_ectx;
1385 isn_T *subs_instr;
1386 int subs_status;
1387} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388
1389// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001390#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391
1392// Get pointer to item at the bottom of the stack, -1 is the bottom.
1393#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001394#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 +01001395
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001396// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001397#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 +01001398
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001399// Set when calling do_debug().
1400static ectx_T *debug_context = NULL;
1401static int debug_arg_count;
1402
1403/*
1404 * When debugging lookup "name" and return the typeval.
1405 * When not found return NULL.
1406 */
1407 typval_T *
1408lookup_debug_var(char_u *name)
1409{
1410 int idx;
1411 dfunc_T *dfunc;
1412 ectx_T *ectx = debug_context;
1413
1414 if (ectx == NULL)
1415 return NULL;
1416 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1417
1418 // Go through the local variable names, from last to first.
1419 for (idx = debug_arg_count - 1; idx >= 0; --idx)
1420 {
1421 char_u *s = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1422 if (STRCMP(s, name) == 0)
1423 return STACK_TV_VAR(idx);
1424 }
1425
1426 return NULL;
1427}
1428
Bram Moolenaar4c137212021-04-19 16:48:48 +02001429/*
1430 * Execute instructions in execution context "ectx".
1431 * Return OK or FAIL;
1432 */
1433 static int
1434exec_instructions(ectx_T *ectx)
1435{
1436 int breakcheck_count = 0;
1437 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001438 int ret = FAIL;
1439 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001440
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001441 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001442 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001443
Bram Moolenaarff652882021-05-16 15:24:49 +02001444 // Only catch exceptions in this instruction list.
1445 ectx->ec_trylevel_at_start = trylevel;
1446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 for (;;)
1448 {
1449 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001450
Bram Moolenaar270d0382020-05-15 21:42:53 +02001451 if (++breakcheck_count >= 100)
1452 {
1453 line_breakcheck();
1454 breakcheck_count = 0;
1455 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001456 if (got_int)
1457 {
1458 // Turn CTRL-C into an exception.
1459 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001460 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001461 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001462 did_throw = TRUE;
1463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464
Bram Moolenaara26b9702020-04-18 19:53:28 +02001465 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1466 {
1467 // Turn an error message into an exception.
1468 did_emsg = FALSE;
1469 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001470 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001471 did_throw = TRUE;
1472 *msg_list = NULL;
1473 }
1474
Bram Moolenaar4c137212021-04-19 16:48:48 +02001475 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001477 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001478 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479
1480 // An exception jumps to the first catch, finally, or returns from
1481 // the current function.
1482 if (trystack->ga_len > 0)
1483 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001484 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 {
1486 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001487 ectx->ec_in_catch = TRUE;
1488 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 }
1490 else
1491 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001492 // Not inside try or need to return from current functions.
1493 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001494 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001495 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001496 tv = STACK_TV_BOT(0);
1497 tv->v_type = VAR_NUMBER;
1498 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001499 ++ectx->ec_stack.ga_len;
1500 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001502 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001503 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001504 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001505 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 goto done;
1507 }
1508
Bram Moolenaar4c137212021-04-19 16:48:48 +02001509 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001510 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 }
1512 continue;
1513 }
1514
Bram Moolenaar4c137212021-04-19 16:48:48 +02001515 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516 switch (iptr->isn_type)
1517 {
1518 // execute Ex command line
1519 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001520 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001521 source_cookie_T cookie;
1522
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001523 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001524 // Pass getsourceline to get an error for a missing ":end"
1525 // command.
1526 CLEAR_FIELD(cookie);
1527 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1528 if (do_cmdline(iptr->isn_arg.string,
1529 getsourceline, &cookie,
1530 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1531 == FAIL
1532 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001533 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 break;
1536
Bram Moolenaar20677332021-06-06 17:02:53 +02001537 // execute Ex command line split at NL characters.
1538 case ISN_EXEC_SPLIT:
1539 {
1540 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001541 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001542
1543 SOURCING_LNUM = iptr->isn_lnum;
1544 CLEAR_FIELD(cookie);
1545 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1546 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001547 line = get_split_sourceline(0, &cookie, 0, 0);
1548 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001549 get_split_sourceline, &cookie,
1550 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1551 == FAIL
1552 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001553 {
1554 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001555 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001556 }
1557 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001558 }
1559 break;
1560
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001561 // Evaluate an expression with legacy syntax, push it onto the
1562 // stack.
1563 case ISN_LEGACY_EVAL:
1564 {
1565 char_u *arg = iptr->isn_arg.string;
1566 int res;
1567 int save_flags = cmdmod.cmod_flags;
1568
1569 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001570 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001571 tv = STACK_TV_BOT(0);
1572 init_tv(tv);
1573 cmdmod.cmod_flags |= CMOD_LEGACY;
1574 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1575 cmdmod.cmod_flags = save_flags;
1576 if (res == FAIL)
1577 goto on_error;
1578 ++ectx->ec_stack.ga_len;
1579 }
1580 break;
1581
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001582 // push typeval VAR_INSTR with instructions to be executed
1583 case ISN_INSTR:
1584 {
1585 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001586 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001587 tv = STACK_TV_BOT(0);
1588 tv->vval.v_instr = ALLOC_ONE(instr_T);
1589 if (tv->vval.v_instr == NULL)
1590 goto on_error;
1591 ++ectx->ec_stack.ga_len;
1592
1593 tv->v_type = VAR_INSTR;
1594 tv->vval.v_instr->instr_ectx = ectx;
1595 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1596 }
1597 break;
1598
Bram Moolenaar4c137212021-04-19 16:48:48 +02001599 // execute :substitute with an expression
1600 case ISN_SUBSTITUTE:
1601 {
1602 subs_T *subs = &iptr->isn_arg.subs;
1603 source_cookie_T cookie;
1604 struct subs_expr_S *save_instr = substitute_instr;
1605 struct subs_expr_S subs_instr;
1606 int res;
1607
1608 subs_instr.subs_ectx = ectx;
1609 subs_instr.subs_instr = subs->subs_instr;
1610 subs_instr.subs_status = OK;
1611 substitute_instr = &subs_instr;
1612
1613 SOURCING_LNUM = iptr->isn_lnum;
1614 // This is very much like ISN_EXEC
1615 CLEAR_FIELD(cookie);
1616 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1617 res = do_cmdline(subs->subs_cmd,
1618 getsourceline, &cookie,
1619 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1620 substitute_instr = save_instr;
1621
1622 if (res == FAIL || did_emsg
1623 || subs_instr.subs_status == FAIL)
1624 goto on_error;
1625 }
1626 break;
1627
1628 case ISN_FINISH:
1629 goto done;
1630
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001631 case ISN_REDIRSTART:
1632 // create a dummy entry for var_redir_str()
1633 if (alloc_redir_lval() == FAIL)
1634 goto on_error;
1635
1636 // The output is stored in growarray "redir_ga" until
1637 // redirection ends.
1638 init_redir_ga();
1639 redir_vname = 1;
1640 break;
1641
1642 case ISN_REDIREND:
1643 {
1644 char_u *res = get_clear_redir_ga();
1645
1646 // End redirection, put redirected text on the stack.
1647 clear_redir_lval();
1648 redir_vname = 0;
1649
1650 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1651 {
1652 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001653 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001654 }
1655 tv = STACK_TV_BOT(0);
1656 tv->v_type = VAR_STRING;
1657 tv->vval.v_string = res;
1658 ++ectx->ec_stack.ga_len;
1659 }
1660 break;
1661
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001662 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001663#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001664 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1665 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001666#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001667 break;
1668
1669 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001670#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001671 {
1672 exarg_T ea;
1673 int res;
1674
1675 CLEAR_FIELD(ea);
1676 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1677 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1678 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1679 --ectx->ec_stack.ga_len;
1680 tv = STACK_TV_BOT(0);
1681 res = cexpr_core(&ea, tv);
1682 clear_tv(tv);
1683 if (res == FAIL)
1684 goto on_error;
1685 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001686#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001687 break;
1688
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001689 // execute Ex command from pieces on the stack
1690 case ISN_EXECCONCAT:
1691 {
1692 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001693 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001694 int pass;
1695 int i;
1696 char_u *cmd = NULL;
1697 char_u *str;
1698
1699 for (pass = 1; pass <= 2; ++pass)
1700 {
1701 for (i = 0; i < count; ++i)
1702 {
1703 tv = STACK_TV_BOT(i - count);
1704 str = tv->vval.v_string;
1705 if (str != NULL && *str != NUL)
1706 {
1707 if (pass == 2)
1708 STRCPY(cmd + len, str);
1709 len += STRLEN(str);
1710 }
1711 if (pass == 2)
1712 clear_tv(tv);
1713 }
1714 if (pass == 1)
1715 {
1716 cmd = alloc(len + 1);
1717 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001718 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001719 len = 0;
1720 }
1721 }
1722
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001723 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001724 do_cmdline_cmd(cmd);
1725 vim_free(cmd);
1726 }
1727 break;
1728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 // execute :echo {string} ...
1730 case ISN_ECHO:
1731 {
1732 int count = iptr->isn_arg.echo.echo_count;
1733 int atstart = TRUE;
1734 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001735 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736
1737 for (idx = 0; idx < count; ++idx)
1738 {
1739 tv = STACK_TV_BOT(idx - count);
1740 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1741 &atstart, &needclr);
1742 clear_tv(tv);
1743 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001744 if (needclr)
1745 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001746 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 }
1748 break;
1749
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001750 // :execute {string} ...
1751 // :echomsg {string} ...
1752 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001753 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001754 case ISN_ECHOMSG:
1755 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001756 {
1757 int count = iptr->isn_arg.number;
1758 garray_T ga;
1759 char_u buf[NUMBUFLEN];
1760 char_u *p;
1761 int len;
1762 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001763 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001764
1765 ga_init2(&ga, 1, 80);
1766 for (idx = 0; idx < count; ++idx)
1767 {
1768 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001769 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001770 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001771 if (tv->v_type == VAR_CHANNEL
1772 || tv->v_type == VAR_JOB)
1773 {
1774 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001775 semsg(_(e_using_invalid_value_as_string_str),
1776 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001777 break;
1778 }
1779 else
1780 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001781 }
1782 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001783 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001784
1785 len = (int)STRLEN(p);
1786 if (ga_grow(&ga, len + 2) == FAIL)
1787 failed = TRUE;
1788 else
1789 {
1790 if (ga.ga_len > 0)
1791 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1792 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1793 ga.ga_len += len;
1794 }
1795 clear_tv(tv);
1796 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001797 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001798 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001799 {
1800 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001801 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001802 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001803
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001804 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001805 {
1806 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001807 {
1808 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001809 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001810 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001811 {
1812 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001813 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001814 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001815 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001816 else
1817 {
1818 msg_sb_eol();
1819 if (iptr->isn_type == ISN_ECHOMSG)
1820 {
1821 msg_attr(ga.ga_data, echo_attr);
1822 out_flush();
1823 }
1824 else
1825 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001826 SOURCING_LNUM = iptr->isn_lnum;
1827 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001828 }
1829 }
1830 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001831 ga_clear(&ga);
1832 }
1833 break;
1834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 // load local variable or argument
1836 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001837 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001838 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001840 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 break;
1842
1843 // load v: variable
1844 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001845 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001846 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001848 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 break;
1850
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001851 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 case ISN_LOADSCRIPT:
1853 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001854 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 svar_T *sv;
1856
Bram Moolenaar4c137212021-04-19 16:48:48 +02001857 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001858 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001859 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001860 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001861 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001862 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001864 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 }
1866 break;
1867
1868 // load s: variable in old script
1869 case ISN_LOADS:
1870 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001871 hashtab_T *ht = &SCRIPT_VARS(
1872 iptr->isn_arg.loadstore.ls_sid);
1873 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 if (di == NULL)
1877 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001878 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001879 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001880 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 }
1882 else
1883 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001884 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001885 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001887 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 }
1889 }
1890 break;
1891
Bram Moolenaard3aac292020-04-19 14:32:17 +02001892 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001894 case ISN_LOADB:
1895 case ISN_LOADW:
1896 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001898 dictitem_T *di = NULL;
1899 hashtab_T *ht = NULL;
1900 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001901
Bram Moolenaard3aac292020-04-19 14:32:17 +02001902 switch (iptr->isn_type)
1903 {
1904 case ISN_LOADG:
1905 ht = get_globvar_ht();
1906 namespace = 'g';
1907 break;
1908 case ISN_LOADB:
1909 ht = &curbuf->b_vars->dv_hashtab;
1910 namespace = 'b';
1911 break;
1912 case ISN_LOADW:
1913 ht = &curwin->w_vars->dv_hashtab;
1914 namespace = 'w';
1915 break;
1916 case ISN_LOADT:
1917 ht = &curtab->tp_vars->dv_hashtab;
1918 namespace = 't';
1919 break;
1920 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001921 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001922 }
1923 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 if (di == NULL)
1926 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001927 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001928 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001929 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001930 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 }
1932 else
1933 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001934 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001935 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001937 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 }
1939 }
1940 break;
1941
Bram Moolenaar03290b82020-12-19 16:30:44 +01001942 // load autoload variable
1943 case ISN_LOADAUTO:
1944 {
1945 char_u *name = iptr->isn_arg.string;
1946
Bram Moolenaar4c137212021-04-19 16:48:48 +02001947 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001948 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001949 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001950 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001951 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001952 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001953 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001954 }
1955 break;
1956
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001957 // load g:/b:/w:/t: namespace
1958 case ISN_LOADGDICT:
1959 case ISN_LOADBDICT:
1960 case ISN_LOADWDICT:
1961 case ISN_LOADTDICT:
1962 {
1963 dict_T *d = NULL;
1964
1965 switch (iptr->isn_type)
1966 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001967 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1968 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1969 case ISN_LOADWDICT: d = curwin->w_vars; break;
1970 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001971 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001972 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001973 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001974 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001975 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001976 tv = STACK_TV_BOT(0);
1977 tv->v_type = VAR_DICT;
1978 tv->v_lock = 0;
1979 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001980 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001981 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001982 }
1983 break;
1984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 // load &option
1986 case ISN_LOADOPT:
1987 {
1988 typval_T optval;
1989 char_u *name = iptr->isn_arg.string;
1990
Bram Moolenaara8c17702020-04-01 21:17:24 +02001991 // This is not expected to fail, name is checked during
1992 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001993 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001994 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001995 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001996 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001998 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 }
2000 break;
2001
2002 // load $ENV
2003 case ISN_LOADENV:
2004 {
2005 typval_T optval;
2006 char_u *name = iptr->isn_arg.string;
2007
Bram Moolenaar4c137212021-04-19 16:48:48 +02002008 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002009 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002010 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002011 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002013 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 }
2015 break;
2016
2017 // load @register
2018 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002019 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002020 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 tv = STACK_TV_BOT(0);
2022 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002023 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002024 // This may result in NULL, which should be equivalent to an
2025 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 tv->vval.v_string = get_reg_contents(
2027 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002028 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 break;
2030
2031 // store local variable
2032 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002033 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 tv = STACK_TV_VAR(iptr->isn_arg.number);
2035 clear_tv(tv);
2036 *tv = *STACK_TV_BOT(0);
2037 break;
2038
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002039 // store s: variable in old script
2040 case ISN_STORES:
2041 {
2042 hashtab_T *ht = &SCRIPT_VARS(
2043 iptr->isn_arg.loadstore.ls_sid);
2044 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002045 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002046
Bram Moolenaar4c137212021-04-19 16:48:48 +02002047 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002048 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002049 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002050 else
2051 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002052 SOURCING_LNUM = iptr->isn_lnum;
2053 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002054 {
2055 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002056 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002057 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002058 clear_tv(&di->di_tv);
2059 di->di_tv = *STACK_TV_BOT(0);
2060 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002061 }
2062 break;
2063
2064 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 case ISN_STORESCRIPT:
2066 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002067 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2068 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069
Bram Moolenaar4c137212021-04-19 16:48:48 +02002070 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002071 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002072 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002073 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002074
2075 // "const" and "final" are checked at compile time, locking
2076 // the value needs to be checked here.
2077 SOURCING_LNUM = iptr->isn_lnum;
2078 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002079 {
2080 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002081 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002082 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 clear_tv(sv->sv_tv);
2085 *sv->sv_tv = *STACK_TV_BOT(0);
2086 }
2087 break;
2088
2089 // store option
2090 case ISN_STOREOPT:
2091 {
2092 long n = 0;
2093 char_u *s = NULL;
2094 char *msg;
2095
Bram Moolenaar4c137212021-04-19 16:48:48 +02002096 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 tv = STACK_TV_BOT(0);
2098 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002099 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002101 if (s == NULL)
2102 s = (char_u *)"";
2103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002105 // must be VAR_NUMBER, CHECKTYPE makes sure
2106 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2108 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002109 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 if (msg != NULL)
2111 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002112 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002114 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 }
2117 break;
2118
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002119 // store $ENV
2120 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002121 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002122 tv = STACK_TV_BOT(0);
2123 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2124 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002125 break;
2126
2127 // store @r
2128 case ISN_STOREREG:
2129 {
2130 int reg = iptr->isn_arg.number;
2131
Bram Moolenaar4c137212021-04-19 16:48:48 +02002132 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002133 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002134 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002135 tv_get_string(tv), -1, FALSE);
2136 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002137 }
2138 break;
2139
2140 // store v: variable
2141 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002142 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002143 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2144 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002145 // should not happen, type is checked when compiling
2146 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002147 break;
2148
Bram Moolenaard3aac292020-04-19 14:32:17 +02002149 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002151 case ISN_STOREB:
2152 case ISN_STOREW:
2153 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002155 dictitem_T *di;
2156 hashtab_T *ht;
2157 char_u *name = iptr->isn_arg.string + 2;
2158
Bram Moolenaard3aac292020-04-19 14:32:17 +02002159 switch (iptr->isn_type)
2160 {
2161 case ISN_STOREG:
2162 ht = get_globvar_ht();
2163 break;
2164 case ISN_STOREB:
2165 ht = &curbuf->b_vars->dv_hashtab;
2166 break;
2167 case ISN_STOREW:
2168 ht = &curwin->w_vars->dv_hashtab;
2169 break;
2170 case ISN_STORET:
2171 ht = &curtab->tp_vars->dv_hashtab;
2172 break;
2173 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002174 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176
Bram Moolenaar4c137212021-04-19 16:48:48 +02002177 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002178 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002180 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 else
2182 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002183 SOURCING_LNUM = iptr->isn_lnum;
2184 if (var_check_permission(di, name) == FAIL)
2185 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 clear_tv(&di->di_tv);
2187 di->di_tv = *STACK_TV_BOT(0);
2188 }
2189 }
2190 break;
2191
Bram Moolenaar03290b82020-12-19 16:30:44 +01002192 // store an autoload variable
2193 case ISN_STOREAUTO:
2194 SOURCING_LNUM = iptr->isn_lnum;
2195 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2196 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002197 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002198 break;
2199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 // store number in local variable
2201 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002202 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 clear_tv(tv);
2204 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002205 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 break;
2207
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002208 // store value in list or dict variable
2209 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002210 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002211 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002212 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002213 typval_T *tv_dest = STACK_TV_BOT(-1);
2214 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002215
Bram Moolenaar752fc692021-01-04 21:57:11 +01002216 // Stack contains:
2217 // -3 value to be stored
2218 // -2 index
2219 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002220 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002221 SOURCING_LNUM = iptr->isn_lnum;
2222 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002223 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002224 dest_type = tv_dest->v_type;
2225 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002226 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002227 else if (dest_type == VAR_LIST
2228 && tv_idx->v_type != VAR_NUMBER)
2229 {
2230 emsg(_(e_number_exp));
2231 status = FAIL;
2232 }
2233 }
2234 else if (dest_type != tv_dest->v_type)
2235 {
2236 // just in case, should be OK
2237 semsg(_(e_expected_str_but_got_str),
2238 vartype_name(dest_type),
2239 vartype_name(tv_dest->v_type));
2240 status = FAIL;
2241 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002242
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002243 if (status == OK && dest_type == VAR_LIST)
2244 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002245 long lidx = (long)tv_idx->vval.v_number;
2246 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002247
2248 if (list == NULL)
2249 {
2250 emsg(_(e_list_not_set));
2251 goto on_error;
2252 }
2253 if (lidx < 0 && list->lv_len + lidx >= 0)
2254 // negative index is relative to the end
2255 lidx = list->lv_len + lidx;
2256 if (lidx < 0 || lidx > list->lv_len)
2257 {
2258 semsg(_(e_listidx), lidx);
2259 goto on_error;
2260 }
2261 if (lidx < list->lv_len)
2262 {
2263 listitem_T *li = list_find(list, lidx);
2264
2265 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002266 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002267 goto on_error;
2268 // overwrite existing list item
2269 clear_tv(&li->li_tv);
2270 li->li_tv = *tv;
2271 }
2272 else
2273 {
2274 if (error_if_locked(list->lv_lock,
2275 e_cannot_change_list))
2276 goto on_error;
2277 // append to list, only fails when out of memory
2278 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002279 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002280 clear_tv(tv);
2281 }
2282 }
2283 else if (status == OK && dest_type == VAR_DICT)
2284 {
2285 char_u *key = tv_idx->vval.v_string;
2286 dict_T *dict = tv_dest->vval.v_dict;
2287 dictitem_T *di;
2288
2289 SOURCING_LNUM = iptr->isn_lnum;
2290 if (dict == NULL)
2291 {
2292 emsg(_(e_dictionary_not_set));
2293 goto on_error;
2294 }
2295 if (key == NULL)
2296 key = (char_u *)"";
2297 di = dict_find(dict, key, -1);
2298 if (di != NULL)
2299 {
2300 if (error_if_locked(di->di_tv.v_lock,
2301 e_cannot_change_dict_item))
2302 goto on_error;
2303 // overwrite existing value
2304 clear_tv(&di->di_tv);
2305 di->di_tv = *tv;
2306 }
2307 else
2308 {
2309 if (error_if_locked(dict->dv_lock,
2310 e_cannot_change_dict))
2311 goto on_error;
2312 // add to dict, only fails when out of memory
2313 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002314 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002315 clear_tv(tv);
2316 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002317 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002318 else if (status == OK && dest_type == VAR_BLOB)
2319 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002320 long lidx = (long)tv_idx->vval.v_number;
2321 blob_T *blob = tv_dest->vval.v_blob;
2322 varnumber_T nr;
2323 int error = FALSE;
2324 int len;
2325
2326 if (blob == NULL)
2327 {
2328 emsg(_(e_blob_not_set));
2329 goto on_error;
2330 }
2331 len = blob_len(blob);
2332 if (lidx < 0 && len + lidx >= 0)
2333 // negative index is relative to the end
2334 lidx = len + lidx;
2335
2336 // Can add one byte at the end.
2337 if (lidx < 0 || lidx > len)
2338 {
2339 semsg(_(e_blobidx), lidx);
2340 goto on_error;
2341 }
2342 if (value_check_lock(blob->bv_lock,
2343 (char_u *)"blob", FALSE))
2344 goto on_error;
2345 nr = tv_get_number_chk(tv, &error);
2346 if (error)
2347 goto on_error;
2348 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002349 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002350 else
2351 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002352 status = FAIL;
2353 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002354 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002355
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002356 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002357 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002358 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002359 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002360 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002361 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002362 goto on_error;
2363 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002364 }
2365 break;
2366
Bram Moolenaar68452172021-04-12 21:21:02 +02002367 // store value in blob range
2368 case ISN_STORERANGE:
2369 {
2370 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2371 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2372 typval_T *tv_dest = STACK_TV_BOT(-1);
2373 int status = OK;
2374
2375 // Stack contains:
2376 // -4 value to be stored
2377 // -3 first index or "none"
2378 // -2 second index or "none"
2379 // -1 destination blob
2380 tv = STACK_TV_BOT(-4);
2381 if (tv_dest->v_type != VAR_BLOB)
2382 {
2383 status = FAIL;
2384 emsg(_(e_blob_required));
2385 }
2386 else
2387 {
2388 varnumber_T n1;
2389 varnumber_T n2;
2390 int error = FALSE;
2391
2392 n1 = tv_get_number_chk(tv_idx1, &error);
2393 if (error)
2394 status = FAIL;
2395 else
2396 {
2397 if (tv_idx2->v_type == VAR_SPECIAL
2398 && tv_idx2->vval.v_number == VVAL_NONE)
2399 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2400 else
2401 n2 = tv_get_number_chk(tv_idx2, &error);
2402 if (error)
2403 status = FAIL;
2404 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002405 {
2406 long bloblen = blob_len(tv_dest->vval.v_blob);
2407
2408 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002409 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002410 || check_blob_range(bloblen,
2411 n1, n2, FALSE) == FAIL)
2412 status = FAIL;
2413 else
2414 status = blob_set_range(
2415 tv_dest->vval.v_blob, n1, n2, tv);
2416 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002417 }
2418 }
2419
2420 clear_tv(tv_idx1);
2421 clear_tv(tv_idx2);
2422 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002423 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002424 clear_tv(tv);
2425
2426 if (status == FAIL)
2427 goto on_error;
2428 }
2429 break;
2430
Bram Moolenaar0186e582021-01-10 18:33:11 +01002431 // load or store variable or argument from outer scope
2432 case ISN_LOADOUTER:
2433 case ISN_STOREOUTER:
2434 {
2435 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002436 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2437 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002438
2439 while (depth > 1 && outer != NULL)
2440 {
2441 outer = outer->out_up;
2442 --depth;
2443 }
2444 if (outer == NULL)
2445 {
2446 SOURCING_LNUM = iptr->isn_lnum;
2447 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002448 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002449 }
2450 tv = ((typval_T *)outer->out_stack->ga_data)
2451 + outer->out_frame_idx + STACK_FRAME_SIZE
2452 + iptr->isn_arg.outer.outer_idx;
2453 if (iptr->isn_type == ISN_LOADOUTER)
2454 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002455 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002456 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002457 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002458 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002459 }
2460 else
2461 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002462 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002463 clear_tv(tv);
2464 *tv = *STACK_TV_BOT(0);
2465 }
2466 }
2467 break;
2468
Bram Moolenaar752fc692021-01-04 21:57:11 +01002469 // unlet item in list or dict variable
2470 case ISN_UNLETINDEX:
2471 {
2472 typval_T *tv_idx = STACK_TV_BOT(-2);
2473 typval_T *tv_dest = STACK_TV_BOT(-1);
2474 int status = OK;
2475
2476 // Stack contains:
2477 // -2 index
2478 // -1 dict or list
2479 if (tv_dest->v_type == VAR_DICT)
2480 {
2481 // unlet a dict item, index must be a string
2482 if (tv_idx->v_type != VAR_STRING)
2483 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002485 semsg(_(e_expected_str_but_got_str),
2486 vartype_name(VAR_STRING),
2487 vartype_name(tv_idx->v_type));
2488 status = FAIL;
2489 }
2490 else
2491 {
2492 dict_T *d = tv_dest->vval.v_dict;
2493 char_u *key = tv_idx->vval.v_string;
2494 dictitem_T *di = NULL;
2495
2496 if (key == NULL)
2497 key = (char_u *)"";
2498 if (d != NULL)
2499 di = dict_find(d, key, (int)STRLEN(key));
2500 if (di == NULL)
2501 {
2502 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002503 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002504 semsg(_(e_dictkey), key);
2505 status = FAIL;
2506 }
2507 else
2508 {
2509 // TODO: check for dict or item locked
2510 dictitem_remove(d, di);
2511 }
2512 }
2513 }
2514 else if (tv_dest->v_type == VAR_LIST)
2515 {
2516 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002517 SOURCING_LNUM = iptr->isn_lnum;
2518 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002519 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002520 status = FAIL;
2521 }
2522 else
2523 {
2524 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002525 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002526 listitem_T *li = NULL;
2527
2528 li = list_find(l, n);
2529 if (li == NULL)
2530 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002531 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002532 semsg(_(e_listidx), n);
2533 status = FAIL;
2534 }
2535 else
2536 // TODO: check for list or item locked
2537 listitem_remove(l, li);
2538 }
2539 }
2540 else
2541 {
2542 status = FAIL;
2543 semsg(_(e_cannot_index_str),
2544 vartype_name(tv_dest->v_type));
2545 }
2546
2547 clear_tv(tv_idx);
2548 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002549 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002550 if (status == FAIL)
2551 goto on_error;
2552 }
2553 break;
2554
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002555 // unlet range of items in list variable
2556 case ISN_UNLETRANGE:
2557 {
2558 // Stack contains:
2559 // -3 index1
2560 // -2 index2
2561 // -1 dict or list
2562 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2563 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2564 typval_T *tv_dest = STACK_TV_BOT(-1);
2565 int status = OK;
2566
2567 if (tv_dest->v_type == VAR_LIST)
2568 {
2569 // indexes must be a number
2570 SOURCING_LNUM = iptr->isn_lnum;
2571 if (check_for_number(tv_idx1) == FAIL
2572 || check_for_number(tv_idx2) == FAIL)
2573 {
2574 status = FAIL;
2575 }
2576 else
2577 {
2578 list_T *l = tv_dest->vval.v_list;
2579 long n1 = (long)tv_idx1->vval.v_number;
2580 long n2 = (long)tv_idx2->vval.v_number;
2581 listitem_T *li;
2582
2583 li = list_find_index(l, &n1);
2584 if (li == NULL
2585 || list_unlet_range(l, li, NULL, n1,
2586 TRUE, n2) == FAIL)
2587 status = FAIL;
2588 }
2589 }
2590 else
2591 {
2592 status = FAIL;
2593 SOURCING_LNUM = iptr->isn_lnum;
2594 semsg(_(e_cannot_index_str),
2595 vartype_name(tv_dest->v_type));
2596 }
2597
2598 clear_tv(tv_idx1);
2599 clear_tv(tv_idx2);
2600 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002601 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002602 if (status == FAIL)
2603 goto on_error;
2604 }
2605 break;
2606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 // push constant
2608 case ISN_PUSHNR:
2609 case ISN_PUSHBOOL:
2610 case ISN_PUSHSPEC:
2611 case ISN_PUSHF:
2612 case ISN_PUSHS:
2613 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002614 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002615 case ISN_PUSHCHANNEL:
2616 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002617 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002618 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002620 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002621 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 switch (iptr->isn_type)
2623 {
2624 case ISN_PUSHNR:
2625 tv->v_type = VAR_NUMBER;
2626 tv->vval.v_number = iptr->isn_arg.number;
2627 break;
2628 case ISN_PUSHBOOL:
2629 tv->v_type = VAR_BOOL;
2630 tv->vval.v_number = iptr->isn_arg.number;
2631 break;
2632 case ISN_PUSHSPEC:
2633 tv->v_type = VAR_SPECIAL;
2634 tv->vval.v_number = iptr->isn_arg.number;
2635 break;
2636#ifdef FEAT_FLOAT
2637 case ISN_PUSHF:
2638 tv->v_type = VAR_FLOAT;
2639 tv->vval.v_float = iptr->isn_arg.fnumber;
2640 break;
2641#endif
2642 case ISN_PUSHBLOB:
2643 blob_copy(iptr->isn_arg.blob, tv);
2644 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002645 case ISN_PUSHFUNC:
2646 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002647 if (iptr->isn_arg.string == NULL)
2648 tv->vval.v_string = NULL;
2649 else
2650 tv->vval.v_string =
2651 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002652 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002653 case ISN_PUSHCHANNEL:
2654#ifdef FEAT_JOB_CHANNEL
2655 tv->v_type = VAR_CHANNEL;
2656 tv->vval.v_channel = iptr->isn_arg.channel;
2657 if (tv->vval.v_channel != NULL)
2658 ++tv->vval.v_channel->ch_refcount;
2659#endif
2660 break;
2661 case ISN_PUSHJOB:
2662#ifdef FEAT_JOB_CHANNEL
2663 tv->v_type = VAR_JOB;
2664 tv->vval.v_job = iptr->isn_arg.job;
2665 if (tv->vval.v_job != NULL)
2666 ++tv->vval.v_job->jv_refcount;
2667#endif
2668 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 default:
2670 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002671 tv->vval.v_string = vim_strsave(
2672 iptr->isn_arg.string == NULL
2673 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 }
2675 break;
2676
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002677 case ISN_UNLET:
2678 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2679 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002680 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002681 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002682 case ISN_UNLETENV:
2683 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2684 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002685
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002686 case ISN_LOCKCONST:
2687 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2688 break;
2689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 // create a list from items on the stack; uses a single allocation
2691 // for the list header and the items
2692 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002693 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002694 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 break;
2696
2697 // create a dict from items on the stack
2698 case ISN_NEWDICT:
2699 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002700 int count = iptr->isn_arg.number;
2701 dict_T *dict = dict_alloc();
2702 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002703 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002704 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705
2706 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002707 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 for (idx = 0; idx < count; ++idx)
2709 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002710 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002712 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002713 key = tv->vval.v_string == NULL
2714 ? (char_u *)"" : tv->vval.v_string;
2715 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002716 if (item != NULL)
2717 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002718 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002719 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002720 dict_unref(dict);
2721 goto on_error;
2722 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002723 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 clear_tv(tv);
2725 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002726 {
2727 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002728 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2731 item->di_tv.v_lock = 0;
2732 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002733 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002734 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002735 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002736 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 }
2739
2740 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002741 ectx->ec_stack.ga_len -= 2 * count - 1;
2742 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002743 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002745 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 tv = STACK_TV_BOT(-1);
2747 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002748 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 tv->vval.v_dict = dict;
2750 ++dict->dv_refcount;
2751 }
2752 break;
2753
2754 // call a :def function
2755 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002756 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002757 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2758 NULL,
2759 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002760 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002761 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 break;
2763
2764 // call a builtin function
2765 case ISN_BCALL:
2766 SOURCING_LNUM = iptr->isn_lnum;
2767 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2768 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002769 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002770 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 break;
2772
2773 // call a funcref or partial
2774 case ISN_PCALL:
2775 {
2776 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2777 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002778 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779
2780 SOURCING_LNUM = iptr->isn_lnum;
2781 if (pfunc->cpf_top)
2782 {
2783 // funcref is above the arguments
2784 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2785 }
2786 else
2787 {
2788 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002789 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002790 partial_tv = *STACK_TV_BOT(0);
2791 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002793 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002794 if (tv == &partial_tv)
2795 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002797 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 }
2799 break;
2800
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002801 case ISN_PCALL_END:
2802 // PCALL finished, arguments have been consumed and replaced by
2803 // the return value. Now clear the funcref from the stack,
2804 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002805 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002806 clear_tv(STACK_TV_BOT(-1));
2807 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2808 break;
2809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 // call a user defined function or funcref/partial
2811 case ISN_UCALL:
2812 {
2813 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2814
2815 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002816 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002817 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002818 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 }
2820 break;
2821
2822 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002823 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002824 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002825 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002826 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002827 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002828 tv->v_type = VAR_NUMBER;
2829 tv->vval.v_number = 0;
2830 tv->v_lock = 0;
2831 // FALLTHROUGH
2832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 case ISN_RETURN:
2834 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002835 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002836 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002837
2838 if (trystack->ga_len > 0)
2839 trycmd = ((trycmd_T *)trystack->ga_data)
2840 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002841 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002842 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002844 // jump to ":finally" or ":endtry"
2845 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002846 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002847 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002848 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 trycmd->tcd_return = TRUE;
2850 }
2851 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002852 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 }
2854 break;
2855
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002856 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 case ISN_FUNCREF:
2858 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002859 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2860 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2861 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002864 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002865 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002866 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002867 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002868 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002869 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002870 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002871 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002872 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002874 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 tv->vval.v_partial = pt;
2876 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002877 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 }
2879 break;
2880
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002881 // Create a global function from a lambda.
2882 case ISN_NEWFUNC:
2883 {
2884 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2885
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002886 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002887 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002888 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002889 }
2890 break;
2891
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002892 // List functions
2893 case ISN_DEF:
2894 if (iptr->isn_arg.string == NULL)
2895 list_functions(NULL);
2896 else
2897 {
2898 exarg_T ea;
2899
2900 CLEAR_FIELD(ea);
2901 ea.cmd = ea.arg = iptr->isn_arg.string;
2902 define_function(&ea, NULL);
2903 }
2904 break;
2905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 // jump if a condition is met
2907 case ISN_JUMP:
2908 {
2909 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002910 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 int jump = TRUE;
2912
2913 if (when != JUMP_ALWAYS)
2914 {
2915 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002916 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002917 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002918 || when == JUMP_IF_COND_TRUE)
2919 {
2920 SOURCING_LNUM = iptr->isn_lnum;
2921 jump = tv_get_bool_chk(tv, &error);
2922 if (error)
2923 goto on_error;
2924 }
2925 else
2926 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002928 || when == JUMP_AND_KEEP_IF_FALSE
2929 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002931 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 {
2933 // drop the value from the stack
2934 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002935 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 }
2937 }
2938 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002939 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 }
2941 break;
2942
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002943 // Jump if an argument with a default value was already set and not
2944 // v:none.
2945 case ISN_JUMP_IF_ARG_SET:
2946 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2947 if (tv->v_type != VAR_UNKNOWN
2948 && !(tv->v_type == VAR_SPECIAL
2949 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002950 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002951 break;
2952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 // top of a for loop
2954 case ISN_FOR:
2955 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002956 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 typval_T *idxtv =
2958 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2959
Bram Moolenaar4c137212021-04-19 16:48:48 +02002960 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002961 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002962 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002963 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002964 list_T *list = ltv->vval.v_list;
2965
2966 // push the next item from the list
2967 ++idxtv->vval.v_number;
2968 if (list == NULL
2969 || idxtv->vval.v_number >= list->lv_len)
2970 {
2971 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002972 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2973 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002974 }
2975 else if (list->lv_first == &range_list_item)
2976 {
2977 // non-materialized range() list
2978 tv = STACK_TV_BOT(0);
2979 tv->v_type = VAR_NUMBER;
2980 tv->v_lock = 0;
2981 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002983 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002984 }
2985 else
2986 {
2987 listitem_T *li = list_find(list,
2988 idxtv->vval.v_number);
2989
2990 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002991 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002992 }
2993 }
2994 else if (ltv->v_type == VAR_STRING)
2995 {
2996 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002997
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002998 // The index is for the last byte of the previous
2999 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003000 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003001 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003002 {
3003 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003004 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3005 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003006 }
3007 else
3008 {
3009 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3010
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003011 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003012 tv = STACK_TV_BOT(0);
3013 tv->v_type = VAR_STRING;
3014 tv->vval.v_string = vim_strnsave(
3015 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003016 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003017 idxtv->vval.v_number += clen - 1;
3018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003020 else if (ltv->v_type == VAR_BLOB)
3021 {
3022 blob_T *blob = ltv->vval.v_blob;
3023
3024 // When we get here the first time make a copy of the
3025 // blob, so that the iteration still works when it is
3026 // changed.
3027 if (idxtv->vval.v_number == -1 && blob != NULL)
3028 {
3029 blob_copy(blob, ltv);
3030 blob_unref(blob);
3031 blob = ltv->vval.v_blob;
3032 }
3033
3034 // The index is for the previous byte.
3035 ++idxtv->vval.v_number;
3036 if (blob == NULL
3037 || idxtv->vval.v_number >= blob_len(blob))
3038 {
3039 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003040 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3041 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003042 }
3043 else
3044 {
3045 // Push the next byte from the blob.
3046 tv = STACK_TV_BOT(0);
3047 tv->v_type = VAR_NUMBER;
3048 tv->vval.v_number = blob_get(blob,
3049 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003050 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003051 }
3052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 else
3054 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003055 semsg(_(e_for_loop_on_str_not_supported),
3056 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003057 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 }
3059 }
3060 break;
3061
3062 // start of ":try" block
3063 case ISN_TRY:
3064 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003065 trycmd_T *trycmd = NULL;
3066
Bram Moolenaar4c137212021-04-19 16:48:48 +02003067 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003068 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003069 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3070 + ectx->ec_trystack.ga_len;
3071 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003073 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003074 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3075 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003076 trycmd->tcd_catch_idx =
3077 iptr->isn_arg.try.try_ref->try_catch;
3078 trycmd->tcd_finally_idx =
3079 iptr->isn_arg.try.try_ref->try_finally;
3080 trycmd->tcd_endtry_idx =
3081 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 }
3083 break;
3084
3085 case ISN_PUSHEXC:
3086 if (current_exception == NULL)
3087 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003088 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003090 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003092 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003093 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003095 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003097 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 tv->vval.v_string = vim_strsave(
3099 (char_u *)current_exception->value);
3100 break;
3101
3102 case ISN_CATCH:
3103 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003104 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 if (trystack->ga_len > 0)
3108 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003109 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 + trystack->ga_len - 1;
3111 trycmd->tcd_caught = TRUE;
3112 }
3113 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003114 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 catch_exception(current_exception);
3116 }
3117 break;
3118
Bram Moolenaarc150c092021-02-13 15:02:46 +01003119 case ISN_TRYCONT:
3120 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003121 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003122 trycont_T *trycont = &iptr->isn_arg.trycont;
3123 int i;
3124 trycmd_T *trycmd;
3125 int iidx = trycont->tct_where;
3126
3127 if (trystack->ga_len < trycont->tct_levels)
3128 {
3129 siemsg("TRYCONT: expected %d levels, found %d",
3130 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003131 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003132 }
3133 // Make :endtry jump to any outer try block and the last
3134 // :endtry inside the loop to the loop start.
3135 for (i = trycont->tct_levels; i > 0; --i)
3136 {
3137 trycmd = ((trycmd_T *)trystack->ga_data)
3138 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003139 // Add one to tcd_cont to be able to jump to
3140 // instruction with index zero.
3141 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003142 iidx = trycmd->tcd_finally_idx == 0
3143 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003144 }
3145 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003146 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003147 }
3148 break;
3149
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003150 case ISN_FINALLY:
3151 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003152 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003153 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3154 + trystack->ga_len - 1;
3155
3156 // Reset the index to avoid a return statement jumps here
3157 // again.
3158 trycmd->tcd_finally_idx = 0;
3159 break;
3160 }
3161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 // end of ":try" block
3163 case ISN_ENDTRY:
3164 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003165 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166
3167 if (trystack->ga_len > 0)
3168 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003169 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 --trystack->ga_len;
3172 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003173 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 trycmd = ((trycmd_T *)trystack->ga_data)
3175 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003176 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 {
3178 // discard the exception
3179 if (caught_stack == current_exception)
3180 caught_stack = caught_stack->caught;
3181 discard_current_exception();
3182 }
3183
3184 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003185 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003186
Bram Moolenaar4c137212021-04-19 16:48:48 +02003187 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003188 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003189 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003190 clear_tv(STACK_TV_BOT(0));
3191 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003192 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003193 // handling :continue: jump to outer try block or
3194 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 }
3197 }
3198 break;
3199
3200 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003201 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003202 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003203
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003204 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3205 {
3206 // throwing an exception while using "silent!" causes
3207 // the function to abort but not display an error.
3208 tv = STACK_TV_BOT(-1);
3209 clear_tv(tv);
3210 tv->v_type = VAR_NUMBER;
3211 tv->vval.v_number = 0;
3212 goto done;
3213 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003214 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003215 tv = STACK_TV_BOT(0);
3216 if (tv->vval.v_string == NULL
3217 || *skipwhite(tv->vval.v_string) == NUL)
3218 {
3219 vim_free(tv->vval.v_string);
3220 SOURCING_LNUM = iptr->isn_lnum;
3221 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003222 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003223 }
3224
3225 // Inside a "catch" we need to first discard the caught
3226 // exception.
3227 if (trystack->ga_len > 0)
3228 {
3229 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3230 + trystack->ga_len - 1;
3231 if (trycmd->tcd_caught && current_exception != NULL)
3232 {
3233 // discard the exception
3234 if (caught_stack == current_exception)
3235 caught_stack = caught_stack->caught;
3236 discard_current_exception();
3237 trycmd->tcd_caught = FALSE;
3238 }
3239 }
3240
3241 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3242 == FAIL)
3243 {
3244 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003245 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003246 }
3247 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 break;
3250
3251 // compare with special values
3252 case ISN_COMPAREBOOL:
3253 case ISN_COMPARESPECIAL:
3254 {
3255 typval_T *tv1 = STACK_TV_BOT(-2);
3256 typval_T *tv2 = STACK_TV_BOT(-1);
3257 varnumber_T arg1 = tv1->vval.v_number;
3258 varnumber_T arg2 = tv2->vval.v_number;
3259 int res;
3260
3261 switch (iptr->isn_arg.op.op_type)
3262 {
3263 case EXPR_EQUAL: res = arg1 == arg2; break;
3264 case EXPR_NEQUAL: res = arg1 != arg2; break;
3265 default: res = 0; break;
3266 }
3267
Bram Moolenaar4c137212021-04-19 16:48:48 +02003268 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 tv1->v_type = VAR_BOOL;
3270 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3271 }
3272 break;
3273
3274 // Operation with two number arguments
3275 case ISN_OPNR:
3276 case ISN_COMPARENR:
3277 {
3278 typval_T *tv1 = STACK_TV_BOT(-2);
3279 typval_T *tv2 = STACK_TV_BOT(-1);
3280 varnumber_T arg1 = tv1->vval.v_number;
3281 varnumber_T arg2 = tv2->vval.v_number;
3282 varnumber_T res;
3283
3284 switch (iptr->isn_arg.op.op_type)
3285 {
3286 case EXPR_MULT: res = arg1 * arg2; break;
3287 case EXPR_DIV: res = arg1 / arg2; break;
3288 case EXPR_REM: res = arg1 % arg2; break;
3289 case EXPR_SUB: res = arg1 - arg2; break;
3290 case EXPR_ADD: res = arg1 + arg2; break;
3291
3292 case EXPR_EQUAL: res = arg1 == arg2; break;
3293 case EXPR_NEQUAL: res = arg1 != arg2; break;
3294 case EXPR_GREATER: res = arg1 > arg2; break;
3295 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3296 case EXPR_SMALLER: res = arg1 < arg2; break;
3297 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3298 default: res = 0; break;
3299 }
3300
Bram Moolenaar4c137212021-04-19 16:48:48 +02003301 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 if (iptr->isn_type == ISN_COMPARENR)
3303 {
3304 tv1->v_type = VAR_BOOL;
3305 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3306 }
3307 else
3308 tv1->vval.v_number = res;
3309 }
3310 break;
3311
3312 // Computation with two float arguments
3313 case ISN_OPFLOAT:
3314 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003315#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 {
3317 typval_T *tv1 = STACK_TV_BOT(-2);
3318 typval_T *tv2 = STACK_TV_BOT(-1);
3319 float_T arg1 = tv1->vval.v_float;
3320 float_T arg2 = tv2->vval.v_float;
3321 float_T res = 0;
3322 int cmp = FALSE;
3323
3324 switch (iptr->isn_arg.op.op_type)
3325 {
3326 case EXPR_MULT: res = arg1 * arg2; break;
3327 case EXPR_DIV: res = arg1 / arg2; break;
3328 case EXPR_SUB: res = arg1 - arg2; break;
3329 case EXPR_ADD: res = arg1 + arg2; break;
3330
3331 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3332 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3333 case EXPR_GREATER: cmp = arg1 > arg2; break;
3334 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3335 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3336 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3337 default: cmp = 0; break;
3338 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003339 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 if (iptr->isn_type == ISN_COMPAREFLOAT)
3341 {
3342 tv1->v_type = VAR_BOOL;
3343 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3344 }
3345 else
3346 tv1->vval.v_float = res;
3347 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003348#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 break;
3350
3351 case ISN_COMPARELIST:
3352 {
3353 typval_T *tv1 = STACK_TV_BOT(-2);
3354 typval_T *tv2 = STACK_TV_BOT(-1);
3355 list_T *arg1 = tv1->vval.v_list;
3356 list_T *arg2 = tv2->vval.v_list;
3357 int cmp = FALSE;
3358 int ic = iptr->isn_arg.op.op_ic;
3359
3360 switch (iptr->isn_arg.op.op_type)
3361 {
3362 case EXPR_EQUAL: cmp =
3363 list_equal(arg1, arg2, ic, FALSE); break;
3364 case EXPR_NEQUAL: cmp =
3365 !list_equal(arg1, arg2, ic, FALSE); break;
3366 case EXPR_IS: cmp = arg1 == arg2; break;
3367 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3368 default: cmp = 0; break;
3369 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003370 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 clear_tv(tv1);
3372 clear_tv(tv2);
3373 tv1->v_type = VAR_BOOL;
3374 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3375 }
3376 break;
3377
3378 case ISN_COMPAREBLOB:
3379 {
3380 typval_T *tv1 = STACK_TV_BOT(-2);
3381 typval_T *tv2 = STACK_TV_BOT(-1);
3382 blob_T *arg1 = tv1->vval.v_blob;
3383 blob_T *arg2 = tv2->vval.v_blob;
3384 int cmp = FALSE;
3385
3386 switch (iptr->isn_arg.op.op_type)
3387 {
3388 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3389 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3390 case EXPR_IS: cmp = arg1 == arg2; break;
3391 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3392 default: cmp = 0; break;
3393 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003394 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 clear_tv(tv1);
3396 clear_tv(tv2);
3397 tv1->v_type = VAR_BOOL;
3398 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3399 }
3400 break;
3401
3402 // TODO: handle separately
3403 case ISN_COMPARESTRING:
3404 case ISN_COMPAREDICT:
3405 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 case ISN_COMPAREANY:
3407 {
3408 typval_T *tv1 = STACK_TV_BOT(-2);
3409 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003410 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 int ic = iptr->isn_arg.op.op_ic;
3412
Bram Moolenaareb26f432020-09-14 16:50:05 +02003413 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003414 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 }
3418 break;
3419
3420 case ISN_ADDLIST:
3421 case ISN_ADDBLOB:
3422 {
3423 typval_T *tv1 = STACK_TV_BOT(-2);
3424 typval_T *tv2 = STACK_TV_BOT(-1);
3425
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003426 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 if (iptr->isn_type == ISN_ADDLIST)
3428 eval_addlist(tv1, tv2);
3429 else
3430 eval_addblob(tv1, tv2);
3431 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003432 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 }
3434 break;
3435
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003436 case ISN_LISTAPPEND:
3437 {
3438 typval_T *tv1 = STACK_TV_BOT(-2);
3439 typval_T *tv2 = STACK_TV_BOT(-1);
3440 list_T *l = tv1->vval.v_list;
3441
3442 // add an item to a list
3443 if (l == NULL)
3444 {
3445 SOURCING_LNUM = iptr->isn_lnum;
3446 emsg(_(e_cannot_add_to_null_list));
3447 goto on_error;
3448 }
3449 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003450 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003451 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003452 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003453 }
3454 break;
3455
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003456 case ISN_BLOBAPPEND:
3457 {
3458 typval_T *tv1 = STACK_TV_BOT(-2);
3459 typval_T *tv2 = STACK_TV_BOT(-1);
3460 blob_T *b = tv1->vval.v_blob;
3461 int error = FALSE;
3462 varnumber_T n;
3463
3464 // add a number to a blob
3465 if (b == NULL)
3466 {
3467 SOURCING_LNUM = iptr->isn_lnum;
3468 emsg(_(e_cannot_add_to_null_blob));
3469 goto on_error;
3470 }
3471 n = tv_get_number_chk(tv2, &error);
3472 if (error)
3473 goto on_error;
3474 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003475 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003476 }
3477 break;
3478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 // Computation with two arguments of unknown type
3480 case ISN_OPANY:
3481 {
3482 typval_T *tv1 = STACK_TV_BOT(-2);
3483 typval_T *tv2 = STACK_TV_BOT(-1);
3484 varnumber_T n1, n2;
3485#ifdef FEAT_FLOAT
3486 float_T f1 = 0, f2 = 0;
3487#endif
3488 int error = FALSE;
3489
3490 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3491 {
3492 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3493 {
3494 eval_addlist(tv1, tv2);
3495 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003496 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 break;
3498 }
3499 else if (tv1->v_type == VAR_BLOB
3500 && tv2->v_type == VAR_BLOB)
3501 {
3502 eval_addblob(tv1, tv2);
3503 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003504 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 break;
3506 }
3507 }
3508#ifdef FEAT_FLOAT
3509 if (tv1->v_type == VAR_FLOAT)
3510 {
3511 f1 = tv1->vval.v_float;
3512 n1 = 0;
3513 }
3514 else
3515#endif
3516 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 n1 = tv_get_number_chk(tv1, &error);
3519 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003520 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521#ifdef FEAT_FLOAT
3522 if (tv2->v_type == VAR_FLOAT)
3523 f1 = n1;
3524#endif
3525 }
3526#ifdef FEAT_FLOAT
3527 if (tv2->v_type == VAR_FLOAT)
3528 {
3529 f2 = tv2->vval.v_float;
3530 n2 = 0;
3531 }
3532 else
3533#endif
3534 {
3535 n2 = tv_get_number_chk(tv2, &error);
3536 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003537 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538#ifdef FEAT_FLOAT
3539 if (tv1->v_type == VAR_FLOAT)
3540 f2 = n2;
3541#endif
3542 }
3543#ifdef FEAT_FLOAT
3544 // if there is a float on either side the result is a float
3545 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3546 {
3547 switch (iptr->isn_arg.op.op_type)
3548 {
3549 case EXPR_MULT: f1 = f1 * f2; break;
3550 case EXPR_DIV: f1 = f1 / f2; break;
3551 case EXPR_SUB: f1 = f1 - f2; break;
3552 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003553 default: SOURCING_LNUM = iptr->isn_lnum;
3554 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003555 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 }
3557 clear_tv(tv1);
3558 clear_tv(tv2);
3559 tv1->v_type = VAR_FLOAT;
3560 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003561 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 }
3563 else
3564#endif
3565 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003566 int failed = FALSE;
3567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 switch (iptr->isn_arg.op.op_type)
3569 {
3570 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003571 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3572 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003573 goto on_error;
3574 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 case EXPR_SUB: n1 = n1 - n2; break;
3576 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003577 default: n1 = num_modulus(n1, n2, &failed);
3578 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003579 goto on_error;
3580 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 }
3582 clear_tv(tv1);
3583 clear_tv(tv2);
3584 tv1->v_type = VAR_NUMBER;
3585 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003586 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 }
3588 }
3589 break;
3590
3591 case ISN_CONCAT:
3592 {
3593 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3594 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3595 char_u *res;
3596
3597 res = concat_str(str1, str2);
3598 clear_tv(STACK_TV_BOT(-2));
3599 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003600 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 STACK_TV_BOT(-1)->vval.v_string = res;
3602 }
3603 break;
3604
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003605 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003606 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003607 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003608 int is_slice = iptr->isn_type == ISN_STRSLICE;
3609 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003610 char_u *res;
3611
3612 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003613 // string slice: string is at stack-3, first index at
3614 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003615 if (is_slice)
3616 {
3617 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003618 n1 = tv->vval.v_number;
3619 }
3620
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003621 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003622 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003623
Bram Moolenaar4c137212021-04-19 16:48:48 +02003624 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003625 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003626 if (is_slice)
3627 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003628 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003629 else
3630 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003631 // single character (including composing characters).
3632 // If the index is too big or negative the result is
3633 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003634 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003635 vim_free(tv->vval.v_string);
3636 tv->vval.v_string = res;
3637 }
3638 break;
3639
3640 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003641 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003642 case ISN_BLOBINDEX:
3643 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003645 int is_slice = iptr->isn_type == ISN_LISTSLICE
3646 || iptr->isn_type == ISN_BLOBSLICE;
3647 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3648 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003649 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003650 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651
3652 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003653 // list slice: list is at stack-3, indexes at stack-2 and
3654 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003655 // Same for blob.
3656 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657
3658 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003659 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003661
3662 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 {
Bram Moolenaared591872020-08-15 22:14:53 +02003664 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003665 n1 = tv->vval.v_number;
3666 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 }
Bram Moolenaared591872020-08-15 22:14:53 +02003668
Bram Moolenaar4c137212021-04-19 16:48:48 +02003669 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003670 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003671 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003672 if (is_blob)
3673 {
3674 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3675 n1, n2, FALSE, tv) == FAIL)
3676 goto on_error;
3677 }
3678 else
3679 {
3680 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3681 n1, n2, FALSE, tv, TRUE) == FAIL)
3682 goto on_error;
3683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 }
3685 break;
3686
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003687 case ISN_ANYINDEX:
3688 case ISN_ANYSLICE:
3689 {
3690 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3691 typval_T *var1, *var2;
3692 int res;
3693
3694 // index: composite is at stack-2, index at stack-1
3695 // slice: composite is at stack-3, indexes at stack-2 and
3696 // stack-1
3697 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003698 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003699 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3700 goto on_error;
3701 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3702 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003703 res = eval_index_inner(tv, is_slice, var1, var2,
3704 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003705 clear_tv(var1);
3706 if (is_slice)
3707 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003708 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003709 if (res == FAIL)
3710 goto on_error;
3711 }
3712 break;
3713
Bram Moolenaar9af78762020-06-16 11:34:42 +02003714 case ISN_SLICE:
3715 {
3716 list_T *list;
3717 int count = iptr->isn_arg.number;
3718
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003719 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003720 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003721 list = tv->vval.v_list;
3722
3723 // no error for short list, expect it to be checked earlier
3724 if (list != NULL && list->lv_len >= count)
3725 {
3726 list_T *newlist = list_slice(list,
3727 count, list->lv_len - 1);
3728
3729 if (newlist != NULL)
3730 {
3731 list_unref(list);
3732 tv->vval.v_list = newlist;
3733 ++newlist->lv_refcount;
3734 }
3735 }
3736 }
3737 break;
3738
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003739 case ISN_GETITEM:
3740 {
3741 listitem_T *li;
3742 int index = iptr->isn_arg.number;
3743
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003744 // Get list item: list is at stack-1, push item.
3745 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003746 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003747 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003748
Bram Moolenaar4c137212021-04-19 16:48:48 +02003749 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003750 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003751 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003752 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003753
3754 // Useful when used in unpack assignment. Reset at
3755 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003756 ectx->ec_where.wt_index = index + 1;
3757 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003758 }
3759 break;
3760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 case ISN_MEMBER:
3762 {
3763 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003764 char_u *key;
3765 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003766 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003767
3768 // dict member: dict is at stack-2, key at stack-1
3769 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003770 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003771 dict = tv->vval.v_dict;
3772
3773 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003774 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003775 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003776 if (key == NULL)
3777 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003778
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003779 if ((di = dict_find(dict, key, -1)) == NULL)
3780 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003781 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003782 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003783
3784 // If :silent! is used we will continue, make sure the
3785 // stack contents makes sense.
3786 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003787 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003788 tv = STACK_TV_BOT(-1);
3789 clear_tv(tv);
3790 tv->v_type = VAR_NUMBER;
3791 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003792 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003793 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003794 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003795 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003796 // Clear the dict only after getting the item, to avoid
3797 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003798 tv = STACK_TV_BOT(-1);
3799 temp_tv = *tv;
3800 copy_tv(&di->di_tv, tv);
3801 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003802 }
3803 break;
3804
3805 // dict member with string key
3806 case ISN_STRINGMEMBER:
3807 {
3808 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003810 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811
3812 tv = STACK_TV_BOT(-1);
3813 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3814 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003815 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003817 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 }
3819 dict = tv->vval.v_dict;
3820
3821 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3822 == NULL)
3823 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003824 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003826 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003828 // Clear the dict after getting the item, to avoid that it
3829 // make the item invalid.
3830 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003832 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 }
3834 break;
3835
3836 case ISN_NEGATENR:
3837 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003838 if (tv->v_type != VAR_NUMBER
3839#ifdef FEAT_FLOAT
3840 && tv->v_type != VAR_FLOAT
3841#endif
3842 )
3843 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003844 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003845 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003846 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003847 }
3848#ifdef FEAT_FLOAT
3849 if (tv->v_type == VAR_FLOAT)
3850 tv->vval.v_float = -tv->vval.v_float;
3851 else
3852#endif
3853 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 break;
3855
3856 case ISN_CHECKNR:
3857 {
3858 int error = FALSE;
3859
3860 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003861 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003863 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 (void)tv_get_number_chk(tv, &error);
3865 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003866 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
3868 break;
3869
3870 case ISN_CHECKTYPE:
3871 {
3872 checktype_T *ct = &iptr->isn_arg.type;
3873
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003874 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003875 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003876 if (!ectx->ec_where.wt_variable)
3877 ectx->ec_where.wt_index = ct->ct_arg_idx;
3878 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3879 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003880 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003881 if (!ectx->ec_where.wt_variable)
3882 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003883
3884 // number 0 is FALSE, number 1 is TRUE
3885 if (tv->v_type == VAR_NUMBER
3886 && ct->ct_type->tt_type == VAR_BOOL
3887 && (tv->vval.v_number == 0
3888 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003890 tv->v_type = VAR_BOOL;
3891 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003892 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 }
3894 }
3895 break;
3896
Bram Moolenaar9af78762020-06-16 11:34:42 +02003897 case ISN_CHECKLEN:
3898 {
3899 int min_len = iptr->isn_arg.checklen.cl_min_len;
3900 list_T *list = NULL;
3901
3902 tv = STACK_TV_BOT(-1);
3903 if (tv->v_type == VAR_LIST)
3904 list = tv->vval.v_list;
3905 if (list == NULL || list->lv_len < min_len
3906 || (list->lv_len > min_len
3907 && !iptr->isn_arg.checklen.cl_more_OK))
3908 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003909 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003910 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003911 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003912 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003913 }
3914 }
3915 break;
3916
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003917 case ISN_SETTYPE:
3918 {
3919 checktype_T *ct = &iptr->isn_arg.type;
3920
3921 tv = STACK_TV_BOT(-1);
3922 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3923 {
3924 free_type(tv->vval.v_dict->dv_type);
3925 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3926 }
3927 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3928 {
3929 free_type(tv->vval.v_list->lv_type);
3930 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3931 }
3932 }
3933 break;
3934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003936 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 {
3938 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003939 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003941 if (iptr->isn_type == ISN_2BOOL)
3942 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003943 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003944 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003945 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003946 n = !n;
3947 }
3948 else
3949 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003950 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003951 SOURCING_LNUM = iptr->isn_lnum;
3952 n = tv_get_bool_chk(tv, &error);
3953 if (error)
3954 goto on_error;
3955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 clear_tv(tv);
3957 tv->v_type = VAR_BOOL;
3958 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3959 }
3960 break;
3961
3962 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003963 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003964 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003965 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3966 iptr->isn_type == ISN_2STRING_ANY,
3967 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003968 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 break;
3970
Bram Moolenaar08597872020-12-10 19:43:40 +01003971 case ISN_RANGE:
3972 {
3973 exarg_T ea;
3974 char *errormsg;
3975
Bram Moolenaarece0b872021-01-08 20:40:45 +01003976 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003977 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003978 ea.addr_type = ADDR_LINES;
3979 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003980 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003981 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003982 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003983
Bram Moolenaar4c137212021-04-19 16:48:48 +02003984 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003985 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003986 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003987 tv = STACK_TV_BOT(-1);
3988 tv->v_type = VAR_NUMBER;
3989 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003990 if (ea.addr_count == 0)
3991 tv->vval.v_number = curwin->w_cursor.lnum;
3992 else
3993 tv->vval.v_number = ea.line2;
3994 }
3995 break;
3996
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003997 case ISN_PUT:
3998 {
3999 int regname = iptr->isn_arg.put.put_regname;
4000 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4001 char_u *expr = NULL;
4002 int dir = FORWARD;
4003
Bram Moolenaar08597872020-12-10 19:43:40 +01004004 if (lnum < -2)
4005 {
4006 // line number was put on the stack by ISN_RANGE
4007 tv = STACK_TV_BOT(-1);
4008 curwin->w_cursor.lnum = tv->vval.v_number;
4009 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4010 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004011 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004012 }
4013 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004014 // :put! above cursor
4015 dir = BACKWARD;
4016 else if (lnum >= 0)
4017 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004018
4019 if (regname == '=')
4020 {
4021 tv = STACK_TV_BOT(-1);
4022 if (tv->v_type == VAR_STRING)
4023 expr = tv->vval.v_string;
4024 else
4025 {
4026 expr = typval2string(tv, TRUE); // allocates value
4027 clear_tv(tv);
4028 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004029 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004030 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004031 check_cursor();
4032 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4033 vim_free(expr);
4034 }
4035 break;
4036
Bram Moolenaar02194d22020-10-24 23:08:38 +02004037 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004038 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4039 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4040 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4041 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004042 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4043 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004044 break;
4045
Bram Moolenaar02194d22020-10-24 23:08:38 +02004046 case ISN_CMDMOD_REV:
4047 // filter regprog is owned by the instruction, don't free it
4048 cmdmod.cmod_filter_regmatch.regprog = NULL;
4049 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004050 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4051 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004052 break;
4053
Bram Moolenaar792f7862020-11-23 08:31:18 +01004054 case ISN_UNPACK:
4055 {
4056 int count = iptr->isn_arg.unpack.unp_count;
4057 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4058 list_T *l;
4059 listitem_T *li;
4060 int i;
4061
4062 // Check there is a valid list to unpack.
4063 tv = STACK_TV_BOT(-1);
4064 if (tv->v_type != VAR_LIST)
4065 {
4066 SOURCING_LNUM = iptr->isn_lnum;
4067 emsg(_(e_for_argument_must_be_sequence_of_lists));
4068 goto on_error;
4069 }
4070 l = tv->vval.v_list;
4071 if (l == NULL
4072 || l->lv_len < (semicolon ? count - 1 : count))
4073 {
4074 SOURCING_LNUM = iptr->isn_lnum;
4075 emsg(_(e_list_value_does_not_have_enough_items));
4076 goto on_error;
4077 }
4078 else if (!semicolon && l->lv_len > count)
4079 {
4080 SOURCING_LNUM = iptr->isn_lnum;
4081 emsg(_(e_list_value_has_more_items_than_targets));
4082 goto on_error;
4083 }
4084
4085 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004086 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004087 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004088 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004089
4090 // Variable after semicolon gets a list with the remaining
4091 // items.
4092 if (semicolon)
4093 {
4094 list_T *rem_list =
4095 list_alloc_with_items(l->lv_len - count + 1);
4096
4097 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004098 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004099 tv = STACK_TV_BOT(-count);
4100 tv->vval.v_list = rem_list;
4101 ++rem_list->lv_refcount;
4102 tv->v_lock = 0;
4103 li = l->lv_first;
4104 for (i = 0; i < count - 1; ++i)
4105 li = li->li_next;
4106 for (i = 0; li != NULL; ++i)
4107 {
4108 list_set_item(rem_list, i, &li->li_tv);
4109 li = li->li_next;
4110 }
4111 --count;
4112 }
4113
4114 // Produce the values in reverse order, first item last.
4115 li = l->lv_first;
4116 for (i = 0; i < count; ++i)
4117 {
4118 tv = STACK_TV_BOT(-i - 1);
4119 copy_tv(&li->li_tv, tv);
4120 li = li->li_next;
4121 }
4122
4123 list_unref(l);
4124 }
4125 break;
4126
Bram Moolenaarb2049902021-01-24 12:53:53 +01004127 case ISN_PROF_START:
4128 case ISN_PROF_END:
4129 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004130#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004131 funccall_T cookie;
4132 ufunc_T *cur_ufunc =
4133 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004134 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004135
4136 cookie.func = cur_ufunc;
4137 if (iptr->isn_type == ISN_PROF_START)
4138 {
4139 func_line_start(&cookie, iptr->isn_lnum);
4140 // if we get here the instruction is executed
4141 func_line_exec(&cookie);
4142 }
4143 else
4144 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004145#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004146 }
4147 break;
4148
Bram Moolenaare99d4222021-06-13 14:01:26 +02004149 case ISN_DEBUG:
4150 if (ex_nesting_level <= debug_break_level)
4151 {
4152 char_u *line;
4153 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
4154 + ectx->ec_dfunc_idx)->df_ufunc;
4155
4156 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004157 debug_context = ectx;
4158 debug_arg_count = iptr->isn_arg.number;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004159 line = ((char_u **)ufunc->uf_lines.ga_data)[
4160 iptr->isn_lnum - 1];
4161 if (line == NULL)
4162 line = (char_u *)"[empty]";
4163 do_debug(line);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004164 debug_context = NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004165 }
4166 break;
4167
Bram Moolenaar389df252020-07-09 21:20:47 +02004168 case ISN_SHUFFLE:
4169 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004170 typval_T tmp_tv;
4171 int item = iptr->isn_arg.shuffle.shfl_item;
4172 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004173
4174 tmp_tv = *STACK_TV_BOT(-item);
4175 for ( ; up > 0 && item > 1; --up)
4176 {
4177 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4178 --item;
4179 }
4180 *STACK_TV_BOT(-item) = tmp_tv;
4181 }
4182 break;
4183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004185 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004187 ectx->ec_where.wt_index = 0;
4188 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 break;
4190 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004191 continue;
4192
Bram Moolenaard032f342020-07-18 18:13:02 +02004193func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004194 // Restore previous function. If the frame pointer is where we started
4195 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004196 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004197 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004198
Bram Moolenaar4c137212021-04-19 16:48:48 +02004199 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004200 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004201 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004202 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004203
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004204on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004205 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004206 // If "emsg_silent" is set then ignore the error, unless it was set
4207 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004208 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004209 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004210 {
4211 // If a sequence of instructions causes an error while ":silent!"
4212 // was used, restore the stack length and jump ahead to restoring
4213 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004214 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004215 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004216 while (ectx->ec_stack.ga_len
4217 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004218 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004219 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004220 clear_tv(STACK_TV_BOT(0));
4221 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004222 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4223 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004224 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004225 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004226 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004227on_fatal_error:
4228 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004229 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004230 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004231 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 }
4233
4234done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004235 ret = OK;
4236theend:
4237 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4238 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004239}
4240
4241/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004242 * Execute the instructions from a VAR_INSTR typeval and put the result in
4243 * "rettv".
4244 * Return OK or FAIL.
4245 */
4246 int
4247exe_typval_instr(typval_T *tv, typval_T *rettv)
4248{
4249 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4250 isn_T *save_instr = ectx->ec_instr;
4251 int save_iidx = ectx->ec_iidx;
4252 int res;
4253
4254 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4255 res = exec_instructions(ectx);
4256 if (res == OK)
4257 {
4258 *rettv = *STACK_TV_BOT(-1);
4259 --ectx->ec_stack.ga_len;
4260 }
4261
4262 ectx->ec_instr = save_instr;
4263 ectx->ec_iidx = save_iidx;
4264
4265 return res;
4266}
4267
4268/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004269 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4270 * "substitute_instr".
4271 */
4272 char_u *
4273exe_substitute_instr(void)
4274{
4275 ectx_T *ectx = substitute_instr->subs_ectx;
4276 isn_T *save_instr = ectx->ec_instr;
4277 int save_iidx = ectx->ec_iidx;
4278 char_u *res;
4279
4280 ectx->ec_instr = substitute_instr->subs_instr;
4281 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004282 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004283 typval_T *tv = STACK_TV_BOT(-1);
4284
Bram Moolenaar27523602021-06-05 21:36:19 +02004285 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004286 --ectx->ec_stack.ga_len;
4287 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004288 }
4289 else
4290 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004291 substitute_instr->subs_status = FAIL;
4292 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294
Bram Moolenaar4c137212021-04-19 16:48:48 +02004295 ectx->ec_instr = save_instr;
4296 ectx->ec_iidx = save_iidx;
4297
4298 return res;
4299}
4300
4301/*
4302 * Call a "def" function from old Vim script.
4303 * Return OK or FAIL.
4304 */
4305 int
4306call_def_function(
4307 ufunc_T *ufunc,
4308 int argc_arg, // nr of arguments
4309 typval_T *argv, // arguments
4310 partial_T *partial, // optional partial for context
4311 typval_T *rettv) // return value
4312{
4313 ectx_T ectx; // execution context
4314 int argc = argc_arg;
4315 typval_T *tv;
4316 int idx;
4317 int ret = FAIL;
4318 int defcount = ufunc->uf_args.ga_len - argc;
4319 sctx_T save_current_sctx = current_sctx;
4320 int did_emsg_before = did_emsg_cumul + did_emsg;
4321 int save_suppress_errthrow = suppress_errthrow;
4322 msglist_T **saved_msg_list = NULL;
4323 msglist_T *private_msg_list = NULL;
4324 int save_emsg_silent_def = emsg_silent_def;
4325 int save_did_emsg_def = did_emsg_def;
4326 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004327 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004328
4329// Get pointer to item in the stack.
4330#undef STACK_TV
4331#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4332
4333// Get pointer to item at the bottom of the stack, -1 is the bottom.
4334#undef STACK_TV_BOT
4335#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4336
4337// Get pointer to a local variable on the stack. Negative for arguments.
4338#undef STACK_TV_VAR
4339#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4340
4341 if (ufunc->uf_def_status == UF_NOT_COMPILED
4342 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004343 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4344 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004345 == FAIL))
4346 {
4347 if (did_emsg_cumul + did_emsg == did_emsg_before)
4348 semsg(_(e_function_is_not_compiled_str),
4349 printable_func_name(ufunc));
4350 return FAIL;
4351 }
4352
4353 {
4354 // Check the function was really compiled.
4355 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4356 + ufunc->uf_dfunc_idx;
4357 if (INSTRUCTIONS(dfunc) == NULL)
4358 {
4359 iemsg("using call_def_function() on not compiled function");
4360 return FAIL;
4361 }
4362 }
4363
4364 // If depth of calling is getting too high, don't execute the function.
4365 orig_funcdepth = funcdepth_get();
4366 if (funcdepth_increment() == FAIL)
4367 return FAIL;
4368
4369 CLEAR_FIELD(ectx);
4370 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4371 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4372 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4373 {
4374 funcdepth_decrement();
4375 return FAIL;
4376 }
4377 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4378 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4379 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004380 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004381
4382 idx = argc - ufunc->uf_args.ga_len;
4383 if (idx > 0 && ufunc->uf_va_name == NULL)
4384 {
4385 if (idx == 1)
4386 emsg(_(e_one_argument_too_many));
4387 else
4388 semsg(_(e_nr_arguments_too_many), idx);
4389 goto failed_early;
4390 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004391 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4392 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004393 {
4394 if (idx == -1)
4395 emsg(_(e_one_argument_too_few));
4396 else
4397 semsg(_(e_nr_arguments_too_few), -idx);
4398 goto failed_early;
4399 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004400
4401 // Put arguments on the stack, but no more than what the function expects.
4402 // A lambda can be called with more arguments than it uses.
4403 for (idx = 0; idx < argc
4404 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4405 ++idx)
4406 {
4407 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4408 && argv[idx].v_type == VAR_SPECIAL
4409 && argv[idx].vval.v_number == VVAL_NONE)
4410 {
4411 // Use the default value.
4412 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4413 }
4414 else
4415 {
4416 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4417 && check_typval_arg_type(
4418 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4419 goto failed_early;
4420 copy_tv(&argv[idx], STACK_TV_BOT(0));
4421 }
4422 ++ectx.ec_stack.ga_len;
4423 }
4424
4425 // Turn varargs into a list. Empty list if no args.
4426 if (ufunc->uf_va_name != NULL)
4427 {
4428 int vararg_count = argc - ufunc->uf_args.ga_len;
4429
4430 if (vararg_count < 0)
4431 vararg_count = 0;
4432 else
4433 argc -= vararg_count;
4434 if (exe_newlist(vararg_count, &ectx) == FAIL)
4435 goto failed_early;
4436
4437 // Check the type of the list items.
4438 tv = STACK_TV_BOT(-1);
4439 if (ufunc->uf_va_type != NULL
4440 && ufunc->uf_va_type != &t_list_any
4441 && ufunc->uf_va_type->tt_member != &t_any
4442 && tv->vval.v_list != NULL)
4443 {
4444 type_T *expected = ufunc->uf_va_type->tt_member;
4445 listitem_T *li = tv->vval.v_list->lv_first;
4446
4447 for (idx = 0; idx < vararg_count; ++idx)
4448 {
4449 if (check_typval_arg_type(expected, &li->li_tv,
4450 argc + idx + 1) == FAIL)
4451 goto failed_early;
4452 li = li->li_next;
4453 }
4454 }
4455
4456 if (defcount > 0)
4457 // Move varargs list to below missing default arguments.
4458 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4459 --ectx.ec_stack.ga_len;
4460 }
4461
4462 // Make space for omitted arguments, will store default value below.
4463 // Any varargs list goes after them.
4464 if (defcount > 0)
4465 for (idx = 0; idx < defcount; ++idx)
4466 {
4467 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4468 ++ectx.ec_stack.ga_len;
4469 }
4470 if (ufunc->uf_va_name != NULL)
4471 ++ectx.ec_stack.ga_len;
4472
4473 // Frame pointer points to just after arguments.
4474 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4475 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4476
4477 {
4478 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4479 + ufunc->uf_dfunc_idx;
4480 ufunc_T *base_ufunc = dfunc->df_ufunc;
4481
4482 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4483 // by copy_func().
4484 if (partial != NULL || base_ufunc->uf_partial != NULL)
4485 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004486 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4487 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004488 goto failed_early;
4489 if (partial != NULL)
4490 {
4491 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4492 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004493 if (current_ectx->ec_outer_ref != NULL
4494 && current_ectx->ec_outer_ref->or_outer != NULL)
4495 ectx.ec_outer_ref->or_outer =
4496 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004497 }
4498 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004499 {
4500 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4501 ++partial->pt_refcount;
4502 ectx.ec_outer_ref->or_partial = partial;
4503 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004504 }
4505 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004506 {
4507 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4508 ++base_ufunc->uf_partial->pt_refcount;
4509 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4510 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004511 }
4512 }
4513
4514 // dummy frame entries
4515 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4516 {
4517 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4518 ++ectx.ec_stack.ga_len;
4519 }
4520
4521 {
4522 // Reserve space for local variables and any closure reference count.
4523 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4524 + ufunc->uf_dfunc_idx;
4525
4526 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4527 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4528 ectx.ec_stack.ga_len += dfunc->df_varcount;
4529 if (dfunc->df_has_closure)
4530 {
4531 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4532 STACK_TV_VAR(idx)->vval.v_number = 0;
4533 ++ectx.ec_stack.ga_len;
4534 }
4535
4536 ectx.ec_instr = INSTRUCTIONS(dfunc);
4537 }
4538
4539 // Following errors are in the function, not the caller.
4540 // Commands behave like vim9script.
4541 estack_push_ufunc(ufunc, 1);
4542 current_sctx = ufunc->uf_script_ctx;
4543 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4544
4545 // Use a specific location for storing error messages to be converted to an
4546 // exception.
4547 saved_msg_list = msg_list;
4548 msg_list = &private_msg_list;
4549
4550 // Do turn errors into exceptions.
4551 suppress_errthrow = FALSE;
4552
4553 // Do not delete the function while executing it.
4554 ++ufunc->uf_calls;
4555
4556 // When ":silent!" was used before calling then we still abort the
4557 // function. If ":silent!" is used in the function then we don't.
4558 emsg_silent_def = emsg_silent;
4559 did_emsg_def = 0;
4560
4561 ectx.ec_where.wt_index = 0;
4562 ectx.ec_where.wt_variable = FALSE;
4563
4564 // Execute the instructions until done.
4565 ret = exec_instructions(&ectx);
4566 if (ret == OK)
4567 {
4568 // function finished, get result from the stack.
4569 if (ufunc->uf_ret_type == &t_void)
4570 {
4571 rettv->v_type = VAR_VOID;
4572 }
4573 else
4574 {
4575 tv = STACK_TV_BOT(-1);
4576 *rettv = *tv;
4577 tv->v_type = VAR_UNKNOWN;
4578 }
4579 }
4580
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004581 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004582 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4583 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004584
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004585 // Deal with any remaining closures, they may be in use somewhere.
4586 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004587 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004588 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004589 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4590 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004591
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004592 estack_pop();
4593 current_sctx = save_current_sctx;
4594
Bram Moolenaarc970e422021-03-17 15:03:04 +01004595 // TODO: when is it safe to delete the function if it is no longer used?
4596 --ufunc->uf_calls;
4597
Bram Moolenaar352134b2020-10-17 22:04:08 +02004598 if (*msg_list != NULL && saved_msg_list != NULL)
4599 {
4600 msglist_T **plist = saved_msg_list;
4601
4602 // Append entries from the current msg_list (uncaught exceptions) to
4603 // the saved msg_list.
4604 while (*plist != NULL)
4605 plist = &(*plist)->next;
4606
4607 *plist = *msg_list;
4608 }
4609 msg_list = saved_msg_list;
4610
Bram Moolenaar4c137212021-04-19 16:48:48 +02004611 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004612 {
4613 cmdmod.cmod_filter_regmatch.regprog = NULL;
4614 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004615 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004616 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004617 emsg_silent_def = save_emsg_silent_def;
4618 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004619
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004620failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004621 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4623 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004624 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004627 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004628 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004629 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004630 if (ectx.ec_outer_ref->or_outer_allocated)
4631 vim_free(ectx.ec_outer_ref->or_outer);
4632 partial_unref(ectx.ec_outer_ref->or_partial);
4633 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004634 }
4635
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004636 // Not sure if this is necessary.
4637 suppress_errthrow = save_suppress_errthrow;
4638
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004639 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004640 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004641 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004642 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 return ret;
4644}
4645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004647 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4648 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4649 * "pfx" is prefixed to every line.
4650 */
4651 static void
4652list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4653{
4654 int line_idx = 0;
4655 int prev_current = 0;
4656 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004657 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004658
4659 for (current = 0; current < instr_count; ++current)
4660 {
4661 isn_T *iptr = &instr[current];
4662 char *line;
4663
4664 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004665 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004666 while (line_idx < iptr->isn_lnum
4667 && line_idx < ufunc->uf_lines.ga_len)
4668 {
4669 if (current > prev_current)
4670 {
4671 msg_puts("\n\n");
4672 prev_current = current;
4673 }
4674 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4675 if (line != NULL)
4676 msg(line);
4677 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004678 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4679 {
4680 int first_def_arg = ufunc->uf_args.ga_len
4681 - ufunc->uf_def_args.ga_len;
4682
4683 if (def_arg_idx > 0)
4684 msg_puts("\n\n");
4685 msg_start();
4686 msg_puts(" ");
4687 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4688 first_def_arg + def_arg_idx]);
4689 msg_puts(" = ");
4690 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4691 msg_clr_eos();
4692 msg_end();
4693 }
4694 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004695
4696 switch (iptr->isn_type)
4697 {
4698 case ISN_EXEC:
4699 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4700 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004701 case ISN_EXEC_SPLIT:
4702 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4703 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004704 case ISN_LEGACY_EVAL:
4705 smsg("%s%4d EVAL legacy %s", pfx, current,
4706 iptr->isn_arg.string);
4707 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004708 case ISN_REDIRSTART:
4709 smsg("%s%4d REDIR", pfx, current);
4710 break;
4711 case ISN_REDIREND:
4712 smsg("%s%4d REDIR END%s", pfx, current,
4713 iptr->isn_arg.number ? " append" : "");
4714 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004715 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004716#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004717 smsg("%s%4d CEXPR pre %s", pfx, current,
4718 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004719#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004720 break;
4721 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004722#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004723 {
4724 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4725
4726 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4727 cexpr_get_auname(cer->cer_cmdidx),
4728 cer->cer_forceit ? "!" : "",
4729 cer->cer_cmdline);
4730 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004731#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004732 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004733 case ISN_INSTR:
4734 {
4735 smsg("%s%4d INSTR", pfx, current);
4736 list_instructions(" ", iptr->isn_arg.instr,
4737 INT_MAX, NULL);
4738 msg(" -------------");
4739 }
4740 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004741 case ISN_SUBSTITUTE:
4742 {
4743 subs_T *subs = &iptr->isn_arg.subs;
4744
4745 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4746 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4747 msg(" -------------");
4748 }
4749 break;
4750 case ISN_EXECCONCAT:
4751 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4752 (varnumber_T)iptr->isn_arg.number);
4753 break;
4754 case ISN_ECHO:
4755 {
4756 echo_T *echo = &iptr->isn_arg.echo;
4757
4758 smsg("%s%4d %s %d", pfx, current,
4759 echo->echo_with_white ? "ECHO" : "ECHON",
4760 echo->echo_count);
4761 }
4762 break;
4763 case ISN_EXECUTE:
4764 smsg("%s%4d EXECUTE %lld", pfx, current,
4765 (varnumber_T)(iptr->isn_arg.number));
4766 break;
4767 case ISN_ECHOMSG:
4768 smsg("%s%4d ECHOMSG %lld", pfx, current,
4769 (varnumber_T)(iptr->isn_arg.number));
4770 break;
4771 case ISN_ECHOERR:
4772 smsg("%s%4d ECHOERR %lld", pfx, current,
4773 (varnumber_T)(iptr->isn_arg.number));
4774 break;
4775 case ISN_LOAD:
4776 {
4777 if (iptr->isn_arg.number < 0)
4778 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4779 (varnumber_T)(iptr->isn_arg.number
4780 + STACK_FRAME_SIZE));
4781 else
4782 smsg("%s%4d LOAD $%lld", pfx, current,
4783 (varnumber_T)(iptr->isn_arg.number));
4784 }
4785 break;
4786 case ISN_LOADOUTER:
4787 {
4788 if (iptr->isn_arg.number < 0)
4789 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4790 iptr->isn_arg.outer.outer_depth,
4791 iptr->isn_arg.outer.outer_idx
4792 + STACK_FRAME_SIZE);
4793 else
4794 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4795 iptr->isn_arg.outer.outer_depth,
4796 iptr->isn_arg.outer.outer_idx);
4797 }
4798 break;
4799 case ISN_LOADV:
4800 smsg("%s%4d LOADV v:%s", pfx, current,
4801 get_vim_var_name(iptr->isn_arg.number));
4802 break;
4803 case ISN_LOADSCRIPT:
4804 {
4805 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4806 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4807 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4808 + sref->sref_idx;
4809
4810 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4811 sv->sv_name,
4812 sref->sref_idx,
4813 si->sn_name);
4814 }
4815 break;
4816 case ISN_LOADS:
4817 {
4818 scriptitem_T *si = SCRIPT_ITEM(
4819 iptr->isn_arg.loadstore.ls_sid);
4820
4821 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4822 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4823 }
4824 break;
4825 case ISN_LOADAUTO:
4826 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4827 break;
4828 case ISN_LOADG:
4829 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4830 break;
4831 case ISN_LOADB:
4832 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4833 break;
4834 case ISN_LOADW:
4835 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4836 break;
4837 case ISN_LOADT:
4838 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4839 break;
4840 case ISN_LOADGDICT:
4841 smsg("%s%4d LOAD g:", pfx, current);
4842 break;
4843 case ISN_LOADBDICT:
4844 smsg("%s%4d LOAD b:", pfx, current);
4845 break;
4846 case ISN_LOADWDICT:
4847 smsg("%s%4d LOAD w:", pfx, current);
4848 break;
4849 case ISN_LOADTDICT:
4850 smsg("%s%4d LOAD t:", pfx, current);
4851 break;
4852 case ISN_LOADOPT:
4853 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4854 break;
4855 case ISN_LOADENV:
4856 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4857 break;
4858 case ISN_LOADREG:
4859 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4860 break;
4861
4862 case ISN_STORE:
4863 if (iptr->isn_arg.number < 0)
4864 smsg("%s%4d STORE arg[%lld]", pfx, current,
4865 iptr->isn_arg.number + STACK_FRAME_SIZE);
4866 else
4867 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4868 break;
4869 case ISN_STOREOUTER:
4870 {
4871 if (iptr->isn_arg.number < 0)
4872 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4873 iptr->isn_arg.outer.outer_depth,
4874 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4875 else
4876 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4877 iptr->isn_arg.outer.outer_depth,
4878 iptr->isn_arg.outer.outer_idx);
4879 }
4880 break;
4881 case ISN_STOREV:
4882 smsg("%s%4d STOREV v:%s", pfx, current,
4883 get_vim_var_name(iptr->isn_arg.number));
4884 break;
4885 case ISN_STOREAUTO:
4886 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4887 break;
4888 case ISN_STOREG:
4889 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4890 break;
4891 case ISN_STOREB:
4892 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4893 break;
4894 case ISN_STOREW:
4895 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4896 break;
4897 case ISN_STORET:
4898 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4899 break;
4900 case ISN_STORES:
4901 {
4902 scriptitem_T *si = SCRIPT_ITEM(
4903 iptr->isn_arg.loadstore.ls_sid);
4904
4905 smsg("%s%4d STORES %s in %s", pfx, current,
4906 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4907 }
4908 break;
4909 case ISN_STORESCRIPT:
4910 {
4911 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4912 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4913 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4914 + sref->sref_idx;
4915
4916 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4917 sv->sv_name,
4918 sref->sref_idx,
4919 si->sn_name);
4920 }
4921 break;
4922 case ISN_STOREOPT:
4923 smsg("%s%4d STOREOPT &%s", pfx, current,
4924 iptr->isn_arg.storeopt.so_name);
4925 break;
4926 case ISN_STOREENV:
4927 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4928 break;
4929 case ISN_STOREREG:
4930 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4931 break;
4932 case ISN_STORENR:
4933 smsg("%s%4d STORE %lld in $%d", pfx, current,
4934 iptr->isn_arg.storenr.stnr_val,
4935 iptr->isn_arg.storenr.stnr_idx);
4936 break;
4937
4938 case ISN_STOREINDEX:
4939 smsg("%s%4d STOREINDEX %s", pfx, current,
4940 vartype_name(iptr->isn_arg.vartype));
4941 break;
4942
4943 case ISN_STORERANGE:
4944 smsg("%s%4d STORERANGE", pfx, current);
4945 break;
4946
4947 // constants
4948 case ISN_PUSHNR:
4949 smsg("%s%4d PUSHNR %lld", pfx, current,
4950 (varnumber_T)(iptr->isn_arg.number));
4951 break;
4952 case ISN_PUSHBOOL:
4953 case ISN_PUSHSPEC:
4954 smsg("%s%4d PUSH %s", pfx, current,
4955 get_var_special_name(iptr->isn_arg.number));
4956 break;
4957 case ISN_PUSHF:
4958#ifdef FEAT_FLOAT
4959 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4960#endif
4961 break;
4962 case ISN_PUSHS:
4963 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4964 break;
4965 case ISN_PUSHBLOB:
4966 {
4967 char_u *r;
4968 char_u numbuf[NUMBUFLEN];
4969 char_u *tofree;
4970
4971 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4972 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4973 vim_free(tofree);
4974 }
4975 break;
4976 case ISN_PUSHFUNC:
4977 {
4978 char *name = (char *)iptr->isn_arg.string;
4979
4980 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4981 name == NULL ? "[none]" : name);
4982 }
4983 break;
4984 case ISN_PUSHCHANNEL:
4985#ifdef FEAT_JOB_CHANNEL
4986 {
4987 channel_T *channel = iptr->isn_arg.channel;
4988
4989 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4990 channel == NULL ? 0 : channel->ch_id);
4991 }
4992#endif
4993 break;
4994 case ISN_PUSHJOB:
4995#ifdef FEAT_JOB_CHANNEL
4996 {
4997 typval_T tv;
4998 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004999 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005000
5001 tv.v_type = VAR_JOB;
5002 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005003 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005004 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5005 }
5006#endif
5007 break;
5008 case ISN_PUSHEXC:
5009 smsg("%s%4d PUSH v:exception", pfx, current);
5010 break;
5011 case ISN_UNLET:
5012 smsg("%s%4d UNLET%s %s", pfx, current,
5013 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5014 iptr->isn_arg.unlet.ul_name);
5015 break;
5016 case ISN_UNLETENV:
5017 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5018 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5019 iptr->isn_arg.unlet.ul_name);
5020 break;
5021 case ISN_UNLETINDEX:
5022 smsg("%s%4d UNLETINDEX", pfx, current);
5023 break;
5024 case ISN_UNLETRANGE:
5025 smsg("%s%4d UNLETRANGE", pfx, current);
5026 break;
5027 case ISN_LOCKCONST:
5028 smsg("%s%4d LOCKCONST", pfx, current);
5029 break;
5030 case ISN_NEWLIST:
5031 smsg("%s%4d NEWLIST size %lld", pfx, current,
5032 (varnumber_T)(iptr->isn_arg.number));
5033 break;
5034 case ISN_NEWDICT:
5035 smsg("%s%4d NEWDICT size %lld", pfx, current,
5036 (varnumber_T)(iptr->isn_arg.number));
5037 break;
5038
5039 // function call
5040 case ISN_BCALL:
5041 {
5042 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5043
5044 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5045 internal_func_name(cbfunc->cbf_idx),
5046 cbfunc->cbf_argcount);
5047 }
5048 break;
5049 case ISN_DCALL:
5050 {
5051 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5052 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5053 + cdfunc->cdf_idx;
5054
5055 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5056 df->df_ufunc->uf_name_exp != NULL
5057 ? df->df_ufunc->uf_name_exp
5058 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5059 }
5060 break;
5061 case ISN_UCALL:
5062 {
5063 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5064
5065 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5066 cufunc->cuf_name, cufunc->cuf_argcount);
5067 }
5068 break;
5069 case ISN_PCALL:
5070 {
5071 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5072
5073 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5074 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5075 }
5076 break;
5077 case ISN_PCALL_END:
5078 smsg("%s%4d PCALL end", pfx, current);
5079 break;
5080 case ISN_RETURN:
5081 smsg("%s%4d RETURN", pfx, current);
5082 break;
5083 case ISN_RETURN_ZERO:
5084 smsg("%s%4d RETURN 0", pfx, current);
5085 break;
5086 case ISN_FUNCREF:
5087 {
5088 funcref_T *funcref = &iptr->isn_arg.funcref;
5089 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5090 + funcref->fr_func;
5091
5092 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5093 }
5094 break;
5095
5096 case ISN_NEWFUNC:
5097 {
5098 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5099
5100 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5101 newfunc->nf_lambda, newfunc->nf_global);
5102 }
5103 break;
5104
5105 case ISN_DEF:
5106 {
5107 char_u *name = iptr->isn_arg.string;
5108
5109 smsg("%s%4d DEF %s", pfx, current,
5110 name == NULL ? (char_u *)"" : name);
5111 }
5112 break;
5113
5114 case ISN_JUMP:
5115 {
5116 char *when = "?";
5117
5118 switch (iptr->isn_arg.jump.jump_when)
5119 {
5120 case JUMP_ALWAYS:
5121 when = "JUMP";
5122 break;
5123 case JUMP_AND_KEEP_IF_TRUE:
5124 when = "JUMP_AND_KEEP_IF_TRUE";
5125 break;
5126 case JUMP_IF_FALSE:
5127 when = "JUMP_IF_FALSE";
5128 break;
5129 case JUMP_AND_KEEP_IF_FALSE:
5130 when = "JUMP_AND_KEEP_IF_FALSE";
5131 break;
5132 case JUMP_IF_COND_FALSE:
5133 when = "JUMP_IF_COND_FALSE";
5134 break;
5135 case JUMP_IF_COND_TRUE:
5136 when = "JUMP_IF_COND_TRUE";
5137 break;
5138 }
5139 smsg("%s%4d %s -> %d", pfx, current, when,
5140 iptr->isn_arg.jump.jump_where);
5141 }
5142 break;
5143
5144 case ISN_JUMP_IF_ARG_SET:
5145 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5146 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5147 iptr->isn_arg.jump.jump_where);
5148 break;
5149
5150 case ISN_FOR:
5151 {
5152 forloop_T *forloop = &iptr->isn_arg.forloop;
5153
5154 smsg("%s%4d FOR $%d -> %d", pfx, current,
5155 forloop->for_idx, forloop->for_end);
5156 }
5157 break;
5158
5159 case ISN_TRY:
5160 {
5161 try_T *try = &iptr->isn_arg.try;
5162
5163 if (try->try_ref->try_finally == 0)
5164 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5165 pfx, current,
5166 try->try_ref->try_catch,
5167 try->try_ref->try_endtry);
5168 else
5169 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5170 pfx, current,
5171 try->try_ref->try_catch,
5172 try->try_ref->try_finally,
5173 try->try_ref->try_endtry);
5174 }
5175 break;
5176 case ISN_CATCH:
5177 // TODO
5178 smsg("%s%4d CATCH", pfx, current);
5179 break;
5180 case ISN_TRYCONT:
5181 {
5182 trycont_T *trycont = &iptr->isn_arg.trycont;
5183
5184 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5185 trycont->tct_levels,
5186 trycont->tct_levels == 1 ? "" : "s",
5187 trycont->tct_where);
5188 }
5189 break;
5190 case ISN_FINALLY:
5191 smsg("%s%4d FINALLY", pfx, current);
5192 break;
5193 case ISN_ENDTRY:
5194 smsg("%s%4d ENDTRY", pfx, current);
5195 break;
5196 case ISN_THROW:
5197 smsg("%s%4d THROW", pfx, current);
5198 break;
5199
5200 // expression operations on number
5201 case ISN_OPNR:
5202 case ISN_OPFLOAT:
5203 case ISN_OPANY:
5204 {
5205 char *what;
5206 char *ins;
5207
5208 switch (iptr->isn_arg.op.op_type)
5209 {
5210 case EXPR_MULT: what = "*"; break;
5211 case EXPR_DIV: what = "/"; break;
5212 case EXPR_REM: what = "%"; break;
5213 case EXPR_SUB: what = "-"; break;
5214 case EXPR_ADD: what = "+"; break;
5215 default: what = "???"; break;
5216 }
5217 switch (iptr->isn_type)
5218 {
5219 case ISN_OPNR: ins = "OPNR"; break;
5220 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5221 case ISN_OPANY: ins = "OPANY"; break;
5222 default: ins = "???"; break;
5223 }
5224 smsg("%s%4d %s %s", pfx, current, ins, what);
5225 }
5226 break;
5227
5228 case ISN_COMPAREBOOL:
5229 case ISN_COMPARESPECIAL:
5230 case ISN_COMPARENR:
5231 case ISN_COMPAREFLOAT:
5232 case ISN_COMPARESTRING:
5233 case ISN_COMPAREBLOB:
5234 case ISN_COMPARELIST:
5235 case ISN_COMPAREDICT:
5236 case ISN_COMPAREFUNC:
5237 case ISN_COMPAREANY:
5238 {
5239 char *p;
5240 char buf[10];
5241 char *type;
5242
5243 switch (iptr->isn_arg.op.op_type)
5244 {
5245 case EXPR_EQUAL: p = "=="; break;
5246 case EXPR_NEQUAL: p = "!="; break;
5247 case EXPR_GREATER: p = ">"; break;
5248 case EXPR_GEQUAL: p = ">="; break;
5249 case EXPR_SMALLER: p = "<"; break;
5250 case EXPR_SEQUAL: p = "<="; break;
5251 case EXPR_MATCH: p = "=~"; break;
5252 case EXPR_IS: p = "is"; break;
5253 case EXPR_ISNOT: p = "isnot"; break;
5254 case EXPR_NOMATCH: p = "!~"; break;
5255 default: p = "???"; break;
5256 }
5257 STRCPY(buf, p);
5258 if (iptr->isn_arg.op.op_ic == TRUE)
5259 strcat(buf, "?");
5260 switch(iptr->isn_type)
5261 {
5262 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5263 case ISN_COMPARESPECIAL:
5264 type = "COMPARESPECIAL"; break;
5265 case ISN_COMPARENR: type = "COMPARENR"; break;
5266 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5267 case ISN_COMPARESTRING:
5268 type = "COMPARESTRING"; break;
5269 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5270 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5271 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5272 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5273 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5274 default: type = "???"; break;
5275 }
5276
5277 smsg("%s%4d %s %s", pfx, current, type, buf);
5278 }
5279 break;
5280
5281 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5282 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5283
5284 // expression operations
5285 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5286 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5287 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5288 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5289 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5290 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5291 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5292 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5293 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5294 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5295 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5296 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5297 pfx, current, iptr->isn_arg.number); break;
5298 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5299 pfx, current, iptr->isn_arg.number); break;
5300 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5301 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5302 iptr->isn_arg.string); break;
5303 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5304
5305 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5306 case ISN_CHECKTYPE:
5307 {
5308 checktype_T *ct = &iptr->isn_arg.type;
5309 char *tofree;
5310
5311 if (ct->ct_arg_idx == 0)
5312 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5313 type_name(ct->ct_type, &tofree),
5314 (int)ct->ct_off);
5315 else
5316 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5317 type_name(ct->ct_type, &tofree),
5318 (int)ct->ct_off,
5319 (int)ct->ct_arg_idx);
5320 vim_free(tofree);
5321 break;
5322 }
5323 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5324 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5325 iptr->isn_arg.checklen.cl_min_len);
5326 break;
5327 case ISN_SETTYPE:
5328 {
5329 char *tofree;
5330
5331 smsg("%s%4d SETTYPE %s", pfx, current,
5332 type_name(iptr->isn_arg.type.ct_type, &tofree));
5333 vim_free(tofree);
5334 break;
5335 }
5336 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005337 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5338 smsg("%s%4d INVERT %d (!val)", pfx, current,
5339 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005340 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005341 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5342 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005343 break;
5344 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005345 (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_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5348 pfx, current,
5349 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005350 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005351 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5352 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005353 break;
5354 case ISN_PUT:
5355 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5356 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005357 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005358 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5359 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005360 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005361 else
5362 smsg("%s%4d PUT %c %ld", pfx, current,
5363 iptr->isn_arg.put.put_regname,
5364 (long)iptr->isn_arg.put.put_lnum);
5365 break;
5366
5367 // TODO: summarize modifiers
5368 case ISN_CMDMOD:
5369 {
5370 char_u *buf;
5371 size_t len = produce_cmdmods(
5372 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5373
5374 buf = alloc(len + 1);
5375 if (buf != NULL)
5376 {
5377 (void)produce_cmdmods(
5378 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5379 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5380 vim_free(buf);
5381 }
5382 break;
5383 }
5384 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5385
5386 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005387 smsg("%s%4d PROFILE START line %d", pfx, current,
5388 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005389 break;
5390
5391 case ISN_PROF_END:
5392 smsg("%s%4d PROFILE END", pfx, current);
5393 break;
5394
Bram Moolenaare99d4222021-06-13 14:01:26 +02005395 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005396 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5397 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005398 break;
5399
Bram Moolenaar4c137212021-04-19 16:48:48 +02005400 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5401 iptr->isn_arg.unpack.unp_count,
5402 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5403 break;
5404 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5405 iptr->isn_arg.shuffle.shfl_item,
5406 iptr->isn_arg.shuffle.shfl_up);
5407 break;
5408 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5409
5410 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5411 return;
5412 }
5413
5414 out_flush(); // output one line at a time
5415 ui_breakcheck();
5416 if (got_int)
5417 break;
5418 }
5419}
5420
5421/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005422 * Handle command line completion for the :disassemble command.
5423 */
5424 void
5425set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5426{
5427 char_u *p;
5428
5429 // Default: expand user functions, "debug" and "profile"
5430 xp->xp_context = EXPAND_DISASSEMBLE;
5431 xp->xp_pattern = arg;
5432
5433 // first argument already typed: only user function names
5434 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5435 {
5436 xp->xp_context = EXPAND_USER_FUNC;
5437 xp->xp_pattern = skipwhite(p);
5438 }
5439}
5440
5441/*
5442 * Function given to ExpandGeneric() to obtain the list of :disassemble
5443 * arguments.
5444 */
5445 char_u *
5446get_disassemble_argument(expand_T *xp, int idx)
5447{
5448 if (idx == 0)
5449 return (char_u *)"debug";
5450 if (idx == 1)
5451 return (char_u *)"profile";
5452 return get_user_func_name(xp, idx - 2);
5453}
5454
5455/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005456 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005457 * We don't really need this at runtime, but we do have tests that require it,
5458 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 */
5460 void
5461ex_disassemble(exarg_T *eap)
5462{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005463 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005464 char_u *fname;
5465 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 dfunc_T *dfunc;
5467 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005468 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005469 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005470 compiletype_T compile_type = CT_NONE;
5471
5472 if (STRNCMP(arg, "profile", 7) == 0)
5473 {
5474 compile_type = CT_PROFILE;
5475 arg = skipwhite(arg + 7);
5476 }
5477 else if (STRNCMP(arg, "debug", 5) == 0)
5478 {
5479 compile_type = CT_DEBUG;
5480 arg = skipwhite(arg + 5);
5481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005483 if (STRNCMP(arg, "<lambda>", 8) == 0)
5484 {
5485 arg += 8;
5486 (void)getdigits(&arg);
5487 fname = vim_strnsave(eap->arg, arg - eap->arg);
5488 }
5489 else
5490 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005491 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005492 if (fname == NULL)
5493 {
5494 semsg(_(e_invarg2), eap->arg);
5495 return;
5496 }
5497
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005498 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005499 if (ufunc == NULL)
5500 {
5501 char_u *p = untrans_function_name(fname);
5502
5503 if (p != NULL)
5504 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005505 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005506 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005507 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508 if (ufunc == NULL)
5509 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005510 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005511 return;
5512 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005513 if (func_needs_compiling(ufunc, compile_type)
5514 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005515 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005516 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005517 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005518 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519 return;
5520 }
5521 if (ufunc->uf_name_exp != NULL)
5522 msg((char *)ufunc->uf_name_exp);
5523 else
5524 msg((char *)ufunc->uf_name);
5525
5526 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005527 switch (compile_type)
5528 {
5529 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005530#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005531 instr = dfunc->df_instr_prof;
5532 instr_count = dfunc->df_instr_prof_count;
5533 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005534#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005535 // FALLTHROUGH
5536 case CT_NONE:
5537 instr = dfunc->df_instr;
5538 instr_count = dfunc->df_instr_count;
5539 break;
5540 case CT_DEBUG:
5541 instr = dfunc->df_instr_debug;
5542 instr_count = dfunc->df_instr_debug_count;
5543 break;
5544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545
Bram Moolenaar4c137212021-04-19 16:48:48 +02005546 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547}
5548
5549/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005550 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551 * list, etc. Mostly like what JavaScript does, except that empty list and
5552 * empty dictionary are FALSE.
5553 */
5554 int
5555tv2bool(typval_T *tv)
5556{
5557 switch (tv->v_type)
5558 {
5559 case VAR_NUMBER:
5560 return tv->vval.v_number != 0;
5561 case VAR_FLOAT:
5562#ifdef FEAT_FLOAT
5563 return tv->vval.v_float != 0.0;
5564#else
5565 break;
5566#endif
5567 case VAR_PARTIAL:
5568 return tv->vval.v_partial != NULL;
5569 case VAR_FUNC:
5570 case VAR_STRING:
5571 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5572 case VAR_LIST:
5573 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5574 case VAR_DICT:
5575 return tv->vval.v_dict != NULL
5576 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5577 case VAR_BOOL:
5578 case VAR_SPECIAL:
5579 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5580 case VAR_JOB:
5581#ifdef FEAT_JOB_CHANNEL
5582 return tv->vval.v_job != NULL;
5583#else
5584 break;
5585#endif
5586 case VAR_CHANNEL:
5587#ifdef FEAT_JOB_CHANNEL
5588 return tv->vval.v_channel != NULL;
5589#else
5590 break;
5591#endif
5592 case VAR_BLOB:
5593 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5594 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005595 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005596 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005597 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005598 break;
5599 }
5600 return FALSE;
5601}
5602
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005603 void
5604emsg_using_string_as(typval_T *tv, int as_number)
5605{
5606 semsg(_(as_number ? e_using_string_as_number_str
5607 : e_using_string_as_bool_str),
5608 tv->vval.v_string == NULL
5609 ? (char_u *)"" : tv->vval.v_string);
5610}
5611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612/*
5613 * If "tv" is a string give an error and return FAIL.
5614 */
5615 int
5616check_not_string(typval_T *tv)
5617{
5618 if (tv->v_type == VAR_STRING)
5619 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005620 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621 clear_tv(tv);
5622 return FAIL;
5623 }
5624 return OK;
5625}
5626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627
5628#endif // FEAT_EVAL