blob: fabce4dbe7c86bb2d29619f6daa731d8580a244e [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 Moolenaarb283a8a2020-02-02 22:24:04 +01002185 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002186 tv_get_string(tv), -1, FALSE);
2187 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002188 }
2189 break;
2190
2191 // store v: variable
2192 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002193 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002194 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2195 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002196 // should not happen, type is checked when compiling
2197 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002198 break;
2199
Bram Moolenaard3aac292020-04-19 14:32:17 +02002200 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002202 case ISN_STOREB:
2203 case ISN_STOREW:
2204 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002206 dictitem_T *di;
2207 hashtab_T *ht;
2208 char_u *name = iptr->isn_arg.string + 2;
2209
Bram Moolenaard3aac292020-04-19 14:32:17 +02002210 switch (iptr->isn_type)
2211 {
2212 case ISN_STOREG:
2213 ht = get_globvar_ht();
2214 break;
2215 case ISN_STOREB:
2216 ht = &curbuf->b_vars->dv_hashtab;
2217 break;
2218 case ISN_STOREW:
2219 ht = &curwin->w_vars->dv_hashtab;
2220 break;
2221 case ISN_STORET:
2222 ht = &curtab->tp_vars->dv_hashtab;
2223 break;
2224 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002225 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227
Bram Moolenaar4c137212021-04-19 16:48:48 +02002228 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002229 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002231 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 else
2233 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002234 SOURCING_LNUM = iptr->isn_lnum;
2235 if (var_check_permission(di, name) == FAIL)
2236 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 clear_tv(&di->di_tv);
2238 di->di_tv = *STACK_TV_BOT(0);
2239 }
2240 }
2241 break;
2242
Bram Moolenaar03290b82020-12-19 16:30:44 +01002243 // store an autoload variable
2244 case ISN_STOREAUTO:
2245 SOURCING_LNUM = iptr->isn_lnum;
2246 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2247 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002248 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002249 break;
2250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 // store number in local variable
2252 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002253 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 clear_tv(tv);
2255 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002256 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 break;
2258
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002259 // store value in list or dict variable
2260 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002261 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002262 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002263 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002264 typval_T *tv_dest = STACK_TV_BOT(-1);
2265 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002266
Bram Moolenaar752fc692021-01-04 21:57:11 +01002267 // Stack contains:
2268 // -3 value to be stored
2269 // -2 index
2270 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002271 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002272 SOURCING_LNUM = iptr->isn_lnum;
2273 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002274 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002275 dest_type = tv_dest->v_type;
2276 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002277 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002278 else if (dest_type == VAR_LIST
2279 && tv_idx->v_type != VAR_NUMBER)
2280 {
2281 emsg(_(e_number_exp));
2282 status = FAIL;
2283 }
2284 }
2285 else if (dest_type != tv_dest->v_type)
2286 {
2287 // just in case, should be OK
2288 semsg(_(e_expected_str_but_got_str),
2289 vartype_name(dest_type),
2290 vartype_name(tv_dest->v_type));
2291 status = FAIL;
2292 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002293
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002294 if (status == OK && dest_type == VAR_LIST)
2295 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002296 long lidx = (long)tv_idx->vval.v_number;
2297 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002298
2299 if (list == NULL)
2300 {
2301 emsg(_(e_list_not_set));
2302 goto on_error;
2303 }
2304 if (lidx < 0 && list->lv_len + lidx >= 0)
2305 // negative index is relative to the end
2306 lidx = list->lv_len + lidx;
2307 if (lidx < 0 || lidx > list->lv_len)
2308 {
2309 semsg(_(e_listidx), lidx);
2310 goto on_error;
2311 }
2312 if (lidx < list->lv_len)
2313 {
2314 listitem_T *li = list_find(list, lidx);
2315
2316 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002317 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002318 goto on_error;
2319 // overwrite existing list item
2320 clear_tv(&li->li_tv);
2321 li->li_tv = *tv;
2322 }
2323 else
2324 {
2325 if (error_if_locked(list->lv_lock,
2326 e_cannot_change_list))
2327 goto on_error;
2328 // append to list, only fails when out of memory
2329 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002330 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002331 clear_tv(tv);
2332 }
2333 }
2334 else if (status == OK && dest_type == VAR_DICT)
2335 {
2336 char_u *key = tv_idx->vval.v_string;
2337 dict_T *dict = tv_dest->vval.v_dict;
2338 dictitem_T *di;
2339
2340 SOURCING_LNUM = iptr->isn_lnum;
2341 if (dict == NULL)
2342 {
2343 emsg(_(e_dictionary_not_set));
2344 goto on_error;
2345 }
2346 if (key == NULL)
2347 key = (char_u *)"";
2348 di = dict_find(dict, key, -1);
2349 if (di != NULL)
2350 {
2351 if (error_if_locked(di->di_tv.v_lock,
2352 e_cannot_change_dict_item))
2353 goto on_error;
2354 // overwrite existing value
2355 clear_tv(&di->di_tv);
2356 di->di_tv = *tv;
2357 }
2358 else
2359 {
2360 if (error_if_locked(dict->dv_lock,
2361 e_cannot_change_dict))
2362 goto on_error;
2363 // add to dict, only fails when out of memory
2364 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002365 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002366 clear_tv(tv);
2367 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002368 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002369 else if (status == OK && dest_type == VAR_BLOB)
2370 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002371 long lidx = (long)tv_idx->vval.v_number;
2372 blob_T *blob = tv_dest->vval.v_blob;
2373 varnumber_T nr;
2374 int error = FALSE;
2375 int len;
2376
2377 if (blob == NULL)
2378 {
2379 emsg(_(e_blob_not_set));
2380 goto on_error;
2381 }
2382 len = blob_len(blob);
2383 if (lidx < 0 && len + lidx >= 0)
2384 // negative index is relative to the end
2385 lidx = len + lidx;
2386
2387 // Can add one byte at the end.
2388 if (lidx < 0 || lidx > len)
2389 {
2390 semsg(_(e_blobidx), lidx);
2391 goto on_error;
2392 }
2393 if (value_check_lock(blob->bv_lock,
2394 (char_u *)"blob", FALSE))
2395 goto on_error;
2396 nr = tv_get_number_chk(tv, &error);
2397 if (error)
2398 goto on_error;
2399 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002400 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002401 else
2402 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002403 status = FAIL;
2404 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002405 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002406
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002407 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002408 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002409 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002410 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002411 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002412 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002413 goto on_error;
2414 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002415 }
2416 break;
2417
Bram Moolenaar68452172021-04-12 21:21:02 +02002418 // store value in blob range
2419 case ISN_STORERANGE:
2420 {
2421 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2422 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2423 typval_T *tv_dest = STACK_TV_BOT(-1);
2424 int status = OK;
2425
2426 // Stack contains:
2427 // -4 value to be stored
2428 // -3 first index or "none"
2429 // -2 second index or "none"
2430 // -1 destination blob
2431 tv = STACK_TV_BOT(-4);
2432 if (tv_dest->v_type != VAR_BLOB)
2433 {
2434 status = FAIL;
2435 emsg(_(e_blob_required));
2436 }
2437 else
2438 {
2439 varnumber_T n1;
2440 varnumber_T n2;
2441 int error = FALSE;
2442
2443 n1 = tv_get_number_chk(tv_idx1, &error);
2444 if (error)
2445 status = FAIL;
2446 else
2447 {
2448 if (tv_idx2->v_type == VAR_SPECIAL
2449 && tv_idx2->vval.v_number == VVAL_NONE)
2450 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2451 else
2452 n2 = tv_get_number_chk(tv_idx2, &error);
2453 if (error)
2454 status = FAIL;
2455 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002456 {
2457 long bloblen = blob_len(tv_dest->vval.v_blob);
2458
2459 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002460 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002461 || check_blob_range(bloblen,
2462 n1, n2, FALSE) == FAIL)
2463 status = FAIL;
2464 else
2465 status = blob_set_range(
2466 tv_dest->vval.v_blob, n1, n2, tv);
2467 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002468 }
2469 }
2470
2471 clear_tv(tv_idx1);
2472 clear_tv(tv_idx2);
2473 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002474 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002475 clear_tv(tv);
2476
2477 if (status == FAIL)
2478 goto on_error;
2479 }
2480 break;
2481
Bram Moolenaar0186e582021-01-10 18:33:11 +01002482 // load or store variable or argument from outer scope
2483 case ISN_LOADOUTER:
2484 case ISN_STOREOUTER:
2485 {
2486 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002487 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2488 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002489
2490 while (depth > 1 && outer != NULL)
2491 {
2492 outer = outer->out_up;
2493 --depth;
2494 }
2495 if (outer == NULL)
2496 {
2497 SOURCING_LNUM = iptr->isn_lnum;
2498 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002499 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002500 }
2501 tv = ((typval_T *)outer->out_stack->ga_data)
2502 + outer->out_frame_idx + STACK_FRAME_SIZE
2503 + iptr->isn_arg.outer.outer_idx;
2504 if (iptr->isn_type == ISN_LOADOUTER)
2505 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002506 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002507 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002508 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002509 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002510 }
2511 else
2512 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002513 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002514 clear_tv(tv);
2515 *tv = *STACK_TV_BOT(0);
2516 }
2517 }
2518 break;
2519
Bram Moolenaar752fc692021-01-04 21:57:11 +01002520 // unlet item in list or dict variable
2521 case ISN_UNLETINDEX:
2522 {
2523 typval_T *tv_idx = STACK_TV_BOT(-2);
2524 typval_T *tv_dest = STACK_TV_BOT(-1);
2525 int status = OK;
2526
2527 // Stack contains:
2528 // -2 index
2529 // -1 dict or list
2530 if (tv_dest->v_type == VAR_DICT)
2531 {
2532 // unlet a dict item, index must be a string
2533 if (tv_idx->v_type != VAR_STRING)
2534 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002535 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002536 semsg(_(e_expected_str_but_got_str),
2537 vartype_name(VAR_STRING),
2538 vartype_name(tv_idx->v_type));
2539 status = FAIL;
2540 }
2541 else
2542 {
2543 dict_T *d = tv_dest->vval.v_dict;
2544 char_u *key = tv_idx->vval.v_string;
2545 dictitem_T *di = NULL;
2546
2547 if (key == NULL)
2548 key = (char_u *)"";
2549 if (d != NULL)
2550 di = dict_find(d, key, (int)STRLEN(key));
2551 if (di == NULL)
2552 {
2553 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002554 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002555 semsg(_(e_dictkey), key);
2556 status = FAIL;
2557 }
2558 else
2559 {
2560 // TODO: check for dict or item locked
2561 dictitem_remove(d, di);
2562 }
2563 }
2564 }
2565 else if (tv_dest->v_type == VAR_LIST)
2566 {
2567 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002568 SOURCING_LNUM = iptr->isn_lnum;
2569 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002570 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002571 status = FAIL;
2572 }
2573 else
2574 {
2575 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002576 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002577 listitem_T *li = NULL;
2578
2579 li = list_find(l, n);
2580 if (li == NULL)
2581 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002582 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002583 semsg(_(e_listidx), n);
2584 status = FAIL;
2585 }
2586 else
2587 // TODO: check for list or item locked
2588 listitem_remove(l, li);
2589 }
2590 }
2591 else
2592 {
2593 status = FAIL;
2594 semsg(_(e_cannot_index_str),
2595 vartype_name(tv_dest->v_type));
2596 }
2597
2598 clear_tv(tv_idx);
2599 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002600 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002601 if (status == FAIL)
2602 goto on_error;
2603 }
2604 break;
2605
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002606 // unlet range of items in list variable
2607 case ISN_UNLETRANGE:
2608 {
2609 // Stack contains:
2610 // -3 index1
2611 // -2 index2
2612 // -1 dict or list
2613 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2614 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2615 typval_T *tv_dest = STACK_TV_BOT(-1);
2616 int status = OK;
2617
2618 if (tv_dest->v_type == VAR_LIST)
2619 {
2620 // indexes must be a number
2621 SOURCING_LNUM = iptr->isn_lnum;
2622 if (check_for_number(tv_idx1) == FAIL
2623 || check_for_number(tv_idx2) == FAIL)
2624 {
2625 status = FAIL;
2626 }
2627 else
2628 {
2629 list_T *l = tv_dest->vval.v_list;
2630 long n1 = (long)tv_idx1->vval.v_number;
2631 long n2 = (long)tv_idx2->vval.v_number;
2632 listitem_T *li;
2633
2634 li = list_find_index(l, &n1);
2635 if (li == NULL
2636 || list_unlet_range(l, li, NULL, n1,
2637 TRUE, n2) == FAIL)
2638 status = FAIL;
2639 }
2640 }
2641 else
2642 {
2643 status = FAIL;
2644 SOURCING_LNUM = iptr->isn_lnum;
2645 semsg(_(e_cannot_index_str),
2646 vartype_name(tv_dest->v_type));
2647 }
2648
2649 clear_tv(tv_idx1);
2650 clear_tv(tv_idx2);
2651 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002652 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002653 if (status == FAIL)
2654 goto on_error;
2655 }
2656 break;
2657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 // push constant
2659 case ISN_PUSHNR:
2660 case ISN_PUSHBOOL:
2661 case ISN_PUSHSPEC:
2662 case ISN_PUSHF:
2663 case ISN_PUSHS:
2664 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002665 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002666 case ISN_PUSHCHANNEL:
2667 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002668 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002669 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002671 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002672 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 switch (iptr->isn_type)
2674 {
2675 case ISN_PUSHNR:
2676 tv->v_type = VAR_NUMBER;
2677 tv->vval.v_number = iptr->isn_arg.number;
2678 break;
2679 case ISN_PUSHBOOL:
2680 tv->v_type = VAR_BOOL;
2681 tv->vval.v_number = iptr->isn_arg.number;
2682 break;
2683 case ISN_PUSHSPEC:
2684 tv->v_type = VAR_SPECIAL;
2685 tv->vval.v_number = iptr->isn_arg.number;
2686 break;
2687#ifdef FEAT_FLOAT
2688 case ISN_PUSHF:
2689 tv->v_type = VAR_FLOAT;
2690 tv->vval.v_float = iptr->isn_arg.fnumber;
2691 break;
2692#endif
2693 case ISN_PUSHBLOB:
2694 blob_copy(iptr->isn_arg.blob, tv);
2695 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002696 case ISN_PUSHFUNC:
2697 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002698 if (iptr->isn_arg.string == NULL)
2699 tv->vval.v_string = NULL;
2700 else
2701 tv->vval.v_string =
2702 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002703 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002704 case ISN_PUSHCHANNEL:
2705#ifdef FEAT_JOB_CHANNEL
2706 tv->v_type = VAR_CHANNEL;
2707 tv->vval.v_channel = iptr->isn_arg.channel;
2708 if (tv->vval.v_channel != NULL)
2709 ++tv->vval.v_channel->ch_refcount;
2710#endif
2711 break;
2712 case ISN_PUSHJOB:
2713#ifdef FEAT_JOB_CHANNEL
2714 tv->v_type = VAR_JOB;
2715 tv->vval.v_job = iptr->isn_arg.job;
2716 if (tv->vval.v_job != NULL)
2717 ++tv->vval.v_job->jv_refcount;
2718#endif
2719 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 default:
2721 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002722 tv->vval.v_string = vim_strsave(
2723 iptr->isn_arg.string == NULL
2724 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 }
2726 break;
2727
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002728 case ISN_UNLET:
2729 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2730 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002731 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002732 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002733 case ISN_UNLETENV:
2734 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2735 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002736
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002737 case ISN_LOCKCONST:
2738 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2739 break;
2740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 // create a list from items on the stack; uses a single allocation
2742 // for the list header and the items
2743 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002744 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002745 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 break;
2747
2748 // create a dict from items on the stack
2749 case ISN_NEWDICT:
2750 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002751 int count = iptr->isn_arg.number;
2752 dict_T *dict = dict_alloc();
2753 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002754 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002755 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756
2757 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002758 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 for (idx = 0; idx < count; ++idx)
2760 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002761 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002763 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002764 key = tv->vval.v_string == NULL
2765 ? (char_u *)"" : tv->vval.v_string;
2766 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002767 if (item != NULL)
2768 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002769 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002770 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002771 dict_unref(dict);
2772 goto on_error;
2773 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002774 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 clear_tv(tv);
2776 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002777 {
2778 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002779 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2782 item->di_tv.v_lock = 0;
2783 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002784 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002785 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002786 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002787 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 }
2790
2791 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002792 ectx->ec_stack.ga_len -= 2 * count - 1;
2793 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002794 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002796 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 tv = STACK_TV_BOT(-1);
2798 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002799 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 tv->vval.v_dict = dict;
2801 ++dict->dv_refcount;
2802 }
2803 break;
2804
2805 // call a :def function
2806 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002807 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002808 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2809 NULL,
2810 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002811 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002812 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 break;
2814
2815 // call a builtin function
2816 case ISN_BCALL:
2817 SOURCING_LNUM = iptr->isn_lnum;
2818 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2819 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002820 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002821 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 break;
2823
2824 // call a funcref or partial
2825 case ISN_PCALL:
2826 {
2827 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2828 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002829 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830
2831 SOURCING_LNUM = iptr->isn_lnum;
2832 if (pfunc->cpf_top)
2833 {
2834 // funcref is above the arguments
2835 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2836 }
2837 else
2838 {
2839 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002840 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002841 partial_tv = *STACK_TV_BOT(0);
2842 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002844 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002845 if (tv == &partial_tv)
2846 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002848 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 }
2850 break;
2851
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002852 case ISN_PCALL_END:
2853 // PCALL finished, arguments have been consumed and replaced by
2854 // the return value. Now clear the funcref from the stack,
2855 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002856 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002857 clear_tv(STACK_TV_BOT(-1));
2858 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2859 break;
2860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 // call a user defined function or funcref/partial
2862 case ISN_UCALL:
2863 {
2864 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2865
2866 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002867 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002868 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002869 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 }
2871 break;
2872
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002873 // return from a :def function call without a value
2874 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002875 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002876 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002877 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002878 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002879 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002880 tv->vval.v_number = 0;
2881 tv->v_lock = 0;
2882 // FALLTHROUGH
2883
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002884 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 case ISN_RETURN:
2886 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002887 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002888 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002889
2890 if (trystack->ga_len > 0)
2891 trycmd = ((trycmd_T *)trystack->ga_data)
2892 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002893 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002894 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002896 // jump to ":finally" or ":endtry"
2897 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002898 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002899 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002900 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 trycmd->tcd_return = TRUE;
2902 }
2903 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002904 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 }
2906 break;
2907
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002908 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 case ISN_FUNCREF:
2910 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002911 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2912 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2913 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002916 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002917 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002918 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002919 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002920 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002921 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002922 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002923 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002924 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002926 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 tv->vval.v_partial = pt;
2928 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002929 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 }
2931 break;
2932
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002933 // Create a global function from a lambda.
2934 case ISN_NEWFUNC:
2935 {
2936 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2937
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002938 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002939 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002940 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002941 }
2942 break;
2943
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002944 // List functions
2945 case ISN_DEF:
2946 if (iptr->isn_arg.string == NULL)
2947 list_functions(NULL);
2948 else
2949 {
2950 exarg_T ea;
2951
2952 CLEAR_FIELD(ea);
2953 ea.cmd = ea.arg = iptr->isn_arg.string;
2954 define_function(&ea, NULL);
2955 }
2956 break;
2957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 // jump if a condition is met
2959 case ISN_JUMP:
2960 {
2961 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002962 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 int jump = TRUE;
2964
2965 if (when != JUMP_ALWAYS)
2966 {
2967 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002968 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002969 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002970 || when == JUMP_IF_COND_TRUE)
2971 {
2972 SOURCING_LNUM = iptr->isn_lnum;
2973 jump = tv_get_bool_chk(tv, &error);
2974 if (error)
2975 goto on_error;
2976 }
2977 else
2978 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002980 || when == JUMP_AND_KEEP_IF_FALSE
2981 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002983 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 {
2985 // drop the value from the stack
2986 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002987 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 }
2989 }
2990 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002991 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 }
2993 break;
2994
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002995 // Jump if an argument with a default value was already set and not
2996 // v:none.
2997 case ISN_JUMP_IF_ARG_SET:
2998 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2999 if (tv->v_type != VAR_UNKNOWN
3000 && !(tv->v_type == VAR_SPECIAL
3001 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003002 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003003 break;
3004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 // top of a for loop
3006 case ISN_FOR:
3007 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003008 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 typval_T *idxtv =
3010 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3011
Bram Moolenaar4c137212021-04-19 16:48:48 +02003012 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003013 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003014 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003015 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003016 list_T *list = ltv->vval.v_list;
3017
3018 // push the next item from the list
3019 ++idxtv->vval.v_number;
3020 if (list == NULL
3021 || idxtv->vval.v_number >= list->lv_len)
3022 {
3023 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003024 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3025 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003026 }
3027 else if (list->lv_first == &range_list_item)
3028 {
3029 // non-materialized range() list
3030 tv = STACK_TV_BOT(0);
3031 tv->v_type = VAR_NUMBER;
3032 tv->v_lock = 0;
3033 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003035 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003036 }
3037 else
3038 {
3039 listitem_T *li = list_find(list,
3040 idxtv->vval.v_number);
3041
3042 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003043 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003044 }
3045 }
3046 else if (ltv->v_type == VAR_STRING)
3047 {
3048 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003049
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003050 // The index is for the last byte of the previous
3051 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003052 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003053 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003054 {
3055 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003056 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3057 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003058 }
3059 else
3060 {
3061 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3062
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003063 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003064 tv = STACK_TV_BOT(0);
3065 tv->v_type = VAR_STRING;
3066 tv->vval.v_string = vim_strnsave(
3067 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003068 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003069 idxtv->vval.v_number += clen - 1;
3070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003072 else if (ltv->v_type == VAR_BLOB)
3073 {
3074 blob_T *blob = ltv->vval.v_blob;
3075
3076 // When we get here the first time make a copy of the
3077 // blob, so that the iteration still works when it is
3078 // changed.
3079 if (idxtv->vval.v_number == -1 && blob != NULL)
3080 {
3081 blob_copy(blob, ltv);
3082 blob_unref(blob);
3083 blob = ltv->vval.v_blob;
3084 }
3085
3086 // The index is for the previous byte.
3087 ++idxtv->vval.v_number;
3088 if (blob == NULL
3089 || idxtv->vval.v_number >= blob_len(blob))
3090 {
3091 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003092 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3093 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003094 }
3095 else
3096 {
3097 // Push the next byte from the blob.
3098 tv = STACK_TV_BOT(0);
3099 tv->v_type = VAR_NUMBER;
3100 tv->vval.v_number = blob_get(blob,
3101 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003102 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003103 }
3104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 else
3106 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003107 semsg(_(e_for_loop_on_str_not_supported),
3108 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003109 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 }
3111 }
3112 break;
3113
3114 // start of ":try" block
3115 case ISN_TRY:
3116 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003117 trycmd_T *trycmd = NULL;
3118
Bram Moolenaar4c137212021-04-19 16:48:48 +02003119 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003120 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003121 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3122 + ectx->ec_trystack.ga_len;
3123 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003125 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003126 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3127 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003128 trycmd->tcd_catch_idx =
3129 iptr->isn_arg.try.try_ref->try_catch;
3130 trycmd->tcd_finally_idx =
3131 iptr->isn_arg.try.try_ref->try_finally;
3132 trycmd->tcd_endtry_idx =
3133 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 }
3135 break;
3136
3137 case ISN_PUSHEXC:
3138 if (current_exception == NULL)
3139 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003140 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003142 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003145 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003147 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003149 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 tv->vval.v_string = vim_strsave(
3151 (char_u *)current_exception->value);
3152 break;
3153
3154 case ISN_CATCH:
3155 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003156 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157
Bram Moolenaar4c137212021-04-19 16:48:48 +02003158 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 if (trystack->ga_len > 0)
3160 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003161 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 + trystack->ga_len - 1;
3163 trycmd->tcd_caught = TRUE;
3164 }
3165 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003166 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 catch_exception(current_exception);
3168 }
3169 break;
3170
Bram Moolenaarc150c092021-02-13 15:02:46 +01003171 case ISN_TRYCONT:
3172 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003173 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003174 trycont_T *trycont = &iptr->isn_arg.trycont;
3175 int i;
3176 trycmd_T *trycmd;
3177 int iidx = trycont->tct_where;
3178
3179 if (trystack->ga_len < trycont->tct_levels)
3180 {
3181 siemsg("TRYCONT: expected %d levels, found %d",
3182 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003183 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003184 }
3185 // Make :endtry jump to any outer try block and the last
3186 // :endtry inside the loop to the loop start.
3187 for (i = trycont->tct_levels; i > 0; --i)
3188 {
3189 trycmd = ((trycmd_T *)trystack->ga_data)
3190 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003191 // Add one to tcd_cont to be able to jump to
3192 // instruction with index zero.
3193 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003194 iidx = trycmd->tcd_finally_idx == 0
3195 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003196 }
3197 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003198 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003199 }
3200 break;
3201
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003202 case ISN_FINALLY:
3203 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003204 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003205 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3206 + trystack->ga_len - 1;
3207
3208 // Reset the index to avoid a return statement jumps here
3209 // again.
3210 trycmd->tcd_finally_idx = 0;
3211 break;
3212 }
3213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 // end of ":try" block
3215 case ISN_ENDTRY:
3216 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218
3219 if (trystack->ga_len > 0)
3220 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003221 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 --trystack->ga_len;
3224 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003225 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 trycmd = ((trycmd_T *)trystack->ga_data)
3227 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003228 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 {
3230 // discard the exception
3231 if (caught_stack == current_exception)
3232 caught_stack = caught_stack->caught;
3233 discard_current_exception();
3234 }
3235
3236 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003237 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003238
Bram Moolenaar4c137212021-04-19 16:48:48 +02003239 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003240 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003241 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003242 clear_tv(STACK_TV_BOT(0));
3243 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003244 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003245 // handling :continue: jump to outer try block or
3246 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003247 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 }
3249 }
3250 break;
3251
3252 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003253 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003254 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003255
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003256 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3257 {
3258 // throwing an exception while using "silent!" causes
3259 // the function to abort but not display an error.
3260 tv = STACK_TV_BOT(-1);
3261 clear_tv(tv);
3262 tv->v_type = VAR_NUMBER;
3263 tv->vval.v_number = 0;
3264 goto done;
3265 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003266 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003267 tv = STACK_TV_BOT(0);
3268 if (tv->vval.v_string == NULL
3269 || *skipwhite(tv->vval.v_string) == NUL)
3270 {
3271 vim_free(tv->vval.v_string);
3272 SOURCING_LNUM = iptr->isn_lnum;
3273 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003274 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003275 }
3276
3277 // Inside a "catch" we need to first discard the caught
3278 // exception.
3279 if (trystack->ga_len > 0)
3280 {
3281 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3282 + trystack->ga_len - 1;
3283 if (trycmd->tcd_caught && current_exception != NULL)
3284 {
3285 // discard the exception
3286 if (caught_stack == current_exception)
3287 caught_stack = caught_stack->caught;
3288 discard_current_exception();
3289 trycmd->tcd_caught = FALSE;
3290 }
3291 }
3292
3293 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3294 == FAIL)
3295 {
3296 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003297 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003298 }
3299 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 break;
3302
3303 // compare with special values
3304 case ISN_COMPAREBOOL:
3305 case ISN_COMPARESPECIAL:
3306 {
3307 typval_T *tv1 = STACK_TV_BOT(-2);
3308 typval_T *tv2 = STACK_TV_BOT(-1);
3309 varnumber_T arg1 = tv1->vval.v_number;
3310 varnumber_T arg2 = tv2->vval.v_number;
3311 int res;
3312
3313 switch (iptr->isn_arg.op.op_type)
3314 {
3315 case EXPR_EQUAL: res = arg1 == arg2; break;
3316 case EXPR_NEQUAL: res = arg1 != arg2; break;
3317 default: res = 0; break;
3318 }
3319
Bram Moolenaar4c137212021-04-19 16:48:48 +02003320 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 tv1->v_type = VAR_BOOL;
3322 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3323 }
3324 break;
3325
3326 // Operation with two number arguments
3327 case ISN_OPNR:
3328 case ISN_COMPARENR:
3329 {
3330 typval_T *tv1 = STACK_TV_BOT(-2);
3331 typval_T *tv2 = STACK_TV_BOT(-1);
3332 varnumber_T arg1 = tv1->vval.v_number;
3333 varnumber_T arg2 = tv2->vval.v_number;
3334 varnumber_T res;
3335
3336 switch (iptr->isn_arg.op.op_type)
3337 {
3338 case EXPR_MULT: res = arg1 * arg2; break;
3339 case EXPR_DIV: res = arg1 / arg2; break;
3340 case EXPR_REM: res = arg1 % arg2; break;
3341 case EXPR_SUB: res = arg1 - arg2; break;
3342 case EXPR_ADD: res = arg1 + arg2; break;
3343
3344 case EXPR_EQUAL: res = arg1 == arg2; break;
3345 case EXPR_NEQUAL: res = arg1 != arg2; break;
3346 case EXPR_GREATER: res = arg1 > arg2; break;
3347 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3348 case EXPR_SMALLER: res = arg1 < arg2; break;
3349 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3350 default: res = 0; break;
3351 }
3352
Bram Moolenaar4c137212021-04-19 16:48:48 +02003353 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 if (iptr->isn_type == ISN_COMPARENR)
3355 {
3356 tv1->v_type = VAR_BOOL;
3357 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3358 }
3359 else
3360 tv1->vval.v_number = res;
3361 }
3362 break;
3363
3364 // Computation with two float arguments
3365 case ISN_OPFLOAT:
3366 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003367#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 {
3369 typval_T *tv1 = STACK_TV_BOT(-2);
3370 typval_T *tv2 = STACK_TV_BOT(-1);
3371 float_T arg1 = tv1->vval.v_float;
3372 float_T arg2 = tv2->vval.v_float;
3373 float_T res = 0;
3374 int cmp = FALSE;
3375
3376 switch (iptr->isn_arg.op.op_type)
3377 {
3378 case EXPR_MULT: res = arg1 * arg2; break;
3379 case EXPR_DIV: res = arg1 / arg2; break;
3380 case EXPR_SUB: res = arg1 - arg2; break;
3381 case EXPR_ADD: res = arg1 + arg2; break;
3382
3383 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3384 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3385 case EXPR_GREATER: cmp = arg1 > arg2; break;
3386 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3387 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3388 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3389 default: cmp = 0; break;
3390 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003391 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 if (iptr->isn_type == ISN_COMPAREFLOAT)
3393 {
3394 tv1->v_type = VAR_BOOL;
3395 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3396 }
3397 else
3398 tv1->vval.v_float = res;
3399 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003400#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 break;
3402
3403 case ISN_COMPARELIST:
3404 {
3405 typval_T *tv1 = STACK_TV_BOT(-2);
3406 typval_T *tv2 = STACK_TV_BOT(-1);
3407 list_T *arg1 = tv1->vval.v_list;
3408 list_T *arg2 = tv2->vval.v_list;
3409 int cmp = FALSE;
3410 int ic = iptr->isn_arg.op.op_ic;
3411
3412 switch (iptr->isn_arg.op.op_type)
3413 {
3414 case EXPR_EQUAL: cmp =
3415 list_equal(arg1, arg2, ic, FALSE); break;
3416 case EXPR_NEQUAL: cmp =
3417 !list_equal(arg1, arg2, ic, FALSE); break;
3418 case EXPR_IS: cmp = arg1 == arg2; break;
3419 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3420 default: cmp = 0; break;
3421 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003422 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 clear_tv(tv1);
3424 clear_tv(tv2);
3425 tv1->v_type = VAR_BOOL;
3426 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3427 }
3428 break;
3429
3430 case ISN_COMPAREBLOB:
3431 {
3432 typval_T *tv1 = STACK_TV_BOT(-2);
3433 typval_T *tv2 = STACK_TV_BOT(-1);
3434 blob_T *arg1 = tv1->vval.v_blob;
3435 blob_T *arg2 = tv2->vval.v_blob;
3436 int cmp = FALSE;
3437
3438 switch (iptr->isn_arg.op.op_type)
3439 {
3440 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3441 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3442 case EXPR_IS: cmp = arg1 == arg2; break;
3443 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3444 default: cmp = 0; break;
3445 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003446 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 clear_tv(tv1);
3448 clear_tv(tv2);
3449 tv1->v_type = VAR_BOOL;
3450 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3451 }
3452 break;
3453
3454 // TODO: handle separately
3455 case ISN_COMPARESTRING:
3456 case ISN_COMPAREDICT:
3457 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 case ISN_COMPAREANY:
3459 {
3460 typval_T *tv1 = STACK_TV_BOT(-2);
3461 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003462 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 int ic = iptr->isn_arg.op.op_ic;
3464
Bram Moolenaareb26f432020-09-14 16:50:05 +02003465 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003466 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003468 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 }
3470 break;
3471
3472 case ISN_ADDLIST:
3473 case ISN_ADDBLOB:
3474 {
3475 typval_T *tv1 = STACK_TV_BOT(-2);
3476 typval_T *tv2 = STACK_TV_BOT(-1);
3477
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003478 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 if (iptr->isn_type == ISN_ADDLIST)
3480 eval_addlist(tv1, tv2);
3481 else
3482 eval_addblob(tv1, tv2);
3483 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003484 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 }
3486 break;
3487
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003488 case ISN_LISTAPPEND:
3489 {
3490 typval_T *tv1 = STACK_TV_BOT(-2);
3491 typval_T *tv2 = STACK_TV_BOT(-1);
3492 list_T *l = tv1->vval.v_list;
3493
3494 // add an item to a list
3495 if (l == NULL)
3496 {
3497 SOURCING_LNUM = iptr->isn_lnum;
3498 emsg(_(e_cannot_add_to_null_list));
3499 goto on_error;
3500 }
3501 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003502 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003503 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003504 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003505 }
3506 break;
3507
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003508 case ISN_BLOBAPPEND:
3509 {
3510 typval_T *tv1 = STACK_TV_BOT(-2);
3511 typval_T *tv2 = STACK_TV_BOT(-1);
3512 blob_T *b = tv1->vval.v_blob;
3513 int error = FALSE;
3514 varnumber_T n;
3515
3516 // add a number to a blob
3517 if (b == NULL)
3518 {
3519 SOURCING_LNUM = iptr->isn_lnum;
3520 emsg(_(e_cannot_add_to_null_blob));
3521 goto on_error;
3522 }
3523 n = tv_get_number_chk(tv2, &error);
3524 if (error)
3525 goto on_error;
3526 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003527 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003528 }
3529 break;
3530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 // Computation with two arguments of unknown type
3532 case ISN_OPANY:
3533 {
3534 typval_T *tv1 = STACK_TV_BOT(-2);
3535 typval_T *tv2 = STACK_TV_BOT(-1);
3536 varnumber_T n1, n2;
3537#ifdef FEAT_FLOAT
3538 float_T f1 = 0, f2 = 0;
3539#endif
3540 int error = FALSE;
3541
3542 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3543 {
3544 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3545 {
3546 eval_addlist(tv1, tv2);
3547 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003548 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 break;
3550 }
3551 else if (tv1->v_type == VAR_BLOB
3552 && tv2->v_type == VAR_BLOB)
3553 {
3554 eval_addblob(tv1, tv2);
3555 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003556 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 break;
3558 }
3559 }
3560#ifdef FEAT_FLOAT
3561 if (tv1->v_type == VAR_FLOAT)
3562 {
3563 f1 = tv1->vval.v_float;
3564 n1 = 0;
3565 }
3566 else
3567#endif
3568 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003569 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 n1 = tv_get_number_chk(tv1, &error);
3571 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003572 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573#ifdef FEAT_FLOAT
3574 if (tv2->v_type == VAR_FLOAT)
3575 f1 = n1;
3576#endif
3577 }
3578#ifdef FEAT_FLOAT
3579 if (tv2->v_type == VAR_FLOAT)
3580 {
3581 f2 = tv2->vval.v_float;
3582 n2 = 0;
3583 }
3584 else
3585#endif
3586 {
3587 n2 = tv_get_number_chk(tv2, &error);
3588 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003589 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590#ifdef FEAT_FLOAT
3591 if (tv1->v_type == VAR_FLOAT)
3592 f2 = n2;
3593#endif
3594 }
3595#ifdef FEAT_FLOAT
3596 // if there is a float on either side the result is a float
3597 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3598 {
3599 switch (iptr->isn_arg.op.op_type)
3600 {
3601 case EXPR_MULT: f1 = f1 * f2; break;
3602 case EXPR_DIV: f1 = f1 / f2; break;
3603 case EXPR_SUB: f1 = f1 - f2; break;
3604 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003605 default: SOURCING_LNUM = iptr->isn_lnum;
3606 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003607 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 }
3609 clear_tv(tv1);
3610 clear_tv(tv2);
3611 tv1->v_type = VAR_FLOAT;
3612 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003613 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 }
3615 else
3616#endif
3617 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003618 int failed = FALSE;
3619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 switch (iptr->isn_arg.op.op_type)
3621 {
3622 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003623 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3624 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003625 goto on_error;
3626 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 case EXPR_SUB: n1 = n1 - n2; break;
3628 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003629 default: n1 = num_modulus(n1, n2, &failed);
3630 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003631 goto on_error;
3632 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 }
3634 clear_tv(tv1);
3635 clear_tv(tv2);
3636 tv1->v_type = VAR_NUMBER;
3637 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003638 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 }
3640 }
3641 break;
3642
3643 case ISN_CONCAT:
3644 {
3645 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3646 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3647 char_u *res;
3648
3649 res = concat_str(str1, str2);
3650 clear_tv(STACK_TV_BOT(-2));
3651 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003652 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 STACK_TV_BOT(-1)->vval.v_string = res;
3654 }
3655 break;
3656
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003657 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003658 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003659 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003660 int is_slice = iptr->isn_type == ISN_STRSLICE;
3661 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003662 char_u *res;
3663
3664 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003665 // string slice: string is at stack-3, first index at
3666 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003667 if (is_slice)
3668 {
3669 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003670 n1 = tv->vval.v_number;
3671 }
3672
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003673 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003674 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003675
Bram Moolenaar4c137212021-04-19 16:48:48 +02003676 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003677 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003678 if (is_slice)
3679 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003680 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003681 else
3682 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003683 // single character (including composing characters).
3684 // If the index is too big or negative the result is
3685 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003686 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003687 vim_free(tv->vval.v_string);
3688 tv->vval.v_string = res;
3689 }
3690 break;
3691
3692 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003693 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003694 case ISN_BLOBINDEX:
3695 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003697 int is_slice = iptr->isn_type == ISN_LISTSLICE
3698 || iptr->isn_type == ISN_BLOBSLICE;
3699 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3700 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003701 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003702 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703
3704 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003705 // list slice: list is at stack-3, indexes at stack-2 and
3706 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003707 // Same for blob.
3708 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709
3710 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003711 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003713
3714 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 {
Bram Moolenaared591872020-08-15 22:14:53 +02003716 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003717 n1 = tv->vval.v_number;
3718 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 }
Bram Moolenaared591872020-08-15 22:14:53 +02003720
Bram Moolenaar4c137212021-04-19 16:48:48 +02003721 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003722 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003723 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003724 if (is_blob)
3725 {
3726 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3727 n1, n2, FALSE, tv) == FAIL)
3728 goto on_error;
3729 }
3730 else
3731 {
3732 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3733 n1, n2, FALSE, tv, TRUE) == FAIL)
3734 goto on_error;
3735 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 }
3737 break;
3738
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003739 case ISN_ANYINDEX:
3740 case ISN_ANYSLICE:
3741 {
3742 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3743 typval_T *var1, *var2;
3744 int res;
3745
3746 // index: composite is at stack-2, index at stack-1
3747 // slice: composite is at stack-3, indexes at stack-2 and
3748 // stack-1
3749 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003750 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003751 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3752 goto on_error;
3753 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3754 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003755 res = eval_index_inner(tv, is_slice, var1, var2,
3756 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003757 clear_tv(var1);
3758 if (is_slice)
3759 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003760 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003761 if (res == FAIL)
3762 goto on_error;
3763 }
3764 break;
3765
Bram Moolenaar9af78762020-06-16 11:34:42 +02003766 case ISN_SLICE:
3767 {
3768 list_T *list;
3769 int count = iptr->isn_arg.number;
3770
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003771 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003772 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003773 list = tv->vval.v_list;
3774
3775 // no error for short list, expect it to be checked earlier
3776 if (list != NULL && list->lv_len >= count)
3777 {
3778 list_T *newlist = list_slice(list,
3779 count, list->lv_len - 1);
3780
3781 if (newlist != NULL)
3782 {
3783 list_unref(list);
3784 tv->vval.v_list = newlist;
3785 ++newlist->lv_refcount;
3786 }
3787 }
3788 }
3789 break;
3790
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003791 case ISN_GETITEM:
3792 {
3793 listitem_T *li;
3794 int index = iptr->isn_arg.number;
3795
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003796 // Get list item: list is at stack-1, push item.
3797 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003798 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003799 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003800
Bram Moolenaar4c137212021-04-19 16:48:48 +02003801 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003802 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003803 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003804 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003805
3806 // Useful when used in unpack assignment. Reset at
3807 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 ectx->ec_where.wt_index = index + 1;
3809 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003810 }
3811 break;
3812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 case ISN_MEMBER:
3814 {
3815 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003816 char_u *key;
3817 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003818 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003819
3820 // dict member: dict is at stack-2, key at stack-1
3821 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003822 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003823 dict = tv->vval.v_dict;
3824
3825 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003826 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003827 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003828 if (key == NULL)
3829 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003830
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003831 if ((di = dict_find(dict, key, -1)) == NULL)
3832 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003833 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003834 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003835
3836 // If :silent! is used we will continue, make sure the
3837 // stack contents makes sense.
3838 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003840 tv = STACK_TV_BOT(-1);
3841 clear_tv(tv);
3842 tv->v_type = VAR_NUMBER;
3843 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003844 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003845 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003846 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003847 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003848 // Clear the dict only after getting the item, to avoid
3849 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003850 tv = STACK_TV_BOT(-1);
3851 temp_tv = *tv;
3852 copy_tv(&di->di_tv, tv);
3853 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003854 }
3855 break;
3856
3857 // dict member with string key
3858 case ISN_STRINGMEMBER:
3859 {
3860 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003862 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863
3864 tv = STACK_TV_BOT(-1);
3865 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3866 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003867 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003869 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 }
3871 dict = tv->vval.v_dict;
3872
3873 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3874 == NULL)
3875 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003876 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003878 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003880 // Clear the dict after getting the item, to avoid that it
3881 // make the item invalid.
3882 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003884 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 }
3886 break;
3887
3888 case ISN_NEGATENR:
3889 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003890 if (tv->v_type != VAR_NUMBER
3891#ifdef FEAT_FLOAT
3892 && tv->v_type != VAR_FLOAT
3893#endif
3894 )
3895 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003896 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003897 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003898 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003899 }
3900#ifdef FEAT_FLOAT
3901 if (tv->v_type == VAR_FLOAT)
3902 tv->vval.v_float = -tv->vval.v_float;
3903 else
3904#endif
3905 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 break;
3907
3908 case ISN_CHECKNR:
3909 {
3910 int error = FALSE;
3911
3912 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003913 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003915 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 (void)tv_get_number_chk(tv, &error);
3917 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003918 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 }
3920 break;
3921
3922 case ISN_CHECKTYPE:
3923 {
3924 checktype_T *ct = &iptr->isn_arg.type;
3925
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003926 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003927 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003928 if (!ectx->ec_where.wt_variable)
3929 ectx->ec_where.wt_index = ct->ct_arg_idx;
3930 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3931 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003932 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003933 if (!ectx->ec_where.wt_variable)
3934 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003935
3936 // number 0 is FALSE, number 1 is TRUE
3937 if (tv->v_type == VAR_NUMBER
3938 && ct->ct_type->tt_type == VAR_BOOL
3939 && (tv->vval.v_number == 0
3940 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003942 tv->v_type = VAR_BOOL;
3943 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003944 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 }
3946 }
3947 break;
3948
Bram Moolenaar9af78762020-06-16 11:34:42 +02003949 case ISN_CHECKLEN:
3950 {
3951 int min_len = iptr->isn_arg.checklen.cl_min_len;
3952 list_T *list = NULL;
3953
3954 tv = STACK_TV_BOT(-1);
3955 if (tv->v_type == VAR_LIST)
3956 list = tv->vval.v_list;
3957 if (list == NULL || list->lv_len < min_len
3958 || (list->lv_len > min_len
3959 && !iptr->isn_arg.checklen.cl_more_OK))
3960 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003961 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003962 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003963 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003964 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003965 }
3966 }
3967 break;
3968
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003969 case ISN_SETTYPE:
3970 {
3971 checktype_T *ct = &iptr->isn_arg.type;
3972
3973 tv = STACK_TV_BOT(-1);
3974 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3975 {
3976 free_type(tv->vval.v_dict->dv_type);
3977 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3978 }
3979 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3980 {
3981 free_type(tv->vval.v_list->lv_type);
3982 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3983 }
3984 }
3985 break;
3986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003988 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 {
3990 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003991 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003993 if (iptr->isn_type == ISN_2BOOL)
3994 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003995 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003996 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003997 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003998 n = !n;
3999 }
4000 else
4001 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004002 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004003 SOURCING_LNUM = iptr->isn_lnum;
4004 n = tv_get_bool_chk(tv, &error);
4005 if (error)
4006 goto on_error;
4007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 clear_tv(tv);
4009 tv->v_type = VAR_BOOL;
4010 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4011 }
4012 break;
4013
4014 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004015 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004016 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004017 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4018 iptr->isn_type == ISN_2STRING_ANY,
4019 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004020 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 break;
4022
Bram Moolenaar08597872020-12-10 19:43:40 +01004023 case ISN_RANGE:
4024 {
4025 exarg_T ea;
4026 char *errormsg;
4027
Bram Moolenaarece0b872021-01-08 20:40:45 +01004028 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004029 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004030 ea.addr_type = ADDR_LINES;
4031 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004032 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004033 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004034 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004035
Bram Moolenaar4c137212021-04-19 16:48:48 +02004036 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004037 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004038 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004039 tv = STACK_TV_BOT(-1);
4040 tv->v_type = VAR_NUMBER;
4041 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004042 if (ea.addr_count == 0)
4043 tv->vval.v_number = curwin->w_cursor.lnum;
4044 else
4045 tv->vval.v_number = ea.line2;
4046 }
4047 break;
4048
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004049 case ISN_PUT:
4050 {
4051 int regname = iptr->isn_arg.put.put_regname;
4052 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4053 char_u *expr = NULL;
4054 int dir = FORWARD;
4055
Bram Moolenaar08597872020-12-10 19:43:40 +01004056 if (lnum < -2)
4057 {
4058 // line number was put on the stack by ISN_RANGE
4059 tv = STACK_TV_BOT(-1);
4060 curwin->w_cursor.lnum = tv->vval.v_number;
4061 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4062 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004063 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004064 }
4065 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004066 // :put! above cursor
4067 dir = BACKWARD;
4068 else if (lnum >= 0)
4069 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004070
4071 if (regname == '=')
4072 {
4073 tv = STACK_TV_BOT(-1);
4074 if (tv->v_type == VAR_STRING)
4075 expr = tv->vval.v_string;
4076 else
4077 {
4078 expr = typval2string(tv, TRUE); // allocates value
4079 clear_tv(tv);
4080 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004081 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004082 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004083 check_cursor();
4084 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4085 vim_free(expr);
4086 }
4087 break;
4088
Bram Moolenaar02194d22020-10-24 23:08:38 +02004089 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004090 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4091 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4092 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4093 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004094 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4095 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004096 break;
4097
Bram Moolenaar02194d22020-10-24 23:08:38 +02004098 case ISN_CMDMOD_REV:
4099 // filter regprog is owned by the instruction, don't free it
4100 cmdmod.cmod_filter_regmatch.regprog = NULL;
4101 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004102 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4103 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004104 break;
4105
Bram Moolenaar792f7862020-11-23 08:31:18 +01004106 case ISN_UNPACK:
4107 {
4108 int count = iptr->isn_arg.unpack.unp_count;
4109 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4110 list_T *l;
4111 listitem_T *li;
4112 int i;
4113
4114 // Check there is a valid list to unpack.
4115 tv = STACK_TV_BOT(-1);
4116 if (tv->v_type != VAR_LIST)
4117 {
4118 SOURCING_LNUM = iptr->isn_lnum;
4119 emsg(_(e_for_argument_must_be_sequence_of_lists));
4120 goto on_error;
4121 }
4122 l = tv->vval.v_list;
4123 if (l == NULL
4124 || l->lv_len < (semicolon ? count - 1 : count))
4125 {
4126 SOURCING_LNUM = iptr->isn_lnum;
4127 emsg(_(e_list_value_does_not_have_enough_items));
4128 goto on_error;
4129 }
4130 else if (!semicolon && l->lv_len > count)
4131 {
4132 SOURCING_LNUM = iptr->isn_lnum;
4133 emsg(_(e_list_value_has_more_items_than_targets));
4134 goto on_error;
4135 }
4136
4137 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004138 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004139 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004140 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004141
4142 // Variable after semicolon gets a list with the remaining
4143 // items.
4144 if (semicolon)
4145 {
4146 list_T *rem_list =
4147 list_alloc_with_items(l->lv_len - count + 1);
4148
4149 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004150 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004151 tv = STACK_TV_BOT(-count);
4152 tv->vval.v_list = rem_list;
4153 ++rem_list->lv_refcount;
4154 tv->v_lock = 0;
4155 li = l->lv_first;
4156 for (i = 0; i < count - 1; ++i)
4157 li = li->li_next;
4158 for (i = 0; li != NULL; ++i)
4159 {
4160 list_set_item(rem_list, i, &li->li_tv);
4161 li = li->li_next;
4162 }
4163 --count;
4164 }
4165
4166 // Produce the values in reverse order, first item last.
4167 li = l->lv_first;
4168 for (i = 0; i < count; ++i)
4169 {
4170 tv = STACK_TV_BOT(-i - 1);
4171 copy_tv(&li->li_tv, tv);
4172 li = li->li_next;
4173 }
4174
4175 list_unref(l);
4176 }
4177 break;
4178
Bram Moolenaarb2049902021-01-24 12:53:53 +01004179 case ISN_PROF_START:
4180 case ISN_PROF_END:
4181 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004182#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004183 funccall_T cookie;
4184 ufunc_T *cur_ufunc =
4185 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004186 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004187
4188 cookie.func = cur_ufunc;
4189 if (iptr->isn_type == ISN_PROF_START)
4190 {
4191 func_line_start(&cookie, iptr->isn_lnum);
4192 // if we get here the instruction is executed
4193 func_line_exec(&cookie);
4194 }
4195 else
4196 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004197#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004198 }
4199 break;
4200
Bram Moolenaare99d4222021-06-13 14:01:26 +02004201 case ISN_DEBUG:
4202 if (ex_nesting_level <= debug_break_level)
Bram Moolenaar4cea5362021-06-16 22:24:40 +02004203 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004204 break;
4205
Bram Moolenaar389df252020-07-09 21:20:47 +02004206 case ISN_SHUFFLE:
4207 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004208 typval_T tmp_tv;
4209 int item = iptr->isn_arg.shuffle.shfl_item;
4210 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004211
4212 tmp_tv = *STACK_TV_BOT(-item);
4213 for ( ; up > 0 && item > 1; --up)
4214 {
4215 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4216 --item;
4217 }
4218 *STACK_TV_BOT(-item) = tmp_tv;
4219 }
4220 break;
4221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004223 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004225 ectx->ec_where.wt_index = 0;
4226 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 break;
4228 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004229 continue;
4230
Bram Moolenaard032f342020-07-18 18:13:02 +02004231func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004232 // Restore previous function. If the frame pointer is where we started
4233 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004234 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004235 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004236
Bram Moolenaar4c137212021-04-19 16:48:48 +02004237 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004238 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004239 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004240 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004241
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004242on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004243 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004244 // If "emsg_silent" is set then ignore the error, unless it was set
4245 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004246 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004247 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004248 {
4249 // If a sequence of instructions causes an error while ":silent!"
4250 // was used, restore the stack length and jump ahead to restoring
4251 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004252 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004253 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004254 while (ectx->ec_stack.ga_len
4255 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004256 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004257 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004258 clear_tv(STACK_TV_BOT(0));
4259 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004260 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4261 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004262 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004263 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004264 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004265on_fatal_error:
4266 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004267 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004268 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004269 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 }
4271
4272done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004273 ret = OK;
4274theend:
4275 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4276 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004277}
4278
4279/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004280 * Execute the instructions from a VAR_INSTR typeval and put the result in
4281 * "rettv".
4282 * Return OK or FAIL.
4283 */
4284 int
4285exe_typval_instr(typval_T *tv, typval_T *rettv)
4286{
4287 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4288 isn_T *save_instr = ectx->ec_instr;
4289 int save_iidx = ectx->ec_iidx;
4290 int res;
4291
4292 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4293 res = exec_instructions(ectx);
4294 if (res == OK)
4295 {
4296 *rettv = *STACK_TV_BOT(-1);
4297 --ectx->ec_stack.ga_len;
4298 }
4299
4300 ectx->ec_instr = save_instr;
4301 ectx->ec_iidx = save_iidx;
4302
4303 return res;
4304}
4305
4306/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004307 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4308 * "substitute_instr".
4309 */
4310 char_u *
4311exe_substitute_instr(void)
4312{
4313 ectx_T *ectx = substitute_instr->subs_ectx;
4314 isn_T *save_instr = ectx->ec_instr;
4315 int save_iidx = ectx->ec_iidx;
4316 char_u *res;
4317
4318 ectx->ec_instr = substitute_instr->subs_instr;
4319 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004320 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004321 typval_T *tv = STACK_TV_BOT(-1);
4322
Bram Moolenaar27523602021-06-05 21:36:19 +02004323 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004324 --ectx->ec_stack.ga_len;
4325 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004326 }
4327 else
4328 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004329 substitute_instr->subs_status = FAIL;
4330 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332
Bram Moolenaar4c137212021-04-19 16:48:48 +02004333 ectx->ec_instr = save_instr;
4334 ectx->ec_iidx = save_iidx;
4335
4336 return res;
4337}
4338
4339/*
4340 * Call a "def" function from old Vim script.
4341 * Return OK or FAIL.
4342 */
4343 int
4344call_def_function(
4345 ufunc_T *ufunc,
4346 int argc_arg, // nr of arguments
4347 typval_T *argv, // arguments
4348 partial_T *partial, // optional partial for context
4349 typval_T *rettv) // return value
4350{
4351 ectx_T ectx; // execution context
4352 int argc = argc_arg;
4353 typval_T *tv;
4354 int idx;
4355 int ret = FAIL;
4356 int defcount = ufunc->uf_args.ga_len - argc;
4357 sctx_T save_current_sctx = current_sctx;
4358 int did_emsg_before = did_emsg_cumul + did_emsg;
4359 int save_suppress_errthrow = suppress_errthrow;
4360 msglist_T **saved_msg_list = NULL;
4361 msglist_T *private_msg_list = NULL;
4362 int save_emsg_silent_def = emsg_silent_def;
4363 int save_did_emsg_def = did_emsg_def;
4364 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004365 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004366
4367// Get pointer to item in the stack.
4368#undef STACK_TV
4369#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4370
4371// Get pointer to item at the bottom of the stack, -1 is the bottom.
4372#undef STACK_TV_BOT
4373#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4374
4375// Get pointer to a local variable on the stack. Negative for arguments.
4376#undef STACK_TV_VAR
4377#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4378
4379 if (ufunc->uf_def_status == UF_NOT_COMPILED
4380 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004381 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4382 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004383 == FAIL))
4384 {
4385 if (did_emsg_cumul + did_emsg == did_emsg_before)
4386 semsg(_(e_function_is_not_compiled_str),
4387 printable_func_name(ufunc));
4388 return FAIL;
4389 }
4390
4391 {
4392 // Check the function was really compiled.
4393 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4394 + ufunc->uf_dfunc_idx;
4395 if (INSTRUCTIONS(dfunc) == NULL)
4396 {
4397 iemsg("using call_def_function() on not compiled function");
4398 return FAIL;
4399 }
4400 }
4401
4402 // If depth of calling is getting too high, don't execute the function.
4403 orig_funcdepth = funcdepth_get();
4404 if (funcdepth_increment() == FAIL)
4405 return FAIL;
4406
4407 CLEAR_FIELD(ectx);
4408 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4409 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4410 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4411 {
4412 funcdepth_decrement();
4413 return FAIL;
4414 }
4415 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4416 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4417 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004418 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004419
4420 idx = argc - ufunc->uf_args.ga_len;
4421 if (idx > 0 && ufunc->uf_va_name == NULL)
4422 {
4423 if (idx == 1)
4424 emsg(_(e_one_argument_too_many));
4425 else
4426 semsg(_(e_nr_arguments_too_many), idx);
4427 goto failed_early;
4428 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004429 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4430 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004431 {
4432 if (idx == -1)
4433 emsg(_(e_one_argument_too_few));
4434 else
4435 semsg(_(e_nr_arguments_too_few), -idx);
4436 goto failed_early;
4437 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004438
4439 // Put arguments on the stack, but no more than what the function expects.
4440 // A lambda can be called with more arguments than it uses.
4441 for (idx = 0; idx < argc
4442 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4443 ++idx)
4444 {
4445 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4446 && argv[idx].v_type == VAR_SPECIAL
4447 && argv[idx].vval.v_number == VVAL_NONE)
4448 {
4449 // Use the default value.
4450 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4451 }
4452 else
4453 {
4454 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4455 && check_typval_arg_type(
4456 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4457 goto failed_early;
4458 copy_tv(&argv[idx], STACK_TV_BOT(0));
4459 }
4460 ++ectx.ec_stack.ga_len;
4461 }
4462
4463 // Turn varargs into a list. Empty list if no args.
4464 if (ufunc->uf_va_name != NULL)
4465 {
4466 int vararg_count = argc - ufunc->uf_args.ga_len;
4467
4468 if (vararg_count < 0)
4469 vararg_count = 0;
4470 else
4471 argc -= vararg_count;
4472 if (exe_newlist(vararg_count, &ectx) == FAIL)
4473 goto failed_early;
4474
4475 // Check the type of the list items.
4476 tv = STACK_TV_BOT(-1);
4477 if (ufunc->uf_va_type != NULL
4478 && ufunc->uf_va_type != &t_list_any
4479 && ufunc->uf_va_type->tt_member != &t_any
4480 && tv->vval.v_list != NULL)
4481 {
4482 type_T *expected = ufunc->uf_va_type->tt_member;
4483 listitem_T *li = tv->vval.v_list->lv_first;
4484
4485 for (idx = 0; idx < vararg_count; ++idx)
4486 {
4487 if (check_typval_arg_type(expected, &li->li_tv,
4488 argc + idx + 1) == FAIL)
4489 goto failed_early;
4490 li = li->li_next;
4491 }
4492 }
4493
4494 if (defcount > 0)
4495 // Move varargs list to below missing default arguments.
4496 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4497 --ectx.ec_stack.ga_len;
4498 }
4499
4500 // Make space for omitted arguments, will store default value below.
4501 // Any varargs list goes after them.
4502 if (defcount > 0)
4503 for (idx = 0; idx < defcount; ++idx)
4504 {
4505 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4506 ++ectx.ec_stack.ga_len;
4507 }
4508 if (ufunc->uf_va_name != NULL)
4509 ++ectx.ec_stack.ga_len;
4510
4511 // Frame pointer points to just after arguments.
4512 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4513 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4514
4515 {
4516 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4517 + ufunc->uf_dfunc_idx;
4518 ufunc_T *base_ufunc = dfunc->df_ufunc;
4519
4520 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4521 // by copy_func().
4522 if (partial != NULL || base_ufunc->uf_partial != NULL)
4523 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004524 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4525 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004526 goto failed_early;
4527 if (partial != NULL)
4528 {
4529 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4530 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004531 if (current_ectx->ec_outer_ref != NULL
4532 && current_ectx->ec_outer_ref->or_outer != NULL)
4533 ectx.ec_outer_ref->or_outer =
4534 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004535 }
4536 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004537 {
4538 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4539 ++partial->pt_refcount;
4540 ectx.ec_outer_ref->or_partial = partial;
4541 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004542 }
4543 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004544 {
4545 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4546 ++base_ufunc->uf_partial->pt_refcount;
4547 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4548 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004549 }
4550 }
4551
4552 // dummy frame entries
4553 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4554 {
4555 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4556 ++ectx.ec_stack.ga_len;
4557 }
4558
4559 {
4560 // Reserve space for local variables and any closure reference count.
4561 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4562 + ufunc->uf_dfunc_idx;
4563
4564 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4565 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4566 ectx.ec_stack.ga_len += dfunc->df_varcount;
4567 if (dfunc->df_has_closure)
4568 {
4569 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4570 STACK_TV_VAR(idx)->vval.v_number = 0;
4571 ++ectx.ec_stack.ga_len;
4572 }
4573
4574 ectx.ec_instr = INSTRUCTIONS(dfunc);
4575 }
4576
4577 // Following errors are in the function, not the caller.
4578 // Commands behave like vim9script.
4579 estack_push_ufunc(ufunc, 1);
4580 current_sctx = ufunc->uf_script_ctx;
4581 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4582
4583 // Use a specific location for storing error messages to be converted to an
4584 // exception.
4585 saved_msg_list = msg_list;
4586 msg_list = &private_msg_list;
4587
4588 // Do turn errors into exceptions.
4589 suppress_errthrow = FALSE;
4590
4591 // Do not delete the function while executing it.
4592 ++ufunc->uf_calls;
4593
4594 // When ":silent!" was used before calling then we still abort the
4595 // function. If ":silent!" is used in the function then we don't.
4596 emsg_silent_def = emsg_silent;
4597 did_emsg_def = 0;
4598
4599 ectx.ec_where.wt_index = 0;
4600 ectx.ec_where.wt_variable = FALSE;
4601
4602 // Execute the instructions until done.
4603 ret = exec_instructions(&ectx);
4604 if (ret == OK)
4605 {
4606 // function finished, get result from the stack.
4607 if (ufunc->uf_ret_type == &t_void)
4608 {
4609 rettv->v_type = VAR_VOID;
4610 }
4611 else
4612 {
4613 tv = STACK_TV_BOT(-1);
4614 *rettv = *tv;
4615 tv->v_type = VAR_UNKNOWN;
4616 }
4617 }
4618
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004619 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004620 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4621 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004622
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004623 // Deal with any remaining closures, they may be in use somewhere.
4624 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004625 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004626 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004627 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4628 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004629
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004630 estack_pop();
4631 current_sctx = save_current_sctx;
4632
Bram Moolenaarc970e422021-03-17 15:03:04 +01004633 // TODO: when is it safe to delete the function if it is no longer used?
4634 --ufunc->uf_calls;
4635
Bram Moolenaar352134b2020-10-17 22:04:08 +02004636 if (*msg_list != NULL && saved_msg_list != NULL)
4637 {
4638 msglist_T **plist = saved_msg_list;
4639
4640 // Append entries from the current msg_list (uncaught exceptions) to
4641 // the saved msg_list.
4642 while (*plist != NULL)
4643 plist = &(*plist)->next;
4644
4645 *plist = *msg_list;
4646 }
4647 msg_list = saved_msg_list;
4648
Bram Moolenaar4c137212021-04-19 16:48:48 +02004649 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004650 {
4651 cmdmod.cmod_filter_regmatch.regprog = NULL;
4652 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004653 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004654 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004655 emsg_silent_def = save_emsg_silent_def;
4656 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004657
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004658failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004659 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4661 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004662 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004665 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004666 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004667 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004668 if (ectx.ec_outer_ref->or_outer_allocated)
4669 vim_free(ectx.ec_outer_ref->or_outer);
4670 partial_unref(ectx.ec_outer_ref->or_partial);
4671 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004672 }
4673
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004674 // Not sure if this is necessary.
4675 suppress_errthrow = save_suppress_errthrow;
4676
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004677 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004678 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004679 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004680 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 return ret;
4682}
4683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004685 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4686 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4687 * "pfx" is prefixed to every line.
4688 */
4689 static void
4690list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4691{
4692 int line_idx = 0;
4693 int prev_current = 0;
4694 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004695 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004696
4697 for (current = 0; current < instr_count; ++current)
4698 {
4699 isn_T *iptr = &instr[current];
4700 char *line;
4701
4702 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004703 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004704 while (line_idx < iptr->isn_lnum
4705 && line_idx < ufunc->uf_lines.ga_len)
4706 {
4707 if (current > prev_current)
4708 {
4709 msg_puts("\n\n");
4710 prev_current = current;
4711 }
4712 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4713 if (line != NULL)
4714 msg(line);
4715 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004716 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4717 {
4718 int first_def_arg = ufunc->uf_args.ga_len
4719 - ufunc->uf_def_args.ga_len;
4720
4721 if (def_arg_idx > 0)
4722 msg_puts("\n\n");
4723 msg_start();
4724 msg_puts(" ");
4725 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4726 first_def_arg + def_arg_idx]);
4727 msg_puts(" = ");
4728 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4729 msg_clr_eos();
4730 msg_end();
4731 }
4732 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004733
4734 switch (iptr->isn_type)
4735 {
4736 case ISN_EXEC:
4737 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4738 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004739 case ISN_EXEC_SPLIT:
4740 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4741 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004742 case ISN_LEGACY_EVAL:
4743 smsg("%s%4d EVAL legacy %s", pfx, current,
4744 iptr->isn_arg.string);
4745 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004746 case ISN_REDIRSTART:
4747 smsg("%s%4d REDIR", pfx, current);
4748 break;
4749 case ISN_REDIREND:
4750 smsg("%s%4d REDIR END%s", pfx, current,
4751 iptr->isn_arg.number ? " append" : "");
4752 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004753 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004754#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004755 smsg("%s%4d CEXPR pre %s", pfx, current,
4756 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004757#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004758 break;
4759 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004760#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004761 {
4762 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4763
4764 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4765 cexpr_get_auname(cer->cer_cmdidx),
4766 cer->cer_forceit ? "!" : "",
4767 cer->cer_cmdline);
4768 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004769#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004770 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004771 case ISN_INSTR:
4772 {
4773 smsg("%s%4d INSTR", pfx, current);
4774 list_instructions(" ", iptr->isn_arg.instr,
4775 INT_MAX, NULL);
4776 msg(" -------------");
4777 }
4778 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004779 case ISN_SUBSTITUTE:
4780 {
4781 subs_T *subs = &iptr->isn_arg.subs;
4782
4783 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4784 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4785 msg(" -------------");
4786 }
4787 break;
4788 case ISN_EXECCONCAT:
4789 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4790 (varnumber_T)iptr->isn_arg.number);
4791 break;
4792 case ISN_ECHO:
4793 {
4794 echo_T *echo = &iptr->isn_arg.echo;
4795
4796 smsg("%s%4d %s %d", pfx, current,
4797 echo->echo_with_white ? "ECHO" : "ECHON",
4798 echo->echo_count);
4799 }
4800 break;
4801 case ISN_EXECUTE:
4802 smsg("%s%4d EXECUTE %lld", pfx, current,
4803 (varnumber_T)(iptr->isn_arg.number));
4804 break;
4805 case ISN_ECHOMSG:
4806 smsg("%s%4d ECHOMSG %lld", pfx, current,
4807 (varnumber_T)(iptr->isn_arg.number));
4808 break;
4809 case ISN_ECHOERR:
4810 smsg("%s%4d ECHOERR %lld", pfx, current,
4811 (varnumber_T)(iptr->isn_arg.number));
4812 break;
4813 case ISN_LOAD:
4814 {
4815 if (iptr->isn_arg.number < 0)
4816 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4817 (varnumber_T)(iptr->isn_arg.number
4818 + STACK_FRAME_SIZE));
4819 else
4820 smsg("%s%4d LOAD $%lld", pfx, current,
4821 (varnumber_T)(iptr->isn_arg.number));
4822 }
4823 break;
4824 case ISN_LOADOUTER:
4825 {
4826 if (iptr->isn_arg.number < 0)
4827 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4828 iptr->isn_arg.outer.outer_depth,
4829 iptr->isn_arg.outer.outer_idx
4830 + STACK_FRAME_SIZE);
4831 else
4832 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4833 iptr->isn_arg.outer.outer_depth,
4834 iptr->isn_arg.outer.outer_idx);
4835 }
4836 break;
4837 case ISN_LOADV:
4838 smsg("%s%4d LOADV v:%s", pfx, current,
4839 get_vim_var_name(iptr->isn_arg.number));
4840 break;
4841 case ISN_LOADSCRIPT:
4842 {
4843 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4844 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4845 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4846 + sref->sref_idx;
4847
4848 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4849 sv->sv_name,
4850 sref->sref_idx,
4851 si->sn_name);
4852 }
4853 break;
4854 case ISN_LOADS:
4855 {
4856 scriptitem_T *si = SCRIPT_ITEM(
4857 iptr->isn_arg.loadstore.ls_sid);
4858
4859 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4860 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4861 }
4862 break;
4863 case ISN_LOADAUTO:
4864 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4865 break;
4866 case ISN_LOADG:
4867 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4868 break;
4869 case ISN_LOADB:
4870 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4871 break;
4872 case ISN_LOADW:
4873 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4874 break;
4875 case ISN_LOADT:
4876 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4877 break;
4878 case ISN_LOADGDICT:
4879 smsg("%s%4d LOAD g:", pfx, current);
4880 break;
4881 case ISN_LOADBDICT:
4882 smsg("%s%4d LOAD b:", pfx, current);
4883 break;
4884 case ISN_LOADWDICT:
4885 smsg("%s%4d LOAD w:", pfx, current);
4886 break;
4887 case ISN_LOADTDICT:
4888 smsg("%s%4d LOAD t:", pfx, current);
4889 break;
4890 case ISN_LOADOPT:
4891 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4892 break;
4893 case ISN_LOADENV:
4894 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4895 break;
4896 case ISN_LOADREG:
4897 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4898 break;
4899
4900 case ISN_STORE:
4901 if (iptr->isn_arg.number < 0)
4902 smsg("%s%4d STORE arg[%lld]", pfx, current,
4903 iptr->isn_arg.number + STACK_FRAME_SIZE);
4904 else
4905 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4906 break;
4907 case ISN_STOREOUTER:
4908 {
4909 if (iptr->isn_arg.number < 0)
4910 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4911 iptr->isn_arg.outer.outer_depth,
4912 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4913 else
4914 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4915 iptr->isn_arg.outer.outer_depth,
4916 iptr->isn_arg.outer.outer_idx);
4917 }
4918 break;
4919 case ISN_STOREV:
4920 smsg("%s%4d STOREV v:%s", pfx, current,
4921 get_vim_var_name(iptr->isn_arg.number));
4922 break;
4923 case ISN_STOREAUTO:
4924 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4925 break;
4926 case ISN_STOREG:
4927 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4928 break;
4929 case ISN_STOREB:
4930 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4931 break;
4932 case ISN_STOREW:
4933 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4934 break;
4935 case ISN_STORET:
4936 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4937 break;
4938 case ISN_STORES:
4939 {
4940 scriptitem_T *si = SCRIPT_ITEM(
4941 iptr->isn_arg.loadstore.ls_sid);
4942
4943 smsg("%s%4d STORES %s in %s", pfx, current,
4944 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4945 }
4946 break;
4947 case ISN_STORESCRIPT:
4948 {
4949 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4950 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4951 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4952 + sref->sref_idx;
4953
4954 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4955 sv->sv_name,
4956 sref->sref_idx,
4957 si->sn_name);
4958 }
4959 break;
4960 case ISN_STOREOPT:
4961 smsg("%s%4d STOREOPT &%s", pfx, current,
4962 iptr->isn_arg.storeopt.so_name);
4963 break;
4964 case ISN_STOREENV:
4965 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4966 break;
4967 case ISN_STOREREG:
4968 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4969 break;
4970 case ISN_STORENR:
4971 smsg("%s%4d STORE %lld in $%d", pfx, current,
4972 iptr->isn_arg.storenr.stnr_val,
4973 iptr->isn_arg.storenr.stnr_idx);
4974 break;
4975
4976 case ISN_STOREINDEX:
4977 smsg("%s%4d STOREINDEX %s", pfx, current,
4978 vartype_name(iptr->isn_arg.vartype));
4979 break;
4980
4981 case ISN_STORERANGE:
4982 smsg("%s%4d STORERANGE", pfx, current);
4983 break;
4984
4985 // constants
4986 case ISN_PUSHNR:
4987 smsg("%s%4d PUSHNR %lld", pfx, current,
4988 (varnumber_T)(iptr->isn_arg.number));
4989 break;
4990 case ISN_PUSHBOOL:
4991 case ISN_PUSHSPEC:
4992 smsg("%s%4d PUSH %s", pfx, current,
4993 get_var_special_name(iptr->isn_arg.number));
4994 break;
4995 case ISN_PUSHF:
4996#ifdef FEAT_FLOAT
4997 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4998#endif
4999 break;
5000 case ISN_PUSHS:
5001 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5002 break;
5003 case ISN_PUSHBLOB:
5004 {
5005 char_u *r;
5006 char_u numbuf[NUMBUFLEN];
5007 char_u *tofree;
5008
5009 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5010 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5011 vim_free(tofree);
5012 }
5013 break;
5014 case ISN_PUSHFUNC:
5015 {
5016 char *name = (char *)iptr->isn_arg.string;
5017
5018 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5019 name == NULL ? "[none]" : name);
5020 }
5021 break;
5022 case ISN_PUSHCHANNEL:
5023#ifdef FEAT_JOB_CHANNEL
5024 {
5025 channel_T *channel = iptr->isn_arg.channel;
5026
5027 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5028 channel == NULL ? 0 : channel->ch_id);
5029 }
5030#endif
5031 break;
5032 case ISN_PUSHJOB:
5033#ifdef FEAT_JOB_CHANNEL
5034 {
5035 typval_T tv;
5036 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005037 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005038
5039 tv.v_type = VAR_JOB;
5040 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005041 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005042 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5043 }
5044#endif
5045 break;
5046 case ISN_PUSHEXC:
5047 smsg("%s%4d PUSH v:exception", pfx, current);
5048 break;
5049 case ISN_UNLET:
5050 smsg("%s%4d UNLET%s %s", pfx, current,
5051 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5052 iptr->isn_arg.unlet.ul_name);
5053 break;
5054 case ISN_UNLETENV:
5055 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5056 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5057 iptr->isn_arg.unlet.ul_name);
5058 break;
5059 case ISN_UNLETINDEX:
5060 smsg("%s%4d UNLETINDEX", pfx, current);
5061 break;
5062 case ISN_UNLETRANGE:
5063 smsg("%s%4d UNLETRANGE", pfx, current);
5064 break;
5065 case ISN_LOCKCONST:
5066 smsg("%s%4d LOCKCONST", pfx, current);
5067 break;
5068 case ISN_NEWLIST:
5069 smsg("%s%4d NEWLIST size %lld", pfx, current,
5070 (varnumber_T)(iptr->isn_arg.number));
5071 break;
5072 case ISN_NEWDICT:
5073 smsg("%s%4d NEWDICT size %lld", pfx, current,
5074 (varnumber_T)(iptr->isn_arg.number));
5075 break;
5076
5077 // function call
5078 case ISN_BCALL:
5079 {
5080 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5081
5082 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5083 internal_func_name(cbfunc->cbf_idx),
5084 cbfunc->cbf_argcount);
5085 }
5086 break;
5087 case ISN_DCALL:
5088 {
5089 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5090 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5091 + cdfunc->cdf_idx;
5092
5093 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5094 df->df_ufunc->uf_name_exp != NULL
5095 ? df->df_ufunc->uf_name_exp
5096 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5097 }
5098 break;
5099 case ISN_UCALL:
5100 {
5101 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5102
5103 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5104 cufunc->cuf_name, cufunc->cuf_argcount);
5105 }
5106 break;
5107 case ISN_PCALL:
5108 {
5109 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5110
5111 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5112 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5113 }
5114 break;
5115 case ISN_PCALL_END:
5116 smsg("%s%4d PCALL end", pfx, current);
5117 break;
5118 case ISN_RETURN:
5119 smsg("%s%4d RETURN", pfx, current);
5120 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005121 case ISN_RETURN_VOID:
5122 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005123 break;
5124 case ISN_FUNCREF:
5125 {
5126 funcref_T *funcref = &iptr->isn_arg.funcref;
5127 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5128 + funcref->fr_func;
5129
5130 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5131 }
5132 break;
5133
5134 case ISN_NEWFUNC:
5135 {
5136 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5137
5138 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5139 newfunc->nf_lambda, newfunc->nf_global);
5140 }
5141 break;
5142
5143 case ISN_DEF:
5144 {
5145 char_u *name = iptr->isn_arg.string;
5146
5147 smsg("%s%4d DEF %s", pfx, current,
5148 name == NULL ? (char_u *)"" : name);
5149 }
5150 break;
5151
5152 case ISN_JUMP:
5153 {
5154 char *when = "?";
5155
5156 switch (iptr->isn_arg.jump.jump_when)
5157 {
5158 case JUMP_ALWAYS:
5159 when = "JUMP";
5160 break;
5161 case JUMP_AND_KEEP_IF_TRUE:
5162 when = "JUMP_AND_KEEP_IF_TRUE";
5163 break;
5164 case JUMP_IF_FALSE:
5165 when = "JUMP_IF_FALSE";
5166 break;
5167 case JUMP_AND_KEEP_IF_FALSE:
5168 when = "JUMP_AND_KEEP_IF_FALSE";
5169 break;
5170 case JUMP_IF_COND_FALSE:
5171 when = "JUMP_IF_COND_FALSE";
5172 break;
5173 case JUMP_IF_COND_TRUE:
5174 when = "JUMP_IF_COND_TRUE";
5175 break;
5176 }
5177 smsg("%s%4d %s -> %d", pfx, current, when,
5178 iptr->isn_arg.jump.jump_where);
5179 }
5180 break;
5181
5182 case ISN_JUMP_IF_ARG_SET:
5183 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5184 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5185 iptr->isn_arg.jump.jump_where);
5186 break;
5187
5188 case ISN_FOR:
5189 {
5190 forloop_T *forloop = &iptr->isn_arg.forloop;
5191
5192 smsg("%s%4d FOR $%d -> %d", pfx, current,
5193 forloop->for_idx, forloop->for_end);
5194 }
5195 break;
5196
5197 case ISN_TRY:
5198 {
5199 try_T *try = &iptr->isn_arg.try;
5200
5201 if (try->try_ref->try_finally == 0)
5202 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5203 pfx, current,
5204 try->try_ref->try_catch,
5205 try->try_ref->try_endtry);
5206 else
5207 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5208 pfx, current,
5209 try->try_ref->try_catch,
5210 try->try_ref->try_finally,
5211 try->try_ref->try_endtry);
5212 }
5213 break;
5214 case ISN_CATCH:
5215 // TODO
5216 smsg("%s%4d CATCH", pfx, current);
5217 break;
5218 case ISN_TRYCONT:
5219 {
5220 trycont_T *trycont = &iptr->isn_arg.trycont;
5221
5222 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5223 trycont->tct_levels,
5224 trycont->tct_levels == 1 ? "" : "s",
5225 trycont->tct_where);
5226 }
5227 break;
5228 case ISN_FINALLY:
5229 smsg("%s%4d FINALLY", pfx, current);
5230 break;
5231 case ISN_ENDTRY:
5232 smsg("%s%4d ENDTRY", pfx, current);
5233 break;
5234 case ISN_THROW:
5235 smsg("%s%4d THROW", pfx, current);
5236 break;
5237
5238 // expression operations on number
5239 case ISN_OPNR:
5240 case ISN_OPFLOAT:
5241 case ISN_OPANY:
5242 {
5243 char *what;
5244 char *ins;
5245
5246 switch (iptr->isn_arg.op.op_type)
5247 {
5248 case EXPR_MULT: what = "*"; break;
5249 case EXPR_DIV: what = "/"; break;
5250 case EXPR_REM: what = "%"; break;
5251 case EXPR_SUB: what = "-"; break;
5252 case EXPR_ADD: what = "+"; break;
5253 default: what = "???"; break;
5254 }
5255 switch (iptr->isn_type)
5256 {
5257 case ISN_OPNR: ins = "OPNR"; break;
5258 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5259 case ISN_OPANY: ins = "OPANY"; break;
5260 default: ins = "???"; break;
5261 }
5262 smsg("%s%4d %s %s", pfx, current, ins, what);
5263 }
5264 break;
5265
5266 case ISN_COMPAREBOOL:
5267 case ISN_COMPARESPECIAL:
5268 case ISN_COMPARENR:
5269 case ISN_COMPAREFLOAT:
5270 case ISN_COMPARESTRING:
5271 case ISN_COMPAREBLOB:
5272 case ISN_COMPARELIST:
5273 case ISN_COMPAREDICT:
5274 case ISN_COMPAREFUNC:
5275 case ISN_COMPAREANY:
5276 {
5277 char *p;
5278 char buf[10];
5279 char *type;
5280
5281 switch (iptr->isn_arg.op.op_type)
5282 {
5283 case EXPR_EQUAL: p = "=="; break;
5284 case EXPR_NEQUAL: p = "!="; break;
5285 case EXPR_GREATER: p = ">"; break;
5286 case EXPR_GEQUAL: p = ">="; break;
5287 case EXPR_SMALLER: p = "<"; break;
5288 case EXPR_SEQUAL: p = "<="; break;
5289 case EXPR_MATCH: p = "=~"; break;
5290 case EXPR_IS: p = "is"; break;
5291 case EXPR_ISNOT: p = "isnot"; break;
5292 case EXPR_NOMATCH: p = "!~"; break;
5293 default: p = "???"; break;
5294 }
5295 STRCPY(buf, p);
5296 if (iptr->isn_arg.op.op_ic == TRUE)
5297 strcat(buf, "?");
5298 switch(iptr->isn_type)
5299 {
5300 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5301 case ISN_COMPARESPECIAL:
5302 type = "COMPARESPECIAL"; break;
5303 case ISN_COMPARENR: type = "COMPARENR"; break;
5304 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5305 case ISN_COMPARESTRING:
5306 type = "COMPARESTRING"; break;
5307 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5308 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5309 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5310 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5311 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5312 default: type = "???"; break;
5313 }
5314
5315 smsg("%s%4d %s %s", pfx, current, type, buf);
5316 }
5317 break;
5318
5319 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5320 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5321
5322 // expression operations
5323 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5324 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5325 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5326 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5327 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5328 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5329 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5330 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5331 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5332 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5333 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5334 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5335 pfx, current, iptr->isn_arg.number); break;
5336 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5337 pfx, current, iptr->isn_arg.number); break;
5338 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5339 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5340 iptr->isn_arg.string); break;
5341 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5342
5343 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5344 case ISN_CHECKTYPE:
5345 {
5346 checktype_T *ct = &iptr->isn_arg.type;
5347 char *tofree;
5348
5349 if (ct->ct_arg_idx == 0)
5350 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5351 type_name(ct->ct_type, &tofree),
5352 (int)ct->ct_off);
5353 else
5354 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5355 type_name(ct->ct_type, &tofree),
5356 (int)ct->ct_off,
5357 (int)ct->ct_arg_idx);
5358 vim_free(tofree);
5359 break;
5360 }
5361 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5362 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5363 iptr->isn_arg.checklen.cl_min_len);
5364 break;
5365 case ISN_SETTYPE:
5366 {
5367 char *tofree;
5368
5369 smsg("%s%4d SETTYPE %s", pfx, current,
5370 type_name(iptr->isn_arg.type.ct_type, &tofree));
5371 vim_free(tofree);
5372 break;
5373 }
5374 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005375 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5376 smsg("%s%4d INVERT %d (!val)", pfx, current,
5377 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005378 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005379 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5380 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005381 break;
5382 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005383 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005384 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005385 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5386 pfx, current,
5387 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005388 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005389 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5390 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005391 break;
5392 case ISN_PUT:
5393 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5394 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005395 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005396 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5397 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005398 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005399 else
5400 smsg("%s%4d PUT %c %ld", pfx, current,
5401 iptr->isn_arg.put.put_regname,
5402 (long)iptr->isn_arg.put.put_lnum);
5403 break;
5404
5405 // TODO: summarize modifiers
5406 case ISN_CMDMOD:
5407 {
5408 char_u *buf;
5409 size_t len = produce_cmdmods(
5410 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5411
5412 buf = alloc(len + 1);
5413 if (buf != NULL)
5414 {
5415 (void)produce_cmdmods(
5416 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5417 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5418 vim_free(buf);
5419 }
5420 break;
5421 }
5422 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5423
5424 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005425 smsg("%s%4d PROFILE START line %d", pfx, current,
5426 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005427 break;
5428
5429 case ISN_PROF_END:
5430 smsg("%s%4d PROFILE END", pfx, current);
5431 break;
5432
Bram Moolenaare99d4222021-06-13 14:01:26 +02005433 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005434 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5435 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005436 break;
5437
Bram Moolenaar4c137212021-04-19 16:48:48 +02005438 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5439 iptr->isn_arg.unpack.unp_count,
5440 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5441 break;
5442 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5443 iptr->isn_arg.shuffle.shfl_item,
5444 iptr->isn_arg.shuffle.shfl_up);
5445 break;
5446 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5447
5448 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5449 return;
5450 }
5451
5452 out_flush(); // output one line at a time
5453 ui_breakcheck();
5454 if (got_int)
5455 break;
5456 }
5457}
5458
5459/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005460 * Handle command line completion for the :disassemble command.
5461 */
5462 void
5463set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5464{
5465 char_u *p;
5466
5467 // Default: expand user functions, "debug" and "profile"
5468 xp->xp_context = EXPAND_DISASSEMBLE;
5469 xp->xp_pattern = arg;
5470
5471 // first argument already typed: only user function names
5472 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5473 {
5474 xp->xp_context = EXPAND_USER_FUNC;
5475 xp->xp_pattern = skipwhite(p);
5476 }
5477}
5478
5479/*
5480 * Function given to ExpandGeneric() to obtain the list of :disassemble
5481 * arguments.
5482 */
5483 char_u *
5484get_disassemble_argument(expand_T *xp, int idx)
5485{
5486 if (idx == 0)
5487 return (char_u *)"debug";
5488 if (idx == 1)
5489 return (char_u *)"profile";
5490 return get_user_func_name(xp, idx - 2);
5491}
5492
5493/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005494 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005495 * We don't really need this at runtime, but we do have tests that require it,
5496 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 */
5498 void
5499ex_disassemble(exarg_T *eap)
5500{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005501 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005502 char_u *fname;
5503 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005504 dfunc_T *dfunc;
5505 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005506 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005507 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005508 compiletype_T compile_type = CT_NONE;
5509
5510 if (STRNCMP(arg, "profile", 7) == 0)
5511 {
5512 compile_type = CT_PROFILE;
5513 arg = skipwhite(arg + 7);
5514 }
5515 else if (STRNCMP(arg, "debug", 5) == 0)
5516 {
5517 compile_type = CT_DEBUG;
5518 arg = skipwhite(arg + 5);
5519 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005520
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005521 if (STRNCMP(arg, "<lambda>", 8) == 0)
5522 {
5523 arg += 8;
5524 (void)getdigits(&arg);
5525 fname = vim_strnsave(eap->arg, arg - eap->arg);
5526 }
5527 else
5528 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005529 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005530 if (fname == NULL)
5531 {
5532 semsg(_(e_invarg2), eap->arg);
5533 return;
5534 }
5535
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005536 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005537 if (ufunc == NULL)
5538 {
5539 char_u *p = untrans_function_name(fname);
5540
5541 if (p != NULL)
5542 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005543 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005544 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005545 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 if (ufunc == NULL)
5547 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005548 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005549 return;
5550 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005551 if (func_needs_compiling(ufunc, compile_type)
5552 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005553 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005554 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005556 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557 return;
5558 }
5559 if (ufunc->uf_name_exp != NULL)
5560 msg((char *)ufunc->uf_name_exp);
5561 else
5562 msg((char *)ufunc->uf_name);
5563
5564 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005565 switch (compile_type)
5566 {
5567 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005568#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005569 instr = dfunc->df_instr_prof;
5570 instr_count = dfunc->df_instr_prof_count;
5571 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005572#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005573 // FALLTHROUGH
5574 case CT_NONE:
5575 instr = dfunc->df_instr;
5576 instr_count = dfunc->df_instr_count;
5577 break;
5578 case CT_DEBUG:
5579 instr = dfunc->df_instr_debug;
5580 instr_count = dfunc->df_instr_debug_count;
5581 break;
5582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005583
Bram Moolenaar4c137212021-04-19 16:48:48 +02005584 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585}
5586
5587/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005588 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005589 * list, etc. Mostly like what JavaScript does, except that empty list and
5590 * empty dictionary are FALSE.
5591 */
5592 int
5593tv2bool(typval_T *tv)
5594{
5595 switch (tv->v_type)
5596 {
5597 case VAR_NUMBER:
5598 return tv->vval.v_number != 0;
5599 case VAR_FLOAT:
5600#ifdef FEAT_FLOAT
5601 return tv->vval.v_float != 0.0;
5602#else
5603 break;
5604#endif
5605 case VAR_PARTIAL:
5606 return tv->vval.v_partial != NULL;
5607 case VAR_FUNC:
5608 case VAR_STRING:
5609 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5610 case VAR_LIST:
5611 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5612 case VAR_DICT:
5613 return tv->vval.v_dict != NULL
5614 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5615 case VAR_BOOL:
5616 case VAR_SPECIAL:
5617 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5618 case VAR_JOB:
5619#ifdef FEAT_JOB_CHANNEL
5620 return tv->vval.v_job != NULL;
5621#else
5622 break;
5623#endif
5624 case VAR_CHANNEL:
5625#ifdef FEAT_JOB_CHANNEL
5626 return tv->vval.v_channel != NULL;
5627#else
5628 break;
5629#endif
5630 case VAR_BLOB:
5631 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5632 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005633 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005635 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636 break;
5637 }
5638 return FALSE;
5639}
5640
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005641 void
5642emsg_using_string_as(typval_T *tv, int as_number)
5643{
5644 semsg(_(as_number ? e_using_string_as_number_str
5645 : e_using_string_as_bool_str),
5646 tv->vval.v_string == NULL
5647 ? (char_u *)"" : tv->vval.v_string);
5648}
5649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650/*
5651 * If "tv" is a string give an error and return FAIL.
5652 */
5653 int
5654check_not_string(typval_T *tv)
5655{
5656 if (tv->v_type == VAR_STRING)
5657 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005658 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659 clear_tv(tv);
5660 return FAIL;
5661 }
5662 return OK;
5663}
5664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005665
5666#endif // FEAT_EVAL