blob: bbcf7ffc3c0cd0bd205ea01e3a18156ed4520751 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010029 int tcd_catch_idx; // instruction of the first :catch or :finally
30 int tcd_finally_idx; // instruction of the :finally block or zero
31 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010032 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010033 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_return; // when TRUE return from end of :finally
35} trycmd_T;
36
Bram Moolenaar4c137212021-04-19 16:48:48 +020037// Data local to a function.
38// On a function call, if not empty, is saved on the stack and restored when
39// returning.
40typedef struct {
41 int floc_restore_cmdmod;
42 cmdmod_T floc_save_cmdmod;
43 int floc_restore_cmdmod_stacklen;
44} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010045
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020046// Structure to hold a reference to an outer_T, with information of whether it
47// was allocated.
48typedef struct {
49 outer_T *or_outer;
50 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
51 int or_outer_allocated; // free "or_outer" later
52} outer_ref_T;
53
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010054// A stack is used to store:
55// - arguments passed to a :def function
56// - info about the calling function, to use when returning
57// - local variables
58// - temporary values
59//
60// In detail (FP == Frame Pointer):
61// arg1 first argument from caller (if present)
62// arg2 second argument from caller (if present)
63// extra_arg1 any missing optional argument default value
64// FP -> cur_func calling function
65// current previous instruction pointer
66// frame_ptr previous Frame Pointer
67// var1 space for local variable
68// var2 space for local variable
69// .... fixed space for max. number of local variables
70// temp temporary values
71// .... flexible space for temporary values (can grow big)
72
73/*
74 * Execution context.
75 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010076struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020078 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020079 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020081 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020082 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 garray_T ec_trystack; // stack of trycmd_T values
85 int ec_in_catch; // when TRUE in catch or finally block
86
87 int ec_dfunc_idx; // current function index
88 isn_T *ec_instr; // array with instructions
89 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020090
91 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020092
93 int ec_did_emsg_before;
94 int ec_trylevel_at_start;
95 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010096};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097
Bram Moolenaar12d26532021-02-19 19:13:21 +010098#ifdef FEAT_PROFILE
99// stack of profinfo_T used when profiling.
100static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
101#endif
102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200104#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100105
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200106 void
107to_string_error(vartype_T vartype)
108{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200109 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200110}
111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100113 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 */
115 static int
116ufunc_argcount(ufunc_T *ufunc)
117{
118 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100152 * This adds a stack frame and sets the instruction pointer to the start of the
153 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200154 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 *
156 * Stack has:
157 * - current arguments (already there)
158 * - omitted optional argument (default values) added here
159 * - stack frame:
160 * - pointer to calling function
161 * - Index of next instruction in calling function
162 * - previous frame pointer
163 * - reserved space for local variables
164 */
165 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100166call_dfunc(
167 int cdf_idx,
168 partial_T *pt,
169 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100170 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100172 int argcount = argcount_arg;
173 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
174 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200175 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100176 int arg_to_add;
177 int vararg_count = 0;
178 int varcount;
179 int idx;
180 estack_T *entry;
181 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182
183 if (dfunc->df_deleted)
184 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100185 // don't use ufunc->uf_name, it may have been freed
186 emsg_funcname(e_func_deleted,
187 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188 return FAIL;
189 }
190
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100191#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100192 if (do_profiling == PROF_YES)
193 {
194 if (ga_grow(&profile_info_ga, 1) == OK)
195 {
196 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
197 + profile_info_ga.ga_len;
198 ++profile_info_ga.ga_len;
199 CLEAR_POINTER(info);
200 profile_may_start_func(info, ufunc,
201 (((dfunc_T *)def_functions.ga_data)
202 + ectx->ec_dfunc_idx)->df_ufunc);
203 }
204
205 // Profiling might be enabled/disabled along the way. This should not
206 // fail, since the function was compiled before and toggling profiling
207 // doesn't change any errors.
Bram Moolenaare99d4222021-06-13 14:01:26 +0200208 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
209 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100210 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100211 return FAIL;
212 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100213#endif
214
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200215 // When debugging and using "cont" switches to the not-debugged
216 // instructions, may need to still compile them.
217 if ((func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
218 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
219 == FAIL)
220 || INSTRUCTIONS(dfunc) == NULL)
221 {
222 if (did_emsg_cumul + did_emsg == did_emsg_before)
223 semsg(_(e_function_is_not_compiled_str),
224 printable_func_name(ufunc));
225 return FAIL;
226 }
227
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200228 if (ufunc->uf_va_name != NULL)
229 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200230 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200231 // Stack at time of call with 2 varargs:
232 // normal_arg
233 // optional_arg
234 // vararg_1
235 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200236 // After creating the list:
237 // normal_arg
238 // optional_arg
239 // vararg-list
240 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200241 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200242 // After creating the list
243 // normal_arg
244 // (space for optional_arg)
245 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200246 vararg_count = argcount - ufunc->uf_args.ga_len;
247 if (vararg_count < 0)
248 vararg_count = 0;
249 else
250 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200251 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200252 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200253
254 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200255 }
256
Bram Moolenaarfe270812020-04-11 22:31:27 +0200257 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200258 if (arg_to_add < 0)
259 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200260 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200261 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200262 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200263 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200264 return FAIL;
265 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200266
267 // Reserve space for:
268 // - missing arguments
269 // - stack frame
270 // - local variables
271 // - if needed: a counter for number of closures created in
272 // ectx->ec_funcrefs.
273 varcount = dfunc->df_varcount + dfunc->df_has_closure;
274 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
275 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 return FAIL;
277
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100278 // If depth of calling is getting too high, don't execute the function.
279 if (funcdepth_increment() == FAIL)
280 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200281 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100282
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100283 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200284 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100285 {
286 floc = ALLOC_ONE(funclocal_T);
287 if (floc == NULL)
288 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200289 *floc = ectx->ec_funclocal;
290 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100291 }
292
Bram Moolenaarfe270812020-04-11 22:31:27 +0200293 // Move the vararg-list to below the missing optional arguments.
294 if (vararg_count > 0 && arg_to_add > 0)
295 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100296
297 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200298 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200299 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200300 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301
302 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100303 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
304 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200305 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200306 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
307 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100308 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100309 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200310 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311
312 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200313 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200315 if (dfunc->df_has_closure)
316 {
317 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
318
319 tv->v_type = VAR_NUMBER;
320 tv->vval.v_number = 0;
321 }
322 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100324 if (pt != NULL || ufunc->uf_partial != NULL
325 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100326 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200327 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100328
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200329 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100330 return FAIL;
331 if (pt != NULL)
332 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200333 ref->or_outer = &pt->pt_outer;
334 ++pt->pt_refcount;
335 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100336 }
337 else if (ufunc->uf_partial != NULL)
338 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200339 ref->or_outer = &ufunc->uf_partial->pt_outer;
340 ++ufunc->uf_partial->pt_refcount;
341 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100342 }
343 else
344 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200345 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
346 if (ref->or_outer == NULL)
347 {
348 vim_free(ref);
349 return FAIL;
350 }
351 ref->or_outer_allocated = TRUE;
352 ref->or_outer->out_stack = &ectx->ec_stack;
353 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
354 if (ectx->ec_outer_ref != NULL)
355 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100356 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200357 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100358 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100359 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200360 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100361
Bram Moolenaarc970e422021-03-17 15:03:04 +0100362 ++ufunc->uf_calls;
363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364 // Set execution state to the start of the called function.
365 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100366 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100367 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200368 if (entry != NULL)
369 {
370 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200371 // Save the current context so it can be restored on return.
372 entry->es_save_sctx = current_sctx;
373 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200374 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100375
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200376 // Start execution at the first instruction.
377 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378
379 return OK;
380}
381
382// Get pointer to item in the stack.
383#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
384
385/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200386 * Used when returning from a function: Check if any closure is still
387 * referenced. If so then move the arguments and variables to a separate piece
388 * of stack to be used when the closure is called.
389 * When "free_arguments" is TRUE the arguments are to be freed.
390 * Returns FAIL when out of memory.
391 */
392 static int
393handle_closure_in_use(ectx_T *ectx, int free_arguments)
394{
395 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
396 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200397 int argcount;
398 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 int idx;
400 typval_T *tv;
401 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200402 garray_T *gap = &ectx->ec_funcrefs;
403 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200405 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200406 return OK; // function was freed
407 if (dfunc->df_has_closure == 0)
408 return OK; // no closures
409 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
410 closure_count = tv->vval.v_number;
411 if (closure_count == 0)
412 return OK; // no funcrefs created
413
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200414 argcount = ufunc_argcount(dfunc->df_ufunc);
415 top = ectx->ec_frame_idx - argcount;
416
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200418 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200419 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200420 partial_T *pt;
421 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200422
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200423 if (off < 0)
424 continue; // count is off or already done
425 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200426 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200427 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200428 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200429 int i;
430
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200431 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200432 // unreferenced on return.
433 for (i = 0; i < dfunc->df_varcount; ++i)
434 {
435 typval_T *stv = STACK_TV(ectx->ec_frame_idx
436 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200437 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200438 --refcount;
439 }
440 if (refcount > 1)
441 {
442 closure_in_use = TRUE;
443 break;
444 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200445 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200446 }
447
448 if (closure_in_use)
449 {
450 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
451 typval_T *stack;
452
453 // A closure is using the arguments and/or local variables.
454 // Move them to the called function.
455 if (funcstack == NULL)
456 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200457 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
458 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200459 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
460 funcstack->fs_ga.ga_data = stack;
461 if (stack == NULL)
462 {
463 vim_free(funcstack);
464 return FAIL;
465 }
466
467 // Move or copy the arguments.
468 for (idx = 0; idx < argcount; ++idx)
469 {
470 tv = STACK_TV(top + idx);
471 if (free_arguments)
472 {
473 *(stack + idx) = *tv;
474 tv->v_type = VAR_UNKNOWN;
475 }
476 else
477 copy_tv(tv, stack + idx);
478 }
479 // Move the local variables.
480 for (idx = 0; idx < dfunc->df_varcount; ++idx)
481 {
482 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200483
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200484 // A partial created for a local function, that is also used as a
485 // local variable, has a reference count for the variable, thus
486 // will never go down to zero. When all these refcounts are one
487 // then the funcstack is unused. We need to count how many we have
488 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200489 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
490 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200491 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200492
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200493 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200494 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
495 gap->ga_len - closure_count + i])
496 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200497 }
498
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200499 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200500 tv->v_type = VAR_UNKNOWN;
501 }
502
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200503 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200504 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200505 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
506 - closure_count + idx];
507 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200508 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200509 ++funcstack->fs_refcount;
510 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100511 pt->pt_outer.out_stack = &funcstack->fs_ga;
512 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200513 }
514 }
515 }
516
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200517 for (idx = 0; idx < closure_count; ++idx)
518 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
519 - closure_count + idx]);
520 gap->ga_len -= closure_count;
521 if (gap->ga_len == 0)
522 ga_clear(gap);
523
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200524 return OK;
525}
526
527/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200528 * Called when a partial is freed or its reference count goes down to one. The
529 * funcstack may be the only reference to the partials in the local variables.
530 * Go over all of them, the funcref and can be freed if all partials
531 * referencing the funcstack have a reference count of one.
532 */
533 void
534funcstack_check_refcount(funcstack_T *funcstack)
535{
536 int i;
537 garray_T *gap = &funcstack->fs_ga;
538 int done = 0;
539
540 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
541 return;
542 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
543 {
544 typval_T *tv = ((typval_T *)gap->ga_data) + i;
545
546 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
547 && tv->vval.v_partial->pt_funcstack == funcstack
548 && tv->vval.v_partial->pt_refcount == 1)
549 ++done;
550 }
551 if (done == funcstack->fs_min_refcount)
552 {
553 typval_T *stack = gap->ga_data;
554
555 // All partials referencing the funcstack have a reference count of
556 // one, thus the funcstack is no longer of use.
557 for (i = 0; i < gap->ga_len; ++i)
558 clear_tv(stack + i);
559 vim_free(stack);
560 vim_free(funcstack);
561 }
562}
563
564/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565 * Return from the current function.
566 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200567 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200568func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100571 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200572 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
573 + ectx->ec_dfunc_idx;
574 int argcount = ufunc_argcount(dfunc->df_ufunc);
575 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200576 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100577 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
578 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200579 funclocal_T *floc;
580#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100581 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
582 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583
Bram Moolenaar12d26532021-02-19 19:13:21 +0100584 if (do_profiling == PROF_YES)
585 {
586 ufunc_T *caller = prev_dfunc->df_ufunc;
587
588 if (dfunc->df_ufunc->uf_profiling
589 || (caller != NULL && caller->uf_profiling))
590 {
591 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
592 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
593 --profile_info_ga.ga_len;
594 }
595 }
596#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100597 // TODO: when is it safe to delete the function when it is no longer used?
598 --dfunc->df_ufunc->uf_calls;
599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200601 entry = estack_pop();
602 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200603 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200605 if (handle_closure_in_use(ectx, TRUE) == FAIL)
606 return FAIL;
607
608 // Clear the arguments.
609 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
610 clear_tv(STACK_TV(idx));
611
612 // Clear local variables and temp values, but not the return value.
613 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100614 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100616
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100617 // The return value should be on top of the stack. However, when aborting
618 // it may not be there and ec_frame_idx is the top of the stack.
619 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100620 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100621 ret_idx = 0;
622
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200623 if (ectx->ec_outer_ref != NULL)
624 {
625 if (ectx->ec_outer_ref->or_outer_allocated)
626 vim_free(ectx->ec_outer_ref->or_outer);
627 partial_unref(ectx->ec_outer_ref->or_partial);
628 vim_free(ectx->ec_outer_ref);
629 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100630
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100631 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100632 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100633 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
634 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200635 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
636 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200637 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100638 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100639 floc = (void *)STACK_TV(ectx->ec_frame_idx
640 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200641 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100642 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
643 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200644
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100645 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200646 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100647 else
648 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200649 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100650 vim_free(floc);
651 }
652
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100653 if (ret_idx > 0)
654 {
655 // Reset the stack to the position before the call, with a spot for the
656 // return value, moved there from above the frame.
657 ectx->ec_stack.ga_len = top + 1;
658 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
659 }
660 else
661 // Reset the stack to the position before the call.
662 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200663
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100664 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200665 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200666 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667}
668
669#undef STACK_TV
670
671/*
672 * Prepare arguments and rettv for calling a builtin or user function.
673 */
674 static int
675call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
676{
677 int idx;
678 typval_T *tv;
679
680 // Move arguments from bottom of the stack to argvars[] and add terminator.
681 for (idx = 0; idx < argcount; ++idx)
682 argvars[idx] = *STACK_TV_BOT(idx - argcount);
683 argvars[argcount].v_type = VAR_UNKNOWN;
684
685 // Result replaces the arguments on the stack.
686 if (argcount > 0)
687 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200688 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 return FAIL;
690 else
691 ++ectx->ec_stack.ga_len;
692
693 // Default return value is zero.
694 tv = STACK_TV_BOT(-1);
695 tv->v_type = VAR_NUMBER;
696 tv->vval.v_number = 0;
697
698 return OK;
699}
700
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200701// Ugly global to avoid passing the execution context around through many
702// layers.
703static ectx_T *current_ectx = NULL;
704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705/*
706 * Call a builtin function by index.
707 */
708 static int
709call_bfunc(int func_idx, int argcount, ectx_T *ectx)
710{
711 typval_T argvars[MAX_FUNC_ARGS];
712 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100713 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200714 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715
716 if (call_prepare(argcount, argvars, ectx) == FAIL)
717 return FAIL;
718
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200719 // Call the builtin function. Set "current_ectx" so that when it
720 // recursively invokes call_def_function() a closure context can be set.
721 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200723 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724
725 // Clear the arguments.
726 for (idx = 0; idx < argcount; ++idx)
727 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200728
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100729 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200730 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 return OK;
732}
733
734/*
735 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100736 * If the function is compiled this will add a stack frame and set the
737 * instruction pointer at the start of the function.
738 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200739 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100740 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 */
742 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100743call_ufunc(
744 ufunc_T *ufunc,
745 partial_T *pt,
746 int argcount,
747 ectx_T *ectx,
748 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749{
750 typval_T argvars[MAX_FUNC_ARGS];
751 funcexe_T funcexe;
752 int error;
753 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100754 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200755 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756
Bram Moolenaare99d4222021-06-13 14:01:26 +0200757 if (func_needs_compiling(ufunc, compile_type)
758 && compile_def_function(ufunc, FALSE, compile_type, NULL)
759 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200760 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200761 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100762 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100763 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100764 if (error != FCERR_UNKNOWN)
765 {
766 if (error == FCERR_TOOMANY)
767 semsg(_(e_toomanyarg), ufunc->uf_name);
768 else
769 semsg(_(e_toofewarg), ufunc->uf_name);
770 return FAIL;
771 }
772
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100773 // The function has been compiled, can call it quickly. For a function
774 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100775 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100776 if (iptr != NULL)
777 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100778 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100779 iptr->isn_type = ISN_DCALL;
780 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
781 iptr->isn_arg.dfunc.cdf_argcount = argcount;
782 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200783 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785
786 if (call_prepare(argcount, argvars, ectx) == FAIL)
787 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200788 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 funcexe.evaluate = TRUE;
790
791 // Call the user function. Result goes in last position on the stack.
792 // TODO: add selfdict if there is one
793 error = call_user_func_check(ufunc, argcount, argvars,
794 STACK_TV_BOT(-1), &funcexe, NULL);
795
796 // Clear the arguments.
797 for (idx = 0; idx < argcount; ++idx)
798 clear_tv(&argvars[idx]);
799
800 if (error != FCERR_NONE)
801 {
802 user_func_error(error, ufunc->uf_name);
803 return FAIL;
804 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100805 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200806 // Error other than from calling the function itself.
807 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 return OK;
809}
810
811/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100812 * If command modifiers were applied restore them.
813 */
814 static void
815may_restore_cmdmod(funclocal_T *funclocal)
816{
817 if (funclocal->floc_restore_cmdmod)
818 {
819 cmdmod.cmod_filter_regmatch.regprog = NULL;
820 undo_cmdmod(&cmdmod);
821 cmdmod = funclocal->floc_save_cmdmod;
822 funclocal->floc_restore_cmdmod = FALSE;
823 }
824}
825
826/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200827 * Return TRUE if an error was given or CTRL-C was pressed.
828 */
829 static int
830vim9_aborting(int prev_called_emsg)
831{
832 return called_emsg > prev_called_emsg || got_int || did_throw;
833}
834
835/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 * Execute a function by "name".
837 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100838 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 * Returns FAIL if not found without an error message.
840 */
841 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100842call_by_name(
843 char_u *name,
844 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100845 ectx_T *ectx,
846 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847{
848 ufunc_T *ufunc;
849
850 if (builtin_function(name, -1))
851 {
852 int func_idx = find_internal_func(name);
853
854 if (func_idx < 0)
855 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200856 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 return FAIL;
858 return call_bfunc(func_idx, argcount, ectx);
859 }
860
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200861 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200862
863 if (ufunc == NULL)
864 {
865 int called_emsg_before = called_emsg;
866
867 if (script_autoload(name, TRUE))
868 // loaded a package, search for the function again
869 ufunc = find_func(name, FALSE, NULL);
870 if (vim9_aborting(called_emsg_before))
871 return FAIL; // bail out if loading the script caused an error
872 }
873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100875 {
876 if (ufunc->uf_arg_types != NULL)
877 {
878 int i;
879 typval_T *argv = STACK_TV_BOT(0) - argcount;
880
881 // The function can change at runtime, check that the argument
882 // types are correct.
883 for (i = 0; i < argcount; ++i)
884 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100885 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100886
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100887 if (i < ufunc->uf_args.ga_len)
888 type = ufunc->uf_arg_types[i];
889 else if (ufunc->uf_va_type != NULL)
890 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100891 if (type != NULL && check_typval_arg_type(type,
892 &argv[i], i + 1) == FAIL)
893 return FAIL;
894 }
895 }
896
Bram Moolenaar4c137212021-04-19 16:48:48 +0200897 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100898 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899
900 return FAIL;
901}
902
903 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100904call_partial(
905 typval_T *tv,
906 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100907 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200909 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200910 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100912 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913
914 if (tv->v_type == VAR_PARTIAL)
915 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200916 partial_T *pt = tv->vval.v_partial;
917 int i;
918
919 if (pt->pt_argc > 0)
920 {
921 // Make space for arguments from the partial, shift the "argcount"
922 // arguments up.
923 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
924 return FAIL;
925 for (i = 1; i <= argcount; ++i)
926 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
927 ectx->ec_stack.ga_len += pt->pt_argc;
928 argcount += pt->pt_argc;
929
930 // copy the arguments from the partial onto the stack
931 for (i = 0; i < pt->pt_argc; ++i)
932 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200936 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 name = pt->pt_name;
939 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200940 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200942 if (name != NULL)
943 {
944 char_u fname_buf[FLEN_FIXED + 1];
945 char_u *tofree = NULL;
946 int error = FCERR_NONE;
947 char_u *fname;
948
949 // May need to translate <SNR>123_ to K_SNR.
950 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
951 if (error != FCERR_NONE)
952 res = FAIL;
953 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200954 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200955 vim_free(tofree);
956 }
957
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100958 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 {
960 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200961 semsg(_(e_unknownfunc),
962 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 return FAIL;
964 }
965 return OK;
966}
967
968/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200969 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
970 * TRUE.
971 */
972 static int
973error_if_locked(int lock, char *error)
974{
975 if (lock & (VAR_LOCKED | VAR_FIXED))
976 {
977 emsg(_(error));
978 return TRUE;
979 }
980 return FALSE;
981}
982
983/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100984 * Give an error if "tv" is not a number and return FAIL.
985 */
986 static int
987check_for_number(typval_T *tv)
988{
989 if (tv->v_type != VAR_NUMBER)
990 {
991 semsg(_(e_expected_str_but_got_str),
992 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
993 return FAIL;
994 }
995 return OK;
996}
997
998/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100999 * Store "tv" in variable "name".
1000 * This is for s: and g: variables.
1001 */
1002 static void
1003store_var(char_u *name, typval_T *tv)
1004{
1005 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001006 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001007
Bram Moolenaard877a572021-04-01 19:42:48 +02001008 if (tv->v_lock)
1009 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001010 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001011 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001012 restore_funccal();
1013}
1014
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001015/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001016 * Convert "tv" to a string.
1017 * Return FAIL if not allowed.
1018 */
1019 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001020do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001021{
1022 if (tv->v_type != VAR_STRING)
1023 {
1024 char_u *str;
1025
1026 if (is_2string_any)
1027 {
1028 switch (tv->v_type)
1029 {
1030 case VAR_SPECIAL:
1031 case VAR_BOOL:
1032 case VAR_NUMBER:
1033 case VAR_FLOAT:
1034 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001035
1036 case VAR_LIST:
1037 if (tolerant)
1038 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001039 char_u *s, *e, *p;
1040 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001041
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001042 ga_init2(&ga, sizeof(char_u *), 1);
1043
1044 // Convert to NL separated items, then
1045 // escape the items and replace the NL with
1046 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001047 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001048 if (str == NULL)
1049 return FAIL;
1050 s = str;
1051 while ((e = vim_strchr(s, '\n')) != NULL)
1052 {
1053 *e = NUL;
1054 p = vim_strsave_fnameescape(s, FALSE);
1055 if (p != NULL)
1056 {
1057 ga_concat(&ga, p);
1058 ga_concat(&ga, (char_u *)" ");
1059 vim_free(p);
1060 }
1061 s = e + 1;
1062 }
1063 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001064 clear_tv(tv);
1065 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001066 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001067 return OK;
1068 }
1069 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001070 default: to_string_error(tv->v_type);
1071 return FAIL;
1072 }
1073 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001074 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001075 clear_tv(tv);
1076 tv->v_type = VAR_STRING;
1077 tv->vval.v_string = str;
1078 }
1079 return OK;
1080}
1081
1082/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001083 * When the value of "sv" is a null list of dict, allocate it.
1084 */
1085 static void
1086allocate_if_null(typval_T *tv)
1087{
1088 switch (tv->v_type)
1089 {
1090 case VAR_LIST:
1091 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001092 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001093 break;
1094 case VAR_DICT:
1095 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001096 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001097 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001098 case VAR_BLOB:
1099 if (tv->vval.v_blob == NULL)
1100 (void)rettv_blob_alloc(tv);
1101 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001102 default:
1103 break;
1104 }
1105}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001106
Bram Moolenaare7525c52021-01-09 13:20:37 +01001107/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001108 * Return the character "str[index]" where "index" is the character index,
1109 * including composing characters.
1110 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001111 */
1112 char_u *
1113char_from_string(char_u *str, varnumber_T index)
1114{
1115 size_t nbyte = 0;
1116 varnumber_T nchar = index;
1117 size_t slen;
1118
1119 if (str == NULL)
1120 return NULL;
1121 slen = STRLEN(str);
1122
Bram Moolenaarff871402021-03-26 13:34:05 +01001123 // Do the same as for a list: a negative index counts from the end.
1124 // Optimization to check the first byte to be below 0x80 (and no composing
1125 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001126 if (index < 0)
1127 {
1128 int clen = 0;
1129
1130 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001131 {
1132 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1133 ++nbyte;
1134 else if (enc_utf8)
1135 nbyte += utfc_ptr2len(str + nbyte);
1136 else
1137 nbyte += mb_ptr2len(str + nbyte);
1138 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001139 nchar = clen + index;
1140 if (nchar < 0)
1141 // unlike list: index out of range results in empty string
1142 return NULL;
1143 }
1144
1145 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001146 {
1147 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1148 ++nbyte;
1149 else if (enc_utf8)
1150 nbyte += utfc_ptr2len(str + nbyte);
1151 else
1152 nbyte += mb_ptr2len(str + nbyte);
1153 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001154 if (nbyte >= slen)
1155 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001156 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001157}
1158
1159/*
1160 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001161 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001162 * If going over the end return "str_len".
1163 * If "idx" is negative count from the end, -1 is the last character.
1164 * When going over the start return -1.
1165 */
1166 static long
1167char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1168{
1169 varnumber_T nchar = idx;
1170 size_t nbyte = 0;
1171
1172 if (nchar >= 0)
1173 {
1174 while (nchar > 0 && nbyte < str_len)
1175 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001176 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001177 --nchar;
1178 }
1179 }
1180 else
1181 {
1182 nbyte = str_len;
1183 while (nchar < 0 && nbyte > 0)
1184 {
1185 --nbyte;
1186 nbyte -= mb_head_off(str, str + nbyte);
1187 ++nchar;
1188 }
1189 if (nchar < 0)
1190 return -1;
1191 }
1192 return (long)nbyte;
1193}
1194
1195/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001196 * Return the slice "str[first : last]" using character indexes. Composing
1197 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001198 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001199 * Return NULL when the result is empty.
1200 */
1201 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001202string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001203{
1204 long start_byte, end_byte;
1205 size_t slen;
1206
1207 if (str == NULL)
1208 return NULL;
1209 slen = STRLEN(str);
1210 start_byte = char_idx2byte(str, slen, first);
1211 if (start_byte < 0)
1212 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001213 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001214 end_byte = (long)slen;
1215 else
1216 {
1217 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001218 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001219 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001220 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001221 }
1222
1223 if (start_byte >= (long)slen || end_byte <= start_byte)
1224 return NULL;
1225 return vim_strnsave(str + start_byte, end_byte - start_byte);
1226}
1227
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001228 static svar_T *
1229get_script_svar(scriptref_T *sref, ectx_T *ectx)
1230{
1231 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1232 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1233 + ectx->ec_dfunc_idx;
1234 svar_T *sv;
1235
1236 if (sref->sref_seq != si->sn_script_seq)
1237 {
1238 // The script was reloaded after the function was
1239 // compiled, the script_idx may not be valid.
1240 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1241 dfunc->df_ufunc->uf_name_exp);
1242 return NULL;
1243 }
1244 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1245 if (!equal_type(sv->sv_type, sref->sref_type))
1246 {
1247 emsg(_(e_script_variable_type_changed));
1248 return NULL;
1249 }
1250 return sv;
1251}
1252
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001253/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001254 * Function passed to do_cmdline() for splitting a script joined by NL
1255 * characters.
1256 */
1257 static char_u *
1258get_split_sourceline(
1259 int c UNUSED,
1260 void *cookie,
1261 int indent UNUSED,
1262 getline_opt_T options UNUSED)
1263{
1264 source_cookie_T *sp = (source_cookie_T *)cookie;
1265 char_u *p;
1266 char_u *line;
1267
1268 if (*sp->nextline == NUL)
1269 return NULL;
1270 p = vim_strchr(sp->nextline, '\n');
1271 if (p == NULL)
1272 {
1273 line = vim_strsave(sp->nextline);
1274 sp->nextline += STRLEN(sp->nextline);
1275 }
1276 else
1277 {
1278 line = vim_strnsave(sp->nextline, p - sp->nextline);
1279 sp->nextline = p + 1;
1280 }
1281 return line;
1282}
1283
1284/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 * Execute a function by "name".
1286 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001287 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 */
1289 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001290call_eval_func(
1291 char_u *name,
1292 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001293 ectx_T *ectx,
1294 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295{
Bram Moolenaared677f52020-08-12 16:38:10 +02001296 int called_emsg_before = called_emsg;
1297 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298
Bram Moolenaar4c137212021-04-19 16:48:48 +02001299 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001300 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001302 dictitem_T *v;
1303
1304 v = find_var(name, NULL, FALSE);
1305 if (v == NULL)
1306 {
1307 semsg(_(e_unknownfunc), name);
1308 return FAIL;
1309 }
1310 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1311 {
1312 semsg(_(e_unknownfunc), name);
1313 return FAIL;
1314 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001315 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001317 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318}
1319
1320/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001321 * When a function reference is used, fill a partial with the information
1322 * needed, especially when it is used as a closure.
1323 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001324 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001325fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1326{
1327 pt->pt_func = ufunc;
1328 pt->pt_refcount = 1;
1329
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001330 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001331 {
1332 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1333 + ectx->ec_dfunc_idx;
1334
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001335 // The closure may need to find arguments and local variables in the
1336 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001337 pt->pt_outer.out_stack = &ectx->ec_stack;
1338 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001339 if (ectx->ec_outer_ref != NULL)
1340 {
1341 // The current context already has a context, link to that one.
1342 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1343 if (ectx->ec_outer_ref->or_partial != NULL)
1344 {
1345 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1346 ++pt->pt_outer.out_up_partial->pt_refcount;
1347 }
1348 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001349
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001350 // If this function returns and the closure is still being used, we
1351 // need to make a copy of the context (arguments and local variables).
1352 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001353 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1354 {
1355 vim_free(pt);
1356 return FAIL;
1357 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001358 // Extra variable keeps the count of closures created in the current
1359 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001360 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1361 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1362
1363 ((partial_T **)ectx->ec_funcrefs.ga_data)
1364 [ectx->ec_funcrefs.ga_len] = pt;
1365 ++pt->pt_refcount;
1366 ++ectx->ec_funcrefs.ga_len;
1367 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001368 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001369 return OK;
1370}
1371
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001372// used for v_instr of typval of VAR_INSTR
1373struct instr_S {
1374 ectx_T *instr_ectx;
1375 isn_T *instr_instr;
1376};
1377
Bram Moolenaar4c137212021-04-19 16:48:48 +02001378// used for substitute_instr
1379typedef struct subs_expr_S {
1380 ectx_T *subs_ectx;
1381 isn_T *subs_instr;
1382 int subs_status;
1383} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384
1385// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001386#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387
1388// Get pointer to item at the bottom of the stack, -1 is the bottom.
1389#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001390#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001392// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001393#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001395// Set when calling do_debug().
1396static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001397static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001398
1399/*
1400 * When debugging lookup "name" and return the typeval.
1401 * When not found return NULL.
1402 */
1403 typval_T *
1404lookup_debug_var(char_u *name)
1405{
1406 int idx;
1407 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001408 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001409 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001410 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001411
1412 if (ectx == NULL)
1413 return NULL;
1414 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1415
1416 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001417 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001418 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001419 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001420 return STACK_TV_VAR(idx);
1421 }
1422
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001423 // Go through argument names.
1424 ufunc = dfunc->df_ufunc;
1425 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1426 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1427 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1428 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1429 - varargs_off + idx);
1430 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1431 return STACK_TV(ectx->ec_frame_idx - 1);
1432
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001433 return NULL;
1434}
1435
Bram Moolenaar4c137212021-04-19 16:48:48 +02001436/*
1437 * Execute instructions in execution context "ectx".
1438 * Return OK or FAIL;
1439 */
1440 static int
1441exec_instructions(ectx_T *ectx)
1442{
1443 int breakcheck_count = 0;
1444 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001445 int ret = FAIL;
1446 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001447
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001448 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001449 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001450
Bram Moolenaarff652882021-05-16 15:24:49 +02001451 // Only catch exceptions in this instruction list.
1452 ectx->ec_trylevel_at_start = trylevel;
1453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 for (;;)
1455 {
1456 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001457
Bram Moolenaar270d0382020-05-15 21:42:53 +02001458 if (++breakcheck_count >= 100)
1459 {
1460 line_breakcheck();
1461 breakcheck_count = 0;
1462 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001463 if (got_int)
1464 {
1465 // Turn CTRL-C into an exception.
1466 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001467 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001468 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001469 did_throw = TRUE;
1470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471
Bram Moolenaara26b9702020-04-18 19:53:28 +02001472 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1473 {
1474 // Turn an error message into an exception.
1475 did_emsg = FALSE;
1476 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001477 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001478 did_throw = TRUE;
1479 *msg_list = NULL;
1480 }
1481
Bram Moolenaar4c137212021-04-19 16:48:48 +02001482 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001484 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001485 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486
1487 // An exception jumps to the first catch, finally, or returns from
1488 // the current function.
1489 if (trystack->ga_len > 0)
1490 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001491 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 {
1493 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001494 ectx->ec_in_catch = TRUE;
1495 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 }
1497 else
1498 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001499 // Not inside try or need to return from current functions.
1500 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001501 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001502 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001503 tv = STACK_TV_BOT(0);
1504 tv->v_type = VAR_NUMBER;
1505 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001506 ++ectx->ec_stack.ga_len;
1507 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001509 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001510 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001511 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001512 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 goto done;
1514 }
1515
Bram Moolenaar4c137212021-04-19 16:48:48 +02001516 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001517 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 }
1519 continue;
1520 }
1521
Bram Moolenaar4c137212021-04-19 16:48:48 +02001522 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 switch (iptr->isn_type)
1524 {
1525 // execute Ex command line
1526 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001527 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001528 source_cookie_T cookie;
1529
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001530 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001531 // Pass getsourceline to get an error for a missing ":end"
1532 // command.
1533 CLEAR_FIELD(cookie);
1534 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1535 if (do_cmdline(iptr->isn_arg.string,
1536 getsourceline, &cookie,
1537 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1538 == FAIL
1539 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001540 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542 break;
1543
Bram Moolenaar20677332021-06-06 17:02:53 +02001544 // execute Ex command line split at NL characters.
1545 case ISN_EXEC_SPLIT:
1546 {
1547 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001548 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001549
1550 SOURCING_LNUM = iptr->isn_lnum;
1551 CLEAR_FIELD(cookie);
1552 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1553 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001554 line = get_split_sourceline(0, &cookie, 0, 0);
1555 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001556 get_split_sourceline, &cookie,
1557 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1558 == FAIL
1559 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001560 {
1561 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001562 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001563 }
1564 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001565 }
1566 break;
1567
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001568 // Evaluate an expression with legacy syntax, push it onto the
1569 // stack.
1570 case ISN_LEGACY_EVAL:
1571 {
1572 char_u *arg = iptr->isn_arg.string;
1573 int res;
1574 int save_flags = cmdmod.cmod_flags;
1575
1576 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001577 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001578 tv = STACK_TV_BOT(0);
1579 init_tv(tv);
1580 cmdmod.cmod_flags |= CMOD_LEGACY;
1581 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1582 cmdmod.cmod_flags = save_flags;
1583 if (res == FAIL)
1584 goto on_error;
1585 ++ectx->ec_stack.ga_len;
1586 }
1587 break;
1588
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001589 // push typeval VAR_INSTR with instructions to be executed
1590 case ISN_INSTR:
1591 {
1592 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001593 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001594 tv = STACK_TV_BOT(0);
1595 tv->vval.v_instr = ALLOC_ONE(instr_T);
1596 if (tv->vval.v_instr == NULL)
1597 goto on_error;
1598 ++ectx->ec_stack.ga_len;
1599
1600 tv->v_type = VAR_INSTR;
1601 tv->vval.v_instr->instr_ectx = ectx;
1602 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1603 }
1604 break;
1605
Bram Moolenaar4c137212021-04-19 16:48:48 +02001606 // execute :substitute with an expression
1607 case ISN_SUBSTITUTE:
1608 {
1609 subs_T *subs = &iptr->isn_arg.subs;
1610 source_cookie_T cookie;
1611 struct subs_expr_S *save_instr = substitute_instr;
1612 struct subs_expr_S subs_instr;
1613 int res;
1614
1615 subs_instr.subs_ectx = ectx;
1616 subs_instr.subs_instr = subs->subs_instr;
1617 subs_instr.subs_status = OK;
1618 substitute_instr = &subs_instr;
1619
1620 SOURCING_LNUM = iptr->isn_lnum;
1621 // This is very much like ISN_EXEC
1622 CLEAR_FIELD(cookie);
1623 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1624 res = do_cmdline(subs->subs_cmd,
1625 getsourceline, &cookie,
1626 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1627 substitute_instr = save_instr;
1628
1629 if (res == FAIL || did_emsg
1630 || subs_instr.subs_status == FAIL)
1631 goto on_error;
1632 }
1633 break;
1634
1635 case ISN_FINISH:
1636 goto done;
1637
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001638 case ISN_REDIRSTART:
1639 // create a dummy entry for var_redir_str()
1640 if (alloc_redir_lval() == FAIL)
1641 goto on_error;
1642
1643 // The output is stored in growarray "redir_ga" until
1644 // redirection ends.
1645 init_redir_ga();
1646 redir_vname = 1;
1647 break;
1648
1649 case ISN_REDIREND:
1650 {
1651 char_u *res = get_clear_redir_ga();
1652
1653 // End redirection, put redirected text on the stack.
1654 clear_redir_lval();
1655 redir_vname = 0;
1656
1657 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1658 {
1659 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001660 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001661 }
1662 tv = STACK_TV_BOT(0);
1663 tv->v_type = VAR_STRING;
1664 tv->vval.v_string = res;
1665 ++ectx->ec_stack.ga_len;
1666 }
1667 break;
1668
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001669 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001670#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001671 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1672 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001673#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001674 break;
1675
1676 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001677#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001678 {
1679 exarg_T ea;
1680 int res;
1681
1682 CLEAR_FIELD(ea);
1683 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1684 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1685 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1686 --ectx->ec_stack.ga_len;
1687 tv = STACK_TV_BOT(0);
1688 res = cexpr_core(&ea, tv);
1689 clear_tv(tv);
1690 if (res == FAIL)
1691 goto on_error;
1692 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001693#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001694 break;
1695
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001696 // execute Ex command from pieces on the stack
1697 case ISN_EXECCONCAT:
1698 {
1699 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001700 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001701 int pass;
1702 int i;
1703 char_u *cmd = NULL;
1704 char_u *str;
1705
1706 for (pass = 1; pass <= 2; ++pass)
1707 {
1708 for (i = 0; i < count; ++i)
1709 {
1710 tv = STACK_TV_BOT(i - count);
1711 str = tv->vval.v_string;
1712 if (str != NULL && *str != NUL)
1713 {
1714 if (pass == 2)
1715 STRCPY(cmd + len, str);
1716 len += STRLEN(str);
1717 }
1718 if (pass == 2)
1719 clear_tv(tv);
1720 }
1721 if (pass == 1)
1722 {
1723 cmd = alloc(len + 1);
1724 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001725 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001726 len = 0;
1727 }
1728 }
1729
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001730 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001731 do_cmdline_cmd(cmd);
1732 vim_free(cmd);
1733 }
1734 break;
1735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 // execute :echo {string} ...
1737 case ISN_ECHO:
1738 {
1739 int count = iptr->isn_arg.echo.echo_count;
1740 int atstart = TRUE;
1741 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001742 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
1744 for (idx = 0; idx < count; ++idx)
1745 {
1746 tv = STACK_TV_BOT(idx - count);
1747 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1748 &atstart, &needclr);
1749 clear_tv(tv);
1750 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001751 if (needclr)
1752 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001753 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 }
1755 break;
1756
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001757 // :execute {string} ...
1758 // :echomsg {string} ...
1759 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001760 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001761 case ISN_ECHOMSG:
1762 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001763 {
1764 int count = iptr->isn_arg.number;
1765 garray_T ga;
1766 char_u buf[NUMBUFLEN];
1767 char_u *p;
1768 int len;
1769 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001770 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001771
1772 ga_init2(&ga, 1, 80);
1773 for (idx = 0; idx < count; ++idx)
1774 {
1775 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001776 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001777 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001778 if (tv->v_type == VAR_CHANNEL
1779 || tv->v_type == VAR_JOB)
1780 {
1781 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001782 semsg(_(e_using_invalid_value_as_string_str),
1783 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001784 break;
1785 }
1786 else
1787 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001788 }
1789 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001790 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001791
1792 len = (int)STRLEN(p);
1793 if (ga_grow(&ga, len + 2) == FAIL)
1794 failed = TRUE;
1795 else
1796 {
1797 if (ga.ga_len > 0)
1798 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1799 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1800 ga.ga_len += len;
1801 }
1802 clear_tv(tv);
1803 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001804 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001805 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001806 {
1807 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001808 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001809 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001810
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001811 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001812 {
1813 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001814 {
1815 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001816 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001817 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001818 {
1819 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001820 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001821 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001822 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001823 else
1824 {
1825 msg_sb_eol();
1826 if (iptr->isn_type == ISN_ECHOMSG)
1827 {
1828 msg_attr(ga.ga_data, echo_attr);
1829 out_flush();
1830 }
1831 else
1832 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001833 SOURCING_LNUM = iptr->isn_lnum;
1834 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001835 }
1836 }
1837 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001838 ga_clear(&ga);
1839 }
1840 break;
1841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 // load local variable or argument
1843 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001844 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001845 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001847 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 break;
1849
1850 // load v: variable
1851 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001852 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001853 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001855 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 break;
1857
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001858 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 case ISN_LOADSCRIPT:
1860 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001861 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 svar_T *sv;
1863
Bram Moolenaar4c137212021-04-19 16:48:48 +02001864 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001865 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001866 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001867 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001868 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001869 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001871 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 }
1873 break;
1874
1875 // load s: variable in old script
1876 case ISN_LOADS:
1877 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001878 hashtab_T *ht = &SCRIPT_VARS(
1879 iptr->isn_arg.loadstore.ls_sid);
1880 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 if (di == NULL)
1884 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001885 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001886 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001887 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 }
1889 else
1890 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001891 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001892 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001894 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 }
1896 }
1897 break;
1898
Bram Moolenaard3aac292020-04-19 14:32:17 +02001899 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001901 case ISN_LOADB:
1902 case ISN_LOADW:
1903 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001905 dictitem_T *di = NULL;
1906 hashtab_T *ht = NULL;
1907 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001908
Bram Moolenaard3aac292020-04-19 14:32:17 +02001909 switch (iptr->isn_type)
1910 {
1911 case ISN_LOADG:
1912 ht = get_globvar_ht();
1913 namespace = 'g';
1914 break;
1915 case ISN_LOADB:
1916 ht = &curbuf->b_vars->dv_hashtab;
1917 namespace = 'b';
1918 break;
1919 case ISN_LOADW:
1920 ht = &curwin->w_vars->dv_hashtab;
1921 namespace = 'w';
1922 break;
1923 case ISN_LOADT:
1924 ht = &curtab->tp_vars->dv_hashtab;
1925 namespace = 't';
1926 break;
1927 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001928 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001929 }
1930 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 if (di == NULL)
1933 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001934 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001935 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001936 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001937 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 }
1939 else
1940 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001941 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001942 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001944 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 }
1946 }
1947 break;
1948
Bram Moolenaar03290b82020-12-19 16:30:44 +01001949 // load autoload variable
1950 case ISN_LOADAUTO:
1951 {
1952 char_u *name = iptr->isn_arg.string;
1953
Bram Moolenaar4c137212021-04-19 16:48:48 +02001954 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001955 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001956 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001957 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001958 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001959 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001960 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001961 }
1962 break;
1963
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001964 // load g:/b:/w:/t: namespace
1965 case ISN_LOADGDICT:
1966 case ISN_LOADBDICT:
1967 case ISN_LOADWDICT:
1968 case ISN_LOADTDICT:
1969 {
1970 dict_T *d = NULL;
1971
1972 switch (iptr->isn_type)
1973 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001974 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1975 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1976 case ISN_LOADWDICT: d = curwin->w_vars; break;
1977 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001978 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001979 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001980 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001981 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001982 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001983 tv = STACK_TV_BOT(0);
1984 tv->v_type = VAR_DICT;
1985 tv->v_lock = 0;
1986 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001987 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001988 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001989 }
1990 break;
1991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 // load &option
1993 case ISN_LOADOPT:
1994 {
1995 typval_T optval;
1996 char_u *name = iptr->isn_arg.string;
1997
Bram Moolenaara8c17702020-04-01 21:17:24 +02001998 // This is not expected to fail, name is checked during
1999 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002000 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002001 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002002 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002003 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002005 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 }
2007 break;
2008
2009 // load $ENV
2010 case ISN_LOADENV:
2011 {
2012 typval_T optval;
2013 char_u *name = iptr->isn_arg.string;
2014
Bram Moolenaar4c137212021-04-19 16:48:48 +02002015 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002016 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002017 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002018 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002020 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 }
2022 break;
2023
2024 // load @register
2025 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002026 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002027 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 tv = STACK_TV_BOT(0);
2029 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002030 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002031 // This may result in NULL, which should be equivalent to an
2032 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 tv->vval.v_string = get_reg_contents(
2034 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002035 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 break;
2037
2038 // store local variable
2039 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002040 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 tv = STACK_TV_VAR(iptr->isn_arg.number);
2042 clear_tv(tv);
2043 *tv = *STACK_TV_BOT(0);
2044 break;
2045
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002046 // store s: variable in old script
2047 case ISN_STORES:
2048 {
2049 hashtab_T *ht = &SCRIPT_VARS(
2050 iptr->isn_arg.loadstore.ls_sid);
2051 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002052 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002053
Bram Moolenaar4c137212021-04-19 16:48:48 +02002054 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002055 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002056 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002057 else
2058 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002059 SOURCING_LNUM = iptr->isn_lnum;
2060 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002061 {
2062 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002063 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002064 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002065 clear_tv(&di->di_tv);
2066 di->di_tv = *STACK_TV_BOT(0);
2067 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002068 }
2069 break;
2070
2071 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 case ISN_STORESCRIPT:
2073 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002074 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2075 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076
Bram Moolenaar4c137212021-04-19 16:48:48 +02002077 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002078 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002079 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002080 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002081
2082 // "const" and "final" are checked at compile time, locking
2083 // the value needs to be checked here.
2084 SOURCING_LNUM = iptr->isn_lnum;
2085 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002086 {
2087 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002088 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002089 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 clear_tv(sv->sv_tv);
2092 *sv->sv_tv = *STACK_TV_BOT(0);
2093 }
2094 break;
2095
2096 // store option
2097 case ISN_STOREOPT:
2098 {
2099 long n = 0;
2100 char_u *s = NULL;
2101 char *msg;
2102
Bram Moolenaar4c137212021-04-19 16:48:48 +02002103 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 tv = STACK_TV_BOT(0);
2105 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002106 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002108 if (s == NULL)
2109 s = (char_u *)"";
2110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002112 // must be VAR_NUMBER, CHECKTYPE makes sure
2113 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2115 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002116 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 if (msg != NULL)
2118 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002119 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002121 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 }
2124 break;
2125
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002126 // store $ENV
2127 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002128 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002129 tv = STACK_TV_BOT(0);
2130 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2131 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002132 break;
2133
2134 // store @r
2135 case ISN_STOREREG:
2136 {
2137 int reg = iptr->isn_arg.number;
2138
Bram Moolenaar4c137212021-04-19 16:48:48 +02002139 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002140 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002141 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002142 tv_get_string(tv), -1, FALSE);
2143 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002144 }
2145 break;
2146
2147 // store v: variable
2148 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002149 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002150 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2151 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002152 // should not happen, type is checked when compiling
2153 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002154 break;
2155
Bram Moolenaard3aac292020-04-19 14:32:17 +02002156 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002158 case ISN_STOREB:
2159 case ISN_STOREW:
2160 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002162 dictitem_T *di;
2163 hashtab_T *ht;
2164 char_u *name = iptr->isn_arg.string + 2;
2165
Bram Moolenaard3aac292020-04-19 14:32:17 +02002166 switch (iptr->isn_type)
2167 {
2168 case ISN_STOREG:
2169 ht = get_globvar_ht();
2170 break;
2171 case ISN_STOREB:
2172 ht = &curbuf->b_vars->dv_hashtab;
2173 break;
2174 case ISN_STOREW:
2175 ht = &curwin->w_vars->dv_hashtab;
2176 break;
2177 case ISN_STORET:
2178 ht = &curtab->tp_vars->dv_hashtab;
2179 break;
2180 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002181 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183
Bram Moolenaar4c137212021-04-19 16:48:48 +02002184 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002185 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002187 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 else
2189 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002190 SOURCING_LNUM = iptr->isn_lnum;
2191 if (var_check_permission(di, name) == FAIL)
2192 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 clear_tv(&di->di_tv);
2194 di->di_tv = *STACK_TV_BOT(0);
2195 }
2196 }
2197 break;
2198
Bram Moolenaar03290b82020-12-19 16:30:44 +01002199 // store an autoload variable
2200 case ISN_STOREAUTO:
2201 SOURCING_LNUM = iptr->isn_lnum;
2202 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2203 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002204 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002205 break;
2206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 // store number in local variable
2208 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002209 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 clear_tv(tv);
2211 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002212 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 break;
2214
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002215 // store value in list or dict variable
2216 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002217 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002218 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002219 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002220 typval_T *tv_dest = STACK_TV_BOT(-1);
2221 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002222
Bram Moolenaar752fc692021-01-04 21:57:11 +01002223 // Stack contains:
2224 // -3 value to be stored
2225 // -2 index
2226 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002227 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002228 SOURCING_LNUM = iptr->isn_lnum;
2229 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002230 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002231 dest_type = tv_dest->v_type;
2232 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002233 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002234 else if (dest_type == VAR_LIST
2235 && tv_idx->v_type != VAR_NUMBER)
2236 {
2237 emsg(_(e_number_exp));
2238 status = FAIL;
2239 }
2240 }
2241 else if (dest_type != tv_dest->v_type)
2242 {
2243 // just in case, should be OK
2244 semsg(_(e_expected_str_but_got_str),
2245 vartype_name(dest_type),
2246 vartype_name(tv_dest->v_type));
2247 status = FAIL;
2248 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002249
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002250 if (status == OK && dest_type == VAR_LIST)
2251 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002252 long lidx = (long)tv_idx->vval.v_number;
2253 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002254
2255 if (list == NULL)
2256 {
2257 emsg(_(e_list_not_set));
2258 goto on_error;
2259 }
2260 if (lidx < 0 && list->lv_len + lidx >= 0)
2261 // negative index is relative to the end
2262 lidx = list->lv_len + lidx;
2263 if (lidx < 0 || lidx > list->lv_len)
2264 {
2265 semsg(_(e_listidx), lidx);
2266 goto on_error;
2267 }
2268 if (lidx < list->lv_len)
2269 {
2270 listitem_T *li = list_find(list, lidx);
2271
2272 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002273 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002274 goto on_error;
2275 // overwrite existing list item
2276 clear_tv(&li->li_tv);
2277 li->li_tv = *tv;
2278 }
2279 else
2280 {
2281 if (error_if_locked(list->lv_lock,
2282 e_cannot_change_list))
2283 goto on_error;
2284 // append to list, only fails when out of memory
2285 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002286 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002287 clear_tv(tv);
2288 }
2289 }
2290 else if (status == OK && dest_type == VAR_DICT)
2291 {
2292 char_u *key = tv_idx->vval.v_string;
2293 dict_T *dict = tv_dest->vval.v_dict;
2294 dictitem_T *di;
2295
2296 SOURCING_LNUM = iptr->isn_lnum;
2297 if (dict == NULL)
2298 {
2299 emsg(_(e_dictionary_not_set));
2300 goto on_error;
2301 }
2302 if (key == NULL)
2303 key = (char_u *)"";
2304 di = dict_find(dict, key, -1);
2305 if (di != NULL)
2306 {
2307 if (error_if_locked(di->di_tv.v_lock,
2308 e_cannot_change_dict_item))
2309 goto on_error;
2310 // overwrite existing value
2311 clear_tv(&di->di_tv);
2312 di->di_tv = *tv;
2313 }
2314 else
2315 {
2316 if (error_if_locked(dict->dv_lock,
2317 e_cannot_change_dict))
2318 goto on_error;
2319 // add to dict, only fails when out of memory
2320 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002321 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002322 clear_tv(tv);
2323 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002324 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002325 else if (status == OK && dest_type == VAR_BLOB)
2326 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002327 long lidx = (long)tv_idx->vval.v_number;
2328 blob_T *blob = tv_dest->vval.v_blob;
2329 varnumber_T nr;
2330 int error = FALSE;
2331 int len;
2332
2333 if (blob == NULL)
2334 {
2335 emsg(_(e_blob_not_set));
2336 goto on_error;
2337 }
2338 len = blob_len(blob);
2339 if (lidx < 0 && len + lidx >= 0)
2340 // negative index is relative to the end
2341 lidx = len + lidx;
2342
2343 // Can add one byte at the end.
2344 if (lidx < 0 || lidx > len)
2345 {
2346 semsg(_(e_blobidx), lidx);
2347 goto on_error;
2348 }
2349 if (value_check_lock(blob->bv_lock,
2350 (char_u *)"blob", FALSE))
2351 goto on_error;
2352 nr = tv_get_number_chk(tv, &error);
2353 if (error)
2354 goto on_error;
2355 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002356 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002357 else
2358 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002359 status = FAIL;
2360 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002361 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002362
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002363 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002364 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002365 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002366 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002367 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002368 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002369 goto on_error;
2370 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002371 }
2372 break;
2373
Bram Moolenaar68452172021-04-12 21:21:02 +02002374 // store value in blob range
2375 case ISN_STORERANGE:
2376 {
2377 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2378 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2379 typval_T *tv_dest = STACK_TV_BOT(-1);
2380 int status = OK;
2381
2382 // Stack contains:
2383 // -4 value to be stored
2384 // -3 first index or "none"
2385 // -2 second index or "none"
2386 // -1 destination blob
2387 tv = STACK_TV_BOT(-4);
2388 if (tv_dest->v_type != VAR_BLOB)
2389 {
2390 status = FAIL;
2391 emsg(_(e_blob_required));
2392 }
2393 else
2394 {
2395 varnumber_T n1;
2396 varnumber_T n2;
2397 int error = FALSE;
2398
2399 n1 = tv_get_number_chk(tv_idx1, &error);
2400 if (error)
2401 status = FAIL;
2402 else
2403 {
2404 if (tv_idx2->v_type == VAR_SPECIAL
2405 && tv_idx2->vval.v_number == VVAL_NONE)
2406 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2407 else
2408 n2 = tv_get_number_chk(tv_idx2, &error);
2409 if (error)
2410 status = FAIL;
2411 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002412 {
2413 long bloblen = blob_len(tv_dest->vval.v_blob);
2414
2415 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002416 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002417 || check_blob_range(bloblen,
2418 n1, n2, FALSE) == FAIL)
2419 status = FAIL;
2420 else
2421 status = blob_set_range(
2422 tv_dest->vval.v_blob, n1, n2, tv);
2423 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002424 }
2425 }
2426
2427 clear_tv(tv_idx1);
2428 clear_tv(tv_idx2);
2429 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002430 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002431 clear_tv(tv);
2432
2433 if (status == FAIL)
2434 goto on_error;
2435 }
2436 break;
2437
Bram Moolenaar0186e582021-01-10 18:33:11 +01002438 // load or store variable or argument from outer scope
2439 case ISN_LOADOUTER:
2440 case ISN_STOREOUTER:
2441 {
2442 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002443 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2444 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002445
2446 while (depth > 1 && outer != NULL)
2447 {
2448 outer = outer->out_up;
2449 --depth;
2450 }
2451 if (outer == NULL)
2452 {
2453 SOURCING_LNUM = iptr->isn_lnum;
2454 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002455 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002456 }
2457 tv = ((typval_T *)outer->out_stack->ga_data)
2458 + outer->out_frame_idx + STACK_FRAME_SIZE
2459 + iptr->isn_arg.outer.outer_idx;
2460 if (iptr->isn_type == ISN_LOADOUTER)
2461 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002462 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002463 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002464 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002465 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002466 }
2467 else
2468 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002469 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002470 clear_tv(tv);
2471 *tv = *STACK_TV_BOT(0);
2472 }
2473 }
2474 break;
2475
Bram Moolenaar752fc692021-01-04 21:57:11 +01002476 // unlet item in list or dict variable
2477 case ISN_UNLETINDEX:
2478 {
2479 typval_T *tv_idx = STACK_TV_BOT(-2);
2480 typval_T *tv_dest = STACK_TV_BOT(-1);
2481 int status = OK;
2482
2483 // Stack contains:
2484 // -2 index
2485 // -1 dict or list
2486 if (tv_dest->v_type == VAR_DICT)
2487 {
2488 // unlet a dict item, index must be a string
2489 if (tv_idx->v_type != VAR_STRING)
2490 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002491 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002492 semsg(_(e_expected_str_but_got_str),
2493 vartype_name(VAR_STRING),
2494 vartype_name(tv_idx->v_type));
2495 status = FAIL;
2496 }
2497 else
2498 {
2499 dict_T *d = tv_dest->vval.v_dict;
2500 char_u *key = tv_idx->vval.v_string;
2501 dictitem_T *di = NULL;
2502
2503 if (key == NULL)
2504 key = (char_u *)"";
2505 if (d != NULL)
2506 di = dict_find(d, key, (int)STRLEN(key));
2507 if (di == NULL)
2508 {
2509 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002510 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002511 semsg(_(e_dictkey), key);
2512 status = FAIL;
2513 }
2514 else
2515 {
2516 // TODO: check for dict or item locked
2517 dictitem_remove(d, di);
2518 }
2519 }
2520 }
2521 else if (tv_dest->v_type == VAR_LIST)
2522 {
2523 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002524 SOURCING_LNUM = iptr->isn_lnum;
2525 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002526 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002527 status = FAIL;
2528 }
2529 else
2530 {
2531 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002532 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002533 listitem_T *li = NULL;
2534
2535 li = list_find(l, n);
2536 if (li == NULL)
2537 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002538 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002539 semsg(_(e_listidx), n);
2540 status = FAIL;
2541 }
2542 else
2543 // TODO: check for list or item locked
2544 listitem_remove(l, li);
2545 }
2546 }
2547 else
2548 {
2549 status = FAIL;
2550 semsg(_(e_cannot_index_str),
2551 vartype_name(tv_dest->v_type));
2552 }
2553
2554 clear_tv(tv_idx);
2555 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002556 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002557 if (status == FAIL)
2558 goto on_error;
2559 }
2560 break;
2561
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002562 // unlet range of items in list variable
2563 case ISN_UNLETRANGE:
2564 {
2565 // Stack contains:
2566 // -3 index1
2567 // -2 index2
2568 // -1 dict or list
2569 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2570 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2571 typval_T *tv_dest = STACK_TV_BOT(-1);
2572 int status = OK;
2573
2574 if (tv_dest->v_type == VAR_LIST)
2575 {
2576 // indexes must be a number
2577 SOURCING_LNUM = iptr->isn_lnum;
2578 if (check_for_number(tv_idx1) == FAIL
2579 || check_for_number(tv_idx2) == FAIL)
2580 {
2581 status = FAIL;
2582 }
2583 else
2584 {
2585 list_T *l = tv_dest->vval.v_list;
2586 long n1 = (long)tv_idx1->vval.v_number;
2587 long n2 = (long)tv_idx2->vval.v_number;
2588 listitem_T *li;
2589
2590 li = list_find_index(l, &n1);
2591 if (li == NULL
2592 || list_unlet_range(l, li, NULL, n1,
2593 TRUE, n2) == FAIL)
2594 status = FAIL;
2595 }
2596 }
2597 else
2598 {
2599 status = FAIL;
2600 SOURCING_LNUM = iptr->isn_lnum;
2601 semsg(_(e_cannot_index_str),
2602 vartype_name(tv_dest->v_type));
2603 }
2604
2605 clear_tv(tv_idx1);
2606 clear_tv(tv_idx2);
2607 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002608 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002609 if (status == FAIL)
2610 goto on_error;
2611 }
2612 break;
2613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 // push constant
2615 case ISN_PUSHNR:
2616 case ISN_PUSHBOOL:
2617 case ISN_PUSHSPEC:
2618 case ISN_PUSHF:
2619 case ISN_PUSHS:
2620 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002621 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002622 case ISN_PUSHCHANNEL:
2623 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002624 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002625 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002627 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002628 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 switch (iptr->isn_type)
2630 {
2631 case ISN_PUSHNR:
2632 tv->v_type = VAR_NUMBER;
2633 tv->vval.v_number = iptr->isn_arg.number;
2634 break;
2635 case ISN_PUSHBOOL:
2636 tv->v_type = VAR_BOOL;
2637 tv->vval.v_number = iptr->isn_arg.number;
2638 break;
2639 case ISN_PUSHSPEC:
2640 tv->v_type = VAR_SPECIAL;
2641 tv->vval.v_number = iptr->isn_arg.number;
2642 break;
2643#ifdef FEAT_FLOAT
2644 case ISN_PUSHF:
2645 tv->v_type = VAR_FLOAT;
2646 tv->vval.v_float = iptr->isn_arg.fnumber;
2647 break;
2648#endif
2649 case ISN_PUSHBLOB:
2650 blob_copy(iptr->isn_arg.blob, tv);
2651 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002652 case ISN_PUSHFUNC:
2653 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002654 if (iptr->isn_arg.string == NULL)
2655 tv->vval.v_string = NULL;
2656 else
2657 tv->vval.v_string =
2658 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002659 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002660 case ISN_PUSHCHANNEL:
2661#ifdef FEAT_JOB_CHANNEL
2662 tv->v_type = VAR_CHANNEL;
2663 tv->vval.v_channel = iptr->isn_arg.channel;
2664 if (tv->vval.v_channel != NULL)
2665 ++tv->vval.v_channel->ch_refcount;
2666#endif
2667 break;
2668 case ISN_PUSHJOB:
2669#ifdef FEAT_JOB_CHANNEL
2670 tv->v_type = VAR_JOB;
2671 tv->vval.v_job = iptr->isn_arg.job;
2672 if (tv->vval.v_job != NULL)
2673 ++tv->vval.v_job->jv_refcount;
2674#endif
2675 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 default:
2677 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002678 tv->vval.v_string = vim_strsave(
2679 iptr->isn_arg.string == NULL
2680 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 }
2682 break;
2683
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002684 case ISN_UNLET:
2685 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2686 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002687 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002688 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002689 case ISN_UNLETENV:
2690 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2691 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002692
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002693 case ISN_LOCKCONST:
2694 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2695 break;
2696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 // create a list from items on the stack; uses a single allocation
2698 // for the list header and the items
2699 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002700 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002701 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 break;
2703
2704 // create a dict from items on the stack
2705 case ISN_NEWDICT:
2706 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002707 int count = iptr->isn_arg.number;
2708 dict_T *dict = dict_alloc();
2709 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002710 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002711 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712
2713 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002714 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 for (idx = 0; idx < count; ++idx)
2716 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002717 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002719 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002720 key = tv->vval.v_string == NULL
2721 ? (char_u *)"" : tv->vval.v_string;
2722 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002723 if (item != NULL)
2724 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002725 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002726 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002727 dict_unref(dict);
2728 goto on_error;
2729 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002730 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 clear_tv(tv);
2732 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002733 {
2734 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002735 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2738 item->di_tv.v_lock = 0;
2739 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002740 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002741 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002742 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002743 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 }
2746
2747 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002748 ectx->ec_stack.ga_len -= 2 * count - 1;
2749 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002750 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002752 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 tv = STACK_TV_BOT(-1);
2754 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002755 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 tv->vval.v_dict = dict;
2757 ++dict->dv_refcount;
2758 }
2759 break;
2760
2761 // call a :def function
2762 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002763 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002764 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2765 NULL,
2766 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002767 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002768 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 break;
2770
2771 // call a builtin function
2772 case ISN_BCALL:
2773 SOURCING_LNUM = iptr->isn_lnum;
2774 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2775 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002776 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002777 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 break;
2779
2780 // call a funcref or partial
2781 case ISN_PCALL:
2782 {
2783 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2784 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002785 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786
2787 SOURCING_LNUM = iptr->isn_lnum;
2788 if (pfunc->cpf_top)
2789 {
2790 // funcref is above the arguments
2791 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2792 }
2793 else
2794 {
2795 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002796 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002797 partial_tv = *STACK_TV_BOT(0);
2798 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002800 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002801 if (tv == &partial_tv)
2802 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002804 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 }
2806 break;
2807
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002808 case ISN_PCALL_END:
2809 // PCALL finished, arguments have been consumed and replaced by
2810 // the return value. Now clear the funcref from the stack,
2811 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002812 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002813 clear_tv(STACK_TV_BOT(-1));
2814 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2815 break;
2816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 // call a user defined function or funcref/partial
2818 case ISN_UCALL:
2819 {
2820 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2821
2822 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002823 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002824 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002825 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 }
2827 break;
2828
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002829 // return from a :def function call without a value
2830 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002831 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002832 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002833 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002834 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002835 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002836 tv->vval.v_number = 0;
2837 tv->v_lock = 0;
2838 // FALLTHROUGH
2839
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002840 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 case ISN_RETURN:
2842 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002843 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002844 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002845
2846 if (trystack->ga_len > 0)
2847 trycmd = ((trycmd_T *)trystack->ga_data)
2848 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002849 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002850 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002852 // jump to ":finally" or ":endtry"
2853 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002854 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002855 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002856 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 trycmd->tcd_return = TRUE;
2858 }
2859 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002860 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 }
2862 break;
2863
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002864 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 case ISN_FUNCREF:
2866 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002867 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2868 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2869 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002872 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002873 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002874 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002875 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002876 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002877 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002878 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002879 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002880 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002882 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 tv->vval.v_partial = pt;
2884 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002885 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 }
2887 break;
2888
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002889 // Create a global function from a lambda.
2890 case ISN_NEWFUNC:
2891 {
2892 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2893
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002894 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002895 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002896 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002897 }
2898 break;
2899
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002900 // List functions
2901 case ISN_DEF:
2902 if (iptr->isn_arg.string == NULL)
2903 list_functions(NULL);
2904 else
2905 {
2906 exarg_T ea;
2907
2908 CLEAR_FIELD(ea);
2909 ea.cmd = ea.arg = iptr->isn_arg.string;
2910 define_function(&ea, NULL);
2911 }
2912 break;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 // jump if a condition is met
2915 case ISN_JUMP:
2916 {
2917 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002918 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 int jump = TRUE;
2920
2921 if (when != JUMP_ALWAYS)
2922 {
2923 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002924 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002925 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002926 || when == JUMP_IF_COND_TRUE)
2927 {
2928 SOURCING_LNUM = iptr->isn_lnum;
2929 jump = tv_get_bool_chk(tv, &error);
2930 if (error)
2931 goto on_error;
2932 }
2933 else
2934 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002936 || when == JUMP_AND_KEEP_IF_FALSE
2937 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002939 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 {
2941 // drop the value from the stack
2942 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002943 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 }
2945 }
2946 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002947 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 }
2949 break;
2950
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002951 // Jump if an argument with a default value was already set and not
2952 // v:none.
2953 case ISN_JUMP_IF_ARG_SET:
2954 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2955 if (tv->v_type != VAR_UNKNOWN
2956 && !(tv->v_type == VAR_SPECIAL
2957 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002958 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002959 break;
2960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 // top of a for loop
2962 case ISN_FOR:
2963 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002964 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 typval_T *idxtv =
2966 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2967
Bram Moolenaar4c137212021-04-19 16:48:48 +02002968 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002969 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002970 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002971 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002972 list_T *list = ltv->vval.v_list;
2973
2974 // push the next item from the list
2975 ++idxtv->vval.v_number;
2976 if (list == NULL
2977 || idxtv->vval.v_number >= list->lv_len)
2978 {
2979 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002980 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2981 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002982 }
2983 else if (list->lv_first == &range_list_item)
2984 {
2985 // non-materialized range() list
2986 tv = STACK_TV_BOT(0);
2987 tv->v_type = VAR_NUMBER;
2988 tv->v_lock = 0;
2989 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002991 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002992 }
2993 else
2994 {
2995 listitem_T *li = list_find(list,
2996 idxtv->vval.v_number);
2997
2998 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002999 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003000 }
3001 }
3002 else if (ltv->v_type == VAR_STRING)
3003 {
3004 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003005
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003006 // The index is for the last byte of the previous
3007 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003008 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003009 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003010 {
3011 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003012 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3013 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003014 }
3015 else
3016 {
3017 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3018
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003019 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003020 tv = STACK_TV_BOT(0);
3021 tv->v_type = VAR_STRING;
3022 tv->vval.v_string = vim_strnsave(
3023 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003024 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003025 idxtv->vval.v_number += clen - 1;
3026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003028 else if (ltv->v_type == VAR_BLOB)
3029 {
3030 blob_T *blob = ltv->vval.v_blob;
3031
3032 // When we get here the first time make a copy of the
3033 // blob, so that the iteration still works when it is
3034 // changed.
3035 if (idxtv->vval.v_number == -1 && blob != NULL)
3036 {
3037 blob_copy(blob, ltv);
3038 blob_unref(blob);
3039 blob = ltv->vval.v_blob;
3040 }
3041
3042 // The index is for the previous byte.
3043 ++idxtv->vval.v_number;
3044 if (blob == NULL
3045 || idxtv->vval.v_number >= blob_len(blob))
3046 {
3047 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003048 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3049 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003050 }
3051 else
3052 {
3053 // Push the next byte from the blob.
3054 tv = STACK_TV_BOT(0);
3055 tv->v_type = VAR_NUMBER;
3056 tv->vval.v_number = blob_get(blob,
3057 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003058 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003059 }
3060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 else
3062 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003063 semsg(_(e_for_loop_on_str_not_supported),
3064 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003065 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 }
3067 }
3068 break;
3069
3070 // start of ":try" block
3071 case ISN_TRY:
3072 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003073 trycmd_T *trycmd = NULL;
3074
Bram Moolenaar4c137212021-04-19 16:48:48 +02003075 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003076 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003077 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3078 + ectx->ec_trystack.ga_len;
3079 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003081 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003082 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3083 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003084 trycmd->tcd_catch_idx =
3085 iptr->isn_arg.try.try_ref->try_catch;
3086 trycmd->tcd_finally_idx =
3087 iptr->isn_arg.try.try_ref->try_finally;
3088 trycmd->tcd_endtry_idx =
3089 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 }
3091 break;
3092
3093 case ISN_PUSHEXC:
3094 if (current_exception == NULL)
3095 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003096 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003098 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003100 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003101 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003103 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003105 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 tv->vval.v_string = vim_strsave(
3107 (char_u *)current_exception->value);
3108 break;
3109
3110 case ISN_CATCH:
3111 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003112 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113
Bram Moolenaar4c137212021-04-19 16:48:48 +02003114 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 if (trystack->ga_len > 0)
3116 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003117 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 + trystack->ga_len - 1;
3119 trycmd->tcd_caught = TRUE;
3120 }
3121 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003122 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 catch_exception(current_exception);
3124 }
3125 break;
3126
Bram Moolenaarc150c092021-02-13 15:02:46 +01003127 case ISN_TRYCONT:
3128 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003129 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003130 trycont_T *trycont = &iptr->isn_arg.trycont;
3131 int i;
3132 trycmd_T *trycmd;
3133 int iidx = trycont->tct_where;
3134
3135 if (trystack->ga_len < trycont->tct_levels)
3136 {
3137 siemsg("TRYCONT: expected %d levels, found %d",
3138 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003139 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003140 }
3141 // Make :endtry jump to any outer try block and the last
3142 // :endtry inside the loop to the loop start.
3143 for (i = trycont->tct_levels; i > 0; --i)
3144 {
3145 trycmd = ((trycmd_T *)trystack->ga_data)
3146 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003147 // Add one to tcd_cont to be able to jump to
3148 // instruction with index zero.
3149 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003150 iidx = trycmd->tcd_finally_idx == 0
3151 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003152 }
3153 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003154 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003155 }
3156 break;
3157
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003158 case ISN_FINALLY:
3159 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003160 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003161 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3162 + trystack->ga_len - 1;
3163
3164 // Reset the index to avoid a return statement jumps here
3165 // again.
3166 trycmd->tcd_finally_idx = 0;
3167 break;
3168 }
3169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 // end of ":try" block
3171 case ISN_ENDTRY:
3172 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003173 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174
3175 if (trystack->ga_len > 0)
3176 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003177 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 --trystack->ga_len;
3180 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003181 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 trycmd = ((trycmd_T *)trystack->ga_data)
3183 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003184 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 {
3186 // discard the exception
3187 if (caught_stack == current_exception)
3188 caught_stack = caught_stack->caught;
3189 discard_current_exception();
3190 }
3191
3192 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003193 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003194
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003196 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003197 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003198 clear_tv(STACK_TV_BOT(0));
3199 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003200 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003201 // handling :continue: jump to outer try block or
3202 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003203 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 }
3205 }
3206 break;
3207
3208 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003209 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003210 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003211
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003212 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3213 {
3214 // throwing an exception while using "silent!" causes
3215 // the function to abort but not display an error.
3216 tv = STACK_TV_BOT(-1);
3217 clear_tv(tv);
3218 tv->v_type = VAR_NUMBER;
3219 tv->vval.v_number = 0;
3220 goto done;
3221 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003222 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003223 tv = STACK_TV_BOT(0);
3224 if (tv->vval.v_string == NULL
3225 || *skipwhite(tv->vval.v_string) == NUL)
3226 {
3227 vim_free(tv->vval.v_string);
3228 SOURCING_LNUM = iptr->isn_lnum;
3229 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003230 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003231 }
3232
3233 // Inside a "catch" we need to first discard the caught
3234 // exception.
3235 if (trystack->ga_len > 0)
3236 {
3237 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3238 + trystack->ga_len - 1;
3239 if (trycmd->tcd_caught && current_exception != NULL)
3240 {
3241 // discard the exception
3242 if (caught_stack == current_exception)
3243 caught_stack = caught_stack->caught;
3244 discard_current_exception();
3245 trycmd->tcd_caught = FALSE;
3246 }
3247 }
3248
3249 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3250 == FAIL)
3251 {
3252 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003253 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003254 }
3255 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 break;
3258
3259 // compare with special values
3260 case ISN_COMPAREBOOL:
3261 case ISN_COMPARESPECIAL:
3262 {
3263 typval_T *tv1 = STACK_TV_BOT(-2);
3264 typval_T *tv2 = STACK_TV_BOT(-1);
3265 varnumber_T arg1 = tv1->vval.v_number;
3266 varnumber_T arg2 = tv2->vval.v_number;
3267 int res;
3268
3269 switch (iptr->isn_arg.op.op_type)
3270 {
3271 case EXPR_EQUAL: res = arg1 == arg2; break;
3272 case EXPR_NEQUAL: res = arg1 != arg2; break;
3273 default: res = 0; break;
3274 }
3275
Bram Moolenaar4c137212021-04-19 16:48:48 +02003276 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 tv1->v_type = VAR_BOOL;
3278 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3279 }
3280 break;
3281
3282 // Operation with two number arguments
3283 case ISN_OPNR:
3284 case ISN_COMPARENR:
3285 {
3286 typval_T *tv1 = STACK_TV_BOT(-2);
3287 typval_T *tv2 = STACK_TV_BOT(-1);
3288 varnumber_T arg1 = tv1->vval.v_number;
3289 varnumber_T arg2 = tv2->vval.v_number;
3290 varnumber_T res;
3291
3292 switch (iptr->isn_arg.op.op_type)
3293 {
3294 case EXPR_MULT: res = arg1 * arg2; break;
3295 case EXPR_DIV: res = arg1 / arg2; break;
3296 case EXPR_REM: res = arg1 % arg2; break;
3297 case EXPR_SUB: res = arg1 - arg2; break;
3298 case EXPR_ADD: res = arg1 + arg2; break;
3299
3300 case EXPR_EQUAL: res = arg1 == arg2; break;
3301 case EXPR_NEQUAL: res = arg1 != arg2; break;
3302 case EXPR_GREATER: res = arg1 > arg2; break;
3303 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3304 case EXPR_SMALLER: res = arg1 < arg2; break;
3305 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3306 default: res = 0; break;
3307 }
3308
Bram Moolenaar4c137212021-04-19 16:48:48 +02003309 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 if (iptr->isn_type == ISN_COMPARENR)
3311 {
3312 tv1->v_type = VAR_BOOL;
3313 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3314 }
3315 else
3316 tv1->vval.v_number = res;
3317 }
3318 break;
3319
3320 // Computation with two float arguments
3321 case ISN_OPFLOAT:
3322 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003323#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 {
3325 typval_T *tv1 = STACK_TV_BOT(-2);
3326 typval_T *tv2 = STACK_TV_BOT(-1);
3327 float_T arg1 = tv1->vval.v_float;
3328 float_T arg2 = tv2->vval.v_float;
3329 float_T res = 0;
3330 int cmp = FALSE;
3331
3332 switch (iptr->isn_arg.op.op_type)
3333 {
3334 case EXPR_MULT: res = arg1 * arg2; break;
3335 case EXPR_DIV: res = arg1 / arg2; break;
3336 case EXPR_SUB: res = arg1 - arg2; break;
3337 case EXPR_ADD: res = arg1 + arg2; break;
3338
3339 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3340 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3341 case EXPR_GREATER: cmp = arg1 > arg2; break;
3342 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3343 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3344 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3345 default: cmp = 0; break;
3346 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003347 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 if (iptr->isn_type == ISN_COMPAREFLOAT)
3349 {
3350 tv1->v_type = VAR_BOOL;
3351 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3352 }
3353 else
3354 tv1->vval.v_float = res;
3355 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003356#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 break;
3358
3359 case ISN_COMPARELIST:
3360 {
3361 typval_T *tv1 = STACK_TV_BOT(-2);
3362 typval_T *tv2 = STACK_TV_BOT(-1);
3363 list_T *arg1 = tv1->vval.v_list;
3364 list_T *arg2 = tv2->vval.v_list;
3365 int cmp = FALSE;
3366 int ic = iptr->isn_arg.op.op_ic;
3367
3368 switch (iptr->isn_arg.op.op_type)
3369 {
3370 case EXPR_EQUAL: cmp =
3371 list_equal(arg1, arg2, ic, FALSE); break;
3372 case EXPR_NEQUAL: cmp =
3373 !list_equal(arg1, arg2, ic, FALSE); break;
3374 case EXPR_IS: cmp = arg1 == arg2; break;
3375 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3376 default: cmp = 0; break;
3377 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003378 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 clear_tv(tv1);
3380 clear_tv(tv2);
3381 tv1->v_type = VAR_BOOL;
3382 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3383 }
3384 break;
3385
3386 case ISN_COMPAREBLOB:
3387 {
3388 typval_T *tv1 = STACK_TV_BOT(-2);
3389 typval_T *tv2 = STACK_TV_BOT(-1);
3390 blob_T *arg1 = tv1->vval.v_blob;
3391 blob_T *arg2 = tv2->vval.v_blob;
3392 int cmp = FALSE;
3393
3394 switch (iptr->isn_arg.op.op_type)
3395 {
3396 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3397 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3398 case EXPR_IS: cmp = arg1 == arg2; break;
3399 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3400 default: cmp = 0; break;
3401 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003402 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 clear_tv(tv1);
3404 clear_tv(tv2);
3405 tv1->v_type = VAR_BOOL;
3406 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3407 }
3408 break;
3409
3410 // TODO: handle separately
3411 case ISN_COMPARESTRING:
3412 case ISN_COMPAREDICT:
3413 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 case ISN_COMPAREANY:
3415 {
3416 typval_T *tv1 = STACK_TV_BOT(-2);
3417 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003418 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 int ic = iptr->isn_arg.op.op_ic;
3420
Bram Moolenaareb26f432020-09-14 16:50:05 +02003421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003422 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003424 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 }
3426 break;
3427
3428 case ISN_ADDLIST:
3429 case ISN_ADDBLOB:
3430 {
3431 typval_T *tv1 = STACK_TV_BOT(-2);
3432 typval_T *tv2 = STACK_TV_BOT(-1);
3433
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003434 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 if (iptr->isn_type == ISN_ADDLIST)
3436 eval_addlist(tv1, tv2);
3437 else
3438 eval_addblob(tv1, tv2);
3439 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003440 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 }
3442 break;
3443
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003444 case ISN_LISTAPPEND:
3445 {
3446 typval_T *tv1 = STACK_TV_BOT(-2);
3447 typval_T *tv2 = STACK_TV_BOT(-1);
3448 list_T *l = tv1->vval.v_list;
3449
3450 // add an item to a list
3451 if (l == NULL)
3452 {
3453 SOURCING_LNUM = iptr->isn_lnum;
3454 emsg(_(e_cannot_add_to_null_list));
3455 goto on_error;
3456 }
3457 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003458 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003459 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003460 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003461 }
3462 break;
3463
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003464 case ISN_BLOBAPPEND:
3465 {
3466 typval_T *tv1 = STACK_TV_BOT(-2);
3467 typval_T *tv2 = STACK_TV_BOT(-1);
3468 blob_T *b = tv1->vval.v_blob;
3469 int error = FALSE;
3470 varnumber_T n;
3471
3472 // add a number to a blob
3473 if (b == NULL)
3474 {
3475 SOURCING_LNUM = iptr->isn_lnum;
3476 emsg(_(e_cannot_add_to_null_blob));
3477 goto on_error;
3478 }
3479 n = tv_get_number_chk(tv2, &error);
3480 if (error)
3481 goto on_error;
3482 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003483 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003484 }
3485 break;
3486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 // Computation with two arguments of unknown type
3488 case ISN_OPANY:
3489 {
3490 typval_T *tv1 = STACK_TV_BOT(-2);
3491 typval_T *tv2 = STACK_TV_BOT(-1);
3492 varnumber_T n1, n2;
3493#ifdef FEAT_FLOAT
3494 float_T f1 = 0, f2 = 0;
3495#endif
3496 int error = FALSE;
3497
3498 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3499 {
3500 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3501 {
3502 eval_addlist(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 else if (tv1->v_type == VAR_BLOB
3508 && tv2->v_type == VAR_BLOB)
3509 {
3510 eval_addblob(tv1, tv2);
3511 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 break;
3514 }
3515 }
3516#ifdef FEAT_FLOAT
3517 if (tv1->v_type == VAR_FLOAT)
3518 {
3519 f1 = tv1->vval.v_float;
3520 n1 = 0;
3521 }
3522 else
3523#endif
3524 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003525 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 n1 = tv_get_number_chk(tv1, &error);
3527 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003528 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529#ifdef FEAT_FLOAT
3530 if (tv2->v_type == VAR_FLOAT)
3531 f1 = n1;
3532#endif
3533 }
3534#ifdef FEAT_FLOAT
3535 if (tv2->v_type == VAR_FLOAT)
3536 {
3537 f2 = tv2->vval.v_float;
3538 n2 = 0;
3539 }
3540 else
3541#endif
3542 {
3543 n2 = tv_get_number_chk(tv2, &error);
3544 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003545 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546#ifdef FEAT_FLOAT
3547 if (tv1->v_type == VAR_FLOAT)
3548 f2 = n2;
3549#endif
3550 }
3551#ifdef FEAT_FLOAT
3552 // if there is a float on either side the result is a float
3553 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3554 {
3555 switch (iptr->isn_arg.op.op_type)
3556 {
3557 case EXPR_MULT: f1 = f1 * f2; break;
3558 case EXPR_DIV: f1 = f1 / f2; break;
3559 case EXPR_SUB: f1 = f1 - f2; break;
3560 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003561 default: SOURCING_LNUM = iptr->isn_lnum;
3562 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003563 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 }
3565 clear_tv(tv1);
3566 clear_tv(tv2);
3567 tv1->v_type = VAR_FLOAT;
3568 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003569 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 }
3571 else
3572#endif
3573 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003574 int failed = FALSE;
3575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 switch (iptr->isn_arg.op.op_type)
3577 {
3578 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003579 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3580 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003581 goto on_error;
3582 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 case EXPR_SUB: n1 = n1 - n2; break;
3584 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003585 default: n1 = num_modulus(n1, n2, &failed);
3586 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003587 goto on_error;
3588 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 }
3590 clear_tv(tv1);
3591 clear_tv(tv2);
3592 tv1->v_type = VAR_NUMBER;
3593 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003594 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 }
3596 }
3597 break;
3598
3599 case ISN_CONCAT:
3600 {
3601 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3602 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3603 char_u *res;
3604
3605 res = concat_str(str1, str2);
3606 clear_tv(STACK_TV_BOT(-2));
3607 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003608 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 STACK_TV_BOT(-1)->vval.v_string = res;
3610 }
3611 break;
3612
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003613 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003614 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003615 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003616 int is_slice = iptr->isn_type == ISN_STRSLICE;
3617 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003618 char_u *res;
3619
3620 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003621 // string slice: string is at stack-3, first index at
3622 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003623 if (is_slice)
3624 {
3625 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003626 n1 = tv->vval.v_number;
3627 }
3628
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003629 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003630 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003631
Bram Moolenaar4c137212021-04-19 16:48:48 +02003632 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003633 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003634 if (is_slice)
3635 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003636 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003637 else
3638 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003639 // single character (including composing characters).
3640 // If the index is too big or negative the result is
3641 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003642 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003643 vim_free(tv->vval.v_string);
3644 tv->vval.v_string = res;
3645 }
3646 break;
3647
3648 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003649 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003650 case ISN_BLOBINDEX:
3651 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003653 int is_slice = iptr->isn_type == ISN_LISTSLICE
3654 || iptr->isn_type == ISN_BLOBSLICE;
3655 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3656 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003657 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003658 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659
3660 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003661 // list slice: list is at stack-3, indexes at stack-2 and
3662 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003663 // Same for blob.
3664 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665
3666 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003667 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003669
3670 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 {
Bram Moolenaared591872020-08-15 22:14:53 +02003672 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003673 n1 = tv->vval.v_number;
3674 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 }
Bram Moolenaared591872020-08-15 22:14:53 +02003676
Bram Moolenaar4c137212021-04-19 16:48:48 +02003677 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003678 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003679 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003680 if (is_blob)
3681 {
3682 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3683 n1, n2, FALSE, tv) == FAIL)
3684 goto on_error;
3685 }
3686 else
3687 {
3688 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3689 n1, n2, FALSE, tv, TRUE) == FAIL)
3690 goto on_error;
3691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 }
3693 break;
3694
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003695 case ISN_ANYINDEX:
3696 case ISN_ANYSLICE:
3697 {
3698 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3699 typval_T *var1, *var2;
3700 int res;
3701
3702 // index: composite is at stack-2, index at stack-1
3703 // slice: composite is at stack-3, indexes at stack-2 and
3704 // stack-1
3705 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003706 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003707 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3708 goto on_error;
3709 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3710 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003711 res = eval_index_inner(tv, is_slice, var1, var2,
3712 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003713 clear_tv(var1);
3714 if (is_slice)
3715 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003716 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003717 if (res == FAIL)
3718 goto on_error;
3719 }
3720 break;
3721
Bram Moolenaar9af78762020-06-16 11:34:42 +02003722 case ISN_SLICE:
3723 {
3724 list_T *list;
3725 int count = iptr->isn_arg.number;
3726
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003727 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003728 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003729 list = tv->vval.v_list;
3730
3731 // no error for short list, expect it to be checked earlier
3732 if (list != NULL && list->lv_len >= count)
3733 {
3734 list_T *newlist = list_slice(list,
3735 count, list->lv_len - 1);
3736
3737 if (newlist != NULL)
3738 {
3739 list_unref(list);
3740 tv->vval.v_list = newlist;
3741 ++newlist->lv_refcount;
3742 }
3743 }
3744 }
3745 break;
3746
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003747 case ISN_GETITEM:
3748 {
3749 listitem_T *li;
3750 int index = iptr->isn_arg.number;
3751
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003752 // Get list item: list is at stack-1, push item.
3753 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003754 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003755 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003756
Bram Moolenaar4c137212021-04-19 16:48:48 +02003757 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003758 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003759 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003760 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003761
3762 // Useful when used in unpack assignment. Reset at
3763 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003764 ectx->ec_where.wt_index = index + 1;
3765 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003766 }
3767 break;
3768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 case ISN_MEMBER:
3770 {
3771 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003772 char_u *key;
3773 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003774 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003775
3776 // dict member: dict is at stack-2, key at stack-1
3777 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003778 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003779 dict = tv->vval.v_dict;
3780
3781 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003782 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003783 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003784 if (key == NULL)
3785 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003786
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003787 if ((di = dict_find(dict, key, -1)) == NULL)
3788 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003789 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003790 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003791
3792 // If :silent! is used we will continue, make sure the
3793 // stack contents makes sense.
3794 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003795 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003796 tv = STACK_TV_BOT(-1);
3797 clear_tv(tv);
3798 tv->v_type = VAR_NUMBER;
3799 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003800 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003801 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003802 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003803 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003804 // Clear the dict only after getting the item, to avoid
3805 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003806 tv = STACK_TV_BOT(-1);
3807 temp_tv = *tv;
3808 copy_tv(&di->di_tv, tv);
3809 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003810 }
3811 break;
3812
3813 // dict member with string key
3814 case ISN_STRINGMEMBER:
3815 {
3816 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003818 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819
3820 tv = STACK_TV_BOT(-1);
3821 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3822 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003823 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003825 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 }
3827 dict = tv->vval.v_dict;
3828
3829 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3830 == NULL)
3831 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003832 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003834 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003836 // Clear the dict after getting the item, to avoid that it
3837 // make the item invalid.
3838 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003840 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 }
3842 break;
3843
3844 case ISN_NEGATENR:
3845 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003846 if (tv->v_type != VAR_NUMBER
3847#ifdef FEAT_FLOAT
3848 && tv->v_type != VAR_FLOAT
3849#endif
3850 )
3851 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003852 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003853 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003854 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003855 }
3856#ifdef FEAT_FLOAT
3857 if (tv->v_type == VAR_FLOAT)
3858 tv->vval.v_float = -tv->vval.v_float;
3859 else
3860#endif
3861 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 break;
3863
3864 case ISN_CHECKNR:
3865 {
3866 int error = FALSE;
3867
3868 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003869 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003871 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 (void)tv_get_number_chk(tv, &error);
3873 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003874 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 }
3876 break;
3877
3878 case ISN_CHECKTYPE:
3879 {
3880 checktype_T *ct = &iptr->isn_arg.type;
3881
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003882 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003883 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003884 if (!ectx->ec_where.wt_variable)
3885 ectx->ec_where.wt_index = ct->ct_arg_idx;
3886 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3887 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003888 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003889 if (!ectx->ec_where.wt_variable)
3890 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003891
3892 // number 0 is FALSE, number 1 is TRUE
3893 if (tv->v_type == VAR_NUMBER
3894 && ct->ct_type->tt_type == VAR_BOOL
3895 && (tv->vval.v_number == 0
3896 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003898 tv->v_type = VAR_BOOL;
3899 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003900 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 }
3902 }
3903 break;
3904
Bram Moolenaar9af78762020-06-16 11:34:42 +02003905 case ISN_CHECKLEN:
3906 {
3907 int min_len = iptr->isn_arg.checklen.cl_min_len;
3908 list_T *list = NULL;
3909
3910 tv = STACK_TV_BOT(-1);
3911 if (tv->v_type == VAR_LIST)
3912 list = tv->vval.v_list;
3913 if (list == NULL || list->lv_len < min_len
3914 || (list->lv_len > min_len
3915 && !iptr->isn_arg.checklen.cl_more_OK))
3916 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003917 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003918 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003919 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003920 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003921 }
3922 }
3923 break;
3924
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003925 case ISN_SETTYPE:
3926 {
3927 checktype_T *ct = &iptr->isn_arg.type;
3928
3929 tv = STACK_TV_BOT(-1);
3930 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3931 {
3932 free_type(tv->vval.v_dict->dv_type);
3933 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3934 }
3935 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3936 {
3937 free_type(tv->vval.v_list->lv_type);
3938 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3939 }
3940 }
3941 break;
3942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003944 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 {
3946 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003947 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003949 if (iptr->isn_type == ISN_2BOOL)
3950 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003951 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003952 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003953 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003954 n = !n;
3955 }
3956 else
3957 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003958 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003959 SOURCING_LNUM = iptr->isn_lnum;
3960 n = tv_get_bool_chk(tv, &error);
3961 if (error)
3962 goto on_error;
3963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 clear_tv(tv);
3965 tv->v_type = VAR_BOOL;
3966 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3967 }
3968 break;
3969
3970 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003971 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003972 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003973 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3974 iptr->isn_type == ISN_2STRING_ANY,
3975 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003976 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 break;
3978
Bram Moolenaar08597872020-12-10 19:43:40 +01003979 case ISN_RANGE:
3980 {
3981 exarg_T ea;
3982 char *errormsg;
3983
Bram Moolenaarece0b872021-01-08 20:40:45 +01003984 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003985 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003986 ea.addr_type = ADDR_LINES;
3987 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003988 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003989 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003990 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003991
Bram Moolenaar4c137212021-04-19 16:48:48 +02003992 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003993 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003994 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003995 tv = STACK_TV_BOT(-1);
3996 tv->v_type = VAR_NUMBER;
3997 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003998 if (ea.addr_count == 0)
3999 tv->vval.v_number = curwin->w_cursor.lnum;
4000 else
4001 tv->vval.v_number = ea.line2;
4002 }
4003 break;
4004
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004005 case ISN_PUT:
4006 {
4007 int regname = iptr->isn_arg.put.put_regname;
4008 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4009 char_u *expr = NULL;
4010 int dir = FORWARD;
4011
Bram Moolenaar08597872020-12-10 19:43:40 +01004012 if (lnum < -2)
4013 {
4014 // line number was put on the stack by ISN_RANGE
4015 tv = STACK_TV_BOT(-1);
4016 curwin->w_cursor.lnum = tv->vval.v_number;
4017 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4018 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004019 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004020 }
4021 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004022 // :put! above cursor
4023 dir = BACKWARD;
4024 else if (lnum >= 0)
4025 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004026
4027 if (regname == '=')
4028 {
4029 tv = STACK_TV_BOT(-1);
4030 if (tv->v_type == VAR_STRING)
4031 expr = tv->vval.v_string;
4032 else
4033 {
4034 expr = typval2string(tv, TRUE); // allocates value
4035 clear_tv(tv);
4036 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004037 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004038 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004039 check_cursor();
4040 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4041 vim_free(expr);
4042 }
4043 break;
4044
Bram Moolenaar02194d22020-10-24 23:08:38 +02004045 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004046 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4047 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4048 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4049 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004050 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4051 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004052 break;
4053
Bram Moolenaar02194d22020-10-24 23:08:38 +02004054 case ISN_CMDMOD_REV:
4055 // filter regprog is owned by the instruction, don't free it
4056 cmdmod.cmod_filter_regmatch.regprog = NULL;
4057 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004058 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4059 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004060 break;
4061
Bram Moolenaar792f7862020-11-23 08:31:18 +01004062 case ISN_UNPACK:
4063 {
4064 int count = iptr->isn_arg.unpack.unp_count;
4065 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4066 list_T *l;
4067 listitem_T *li;
4068 int i;
4069
4070 // Check there is a valid list to unpack.
4071 tv = STACK_TV_BOT(-1);
4072 if (tv->v_type != VAR_LIST)
4073 {
4074 SOURCING_LNUM = iptr->isn_lnum;
4075 emsg(_(e_for_argument_must_be_sequence_of_lists));
4076 goto on_error;
4077 }
4078 l = tv->vval.v_list;
4079 if (l == NULL
4080 || l->lv_len < (semicolon ? count - 1 : count))
4081 {
4082 SOURCING_LNUM = iptr->isn_lnum;
4083 emsg(_(e_list_value_does_not_have_enough_items));
4084 goto on_error;
4085 }
4086 else if (!semicolon && l->lv_len > count)
4087 {
4088 SOURCING_LNUM = iptr->isn_lnum;
4089 emsg(_(e_list_value_has_more_items_than_targets));
4090 goto on_error;
4091 }
4092
4093 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004094 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004095 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004096 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004097
4098 // Variable after semicolon gets a list with the remaining
4099 // items.
4100 if (semicolon)
4101 {
4102 list_T *rem_list =
4103 list_alloc_with_items(l->lv_len - count + 1);
4104
4105 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004106 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004107 tv = STACK_TV_BOT(-count);
4108 tv->vval.v_list = rem_list;
4109 ++rem_list->lv_refcount;
4110 tv->v_lock = 0;
4111 li = l->lv_first;
4112 for (i = 0; i < count - 1; ++i)
4113 li = li->li_next;
4114 for (i = 0; li != NULL; ++i)
4115 {
4116 list_set_item(rem_list, i, &li->li_tv);
4117 li = li->li_next;
4118 }
4119 --count;
4120 }
4121
4122 // Produce the values in reverse order, first item last.
4123 li = l->lv_first;
4124 for (i = 0; i < count; ++i)
4125 {
4126 tv = STACK_TV_BOT(-i - 1);
4127 copy_tv(&li->li_tv, tv);
4128 li = li->li_next;
4129 }
4130
4131 list_unref(l);
4132 }
4133 break;
4134
Bram Moolenaarb2049902021-01-24 12:53:53 +01004135 case ISN_PROF_START:
4136 case ISN_PROF_END:
4137 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004138#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004139 funccall_T cookie;
4140 ufunc_T *cur_ufunc =
4141 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004142 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004143
4144 cookie.func = cur_ufunc;
4145 if (iptr->isn_type == ISN_PROF_START)
4146 {
4147 func_line_start(&cookie, iptr->isn_lnum);
4148 // if we get here the instruction is executed
4149 func_line_exec(&cookie);
4150 }
4151 else
4152 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004153#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004154 }
4155 break;
4156
Bram Moolenaare99d4222021-06-13 14:01:26 +02004157 case ISN_DEBUG:
4158 if (ex_nesting_level <= debug_break_level)
4159 {
4160 char_u *line;
4161 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
4162 + ectx->ec_dfunc_idx)->df_ufunc;
4163
4164 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004165 debug_context = ectx;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02004166 debug_var_count = iptr->isn_arg.number;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004167 line = ((char_u **)ufunc->uf_lines.ga_data)[
4168 iptr->isn_lnum - 1];
4169 if (line == NULL)
4170 line = (char_u *)"[empty]";
4171 do_debug(line);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004172 debug_context = NULL;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004173 }
4174 break;
4175
Bram Moolenaar389df252020-07-09 21:20:47 +02004176 case ISN_SHUFFLE:
4177 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004178 typval_T tmp_tv;
4179 int item = iptr->isn_arg.shuffle.shfl_item;
4180 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004181
4182 tmp_tv = *STACK_TV_BOT(-item);
4183 for ( ; up > 0 && item > 1; --up)
4184 {
4185 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4186 --item;
4187 }
4188 *STACK_TV_BOT(-item) = tmp_tv;
4189 }
4190 break;
4191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004193 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004195 ectx->ec_where.wt_index = 0;
4196 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 break;
4198 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004199 continue;
4200
Bram Moolenaard032f342020-07-18 18:13:02 +02004201func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004202 // Restore previous function. If the frame pointer is where we started
4203 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004204 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004205 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004206
Bram Moolenaar4c137212021-04-19 16:48:48 +02004207 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004208 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004209 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004210 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004211
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004212on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004213 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004214 // If "emsg_silent" is set then ignore the error, unless it was set
4215 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004216 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004217 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004218 {
4219 // If a sequence of instructions causes an error while ":silent!"
4220 // was used, restore the stack length and jump ahead to restoring
4221 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004222 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004223 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004224 while (ectx->ec_stack.ga_len
4225 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004226 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004227 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004228 clear_tv(STACK_TV_BOT(0));
4229 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004230 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4231 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004232 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004233 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004234 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004235on_fatal_error:
4236 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004237 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004238 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004239 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 }
4241
4242done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004243 ret = OK;
4244theend:
4245 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4246 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004247}
4248
4249/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004250 * Execute the instructions from a VAR_INSTR typeval and put the result in
4251 * "rettv".
4252 * Return OK or FAIL.
4253 */
4254 int
4255exe_typval_instr(typval_T *tv, typval_T *rettv)
4256{
4257 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4258 isn_T *save_instr = ectx->ec_instr;
4259 int save_iidx = ectx->ec_iidx;
4260 int res;
4261
4262 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4263 res = exec_instructions(ectx);
4264 if (res == OK)
4265 {
4266 *rettv = *STACK_TV_BOT(-1);
4267 --ectx->ec_stack.ga_len;
4268 }
4269
4270 ectx->ec_instr = save_instr;
4271 ectx->ec_iidx = save_iidx;
4272
4273 return res;
4274}
4275
4276/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004277 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4278 * "substitute_instr".
4279 */
4280 char_u *
4281exe_substitute_instr(void)
4282{
4283 ectx_T *ectx = substitute_instr->subs_ectx;
4284 isn_T *save_instr = ectx->ec_instr;
4285 int save_iidx = ectx->ec_iidx;
4286 char_u *res;
4287
4288 ectx->ec_instr = substitute_instr->subs_instr;
4289 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004290 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004291 typval_T *tv = STACK_TV_BOT(-1);
4292
Bram Moolenaar27523602021-06-05 21:36:19 +02004293 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004294 --ectx->ec_stack.ga_len;
4295 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004296 }
4297 else
4298 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004299 substitute_instr->subs_status = FAIL;
4300 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302
Bram Moolenaar4c137212021-04-19 16:48:48 +02004303 ectx->ec_instr = save_instr;
4304 ectx->ec_iidx = save_iidx;
4305
4306 return res;
4307}
4308
4309/*
4310 * Call a "def" function from old Vim script.
4311 * Return OK or FAIL.
4312 */
4313 int
4314call_def_function(
4315 ufunc_T *ufunc,
4316 int argc_arg, // nr of arguments
4317 typval_T *argv, // arguments
4318 partial_T *partial, // optional partial for context
4319 typval_T *rettv) // return value
4320{
4321 ectx_T ectx; // execution context
4322 int argc = argc_arg;
4323 typval_T *tv;
4324 int idx;
4325 int ret = FAIL;
4326 int defcount = ufunc->uf_args.ga_len - argc;
4327 sctx_T save_current_sctx = current_sctx;
4328 int did_emsg_before = did_emsg_cumul + did_emsg;
4329 int save_suppress_errthrow = suppress_errthrow;
4330 msglist_T **saved_msg_list = NULL;
4331 msglist_T *private_msg_list = NULL;
4332 int save_emsg_silent_def = emsg_silent_def;
4333 int save_did_emsg_def = did_emsg_def;
4334 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004335 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004336
4337// Get pointer to item in the stack.
4338#undef STACK_TV
4339#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4340
4341// Get pointer to item at the bottom of the stack, -1 is the bottom.
4342#undef STACK_TV_BOT
4343#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4344
4345// Get pointer to a local variable on the stack. Negative for arguments.
4346#undef STACK_TV_VAR
4347#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4348
4349 if (ufunc->uf_def_status == UF_NOT_COMPILED
4350 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004351 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4352 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004353 == FAIL))
4354 {
4355 if (did_emsg_cumul + did_emsg == did_emsg_before)
4356 semsg(_(e_function_is_not_compiled_str),
4357 printable_func_name(ufunc));
4358 return FAIL;
4359 }
4360
4361 {
4362 // Check the function was really compiled.
4363 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4364 + ufunc->uf_dfunc_idx;
4365 if (INSTRUCTIONS(dfunc) == NULL)
4366 {
4367 iemsg("using call_def_function() on not compiled function");
4368 return FAIL;
4369 }
4370 }
4371
4372 // If depth of calling is getting too high, don't execute the function.
4373 orig_funcdepth = funcdepth_get();
4374 if (funcdepth_increment() == FAIL)
4375 return FAIL;
4376
4377 CLEAR_FIELD(ectx);
4378 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4379 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4380 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4381 {
4382 funcdepth_decrement();
4383 return FAIL;
4384 }
4385 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4386 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4387 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004388 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004389
4390 idx = argc - ufunc->uf_args.ga_len;
4391 if (idx > 0 && ufunc->uf_va_name == NULL)
4392 {
4393 if (idx == 1)
4394 emsg(_(e_one_argument_too_many));
4395 else
4396 semsg(_(e_nr_arguments_too_many), idx);
4397 goto failed_early;
4398 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004399 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4400 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004401 {
4402 if (idx == -1)
4403 emsg(_(e_one_argument_too_few));
4404 else
4405 semsg(_(e_nr_arguments_too_few), -idx);
4406 goto failed_early;
4407 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004408
4409 // Put arguments on the stack, but no more than what the function expects.
4410 // A lambda can be called with more arguments than it uses.
4411 for (idx = 0; idx < argc
4412 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4413 ++idx)
4414 {
4415 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4416 && argv[idx].v_type == VAR_SPECIAL
4417 && argv[idx].vval.v_number == VVAL_NONE)
4418 {
4419 // Use the default value.
4420 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4421 }
4422 else
4423 {
4424 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4425 && check_typval_arg_type(
4426 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4427 goto failed_early;
4428 copy_tv(&argv[idx], STACK_TV_BOT(0));
4429 }
4430 ++ectx.ec_stack.ga_len;
4431 }
4432
4433 // Turn varargs into a list. Empty list if no args.
4434 if (ufunc->uf_va_name != NULL)
4435 {
4436 int vararg_count = argc - ufunc->uf_args.ga_len;
4437
4438 if (vararg_count < 0)
4439 vararg_count = 0;
4440 else
4441 argc -= vararg_count;
4442 if (exe_newlist(vararg_count, &ectx) == FAIL)
4443 goto failed_early;
4444
4445 // Check the type of the list items.
4446 tv = STACK_TV_BOT(-1);
4447 if (ufunc->uf_va_type != NULL
4448 && ufunc->uf_va_type != &t_list_any
4449 && ufunc->uf_va_type->tt_member != &t_any
4450 && tv->vval.v_list != NULL)
4451 {
4452 type_T *expected = ufunc->uf_va_type->tt_member;
4453 listitem_T *li = tv->vval.v_list->lv_first;
4454
4455 for (idx = 0; idx < vararg_count; ++idx)
4456 {
4457 if (check_typval_arg_type(expected, &li->li_tv,
4458 argc + idx + 1) == FAIL)
4459 goto failed_early;
4460 li = li->li_next;
4461 }
4462 }
4463
4464 if (defcount > 0)
4465 // Move varargs list to below missing default arguments.
4466 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4467 --ectx.ec_stack.ga_len;
4468 }
4469
4470 // Make space for omitted arguments, will store default value below.
4471 // Any varargs list goes after them.
4472 if (defcount > 0)
4473 for (idx = 0; idx < defcount; ++idx)
4474 {
4475 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4476 ++ectx.ec_stack.ga_len;
4477 }
4478 if (ufunc->uf_va_name != NULL)
4479 ++ectx.ec_stack.ga_len;
4480
4481 // Frame pointer points to just after arguments.
4482 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4483 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4484
4485 {
4486 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4487 + ufunc->uf_dfunc_idx;
4488 ufunc_T *base_ufunc = dfunc->df_ufunc;
4489
4490 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4491 // by copy_func().
4492 if (partial != NULL || base_ufunc->uf_partial != NULL)
4493 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004494 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4495 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004496 goto failed_early;
4497 if (partial != NULL)
4498 {
4499 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4500 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004501 if (current_ectx->ec_outer_ref != NULL
4502 && current_ectx->ec_outer_ref->or_outer != NULL)
4503 ectx.ec_outer_ref->or_outer =
4504 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004505 }
4506 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004507 {
4508 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4509 ++partial->pt_refcount;
4510 ectx.ec_outer_ref->or_partial = partial;
4511 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004512 }
4513 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004514 {
4515 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4516 ++base_ufunc->uf_partial->pt_refcount;
4517 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4518 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004519 }
4520 }
4521
4522 // dummy frame entries
4523 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4524 {
4525 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4526 ++ectx.ec_stack.ga_len;
4527 }
4528
4529 {
4530 // Reserve space for local variables and any closure reference count.
4531 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4532 + ufunc->uf_dfunc_idx;
4533
4534 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4535 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4536 ectx.ec_stack.ga_len += dfunc->df_varcount;
4537 if (dfunc->df_has_closure)
4538 {
4539 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4540 STACK_TV_VAR(idx)->vval.v_number = 0;
4541 ++ectx.ec_stack.ga_len;
4542 }
4543
4544 ectx.ec_instr = INSTRUCTIONS(dfunc);
4545 }
4546
4547 // Following errors are in the function, not the caller.
4548 // Commands behave like vim9script.
4549 estack_push_ufunc(ufunc, 1);
4550 current_sctx = ufunc->uf_script_ctx;
4551 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4552
4553 // Use a specific location for storing error messages to be converted to an
4554 // exception.
4555 saved_msg_list = msg_list;
4556 msg_list = &private_msg_list;
4557
4558 // Do turn errors into exceptions.
4559 suppress_errthrow = FALSE;
4560
4561 // Do not delete the function while executing it.
4562 ++ufunc->uf_calls;
4563
4564 // When ":silent!" was used before calling then we still abort the
4565 // function. If ":silent!" is used in the function then we don't.
4566 emsg_silent_def = emsg_silent;
4567 did_emsg_def = 0;
4568
4569 ectx.ec_where.wt_index = 0;
4570 ectx.ec_where.wt_variable = FALSE;
4571
4572 // Execute the instructions until done.
4573 ret = exec_instructions(&ectx);
4574 if (ret == OK)
4575 {
4576 // function finished, get result from the stack.
4577 if (ufunc->uf_ret_type == &t_void)
4578 {
4579 rettv->v_type = VAR_VOID;
4580 }
4581 else
4582 {
4583 tv = STACK_TV_BOT(-1);
4584 *rettv = *tv;
4585 tv->v_type = VAR_UNKNOWN;
4586 }
4587 }
4588
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004589 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004590 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4591 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004592
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004593 // Deal with any remaining closures, they may be in use somewhere.
4594 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004595 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004596 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004597 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4598 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004599
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004600 estack_pop();
4601 current_sctx = save_current_sctx;
4602
Bram Moolenaarc970e422021-03-17 15:03:04 +01004603 // TODO: when is it safe to delete the function if it is no longer used?
4604 --ufunc->uf_calls;
4605
Bram Moolenaar352134b2020-10-17 22:04:08 +02004606 if (*msg_list != NULL && saved_msg_list != NULL)
4607 {
4608 msglist_T **plist = saved_msg_list;
4609
4610 // Append entries from the current msg_list (uncaught exceptions) to
4611 // the saved msg_list.
4612 while (*plist != NULL)
4613 plist = &(*plist)->next;
4614
4615 *plist = *msg_list;
4616 }
4617 msg_list = saved_msg_list;
4618
Bram Moolenaar4c137212021-04-19 16:48:48 +02004619 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004620 {
4621 cmdmod.cmod_filter_regmatch.regprog = NULL;
4622 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004623 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004624 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004625 emsg_silent_def = save_emsg_silent_def;
4626 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004627
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004628failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004629 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4631 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004632 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004635 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004636 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004637 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004638 if (ectx.ec_outer_ref->or_outer_allocated)
4639 vim_free(ectx.ec_outer_ref->or_outer);
4640 partial_unref(ectx.ec_outer_ref->or_partial);
4641 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004642 }
4643
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004644 // Not sure if this is necessary.
4645 suppress_errthrow = save_suppress_errthrow;
4646
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004647 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004648 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004649 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004650 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 return ret;
4652}
4653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004655 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4656 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4657 * "pfx" is prefixed to every line.
4658 */
4659 static void
4660list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4661{
4662 int line_idx = 0;
4663 int prev_current = 0;
4664 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004665 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004666
4667 for (current = 0; current < instr_count; ++current)
4668 {
4669 isn_T *iptr = &instr[current];
4670 char *line;
4671
4672 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004673 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004674 while (line_idx < iptr->isn_lnum
4675 && line_idx < ufunc->uf_lines.ga_len)
4676 {
4677 if (current > prev_current)
4678 {
4679 msg_puts("\n\n");
4680 prev_current = current;
4681 }
4682 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4683 if (line != NULL)
4684 msg(line);
4685 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004686 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4687 {
4688 int first_def_arg = ufunc->uf_args.ga_len
4689 - ufunc->uf_def_args.ga_len;
4690
4691 if (def_arg_idx > 0)
4692 msg_puts("\n\n");
4693 msg_start();
4694 msg_puts(" ");
4695 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4696 first_def_arg + def_arg_idx]);
4697 msg_puts(" = ");
4698 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4699 msg_clr_eos();
4700 msg_end();
4701 }
4702 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004703
4704 switch (iptr->isn_type)
4705 {
4706 case ISN_EXEC:
4707 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4708 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004709 case ISN_EXEC_SPLIT:
4710 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4711 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004712 case ISN_LEGACY_EVAL:
4713 smsg("%s%4d EVAL legacy %s", pfx, current,
4714 iptr->isn_arg.string);
4715 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004716 case ISN_REDIRSTART:
4717 smsg("%s%4d REDIR", pfx, current);
4718 break;
4719 case ISN_REDIREND:
4720 smsg("%s%4d REDIR END%s", pfx, current,
4721 iptr->isn_arg.number ? " append" : "");
4722 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004723 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004724#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004725 smsg("%s%4d CEXPR pre %s", pfx, current,
4726 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004727#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004728 break;
4729 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004730#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004731 {
4732 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4733
4734 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4735 cexpr_get_auname(cer->cer_cmdidx),
4736 cer->cer_forceit ? "!" : "",
4737 cer->cer_cmdline);
4738 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004739#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004740 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004741 case ISN_INSTR:
4742 {
4743 smsg("%s%4d INSTR", pfx, current);
4744 list_instructions(" ", iptr->isn_arg.instr,
4745 INT_MAX, NULL);
4746 msg(" -------------");
4747 }
4748 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004749 case ISN_SUBSTITUTE:
4750 {
4751 subs_T *subs = &iptr->isn_arg.subs;
4752
4753 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4754 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4755 msg(" -------------");
4756 }
4757 break;
4758 case ISN_EXECCONCAT:
4759 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4760 (varnumber_T)iptr->isn_arg.number);
4761 break;
4762 case ISN_ECHO:
4763 {
4764 echo_T *echo = &iptr->isn_arg.echo;
4765
4766 smsg("%s%4d %s %d", pfx, current,
4767 echo->echo_with_white ? "ECHO" : "ECHON",
4768 echo->echo_count);
4769 }
4770 break;
4771 case ISN_EXECUTE:
4772 smsg("%s%4d EXECUTE %lld", pfx, current,
4773 (varnumber_T)(iptr->isn_arg.number));
4774 break;
4775 case ISN_ECHOMSG:
4776 smsg("%s%4d ECHOMSG %lld", pfx, current,
4777 (varnumber_T)(iptr->isn_arg.number));
4778 break;
4779 case ISN_ECHOERR:
4780 smsg("%s%4d ECHOERR %lld", pfx, current,
4781 (varnumber_T)(iptr->isn_arg.number));
4782 break;
4783 case ISN_LOAD:
4784 {
4785 if (iptr->isn_arg.number < 0)
4786 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4787 (varnumber_T)(iptr->isn_arg.number
4788 + STACK_FRAME_SIZE));
4789 else
4790 smsg("%s%4d LOAD $%lld", pfx, current,
4791 (varnumber_T)(iptr->isn_arg.number));
4792 }
4793 break;
4794 case ISN_LOADOUTER:
4795 {
4796 if (iptr->isn_arg.number < 0)
4797 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4798 iptr->isn_arg.outer.outer_depth,
4799 iptr->isn_arg.outer.outer_idx
4800 + STACK_FRAME_SIZE);
4801 else
4802 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4803 iptr->isn_arg.outer.outer_depth,
4804 iptr->isn_arg.outer.outer_idx);
4805 }
4806 break;
4807 case ISN_LOADV:
4808 smsg("%s%4d LOADV v:%s", pfx, current,
4809 get_vim_var_name(iptr->isn_arg.number));
4810 break;
4811 case ISN_LOADSCRIPT:
4812 {
4813 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4814 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4815 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4816 + sref->sref_idx;
4817
4818 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4819 sv->sv_name,
4820 sref->sref_idx,
4821 si->sn_name);
4822 }
4823 break;
4824 case ISN_LOADS:
4825 {
4826 scriptitem_T *si = SCRIPT_ITEM(
4827 iptr->isn_arg.loadstore.ls_sid);
4828
4829 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4830 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4831 }
4832 break;
4833 case ISN_LOADAUTO:
4834 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4835 break;
4836 case ISN_LOADG:
4837 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4838 break;
4839 case ISN_LOADB:
4840 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4841 break;
4842 case ISN_LOADW:
4843 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4844 break;
4845 case ISN_LOADT:
4846 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4847 break;
4848 case ISN_LOADGDICT:
4849 smsg("%s%4d LOAD g:", pfx, current);
4850 break;
4851 case ISN_LOADBDICT:
4852 smsg("%s%4d LOAD b:", pfx, current);
4853 break;
4854 case ISN_LOADWDICT:
4855 smsg("%s%4d LOAD w:", pfx, current);
4856 break;
4857 case ISN_LOADTDICT:
4858 smsg("%s%4d LOAD t:", pfx, current);
4859 break;
4860 case ISN_LOADOPT:
4861 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4862 break;
4863 case ISN_LOADENV:
4864 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4865 break;
4866 case ISN_LOADREG:
4867 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4868 break;
4869
4870 case ISN_STORE:
4871 if (iptr->isn_arg.number < 0)
4872 smsg("%s%4d STORE arg[%lld]", pfx, current,
4873 iptr->isn_arg.number + STACK_FRAME_SIZE);
4874 else
4875 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4876 break;
4877 case ISN_STOREOUTER:
4878 {
4879 if (iptr->isn_arg.number < 0)
4880 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4881 iptr->isn_arg.outer.outer_depth,
4882 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4883 else
4884 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4885 iptr->isn_arg.outer.outer_depth,
4886 iptr->isn_arg.outer.outer_idx);
4887 }
4888 break;
4889 case ISN_STOREV:
4890 smsg("%s%4d STOREV v:%s", pfx, current,
4891 get_vim_var_name(iptr->isn_arg.number));
4892 break;
4893 case ISN_STOREAUTO:
4894 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4895 break;
4896 case ISN_STOREG:
4897 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4898 break;
4899 case ISN_STOREB:
4900 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4901 break;
4902 case ISN_STOREW:
4903 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4904 break;
4905 case ISN_STORET:
4906 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4907 break;
4908 case ISN_STORES:
4909 {
4910 scriptitem_T *si = SCRIPT_ITEM(
4911 iptr->isn_arg.loadstore.ls_sid);
4912
4913 smsg("%s%4d STORES %s in %s", pfx, current,
4914 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4915 }
4916 break;
4917 case ISN_STORESCRIPT:
4918 {
4919 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4920 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4921 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4922 + sref->sref_idx;
4923
4924 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4925 sv->sv_name,
4926 sref->sref_idx,
4927 si->sn_name);
4928 }
4929 break;
4930 case ISN_STOREOPT:
4931 smsg("%s%4d STOREOPT &%s", pfx, current,
4932 iptr->isn_arg.storeopt.so_name);
4933 break;
4934 case ISN_STOREENV:
4935 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4936 break;
4937 case ISN_STOREREG:
4938 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4939 break;
4940 case ISN_STORENR:
4941 smsg("%s%4d STORE %lld in $%d", pfx, current,
4942 iptr->isn_arg.storenr.stnr_val,
4943 iptr->isn_arg.storenr.stnr_idx);
4944 break;
4945
4946 case ISN_STOREINDEX:
4947 smsg("%s%4d STOREINDEX %s", pfx, current,
4948 vartype_name(iptr->isn_arg.vartype));
4949 break;
4950
4951 case ISN_STORERANGE:
4952 smsg("%s%4d STORERANGE", pfx, current);
4953 break;
4954
4955 // constants
4956 case ISN_PUSHNR:
4957 smsg("%s%4d PUSHNR %lld", pfx, current,
4958 (varnumber_T)(iptr->isn_arg.number));
4959 break;
4960 case ISN_PUSHBOOL:
4961 case ISN_PUSHSPEC:
4962 smsg("%s%4d PUSH %s", pfx, current,
4963 get_var_special_name(iptr->isn_arg.number));
4964 break;
4965 case ISN_PUSHF:
4966#ifdef FEAT_FLOAT
4967 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4968#endif
4969 break;
4970 case ISN_PUSHS:
4971 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4972 break;
4973 case ISN_PUSHBLOB:
4974 {
4975 char_u *r;
4976 char_u numbuf[NUMBUFLEN];
4977 char_u *tofree;
4978
4979 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4980 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4981 vim_free(tofree);
4982 }
4983 break;
4984 case ISN_PUSHFUNC:
4985 {
4986 char *name = (char *)iptr->isn_arg.string;
4987
4988 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4989 name == NULL ? "[none]" : name);
4990 }
4991 break;
4992 case ISN_PUSHCHANNEL:
4993#ifdef FEAT_JOB_CHANNEL
4994 {
4995 channel_T *channel = iptr->isn_arg.channel;
4996
4997 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4998 channel == NULL ? 0 : channel->ch_id);
4999 }
5000#endif
5001 break;
5002 case ISN_PUSHJOB:
5003#ifdef FEAT_JOB_CHANNEL
5004 {
5005 typval_T tv;
5006 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005007 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005008
5009 tv.v_type = VAR_JOB;
5010 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005011 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005012 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5013 }
5014#endif
5015 break;
5016 case ISN_PUSHEXC:
5017 smsg("%s%4d PUSH v:exception", pfx, current);
5018 break;
5019 case ISN_UNLET:
5020 smsg("%s%4d UNLET%s %s", pfx, current,
5021 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5022 iptr->isn_arg.unlet.ul_name);
5023 break;
5024 case ISN_UNLETENV:
5025 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5026 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5027 iptr->isn_arg.unlet.ul_name);
5028 break;
5029 case ISN_UNLETINDEX:
5030 smsg("%s%4d UNLETINDEX", pfx, current);
5031 break;
5032 case ISN_UNLETRANGE:
5033 smsg("%s%4d UNLETRANGE", pfx, current);
5034 break;
5035 case ISN_LOCKCONST:
5036 smsg("%s%4d LOCKCONST", pfx, current);
5037 break;
5038 case ISN_NEWLIST:
5039 smsg("%s%4d NEWLIST size %lld", pfx, current,
5040 (varnumber_T)(iptr->isn_arg.number));
5041 break;
5042 case ISN_NEWDICT:
5043 smsg("%s%4d NEWDICT size %lld", pfx, current,
5044 (varnumber_T)(iptr->isn_arg.number));
5045 break;
5046
5047 // function call
5048 case ISN_BCALL:
5049 {
5050 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5051
5052 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5053 internal_func_name(cbfunc->cbf_idx),
5054 cbfunc->cbf_argcount);
5055 }
5056 break;
5057 case ISN_DCALL:
5058 {
5059 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5060 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5061 + cdfunc->cdf_idx;
5062
5063 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5064 df->df_ufunc->uf_name_exp != NULL
5065 ? df->df_ufunc->uf_name_exp
5066 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5067 }
5068 break;
5069 case ISN_UCALL:
5070 {
5071 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5072
5073 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5074 cufunc->cuf_name, cufunc->cuf_argcount);
5075 }
5076 break;
5077 case ISN_PCALL:
5078 {
5079 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5080
5081 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5082 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5083 }
5084 break;
5085 case ISN_PCALL_END:
5086 smsg("%s%4d PCALL end", pfx, current);
5087 break;
5088 case ISN_RETURN:
5089 smsg("%s%4d RETURN", pfx, current);
5090 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005091 case ISN_RETURN_VOID:
5092 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005093 break;
5094 case ISN_FUNCREF:
5095 {
5096 funcref_T *funcref = &iptr->isn_arg.funcref;
5097 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5098 + funcref->fr_func;
5099
5100 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5101 }
5102 break;
5103
5104 case ISN_NEWFUNC:
5105 {
5106 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5107
5108 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5109 newfunc->nf_lambda, newfunc->nf_global);
5110 }
5111 break;
5112
5113 case ISN_DEF:
5114 {
5115 char_u *name = iptr->isn_arg.string;
5116
5117 smsg("%s%4d DEF %s", pfx, current,
5118 name == NULL ? (char_u *)"" : name);
5119 }
5120 break;
5121
5122 case ISN_JUMP:
5123 {
5124 char *when = "?";
5125
5126 switch (iptr->isn_arg.jump.jump_when)
5127 {
5128 case JUMP_ALWAYS:
5129 when = "JUMP";
5130 break;
5131 case JUMP_AND_KEEP_IF_TRUE:
5132 when = "JUMP_AND_KEEP_IF_TRUE";
5133 break;
5134 case JUMP_IF_FALSE:
5135 when = "JUMP_IF_FALSE";
5136 break;
5137 case JUMP_AND_KEEP_IF_FALSE:
5138 when = "JUMP_AND_KEEP_IF_FALSE";
5139 break;
5140 case JUMP_IF_COND_FALSE:
5141 when = "JUMP_IF_COND_FALSE";
5142 break;
5143 case JUMP_IF_COND_TRUE:
5144 when = "JUMP_IF_COND_TRUE";
5145 break;
5146 }
5147 smsg("%s%4d %s -> %d", pfx, current, when,
5148 iptr->isn_arg.jump.jump_where);
5149 }
5150 break;
5151
5152 case ISN_JUMP_IF_ARG_SET:
5153 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5154 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5155 iptr->isn_arg.jump.jump_where);
5156 break;
5157
5158 case ISN_FOR:
5159 {
5160 forloop_T *forloop = &iptr->isn_arg.forloop;
5161
5162 smsg("%s%4d FOR $%d -> %d", pfx, current,
5163 forloop->for_idx, forloop->for_end);
5164 }
5165 break;
5166
5167 case ISN_TRY:
5168 {
5169 try_T *try = &iptr->isn_arg.try;
5170
5171 if (try->try_ref->try_finally == 0)
5172 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5173 pfx, current,
5174 try->try_ref->try_catch,
5175 try->try_ref->try_endtry);
5176 else
5177 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5178 pfx, current,
5179 try->try_ref->try_catch,
5180 try->try_ref->try_finally,
5181 try->try_ref->try_endtry);
5182 }
5183 break;
5184 case ISN_CATCH:
5185 // TODO
5186 smsg("%s%4d CATCH", pfx, current);
5187 break;
5188 case ISN_TRYCONT:
5189 {
5190 trycont_T *trycont = &iptr->isn_arg.trycont;
5191
5192 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5193 trycont->tct_levels,
5194 trycont->tct_levels == 1 ? "" : "s",
5195 trycont->tct_where);
5196 }
5197 break;
5198 case ISN_FINALLY:
5199 smsg("%s%4d FINALLY", pfx, current);
5200 break;
5201 case ISN_ENDTRY:
5202 smsg("%s%4d ENDTRY", pfx, current);
5203 break;
5204 case ISN_THROW:
5205 smsg("%s%4d THROW", pfx, current);
5206 break;
5207
5208 // expression operations on number
5209 case ISN_OPNR:
5210 case ISN_OPFLOAT:
5211 case ISN_OPANY:
5212 {
5213 char *what;
5214 char *ins;
5215
5216 switch (iptr->isn_arg.op.op_type)
5217 {
5218 case EXPR_MULT: what = "*"; break;
5219 case EXPR_DIV: what = "/"; break;
5220 case EXPR_REM: what = "%"; break;
5221 case EXPR_SUB: what = "-"; break;
5222 case EXPR_ADD: what = "+"; break;
5223 default: what = "???"; break;
5224 }
5225 switch (iptr->isn_type)
5226 {
5227 case ISN_OPNR: ins = "OPNR"; break;
5228 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5229 case ISN_OPANY: ins = "OPANY"; break;
5230 default: ins = "???"; break;
5231 }
5232 smsg("%s%4d %s %s", pfx, current, ins, what);
5233 }
5234 break;
5235
5236 case ISN_COMPAREBOOL:
5237 case ISN_COMPARESPECIAL:
5238 case ISN_COMPARENR:
5239 case ISN_COMPAREFLOAT:
5240 case ISN_COMPARESTRING:
5241 case ISN_COMPAREBLOB:
5242 case ISN_COMPARELIST:
5243 case ISN_COMPAREDICT:
5244 case ISN_COMPAREFUNC:
5245 case ISN_COMPAREANY:
5246 {
5247 char *p;
5248 char buf[10];
5249 char *type;
5250
5251 switch (iptr->isn_arg.op.op_type)
5252 {
5253 case EXPR_EQUAL: p = "=="; break;
5254 case EXPR_NEQUAL: p = "!="; break;
5255 case EXPR_GREATER: p = ">"; break;
5256 case EXPR_GEQUAL: p = ">="; break;
5257 case EXPR_SMALLER: p = "<"; break;
5258 case EXPR_SEQUAL: p = "<="; break;
5259 case EXPR_MATCH: p = "=~"; break;
5260 case EXPR_IS: p = "is"; break;
5261 case EXPR_ISNOT: p = "isnot"; break;
5262 case EXPR_NOMATCH: p = "!~"; break;
5263 default: p = "???"; break;
5264 }
5265 STRCPY(buf, p);
5266 if (iptr->isn_arg.op.op_ic == TRUE)
5267 strcat(buf, "?");
5268 switch(iptr->isn_type)
5269 {
5270 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5271 case ISN_COMPARESPECIAL:
5272 type = "COMPARESPECIAL"; break;
5273 case ISN_COMPARENR: type = "COMPARENR"; break;
5274 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5275 case ISN_COMPARESTRING:
5276 type = "COMPARESTRING"; break;
5277 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5278 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5279 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5280 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5281 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5282 default: type = "???"; break;
5283 }
5284
5285 smsg("%s%4d %s %s", pfx, current, type, buf);
5286 }
5287 break;
5288
5289 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5290 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5291
5292 // expression operations
5293 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5294 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5295 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5296 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5297 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5298 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5299 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5300 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5301 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5302 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5303 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5304 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5305 pfx, current, iptr->isn_arg.number); break;
5306 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5307 pfx, current, iptr->isn_arg.number); break;
5308 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5309 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5310 iptr->isn_arg.string); break;
5311 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5312
5313 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5314 case ISN_CHECKTYPE:
5315 {
5316 checktype_T *ct = &iptr->isn_arg.type;
5317 char *tofree;
5318
5319 if (ct->ct_arg_idx == 0)
5320 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5321 type_name(ct->ct_type, &tofree),
5322 (int)ct->ct_off);
5323 else
5324 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5325 type_name(ct->ct_type, &tofree),
5326 (int)ct->ct_off,
5327 (int)ct->ct_arg_idx);
5328 vim_free(tofree);
5329 break;
5330 }
5331 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5332 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5333 iptr->isn_arg.checklen.cl_min_len);
5334 break;
5335 case ISN_SETTYPE:
5336 {
5337 char *tofree;
5338
5339 smsg("%s%4d SETTYPE %s", pfx, current,
5340 type_name(iptr->isn_arg.type.ct_type, &tofree));
5341 vim_free(tofree);
5342 break;
5343 }
5344 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005345 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5346 smsg("%s%4d INVERT %d (!val)", pfx, current,
5347 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005348 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005349 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5350 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005351 break;
5352 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005353 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005354 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005355 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5356 pfx, current,
5357 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005358 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005359 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5360 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005361 break;
5362 case ISN_PUT:
5363 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5364 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005365 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005366 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5367 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005368 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005369 else
5370 smsg("%s%4d PUT %c %ld", pfx, current,
5371 iptr->isn_arg.put.put_regname,
5372 (long)iptr->isn_arg.put.put_lnum);
5373 break;
5374
5375 // TODO: summarize modifiers
5376 case ISN_CMDMOD:
5377 {
5378 char_u *buf;
5379 size_t len = produce_cmdmods(
5380 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5381
5382 buf = alloc(len + 1);
5383 if (buf != NULL)
5384 {
5385 (void)produce_cmdmods(
5386 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5387 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5388 vim_free(buf);
5389 }
5390 break;
5391 }
5392 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5393
5394 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005395 smsg("%s%4d PROFILE START line %d", pfx, current,
5396 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005397 break;
5398
5399 case ISN_PROF_END:
5400 smsg("%s%4d PROFILE END", pfx, current);
5401 break;
5402
Bram Moolenaare99d4222021-06-13 14:01:26 +02005403 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005404 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5405 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005406 break;
5407
Bram Moolenaar4c137212021-04-19 16:48:48 +02005408 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5409 iptr->isn_arg.unpack.unp_count,
5410 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5411 break;
5412 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5413 iptr->isn_arg.shuffle.shfl_item,
5414 iptr->isn_arg.shuffle.shfl_up);
5415 break;
5416 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5417
5418 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5419 return;
5420 }
5421
5422 out_flush(); // output one line at a time
5423 ui_breakcheck();
5424 if (got_int)
5425 break;
5426 }
5427}
5428
5429/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005430 * Handle command line completion for the :disassemble command.
5431 */
5432 void
5433set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5434{
5435 char_u *p;
5436
5437 // Default: expand user functions, "debug" and "profile"
5438 xp->xp_context = EXPAND_DISASSEMBLE;
5439 xp->xp_pattern = arg;
5440
5441 // first argument already typed: only user function names
5442 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5443 {
5444 xp->xp_context = EXPAND_USER_FUNC;
5445 xp->xp_pattern = skipwhite(p);
5446 }
5447}
5448
5449/*
5450 * Function given to ExpandGeneric() to obtain the list of :disassemble
5451 * arguments.
5452 */
5453 char_u *
5454get_disassemble_argument(expand_T *xp, int idx)
5455{
5456 if (idx == 0)
5457 return (char_u *)"debug";
5458 if (idx == 1)
5459 return (char_u *)"profile";
5460 return get_user_func_name(xp, idx - 2);
5461}
5462
5463/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005464 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005465 * We don't really need this at runtime, but we do have tests that require it,
5466 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005467 */
5468 void
5469ex_disassemble(exarg_T *eap)
5470{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005471 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005472 char_u *fname;
5473 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 dfunc_T *dfunc;
5475 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005476 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005477 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005478 compiletype_T compile_type = CT_NONE;
5479
5480 if (STRNCMP(arg, "profile", 7) == 0)
5481 {
5482 compile_type = CT_PROFILE;
5483 arg = skipwhite(arg + 7);
5484 }
5485 else if (STRNCMP(arg, "debug", 5) == 0)
5486 {
5487 compile_type = CT_DEBUG;
5488 arg = skipwhite(arg + 5);
5489 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005491 if (STRNCMP(arg, "<lambda>", 8) == 0)
5492 {
5493 arg += 8;
5494 (void)getdigits(&arg);
5495 fname = vim_strnsave(eap->arg, arg - eap->arg);
5496 }
5497 else
5498 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005499 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005500 if (fname == NULL)
5501 {
5502 semsg(_(e_invarg2), eap->arg);
5503 return;
5504 }
5505
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005506 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005507 if (ufunc == NULL)
5508 {
5509 char_u *p = untrans_function_name(fname);
5510
5511 if (p != NULL)
5512 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005513 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005514 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005515 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516 if (ufunc == NULL)
5517 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005518 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519 return;
5520 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005521 if (func_needs_compiling(ufunc, compile_type)
5522 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005523 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005524 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005526 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527 return;
5528 }
5529 if (ufunc->uf_name_exp != NULL)
5530 msg((char *)ufunc->uf_name_exp);
5531 else
5532 msg((char *)ufunc->uf_name);
5533
5534 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005535 switch (compile_type)
5536 {
5537 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005538#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005539 instr = dfunc->df_instr_prof;
5540 instr_count = dfunc->df_instr_prof_count;
5541 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005542#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005543 // FALLTHROUGH
5544 case CT_NONE:
5545 instr = dfunc->df_instr;
5546 instr_count = dfunc->df_instr_count;
5547 break;
5548 case CT_DEBUG:
5549 instr = dfunc->df_instr_debug;
5550 instr_count = dfunc->df_instr_debug_count;
5551 break;
5552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553
Bram Moolenaar4c137212021-04-19 16:48:48 +02005554 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555}
5556
5557/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005558 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559 * list, etc. Mostly like what JavaScript does, except that empty list and
5560 * empty dictionary are FALSE.
5561 */
5562 int
5563tv2bool(typval_T *tv)
5564{
5565 switch (tv->v_type)
5566 {
5567 case VAR_NUMBER:
5568 return tv->vval.v_number != 0;
5569 case VAR_FLOAT:
5570#ifdef FEAT_FLOAT
5571 return tv->vval.v_float != 0.0;
5572#else
5573 break;
5574#endif
5575 case VAR_PARTIAL:
5576 return tv->vval.v_partial != NULL;
5577 case VAR_FUNC:
5578 case VAR_STRING:
5579 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5580 case VAR_LIST:
5581 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5582 case VAR_DICT:
5583 return tv->vval.v_dict != NULL
5584 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5585 case VAR_BOOL:
5586 case VAR_SPECIAL:
5587 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5588 case VAR_JOB:
5589#ifdef FEAT_JOB_CHANNEL
5590 return tv->vval.v_job != NULL;
5591#else
5592 break;
5593#endif
5594 case VAR_CHANNEL:
5595#ifdef FEAT_JOB_CHANNEL
5596 return tv->vval.v_channel != NULL;
5597#else
5598 break;
5599#endif
5600 case VAR_BLOB:
5601 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5602 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005603 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005604 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005605 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606 break;
5607 }
5608 return FALSE;
5609}
5610
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005611 void
5612emsg_using_string_as(typval_T *tv, int as_number)
5613{
5614 semsg(_(as_number ? e_using_string_as_number_str
5615 : e_using_string_as_bool_str),
5616 tv->vval.v_string == NULL
5617 ? (char_u *)"" : tv->vval.v_string);
5618}
5619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005620/*
5621 * If "tv" is a string give an error and return FAIL.
5622 */
5623 int
5624check_not_string(typval_T *tv)
5625{
5626 if (tv->v_type == VAR_STRING)
5627 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005628 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629 clear_tv(tv);
5630 return FAIL;
5631 }
5632 return OK;
5633}
5634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005635
5636#endif // FEAT_EVAL