blob: 1f2f7357e83bb8ecc67d884bcebae36919af4a90 [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
46// A stack is used to store:
47// - arguments passed to a :def function
48// - info about the calling function, to use when returning
49// - local variables
50// - temporary values
51//
52// In detail (FP == Frame Pointer):
53// arg1 first argument from caller (if present)
54// arg2 second argument from caller (if present)
55// extra_arg1 any missing optional argument default value
56// FP -> cur_func calling function
57// current previous instruction pointer
58// frame_ptr previous Frame Pointer
59// var1 space for local variable
60// var2 space for local variable
61// .... fixed space for max. number of local variables
62// temp temporary values
63// .... flexible space for temporary values (can grow big)
64
65/*
66 * Execution context.
67 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010068struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010069 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020070 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020071 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072
Bram Moolenaar0186e582021-01-10 18:33:11 +010073 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020074 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076 garray_T ec_trystack; // stack of trycmd_T values
77 int ec_in_catch; // when TRUE in catch or finally block
78
79 int ec_dfunc_idx; // current function index
80 isn_T *ec_instr; // array with instructions
81 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020082
83 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020084
85 int ec_did_emsg_before;
86 int ec_trylevel_at_start;
87 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010088};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010089
Bram Moolenaar12d26532021-02-19 19:13:21 +010090#ifdef FEAT_PROFILE
91// stack of profinfo_T used when profiling.
92static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
93#endif
94
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020096#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 +010097
Bram Moolenaar418f1df2020-08-12 21:34:49 +020098 void
99to_string_error(vartype_T vartype)
100{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200101 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200102}
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100105 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106 */
107 static int
108ufunc_argcount(ufunc_T *ufunc)
109{
110 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100144 * This adds a stack frame and sets the instruction pointer to the start of the
145 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100146 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147 *
148 * Stack has:
149 * - current arguments (already there)
150 * - omitted optional argument (default values) added here
151 * - stack frame:
152 * - pointer to calling function
153 * - Index of next instruction in calling function
154 * - previous frame pointer
155 * - reserved space for local variables
156 */
157 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100158call_dfunc(
159 int cdf_idx,
160 partial_T *pt,
161 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100162 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100164 int argcount = argcount_arg;
165 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
166 ufunc_T *ufunc = dfunc->df_ufunc;
167 int arg_to_add;
168 int vararg_count = 0;
169 int varcount;
170 int idx;
171 estack_T *entry;
172 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100176 // don't use ufunc->uf_name, it may have been freed
177 emsg_funcname(e_func_deleted,
178 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 return FAIL;
180 }
181
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100182#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100183 if (do_profiling == PROF_YES)
184 {
185 if (ga_grow(&profile_info_ga, 1) == OK)
186 {
187 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
188 + profile_info_ga.ga_len;
189 ++profile_info_ga.ga_len;
190 CLEAR_POINTER(info);
191 profile_may_start_func(info, ufunc,
192 (((dfunc_T *)def_functions.ga_data)
193 + ectx->ec_dfunc_idx)->df_ufunc);
194 }
195
196 // Profiling might be enabled/disabled along the way. This should not
197 // fail, since the function was compiled before and toggling profiling
198 // doesn't change any errors.
199 if (func_needs_compiling(ufunc, PROFILING(ufunc))
200 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100201 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100202 return FAIL;
203 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100204#endif
205
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200206 if (ufunc->uf_va_name != NULL)
207 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200208 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200209 // Stack at time of call with 2 varargs:
210 // normal_arg
211 // optional_arg
212 // vararg_1
213 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200214 // After creating the list:
215 // normal_arg
216 // optional_arg
217 // vararg-list
218 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200219 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200220 // After creating the list
221 // normal_arg
222 // (space for optional_arg)
223 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200224 vararg_count = argcount - ufunc->uf_args.ga_len;
225 if (vararg_count < 0)
226 vararg_count = 0;
227 else
228 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200229 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200230 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200231
232 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200233 }
234
Bram Moolenaarfe270812020-04-11 22:31:27 +0200235 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200236 if (arg_to_add < 0)
237 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200238 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200239 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200240 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200241 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200242 return FAIL;
243 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200244
245 // Reserve space for:
246 // - missing arguments
247 // - stack frame
248 // - local variables
249 // - if needed: a counter for number of closures created in
250 // ectx->ec_funcrefs.
251 varcount = dfunc->df_varcount + dfunc->df_has_closure;
252 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
253 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254 return FAIL;
255
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100256 // If depth of calling is getting too high, don't execute the function.
257 if (funcdepth_increment() == FAIL)
258 return FAIL;
259
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100260 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200261 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100262 {
263 floc = ALLOC_ONE(funclocal_T);
264 if (floc == NULL)
265 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200266 *floc = ectx->ec_funclocal;
267 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100268 }
269
Bram Moolenaarfe270812020-04-11 22:31:27 +0200270 // Move the vararg-list to below the missing optional arguments.
271 if (vararg_count > 0 && arg_to_add > 0)
272 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100273
274 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200275 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200276 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200277 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278
279 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
281 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200282 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100283 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100284 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100285 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200286 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287
288 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200289 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200291 if (dfunc->df_has_closure)
292 {
293 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
294
295 tv->v_type = VAR_NUMBER;
296 tv->vval.v_number = 0;
297 }
298 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100300 if (pt != NULL || ufunc->uf_partial != NULL
301 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100302 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100303 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
304
305 if (outer == NULL)
306 return FAIL;
307 if (pt != NULL)
308 {
309 *outer = pt->pt_outer;
310 outer->out_up_is_copy = TRUE;
311 }
312 else if (ufunc->uf_partial != NULL)
313 {
314 *outer = ufunc->uf_partial->pt_outer;
315 outer->out_up_is_copy = TRUE;
316 }
317 else
318 {
319 outer->out_stack = &ectx->ec_stack;
320 outer->out_frame_idx = ectx->ec_frame_idx;
321 outer->out_up = ectx->ec_outer;
322 }
323 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100324 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100325 else
326 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100327
Bram Moolenaarc970e422021-03-17 15:03:04 +0100328 ++ufunc->uf_calls;
329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330 // Set execution state to the start of the called function.
331 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100332 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100333 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200334 if (entry != NULL)
335 {
336 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200337 // Save the current context so it can be restored on return.
338 entry->es_save_sctx = current_sctx;
339 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200340 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100341
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200342 // Start execution at the first instruction.
343 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344
345 return OK;
346}
347
348// Get pointer to item in the stack.
349#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
350
351/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200352 * Used when returning from a function: Check if any closure is still
353 * referenced. If so then move the arguments and variables to a separate piece
354 * of stack to be used when the closure is called.
355 * When "free_arguments" is TRUE the arguments are to be freed.
356 * Returns FAIL when out of memory.
357 */
358 static int
359handle_closure_in_use(ectx_T *ectx, int free_arguments)
360{
361 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
362 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200363 int argcount;
364 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200365 int idx;
366 typval_T *tv;
367 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200368 garray_T *gap = &ectx->ec_funcrefs;
369 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200370
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200371 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200372 return OK; // function was freed
373 if (dfunc->df_has_closure == 0)
374 return OK; // no closures
375 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
376 closure_count = tv->vval.v_number;
377 if (closure_count == 0)
378 return OK; // no funcrefs created
379
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200380 argcount = ufunc_argcount(dfunc->df_ufunc);
381 top = ectx->ec_frame_idx - argcount;
382
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200383 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200384 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200385 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200386 partial_T *pt;
387 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200389 if (off < 0)
390 continue; // count is off or already done
391 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200392 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200393 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200394 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200395 int i;
396
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200397 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200398 // unreferenced on return.
399 for (i = 0; i < dfunc->df_varcount; ++i)
400 {
401 typval_T *stv = STACK_TV(ectx->ec_frame_idx
402 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200403 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200404 --refcount;
405 }
406 if (refcount > 1)
407 {
408 closure_in_use = TRUE;
409 break;
410 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200411 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200412 }
413
414 if (closure_in_use)
415 {
416 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
417 typval_T *stack;
418
419 // A closure is using the arguments and/or local variables.
420 // Move them to the called function.
421 if (funcstack == NULL)
422 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200423 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
424 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200425 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
426 funcstack->fs_ga.ga_data = stack;
427 if (stack == NULL)
428 {
429 vim_free(funcstack);
430 return FAIL;
431 }
432
433 // Move or copy the arguments.
434 for (idx = 0; idx < argcount; ++idx)
435 {
436 tv = STACK_TV(top + idx);
437 if (free_arguments)
438 {
439 *(stack + idx) = *tv;
440 tv->v_type = VAR_UNKNOWN;
441 }
442 else
443 copy_tv(tv, stack + idx);
444 }
445 // Move the local variables.
446 for (idx = 0; idx < dfunc->df_varcount; ++idx)
447 {
448 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200449
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200450 // A partial created for a local function, that is also used as a
451 // local variable, has a reference count for the variable, thus
452 // will never go down to zero. When all these refcounts are one
453 // then the funcstack is unused. We need to count how many we have
454 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200455 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
456 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200457 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200458
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200459 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200460 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
461 gap->ga_len - closure_count + i])
462 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200463 }
464
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200465 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 tv->v_type = VAR_UNKNOWN;
467 }
468
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200469 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200470 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200471 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
472 - closure_count + idx];
473 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200474 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200475 ++funcstack->fs_refcount;
476 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100477 pt->pt_outer.out_stack = &funcstack->fs_ga;
478 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
479 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200480 }
481 }
482 }
483
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200484 for (idx = 0; idx < closure_count; ++idx)
485 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
486 - closure_count + idx]);
487 gap->ga_len -= closure_count;
488 if (gap->ga_len == 0)
489 ga_clear(gap);
490
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200491 return OK;
492}
493
494/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200495 * Called when a partial is freed or its reference count goes down to one. The
496 * funcstack may be the only reference to the partials in the local variables.
497 * Go over all of them, the funcref and can be freed if all partials
498 * referencing the funcstack have a reference count of one.
499 */
500 void
501funcstack_check_refcount(funcstack_T *funcstack)
502{
503 int i;
504 garray_T *gap = &funcstack->fs_ga;
505 int done = 0;
506
507 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
508 return;
509 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
510 {
511 typval_T *tv = ((typval_T *)gap->ga_data) + i;
512
513 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
514 && tv->vval.v_partial->pt_funcstack == funcstack
515 && tv->vval.v_partial->pt_refcount == 1)
516 ++done;
517 }
518 if (done == funcstack->fs_min_refcount)
519 {
520 typval_T *stack = gap->ga_data;
521
522 // All partials referencing the funcstack have a reference count of
523 // one, thus the funcstack is no longer of use.
524 for (i = 0; i < gap->ga_len; ++i)
525 clear_tv(stack + i);
526 vim_free(stack);
527 vim_free(funcstack);
528 }
529}
530
531/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 * Return from the current function.
533 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200534 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200535func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100538 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200539 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
540 + ectx->ec_dfunc_idx;
541 int argcount = ufunc_argcount(dfunc->df_ufunc);
542 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200543 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100544 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
545 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200546 funclocal_T *floc;
547#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100548 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
549 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550
Bram Moolenaar12d26532021-02-19 19:13:21 +0100551 if (do_profiling == PROF_YES)
552 {
553 ufunc_T *caller = prev_dfunc->df_ufunc;
554
555 if (dfunc->df_ufunc->uf_profiling
556 || (caller != NULL && caller->uf_profiling))
557 {
558 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
559 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
560 --profile_info_ga.ga_len;
561 }
562 }
563#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100564 // TODO: when is it safe to delete the function when it is no longer used?
565 --dfunc->df_ufunc->uf_calls;
566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200568 entry = estack_pop();
569 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200570 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200572 if (handle_closure_in_use(ectx, TRUE) == FAIL)
573 return FAIL;
574
575 // Clear the arguments.
576 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
577 clear_tv(STACK_TV(idx));
578
579 // Clear local variables and temp values, but not the return value.
580 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100581 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100583
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100584 // The return value should be on top of the stack. However, when aborting
585 // it may not be there and ec_frame_idx is the top of the stack.
586 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100587 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100588 ret_idx = 0;
589
Bram Moolenaar0186e582021-01-10 18:33:11 +0100590 vim_free(ectx->ec_outer);
591
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100592 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100593 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100594 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
595 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200596 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
597 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100598 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
599 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100600 floc = (void *)STACK_TV(ectx->ec_frame_idx
601 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200602 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100603 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
604 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200605
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100606 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200607 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100608 else
609 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200610 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100611 vim_free(floc);
612 }
613
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100614 if (ret_idx > 0)
615 {
616 // Reset the stack to the position before the call, with a spot for the
617 // return value, moved there from above the frame.
618 ectx->ec_stack.ga_len = top + 1;
619 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
620 }
621 else
622 // Reset the stack to the position before the call.
623 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200624
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100625 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627}
628
629#undef STACK_TV
630
631/*
632 * Prepare arguments and rettv for calling a builtin or user function.
633 */
634 static int
635call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
636{
637 int idx;
638 typval_T *tv;
639
640 // Move arguments from bottom of the stack to argvars[] and add terminator.
641 for (idx = 0; idx < argcount; ++idx)
642 argvars[idx] = *STACK_TV_BOT(idx - argcount);
643 argvars[argcount].v_type = VAR_UNKNOWN;
644
645 // Result replaces the arguments on the stack.
646 if (argcount > 0)
647 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200648 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 return FAIL;
650 else
651 ++ectx->ec_stack.ga_len;
652
653 // Default return value is zero.
654 tv = STACK_TV_BOT(-1);
655 tv->v_type = VAR_NUMBER;
656 tv->vval.v_number = 0;
657
658 return OK;
659}
660
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200661// Ugly global to avoid passing the execution context around through many
662// layers.
663static ectx_T *current_ectx = NULL;
664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665/*
666 * Call a builtin function by index.
667 */
668 static int
669call_bfunc(int func_idx, int argcount, ectx_T *ectx)
670{
671 typval_T argvars[MAX_FUNC_ARGS];
672 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100673 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200674 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 if (call_prepare(argcount, argvars, ectx) == FAIL)
677 return FAIL;
678
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200679 // Call the builtin function. Set "current_ectx" so that when it
680 // recursively invokes call_def_function() a closure context can be set.
681 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200683 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684
685 // Clear the arguments.
686 for (idx = 0; idx < argcount; ++idx)
687 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200688
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100689 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200690 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 return OK;
692}
693
694/*
695 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100696 * If the function is compiled this will add a stack frame and set the
697 * instruction pointer at the start of the function.
698 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100699 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100700 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 */
702 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100703call_ufunc(
704 ufunc_T *ufunc,
705 partial_T *pt,
706 int argcount,
707 ectx_T *ectx,
708 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709{
710 typval_T argvars[MAX_FUNC_ARGS];
711 funcexe_T funcexe;
712 int error;
713 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100714 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100715#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100716 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100717#else
718# define profiling FALSE
719#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720
Bram Moolenaarb2049902021-01-24 12:53:53 +0100721 if (func_needs_compiling(ufunc, profiling)
722 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200723 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200724 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100725 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100726 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100727 if (error != FCERR_UNKNOWN)
728 {
729 if (error == FCERR_TOOMANY)
730 semsg(_(e_toomanyarg), ufunc->uf_name);
731 else
732 semsg(_(e_toofewarg), ufunc->uf_name);
733 return FAIL;
734 }
735
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100736 // The function has been compiled, can call it quickly. For a function
737 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100738 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100739 if (iptr != NULL)
740 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100741 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100742 iptr->isn_type = ISN_DCALL;
743 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
744 iptr->isn_arg.dfunc.cdf_argcount = argcount;
745 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200746 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748
749 if (call_prepare(argcount, argvars, ectx) == FAIL)
750 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200751 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 funcexe.evaluate = TRUE;
753
754 // Call the user function. Result goes in last position on the stack.
755 // TODO: add selfdict if there is one
756 error = call_user_func_check(ufunc, argcount, argvars,
757 STACK_TV_BOT(-1), &funcexe, NULL);
758
759 // Clear the arguments.
760 for (idx = 0; idx < argcount; ++idx)
761 clear_tv(&argvars[idx]);
762
763 if (error != FCERR_NONE)
764 {
765 user_func_error(error, ufunc->uf_name);
766 return FAIL;
767 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100768 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200769 // Error other than from calling the function itself.
770 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 return OK;
772}
773
774/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100775 * If command modifiers were applied restore them.
776 */
777 static void
778may_restore_cmdmod(funclocal_T *funclocal)
779{
780 if (funclocal->floc_restore_cmdmod)
781 {
782 cmdmod.cmod_filter_regmatch.regprog = NULL;
783 undo_cmdmod(&cmdmod);
784 cmdmod = funclocal->floc_save_cmdmod;
785 funclocal->floc_restore_cmdmod = FALSE;
786 }
787}
788
789/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200790 * Return TRUE if an error was given or CTRL-C was pressed.
791 */
792 static int
793vim9_aborting(int prev_called_emsg)
794{
795 return called_emsg > prev_called_emsg || got_int || did_throw;
796}
797
798/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 * Execute a function by "name".
800 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100801 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802 * Returns FAIL if not found without an error message.
803 */
804 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100805call_by_name(
806 char_u *name,
807 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100808 ectx_T *ectx,
809 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810{
811 ufunc_T *ufunc;
812
813 if (builtin_function(name, -1))
814 {
815 int func_idx = find_internal_func(name);
816
817 if (func_idx < 0)
818 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200819 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 return FAIL;
821 return call_bfunc(func_idx, argcount, ectx);
822 }
823
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200824 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200825
826 if (ufunc == NULL)
827 {
828 int called_emsg_before = called_emsg;
829
830 if (script_autoload(name, TRUE))
831 // loaded a package, search for the function again
832 ufunc = find_func(name, FALSE, NULL);
833 if (vim9_aborting(called_emsg_before))
834 return FAIL; // bail out if loading the script caused an error
835 }
836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100838 {
839 if (ufunc->uf_arg_types != NULL)
840 {
841 int i;
842 typval_T *argv = STACK_TV_BOT(0) - argcount;
843
844 // The function can change at runtime, check that the argument
845 // types are correct.
846 for (i = 0; i < argcount; ++i)
847 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100848 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100849
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100850 if (i < ufunc->uf_args.ga_len)
851 type = ufunc->uf_arg_types[i];
852 else if (ufunc->uf_va_type != NULL)
853 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100854 if (type != NULL && check_typval_arg_type(type,
855 &argv[i], i + 1) == FAIL)
856 return FAIL;
857 }
858 }
859
Bram Moolenaar4c137212021-04-19 16:48:48 +0200860 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862
863 return FAIL;
864}
865
866 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100867call_partial(
868 typval_T *tv,
869 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100870 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200872 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200873 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100875 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876
877 if (tv->v_type == VAR_PARTIAL)
878 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200879 partial_T *pt = tv->vval.v_partial;
880 int i;
881
882 if (pt->pt_argc > 0)
883 {
884 // Make space for arguments from the partial, shift the "argcount"
885 // arguments up.
886 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
887 return FAIL;
888 for (i = 1; i <= argcount; ++i)
889 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
890 ectx->ec_stack.ga_len += pt->pt_argc;
891 argcount += pt->pt_argc;
892
893 // copy the arguments from the partial onto the stack
894 for (i = 0; i < pt->pt_argc; ++i)
895 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897
898 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200899 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 name = pt->pt_name;
902 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200903 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200905 if (name != NULL)
906 {
907 char_u fname_buf[FLEN_FIXED + 1];
908 char_u *tofree = NULL;
909 int error = FCERR_NONE;
910 char_u *fname;
911
912 // May need to translate <SNR>123_ to K_SNR.
913 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
914 if (error != FCERR_NONE)
915 res = FAIL;
916 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200917 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200918 vim_free(tofree);
919 }
920
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100921 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 {
923 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200924 semsg(_(e_unknownfunc),
925 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 return FAIL;
927 }
928 return OK;
929}
930
931/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200932 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
933 * TRUE.
934 */
935 static int
936error_if_locked(int lock, char *error)
937{
938 if (lock & (VAR_LOCKED | VAR_FIXED))
939 {
940 emsg(_(error));
941 return TRUE;
942 }
943 return FALSE;
944}
945
946/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100947 * Give an error if "tv" is not a number and return FAIL.
948 */
949 static int
950check_for_number(typval_T *tv)
951{
952 if (tv->v_type != VAR_NUMBER)
953 {
954 semsg(_(e_expected_str_but_got_str),
955 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
956 return FAIL;
957 }
958 return OK;
959}
960
961/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100962 * Store "tv" in variable "name".
963 * This is for s: and g: variables.
964 */
965 static void
966store_var(char_u *name, typval_T *tv)
967{
968 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200969 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100970
Bram Moolenaard877a572021-04-01 19:42:48 +0200971 if (tv->v_lock)
972 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100973 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +0200974 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100975 restore_funccal();
976}
977
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100978/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100979 * Convert "tv" to a string.
980 * Return FAIL if not allowed.
981 */
982 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200983do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100984{
985 if (tv->v_type != VAR_STRING)
986 {
987 char_u *str;
988
989 if (is_2string_any)
990 {
991 switch (tv->v_type)
992 {
993 case VAR_SPECIAL:
994 case VAR_BOOL:
995 case VAR_NUMBER:
996 case VAR_FLOAT:
997 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200998
999 case VAR_LIST:
1000 if (tolerant)
1001 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001002 char_u *s, *e, *p;
1003 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001004
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001005 ga_init2(&ga, sizeof(char_u *), 1);
1006
1007 // Convert to NL separated items, then
1008 // escape the items and replace the NL with
1009 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001010 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001011 if (str == NULL)
1012 return FAIL;
1013 s = str;
1014 while ((e = vim_strchr(s, '\n')) != NULL)
1015 {
1016 *e = NUL;
1017 p = vim_strsave_fnameescape(s, FALSE);
1018 if (p != NULL)
1019 {
1020 ga_concat(&ga, p);
1021 ga_concat(&ga, (char_u *)" ");
1022 vim_free(p);
1023 }
1024 s = e + 1;
1025 }
1026 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001027 clear_tv(tv);
1028 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001029 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001030 return OK;
1031 }
1032 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001033 default: to_string_error(tv->v_type);
1034 return FAIL;
1035 }
1036 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001037 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001038 clear_tv(tv);
1039 tv->v_type = VAR_STRING;
1040 tv->vval.v_string = str;
1041 }
1042 return OK;
1043}
1044
1045/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001046 * When the value of "sv" is a null list of dict, allocate it.
1047 */
1048 static void
1049allocate_if_null(typval_T *tv)
1050{
1051 switch (tv->v_type)
1052 {
1053 case VAR_LIST:
1054 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001055 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001056 break;
1057 case VAR_DICT:
1058 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001059 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001060 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001061 case VAR_BLOB:
1062 if (tv->vval.v_blob == NULL)
1063 (void)rettv_blob_alloc(tv);
1064 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001065 default:
1066 break;
1067 }
1068}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001069
Bram Moolenaare7525c52021-01-09 13:20:37 +01001070/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001071 * Return the character "str[index]" where "index" is the character index,
1072 * including composing characters.
1073 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001074 */
1075 char_u *
1076char_from_string(char_u *str, varnumber_T index)
1077{
1078 size_t nbyte = 0;
1079 varnumber_T nchar = index;
1080 size_t slen;
1081
1082 if (str == NULL)
1083 return NULL;
1084 slen = STRLEN(str);
1085
Bram Moolenaarff871402021-03-26 13:34:05 +01001086 // Do the same as for a list: a negative index counts from the end.
1087 // Optimization to check the first byte to be below 0x80 (and no composing
1088 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001089 if (index < 0)
1090 {
1091 int clen = 0;
1092
1093 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001094 {
1095 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1096 ++nbyte;
1097 else if (enc_utf8)
1098 nbyte += utfc_ptr2len(str + nbyte);
1099 else
1100 nbyte += mb_ptr2len(str + nbyte);
1101 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001102 nchar = clen + index;
1103 if (nchar < 0)
1104 // unlike list: index out of range results in empty string
1105 return NULL;
1106 }
1107
1108 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001109 {
1110 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1111 ++nbyte;
1112 else if (enc_utf8)
1113 nbyte += utfc_ptr2len(str + nbyte);
1114 else
1115 nbyte += mb_ptr2len(str + nbyte);
1116 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001117 if (nbyte >= slen)
1118 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001119 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001120}
1121
1122/*
1123 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001124 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001125 * If going over the end return "str_len".
1126 * If "idx" is negative count from the end, -1 is the last character.
1127 * When going over the start return -1.
1128 */
1129 static long
1130char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1131{
1132 varnumber_T nchar = idx;
1133 size_t nbyte = 0;
1134
1135 if (nchar >= 0)
1136 {
1137 while (nchar > 0 && nbyte < str_len)
1138 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001139 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001140 --nchar;
1141 }
1142 }
1143 else
1144 {
1145 nbyte = str_len;
1146 while (nchar < 0 && nbyte > 0)
1147 {
1148 --nbyte;
1149 nbyte -= mb_head_off(str, str + nbyte);
1150 ++nchar;
1151 }
1152 if (nchar < 0)
1153 return -1;
1154 }
1155 return (long)nbyte;
1156}
1157
1158/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001159 * Return the slice "str[first : last]" using character indexes. Composing
1160 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001161 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001162 * Return NULL when the result is empty.
1163 */
1164 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001165string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001166{
1167 long start_byte, end_byte;
1168 size_t slen;
1169
1170 if (str == NULL)
1171 return NULL;
1172 slen = STRLEN(str);
1173 start_byte = char_idx2byte(str, slen, first);
1174 if (start_byte < 0)
1175 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001176 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001177 end_byte = (long)slen;
1178 else
1179 {
1180 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001181 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001182 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001183 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001184 }
1185
1186 if (start_byte >= (long)slen || end_byte <= start_byte)
1187 return NULL;
1188 return vim_strnsave(str + start_byte, end_byte - start_byte);
1189}
1190
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001191 static svar_T *
1192get_script_svar(scriptref_T *sref, ectx_T *ectx)
1193{
1194 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1195 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1196 + ectx->ec_dfunc_idx;
1197 svar_T *sv;
1198
1199 if (sref->sref_seq != si->sn_script_seq)
1200 {
1201 // The script was reloaded after the function was
1202 // compiled, the script_idx may not be valid.
1203 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1204 dfunc->df_ufunc->uf_name_exp);
1205 return NULL;
1206 }
1207 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1208 if (!equal_type(sv->sv_type, sref->sref_type))
1209 {
1210 emsg(_(e_script_variable_type_changed));
1211 return NULL;
1212 }
1213 return sv;
1214}
1215
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001216/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001217 * Function passed to do_cmdline() for splitting a script joined by NL
1218 * characters.
1219 */
1220 static char_u *
1221get_split_sourceline(
1222 int c UNUSED,
1223 void *cookie,
1224 int indent UNUSED,
1225 getline_opt_T options UNUSED)
1226{
1227 source_cookie_T *sp = (source_cookie_T *)cookie;
1228 char_u *p;
1229 char_u *line;
1230
1231 if (*sp->nextline == NUL)
1232 return NULL;
1233 p = vim_strchr(sp->nextline, '\n');
1234 if (p == NULL)
1235 {
1236 line = vim_strsave(sp->nextline);
1237 sp->nextline += STRLEN(sp->nextline);
1238 }
1239 else
1240 {
1241 line = vim_strnsave(sp->nextline, p - sp->nextline);
1242 sp->nextline = p + 1;
1243 }
1244 return line;
1245}
1246
1247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 * Execute a function by "name".
1249 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001250 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 */
1252 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001253call_eval_func(
1254 char_u *name,
1255 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001256 ectx_T *ectx,
1257 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258{
Bram Moolenaared677f52020-08-12 16:38:10 +02001259 int called_emsg_before = called_emsg;
1260 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261
Bram Moolenaar4c137212021-04-19 16:48:48 +02001262 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001263 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001265 dictitem_T *v;
1266
1267 v = find_var(name, NULL, FALSE);
1268 if (v == NULL)
1269 {
1270 semsg(_(e_unknownfunc), name);
1271 return FAIL;
1272 }
1273 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1274 {
1275 semsg(_(e_unknownfunc), name);
1276 return FAIL;
1277 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001278 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001280 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281}
1282
1283/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001284 * When a function reference is used, fill a partial with the information
1285 * needed, especially when it is used as a closure.
1286 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001287 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001288fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1289{
1290 pt->pt_func = ufunc;
1291 pt->pt_refcount = 1;
1292
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001293 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001294 {
1295 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1296 + ectx->ec_dfunc_idx;
1297
1298 // The closure needs to find arguments and local
1299 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001300 pt->pt_outer.out_stack = &ectx->ec_stack;
1301 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1302 pt->pt_outer.out_up = ectx->ec_outer;
1303 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001304
1305 // If this function returns and the closure is still
1306 // being used, we need to make a copy of the context
1307 // (arguments and local variables). Store a reference
1308 // to the partial so we can handle that.
1309 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1310 {
1311 vim_free(pt);
1312 return FAIL;
1313 }
1314 // Extra variable keeps the count of closures created
1315 // in the current function call.
1316 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1317 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1318
1319 ((partial_T **)ectx->ec_funcrefs.ga_data)
1320 [ectx->ec_funcrefs.ga_len] = pt;
1321 ++pt->pt_refcount;
1322 ++ectx->ec_funcrefs.ga_len;
1323 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001324 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001325 return OK;
1326}
1327
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001328// used for v_instr of typval of VAR_INSTR
1329struct instr_S {
1330 ectx_T *instr_ectx;
1331 isn_T *instr_instr;
1332};
1333
Bram Moolenaar4c137212021-04-19 16:48:48 +02001334// used for substitute_instr
1335typedef struct subs_expr_S {
1336 ectx_T *subs_ectx;
1337 isn_T *subs_instr;
1338 int subs_status;
1339} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340
1341// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001342#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343
1344// Get pointer to item at the bottom of the stack, -1 is the bottom.
1345#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001346#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 +01001347
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001348// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001349#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 +01001350
Bram Moolenaar4c137212021-04-19 16:48:48 +02001351/*
1352 * Execute instructions in execution context "ectx".
1353 * Return OK or FAIL;
1354 */
1355 static int
1356exec_instructions(ectx_T *ectx)
1357{
1358 int breakcheck_count = 0;
1359 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001360 int ret = FAIL;
1361 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001362
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001363 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001364 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001365
Bram Moolenaarff652882021-05-16 15:24:49 +02001366 // Only catch exceptions in this instruction list.
1367 ectx->ec_trylevel_at_start = trylevel;
1368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 for (;;)
1370 {
1371 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001372
Bram Moolenaar270d0382020-05-15 21:42:53 +02001373 if (++breakcheck_count >= 100)
1374 {
1375 line_breakcheck();
1376 breakcheck_count = 0;
1377 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001378 if (got_int)
1379 {
1380 // Turn CTRL-C into an exception.
1381 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001382 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001383 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001384 did_throw = TRUE;
1385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386
Bram Moolenaara26b9702020-04-18 19:53:28 +02001387 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1388 {
1389 // Turn an error message into an exception.
1390 did_emsg = FALSE;
1391 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001392 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001393 did_throw = TRUE;
1394 *msg_list = NULL;
1395 }
1396
Bram Moolenaar4c137212021-04-19 16:48:48 +02001397 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001399 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001400 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401
1402 // An exception jumps to the first catch, finally, or returns from
1403 // the current function.
1404 if (trystack->ga_len > 0)
1405 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001406 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 {
1408 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001409 ectx->ec_in_catch = TRUE;
1410 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 }
1412 else
1413 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001414 // Not inside try or need to return from current functions.
1415 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001416 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001417 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001418 tv = STACK_TV_BOT(0);
1419 tv->v_type = VAR_NUMBER;
1420 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001421 ++ectx->ec_stack.ga_len;
1422 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001424 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001425 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001426 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001427 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 goto done;
1429 }
1430
Bram Moolenaar4c137212021-04-19 16:48:48 +02001431 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001432 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 }
1434 continue;
1435 }
1436
Bram Moolenaar4c137212021-04-19 16:48:48 +02001437 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 switch (iptr->isn_type)
1439 {
1440 // execute Ex command line
1441 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001442 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001443 source_cookie_T cookie;
1444
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001445 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001446 // Pass getsourceline to get an error for a missing ":end"
1447 // command.
1448 CLEAR_FIELD(cookie);
1449 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1450 if (do_cmdline(iptr->isn_arg.string,
1451 getsourceline, &cookie,
1452 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1453 == FAIL
1454 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001455 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 break;
1458
Bram Moolenaar20677332021-06-06 17:02:53 +02001459 // execute Ex command line split at NL characters.
1460 case ISN_EXEC_SPLIT:
1461 {
1462 source_cookie_T cookie;
1463
1464 SOURCING_LNUM = iptr->isn_lnum;
1465 CLEAR_FIELD(cookie);
1466 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1467 cookie.nextline = iptr->isn_arg.string;
1468 if (do_cmdline(get_split_sourceline(0, &cookie, 0, 0),
1469 get_split_sourceline, &cookie,
1470 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1471 == FAIL
1472 || did_emsg)
1473 goto on_error;
1474 }
1475 break;
1476
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001477 // Evaluate an expression with legacy syntax, push it onto the
1478 // stack.
1479 case ISN_LEGACY_EVAL:
1480 {
1481 char_u *arg = iptr->isn_arg.string;
1482 int res;
1483 int save_flags = cmdmod.cmod_flags;
1484
1485 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001486 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001487 tv = STACK_TV_BOT(0);
1488 init_tv(tv);
1489 cmdmod.cmod_flags |= CMOD_LEGACY;
1490 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1491 cmdmod.cmod_flags = save_flags;
1492 if (res == FAIL)
1493 goto on_error;
1494 ++ectx->ec_stack.ga_len;
1495 }
1496 break;
1497
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001498 // push typeval VAR_INSTR with instructions to be executed
1499 case ISN_INSTR:
1500 {
1501 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001502 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001503 tv = STACK_TV_BOT(0);
1504 tv->vval.v_instr = ALLOC_ONE(instr_T);
1505 if (tv->vval.v_instr == NULL)
1506 goto on_error;
1507 ++ectx->ec_stack.ga_len;
1508
1509 tv->v_type = VAR_INSTR;
1510 tv->vval.v_instr->instr_ectx = ectx;
1511 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1512 }
1513 break;
1514
Bram Moolenaar4c137212021-04-19 16:48:48 +02001515 // execute :substitute with an expression
1516 case ISN_SUBSTITUTE:
1517 {
1518 subs_T *subs = &iptr->isn_arg.subs;
1519 source_cookie_T cookie;
1520 struct subs_expr_S *save_instr = substitute_instr;
1521 struct subs_expr_S subs_instr;
1522 int res;
1523
1524 subs_instr.subs_ectx = ectx;
1525 subs_instr.subs_instr = subs->subs_instr;
1526 subs_instr.subs_status = OK;
1527 substitute_instr = &subs_instr;
1528
1529 SOURCING_LNUM = iptr->isn_lnum;
1530 // This is very much like ISN_EXEC
1531 CLEAR_FIELD(cookie);
1532 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1533 res = do_cmdline(subs->subs_cmd,
1534 getsourceline, &cookie,
1535 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1536 substitute_instr = save_instr;
1537
1538 if (res == FAIL || did_emsg
1539 || subs_instr.subs_status == FAIL)
1540 goto on_error;
1541 }
1542 break;
1543
1544 case ISN_FINISH:
1545 goto done;
1546
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001547 case ISN_REDIRSTART:
1548 // create a dummy entry for var_redir_str()
1549 if (alloc_redir_lval() == FAIL)
1550 goto on_error;
1551
1552 // The output is stored in growarray "redir_ga" until
1553 // redirection ends.
1554 init_redir_ga();
1555 redir_vname = 1;
1556 break;
1557
1558 case ISN_REDIREND:
1559 {
1560 char_u *res = get_clear_redir_ga();
1561
1562 // End redirection, put redirected text on the stack.
1563 clear_redir_lval();
1564 redir_vname = 0;
1565
1566 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1567 {
1568 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001569 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001570 }
1571 tv = STACK_TV_BOT(0);
1572 tv->v_type = VAR_STRING;
1573 tv->vval.v_string = res;
1574 ++ectx->ec_stack.ga_len;
1575 }
1576 break;
1577
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001578 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001579#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001580 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1581 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001582#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001583 break;
1584
1585 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001586#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001587 {
1588 exarg_T ea;
1589 int res;
1590
1591 CLEAR_FIELD(ea);
1592 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1593 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1594 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1595 --ectx->ec_stack.ga_len;
1596 tv = STACK_TV_BOT(0);
1597 res = cexpr_core(&ea, tv);
1598 clear_tv(tv);
1599 if (res == FAIL)
1600 goto on_error;
1601 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001602#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001603 break;
1604
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001605 // execute Ex command from pieces on the stack
1606 case ISN_EXECCONCAT:
1607 {
1608 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001609 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001610 int pass;
1611 int i;
1612 char_u *cmd = NULL;
1613 char_u *str;
1614
1615 for (pass = 1; pass <= 2; ++pass)
1616 {
1617 for (i = 0; i < count; ++i)
1618 {
1619 tv = STACK_TV_BOT(i - count);
1620 str = tv->vval.v_string;
1621 if (str != NULL && *str != NUL)
1622 {
1623 if (pass == 2)
1624 STRCPY(cmd + len, str);
1625 len += STRLEN(str);
1626 }
1627 if (pass == 2)
1628 clear_tv(tv);
1629 }
1630 if (pass == 1)
1631 {
1632 cmd = alloc(len + 1);
1633 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001634 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001635 len = 0;
1636 }
1637 }
1638
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001639 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001640 do_cmdline_cmd(cmd);
1641 vim_free(cmd);
1642 }
1643 break;
1644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 // execute :echo {string} ...
1646 case ISN_ECHO:
1647 {
1648 int count = iptr->isn_arg.echo.echo_count;
1649 int atstart = TRUE;
1650 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001651 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652
1653 for (idx = 0; idx < count; ++idx)
1654 {
1655 tv = STACK_TV_BOT(idx - count);
1656 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1657 &atstart, &needclr);
1658 clear_tv(tv);
1659 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001660 if (needclr)
1661 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001662 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 }
1664 break;
1665
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001666 // :execute {string} ...
1667 // :echomsg {string} ...
1668 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001669 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001670 case ISN_ECHOMSG:
1671 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001672 {
1673 int count = iptr->isn_arg.number;
1674 garray_T ga;
1675 char_u buf[NUMBUFLEN];
1676 char_u *p;
1677 int len;
1678 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001679 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001680
1681 ga_init2(&ga, 1, 80);
1682 for (idx = 0; idx < count; ++idx)
1683 {
1684 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001685 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001686 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001687 if (tv->v_type == VAR_CHANNEL
1688 || tv->v_type == VAR_JOB)
1689 {
1690 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001691 semsg(_(e_using_invalid_value_as_string_str),
1692 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001693 break;
1694 }
1695 else
1696 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001697 }
1698 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001699 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001700
1701 len = (int)STRLEN(p);
1702 if (ga_grow(&ga, len + 2) == FAIL)
1703 failed = TRUE;
1704 else
1705 {
1706 if (ga.ga_len > 0)
1707 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1708 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1709 ga.ga_len += len;
1710 }
1711 clear_tv(tv);
1712 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001713 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001714 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001715 {
1716 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001717 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001718 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001719
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001720 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001721 {
1722 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001723 {
1724 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001725 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001726 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001727 {
1728 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001729 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001730 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001731 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001732 else
1733 {
1734 msg_sb_eol();
1735 if (iptr->isn_type == ISN_ECHOMSG)
1736 {
1737 msg_attr(ga.ga_data, echo_attr);
1738 out_flush();
1739 }
1740 else
1741 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001742 SOURCING_LNUM = iptr->isn_lnum;
1743 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001744 }
1745 }
1746 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001747 ga_clear(&ga);
1748 }
1749 break;
1750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 // load local variable or argument
1752 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001753 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001754 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001756 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 break;
1758
1759 // load v: variable
1760 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001761 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001762 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001764 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 break;
1766
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001767 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 case ISN_LOADSCRIPT:
1769 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001770 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 svar_T *sv;
1772
Bram Moolenaar4c137212021-04-19 16:48:48 +02001773 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001774 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001775 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001776 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001777 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001778 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001780 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 }
1782 break;
1783
1784 // load s: variable in old script
1785 case ISN_LOADS:
1786 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001787 hashtab_T *ht = &SCRIPT_VARS(
1788 iptr->isn_arg.loadstore.ls_sid);
1789 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 if (di == NULL)
1793 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001794 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001795 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001796 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 }
1798 else
1799 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001800 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001801 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001803 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 }
1805 }
1806 break;
1807
Bram Moolenaard3aac292020-04-19 14:32:17 +02001808 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001810 case ISN_LOADB:
1811 case ISN_LOADW:
1812 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001814 dictitem_T *di = NULL;
1815 hashtab_T *ht = NULL;
1816 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001817
Bram Moolenaard3aac292020-04-19 14:32:17 +02001818 switch (iptr->isn_type)
1819 {
1820 case ISN_LOADG:
1821 ht = get_globvar_ht();
1822 namespace = 'g';
1823 break;
1824 case ISN_LOADB:
1825 ht = &curbuf->b_vars->dv_hashtab;
1826 namespace = 'b';
1827 break;
1828 case ISN_LOADW:
1829 ht = &curwin->w_vars->dv_hashtab;
1830 namespace = 'w';
1831 break;
1832 case ISN_LOADT:
1833 ht = &curtab->tp_vars->dv_hashtab;
1834 namespace = 't';
1835 break;
1836 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001837 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001838 }
1839 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 if (di == NULL)
1842 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001843 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001844 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001845 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001846 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 }
1848 else
1849 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001850 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001851 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001853 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 }
1855 }
1856 break;
1857
Bram Moolenaar03290b82020-12-19 16:30:44 +01001858 // load autoload variable
1859 case ISN_LOADAUTO:
1860 {
1861 char_u *name = iptr->isn_arg.string;
1862
Bram Moolenaar4c137212021-04-19 16:48:48 +02001863 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001864 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001865 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001866 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001867 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001868 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001869 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001870 }
1871 break;
1872
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001873 // load g:/b:/w:/t: namespace
1874 case ISN_LOADGDICT:
1875 case ISN_LOADBDICT:
1876 case ISN_LOADWDICT:
1877 case ISN_LOADTDICT:
1878 {
1879 dict_T *d = NULL;
1880
1881 switch (iptr->isn_type)
1882 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001883 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1884 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1885 case ISN_LOADWDICT: d = curwin->w_vars; break;
1886 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001887 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001888 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001889 }
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 Moolenaar2f8ce0a2020-07-19 19:47:35 +02001892 tv = STACK_TV_BOT(0);
1893 tv->v_type = VAR_DICT;
1894 tv->v_lock = 0;
1895 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001896 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001897 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001898 }
1899 break;
1900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 // load &option
1902 case ISN_LOADOPT:
1903 {
1904 typval_T optval;
1905 char_u *name = iptr->isn_arg.string;
1906
Bram Moolenaara8c17702020-04-01 21:17:24 +02001907 // This is not expected to fail, name is checked during
1908 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001909 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001910 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001911 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001912 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001914 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 }
1916 break;
1917
1918 // load $ENV
1919 case ISN_LOADENV:
1920 {
1921 typval_T optval;
1922 char_u *name = iptr->isn_arg.string;
1923
Bram Moolenaar4c137212021-04-19 16:48:48 +02001924 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001925 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001926 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001927 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001929 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 }
1931 break;
1932
1933 // load @register
1934 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001935 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001936 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 tv = STACK_TV_BOT(0);
1938 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001939 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001940 // This may result in NULL, which should be equivalent to an
1941 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 tv->vval.v_string = get_reg_contents(
1943 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001944 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 break;
1946
1947 // store local variable
1948 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001949 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 tv = STACK_TV_VAR(iptr->isn_arg.number);
1951 clear_tv(tv);
1952 *tv = *STACK_TV_BOT(0);
1953 break;
1954
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001955 // store s: variable in old script
1956 case ISN_STORES:
1957 {
1958 hashtab_T *ht = &SCRIPT_VARS(
1959 iptr->isn_arg.loadstore.ls_sid);
1960 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001961 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001962
Bram Moolenaar4c137212021-04-19 16:48:48 +02001963 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001964 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001965 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001966 else
1967 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001968 SOURCING_LNUM = iptr->isn_lnum;
1969 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001970 {
1971 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001972 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001973 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001974 clear_tv(&di->di_tv);
1975 di->di_tv = *STACK_TV_BOT(0);
1976 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001977 }
1978 break;
1979
1980 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 case ISN_STORESCRIPT:
1982 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001983 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1984 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985
Bram Moolenaar4c137212021-04-19 16:48:48 +02001986 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001987 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001988 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001989 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001990
1991 // "const" and "final" are checked at compile time, locking
1992 // the value needs to be checked here.
1993 SOURCING_LNUM = iptr->isn_lnum;
1994 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001995 {
1996 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001997 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001998 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 clear_tv(sv->sv_tv);
2001 *sv->sv_tv = *STACK_TV_BOT(0);
2002 }
2003 break;
2004
2005 // store option
2006 case ISN_STOREOPT:
2007 {
2008 long n = 0;
2009 char_u *s = NULL;
2010 char *msg;
2011
Bram Moolenaar4c137212021-04-19 16:48:48 +02002012 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 tv = STACK_TV_BOT(0);
2014 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002015 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002017 if (s == NULL)
2018 s = (char_u *)"";
2019 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002021 // must be VAR_NUMBER, CHECKTYPE makes sure
2022 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2024 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002025 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 if (msg != NULL)
2027 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002028 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002030 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 }
2033 break;
2034
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002035 // store $ENV
2036 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002037 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002038 tv = STACK_TV_BOT(0);
2039 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2040 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002041 break;
2042
2043 // store @r
2044 case ISN_STOREREG:
2045 {
2046 int reg = iptr->isn_arg.number;
2047
Bram Moolenaar4c137212021-04-19 16:48:48 +02002048 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002049 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002050 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002051 tv_get_string(tv), -1, FALSE);
2052 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002053 }
2054 break;
2055
2056 // store v: variable
2057 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002058 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002059 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2060 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002061 // should not happen, type is checked when compiling
2062 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002063 break;
2064
Bram Moolenaard3aac292020-04-19 14:32:17 +02002065 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002067 case ISN_STOREB:
2068 case ISN_STOREW:
2069 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002071 dictitem_T *di;
2072 hashtab_T *ht;
2073 char_u *name = iptr->isn_arg.string + 2;
2074
Bram Moolenaard3aac292020-04-19 14:32:17 +02002075 switch (iptr->isn_type)
2076 {
2077 case ISN_STOREG:
2078 ht = get_globvar_ht();
2079 break;
2080 case ISN_STOREB:
2081 ht = &curbuf->b_vars->dv_hashtab;
2082 break;
2083 case ISN_STOREW:
2084 ht = &curwin->w_vars->dv_hashtab;
2085 break;
2086 case ISN_STORET:
2087 ht = &curtab->tp_vars->dv_hashtab;
2088 break;
2089 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002090 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092
Bram Moolenaar4c137212021-04-19 16:48:48 +02002093 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002094 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002095 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002096 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 else
2098 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002099 SOURCING_LNUM = iptr->isn_lnum;
2100 if (var_check_permission(di, name) == FAIL)
2101 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 clear_tv(&di->di_tv);
2103 di->di_tv = *STACK_TV_BOT(0);
2104 }
2105 }
2106 break;
2107
Bram Moolenaar03290b82020-12-19 16:30:44 +01002108 // store an autoload variable
2109 case ISN_STOREAUTO:
2110 SOURCING_LNUM = iptr->isn_lnum;
2111 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2112 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002113 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002114 break;
2115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 // store number in local variable
2117 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002118 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 clear_tv(tv);
2120 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002121 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 break;
2123
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002124 // store value in list or dict variable
2125 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002126 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002127 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002128 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002129 typval_T *tv_dest = STACK_TV_BOT(-1);
2130 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002131
Bram Moolenaar752fc692021-01-04 21:57:11 +01002132 // Stack contains:
2133 // -3 value to be stored
2134 // -2 index
2135 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002136 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002137 SOURCING_LNUM = iptr->isn_lnum;
2138 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002139 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002140 dest_type = tv_dest->v_type;
2141 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002142 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002143 else if (dest_type == VAR_LIST
2144 && tv_idx->v_type != VAR_NUMBER)
2145 {
2146 emsg(_(e_number_exp));
2147 status = FAIL;
2148 }
2149 }
2150 else if (dest_type != tv_dest->v_type)
2151 {
2152 // just in case, should be OK
2153 semsg(_(e_expected_str_but_got_str),
2154 vartype_name(dest_type),
2155 vartype_name(tv_dest->v_type));
2156 status = FAIL;
2157 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002158
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002159 if (status == OK && dest_type == VAR_LIST)
2160 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002161 long lidx = (long)tv_idx->vval.v_number;
2162 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002163
2164 if (list == NULL)
2165 {
2166 emsg(_(e_list_not_set));
2167 goto on_error;
2168 }
2169 if (lidx < 0 && list->lv_len + lidx >= 0)
2170 // negative index is relative to the end
2171 lidx = list->lv_len + lidx;
2172 if (lidx < 0 || lidx > list->lv_len)
2173 {
2174 semsg(_(e_listidx), lidx);
2175 goto on_error;
2176 }
2177 if (lidx < list->lv_len)
2178 {
2179 listitem_T *li = list_find(list, lidx);
2180
2181 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002182 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002183 goto on_error;
2184 // overwrite existing list item
2185 clear_tv(&li->li_tv);
2186 li->li_tv = *tv;
2187 }
2188 else
2189 {
2190 if (error_if_locked(list->lv_lock,
2191 e_cannot_change_list))
2192 goto on_error;
2193 // append to list, only fails when out of memory
2194 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002195 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002196 clear_tv(tv);
2197 }
2198 }
2199 else if (status == OK && dest_type == VAR_DICT)
2200 {
2201 char_u *key = tv_idx->vval.v_string;
2202 dict_T *dict = tv_dest->vval.v_dict;
2203 dictitem_T *di;
2204
2205 SOURCING_LNUM = iptr->isn_lnum;
2206 if (dict == NULL)
2207 {
2208 emsg(_(e_dictionary_not_set));
2209 goto on_error;
2210 }
2211 if (key == NULL)
2212 key = (char_u *)"";
2213 di = dict_find(dict, key, -1);
2214 if (di != NULL)
2215 {
2216 if (error_if_locked(di->di_tv.v_lock,
2217 e_cannot_change_dict_item))
2218 goto on_error;
2219 // overwrite existing value
2220 clear_tv(&di->di_tv);
2221 di->di_tv = *tv;
2222 }
2223 else
2224 {
2225 if (error_if_locked(dict->dv_lock,
2226 e_cannot_change_dict))
2227 goto on_error;
2228 // add to dict, only fails when out of memory
2229 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002230 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002231 clear_tv(tv);
2232 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002233 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002234 else if (status == OK && dest_type == VAR_BLOB)
2235 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002236 long lidx = (long)tv_idx->vval.v_number;
2237 blob_T *blob = tv_dest->vval.v_blob;
2238 varnumber_T nr;
2239 int error = FALSE;
2240 int len;
2241
2242 if (blob == NULL)
2243 {
2244 emsg(_(e_blob_not_set));
2245 goto on_error;
2246 }
2247 len = blob_len(blob);
2248 if (lidx < 0 && len + lidx >= 0)
2249 // negative index is relative to the end
2250 lidx = len + lidx;
2251
2252 // Can add one byte at the end.
2253 if (lidx < 0 || lidx > len)
2254 {
2255 semsg(_(e_blobidx), lidx);
2256 goto on_error;
2257 }
2258 if (value_check_lock(blob->bv_lock,
2259 (char_u *)"blob", FALSE))
2260 goto on_error;
2261 nr = tv_get_number_chk(tv, &error);
2262 if (error)
2263 goto on_error;
2264 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002265 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002266 else
2267 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002268 status = FAIL;
2269 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002270 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002271
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002272 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002273 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002274 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002275 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002276 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002277 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002278 goto on_error;
2279 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002280 }
2281 break;
2282
Bram Moolenaar68452172021-04-12 21:21:02 +02002283 // store value in blob range
2284 case ISN_STORERANGE:
2285 {
2286 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2287 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2288 typval_T *tv_dest = STACK_TV_BOT(-1);
2289 int status = OK;
2290
2291 // Stack contains:
2292 // -4 value to be stored
2293 // -3 first index or "none"
2294 // -2 second index or "none"
2295 // -1 destination blob
2296 tv = STACK_TV_BOT(-4);
2297 if (tv_dest->v_type != VAR_BLOB)
2298 {
2299 status = FAIL;
2300 emsg(_(e_blob_required));
2301 }
2302 else
2303 {
2304 varnumber_T n1;
2305 varnumber_T n2;
2306 int error = FALSE;
2307
2308 n1 = tv_get_number_chk(tv_idx1, &error);
2309 if (error)
2310 status = FAIL;
2311 else
2312 {
2313 if (tv_idx2->v_type == VAR_SPECIAL
2314 && tv_idx2->vval.v_number == VVAL_NONE)
2315 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2316 else
2317 n2 = tv_get_number_chk(tv_idx2, &error);
2318 if (error)
2319 status = FAIL;
2320 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002321 {
2322 long bloblen = blob_len(tv_dest->vval.v_blob);
2323
2324 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002325 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002326 || check_blob_range(bloblen,
2327 n1, n2, FALSE) == FAIL)
2328 status = FAIL;
2329 else
2330 status = blob_set_range(
2331 tv_dest->vval.v_blob, n1, n2, tv);
2332 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002333 }
2334 }
2335
2336 clear_tv(tv_idx1);
2337 clear_tv(tv_idx2);
2338 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002339 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002340 clear_tv(tv);
2341
2342 if (status == FAIL)
2343 goto on_error;
2344 }
2345 break;
2346
Bram Moolenaar0186e582021-01-10 18:33:11 +01002347 // load or store variable or argument from outer scope
2348 case ISN_LOADOUTER:
2349 case ISN_STOREOUTER:
2350 {
2351 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002352 outer_T *outer = ectx->ec_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002353
2354 while (depth > 1 && outer != NULL)
2355 {
2356 outer = outer->out_up;
2357 --depth;
2358 }
2359 if (outer == NULL)
2360 {
2361 SOURCING_LNUM = iptr->isn_lnum;
2362 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002363 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002364 }
2365 tv = ((typval_T *)outer->out_stack->ga_data)
2366 + outer->out_frame_idx + STACK_FRAME_SIZE
2367 + iptr->isn_arg.outer.outer_idx;
2368 if (iptr->isn_type == ISN_LOADOUTER)
2369 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002370 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002371 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002372 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002373 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002374 }
2375 else
2376 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002377 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002378 clear_tv(tv);
2379 *tv = *STACK_TV_BOT(0);
2380 }
2381 }
2382 break;
2383
Bram Moolenaar752fc692021-01-04 21:57:11 +01002384 // unlet item in list or dict variable
2385 case ISN_UNLETINDEX:
2386 {
2387 typval_T *tv_idx = STACK_TV_BOT(-2);
2388 typval_T *tv_dest = STACK_TV_BOT(-1);
2389 int status = OK;
2390
2391 // Stack contains:
2392 // -2 index
2393 // -1 dict or list
2394 if (tv_dest->v_type == VAR_DICT)
2395 {
2396 // unlet a dict item, index must be a string
2397 if (tv_idx->v_type != VAR_STRING)
2398 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002399 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002400 semsg(_(e_expected_str_but_got_str),
2401 vartype_name(VAR_STRING),
2402 vartype_name(tv_idx->v_type));
2403 status = FAIL;
2404 }
2405 else
2406 {
2407 dict_T *d = tv_dest->vval.v_dict;
2408 char_u *key = tv_idx->vval.v_string;
2409 dictitem_T *di = NULL;
2410
2411 if (key == NULL)
2412 key = (char_u *)"";
2413 if (d != NULL)
2414 di = dict_find(d, key, (int)STRLEN(key));
2415 if (di == NULL)
2416 {
2417 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002418 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002419 semsg(_(e_dictkey), key);
2420 status = FAIL;
2421 }
2422 else
2423 {
2424 // TODO: check for dict or item locked
2425 dictitem_remove(d, di);
2426 }
2427 }
2428 }
2429 else if (tv_dest->v_type == VAR_LIST)
2430 {
2431 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002432 SOURCING_LNUM = iptr->isn_lnum;
2433 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002434 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002435 status = FAIL;
2436 }
2437 else
2438 {
2439 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002440 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002441 listitem_T *li = NULL;
2442
2443 li = list_find(l, n);
2444 if (li == NULL)
2445 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002446 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002447 semsg(_(e_listidx), n);
2448 status = FAIL;
2449 }
2450 else
2451 // TODO: check for list or item locked
2452 listitem_remove(l, li);
2453 }
2454 }
2455 else
2456 {
2457 status = FAIL;
2458 semsg(_(e_cannot_index_str),
2459 vartype_name(tv_dest->v_type));
2460 }
2461
2462 clear_tv(tv_idx);
2463 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002464 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002465 if (status == FAIL)
2466 goto on_error;
2467 }
2468 break;
2469
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002470 // unlet range of items in list variable
2471 case ISN_UNLETRANGE:
2472 {
2473 // Stack contains:
2474 // -3 index1
2475 // -2 index2
2476 // -1 dict or list
2477 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2478 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2479 typval_T *tv_dest = STACK_TV_BOT(-1);
2480 int status = OK;
2481
2482 if (tv_dest->v_type == VAR_LIST)
2483 {
2484 // indexes must be a number
2485 SOURCING_LNUM = iptr->isn_lnum;
2486 if (check_for_number(tv_idx1) == FAIL
2487 || check_for_number(tv_idx2) == FAIL)
2488 {
2489 status = FAIL;
2490 }
2491 else
2492 {
2493 list_T *l = tv_dest->vval.v_list;
2494 long n1 = (long)tv_idx1->vval.v_number;
2495 long n2 = (long)tv_idx2->vval.v_number;
2496 listitem_T *li;
2497
2498 li = list_find_index(l, &n1);
2499 if (li == NULL
2500 || list_unlet_range(l, li, NULL, n1,
2501 TRUE, n2) == FAIL)
2502 status = FAIL;
2503 }
2504 }
2505 else
2506 {
2507 status = FAIL;
2508 SOURCING_LNUM = iptr->isn_lnum;
2509 semsg(_(e_cannot_index_str),
2510 vartype_name(tv_dest->v_type));
2511 }
2512
2513 clear_tv(tv_idx1);
2514 clear_tv(tv_idx2);
2515 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002516 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002517 if (status == FAIL)
2518 goto on_error;
2519 }
2520 break;
2521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 // push constant
2523 case ISN_PUSHNR:
2524 case ISN_PUSHBOOL:
2525 case ISN_PUSHSPEC:
2526 case ISN_PUSHF:
2527 case ISN_PUSHS:
2528 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002529 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002530 case ISN_PUSHCHANNEL:
2531 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002532 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002533 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002535 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002536 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 switch (iptr->isn_type)
2538 {
2539 case ISN_PUSHNR:
2540 tv->v_type = VAR_NUMBER;
2541 tv->vval.v_number = iptr->isn_arg.number;
2542 break;
2543 case ISN_PUSHBOOL:
2544 tv->v_type = VAR_BOOL;
2545 tv->vval.v_number = iptr->isn_arg.number;
2546 break;
2547 case ISN_PUSHSPEC:
2548 tv->v_type = VAR_SPECIAL;
2549 tv->vval.v_number = iptr->isn_arg.number;
2550 break;
2551#ifdef FEAT_FLOAT
2552 case ISN_PUSHF:
2553 tv->v_type = VAR_FLOAT;
2554 tv->vval.v_float = iptr->isn_arg.fnumber;
2555 break;
2556#endif
2557 case ISN_PUSHBLOB:
2558 blob_copy(iptr->isn_arg.blob, tv);
2559 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002560 case ISN_PUSHFUNC:
2561 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002562 if (iptr->isn_arg.string == NULL)
2563 tv->vval.v_string = NULL;
2564 else
2565 tv->vval.v_string =
2566 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002567 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002568 case ISN_PUSHCHANNEL:
2569#ifdef FEAT_JOB_CHANNEL
2570 tv->v_type = VAR_CHANNEL;
2571 tv->vval.v_channel = iptr->isn_arg.channel;
2572 if (tv->vval.v_channel != NULL)
2573 ++tv->vval.v_channel->ch_refcount;
2574#endif
2575 break;
2576 case ISN_PUSHJOB:
2577#ifdef FEAT_JOB_CHANNEL
2578 tv->v_type = VAR_JOB;
2579 tv->vval.v_job = iptr->isn_arg.job;
2580 if (tv->vval.v_job != NULL)
2581 ++tv->vval.v_job->jv_refcount;
2582#endif
2583 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 default:
2585 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002586 tv->vval.v_string = vim_strsave(
2587 iptr->isn_arg.string == NULL
2588 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 }
2590 break;
2591
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002592 case ISN_UNLET:
2593 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2594 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002595 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002596 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002597 case ISN_UNLETENV:
2598 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2599 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002600
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002601 case ISN_LOCKCONST:
2602 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2603 break;
2604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 // create a list from items on the stack; uses a single allocation
2606 // for the list header and the items
2607 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002608 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002609 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 break;
2611
2612 // create a dict from items on the stack
2613 case ISN_NEWDICT:
2614 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002615 int count = iptr->isn_arg.number;
2616 dict_T *dict = dict_alloc();
2617 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002618 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002619 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620
2621 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002622 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 for (idx = 0; idx < count; ++idx)
2624 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002625 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002627 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002628 key = tv->vval.v_string == NULL
2629 ? (char_u *)"" : tv->vval.v_string;
2630 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002631 if (item != NULL)
2632 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002633 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002634 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002635 dict_unref(dict);
2636 goto on_error;
2637 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002638 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 clear_tv(tv);
2640 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002641 {
2642 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002643 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002644 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2646 item->di_tv.v_lock = 0;
2647 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002648 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002649 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002650 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002651 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002652 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 }
2654
2655 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002656 ectx->ec_stack.ga_len -= 2 * count - 1;
2657 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002658 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002660 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 tv = STACK_TV_BOT(-1);
2662 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002663 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 tv->vval.v_dict = dict;
2665 ++dict->dv_refcount;
2666 }
2667 break;
2668
2669 // call a :def function
2670 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002671 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002672 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2673 NULL,
2674 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002675 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002676 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 break;
2678
2679 // call a builtin function
2680 case ISN_BCALL:
2681 SOURCING_LNUM = iptr->isn_lnum;
2682 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2683 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002684 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002685 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 break;
2687
2688 // call a funcref or partial
2689 case ISN_PCALL:
2690 {
2691 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2692 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002693 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694
2695 SOURCING_LNUM = iptr->isn_lnum;
2696 if (pfunc->cpf_top)
2697 {
2698 // funcref is above the arguments
2699 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2700 }
2701 else
2702 {
2703 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002704 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002705 partial_tv = *STACK_TV_BOT(0);
2706 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002708 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002709 if (tv == &partial_tv)
2710 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002712 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 }
2714 break;
2715
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002716 case ISN_PCALL_END:
2717 // PCALL finished, arguments have been consumed and replaced by
2718 // the return value. Now clear the funcref from the stack,
2719 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002720 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002721 clear_tv(STACK_TV_BOT(-1));
2722 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2723 break;
2724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 // call a user defined function or funcref/partial
2726 case ISN_UCALL:
2727 {
2728 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2729
2730 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002731 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002732 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002733 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 }
2735 break;
2736
2737 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002738 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002739 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002740 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002741 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002742 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002743 tv->v_type = VAR_NUMBER;
2744 tv->vval.v_number = 0;
2745 tv->v_lock = 0;
2746 // FALLTHROUGH
2747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 case ISN_RETURN:
2749 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002750 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002751 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002752
2753 if (trystack->ga_len > 0)
2754 trycmd = ((trycmd_T *)trystack->ga_data)
2755 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002756 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002757 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002759 // jump to ":finally" or ":endtry"
2760 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002761 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002762 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002763 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 trycmd->tcd_return = TRUE;
2765 }
2766 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002767 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 }
2769 break;
2770
2771 // push a function reference to a compiled function
2772 case ISN_FUNCREF:
2773 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002774 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2775 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2776 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002779 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002780 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002781 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002782 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002783 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002784 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002785 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002786 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002787 goto theend;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002790 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 tv->vval.v_partial = pt;
2792 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002793 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 }
2795 break;
2796
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002797 // Create a global function from a lambda.
2798 case ISN_NEWFUNC:
2799 {
2800 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2801
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002802 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002803 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002804 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002805 }
2806 break;
2807
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002808 // List functions
2809 case ISN_DEF:
2810 if (iptr->isn_arg.string == NULL)
2811 list_functions(NULL);
2812 else
2813 {
2814 exarg_T ea;
2815
2816 CLEAR_FIELD(ea);
2817 ea.cmd = ea.arg = iptr->isn_arg.string;
2818 define_function(&ea, NULL);
2819 }
2820 break;
2821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 // jump if a condition is met
2823 case ISN_JUMP:
2824 {
2825 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002826 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 int jump = TRUE;
2828
2829 if (when != JUMP_ALWAYS)
2830 {
2831 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002832 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002833 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002834 || when == JUMP_IF_COND_TRUE)
2835 {
2836 SOURCING_LNUM = iptr->isn_lnum;
2837 jump = tv_get_bool_chk(tv, &error);
2838 if (error)
2839 goto on_error;
2840 }
2841 else
2842 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002844 || when == JUMP_AND_KEEP_IF_FALSE
2845 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002847 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 {
2849 // drop the value from the stack
2850 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002851 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 }
2853 }
2854 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002855 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 }
2857 break;
2858
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002859 // Jump if an argument with a default value was already set and not
2860 // v:none.
2861 case ISN_JUMP_IF_ARG_SET:
2862 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2863 if (tv->v_type != VAR_UNKNOWN
2864 && !(tv->v_type == VAR_SPECIAL
2865 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002866 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002867 break;
2868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 // top of a for loop
2870 case ISN_FOR:
2871 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002872 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 typval_T *idxtv =
2874 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2875
Bram Moolenaar4c137212021-04-19 16:48:48 +02002876 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002877 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002878 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002879 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002880 list_T *list = ltv->vval.v_list;
2881
2882 // push the next item from the list
2883 ++idxtv->vval.v_number;
2884 if (list == NULL
2885 || idxtv->vval.v_number >= list->lv_len)
2886 {
2887 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002888 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2889 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002890 }
2891 else if (list->lv_first == &range_list_item)
2892 {
2893 // non-materialized range() list
2894 tv = STACK_TV_BOT(0);
2895 tv->v_type = VAR_NUMBER;
2896 tv->v_lock = 0;
2897 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002899 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002900 }
2901 else
2902 {
2903 listitem_T *li = list_find(list,
2904 idxtv->vval.v_number);
2905
2906 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002907 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002908 }
2909 }
2910 else if (ltv->v_type == VAR_STRING)
2911 {
2912 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002913
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002914 // The index is for the last byte of the previous
2915 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002916 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002917 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002918 {
2919 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002920 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2921 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002922 }
2923 else
2924 {
2925 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2926
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002927 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002928 tv = STACK_TV_BOT(0);
2929 tv->v_type = VAR_STRING;
2930 tv->vval.v_string = vim_strnsave(
2931 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002932 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002933 idxtv->vval.v_number += clen - 1;
2934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002936 else if (ltv->v_type == VAR_BLOB)
2937 {
2938 blob_T *blob = ltv->vval.v_blob;
2939
2940 // When we get here the first time make a copy of the
2941 // blob, so that the iteration still works when it is
2942 // changed.
2943 if (idxtv->vval.v_number == -1 && blob != NULL)
2944 {
2945 blob_copy(blob, ltv);
2946 blob_unref(blob);
2947 blob = ltv->vval.v_blob;
2948 }
2949
2950 // The index is for the previous byte.
2951 ++idxtv->vval.v_number;
2952 if (blob == NULL
2953 || idxtv->vval.v_number >= blob_len(blob))
2954 {
2955 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002956 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2957 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002958 }
2959 else
2960 {
2961 // Push the next byte from the blob.
2962 tv = STACK_TV_BOT(0);
2963 tv->v_type = VAR_NUMBER;
2964 tv->vval.v_number = blob_get(blob,
2965 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002966 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002967 }
2968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 else
2970 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002971 semsg(_(e_for_loop_on_str_not_supported),
2972 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002973 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 }
2975 }
2976 break;
2977
2978 // start of ":try" block
2979 case ISN_TRY:
2980 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002981 trycmd_T *trycmd = NULL;
2982
Bram Moolenaar4c137212021-04-19 16:48:48 +02002983 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002984 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002985 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
2986 + ectx->ec_trystack.ga_len;
2987 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002989 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002990 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
2991 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002992 trycmd->tcd_catch_idx =
2993 iptr->isn_arg.try.try_ref->try_catch;
2994 trycmd->tcd_finally_idx =
2995 iptr->isn_arg.try.try_ref->try_finally;
2996 trycmd->tcd_endtry_idx =
2997 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 }
2999 break;
3000
3001 case ISN_PUSHEXC:
3002 if (current_exception == NULL)
3003 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003004 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003006 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003008 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003009 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003011 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003013 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 tv->vval.v_string = vim_strsave(
3015 (char_u *)current_exception->value);
3016 break;
3017
3018 case ISN_CATCH:
3019 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003020 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021
Bram Moolenaar4c137212021-04-19 16:48:48 +02003022 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 if (trystack->ga_len > 0)
3024 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003025 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 + trystack->ga_len - 1;
3027 trycmd->tcd_caught = TRUE;
3028 }
3029 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003030 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 catch_exception(current_exception);
3032 }
3033 break;
3034
Bram Moolenaarc150c092021-02-13 15:02:46 +01003035 case ISN_TRYCONT:
3036 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003037 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003038 trycont_T *trycont = &iptr->isn_arg.trycont;
3039 int i;
3040 trycmd_T *trycmd;
3041 int iidx = trycont->tct_where;
3042
3043 if (trystack->ga_len < trycont->tct_levels)
3044 {
3045 siemsg("TRYCONT: expected %d levels, found %d",
3046 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003047 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003048 }
3049 // Make :endtry jump to any outer try block and the last
3050 // :endtry inside the loop to the loop start.
3051 for (i = trycont->tct_levels; i > 0; --i)
3052 {
3053 trycmd = ((trycmd_T *)trystack->ga_data)
3054 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003055 // Add one to tcd_cont to be able to jump to
3056 // instruction with index zero.
3057 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003058 iidx = trycmd->tcd_finally_idx == 0
3059 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003060 }
3061 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003062 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003063 }
3064 break;
3065
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003066 case ISN_FINALLY:
3067 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003068 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003069 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3070 + trystack->ga_len - 1;
3071
3072 // Reset the index to avoid a return statement jumps here
3073 // again.
3074 trycmd->tcd_finally_idx = 0;
3075 break;
3076 }
3077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 // end of ":try" block
3079 case ISN_ENDTRY:
3080 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003081 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082
3083 if (trystack->ga_len > 0)
3084 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003085 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 --trystack->ga_len;
3088 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003089 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 trycmd = ((trycmd_T *)trystack->ga_data)
3091 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003092 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 {
3094 // discard the exception
3095 if (caught_stack == current_exception)
3096 caught_stack = caught_stack->caught;
3097 discard_current_exception();
3098 }
3099
3100 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003101 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003102
Bram Moolenaar4c137212021-04-19 16:48:48 +02003103 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003104 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003105 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003106 clear_tv(STACK_TV_BOT(0));
3107 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003108 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003109 // handling :continue: jump to outer try block or
3110 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003111 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 }
3113 }
3114 break;
3115
3116 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003117 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003118 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003119
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003120 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3121 {
3122 // throwing an exception while using "silent!" causes
3123 // the function to abort but not display an error.
3124 tv = STACK_TV_BOT(-1);
3125 clear_tv(tv);
3126 tv->v_type = VAR_NUMBER;
3127 tv->vval.v_number = 0;
3128 goto done;
3129 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003131 tv = STACK_TV_BOT(0);
3132 if (tv->vval.v_string == NULL
3133 || *skipwhite(tv->vval.v_string) == NUL)
3134 {
3135 vim_free(tv->vval.v_string);
3136 SOURCING_LNUM = iptr->isn_lnum;
3137 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003138 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003139 }
3140
3141 // Inside a "catch" we need to first discard the caught
3142 // exception.
3143 if (trystack->ga_len > 0)
3144 {
3145 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3146 + trystack->ga_len - 1;
3147 if (trycmd->tcd_caught && current_exception != NULL)
3148 {
3149 // discard the exception
3150 if (caught_stack == current_exception)
3151 caught_stack = caught_stack->caught;
3152 discard_current_exception();
3153 trycmd->tcd_caught = FALSE;
3154 }
3155 }
3156
3157 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3158 == FAIL)
3159 {
3160 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003161 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003162 }
3163 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 break;
3166
3167 // compare with special values
3168 case ISN_COMPAREBOOL:
3169 case ISN_COMPARESPECIAL:
3170 {
3171 typval_T *tv1 = STACK_TV_BOT(-2);
3172 typval_T *tv2 = STACK_TV_BOT(-1);
3173 varnumber_T arg1 = tv1->vval.v_number;
3174 varnumber_T arg2 = tv2->vval.v_number;
3175 int res;
3176
3177 switch (iptr->isn_arg.op.op_type)
3178 {
3179 case EXPR_EQUAL: res = arg1 == arg2; break;
3180 case EXPR_NEQUAL: res = arg1 != arg2; break;
3181 default: res = 0; break;
3182 }
3183
Bram Moolenaar4c137212021-04-19 16:48:48 +02003184 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 tv1->v_type = VAR_BOOL;
3186 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3187 }
3188 break;
3189
3190 // Operation with two number arguments
3191 case ISN_OPNR:
3192 case ISN_COMPARENR:
3193 {
3194 typval_T *tv1 = STACK_TV_BOT(-2);
3195 typval_T *tv2 = STACK_TV_BOT(-1);
3196 varnumber_T arg1 = tv1->vval.v_number;
3197 varnumber_T arg2 = tv2->vval.v_number;
3198 varnumber_T res;
3199
3200 switch (iptr->isn_arg.op.op_type)
3201 {
3202 case EXPR_MULT: res = arg1 * arg2; break;
3203 case EXPR_DIV: res = arg1 / arg2; break;
3204 case EXPR_REM: res = arg1 % arg2; break;
3205 case EXPR_SUB: res = arg1 - arg2; break;
3206 case EXPR_ADD: res = arg1 + arg2; break;
3207
3208 case EXPR_EQUAL: res = arg1 == arg2; break;
3209 case EXPR_NEQUAL: res = arg1 != arg2; break;
3210 case EXPR_GREATER: res = arg1 > arg2; break;
3211 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3212 case EXPR_SMALLER: res = arg1 < arg2; break;
3213 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3214 default: res = 0; break;
3215 }
3216
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 if (iptr->isn_type == ISN_COMPARENR)
3219 {
3220 tv1->v_type = VAR_BOOL;
3221 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3222 }
3223 else
3224 tv1->vval.v_number = res;
3225 }
3226 break;
3227
3228 // Computation with two float arguments
3229 case ISN_OPFLOAT:
3230 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003231#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 {
3233 typval_T *tv1 = STACK_TV_BOT(-2);
3234 typval_T *tv2 = STACK_TV_BOT(-1);
3235 float_T arg1 = tv1->vval.v_float;
3236 float_T arg2 = tv2->vval.v_float;
3237 float_T res = 0;
3238 int cmp = FALSE;
3239
3240 switch (iptr->isn_arg.op.op_type)
3241 {
3242 case EXPR_MULT: res = arg1 * arg2; break;
3243 case EXPR_DIV: res = arg1 / arg2; break;
3244 case EXPR_SUB: res = arg1 - arg2; break;
3245 case EXPR_ADD: res = arg1 + arg2; break;
3246
3247 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3248 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3249 case EXPR_GREATER: cmp = arg1 > arg2; break;
3250 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3251 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3252 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3253 default: cmp = 0; break;
3254 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003255 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 if (iptr->isn_type == ISN_COMPAREFLOAT)
3257 {
3258 tv1->v_type = VAR_BOOL;
3259 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3260 }
3261 else
3262 tv1->vval.v_float = res;
3263 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003264#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 break;
3266
3267 case ISN_COMPARELIST:
3268 {
3269 typval_T *tv1 = STACK_TV_BOT(-2);
3270 typval_T *tv2 = STACK_TV_BOT(-1);
3271 list_T *arg1 = tv1->vval.v_list;
3272 list_T *arg2 = tv2->vval.v_list;
3273 int cmp = FALSE;
3274 int ic = iptr->isn_arg.op.op_ic;
3275
3276 switch (iptr->isn_arg.op.op_type)
3277 {
3278 case EXPR_EQUAL: cmp =
3279 list_equal(arg1, arg2, ic, FALSE); break;
3280 case EXPR_NEQUAL: cmp =
3281 !list_equal(arg1, arg2, ic, FALSE); break;
3282 case EXPR_IS: cmp = arg1 == arg2; break;
3283 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3284 default: cmp = 0; break;
3285 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003286 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 clear_tv(tv1);
3288 clear_tv(tv2);
3289 tv1->v_type = VAR_BOOL;
3290 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3291 }
3292 break;
3293
3294 case ISN_COMPAREBLOB:
3295 {
3296 typval_T *tv1 = STACK_TV_BOT(-2);
3297 typval_T *tv2 = STACK_TV_BOT(-1);
3298 blob_T *arg1 = tv1->vval.v_blob;
3299 blob_T *arg2 = tv2->vval.v_blob;
3300 int cmp = FALSE;
3301
3302 switch (iptr->isn_arg.op.op_type)
3303 {
3304 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3305 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3306 case EXPR_IS: cmp = arg1 == arg2; break;
3307 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3308 default: cmp = 0; break;
3309 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003310 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 clear_tv(tv1);
3312 clear_tv(tv2);
3313 tv1->v_type = VAR_BOOL;
3314 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3315 }
3316 break;
3317
3318 // TODO: handle separately
3319 case ISN_COMPARESTRING:
3320 case ISN_COMPAREDICT:
3321 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 case ISN_COMPAREANY:
3323 {
3324 typval_T *tv1 = STACK_TV_BOT(-2);
3325 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003326 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 int ic = iptr->isn_arg.op.op_ic;
3328
Bram Moolenaareb26f432020-09-14 16:50:05 +02003329 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003330 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003332 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 }
3334 break;
3335
3336 case ISN_ADDLIST:
3337 case ISN_ADDBLOB:
3338 {
3339 typval_T *tv1 = STACK_TV_BOT(-2);
3340 typval_T *tv2 = STACK_TV_BOT(-1);
3341
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003342 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 if (iptr->isn_type == ISN_ADDLIST)
3344 eval_addlist(tv1, tv2);
3345 else
3346 eval_addblob(tv1, tv2);
3347 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003348 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 }
3350 break;
3351
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003352 case ISN_LISTAPPEND:
3353 {
3354 typval_T *tv1 = STACK_TV_BOT(-2);
3355 typval_T *tv2 = STACK_TV_BOT(-1);
3356 list_T *l = tv1->vval.v_list;
3357
3358 // add an item to a list
3359 if (l == NULL)
3360 {
3361 SOURCING_LNUM = iptr->isn_lnum;
3362 emsg(_(e_cannot_add_to_null_list));
3363 goto on_error;
3364 }
3365 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003366 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003367 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003368 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003369 }
3370 break;
3371
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003372 case ISN_BLOBAPPEND:
3373 {
3374 typval_T *tv1 = STACK_TV_BOT(-2);
3375 typval_T *tv2 = STACK_TV_BOT(-1);
3376 blob_T *b = tv1->vval.v_blob;
3377 int error = FALSE;
3378 varnumber_T n;
3379
3380 // add a number to a blob
3381 if (b == NULL)
3382 {
3383 SOURCING_LNUM = iptr->isn_lnum;
3384 emsg(_(e_cannot_add_to_null_blob));
3385 goto on_error;
3386 }
3387 n = tv_get_number_chk(tv2, &error);
3388 if (error)
3389 goto on_error;
3390 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003391 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003392 }
3393 break;
3394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 // Computation with two arguments of unknown type
3396 case ISN_OPANY:
3397 {
3398 typval_T *tv1 = STACK_TV_BOT(-2);
3399 typval_T *tv2 = STACK_TV_BOT(-1);
3400 varnumber_T n1, n2;
3401#ifdef FEAT_FLOAT
3402 float_T f1 = 0, f2 = 0;
3403#endif
3404 int error = FALSE;
3405
3406 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3407 {
3408 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3409 {
3410 eval_addlist(tv1, tv2);
3411 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003412 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 break;
3414 }
3415 else if (tv1->v_type == VAR_BLOB
3416 && tv2->v_type == VAR_BLOB)
3417 {
3418 eval_addblob(tv1, tv2);
3419 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003420 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 break;
3422 }
3423 }
3424#ifdef FEAT_FLOAT
3425 if (tv1->v_type == VAR_FLOAT)
3426 {
3427 f1 = tv1->vval.v_float;
3428 n1 = 0;
3429 }
3430 else
3431#endif
3432 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003433 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 n1 = tv_get_number_chk(tv1, &error);
3435 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003436 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437#ifdef FEAT_FLOAT
3438 if (tv2->v_type == VAR_FLOAT)
3439 f1 = n1;
3440#endif
3441 }
3442#ifdef FEAT_FLOAT
3443 if (tv2->v_type == VAR_FLOAT)
3444 {
3445 f2 = tv2->vval.v_float;
3446 n2 = 0;
3447 }
3448 else
3449#endif
3450 {
3451 n2 = tv_get_number_chk(tv2, &error);
3452 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003453 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454#ifdef FEAT_FLOAT
3455 if (tv1->v_type == VAR_FLOAT)
3456 f2 = n2;
3457#endif
3458 }
3459#ifdef FEAT_FLOAT
3460 // if there is a float on either side the result is a float
3461 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3462 {
3463 switch (iptr->isn_arg.op.op_type)
3464 {
3465 case EXPR_MULT: f1 = f1 * f2; break;
3466 case EXPR_DIV: f1 = f1 / f2; break;
3467 case EXPR_SUB: f1 = f1 - f2; break;
3468 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003469 default: SOURCING_LNUM = iptr->isn_lnum;
3470 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003471 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 }
3473 clear_tv(tv1);
3474 clear_tv(tv2);
3475 tv1->v_type = VAR_FLOAT;
3476 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003477 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 }
3479 else
3480#endif
3481 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003482 int failed = FALSE;
3483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 switch (iptr->isn_arg.op.op_type)
3485 {
3486 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003487 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3488 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003489 goto on_error;
3490 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 case EXPR_SUB: n1 = n1 - n2; break;
3492 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003493 default: n1 = num_modulus(n1, n2, &failed);
3494 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003495 goto on_error;
3496 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 }
3498 clear_tv(tv1);
3499 clear_tv(tv2);
3500 tv1->v_type = VAR_NUMBER;
3501 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003502 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 }
3504 }
3505 break;
3506
3507 case ISN_CONCAT:
3508 {
3509 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3510 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3511 char_u *res;
3512
3513 res = concat_str(str1, str2);
3514 clear_tv(STACK_TV_BOT(-2));
3515 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003516 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 STACK_TV_BOT(-1)->vval.v_string = res;
3518 }
3519 break;
3520
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003521 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003522 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003523 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003524 int is_slice = iptr->isn_type == ISN_STRSLICE;
3525 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003526 char_u *res;
3527
3528 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003529 // string slice: string is at stack-3, first index at
3530 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003531 if (is_slice)
3532 {
3533 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003534 n1 = tv->vval.v_number;
3535 }
3536
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003537 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003538 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003539
Bram Moolenaar4c137212021-04-19 16:48:48 +02003540 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003541 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003542 if (is_slice)
3543 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003544 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003545 else
3546 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003547 // single character (including composing characters).
3548 // If the index is too big or negative the result is
3549 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003550 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003551 vim_free(tv->vval.v_string);
3552 tv->vval.v_string = res;
3553 }
3554 break;
3555
3556 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003557 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003558 case ISN_BLOBINDEX:
3559 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003561 int is_slice = iptr->isn_type == ISN_LISTSLICE
3562 || iptr->isn_type == ISN_BLOBSLICE;
3563 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3564 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003565 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003566 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567
3568 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003569 // list slice: list is at stack-3, indexes at stack-2 and
3570 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003571 // Same for blob.
3572 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573
3574 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003575 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003577
3578 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 {
Bram Moolenaared591872020-08-15 22:14:53 +02003580 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003581 n1 = tv->vval.v_number;
3582 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 }
Bram Moolenaared591872020-08-15 22:14:53 +02003584
Bram Moolenaar4c137212021-04-19 16:48:48 +02003585 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003586 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003587 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003588 if (is_blob)
3589 {
3590 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3591 n1, n2, FALSE, tv) == FAIL)
3592 goto on_error;
3593 }
3594 else
3595 {
3596 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3597 n1, n2, FALSE, tv, TRUE) == FAIL)
3598 goto on_error;
3599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 }
3601 break;
3602
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003603 case ISN_ANYINDEX:
3604 case ISN_ANYSLICE:
3605 {
3606 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3607 typval_T *var1, *var2;
3608 int res;
3609
3610 // index: composite is at stack-2, index at stack-1
3611 // slice: composite is at stack-3, indexes at stack-2 and
3612 // stack-1
3613 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003614 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003615 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3616 goto on_error;
3617 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3618 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003619 res = eval_index_inner(tv, is_slice, var1, var2,
3620 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003621 clear_tv(var1);
3622 if (is_slice)
3623 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003624 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003625 if (res == FAIL)
3626 goto on_error;
3627 }
3628 break;
3629
Bram Moolenaar9af78762020-06-16 11:34:42 +02003630 case ISN_SLICE:
3631 {
3632 list_T *list;
3633 int count = iptr->isn_arg.number;
3634
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003635 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003636 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003637 list = tv->vval.v_list;
3638
3639 // no error for short list, expect it to be checked earlier
3640 if (list != NULL && list->lv_len >= count)
3641 {
3642 list_T *newlist = list_slice(list,
3643 count, list->lv_len - 1);
3644
3645 if (newlist != NULL)
3646 {
3647 list_unref(list);
3648 tv->vval.v_list = newlist;
3649 ++newlist->lv_refcount;
3650 }
3651 }
3652 }
3653 break;
3654
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003655 case ISN_GETITEM:
3656 {
3657 listitem_T *li;
3658 int index = iptr->isn_arg.number;
3659
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003660 // Get list item: list is at stack-1, push item.
3661 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003662 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003663 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003664
Bram Moolenaar4c137212021-04-19 16:48:48 +02003665 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003666 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003667 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003668 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003669
3670 // Useful when used in unpack assignment. Reset at
3671 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003672 ectx->ec_where.wt_index = index + 1;
3673 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003674 }
3675 break;
3676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 case ISN_MEMBER:
3678 {
3679 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003680 char_u *key;
3681 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003682 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003683
3684 // dict member: dict is at stack-2, key at stack-1
3685 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003686 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003687 dict = tv->vval.v_dict;
3688
3689 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003690 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003691 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003692 if (key == NULL)
3693 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003694
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003695 if ((di = dict_find(dict, key, -1)) == NULL)
3696 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003697 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003698 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003699
3700 // If :silent! is used we will continue, make sure the
3701 // stack contents makes sense.
3702 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003703 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003704 tv = STACK_TV_BOT(-1);
3705 clear_tv(tv);
3706 tv->v_type = VAR_NUMBER;
3707 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003708 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003709 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003710 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003711 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003712 // Clear the dict only after getting the item, to avoid
3713 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003714 tv = STACK_TV_BOT(-1);
3715 temp_tv = *tv;
3716 copy_tv(&di->di_tv, tv);
3717 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003718 }
3719 break;
3720
3721 // dict member with string key
3722 case ISN_STRINGMEMBER:
3723 {
3724 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003726 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727
3728 tv = STACK_TV_BOT(-1);
3729 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3730 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003731 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003733 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 }
3735 dict = tv->vval.v_dict;
3736
3737 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3738 == NULL)
3739 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003740 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003742 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003744 // Clear the dict after getting the item, to avoid that it
3745 // make the item invalid.
3746 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003748 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 }
3750 break;
3751
3752 case ISN_NEGATENR:
3753 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003754 if (tv->v_type != VAR_NUMBER
3755#ifdef FEAT_FLOAT
3756 && tv->v_type != VAR_FLOAT
3757#endif
3758 )
3759 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003760 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003761 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003762 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003763 }
3764#ifdef FEAT_FLOAT
3765 if (tv->v_type == VAR_FLOAT)
3766 tv->vval.v_float = -tv->vval.v_float;
3767 else
3768#endif
3769 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 break;
3771
3772 case ISN_CHECKNR:
3773 {
3774 int error = FALSE;
3775
3776 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003777 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003779 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 (void)tv_get_number_chk(tv, &error);
3781 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003782 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 }
3784 break;
3785
3786 case ISN_CHECKTYPE:
3787 {
3788 checktype_T *ct = &iptr->isn_arg.type;
3789
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003790 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003791 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003792 if (!ectx->ec_where.wt_variable)
3793 ectx->ec_where.wt_index = ct->ct_arg_idx;
3794 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3795 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003796 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003797 if (!ectx->ec_where.wt_variable)
3798 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003799
3800 // number 0 is FALSE, number 1 is TRUE
3801 if (tv->v_type == VAR_NUMBER
3802 && ct->ct_type->tt_type == VAR_BOOL
3803 && (tv->vval.v_number == 0
3804 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003806 tv->v_type = VAR_BOOL;
3807 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003808 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 }
3810 }
3811 break;
3812
Bram Moolenaar9af78762020-06-16 11:34:42 +02003813 case ISN_CHECKLEN:
3814 {
3815 int min_len = iptr->isn_arg.checklen.cl_min_len;
3816 list_T *list = NULL;
3817
3818 tv = STACK_TV_BOT(-1);
3819 if (tv->v_type == VAR_LIST)
3820 list = tv->vval.v_list;
3821 if (list == NULL || list->lv_len < min_len
3822 || (list->lv_len > min_len
3823 && !iptr->isn_arg.checklen.cl_more_OK))
3824 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003825 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003826 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003827 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003828 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003829 }
3830 }
3831 break;
3832
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003833 case ISN_SETTYPE:
3834 {
3835 checktype_T *ct = &iptr->isn_arg.type;
3836
3837 tv = STACK_TV_BOT(-1);
3838 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3839 {
3840 free_type(tv->vval.v_dict->dv_type);
3841 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3842 }
3843 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3844 {
3845 free_type(tv->vval.v_list->lv_type);
3846 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3847 }
3848 }
3849 break;
3850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003852 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 {
3854 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003855 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003857 if (iptr->isn_type == ISN_2BOOL)
3858 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003859 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003860 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003861 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003862 n = !n;
3863 }
3864 else
3865 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003866 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003867 SOURCING_LNUM = iptr->isn_lnum;
3868 n = tv_get_bool_chk(tv, &error);
3869 if (error)
3870 goto on_error;
3871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 clear_tv(tv);
3873 tv->v_type = VAR_BOOL;
3874 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3875 }
3876 break;
3877
3878 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003879 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003880 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003881 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3882 iptr->isn_type == ISN_2STRING_ANY,
3883 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003884 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 break;
3886
Bram Moolenaar08597872020-12-10 19:43:40 +01003887 case ISN_RANGE:
3888 {
3889 exarg_T ea;
3890 char *errormsg;
3891
Bram Moolenaarece0b872021-01-08 20:40:45 +01003892 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003893 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003894 ea.addr_type = ADDR_LINES;
3895 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003896 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003897 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003898 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003899
Bram Moolenaar4c137212021-04-19 16:48:48 +02003900 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003901 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003902 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003903 tv = STACK_TV_BOT(-1);
3904 tv->v_type = VAR_NUMBER;
3905 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003906 if (ea.addr_count == 0)
3907 tv->vval.v_number = curwin->w_cursor.lnum;
3908 else
3909 tv->vval.v_number = ea.line2;
3910 }
3911 break;
3912
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003913 case ISN_PUT:
3914 {
3915 int regname = iptr->isn_arg.put.put_regname;
3916 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3917 char_u *expr = NULL;
3918 int dir = FORWARD;
3919
Bram Moolenaar08597872020-12-10 19:43:40 +01003920 if (lnum < -2)
3921 {
3922 // line number was put on the stack by ISN_RANGE
3923 tv = STACK_TV_BOT(-1);
3924 curwin->w_cursor.lnum = tv->vval.v_number;
3925 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3926 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003927 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003928 }
3929 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003930 // :put! above cursor
3931 dir = BACKWARD;
3932 else if (lnum >= 0)
3933 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003934
3935 if (regname == '=')
3936 {
3937 tv = STACK_TV_BOT(-1);
3938 if (tv->v_type == VAR_STRING)
3939 expr = tv->vval.v_string;
3940 else
3941 {
3942 expr = typval2string(tv, TRUE); // allocates value
3943 clear_tv(tv);
3944 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003945 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003946 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003947 check_cursor();
3948 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3949 vim_free(expr);
3950 }
3951 break;
3952
Bram Moolenaar02194d22020-10-24 23:08:38 +02003953 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3955 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3956 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3957 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003958 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3959 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003960 break;
3961
Bram Moolenaar02194d22020-10-24 23:08:38 +02003962 case ISN_CMDMOD_REV:
3963 // filter regprog is owned by the instruction, don't free it
3964 cmdmod.cmod_filter_regmatch.regprog = NULL;
3965 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003966 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
3967 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003968 break;
3969
Bram Moolenaar792f7862020-11-23 08:31:18 +01003970 case ISN_UNPACK:
3971 {
3972 int count = iptr->isn_arg.unpack.unp_count;
3973 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3974 list_T *l;
3975 listitem_T *li;
3976 int i;
3977
3978 // Check there is a valid list to unpack.
3979 tv = STACK_TV_BOT(-1);
3980 if (tv->v_type != VAR_LIST)
3981 {
3982 SOURCING_LNUM = iptr->isn_lnum;
3983 emsg(_(e_for_argument_must_be_sequence_of_lists));
3984 goto on_error;
3985 }
3986 l = tv->vval.v_list;
3987 if (l == NULL
3988 || l->lv_len < (semicolon ? count - 1 : count))
3989 {
3990 SOURCING_LNUM = iptr->isn_lnum;
3991 emsg(_(e_list_value_does_not_have_enough_items));
3992 goto on_error;
3993 }
3994 else if (!semicolon && l->lv_len > count)
3995 {
3996 SOURCING_LNUM = iptr->isn_lnum;
3997 emsg(_(e_list_value_has_more_items_than_targets));
3998 goto on_error;
3999 }
4000
4001 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004002 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004003 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004004 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004005
4006 // Variable after semicolon gets a list with the remaining
4007 // items.
4008 if (semicolon)
4009 {
4010 list_T *rem_list =
4011 list_alloc_with_items(l->lv_len - count + 1);
4012
4013 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004014 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004015 tv = STACK_TV_BOT(-count);
4016 tv->vval.v_list = rem_list;
4017 ++rem_list->lv_refcount;
4018 tv->v_lock = 0;
4019 li = l->lv_first;
4020 for (i = 0; i < count - 1; ++i)
4021 li = li->li_next;
4022 for (i = 0; li != NULL; ++i)
4023 {
4024 list_set_item(rem_list, i, &li->li_tv);
4025 li = li->li_next;
4026 }
4027 --count;
4028 }
4029
4030 // Produce the values in reverse order, first item last.
4031 li = l->lv_first;
4032 for (i = 0; i < count; ++i)
4033 {
4034 tv = STACK_TV_BOT(-i - 1);
4035 copy_tv(&li->li_tv, tv);
4036 li = li->li_next;
4037 }
4038
4039 list_unref(l);
4040 }
4041 break;
4042
Bram Moolenaarb2049902021-01-24 12:53:53 +01004043 case ISN_PROF_START:
4044 case ISN_PROF_END:
4045 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004046#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004047 funccall_T cookie;
4048 ufunc_T *cur_ufunc =
4049 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004050 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004051
4052 cookie.func = cur_ufunc;
4053 if (iptr->isn_type == ISN_PROF_START)
4054 {
4055 func_line_start(&cookie, iptr->isn_lnum);
4056 // if we get here the instruction is executed
4057 func_line_exec(&cookie);
4058 }
4059 else
4060 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004061#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004062 }
4063 break;
4064
Bram Moolenaar389df252020-07-09 21:20:47 +02004065 case ISN_SHUFFLE:
4066 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004067 typval_T tmp_tv;
4068 int item = iptr->isn_arg.shuffle.shfl_item;
4069 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004070
4071 tmp_tv = *STACK_TV_BOT(-item);
4072 for ( ; up > 0 && item > 1; --up)
4073 {
4074 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4075 --item;
4076 }
4077 *STACK_TV_BOT(-item) = tmp_tv;
4078 }
4079 break;
4080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004082 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004084 ectx->ec_where.wt_index = 0;
4085 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 break;
4087 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004088 continue;
4089
Bram Moolenaard032f342020-07-18 18:13:02 +02004090func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004091 // Restore previous function. If the frame pointer is where we started
4092 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004093 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004094 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004095
Bram Moolenaar4c137212021-04-19 16:48:48 +02004096 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004097 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004098 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004099 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004100
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004101on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004102 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004103 // If "emsg_silent" is set then ignore the error, unless it was set
4104 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004105 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004106 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004107 {
4108 // If a sequence of instructions causes an error while ":silent!"
4109 // was used, restore the stack length and jump ahead to restoring
4110 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004111 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004112 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004113 while (ectx->ec_stack.ga_len
4114 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004115 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004116 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004117 clear_tv(STACK_TV_BOT(0));
4118 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004119 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4120 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004121 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004122 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004123 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004124on_fatal_error:
4125 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004126 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004127 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004128 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 }
4130
4131done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004132 ret = OK;
4133theend:
4134 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4135 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004136}
4137
4138/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004139 * Execute the instructions from a VAR_INSTR typeval and put the result in
4140 * "rettv".
4141 * Return OK or FAIL.
4142 */
4143 int
4144exe_typval_instr(typval_T *tv, typval_T *rettv)
4145{
4146 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4147 isn_T *save_instr = ectx->ec_instr;
4148 int save_iidx = ectx->ec_iidx;
4149 int res;
4150
4151 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4152 res = exec_instructions(ectx);
4153 if (res == OK)
4154 {
4155 *rettv = *STACK_TV_BOT(-1);
4156 --ectx->ec_stack.ga_len;
4157 }
4158
4159 ectx->ec_instr = save_instr;
4160 ectx->ec_iidx = save_iidx;
4161
4162 return res;
4163}
4164
4165/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004166 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4167 * "substitute_instr".
4168 */
4169 char_u *
4170exe_substitute_instr(void)
4171{
4172 ectx_T *ectx = substitute_instr->subs_ectx;
4173 isn_T *save_instr = ectx->ec_instr;
4174 int save_iidx = ectx->ec_iidx;
4175 char_u *res;
4176
4177 ectx->ec_instr = substitute_instr->subs_instr;
4178 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004179 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004180 typval_T *tv = STACK_TV_BOT(-1);
4181
Bram Moolenaar27523602021-06-05 21:36:19 +02004182 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004183 --ectx->ec_stack.ga_len;
4184 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004185 }
4186 else
4187 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004188 substitute_instr->subs_status = FAIL;
4189 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191
Bram Moolenaar4c137212021-04-19 16:48:48 +02004192 ectx->ec_instr = save_instr;
4193 ectx->ec_iidx = save_iidx;
4194
4195 return res;
4196}
4197
4198/*
4199 * Call a "def" function from old Vim script.
4200 * Return OK or FAIL.
4201 */
4202 int
4203call_def_function(
4204 ufunc_T *ufunc,
4205 int argc_arg, // nr of arguments
4206 typval_T *argv, // arguments
4207 partial_T *partial, // optional partial for context
4208 typval_T *rettv) // return value
4209{
4210 ectx_T ectx; // execution context
4211 int argc = argc_arg;
4212 typval_T *tv;
4213 int idx;
4214 int ret = FAIL;
4215 int defcount = ufunc->uf_args.ga_len - argc;
4216 sctx_T save_current_sctx = current_sctx;
4217 int did_emsg_before = did_emsg_cumul + did_emsg;
4218 int save_suppress_errthrow = suppress_errthrow;
4219 msglist_T **saved_msg_list = NULL;
4220 msglist_T *private_msg_list = NULL;
4221 int save_emsg_silent_def = emsg_silent_def;
4222 int save_did_emsg_def = did_emsg_def;
4223 int orig_funcdepth;
4224
4225// Get pointer to item in the stack.
4226#undef STACK_TV
4227#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4228
4229// Get pointer to item at the bottom of the stack, -1 is the bottom.
4230#undef STACK_TV_BOT
4231#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4232
4233// Get pointer to a local variable on the stack. Negative for arguments.
4234#undef STACK_TV_VAR
4235#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4236
4237 if (ufunc->uf_def_status == UF_NOT_COMPILED
4238 || ufunc->uf_def_status == UF_COMPILE_ERROR
4239 || (func_needs_compiling(ufunc, PROFILING(ufunc))
4240 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
4241 == FAIL))
4242 {
4243 if (did_emsg_cumul + did_emsg == did_emsg_before)
4244 semsg(_(e_function_is_not_compiled_str),
4245 printable_func_name(ufunc));
4246 return FAIL;
4247 }
4248
4249 {
4250 // Check the function was really compiled.
4251 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4252 + ufunc->uf_dfunc_idx;
4253 if (INSTRUCTIONS(dfunc) == NULL)
4254 {
4255 iemsg("using call_def_function() on not compiled function");
4256 return FAIL;
4257 }
4258 }
4259
4260 // If depth of calling is getting too high, don't execute the function.
4261 orig_funcdepth = funcdepth_get();
4262 if (funcdepth_increment() == FAIL)
4263 return FAIL;
4264
4265 CLEAR_FIELD(ectx);
4266 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4267 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4268 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4269 {
4270 funcdepth_decrement();
4271 return FAIL;
4272 }
4273 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4274 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4275 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004276
4277 idx = argc - ufunc->uf_args.ga_len;
4278 if (idx > 0 && ufunc->uf_va_name == NULL)
4279 {
4280 if (idx == 1)
4281 emsg(_(e_one_argument_too_many));
4282 else
4283 semsg(_(e_nr_arguments_too_many), idx);
4284 goto failed_early;
4285 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004286 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4287 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004288 {
4289 if (idx == -1)
4290 emsg(_(e_one_argument_too_few));
4291 else
4292 semsg(_(e_nr_arguments_too_few), -idx);
4293 goto failed_early;
4294 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004295
4296 // Put arguments on the stack, but no more than what the function expects.
4297 // A lambda can be called with more arguments than it uses.
4298 for (idx = 0; idx < argc
4299 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4300 ++idx)
4301 {
4302 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4303 && argv[idx].v_type == VAR_SPECIAL
4304 && argv[idx].vval.v_number == VVAL_NONE)
4305 {
4306 // Use the default value.
4307 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4308 }
4309 else
4310 {
4311 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4312 && check_typval_arg_type(
4313 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4314 goto failed_early;
4315 copy_tv(&argv[idx], STACK_TV_BOT(0));
4316 }
4317 ++ectx.ec_stack.ga_len;
4318 }
4319
4320 // Turn varargs into a list. Empty list if no args.
4321 if (ufunc->uf_va_name != NULL)
4322 {
4323 int vararg_count = argc - ufunc->uf_args.ga_len;
4324
4325 if (vararg_count < 0)
4326 vararg_count = 0;
4327 else
4328 argc -= vararg_count;
4329 if (exe_newlist(vararg_count, &ectx) == FAIL)
4330 goto failed_early;
4331
4332 // Check the type of the list items.
4333 tv = STACK_TV_BOT(-1);
4334 if (ufunc->uf_va_type != NULL
4335 && ufunc->uf_va_type != &t_list_any
4336 && ufunc->uf_va_type->tt_member != &t_any
4337 && tv->vval.v_list != NULL)
4338 {
4339 type_T *expected = ufunc->uf_va_type->tt_member;
4340 listitem_T *li = tv->vval.v_list->lv_first;
4341
4342 for (idx = 0; idx < vararg_count; ++idx)
4343 {
4344 if (check_typval_arg_type(expected, &li->li_tv,
4345 argc + idx + 1) == FAIL)
4346 goto failed_early;
4347 li = li->li_next;
4348 }
4349 }
4350
4351 if (defcount > 0)
4352 // Move varargs list to below missing default arguments.
4353 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4354 --ectx.ec_stack.ga_len;
4355 }
4356
4357 // Make space for omitted arguments, will store default value below.
4358 // Any varargs list goes after them.
4359 if (defcount > 0)
4360 for (idx = 0; idx < defcount; ++idx)
4361 {
4362 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4363 ++ectx.ec_stack.ga_len;
4364 }
4365 if (ufunc->uf_va_name != NULL)
4366 ++ectx.ec_stack.ga_len;
4367
4368 // Frame pointer points to just after arguments.
4369 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4370 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4371
4372 {
4373 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4374 + ufunc->uf_dfunc_idx;
4375 ufunc_T *base_ufunc = dfunc->df_ufunc;
4376
4377 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4378 // by copy_func().
4379 if (partial != NULL || base_ufunc->uf_partial != NULL)
4380 {
4381 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
4382 if (ectx.ec_outer == NULL)
4383 goto failed_early;
4384 if (partial != NULL)
4385 {
4386 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4387 {
4388 if (current_ectx->ec_outer != NULL)
4389 *ectx.ec_outer = *current_ectx->ec_outer;
4390 }
4391 else
4392 *ectx.ec_outer = partial->pt_outer;
4393 }
4394 else
4395 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
4396 ectx.ec_outer->out_up_is_copy = TRUE;
4397 }
4398 }
4399
4400 // dummy frame entries
4401 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4402 {
4403 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4404 ++ectx.ec_stack.ga_len;
4405 }
4406
4407 {
4408 // Reserve space for local variables and any closure reference count.
4409 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4410 + ufunc->uf_dfunc_idx;
4411
4412 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4413 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4414 ectx.ec_stack.ga_len += dfunc->df_varcount;
4415 if (dfunc->df_has_closure)
4416 {
4417 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4418 STACK_TV_VAR(idx)->vval.v_number = 0;
4419 ++ectx.ec_stack.ga_len;
4420 }
4421
4422 ectx.ec_instr = INSTRUCTIONS(dfunc);
4423 }
4424
4425 // Following errors are in the function, not the caller.
4426 // Commands behave like vim9script.
4427 estack_push_ufunc(ufunc, 1);
4428 current_sctx = ufunc->uf_script_ctx;
4429 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4430
4431 // Use a specific location for storing error messages to be converted to an
4432 // exception.
4433 saved_msg_list = msg_list;
4434 msg_list = &private_msg_list;
4435
4436 // Do turn errors into exceptions.
4437 suppress_errthrow = FALSE;
4438
4439 // Do not delete the function while executing it.
4440 ++ufunc->uf_calls;
4441
4442 // When ":silent!" was used before calling then we still abort the
4443 // function. If ":silent!" is used in the function then we don't.
4444 emsg_silent_def = emsg_silent;
4445 did_emsg_def = 0;
4446
4447 ectx.ec_where.wt_index = 0;
4448 ectx.ec_where.wt_variable = FALSE;
4449
4450 // Execute the instructions until done.
4451 ret = exec_instructions(&ectx);
4452 if (ret == OK)
4453 {
4454 // function finished, get result from the stack.
4455 if (ufunc->uf_ret_type == &t_void)
4456 {
4457 rettv->v_type = VAR_VOID;
4458 }
4459 else
4460 {
4461 tv = STACK_TV_BOT(-1);
4462 *rettv = *tv;
4463 tv->v_type = VAR_UNKNOWN;
4464 }
4465 }
4466
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004467 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004468 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4469 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004470
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004471 // Deal with any remaining closures, they may be in use somewhere.
4472 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004473 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004474 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004475 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4476 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004477
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004478 estack_pop();
4479 current_sctx = save_current_sctx;
4480
Bram Moolenaarc970e422021-03-17 15:03:04 +01004481 // TODO: when is it safe to delete the function if it is no longer used?
4482 --ufunc->uf_calls;
4483
Bram Moolenaar352134b2020-10-17 22:04:08 +02004484 if (*msg_list != NULL && saved_msg_list != NULL)
4485 {
4486 msglist_T **plist = saved_msg_list;
4487
4488 // Append entries from the current msg_list (uncaught exceptions) to
4489 // the saved msg_list.
4490 while (*plist != NULL)
4491 plist = &(*plist)->next;
4492
4493 *plist = *msg_list;
4494 }
4495 msg_list = saved_msg_list;
4496
Bram Moolenaar4c137212021-04-19 16:48:48 +02004497 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004498 {
4499 cmdmod.cmod_filter_regmatch.regprog = NULL;
4500 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004501 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004502 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004503 emsg_silent_def = save_emsg_silent_def;
4504 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004505
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004506failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004507 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4509 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004512 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004513
Bram Moolenaar0186e582021-01-10 18:33:11 +01004514 while (ectx.ec_outer != NULL)
4515 {
4516 outer_T *up = ectx.ec_outer->out_up_is_copy
4517 ? NULL : ectx.ec_outer->out_up;
4518
4519 vim_free(ectx.ec_outer);
4520 ectx.ec_outer = up;
4521 }
4522
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004523 // Not sure if this is necessary.
4524 suppress_errthrow = save_suppress_errthrow;
4525
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004526 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004527 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004528 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004529 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 return ret;
4531}
4532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004534 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4535 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4536 * "pfx" is prefixed to every line.
4537 */
4538 static void
4539list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4540{
4541 int line_idx = 0;
4542 int prev_current = 0;
4543 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004544 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004545
4546 for (current = 0; current < instr_count; ++current)
4547 {
4548 isn_T *iptr = &instr[current];
4549 char *line;
4550
4551 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004552 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004553 while (line_idx < iptr->isn_lnum
4554 && line_idx < ufunc->uf_lines.ga_len)
4555 {
4556 if (current > prev_current)
4557 {
4558 msg_puts("\n\n");
4559 prev_current = current;
4560 }
4561 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4562 if (line != NULL)
4563 msg(line);
4564 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004565 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4566 {
4567 int first_def_arg = ufunc->uf_args.ga_len
4568 - ufunc->uf_def_args.ga_len;
4569
4570 if (def_arg_idx > 0)
4571 msg_puts("\n\n");
4572 msg_start();
4573 msg_puts(" ");
4574 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4575 first_def_arg + def_arg_idx]);
4576 msg_puts(" = ");
4577 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4578 msg_clr_eos();
4579 msg_end();
4580 }
4581 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004582
4583 switch (iptr->isn_type)
4584 {
4585 case ISN_EXEC:
4586 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4587 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004588 case ISN_EXEC_SPLIT:
4589 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4590 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004591 case ISN_LEGACY_EVAL:
4592 smsg("%s%4d EVAL legacy %s", pfx, current,
4593 iptr->isn_arg.string);
4594 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004595 case ISN_REDIRSTART:
4596 smsg("%s%4d REDIR", pfx, current);
4597 break;
4598 case ISN_REDIREND:
4599 smsg("%s%4d REDIR END%s", pfx, current,
4600 iptr->isn_arg.number ? " append" : "");
4601 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004602 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004603#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004604 smsg("%s%4d CEXPR pre %s", pfx, current,
4605 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004606#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004607 break;
4608 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004609#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004610 {
4611 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4612
4613 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4614 cexpr_get_auname(cer->cer_cmdidx),
4615 cer->cer_forceit ? "!" : "",
4616 cer->cer_cmdline);
4617 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004618#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004619 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004620 case ISN_INSTR:
4621 {
4622 smsg("%s%4d INSTR", pfx, current);
4623 list_instructions(" ", iptr->isn_arg.instr,
4624 INT_MAX, NULL);
4625 msg(" -------------");
4626 }
4627 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004628 case ISN_SUBSTITUTE:
4629 {
4630 subs_T *subs = &iptr->isn_arg.subs;
4631
4632 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4633 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4634 msg(" -------------");
4635 }
4636 break;
4637 case ISN_EXECCONCAT:
4638 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4639 (varnumber_T)iptr->isn_arg.number);
4640 break;
4641 case ISN_ECHO:
4642 {
4643 echo_T *echo = &iptr->isn_arg.echo;
4644
4645 smsg("%s%4d %s %d", pfx, current,
4646 echo->echo_with_white ? "ECHO" : "ECHON",
4647 echo->echo_count);
4648 }
4649 break;
4650 case ISN_EXECUTE:
4651 smsg("%s%4d EXECUTE %lld", pfx, current,
4652 (varnumber_T)(iptr->isn_arg.number));
4653 break;
4654 case ISN_ECHOMSG:
4655 smsg("%s%4d ECHOMSG %lld", pfx, current,
4656 (varnumber_T)(iptr->isn_arg.number));
4657 break;
4658 case ISN_ECHOERR:
4659 smsg("%s%4d ECHOERR %lld", pfx, current,
4660 (varnumber_T)(iptr->isn_arg.number));
4661 break;
4662 case ISN_LOAD:
4663 {
4664 if (iptr->isn_arg.number < 0)
4665 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4666 (varnumber_T)(iptr->isn_arg.number
4667 + STACK_FRAME_SIZE));
4668 else
4669 smsg("%s%4d LOAD $%lld", pfx, current,
4670 (varnumber_T)(iptr->isn_arg.number));
4671 }
4672 break;
4673 case ISN_LOADOUTER:
4674 {
4675 if (iptr->isn_arg.number < 0)
4676 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4677 iptr->isn_arg.outer.outer_depth,
4678 iptr->isn_arg.outer.outer_idx
4679 + STACK_FRAME_SIZE);
4680 else
4681 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4682 iptr->isn_arg.outer.outer_depth,
4683 iptr->isn_arg.outer.outer_idx);
4684 }
4685 break;
4686 case ISN_LOADV:
4687 smsg("%s%4d LOADV v:%s", pfx, current,
4688 get_vim_var_name(iptr->isn_arg.number));
4689 break;
4690 case ISN_LOADSCRIPT:
4691 {
4692 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4693 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4694 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4695 + sref->sref_idx;
4696
4697 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4698 sv->sv_name,
4699 sref->sref_idx,
4700 si->sn_name);
4701 }
4702 break;
4703 case ISN_LOADS:
4704 {
4705 scriptitem_T *si = SCRIPT_ITEM(
4706 iptr->isn_arg.loadstore.ls_sid);
4707
4708 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4709 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4710 }
4711 break;
4712 case ISN_LOADAUTO:
4713 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4714 break;
4715 case ISN_LOADG:
4716 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4717 break;
4718 case ISN_LOADB:
4719 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4720 break;
4721 case ISN_LOADW:
4722 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4723 break;
4724 case ISN_LOADT:
4725 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4726 break;
4727 case ISN_LOADGDICT:
4728 smsg("%s%4d LOAD g:", pfx, current);
4729 break;
4730 case ISN_LOADBDICT:
4731 smsg("%s%4d LOAD b:", pfx, current);
4732 break;
4733 case ISN_LOADWDICT:
4734 smsg("%s%4d LOAD w:", pfx, current);
4735 break;
4736 case ISN_LOADTDICT:
4737 smsg("%s%4d LOAD t:", pfx, current);
4738 break;
4739 case ISN_LOADOPT:
4740 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4741 break;
4742 case ISN_LOADENV:
4743 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4744 break;
4745 case ISN_LOADREG:
4746 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4747 break;
4748
4749 case ISN_STORE:
4750 if (iptr->isn_arg.number < 0)
4751 smsg("%s%4d STORE arg[%lld]", pfx, current,
4752 iptr->isn_arg.number + STACK_FRAME_SIZE);
4753 else
4754 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4755 break;
4756 case ISN_STOREOUTER:
4757 {
4758 if (iptr->isn_arg.number < 0)
4759 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4760 iptr->isn_arg.outer.outer_depth,
4761 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4762 else
4763 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4764 iptr->isn_arg.outer.outer_depth,
4765 iptr->isn_arg.outer.outer_idx);
4766 }
4767 break;
4768 case ISN_STOREV:
4769 smsg("%s%4d STOREV v:%s", pfx, current,
4770 get_vim_var_name(iptr->isn_arg.number));
4771 break;
4772 case ISN_STOREAUTO:
4773 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4774 break;
4775 case ISN_STOREG:
4776 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4777 break;
4778 case ISN_STOREB:
4779 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4780 break;
4781 case ISN_STOREW:
4782 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4783 break;
4784 case ISN_STORET:
4785 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4786 break;
4787 case ISN_STORES:
4788 {
4789 scriptitem_T *si = SCRIPT_ITEM(
4790 iptr->isn_arg.loadstore.ls_sid);
4791
4792 smsg("%s%4d STORES %s in %s", pfx, current,
4793 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4794 }
4795 break;
4796 case ISN_STORESCRIPT:
4797 {
4798 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4799 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4800 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4801 + sref->sref_idx;
4802
4803 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4804 sv->sv_name,
4805 sref->sref_idx,
4806 si->sn_name);
4807 }
4808 break;
4809 case ISN_STOREOPT:
4810 smsg("%s%4d STOREOPT &%s", pfx, current,
4811 iptr->isn_arg.storeopt.so_name);
4812 break;
4813 case ISN_STOREENV:
4814 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4815 break;
4816 case ISN_STOREREG:
4817 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4818 break;
4819 case ISN_STORENR:
4820 smsg("%s%4d STORE %lld in $%d", pfx, current,
4821 iptr->isn_arg.storenr.stnr_val,
4822 iptr->isn_arg.storenr.stnr_idx);
4823 break;
4824
4825 case ISN_STOREINDEX:
4826 smsg("%s%4d STOREINDEX %s", pfx, current,
4827 vartype_name(iptr->isn_arg.vartype));
4828 break;
4829
4830 case ISN_STORERANGE:
4831 smsg("%s%4d STORERANGE", pfx, current);
4832 break;
4833
4834 // constants
4835 case ISN_PUSHNR:
4836 smsg("%s%4d PUSHNR %lld", pfx, current,
4837 (varnumber_T)(iptr->isn_arg.number));
4838 break;
4839 case ISN_PUSHBOOL:
4840 case ISN_PUSHSPEC:
4841 smsg("%s%4d PUSH %s", pfx, current,
4842 get_var_special_name(iptr->isn_arg.number));
4843 break;
4844 case ISN_PUSHF:
4845#ifdef FEAT_FLOAT
4846 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4847#endif
4848 break;
4849 case ISN_PUSHS:
4850 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4851 break;
4852 case ISN_PUSHBLOB:
4853 {
4854 char_u *r;
4855 char_u numbuf[NUMBUFLEN];
4856 char_u *tofree;
4857
4858 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4859 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4860 vim_free(tofree);
4861 }
4862 break;
4863 case ISN_PUSHFUNC:
4864 {
4865 char *name = (char *)iptr->isn_arg.string;
4866
4867 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4868 name == NULL ? "[none]" : name);
4869 }
4870 break;
4871 case ISN_PUSHCHANNEL:
4872#ifdef FEAT_JOB_CHANNEL
4873 {
4874 channel_T *channel = iptr->isn_arg.channel;
4875
4876 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4877 channel == NULL ? 0 : channel->ch_id);
4878 }
4879#endif
4880 break;
4881 case ISN_PUSHJOB:
4882#ifdef FEAT_JOB_CHANNEL
4883 {
4884 typval_T tv;
4885 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004886 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02004887
4888 tv.v_type = VAR_JOB;
4889 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004890 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004891 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4892 }
4893#endif
4894 break;
4895 case ISN_PUSHEXC:
4896 smsg("%s%4d PUSH v:exception", pfx, current);
4897 break;
4898 case ISN_UNLET:
4899 smsg("%s%4d UNLET%s %s", pfx, current,
4900 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4901 iptr->isn_arg.unlet.ul_name);
4902 break;
4903 case ISN_UNLETENV:
4904 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4905 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4906 iptr->isn_arg.unlet.ul_name);
4907 break;
4908 case ISN_UNLETINDEX:
4909 smsg("%s%4d UNLETINDEX", pfx, current);
4910 break;
4911 case ISN_UNLETRANGE:
4912 smsg("%s%4d UNLETRANGE", pfx, current);
4913 break;
4914 case ISN_LOCKCONST:
4915 smsg("%s%4d LOCKCONST", pfx, current);
4916 break;
4917 case ISN_NEWLIST:
4918 smsg("%s%4d NEWLIST size %lld", pfx, current,
4919 (varnumber_T)(iptr->isn_arg.number));
4920 break;
4921 case ISN_NEWDICT:
4922 smsg("%s%4d NEWDICT size %lld", pfx, current,
4923 (varnumber_T)(iptr->isn_arg.number));
4924 break;
4925
4926 // function call
4927 case ISN_BCALL:
4928 {
4929 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4930
4931 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4932 internal_func_name(cbfunc->cbf_idx),
4933 cbfunc->cbf_argcount);
4934 }
4935 break;
4936 case ISN_DCALL:
4937 {
4938 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4939 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4940 + cdfunc->cdf_idx;
4941
4942 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
4943 df->df_ufunc->uf_name_exp != NULL
4944 ? df->df_ufunc->uf_name_exp
4945 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4946 }
4947 break;
4948 case ISN_UCALL:
4949 {
4950 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4951
4952 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
4953 cufunc->cuf_name, cufunc->cuf_argcount);
4954 }
4955 break;
4956 case ISN_PCALL:
4957 {
4958 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4959
4960 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
4961 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4962 }
4963 break;
4964 case ISN_PCALL_END:
4965 smsg("%s%4d PCALL end", pfx, current);
4966 break;
4967 case ISN_RETURN:
4968 smsg("%s%4d RETURN", pfx, current);
4969 break;
4970 case ISN_RETURN_ZERO:
4971 smsg("%s%4d RETURN 0", pfx, current);
4972 break;
4973 case ISN_FUNCREF:
4974 {
4975 funcref_T *funcref = &iptr->isn_arg.funcref;
4976 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4977 + funcref->fr_func;
4978
4979 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
4980 }
4981 break;
4982
4983 case ISN_NEWFUNC:
4984 {
4985 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4986
4987 smsg("%s%4d NEWFUNC %s %s", pfx, current,
4988 newfunc->nf_lambda, newfunc->nf_global);
4989 }
4990 break;
4991
4992 case ISN_DEF:
4993 {
4994 char_u *name = iptr->isn_arg.string;
4995
4996 smsg("%s%4d DEF %s", pfx, current,
4997 name == NULL ? (char_u *)"" : name);
4998 }
4999 break;
5000
5001 case ISN_JUMP:
5002 {
5003 char *when = "?";
5004
5005 switch (iptr->isn_arg.jump.jump_when)
5006 {
5007 case JUMP_ALWAYS:
5008 when = "JUMP";
5009 break;
5010 case JUMP_AND_KEEP_IF_TRUE:
5011 when = "JUMP_AND_KEEP_IF_TRUE";
5012 break;
5013 case JUMP_IF_FALSE:
5014 when = "JUMP_IF_FALSE";
5015 break;
5016 case JUMP_AND_KEEP_IF_FALSE:
5017 when = "JUMP_AND_KEEP_IF_FALSE";
5018 break;
5019 case JUMP_IF_COND_FALSE:
5020 when = "JUMP_IF_COND_FALSE";
5021 break;
5022 case JUMP_IF_COND_TRUE:
5023 when = "JUMP_IF_COND_TRUE";
5024 break;
5025 }
5026 smsg("%s%4d %s -> %d", pfx, current, when,
5027 iptr->isn_arg.jump.jump_where);
5028 }
5029 break;
5030
5031 case ISN_JUMP_IF_ARG_SET:
5032 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5033 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5034 iptr->isn_arg.jump.jump_where);
5035 break;
5036
5037 case ISN_FOR:
5038 {
5039 forloop_T *forloop = &iptr->isn_arg.forloop;
5040
5041 smsg("%s%4d FOR $%d -> %d", pfx, current,
5042 forloop->for_idx, forloop->for_end);
5043 }
5044 break;
5045
5046 case ISN_TRY:
5047 {
5048 try_T *try = &iptr->isn_arg.try;
5049
5050 if (try->try_ref->try_finally == 0)
5051 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5052 pfx, current,
5053 try->try_ref->try_catch,
5054 try->try_ref->try_endtry);
5055 else
5056 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5057 pfx, current,
5058 try->try_ref->try_catch,
5059 try->try_ref->try_finally,
5060 try->try_ref->try_endtry);
5061 }
5062 break;
5063 case ISN_CATCH:
5064 // TODO
5065 smsg("%s%4d CATCH", pfx, current);
5066 break;
5067 case ISN_TRYCONT:
5068 {
5069 trycont_T *trycont = &iptr->isn_arg.trycont;
5070
5071 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5072 trycont->tct_levels,
5073 trycont->tct_levels == 1 ? "" : "s",
5074 trycont->tct_where);
5075 }
5076 break;
5077 case ISN_FINALLY:
5078 smsg("%s%4d FINALLY", pfx, current);
5079 break;
5080 case ISN_ENDTRY:
5081 smsg("%s%4d ENDTRY", pfx, current);
5082 break;
5083 case ISN_THROW:
5084 smsg("%s%4d THROW", pfx, current);
5085 break;
5086
5087 // expression operations on number
5088 case ISN_OPNR:
5089 case ISN_OPFLOAT:
5090 case ISN_OPANY:
5091 {
5092 char *what;
5093 char *ins;
5094
5095 switch (iptr->isn_arg.op.op_type)
5096 {
5097 case EXPR_MULT: what = "*"; break;
5098 case EXPR_DIV: what = "/"; break;
5099 case EXPR_REM: what = "%"; break;
5100 case EXPR_SUB: what = "-"; break;
5101 case EXPR_ADD: what = "+"; break;
5102 default: what = "???"; break;
5103 }
5104 switch (iptr->isn_type)
5105 {
5106 case ISN_OPNR: ins = "OPNR"; break;
5107 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5108 case ISN_OPANY: ins = "OPANY"; break;
5109 default: ins = "???"; break;
5110 }
5111 smsg("%s%4d %s %s", pfx, current, ins, what);
5112 }
5113 break;
5114
5115 case ISN_COMPAREBOOL:
5116 case ISN_COMPARESPECIAL:
5117 case ISN_COMPARENR:
5118 case ISN_COMPAREFLOAT:
5119 case ISN_COMPARESTRING:
5120 case ISN_COMPAREBLOB:
5121 case ISN_COMPARELIST:
5122 case ISN_COMPAREDICT:
5123 case ISN_COMPAREFUNC:
5124 case ISN_COMPAREANY:
5125 {
5126 char *p;
5127 char buf[10];
5128 char *type;
5129
5130 switch (iptr->isn_arg.op.op_type)
5131 {
5132 case EXPR_EQUAL: p = "=="; break;
5133 case EXPR_NEQUAL: p = "!="; break;
5134 case EXPR_GREATER: p = ">"; break;
5135 case EXPR_GEQUAL: p = ">="; break;
5136 case EXPR_SMALLER: p = "<"; break;
5137 case EXPR_SEQUAL: p = "<="; break;
5138 case EXPR_MATCH: p = "=~"; break;
5139 case EXPR_IS: p = "is"; break;
5140 case EXPR_ISNOT: p = "isnot"; break;
5141 case EXPR_NOMATCH: p = "!~"; break;
5142 default: p = "???"; break;
5143 }
5144 STRCPY(buf, p);
5145 if (iptr->isn_arg.op.op_ic == TRUE)
5146 strcat(buf, "?");
5147 switch(iptr->isn_type)
5148 {
5149 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5150 case ISN_COMPARESPECIAL:
5151 type = "COMPARESPECIAL"; break;
5152 case ISN_COMPARENR: type = "COMPARENR"; break;
5153 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5154 case ISN_COMPARESTRING:
5155 type = "COMPARESTRING"; break;
5156 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5157 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5158 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5159 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5160 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5161 default: type = "???"; break;
5162 }
5163
5164 smsg("%s%4d %s %s", pfx, current, type, buf);
5165 }
5166 break;
5167
5168 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5169 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5170
5171 // expression operations
5172 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5173 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5174 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5175 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5176 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5177 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5178 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5179 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5180 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5181 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5182 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5183 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5184 pfx, current, iptr->isn_arg.number); break;
5185 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5186 pfx, current, iptr->isn_arg.number); break;
5187 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5188 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5189 iptr->isn_arg.string); break;
5190 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5191
5192 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5193 case ISN_CHECKTYPE:
5194 {
5195 checktype_T *ct = &iptr->isn_arg.type;
5196 char *tofree;
5197
5198 if (ct->ct_arg_idx == 0)
5199 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5200 type_name(ct->ct_type, &tofree),
5201 (int)ct->ct_off);
5202 else
5203 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5204 type_name(ct->ct_type, &tofree),
5205 (int)ct->ct_off,
5206 (int)ct->ct_arg_idx);
5207 vim_free(tofree);
5208 break;
5209 }
5210 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5211 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5212 iptr->isn_arg.checklen.cl_min_len);
5213 break;
5214 case ISN_SETTYPE:
5215 {
5216 char *tofree;
5217
5218 smsg("%s%4d SETTYPE %s", pfx, current,
5219 type_name(iptr->isn_arg.type.ct_type, &tofree));
5220 vim_free(tofree);
5221 break;
5222 }
5223 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005224 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5225 smsg("%s%4d INVERT %d (!val)", pfx, current,
5226 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005227 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005228 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5229 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005230 break;
5231 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005232 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005233 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005234 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5235 pfx, current,
5236 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005237 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005238 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5239 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005240 break;
5241 case ISN_PUT:
5242 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5243 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005244 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005245 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5246 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005247 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005248 else
5249 smsg("%s%4d PUT %c %ld", pfx, current,
5250 iptr->isn_arg.put.put_regname,
5251 (long)iptr->isn_arg.put.put_lnum);
5252 break;
5253
5254 // TODO: summarize modifiers
5255 case ISN_CMDMOD:
5256 {
5257 char_u *buf;
5258 size_t len = produce_cmdmods(
5259 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5260
5261 buf = alloc(len + 1);
5262 if (buf != NULL)
5263 {
5264 (void)produce_cmdmods(
5265 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5266 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5267 vim_free(buf);
5268 }
5269 break;
5270 }
5271 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5272
5273 case ISN_PROF_START:
5274 smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum);
5275 break;
5276
5277 case ISN_PROF_END:
5278 smsg("%s%4d PROFILE END", pfx, current);
5279 break;
5280
5281 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5282 iptr->isn_arg.unpack.unp_count,
5283 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5284 break;
5285 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5286 iptr->isn_arg.shuffle.shfl_item,
5287 iptr->isn_arg.shuffle.shfl_up);
5288 break;
5289 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5290
5291 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5292 return;
5293 }
5294
5295 out_flush(); // output one line at a time
5296 ui_breakcheck();
5297 if (got_int)
5298 break;
5299 }
5300}
5301
5302/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005303 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005304 * We don't really need this at runtime, but we do have tests that require it,
5305 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 */
5307 void
5308ex_disassemble(exarg_T *eap)
5309{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005310 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005311 char_u *fname;
5312 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313 dfunc_T *dfunc;
5314 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005315 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005316 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005318 if (STRNCMP(arg, "<lambda>", 8) == 0)
5319 {
5320 arg += 8;
5321 (void)getdigits(&arg);
5322 fname = vim_strnsave(eap->arg, arg - eap->arg);
5323 }
5324 else
5325 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005326 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005327 if (fname == NULL)
5328 {
5329 semsg(_(e_invarg2), eap->arg);
5330 return;
5331 }
5332
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005333 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005334 if (ufunc == NULL)
5335 {
5336 char_u *p = untrans_function_name(fname);
5337
5338 if (p != NULL)
5339 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005340 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005341 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005342 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005343 if (ufunc == NULL)
5344 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005345 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 return;
5347 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005348 if (func_needs_compiling(ufunc, eap->forceit)
5349 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005350 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005351 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005352 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005353 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 return;
5355 }
5356 if (ufunc->uf_name_exp != NULL)
5357 msg((char *)ufunc->uf_name_exp);
5358 else
5359 msg((char *)ufunc->uf_name);
5360
5361 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005362#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005363 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
5364 instr_count = eap->forceit ? dfunc->df_instr_prof_count
5365 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005366#else
5367 instr = dfunc->df_instr;
5368 instr_count = dfunc->df_instr_count;
5369#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370
Bram Moolenaar4c137212021-04-19 16:48:48 +02005371 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005372}
5373
5374/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005375 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376 * list, etc. Mostly like what JavaScript does, except that empty list and
5377 * empty dictionary are FALSE.
5378 */
5379 int
5380tv2bool(typval_T *tv)
5381{
5382 switch (tv->v_type)
5383 {
5384 case VAR_NUMBER:
5385 return tv->vval.v_number != 0;
5386 case VAR_FLOAT:
5387#ifdef FEAT_FLOAT
5388 return tv->vval.v_float != 0.0;
5389#else
5390 break;
5391#endif
5392 case VAR_PARTIAL:
5393 return tv->vval.v_partial != NULL;
5394 case VAR_FUNC:
5395 case VAR_STRING:
5396 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5397 case VAR_LIST:
5398 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5399 case VAR_DICT:
5400 return tv->vval.v_dict != NULL
5401 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5402 case VAR_BOOL:
5403 case VAR_SPECIAL:
5404 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5405 case VAR_JOB:
5406#ifdef FEAT_JOB_CHANNEL
5407 return tv->vval.v_job != NULL;
5408#else
5409 break;
5410#endif
5411 case VAR_CHANNEL:
5412#ifdef FEAT_JOB_CHANNEL
5413 return tv->vval.v_channel != NULL;
5414#else
5415 break;
5416#endif
5417 case VAR_BLOB:
5418 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5419 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005420 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005422 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 break;
5424 }
5425 return FALSE;
5426}
5427
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005428 void
5429emsg_using_string_as(typval_T *tv, int as_number)
5430{
5431 semsg(_(as_number ? e_using_string_as_number_str
5432 : e_using_string_as_bool_str),
5433 tv->vval.v_string == NULL
5434 ? (char_u *)"" : tv->vval.v_string);
5435}
5436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437/*
5438 * If "tv" is a string give an error and return FAIL.
5439 */
5440 int
5441check_not_string(typval_T *tv)
5442{
5443 if (tv->v_type == VAR_STRING)
5444 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005445 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446 clear_tv(tv);
5447 return FAIL;
5448 }
5449 return OK;
5450}
5451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452
5453#endif // FEAT_EVAL