blob: 2577e63e2761e79bed36dcb5dc28a90258529f66 [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)
1464 if (ga_grow(&ga, 1) == OK)
1465 ((char_u **)(ga.ga_data))[ga.ga_len++] =
1466 skipwhite(((char_u **)ufunc->uf_lines.ga_data)[lnum - 1]);
1467 line = ga_concat_strings(&ga, " ");
1468 vim_free(ga.ga_data);
1469 }
1470 else
1471 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001472
Dominique Pelle6e969552021-06-17 13:53:41 +02001473 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001474 debug_context = NULL;
1475
1476 if (end_lnum > iptr->isn_lnum)
1477 vim_free(line);
1478}
1479
Bram Moolenaar4c137212021-04-19 16:48:48 +02001480/*
1481 * Execute instructions in execution context "ectx".
1482 * Return OK or FAIL;
1483 */
1484 static int
1485exec_instructions(ectx_T *ectx)
1486{
1487 int breakcheck_count = 0;
1488 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001489 int ret = FAIL;
1490 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001491
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001492 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001493 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001494
Bram Moolenaarff652882021-05-16 15:24:49 +02001495 // Only catch exceptions in this instruction list.
1496 ectx->ec_trylevel_at_start = trylevel;
1497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498 for (;;)
1499 {
1500 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001501
Bram Moolenaar270d0382020-05-15 21:42:53 +02001502 if (++breakcheck_count >= 100)
1503 {
1504 line_breakcheck();
1505 breakcheck_count = 0;
1506 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001507 if (got_int)
1508 {
1509 // Turn CTRL-C into an exception.
1510 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001511 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001512 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001513 did_throw = TRUE;
1514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515
Bram Moolenaara26b9702020-04-18 19:53:28 +02001516 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1517 {
1518 // Turn an error message into an exception.
1519 did_emsg = FALSE;
1520 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001521 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001522 did_throw = TRUE;
1523 *msg_list = NULL;
1524 }
1525
Bram Moolenaar4c137212021-04-19 16:48:48 +02001526 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001528 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001529 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530
1531 // An exception jumps to the first catch, finally, or returns from
1532 // the current function.
1533 if (trystack->ga_len > 0)
1534 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001535 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 {
1537 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001538 ectx->ec_in_catch = TRUE;
1539 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 }
1541 else
1542 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001543 // Not inside try or need to return from current functions.
1544 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001545 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001546 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001547 tv = STACK_TV_BOT(0);
1548 tv->v_type = VAR_NUMBER;
1549 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001550 ++ectx->ec_stack.ga_len;
1551 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001553 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001554 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001555 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001556 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 goto done;
1558 }
1559
Bram Moolenaar4c137212021-04-19 16:48:48 +02001560 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001561 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 }
1563 continue;
1564 }
1565
Bram Moolenaar4c137212021-04-19 16:48:48 +02001566 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 switch (iptr->isn_type)
1568 {
1569 // execute Ex command line
1570 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001571 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001572 source_cookie_T cookie;
1573
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001574 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001575 // Pass getsourceline to get an error for a missing ":end"
1576 // command.
1577 CLEAR_FIELD(cookie);
1578 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1579 if (do_cmdline(iptr->isn_arg.string,
1580 getsourceline, &cookie,
1581 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1582 == FAIL
1583 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001584 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001585 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 break;
1587
Bram Moolenaar20677332021-06-06 17:02:53 +02001588 // execute Ex command line split at NL characters.
1589 case ISN_EXEC_SPLIT:
1590 {
1591 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001592 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001593
1594 SOURCING_LNUM = iptr->isn_lnum;
1595 CLEAR_FIELD(cookie);
1596 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1597 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001598 line = get_split_sourceline(0, &cookie, 0, 0);
1599 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001600 get_split_sourceline, &cookie,
1601 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1602 == FAIL
1603 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001604 {
1605 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001606 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001607 }
1608 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001609 }
1610 break;
1611
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001612 // Evaluate an expression with legacy syntax, push it onto the
1613 // stack.
1614 case ISN_LEGACY_EVAL:
1615 {
1616 char_u *arg = iptr->isn_arg.string;
1617 int res;
1618 int save_flags = cmdmod.cmod_flags;
1619
1620 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001621 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001622 tv = STACK_TV_BOT(0);
1623 init_tv(tv);
1624 cmdmod.cmod_flags |= CMOD_LEGACY;
1625 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1626 cmdmod.cmod_flags = save_flags;
1627 if (res == FAIL)
1628 goto on_error;
1629 ++ectx->ec_stack.ga_len;
1630 }
1631 break;
1632
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001633 // push typeval VAR_INSTR with instructions to be executed
1634 case ISN_INSTR:
1635 {
1636 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001637 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001638 tv = STACK_TV_BOT(0);
1639 tv->vval.v_instr = ALLOC_ONE(instr_T);
1640 if (tv->vval.v_instr == NULL)
1641 goto on_error;
1642 ++ectx->ec_stack.ga_len;
1643
1644 tv->v_type = VAR_INSTR;
1645 tv->vval.v_instr->instr_ectx = ectx;
1646 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1647 }
1648 break;
1649
Bram Moolenaar4c137212021-04-19 16:48:48 +02001650 // execute :substitute with an expression
1651 case ISN_SUBSTITUTE:
1652 {
1653 subs_T *subs = &iptr->isn_arg.subs;
1654 source_cookie_T cookie;
1655 struct subs_expr_S *save_instr = substitute_instr;
1656 struct subs_expr_S subs_instr;
1657 int res;
1658
1659 subs_instr.subs_ectx = ectx;
1660 subs_instr.subs_instr = subs->subs_instr;
1661 subs_instr.subs_status = OK;
1662 substitute_instr = &subs_instr;
1663
1664 SOURCING_LNUM = iptr->isn_lnum;
1665 // This is very much like ISN_EXEC
1666 CLEAR_FIELD(cookie);
1667 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1668 res = do_cmdline(subs->subs_cmd,
1669 getsourceline, &cookie,
1670 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1671 substitute_instr = save_instr;
1672
1673 if (res == FAIL || did_emsg
1674 || subs_instr.subs_status == FAIL)
1675 goto on_error;
1676 }
1677 break;
1678
1679 case ISN_FINISH:
1680 goto done;
1681
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001682 case ISN_REDIRSTART:
1683 // create a dummy entry for var_redir_str()
1684 if (alloc_redir_lval() == FAIL)
1685 goto on_error;
1686
1687 // The output is stored in growarray "redir_ga" until
1688 // redirection ends.
1689 init_redir_ga();
1690 redir_vname = 1;
1691 break;
1692
1693 case ISN_REDIREND:
1694 {
1695 char_u *res = get_clear_redir_ga();
1696
1697 // End redirection, put redirected text on the stack.
1698 clear_redir_lval();
1699 redir_vname = 0;
1700
1701 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1702 {
1703 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001704 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001705 }
1706 tv = STACK_TV_BOT(0);
1707 tv->v_type = VAR_STRING;
1708 tv->vval.v_string = res;
1709 ++ectx->ec_stack.ga_len;
1710 }
1711 break;
1712
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001713 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001714#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001715 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1716 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001717#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001718 break;
1719
1720 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001721#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001722 {
1723 exarg_T ea;
1724 int res;
1725
1726 CLEAR_FIELD(ea);
1727 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1728 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1729 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1730 --ectx->ec_stack.ga_len;
1731 tv = STACK_TV_BOT(0);
1732 res = cexpr_core(&ea, tv);
1733 clear_tv(tv);
1734 if (res == FAIL)
1735 goto on_error;
1736 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001737#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001738 break;
1739
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001740 // execute Ex command from pieces on the stack
1741 case ISN_EXECCONCAT:
1742 {
1743 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001744 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001745 int pass;
1746 int i;
1747 char_u *cmd = NULL;
1748 char_u *str;
1749
1750 for (pass = 1; pass <= 2; ++pass)
1751 {
1752 for (i = 0; i < count; ++i)
1753 {
1754 tv = STACK_TV_BOT(i - count);
1755 str = tv->vval.v_string;
1756 if (str != NULL && *str != NUL)
1757 {
1758 if (pass == 2)
1759 STRCPY(cmd + len, str);
1760 len += STRLEN(str);
1761 }
1762 if (pass == 2)
1763 clear_tv(tv);
1764 }
1765 if (pass == 1)
1766 {
1767 cmd = alloc(len + 1);
1768 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001769 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001770 len = 0;
1771 }
1772 }
1773
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001774 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001775 do_cmdline_cmd(cmd);
1776 vim_free(cmd);
1777 }
1778 break;
1779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 // execute :echo {string} ...
1781 case ISN_ECHO:
1782 {
1783 int count = iptr->isn_arg.echo.echo_count;
1784 int atstart = TRUE;
1785 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001786 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787
1788 for (idx = 0; idx < count; ++idx)
1789 {
1790 tv = STACK_TV_BOT(idx - count);
1791 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1792 &atstart, &needclr);
1793 clear_tv(tv);
1794 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001795 if (needclr)
1796 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001797 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 }
1799 break;
1800
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001801 // :execute {string} ...
1802 // :echomsg {string} ...
1803 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001804 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001805 case ISN_ECHOMSG:
1806 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001807 {
1808 int count = iptr->isn_arg.number;
1809 garray_T ga;
1810 char_u buf[NUMBUFLEN];
1811 char_u *p;
1812 int len;
1813 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001814 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001815
1816 ga_init2(&ga, 1, 80);
1817 for (idx = 0; idx < count; ++idx)
1818 {
1819 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001820 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001821 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001822 if (tv->v_type == VAR_CHANNEL
1823 || tv->v_type == VAR_JOB)
1824 {
1825 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001826 semsg(_(e_using_invalid_value_as_string_str),
1827 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001828 break;
1829 }
1830 else
1831 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001832 }
1833 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001834 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001835
1836 len = (int)STRLEN(p);
1837 if (ga_grow(&ga, len + 2) == FAIL)
1838 failed = TRUE;
1839 else
1840 {
1841 if (ga.ga_len > 0)
1842 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1843 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1844 ga.ga_len += len;
1845 }
1846 clear_tv(tv);
1847 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001848 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001849 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001850 {
1851 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001852 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001853 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001854
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001855 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001856 {
1857 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001858 {
1859 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001860 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001861 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001862 {
1863 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001864 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001865 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001866 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001867 else
1868 {
1869 msg_sb_eol();
1870 if (iptr->isn_type == ISN_ECHOMSG)
1871 {
1872 msg_attr(ga.ga_data, echo_attr);
1873 out_flush();
1874 }
1875 else
1876 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001877 SOURCING_LNUM = iptr->isn_lnum;
1878 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001879 }
1880 }
1881 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001882 ga_clear(&ga);
1883 }
1884 break;
1885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 // load local variable or argument
1887 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001888 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001889 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001891 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 break;
1893
1894 // load v: variable
1895 case ISN_LOADV:
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(get_vim_var_tv(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
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001902 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 case ISN_LOADSCRIPT:
1904 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001905 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 svar_T *sv;
1907
Bram Moolenaar4c137212021-04-19 16:48:48 +02001908 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001909 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001910 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001911 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001912 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001913 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001915 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 }
1917 break;
1918
1919 // load s: variable in old script
1920 case ISN_LOADS:
1921 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001922 hashtab_T *ht = &SCRIPT_VARS(
1923 iptr->isn_arg.loadstore.ls_sid);
1924 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 if (di == NULL)
1928 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001929 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001930 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001931 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 }
1933 else
1934 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001935 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001936 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001938 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 }
1940 }
1941 break;
1942
Bram Moolenaard3aac292020-04-19 14:32:17 +02001943 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001945 case ISN_LOADB:
1946 case ISN_LOADW:
1947 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001949 dictitem_T *di = NULL;
1950 hashtab_T *ht = NULL;
1951 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001952
Bram Moolenaard3aac292020-04-19 14:32:17 +02001953 switch (iptr->isn_type)
1954 {
1955 case ISN_LOADG:
1956 ht = get_globvar_ht();
1957 namespace = 'g';
1958 break;
1959 case ISN_LOADB:
1960 ht = &curbuf->b_vars->dv_hashtab;
1961 namespace = 'b';
1962 break;
1963 case ISN_LOADW:
1964 ht = &curwin->w_vars->dv_hashtab;
1965 namespace = 'w';
1966 break;
1967 case ISN_LOADT:
1968 ht = &curtab->tp_vars->dv_hashtab;
1969 namespace = 't';
1970 break;
1971 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001972 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001973 }
1974 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 if (di == NULL)
1977 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001978 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001979 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001980 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001981 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 }
1983 else
1984 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001985 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001986 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001988 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 }
1990 }
1991 break;
1992
Bram Moolenaar03290b82020-12-19 16:30:44 +01001993 // load autoload variable
1994 case ISN_LOADAUTO:
1995 {
1996 char_u *name = iptr->isn_arg.string;
1997
Bram Moolenaar4c137212021-04-19 16:48:48 +02001998 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001999 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002000 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002001 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002002 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002003 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002004 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002005 }
2006 break;
2007
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002008 // load g:/b:/w:/t: namespace
2009 case ISN_LOADGDICT:
2010 case ISN_LOADBDICT:
2011 case ISN_LOADWDICT:
2012 case ISN_LOADTDICT:
2013 {
2014 dict_T *d = NULL;
2015
2016 switch (iptr->isn_type)
2017 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002018 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2019 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2020 case ISN_LOADWDICT: d = curwin->w_vars; break;
2021 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002022 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002023 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002024 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002025 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002026 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002027 tv = STACK_TV_BOT(0);
2028 tv->v_type = VAR_DICT;
2029 tv->v_lock = 0;
2030 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002031 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002032 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002033 }
2034 break;
2035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 // load &option
2037 case ISN_LOADOPT:
2038 {
2039 typval_T optval;
2040 char_u *name = iptr->isn_arg.string;
2041
Bram Moolenaara8c17702020-04-01 21:17:24 +02002042 // This is not expected to fail, name is checked during
2043 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002044 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002045 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002046 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002047 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002049 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 }
2051 break;
2052
2053 // load $ENV
2054 case ISN_LOADENV:
2055 {
2056 typval_T optval;
2057 char_u *name = iptr->isn_arg.string;
2058
Bram Moolenaar4c137212021-04-19 16:48:48 +02002059 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002060 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002061 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002062 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002064 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 }
2066 break;
2067
2068 // load @register
2069 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002070 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002071 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 tv = STACK_TV_BOT(0);
2073 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002074 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002075 // This may result in NULL, which should be equivalent to an
2076 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077 tv->vval.v_string = get_reg_contents(
2078 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002079 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 break;
2081
2082 // store local variable
2083 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002084 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 tv = STACK_TV_VAR(iptr->isn_arg.number);
2086 clear_tv(tv);
2087 *tv = *STACK_TV_BOT(0);
2088 break;
2089
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002090 // store s: variable in old script
2091 case ISN_STORES:
2092 {
2093 hashtab_T *ht = &SCRIPT_VARS(
2094 iptr->isn_arg.loadstore.ls_sid);
2095 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002096 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002097
Bram Moolenaar4c137212021-04-19 16:48:48 +02002098 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002099 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002100 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002101 else
2102 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002103 SOURCING_LNUM = iptr->isn_lnum;
2104 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002105 {
2106 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002107 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002108 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002109 clear_tv(&di->di_tv);
2110 di->di_tv = *STACK_TV_BOT(0);
2111 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002112 }
2113 break;
2114
2115 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 case ISN_STORESCRIPT:
2117 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002118 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2119 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120
Bram Moolenaar4c137212021-04-19 16:48:48 +02002121 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002122 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002123 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002124 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002125
2126 // "const" and "final" are checked at compile time, locking
2127 // the value needs to be checked here.
2128 SOURCING_LNUM = iptr->isn_lnum;
2129 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002130 {
2131 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002132 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002133 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 clear_tv(sv->sv_tv);
2136 *sv->sv_tv = *STACK_TV_BOT(0);
2137 }
2138 break;
2139
2140 // store option
2141 case ISN_STOREOPT:
2142 {
2143 long n = 0;
2144 char_u *s = NULL;
2145 char *msg;
2146
Bram Moolenaar4c137212021-04-19 16:48:48 +02002147 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 tv = STACK_TV_BOT(0);
2149 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002150 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002152 if (s == NULL)
2153 s = (char_u *)"";
2154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002156 // must be VAR_NUMBER, CHECKTYPE makes sure
2157 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2159 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002160 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 if (msg != NULL)
2162 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002163 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002165 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 }
2168 break;
2169
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002170 // store $ENV
2171 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002172 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002173 tv = STACK_TV_BOT(0);
2174 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2175 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002176 break;
2177
2178 // store @r
2179 case ISN_STOREREG:
2180 {
2181 int reg = iptr->isn_arg.number;
2182
Bram Moolenaar4c137212021-04-19 16:48:48 +02002183 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002184 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002185 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002186 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002187 }
2188 break;
2189
2190 // store v: variable
2191 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002192 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002193 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2194 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002195 // should not happen, type is checked when compiling
2196 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002197 break;
2198
Bram Moolenaard3aac292020-04-19 14:32:17 +02002199 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002201 case ISN_STOREB:
2202 case ISN_STOREW:
2203 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002205 dictitem_T *di;
2206 hashtab_T *ht;
2207 char_u *name = iptr->isn_arg.string + 2;
2208
Bram Moolenaard3aac292020-04-19 14:32:17 +02002209 switch (iptr->isn_type)
2210 {
2211 case ISN_STOREG:
2212 ht = get_globvar_ht();
2213 break;
2214 case ISN_STOREB:
2215 ht = &curbuf->b_vars->dv_hashtab;
2216 break;
2217 case ISN_STOREW:
2218 ht = &curwin->w_vars->dv_hashtab;
2219 break;
2220 case ISN_STORET:
2221 ht = &curtab->tp_vars->dv_hashtab;
2222 break;
2223 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002224 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226
Bram Moolenaar4c137212021-04-19 16:48:48 +02002227 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002228 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002230 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 else
2232 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002233 SOURCING_LNUM = iptr->isn_lnum;
2234 if (var_check_permission(di, name) == FAIL)
2235 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 clear_tv(&di->di_tv);
2237 di->di_tv = *STACK_TV_BOT(0);
2238 }
2239 }
2240 break;
2241
Bram Moolenaar03290b82020-12-19 16:30:44 +01002242 // store an autoload variable
2243 case ISN_STOREAUTO:
2244 SOURCING_LNUM = iptr->isn_lnum;
2245 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2246 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002247 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002248 break;
2249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 // store number in local variable
2251 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002252 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 clear_tv(tv);
2254 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002255 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 break;
2257
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002258 // store value in list or dict variable
2259 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002260 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002261 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002262 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002263 typval_T *tv_dest = STACK_TV_BOT(-1);
2264 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002265
Bram Moolenaar752fc692021-01-04 21:57:11 +01002266 // Stack contains:
2267 // -3 value to be stored
2268 // -2 index
2269 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002270 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002271 SOURCING_LNUM = iptr->isn_lnum;
2272 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002273 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002274 dest_type = tv_dest->v_type;
2275 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002276 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002277 else if (dest_type == VAR_LIST
2278 && tv_idx->v_type != VAR_NUMBER)
2279 {
2280 emsg(_(e_number_exp));
2281 status = FAIL;
2282 }
2283 }
2284 else if (dest_type != tv_dest->v_type)
2285 {
2286 // just in case, should be OK
2287 semsg(_(e_expected_str_but_got_str),
2288 vartype_name(dest_type),
2289 vartype_name(tv_dest->v_type));
2290 status = FAIL;
2291 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002292
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002293 if (status == OK && dest_type == VAR_LIST)
2294 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002295 long lidx = (long)tv_idx->vval.v_number;
2296 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002297
2298 if (list == NULL)
2299 {
2300 emsg(_(e_list_not_set));
2301 goto on_error;
2302 }
2303 if (lidx < 0 && list->lv_len + lidx >= 0)
2304 // negative index is relative to the end
2305 lidx = list->lv_len + lidx;
2306 if (lidx < 0 || lidx > list->lv_len)
2307 {
2308 semsg(_(e_listidx), lidx);
2309 goto on_error;
2310 }
2311 if (lidx < list->lv_len)
2312 {
2313 listitem_T *li = list_find(list, lidx);
2314
2315 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002316 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002317 goto on_error;
2318 // overwrite existing list item
2319 clear_tv(&li->li_tv);
2320 li->li_tv = *tv;
2321 }
2322 else
2323 {
2324 if (error_if_locked(list->lv_lock,
2325 e_cannot_change_list))
2326 goto on_error;
2327 // append to list, only fails when out of memory
2328 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002329 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002330 clear_tv(tv);
2331 }
2332 }
2333 else if (status == OK && dest_type == VAR_DICT)
2334 {
2335 char_u *key = tv_idx->vval.v_string;
2336 dict_T *dict = tv_dest->vval.v_dict;
2337 dictitem_T *di;
2338
2339 SOURCING_LNUM = iptr->isn_lnum;
2340 if (dict == NULL)
2341 {
2342 emsg(_(e_dictionary_not_set));
2343 goto on_error;
2344 }
2345 if (key == NULL)
2346 key = (char_u *)"";
2347 di = dict_find(dict, key, -1);
2348 if (di != NULL)
2349 {
2350 if (error_if_locked(di->di_tv.v_lock,
2351 e_cannot_change_dict_item))
2352 goto on_error;
2353 // overwrite existing value
2354 clear_tv(&di->di_tv);
2355 di->di_tv = *tv;
2356 }
2357 else
2358 {
2359 if (error_if_locked(dict->dv_lock,
2360 e_cannot_change_dict))
2361 goto on_error;
2362 // add to dict, only fails when out of memory
2363 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002364 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002365 clear_tv(tv);
2366 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002367 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002368 else if (status == OK && dest_type == VAR_BLOB)
2369 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002370 long lidx = (long)tv_idx->vval.v_number;
2371 blob_T *blob = tv_dest->vval.v_blob;
2372 varnumber_T nr;
2373 int error = FALSE;
2374 int len;
2375
2376 if (blob == NULL)
2377 {
2378 emsg(_(e_blob_not_set));
2379 goto on_error;
2380 }
2381 len = blob_len(blob);
2382 if (lidx < 0 && len + lidx >= 0)
2383 // negative index is relative to the end
2384 lidx = len + lidx;
2385
2386 // Can add one byte at the end.
2387 if (lidx < 0 || lidx > len)
2388 {
2389 semsg(_(e_blobidx), lidx);
2390 goto on_error;
2391 }
2392 if (value_check_lock(blob->bv_lock,
2393 (char_u *)"blob", FALSE))
2394 goto on_error;
2395 nr = tv_get_number_chk(tv, &error);
2396 if (error)
2397 goto on_error;
2398 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002399 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002400 else
2401 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002402 status = FAIL;
2403 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002404 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002405
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002406 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002407 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002408 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002409 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002410 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002411 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002412 goto on_error;
2413 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002414 }
2415 break;
2416
Bram Moolenaar68452172021-04-12 21:21:02 +02002417 // store value in blob range
2418 case ISN_STORERANGE:
2419 {
2420 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2421 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2422 typval_T *tv_dest = STACK_TV_BOT(-1);
2423 int status = OK;
2424
2425 // Stack contains:
2426 // -4 value to be stored
2427 // -3 first index or "none"
2428 // -2 second index or "none"
2429 // -1 destination blob
2430 tv = STACK_TV_BOT(-4);
2431 if (tv_dest->v_type != VAR_BLOB)
2432 {
2433 status = FAIL;
2434 emsg(_(e_blob_required));
2435 }
2436 else
2437 {
2438 varnumber_T n1;
2439 varnumber_T n2;
2440 int error = FALSE;
2441
2442 n1 = tv_get_number_chk(tv_idx1, &error);
2443 if (error)
2444 status = FAIL;
2445 else
2446 {
2447 if (tv_idx2->v_type == VAR_SPECIAL
2448 && tv_idx2->vval.v_number == VVAL_NONE)
2449 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2450 else
2451 n2 = tv_get_number_chk(tv_idx2, &error);
2452 if (error)
2453 status = FAIL;
2454 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002455 {
2456 long bloblen = blob_len(tv_dest->vval.v_blob);
2457
2458 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002459 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002460 || check_blob_range(bloblen,
2461 n1, n2, FALSE) == FAIL)
2462 status = FAIL;
2463 else
2464 status = blob_set_range(
2465 tv_dest->vval.v_blob, n1, n2, tv);
2466 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002467 }
2468 }
2469
2470 clear_tv(tv_idx1);
2471 clear_tv(tv_idx2);
2472 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002473 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002474 clear_tv(tv);
2475
2476 if (status == FAIL)
2477 goto on_error;
2478 }
2479 break;
2480
Bram Moolenaar0186e582021-01-10 18:33:11 +01002481 // load or store variable or argument from outer scope
2482 case ISN_LOADOUTER:
2483 case ISN_STOREOUTER:
2484 {
2485 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002486 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2487 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002488
2489 while (depth > 1 && outer != NULL)
2490 {
2491 outer = outer->out_up;
2492 --depth;
2493 }
2494 if (outer == NULL)
2495 {
2496 SOURCING_LNUM = iptr->isn_lnum;
2497 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002498 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002499 }
2500 tv = ((typval_T *)outer->out_stack->ga_data)
2501 + outer->out_frame_idx + STACK_FRAME_SIZE
2502 + iptr->isn_arg.outer.outer_idx;
2503 if (iptr->isn_type == ISN_LOADOUTER)
2504 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002505 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002506 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002507 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002508 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002509 }
2510 else
2511 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002512 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002513 clear_tv(tv);
2514 *tv = *STACK_TV_BOT(0);
2515 }
2516 }
2517 break;
2518
Bram Moolenaar752fc692021-01-04 21:57:11 +01002519 // unlet item in list or dict variable
2520 case ISN_UNLETINDEX:
2521 {
2522 typval_T *tv_idx = STACK_TV_BOT(-2);
2523 typval_T *tv_dest = STACK_TV_BOT(-1);
2524 int status = OK;
2525
2526 // Stack contains:
2527 // -2 index
2528 // -1 dict or list
2529 if (tv_dest->v_type == VAR_DICT)
2530 {
2531 // unlet a dict item, index must be a string
2532 if (tv_idx->v_type != VAR_STRING)
2533 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002534 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002535 semsg(_(e_expected_str_but_got_str),
2536 vartype_name(VAR_STRING),
2537 vartype_name(tv_idx->v_type));
2538 status = FAIL;
2539 }
2540 else
2541 {
2542 dict_T *d = tv_dest->vval.v_dict;
2543 char_u *key = tv_idx->vval.v_string;
2544 dictitem_T *di = NULL;
2545
2546 if (key == NULL)
2547 key = (char_u *)"";
2548 if (d != NULL)
2549 di = dict_find(d, key, (int)STRLEN(key));
2550 if (di == NULL)
2551 {
2552 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002553 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002554 semsg(_(e_dictkey), key);
2555 status = FAIL;
2556 }
2557 else
2558 {
2559 // TODO: check for dict or item locked
2560 dictitem_remove(d, di);
2561 }
2562 }
2563 }
2564 else if (tv_dest->v_type == VAR_LIST)
2565 {
2566 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002567 SOURCING_LNUM = iptr->isn_lnum;
2568 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002569 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002570 status = FAIL;
2571 }
2572 else
2573 {
2574 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002575 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002576 listitem_T *li = NULL;
2577
2578 li = list_find(l, n);
2579 if (li == NULL)
2580 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002581 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002582 semsg(_(e_listidx), n);
2583 status = FAIL;
2584 }
2585 else
2586 // TODO: check for list or item locked
2587 listitem_remove(l, li);
2588 }
2589 }
2590 else
2591 {
2592 status = FAIL;
2593 semsg(_(e_cannot_index_str),
2594 vartype_name(tv_dest->v_type));
2595 }
2596
2597 clear_tv(tv_idx);
2598 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002599 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002600 if (status == FAIL)
2601 goto on_error;
2602 }
2603 break;
2604
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002605 // unlet range of items in list variable
2606 case ISN_UNLETRANGE:
2607 {
2608 // Stack contains:
2609 // -3 index1
2610 // -2 index2
2611 // -1 dict or list
2612 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2613 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2614 typval_T *tv_dest = STACK_TV_BOT(-1);
2615 int status = OK;
2616
2617 if (tv_dest->v_type == VAR_LIST)
2618 {
2619 // indexes must be a number
2620 SOURCING_LNUM = iptr->isn_lnum;
2621 if (check_for_number(tv_idx1) == FAIL
2622 || check_for_number(tv_idx2) == FAIL)
2623 {
2624 status = FAIL;
2625 }
2626 else
2627 {
2628 list_T *l = tv_dest->vval.v_list;
2629 long n1 = (long)tv_idx1->vval.v_number;
2630 long n2 = (long)tv_idx2->vval.v_number;
2631 listitem_T *li;
2632
2633 li = list_find_index(l, &n1);
2634 if (li == NULL
2635 || list_unlet_range(l, li, NULL, n1,
2636 TRUE, n2) == FAIL)
2637 status = FAIL;
2638 }
2639 }
2640 else
2641 {
2642 status = FAIL;
2643 SOURCING_LNUM = iptr->isn_lnum;
2644 semsg(_(e_cannot_index_str),
2645 vartype_name(tv_dest->v_type));
2646 }
2647
2648 clear_tv(tv_idx1);
2649 clear_tv(tv_idx2);
2650 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002651 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002652 if (status == FAIL)
2653 goto on_error;
2654 }
2655 break;
2656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 // push constant
2658 case ISN_PUSHNR:
2659 case ISN_PUSHBOOL:
2660 case ISN_PUSHSPEC:
2661 case ISN_PUSHF:
2662 case ISN_PUSHS:
2663 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002664 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002665 case ISN_PUSHCHANNEL:
2666 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002667 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002668 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002670 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002671 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 switch (iptr->isn_type)
2673 {
2674 case ISN_PUSHNR:
2675 tv->v_type = VAR_NUMBER;
2676 tv->vval.v_number = iptr->isn_arg.number;
2677 break;
2678 case ISN_PUSHBOOL:
2679 tv->v_type = VAR_BOOL;
2680 tv->vval.v_number = iptr->isn_arg.number;
2681 break;
2682 case ISN_PUSHSPEC:
2683 tv->v_type = VAR_SPECIAL;
2684 tv->vval.v_number = iptr->isn_arg.number;
2685 break;
2686#ifdef FEAT_FLOAT
2687 case ISN_PUSHF:
2688 tv->v_type = VAR_FLOAT;
2689 tv->vval.v_float = iptr->isn_arg.fnumber;
2690 break;
2691#endif
2692 case ISN_PUSHBLOB:
2693 blob_copy(iptr->isn_arg.blob, tv);
2694 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002695 case ISN_PUSHFUNC:
2696 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002697 if (iptr->isn_arg.string == NULL)
2698 tv->vval.v_string = NULL;
2699 else
2700 tv->vval.v_string =
2701 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002702 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002703 case ISN_PUSHCHANNEL:
2704#ifdef FEAT_JOB_CHANNEL
2705 tv->v_type = VAR_CHANNEL;
2706 tv->vval.v_channel = iptr->isn_arg.channel;
2707 if (tv->vval.v_channel != NULL)
2708 ++tv->vval.v_channel->ch_refcount;
2709#endif
2710 break;
2711 case ISN_PUSHJOB:
2712#ifdef FEAT_JOB_CHANNEL
2713 tv->v_type = VAR_JOB;
2714 tv->vval.v_job = iptr->isn_arg.job;
2715 if (tv->vval.v_job != NULL)
2716 ++tv->vval.v_job->jv_refcount;
2717#endif
2718 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 default:
2720 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002721 tv->vval.v_string = vim_strsave(
2722 iptr->isn_arg.string == NULL
2723 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 }
2725 break;
2726
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002727 case ISN_UNLET:
2728 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2729 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002730 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002731 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002732 case ISN_UNLETENV:
2733 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2734 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002735
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002736 case ISN_LOCKCONST:
2737 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2738 break;
2739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 // create a list from items on the stack; uses a single allocation
2741 // for the list header and the items
2742 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002743 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002744 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 break;
2746
2747 // create a dict from items on the stack
2748 case ISN_NEWDICT:
2749 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002750 int count = iptr->isn_arg.number;
2751 dict_T *dict = dict_alloc();
2752 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002753 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002754 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755
2756 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002757 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 for (idx = 0; idx < count; ++idx)
2759 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002760 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002762 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002763 key = tv->vval.v_string == NULL
2764 ? (char_u *)"" : tv->vval.v_string;
2765 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002766 if (item != NULL)
2767 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002768 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002769 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002770 dict_unref(dict);
2771 goto on_error;
2772 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002773 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 clear_tv(tv);
2775 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002776 {
2777 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002778 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2781 item->di_tv.v_lock = 0;
2782 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002783 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002784 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002785 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 }
2789
2790 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002791 ectx->ec_stack.ga_len -= 2 * count - 1;
2792 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002793 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002795 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 tv = STACK_TV_BOT(-1);
2797 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002798 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 tv->vval.v_dict = dict;
2800 ++dict->dv_refcount;
2801 }
2802 break;
2803
2804 // call a :def function
2805 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002806 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002807 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2808 NULL,
2809 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002810 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002811 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 break;
2813
2814 // call a builtin function
2815 case ISN_BCALL:
2816 SOURCING_LNUM = iptr->isn_lnum;
2817 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2818 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002819 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002820 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 break;
2822
2823 // call a funcref or partial
2824 case ISN_PCALL:
2825 {
2826 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2827 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002828 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829
2830 SOURCING_LNUM = iptr->isn_lnum;
2831 if (pfunc->cpf_top)
2832 {
2833 // funcref is above the arguments
2834 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2835 }
2836 else
2837 {
2838 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002839 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002840 partial_tv = *STACK_TV_BOT(0);
2841 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002843 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002844 if (tv == &partial_tv)
2845 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002847 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 }
2849 break;
2850
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002851 case ISN_PCALL_END:
2852 // PCALL finished, arguments have been consumed and replaced by
2853 // the return value. Now clear the funcref from the stack,
2854 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002855 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002856 clear_tv(STACK_TV_BOT(-1));
2857 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2858 break;
2859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 // call a user defined function or funcref/partial
2861 case ISN_UCALL:
2862 {
2863 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2864
2865 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002866 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002867 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002868 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 }
2870 break;
2871
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002872 // return from a :def function call without a value
2873 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002874 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002875 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002876 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002877 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002878 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002879 tv->vval.v_number = 0;
2880 tv->v_lock = 0;
2881 // FALLTHROUGH
2882
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002883 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 case ISN_RETURN:
2885 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002886 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002887 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002888
2889 if (trystack->ga_len > 0)
2890 trycmd = ((trycmd_T *)trystack->ga_data)
2891 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002892 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002893 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002895 // jump to ":finally" or ":endtry"
2896 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002897 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002898 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002899 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 trycmd->tcd_return = TRUE;
2901 }
2902 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002903 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 }
2905 break;
2906
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002907 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 case ISN_FUNCREF:
2909 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002910 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2911 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2912 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002915 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002916 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002917 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002918 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002919 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002920 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002921 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002922 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002923 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002925 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 tv->vval.v_partial = pt;
2927 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002928 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 }
2930 break;
2931
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002932 // Create a global function from a lambda.
2933 case ISN_NEWFUNC:
2934 {
2935 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2936
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002937 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002938 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002939 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002940 }
2941 break;
2942
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002943 // List functions
2944 case ISN_DEF:
2945 if (iptr->isn_arg.string == NULL)
2946 list_functions(NULL);
2947 else
2948 {
2949 exarg_T ea;
2950
2951 CLEAR_FIELD(ea);
2952 ea.cmd = ea.arg = iptr->isn_arg.string;
2953 define_function(&ea, NULL);
2954 }
2955 break;
2956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 // jump if a condition is met
2958 case ISN_JUMP:
2959 {
2960 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002961 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 int jump = TRUE;
2963
2964 if (when != JUMP_ALWAYS)
2965 {
2966 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002967 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002968 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002969 || when == JUMP_IF_COND_TRUE)
2970 {
2971 SOURCING_LNUM = iptr->isn_lnum;
2972 jump = tv_get_bool_chk(tv, &error);
2973 if (error)
2974 goto on_error;
2975 }
2976 else
2977 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002979 || when == JUMP_AND_KEEP_IF_FALSE
2980 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002982 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 {
2984 // drop the value from the stack
2985 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002986 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 }
2988 }
2989 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002990 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 }
2992 break;
2993
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002994 // Jump if an argument with a default value was already set and not
2995 // v:none.
2996 case ISN_JUMP_IF_ARG_SET:
2997 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2998 if (tv->v_type != VAR_UNKNOWN
2999 && !(tv->v_type == VAR_SPECIAL
3000 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003001 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003002 break;
3003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 // top of a for loop
3005 case ISN_FOR:
3006 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003007 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 typval_T *idxtv =
3009 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3010
Bram Moolenaar4c137212021-04-19 16:48:48 +02003011 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003012 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003013 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003014 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003015 list_T *list = ltv->vval.v_list;
3016
3017 // push the next item from the list
3018 ++idxtv->vval.v_number;
3019 if (list == NULL
3020 || idxtv->vval.v_number >= list->lv_len)
3021 {
3022 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003023 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3024 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003025 }
3026 else if (list->lv_first == &range_list_item)
3027 {
3028 // non-materialized range() list
3029 tv = STACK_TV_BOT(0);
3030 tv->v_type = VAR_NUMBER;
3031 tv->v_lock = 0;
3032 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003034 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003035 }
3036 else
3037 {
3038 listitem_T *li = list_find(list,
3039 idxtv->vval.v_number);
3040
3041 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003042 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003043 }
3044 }
3045 else if (ltv->v_type == VAR_STRING)
3046 {
3047 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003048
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003049 // The index is for the last byte of the previous
3050 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003051 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003052 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003053 {
3054 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003055 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3056 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003057 }
3058 else
3059 {
3060 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3061
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003062 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003063 tv = STACK_TV_BOT(0);
3064 tv->v_type = VAR_STRING;
3065 tv->vval.v_string = vim_strnsave(
3066 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003067 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003068 idxtv->vval.v_number += clen - 1;
3069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003071 else if (ltv->v_type == VAR_BLOB)
3072 {
3073 blob_T *blob = ltv->vval.v_blob;
3074
3075 // When we get here the first time make a copy of the
3076 // blob, so that the iteration still works when it is
3077 // changed.
3078 if (idxtv->vval.v_number == -1 && blob != NULL)
3079 {
3080 blob_copy(blob, ltv);
3081 blob_unref(blob);
3082 blob = ltv->vval.v_blob;
3083 }
3084
3085 // The index is for the previous byte.
3086 ++idxtv->vval.v_number;
3087 if (blob == NULL
3088 || idxtv->vval.v_number >= blob_len(blob))
3089 {
3090 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003091 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3092 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003093 }
3094 else
3095 {
3096 // Push the next byte from the blob.
3097 tv = STACK_TV_BOT(0);
3098 tv->v_type = VAR_NUMBER;
3099 tv->vval.v_number = blob_get(blob,
3100 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003101 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003102 }
3103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 else
3105 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003106 semsg(_(e_for_loop_on_str_not_supported),
3107 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003108 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 }
3110 }
3111 break;
3112
3113 // start of ":try" block
3114 case ISN_TRY:
3115 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003116 trycmd_T *trycmd = NULL;
3117
Bram Moolenaar4c137212021-04-19 16:48:48 +02003118 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003119 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003120 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3121 + ectx->ec_trystack.ga_len;
3122 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003124 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003125 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3126 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003127 trycmd->tcd_catch_idx =
3128 iptr->isn_arg.try.try_ref->try_catch;
3129 trycmd->tcd_finally_idx =
3130 iptr->isn_arg.try.try_ref->try_finally;
3131 trycmd->tcd_endtry_idx =
3132 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 }
3134 break;
3135
3136 case ISN_PUSHEXC:
3137 if (current_exception == NULL)
3138 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003139 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003141 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003143 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003144 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003146 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003148 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 tv->vval.v_string = vim_strsave(
3150 (char_u *)current_exception->value);
3151 break;
3152
3153 case ISN_CATCH:
3154 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003155 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156
Bram Moolenaar4c137212021-04-19 16:48:48 +02003157 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 if (trystack->ga_len > 0)
3159 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003160 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 + trystack->ga_len - 1;
3162 trycmd->tcd_caught = TRUE;
3163 }
3164 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003165 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 catch_exception(current_exception);
3167 }
3168 break;
3169
Bram Moolenaarc150c092021-02-13 15:02:46 +01003170 case ISN_TRYCONT:
3171 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003172 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003173 trycont_T *trycont = &iptr->isn_arg.trycont;
3174 int i;
3175 trycmd_T *trycmd;
3176 int iidx = trycont->tct_where;
3177
3178 if (trystack->ga_len < trycont->tct_levels)
3179 {
3180 siemsg("TRYCONT: expected %d levels, found %d",
3181 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003182 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003183 }
3184 // Make :endtry jump to any outer try block and the last
3185 // :endtry inside the loop to the loop start.
3186 for (i = trycont->tct_levels; i > 0; --i)
3187 {
3188 trycmd = ((trycmd_T *)trystack->ga_data)
3189 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003190 // Add one to tcd_cont to be able to jump to
3191 // instruction with index zero.
3192 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003193 iidx = trycmd->tcd_finally_idx == 0
3194 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003195 }
3196 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003197 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003198 }
3199 break;
3200
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003201 case ISN_FINALLY:
3202 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003203 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003204 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3205 + trystack->ga_len - 1;
3206
3207 // Reset the index to avoid a return statement jumps here
3208 // again.
3209 trycmd->tcd_finally_idx = 0;
3210 break;
3211 }
3212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 // end of ":try" block
3214 case ISN_ENDTRY:
3215 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003216 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217
3218 if (trystack->ga_len > 0)
3219 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003220 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 --trystack->ga_len;
3223 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003224 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 trycmd = ((trycmd_T *)trystack->ga_data)
3226 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003227 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 {
3229 // discard the exception
3230 if (caught_stack == current_exception)
3231 caught_stack = caught_stack->caught;
3232 discard_current_exception();
3233 }
3234
3235 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003236 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003237
Bram Moolenaar4c137212021-04-19 16:48:48 +02003238 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003239 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003240 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003241 clear_tv(STACK_TV_BOT(0));
3242 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003243 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003244 // handling :continue: jump to outer try block or
3245 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003246 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 }
3248 }
3249 break;
3250
3251 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003252 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003253 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003254
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003255 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3256 {
3257 // throwing an exception while using "silent!" causes
3258 // the function to abort but not display an error.
3259 tv = STACK_TV_BOT(-1);
3260 clear_tv(tv);
3261 tv->v_type = VAR_NUMBER;
3262 tv->vval.v_number = 0;
3263 goto done;
3264 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003265 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003266 tv = STACK_TV_BOT(0);
3267 if (tv->vval.v_string == NULL
3268 || *skipwhite(tv->vval.v_string) == NUL)
3269 {
3270 vim_free(tv->vval.v_string);
3271 SOURCING_LNUM = iptr->isn_lnum;
3272 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003273 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003274 }
3275
3276 // Inside a "catch" we need to first discard the caught
3277 // exception.
3278 if (trystack->ga_len > 0)
3279 {
3280 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3281 + trystack->ga_len - 1;
3282 if (trycmd->tcd_caught && current_exception != NULL)
3283 {
3284 // discard the exception
3285 if (caught_stack == current_exception)
3286 caught_stack = caught_stack->caught;
3287 discard_current_exception();
3288 trycmd->tcd_caught = FALSE;
3289 }
3290 }
3291
3292 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3293 == FAIL)
3294 {
3295 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003296 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003297 }
3298 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 break;
3301
3302 // compare with special values
3303 case ISN_COMPAREBOOL:
3304 case ISN_COMPARESPECIAL:
3305 {
3306 typval_T *tv1 = STACK_TV_BOT(-2);
3307 typval_T *tv2 = STACK_TV_BOT(-1);
3308 varnumber_T arg1 = tv1->vval.v_number;
3309 varnumber_T arg2 = tv2->vval.v_number;
3310 int res;
3311
3312 switch (iptr->isn_arg.op.op_type)
3313 {
3314 case EXPR_EQUAL: res = arg1 == arg2; break;
3315 case EXPR_NEQUAL: res = arg1 != arg2; break;
3316 default: res = 0; break;
3317 }
3318
Bram Moolenaar4c137212021-04-19 16:48:48 +02003319 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 tv1->v_type = VAR_BOOL;
3321 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3322 }
3323 break;
3324
3325 // Operation with two number arguments
3326 case ISN_OPNR:
3327 case ISN_COMPARENR:
3328 {
3329 typval_T *tv1 = STACK_TV_BOT(-2);
3330 typval_T *tv2 = STACK_TV_BOT(-1);
3331 varnumber_T arg1 = tv1->vval.v_number;
3332 varnumber_T arg2 = tv2->vval.v_number;
3333 varnumber_T res;
3334
3335 switch (iptr->isn_arg.op.op_type)
3336 {
3337 case EXPR_MULT: res = arg1 * arg2; break;
3338 case EXPR_DIV: res = arg1 / arg2; break;
3339 case EXPR_REM: res = arg1 % arg2; break;
3340 case EXPR_SUB: res = arg1 - arg2; break;
3341 case EXPR_ADD: res = arg1 + arg2; break;
3342
3343 case EXPR_EQUAL: res = arg1 == arg2; break;
3344 case EXPR_NEQUAL: res = arg1 != arg2; break;
3345 case EXPR_GREATER: res = arg1 > arg2; break;
3346 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3347 case EXPR_SMALLER: res = arg1 < arg2; break;
3348 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3349 default: res = 0; break;
3350 }
3351
Bram Moolenaar4c137212021-04-19 16:48:48 +02003352 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 if (iptr->isn_type == ISN_COMPARENR)
3354 {
3355 tv1->v_type = VAR_BOOL;
3356 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3357 }
3358 else
3359 tv1->vval.v_number = res;
3360 }
3361 break;
3362
3363 // Computation with two float arguments
3364 case ISN_OPFLOAT:
3365 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003366#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 {
3368 typval_T *tv1 = STACK_TV_BOT(-2);
3369 typval_T *tv2 = STACK_TV_BOT(-1);
3370 float_T arg1 = tv1->vval.v_float;
3371 float_T arg2 = tv2->vval.v_float;
3372 float_T res = 0;
3373 int cmp = FALSE;
3374
3375 switch (iptr->isn_arg.op.op_type)
3376 {
3377 case EXPR_MULT: res = arg1 * arg2; break;
3378 case EXPR_DIV: res = arg1 / arg2; break;
3379 case EXPR_SUB: res = arg1 - arg2; break;
3380 case EXPR_ADD: res = arg1 + arg2; break;
3381
3382 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3383 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3384 case EXPR_GREATER: cmp = arg1 > arg2; break;
3385 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3386 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3387 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3388 default: cmp = 0; break;
3389 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003390 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 if (iptr->isn_type == ISN_COMPAREFLOAT)
3392 {
3393 tv1->v_type = VAR_BOOL;
3394 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3395 }
3396 else
3397 tv1->vval.v_float = res;
3398 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003399#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 break;
3401
3402 case ISN_COMPARELIST:
3403 {
3404 typval_T *tv1 = STACK_TV_BOT(-2);
3405 typval_T *tv2 = STACK_TV_BOT(-1);
3406 list_T *arg1 = tv1->vval.v_list;
3407 list_T *arg2 = tv2->vval.v_list;
3408 int cmp = FALSE;
3409 int ic = iptr->isn_arg.op.op_ic;
3410
3411 switch (iptr->isn_arg.op.op_type)
3412 {
3413 case EXPR_EQUAL: cmp =
3414 list_equal(arg1, arg2, ic, FALSE); break;
3415 case EXPR_NEQUAL: cmp =
3416 !list_equal(arg1, arg2, ic, FALSE); break;
3417 case EXPR_IS: cmp = arg1 == arg2; break;
3418 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3419 default: cmp = 0; break;
3420 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003421 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 clear_tv(tv1);
3423 clear_tv(tv2);
3424 tv1->v_type = VAR_BOOL;
3425 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3426 }
3427 break;
3428
3429 case ISN_COMPAREBLOB:
3430 {
3431 typval_T *tv1 = STACK_TV_BOT(-2);
3432 typval_T *tv2 = STACK_TV_BOT(-1);
3433 blob_T *arg1 = tv1->vval.v_blob;
3434 blob_T *arg2 = tv2->vval.v_blob;
3435 int cmp = FALSE;
3436
3437 switch (iptr->isn_arg.op.op_type)
3438 {
3439 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3440 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3441 case EXPR_IS: cmp = arg1 == arg2; break;
3442 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3443 default: cmp = 0; break;
3444 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003445 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 clear_tv(tv1);
3447 clear_tv(tv2);
3448 tv1->v_type = VAR_BOOL;
3449 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3450 }
3451 break;
3452
3453 // TODO: handle separately
3454 case ISN_COMPARESTRING:
3455 case ISN_COMPAREDICT:
3456 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 case ISN_COMPAREANY:
3458 {
3459 typval_T *tv1 = STACK_TV_BOT(-2);
3460 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003461 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 int ic = iptr->isn_arg.op.op_ic;
3463
Bram Moolenaareb26f432020-09-14 16:50:05 +02003464 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003465 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003467 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 }
3469 break;
3470
3471 case ISN_ADDLIST:
3472 case ISN_ADDBLOB:
3473 {
3474 typval_T *tv1 = STACK_TV_BOT(-2);
3475 typval_T *tv2 = STACK_TV_BOT(-1);
3476
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003477 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 if (iptr->isn_type == ISN_ADDLIST)
3479 eval_addlist(tv1, tv2);
3480 else
3481 eval_addblob(tv1, tv2);
3482 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003483 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 }
3485 break;
3486
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003487 case ISN_LISTAPPEND:
3488 {
3489 typval_T *tv1 = STACK_TV_BOT(-2);
3490 typval_T *tv2 = STACK_TV_BOT(-1);
3491 list_T *l = tv1->vval.v_list;
3492
3493 // add an item to a list
3494 if (l == NULL)
3495 {
3496 SOURCING_LNUM = iptr->isn_lnum;
3497 emsg(_(e_cannot_add_to_null_list));
3498 goto on_error;
3499 }
3500 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003501 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003502 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003503 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003504 }
3505 break;
3506
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003507 case ISN_BLOBAPPEND:
3508 {
3509 typval_T *tv1 = STACK_TV_BOT(-2);
3510 typval_T *tv2 = STACK_TV_BOT(-1);
3511 blob_T *b = tv1->vval.v_blob;
3512 int error = FALSE;
3513 varnumber_T n;
3514
3515 // add a number to a blob
3516 if (b == NULL)
3517 {
3518 SOURCING_LNUM = iptr->isn_lnum;
3519 emsg(_(e_cannot_add_to_null_blob));
3520 goto on_error;
3521 }
3522 n = tv_get_number_chk(tv2, &error);
3523 if (error)
3524 goto on_error;
3525 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003526 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003527 }
3528 break;
3529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 // Computation with two arguments of unknown type
3531 case ISN_OPANY:
3532 {
3533 typval_T *tv1 = STACK_TV_BOT(-2);
3534 typval_T *tv2 = STACK_TV_BOT(-1);
3535 varnumber_T n1, n2;
3536#ifdef FEAT_FLOAT
3537 float_T f1 = 0, f2 = 0;
3538#endif
3539 int error = FALSE;
3540
3541 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3542 {
3543 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3544 {
3545 eval_addlist(tv1, tv2);
3546 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003547 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 break;
3549 }
3550 else if (tv1->v_type == VAR_BLOB
3551 && tv2->v_type == VAR_BLOB)
3552 {
3553 eval_addblob(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 }
3559#ifdef FEAT_FLOAT
3560 if (tv1->v_type == VAR_FLOAT)
3561 {
3562 f1 = tv1->vval.v_float;
3563 n1 = 0;
3564 }
3565 else
3566#endif
3567 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003568 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 n1 = tv_get_number_chk(tv1, &error);
3570 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003571 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572#ifdef FEAT_FLOAT
3573 if (tv2->v_type == VAR_FLOAT)
3574 f1 = n1;
3575#endif
3576 }
3577#ifdef FEAT_FLOAT
3578 if (tv2->v_type == VAR_FLOAT)
3579 {
3580 f2 = tv2->vval.v_float;
3581 n2 = 0;
3582 }
3583 else
3584#endif
3585 {
3586 n2 = tv_get_number_chk(tv2, &error);
3587 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003588 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589#ifdef FEAT_FLOAT
3590 if (tv1->v_type == VAR_FLOAT)
3591 f2 = n2;
3592#endif
3593 }
3594#ifdef FEAT_FLOAT
3595 // if there is a float on either side the result is a float
3596 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3597 {
3598 switch (iptr->isn_arg.op.op_type)
3599 {
3600 case EXPR_MULT: f1 = f1 * f2; break;
3601 case EXPR_DIV: f1 = f1 / f2; break;
3602 case EXPR_SUB: f1 = f1 - f2; break;
3603 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003604 default: SOURCING_LNUM = iptr->isn_lnum;
3605 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003606 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 }
3608 clear_tv(tv1);
3609 clear_tv(tv2);
3610 tv1->v_type = VAR_FLOAT;
3611 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003612 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 }
3614 else
3615#endif
3616 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003617 int failed = FALSE;
3618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 switch (iptr->isn_arg.op.op_type)
3620 {
3621 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003622 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3623 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003624 goto on_error;
3625 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 case EXPR_SUB: n1 = n1 - n2; break;
3627 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003628 default: n1 = num_modulus(n1, n2, &failed);
3629 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003630 goto on_error;
3631 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 }
3633 clear_tv(tv1);
3634 clear_tv(tv2);
3635 tv1->v_type = VAR_NUMBER;
3636 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003637 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 }
3639 }
3640 break;
3641
3642 case ISN_CONCAT:
3643 {
3644 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3645 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3646 char_u *res;
3647
3648 res = concat_str(str1, str2);
3649 clear_tv(STACK_TV_BOT(-2));
3650 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003651 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 STACK_TV_BOT(-1)->vval.v_string = res;
3653 }
3654 break;
3655
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003656 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003657 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003658 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003659 int is_slice = iptr->isn_type == ISN_STRSLICE;
3660 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003661 char_u *res;
3662
3663 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003664 // string slice: string is at stack-3, first index at
3665 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003666 if (is_slice)
3667 {
3668 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003669 n1 = tv->vval.v_number;
3670 }
3671
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003672 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003673 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003674
Bram Moolenaar4c137212021-04-19 16:48:48 +02003675 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003676 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003677 if (is_slice)
3678 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003679 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003680 else
3681 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003682 // single character (including composing characters).
3683 // If the index is too big or negative the result is
3684 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003685 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003686 vim_free(tv->vval.v_string);
3687 tv->vval.v_string = res;
3688 }
3689 break;
3690
3691 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003692 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003693 case ISN_BLOBINDEX:
3694 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003696 int is_slice = iptr->isn_type == ISN_LISTSLICE
3697 || iptr->isn_type == ISN_BLOBSLICE;
3698 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3699 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003700 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003701 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702
3703 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003704 // list slice: list is at stack-3, indexes at stack-2 and
3705 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003706 // Same for blob.
3707 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708
3709 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003710 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003712
3713 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 {
Bram Moolenaared591872020-08-15 22:14:53 +02003715 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003716 n1 = tv->vval.v_number;
3717 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 }
Bram Moolenaared591872020-08-15 22:14:53 +02003719
Bram Moolenaar4c137212021-04-19 16:48:48 +02003720 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003721 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003722 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003723 if (is_blob)
3724 {
3725 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3726 n1, n2, FALSE, tv) == FAIL)
3727 goto on_error;
3728 }
3729 else
3730 {
3731 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3732 n1, n2, FALSE, tv, TRUE) == FAIL)
3733 goto on_error;
3734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 }
3736 break;
3737
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003738 case ISN_ANYINDEX:
3739 case ISN_ANYSLICE:
3740 {
3741 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3742 typval_T *var1, *var2;
3743 int res;
3744
3745 // index: composite is at stack-2, index at stack-1
3746 // slice: composite is at stack-3, indexes at stack-2 and
3747 // stack-1
3748 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003749 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003750 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3751 goto on_error;
3752 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3753 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003754 res = eval_index_inner(tv, is_slice, var1, var2,
3755 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003756 clear_tv(var1);
3757 if (is_slice)
3758 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003759 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003760 if (res == FAIL)
3761 goto on_error;
3762 }
3763 break;
3764
Bram Moolenaar9af78762020-06-16 11:34:42 +02003765 case ISN_SLICE:
3766 {
3767 list_T *list;
3768 int count = iptr->isn_arg.number;
3769
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003770 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003771 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003772 list = tv->vval.v_list;
3773
3774 // no error for short list, expect it to be checked earlier
3775 if (list != NULL && list->lv_len >= count)
3776 {
3777 list_T *newlist = list_slice(list,
3778 count, list->lv_len - 1);
3779
3780 if (newlist != NULL)
3781 {
3782 list_unref(list);
3783 tv->vval.v_list = newlist;
3784 ++newlist->lv_refcount;
3785 }
3786 }
3787 }
3788 break;
3789
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003790 case ISN_GETITEM:
3791 {
3792 listitem_T *li;
3793 int index = iptr->isn_arg.number;
3794
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003795 // Get list item: list is at stack-1, push item.
3796 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003797 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003798 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003799
Bram Moolenaar4c137212021-04-19 16:48:48 +02003800 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003801 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003802 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003803 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003804
3805 // Useful when used in unpack assignment. Reset at
3806 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003807 ectx->ec_where.wt_index = index + 1;
3808 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003809 }
3810 break;
3811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 case ISN_MEMBER:
3813 {
3814 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003815 char_u *key;
3816 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003817 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003818
3819 // dict member: dict is at stack-2, key at stack-1
3820 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003821 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003822 dict = tv->vval.v_dict;
3823
3824 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003825 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003826 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003827 if (key == NULL)
3828 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003829
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003830 if ((di = dict_find(dict, key, -1)) == NULL)
3831 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003832 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003833 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003834
3835 // If :silent! is used we will continue, make sure the
3836 // stack contents makes sense.
3837 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003839 tv = STACK_TV_BOT(-1);
3840 clear_tv(tv);
3841 tv->v_type = VAR_NUMBER;
3842 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003843 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003844 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003845 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003846 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003847 // Clear the dict only after getting the item, to avoid
3848 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003849 tv = STACK_TV_BOT(-1);
3850 temp_tv = *tv;
3851 copy_tv(&di->di_tv, tv);
3852 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003853 }
3854 break;
3855
3856 // dict member with string key
3857 case ISN_STRINGMEMBER:
3858 {
3859 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003861 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862
3863 tv = STACK_TV_BOT(-1);
3864 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3865 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003866 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003868 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 }
3870 dict = tv->vval.v_dict;
3871
3872 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3873 == NULL)
3874 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003875 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003877 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003879 // Clear the dict after getting the item, to avoid that it
3880 // make the item invalid.
3881 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003883 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 }
3885 break;
3886
3887 case ISN_NEGATENR:
3888 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003889 if (tv->v_type != VAR_NUMBER
3890#ifdef FEAT_FLOAT
3891 && tv->v_type != VAR_FLOAT
3892#endif
3893 )
3894 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003895 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003896 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003897 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003898 }
3899#ifdef FEAT_FLOAT
3900 if (tv->v_type == VAR_FLOAT)
3901 tv->vval.v_float = -tv->vval.v_float;
3902 else
3903#endif
3904 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 break;
3906
3907 case ISN_CHECKNR:
3908 {
3909 int error = FALSE;
3910
3911 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003912 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003914 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 (void)tv_get_number_chk(tv, &error);
3916 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003917 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 }
3919 break;
3920
3921 case ISN_CHECKTYPE:
3922 {
3923 checktype_T *ct = &iptr->isn_arg.type;
3924
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003925 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003926 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003927 if (!ectx->ec_where.wt_variable)
3928 ectx->ec_where.wt_index = ct->ct_arg_idx;
3929 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3930 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003931 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003932 if (!ectx->ec_where.wt_variable)
3933 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003934
3935 // number 0 is FALSE, number 1 is TRUE
3936 if (tv->v_type == VAR_NUMBER
3937 && ct->ct_type->tt_type == VAR_BOOL
3938 && (tv->vval.v_number == 0
3939 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003941 tv->v_type = VAR_BOOL;
3942 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003943 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 }
3945 }
3946 break;
3947
Bram Moolenaar9af78762020-06-16 11:34:42 +02003948 case ISN_CHECKLEN:
3949 {
3950 int min_len = iptr->isn_arg.checklen.cl_min_len;
3951 list_T *list = NULL;
3952
3953 tv = STACK_TV_BOT(-1);
3954 if (tv->v_type == VAR_LIST)
3955 list = tv->vval.v_list;
3956 if (list == NULL || list->lv_len < min_len
3957 || (list->lv_len > min_len
3958 && !iptr->isn_arg.checklen.cl_more_OK))
3959 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003960 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003961 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003962 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003963 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003964 }
3965 }
3966 break;
3967
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003968 case ISN_SETTYPE:
3969 {
3970 checktype_T *ct = &iptr->isn_arg.type;
3971
3972 tv = STACK_TV_BOT(-1);
3973 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3974 {
3975 free_type(tv->vval.v_dict->dv_type);
3976 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3977 }
3978 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3979 {
3980 free_type(tv->vval.v_list->lv_type);
3981 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3982 }
3983 }
3984 break;
3985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003987 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 {
3989 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003990 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003992 if (iptr->isn_type == ISN_2BOOL)
3993 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003994 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003995 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003996 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003997 n = !n;
3998 }
3999 else
4000 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004001 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004002 SOURCING_LNUM = iptr->isn_lnum;
4003 n = tv_get_bool_chk(tv, &error);
4004 if (error)
4005 goto on_error;
4006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 clear_tv(tv);
4008 tv->v_type = VAR_BOOL;
4009 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4010 }
4011 break;
4012
4013 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004014 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004015 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004016 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4017 iptr->isn_type == ISN_2STRING_ANY,
4018 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004019 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 break;
4021
Bram Moolenaar08597872020-12-10 19:43:40 +01004022 case ISN_RANGE:
4023 {
4024 exarg_T ea;
4025 char *errormsg;
4026
Bram Moolenaarece0b872021-01-08 20:40:45 +01004027 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004028 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004029 ea.addr_type = ADDR_LINES;
4030 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004031 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004032 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004033 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004034
Bram Moolenaar4c137212021-04-19 16:48:48 +02004035 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004036 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004037 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004038 tv = STACK_TV_BOT(-1);
4039 tv->v_type = VAR_NUMBER;
4040 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004041 if (ea.addr_count == 0)
4042 tv->vval.v_number = curwin->w_cursor.lnum;
4043 else
4044 tv->vval.v_number = ea.line2;
4045 }
4046 break;
4047
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004048 case ISN_PUT:
4049 {
4050 int regname = iptr->isn_arg.put.put_regname;
4051 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4052 char_u *expr = NULL;
4053 int dir = FORWARD;
4054
Bram Moolenaar08597872020-12-10 19:43:40 +01004055 if (lnum < -2)
4056 {
4057 // line number was put on the stack by ISN_RANGE
4058 tv = STACK_TV_BOT(-1);
4059 curwin->w_cursor.lnum = tv->vval.v_number;
4060 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4061 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004062 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004063 }
4064 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004065 // :put! above cursor
4066 dir = BACKWARD;
4067 else if (lnum >= 0)
4068 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004069
4070 if (regname == '=')
4071 {
4072 tv = STACK_TV_BOT(-1);
4073 if (tv->v_type == VAR_STRING)
4074 expr = tv->vval.v_string;
4075 else
4076 {
4077 expr = typval2string(tv, TRUE); // allocates value
4078 clear_tv(tv);
4079 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004080 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004081 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004082 check_cursor();
4083 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4084 vim_free(expr);
4085 }
4086 break;
4087
Bram Moolenaar02194d22020-10-24 23:08:38 +02004088 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004089 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4090 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4091 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4092 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004093 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4094 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004095 break;
4096
Bram Moolenaar02194d22020-10-24 23:08:38 +02004097 case ISN_CMDMOD_REV:
4098 // filter regprog is owned by the instruction, don't free it
4099 cmdmod.cmod_filter_regmatch.regprog = NULL;
4100 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004101 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4102 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004103 break;
4104
Bram Moolenaar792f7862020-11-23 08:31:18 +01004105 case ISN_UNPACK:
4106 {
4107 int count = iptr->isn_arg.unpack.unp_count;
4108 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4109 list_T *l;
4110 listitem_T *li;
4111 int i;
4112
4113 // Check there is a valid list to unpack.
4114 tv = STACK_TV_BOT(-1);
4115 if (tv->v_type != VAR_LIST)
4116 {
4117 SOURCING_LNUM = iptr->isn_lnum;
4118 emsg(_(e_for_argument_must_be_sequence_of_lists));
4119 goto on_error;
4120 }
4121 l = tv->vval.v_list;
4122 if (l == NULL
4123 || l->lv_len < (semicolon ? count - 1 : count))
4124 {
4125 SOURCING_LNUM = iptr->isn_lnum;
4126 emsg(_(e_list_value_does_not_have_enough_items));
4127 goto on_error;
4128 }
4129 else if (!semicolon && l->lv_len > count)
4130 {
4131 SOURCING_LNUM = iptr->isn_lnum;
4132 emsg(_(e_list_value_has_more_items_than_targets));
4133 goto on_error;
4134 }
4135
4136 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004137 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004138 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004139 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004140
4141 // Variable after semicolon gets a list with the remaining
4142 // items.
4143 if (semicolon)
4144 {
4145 list_T *rem_list =
4146 list_alloc_with_items(l->lv_len - count + 1);
4147
4148 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004149 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004150 tv = STACK_TV_BOT(-count);
4151 tv->vval.v_list = rem_list;
4152 ++rem_list->lv_refcount;
4153 tv->v_lock = 0;
4154 li = l->lv_first;
4155 for (i = 0; i < count - 1; ++i)
4156 li = li->li_next;
4157 for (i = 0; li != NULL; ++i)
4158 {
4159 list_set_item(rem_list, i, &li->li_tv);
4160 li = li->li_next;
4161 }
4162 --count;
4163 }
4164
4165 // Produce the values in reverse order, first item last.
4166 li = l->lv_first;
4167 for (i = 0; i < count; ++i)
4168 {
4169 tv = STACK_TV_BOT(-i - 1);
4170 copy_tv(&li->li_tv, tv);
4171 li = li->li_next;
4172 }
4173
4174 list_unref(l);
4175 }
4176 break;
4177
Bram Moolenaarb2049902021-01-24 12:53:53 +01004178 case ISN_PROF_START:
4179 case ISN_PROF_END:
4180 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004181#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004182 funccall_T cookie;
4183 ufunc_T *cur_ufunc =
4184 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004185 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004186
4187 cookie.func = cur_ufunc;
4188 if (iptr->isn_type == ISN_PROF_START)
4189 {
4190 func_line_start(&cookie, iptr->isn_lnum);
4191 // if we get here the instruction is executed
4192 func_line_exec(&cookie);
4193 }
4194 else
4195 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004196#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004197 }
4198 break;
4199
Bram Moolenaare99d4222021-06-13 14:01:26 +02004200 case ISN_DEBUG:
4201 if (ex_nesting_level <= debug_break_level)
Bram Moolenaar4cea5362021-06-16 22:24:40 +02004202 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004203 break;
4204
Bram Moolenaar389df252020-07-09 21:20:47 +02004205 case ISN_SHUFFLE:
4206 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004207 typval_T tmp_tv;
4208 int item = iptr->isn_arg.shuffle.shfl_item;
4209 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004210
4211 tmp_tv = *STACK_TV_BOT(-item);
4212 for ( ; up > 0 && item > 1; --up)
4213 {
4214 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4215 --item;
4216 }
4217 *STACK_TV_BOT(-item) = tmp_tv;
4218 }
4219 break;
4220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004222 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004224 ectx->ec_where.wt_index = 0;
4225 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 break;
4227 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004228 continue;
4229
Bram Moolenaard032f342020-07-18 18:13:02 +02004230func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004231 // Restore previous function. If the frame pointer is where we started
4232 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004233 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004234 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004235
Bram Moolenaar4c137212021-04-19 16:48:48 +02004236 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004237 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004238 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004239 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004240
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004241on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004242 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004243 // If "emsg_silent" is set then ignore the error, unless it was set
4244 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004245 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004246 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004247 {
4248 // If a sequence of instructions causes an error while ":silent!"
4249 // was used, restore the stack length and jump ahead to restoring
4250 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004251 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004252 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004253 while (ectx->ec_stack.ga_len
4254 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004255 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004256 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004257 clear_tv(STACK_TV_BOT(0));
4258 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004259 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4260 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004261 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004262 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004263 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004264on_fatal_error:
4265 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004266 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004267 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004268 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 }
4270
4271done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004272 ret = OK;
4273theend:
4274 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4275 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004276}
4277
4278/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004279 * Execute the instructions from a VAR_INSTR typeval and put the result in
4280 * "rettv".
4281 * Return OK or FAIL.
4282 */
4283 int
4284exe_typval_instr(typval_T *tv, typval_T *rettv)
4285{
4286 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4287 isn_T *save_instr = ectx->ec_instr;
4288 int save_iidx = ectx->ec_iidx;
4289 int res;
4290
4291 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4292 res = exec_instructions(ectx);
4293 if (res == OK)
4294 {
4295 *rettv = *STACK_TV_BOT(-1);
4296 --ectx->ec_stack.ga_len;
4297 }
4298
4299 ectx->ec_instr = save_instr;
4300 ectx->ec_iidx = save_iidx;
4301
4302 return res;
4303}
4304
4305/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004306 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4307 * "substitute_instr".
4308 */
4309 char_u *
4310exe_substitute_instr(void)
4311{
4312 ectx_T *ectx = substitute_instr->subs_ectx;
4313 isn_T *save_instr = ectx->ec_instr;
4314 int save_iidx = ectx->ec_iidx;
4315 char_u *res;
4316
4317 ectx->ec_instr = substitute_instr->subs_instr;
4318 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004319 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004320 typval_T *tv = STACK_TV_BOT(-1);
4321
Bram Moolenaar27523602021-06-05 21:36:19 +02004322 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004323 --ectx->ec_stack.ga_len;
4324 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004325 }
4326 else
4327 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004328 substitute_instr->subs_status = FAIL;
4329 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331
Bram Moolenaar4c137212021-04-19 16:48:48 +02004332 ectx->ec_instr = save_instr;
4333 ectx->ec_iidx = save_iidx;
4334
4335 return res;
4336}
4337
4338/*
4339 * Call a "def" function from old Vim script.
4340 * Return OK or FAIL.
4341 */
4342 int
4343call_def_function(
4344 ufunc_T *ufunc,
4345 int argc_arg, // nr of arguments
4346 typval_T *argv, // arguments
4347 partial_T *partial, // optional partial for context
4348 typval_T *rettv) // return value
4349{
4350 ectx_T ectx; // execution context
4351 int argc = argc_arg;
4352 typval_T *tv;
4353 int idx;
4354 int ret = FAIL;
4355 int defcount = ufunc->uf_args.ga_len - argc;
4356 sctx_T save_current_sctx = current_sctx;
4357 int did_emsg_before = did_emsg_cumul + did_emsg;
4358 int save_suppress_errthrow = suppress_errthrow;
4359 msglist_T **saved_msg_list = NULL;
4360 msglist_T *private_msg_list = NULL;
4361 int save_emsg_silent_def = emsg_silent_def;
4362 int save_did_emsg_def = did_emsg_def;
4363 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004364 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004365
4366// Get pointer to item in the stack.
4367#undef STACK_TV
4368#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4369
4370// Get pointer to item at the bottom of the stack, -1 is the bottom.
4371#undef STACK_TV_BOT
4372#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4373
4374// Get pointer to a local variable on the stack. Negative for arguments.
4375#undef STACK_TV_VAR
4376#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4377
4378 if (ufunc->uf_def_status == UF_NOT_COMPILED
4379 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004380 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4381 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004382 == FAIL))
4383 {
4384 if (did_emsg_cumul + did_emsg == did_emsg_before)
4385 semsg(_(e_function_is_not_compiled_str),
4386 printable_func_name(ufunc));
4387 return FAIL;
4388 }
4389
4390 {
4391 // Check the function was really compiled.
4392 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4393 + ufunc->uf_dfunc_idx;
4394 if (INSTRUCTIONS(dfunc) == NULL)
4395 {
4396 iemsg("using call_def_function() on not compiled function");
4397 return FAIL;
4398 }
4399 }
4400
4401 // If depth of calling is getting too high, don't execute the function.
4402 orig_funcdepth = funcdepth_get();
4403 if (funcdepth_increment() == FAIL)
4404 return FAIL;
4405
4406 CLEAR_FIELD(ectx);
4407 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4408 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4409 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4410 {
4411 funcdepth_decrement();
4412 return FAIL;
4413 }
4414 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4415 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4416 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004417 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004418
4419 idx = argc - ufunc->uf_args.ga_len;
4420 if (idx > 0 && ufunc->uf_va_name == NULL)
4421 {
4422 if (idx == 1)
4423 emsg(_(e_one_argument_too_many));
4424 else
4425 semsg(_(e_nr_arguments_too_many), idx);
4426 goto failed_early;
4427 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004428 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4429 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004430 {
4431 if (idx == -1)
4432 emsg(_(e_one_argument_too_few));
4433 else
4434 semsg(_(e_nr_arguments_too_few), -idx);
4435 goto failed_early;
4436 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004437
4438 // Put arguments on the stack, but no more than what the function expects.
4439 // A lambda can be called with more arguments than it uses.
4440 for (idx = 0; idx < argc
4441 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4442 ++idx)
4443 {
4444 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4445 && argv[idx].v_type == VAR_SPECIAL
4446 && argv[idx].vval.v_number == VVAL_NONE)
4447 {
4448 // Use the default value.
4449 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4450 }
4451 else
4452 {
4453 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4454 && check_typval_arg_type(
4455 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4456 goto failed_early;
4457 copy_tv(&argv[idx], STACK_TV_BOT(0));
4458 }
4459 ++ectx.ec_stack.ga_len;
4460 }
4461
4462 // Turn varargs into a list. Empty list if no args.
4463 if (ufunc->uf_va_name != NULL)
4464 {
4465 int vararg_count = argc - ufunc->uf_args.ga_len;
4466
4467 if (vararg_count < 0)
4468 vararg_count = 0;
4469 else
4470 argc -= vararg_count;
4471 if (exe_newlist(vararg_count, &ectx) == FAIL)
4472 goto failed_early;
4473
4474 // Check the type of the list items.
4475 tv = STACK_TV_BOT(-1);
4476 if (ufunc->uf_va_type != NULL
4477 && ufunc->uf_va_type != &t_list_any
4478 && ufunc->uf_va_type->tt_member != &t_any
4479 && tv->vval.v_list != NULL)
4480 {
4481 type_T *expected = ufunc->uf_va_type->tt_member;
4482 listitem_T *li = tv->vval.v_list->lv_first;
4483
4484 for (idx = 0; idx < vararg_count; ++idx)
4485 {
4486 if (check_typval_arg_type(expected, &li->li_tv,
4487 argc + idx + 1) == FAIL)
4488 goto failed_early;
4489 li = li->li_next;
4490 }
4491 }
4492
4493 if (defcount > 0)
4494 // Move varargs list to below missing default arguments.
4495 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4496 --ectx.ec_stack.ga_len;
4497 }
4498
4499 // Make space for omitted arguments, will store default value below.
4500 // Any varargs list goes after them.
4501 if (defcount > 0)
4502 for (idx = 0; idx < defcount; ++idx)
4503 {
4504 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4505 ++ectx.ec_stack.ga_len;
4506 }
4507 if (ufunc->uf_va_name != NULL)
4508 ++ectx.ec_stack.ga_len;
4509
4510 // Frame pointer points to just after arguments.
4511 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4512 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4513
4514 {
4515 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4516 + ufunc->uf_dfunc_idx;
4517 ufunc_T *base_ufunc = dfunc->df_ufunc;
4518
4519 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4520 // by copy_func().
4521 if (partial != NULL || base_ufunc->uf_partial != NULL)
4522 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004523 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4524 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004525 goto failed_early;
4526 if (partial != NULL)
4527 {
4528 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4529 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004530 if (current_ectx->ec_outer_ref != NULL
4531 && current_ectx->ec_outer_ref->or_outer != NULL)
4532 ectx.ec_outer_ref->or_outer =
4533 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004534 }
4535 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004536 {
4537 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4538 ++partial->pt_refcount;
4539 ectx.ec_outer_ref->or_partial = partial;
4540 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004541 }
4542 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004543 {
4544 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4545 ++base_ufunc->uf_partial->pt_refcount;
4546 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4547 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004548 }
4549 }
4550
4551 // dummy frame entries
4552 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4553 {
4554 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4555 ++ectx.ec_stack.ga_len;
4556 }
4557
4558 {
4559 // Reserve space for local variables and any closure reference count.
4560 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4561 + ufunc->uf_dfunc_idx;
4562
4563 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4564 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4565 ectx.ec_stack.ga_len += dfunc->df_varcount;
4566 if (dfunc->df_has_closure)
4567 {
4568 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4569 STACK_TV_VAR(idx)->vval.v_number = 0;
4570 ++ectx.ec_stack.ga_len;
4571 }
4572
4573 ectx.ec_instr = INSTRUCTIONS(dfunc);
4574 }
4575
4576 // Following errors are in the function, not the caller.
4577 // Commands behave like vim9script.
4578 estack_push_ufunc(ufunc, 1);
4579 current_sctx = ufunc->uf_script_ctx;
4580 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4581
4582 // Use a specific location for storing error messages to be converted to an
4583 // exception.
4584 saved_msg_list = msg_list;
4585 msg_list = &private_msg_list;
4586
4587 // Do turn errors into exceptions.
4588 suppress_errthrow = FALSE;
4589
4590 // Do not delete the function while executing it.
4591 ++ufunc->uf_calls;
4592
4593 // When ":silent!" was used before calling then we still abort the
4594 // function. If ":silent!" is used in the function then we don't.
4595 emsg_silent_def = emsg_silent;
4596 did_emsg_def = 0;
4597
4598 ectx.ec_where.wt_index = 0;
4599 ectx.ec_where.wt_variable = FALSE;
4600
4601 // Execute the instructions until done.
4602 ret = exec_instructions(&ectx);
4603 if (ret == OK)
4604 {
4605 // function finished, get result from the stack.
4606 if (ufunc->uf_ret_type == &t_void)
4607 {
4608 rettv->v_type = VAR_VOID;
4609 }
4610 else
4611 {
4612 tv = STACK_TV_BOT(-1);
4613 *rettv = *tv;
4614 tv->v_type = VAR_UNKNOWN;
4615 }
4616 }
4617
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004618 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004619 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4620 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004621
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004622 // Deal with any remaining closures, they may be in use somewhere.
4623 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004624 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004625 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004626 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4627 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004628
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004629 estack_pop();
4630 current_sctx = save_current_sctx;
4631
Bram Moolenaarc970e422021-03-17 15:03:04 +01004632 // TODO: when is it safe to delete the function if it is no longer used?
4633 --ufunc->uf_calls;
4634
Bram Moolenaar352134b2020-10-17 22:04:08 +02004635 if (*msg_list != NULL && saved_msg_list != NULL)
4636 {
4637 msglist_T **plist = saved_msg_list;
4638
4639 // Append entries from the current msg_list (uncaught exceptions) to
4640 // the saved msg_list.
4641 while (*plist != NULL)
4642 plist = &(*plist)->next;
4643
4644 *plist = *msg_list;
4645 }
4646 msg_list = saved_msg_list;
4647
Bram Moolenaar4c137212021-04-19 16:48:48 +02004648 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004649 {
4650 cmdmod.cmod_filter_regmatch.regprog = NULL;
4651 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004652 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004653 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004654 emsg_silent_def = save_emsg_silent_def;
4655 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004656
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004657failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004658 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4660 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004661 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004664 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004665 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004666 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004667 if (ectx.ec_outer_ref->or_outer_allocated)
4668 vim_free(ectx.ec_outer_ref->or_outer);
4669 partial_unref(ectx.ec_outer_ref->or_partial);
4670 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004671 }
4672
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004673 // Not sure if this is necessary.
4674 suppress_errthrow = save_suppress_errthrow;
4675
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004676 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004677 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004678 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004679 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 return ret;
4681}
4682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004684 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4685 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4686 * "pfx" is prefixed to every line.
4687 */
4688 static void
4689list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4690{
4691 int line_idx = 0;
4692 int prev_current = 0;
4693 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004694 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004695
4696 for (current = 0; current < instr_count; ++current)
4697 {
4698 isn_T *iptr = &instr[current];
4699 char *line;
4700
4701 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004702 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004703 while (line_idx < iptr->isn_lnum
4704 && line_idx < ufunc->uf_lines.ga_len)
4705 {
4706 if (current > prev_current)
4707 {
4708 msg_puts("\n\n");
4709 prev_current = current;
4710 }
4711 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4712 if (line != NULL)
4713 msg(line);
4714 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004715 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4716 {
4717 int first_def_arg = ufunc->uf_args.ga_len
4718 - ufunc->uf_def_args.ga_len;
4719
4720 if (def_arg_idx > 0)
4721 msg_puts("\n\n");
4722 msg_start();
4723 msg_puts(" ");
4724 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4725 first_def_arg + def_arg_idx]);
4726 msg_puts(" = ");
4727 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4728 msg_clr_eos();
4729 msg_end();
4730 }
4731 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004732
4733 switch (iptr->isn_type)
4734 {
4735 case ISN_EXEC:
4736 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4737 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004738 case ISN_EXEC_SPLIT:
4739 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4740 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004741 case ISN_LEGACY_EVAL:
4742 smsg("%s%4d EVAL legacy %s", pfx, current,
4743 iptr->isn_arg.string);
4744 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004745 case ISN_REDIRSTART:
4746 smsg("%s%4d REDIR", pfx, current);
4747 break;
4748 case ISN_REDIREND:
4749 smsg("%s%4d REDIR END%s", pfx, current,
4750 iptr->isn_arg.number ? " append" : "");
4751 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004752 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004753#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004754 smsg("%s%4d CEXPR pre %s", pfx, current,
4755 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004756#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004757 break;
4758 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004759#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004760 {
4761 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4762
4763 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4764 cexpr_get_auname(cer->cer_cmdidx),
4765 cer->cer_forceit ? "!" : "",
4766 cer->cer_cmdline);
4767 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004768#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004769 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004770 case ISN_INSTR:
4771 {
4772 smsg("%s%4d INSTR", pfx, current);
4773 list_instructions(" ", iptr->isn_arg.instr,
4774 INT_MAX, NULL);
4775 msg(" -------------");
4776 }
4777 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004778 case ISN_SUBSTITUTE:
4779 {
4780 subs_T *subs = &iptr->isn_arg.subs;
4781
4782 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4783 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4784 msg(" -------------");
4785 }
4786 break;
4787 case ISN_EXECCONCAT:
4788 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4789 (varnumber_T)iptr->isn_arg.number);
4790 break;
4791 case ISN_ECHO:
4792 {
4793 echo_T *echo = &iptr->isn_arg.echo;
4794
4795 smsg("%s%4d %s %d", pfx, current,
4796 echo->echo_with_white ? "ECHO" : "ECHON",
4797 echo->echo_count);
4798 }
4799 break;
4800 case ISN_EXECUTE:
4801 smsg("%s%4d EXECUTE %lld", pfx, current,
4802 (varnumber_T)(iptr->isn_arg.number));
4803 break;
4804 case ISN_ECHOMSG:
4805 smsg("%s%4d ECHOMSG %lld", pfx, current,
4806 (varnumber_T)(iptr->isn_arg.number));
4807 break;
4808 case ISN_ECHOERR:
4809 smsg("%s%4d ECHOERR %lld", pfx, current,
4810 (varnumber_T)(iptr->isn_arg.number));
4811 break;
4812 case ISN_LOAD:
4813 {
4814 if (iptr->isn_arg.number < 0)
4815 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4816 (varnumber_T)(iptr->isn_arg.number
4817 + STACK_FRAME_SIZE));
4818 else
4819 smsg("%s%4d LOAD $%lld", pfx, current,
4820 (varnumber_T)(iptr->isn_arg.number));
4821 }
4822 break;
4823 case ISN_LOADOUTER:
4824 {
4825 if (iptr->isn_arg.number < 0)
4826 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4827 iptr->isn_arg.outer.outer_depth,
4828 iptr->isn_arg.outer.outer_idx
4829 + STACK_FRAME_SIZE);
4830 else
4831 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4832 iptr->isn_arg.outer.outer_depth,
4833 iptr->isn_arg.outer.outer_idx);
4834 }
4835 break;
4836 case ISN_LOADV:
4837 smsg("%s%4d LOADV v:%s", pfx, current,
4838 get_vim_var_name(iptr->isn_arg.number));
4839 break;
4840 case ISN_LOADSCRIPT:
4841 {
4842 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4843 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4844 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4845 + sref->sref_idx;
4846
4847 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4848 sv->sv_name,
4849 sref->sref_idx,
4850 si->sn_name);
4851 }
4852 break;
4853 case ISN_LOADS:
4854 {
4855 scriptitem_T *si = SCRIPT_ITEM(
4856 iptr->isn_arg.loadstore.ls_sid);
4857
4858 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4859 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4860 }
4861 break;
4862 case ISN_LOADAUTO:
4863 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4864 break;
4865 case ISN_LOADG:
4866 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4867 break;
4868 case ISN_LOADB:
4869 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4870 break;
4871 case ISN_LOADW:
4872 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4873 break;
4874 case ISN_LOADT:
4875 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4876 break;
4877 case ISN_LOADGDICT:
4878 smsg("%s%4d LOAD g:", pfx, current);
4879 break;
4880 case ISN_LOADBDICT:
4881 smsg("%s%4d LOAD b:", pfx, current);
4882 break;
4883 case ISN_LOADWDICT:
4884 smsg("%s%4d LOAD w:", pfx, current);
4885 break;
4886 case ISN_LOADTDICT:
4887 smsg("%s%4d LOAD t:", pfx, current);
4888 break;
4889 case ISN_LOADOPT:
4890 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4891 break;
4892 case ISN_LOADENV:
4893 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4894 break;
4895 case ISN_LOADREG:
4896 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4897 break;
4898
4899 case ISN_STORE:
4900 if (iptr->isn_arg.number < 0)
4901 smsg("%s%4d STORE arg[%lld]", pfx, current,
4902 iptr->isn_arg.number + STACK_FRAME_SIZE);
4903 else
4904 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4905 break;
4906 case ISN_STOREOUTER:
4907 {
4908 if (iptr->isn_arg.number < 0)
4909 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4910 iptr->isn_arg.outer.outer_depth,
4911 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4912 else
4913 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4914 iptr->isn_arg.outer.outer_depth,
4915 iptr->isn_arg.outer.outer_idx);
4916 }
4917 break;
4918 case ISN_STOREV:
4919 smsg("%s%4d STOREV v:%s", pfx, current,
4920 get_vim_var_name(iptr->isn_arg.number));
4921 break;
4922 case ISN_STOREAUTO:
4923 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4924 break;
4925 case ISN_STOREG:
4926 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4927 break;
4928 case ISN_STOREB:
4929 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4930 break;
4931 case ISN_STOREW:
4932 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4933 break;
4934 case ISN_STORET:
4935 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4936 break;
4937 case ISN_STORES:
4938 {
4939 scriptitem_T *si = SCRIPT_ITEM(
4940 iptr->isn_arg.loadstore.ls_sid);
4941
4942 smsg("%s%4d STORES %s in %s", pfx, current,
4943 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4944 }
4945 break;
4946 case ISN_STORESCRIPT:
4947 {
4948 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4949 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4950 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4951 + sref->sref_idx;
4952
4953 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4954 sv->sv_name,
4955 sref->sref_idx,
4956 si->sn_name);
4957 }
4958 break;
4959 case ISN_STOREOPT:
4960 smsg("%s%4d STOREOPT &%s", pfx, current,
4961 iptr->isn_arg.storeopt.so_name);
4962 break;
4963 case ISN_STOREENV:
4964 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4965 break;
4966 case ISN_STOREREG:
4967 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4968 break;
4969 case ISN_STORENR:
4970 smsg("%s%4d STORE %lld in $%d", pfx, current,
4971 iptr->isn_arg.storenr.stnr_val,
4972 iptr->isn_arg.storenr.stnr_idx);
4973 break;
4974
4975 case ISN_STOREINDEX:
4976 smsg("%s%4d STOREINDEX %s", pfx, current,
4977 vartype_name(iptr->isn_arg.vartype));
4978 break;
4979
4980 case ISN_STORERANGE:
4981 smsg("%s%4d STORERANGE", pfx, current);
4982 break;
4983
4984 // constants
4985 case ISN_PUSHNR:
4986 smsg("%s%4d PUSHNR %lld", pfx, current,
4987 (varnumber_T)(iptr->isn_arg.number));
4988 break;
4989 case ISN_PUSHBOOL:
4990 case ISN_PUSHSPEC:
4991 smsg("%s%4d PUSH %s", pfx, current,
4992 get_var_special_name(iptr->isn_arg.number));
4993 break;
4994 case ISN_PUSHF:
4995#ifdef FEAT_FLOAT
4996 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4997#endif
4998 break;
4999 case ISN_PUSHS:
5000 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5001 break;
5002 case ISN_PUSHBLOB:
5003 {
5004 char_u *r;
5005 char_u numbuf[NUMBUFLEN];
5006 char_u *tofree;
5007
5008 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5009 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5010 vim_free(tofree);
5011 }
5012 break;
5013 case ISN_PUSHFUNC:
5014 {
5015 char *name = (char *)iptr->isn_arg.string;
5016
5017 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5018 name == NULL ? "[none]" : name);
5019 }
5020 break;
5021 case ISN_PUSHCHANNEL:
5022#ifdef FEAT_JOB_CHANNEL
5023 {
5024 channel_T *channel = iptr->isn_arg.channel;
5025
5026 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5027 channel == NULL ? 0 : channel->ch_id);
5028 }
5029#endif
5030 break;
5031 case ISN_PUSHJOB:
5032#ifdef FEAT_JOB_CHANNEL
5033 {
5034 typval_T tv;
5035 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005036 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005037
5038 tv.v_type = VAR_JOB;
5039 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005040 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005041 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5042 }
5043#endif
5044 break;
5045 case ISN_PUSHEXC:
5046 smsg("%s%4d PUSH v:exception", pfx, current);
5047 break;
5048 case ISN_UNLET:
5049 smsg("%s%4d UNLET%s %s", pfx, current,
5050 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5051 iptr->isn_arg.unlet.ul_name);
5052 break;
5053 case ISN_UNLETENV:
5054 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5055 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5056 iptr->isn_arg.unlet.ul_name);
5057 break;
5058 case ISN_UNLETINDEX:
5059 smsg("%s%4d UNLETINDEX", pfx, current);
5060 break;
5061 case ISN_UNLETRANGE:
5062 smsg("%s%4d UNLETRANGE", pfx, current);
5063 break;
5064 case ISN_LOCKCONST:
5065 smsg("%s%4d LOCKCONST", pfx, current);
5066 break;
5067 case ISN_NEWLIST:
5068 smsg("%s%4d NEWLIST size %lld", pfx, current,
5069 (varnumber_T)(iptr->isn_arg.number));
5070 break;
5071 case ISN_NEWDICT:
5072 smsg("%s%4d NEWDICT size %lld", pfx, current,
5073 (varnumber_T)(iptr->isn_arg.number));
5074 break;
5075
5076 // function call
5077 case ISN_BCALL:
5078 {
5079 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5080
5081 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5082 internal_func_name(cbfunc->cbf_idx),
5083 cbfunc->cbf_argcount);
5084 }
5085 break;
5086 case ISN_DCALL:
5087 {
5088 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5089 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5090 + cdfunc->cdf_idx;
5091
5092 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5093 df->df_ufunc->uf_name_exp != NULL
5094 ? df->df_ufunc->uf_name_exp
5095 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5096 }
5097 break;
5098 case ISN_UCALL:
5099 {
5100 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5101
5102 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5103 cufunc->cuf_name, cufunc->cuf_argcount);
5104 }
5105 break;
5106 case ISN_PCALL:
5107 {
5108 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5109
5110 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5111 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5112 }
5113 break;
5114 case ISN_PCALL_END:
5115 smsg("%s%4d PCALL end", pfx, current);
5116 break;
5117 case ISN_RETURN:
5118 smsg("%s%4d RETURN", pfx, current);
5119 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005120 case ISN_RETURN_VOID:
5121 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005122 break;
5123 case ISN_FUNCREF:
5124 {
5125 funcref_T *funcref = &iptr->isn_arg.funcref;
5126 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5127 + funcref->fr_func;
5128
5129 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5130 }
5131 break;
5132
5133 case ISN_NEWFUNC:
5134 {
5135 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5136
5137 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5138 newfunc->nf_lambda, newfunc->nf_global);
5139 }
5140 break;
5141
5142 case ISN_DEF:
5143 {
5144 char_u *name = iptr->isn_arg.string;
5145
5146 smsg("%s%4d DEF %s", pfx, current,
5147 name == NULL ? (char_u *)"" : name);
5148 }
5149 break;
5150
5151 case ISN_JUMP:
5152 {
5153 char *when = "?";
5154
5155 switch (iptr->isn_arg.jump.jump_when)
5156 {
5157 case JUMP_ALWAYS:
5158 when = "JUMP";
5159 break;
5160 case JUMP_AND_KEEP_IF_TRUE:
5161 when = "JUMP_AND_KEEP_IF_TRUE";
5162 break;
5163 case JUMP_IF_FALSE:
5164 when = "JUMP_IF_FALSE";
5165 break;
5166 case JUMP_AND_KEEP_IF_FALSE:
5167 when = "JUMP_AND_KEEP_IF_FALSE";
5168 break;
5169 case JUMP_IF_COND_FALSE:
5170 when = "JUMP_IF_COND_FALSE";
5171 break;
5172 case JUMP_IF_COND_TRUE:
5173 when = "JUMP_IF_COND_TRUE";
5174 break;
5175 }
5176 smsg("%s%4d %s -> %d", pfx, current, when,
5177 iptr->isn_arg.jump.jump_where);
5178 }
5179 break;
5180
5181 case ISN_JUMP_IF_ARG_SET:
5182 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5183 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5184 iptr->isn_arg.jump.jump_where);
5185 break;
5186
5187 case ISN_FOR:
5188 {
5189 forloop_T *forloop = &iptr->isn_arg.forloop;
5190
5191 smsg("%s%4d FOR $%d -> %d", pfx, current,
5192 forloop->for_idx, forloop->for_end);
5193 }
5194 break;
5195
5196 case ISN_TRY:
5197 {
5198 try_T *try = &iptr->isn_arg.try;
5199
5200 if (try->try_ref->try_finally == 0)
5201 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5202 pfx, current,
5203 try->try_ref->try_catch,
5204 try->try_ref->try_endtry);
5205 else
5206 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5207 pfx, current,
5208 try->try_ref->try_catch,
5209 try->try_ref->try_finally,
5210 try->try_ref->try_endtry);
5211 }
5212 break;
5213 case ISN_CATCH:
5214 // TODO
5215 smsg("%s%4d CATCH", pfx, current);
5216 break;
5217 case ISN_TRYCONT:
5218 {
5219 trycont_T *trycont = &iptr->isn_arg.trycont;
5220
5221 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5222 trycont->tct_levels,
5223 trycont->tct_levels == 1 ? "" : "s",
5224 trycont->tct_where);
5225 }
5226 break;
5227 case ISN_FINALLY:
5228 smsg("%s%4d FINALLY", pfx, current);
5229 break;
5230 case ISN_ENDTRY:
5231 smsg("%s%4d ENDTRY", pfx, current);
5232 break;
5233 case ISN_THROW:
5234 smsg("%s%4d THROW", pfx, current);
5235 break;
5236
5237 // expression operations on number
5238 case ISN_OPNR:
5239 case ISN_OPFLOAT:
5240 case ISN_OPANY:
5241 {
5242 char *what;
5243 char *ins;
5244
5245 switch (iptr->isn_arg.op.op_type)
5246 {
5247 case EXPR_MULT: what = "*"; break;
5248 case EXPR_DIV: what = "/"; break;
5249 case EXPR_REM: what = "%"; break;
5250 case EXPR_SUB: what = "-"; break;
5251 case EXPR_ADD: what = "+"; break;
5252 default: what = "???"; break;
5253 }
5254 switch (iptr->isn_type)
5255 {
5256 case ISN_OPNR: ins = "OPNR"; break;
5257 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5258 case ISN_OPANY: ins = "OPANY"; break;
5259 default: ins = "???"; break;
5260 }
5261 smsg("%s%4d %s %s", pfx, current, ins, what);
5262 }
5263 break;
5264
5265 case ISN_COMPAREBOOL:
5266 case ISN_COMPARESPECIAL:
5267 case ISN_COMPARENR:
5268 case ISN_COMPAREFLOAT:
5269 case ISN_COMPARESTRING:
5270 case ISN_COMPAREBLOB:
5271 case ISN_COMPARELIST:
5272 case ISN_COMPAREDICT:
5273 case ISN_COMPAREFUNC:
5274 case ISN_COMPAREANY:
5275 {
5276 char *p;
5277 char buf[10];
5278 char *type;
5279
5280 switch (iptr->isn_arg.op.op_type)
5281 {
5282 case EXPR_EQUAL: p = "=="; break;
5283 case EXPR_NEQUAL: p = "!="; break;
5284 case EXPR_GREATER: p = ">"; break;
5285 case EXPR_GEQUAL: p = ">="; break;
5286 case EXPR_SMALLER: p = "<"; break;
5287 case EXPR_SEQUAL: p = "<="; break;
5288 case EXPR_MATCH: p = "=~"; break;
5289 case EXPR_IS: p = "is"; break;
5290 case EXPR_ISNOT: p = "isnot"; break;
5291 case EXPR_NOMATCH: p = "!~"; break;
5292 default: p = "???"; break;
5293 }
5294 STRCPY(buf, p);
5295 if (iptr->isn_arg.op.op_ic == TRUE)
5296 strcat(buf, "?");
5297 switch(iptr->isn_type)
5298 {
5299 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5300 case ISN_COMPARESPECIAL:
5301 type = "COMPARESPECIAL"; break;
5302 case ISN_COMPARENR: type = "COMPARENR"; break;
5303 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5304 case ISN_COMPARESTRING:
5305 type = "COMPARESTRING"; break;
5306 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5307 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5308 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5309 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5310 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5311 default: type = "???"; break;
5312 }
5313
5314 smsg("%s%4d %s %s", pfx, current, type, buf);
5315 }
5316 break;
5317
5318 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5319 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5320
5321 // expression operations
5322 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5323 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5324 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5325 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5326 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5327 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5328 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5329 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5330 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5331 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5332 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5333 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5334 pfx, current, iptr->isn_arg.number); break;
5335 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5336 pfx, current, iptr->isn_arg.number); break;
5337 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5338 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5339 iptr->isn_arg.string); break;
5340 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5341
5342 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5343 case ISN_CHECKTYPE:
5344 {
5345 checktype_T *ct = &iptr->isn_arg.type;
5346 char *tofree;
5347
5348 if (ct->ct_arg_idx == 0)
5349 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5350 type_name(ct->ct_type, &tofree),
5351 (int)ct->ct_off);
5352 else
5353 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5354 type_name(ct->ct_type, &tofree),
5355 (int)ct->ct_off,
5356 (int)ct->ct_arg_idx);
5357 vim_free(tofree);
5358 break;
5359 }
5360 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5361 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5362 iptr->isn_arg.checklen.cl_min_len);
5363 break;
5364 case ISN_SETTYPE:
5365 {
5366 char *tofree;
5367
5368 smsg("%s%4d SETTYPE %s", pfx, current,
5369 type_name(iptr->isn_arg.type.ct_type, &tofree));
5370 vim_free(tofree);
5371 break;
5372 }
5373 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005374 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5375 smsg("%s%4d INVERT %d (!val)", pfx, current,
5376 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005377 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005378 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5379 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005380 break;
5381 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005382 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005383 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005384 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5385 pfx, current,
5386 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005387 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005388 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5389 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005390 break;
5391 case ISN_PUT:
5392 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5393 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005394 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005395 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5396 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005397 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005398 else
5399 smsg("%s%4d PUT %c %ld", pfx, current,
5400 iptr->isn_arg.put.put_regname,
5401 (long)iptr->isn_arg.put.put_lnum);
5402 break;
5403
5404 // TODO: summarize modifiers
5405 case ISN_CMDMOD:
5406 {
5407 char_u *buf;
5408 size_t len = produce_cmdmods(
5409 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5410
5411 buf = alloc(len + 1);
5412 if (buf != NULL)
5413 {
5414 (void)produce_cmdmods(
5415 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5416 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5417 vim_free(buf);
5418 }
5419 break;
5420 }
5421 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5422
5423 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005424 smsg("%s%4d PROFILE START line %d", pfx, current,
5425 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005426 break;
5427
5428 case ISN_PROF_END:
5429 smsg("%s%4d PROFILE END", pfx, current);
5430 break;
5431
Bram Moolenaare99d4222021-06-13 14:01:26 +02005432 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005433 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5434 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005435 break;
5436
Bram Moolenaar4c137212021-04-19 16:48:48 +02005437 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5438 iptr->isn_arg.unpack.unp_count,
5439 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5440 break;
5441 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5442 iptr->isn_arg.shuffle.shfl_item,
5443 iptr->isn_arg.shuffle.shfl_up);
5444 break;
5445 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5446
5447 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5448 return;
5449 }
5450
5451 out_flush(); // output one line at a time
5452 ui_breakcheck();
5453 if (got_int)
5454 break;
5455 }
5456}
5457
5458/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005459 * Handle command line completion for the :disassemble command.
5460 */
5461 void
5462set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5463{
5464 char_u *p;
5465
5466 // Default: expand user functions, "debug" and "profile"
5467 xp->xp_context = EXPAND_DISASSEMBLE;
5468 xp->xp_pattern = arg;
5469
5470 // first argument already typed: only user function names
5471 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5472 {
5473 xp->xp_context = EXPAND_USER_FUNC;
5474 xp->xp_pattern = skipwhite(p);
5475 }
5476}
5477
5478/*
5479 * Function given to ExpandGeneric() to obtain the list of :disassemble
5480 * arguments.
5481 */
5482 char_u *
5483get_disassemble_argument(expand_T *xp, int idx)
5484{
5485 if (idx == 0)
5486 return (char_u *)"debug";
5487 if (idx == 1)
5488 return (char_u *)"profile";
5489 return get_user_func_name(xp, idx - 2);
5490}
5491
5492/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005493 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005494 * We don't really need this at runtime, but we do have tests that require it,
5495 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496 */
5497 void
5498ex_disassemble(exarg_T *eap)
5499{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005500 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005501 char_u *fname;
5502 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503 dfunc_T *dfunc;
5504 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005505 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005506 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005507 compiletype_T compile_type = CT_NONE;
5508
5509 if (STRNCMP(arg, "profile", 7) == 0)
5510 {
5511 compile_type = CT_PROFILE;
5512 arg = skipwhite(arg + 7);
5513 }
5514 else if (STRNCMP(arg, "debug", 5) == 0)
5515 {
5516 compile_type = CT_DEBUG;
5517 arg = skipwhite(arg + 5);
5518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005520 if (STRNCMP(arg, "<lambda>", 8) == 0)
5521 {
5522 arg += 8;
5523 (void)getdigits(&arg);
5524 fname = vim_strnsave(eap->arg, arg - eap->arg);
5525 }
5526 else
5527 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005528 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005529 if (fname == NULL)
5530 {
5531 semsg(_(e_invarg2), eap->arg);
5532 return;
5533 }
5534
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005535 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005536 if (ufunc == NULL)
5537 {
5538 char_u *p = untrans_function_name(fname);
5539
5540 if (p != NULL)
5541 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005542 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005543 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005544 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545 if (ufunc == NULL)
5546 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005547 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 return;
5549 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005550 if (func_needs_compiling(ufunc, compile_type)
5551 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005552 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005553 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005555 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 return;
5557 }
5558 if (ufunc->uf_name_exp != NULL)
5559 msg((char *)ufunc->uf_name_exp);
5560 else
5561 msg((char *)ufunc->uf_name);
5562
5563 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005564 switch (compile_type)
5565 {
5566 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005567#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005568 instr = dfunc->df_instr_prof;
5569 instr_count = dfunc->df_instr_prof_count;
5570 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005571#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005572 // FALLTHROUGH
5573 case CT_NONE:
5574 instr = dfunc->df_instr;
5575 instr_count = dfunc->df_instr_count;
5576 break;
5577 case CT_DEBUG:
5578 instr = dfunc->df_instr_debug;
5579 instr_count = dfunc->df_instr_debug_count;
5580 break;
5581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005582
Bram Moolenaar4c137212021-04-19 16:48:48 +02005583 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584}
5585
5586/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005587 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588 * list, etc. Mostly like what JavaScript does, except that empty list and
5589 * empty dictionary are FALSE.
5590 */
5591 int
5592tv2bool(typval_T *tv)
5593{
5594 switch (tv->v_type)
5595 {
5596 case VAR_NUMBER:
5597 return tv->vval.v_number != 0;
5598 case VAR_FLOAT:
5599#ifdef FEAT_FLOAT
5600 return tv->vval.v_float != 0.0;
5601#else
5602 break;
5603#endif
5604 case VAR_PARTIAL:
5605 return tv->vval.v_partial != NULL;
5606 case VAR_FUNC:
5607 case VAR_STRING:
5608 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5609 case VAR_LIST:
5610 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5611 case VAR_DICT:
5612 return tv->vval.v_dict != NULL
5613 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5614 case VAR_BOOL:
5615 case VAR_SPECIAL:
5616 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5617 case VAR_JOB:
5618#ifdef FEAT_JOB_CHANNEL
5619 return tv->vval.v_job != NULL;
5620#else
5621 break;
5622#endif
5623 case VAR_CHANNEL:
5624#ifdef FEAT_JOB_CHANNEL
5625 return tv->vval.v_channel != NULL;
5626#else
5627 break;
5628#endif
5629 case VAR_BLOB:
5630 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5631 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005632 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005634 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005635 break;
5636 }
5637 return FALSE;
5638}
5639
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005640 void
5641emsg_using_string_as(typval_T *tv, int as_number)
5642{
5643 semsg(_(as_number ? e_using_string_as_number_str
5644 : e_using_string_as_bool_str),
5645 tv->vval.v_string == NULL
5646 ? (char_u *)"" : tv->vval.v_string);
5647}
5648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005649/*
5650 * If "tv" is a string give an error and return FAIL.
5651 */
5652 int
5653check_not_string(typval_T *tv)
5654{
5655 if (tv->v_type == VAR_STRING)
5656 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005657 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658 clear_tv(tv);
5659 return FAIL;
5660 }
5661 return OK;
5662}
5663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005664
5665#endif // FEAT_EVAL