blob: 33e92442451db53e42c2de0e0c05c7146a9a94cd [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];
1472 if (line == NULL)
1473 line = (char_u *)"[empty]";
1474
1475 do_debug(line);
1476 debug_context = NULL;
1477
1478 if (end_lnum > iptr->isn_lnum)
1479 vim_free(line);
1480}
1481
Bram Moolenaar4c137212021-04-19 16:48:48 +02001482/*
1483 * Execute instructions in execution context "ectx".
1484 * Return OK or FAIL;
1485 */
1486 static int
1487exec_instructions(ectx_T *ectx)
1488{
1489 int breakcheck_count = 0;
1490 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001491 int ret = FAIL;
1492 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001493
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001494 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001495 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001496
Bram Moolenaarff652882021-05-16 15:24:49 +02001497 // Only catch exceptions in this instruction list.
1498 ectx->ec_trylevel_at_start = trylevel;
1499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 for (;;)
1501 {
1502 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001503
Bram Moolenaar270d0382020-05-15 21:42:53 +02001504 if (++breakcheck_count >= 100)
1505 {
1506 line_breakcheck();
1507 breakcheck_count = 0;
1508 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001509 if (got_int)
1510 {
1511 // Turn CTRL-C into an exception.
1512 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001513 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001514 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001515 did_throw = TRUE;
1516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517
Bram Moolenaara26b9702020-04-18 19:53:28 +02001518 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1519 {
1520 // Turn an error message into an exception.
1521 did_emsg = FALSE;
1522 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001523 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001524 did_throw = TRUE;
1525 *msg_list = NULL;
1526 }
1527
Bram Moolenaar4c137212021-04-19 16:48:48 +02001528 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001530 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001531 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532
1533 // An exception jumps to the first catch, finally, or returns from
1534 // the current function.
1535 if (trystack->ga_len > 0)
1536 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001537 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 {
1539 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001540 ectx->ec_in_catch = TRUE;
1541 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542 }
1543 else
1544 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001545 // Not inside try or need to return from current functions.
1546 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001547 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001548 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001549 tv = STACK_TV_BOT(0);
1550 tv->v_type = VAR_NUMBER;
1551 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001552 ++ectx->ec_stack.ga_len;
1553 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001555 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001556 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001557 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001558 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 goto done;
1560 }
1561
Bram Moolenaar4c137212021-04-19 16:48:48 +02001562 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001563 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 }
1565 continue;
1566 }
1567
Bram Moolenaar4c137212021-04-19 16:48:48 +02001568 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 switch (iptr->isn_type)
1570 {
1571 // execute Ex command line
1572 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001573 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001574 source_cookie_T cookie;
1575
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001576 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001577 // Pass getsourceline to get an error for a missing ":end"
1578 // command.
1579 CLEAR_FIELD(cookie);
1580 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1581 if (do_cmdline(iptr->isn_arg.string,
1582 getsourceline, &cookie,
1583 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1584 == FAIL
1585 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001586 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 break;
1589
Bram Moolenaar20677332021-06-06 17:02:53 +02001590 // execute Ex command line split at NL characters.
1591 case ISN_EXEC_SPLIT:
1592 {
1593 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001594 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001595
1596 SOURCING_LNUM = iptr->isn_lnum;
1597 CLEAR_FIELD(cookie);
1598 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1599 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001600 line = get_split_sourceline(0, &cookie, 0, 0);
1601 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001602 get_split_sourceline, &cookie,
1603 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1604 == FAIL
1605 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001606 {
1607 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001608 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001609 }
1610 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001611 }
1612 break;
1613
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001614 // Evaluate an expression with legacy syntax, push it onto the
1615 // stack.
1616 case ISN_LEGACY_EVAL:
1617 {
1618 char_u *arg = iptr->isn_arg.string;
1619 int res;
1620 int save_flags = cmdmod.cmod_flags;
1621
1622 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001623 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001624 tv = STACK_TV_BOT(0);
1625 init_tv(tv);
1626 cmdmod.cmod_flags |= CMOD_LEGACY;
1627 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1628 cmdmod.cmod_flags = save_flags;
1629 if (res == FAIL)
1630 goto on_error;
1631 ++ectx->ec_stack.ga_len;
1632 }
1633 break;
1634
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001635 // push typeval VAR_INSTR with instructions to be executed
1636 case ISN_INSTR:
1637 {
1638 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001639 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001640 tv = STACK_TV_BOT(0);
1641 tv->vval.v_instr = ALLOC_ONE(instr_T);
1642 if (tv->vval.v_instr == NULL)
1643 goto on_error;
1644 ++ectx->ec_stack.ga_len;
1645
1646 tv->v_type = VAR_INSTR;
1647 tv->vval.v_instr->instr_ectx = ectx;
1648 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1649 }
1650 break;
1651
Bram Moolenaar4c137212021-04-19 16:48:48 +02001652 // execute :substitute with an expression
1653 case ISN_SUBSTITUTE:
1654 {
1655 subs_T *subs = &iptr->isn_arg.subs;
1656 source_cookie_T cookie;
1657 struct subs_expr_S *save_instr = substitute_instr;
1658 struct subs_expr_S subs_instr;
1659 int res;
1660
1661 subs_instr.subs_ectx = ectx;
1662 subs_instr.subs_instr = subs->subs_instr;
1663 subs_instr.subs_status = OK;
1664 substitute_instr = &subs_instr;
1665
1666 SOURCING_LNUM = iptr->isn_lnum;
1667 // This is very much like ISN_EXEC
1668 CLEAR_FIELD(cookie);
1669 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1670 res = do_cmdline(subs->subs_cmd,
1671 getsourceline, &cookie,
1672 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1673 substitute_instr = save_instr;
1674
1675 if (res == FAIL || did_emsg
1676 || subs_instr.subs_status == FAIL)
1677 goto on_error;
1678 }
1679 break;
1680
1681 case ISN_FINISH:
1682 goto done;
1683
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001684 case ISN_REDIRSTART:
1685 // create a dummy entry for var_redir_str()
1686 if (alloc_redir_lval() == FAIL)
1687 goto on_error;
1688
1689 // The output is stored in growarray "redir_ga" until
1690 // redirection ends.
1691 init_redir_ga();
1692 redir_vname = 1;
1693 break;
1694
1695 case ISN_REDIREND:
1696 {
1697 char_u *res = get_clear_redir_ga();
1698
1699 // End redirection, put redirected text on the stack.
1700 clear_redir_lval();
1701 redir_vname = 0;
1702
1703 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1704 {
1705 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001706 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001707 }
1708 tv = STACK_TV_BOT(0);
1709 tv->v_type = VAR_STRING;
1710 tv->vval.v_string = res;
1711 ++ectx->ec_stack.ga_len;
1712 }
1713 break;
1714
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001715 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001716#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001717 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1718 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001719#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001720 break;
1721
1722 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001723#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001724 {
1725 exarg_T ea;
1726 int res;
1727
1728 CLEAR_FIELD(ea);
1729 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1730 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1731 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1732 --ectx->ec_stack.ga_len;
1733 tv = STACK_TV_BOT(0);
1734 res = cexpr_core(&ea, tv);
1735 clear_tv(tv);
1736 if (res == FAIL)
1737 goto on_error;
1738 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001739#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001740 break;
1741
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001742 // execute Ex command from pieces on the stack
1743 case ISN_EXECCONCAT:
1744 {
1745 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001746 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001747 int pass;
1748 int i;
1749 char_u *cmd = NULL;
1750 char_u *str;
1751
1752 for (pass = 1; pass <= 2; ++pass)
1753 {
1754 for (i = 0; i < count; ++i)
1755 {
1756 tv = STACK_TV_BOT(i - count);
1757 str = tv->vval.v_string;
1758 if (str != NULL && *str != NUL)
1759 {
1760 if (pass == 2)
1761 STRCPY(cmd + len, str);
1762 len += STRLEN(str);
1763 }
1764 if (pass == 2)
1765 clear_tv(tv);
1766 }
1767 if (pass == 1)
1768 {
1769 cmd = alloc(len + 1);
1770 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001771 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001772 len = 0;
1773 }
1774 }
1775
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001777 do_cmdline_cmd(cmd);
1778 vim_free(cmd);
1779 }
1780 break;
1781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 // execute :echo {string} ...
1783 case ISN_ECHO:
1784 {
1785 int count = iptr->isn_arg.echo.echo_count;
1786 int atstart = TRUE;
1787 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001788 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789
1790 for (idx = 0; idx < count; ++idx)
1791 {
1792 tv = STACK_TV_BOT(idx - count);
1793 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1794 &atstart, &needclr);
1795 clear_tv(tv);
1796 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001797 if (needclr)
1798 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001799 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 }
1801 break;
1802
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001803 // :execute {string} ...
1804 // :echomsg {string} ...
1805 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001806 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001807 case ISN_ECHOMSG:
1808 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001809 {
1810 int count = iptr->isn_arg.number;
1811 garray_T ga;
1812 char_u buf[NUMBUFLEN];
1813 char_u *p;
1814 int len;
1815 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001816 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001817
1818 ga_init2(&ga, 1, 80);
1819 for (idx = 0; idx < count; ++idx)
1820 {
1821 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001822 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001823 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001824 if (tv->v_type == VAR_CHANNEL
1825 || tv->v_type == VAR_JOB)
1826 {
1827 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001828 semsg(_(e_using_invalid_value_as_string_str),
1829 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001830 break;
1831 }
1832 else
1833 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001834 }
1835 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001836 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001837
1838 len = (int)STRLEN(p);
1839 if (ga_grow(&ga, len + 2) == FAIL)
1840 failed = TRUE;
1841 else
1842 {
1843 if (ga.ga_len > 0)
1844 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1845 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1846 ga.ga_len += len;
1847 }
1848 clear_tv(tv);
1849 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001850 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001851 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001852 {
1853 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001854 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001855 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001856
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001857 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001858 {
1859 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001860 {
1861 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001862 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001863 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001864 {
1865 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001866 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001867 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001868 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001869 else
1870 {
1871 msg_sb_eol();
1872 if (iptr->isn_type == ISN_ECHOMSG)
1873 {
1874 msg_attr(ga.ga_data, echo_attr);
1875 out_flush();
1876 }
1877 else
1878 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001879 SOURCING_LNUM = iptr->isn_lnum;
1880 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001881 }
1882 }
1883 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001884 ga_clear(&ga);
1885 }
1886 break;
1887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 // load local variable or argument
1889 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001890 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001891 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001893 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 break;
1895
1896 // load v: variable
1897 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001898 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001899 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001901 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 break;
1903
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001904 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 case ISN_LOADSCRIPT:
1906 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001907 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 svar_T *sv;
1909
Bram Moolenaar4c137212021-04-19 16:48:48 +02001910 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001911 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001912 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001913 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001914 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001915 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001917 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 }
1919 break;
1920
1921 // load s: variable in old script
1922 case ISN_LOADS:
1923 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001924 hashtab_T *ht = &SCRIPT_VARS(
1925 iptr->isn_arg.loadstore.ls_sid);
1926 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 if (di == NULL)
1930 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001931 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001932 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001933 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 }
1935 else
1936 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001937 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001938 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001940 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 }
1942 }
1943 break;
1944
Bram Moolenaard3aac292020-04-19 14:32:17 +02001945 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001947 case ISN_LOADB:
1948 case ISN_LOADW:
1949 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001951 dictitem_T *di = NULL;
1952 hashtab_T *ht = NULL;
1953 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001954
Bram Moolenaard3aac292020-04-19 14:32:17 +02001955 switch (iptr->isn_type)
1956 {
1957 case ISN_LOADG:
1958 ht = get_globvar_ht();
1959 namespace = 'g';
1960 break;
1961 case ISN_LOADB:
1962 ht = &curbuf->b_vars->dv_hashtab;
1963 namespace = 'b';
1964 break;
1965 case ISN_LOADW:
1966 ht = &curwin->w_vars->dv_hashtab;
1967 namespace = 'w';
1968 break;
1969 case ISN_LOADT:
1970 ht = &curtab->tp_vars->dv_hashtab;
1971 namespace = 't';
1972 break;
1973 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001974 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001975 }
1976 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 if (di == NULL)
1979 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001980 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001981 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001982 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001983 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 }
1985 else
1986 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001987 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001988 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001990 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 }
1992 }
1993 break;
1994
Bram Moolenaar03290b82020-12-19 16:30:44 +01001995 // load autoload variable
1996 case ISN_LOADAUTO:
1997 {
1998 char_u *name = iptr->isn_arg.string;
1999
Bram Moolenaar4c137212021-04-19 16:48:48 +02002000 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002001 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002002 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002003 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002004 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002005 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002006 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002007 }
2008 break;
2009
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002010 // load g:/b:/w:/t: namespace
2011 case ISN_LOADGDICT:
2012 case ISN_LOADBDICT:
2013 case ISN_LOADWDICT:
2014 case ISN_LOADTDICT:
2015 {
2016 dict_T *d = NULL;
2017
2018 switch (iptr->isn_type)
2019 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002020 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2021 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2022 case ISN_LOADWDICT: d = curwin->w_vars; break;
2023 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002024 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002025 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002026 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002027 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002028 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002029 tv = STACK_TV_BOT(0);
2030 tv->v_type = VAR_DICT;
2031 tv->v_lock = 0;
2032 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002033 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002034 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002035 }
2036 break;
2037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 // load &option
2039 case ISN_LOADOPT:
2040 {
2041 typval_T optval;
2042 char_u *name = iptr->isn_arg.string;
2043
Bram Moolenaara8c17702020-04-01 21:17:24 +02002044 // This is not expected to fail, name is checked during
2045 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002046 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002047 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002048 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002049 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002051 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 }
2053 break;
2054
2055 // load $ENV
2056 case ISN_LOADENV:
2057 {
2058 typval_T optval;
2059 char_u *name = iptr->isn_arg.string;
2060
Bram Moolenaar4c137212021-04-19 16:48:48 +02002061 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002062 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002063 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002064 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002066 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 }
2068 break;
2069
2070 // load @register
2071 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002072 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002073 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 tv = STACK_TV_BOT(0);
2075 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002076 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002077 // This may result in NULL, which should be equivalent to an
2078 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 tv->vval.v_string = get_reg_contents(
2080 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002081 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 break;
2083
2084 // store local variable
2085 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002086 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 tv = STACK_TV_VAR(iptr->isn_arg.number);
2088 clear_tv(tv);
2089 *tv = *STACK_TV_BOT(0);
2090 break;
2091
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002092 // store s: variable in old script
2093 case ISN_STORES:
2094 {
2095 hashtab_T *ht = &SCRIPT_VARS(
2096 iptr->isn_arg.loadstore.ls_sid);
2097 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002098 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002099
Bram Moolenaar4c137212021-04-19 16:48:48 +02002100 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002101 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002102 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002103 else
2104 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002105 SOURCING_LNUM = iptr->isn_lnum;
2106 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002107 {
2108 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002109 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002110 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002111 clear_tv(&di->di_tv);
2112 di->di_tv = *STACK_TV_BOT(0);
2113 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002114 }
2115 break;
2116
2117 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 case ISN_STORESCRIPT:
2119 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002120 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2121 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122
Bram Moolenaar4c137212021-04-19 16:48:48 +02002123 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002124 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002125 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002126 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002127
2128 // "const" and "final" are checked at compile time, locking
2129 // the value needs to be checked here.
2130 SOURCING_LNUM = iptr->isn_lnum;
2131 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002132 {
2133 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002134 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002135 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 clear_tv(sv->sv_tv);
2138 *sv->sv_tv = *STACK_TV_BOT(0);
2139 }
2140 break;
2141
2142 // store option
2143 case ISN_STOREOPT:
2144 {
2145 long n = 0;
2146 char_u *s = NULL;
2147 char *msg;
2148
Bram Moolenaar4c137212021-04-19 16:48:48 +02002149 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 tv = STACK_TV_BOT(0);
2151 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002152 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002154 if (s == NULL)
2155 s = (char_u *)"";
2156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002158 // must be VAR_NUMBER, CHECKTYPE makes sure
2159 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2161 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002162 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 if (msg != NULL)
2164 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002165 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002167 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 }
2170 break;
2171
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002172 // store $ENV
2173 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002174 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002175 tv = STACK_TV_BOT(0);
2176 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2177 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002178 break;
2179
2180 // store @r
2181 case ISN_STOREREG:
2182 {
2183 int reg = iptr->isn_arg.number;
2184
Bram Moolenaar4c137212021-04-19 16:48:48 +02002185 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002186 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002187 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002188 tv_get_string(tv), -1, FALSE);
2189 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002190 }
2191 break;
2192
2193 // store v: variable
2194 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002195 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002196 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2197 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002198 // should not happen, type is checked when compiling
2199 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002200 break;
2201
Bram Moolenaard3aac292020-04-19 14:32:17 +02002202 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002204 case ISN_STOREB:
2205 case ISN_STOREW:
2206 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002208 dictitem_T *di;
2209 hashtab_T *ht;
2210 char_u *name = iptr->isn_arg.string + 2;
2211
Bram Moolenaard3aac292020-04-19 14:32:17 +02002212 switch (iptr->isn_type)
2213 {
2214 case ISN_STOREG:
2215 ht = get_globvar_ht();
2216 break;
2217 case ISN_STOREB:
2218 ht = &curbuf->b_vars->dv_hashtab;
2219 break;
2220 case ISN_STOREW:
2221 ht = &curwin->w_vars->dv_hashtab;
2222 break;
2223 case ISN_STORET:
2224 ht = &curtab->tp_vars->dv_hashtab;
2225 break;
2226 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002227 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229
Bram Moolenaar4c137212021-04-19 16:48:48 +02002230 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002231 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002233 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 else
2235 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002236 SOURCING_LNUM = iptr->isn_lnum;
2237 if (var_check_permission(di, name) == FAIL)
2238 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 clear_tv(&di->di_tv);
2240 di->di_tv = *STACK_TV_BOT(0);
2241 }
2242 }
2243 break;
2244
Bram Moolenaar03290b82020-12-19 16:30:44 +01002245 // store an autoload variable
2246 case ISN_STOREAUTO:
2247 SOURCING_LNUM = iptr->isn_lnum;
2248 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2249 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002250 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002251 break;
2252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 // store number in local variable
2254 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002255 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 clear_tv(tv);
2257 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002258 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 break;
2260
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002261 // store value in list or dict variable
2262 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002263 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002264 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002265 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002266 typval_T *tv_dest = STACK_TV_BOT(-1);
2267 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002268
Bram Moolenaar752fc692021-01-04 21:57:11 +01002269 // Stack contains:
2270 // -3 value to be stored
2271 // -2 index
2272 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002273 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002274 SOURCING_LNUM = iptr->isn_lnum;
2275 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002276 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002277 dest_type = tv_dest->v_type;
2278 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002279 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002280 else if (dest_type == VAR_LIST
2281 && tv_idx->v_type != VAR_NUMBER)
2282 {
2283 emsg(_(e_number_exp));
2284 status = FAIL;
2285 }
2286 }
2287 else if (dest_type != tv_dest->v_type)
2288 {
2289 // just in case, should be OK
2290 semsg(_(e_expected_str_but_got_str),
2291 vartype_name(dest_type),
2292 vartype_name(tv_dest->v_type));
2293 status = FAIL;
2294 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002295
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002296 if (status == OK && dest_type == VAR_LIST)
2297 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002298 long lidx = (long)tv_idx->vval.v_number;
2299 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002300
2301 if (list == NULL)
2302 {
2303 emsg(_(e_list_not_set));
2304 goto on_error;
2305 }
2306 if (lidx < 0 && list->lv_len + lidx >= 0)
2307 // negative index is relative to the end
2308 lidx = list->lv_len + lidx;
2309 if (lidx < 0 || lidx > list->lv_len)
2310 {
2311 semsg(_(e_listidx), lidx);
2312 goto on_error;
2313 }
2314 if (lidx < list->lv_len)
2315 {
2316 listitem_T *li = list_find(list, lidx);
2317
2318 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002319 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002320 goto on_error;
2321 // overwrite existing list item
2322 clear_tv(&li->li_tv);
2323 li->li_tv = *tv;
2324 }
2325 else
2326 {
2327 if (error_if_locked(list->lv_lock,
2328 e_cannot_change_list))
2329 goto on_error;
2330 // append to list, only fails when out of memory
2331 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002332 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002333 clear_tv(tv);
2334 }
2335 }
2336 else if (status == OK && dest_type == VAR_DICT)
2337 {
2338 char_u *key = tv_idx->vval.v_string;
2339 dict_T *dict = tv_dest->vval.v_dict;
2340 dictitem_T *di;
2341
2342 SOURCING_LNUM = iptr->isn_lnum;
2343 if (dict == NULL)
2344 {
2345 emsg(_(e_dictionary_not_set));
2346 goto on_error;
2347 }
2348 if (key == NULL)
2349 key = (char_u *)"";
2350 di = dict_find(dict, key, -1);
2351 if (di != NULL)
2352 {
2353 if (error_if_locked(di->di_tv.v_lock,
2354 e_cannot_change_dict_item))
2355 goto on_error;
2356 // overwrite existing value
2357 clear_tv(&di->di_tv);
2358 di->di_tv = *tv;
2359 }
2360 else
2361 {
2362 if (error_if_locked(dict->dv_lock,
2363 e_cannot_change_dict))
2364 goto on_error;
2365 // add to dict, only fails when out of memory
2366 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002367 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002368 clear_tv(tv);
2369 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002370 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002371 else if (status == OK && dest_type == VAR_BLOB)
2372 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002373 long lidx = (long)tv_idx->vval.v_number;
2374 blob_T *blob = tv_dest->vval.v_blob;
2375 varnumber_T nr;
2376 int error = FALSE;
2377 int len;
2378
2379 if (blob == NULL)
2380 {
2381 emsg(_(e_blob_not_set));
2382 goto on_error;
2383 }
2384 len = blob_len(blob);
2385 if (lidx < 0 && len + lidx >= 0)
2386 // negative index is relative to the end
2387 lidx = len + lidx;
2388
2389 // Can add one byte at the end.
2390 if (lidx < 0 || lidx > len)
2391 {
2392 semsg(_(e_blobidx), lidx);
2393 goto on_error;
2394 }
2395 if (value_check_lock(blob->bv_lock,
2396 (char_u *)"blob", FALSE))
2397 goto on_error;
2398 nr = tv_get_number_chk(tv, &error);
2399 if (error)
2400 goto on_error;
2401 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002402 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002403 else
2404 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002405 status = FAIL;
2406 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002407 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002408
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002409 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002410 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002411 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002412 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002413 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002414 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002415 goto on_error;
2416 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002417 }
2418 break;
2419
Bram Moolenaar68452172021-04-12 21:21:02 +02002420 // store value in blob range
2421 case ISN_STORERANGE:
2422 {
2423 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2424 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2425 typval_T *tv_dest = STACK_TV_BOT(-1);
2426 int status = OK;
2427
2428 // Stack contains:
2429 // -4 value to be stored
2430 // -3 first index or "none"
2431 // -2 second index or "none"
2432 // -1 destination blob
2433 tv = STACK_TV_BOT(-4);
2434 if (tv_dest->v_type != VAR_BLOB)
2435 {
2436 status = FAIL;
2437 emsg(_(e_blob_required));
2438 }
2439 else
2440 {
2441 varnumber_T n1;
2442 varnumber_T n2;
2443 int error = FALSE;
2444
2445 n1 = tv_get_number_chk(tv_idx1, &error);
2446 if (error)
2447 status = FAIL;
2448 else
2449 {
2450 if (tv_idx2->v_type == VAR_SPECIAL
2451 && tv_idx2->vval.v_number == VVAL_NONE)
2452 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2453 else
2454 n2 = tv_get_number_chk(tv_idx2, &error);
2455 if (error)
2456 status = FAIL;
2457 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002458 {
2459 long bloblen = blob_len(tv_dest->vval.v_blob);
2460
2461 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002462 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002463 || check_blob_range(bloblen,
2464 n1, n2, FALSE) == FAIL)
2465 status = FAIL;
2466 else
2467 status = blob_set_range(
2468 tv_dest->vval.v_blob, n1, n2, tv);
2469 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002470 }
2471 }
2472
2473 clear_tv(tv_idx1);
2474 clear_tv(tv_idx2);
2475 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002476 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002477 clear_tv(tv);
2478
2479 if (status == FAIL)
2480 goto on_error;
2481 }
2482 break;
2483
Bram Moolenaar0186e582021-01-10 18:33:11 +01002484 // load or store variable or argument from outer scope
2485 case ISN_LOADOUTER:
2486 case ISN_STOREOUTER:
2487 {
2488 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002489 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2490 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002491
2492 while (depth > 1 && outer != NULL)
2493 {
2494 outer = outer->out_up;
2495 --depth;
2496 }
2497 if (outer == NULL)
2498 {
2499 SOURCING_LNUM = iptr->isn_lnum;
2500 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002501 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002502 }
2503 tv = ((typval_T *)outer->out_stack->ga_data)
2504 + outer->out_frame_idx + STACK_FRAME_SIZE
2505 + iptr->isn_arg.outer.outer_idx;
2506 if (iptr->isn_type == ISN_LOADOUTER)
2507 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002508 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002509 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002510 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002511 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002512 }
2513 else
2514 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002515 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002516 clear_tv(tv);
2517 *tv = *STACK_TV_BOT(0);
2518 }
2519 }
2520 break;
2521
Bram Moolenaar752fc692021-01-04 21:57:11 +01002522 // unlet item in list or dict variable
2523 case ISN_UNLETINDEX:
2524 {
2525 typval_T *tv_idx = STACK_TV_BOT(-2);
2526 typval_T *tv_dest = STACK_TV_BOT(-1);
2527 int status = OK;
2528
2529 // Stack contains:
2530 // -2 index
2531 // -1 dict or list
2532 if (tv_dest->v_type == VAR_DICT)
2533 {
2534 // unlet a dict item, index must be a string
2535 if (tv_idx->v_type != VAR_STRING)
2536 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002537 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002538 semsg(_(e_expected_str_but_got_str),
2539 vartype_name(VAR_STRING),
2540 vartype_name(tv_idx->v_type));
2541 status = FAIL;
2542 }
2543 else
2544 {
2545 dict_T *d = tv_dest->vval.v_dict;
2546 char_u *key = tv_idx->vval.v_string;
2547 dictitem_T *di = NULL;
2548
2549 if (key == NULL)
2550 key = (char_u *)"";
2551 if (d != NULL)
2552 di = dict_find(d, key, (int)STRLEN(key));
2553 if (di == NULL)
2554 {
2555 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002556 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002557 semsg(_(e_dictkey), key);
2558 status = FAIL;
2559 }
2560 else
2561 {
2562 // TODO: check for dict or item locked
2563 dictitem_remove(d, di);
2564 }
2565 }
2566 }
2567 else if (tv_dest->v_type == VAR_LIST)
2568 {
2569 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002570 SOURCING_LNUM = iptr->isn_lnum;
2571 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002572 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002573 status = FAIL;
2574 }
2575 else
2576 {
2577 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002578 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002579 listitem_T *li = NULL;
2580
2581 li = list_find(l, n);
2582 if (li == NULL)
2583 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002584 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002585 semsg(_(e_listidx), n);
2586 status = FAIL;
2587 }
2588 else
2589 // TODO: check for list or item locked
2590 listitem_remove(l, li);
2591 }
2592 }
2593 else
2594 {
2595 status = FAIL;
2596 semsg(_(e_cannot_index_str),
2597 vartype_name(tv_dest->v_type));
2598 }
2599
2600 clear_tv(tv_idx);
2601 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002602 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002603 if (status == FAIL)
2604 goto on_error;
2605 }
2606 break;
2607
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002608 // unlet range of items in list variable
2609 case ISN_UNLETRANGE:
2610 {
2611 // Stack contains:
2612 // -3 index1
2613 // -2 index2
2614 // -1 dict or list
2615 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2616 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2617 typval_T *tv_dest = STACK_TV_BOT(-1);
2618 int status = OK;
2619
2620 if (tv_dest->v_type == VAR_LIST)
2621 {
2622 // indexes must be a number
2623 SOURCING_LNUM = iptr->isn_lnum;
2624 if (check_for_number(tv_idx1) == FAIL
2625 || check_for_number(tv_idx2) == FAIL)
2626 {
2627 status = FAIL;
2628 }
2629 else
2630 {
2631 list_T *l = tv_dest->vval.v_list;
2632 long n1 = (long)tv_idx1->vval.v_number;
2633 long n2 = (long)tv_idx2->vval.v_number;
2634 listitem_T *li;
2635
2636 li = list_find_index(l, &n1);
2637 if (li == NULL
2638 || list_unlet_range(l, li, NULL, n1,
2639 TRUE, n2) == FAIL)
2640 status = FAIL;
2641 }
2642 }
2643 else
2644 {
2645 status = FAIL;
2646 SOURCING_LNUM = iptr->isn_lnum;
2647 semsg(_(e_cannot_index_str),
2648 vartype_name(tv_dest->v_type));
2649 }
2650
2651 clear_tv(tv_idx1);
2652 clear_tv(tv_idx2);
2653 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002654 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002655 if (status == FAIL)
2656 goto on_error;
2657 }
2658 break;
2659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 // push constant
2661 case ISN_PUSHNR:
2662 case ISN_PUSHBOOL:
2663 case ISN_PUSHSPEC:
2664 case ISN_PUSHF:
2665 case ISN_PUSHS:
2666 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002667 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002668 case ISN_PUSHCHANNEL:
2669 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002670 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002671 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002673 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002674 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 switch (iptr->isn_type)
2676 {
2677 case ISN_PUSHNR:
2678 tv->v_type = VAR_NUMBER;
2679 tv->vval.v_number = iptr->isn_arg.number;
2680 break;
2681 case ISN_PUSHBOOL:
2682 tv->v_type = VAR_BOOL;
2683 tv->vval.v_number = iptr->isn_arg.number;
2684 break;
2685 case ISN_PUSHSPEC:
2686 tv->v_type = VAR_SPECIAL;
2687 tv->vval.v_number = iptr->isn_arg.number;
2688 break;
2689#ifdef FEAT_FLOAT
2690 case ISN_PUSHF:
2691 tv->v_type = VAR_FLOAT;
2692 tv->vval.v_float = iptr->isn_arg.fnumber;
2693 break;
2694#endif
2695 case ISN_PUSHBLOB:
2696 blob_copy(iptr->isn_arg.blob, tv);
2697 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002698 case ISN_PUSHFUNC:
2699 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002700 if (iptr->isn_arg.string == NULL)
2701 tv->vval.v_string = NULL;
2702 else
2703 tv->vval.v_string =
2704 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002705 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002706 case ISN_PUSHCHANNEL:
2707#ifdef FEAT_JOB_CHANNEL
2708 tv->v_type = VAR_CHANNEL;
2709 tv->vval.v_channel = iptr->isn_arg.channel;
2710 if (tv->vval.v_channel != NULL)
2711 ++tv->vval.v_channel->ch_refcount;
2712#endif
2713 break;
2714 case ISN_PUSHJOB:
2715#ifdef FEAT_JOB_CHANNEL
2716 tv->v_type = VAR_JOB;
2717 tv->vval.v_job = iptr->isn_arg.job;
2718 if (tv->vval.v_job != NULL)
2719 ++tv->vval.v_job->jv_refcount;
2720#endif
2721 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 default:
2723 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002724 tv->vval.v_string = vim_strsave(
2725 iptr->isn_arg.string == NULL
2726 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 }
2728 break;
2729
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002730 case ISN_UNLET:
2731 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2732 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002733 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002734 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002735 case ISN_UNLETENV:
2736 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2737 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002738
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002739 case ISN_LOCKCONST:
2740 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2741 break;
2742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 // create a list from items on the stack; uses a single allocation
2744 // for the list header and the items
2745 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002746 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002747 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 break;
2749
2750 // create a dict from items on the stack
2751 case ISN_NEWDICT:
2752 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002753 int count = iptr->isn_arg.number;
2754 dict_T *dict = dict_alloc();
2755 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002756 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002757 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758
2759 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002760 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 for (idx = 0; idx < count; ++idx)
2762 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002763 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002765 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002766 key = tv->vval.v_string == NULL
2767 ? (char_u *)"" : tv->vval.v_string;
2768 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002769 if (item != NULL)
2770 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002771 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002772 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002773 dict_unref(dict);
2774 goto on_error;
2775 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002776 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 clear_tv(tv);
2778 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002779 {
2780 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002781 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002782 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2784 item->di_tv.v_lock = 0;
2785 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002786 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002787 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002788 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002789 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 }
2792
2793 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002794 ectx->ec_stack.ga_len -= 2 * count - 1;
2795 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002796 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002798 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 tv = STACK_TV_BOT(-1);
2800 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002801 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 tv->vval.v_dict = dict;
2803 ++dict->dv_refcount;
2804 }
2805 break;
2806
2807 // call a :def function
2808 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002809 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002810 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2811 NULL,
2812 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002813 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002814 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 break;
2816
2817 // call a builtin function
2818 case ISN_BCALL:
2819 SOURCING_LNUM = iptr->isn_lnum;
2820 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2821 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002822 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002823 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 break;
2825
2826 // call a funcref or partial
2827 case ISN_PCALL:
2828 {
2829 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2830 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002831 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832
2833 SOURCING_LNUM = iptr->isn_lnum;
2834 if (pfunc->cpf_top)
2835 {
2836 // funcref is above the arguments
2837 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2838 }
2839 else
2840 {
2841 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002842 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002843 partial_tv = *STACK_TV_BOT(0);
2844 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002846 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002847 if (tv == &partial_tv)
2848 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002850 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 }
2852 break;
2853
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002854 case ISN_PCALL_END:
2855 // PCALL finished, arguments have been consumed and replaced by
2856 // the return value. Now clear the funcref from the stack,
2857 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002858 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002859 clear_tv(STACK_TV_BOT(-1));
2860 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2861 break;
2862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 // call a user defined function or funcref/partial
2864 case ISN_UCALL:
2865 {
2866 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2867
2868 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002869 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002870 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002871 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 }
2873 break;
2874
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002875 // return from a :def function call without a value
2876 case ISN_RETURN_VOID:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002877 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002878 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002879 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002880 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002881 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002882 tv->vval.v_number = 0;
2883 tv->v_lock = 0;
2884 // FALLTHROUGH
2885
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02002886 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 case ISN_RETURN:
2888 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002889 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002890 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002891
2892 if (trystack->ga_len > 0)
2893 trycmd = ((trycmd_T *)trystack->ga_data)
2894 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002895 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002896 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002898 // jump to ":finally" or ":endtry"
2899 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002900 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002901 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002902 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 trycmd->tcd_return = TRUE;
2904 }
2905 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002906 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 }
2908 break;
2909
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002910 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 case ISN_FUNCREF:
2912 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002913 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2914 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2915 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002918 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002919 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002920 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002921 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002922 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002923 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002924 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002925 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002926 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002928 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 tv->vval.v_partial = pt;
2930 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002931 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 }
2933 break;
2934
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002935 // Create a global function from a lambda.
2936 case ISN_NEWFUNC:
2937 {
2938 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2939
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002940 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002941 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002942 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002943 }
2944 break;
2945
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002946 // List functions
2947 case ISN_DEF:
2948 if (iptr->isn_arg.string == NULL)
2949 list_functions(NULL);
2950 else
2951 {
2952 exarg_T ea;
2953
2954 CLEAR_FIELD(ea);
2955 ea.cmd = ea.arg = iptr->isn_arg.string;
2956 define_function(&ea, NULL);
2957 }
2958 break;
2959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 // jump if a condition is met
2961 case ISN_JUMP:
2962 {
2963 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002964 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 int jump = TRUE;
2966
2967 if (when != JUMP_ALWAYS)
2968 {
2969 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002970 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002971 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002972 || when == JUMP_IF_COND_TRUE)
2973 {
2974 SOURCING_LNUM = iptr->isn_lnum;
2975 jump = tv_get_bool_chk(tv, &error);
2976 if (error)
2977 goto on_error;
2978 }
2979 else
2980 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002982 || when == JUMP_AND_KEEP_IF_FALSE
2983 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002985 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 {
2987 // drop the value from the stack
2988 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002989 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 }
2991 }
2992 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002993 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 }
2995 break;
2996
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002997 // Jump if an argument with a default value was already set and not
2998 // v:none.
2999 case ISN_JUMP_IF_ARG_SET:
3000 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3001 if (tv->v_type != VAR_UNKNOWN
3002 && !(tv->v_type == VAR_SPECIAL
3003 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003004 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003005 break;
3006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 // top of a for loop
3008 case ISN_FOR:
3009 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003010 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 typval_T *idxtv =
3012 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3013
Bram Moolenaar4c137212021-04-19 16:48:48 +02003014 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003015 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003016 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003017 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003018 list_T *list = ltv->vval.v_list;
3019
3020 // push the next item from the list
3021 ++idxtv->vval.v_number;
3022 if (list == NULL
3023 || idxtv->vval.v_number >= list->lv_len)
3024 {
3025 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003026 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3027 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003028 }
3029 else if (list->lv_first == &range_list_item)
3030 {
3031 // non-materialized range() list
3032 tv = STACK_TV_BOT(0);
3033 tv->v_type = VAR_NUMBER;
3034 tv->v_lock = 0;
3035 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003037 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003038 }
3039 else
3040 {
3041 listitem_T *li = list_find(list,
3042 idxtv->vval.v_number);
3043
3044 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003045 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003046 }
3047 }
3048 else if (ltv->v_type == VAR_STRING)
3049 {
3050 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003051
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003052 // The index is for the last byte of the previous
3053 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003054 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003055 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003056 {
3057 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003058 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3059 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003060 }
3061 else
3062 {
3063 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3064
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003065 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003066 tv = STACK_TV_BOT(0);
3067 tv->v_type = VAR_STRING;
3068 tv->vval.v_string = vim_strnsave(
3069 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003070 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003071 idxtv->vval.v_number += clen - 1;
3072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003074 else if (ltv->v_type == VAR_BLOB)
3075 {
3076 blob_T *blob = ltv->vval.v_blob;
3077
3078 // When we get here the first time make a copy of the
3079 // blob, so that the iteration still works when it is
3080 // changed.
3081 if (idxtv->vval.v_number == -1 && blob != NULL)
3082 {
3083 blob_copy(blob, ltv);
3084 blob_unref(blob);
3085 blob = ltv->vval.v_blob;
3086 }
3087
3088 // The index is for the previous byte.
3089 ++idxtv->vval.v_number;
3090 if (blob == NULL
3091 || idxtv->vval.v_number >= blob_len(blob))
3092 {
3093 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003094 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3095 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003096 }
3097 else
3098 {
3099 // Push the next byte from the blob.
3100 tv = STACK_TV_BOT(0);
3101 tv->v_type = VAR_NUMBER;
3102 tv->vval.v_number = blob_get(blob,
3103 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003104 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003105 }
3106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 else
3108 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003109 semsg(_(e_for_loop_on_str_not_supported),
3110 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003111 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 }
3113 }
3114 break;
3115
3116 // start of ":try" block
3117 case ISN_TRY:
3118 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003119 trycmd_T *trycmd = NULL;
3120
Bram Moolenaar4c137212021-04-19 16:48:48 +02003121 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003122 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003123 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3124 + ectx->ec_trystack.ga_len;
3125 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003127 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003128 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3129 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003130 trycmd->tcd_catch_idx =
3131 iptr->isn_arg.try.try_ref->try_catch;
3132 trycmd->tcd_finally_idx =
3133 iptr->isn_arg.try.try_ref->try_finally;
3134 trycmd->tcd_endtry_idx =
3135 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 }
3137 break;
3138
3139 case ISN_PUSHEXC:
3140 if (current_exception == NULL)
3141 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003142 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003144 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003146 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003147 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003149 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003151 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 tv->vval.v_string = vim_strsave(
3153 (char_u *)current_exception->value);
3154 break;
3155
3156 case ISN_CATCH:
3157 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003158 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159
Bram Moolenaar4c137212021-04-19 16:48:48 +02003160 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 if (trystack->ga_len > 0)
3162 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003163 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 + trystack->ga_len - 1;
3165 trycmd->tcd_caught = TRUE;
3166 }
3167 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003168 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 catch_exception(current_exception);
3170 }
3171 break;
3172
Bram Moolenaarc150c092021-02-13 15:02:46 +01003173 case ISN_TRYCONT:
3174 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003175 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003176 trycont_T *trycont = &iptr->isn_arg.trycont;
3177 int i;
3178 trycmd_T *trycmd;
3179 int iidx = trycont->tct_where;
3180
3181 if (trystack->ga_len < trycont->tct_levels)
3182 {
3183 siemsg("TRYCONT: expected %d levels, found %d",
3184 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003185 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003186 }
3187 // Make :endtry jump to any outer try block and the last
3188 // :endtry inside the loop to the loop start.
3189 for (i = trycont->tct_levels; i > 0; --i)
3190 {
3191 trycmd = ((trycmd_T *)trystack->ga_data)
3192 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003193 // Add one to tcd_cont to be able to jump to
3194 // instruction with index zero.
3195 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003196 iidx = trycmd->tcd_finally_idx == 0
3197 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003198 }
3199 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003200 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003201 }
3202 break;
3203
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003204 case ISN_FINALLY:
3205 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003206 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003207 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3208 + trystack->ga_len - 1;
3209
3210 // Reset the index to avoid a return statement jumps here
3211 // again.
3212 trycmd->tcd_finally_idx = 0;
3213 break;
3214 }
3215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 // end of ":try" block
3217 case ISN_ENDTRY:
3218 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003219 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220
3221 if (trystack->ga_len > 0)
3222 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003223 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 --trystack->ga_len;
3226 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003227 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 trycmd = ((trycmd_T *)trystack->ga_data)
3229 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003230 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 {
3232 // discard the exception
3233 if (caught_stack == current_exception)
3234 caught_stack = caught_stack->caught;
3235 discard_current_exception();
3236 }
3237
3238 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003239 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003240
Bram Moolenaar4c137212021-04-19 16:48:48 +02003241 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003242 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003243 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003244 clear_tv(STACK_TV_BOT(0));
3245 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003246 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003247 // handling :continue: jump to outer try block or
3248 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003249 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 }
3251 }
3252 break;
3253
3254 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003255 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003256 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003257
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003258 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3259 {
3260 // throwing an exception while using "silent!" causes
3261 // the function to abort but not display an error.
3262 tv = STACK_TV_BOT(-1);
3263 clear_tv(tv);
3264 tv->v_type = VAR_NUMBER;
3265 tv->vval.v_number = 0;
3266 goto done;
3267 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003268 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003269 tv = STACK_TV_BOT(0);
3270 if (tv->vval.v_string == NULL
3271 || *skipwhite(tv->vval.v_string) == NUL)
3272 {
3273 vim_free(tv->vval.v_string);
3274 SOURCING_LNUM = iptr->isn_lnum;
3275 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003276 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003277 }
3278
3279 // Inside a "catch" we need to first discard the caught
3280 // exception.
3281 if (trystack->ga_len > 0)
3282 {
3283 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3284 + trystack->ga_len - 1;
3285 if (trycmd->tcd_caught && current_exception != NULL)
3286 {
3287 // discard the exception
3288 if (caught_stack == current_exception)
3289 caught_stack = caught_stack->caught;
3290 discard_current_exception();
3291 trycmd->tcd_caught = FALSE;
3292 }
3293 }
3294
3295 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3296 == FAIL)
3297 {
3298 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003299 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003300 }
3301 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 break;
3304
3305 // compare with special values
3306 case ISN_COMPAREBOOL:
3307 case ISN_COMPARESPECIAL:
3308 {
3309 typval_T *tv1 = STACK_TV_BOT(-2);
3310 typval_T *tv2 = STACK_TV_BOT(-1);
3311 varnumber_T arg1 = tv1->vval.v_number;
3312 varnumber_T arg2 = tv2->vval.v_number;
3313 int res;
3314
3315 switch (iptr->isn_arg.op.op_type)
3316 {
3317 case EXPR_EQUAL: res = arg1 == arg2; break;
3318 case EXPR_NEQUAL: res = arg1 != arg2; break;
3319 default: res = 0; break;
3320 }
3321
Bram Moolenaar4c137212021-04-19 16:48:48 +02003322 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 tv1->v_type = VAR_BOOL;
3324 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3325 }
3326 break;
3327
3328 // Operation with two number arguments
3329 case ISN_OPNR:
3330 case ISN_COMPARENR:
3331 {
3332 typval_T *tv1 = STACK_TV_BOT(-2);
3333 typval_T *tv2 = STACK_TV_BOT(-1);
3334 varnumber_T arg1 = tv1->vval.v_number;
3335 varnumber_T arg2 = tv2->vval.v_number;
3336 varnumber_T res;
3337
3338 switch (iptr->isn_arg.op.op_type)
3339 {
3340 case EXPR_MULT: res = arg1 * arg2; break;
3341 case EXPR_DIV: res = arg1 / arg2; break;
3342 case EXPR_REM: res = arg1 % arg2; break;
3343 case EXPR_SUB: res = arg1 - arg2; break;
3344 case EXPR_ADD: res = arg1 + arg2; break;
3345
3346 case EXPR_EQUAL: res = arg1 == arg2; break;
3347 case EXPR_NEQUAL: res = arg1 != arg2; break;
3348 case EXPR_GREATER: res = arg1 > arg2; break;
3349 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3350 case EXPR_SMALLER: res = arg1 < arg2; break;
3351 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3352 default: res = 0; break;
3353 }
3354
Bram Moolenaar4c137212021-04-19 16:48:48 +02003355 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 if (iptr->isn_type == ISN_COMPARENR)
3357 {
3358 tv1->v_type = VAR_BOOL;
3359 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3360 }
3361 else
3362 tv1->vval.v_number = res;
3363 }
3364 break;
3365
3366 // Computation with two float arguments
3367 case ISN_OPFLOAT:
3368 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003369#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 {
3371 typval_T *tv1 = STACK_TV_BOT(-2);
3372 typval_T *tv2 = STACK_TV_BOT(-1);
3373 float_T arg1 = tv1->vval.v_float;
3374 float_T arg2 = tv2->vval.v_float;
3375 float_T res = 0;
3376 int cmp = FALSE;
3377
3378 switch (iptr->isn_arg.op.op_type)
3379 {
3380 case EXPR_MULT: res = arg1 * arg2; break;
3381 case EXPR_DIV: res = arg1 / arg2; break;
3382 case EXPR_SUB: res = arg1 - arg2; break;
3383 case EXPR_ADD: res = arg1 + arg2; break;
3384
3385 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3386 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3387 case EXPR_GREATER: cmp = arg1 > arg2; break;
3388 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3389 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3390 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3391 default: cmp = 0; break;
3392 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003393 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 if (iptr->isn_type == ISN_COMPAREFLOAT)
3395 {
3396 tv1->v_type = VAR_BOOL;
3397 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3398 }
3399 else
3400 tv1->vval.v_float = res;
3401 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003402#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 break;
3404
3405 case ISN_COMPARELIST:
3406 {
3407 typval_T *tv1 = STACK_TV_BOT(-2);
3408 typval_T *tv2 = STACK_TV_BOT(-1);
3409 list_T *arg1 = tv1->vval.v_list;
3410 list_T *arg2 = tv2->vval.v_list;
3411 int cmp = FALSE;
3412 int ic = iptr->isn_arg.op.op_ic;
3413
3414 switch (iptr->isn_arg.op.op_type)
3415 {
3416 case EXPR_EQUAL: cmp =
3417 list_equal(arg1, arg2, ic, FALSE); break;
3418 case EXPR_NEQUAL: cmp =
3419 !list_equal(arg1, arg2, ic, FALSE); break;
3420 case EXPR_IS: cmp = arg1 == arg2; break;
3421 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3422 default: cmp = 0; break;
3423 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003424 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 clear_tv(tv1);
3426 clear_tv(tv2);
3427 tv1->v_type = VAR_BOOL;
3428 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3429 }
3430 break;
3431
3432 case ISN_COMPAREBLOB:
3433 {
3434 typval_T *tv1 = STACK_TV_BOT(-2);
3435 typval_T *tv2 = STACK_TV_BOT(-1);
3436 blob_T *arg1 = tv1->vval.v_blob;
3437 blob_T *arg2 = tv2->vval.v_blob;
3438 int cmp = FALSE;
3439
3440 switch (iptr->isn_arg.op.op_type)
3441 {
3442 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3443 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3444 case EXPR_IS: cmp = arg1 == arg2; break;
3445 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3446 default: cmp = 0; break;
3447 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003448 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 clear_tv(tv1);
3450 clear_tv(tv2);
3451 tv1->v_type = VAR_BOOL;
3452 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3453 }
3454 break;
3455
3456 // TODO: handle separately
3457 case ISN_COMPARESTRING:
3458 case ISN_COMPAREDICT:
3459 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 case ISN_COMPAREANY:
3461 {
3462 typval_T *tv1 = STACK_TV_BOT(-2);
3463 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003464 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 int ic = iptr->isn_arg.op.op_ic;
3466
Bram Moolenaareb26f432020-09-14 16:50:05 +02003467 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003468 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003470 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 }
3472 break;
3473
3474 case ISN_ADDLIST:
3475 case ISN_ADDBLOB:
3476 {
3477 typval_T *tv1 = STACK_TV_BOT(-2);
3478 typval_T *tv2 = STACK_TV_BOT(-1);
3479
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003480 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 if (iptr->isn_type == ISN_ADDLIST)
3482 eval_addlist(tv1, tv2);
3483 else
3484 eval_addblob(tv1, tv2);
3485 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003486 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 }
3488 break;
3489
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003490 case ISN_LISTAPPEND:
3491 {
3492 typval_T *tv1 = STACK_TV_BOT(-2);
3493 typval_T *tv2 = STACK_TV_BOT(-1);
3494 list_T *l = tv1->vval.v_list;
3495
3496 // add an item to a list
3497 if (l == NULL)
3498 {
3499 SOURCING_LNUM = iptr->isn_lnum;
3500 emsg(_(e_cannot_add_to_null_list));
3501 goto on_error;
3502 }
3503 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003504 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003505 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003506 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003507 }
3508 break;
3509
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003510 case ISN_BLOBAPPEND:
3511 {
3512 typval_T *tv1 = STACK_TV_BOT(-2);
3513 typval_T *tv2 = STACK_TV_BOT(-1);
3514 blob_T *b = tv1->vval.v_blob;
3515 int error = FALSE;
3516 varnumber_T n;
3517
3518 // add a number to a blob
3519 if (b == NULL)
3520 {
3521 SOURCING_LNUM = iptr->isn_lnum;
3522 emsg(_(e_cannot_add_to_null_blob));
3523 goto on_error;
3524 }
3525 n = tv_get_number_chk(tv2, &error);
3526 if (error)
3527 goto on_error;
3528 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003529 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003530 }
3531 break;
3532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 // Computation with two arguments of unknown type
3534 case ISN_OPANY:
3535 {
3536 typval_T *tv1 = STACK_TV_BOT(-2);
3537 typval_T *tv2 = STACK_TV_BOT(-1);
3538 varnumber_T n1, n2;
3539#ifdef FEAT_FLOAT
3540 float_T f1 = 0, f2 = 0;
3541#endif
3542 int error = FALSE;
3543
3544 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3545 {
3546 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3547 {
3548 eval_addlist(tv1, tv2);
3549 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003550 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 break;
3552 }
3553 else if (tv1->v_type == VAR_BLOB
3554 && tv2->v_type == VAR_BLOB)
3555 {
3556 eval_addblob(tv1, tv2);
3557 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003558 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 break;
3560 }
3561 }
3562#ifdef FEAT_FLOAT
3563 if (tv1->v_type == VAR_FLOAT)
3564 {
3565 f1 = tv1->vval.v_float;
3566 n1 = 0;
3567 }
3568 else
3569#endif
3570 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003571 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 n1 = tv_get_number_chk(tv1, &error);
3573 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003574 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575#ifdef FEAT_FLOAT
3576 if (tv2->v_type == VAR_FLOAT)
3577 f1 = n1;
3578#endif
3579 }
3580#ifdef FEAT_FLOAT
3581 if (tv2->v_type == VAR_FLOAT)
3582 {
3583 f2 = tv2->vval.v_float;
3584 n2 = 0;
3585 }
3586 else
3587#endif
3588 {
3589 n2 = tv_get_number_chk(tv2, &error);
3590 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003591 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592#ifdef FEAT_FLOAT
3593 if (tv1->v_type == VAR_FLOAT)
3594 f2 = n2;
3595#endif
3596 }
3597#ifdef FEAT_FLOAT
3598 // if there is a float on either side the result is a float
3599 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3600 {
3601 switch (iptr->isn_arg.op.op_type)
3602 {
3603 case EXPR_MULT: f1 = f1 * f2; break;
3604 case EXPR_DIV: f1 = f1 / f2; break;
3605 case EXPR_SUB: f1 = f1 - f2; break;
3606 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003607 default: SOURCING_LNUM = iptr->isn_lnum;
3608 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003609 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 }
3611 clear_tv(tv1);
3612 clear_tv(tv2);
3613 tv1->v_type = VAR_FLOAT;
3614 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003615 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 }
3617 else
3618#endif
3619 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003620 int failed = FALSE;
3621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 switch (iptr->isn_arg.op.op_type)
3623 {
3624 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003625 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3626 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003627 goto on_error;
3628 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 case EXPR_SUB: n1 = n1 - n2; break;
3630 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003631 default: n1 = num_modulus(n1, n2, &failed);
3632 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003633 goto on_error;
3634 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 }
3636 clear_tv(tv1);
3637 clear_tv(tv2);
3638 tv1->v_type = VAR_NUMBER;
3639 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003640 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 }
3642 }
3643 break;
3644
3645 case ISN_CONCAT:
3646 {
3647 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3648 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3649 char_u *res;
3650
3651 res = concat_str(str1, str2);
3652 clear_tv(STACK_TV_BOT(-2));
3653 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003654 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 STACK_TV_BOT(-1)->vval.v_string = res;
3656 }
3657 break;
3658
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003659 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003660 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003661 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003662 int is_slice = iptr->isn_type == ISN_STRSLICE;
3663 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003664 char_u *res;
3665
3666 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003667 // string slice: string is at stack-3, first index at
3668 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003669 if (is_slice)
3670 {
3671 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003672 n1 = tv->vval.v_number;
3673 }
3674
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003675 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003676 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003677
Bram Moolenaar4c137212021-04-19 16:48:48 +02003678 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003679 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003680 if (is_slice)
3681 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003682 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003683 else
3684 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003685 // single character (including composing characters).
3686 // If the index is too big or negative the result is
3687 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003688 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003689 vim_free(tv->vval.v_string);
3690 tv->vval.v_string = res;
3691 }
3692 break;
3693
3694 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003695 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003696 case ISN_BLOBINDEX:
3697 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003699 int is_slice = iptr->isn_type == ISN_LISTSLICE
3700 || iptr->isn_type == ISN_BLOBSLICE;
3701 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3702 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003703 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003704 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705
3706 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003707 // list slice: list is at stack-3, indexes at stack-2 and
3708 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003709 // Same for blob.
3710 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711
3712 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003713 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003715
3716 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 {
Bram Moolenaared591872020-08-15 22:14:53 +02003718 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003719 n1 = tv->vval.v_number;
3720 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 }
Bram Moolenaared591872020-08-15 22:14:53 +02003722
Bram Moolenaar4c137212021-04-19 16:48:48 +02003723 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003724 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003725 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003726 if (is_blob)
3727 {
3728 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3729 n1, n2, FALSE, tv) == FAIL)
3730 goto on_error;
3731 }
3732 else
3733 {
3734 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3735 n1, n2, FALSE, tv, TRUE) == FAIL)
3736 goto on_error;
3737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 }
3739 break;
3740
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003741 case ISN_ANYINDEX:
3742 case ISN_ANYSLICE:
3743 {
3744 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3745 typval_T *var1, *var2;
3746 int res;
3747
3748 // index: composite is at stack-2, index at stack-1
3749 // slice: composite is at stack-3, indexes at stack-2 and
3750 // stack-1
3751 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003752 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003753 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3754 goto on_error;
3755 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3756 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003757 res = eval_index_inner(tv, is_slice, var1, var2,
3758 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003759 clear_tv(var1);
3760 if (is_slice)
3761 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003762 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003763 if (res == FAIL)
3764 goto on_error;
3765 }
3766 break;
3767
Bram Moolenaar9af78762020-06-16 11:34:42 +02003768 case ISN_SLICE:
3769 {
3770 list_T *list;
3771 int count = iptr->isn_arg.number;
3772
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003773 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003774 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003775 list = tv->vval.v_list;
3776
3777 // no error for short list, expect it to be checked earlier
3778 if (list != NULL && list->lv_len >= count)
3779 {
3780 list_T *newlist = list_slice(list,
3781 count, list->lv_len - 1);
3782
3783 if (newlist != NULL)
3784 {
3785 list_unref(list);
3786 tv->vval.v_list = newlist;
3787 ++newlist->lv_refcount;
3788 }
3789 }
3790 }
3791 break;
3792
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003793 case ISN_GETITEM:
3794 {
3795 listitem_T *li;
3796 int index = iptr->isn_arg.number;
3797
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003798 // Get list item: list is at stack-1, push item.
3799 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003800 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003801 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003802
Bram Moolenaar4c137212021-04-19 16:48:48 +02003803 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003804 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003805 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003806 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003807
3808 // Useful when used in unpack assignment. Reset at
3809 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003810 ectx->ec_where.wt_index = index + 1;
3811 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003812 }
3813 break;
3814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 case ISN_MEMBER:
3816 {
3817 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003818 char_u *key;
3819 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003820 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003821
3822 // dict member: dict is at stack-2, key at stack-1
3823 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003824 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003825 dict = tv->vval.v_dict;
3826
3827 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003828 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003829 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003830 if (key == NULL)
3831 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003832
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003833 if ((di = dict_find(dict, key, -1)) == NULL)
3834 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003836 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003837
3838 // If :silent! is used we will continue, make sure the
3839 // stack contents makes sense.
3840 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003841 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003842 tv = STACK_TV_BOT(-1);
3843 clear_tv(tv);
3844 tv->v_type = VAR_NUMBER;
3845 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003846 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003847 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003848 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003849 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003850 // Clear the dict only after getting the item, to avoid
3851 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003852 tv = STACK_TV_BOT(-1);
3853 temp_tv = *tv;
3854 copy_tv(&di->di_tv, tv);
3855 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003856 }
3857 break;
3858
3859 // dict member with string key
3860 case ISN_STRINGMEMBER:
3861 {
3862 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003864 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
3866 tv = STACK_TV_BOT(-1);
3867 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3868 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003869 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003871 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 }
3873 dict = tv->vval.v_dict;
3874
3875 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3876 == NULL)
3877 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003878 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003880 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003882 // Clear the dict after getting the item, to avoid that it
3883 // make the item invalid.
3884 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003886 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 }
3888 break;
3889
3890 case ISN_NEGATENR:
3891 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003892 if (tv->v_type != VAR_NUMBER
3893#ifdef FEAT_FLOAT
3894 && tv->v_type != VAR_FLOAT
3895#endif
3896 )
3897 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003898 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003899 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003900 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003901 }
3902#ifdef FEAT_FLOAT
3903 if (tv->v_type == VAR_FLOAT)
3904 tv->vval.v_float = -tv->vval.v_float;
3905 else
3906#endif
3907 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 break;
3909
3910 case ISN_CHECKNR:
3911 {
3912 int error = FALSE;
3913
3914 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003915 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003917 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 (void)tv_get_number_chk(tv, &error);
3919 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003920 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 }
3922 break;
3923
3924 case ISN_CHECKTYPE:
3925 {
3926 checktype_T *ct = &iptr->isn_arg.type;
3927
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003928 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003929 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003930 if (!ectx->ec_where.wt_variable)
3931 ectx->ec_where.wt_index = ct->ct_arg_idx;
3932 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3933 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003934 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003935 if (!ectx->ec_where.wt_variable)
3936 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003937
3938 // number 0 is FALSE, number 1 is TRUE
3939 if (tv->v_type == VAR_NUMBER
3940 && ct->ct_type->tt_type == VAR_BOOL
3941 && (tv->vval.v_number == 0
3942 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003944 tv->v_type = VAR_BOOL;
3945 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003946 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 }
3948 }
3949 break;
3950
Bram Moolenaar9af78762020-06-16 11:34:42 +02003951 case ISN_CHECKLEN:
3952 {
3953 int min_len = iptr->isn_arg.checklen.cl_min_len;
3954 list_T *list = NULL;
3955
3956 tv = STACK_TV_BOT(-1);
3957 if (tv->v_type == VAR_LIST)
3958 list = tv->vval.v_list;
3959 if (list == NULL || list->lv_len < min_len
3960 || (list->lv_len > min_len
3961 && !iptr->isn_arg.checklen.cl_more_OK))
3962 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003963 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003964 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003965 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003966 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003967 }
3968 }
3969 break;
3970
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003971 case ISN_SETTYPE:
3972 {
3973 checktype_T *ct = &iptr->isn_arg.type;
3974
3975 tv = STACK_TV_BOT(-1);
3976 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3977 {
3978 free_type(tv->vval.v_dict->dv_type);
3979 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3980 }
3981 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3982 {
3983 free_type(tv->vval.v_list->lv_type);
3984 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3985 }
3986 }
3987 break;
3988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003990 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 {
3992 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003993 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003995 if (iptr->isn_type == ISN_2BOOL)
3996 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003997 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003998 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003999 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004000 n = !n;
4001 }
4002 else
4003 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004004 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004005 SOURCING_LNUM = iptr->isn_lnum;
4006 n = tv_get_bool_chk(tv, &error);
4007 if (error)
4008 goto on_error;
4009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 clear_tv(tv);
4011 tv->v_type = VAR_BOOL;
4012 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4013 }
4014 break;
4015
4016 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004017 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004018 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004019 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4020 iptr->isn_type == ISN_2STRING_ANY,
4021 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004022 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 break;
4024
Bram Moolenaar08597872020-12-10 19:43:40 +01004025 case ISN_RANGE:
4026 {
4027 exarg_T ea;
4028 char *errormsg;
4029
Bram Moolenaarece0b872021-01-08 20:40:45 +01004030 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004031 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004032 ea.addr_type = ADDR_LINES;
4033 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004034 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004035 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004036 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004037
Bram Moolenaar4c137212021-04-19 16:48:48 +02004038 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004039 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004040 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004041 tv = STACK_TV_BOT(-1);
4042 tv->v_type = VAR_NUMBER;
4043 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004044 if (ea.addr_count == 0)
4045 tv->vval.v_number = curwin->w_cursor.lnum;
4046 else
4047 tv->vval.v_number = ea.line2;
4048 }
4049 break;
4050
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004051 case ISN_PUT:
4052 {
4053 int regname = iptr->isn_arg.put.put_regname;
4054 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4055 char_u *expr = NULL;
4056 int dir = FORWARD;
4057
Bram Moolenaar08597872020-12-10 19:43:40 +01004058 if (lnum < -2)
4059 {
4060 // line number was put on the stack by ISN_RANGE
4061 tv = STACK_TV_BOT(-1);
4062 curwin->w_cursor.lnum = tv->vval.v_number;
4063 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4064 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004065 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004066 }
4067 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004068 // :put! above cursor
4069 dir = BACKWARD;
4070 else if (lnum >= 0)
4071 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004072
4073 if (regname == '=')
4074 {
4075 tv = STACK_TV_BOT(-1);
4076 if (tv->v_type == VAR_STRING)
4077 expr = tv->vval.v_string;
4078 else
4079 {
4080 expr = typval2string(tv, TRUE); // allocates value
4081 clear_tv(tv);
4082 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004083 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004084 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004085 check_cursor();
4086 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4087 vim_free(expr);
4088 }
4089 break;
4090
Bram Moolenaar02194d22020-10-24 23:08:38 +02004091 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004092 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4093 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4094 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4095 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004096 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4097 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004098 break;
4099
Bram Moolenaar02194d22020-10-24 23:08:38 +02004100 case ISN_CMDMOD_REV:
4101 // filter regprog is owned by the instruction, don't free it
4102 cmdmod.cmod_filter_regmatch.regprog = NULL;
4103 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004104 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4105 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004106 break;
4107
Bram Moolenaar792f7862020-11-23 08:31:18 +01004108 case ISN_UNPACK:
4109 {
4110 int count = iptr->isn_arg.unpack.unp_count;
4111 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4112 list_T *l;
4113 listitem_T *li;
4114 int i;
4115
4116 // Check there is a valid list to unpack.
4117 tv = STACK_TV_BOT(-1);
4118 if (tv->v_type != VAR_LIST)
4119 {
4120 SOURCING_LNUM = iptr->isn_lnum;
4121 emsg(_(e_for_argument_must_be_sequence_of_lists));
4122 goto on_error;
4123 }
4124 l = tv->vval.v_list;
4125 if (l == NULL
4126 || l->lv_len < (semicolon ? count - 1 : count))
4127 {
4128 SOURCING_LNUM = iptr->isn_lnum;
4129 emsg(_(e_list_value_does_not_have_enough_items));
4130 goto on_error;
4131 }
4132 else if (!semicolon && l->lv_len > count)
4133 {
4134 SOURCING_LNUM = iptr->isn_lnum;
4135 emsg(_(e_list_value_has_more_items_than_targets));
4136 goto on_error;
4137 }
4138
4139 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004140 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004141 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004142 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004143
4144 // Variable after semicolon gets a list with the remaining
4145 // items.
4146 if (semicolon)
4147 {
4148 list_T *rem_list =
4149 list_alloc_with_items(l->lv_len - count + 1);
4150
4151 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004152 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004153 tv = STACK_TV_BOT(-count);
4154 tv->vval.v_list = rem_list;
4155 ++rem_list->lv_refcount;
4156 tv->v_lock = 0;
4157 li = l->lv_first;
4158 for (i = 0; i < count - 1; ++i)
4159 li = li->li_next;
4160 for (i = 0; li != NULL; ++i)
4161 {
4162 list_set_item(rem_list, i, &li->li_tv);
4163 li = li->li_next;
4164 }
4165 --count;
4166 }
4167
4168 // Produce the values in reverse order, first item last.
4169 li = l->lv_first;
4170 for (i = 0; i < count; ++i)
4171 {
4172 tv = STACK_TV_BOT(-i - 1);
4173 copy_tv(&li->li_tv, tv);
4174 li = li->li_next;
4175 }
4176
4177 list_unref(l);
4178 }
4179 break;
4180
Bram Moolenaarb2049902021-01-24 12:53:53 +01004181 case ISN_PROF_START:
4182 case ISN_PROF_END:
4183 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004184#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004185 funccall_T cookie;
4186 ufunc_T *cur_ufunc =
4187 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004188 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004189
4190 cookie.func = cur_ufunc;
4191 if (iptr->isn_type == ISN_PROF_START)
4192 {
4193 func_line_start(&cookie, iptr->isn_lnum);
4194 // if we get here the instruction is executed
4195 func_line_exec(&cookie);
4196 }
4197 else
4198 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004199#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004200 }
4201 break;
4202
Bram Moolenaare99d4222021-06-13 14:01:26 +02004203 case ISN_DEBUG:
4204 if (ex_nesting_level <= debug_break_level)
Bram Moolenaar4cea5362021-06-16 22:24:40 +02004205 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004206 break;
4207
Bram Moolenaar389df252020-07-09 21:20:47 +02004208 case ISN_SHUFFLE:
4209 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004210 typval_T tmp_tv;
4211 int item = iptr->isn_arg.shuffle.shfl_item;
4212 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004213
4214 tmp_tv = *STACK_TV_BOT(-item);
4215 for ( ; up > 0 && item > 1; --up)
4216 {
4217 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4218 --item;
4219 }
4220 *STACK_TV_BOT(-item) = tmp_tv;
4221 }
4222 break;
4223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004225 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004227 ectx->ec_where.wt_index = 0;
4228 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 break;
4230 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004231 continue;
4232
Bram Moolenaard032f342020-07-18 18:13:02 +02004233func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004234 // Restore previous function. If the frame pointer is where we started
4235 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004236 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004237 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004238
Bram Moolenaar4c137212021-04-19 16:48:48 +02004239 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004240 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004241 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004242 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004243
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004244on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004245 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004246 // If "emsg_silent" is set then ignore the error, unless it was set
4247 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004248 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004249 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004250 {
4251 // If a sequence of instructions causes an error while ":silent!"
4252 // was used, restore the stack length and jump ahead to restoring
4253 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004254 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004255 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004256 while (ectx->ec_stack.ga_len
4257 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004258 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004259 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004260 clear_tv(STACK_TV_BOT(0));
4261 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004262 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4263 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004264 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004265 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004266 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004267on_fatal_error:
4268 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004269 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004270 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004271 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 }
4273
4274done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004275 ret = OK;
4276theend:
4277 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4278 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004279}
4280
4281/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004282 * Execute the instructions from a VAR_INSTR typeval and put the result in
4283 * "rettv".
4284 * Return OK or FAIL.
4285 */
4286 int
4287exe_typval_instr(typval_T *tv, typval_T *rettv)
4288{
4289 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4290 isn_T *save_instr = ectx->ec_instr;
4291 int save_iidx = ectx->ec_iidx;
4292 int res;
4293
4294 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4295 res = exec_instructions(ectx);
4296 if (res == OK)
4297 {
4298 *rettv = *STACK_TV_BOT(-1);
4299 --ectx->ec_stack.ga_len;
4300 }
4301
4302 ectx->ec_instr = save_instr;
4303 ectx->ec_iidx = save_iidx;
4304
4305 return res;
4306}
4307
4308/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004309 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4310 * "substitute_instr".
4311 */
4312 char_u *
4313exe_substitute_instr(void)
4314{
4315 ectx_T *ectx = substitute_instr->subs_ectx;
4316 isn_T *save_instr = ectx->ec_instr;
4317 int save_iidx = ectx->ec_iidx;
4318 char_u *res;
4319
4320 ectx->ec_instr = substitute_instr->subs_instr;
4321 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004322 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004323 typval_T *tv = STACK_TV_BOT(-1);
4324
Bram Moolenaar27523602021-06-05 21:36:19 +02004325 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004326 --ectx->ec_stack.ga_len;
4327 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004328 }
4329 else
4330 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004331 substitute_instr->subs_status = FAIL;
4332 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334
Bram Moolenaar4c137212021-04-19 16:48:48 +02004335 ectx->ec_instr = save_instr;
4336 ectx->ec_iidx = save_iidx;
4337
4338 return res;
4339}
4340
4341/*
4342 * Call a "def" function from old Vim script.
4343 * Return OK or FAIL.
4344 */
4345 int
4346call_def_function(
4347 ufunc_T *ufunc,
4348 int argc_arg, // nr of arguments
4349 typval_T *argv, // arguments
4350 partial_T *partial, // optional partial for context
4351 typval_T *rettv) // return value
4352{
4353 ectx_T ectx; // execution context
4354 int argc = argc_arg;
4355 typval_T *tv;
4356 int idx;
4357 int ret = FAIL;
4358 int defcount = ufunc->uf_args.ga_len - argc;
4359 sctx_T save_current_sctx = current_sctx;
4360 int did_emsg_before = did_emsg_cumul + did_emsg;
4361 int save_suppress_errthrow = suppress_errthrow;
4362 msglist_T **saved_msg_list = NULL;
4363 msglist_T *private_msg_list = NULL;
4364 int save_emsg_silent_def = emsg_silent_def;
4365 int save_did_emsg_def = did_emsg_def;
4366 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004367 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004368
4369// Get pointer to item in the stack.
4370#undef STACK_TV
4371#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4372
4373// Get pointer to item at the bottom of the stack, -1 is the bottom.
4374#undef STACK_TV_BOT
4375#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4376
4377// Get pointer to a local variable on the stack. Negative for arguments.
4378#undef STACK_TV_VAR
4379#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4380
4381 if (ufunc->uf_def_status == UF_NOT_COMPILED
4382 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004383 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4384 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385 == FAIL))
4386 {
4387 if (did_emsg_cumul + did_emsg == did_emsg_before)
4388 semsg(_(e_function_is_not_compiled_str),
4389 printable_func_name(ufunc));
4390 return FAIL;
4391 }
4392
4393 {
4394 // Check the function was really compiled.
4395 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4396 + ufunc->uf_dfunc_idx;
4397 if (INSTRUCTIONS(dfunc) == NULL)
4398 {
4399 iemsg("using call_def_function() on not compiled function");
4400 return FAIL;
4401 }
4402 }
4403
4404 // If depth of calling is getting too high, don't execute the function.
4405 orig_funcdepth = funcdepth_get();
4406 if (funcdepth_increment() == FAIL)
4407 return FAIL;
4408
4409 CLEAR_FIELD(ectx);
4410 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4411 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4412 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4413 {
4414 funcdepth_decrement();
4415 return FAIL;
4416 }
4417 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4418 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4419 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004420 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004421
4422 idx = argc - ufunc->uf_args.ga_len;
4423 if (idx > 0 && ufunc->uf_va_name == NULL)
4424 {
4425 if (idx == 1)
4426 emsg(_(e_one_argument_too_many));
4427 else
4428 semsg(_(e_nr_arguments_too_many), idx);
4429 goto failed_early;
4430 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004431 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4432 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004433 {
4434 if (idx == -1)
4435 emsg(_(e_one_argument_too_few));
4436 else
4437 semsg(_(e_nr_arguments_too_few), -idx);
4438 goto failed_early;
4439 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004440
4441 // Put arguments on the stack, but no more than what the function expects.
4442 // A lambda can be called with more arguments than it uses.
4443 for (idx = 0; idx < argc
4444 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4445 ++idx)
4446 {
4447 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4448 && argv[idx].v_type == VAR_SPECIAL
4449 && argv[idx].vval.v_number == VVAL_NONE)
4450 {
4451 // Use the default value.
4452 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4453 }
4454 else
4455 {
4456 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4457 && check_typval_arg_type(
4458 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4459 goto failed_early;
4460 copy_tv(&argv[idx], STACK_TV_BOT(0));
4461 }
4462 ++ectx.ec_stack.ga_len;
4463 }
4464
4465 // Turn varargs into a list. Empty list if no args.
4466 if (ufunc->uf_va_name != NULL)
4467 {
4468 int vararg_count = argc - ufunc->uf_args.ga_len;
4469
4470 if (vararg_count < 0)
4471 vararg_count = 0;
4472 else
4473 argc -= vararg_count;
4474 if (exe_newlist(vararg_count, &ectx) == FAIL)
4475 goto failed_early;
4476
4477 // Check the type of the list items.
4478 tv = STACK_TV_BOT(-1);
4479 if (ufunc->uf_va_type != NULL
4480 && ufunc->uf_va_type != &t_list_any
4481 && ufunc->uf_va_type->tt_member != &t_any
4482 && tv->vval.v_list != NULL)
4483 {
4484 type_T *expected = ufunc->uf_va_type->tt_member;
4485 listitem_T *li = tv->vval.v_list->lv_first;
4486
4487 for (idx = 0; idx < vararg_count; ++idx)
4488 {
4489 if (check_typval_arg_type(expected, &li->li_tv,
4490 argc + idx + 1) == FAIL)
4491 goto failed_early;
4492 li = li->li_next;
4493 }
4494 }
4495
4496 if (defcount > 0)
4497 // Move varargs list to below missing default arguments.
4498 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4499 --ectx.ec_stack.ga_len;
4500 }
4501
4502 // Make space for omitted arguments, will store default value below.
4503 // Any varargs list goes after them.
4504 if (defcount > 0)
4505 for (idx = 0; idx < defcount; ++idx)
4506 {
4507 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4508 ++ectx.ec_stack.ga_len;
4509 }
4510 if (ufunc->uf_va_name != NULL)
4511 ++ectx.ec_stack.ga_len;
4512
4513 // Frame pointer points to just after arguments.
4514 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4515 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4516
4517 {
4518 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4519 + ufunc->uf_dfunc_idx;
4520 ufunc_T *base_ufunc = dfunc->df_ufunc;
4521
4522 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4523 // by copy_func().
4524 if (partial != NULL || base_ufunc->uf_partial != NULL)
4525 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004526 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4527 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004528 goto failed_early;
4529 if (partial != NULL)
4530 {
4531 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4532 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004533 if (current_ectx->ec_outer_ref != NULL
4534 && current_ectx->ec_outer_ref->or_outer != NULL)
4535 ectx.ec_outer_ref->or_outer =
4536 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004537 }
4538 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004539 {
4540 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4541 ++partial->pt_refcount;
4542 ectx.ec_outer_ref->or_partial = partial;
4543 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004544 }
4545 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004546 {
4547 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4548 ++base_ufunc->uf_partial->pt_refcount;
4549 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4550 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004551 }
4552 }
4553
4554 // dummy frame entries
4555 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4556 {
4557 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4558 ++ectx.ec_stack.ga_len;
4559 }
4560
4561 {
4562 // Reserve space for local variables and any closure reference count.
4563 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4564 + ufunc->uf_dfunc_idx;
4565
4566 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4567 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4568 ectx.ec_stack.ga_len += dfunc->df_varcount;
4569 if (dfunc->df_has_closure)
4570 {
4571 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4572 STACK_TV_VAR(idx)->vval.v_number = 0;
4573 ++ectx.ec_stack.ga_len;
4574 }
4575
4576 ectx.ec_instr = INSTRUCTIONS(dfunc);
4577 }
4578
4579 // Following errors are in the function, not the caller.
4580 // Commands behave like vim9script.
4581 estack_push_ufunc(ufunc, 1);
4582 current_sctx = ufunc->uf_script_ctx;
4583 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4584
4585 // Use a specific location for storing error messages to be converted to an
4586 // exception.
4587 saved_msg_list = msg_list;
4588 msg_list = &private_msg_list;
4589
4590 // Do turn errors into exceptions.
4591 suppress_errthrow = FALSE;
4592
4593 // Do not delete the function while executing it.
4594 ++ufunc->uf_calls;
4595
4596 // When ":silent!" was used before calling then we still abort the
4597 // function. If ":silent!" is used in the function then we don't.
4598 emsg_silent_def = emsg_silent;
4599 did_emsg_def = 0;
4600
4601 ectx.ec_where.wt_index = 0;
4602 ectx.ec_where.wt_variable = FALSE;
4603
4604 // Execute the instructions until done.
4605 ret = exec_instructions(&ectx);
4606 if (ret == OK)
4607 {
4608 // function finished, get result from the stack.
4609 if (ufunc->uf_ret_type == &t_void)
4610 {
4611 rettv->v_type = VAR_VOID;
4612 }
4613 else
4614 {
4615 tv = STACK_TV_BOT(-1);
4616 *rettv = *tv;
4617 tv->v_type = VAR_UNKNOWN;
4618 }
4619 }
4620
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004621 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004622 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4623 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004624
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004625 // Deal with any remaining closures, they may be in use somewhere.
4626 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004627 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004628 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004629 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4630 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004631
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004632 estack_pop();
4633 current_sctx = save_current_sctx;
4634
Bram Moolenaarc970e422021-03-17 15:03:04 +01004635 // TODO: when is it safe to delete the function if it is no longer used?
4636 --ufunc->uf_calls;
4637
Bram Moolenaar352134b2020-10-17 22:04:08 +02004638 if (*msg_list != NULL && saved_msg_list != NULL)
4639 {
4640 msglist_T **plist = saved_msg_list;
4641
4642 // Append entries from the current msg_list (uncaught exceptions) to
4643 // the saved msg_list.
4644 while (*plist != NULL)
4645 plist = &(*plist)->next;
4646
4647 *plist = *msg_list;
4648 }
4649 msg_list = saved_msg_list;
4650
Bram Moolenaar4c137212021-04-19 16:48:48 +02004651 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004652 {
4653 cmdmod.cmod_filter_regmatch.regprog = NULL;
4654 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004655 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004656 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004657 emsg_silent_def = save_emsg_silent_def;
4658 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004659
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004660failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004661 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4663 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004664 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004667 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004668 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004669 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004670 if (ectx.ec_outer_ref->or_outer_allocated)
4671 vim_free(ectx.ec_outer_ref->or_outer);
4672 partial_unref(ectx.ec_outer_ref->or_partial);
4673 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004674 }
4675
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004676 // Not sure if this is necessary.
4677 suppress_errthrow = save_suppress_errthrow;
4678
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004679 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004680 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004681 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004682 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 return ret;
4684}
4685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004687 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4688 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4689 * "pfx" is prefixed to every line.
4690 */
4691 static void
4692list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4693{
4694 int line_idx = 0;
4695 int prev_current = 0;
4696 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004697 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004698
4699 for (current = 0; current < instr_count; ++current)
4700 {
4701 isn_T *iptr = &instr[current];
4702 char *line;
4703
4704 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004705 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004706 while (line_idx < iptr->isn_lnum
4707 && line_idx < ufunc->uf_lines.ga_len)
4708 {
4709 if (current > prev_current)
4710 {
4711 msg_puts("\n\n");
4712 prev_current = current;
4713 }
4714 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4715 if (line != NULL)
4716 msg(line);
4717 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004718 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4719 {
4720 int first_def_arg = ufunc->uf_args.ga_len
4721 - ufunc->uf_def_args.ga_len;
4722
4723 if (def_arg_idx > 0)
4724 msg_puts("\n\n");
4725 msg_start();
4726 msg_puts(" ");
4727 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4728 first_def_arg + def_arg_idx]);
4729 msg_puts(" = ");
4730 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4731 msg_clr_eos();
4732 msg_end();
4733 }
4734 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004735
4736 switch (iptr->isn_type)
4737 {
4738 case ISN_EXEC:
4739 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4740 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004741 case ISN_EXEC_SPLIT:
4742 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4743 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004744 case ISN_LEGACY_EVAL:
4745 smsg("%s%4d EVAL legacy %s", pfx, current,
4746 iptr->isn_arg.string);
4747 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004748 case ISN_REDIRSTART:
4749 smsg("%s%4d REDIR", pfx, current);
4750 break;
4751 case ISN_REDIREND:
4752 smsg("%s%4d REDIR END%s", pfx, current,
4753 iptr->isn_arg.number ? " append" : "");
4754 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004755 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004756#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004757 smsg("%s%4d CEXPR pre %s", pfx, current,
4758 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004759#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004760 break;
4761 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004762#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004763 {
4764 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4765
4766 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4767 cexpr_get_auname(cer->cer_cmdidx),
4768 cer->cer_forceit ? "!" : "",
4769 cer->cer_cmdline);
4770 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004771#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004772 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004773 case ISN_INSTR:
4774 {
4775 smsg("%s%4d INSTR", pfx, current);
4776 list_instructions(" ", iptr->isn_arg.instr,
4777 INT_MAX, NULL);
4778 msg(" -------------");
4779 }
4780 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004781 case ISN_SUBSTITUTE:
4782 {
4783 subs_T *subs = &iptr->isn_arg.subs;
4784
4785 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4786 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4787 msg(" -------------");
4788 }
4789 break;
4790 case ISN_EXECCONCAT:
4791 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4792 (varnumber_T)iptr->isn_arg.number);
4793 break;
4794 case ISN_ECHO:
4795 {
4796 echo_T *echo = &iptr->isn_arg.echo;
4797
4798 smsg("%s%4d %s %d", pfx, current,
4799 echo->echo_with_white ? "ECHO" : "ECHON",
4800 echo->echo_count);
4801 }
4802 break;
4803 case ISN_EXECUTE:
4804 smsg("%s%4d EXECUTE %lld", pfx, current,
4805 (varnumber_T)(iptr->isn_arg.number));
4806 break;
4807 case ISN_ECHOMSG:
4808 smsg("%s%4d ECHOMSG %lld", pfx, current,
4809 (varnumber_T)(iptr->isn_arg.number));
4810 break;
4811 case ISN_ECHOERR:
4812 smsg("%s%4d ECHOERR %lld", pfx, current,
4813 (varnumber_T)(iptr->isn_arg.number));
4814 break;
4815 case ISN_LOAD:
4816 {
4817 if (iptr->isn_arg.number < 0)
4818 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4819 (varnumber_T)(iptr->isn_arg.number
4820 + STACK_FRAME_SIZE));
4821 else
4822 smsg("%s%4d LOAD $%lld", pfx, current,
4823 (varnumber_T)(iptr->isn_arg.number));
4824 }
4825 break;
4826 case ISN_LOADOUTER:
4827 {
4828 if (iptr->isn_arg.number < 0)
4829 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4830 iptr->isn_arg.outer.outer_depth,
4831 iptr->isn_arg.outer.outer_idx
4832 + STACK_FRAME_SIZE);
4833 else
4834 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4835 iptr->isn_arg.outer.outer_depth,
4836 iptr->isn_arg.outer.outer_idx);
4837 }
4838 break;
4839 case ISN_LOADV:
4840 smsg("%s%4d LOADV v:%s", pfx, current,
4841 get_vim_var_name(iptr->isn_arg.number));
4842 break;
4843 case ISN_LOADSCRIPT:
4844 {
4845 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4846 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4847 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4848 + sref->sref_idx;
4849
4850 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4851 sv->sv_name,
4852 sref->sref_idx,
4853 si->sn_name);
4854 }
4855 break;
4856 case ISN_LOADS:
4857 {
4858 scriptitem_T *si = SCRIPT_ITEM(
4859 iptr->isn_arg.loadstore.ls_sid);
4860
4861 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4862 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4863 }
4864 break;
4865 case ISN_LOADAUTO:
4866 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4867 break;
4868 case ISN_LOADG:
4869 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4870 break;
4871 case ISN_LOADB:
4872 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4873 break;
4874 case ISN_LOADW:
4875 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4876 break;
4877 case ISN_LOADT:
4878 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4879 break;
4880 case ISN_LOADGDICT:
4881 smsg("%s%4d LOAD g:", pfx, current);
4882 break;
4883 case ISN_LOADBDICT:
4884 smsg("%s%4d LOAD b:", pfx, current);
4885 break;
4886 case ISN_LOADWDICT:
4887 smsg("%s%4d LOAD w:", pfx, current);
4888 break;
4889 case ISN_LOADTDICT:
4890 smsg("%s%4d LOAD t:", pfx, current);
4891 break;
4892 case ISN_LOADOPT:
4893 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4894 break;
4895 case ISN_LOADENV:
4896 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4897 break;
4898 case ISN_LOADREG:
4899 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4900 break;
4901
4902 case ISN_STORE:
4903 if (iptr->isn_arg.number < 0)
4904 smsg("%s%4d STORE arg[%lld]", pfx, current,
4905 iptr->isn_arg.number + STACK_FRAME_SIZE);
4906 else
4907 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4908 break;
4909 case ISN_STOREOUTER:
4910 {
4911 if (iptr->isn_arg.number < 0)
4912 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4913 iptr->isn_arg.outer.outer_depth,
4914 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4915 else
4916 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4917 iptr->isn_arg.outer.outer_depth,
4918 iptr->isn_arg.outer.outer_idx);
4919 }
4920 break;
4921 case ISN_STOREV:
4922 smsg("%s%4d STOREV v:%s", pfx, current,
4923 get_vim_var_name(iptr->isn_arg.number));
4924 break;
4925 case ISN_STOREAUTO:
4926 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4927 break;
4928 case ISN_STOREG:
4929 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4930 break;
4931 case ISN_STOREB:
4932 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4933 break;
4934 case ISN_STOREW:
4935 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4936 break;
4937 case ISN_STORET:
4938 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4939 break;
4940 case ISN_STORES:
4941 {
4942 scriptitem_T *si = SCRIPT_ITEM(
4943 iptr->isn_arg.loadstore.ls_sid);
4944
4945 smsg("%s%4d STORES %s in %s", pfx, current,
4946 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4947 }
4948 break;
4949 case ISN_STORESCRIPT:
4950 {
4951 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4952 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4953 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4954 + sref->sref_idx;
4955
4956 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4957 sv->sv_name,
4958 sref->sref_idx,
4959 si->sn_name);
4960 }
4961 break;
4962 case ISN_STOREOPT:
4963 smsg("%s%4d STOREOPT &%s", pfx, current,
4964 iptr->isn_arg.storeopt.so_name);
4965 break;
4966 case ISN_STOREENV:
4967 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4968 break;
4969 case ISN_STOREREG:
4970 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4971 break;
4972 case ISN_STORENR:
4973 smsg("%s%4d STORE %lld in $%d", pfx, current,
4974 iptr->isn_arg.storenr.stnr_val,
4975 iptr->isn_arg.storenr.stnr_idx);
4976 break;
4977
4978 case ISN_STOREINDEX:
4979 smsg("%s%4d STOREINDEX %s", pfx, current,
4980 vartype_name(iptr->isn_arg.vartype));
4981 break;
4982
4983 case ISN_STORERANGE:
4984 smsg("%s%4d STORERANGE", pfx, current);
4985 break;
4986
4987 // constants
4988 case ISN_PUSHNR:
4989 smsg("%s%4d PUSHNR %lld", pfx, current,
4990 (varnumber_T)(iptr->isn_arg.number));
4991 break;
4992 case ISN_PUSHBOOL:
4993 case ISN_PUSHSPEC:
4994 smsg("%s%4d PUSH %s", pfx, current,
4995 get_var_special_name(iptr->isn_arg.number));
4996 break;
4997 case ISN_PUSHF:
4998#ifdef FEAT_FLOAT
4999 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5000#endif
5001 break;
5002 case ISN_PUSHS:
5003 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5004 break;
5005 case ISN_PUSHBLOB:
5006 {
5007 char_u *r;
5008 char_u numbuf[NUMBUFLEN];
5009 char_u *tofree;
5010
5011 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5012 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5013 vim_free(tofree);
5014 }
5015 break;
5016 case ISN_PUSHFUNC:
5017 {
5018 char *name = (char *)iptr->isn_arg.string;
5019
5020 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5021 name == NULL ? "[none]" : name);
5022 }
5023 break;
5024 case ISN_PUSHCHANNEL:
5025#ifdef FEAT_JOB_CHANNEL
5026 {
5027 channel_T *channel = iptr->isn_arg.channel;
5028
5029 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5030 channel == NULL ? 0 : channel->ch_id);
5031 }
5032#endif
5033 break;
5034 case ISN_PUSHJOB:
5035#ifdef FEAT_JOB_CHANNEL
5036 {
5037 typval_T tv;
5038 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005039 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005040
5041 tv.v_type = VAR_JOB;
5042 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005043 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005044 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5045 }
5046#endif
5047 break;
5048 case ISN_PUSHEXC:
5049 smsg("%s%4d PUSH v:exception", pfx, current);
5050 break;
5051 case ISN_UNLET:
5052 smsg("%s%4d UNLET%s %s", pfx, current,
5053 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5054 iptr->isn_arg.unlet.ul_name);
5055 break;
5056 case ISN_UNLETENV:
5057 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5058 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5059 iptr->isn_arg.unlet.ul_name);
5060 break;
5061 case ISN_UNLETINDEX:
5062 smsg("%s%4d UNLETINDEX", pfx, current);
5063 break;
5064 case ISN_UNLETRANGE:
5065 smsg("%s%4d UNLETRANGE", pfx, current);
5066 break;
5067 case ISN_LOCKCONST:
5068 smsg("%s%4d LOCKCONST", pfx, current);
5069 break;
5070 case ISN_NEWLIST:
5071 smsg("%s%4d NEWLIST size %lld", pfx, current,
5072 (varnumber_T)(iptr->isn_arg.number));
5073 break;
5074 case ISN_NEWDICT:
5075 smsg("%s%4d NEWDICT size %lld", pfx, current,
5076 (varnumber_T)(iptr->isn_arg.number));
5077 break;
5078
5079 // function call
5080 case ISN_BCALL:
5081 {
5082 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5083
5084 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5085 internal_func_name(cbfunc->cbf_idx),
5086 cbfunc->cbf_argcount);
5087 }
5088 break;
5089 case ISN_DCALL:
5090 {
5091 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5092 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5093 + cdfunc->cdf_idx;
5094
5095 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5096 df->df_ufunc->uf_name_exp != NULL
5097 ? df->df_ufunc->uf_name_exp
5098 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5099 }
5100 break;
5101 case ISN_UCALL:
5102 {
5103 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5104
5105 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5106 cufunc->cuf_name, cufunc->cuf_argcount);
5107 }
5108 break;
5109 case ISN_PCALL:
5110 {
5111 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5112
5113 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5114 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5115 }
5116 break;
5117 case ISN_PCALL_END:
5118 smsg("%s%4d PCALL end", pfx, current);
5119 break;
5120 case ISN_RETURN:
5121 smsg("%s%4d RETURN", pfx, current);
5122 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005123 case ISN_RETURN_VOID:
5124 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005125 break;
5126 case ISN_FUNCREF:
5127 {
5128 funcref_T *funcref = &iptr->isn_arg.funcref;
5129 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5130 + funcref->fr_func;
5131
5132 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5133 }
5134 break;
5135
5136 case ISN_NEWFUNC:
5137 {
5138 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5139
5140 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5141 newfunc->nf_lambda, newfunc->nf_global);
5142 }
5143 break;
5144
5145 case ISN_DEF:
5146 {
5147 char_u *name = iptr->isn_arg.string;
5148
5149 smsg("%s%4d DEF %s", pfx, current,
5150 name == NULL ? (char_u *)"" : name);
5151 }
5152 break;
5153
5154 case ISN_JUMP:
5155 {
5156 char *when = "?";
5157
5158 switch (iptr->isn_arg.jump.jump_when)
5159 {
5160 case JUMP_ALWAYS:
5161 when = "JUMP";
5162 break;
5163 case JUMP_AND_KEEP_IF_TRUE:
5164 when = "JUMP_AND_KEEP_IF_TRUE";
5165 break;
5166 case JUMP_IF_FALSE:
5167 when = "JUMP_IF_FALSE";
5168 break;
5169 case JUMP_AND_KEEP_IF_FALSE:
5170 when = "JUMP_AND_KEEP_IF_FALSE";
5171 break;
5172 case JUMP_IF_COND_FALSE:
5173 when = "JUMP_IF_COND_FALSE";
5174 break;
5175 case JUMP_IF_COND_TRUE:
5176 when = "JUMP_IF_COND_TRUE";
5177 break;
5178 }
5179 smsg("%s%4d %s -> %d", pfx, current, when,
5180 iptr->isn_arg.jump.jump_where);
5181 }
5182 break;
5183
5184 case ISN_JUMP_IF_ARG_SET:
5185 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5186 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5187 iptr->isn_arg.jump.jump_where);
5188 break;
5189
5190 case ISN_FOR:
5191 {
5192 forloop_T *forloop = &iptr->isn_arg.forloop;
5193
5194 smsg("%s%4d FOR $%d -> %d", pfx, current,
5195 forloop->for_idx, forloop->for_end);
5196 }
5197 break;
5198
5199 case ISN_TRY:
5200 {
5201 try_T *try = &iptr->isn_arg.try;
5202
5203 if (try->try_ref->try_finally == 0)
5204 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5205 pfx, current,
5206 try->try_ref->try_catch,
5207 try->try_ref->try_endtry);
5208 else
5209 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5210 pfx, current,
5211 try->try_ref->try_catch,
5212 try->try_ref->try_finally,
5213 try->try_ref->try_endtry);
5214 }
5215 break;
5216 case ISN_CATCH:
5217 // TODO
5218 smsg("%s%4d CATCH", pfx, current);
5219 break;
5220 case ISN_TRYCONT:
5221 {
5222 trycont_T *trycont = &iptr->isn_arg.trycont;
5223
5224 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5225 trycont->tct_levels,
5226 trycont->tct_levels == 1 ? "" : "s",
5227 trycont->tct_where);
5228 }
5229 break;
5230 case ISN_FINALLY:
5231 smsg("%s%4d FINALLY", pfx, current);
5232 break;
5233 case ISN_ENDTRY:
5234 smsg("%s%4d ENDTRY", pfx, current);
5235 break;
5236 case ISN_THROW:
5237 smsg("%s%4d THROW", pfx, current);
5238 break;
5239
5240 // expression operations on number
5241 case ISN_OPNR:
5242 case ISN_OPFLOAT:
5243 case ISN_OPANY:
5244 {
5245 char *what;
5246 char *ins;
5247
5248 switch (iptr->isn_arg.op.op_type)
5249 {
5250 case EXPR_MULT: what = "*"; break;
5251 case EXPR_DIV: what = "/"; break;
5252 case EXPR_REM: what = "%"; break;
5253 case EXPR_SUB: what = "-"; break;
5254 case EXPR_ADD: what = "+"; break;
5255 default: what = "???"; break;
5256 }
5257 switch (iptr->isn_type)
5258 {
5259 case ISN_OPNR: ins = "OPNR"; break;
5260 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5261 case ISN_OPANY: ins = "OPANY"; break;
5262 default: ins = "???"; break;
5263 }
5264 smsg("%s%4d %s %s", pfx, current, ins, what);
5265 }
5266 break;
5267
5268 case ISN_COMPAREBOOL:
5269 case ISN_COMPARESPECIAL:
5270 case ISN_COMPARENR:
5271 case ISN_COMPAREFLOAT:
5272 case ISN_COMPARESTRING:
5273 case ISN_COMPAREBLOB:
5274 case ISN_COMPARELIST:
5275 case ISN_COMPAREDICT:
5276 case ISN_COMPAREFUNC:
5277 case ISN_COMPAREANY:
5278 {
5279 char *p;
5280 char buf[10];
5281 char *type;
5282
5283 switch (iptr->isn_arg.op.op_type)
5284 {
5285 case EXPR_EQUAL: p = "=="; break;
5286 case EXPR_NEQUAL: p = "!="; break;
5287 case EXPR_GREATER: p = ">"; break;
5288 case EXPR_GEQUAL: p = ">="; break;
5289 case EXPR_SMALLER: p = "<"; break;
5290 case EXPR_SEQUAL: p = "<="; break;
5291 case EXPR_MATCH: p = "=~"; break;
5292 case EXPR_IS: p = "is"; break;
5293 case EXPR_ISNOT: p = "isnot"; break;
5294 case EXPR_NOMATCH: p = "!~"; break;
5295 default: p = "???"; break;
5296 }
5297 STRCPY(buf, p);
5298 if (iptr->isn_arg.op.op_ic == TRUE)
5299 strcat(buf, "?");
5300 switch(iptr->isn_type)
5301 {
5302 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5303 case ISN_COMPARESPECIAL:
5304 type = "COMPARESPECIAL"; break;
5305 case ISN_COMPARENR: type = "COMPARENR"; break;
5306 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5307 case ISN_COMPARESTRING:
5308 type = "COMPARESTRING"; break;
5309 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5310 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5311 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5312 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5313 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5314 default: type = "???"; break;
5315 }
5316
5317 smsg("%s%4d %s %s", pfx, current, type, buf);
5318 }
5319 break;
5320
5321 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5322 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5323
5324 // expression operations
5325 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5326 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5327 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5328 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5329 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5330 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5331 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5332 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5333 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5334 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5335 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5336 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5337 pfx, current, iptr->isn_arg.number); break;
5338 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5339 pfx, current, iptr->isn_arg.number); break;
5340 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5341 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5342 iptr->isn_arg.string); break;
5343 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5344
5345 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5346 case ISN_CHECKTYPE:
5347 {
5348 checktype_T *ct = &iptr->isn_arg.type;
5349 char *tofree;
5350
5351 if (ct->ct_arg_idx == 0)
5352 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5353 type_name(ct->ct_type, &tofree),
5354 (int)ct->ct_off);
5355 else
5356 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5357 type_name(ct->ct_type, &tofree),
5358 (int)ct->ct_off,
5359 (int)ct->ct_arg_idx);
5360 vim_free(tofree);
5361 break;
5362 }
5363 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5364 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5365 iptr->isn_arg.checklen.cl_min_len);
5366 break;
5367 case ISN_SETTYPE:
5368 {
5369 char *tofree;
5370
5371 smsg("%s%4d SETTYPE %s", pfx, current,
5372 type_name(iptr->isn_arg.type.ct_type, &tofree));
5373 vim_free(tofree);
5374 break;
5375 }
5376 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005377 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5378 smsg("%s%4d INVERT %d (!val)", pfx, current,
5379 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005380 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005381 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5382 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005383 break;
5384 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005385 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005386 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005387 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5388 pfx, current,
5389 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005390 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005391 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5392 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005393 break;
5394 case ISN_PUT:
5395 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5396 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005397 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005398 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5399 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005400 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005401 else
5402 smsg("%s%4d PUT %c %ld", pfx, current,
5403 iptr->isn_arg.put.put_regname,
5404 (long)iptr->isn_arg.put.put_lnum);
5405 break;
5406
5407 // TODO: summarize modifiers
5408 case ISN_CMDMOD:
5409 {
5410 char_u *buf;
5411 size_t len = produce_cmdmods(
5412 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5413
5414 buf = alloc(len + 1);
5415 if (buf != NULL)
5416 {
5417 (void)produce_cmdmods(
5418 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5419 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5420 vim_free(buf);
5421 }
5422 break;
5423 }
5424 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5425
5426 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005427 smsg("%s%4d PROFILE START line %d", pfx, current,
5428 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005429 break;
5430
5431 case ISN_PROF_END:
5432 smsg("%s%4d PROFILE END", pfx, current);
5433 break;
5434
Bram Moolenaare99d4222021-06-13 14:01:26 +02005435 case ISN_DEBUG:
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005436 smsg("%s%4d DEBUG line %d varcount %lld", pfx, current,
5437 iptr->isn_lnum, iptr->isn_arg.number);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005438 break;
5439
Bram Moolenaar4c137212021-04-19 16:48:48 +02005440 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5441 iptr->isn_arg.unpack.unp_count,
5442 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5443 break;
5444 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5445 iptr->isn_arg.shuffle.shfl_item,
5446 iptr->isn_arg.shuffle.shfl_up);
5447 break;
5448 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5449
5450 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5451 return;
5452 }
5453
5454 out_flush(); // output one line at a time
5455 ui_breakcheck();
5456 if (got_int)
5457 break;
5458 }
5459}
5460
5461/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005462 * Handle command line completion for the :disassemble command.
5463 */
5464 void
5465set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5466{
5467 char_u *p;
5468
5469 // Default: expand user functions, "debug" and "profile"
5470 xp->xp_context = EXPAND_DISASSEMBLE;
5471 xp->xp_pattern = arg;
5472
5473 // first argument already typed: only user function names
5474 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5475 {
5476 xp->xp_context = EXPAND_USER_FUNC;
5477 xp->xp_pattern = skipwhite(p);
5478 }
5479}
5480
5481/*
5482 * Function given to ExpandGeneric() to obtain the list of :disassemble
5483 * arguments.
5484 */
5485 char_u *
5486get_disassemble_argument(expand_T *xp, int idx)
5487{
5488 if (idx == 0)
5489 return (char_u *)"debug";
5490 if (idx == 1)
5491 return (char_u *)"profile";
5492 return get_user_func_name(xp, idx - 2);
5493}
5494
5495/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005496 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005497 * We don't really need this at runtime, but we do have tests that require it,
5498 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499 */
5500 void
5501ex_disassemble(exarg_T *eap)
5502{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005503 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005504 char_u *fname;
5505 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005506 dfunc_T *dfunc;
5507 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005508 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005509 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005510 compiletype_T compile_type = CT_NONE;
5511
5512 if (STRNCMP(arg, "profile", 7) == 0)
5513 {
5514 compile_type = CT_PROFILE;
5515 arg = skipwhite(arg + 7);
5516 }
5517 else if (STRNCMP(arg, "debug", 5) == 0)
5518 {
5519 compile_type = CT_DEBUG;
5520 arg = skipwhite(arg + 5);
5521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005522
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005523 if (STRNCMP(arg, "<lambda>", 8) == 0)
5524 {
5525 arg += 8;
5526 (void)getdigits(&arg);
5527 fname = vim_strnsave(eap->arg, arg - eap->arg);
5528 }
5529 else
5530 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005531 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005532 if (fname == NULL)
5533 {
5534 semsg(_(e_invarg2), eap->arg);
5535 return;
5536 }
5537
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005538 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005539 if (ufunc == NULL)
5540 {
5541 char_u *p = untrans_function_name(fname);
5542
5543 if (p != NULL)
5544 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005545 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005546 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005547 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 if (ufunc == NULL)
5549 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005550 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551 return;
5552 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005553 if (func_needs_compiling(ufunc, compile_type)
5554 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005555 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005556 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005558 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559 return;
5560 }
5561 if (ufunc->uf_name_exp != NULL)
5562 msg((char *)ufunc->uf_name_exp);
5563 else
5564 msg((char *)ufunc->uf_name);
5565
5566 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005567 switch (compile_type)
5568 {
5569 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005570#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005571 instr = dfunc->df_instr_prof;
5572 instr_count = dfunc->df_instr_prof_count;
5573 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005574#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005575 // FALLTHROUGH
5576 case CT_NONE:
5577 instr = dfunc->df_instr;
5578 instr_count = dfunc->df_instr_count;
5579 break;
5580 case CT_DEBUG:
5581 instr = dfunc->df_instr_debug;
5582 instr_count = dfunc->df_instr_debug_count;
5583 break;
5584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585
Bram Moolenaar4c137212021-04-19 16:48:48 +02005586 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005587}
5588
5589/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005590 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005591 * list, etc. Mostly like what JavaScript does, except that empty list and
5592 * empty dictionary are FALSE.
5593 */
5594 int
5595tv2bool(typval_T *tv)
5596{
5597 switch (tv->v_type)
5598 {
5599 case VAR_NUMBER:
5600 return tv->vval.v_number != 0;
5601 case VAR_FLOAT:
5602#ifdef FEAT_FLOAT
5603 return tv->vval.v_float != 0.0;
5604#else
5605 break;
5606#endif
5607 case VAR_PARTIAL:
5608 return tv->vval.v_partial != NULL;
5609 case VAR_FUNC:
5610 case VAR_STRING:
5611 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5612 case VAR_LIST:
5613 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5614 case VAR_DICT:
5615 return tv->vval.v_dict != NULL
5616 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5617 case VAR_BOOL:
5618 case VAR_SPECIAL:
5619 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5620 case VAR_JOB:
5621#ifdef FEAT_JOB_CHANNEL
5622 return tv->vval.v_job != NULL;
5623#else
5624 break;
5625#endif
5626 case VAR_CHANNEL:
5627#ifdef FEAT_JOB_CHANNEL
5628 return tv->vval.v_channel != NULL;
5629#else
5630 break;
5631#endif
5632 case VAR_BLOB:
5633 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5634 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005635 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005637 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005638 break;
5639 }
5640 return FALSE;
5641}
5642
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005643 void
5644emsg_using_string_as(typval_T *tv, int as_number)
5645{
5646 semsg(_(as_number ? e_using_string_as_number_str
5647 : e_using_string_as_bool_str),
5648 tv->vval.v_string == NULL
5649 ? (char_u *)"" : tv->vval.v_string);
5650}
5651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652/*
5653 * If "tv" is a string give an error and return FAIL.
5654 */
5655 int
5656check_not_string(typval_T *tv)
5657{
5658 if (tv->v_type == VAR_STRING)
5659 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005660 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661 clear_tv(tv);
5662 return FAIL;
5663 }
5664 return OK;
5665}
5666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667
5668#endif // FEAT_EVAL