blob: 218357a825b28ca8515596248632bf0c4c6838e9 [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 Moolenaar4cea5362021-06-16 22:24:40 +02001436 static void
1437handle_debug(isn_T *iptr, ectx_T *ectx)
1438{
1439 char_u *line;
1440 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1441 + ectx->ec_dfunc_idx)->df_ufunc;
1442 isn_T *ni;
1443 int end_lnum = iptr->isn_lnum;
1444 garray_T ga;
1445 int lnum;
1446
1447 SOURCING_LNUM = iptr->isn_lnum;
1448 debug_context = ectx;
1449 debug_var_count = iptr->isn_arg.number;
1450
1451 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1452 if (ni->isn_type == ISN_DEBUG
1453 || ni->isn_type == ISN_RETURN
1454 || ni->isn_type == ISN_RETURN_VOID)
1455 {
1456 end_lnum = ni->isn_lnum;
1457 break;
1458 }
1459
1460 if (end_lnum > iptr->isn_lnum)
1461 {
1462 ga_init2(&ga, sizeof(char_u *), 10);
1463 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001464 {
1465 char_u *p = skipwhite(
1466 ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1]);
1467
1468 if (*p == '#')
1469 break;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001470 if (ga_grow(&ga, 1) == OK)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001471 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1472 if (STRNCMP(p, "def ", 4) == 0)
1473 break;
1474 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001475 line = ga_concat_strings(&ga, " ");
1476 vim_free(ga.ga_data);
1477 }
1478 else
1479 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001480
Dominique Pelle6e969552021-06-17 13:53:41 +02001481 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001482 debug_context = NULL;
1483
1484 if (end_lnum > iptr->isn_lnum)
1485 vim_free(line);
1486}
1487
Bram Moolenaar4c137212021-04-19 16:48:48 +02001488/*
1489 * Execute instructions in execution context "ectx".
1490 * Return OK or FAIL;
1491 */
1492 static int
1493exec_instructions(ectx_T *ectx)
1494{
1495 int breakcheck_count = 0;
1496 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001497 int ret = FAIL;
1498 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001499
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001500 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001501 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001502
Bram Moolenaarff652882021-05-16 15:24:49 +02001503 // Only catch exceptions in this instruction list.
1504 ectx->ec_trylevel_at_start = trylevel;
1505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 for (;;)
1507 {
1508 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001509
Bram Moolenaar270d0382020-05-15 21:42:53 +02001510 if (++breakcheck_count >= 100)
1511 {
1512 line_breakcheck();
1513 breakcheck_count = 0;
1514 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001515 if (got_int)
1516 {
1517 // Turn CTRL-C into an exception.
1518 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001519 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001520 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001521 did_throw = TRUE;
1522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523
Bram Moolenaara26b9702020-04-18 19:53:28 +02001524 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1525 {
1526 // Turn an error message into an exception.
1527 did_emsg = FALSE;
1528 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001529 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001530 did_throw = TRUE;
1531 *msg_list = NULL;
1532 }
1533
Bram Moolenaar4c137212021-04-19 16:48:48 +02001534 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001536 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001537 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538
1539 // An exception jumps to the first catch, finally, or returns from
1540 // the current function.
1541 if (trystack->ga_len > 0)
1542 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001543 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 {
1545 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001546 ectx->ec_in_catch = TRUE;
1547 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 }
1549 else
1550 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001551 // Not inside try or need to return from current functions.
1552 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001553 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001554 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001555 tv = STACK_TV_BOT(0);
1556 tv->v_type = VAR_NUMBER;
1557 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001558 ++ectx->ec_stack.ga_len;
1559 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001561 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001562 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001563 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001564 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 goto done;
1566 }
1567
Bram Moolenaar4c137212021-04-19 16:48:48 +02001568 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001569 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 }
1571 continue;
1572 }
1573
Bram Moolenaar4c137212021-04-19 16:48:48 +02001574 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 switch (iptr->isn_type)
1576 {
1577 // execute Ex command line
1578 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001579 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001580 source_cookie_T cookie;
1581
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001582 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001583 // Pass getsourceline to get an error for a missing ":end"
1584 // command.
1585 CLEAR_FIELD(cookie);
1586 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1587 if (do_cmdline(iptr->isn_arg.string,
1588 getsourceline, &cookie,
1589 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1590 == FAIL
1591 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001592 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 break;
1595
Bram Moolenaar20677332021-06-06 17:02:53 +02001596 // execute Ex command line split at NL characters.
1597 case ISN_EXEC_SPLIT:
1598 {
1599 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001600 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001601
1602 SOURCING_LNUM = iptr->isn_lnum;
1603 CLEAR_FIELD(cookie);
1604 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1605 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001606 line = get_split_sourceline(0, &cookie, 0, 0);
1607 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001608 get_split_sourceline, &cookie,
1609 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1610 == FAIL
1611 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001612 {
1613 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001614 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001615 }
1616 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001617 }
1618 break;
1619
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001620 // Evaluate an expression with legacy syntax, push it onto the
1621 // stack.
1622 case ISN_LEGACY_EVAL:
1623 {
1624 char_u *arg = iptr->isn_arg.string;
1625 int res;
1626 int save_flags = cmdmod.cmod_flags;
1627
1628 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001629 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001630 tv = STACK_TV_BOT(0);
1631 init_tv(tv);
1632 cmdmod.cmod_flags |= CMOD_LEGACY;
1633 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1634 cmdmod.cmod_flags = save_flags;
1635 if (res == FAIL)
1636 goto on_error;
1637 ++ectx->ec_stack.ga_len;
1638 }
1639 break;
1640
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001641 // push typeval VAR_INSTR with instructions to be executed
1642 case ISN_INSTR:
1643 {
1644 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001645 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001646 tv = STACK_TV_BOT(0);
1647 tv->vval.v_instr = ALLOC_ONE(instr_T);
1648 if (tv->vval.v_instr == NULL)
1649 goto on_error;
1650 ++ectx->ec_stack.ga_len;
1651
1652 tv->v_type = VAR_INSTR;
1653 tv->vval.v_instr->instr_ectx = ectx;
1654 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1655 }
1656 break;
1657
Bram Moolenaar4c137212021-04-19 16:48:48 +02001658 // execute :substitute with an expression
1659 case ISN_SUBSTITUTE:
1660 {
1661 subs_T *subs = &iptr->isn_arg.subs;
1662 source_cookie_T cookie;
1663 struct subs_expr_S *save_instr = substitute_instr;
1664 struct subs_expr_S subs_instr;
1665 int res;
1666
1667 subs_instr.subs_ectx = ectx;
1668 subs_instr.subs_instr = subs->subs_instr;
1669 subs_instr.subs_status = OK;
1670 substitute_instr = &subs_instr;
1671
1672 SOURCING_LNUM = iptr->isn_lnum;
1673 // This is very much like ISN_EXEC
1674 CLEAR_FIELD(cookie);
1675 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1676 res = do_cmdline(subs->subs_cmd,
1677 getsourceline, &cookie,
1678 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1679 substitute_instr = save_instr;
1680
1681 if (res == FAIL || did_emsg
1682 || subs_instr.subs_status == FAIL)
1683 goto on_error;
1684 }
1685 break;
1686
1687 case ISN_FINISH:
1688 goto done;
1689
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001690 case ISN_REDIRSTART:
1691 // create a dummy entry for var_redir_str()
1692 if (alloc_redir_lval() == FAIL)
1693 goto on_error;
1694
1695 // The output is stored in growarray "redir_ga" until
1696 // redirection ends.
1697 init_redir_ga();
1698 redir_vname = 1;
1699 break;
1700
1701 case ISN_REDIREND:
1702 {
1703 char_u *res = get_clear_redir_ga();
1704
1705 // End redirection, put redirected text on the stack.
1706 clear_redir_lval();
1707 redir_vname = 0;
1708
1709 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1710 {
1711 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001712 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001713 }
1714 tv = STACK_TV_BOT(0);
1715 tv->v_type = VAR_STRING;
1716 tv->vval.v_string = res;
1717 ++ectx->ec_stack.ga_len;
1718 }
1719 break;
1720
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001721 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001722#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001723 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1724 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001725#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001726 break;
1727
1728 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001729#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001730 {
1731 exarg_T ea;
1732 int res;
1733
1734 CLEAR_FIELD(ea);
1735 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1736 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1737 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1738 --ectx->ec_stack.ga_len;
1739 tv = STACK_TV_BOT(0);
1740 res = cexpr_core(&ea, tv);
1741 clear_tv(tv);
1742 if (res == FAIL)
1743 goto on_error;
1744 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001745#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001746 break;
1747
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001748 // execute Ex command from pieces on the stack
1749 case ISN_EXECCONCAT:
1750 {
1751 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001752 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001753 int pass;
1754 int i;
1755 char_u *cmd = NULL;
1756 char_u *str;
1757
1758 for (pass = 1; pass <= 2; ++pass)
1759 {
1760 for (i = 0; i < count; ++i)
1761 {
1762 tv = STACK_TV_BOT(i - count);
1763 str = tv->vval.v_string;
1764 if (str != NULL && *str != NUL)
1765 {
1766 if (pass == 2)
1767 STRCPY(cmd + len, str);
1768 len += STRLEN(str);
1769 }
1770 if (pass == 2)
1771 clear_tv(tv);
1772 }
1773 if (pass == 1)
1774 {
1775 cmd = alloc(len + 1);
1776 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001777 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001778 len = 0;
1779 }
1780 }
1781
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001782 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001783 do_cmdline_cmd(cmd);
1784 vim_free(cmd);
1785 }
1786 break;
1787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 // execute :echo {string} ...
1789 case ISN_ECHO:
1790 {
1791 int count = iptr->isn_arg.echo.echo_count;
1792 int atstart = TRUE;
1793 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001794 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795
1796 for (idx = 0; idx < count; ++idx)
1797 {
1798 tv = STACK_TV_BOT(idx - count);
1799 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1800 &atstart, &needclr);
1801 clear_tv(tv);
1802 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001803 if (needclr)
1804 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001805 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 }
1807 break;
1808
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001809 // :execute {string} ...
1810 // :echomsg {string} ...
1811 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001812 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001813 case ISN_ECHOMSG:
1814 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001815 {
1816 int count = iptr->isn_arg.number;
1817 garray_T ga;
1818 char_u buf[NUMBUFLEN];
1819 char_u *p;
1820 int len;
1821 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001822 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001823
1824 ga_init2(&ga, 1, 80);
1825 for (idx = 0; idx < count; ++idx)
1826 {
1827 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001828 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001829 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001830 if (tv->v_type == VAR_CHANNEL
1831 || tv->v_type == VAR_JOB)
1832 {
1833 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001834 semsg(_(e_using_invalid_value_as_string_str),
1835 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001836 break;
1837 }
1838 else
1839 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001840 }
1841 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001842 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001843
1844 len = (int)STRLEN(p);
1845 if (ga_grow(&ga, len + 2) == FAIL)
1846 failed = TRUE;
1847 else
1848 {
1849 if (ga.ga_len > 0)
1850 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1851 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1852 ga.ga_len += len;
1853 }
1854 clear_tv(tv);
1855 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001856 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001857 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001858 {
1859 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001860 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001861 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001862
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001863 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001864 {
1865 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001866 {
1867 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001868 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001869 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001870 {
1871 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001872 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001873 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001874 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001875 else
1876 {
1877 msg_sb_eol();
1878 if (iptr->isn_type == ISN_ECHOMSG)
1879 {
1880 msg_attr(ga.ga_data, echo_attr);
1881 out_flush();
1882 }
1883 else
1884 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001885 SOURCING_LNUM = iptr->isn_lnum;
1886 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001887 }
1888 }
1889 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001890 ga_clear(&ga);
1891 }
1892 break;
1893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 // load local variable or argument
1895 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001896 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001897 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001899 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 break;
1901
1902 // load v: variable
1903 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001904 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001905 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001907 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 break;
1909
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001910 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001911 case ISN_LOADSCRIPT:
1912 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001913 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 svar_T *sv;
1915
Bram Moolenaar4c137212021-04-19 16:48:48 +02001916 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001917 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001918 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001919 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001920 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001921 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001923 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 }
1925 break;
1926
1927 // load s: variable in old script
1928 case ISN_LOADS:
1929 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001930 hashtab_T *ht = &SCRIPT_VARS(
1931 iptr->isn_arg.loadstore.ls_sid);
1932 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 if (di == NULL)
1936 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001937 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001938 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001939 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 }
1941 else
1942 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001943 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001944 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001946 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 }
1948 }
1949 break;
1950
Bram Moolenaard3aac292020-04-19 14:32:17 +02001951 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001953 case ISN_LOADB:
1954 case ISN_LOADW:
1955 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001957 dictitem_T *di = NULL;
1958 hashtab_T *ht = NULL;
1959 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001960
Bram Moolenaard3aac292020-04-19 14:32:17 +02001961 switch (iptr->isn_type)
1962 {
1963 case ISN_LOADG:
1964 ht = get_globvar_ht();
1965 namespace = 'g';
1966 break;
1967 case ISN_LOADB:
1968 ht = &curbuf->b_vars->dv_hashtab;
1969 namespace = 'b';
1970 break;
1971 case ISN_LOADW:
1972 ht = &curwin->w_vars->dv_hashtab;
1973 namespace = 'w';
1974 break;
1975 case ISN_LOADT:
1976 ht = &curtab->tp_vars->dv_hashtab;
1977 namespace = 't';
1978 break;
1979 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001980 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001981 }
1982 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 if (di == NULL)
1985 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001986 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001987 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001988 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001989 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 }
1991 else
1992 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001993 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001994 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001996 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 }
1998 }
1999 break;
2000
Bram Moolenaar03290b82020-12-19 16:30:44 +01002001 // load autoload variable
2002 case ISN_LOADAUTO:
2003 {
2004 char_u *name = iptr->isn_arg.string;
2005
Bram Moolenaar4c137212021-04-19 16:48:48 +02002006 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002007 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002008 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002009 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002010 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002011 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002012 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002013 }
2014 break;
2015
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002016 // load g:/b:/w:/t: namespace
2017 case ISN_LOADGDICT:
2018 case ISN_LOADBDICT:
2019 case ISN_LOADWDICT:
2020 case ISN_LOADTDICT:
2021 {
2022 dict_T *d = NULL;
2023
2024 switch (iptr->isn_type)
2025 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002026 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2027 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2028 case ISN_LOADWDICT: d = curwin->w_vars; break;
2029 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002030 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002031 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002032 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002033 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002034 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002035 tv = STACK_TV_BOT(0);
2036 tv->v_type = VAR_DICT;
2037 tv->v_lock = 0;
2038 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002039 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002040 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002041 }
2042 break;
2043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 // load &option
2045 case ISN_LOADOPT:
2046 {
2047 typval_T optval;
2048 char_u *name = iptr->isn_arg.string;
2049
Bram Moolenaara8c17702020-04-01 21:17:24 +02002050 // This is not expected to fail, name is checked during
2051 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002052 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002053 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002054 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002055 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002057 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 }
2059 break;
2060
2061 // load $ENV
2062 case ISN_LOADENV:
2063 {
2064 typval_T optval;
2065 char_u *name = iptr->isn_arg.string;
2066
Bram Moolenaar4c137212021-04-19 16:48:48 +02002067 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002068 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002069 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002070 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002072 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 }
2074 break;
2075
2076 // load @register
2077 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002078 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002079 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 tv = STACK_TV_BOT(0);
2081 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002082 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002083 // This may result in NULL, which should be equivalent to an
2084 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 tv->vval.v_string = get_reg_contents(
2086 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002087 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 break;
2089
2090 // store local variable
2091 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002092 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 tv = STACK_TV_VAR(iptr->isn_arg.number);
2094 clear_tv(tv);
2095 *tv = *STACK_TV_BOT(0);
2096 break;
2097
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002098 // store s: variable in old script
2099 case ISN_STORES:
2100 {
2101 hashtab_T *ht = &SCRIPT_VARS(
2102 iptr->isn_arg.loadstore.ls_sid);
2103 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002104 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002105
Bram Moolenaar4c137212021-04-19 16:48:48 +02002106 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002107 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002108 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002109 else
2110 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002111 SOURCING_LNUM = iptr->isn_lnum;
2112 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002113 {
2114 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002115 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002116 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002117 clear_tv(&di->di_tv);
2118 di->di_tv = *STACK_TV_BOT(0);
2119 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002120 }
2121 break;
2122
2123 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 case ISN_STORESCRIPT:
2125 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002126 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2127 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128
Bram Moolenaar4c137212021-04-19 16:48:48 +02002129 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002130 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002131 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002132 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002133
2134 // "const" and "final" are checked at compile time, locking
2135 // the value needs to be checked here.
2136 SOURCING_LNUM = iptr->isn_lnum;
2137 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002138 {
2139 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002140 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002141 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 clear_tv(sv->sv_tv);
2144 *sv->sv_tv = *STACK_TV_BOT(0);
2145 }
2146 break;
2147
2148 // store option
2149 case ISN_STOREOPT:
2150 {
2151 long n = 0;
2152 char_u *s = NULL;
2153 char *msg;
2154
Bram Moolenaar4c137212021-04-19 16:48:48 +02002155 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 tv = STACK_TV_BOT(0);
2157 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002158 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002160 if (s == NULL)
2161 s = (char_u *)"";
2162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002164 // must be VAR_NUMBER, CHECKTYPE makes sure
2165 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2167 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002168 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 if (msg != NULL)
2170 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002171 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002173 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 }
2176 break;
2177
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002178 // store $ENV
2179 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002180 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002181 tv = STACK_TV_BOT(0);
2182 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2183 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002184 break;
2185
2186 // store @r
2187 case ISN_STOREREG:
2188 {
2189 int reg = iptr->isn_arg.number;
2190
Bram Moolenaar4c137212021-04-19 16:48:48 +02002191 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002192 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002193 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002194 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002195 }
2196 break;
2197
2198 // store v: variable
2199 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002200 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002201 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2202 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002203 // should not happen, type is checked when compiling
2204 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002205 break;
2206
Bram Moolenaard3aac292020-04-19 14:32:17 +02002207 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002209 case ISN_STOREB:
2210 case ISN_STOREW:
2211 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002213 dictitem_T *di;
2214 hashtab_T *ht;
2215 char_u *name = iptr->isn_arg.string + 2;
2216
Bram Moolenaard3aac292020-04-19 14:32:17 +02002217 switch (iptr->isn_type)
2218 {
2219 case ISN_STOREG:
2220 ht = get_globvar_ht();
2221 break;
2222 case ISN_STOREB:
2223 ht = &curbuf->b_vars->dv_hashtab;
2224 break;
2225 case ISN_STOREW:
2226 ht = &curwin->w_vars->dv_hashtab;
2227 break;
2228 case ISN_STORET:
2229 ht = &curtab->tp_vars->dv_hashtab;
2230 break;
2231 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002232 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234
Bram Moolenaar4c137212021-04-19 16:48:48 +02002235 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002236 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002238 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 else
2240 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002241 SOURCING_LNUM = iptr->isn_lnum;
2242 if (var_check_permission(di, name) == FAIL)
2243 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 clear_tv(&di->di_tv);
2245 di->di_tv = *STACK_TV_BOT(0);
2246 }
2247 }
2248 break;
2249
Bram Moolenaar03290b82020-12-19 16:30:44 +01002250 // store an autoload variable
2251 case ISN_STOREAUTO:
2252 SOURCING_LNUM = iptr->isn_lnum;
2253 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2254 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002255 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002256 break;
2257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 // store number in local variable
2259 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002260 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 clear_tv(tv);
2262 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002263 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 break;
2265
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002266 // store value in list or dict variable
2267 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002268 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002269 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002270 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002271 typval_T *tv_dest = STACK_TV_BOT(-1);
2272 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002273
Bram Moolenaar752fc692021-01-04 21:57:11 +01002274 // Stack contains:
2275 // -3 value to be stored
2276 // -2 index
2277 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002278 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002279 SOURCING_LNUM = iptr->isn_lnum;
2280 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002281 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002282 dest_type = tv_dest->v_type;
2283 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002284 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002285 else if (dest_type == VAR_LIST
2286 && tv_idx->v_type != VAR_NUMBER)
2287 {
2288 emsg(_(e_number_exp));
2289 status = FAIL;
2290 }
2291 }
2292 else if (dest_type != tv_dest->v_type)
2293 {
2294 // just in case, should be OK
2295 semsg(_(e_expected_str_but_got_str),
2296 vartype_name(dest_type),
2297 vartype_name(tv_dest->v_type));
2298 status = FAIL;
2299 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002300
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002301 if (status == OK && dest_type == VAR_LIST)
2302 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002303 long lidx = (long)tv_idx->vval.v_number;
2304 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002305
2306 if (list == NULL)
2307 {
2308 emsg(_(e_list_not_set));
2309 goto on_error;
2310 }
2311 if (lidx < 0 && list->lv_len + lidx >= 0)
2312 // negative index is relative to the end
2313 lidx = list->lv_len + lidx;
2314 if (lidx < 0 || lidx > list->lv_len)
2315 {
2316 semsg(_(e_listidx), lidx);
2317 goto on_error;
2318 }
2319 if (lidx < list->lv_len)
2320 {
2321 listitem_T *li = list_find(list, lidx);
2322
2323 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002324 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002325 goto on_error;
2326 // overwrite existing list item
2327 clear_tv(&li->li_tv);
2328 li->li_tv = *tv;
2329 }
2330 else
2331 {
2332 if (error_if_locked(list->lv_lock,
2333 e_cannot_change_list))
2334 goto on_error;
2335 // append to list, only fails when out of memory
2336 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002337 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002338 clear_tv(tv);
2339 }
2340 }
2341 else if (status == OK && dest_type == VAR_DICT)
2342 {
2343 char_u *key = tv_idx->vval.v_string;
2344 dict_T *dict = tv_dest->vval.v_dict;
2345 dictitem_T *di;
2346
2347 SOURCING_LNUM = iptr->isn_lnum;
2348 if (dict == NULL)
2349 {
2350 emsg(_(e_dictionary_not_set));
2351 goto on_error;
2352 }
2353 if (key == NULL)
2354 key = (char_u *)"";
2355 di = dict_find(dict, key, -1);
2356 if (di != NULL)
2357 {
2358 if (error_if_locked(di->di_tv.v_lock,
2359 e_cannot_change_dict_item))
2360 goto on_error;
2361 // overwrite existing value
2362 clear_tv(&di->di_tv);
2363 di->di_tv = *tv;
2364 }
2365 else
2366 {
2367 if (error_if_locked(dict->dv_lock,
2368 e_cannot_change_dict))
2369 goto on_error;
2370 // add to dict, only fails when out of memory
2371 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002372 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002373 clear_tv(tv);
2374 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002375 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002376 else if (status == OK && dest_type == VAR_BLOB)
2377 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002378 long lidx = (long)tv_idx->vval.v_number;
2379 blob_T *blob = tv_dest->vval.v_blob;
2380 varnumber_T nr;
2381 int error = FALSE;
2382 int len;
2383
2384 if (blob == NULL)
2385 {
2386 emsg(_(e_blob_not_set));
2387 goto on_error;
2388 }
2389 len = blob_len(blob);
2390 if (lidx < 0 && len + lidx >= 0)
2391 // negative index is relative to the end
2392 lidx = len + lidx;
2393
2394 // Can add one byte at the end.
2395 if (lidx < 0 || lidx > len)
2396 {
2397 semsg(_(e_blobidx), lidx);
2398 goto on_error;
2399 }
2400 if (value_check_lock(blob->bv_lock,
2401 (char_u *)"blob", FALSE))
2402 goto on_error;
2403 nr = tv_get_number_chk(tv, &error);
2404 if (error)
2405 goto on_error;
2406 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002407 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002408 else
2409 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002410 status = FAIL;
2411 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002412 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002413
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002414 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002415 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002416 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002417 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002418 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002419 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002420 goto on_error;
2421 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002422 }
2423 break;
2424
Bram Moolenaar68452172021-04-12 21:21:02 +02002425 // store value in blob range
2426 case ISN_STORERANGE:
2427 {
2428 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2429 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2430 typval_T *tv_dest = STACK_TV_BOT(-1);
2431 int status = OK;
2432
2433 // Stack contains:
2434 // -4 value to be stored
2435 // -3 first index or "none"
2436 // -2 second index or "none"
2437 // -1 destination blob
2438 tv = STACK_TV_BOT(-4);
2439 if (tv_dest->v_type != VAR_BLOB)
2440 {
2441 status = FAIL;
2442 emsg(_(e_blob_required));
2443 }
2444 else
2445 {
2446 varnumber_T n1;
2447 varnumber_T n2;
2448 int error = FALSE;
2449
2450 n1 = tv_get_number_chk(tv_idx1, &error);
2451 if (error)
2452 status = FAIL;
2453 else
2454 {
2455 if (tv_idx2->v_type == VAR_SPECIAL
2456 && tv_idx2->vval.v_number == VVAL_NONE)
2457 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2458 else
2459 n2 = tv_get_number_chk(tv_idx2, &error);
2460 if (error)
2461 status = FAIL;
2462 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002463 {
2464 long bloblen = blob_len(tv_dest->vval.v_blob);
2465
2466 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002467 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002468 || check_blob_range(bloblen,
2469 n1, n2, FALSE) == FAIL)
2470 status = FAIL;
2471 else
2472 status = blob_set_range(
2473 tv_dest->vval.v_blob, n1, n2, tv);
2474 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002475 }
2476 }
2477
2478 clear_tv(tv_idx1);
2479 clear_tv(tv_idx2);
2480 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002481 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002482 clear_tv(tv);
2483
2484 if (status == FAIL)
2485 goto on_error;
2486 }
2487 break;
2488
Bram Moolenaar0186e582021-01-10 18:33:11 +01002489 // load or store variable or argument from outer scope
2490 case ISN_LOADOUTER:
2491 case ISN_STOREOUTER:
2492 {
2493 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002494 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2495 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002496
2497 while (depth > 1 && outer != NULL)
2498 {
2499 outer = outer->out_up;
2500 --depth;
2501 }
2502 if (outer == NULL)
2503 {
2504 SOURCING_LNUM = iptr->isn_lnum;
2505 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002506 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002507 }
2508 tv = ((typval_T *)outer->out_stack->ga_data)
2509 + outer->out_frame_idx + STACK_FRAME_SIZE
2510 + iptr->isn_arg.outer.outer_idx;
2511 if (iptr->isn_type == ISN_LOADOUTER)
2512 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002513 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002514 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002515 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002516 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002517 }
2518 else
2519 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002520 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002521 clear_tv(tv);
2522 *tv = *STACK_TV_BOT(0);
2523 }
2524 }
2525 break;
2526
Bram Moolenaar752fc692021-01-04 21:57:11 +01002527 // unlet item in list or dict variable
2528 case ISN_UNLETINDEX:
2529 {
2530 typval_T *tv_idx = STACK_TV_BOT(-2);
2531 typval_T *tv_dest = STACK_TV_BOT(-1);
2532 int status = OK;
2533
2534 // Stack contains:
2535 // -2 index
2536 // -1 dict or list
2537 if (tv_dest->v_type == VAR_DICT)
2538 {
2539 // unlet a dict item, index must be a string
2540 if (tv_idx->v_type != VAR_STRING)
2541 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002542 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002543 semsg(_(e_expected_str_but_got_str),
2544 vartype_name(VAR_STRING),
2545 vartype_name(tv_idx->v_type));
2546 status = FAIL;
2547 }
2548 else
2549 {
2550 dict_T *d = tv_dest->vval.v_dict;
2551 char_u *key = tv_idx->vval.v_string;
2552 dictitem_T *di = NULL;
2553
2554 if (key == NULL)
2555 key = (char_u *)"";
2556 if (d != NULL)
2557 di = dict_find(d, key, (int)STRLEN(key));
2558 if (di == NULL)
2559 {
2560 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002562 semsg(_(e_dictkey), key);
2563 status = FAIL;
2564 }
2565 else
2566 {
2567 // TODO: check for dict or item locked
2568 dictitem_remove(d, di);
2569 }
2570 }
2571 }
2572 else if (tv_dest->v_type == VAR_LIST)
2573 {
2574 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002575 SOURCING_LNUM = iptr->isn_lnum;
2576 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002577 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002578 status = FAIL;
2579 }
2580 else
2581 {
2582 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002583 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002584 listitem_T *li = NULL;
2585
2586 li = list_find(l, n);
2587 if (li == NULL)
2588 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002589 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002590 semsg(_(e_listidx), n);
2591 status = FAIL;
2592 }
2593 else
2594 // TODO: check for list or item locked
2595 listitem_remove(l, li);
2596 }
2597 }
2598 else
2599 {
2600 status = FAIL;
2601 semsg(_(e_cannot_index_str),
2602 vartype_name(tv_dest->v_type));
2603 }
2604
2605 clear_tv(tv_idx);
2606 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002607 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002608 if (status == FAIL)
2609 goto on_error;
2610 }
2611 break;
2612
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002613 // unlet range of items in list variable
2614 case ISN_UNLETRANGE:
2615 {
2616 // Stack contains:
2617 // -3 index1
2618 // -2 index2
2619 // -1 dict or list
2620 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2621 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2622 typval_T *tv_dest = STACK_TV_BOT(-1);
2623 int status = OK;
2624
2625 if (tv_dest->v_type == VAR_LIST)
2626 {
2627 // indexes must be a number
2628 SOURCING_LNUM = iptr->isn_lnum;
2629 if (check_for_number(tv_idx1) == FAIL
2630 || check_for_number(tv_idx2) == FAIL)
2631 {
2632 status = FAIL;
2633 }
2634 else
2635 {
2636 list_T *l = tv_dest->vval.v_list;
2637 long n1 = (long)tv_idx1->vval.v_number;
2638 long n2 = (long)tv_idx2->vval.v_number;
2639 listitem_T *li;
2640
2641 li = list_find_index(l, &n1);
2642 if (li == NULL
2643 || list_unlet_range(l, li, NULL, n1,
2644 TRUE, n2) == FAIL)
2645 status = FAIL;
2646 }
2647 }
2648 else
2649 {
2650 status = FAIL;
2651 SOURCING_LNUM = iptr->isn_lnum;
2652 semsg(_(e_cannot_index_str),
2653 vartype_name(tv_dest->v_type));
2654 }
2655
2656 clear_tv(tv_idx1);
2657 clear_tv(tv_idx2);
2658 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002659 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002660 if (status == FAIL)
2661 goto on_error;
2662 }
2663 break;
2664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 // push constant
2666 case ISN_PUSHNR:
2667 case ISN_PUSHBOOL:
2668 case ISN_PUSHSPEC:
2669 case ISN_PUSHF:
2670 case ISN_PUSHS:
2671 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002672 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002673 case ISN_PUSHCHANNEL:
2674 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002675 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002676 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002678 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002679 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 switch (iptr->isn_type)
2681 {
2682 case ISN_PUSHNR:
2683 tv->v_type = VAR_NUMBER;
2684 tv->vval.v_number = iptr->isn_arg.number;
2685 break;
2686 case ISN_PUSHBOOL:
2687 tv->v_type = VAR_BOOL;
2688 tv->vval.v_number = iptr->isn_arg.number;
2689 break;
2690 case ISN_PUSHSPEC:
2691 tv->v_type = VAR_SPECIAL;
2692 tv->vval.v_number = iptr->isn_arg.number;
2693 break;
2694#ifdef FEAT_FLOAT
2695 case ISN_PUSHF:
2696 tv->v_type = VAR_FLOAT;
2697 tv->vval.v_float = iptr->isn_arg.fnumber;
2698 break;
2699#endif
2700 case ISN_PUSHBLOB:
2701 blob_copy(iptr->isn_arg.blob, tv);
2702 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002703 case ISN_PUSHFUNC:
2704 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002705 if (iptr->isn_arg.string == NULL)
2706 tv->vval.v_string = NULL;
2707 else
2708 tv->vval.v_string =
2709 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002710 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002711 case ISN_PUSHCHANNEL:
2712#ifdef FEAT_JOB_CHANNEL
2713 tv->v_type = VAR_CHANNEL;
2714 tv->vval.v_channel = iptr->isn_arg.channel;
2715 if (tv->vval.v_channel != NULL)
2716 ++tv->vval.v_channel->ch_refcount;
2717#endif
2718 break;
2719 case ISN_PUSHJOB:
2720#ifdef FEAT_JOB_CHANNEL
2721 tv->v_type = VAR_JOB;
2722 tv->vval.v_job = iptr->isn_arg.job;
2723 if (tv->vval.v_job != NULL)
2724 ++tv->vval.v_job->jv_refcount;
2725#endif
2726 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 default:
2728 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002729 tv->vval.v_string = vim_strsave(
2730 iptr->isn_arg.string == NULL
2731 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 }
2733 break;
2734
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002735 case ISN_UNLET:
2736 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2737 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002738 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002739 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002740 case ISN_UNLETENV:
2741 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2742 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002743
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002744 case ISN_LOCKCONST:
2745 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2746 break;
2747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 // create a list from items on the stack; uses a single allocation
2749 // for the list header and the items
2750 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002751 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002752 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 break;
2754
2755 // create a dict from items on the stack
2756 case ISN_NEWDICT:
2757 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002758 int count = iptr->isn_arg.number;
2759 dict_T *dict = dict_alloc();
2760 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002761 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002762 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763
2764 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002765 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 for (idx = 0; idx < count; ++idx)
2767 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002768 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002770 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002771 key = tv->vval.v_string == NULL
2772 ? (char_u *)"" : tv->vval.v_string;
2773 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002774 if (item != NULL)
2775 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002777 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002778 dict_unref(dict);
2779 goto on_error;
2780 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002781 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 clear_tv(tv);
2783 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002784 {
2785 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002786 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2789 item->di_tv.v_lock = 0;
2790 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002791 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002792 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002793 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002794 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 }
2797
2798 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002799 ectx->ec_stack.ga_len -= 2 * count - 1;
2800 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002801 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002803 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 tv = STACK_TV_BOT(-1);
2805 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002806 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 tv->vval.v_dict = dict;
2808 ++dict->dv_refcount;
2809 }
2810 break;
2811
2812 // call a :def function
2813 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002814 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002815 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2816 NULL,
2817 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002818 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002819 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 break;
2821
2822 // call a builtin function
2823 case ISN_BCALL:
2824 SOURCING_LNUM = iptr->isn_lnum;
2825 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2826 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002827 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002828 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 break;
2830
2831 // call a funcref or partial
2832 case ISN_PCALL:
2833 {
2834 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2835 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002836 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837
2838 SOURCING_LNUM = iptr->isn_lnum;
2839 if (pfunc->cpf_top)
2840 {
2841 // funcref is above the arguments
2842 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2843 }
2844 else
2845 {
2846 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002847 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002848 partial_tv = *STACK_TV_BOT(0);
2849 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002851 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002852 if (tv == &partial_tv)
2853 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002855 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 }
2857 break;
2858
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002859 case ISN_PCALL_END:
2860 // PCALL finished, arguments have been consumed and replaced by
2861 // the return value. Now clear the funcref from the stack,
2862 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002863 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002864 clear_tv(STACK_TV_BOT(-1));
2865 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2866 break;
2867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 // call a user defined function or funcref/partial
2869 case ISN_UCALL:
2870 {
2871 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2872
2873 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002874 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002875 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002876 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 }
2878 break;
2879
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002880 // return from a :def function call without a value
2881 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002882 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002883 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002884 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002885 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002886 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002887 tv->vval.v_number = 0;
2888 tv->v_lock = 0;
2889 // FALLTHROUGH
2890
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002891 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 case ISN_RETURN:
2893 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002894 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002895 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002896
2897 if (trystack->ga_len > 0)
2898 trycmd = ((trycmd_T *)trystack->ga_data)
2899 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002900 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002901 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002903 // jump to ":finally" or ":endtry"
2904 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002905 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002906 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002907 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 trycmd->tcd_return = TRUE;
2909 }
2910 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002911 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 }
2913 break;
2914
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002915 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 case ISN_FUNCREF:
2917 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002918 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2919 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2920 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002923 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002924 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002925 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002926 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002927 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002928 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002929 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002930 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002931 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002933 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 tv->vval.v_partial = pt;
2935 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002936 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 }
2938 break;
2939
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002940 // Create a global function from a lambda.
2941 case ISN_NEWFUNC:
2942 {
2943 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2944
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002945 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002946 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002947 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002948 }
2949 break;
2950
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002951 // List functions
2952 case ISN_DEF:
2953 if (iptr->isn_arg.string == NULL)
2954 list_functions(NULL);
2955 else
2956 {
2957 exarg_T ea;
2958
2959 CLEAR_FIELD(ea);
2960 ea.cmd = ea.arg = iptr->isn_arg.string;
2961 define_function(&ea, NULL);
2962 }
2963 break;
2964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 // jump if a condition is met
2966 case ISN_JUMP:
2967 {
2968 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002969 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 int jump = TRUE;
2971
2972 if (when != JUMP_ALWAYS)
2973 {
2974 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002975 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002976 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002977 || when == JUMP_IF_COND_TRUE)
2978 {
2979 SOURCING_LNUM = iptr->isn_lnum;
2980 jump = tv_get_bool_chk(tv, &error);
2981 if (error)
2982 goto on_error;
2983 }
2984 else
2985 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002987 || when == JUMP_AND_KEEP_IF_FALSE
2988 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002990 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 {
2992 // drop the value from the stack
2993 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002994 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 }
2996 }
2997 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002998 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 }
3000 break;
3001
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003002 // Jump if an argument with a default value was already set and not
3003 // v:none.
3004 case ISN_JUMP_IF_ARG_SET:
3005 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3006 if (tv->v_type != VAR_UNKNOWN
3007 && !(tv->v_type == VAR_SPECIAL
3008 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003009 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003010 break;
3011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 // top of a for loop
3013 case ISN_FOR:
3014 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003015 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 typval_T *idxtv =
3017 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3018
Bram Moolenaar4c137212021-04-19 16:48:48 +02003019 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003020 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003021 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003022 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003023 list_T *list = ltv->vval.v_list;
3024
3025 // push the next item from the list
3026 ++idxtv->vval.v_number;
3027 if (list == NULL
3028 || idxtv->vval.v_number >= list->lv_len)
3029 {
3030 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003031 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3032 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003033 }
3034 else if (list->lv_first == &range_list_item)
3035 {
3036 // non-materialized range() list
3037 tv = STACK_TV_BOT(0);
3038 tv->v_type = VAR_NUMBER;
3039 tv->v_lock = 0;
3040 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003042 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003043 }
3044 else
3045 {
3046 listitem_T *li = list_find(list,
3047 idxtv->vval.v_number);
3048
3049 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003050 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003051 }
3052 }
3053 else if (ltv->v_type == VAR_STRING)
3054 {
3055 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003056
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003057 // The index is for the last byte of the previous
3058 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003059 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003060 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003061 {
3062 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003063 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3064 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003065 }
3066 else
3067 {
3068 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3069
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003070 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003071 tv = STACK_TV_BOT(0);
3072 tv->v_type = VAR_STRING;
3073 tv->vval.v_string = vim_strnsave(
3074 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003075 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003076 idxtv->vval.v_number += clen - 1;
3077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003079 else if (ltv->v_type == VAR_BLOB)
3080 {
3081 blob_T *blob = ltv->vval.v_blob;
3082
3083 // When we get here the first time make a copy of the
3084 // blob, so that the iteration still works when it is
3085 // changed.
3086 if (idxtv->vval.v_number == -1 && blob != NULL)
3087 {
3088 blob_copy(blob, ltv);
3089 blob_unref(blob);
3090 blob = ltv->vval.v_blob;
3091 }
3092
3093 // The index is for the previous byte.
3094 ++idxtv->vval.v_number;
3095 if (blob == NULL
3096 || idxtv->vval.v_number >= blob_len(blob))
3097 {
3098 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003099 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3100 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003101 }
3102 else
3103 {
3104 // Push the next byte from the blob.
3105 tv = STACK_TV_BOT(0);
3106 tv->v_type = VAR_NUMBER;
3107 tv->vval.v_number = blob_get(blob,
3108 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003109 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003110 }
3111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 else
3113 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003114 semsg(_(e_for_loop_on_str_not_supported),
3115 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003116 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 }
3118 }
3119 break;
3120
3121 // start of ":try" block
3122 case ISN_TRY:
3123 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003124 trycmd_T *trycmd = NULL;
3125
Bram Moolenaar4c137212021-04-19 16:48:48 +02003126 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003127 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003128 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3129 + ectx->ec_trystack.ga_len;
3130 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003132 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003133 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3134 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003135 trycmd->tcd_catch_idx =
3136 iptr->isn_arg.try.try_ref->try_catch;
3137 trycmd->tcd_finally_idx =
3138 iptr->isn_arg.try.try_ref->try_finally;
3139 trycmd->tcd_endtry_idx =
3140 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 }
3142 break;
3143
3144 case ISN_PUSHEXC:
3145 if (current_exception == NULL)
3146 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003147 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003149 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003151 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003152 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003154 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003156 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 tv->vval.v_string = vim_strsave(
3158 (char_u *)current_exception->value);
3159 break;
3160
3161 case ISN_CATCH:
3162 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003163 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164
Bram Moolenaar4c137212021-04-19 16:48:48 +02003165 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 if (trystack->ga_len > 0)
3167 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003168 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 + trystack->ga_len - 1;
3170 trycmd->tcd_caught = TRUE;
3171 }
3172 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003173 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 catch_exception(current_exception);
3175 }
3176 break;
3177
Bram Moolenaarc150c092021-02-13 15:02:46 +01003178 case ISN_TRYCONT:
3179 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003180 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003181 trycont_T *trycont = &iptr->isn_arg.trycont;
3182 int i;
3183 trycmd_T *trycmd;
3184 int iidx = trycont->tct_where;
3185
3186 if (trystack->ga_len < trycont->tct_levels)
3187 {
3188 siemsg("TRYCONT: expected %d levels, found %d",
3189 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003190 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003191 }
3192 // Make :endtry jump to any outer try block and the last
3193 // :endtry inside the loop to the loop start.
3194 for (i = trycont->tct_levels; i > 0; --i)
3195 {
3196 trycmd = ((trycmd_T *)trystack->ga_data)
3197 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003198 // Add one to tcd_cont to be able to jump to
3199 // instruction with index zero.
3200 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003201 iidx = trycmd->tcd_finally_idx == 0
3202 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003203 }
3204 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003205 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003206 }
3207 break;
3208
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003209 case ISN_FINALLY:
3210 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003211 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003212 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3213 + trystack->ga_len - 1;
3214
3215 // Reset the index to avoid a return statement jumps here
3216 // again.
3217 trycmd->tcd_finally_idx = 0;
3218 break;
3219 }
3220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 // end of ":try" block
3222 case ISN_ENDTRY:
3223 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003224 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225
3226 if (trystack->ga_len > 0)
3227 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003228 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 --trystack->ga_len;
3231 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003232 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 trycmd = ((trycmd_T *)trystack->ga_data)
3234 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003235 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 {
3237 // discard the exception
3238 if (caught_stack == current_exception)
3239 caught_stack = caught_stack->caught;
3240 discard_current_exception();
3241 }
3242
3243 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003244 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003245
Bram Moolenaar4c137212021-04-19 16:48:48 +02003246 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003247 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003248 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003249 clear_tv(STACK_TV_BOT(0));
3250 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003251 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003252 // handling :continue: jump to outer try block or
3253 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003254 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 }
3256 }
3257 break;
3258
3259 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003260 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003261 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003262
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003263 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3264 {
3265 // throwing an exception while using "silent!" causes
3266 // the function to abort but not display an error.
3267 tv = STACK_TV_BOT(-1);
3268 clear_tv(tv);
3269 tv->v_type = VAR_NUMBER;
3270 tv->vval.v_number = 0;
3271 goto done;
3272 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003273 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003274 tv = STACK_TV_BOT(0);
3275 if (tv->vval.v_string == NULL
3276 || *skipwhite(tv->vval.v_string) == NUL)
3277 {
3278 vim_free(tv->vval.v_string);
3279 SOURCING_LNUM = iptr->isn_lnum;
3280 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003281 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003282 }
3283
3284 // Inside a "catch" we need to first discard the caught
3285 // exception.
3286 if (trystack->ga_len > 0)
3287 {
3288 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3289 + trystack->ga_len - 1;
3290 if (trycmd->tcd_caught && current_exception != NULL)
3291 {
3292 // discard the exception
3293 if (caught_stack == current_exception)
3294 caught_stack = caught_stack->caught;
3295 discard_current_exception();
3296 trycmd->tcd_caught = FALSE;
3297 }
3298 }
3299
3300 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3301 == FAIL)
3302 {
3303 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003304 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003305 }
3306 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 break;
3309
3310 // compare with special values
3311 case ISN_COMPAREBOOL:
3312 case ISN_COMPARESPECIAL:
3313 {
3314 typval_T *tv1 = STACK_TV_BOT(-2);
3315 typval_T *tv2 = STACK_TV_BOT(-1);
3316 varnumber_T arg1 = tv1->vval.v_number;
3317 varnumber_T arg2 = tv2->vval.v_number;
3318 int res;
3319
3320 switch (iptr->isn_arg.op.op_type)
3321 {
3322 case EXPR_EQUAL: res = arg1 == arg2; break;
3323 case EXPR_NEQUAL: res = arg1 != arg2; break;
3324 default: res = 0; break;
3325 }
3326
Bram Moolenaar4c137212021-04-19 16:48:48 +02003327 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 tv1->v_type = VAR_BOOL;
3329 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3330 }
3331 break;
3332
3333 // Operation with two number arguments
3334 case ISN_OPNR:
3335 case ISN_COMPARENR:
3336 {
3337 typval_T *tv1 = STACK_TV_BOT(-2);
3338 typval_T *tv2 = STACK_TV_BOT(-1);
3339 varnumber_T arg1 = tv1->vval.v_number;
3340 varnumber_T arg2 = tv2->vval.v_number;
3341 varnumber_T res;
3342
3343 switch (iptr->isn_arg.op.op_type)
3344 {
3345 case EXPR_MULT: res = arg1 * arg2; break;
3346 case EXPR_DIV: res = arg1 / arg2; break;
3347 case EXPR_REM: res = arg1 % arg2; break;
3348 case EXPR_SUB: res = arg1 - arg2; break;
3349 case EXPR_ADD: res = arg1 + arg2; break;
3350
3351 case EXPR_EQUAL: res = arg1 == arg2; break;
3352 case EXPR_NEQUAL: res = arg1 != arg2; break;
3353 case EXPR_GREATER: res = arg1 > arg2; break;
3354 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3355 case EXPR_SMALLER: res = arg1 < arg2; break;
3356 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3357 default: res = 0; break;
3358 }
3359
Bram Moolenaar4c137212021-04-19 16:48:48 +02003360 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 if (iptr->isn_type == ISN_COMPARENR)
3362 {
3363 tv1->v_type = VAR_BOOL;
3364 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3365 }
3366 else
3367 tv1->vval.v_number = res;
3368 }
3369 break;
3370
3371 // Computation with two float arguments
3372 case ISN_OPFLOAT:
3373 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003374#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 {
3376 typval_T *tv1 = STACK_TV_BOT(-2);
3377 typval_T *tv2 = STACK_TV_BOT(-1);
3378 float_T arg1 = tv1->vval.v_float;
3379 float_T arg2 = tv2->vval.v_float;
3380 float_T res = 0;
3381 int cmp = FALSE;
3382
3383 switch (iptr->isn_arg.op.op_type)
3384 {
3385 case EXPR_MULT: res = arg1 * arg2; break;
3386 case EXPR_DIV: res = arg1 / arg2; break;
3387 case EXPR_SUB: res = arg1 - arg2; break;
3388 case EXPR_ADD: res = arg1 + arg2; break;
3389
3390 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3391 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3392 case EXPR_GREATER: cmp = arg1 > arg2; break;
3393 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3394 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3395 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3396 default: cmp = 0; break;
3397 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003398 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 if (iptr->isn_type == ISN_COMPAREFLOAT)
3400 {
3401 tv1->v_type = VAR_BOOL;
3402 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3403 }
3404 else
3405 tv1->vval.v_float = res;
3406 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003407#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 break;
3409
3410 case ISN_COMPARELIST:
3411 {
3412 typval_T *tv1 = STACK_TV_BOT(-2);
3413 typval_T *tv2 = STACK_TV_BOT(-1);
3414 list_T *arg1 = tv1->vval.v_list;
3415 list_T *arg2 = tv2->vval.v_list;
3416 int cmp = FALSE;
3417 int ic = iptr->isn_arg.op.op_ic;
3418
3419 switch (iptr->isn_arg.op.op_type)
3420 {
3421 case EXPR_EQUAL: cmp =
3422 list_equal(arg1, arg2, ic, FALSE); break;
3423 case EXPR_NEQUAL: cmp =
3424 !list_equal(arg1, arg2, ic, FALSE); break;
3425 case EXPR_IS: cmp = arg1 == arg2; break;
3426 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3427 default: cmp = 0; break;
3428 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003429 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 clear_tv(tv1);
3431 clear_tv(tv2);
3432 tv1->v_type = VAR_BOOL;
3433 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3434 }
3435 break;
3436
3437 case ISN_COMPAREBLOB:
3438 {
3439 typval_T *tv1 = STACK_TV_BOT(-2);
3440 typval_T *tv2 = STACK_TV_BOT(-1);
3441 blob_T *arg1 = tv1->vval.v_blob;
3442 blob_T *arg2 = tv2->vval.v_blob;
3443 int cmp = FALSE;
3444
3445 switch (iptr->isn_arg.op.op_type)
3446 {
3447 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3448 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3449 case EXPR_IS: cmp = arg1 == arg2; break;
3450 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3451 default: cmp = 0; break;
3452 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003453 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 clear_tv(tv1);
3455 clear_tv(tv2);
3456 tv1->v_type = VAR_BOOL;
3457 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3458 }
3459 break;
3460
3461 // TODO: handle separately
3462 case ISN_COMPARESTRING:
3463 case ISN_COMPAREDICT:
3464 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 case ISN_COMPAREANY:
3466 {
3467 typval_T *tv1 = STACK_TV_BOT(-2);
3468 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003469 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 int ic = iptr->isn_arg.op.op_ic;
3471
Bram Moolenaareb26f432020-09-14 16:50:05 +02003472 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003473 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003475 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 }
3477 break;
3478
3479 case ISN_ADDLIST:
3480 case ISN_ADDBLOB:
3481 {
3482 typval_T *tv1 = STACK_TV_BOT(-2);
3483 typval_T *tv2 = STACK_TV_BOT(-1);
3484
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003485 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 if (iptr->isn_type == ISN_ADDLIST)
3487 eval_addlist(tv1, tv2);
3488 else
3489 eval_addblob(tv1, tv2);
3490 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003491 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 }
3493 break;
3494
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003495 case ISN_LISTAPPEND:
3496 {
3497 typval_T *tv1 = STACK_TV_BOT(-2);
3498 typval_T *tv2 = STACK_TV_BOT(-1);
3499 list_T *l = tv1->vval.v_list;
3500
3501 // add an item to a list
3502 if (l == NULL)
3503 {
3504 SOURCING_LNUM = iptr->isn_lnum;
3505 emsg(_(e_cannot_add_to_null_list));
3506 goto on_error;
3507 }
3508 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003509 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003510 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003511 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003512 }
3513 break;
3514
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003515 case ISN_BLOBAPPEND:
3516 {
3517 typval_T *tv1 = STACK_TV_BOT(-2);
3518 typval_T *tv2 = STACK_TV_BOT(-1);
3519 blob_T *b = tv1->vval.v_blob;
3520 int error = FALSE;
3521 varnumber_T n;
3522
3523 // add a number to a blob
3524 if (b == NULL)
3525 {
3526 SOURCING_LNUM = iptr->isn_lnum;
3527 emsg(_(e_cannot_add_to_null_blob));
3528 goto on_error;
3529 }
3530 n = tv_get_number_chk(tv2, &error);
3531 if (error)
3532 goto on_error;
3533 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003534 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003535 }
3536 break;
3537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 // Computation with two arguments of unknown type
3539 case ISN_OPANY:
3540 {
3541 typval_T *tv1 = STACK_TV_BOT(-2);
3542 typval_T *tv2 = STACK_TV_BOT(-1);
3543 varnumber_T n1, n2;
3544#ifdef FEAT_FLOAT
3545 float_T f1 = 0, f2 = 0;
3546#endif
3547 int error = FALSE;
3548
3549 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3550 {
3551 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3552 {
3553 eval_addlist(tv1, tv2);
3554 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003555 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 break;
3557 }
3558 else if (tv1->v_type == VAR_BLOB
3559 && tv2->v_type == VAR_BLOB)
3560 {
3561 eval_addblob(tv1, tv2);
3562 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003563 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 break;
3565 }
3566 }
3567#ifdef FEAT_FLOAT
3568 if (tv1->v_type == VAR_FLOAT)
3569 {
3570 f1 = tv1->vval.v_float;
3571 n1 = 0;
3572 }
3573 else
3574#endif
3575 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003576 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 n1 = tv_get_number_chk(tv1, &error);
3578 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003579 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580#ifdef FEAT_FLOAT
3581 if (tv2->v_type == VAR_FLOAT)
3582 f1 = n1;
3583#endif
3584 }
3585#ifdef FEAT_FLOAT
3586 if (tv2->v_type == VAR_FLOAT)
3587 {
3588 f2 = tv2->vval.v_float;
3589 n2 = 0;
3590 }
3591 else
3592#endif
3593 {
3594 n2 = tv_get_number_chk(tv2, &error);
3595 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003596 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597#ifdef FEAT_FLOAT
3598 if (tv1->v_type == VAR_FLOAT)
3599 f2 = n2;
3600#endif
3601 }
3602#ifdef FEAT_FLOAT
3603 // if there is a float on either side the result is a float
3604 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3605 {
3606 switch (iptr->isn_arg.op.op_type)
3607 {
3608 case EXPR_MULT: f1 = f1 * f2; break;
3609 case EXPR_DIV: f1 = f1 / f2; break;
3610 case EXPR_SUB: f1 = f1 - f2; break;
3611 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003612 default: SOURCING_LNUM = iptr->isn_lnum;
3613 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003614 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 }
3616 clear_tv(tv1);
3617 clear_tv(tv2);
3618 tv1->v_type = VAR_FLOAT;
3619 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003620 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 }
3622 else
3623#endif
3624 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003625 int failed = FALSE;
3626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 switch (iptr->isn_arg.op.op_type)
3628 {
3629 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003630 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3631 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003632 goto on_error;
3633 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 case EXPR_SUB: n1 = n1 - n2; break;
3635 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003636 default: n1 = num_modulus(n1, n2, &failed);
3637 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003638 goto on_error;
3639 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 }
3641 clear_tv(tv1);
3642 clear_tv(tv2);
3643 tv1->v_type = VAR_NUMBER;
3644 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003645 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 }
3647 }
3648 break;
3649
3650 case ISN_CONCAT:
3651 {
3652 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3653 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3654 char_u *res;
3655
3656 res = concat_str(str1, str2);
3657 clear_tv(STACK_TV_BOT(-2));
3658 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003659 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 STACK_TV_BOT(-1)->vval.v_string = res;
3661 }
3662 break;
3663
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003664 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003665 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003666 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003667 int is_slice = iptr->isn_type == ISN_STRSLICE;
3668 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003669 char_u *res;
3670
3671 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003672 // string slice: string is at stack-3, first index at
3673 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003674 if (is_slice)
3675 {
3676 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003677 n1 = tv->vval.v_number;
3678 }
3679
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003680 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003681 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003682
Bram Moolenaar4c137212021-04-19 16:48:48 +02003683 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003684 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003685 if (is_slice)
3686 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003687 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003688 else
3689 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003690 // single character (including composing characters).
3691 // If the index is too big or negative the result is
3692 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003693 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003694 vim_free(tv->vval.v_string);
3695 tv->vval.v_string = res;
3696 }
3697 break;
3698
3699 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003700 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003701 case ISN_BLOBINDEX:
3702 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003704 int is_slice = iptr->isn_type == ISN_LISTSLICE
3705 || iptr->isn_type == ISN_BLOBSLICE;
3706 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3707 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003708 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003709 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710
3711 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003712 // list slice: list is at stack-3, indexes at stack-2 and
3713 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003714 // Same for blob.
3715 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716
3717 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003718 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003720
3721 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 {
Bram Moolenaared591872020-08-15 22:14:53 +02003723 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003724 n1 = tv->vval.v_number;
3725 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 }
Bram Moolenaared591872020-08-15 22:14:53 +02003727
Bram Moolenaar4c137212021-04-19 16:48:48 +02003728 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003729 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003730 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003731 if (is_blob)
3732 {
3733 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3734 n1, n2, FALSE, tv) == FAIL)
3735 goto on_error;
3736 }
3737 else
3738 {
3739 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3740 n1, n2, FALSE, tv, TRUE) == FAIL)
3741 goto on_error;
3742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 }
3744 break;
3745
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003746 case ISN_ANYINDEX:
3747 case ISN_ANYSLICE:
3748 {
3749 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3750 typval_T *var1, *var2;
3751 int res;
3752
3753 // index: composite is at stack-2, index at stack-1
3754 // slice: composite is at stack-3, indexes at stack-2 and
3755 // stack-1
3756 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003757 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003758 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3759 goto on_error;
3760 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3761 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003762 res = eval_index_inner(tv, is_slice, var1, var2,
3763 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003764 clear_tv(var1);
3765 if (is_slice)
3766 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003767 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003768 if (res == FAIL)
3769 goto on_error;
3770 }
3771 break;
3772
Bram Moolenaar9af78762020-06-16 11:34:42 +02003773 case ISN_SLICE:
3774 {
3775 list_T *list;
3776 int count = iptr->isn_arg.number;
3777
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003778 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003779 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003780 list = tv->vval.v_list;
3781
3782 // no error for short list, expect it to be checked earlier
3783 if (list != NULL && list->lv_len >= count)
3784 {
3785 list_T *newlist = list_slice(list,
3786 count, list->lv_len - 1);
3787
3788 if (newlist != NULL)
3789 {
3790 list_unref(list);
3791 tv->vval.v_list = newlist;
3792 ++newlist->lv_refcount;
3793 }
3794 }
3795 }
3796 break;
3797
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003798 case ISN_GETITEM:
3799 {
3800 listitem_T *li;
3801 int index = iptr->isn_arg.number;
3802
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003803 // Get list item: list is at stack-1, push item.
3804 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003805 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003806 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003807
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003809 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003810 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003811 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003812
3813 // Useful when used in unpack assignment. Reset at
3814 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003815 ectx->ec_where.wt_index = index + 1;
3816 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003817 }
3818 break;
3819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 case ISN_MEMBER:
3821 {
3822 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003823 char_u *key;
3824 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003825 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003826
3827 // dict member: dict is at stack-2, key at stack-1
3828 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003829 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003830 dict = tv->vval.v_dict;
3831
3832 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003833 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003834 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003835 if (key == NULL)
3836 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003837
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003838 if ((di = dict_find(dict, key, -1)) == NULL)
3839 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003840 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003841 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003842
3843 // If :silent! is used we will continue, make sure the
3844 // stack contents makes sense.
3845 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003846 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003847 tv = STACK_TV_BOT(-1);
3848 clear_tv(tv);
3849 tv->v_type = VAR_NUMBER;
3850 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003851 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003852 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003853 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003854 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003855 // Clear the dict only after getting the item, to avoid
3856 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003857 tv = STACK_TV_BOT(-1);
3858 temp_tv = *tv;
3859 copy_tv(&di->di_tv, tv);
3860 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003861 }
3862 break;
3863
3864 // dict member with string key
3865 case ISN_STRINGMEMBER:
3866 {
3867 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003869 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870
3871 tv = STACK_TV_BOT(-1);
3872 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3873 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003874 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003876 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 }
3878 dict = tv->vval.v_dict;
3879
3880 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3881 == NULL)
3882 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003883 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003885 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003887 // Clear the dict after getting the item, to avoid that it
3888 // make the item invalid.
3889 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003891 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 }
3893 break;
3894
3895 case ISN_NEGATENR:
3896 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003897 if (tv->v_type != VAR_NUMBER
3898#ifdef FEAT_FLOAT
3899 && tv->v_type != VAR_FLOAT
3900#endif
3901 )
3902 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003903 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003904 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003905 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003906 }
3907#ifdef FEAT_FLOAT
3908 if (tv->v_type == VAR_FLOAT)
3909 tv->vval.v_float = -tv->vval.v_float;
3910 else
3911#endif
3912 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 break;
3914
3915 case ISN_CHECKNR:
3916 {
3917 int error = FALSE;
3918
3919 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003920 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003922 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 (void)tv_get_number_chk(tv, &error);
3924 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003925 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 }
3927 break;
3928
3929 case ISN_CHECKTYPE:
3930 {
3931 checktype_T *ct = &iptr->isn_arg.type;
3932
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003933 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003934 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003935 if (!ectx->ec_where.wt_variable)
3936 ectx->ec_where.wt_index = ct->ct_arg_idx;
3937 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3938 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003939 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003940 if (!ectx->ec_where.wt_variable)
3941 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003942
3943 // number 0 is FALSE, number 1 is TRUE
3944 if (tv->v_type == VAR_NUMBER
3945 && ct->ct_type->tt_type == VAR_BOOL
3946 && (tv->vval.v_number == 0
3947 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003949 tv->v_type = VAR_BOOL;
3950 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003951 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 }
3953 }
3954 break;
3955
Bram Moolenaar9af78762020-06-16 11:34:42 +02003956 case ISN_CHECKLEN:
3957 {
3958 int min_len = iptr->isn_arg.checklen.cl_min_len;
3959 list_T *list = NULL;
3960
3961 tv = STACK_TV_BOT(-1);
3962 if (tv->v_type == VAR_LIST)
3963 list = tv->vval.v_list;
3964 if (list == NULL || list->lv_len < min_len
3965 || (list->lv_len > min_len
3966 && !iptr->isn_arg.checklen.cl_more_OK))
3967 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003968 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003969 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003970 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003971 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003972 }
3973 }
3974 break;
3975
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003976 case ISN_SETTYPE:
3977 {
3978 checktype_T *ct = &iptr->isn_arg.type;
3979
3980 tv = STACK_TV_BOT(-1);
3981 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3982 {
3983 free_type(tv->vval.v_dict->dv_type);
3984 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3985 }
3986 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3987 {
3988 free_type(tv->vval.v_list->lv_type);
3989 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3990 }
3991 }
3992 break;
3993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003995 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 {
3997 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003998 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004000 if (iptr->isn_type == ISN_2BOOL)
4001 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004002 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004003 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004004 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004005 n = !n;
4006 }
4007 else
4008 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004009 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004010 SOURCING_LNUM = iptr->isn_lnum;
4011 n = tv_get_bool_chk(tv, &error);
4012 if (error)
4013 goto on_error;
4014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 clear_tv(tv);
4016 tv->v_type = VAR_BOOL;
4017 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4018 }
4019 break;
4020
4021 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004022 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004023 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004024 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4025 iptr->isn_type == ISN_2STRING_ANY,
4026 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004027 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 break;
4029
Bram Moolenaar08597872020-12-10 19:43:40 +01004030 case ISN_RANGE:
4031 {
4032 exarg_T ea;
4033 char *errormsg;
4034
Bram Moolenaarece0b872021-01-08 20:40:45 +01004035 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004036 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004037 ea.addr_type = ADDR_LINES;
4038 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004039 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004040 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004041 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004042
Bram Moolenaar4c137212021-04-19 16:48:48 +02004043 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004044 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004045 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004046 tv = STACK_TV_BOT(-1);
4047 tv->v_type = VAR_NUMBER;
4048 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004049 if (ea.addr_count == 0)
4050 tv->vval.v_number = curwin->w_cursor.lnum;
4051 else
4052 tv->vval.v_number = ea.line2;
4053 }
4054 break;
4055
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004056 case ISN_PUT:
4057 {
4058 int regname = iptr->isn_arg.put.put_regname;
4059 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4060 char_u *expr = NULL;
4061 int dir = FORWARD;
4062
Bram Moolenaar08597872020-12-10 19:43:40 +01004063 if (lnum < -2)
4064 {
4065 // line number was put on the stack by ISN_RANGE
4066 tv = STACK_TV_BOT(-1);
4067 curwin->w_cursor.lnum = tv->vval.v_number;
4068 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4069 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004070 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004071 }
4072 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004073 // :put! above cursor
4074 dir = BACKWARD;
4075 else if (lnum >= 0)
4076 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004077
4078 if (regname == '=')
4079 {
4080 tv = STACK_TV_BOT(-1);
4081 if (tv->v_type == VAR_STRING)
4082 expr = tv->vval.v_string;
4083 else
4084 {
4085 expr = typval2string(tv, TRUE); // allocates value
4086 clear_tv(tv);
4087 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004088 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004089 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004090 check_cursor();
4091 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4092 vim_free(expr);
4093 }
4094 break;
4095
Bram Moolenaar02194d22020-10-24 23:08:38 +02004096 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004097 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4098 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4099 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4100 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004101 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4102 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004103 break;
4104
Bram Moolenaar02194d22020-10-24 23:08:38 +02004105 case ISN_CMDMOD_REV:
4106 // filter regprog is owned by the instruction, don't free it
4107 cmdmod.cmod_filter_regmatch.regprog = NULL;
4108 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004109 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4110 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004111 break;
4112
Bram Moolenaar792f7862020-11-23 08:31:18 +01004113 case ISN_UNPACK:
4114 {
4115 int count = iptr->isn_arg.unpack.unp_count;
4116 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4117 list_T *l;
4118 listitem_T *li;
4119 int i;
4120
4121 // Check there is a valid list to unpack.
4122 tv = STACK_TV_BOT(-1);
4123 if (tv->v_type != VAR_LIST)
4124 {
4125 SOURCING_LNUM = iptr->isn_lnum;
4126 emsg(_(e_for_argument_must_be_sequence_of_lists));
4127 goto on_error;
4128 }
4129 l = tv->vval.v_list;
4130 if (l == NULL
4131 || l->lv_len < (semicolon ? count - 1 : count))
4132 {
4133 SOURCING_LNUM = iptr->isn_lnum;
4134 emsg(_(e_list_value_does_not_have_enough_items));
4135 goto on_error;
4136 }
4137 else if (!semicolon && l->lv_len > count)
4138 {
4139 SOURCING_LNUM = iptr->isn_lnum;
4140 emsg(_(e_list_value_has_more_items_than_targets));
4141 goto on_error;
4142 }
4143
4144 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004145 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004146 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004147 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004148
4149 // Variable after semicolon gets a list with the remaining
4150 // items.
4151 if (semicolon)
4152 {
4153 list_T *rem_list =
4154 list_alloc_with_items(l->lv_len - count + 1);
4155
4156 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004157 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004158 tv = STACK_TV_BOT(-count);
4159 tv->vval.v_list = rem_list;
4160 ++rem_list->lv_refcount;
4161 tv->v_lock = 0;
4162 li = l->lv_first;
4163 for (i = 0; i < count - 1; ++i)
4164 li = li->li_next;
4165 for (i = 0; li != NULL; ++i)
4166 {
4167 list_set_item(rem_list, i, &li->li_tv);
4168 li = li->li_next;
4169 }
4170 --count;
4171 }
4172
4173 // Produce the values in reverse order, first item last.
4174 li = l->lv_first;
4175 for (i = 0; i < count; ++i)
4176 {
4177 tv = STACK_TV_BOT(-i - 1);
4178 copy_tv(&li->li_tv, tv);
4179 li = li->li_next;
4180 }
4181
4182 list_unref(l);
4183 }
4184 break;
4185
Bram Moolenaarb2049902021-01-24 12:53:53 +01004186 case ISN_PROF_START:
4187 case ISN_PROF_END:
4188 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004189#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004190 funccall_T cookie;
4191 ufunc_T *cur_ufunc =
4192 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004193 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004194
4195 cookie.func = cur_ufunc;
4196 if (iptr->isn_type == ISN_PROF_START)
4197 {
4198 func_line_start(&cookie, iptr->isn_lnum);
4199 // if we get here the instruction is executed
4200 func_line_exec(&cookie);
4201 }
4202 else
4203 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004204#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004205 }
4206 break;
4207
Bram Moolenaare99d4222021-06-13 14:01:26 +02004208 case ISN_DEBUG:
4209 if (ex_nesting_level <= debug_break_level)
Bram Moolenaar4cea5362021-06-16 22:24:40 +02004210 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004211 break;
4212
Bram Moolenaar389df252020-07-09 21:20:47 +02004213 case ISN_SHUFFLE:
4214 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004215 typval_T tmp_tv;
4216 int item = iptr->isn_arg.shuffle.shfl_item;
4217 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004218
4219 tmp_tv = *STACK_TV_BOT(-item);
4220 for ( ; up > 0 && item > 1; --up)
4221 {
4222 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4223 --item;
4224 }
4225 *STACK_TV_BOT(-item) = tmp_tv;
4226 }
4227 break;
4228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004230 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004232 ectx->ec_where.wt_index = 0;
4233 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 break;
4235 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004236 continue;
4237
Bram Moolenaard032f342020-07-18 18:13:02 +02004238func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004239 // Restore previous function. If the frame pointer is where we started
4240 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004241 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004242 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004243
Bram Moolenaar4c137212021-04-19 16:48:48 +02004244 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004245 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004246 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004247 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004248
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004249on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004250 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004251 // If "emsg_silent" is set then ignore the error, unless it was set
4252 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004253 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004254 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004255 {
4256 // If a sequence of instructions causes an error while ":silent!"
4257 // was used, restore the stack length and jump ahead to restoring
4258 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004259 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004260 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004261 while (ectx->ec_stack.ga_len
4262 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004263 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004264 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004265 clear_tv(STACK_TV_BOT(0));
4266 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004267 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4268 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004269 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004270 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004271 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004272on_fatal_error:
4273 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004274 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004275 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004276 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 }
4278
4279done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004280 ret = OK;
4281theend:
4282 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4283 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004284}
4285
4286/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004287 * Execute the instructions from a VAR_INSTR typeval and put the result in
4288 * "rettv".
4289 * Return OK or FAIL.
4290 */
4291 int
4292exe_typval_instr(typval_T *tv, typval_T *rettv)
4293{
4294 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4295 isn_T *save_instr = ectx->ec_instr;
4296 int save_iidx = ectx->ec_iidx;
4297 int res;
4298
4299 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4300 res = exec_instructions(ectx);
4301 if (res == OK)
4302 {
4303 *rettv = *STACK_TV_BOT(-1);
4304 --ectx->ec_stack.ga_len;
4305 }
4306
4307 ectx->ec_instr = save_instr;
4308 ectx->ec_iidx = save_iidx;
4309
4310 return res;
4311}
4312
4313/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004314 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4315 * "substitute_instr".
4316 */
4317 char_u *
4318exe_substitute_instr(void)
4319{
4320 ectx_T *ectx = substitute_instr->subs_ectx;
4321 isn_T *save_instr = ectx->ec_instr;
4322 int save_iidx = ectx->ec_iidx;
4323 char_u *res;
4324
4325 ectx->ec_instr = substitute_instr->subs_instr;
4326 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004327 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004328 typval_T *tv = STACK_TV_BOT(-1);
4329
Bram Moolenaar27523602021-06-05 21:36:19 +02004330 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004331 --ectx->ec_stack.ga_len;
4332 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004333 }
4334 else
4335 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004336 substitute_instr->subs_status = FAIL;
4337 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339
Bram Moolenaar4c137212021-04-19 16:48:48 +02004340 ectx->ec_instr = save_instr;
4341 ectx->ec_iidx = save_iidx;
4342
4343 return res;
4344}
4345
4346/*
4347 * Call a "def" function from old Vim script.
4348 * Return OK or FAIL.
4349 */
4350 int
4351call_def_function(
4352 ufunc_T *ufunc,
4353 int argc_arg, // nr of arguments
4354 typval_T *argv, // arguments
4355 partial_T *partial, // optional partial for context
4356 typval_T *rettv) // return value
4357{
4358 ectx_T ectx; // execution context
4359 int argc = argc_arg;
4360 typval_T *tv;
4361 int idx;
4362 int ret = FAIL;
4363 int defcount = ufunc->uf_args.ga_len - argc;
4364 sctx_T save_current_sctx = current_sctx;
4365 int did_emsg_before = did_emsg_cumul + did_emsg;
4366 int save_suppress_errthrow = suppress_errthrow;
4367 msglist_T **saved_msg_list = NULL;
4368 msglist_T *private_msg_list = NULL;
4369 int save_emsg_silent_def = emsg_silent_def;
4370 int save_did_emsg_def = did_emsg_def;
4371 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004372 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004373
4374// Get pointer to item in the stack.
4375#undef STACK_TV
4376#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4377
4378// Get pointer to item at the bottom of the stack, -1 is the bottom.
4379#undef STACK_TV_BOT
4380#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4381
4382// Get pointer to a local variable on the stack. Negative for arguments.
4383#undef STACK_TV_VAR
4384#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4385
4386 if (ufunc->uf_def_status == UF_NOT_COMPILED
4387 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004388 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4389 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004390 == FAIL))
4391 {
4392 if (did_emsg_cumul + did_emsg == did_emsg_before)
4393 semsg(_(e_function_is_not_compiled_str),
4394 printable_func_name(ufunc));
4395 return FAIL;
4396 }
4397
4398 {
4399 // Check the function was really compiled.
4400 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4401 + ufunc->uf_dfunc_idx;
4402 if (INSTRUCTIONS(dfunc) == NULL)
4403 {
4404 iemsg("using call_def_function() on not compiled function");
4405 return FAIL;
4406 }
4407 }
4408
4409 // If depth of calling is getting too high, don't execute the function.
4410 orig_funcdepth = funcdepth_get();
4411 if (funcdepth_increment() == FAIL)
4412 return FAIL;
4413
4414 CLEAR_FIELD(ectx);
4415 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4416 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4417 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4418 {
4419 funcdepth_decrement();
4420 return FAIL;
4421 }
4422 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4423 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4424 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004425 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004426
4427 idx = argc - ufunc->uf_args.ga_len;
4428 if (idx > 0 && ufunc->uf_va_name == NULL)
4429 {
4430 if (idx == 1)
4431 emsg(_(e_one_argument_too_many));
4432 else
4433 semsg(_(e_nr_arguments_too_many), idx);
4434 goto failed_early;
4435 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004436 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4437 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004438 {
4439 if (idx == -1)
4440 emsg(_(e_one_argument_too_few));
4441 else
4442 semsg(_(e_nr_arguments_too_few), -idx);
4443 goto failed_early;
4444 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004445
4446 // Put arguments on the stack, but no more than what the function expects.
4447 // A lambda can be called with more arguments than it uses.
4448 for (idx = 0; idx < argc
4449 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4450 ++idx)
4451 {
4452 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4453 && argv[idx].v_type == VAR_SPECIAL
4454 && argv[idx].vval.v_number == VVAL_NONE)
4455 {
4456 // Use the default value.
4457 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4458 }
4459 else
4460 {
4461 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4462 && check_typval_arg_type(
4463 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4464 goto failed_early;
4465 copy_tv(&argv[idx], STACK_TV_BOT(0));
4466 }
4467 ++ectx.ec_stack.ga_len;
4468 }
4469
4470 // Turn varargs into a list. Empty list if no args.
4471 if (ufunc->uf_va_name != NULL)
4472 {
4473 int vararg_count = argc - ufunc->uf_args.ga_len;
4474
4475 if (vararg_count < 0)
4476 vararg_count = 0;
4477 else
4478 argc -= vararg_count;
4479 if (exe_newlist(vararg_count, &ectx) == FAIL)
4480 goto failed_early;
4481
4482 // Check the type of the list items.
4483 tv = STACK_TV_BOT(-1);
4484 if (ufunc->uf_va_type != NULL
4485 && ufunc->uf_va_type != &t_list_any
4486 && ufunc->uf_va_type->tt_member != &t_any
4487 && tv->vval.v_list != NULL)
4488 {
4489 type_T *expected = ufunc->uf_va_type->tt_member;
4490 listitem_T *li = tv->vval.v_list->lv_first;
4491
4492 for (idx = 0; idx < vararg_count; ++idx)
4493 {
4494 if (check_typval_arg_type(expected, &li->li_tv,
4495 argc + idx + 1) == FAIL)
4496 goto failed_early;
4497 li = li->li_next;
4498 }
4499 }
4500
4501 if (defcount > 0)
4502 // Move varargs list to below missing default arguments.
4503 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4504 --ectx.ec_stack.ga_len;
4505 }
4506
4507 // Make space for omitted arguments, will store default value below.
4508 // Any varargs list goes after them.
4509 if (defcount > 0)
4510 for (idx = 0; idx < defcount; ++idx)
4511 {
4512 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4513 ++ectx.ec_stack.ga_len;
4514 }
4515 if (ufunc->uf_va_name != NULL)
4516 ++ectx.ec_stack.ga_len;
4517
4518 // Frame pointer points to just after arguments.
4519 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4520 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4521
4522 {
4523 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4524 + ufunc->uf_dfunc_idx;
4525 ufunc_T *base_ufunc = dfunc->df_ufunc;
4526
4527 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4528 // by copy_func().
4529 if (partial != NULL || base_ufunc->uf_partial != NULL)
4530 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004531 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4532 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004533 goto failed_early;
4534 if (partial != NULL)
4535 {
4536 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4537 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004538 if (current_ectx->ec_outer_ref != NULL
4539 && current_ectx->ec_outer_ref->or_outer != NULL)
4540 ectx.ec_outer_ref->or_outer =
4541 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004542 }
4543 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004544 {
4545 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4546 ++partial->pt_refcount;
4547 ectx.ec_outer_ref->or_partial = partial;
4548 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004549 }
4550 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004551 {
4552 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4553 ++base_ufunc->uf_partial->pt_refcount;
4554 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4555 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004556 }
4557 }
4558
4559 // dummy frame entries
4560 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4561 {
4562 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4563 ++ectx.ec_stack.ga_len;
4564 }
4565
4566 {
4567 // Reserve space for local variables and any closure reference count.
4568 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4569 + ufunc->uf_dfunc_idx;
4570
4571 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4572 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4573 ectx.ec_stack.ga_len += dfunc->df_varcount;
4574 if (dfunc->df_has_closure)
4575 {
4576 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4577 STACK_TV_VAR(idx)->vval.v_number = 0;
4578 ++ectx.ec_stack.ga_len;
4579 }
4580
4581 ectx.ec_instr = INSTRUCTIONS(dfunc);
4582 }
4583
4584 // Following errors are in the function, not the caller.
4585 // Commands behave like vim9script.
4586 estack_push_ufunc(ufunc, 1);
4587 current_sctx = ufunc->uf_script_ctx;
4588 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4589
4590 // Use a specific location for storing error messages to be converted to an
4591 // exception.
4592 saved_msg_list = msg_list;
4593 msg_list = &private_msg_list;
4594
4595 // Do turn errors into exceptions.
4596 suppress_errthrow = FALSE;
4597
4598 // Do not delete the function while executing it.
4599 ++ufunc->uf_calls;
4600
4601 // When ":silent!" was used before calling then we still abort the
4602 // function. If ":silent!" is used in the function then we don't.
4603 emsg_silent_def = emsg_silent;
4604 did_emsg_def = 0;
4605
4606 ectx.ec_where.wt_index = 0;
4607 ectx.ec_where.wt_variable = FALSE;
4608
4609 // Execute the instructions until done.
4610 ret = exec_instructions(&ectx);
4611 if (ret == OK)
4612 {
4613 // function finished, get result from the stack.
4614 if (ufunc->uf_ret_type == &t_void)
4615 {
4616 rettv->v_type = VAR_VOID;
4617 }
4618 else
4619 {
4620 tv = STACK_TV_BOT(-1);
4621 *rettv = *tv;
4622 tv->v_type = VAR_UNKNOWN;
4623 }
4624 }
4625
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004626 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004627 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4628 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004629
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004630 // Deal with any remaining closures, they may be in use somewhere.
4631 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004632 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004633 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004634 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4635 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004636
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004637 estack_pop();
4638 current_sctx = save_current_sctx;
4639
Bram Moolenaarc970e422021-03-17 15:03:04 +01004640 // TODO: when is it safe to delete the function if it is no longer used?
4641 --ufunc->uf_calls;
4642
Bram Moolenaar352134b2020-10-17 22:04:08 +02004643 if (*msg_list != NULL && saved_msg_list != NULL)
4644 {
4645 msglist_T **plist = saved_msg_list;
4646
4647 // Append entries from the current msg_list (uncaught exceptions) to
4648 // the saved msg_list.
4649 while (*plist != NULL)
4650 plist = &(*plist)->next;
4651
4652 *plist = *msg_list;
4653 }
4654 msg_list = saved_msg_list;
4655
Bram Moolenaar4c137212021-04-19 16:48:48 +02004656 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004657 {
4658 cmdmod.cmod_filter_regmatch.regprog = NULL;
4659 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004660 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004661 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004662 emsg_silent_def = save_emsg_silent_def;
4663 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004664
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004665failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004666 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4668 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004669 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004672 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004673 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004674 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004675 if (ectx.ec_outer_ref->or_outer_allocated)
4676 vim_free(ectx.ec_outer_ref->or_outer);
4677 partial_unref(ectx.ec_outer_ref->or_partial);
4678 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004679 }
4680
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004681 // Not sure if this is necessary.
4682 suppress_errthrow = save_suppress_errthrow;
4683
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004684 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004685 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004686 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004687 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 return ret;
4689}
4690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004692 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4693 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4694 * "pfx" is prefixed to every line.
4695 */
4696 static void
4697list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4698{
4699 int line_idx = 0;
4700 int prev_current = 0;
4701 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004702 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004703
4704 for (current = 0; current < instr_count; ++current)
4705 {
4706 isn_T *iptr = &instr[current];
4707 char *line;
4708
4709 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004710 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004711 while (line_idx < iptr->isn_lnum
4712 && line_idx < ufunc->uf_lines.ga_len)
4713 {
4714 if (current > prev_current)
4715 {
4716 msg_puts("\n\n");
4717 prev_current = current;
4718 }
4719 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4720 if (line != NULL)
4721 msg(line);
4722 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004723 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4724 {
4725 int first_def_arg = ufunc->uf_args.ga_len
4726 - ufunc->uf_def_args.ga_len;
4727
4728 if (def_arg_idx > 0)
4729 msg_puts("\n\n");
4730 msg_start();
4731 msg_puts(" ");
4732 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4733 first_def_arg + def_arg_idx]);
4734 msg_puts(" = ");
4735 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4736 msg_clr_eos();
4737 msg_end();
4738 }
4739 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004740
4741 switch (iptr->isn_type)
4742 {
4743 case ISN_EXEC:
4744 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4745 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004746 case ISN_EXEC_SPLIT:
4747 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4748 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004749 case ISN_LEGACY_EVAL:
4750 smsg("%s%4d EVAL legacy %s", pfx, current,
4751 iptr->isn_arg.string);
4752 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004753 case ISN_REDIRSTART:
4754 smsg("%s%4d REDIR", pfx, current);
4755 break;
4756 case ISN_REDIREND:
4757 smsg("%s%4d REDIR END%s", pfx, current,
4758 iptr->isn_arg.number ? " append" : "");
4759 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004760 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004761#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004762 smsg("%s%4d CEXPR pre %s", pfx, current,
4763 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004764#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004765 break;
4766 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004767#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004768 {
4769 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4770
4771 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4772 cexpr_get_auname(cer->cer_cmdidx),
4773 cer->cer_forceit ? "!" : "",
4774 cer->cer_cmdline);
4775 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004776#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004777 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004778 case ISN_INSTR:
4779 {
4780 smsg("%s%4d INSTR", pfx, current);
4781 list_instructions(" ", iptr->isn_arg.instr,
4782 INT_MAX, NULL);
4783 msg(" -------------");
4784 }
4785 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004786 case ISN_SUBSTITUTE:
4787 {
4788 subs_T *subs = &iptr->isn_arg.subs;
4789
4790 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4791 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4792 msg(" -------------");
4793 }
4794 break;
4795 case ISN_EXECCONCAT:
4796 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4797 (varnumber_T)iptr->isn_arg.number);
4798 break;
4799 case ISN_ECHO:
4800 {
4801 echo_T *echo = &iptr->isn_arg.echo;
4802
4803 smsg("%s%4d %s %d", pfx, current,
4804 echo->echo_with_white ? "ECHO" : "ECHON",
4805 echo->echo_count);
4806 }
4807 break;
4808 case ISN_EXECUTE:
4809 smsg("%s%4d EXECUTE %lld", pfx, current,
4810 (varnumber_T)(iptr->isn_arg.number));
4811 break;
4812 case ISN_ECHOMSG:
4813 smsg("%s%4d ECHOMSG %lld", pfx, current,
4814 (varnumber_T)(iptr->isn_arg.number));
4815 break;
4816 case ISN_ECHOERR:
4817 smsg("%s%4d ECHOERR %lld", pfx, current,
4818 (varnumber_T)(iptr->isn_arg.number));
4819 break;
4820 case ISN_LOAD:
4821 {
4822 if (iptr->isn_arg.number < 0)
4823 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4824 (varnumber_T)(iptr->isn_arg.number
4825 + STACK_FRAME_SIZE));
4826 else
4827 smsg("%s%4d LOAD $%lld", pfx, current,
4828 (varnumber_T)(iptr->isn_arg.number));
4829 }
4830 break;
4831 case ISN_LOADOUTER:
4832 {
4833 if (iptr->isn_arg.number < 0)
4834 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4835 iptr->isn_arg.outer.outer_depth,
4836 iptr->isn_arg.outer.outer_idx
4837 + STACK_FRAME_SIZE);
4838 else
4839 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4840 iptr->isn_arg.outer.outer_depth,
4841 iptr->isn_arg.outer.outer_idx);
4842 }
4843 break;
4844 case ISN_LOADV:
4845 smsg("%s%4d LOADV v:%s", pfx, current,
4846 get_vim_var_name(iptr->isn_arg.number));
4847 break;
4848 case ISN_LOADSCRIPT:
4849 {
4850 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4851 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4852 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4853 + sref->sref_idx;
4854
4855 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4856 sv->sv_name,
4857 sref->sref_idx,
4858 si->sn_name);
4859 }
4860 break;
4861 case ISN_LOADS:
4862 {
4863 scriptitem_T *si = SCRIPT_ITEM(
4864 iptr->isn_arg.loadstore.ls_sid);
4865
4866 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4867 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4868 }
4869 break;
4870 case ISN_LOADAUTO:
4871 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4872 break;
4873 case ISN_LOADG:
4874 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4875 break;
4876 case ISN_LOADB:
4877 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4878 break;
4879 case ISN_LOADW:
4880 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4881 break;
4882 case ISN_LOADT:
4883 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4884 break;
4885 case ISN_LOADGDICT:
4886 smsg("%s%4d LOAD g:", pfx, current);
4887 break;
4888 case ISN_LOADBDICT:
4889 smsg("%s%4d LOAD b:", pfx, current);
4890 break;
4891 case ISN_LOADWDICT:
4892 smsg("%s%4d LOAD w:", pfx, current);
4893 break;
4894 case ISN_LOADTDICT:
4895 smsg("%s%4d LOAD t:", pfx, current);
4896 break;
4897 case ISN_LOADOPT:
4898 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4899 break;
4900 case ISN_LOADENV:
4901 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4902 break;
4903 case ISN_LOADREG:
4904 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4905 break;
4906
4907 case ISN_STORE:
4908 if (iptr->isn_arg.number < 0)
4909 smsg("%s%4d STORE arg[%lld]", pfx, current,
4910 iptr->isn_arg.number + STACK_FRAME_SIZE);
4911 else
4912 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4913 break;
4914 case ISN_STOREOUTER:
4915 {
4916 if (iptr->isn_arg.number < 0)
4917 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4918 iptr->isn_arg.outer.outer_depth,
4919 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4920 else
4921 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4922 iptr->isn_arg.outer.outer_depth,
4923 iptr->isn_arg.outer.outer_idx);
4924 }
4925 break;
4926 case ISN_STOREV:
4927 smsg("%s%4d STOREV v:%s", pfx, current,
4928 get_vim_var_name(iptr->isn_arg.number));
4929 break;
4930 case ISN_STOREAUTO:
4931 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4932 break;
4933 case ISN_STOREG:
4934 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4935 break;
4936 case ISN_STOREB:
4937 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4938 break;
4939 case ISN_STOREW:
4940 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4941 break;
4942 case ISN_STORET:
4943 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4944 break;
4945 case ISN_STORES:
4946 {
4947 scriptitem_T *si = SCRIPT_ITEM(
4948 iptr->isn_arg.loadstore.ls_sid);
4949
4950 smsg("%s%4d STORES %s in %s", pfx, current,
4951 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4952 }
4953 break;
4954 case ISN_STORESCRIPT:
4955 {
4956 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4957 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4958 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4959 + sref->sref_idx;
4960
4961 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4962 sv->sv_name,
4963 sref->sref_idx,
4964 si->sn_name);
4965 }
4966 break;
4967 case ISN_STOREOPT:
4968 smsg("%s%4d STOREOPT &%s", pfx, current,
4969 iptr->isn_arg.storeopt.so_name);
4970 break;
4971 case ISN_STOREENV:
4972 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4973 break;
4974 case ISN_STOREREG:
4975 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4976 break;
4977 case ISN_STORENR:
4978 smsg("%s%4d STORE %lld in $%d", pfx, current,
4979 iptr->isn_arg.storenr.stnr_val,
4980 iptr->isn_arg.storenr.stnr_idx);
4981 break;
4982
4983 case ISN_STOREINDEX:
4984 smsg("%s%4d STOREINDEX %s", pfx, current,
4985 vartype_name(iptr->isn_arg.vartype));
4986 break;
4987
4988 case ISN_STORERANGE:
4989 smsg("%s%4d STORERANGE", pfx, current);
4990 break;
4991
4992 // constants
4993 case ISN_PUSHNR:
4994 smsg("%s%4d PUSHNR %lld", pfx, current,
4995 (varnumber_T)(iptr->isn_arg.number));
4996 break;
4997 case ISN_PUSHBOOL:
4998 case ISN_PUSHSPEC:
4999 smsg("%s%4d PUSH %s", pfx, current,
5000 get_var_special_name(iptr->isn_arg.number));
5001 break;
5002 case ISN_PUSHF:
5003#ifdef FEAT_FLOAT
5004 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5005#endif
5006 break;
5007 case ISN_PUSHS:
5008 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5009 break;
5010 case ISN_PUSHBLOB:
5011 {
5012 char_u *r;
5013 char_u numbuf[NUMBUFLEN];
5014 char_u *tofree;
5015
5016 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5017 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5018 vim_free(tofree);
5019 }
5020 break;
5021 case ISN_PUSHFUNC:
5022 {
5023 char *name = (char *)iptr->isn_arg.string;
5024
5025 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5026 name == NULL ? "[none]" : name);
5027 }
5028 break;
5029 case ISN_PUSHCHANNEL:
5030#ifdef FEAT_JOB_CHANNEL
5031 {
5032 channel_T *channel = iptr->isn_arg.channel;
5033
5034 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5035 channel == NULL ? 0 : channel->ch_id);
5036 }
5037#endif
5038 break;
5039 case ISN_PUSHJOB:
5040#ifdef FEAT_JOB_CHANNEL
5041 {
5042 typval_T tv;
5043 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005044 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005045
5046 tv.v_type = VAR_JOB;
5047 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005048 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005049 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5050 }
5051#endif
5052 break;
5053 case ISN_PUSHEXC:
5054 smsg("%s%4d PUSH v:exception", pfx, current);
5055 break;
5056 case ISN_UNLET:
5057 smsg("%s%4d UNLET%s %s", pfx, current,
5058 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5059 iptr->isn_arg.unlet.ul_name);
5060 break;
5061 case ISN_UNLETENV:
5062 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5063 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5064 iptr->isn_arg.unlet.ul_name);
5065 break;
5066 case ISN_UNLETINDEX:
5067 smsg("%s%4d UNLETINDEX", pfx, current);
5068 break;
5069 case ISN_UNLETRANGE:
5070 smsg("%s%4d UNLETRANGE", pfx, current);
5071 break;
5072 case ISN_LOCKCONST:
5073 smsg("%s%4d LOCKCONST", pfx, current);
5074 break;
5075 case ISN_NEWLIST:
5076 smsg("%s%4d NEWLIST size %lld", pfx, current,
5077 (varnumber_T)(iptr->isn_arg.number));
5078 break;
5079 case ISN_NEWDICT:
5080 smsg("%s%4d NEWDICT size %lld", pfx, current,
5081 (varnumber_T)(iptr->isn_arg.number));
5082 break;
5083
5084 // function call
5085 case ISN_BCALL:
5086 {
5087 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5088
5089 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5090 internal_func_name(cbfunc->cbf_idx),
5091 cbfunc->cbf_argcount);
5092 }
5093 break;
5094 case ISN_DCALL:
5095 {
5096 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5097 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5098 + cdfunc->cdf_idx;
5099
5100 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5101 df->df_ufunc->uf_name_exp != NULL
5102 ? df->df_ufunc->uf_name_exp
5103 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5104 }
5105 break;
5106 case ISN_UCALL:
5107 {
5108 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5109
5110 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5111 cufunc->cuf_name, cufunc->cuf_argcount);
5112 }
5113 break;
5114 case ISN_PCALL:
5115 {
5116 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5117
5118 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5119 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5120 }
5121 break;
5122 case ISN_PCALL_END:
5123 smsg("%s%4d PCALL end", pfx, current);
5124 break;
5125 case ISN_RETURN:
5126 smsg("%s%4d RETURN", pfx, current);
5127 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005128 case ISN_RETURN_VOID:
5129 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005130 break;
5131 case ISN_FUNCREF:
5132 {
5133 funcref_T *funcref = &iptr->isn_arg.funcref;
5134 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5135 + funcref->fr_func;
5136
5137 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5138 }
5139 break;
5140
5141 case ISN_NEWFUNC:
5142 {
5143 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5144
5145 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5146 newfunc->nf_lambda, newfunc->nf_global);
5147 }
5148 break;
5149
5150 case ISN_DEF:
5151 {
5152 char_u *name = iptr->isn_arg.string;
5153
5154 smsg("%s%4d DEF %s", pfx, current,
5155 name == NULL ? (char_u *)"" : name);
5156 }
5157 break;
5158
5159 case ISN_JUMP:
5160 {
5161 char *when = "?";
5162
5163 switch (iptr->isn_arg.jump.jump_when)
5164 {
5165 case JUMP_ALWAYS:
5166 when = "JUMP";
5167 break;
5168 case JUMP_AND_KEEP_IF_TRUE:
5169 when = "JUMP_AND_KEEP_IF_TRUE";
5170 break;
5171 case JUMP_IF_FALSE:
5172 when = "JUMP_IF_FALSE";
5173 break;
5174 case JUMP_AND_KEEP_IF_FALSE:
5175 when = "JUMP_AND_KEEP_IF_FALSE";
5176 break;
5177 case JUMP_IF_COND_FALSE:
5178 when = "JUMP_IF_COND_FALSE";
5179 break;
5180 case JUMP_IF_COND_TRUE:
5181 when = "JUMP_IF_COND_TRUE";
5182 break;
5183 }
5184 smsg("%s%4d %s -> %d", pfx, current, when,
5185 iptr->isn_arg.jump.jump_where);
5186 }
5187 break;
5188
5189 case ISN_JUMP_IF_ARG_SET:
5190 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5191 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5192 iptr->isn_arg.jump.jump_where);
5193 break;
5194
5195 case ISN_FOR:
5196 {
5197 forloop_T *forloop = &iptr->isn_arg.forloop;
5198
5199 smsg("%s%4d FOR $%d -> %d", pfx, current,
5200 forloop->for_idx, forloop->for_end);
5201 }
5202 break;
5203
5204 case ISN_TRY:
5205 {
5206 try_T *try = &iptr->isn_arg.try;
5207
5208 if (try->try_ref->try_finally == 0)
5209 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5210 pfx, current,
5211 try->try_ref->try_catch,
5212 try->try_ref->try_endtry);
5213 else
5214 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5215 pfx, current,
5216 try->try_ref->try_catch,
5217 try->try_ref->try_finally,
5218 try->try_ref->try_endtry);
5219 }
5220 break;
5221 case ISN_CATCH:
5222 // TODO
5223 smsg("%s%4d CATCH", pfx, current);
5224 break;
5225 case ISN_TRYCONT:
5226 {
5227 trycont_T *trycont = &iptr->isn_arg.trycont;
5228
5229 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5230 trycont->tct_levels,
5231 trycont->tct_levels == 1 ? "" : "s",
5232 trycont->tct_where);
5233 }
5234 break;
5235 case ISN_FINALLY:
5236 smsg("%s%4d FINALLY", pfx, current);
5237 break;
5238 case ISN_ENDTRY:
5239 smsg("%s%4d ENDTRY", pfx, current);
5240 break;
5241 case ISN_THROW:
5242 smsg("%s%4d THROW", pfx, current);
5243 break;
5244
5245 // expression operations on number
5246 case ISN_OPNR:
5247 case ISN_OPFLOAT:
5248 case ISN_OPANY:
5249 {
5250 char *what;
5251 char *ins;
5252
5253 switch (iptr->isn_arg.op.op_type)
5254 {
5255 case EXPR_MULT: what = "*"; break;
5256 case EXPR_DIV: what = "/"; break;
5257 case EXPR_REM: what = "%"; break;
5258 case EXPR_SUB: what = "-"; break;
5259 case EXPR_ADD: what = "+"; break;
5260 default: what = "???"; break;
5261 }
5262 switch (iptr->isn_type)
5263 {
5264 case ISN_OPNR: ins = "OPNR"; break;
5265 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5266 case ISN_OPANY: ins = "OPANY"; break;
5267 default: ins = "???"; break;
5268 }
5269 smsg("%s%4d %s %s", pfx, current, ins, what);
5270 }
5271 break;
5272
5273 case ISN_COMPAREBOOL:
5274 case ISN_COMPARESPECIAL:
5275 case ISN_COMPARENR:
5276 case ISN_COMPAREFLOAT:
5277 case ISN_COMPARESTRING:
5278 case ISN_COMPAREBLOB:
5279 case ISN_COMPARELIST:
5280 case ISN_COMPAREDICT:
5281 case ISN_COMPAREFUNC:
5282 case ISN_COMPAREANY:
5283 {
5284 char *p;
5285 char buf[10];
5286 char *type;
5287
5288 switch (iptr->isn_arg.op.op_type)
5289 {
5290 case EXPR_EQUAL: p = "=="; break;
5291 case EXPR_NEQUAL: p = "!="; break;
5292 case EXPR_GREATER: p = ">"; break;
5293 case EXPR_GEQUAL: p = ">="; break;
5294 case EXPR_SMALLER: p = "<"; break;
5295 case EXPR_SEQUAL: p = "<="; break;
5296 case EXPR_MATCH: p = "=~"; break;
5297 case EXPR_IS: p = "is"; break;
5298 case EXPR_ISNOT: p = "isnot"; break;
5299 case EXPR_NOMATCH: p = "!~"; break;
5300 default: p = "???"; break;
5301 }
5302 STRCPY(buf, p);
5303 if (iptr->isn_arg.op.op_ic == TRUE)
5304 strcat(buf, "?");
5305 switch(iptr->isn_type)
5306 {
5307 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5308 case ISN_COMPARESPECIAL:
5309 type = "COMPARESPECIAL"; break;
5310 case ISN_COMPARENR: type = "COMPARENR"; break;
5311 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5312 case ISN_COMPARESTRING:
5313 type = "COMPARESTRING"; break;
5314 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5315 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5316 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5317 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5318 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5319 default: type = "???"; break;
5320 }
5321
5322 smsg("%s%4d %s %s", pfx, current, type, buf);
5323 }
5324 break;
5325
5326 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5327 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5328
5329 // expression operations
5330 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5331 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5332 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5333 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5334 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5335 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5336 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5337 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5338 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5339 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5340 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5341 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5342 pfx, current, iptr->isn_arg.number); break;
5343 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5344 pfx, current, iptr->isn_arg.number); break;
5345 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5346 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5347 iptr->isn_arg.string); break;
5348 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5349
5350 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5351 case ISN_CHECKTYPE:
5352 {
5353 checktype_T *ct = &iptr->isn_arg.type;
5354 char *tofree;
5355
5356 if (ct->ct_arg_idx == 0)
5357 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5358 type_name(ct->ct_type, &tofree),
5359 (int)ct->ct_off);
5360 else
5361 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5362 type_name(ct->ct_type, &tofree),
5363 (int)ct->ct_off,
5364 (int)ct->ct_arg_idx);
5365 vim_free(tofree);
5366 break;
5367 }
5368 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5369 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5370 iptr->isn_arg.checklen.cl_min_len);
5371 break;
5372 case ISN_SETTYPE:
5373 {
5374 char *tofree;
5375
5376 smsg("%s%4d SETTYPE %s", pfx, current,
5377 type_name(iptr->isn_arg.type.ct_type, &tofree));
5378 vim_free(tofree);
5379 break;
5380 }
5381 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005382 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5383 smsg("%s%4d INVERT %d (!val)", pfx, current,
5384 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005385 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005386 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5387 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005388 break;
5389 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005390 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005391 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005392 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5393 pfx, current,
5394 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005395 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005396 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5397 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005398 break;
5399 case ISN_PUT:
5400 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5401 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005402 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005403 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5404 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005405 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005406 else
5407 smsg("%s%4d PUT %c %ld", pfx, current,
5408 iptr->isn_arg.put.put_regname,
5409 (long)iptr->isn_arg.put.put_lnum);
5410 break;
5411
5412 // TODO: summarize modifiers
5413 case ISN_CMDMOD:
5414 {
5415 char_u *buf;
5416 size_t len = produce_cmdmods(
5417 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5418
5419 buf = alloc(len + 1);
5420 if (buf != NULL)
5421 {
5422 (void)produce_cmdmods(
5423 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5424 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5425 vim_free(buf);
5426 }
5427 break;
5428 }
5429 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5430
5431 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005432 smsg("%s%4d PROFILE START line %d", pfx, current,
5433 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005434 break;
5435
5436 case ISN_PROF_END:
5437 smsg("%s%4d PROFILE END", pfx, current);
5438 break;
5439
Bram Moolenaare99d4222021-06-13 14:01:26 +02005440 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005441 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5442 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005443 break;
5444
Bram Moolenaar4c137212021-04-19 16:48:48 +02005445 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5446 iptr->isn_arg.unpack.unp_count,
5447 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5448 break;
5449 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5450 iptr->isn_arg.shuffle.shfl_item,
5451 iptr->isn_arg.shuffle.shfl_up);
5452 break;
5453 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5454
5455 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5456 return;
5457 }
5458
5459 out_flush(); // output one line at a time
5460 ui_breakcheck();
5461 if (got_int)
5462 break;
5463 }
5464}
5465
5466/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005467 * Handle command line completion for the :disassemble command.
5468 */
5469 void
5470set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5471{
5472 char_u *p;
5473
5474 // Default: expand user functions, "debug" and "profile"
5475 xp->xp_context = EXPAND_DISASSEMBLE;
5476 xp->xp_pattern = arg;
5477
5478 // first argument already typed: only user function names
5479 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5480 {
5481 xp->xp_context = EXPAND_USER_FUNC;
5482 xp->xp_pattern = skipwhite(p);
5483 }
5484}
5485
5486/*
5487 * Function given to ExpandGeneric() to obtain the list of :disassemble
5488 * arguments.
5489 */
5490 char_u *
5491get_disassemble_argument(expand_T *xp, int idx)
5492{
5493 if (idx == 0)
5494 return (char_u *)"debug";
5495 if (idx == 1)
5496 return (char_u *)"profile";
5497 return get_user_func_name(xp, idx - 2);
5498}
5499
5500/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005501 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005502 * We don't really need this at runtime, but we do have tests that require it,
5503 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504 */
5505 void
5506ex_disassemble(exarg_T *eap)
5507{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005508 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005509 char_u *fname;
5510 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005511 dfunc_T *dfunc;
5512 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005513 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005514 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005515 compiletype_T compile_type = CT_NONE;
5516
5517 if (STRNCMP(arg, "profile", 7) == 0)
5518 {
5519 compile_type = CT_PROFILE;
5520 arg = skipwhite(arg + 7);
5521 }
5522 else if (STRNCMP(arg, "debug", 5) == 0)
5523 {
5524 compile_type = CT_DEBUG;
5525 arg = skipwhite(arg + 5);
5526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005528 if (STRNCMP(arg, "<lambda>", 8) == 0)
5529 {
5530 arg += 8;
5531 (void)getdigits(&arg);
5532 fname = vim_strnsave(eap->arg, arg - eap->arg);
5533 }
5534 else
5535 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005536 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005537 if (fname == NULL)
5538 {
5539 semsg(_(e_invarg2), eap->arg);
5540 return;
5541 }
5542
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005543 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005544 if (ufunc == NULL)
5545 {
5546 char_u *p = untrans_function_name(fname);
5547
5548 if (p != NULL)
5549 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005550 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005551 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005552 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553 if (ufunc == NULL)
5554 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005555 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 return;
5557 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005558 if (func_needs_compiling(ufunc, compile_type)
5559 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005560 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005561 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005562 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005563 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564 return;
5565 }
5566 if (ufunc->uf_name_exp != NULL)
5567 msg((char *)ufunc->uf_name_exp);
5568 else
5569 msg((char *)ufunc->uf_name);
5570
5571 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005572 switch (compile_type)
5573 {
5574 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005575#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005576 instr = dfunc->df_instr_prof;
5577 instr_count = dfunc->df_instr_prof_count;
5578 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005579#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005580 // FALLTHROUGH
5581 case CT_NONE:
5582 instr = dfunc->df_instr;
5583 instr_count = dfunc->df_instr_count;
5584 break;
5585 case CT_DEBUG:
5586 instr = dfunc->df_instr_debug;
5587 instr_count = dfunc->df_instr_debug_count;
5588 break;
5589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590
Bram Moolenaar4c137212021-04-19 16:48:48 +02005591 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592}
5593
5594/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005595 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005596 * list, etc. Mostly like what JavaScript does, except that empty list and
5597 * empty dictionary are FALSE.
5598 */
5599 int
5600tv2bool(typval_T *tv)
5601{
5602 switch (tv->v_type)
5603 {
5604 case VAR_NUMBER:
5605 return tv->vval.v_number != 0;
5606 case VAR_FLOAT:
5607#ifdef FEAT_FLOAT
5608 return tv->vval.v_float != 0.0;
5609#else
5610 break;
5611#endif
5612 case VAR_PARTIAL:
5613 return tv->vval.v_partial != NULL;
5614 case VAR_FUNC:
5615 case VAR_STRING:
5616 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5617 case VAR_LIST:
5618 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5619 case VAR_DICT:
5620 return tv->vval.v_dict != NULL
5621 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5622 case VAR_BOOL:
5623 case VAR_SPECIAL:
5624 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5625 case VAR_JOB:
5626#ifdef FEAT_JOB_CHANNEL
5627 return tv->vval.v_job != NULL;
5628#else
5629 break;
5630#endif
5631 case VAR_CHANNEL:
5632#ifdef FEAT_JOB_CHANNEL
5633 return tv->vval.v_channel != NULL;
5634#else
5635 break;
5636#endif
5637 case VAR_BLOB:
5638 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5639 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005640 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005642 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643 break;
5644 }
5645 return FALSE;
5646}
5647
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005648 void
5649emsg_using_string_as(typval_T *tv, int as_number)
5650{
5651 semsg(_(as_number ? e_using_string_as_number_str
5652 : e_using_string_as_bool_str),
5653 tv->vval.v_string == NULL
5654 ? (char_u *)"" : tv->vval.v_string);
5655}
5656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657/*
5658 * If "tv" is a string give an error and return FAIL.
5659 */
5660 int
5661check_not_string(typval_T *tv)
5662{
5663 if (tv->v_type == VAR_STRING)
5664 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005665 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666 clear_tv(tv);
5667 return FAIL;
5668 }
5669 return OK;
5670}
5671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672
5673#endif // FEAT_EVAL