blob: b23879d3ef2d924654f140f6915a61d7d9803e4c [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 {
1002 char_u *p;
1003
1004 str = typval2string(tv, TRUE);
1005 clear_tv(tv);
1006 tv->v_type = VAR_STRING;
1007 tv->vval.v_string = str;
1008 // TODO: escaping
1009 while ((p = vim_strchr(str, '\n')) != NULL)
1010 *p = ' ';
1011 return OK;
1012 }
1013 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001014 default: to_string_error(tv->v_type);
1015 return FAIL;
1016 }
1017 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001018 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001019 clear_tv(tv);
1020 tv->v_type = VAR_STRING;
1021 tv->vval.v_string = str;
1022 }
1023 return OK;
1024}
1025
1026/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001027 * When the value of "sv" is a null list of dict, allocate it.
1028 */
1029 static void
1030allocate_if_null(typval_T *tv)
1031{
1032 switch (tv->v_type)
1033 {
1034 case VAR_LIST:
1035 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001036 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001037 break;
1038 case VAR_DICT:
1039 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001040 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001041 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001042 case VAR_BLOB:
1043 if (tv->vval.v_blob == NULL)
1044 (void)rettv_blob_alloc(tv);
1045 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001046 default:
1047 break;
1048 }
1049}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001050
Bram Moolenaare7525c52021-01-09 13:20:37 +01001051/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001052 * Return the character "str[index]" where "index" is the character index,
1053 * including composing characters.
1054 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001055 */
1056 char_u *
1057char_from_string(char_u *str, varnumber_T index)
1058{
1059 size_t nbyte = 0;
1060 varnumber_T nchar = index;
1061 size_t slen;
1062
1063 if (str == NULL)
1064 return NULL;
1065 slen = STRLEN(str);
1066
Bram Moolenaarff871402021-03-26 13:34:05 +01001067 // Do the same as for a list: a negative index counts from the end.
1068 // Optimization to check the first byte to be below 0x80 (and no composing
1069 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001070 if (index < 0)
1071 {
1072 int clen = 0;
1073
1074 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001075 {
1076 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1077 ++nbyte;
1078 else if (enc_utf8)
1079 nbyte += utfc_ptr2len(str + nbyte);
1080 else
1081 nbyte += mb_ptr2len(str + nbyte);
1082 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001083 nchar = clen + index;
1084 if (nchar < 0)
1085 // unlike list: index out of range results in empty string
1086 return NULL;
1087 }
1088
1089 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001090 {
1091 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1092 ++nbyte;
1093 else if (enc_utf8)
1094 nbyte += utfc_ptr2len(str + nbyte);
1095 else
1096 nbyte += mb_ptr2len(str + nbyte);
1097 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001098 if (nbyte >= slen)
1099 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001100 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001101}
1102
1103/*
1104 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001105 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001106 * If going over the end return "str_len".
1107 * If "idx" is negative count from the end, -1 is the last character.
1108 * When going over the start return -1.
1109 */
1110 static long
1111char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1112{
1113 varnumber_T nchar = idx;
1114 size_t nbyte = 0;
1115
1116 if (nchar >= 0)
1117 {
1118 while (nchar > 0 && nbyte < str_len)
1119 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001120 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001121 --nchar;
1122 }
1123 }
1124 else
1125 {
1126 nbyte = str_len;
1127 while (nchar < 0 && nbyte > 0)
1128 {
1129 --nbyte;
1130 nbyte -= mb_head_off(str, str + nbyte);
1131 ++nchar;
1132 }
1133 if (nchar < 0)
1134 return -1;
1135 }
1136 return (long)nbyte;
1137}
1138
1139/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001140 * Return the slice "str[first : last]" using character indexes. Composing
1141 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001142 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001143 * Return NULL when the result is empty.
1144 */
1145 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001146string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001147{
1148 long start_byte, end_byte;
1149 size_t slen;
1150
1151 if (str == NULL)
1152 return NULL;
1153 slen = STRLEN(str);
1154 start_byte = char_idx2byte(str, slen, first);
1155 if (start_byte < 0)
1156 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001157 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001158 end_byte = (long)slen;
1159 else
1160 {
1161 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001162 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001163 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001164 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001165 }
1166
1167 if (start_byte >= (long)slen || end_byte <= start_byte)
1168 return NULL;
1169 return vim_strnsave(str + start_byte, end_byte - start_byte);
1170}
1171
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001172 static svar_T *
1173get_script_svar(scriptref_T *sref, ectx_T *ectx)
1174{
1175 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1176 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1177 + ectx->ec_dfunc_idx;
1178 svar_T *sv;
1179
1180 if (sref->sref_seq != si->sn_script_seq)
1181 {
1182 // The script was reloaded after the function was
1183 // compiled, the script_idx may not be valid.
1184 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1185 dfunc->df_ufunc->uf_name_exp);
1186 return NULL;
1187 }
1188 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1189 if (!equal_type(sv->sv_type, sref->sref_type))
1190 {
1191 emsg(_(e_script_variable_type_changed));
1192 return NULL;
1193 }
1194 return sv;
1195}
1196
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001197/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 * Execute a function by "name".
1199 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001200 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 */
1202 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001203call_eval_func(
1204 char_u *name,
1205 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001206 ectx_T *ectx,
1207 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208{
Bram Moolenaared677f52020-08-12 16:38:10 +02001209 int called_emsg_before = called_emsg;
1210 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211
Bram Moolenaar4c137212021-04-19 16:48:48 +02001212 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001213 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001215 dictitem_T *v;
1216
1217 v = find_var(name, NULL, FALSE);
1218 if (v == NULL)
1219 {
1220 semsg(_(e_unknownfunc), name);
1221 return FAIL;
1222 }
1223 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1224 {
1225 semsg(_(e_unknownfunc), name);
1226 return FAIL;
1227 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001228 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001230 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231}
1232
1233/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001234 * When a function reference is used, fill a partial with the information
1235 * needed, especially when it is used as a closure.
1236 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001237 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001238fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1239{
1240 pt->pt_func = ufunc;
1241 pt->pt_refcount = 1;
1242
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001243 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001244 {
1245 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1246 + ectx->ec_dfunc_idx;
1247
1248 // The closure needs to find arguments and local
1249 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001250 pt->pt_outer.out_stack = &ectx->ec_stack;
1251 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1252 pt->pt_outer.out_up = ectx->ec_outer;
1253 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001254
1255 // If this function returns and the closure is still
1256 // being used, we need to make a copy of the context
1257 // (arguments and local variables). Store a reference
1258 // to the partial so we can handle that.
1259 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1260 {
1261 vim_free(pt);
1262 return FAIL;
1263 }
1264 // Extra variable keeps the count of closures created
1265 // in the current function call.
1266 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1267 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1268
1269 ((partial_T **)ectx->ec_funcrefs.ga_data)
1270 [ectx->ec_funcrefs.ga_len] = pt;
1271 ++pt->pt_refcount;
1272 ++ectx->ec_funcrefs.ga_len;
1273 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001274 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001275 return OK;
1276}
1277
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001278// used for v_instr of typval of VAR_INSTR
1279struct instr_S {
1280 ectx_T *instr_ectx;
1281 isn_T *instr_instr;
1282};
1283
Bram Moolenaar4c137212021-04-19 16:48:48 +02001284// used for substitute_instr
1285typedef struct subs_expr_S {
1286 ectx_T *subs_ectx;
1287 isn_T *subs_instr;
1288 int subs_status;
1289} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290
1291// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001292#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293
1294// Get pointer to item at the bottom of the stack, -1 is the bottom.
1295#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001296#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 +01001297
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001298// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001299#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 +01001300
Bram Moolenaar4c137212021-04-19 16:48:48 +02001301/*
1302 * Execute instructions in execution context "ectx".
1303 * Return OK or FAIL;
1304 */
1305 static int
1306exec_instructions(ectx_T *ectx)
1307{
1308 int breakcheck_count = 0;
1309 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001310 int ret = FAIL;
1311 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001312
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001313 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001314 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001315
Bram Moolenaarff652882021-05-16 15:24:49 +02001316 // Only catch exceptions in this instruction list.
1317 ectx->ec_trylevel_at_start = trylevel;
1318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 for (;;)
1320 {
1321 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001322
Bram Moolenaar270d0382020-05-15 21:42:53 +02001323 if (++breakcheck_count >= 100)
1324 {
1325 line_breakcheck();
1326 breakcheck_count = 0;
1327 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001328 if (got_int)
1329 {
1330 // Turn CTRL-C into an exception.
1331 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001332 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001333 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001334 did_throw = TRUE;
1335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336
Bram Moolenaara26b9702020-04-18 19:53:28 +02001337 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1338 {
1339 // Turn an error message into an exception.
1340 did_emsg = FALSE;
1341 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001342 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001343 did_throw = TRUE;
1344 *msg_list = NULL;
1345 }
1346
Bram Moolenaar4c137212021-04-19 16:48:48 +02001347 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001349 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001350 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351
1352 // An exception jumps to the first catch, finally, or returns from
1353 // the current function.
1354 if (trystack->ga_len > 0)
1355 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001356 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 {
1358 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001359 ectx->ec_in_catch = TRUE;
1360 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 }
1362 else
1363 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001364 // Not inside try or need to return from current functions.
1365 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001366 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001367 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001368 tv = STACK_TV_BOT(0);
1369 tv->v_type = VAR_NUMBER;
1370 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001371 ++ectx->ec_stack.ga_len;
1372 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001374 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001375 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001376 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001377 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 goto done;
1379 }
1380
Bram Moolenaar4c137212021-04-19 16:48:48 +02001381 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001382 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 }
1384 continue;
1385 }
1386
Bram Moolenaar4c137212021-04-19 16:48:48 +02001387 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 switch (iptr->isn_type)
1389 {
1390 // execute Ex command line
1391 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001392 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001393 source_cookie_T cookie;
1394
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001395 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001396 // Pass getsourceline to get an error for a missing ":end"
1397 // command.
1398 CLEAR_FIELD(cookie);
1399 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1400 if (do_cmdline(iptr->isn_arg.string,
1401 getsourceline, &cookie,
1402 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1403 == FAIL
1404 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001405 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 break;
1408
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001409 // Evaluate an expression with legacy syntax, push it onto the
1410 // stack.
1411 case ISN_LEGACY_EVAL:
1412 {
1413 char_u *arg = iptr->isn_arg.string;
1414 int res;
1415 int save_flags = cmdmod.cmod_flags;
1416
1417 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001418 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001419 tv = STACK_TV_BOT(0);
1420 init_tv(tv);
1421 cmdmod.cmod_flags |= CMOD_LEGACY;
1422 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1423 cmdmod.cmod_flags = save_flags;
1424 if (res == FAIL)
1425 goto on_error;
1426 ++ectx->ec_stack.ga_len;
1427 }
1428 break;
1429
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001430 // push typeval VAR_INSTR with instructions to be executed
1431 case ISN_INSTR:
1432 {
1433 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001434 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001435 tv = STACK_TV_BOT(0);
1436 tv->vval.v_instr = ALLOC_ONE(instr_T);
1437 if (tv->vval.v_instr == NULL)
1438 goto on_error;
1439 ++ectx->ec_stack.ga_len;
1440
1441 tv->v_type = VAR_INSTR;
1442 tv->vval.v_instr->instr_ectx = ectx;
1443 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1444 }
1445 break;
1446
Bram Moolenaar4c137212021-04-19 16:48:48 +02001447 // execute :substitute with an expression
1448 case ISN_SUBSTITUTE:
1449 {
1450 subs_T *subs = &iptr->isn_arg.subs;
1451 source_cookie_T cookie;
1452 struct subs_expr_S *save_instr = substitute_instr;
1453 struct subs_expr_S subs_instr;
1454 int res;
1455
1456 subs_instr.subs_ectx = ectx;
1457 subs_instr.subs_instr = subs->subs_instr;
1458 subs_instr.subs_status = OK;
1459 substitute_instr = &subs_instr;
1460
1461 SOURCING_LNUM = iptr->isn_lnum;
1462 // This is very much like ISN_EXEC
1463 CLEAR_FIELD(cookie);
1464 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1465 res = do_cmdline(subs->subs_cmd,
1466 getsourceline, &cookie,
1467 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1468 substitute_instr = save_instr;
1469
1470 if (res == FAIL || did_emsg
1471 || subs_instr.subs_status == FAIL)
1472 goto on_error;
1473 }
1474 break;
1475
1476 case ISN_FINISH:
1477 goto done;
1478
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001479 case ISN_REDIRSTART:
1480 // create a dummy entry for var_redir_str()
1481 if (alloc_redir_lval() == FAIL)
1482 goto on_error;
1483
1484 // The output is stored in growarray "redir_ga" until
1485 // redirection ends.
1486 init_redir_ga();
1487 redir_vname = 1;
1488 break;
1489
1490 case ISN_REDIREND:
1491 {
1492 char_u *res = get_clear_redir_ga();
1493
1494 // End redirection, put redirected text on the stack.
1495 clear_redir_lval();
1496 redir_vname = 0;
1497
1498 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1499 {
1500 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001501 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001502 }
1503 tv = STACK_TV_BOT(0);
1504 tv->v_type = VAR_STRING;
1505 tv->vval.v_string = res;
1506 ++ectx->ec_stack.ga_len;
1507 }
1508 break;
1509
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001510 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001511#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001512 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1513 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001514#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001515 break;
1516
1517 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001518#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001519 {
1520 exarg_T ea;
1521 int res;
1522
1523 CLEAR_FIELD(ea);
1524 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1525 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1526 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1527 --ectx->ec_stack.ga_len;
1528 tv = STACK_TV_BOT(0);
1529 res = cexpr_core(&ea, tv);
1530 clear_tv(tv);
1531 if (res == FAIL)
1532 goto on_error;
1533 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001534#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001535 break;
1536
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001537 // execute Ex command from pieces on the stack
1538 case ISN_EXECCONCAT:
1539 {
1540 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001541 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001542 int pass;
1543 int i;
1544 char_u *cmd = NULL;
1545 char_u *str;
1546
1547 for (pass = 1; pass <= 2; ++pass)
1548 {
1549 for (i = 0; i < count; ++i)
1550 {
1551 tv = STACK_TV_BOT(i - count);
1552 str = tv->vval.v_string;
1553 if (str != NULL && *str != NUL)
1554 {
1555 if (pass == 2)
1556 STRCPY(cmd + len, str);
1557 len += STRLEN(str);
1558 }
1559 if (pass == 2)
1560 clear_tv(tv);
1561 }
1562 if (pass == 1)
1563 {
1564 cmd = alloc(len + 1);
1565 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001566 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001567 len = 0;
1568 }
1569 }
1570
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001571 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001572 do_cmdline_cmd(cmd);
1573 vim_free(cmd);
1574 }
1575 break;
1576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 // execute :echo {string} ...
1578 case ISN_ECHO:
1579 {
1580 int count = iptr->isn_arg.echo.echo_count;
1581 int atstart = TRUE;
1582 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001583 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584
1585 for (idx = 0; idx < count; ++idx)
1586 {
1587 tv = STACK_TV_BOT(idx - count);
1588 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1589 &atstart, &needclr);
1590 clear_tv(tv);
1591 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001592 if (needclr)
1593 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001594 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 }
1596 break;
1597
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001598 // :execute {string} ...
1599 // :echomsg {string} ...
1600 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001601 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001602 case ISN_ECHOMSG:
1603 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001604 {
1605 int count = iptr->isn_arg.number;
1606 garray_T ga;
1607 char_u buf[NUMBUFLEN];
1608 char_u *p;
1609 int len;
1610 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001611 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001612
1613 ga_init2(&ga, 1, 80);
1614 for (idx = 0; idx < count; ++idx)
1615 {
1616 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001617 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001618 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001619 if (tv->v_type == VAR_CHANNEL
1620 || tv->v_type == VAR_JOB)
1621 {
1622 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001623 semsg(_(e_using_invalid_value_as_string_str),
1624 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001625 break;
1626 }
1627 else
1628 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001629 }
1630 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001631 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001632
1633 len = (int)STRLEN(p);
1634 if (ga_grow(&ga, len + 2) == FAIL)
1635 failed = TRUE;
1636 else
1637 {
1638 if (ga.ga_len > 0)
1639 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1640 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1641 ga.ga_len += len;
1642 }
1643 clear_tv(tv);
1644 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001645 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001646 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001647 {
1648 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001649 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001650 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001651
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001652 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001653 {
1654 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001655 {
1656 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001657 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001658 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001659 {
1660 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001661 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001662 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001663 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001664 else
1665 {
1666 msg_sb_eol();
1667 if (iptr->isn_type == ISN_ECHOMSG)
1668 {
1669 msg_attr(ga.ga_data, echo_attr);
1670 out_flush();
1671 }
1672 else
1673 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001674 SOURCING_LNUM = iptr->isn_lnum;
1675 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001676 }
1677 }
1678 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001679 ga_clear(&ga);
1680 }
1681 break;
1682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 // load local variable or argument
1684 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001685 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001686 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001688 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 break;
1690
1691 // load v: variable
1692 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001693 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001694 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001696 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 break;
1698
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001699 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 case ISN_LOADSCRIPT:
1701 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001702 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 svar_T *sv;
1704
Bram Moolenaar4c137212021-04-19 16:48:48 +02001705 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001706 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001707 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001708 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001709 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001710 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001712 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 }
1714 break;
1715
1716 // load s: variable in old script
1717 case ISN_LOADS:
1718 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001719 hashtab_T *ht = &SCRIPT_VARS(
1720 iptr->isn_arg.loadstore.ls_sid);
1721 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 if (di == NULL)
1725 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001726 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001727 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001728 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 }
1730 else
1731 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001732 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001733 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001735 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 }
1737 }
1738 break;
1739
Bram Moolenaard3aac292020-04-19 14:32:17 +02001740 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001742 case ISN_LOADB:
1743 case ISN_LOADW:
1744 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001746 dictitem_T *di = NULL;
1747 hashtab_T *ht = NULL;
1748 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001749
Bram Moolenaard3aac292020-04-19 14:32:17 +02001750 switch (iptr->isn_type)
1751 {
1752 case ISN_LOADG:
1753 ht = get_globvar_ht();
1754 namespace = 'g';
1755 break;
1756 case ISN_LOADB:
1757 ht = &curbuf->b_vars->dv_hashtab;
1758 namespace = 'b';
1759 break;
1760 case ISN_LOADW:
1761 ht = &curwin->w_vars->dv_hashtab;
1762 namespace = 'w';
1763 break;
1764 case ISN_LOADT:
1765 ht = &curtab->tp_vars->dv_hashtab;
1766 namespace = 't';
1767 break;
1768 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001769 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001770 }
1771 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 if (di == NULL)
1774 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001775 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001776 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001777 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001778 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 }
1780 else
1781 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001782 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001783 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001785 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 }
1787 }
1788 break;
1789
Bram Moolenaar03290b82020-12-19 16:30:44 +01001790 // load autoload variable
1791 case ISN_LOADAUTO:
1792 {
1793 char_u *name = iptr->isn_arg.string;
1794
Bram Moolenaar4c137212021-04-19 16:48:48 +02001795 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001796 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001797 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001798 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001799 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001800 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001801 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001802 }
1803 break;
1804
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001805 // load g:/b:/w:/t: namespace
1806 case ISN_LOADGDICT:
1807 case ISN_LOADBDICT:
1808 case ISN_LOADWDICT:
1809 case ISN_LOADTDICT:
1810 {
1811 dict_T *d = NULL;
1812
1813 switch (iptr->isn_type)
1814 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001815 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1816 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1817 case ISN_LOADWDICT: d = curwin->w_vars; break;
1818 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001819 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001820 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001821 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001822 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001823 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001824 tv = STACK_TV_BOT(0);
1825 tv->v_type = VAR_DICT;
1826 tv->v_lock = 0;
1827 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001828 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001829 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001830 }
1831 break;
1832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 // load &option
1834 case ISN_LOADOPT:
1835 {
1836 typval_T optval;
1837 char_u *name = iptr->isn_arg.string;
1838
Bram Moolenaara8c17702020-04-01 21:17:24 +02001839 // This is not expected to fail, name is checked during
1840 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001841 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001842 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001843 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001844 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001846 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 }
1848 break;
1849
1850 // load $ENV
1851 case ISN_LOADENV:
1852 {
1853 typval_T optval;
1854 char_u *name = iptr->isn_arg.string;
1855
Bram Moolenaar4c137212021-04-19 16:48:48 +02001856 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001857 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001858 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001859 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001861 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 }
1863 break;
1864
1865 // load @register
1866 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001867 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001868 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 tv = STACK_TV_BOT(0);
1870 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001871 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001872 // This may result in NULL, which should be equivalent to an
1873 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 tv->vval.v_string = get_reg_contents(
1875 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001876 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 break;
1878
1879 // store local variable
1880 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001881 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 tv = STACK_TV_VAR(iptr->isn_arg.number);
1883 clear_tv(tv);
1884 *tv = *STACK_TV_BOT(0);
1885 break;
1886
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001887 // store s: variable in old script
1888 case ISN_STORES:
1889 {
1890 hashtab_T *ht = &SCRIPT_VARS(
1891 iptr->isn_arg.loadstore.ls_sid);
1892 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001893 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001894
Bram Moolenaar4c137212021-04-19 16:48:48 +02001895 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001896 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001897 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001898 else
1899 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001900 SOURCING_LNUM = iptr->isn_lnum;
1901 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001902 {
1903 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001904 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001905 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001906 clear_tv(&di->di_tv);
1907 di->di_tv = *STACK_TV_BOT(0);
1908 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001909 }
1910 break;
1911
1912 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 case ISN_STORESCRIPT:
1914 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001915 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1916 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917
Bram Moolenaar4c137212021-04-19 16:48:48 +02001918 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001919 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001920 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001921 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001922
1923 // "const" and "final" are checked at compile time, locking
1924 // the value needs to be checked here.
1925 SOURCING_LNUM = iptr->isn_lnum;
1926 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001927 {
1928 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001929 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001930 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 clear_tv(sv->sv_tv);
1933 *sv->sv_tv = *STACK_TV_BOT(0);
1934 }
1935 break;
1936
1937 // store option
1938 case ISN_STOREOPT:
1939 {
1940 long n = 0;
1941 char_u *s = NULL;
1942 char *msg;
1943
Bram Moolenaar4c137212021-04-19 16:48:48 +02001944 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 tv = STACK_TV_BOT(0);
1946 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001947 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001949 if (s == NULL)
1950 s = (char_u *)"";
1951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001953 // must be VAR_NUMBER, CHECKTYPE makes sure
1954 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1956 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001957 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 if (msg != NULL)
1959 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001960 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001962 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 }
1965 break;
1966
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001967 // store $ENV
1968 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001969 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001970 tv = STACK_TV_BOT(0);
1971 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1972 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001973 break;
1974
1975 // store @r
1976 case ISN_STOREREG:
1977 {
1978 int reg = iptr->isn_arg.number;
1979
Bram Moolenaar4c137212021-04-19 16:48:48 +02001980 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001981 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001982 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001983 tv_get_string(tv), -1, FALSE);
1984 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001985 }
1986 break;
1987
1988 // store v: variable
1989 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001990 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001991 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1992 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001993 // should not happen, type is checked when compiling
1994 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001995 break;
1996
Bram Moolenaard3aac292020-04-19 14:32:17 +02001997 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001999 case ISN_STOREB:
2000 case ISN_STOREW:
2001 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002003 dictitem_T *di;
2004 hashtab_T *ht;
2005 char_u *name = iptr->isn_arg.string + 2;
2006
Bram Moolenaard3aac292020-04-19 14:32:17 +02002007 switch (iptr->isn_type)
2008 {
2009 case ISN_STOREG:
2010 ht = get_globvar_ht();
2011 break;
2012 case ISN_STOREB:
2013 ht = &curbuf->b_vars->dv_hashtab;
2014 break;
2015 case ISN_STOREW:
2016 ht = &curwin->w_vars->dv_hashtab;
2017 break;
2018 case ISN_STORET:
2019 ht = &curtab->tp_vars->dv_hashtab;
2020 break;
2021 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002022 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024
Bram Moolenaar4c137212021-04-19 16:48:48 +02002025 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002026 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002028 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 else
2030 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002031 SOURCING_LNUM = iptr->isn_lnum;
2032 if (var_check_permission(di, name) == FAIL)
2033 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 clear_tv(&di->di_tv);
2035 di->di_tv = *STACK_TV_BOT(0);
2036 }
2037 }
2038 break;
2039
Bram Moolenaar03290b82020-12-19 16:30:44 +01002040 // store an autoload variable
2041 case ISN_STOREAUTO:
2042 SOURCING_LNUM = iptr->isn_lnum;
2043 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2044 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002045 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002046 break;
2047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 // store number in local variable
2049 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002050 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 clear_tv(tv);
2052 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002053 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 break;
2055
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002056 // store value in list or dict variable
2057 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002058 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002059 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002060 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002061 typval_T *tv_dest = STACK_TV_BOT(-1);
2062 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002063
Bram Moolenaar752fc692021-01-04 21:57:11 +01002064 // Stack contains:
2065 // -3 value to be stored
2066 // -2 index
2067 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002068 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002069 SOURCING_LNUM = iptr->isn_lnum;
2070 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002071 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002072 dest_type = tv_dest->v_type;
2073 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002074 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002075 else if (dest_type == VAR_LIST
2076 && tv_idx->v_type != VAR_NUMBER)
2077 {
2078 emsg(_(e_number_exp));
2079 status = FAIL;
2080 }
2081 }
2082 else if (dest_type != tv_dest->v_type)
2083 {
2084 // just in case, should be OK
2085 semsg(_(e_expected_str_but_got_str),
2086 vartype_name(dest_type),
2087 vartype_name(tv_dest->v_type));
2088 status = FAIL;
2089 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002090
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002091 if (status == OK && dest_type == VAR_LIST)
2092 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002093 long lidx = (long)tv_idx->vval.v_number;
2094 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002095
2096 if (list == NULL)
2097 {
2098 emsg(_(e_list_not_set));
2099 goto on_error;
2100 }
2101 if (lidx < 0 && list->lv_len + lidx >= 0)
2102 // negative index is relative to the end
2103 lidx = list->lv_len + lidx;
2104 if (lidx < 0 || lidx > list->lv_len)
2105 {
2106 semsg(_(e_listidx), lidx);
2107 goto on_error;
2108 }
2109 if (lidx < list->lv_len)
2110 {
2111 listitem_T *li = list_find(list, lidx);
2112
2113 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002114 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002115 goto on_error;
2116 // overwrite existing list item
2117 clear_tv(&li->li_tv);
2118 li->li_tv = *tv;
2119 }
2120 else
2121 {
2122 if (error_if_locked(list->lv_lock,
2123 e_cannot_change_list))
2124 goto on_error;
2125 // append to list, only fails when out of memory
2126 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002127 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002128 clear_tv(tv);
2129 }
2130 }
2131 else if (status == OK && dest_type == VAR_DICT)
2132 {
2133 char_u *key = tv_idx->vval.v_string;
2134 dict_T *dict = tv_dest->vval.v_dict;
2135 dictitem_T *di;
2136
2137 SOURCING_LNUM = iptr->isn_lnum;
2138 if (dict == NULL)
2139 {
2140 emsg(_(e_dictionary_not_set));
2141 goto on_error;
2142 }
2143 if (key == NULL)
2144 key = (char_u *)"";
2145 di = dict_find(dict, key, -1);
2146 if (di != NULL)
2147 {
2148 if (error_if_locked(di->di_tv.v_lock,
2149 e_cannot_change_dict_item))
2150 goto on_error;
2151 // overwrite existing value
2152 clear_tv(&di->di_tv);
2153 di->di_tv = *tv;
2154 }
2155 else
2156 {
2157 if (error_if_locked(dict->dv_lock,
2158 e_cannot_change_dict))
2159 goto on_error;
2160 // add to dict, only fails when out of memory
2161 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002162 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002163 clear_tv(tv);
2164 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002165 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002166 else if (status == OK && dest_type == VAR_BLOB)
2167 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002168 long lidx = (long)tv_idx->vval.v_number;
2169 blob_T *blob = tv_dest->vval.v_blob;
2170 varnumber_T nr;
2171 int error = FALSE;
2172 int len;
2173
2174 if (blob == NULL)
2175 {
2176 emsg(_(e_blob_not_set));
2177 goto on_error;
2178 }
2179 len = blob_len(blob);
2180 if (lidx < 0 && len + lidx >= 0)
2181 // negative index is relative to the end
2182 lidx = len + lidx;
2183
2184 // Can add one byte at the end.
2185 if (lidx < 0 || lidx > len)
2186 {
2187 semsg(_(e_blobidx), lidx);
2188 goto on_error;
2189 }
2190 if (value_check_lock(blob->bv_lock,
2191 (char_u *)"blob", FALSE))
2192 goto on_error;
2193 nr = tv_get_number_chk(tv, &error);
2194 if (error)
2195 goto on_error;
2196 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002197 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002198 else
2199 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002200 status = FAIL;
2201 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002202 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002203
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002204 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002205 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002206 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002207 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002208 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002209 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002210 goto on_error;
2211 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002212 }
2213 break;
2214
Bram Moolenaar68452172021-04-12 21:21:02 +02002215 // store value in blob range
2216 case ISN_STORERANGE:
2217 {
2218 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2219 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2220 typval_T *tv_dest = STACK_TV_BOT(-1);
2221 int status = OK;
2222
2223 // Stack contains:
2224 // -4 value to be stored
2225 // -3 first index or "none"
2226 // -2 second index or "none"
2227 // -1 destination blob
2228 tv = STACK_TV_BOT(-4);
2229 if (tv_dest->v_type != VAR_BLOB)
2230 {
2231 status = FAIL;
2232 emsg(_(e_blob_required));
2233 }
2234 else
2235 {
2236 varnumber_T n1;
2237 varnumber_T n2;
2238 int error = FALSE;
2239
2240 n1 = tv_get_number_chk(tv_idx1, &error);
2241 if (error)
2242 status = FAIL;
2243 else
2244 {
2245 if (tv_idx2->v_type == VAR_SPECIAL
2246 && tv_idx2->vval.v_number == VVAL_NONE)
2247 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2248 else
2249 n2 = tv_get_number_chk(tv_idx2, &error);
2250 if (error)
2251 status = FAIL;
2252 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002253 {
2254 long bloblen = blob_len(tv_dest->vval.v_blob);
2255
2256 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002257 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002258 || check_blob_range(bloblen,
2259 n1, n2, FALSE) == FAIL)
2260 status = FAIL;
2261 else
2262 status = blob_set_range(
2263 tv_dest->vval.v_blob, n1, n2, tv);
2264 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002265 }
2266 }
2267
2268 clear_tv(tv_idx1);
2269 clear_tv(tv_idx2);
2270 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002271 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002272 clear_tv(tv);
2273
2274 if (status == FAIL)
2275 goto on_error;
2276 }
2277 break;
2278
Bram Moolenaar0186e582021-01-10 18:33:11 +01002279 // load or store variable or argument from outer scope
2280 case ISN_LOADOUTER:
2281 case ISN_STOREOUTER:
2282 {
2283 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002284 outer_T *outer = ectx->ec_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002285
2286 while (depth > 1 && outer != NULL)
2287 {
2288 outer = outer->out_up;
2289 --depth;
2290 }
2291 if (outer == NULL)
2292 {
2293 SOURCING_LNUM = iptr->isn_lnum;
2294 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002295 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002296 }
2297 tv = ((typval_T *)outer->out_stack->ga_data)
2298 + outer->out_frame_idx + STACK_FRAME_SIZE
2299 + iptr->isn_arg.outer.outer_idx;
2300 if (iptr->isn_type == ISN_LOADOUTER)
2301 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002302 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002303 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002304 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002305 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002306 }
2307 else
2308 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002309 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002310 clear_tv(tv);
2311 *tv = *STACK_TV_BOT(0);
2312 }
2313 }
2314 break;
2315
Bram Moolenaar752fc692021-01-04 21:57:11 +01002316 // unlet item in list or dict variable
2317 case ISN_UNLETINDEX:
2318 {
2319 typval_T *tv_idx = STACK_TV_BOT(-2);
2320 typval_T *tv_dest = STACK_TV_BOT(-1);
2321 int status = OK;
2322
2323 // Stack contains:
2324 // -2 index
2325 // -1 dict or list
2326 if (tv_dest->v_type == VAR_DICT)
2327 {
2328 // unlet a dict item, index must be a string
2329 if (tv_idx->v_type != VAR_STRING)
2330 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002332 semsg(_(e_expected_str_but_got_str),
2333 vartype_name(VAR_STRING),
2334 vartype_name(tv_idx->v_type));
2335 status = FAIL;
2336 }
2337 else
2338 {
2339 dict_T *d = tv_dest->vval.v_dict;
2340 char_u *key = tv_idx->vval.v_string;
2341 dictitem_T *di = NULL;
2342
2343 if (key == NULL)
2344 key = (char_u *)"";
2345 if (d != NULL)
2346 di = dict_find(d, key, (int)STRLEN(key));
2347 if (di == NULL)
2348 {
2349 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002350 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002351 semsg(_(e_dictkey), key);
2352 status = FAIL;
2353 }
2354 else
2355 {
2356 // TODO: check for dict or item locked
2357 dictitem_remove(d, di);
2358 }
2359 }
2360 }
2361 else if (tv_dest->v_type == VAR_LIST)
2362 {
2363 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002364 SOURCING_LNUM = iptr->isn_lnum;
2365 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002366 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002367 status = FAIL;
2368 }
2369 else
2370 {
2371 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002372 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002373 listitem_T *li = NULL;
2374
2375 li = list_find(l, n);
2376 if (li == NULL)
2377 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002378 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002379 semsg(_(e_listidx), n);
2380 status = FAIL;
2381 }
2382 else
2383 // TODO: check for list or item locked
2384 listitem_remove(l, li);
2385 }
2386 }
2387 else
2388 {
2389 status = FAIL;
2390 semsg(_(e_cannot_index_str),
2391 vartype_name(tv_dest->v_type));
2392 }
2393
2394 clear_tv(tv_idx);
2395 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002396 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002397 if (status == FAIL)
2398 goto on_error;
2399 }
2400 break;
2401
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002402 // unlet range of items in list variable
2403 case ISN_UNLETRANGE:
2404 {
2405 // Stack contains:
2406 // -3 index1
2407 // -2 index2
2408 // -1 dict or list
2409 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2410 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2411 typval_T *tv_dest = STACK_TV_BOT(-1);
2412 int status = OK;
2413
2414 if (tv_dest->v_type == VAR_LIST)
2415 {
2416 // indexes must be a number
2417 SOURCING_LNUM = iptr->isn_lnum;
2418 if (check_for_number(tv_idx1) == FAIL
2419 || check_for_number(tv_idx2) == FAIL)
2420 {
2421 status = FAIL;
2422 }
2423 else
2424 {
2425 list_T *l = tv_dest->vval.v_list;
2426 long n1 = (long)tv_idx1->vval.v_number;
2427 long n2 = (long)tv_idx2->vval.v_number;
2428 listitem_T *li;
2429
2430 li = list_find_index(l, &n1);
2431 if (li == NULL
2432 || list_unlet_range(l, li, NULL, n1,
2433 TRUE, n2) == FAIL)
2434 status = FAIL;
2435 }
2436 }
2437 else
2438 {
2439 status = FAIL;
2440 SOURCING_LNUM = iptr->isn_lnum;
2441 semsg(_(e_cannot_index_str),
2442 vartype_name(tv_dest->v_type));
2443 }
2444
2445 clear_tv(tv_idx1);
2446 clear_tv(tv_idx2);
2447 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002448 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002449 if (status == FAIL)
2450 goto on_error;
2451 }
2452 break;
2453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 // push constant
2455 case ISN_PUSHNR:
2456 case ISN_PUSHBOOL:
2457 case ISN_PUSHSPEC:
2458 case ISN_PUSHF:
2459 case ISN_PUSHS:
2460 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002461 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002462 case ISN_PUSHCHANNEL:
2463 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002464 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002465 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002467 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002468 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 switch (iptr->isn_type)
2470 {
2471 case ISN_PUSHNR:
2472 tv->v_type = VAR_NUMBER;
2473 tv->vval.v_number = iptr->isn_arg.number;
2474 break;
2475 case ISN_PUSHBOOL:
2476 tv->v_type = VAR_BOOL;
2477 tv->vval.v_number = iptr->isn_arg.number;
2478 break;
2479 case ISN_PUSHSPEC:
2480 tv->v_type = VAR_SPECIAL;
2481 tv->vval.v_number = iptr->isn_arg.number;
2482 break;
2483#ifdef FEAT_FLOAT
2484 case ISN_PUSHF:
2485 tv->v_type = VAR_FLOAT;
2486 tv->vval.v_float = iptr->isn_arg.fnumber;
2487 break;
2488#endif
2489 case ISN_PUSHBLOB:
2490 blob_copy(iptr->isn_arg.blob, tv);
2491 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002492 case ISN_PUSHFUNC:
2493 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002494 if (iptr->isn_arg.string == NULL)
2495 tv->vval.v_string = NULL;
2496 else
2497 tv->vval.v_string =
2498 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002499 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002500 case ISN_PUSHCHANNEL:
2501#ifdef FEAT_JOB_CHANNEL
2502 tv->v_type = VAR_CHANNEL;
2503 tv->vval.v_channel = iptr->isn_arg.channel;
2504 if (tv->vval.v_channel != NULL)
2505 ++tv->vval.v_channel->ch_refcount;
2506#endif
2507 break;
2508 case ISN_PUSHJOB:
2509#ifdef FEAT_JOB_CHANNEL
2510 tv->v_type = VAR_JOB;
2511 tv->vval.v_job = iptr->isn_arg.job;
2512 if (tv->vval.v_job != NULL)
2513 ++tv->vval.v_job->jv_refcount;
2514#endif
2515 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 default:
2517 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002518 tv->vval.v_string = vim_strsave(
2519 iptr->isn_arg.string == NULL
2520 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 }
2522 break;
2523
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002524 case ISN_UNLET:
2525 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2526 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002527 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002528 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002529 case ISN_UNLETENV:
2530 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2531 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002532
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002533 case ISN_LOCKCONST:
2534 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2535 break;
2536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 // create a list from items on the stack; uses a single allocation
2538 // for the list header and the items
2539 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002540 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002541 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 break;
2543
2544 // create a dict from items on the stack
2545 case ISN_NEWDICT:
2546 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002547 int count = iptr->isn_arg.number;
2548 dict_T *dict = dict_alloc();
2549 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002550 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002551 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552
2553 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002554 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 for (idx = 0; idx < count; ++idx)
2556 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002557 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002559 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002560 key = tv->vval.v_string == NULL
2561 ? (char_u *)"" : tv->vval.v_string;
2562 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002563 if (item != NULL)
2564 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002565 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002566 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002567 dict_unref(dict);
2568 goto on_error;
2569 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002570 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 clear_tv(tv);
2572 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002573 {
2574 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002575 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2578 item->di_tv.v_lock = 0;
2579 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002580 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002581 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002582 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002583 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 }
2586
2587 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002588 ectx->ec_stack.ga_len -= 2 * count - 1;
2589 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002590 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002592 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 tv = STACK_TV_BOT(-1);
2594 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002595 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 tv->vval.v_dict = dict;
2597 ++dict->dv_refcount;
2598 }
2599 break;
2600
2601 // call a :def function
2602 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002603 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002604 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2605 NULL,
2606 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002607 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002608 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 break;
2610
2611 // call a builtin function
2612 case ISN_BCALL:
2613 SOURCING_LNUM = iptr->isn_lnum;
2614 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2615 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002616 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002617 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 break;
2619
2620 // call a funcref or partial
2621 case ISN_PCALL:
2622 {
2623 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2624 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002625 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626
2627 SOURCING_LNUM = iptr->isn_lnum;
2628 if (pfunc->cpf_top)
2629 {
2630 // funcref is above the arguments
2631 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2632 }
2633 else
2634 {
2635 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002636 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002637 partial_tv = *STACK_TV_BOT(0);
2638 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002640 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002641 if (tv == &partial_tv)
2642 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002644 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 }
2646 break;
2647
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002648 case ISN_PCALL_END:
2649 // PCALL finished, arguments have been consumed and replaced by
2650 // the return value. Now clear the funcref from the stack,
2651 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002652 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002653 clear_tv(STACK_TV_BOT(-1));
2654 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2655 break;
2656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 // call a user defined function or funcref/partial
2658 case ISN_UCALL:
2659 {
2660 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2661
2662 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002663 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002664 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002665 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 }
2667 break;
2668
2669 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002670 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002671 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002672 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002673 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002674 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002675 tv->v_type = VAR_NUMBER;
2676 tv->vval.v_number = 0;
2677 tv->v_lock = 0;
2678 // FALLTHROUGH
2679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 case ISN_RETURN:
2681 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002682 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002683 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002684
2685 if (trystack->ga_len > 0)
2686 trycmd = ((trycmd_T *)trystack->ga_data)
2687 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002688 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002689 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002691 // jump to ":finally" or ":endtry"
2692 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002693 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002694 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002695 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 trycmd->tcd_return = TRUE;
2697 }
2698 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002699 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 }
2701 break;
2702
2703 // push a function reference to a compiled function
2704 case ISN_FUNCREF:
2705 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002706 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2707 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2708 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002711 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002712 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002713 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002714 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002715 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002716 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002717 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002718 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002719 goto theend;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002722 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 tv->vval.v_partial = pt;
2724 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002725 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 }
2727 break;
2728
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002729 // Create a global function from a lambda.
2730 case ISN_NEWFUNC:
2731 {
2732 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2733
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002734 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002735 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002736 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002737 }
2738 break;
2739
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002740 // List functions
2741 case ISN_DEF:
2742 if (iptr->isn_arg.string == NULL)
2743 list_functions(NULL);
2744 else
2745 {
2746 exarg_T ea;
2747
2748 CLEAR_FIELD(ea);
2749 ea.cmd = ea.arg = iptr->isn_arg.string;
2750 define_function(&ea, NULL);
2751 }
2752 break;
2753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 // jump if a condition is met
2755 case ISN_JUMP:
2756 {
2757 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002758 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 int jump = TRUE;
2760
2761 if (when != JUMP_ALWAYS)
2762 {
2763 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002764 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002765 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002766 || when == JUMP_IF_COND_TRUE)
2767 {
2768 SOURCING_LNUM = iptr->isn_lnum;
2769 jump = tv_get_bool_chk(tv, &error);
2770 if (error)
2771 goto on_error;
2772 }
2773 else
2774 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002776 || when == JUMP_AND_KEEP_IF_FALSE
2777 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002779 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 {
2781 // drop the value from the stack
2782 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002783 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 }
2785 }
2786 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002787 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 }
2789 break;
2790
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002791 // Jump if an argument with a default value was already set and not
2792 // v:none.
2793 case ISN_JUMP_IF_ARG_SET:
2794 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2795 if (tv->v_type != VAR_UNKNOWN
2796 && !(tv->v_type == VAR_SPECIAL
2797 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002798 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002799 break;
2800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 // top of a for loop
2802 case ISN_FOR:
2803 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002804 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 typval_T *idxtv =
2806 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2807
Bram Moolenaar4c137212021-04-19 16:48:48 +02002808 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002809 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002810 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002811 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002812 list_T *list = ltv->vval.v_list;
2813
2814 // push the next item from the list
2815 ++idxtv->vval.v_number;
2816 if (list == NULL
2817 || idxtv->vval.v_number >= list->lv_len)
2818 {
2819 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002820 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2821 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002822 }
2823 else if (list->lv_first == &range_list_item)
2824 {
2825 // non-materialized range() list
2826 tv = STACK_TV_BOT(0);
2827 tv->v_type = VAR_NUMBER;
2828 tv->v_lock = 0;
2829 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002831 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002832 }
2833 else
2834 {
2835 listitem_T *li = list_find(list,
2836 idxtv->vval.v_number);
2837
2838 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002839 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002840 }
2841 }
2842 else if (ltv->v_type == VAR_STRING)
2843 {
2844 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002845
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002846 // The index is for the last byte of the previous
2847 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002848 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002849 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002850 {
2851 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002852 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2853 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002854 }
2855 else
2856 {
2857 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2858
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002859 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002860 tv = STACK_TV_BOT(0);
2861 tv->v_type = VAR_STRING;
2862 tv->vval.v_string = vim_strnsave(
2863 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002864 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002865 idxtv->vval.v_number += clen - 1;
2866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002868 else if (ltv->v_type == VAR_BLOB)
2869 {
2870 blob_T *blob = ltv->vval.v_blob;
2871
2872 // When we get here the first time make a copy of the
2873 // blob, so that the iteration still works when it is
2874 // changed.
2875 if (idxtv->vval.v_number == -1 && blob != NULL)
2876 {
2877 blob_copy(blob, ltv);
2878 blob_unref(blob);
2879 blob = ltv->vval.v_blob;
2880 }
2881
2882 // The index is for the previous byte.
2883 ++idxtv->vval.v_number;
2884 if (blob == NULL
2885 || idxtv->vval.v_number >= blob_len(blob))
2886 {
2887 // past the end of the blob, 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 Moolenaard551d6c2021-04-18 13:15:58 +02002890 }
2891 else
2892 {
2893 // Push the next byte from the blob.
2894 tv = STACK_TV_BOT(0);
2895 tv->v_type = VAR_NUMBER;
2896 tv->vval.v_number = blob_get(blob,
2897 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002898 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002899 }
2900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 else
2902 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002903 semsg(_(e_for_loop_on_str_not_supported),
2904 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002905 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 }
2907 }
2908 break;
2909
2910 // start of ":try" block
2911 case ISN_TRY:
2912 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002913 trycmd_T *trycmd = NULL;
2914
Bram Moolenaar4c137212021-04-19 16:48:48 +02002915 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002916 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002917 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
2918 + ectx->ec_trystack.ga_len;
2919 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002921 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002922 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
2923 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002924 trycmd->tcd_catch_idx =
2925 iptr->isn_arg.try.try_ref->try_catch;
2926 trycmd->tcd_finally_idx =
2927 iptr->isn_arg.try.try_ref->try_finally;
2928 trycmd->tcd_endtry_idx =
2929 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 }
2931 break;
2932
2933 case ISN_PUSHEXC:
2934 if (current_exception == NULL)
2935 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002936 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002938 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002940 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002941 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002943 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002945 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 tv->vval.v_string = vim_strsave(
2947 (char_u *)current_exception->value);
2948 break;
2949
2950 case ISN_CATCH:
2951 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002952 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953
Bram Moolenaar4c137212021-04-19 16:48:48 +02002954 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 if (trystack->ga_len > 0)
2956 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002957 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 + trystack->ga_len - 1;
2959 trycmd->tcd_caught = TRUE;
2960 }
2961 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002962 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 catch_exception(current_exception);
2964 }
2965 break;
2966
Bram Moolenaarc150c092021-02-13 15:02:46 +01002967 case ISN_TRYCONT:
2968 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002969 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002970 trycont_T *trycont = &iptr->isn_arg.trycont;
2971 int i;
2972 trycmd_T *trycmd;
2973 int iidx = trycont->tct_where;
2974
2975 if (trystack->ga_len < trycont->tct_levels)
2976 {
2977 siemsg("TRYCONT: expected %d levels, found %d",
2978 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002979 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002980 }
2981 // Make :endtry jump to any outer try block and the last
2982 // :endtry inside the loop to the loop start.
2983 for (i = trycont->tct_levels; i > 0; --i)
2984 {
2985 trycmd = ((trycmd_T *)trystack->ga_data)
2986 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002987 // Add one to tcd_cont to be able to jump to
2988 // instruction with index zero.
2989 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002990 iidx = trycmd->tcd_finally_idx == 0
2991 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002992 }
2993 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02002994 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002995 }
2996 break;
2997
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002998 case ISN_FINALLY:
2999 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003000 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003001 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3002 + trystack->ga_len - 1;
3003
3004 // Reset the index to avoid a return statement jumps here
3005 // again.
3006 trycmd->tcd_finally_idx = 0;
3007 break;
3008 }
3009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 // end of ":try" block
3011 case ISN_ENDTRY:
3012 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003013 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014
3015 if (trystack->ga_len > 0)
3016 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003017 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 --trystack->ga_len;
3020 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003021 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 trycmd = ((trycmd_T *)trystack->ga_data)
3023 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003024 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 {
3026 // discard the exception
3027 if (caught_stack == current_exception)
3028 caught_stack = caught_stack->caught;
3029 discard_current_exception();
3030 }
3031
3032 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003033 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003034
Bram Moolenaar4c137212021-04-19 16:48:48 +02003035 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003036 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003037 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003038 clear_tv(STACK_TV_BOT(0));
3039 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003040 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003041 // handling :continue: jump to outer try block or
3042 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003043 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 }
3045 }
3046 break;
3047
3048 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003049 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003050 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003051
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003052 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3053 {
3054 // throwing an exception while using "silent!" causes
3055 // the function to abort but not display an error.
3056 tv = STACK_TV_BOT(-1);
3057 clear_tv(tv);
3058 tv->v_type = VAR_NUMBER;
3059 tv->vval.v_number = 0;
3060 goto done;
3061 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003062 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003063 tv = STACK_TV_BOT(0);
3064 if (tv->vval.v_string == NULL
3065 || *skipwhite(tv->vval.v_string) == NUL)
3066 {
3067 vim_free(tv->vval.v_string);
3068 SOURCING_LNUM = iptr->isn_lnum;
3069 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003070 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003071 }
3072
3073 // Inside a "catch" we need to first discard the caught
3074 // exception.
3075 if (trystack->ga_len > 0)
3076 {
3077 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3078 + trystack->ga_len - 1;
3079 if (trycmd->tcd_caught && current_exception != NULL)
3080 {
3081 // discard the exception
3082 if (caught_stack == current_exception)
3083 caught_stack = caught_stack->caught;
3084 discard_current_exception();
3085 trycmd->tcd_caught = FALSE;
3086 }
3087 }
3088
3089 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3090 == FAIL)
3091 {
3092 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003093 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003094 }
3095 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 break;
3098
3099 // compare with special values
3100 case ISN_COMPAREBOOL:
3101 case ISN_COMPARESPECIAL:
3102 {
3103 typval_T *tv1 = STACK_TV_BOT(-2);
3104 typval_T *tv2 = STACK_TV_BOT(-1);
3105 varnumber_T arg1 = tv1->vval.v_number;
3106 varnumber_T arg2 = tv2->vval.v_number;
3107 int res;
3108
3109 switch (iptr->isn_arg.op.op_type)
3110 {
3111 case EXPR_EQUAL: res = arg1 == arg2; break;
3112 case EXPR_NEQUAL: res = arg1 != arg2; break;
3113 default: res = 0; break;
3114 }
3115
Bram Moolenaar4c137212021-04-19 16:48:48 +02003116 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 tv1->v_type = VAR_BOOL;
3118 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3119 }
3120 break;
3121
3122 // Operation with two number arguments
3123 case ISN_OPNR:
3124 case ISN_COMPARENR:
3125 {
3126 typval_T *tv1 = STACK_TV_BOT(-2);
3127 typval_T *tv2 = STACK_TV_BOT(-1);
3128 varnumber_T arg1 = tv1->vval.v_number;
3129 varnumber_T arg2 = tv2->vval.v_number;
3130 varnumber_T res;
3131
3132 switch (iptr->isn_arg.op.op_type)
3133 {
3134 case EXPR_MULT: res = arg1 * arg2; break;
3135 case EXPR_DIV: res = arg1 / arg2; break;
3136 case EXPR_REM: res = arg1 % arg2; break;
3137 case EXPR_SUB: res = arg1 - arg2; break;
3138 case EXPR_ADD: res = arg1 + arg2; break;
3139
3140 case EXPR_EQUAL: res = arg1 == arg2; break;
3141 case EXPR_NEQUAL: res = arg1 != arg2; break;
3142 case EXPR_GREATER: res = arg1 > arg2; break;
3143 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3144 case EXPR_SMALLER: res = arg1 < arg2; break;
3145 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3146 default: res = 0; break;
3147 }
3148
Bram Moolenaar4c137212021-04-19 16:48:48 +02003149 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 if (iptr->isn_type == ISN_COMPARENR)
3151 {
3152 tv1->v_type = VAR_BOOL;
3153 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3154 }
3155 else
3156 tv1->vval.v_number = res;
3157 }
3158 break;
3159
3160 // Computation with two float arguments
3161 case ISN_OPFLOAT:
3162 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003163#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 {
3165 typval_T *tv1 = STACK_TV_BOT(-2);
3166 typval_T *tv2 = STACK_TV_BOT(-1);
3167 float_T arg1 = tv1->vval.v_float;
3168 float_T arg2 = tv2->vval.v_float;
3169 float_T res = 0;
3170 int cmp = FALSE;
3171
3172 switch (iptr->isn_arg.op.op_type)
3173 {
3174 case EXPR_MULT: res = arg1 * arg2; break;
3175 case EXPR_DIV: res = arg1 / arg2; break;
3176 case EXPR_SUB: res = arg1 - arg2; break;
3177 case EXPR_ADD: res = arg1 + arg2; break;
3178
3179 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3180 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3181 case EXPR_GREATER: cmp = arg1 > arg2; break;
3182 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3183 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3184 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3185 default: cmp = 0; break;
3186 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003187 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 if (iptr->isn_type == ISN_COMPAREFLOAT)
3189 {
3190 tv1->v_type = VAR_BOOL;
3191 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3192 }
3193 else
3194 tv1->vval.v_float = res;
3195 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003196#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 break;
3198
3199 case ISN_COMPARELIST:
3200 {
3201 typval_T *tv1 = STACK_TV_BOT(-2);
3202 typval_T *tv2 = STACK_TV_BOT(-1);
3203 list_T *arg1 = tv1->vval.v_list;
3204 list_T *arg2 = tv2->vval.v_list;
3205 int cmp = FALSE;
3206 int ic = iptr->isn_arg.op.op_ic;
3207
3208 switch (iptr->isn_arg.op.op_type)
3209 {
3210 case EXPR_EQUAL: cmp =
3211 list_equal(arg1, arg2, ic, FALSE); break;
3212 case EXPR_NEQUAL: cmp =
3213 !list_equal(arg1, arg2, ic, FALSE); break;
3214 case EXPR_IS: cmp = arg1 == arg2; break;
3215 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3216 default: cmp = 0; break;
3217 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003218 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 clear_tv(tv1);
3220 clear_tv(tv2);
3221 tv1->v_type = VAR_BOOL;
3222 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3223 }
3224 break;
3225
3226 case ISN_COMPAREBLOB:
3227 {
3228 typval_T *tv1 = STACK_TV_BOT(-2);
3229 typval_T *tv2 = STACK_TV_BOT(-1);
3230 blob_T *arg1 = tv1->vval.v_blob;
3231 blob_T *arg2 = tv2->vval.v_blob;
3232 int cmp = FALSE;
3233
3234 switch (iptr->isn_arg.op.op_type)
3235 {
3236 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3237 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3238 case EXPR_IS: cmp = arg1 == arg2; break;
3239 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3240 default: cmp = 0; break;
3241 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003242 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 clear_tv(tv1);
3244 clear_tv(tv2);
3245 tv1->v_type = VAR_BOOL;
3246 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3247 }
3248 break;
3249
3250 // TODO: handle separately
3251 case ISN_COMPARESTRING:
3252 case ISN_COMPAREDICT:
3253 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 case ISN_COMPAREANY:
3255 {
3256 typval_T *tv1 = STACK_TV_BOT(-2);
3257 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003258 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 int ic = iptr->isn_arg.op.op_ic;
3260
Bram Moolenaareb26f432020-09-14 16:50:05 +02003261 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003262 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003264 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 }
3266 break;
3267
3268 case ISN_ADDLIST:
3269 case ISN_ADDBLOB:
3270 {
3271 typval_T *tv1 = STACK_TV_BOT(-2);
3272 typval_T *tv2 = STACK_TV_BOT(-1);
3273
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003274 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 if (iptr->isn_type == ISN_ADDLIST)
3276 eval_addlist(tv1, tv2);
3277 else
3278 eval_addblob(tv1, tv2);
3279 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003280 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 }
3282 break;
3283
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003284 case ISN_LISTAPPEND:
3285 {
3286 typval_T *tv1 = STACK_TV_BOT(-2);
3287 typval_T *tv2 = STACK_TV_BOT(-1);
3288 list_T *l = tv1->vval.v_list;
3289
3290 // add an item to a list
3291 if (l == NULL)
3292 {
3293 SOURCING_LNUM = iptr->isn_lnum;
3294 emsg(_(e_cannot_add_to_null_list));
3295 goto on_error;
3296 }
3297 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003298 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003299 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003300 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003301 }
3302 break;
3303
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003304 case ISN_BLOBAPPEND:
3305 {
3306 typval_T *tv1 = STACK_TV_BOT(-2);
3307 typval_T *tv2 = STACK_TV_BOT(-1);
3308 blob_T *b = tv1->vval.v_blob;
3309 int error = FALSE;
3310 varnumber_T n;
3311
3312 // add a number to a blob
3313 if (b == NULL)
3314 {
3315 SOURCING_LNUM = iptr->isn_lnum;
3316 emsg(_(e_cannot_add_to_null_blob));
3317 goto on_error;
3318 }
3319 n = tv_get_number_chk(tv2, &error);
3320 if (error)
3321 goto on_error;
3322 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003323 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003324 }
3325 break;
3326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 // Computation with two arguments of unknown type
3328 case ISN_OPANY:
3329 {
3330 typval_T *tv1 = STACK_TV_BOT(-2);
3331 typval_T *tv2 = STACK_TV_BOT(-1);
3332 varnumber_T n1, n2;
3333#ifdef FEAT_FLOAT
3334 float_T f1 = 0, f2 = 0;
3335#endif
3336 int error = FALSE;
3337
3338 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3339 {
3340 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3341 {
3342 eval_addlist(tv1, tv2);
3343 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003344 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 break;
3346 }
3347 else if (tv1->v_type == VAR_BLOB
3348 && tv2->v_type == VAR_BLOB)
3349 {
3350 eval_addblob(tv1, tv2);
3351 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003352 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 break;
3354 }
3355 }
3356#ifdef FEAT_FLOAT
3357 if (tv1->v_type == VAR_FLOAT)
3358 {
3359 f1 = tv1->vval.v_float;
3360 n1 = 0;
3361 }
3362 else
3363#endif
3364 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003365 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 n1 = tv_get_number_chk(tv1, &error);
3367 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003368 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369#ifdef FEAT_FLOAT
3370 if (tv2->v_type == VAR_FLOAT)
3371 f1 = n1;
3372#endif
3373 }
3374#ifdef FEAT_FLOAT
3375 if (tv2->v_type == VAR_FLOAT)
3376 {
3377 f2 = tv2->vval.v_float;
3378 n2 = 0;
3379 }
3380 else
3381#endif
3382 {
3383 n2 = tv_get_number_chk(tv2, &error);
3384 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003385 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386#ifdef FEAT_FLOAT
3387 if (tv1->v_type == VAR_FLOAT)
3388 f2 = n2;
3389#endif
3390 }
3391#ifdef FEAT_FLOAT
3392 // if there is a float on either side the result is a float
3393 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3394 {
3395 switch (iptr->isn_arg.op.op_type)
3396 {
3397 case EXPR_MULT: f1 = f1 * f2; break;
3398 case EXPR_DIV: f1 = f1 / f2; break;
3399 case EXPR_SUB: f1 = f1 - f2; break;
3400 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003401 default: SOURCING_LNUM = iptr->isn_lnum;
3402 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003403 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 }
3405 clear_tv(tv1);
3406 clear_tv(tv2);
3407 tv1->v_type = VAR_FLOAT;
3408 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003409 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 }
3411 else
3412#endif
3413 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003414 int failed = FALSE;
3415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 switch (iptr->isn_arg.op.op_type)
3417 {
3418 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003419 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3420 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003421 goto on_error;
3422 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 case EXPR_SUB: n1 = n1 - n2; break;
3424 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003425 default: n1 = num_modulus(n1, n2, &failed);
3426 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003427 goto on_error;
3428 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 }
3430 clear_tv(tv1);
3431 clear_tv(tv2);
3432 tv1->v_type = VAR_NUMBER;
3433 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003434 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 }
3436 }
3437 break;
3438
3439 case ISN_CONCAT:
3440 {
3441 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3442 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3443 char_u *res;
3444
3445 res = concat_str(str1, str2);
3446 clear_tv(STACK_TV_BOT(-2));
3447 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003448 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 STACK_TV_BOT(-1)->vval.v_string = res;
3450 }
3451 break;
3452
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003453 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003454 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003455 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003456 int is_slice = iptr->isn_type == ISN_STRSLICE;
3457 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003458 char_u *res;
3459
3460 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003461 // string slice: string is at stack-3, first index at
3462 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003463 if (is_slice)
3464 {
3465 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003466 n1 = tv->vval.v_number;
3467 }
3468
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003469 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003470 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003471
Bram Moolenaar4c137212021-04-19 16:48:48 +02003472 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003473 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003474 if (is_slice)
3475 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003476 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003477 else
3478 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003479 // single character (including composing characters).
3480 // If the index is too big or negative the result is
3481 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003482 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003483 vim_free(tv->vval.v_string);
3484 tv->vval.v_string = res;
3485 }
3486 break;
3487
3488 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003489 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003490 case ISN_BLOBINDEX:
3491 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003493 int is_slice = iptr->isn_type == ISN_LISTSLICE
3494 || iptr->isn_type == ISN_BLOBSLICE;
3495 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3496 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003497 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003498 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499
3500 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003501 // list slice: list is at stack-3, indexes at stack-2 and
3502 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003503 // Same for blob.
3504 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505
3506 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003507 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003509
3510 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 {
Bram Moolenaared591872020-08-15 22:14:53 +02003512 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003513 n1 = tv->vval.v_number;
3514 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 }
Bram Moolenaared591872020-08-15 22:14:53 +02003516
Bram Moolenaar4c137212021-04-19 16:48:48 +02003517 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003518 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003519 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003520 if (is_blob)
3521 {
3522 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3523 n1, n2, FALSE, tv) == FAIL)
3524 goto on_error;
3525 }
3526 else
3527 {
3528 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3529 n1, n2, FALSE, tv, TRUE) == FAIL)
3530 goto on_error;
3531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 }
3533 break;
3534
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003535 case ISN_ANYINDEX:
3536 case ISN_ANYSLICE:
3537 {
3538 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3539 typval_T *var1, *var2;
3540 int res;
3541
3542 // index: composite is at stack-2, index at stack-1
3543 // slice: composite is at stack-3, indexes at stack-2 and
3544 // stack-1
3545 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003546 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003547 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3548 goto on_error;
3549 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3550 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003551 res = eval_index_inner(tv, is_slice, var1, var2,
3552 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003553 clear_tv(var1);
3554 if (is_slice)
3555 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003556 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003557 if (res == FAIL)
3558 goto on_error;
3559 }
3560 break;
3561
Bram Moolenaar9af78762020-06-16 11:34:42 +02003562 case ISN_SLICE:
3563 {
3564 list_T *list;
3565 int count = iptr->isn_arg.number;
3566
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003567 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003568 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003569 list = tv->vval.v_list;
3570
3571 // no error for short list, expect it to be checked earlier
3572 if (list != NULL && list->lv_len >= count)
3573 {
3574 list_T *newlist = list_slice(list,
3575 count, list->lv_len - 1);
3576
3577 if (newlist != NULL)
3578 {
3579 list_unref(list);
3580 tv->vval.v_list = newlist;
3581 ++newlist->lv_refcount;
3582 }
3583 }
3584 }
3585 break;
3586
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003587 case ISN_GETITEM:
3588 {
3589 listitem_T *li;
3590 int index = iptr->isn_arg.number;
3591
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003592 // Get list item: list is at stack-1, push item.
3593 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003594 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003595 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003596
Bram Moolenaar4c137212021-04-19 16:48:48 +02003597 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003598 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003599 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003600 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003601
3602 // Useful when used in unpack assignment. Reset at
3603 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003604 ectx->ec_where.wt_index = index + 1;
3605 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003606 }
3607 break;
3608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 case ISN_MEMBER:
3610 {
3611 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003612 char_u *key;
3613 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003614 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003615
3616 // dict member: dict is at stack-2, key at stack-1
3617 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003618 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003619 dict = tv->vval.v_dict;
3620
3621 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003622 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003623 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003624 if (key == NULL)
3625 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003626
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003627 if ((di = dict_find(dict, key, -1)) == NULL)
3628 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003629 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003630 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003631
3632 // If :silent! is used we will continue, make sure the
3633 // stack contents makes sense.
3634 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003635 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003636 tv = STACK_TV_BOT(-1);
3637 clear_tv(tv);
3638 tv->v_type = VAR_NUMBER;
3639 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003640 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003641 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003642 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003643 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003644 // Clear the dict only after getting the item, to avoid
3645 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003646 tv = STACK_TV_BOT(-1);
3647 temp_tv = *tv;
3648 copy_tv(&di->di_tv, tv);
3649 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003650 }
3651 break;
3652
3653 // dict member with string key
3654 case ISN_STRINGMEMBER:
3655 {
3656 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003658 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659
3660 tv = STACK_TV_BOT(-1);
3661 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3662 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003663 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003665 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 }
3667 dict = tv->vval.v_dict;
3668
3669 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3670 == NULL)
3671 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003672 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003674 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003676 // Clear the dict after getting the item, to avoid that it
3677 // make the item invalid.
3678 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003680 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 }
3682 break;
3683
3684 case ISN_NEGATENR:
3685 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003686 if (tv->v_type != VAR_NUMBER
3687#ifdef FEAT_FLOAT
3688 && tv->v_type != VAR_FLOAT
3689#endif
3690 )
3691 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003692 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003693 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003694 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003695 }
3696#ifdef FEAT_FLOAT
3697 if (tv->v_type == VAR_FLOAT)
3698 tv->vval.v_float = -tv->vval.v_float;
3699 else
3700#endif
3701 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 break;
3703
3704 case ISN_CHECKNR:
3705 {
3706 int error = FALSE;
3707
3708 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003709 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003711 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 (void)tv_get_number_chk(tv, &error);
3713 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003714 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 }
3716 break;
3717
3718 case ISN_CHECKTYPE:
3719 {
3720 checktype_T *ct = &iptr->isn_arg.type;
3721
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003722 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003723 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003724 if (!ectx->ec_where.wt_variable)
3725 ectx->ec_where.wt_index = ct->ct_arg_idx;
3726 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3727 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003728 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003729 if (!ectx->ec_where.wt_variable)
3730 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003731
3732 // number 0 is FALSE, number 1 is TRUE
3733 if (tv->v_type == VAR_NUMBER
3734 && ct->ct_type->tt_type == VAR_BOOL
3735 && (tv->vval.v_number == 0
3736 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003738 tv->v_type = VAR_BOOL;
3739 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003740 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 }
3742 }
3743 break;
3744
Bram Moolenaar9af78762020-06-16 11:34:42 +02003745 case ISN_CHECKLEN:
3746 {
3747 int min_len = iptr->isn_arg.checklen.cl_min_len;
3748 list_T *list = NULL;
3749
3750 tv = STACK_TV_BOT(-1);
3751 if (tv->v_type == VAR_LIST)
3752 list = tv->vval.v_list;
3753 if (list == NULL || list->lv_len < min_len
3754 || (list->lv_len > min_len
3755 && !iptr->isn_arg.checklen.cl_more_OK))
3756 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003757 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003758 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003759 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003760 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003761 }
3762 }
3763 break;
3764
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003765 case ISN_SETTYPE:
3766 {
3767 checktype_T *ct = &iptr->isn_arg.type;
3768
3769 tv = STACK_TV_BOT(-1);
3770 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3771 {
3772 free_type(tv->vval.v_dict->dv_type);
3773 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3774 }
3775 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3776 {
3777 free_type(tv->vval.v_list->lv_type);
3778 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3779 }
3780 }
3781 break;
3782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003784 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 {
3786 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003787 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003789 if (iptr->isn_type == ISN_2BOOL)
3790 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003791 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003792 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003793 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003794 n = !n;
3795 }
3796 else
3797 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003798 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003799 SOURCING_LNUM = iptr->isn_lnum;
3800 n = tv_get_bool_chk(tv, &error);
3801 if (error)
3802 goto on_error;
3803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 clear_tv(tv);
3805 tv->v_type = VAR_BOOL;
3806 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3807 }
3808 break;
3809
3810 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003811 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003812 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003813 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3814 iptr->isn_type == ISN_2STRING_ANY,
3815 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003816 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 break;
3818
Bram Moolenaar08597872020-12-10 19:43:40 +01003819 case ISN_RANGE:
3820 {
3821 exarg_T ea;
3822 char *errormsg;
3823
Bram Moolenaarece0b872021-01-08 20:40:45 +01003824 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003825 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003826 ea.addr_type = ADDR_LINES;
3827 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003828 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003829 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003830 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003831
Bram Moolenaar4c137212021-04-19 16:48:48 +02003832 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003833 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003834 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003835 tv = STACK_TV_BOT(-1);
3836 tv->v_type = VAR_NUMBER;
3837 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003838 if (ea.addr_count == 0)
3839 tv->vval.v_number = curwin->w_cursor.lnum;
3840 else
3841 tv->vval.v_number = ea.line2;
3842 }
3843 break;
3844
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003845 case ISN_PUT:
3846 {
3847 int regname = iptr->isn_arg.put.put_regname;
3848 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3849 char_u *expr = NULL;
3850 int dir = FORWARD;
3851
Bram Moolenaar08597872020-12-10 19:43:40 +01003852 if (lnum < -2)
3853 {
3854 // line number was put on the stack by ISN_RANGE
3855 tv = STACK_TV_BOT(-1);
3856 curwin->w_cursor.lnum = tv->vval.v_number;
3857 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3858 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003859 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003860 }
3861 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003862 // :put! above cursor
3863 dir = BACKWARD;
3864 else if (lnum >= 0)
3865 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003866
3867 if (regname == '=')
3868 {
3869 tv = STACK_TV_BOT(-1);
3870 if (tv->v_type == VAR_STRING)
3871 expr = tv->vval.v_string;
3872 else
3873 {
3874 expr = typval2string(tv, TRUE); // allocates value
3875 clear_tv(tv);
3876 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003877 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003878 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003879 check_cursor();
3880 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3881 vim_free(expr);
3882 }
3883 break;
3884
Bram Moolenaar02194d22020-10-24 23:08:38 +02003885 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003886 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3887 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3888 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3889 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003890 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3891 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003892 break;
3893
Bram Moolenaar02194d22020-10-24 23:08:38 +02003894 case ISN_CMDMOD_REV:
3895 // filter regprog is owned by the instruction, don't free it
3896 cmdmod.cmod_filter_regmatch.regprog = NULL;
3897 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003898 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
3899 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003900 break;
3901
Bram Moolenaar792f7862020-11-23 08:31:18 +01003902 case ISN_UNPACK:
3903 {
3904 int count = iptr->isn_arg.unpack.unp_count;
3905 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3906 list_T *l;
3907 listitem_T *li;
3908 int i;
3909
3910 // Check there is a valid list to unpack.
3911 tv = STACK_TV_BOT(-1);
3912 if (tv->v_type != VAR_LIST)
3913 {
3914 SOURCING_LNUM = iptr->isn_lnum;
3915 emsg(_(e_for_argument_must_be_sequence_of_lists));
3916 goto on_error;
3917 }
3918 l = tv->vval.v_list;
3919 if (l == NULL
3920 || l->lv_len < (semicolon ? count - 1 : count))
3921 {
3922 SOURCING_LNUM = iptr->isn_lnum;
3923 emsg(_(e_list_value_does_not_have_enough_items));
3924 goto on_error;
3925 }
3926 else if (!semicolon && l->lv_len > count)
3927 {
3928 SOURCING_LNUM = iptr->isn_lnum;
3929 emsg(_(e_list_value_has_more_items_than_targets));
3930 goto on_error;
3931 }
3932
3933 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003934 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003935 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003936 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003937
3938 // Variable after semicolon gets a list with the remaining
3939 // items.
3940 if (semicolon)
3941 {
3942 list_T *rem_list =
3943 list_alloc_with_items(l->lv_len - count + 1);
3944
3945 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003946 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003947 tv = STACK_TV_BOT(-count);
3948 tv->vval.v_list = rem_list;
3949 ++rem_list->lv_refcount;
3950 tv->v_lock = 0;
3951 li = l->lv_first;
3952 for (i = 0; i < count - 1; ++i)
3953 li = li->li_next;
3954 for (i = 0; li != NULL; ++i)
3955 {
3956 list_set_item(rem_list, i, &li->li_tv);
3957 li = li->li_next;
3958 }
3959 --count;
3960 }
3961
3962 // Produce the values in reverse order, first item last.
3963 li = l->lv_first;
3964 for (i = 0; i < count; ++i)
3965 {
3966 tv = STACK_TV_BOT(-i - 1);
3967 copy_tv(&li->li_tv, tv);
3968 li = li->li_next;
3969 }
3970
3971 list_unref(l);
3972 }
3973 break;
3974
Bram Moolenaarb2049902021-01-24 12:53:53 +01003975 case ISN_PROF_START:
3976 case ISN_PROF_END:
3977 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003978#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003979 funccall_T cookie;
3980 ufunc_T *cur_ufunc =
3981 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003982 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003983
3984 cookie.func = cur_ufunc;
3985 if (iptr->isn_type == ISN_PROF_START)
3986 {
3987 func_line_start(&cookie, iptr->isn_lnum);
3988 // if we get here the instruction is executed
3989 func_line_exec(&cookie);
3990 }
3991 else
3992 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003993#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003994 }
3995 break;
3996
Bram Moolenaar389df252020-07-09 21:20:47 +02003997 case ISN_SHUFFLE:
3998 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003999 typval_T tmp_tv;
4000 int item = iptr->isn_arg.shuffle.shfl_item;
4001 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004002
4003 tmp_tv = *STACK_TV_BOT(-item);
4004 for ( ; up > 0 && item > 1; --up)
4005 {
4006 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4007 --item;
4008 }
4009 *STACK_TV_BOT(-item) = tmp_tv;
4010 }
4011 break;
4012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004014 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004016 ectx->ec_where.wt_index = 0;
4017 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 break;
4019 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004020 continue;
4021
Bram Moolenaard032f342020-07-18 18:13:02 +02004022func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004023 // Restore previous function. If the frame pointer is where we started
4024 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004025 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004026 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004027
Bram Moolenaar4c137212021-04-19 16:48:48 +02004028 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004029 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004030 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004031 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004032
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004033on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004034 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004035 // If "emsg_silent" is set then ignore the error, unless it was set
4036 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004037 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004038 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004039 {
4040 // If a sequence of instructions causes an error while ":silent!"
4041 // was used, restore the stack length and jump ahead to restoring
4042 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004043 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004044 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004045 while (ectx->ec_stack.ga_len
4046 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004047 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004048 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004049 clear_tv(STACK_TV_BOT(0));
4050 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004051 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4052 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004053 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004054 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004055 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004056on_fatal_error:
4057 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004058 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004059 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004060 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 }
4062
4063done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004064 ret = OK;
4065theend:
4066 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4067 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004068}
4069
4070/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004071 * Execute the instructions from a VAR_INSTR typeval and put the result in
4072 * "rettv".
4073 * Return OK or FAIL.
4074 */
4075 int
4076exe_typval_instr(typval_T *tv, typval_T *rettv)
4077{
4078 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4079 isn_T *save_instr = ectx->ec_instr;
4080 int save_iidx = ectx->ec_iidx;
4081 int res;
4082
4083 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4084 res = exec_instructions(ectx);
4085 if (res == OK)
4086 {
4087 *rettv = *STACK_TV_BOT(-1);
4088 --ectx->ec_stack.ga_len;
4089 }
4090
4091 ectx->ec_instr = save_instr;
4092 ectx->ec_iidx = save_iidx;
4093
4094 return res;
4095}
4096
4097/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004098 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4099 * "substitute_instr".
4100 */
4101 char_u *
4102exe_substitute_instr(void)
4103{
4104 ectx_T *ectx = substitute_instr->subs_ectx;
4105 isn_T *save_instr = ectx->ec_instr;
4106 int save_iidx = ectx->ec_iidx;
4107 char_u *res;
4108
4109 ectx->ec_instr = substitute_instr->subs_instr;
4110 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004111 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004112 typval_T *tv = STACK_TV_BOT(-1);
4113
4114 res = vim_strsave(tv_get_string(tv));
4115 --ectx->ec_stack.ga_len;
4116 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004117 }
4118 else
4119 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004120 substitute_instr->subs_status = FAIL;
4121 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123
Bram Moolenaar4c137212021-04-19 16:48:48 +02004124 ectx->ec_instr = save_instr;
4125 ectx->ec_iidx = save_iidx;
4126
4127 return res;
4128}
4129
4130/*
4131 * Call a "def" function from old Vim script.
4132 * Return OK or FAIL.
4133 */
4134 int
4135call_def_function(
4136 ufunc_T *ufunc,
4137 int argc_arg, // nr of arguments
4138 typval_T *argv, // arguments
4139 partial_T *partial, // optional partial for context
4140 typval_T *rettv) // return value
4141{
4142 ectx_T ectx; // execution context
4143 int argc = argc_arg;
4144 typval_T *tv;
4145 int idx;
4146 int ret = FAIL;
4147 int defcount = ufunc->uf_args.ga_len - argc;
4148 sctx_T save_current_sctx = current_sctx;
4149 int did_emsg_before = did_emsg_cumul + did_emsg;
4150 int save_suppress_errthrow = suppress_errthrow;
4151 msglist_T **saved_msg_list = NULL;
4152 msglist_T *private_msg_list = NULL;
4153 int save_emsg_silent_def = emsg_silent_def;
4154 int save_did_emsg_def = did_emsg_def;
4155 int orig_funcdepth;
4156
4157// Get pointer to item in the stack.
4158#undef STACK_TV
4159#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4160
4161// Get pointer to item at the bottom of the stack, -1 is the bottom.
4162#undef STACK_TV_BOT
4163#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4164
4165// Get pointer to a local variable on the stack. Negative for arguments.
4166#undef STACK_TV_VAR
4167#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4168
4169 if (ufunc->uf_def_status == UF_NOT_COMPILED
4170 || ufunc->uf_def_status == UF_COMPILE_ERROR
4171 || (func_needs_compiling(ufunc, PROFILING(ufunc))
4172 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
4173 == FAIL))
4174 {
4175 if (did_emsg_cumul + did_emsg == did_emsg_before)
4176 semsg(_(e_function_is_not_compiled_str),
4177 printable_func_name(ufunc));
4178 return FAIL;
4179 }
4180
4181 {
4182 // Check the function was really compiled.
4183 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4184 + ufunc->uf_dfunc_idx;
4185 if (INSTRUCTIONS(dfunc) == NULL)
4186 {
4187 iemsg("using call_def_function() on not compiled function");
4188 return FAIL;
4189 }
4190 }
4191
4192 // If depth of calling is getting too high, don't execute the function.
4193 orig_funcdepth = funcdepth_get();
4194 if (funcdepth_increment() == FAIL)
4195 return FAIL;
4196
4197 CLEAR_FIELD(ectx);
4198 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4199 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4200 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4201 {
4202 funcdepth_decrement();
4203 return FAIL;
4204 }
4205 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4206 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4207 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004208
4209 idx = argc - ufunc->uf_args.ga_len;
4210 if (idx > 0 && ufunc->uf_va_name == NULL)
4211 {
4212 if (idx == 1)
4213 emsg(_(e_one_argument_too_many));
4214 else
4215 semsg(_(e_nr_arguments_too_many), idx);
4216 goto failed_early;
4217 }
4218
4219 // Put arguments on the stack, but no more than what the function expects.
4220 // A lambda can be called with more arguments than it uses.
4221 for (idx = 0; idx < argc
4222 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4223 ++idx)
4224 {
4225 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4226 && argv[idx].v_type == VAR_SPECIAL
4227 && argv[idx].vval.v_number == VVAL_NONE)
4228 {
4229 // Use the default value.
4230 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4231 }
4232 else
4233 {
4234 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4235 && check_typval_arg_type(
4236 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4237 goto failed_early;
4238 copy_tv(&argv[idx], STACK_TV_BOT(0));
4239 }
4240 ++ectx.ec_stack.ga_len;
4241 }
4242
4243 // Turn varargs into a list. Empty list if no args.
4244 if (ufunc->uf_va_name != NULL)
4245 {
4246 int vararg_count = argc - ufunc->uf_args.ga_len;
4247
4248 if (vararg_count < 0)
4249 vararg_count = 0;
4250 else
4251 argc -= vararg_count;
4252 if (exe_newlist(vararg_count, &ectx) == FAIL)
4253 goto failed_early;
4254
4255 // Check the type of the list items.
4256 tv = STACK_TV_BOT(-1);
4257 if (ufunc->uf_va_type != NULL
4258 && ufunc->uf_va_type != &t_list_any
4259 && ufunc->uf_va_type->tt_member != &t_any
4260 && tv->vval.v_list != NULL)
4261 {
4262 type_T *expected = ufunc->uf_va_type->tt_member;
4263 listitem_T *li = tv->vval.v_list->lv_first;
4264
4265 for (idx = 0; idx < vararg_count; ++idx)
4266 {
4267 if (check_typval_arg_type(expected, &li->li_tv,
4268 argc + idx + 1) == FAIL)
4269 goto failed_early;
4270 li = li->li_next;
4271 }
4272 }
4273
4274 if (defcount > 0)
4275 // Move varargs list to below missing default arguments.
4276 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4277 --ectx.ec_stack.ga_len;
4278 }
4279
4280 // Make space for omitted arguments, will store default value below.
4281 // Any varargs list goes after them.
4282 if (defcount > 0)
4283 for (idx = 0; idx < defcount; ++idx)
4284 {
4285 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4286 ++ectx.ec_stack.ga_len;
4287 }
4288 if (ufunc->uf_va_name != NULL)
4289 ++ectx.ec_stack.ga_len;
4290
4291 // Frame pointer points to just after arguments.
4292 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4293 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4294
4295 {
4296 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4297 + ufunc->uf_dfunc_idx;
4298 ufunc_T *base_ufunc = dfunc->df_ufunc;
4299
4300 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4301 // by copy_func().
4302 if (partial != NULL || base_ufunc->uf_partial != NULL)
4303 {
4304 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
4305 if (ectx.ec_outer == NULL)
4306 goto failed_early;
4307 if (partial != NULL)
4308 {
4309 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4310 {
4311 if (current_ectx->ec_outer != NULL)
4312 *ectx.ec_outer = *current_ectx->ec_outer;
4313 }
4314 else
4315 *ectx.ec_outer = partial->pt_outer;
4316 }
4317 else
4318 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
4319 ectx.ec_outer->out_up_is_copy = TRUE;
4320 }
4321 }
4322
4323 // dummy frame entries
4324 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4325 {
4326 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4327 ++ectx.ec_stack.ga_len;
4328 }
4329
4330 {
4331 // Reserve space for local variables and any closure reference count.
4332 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4333 + ufunc->uf_dfunc_idx;
4334
4335 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4336 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4337 ectx.ec_stack.ga_len += dfunc->df_varcount;
4338 if (dfunc->df_has_closure)
4339 {
4340 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4341 STACK_TV_VAR(idx)->vval.v_number = 0;
4342 ++ectx.ec_stack.ga_len;
4343 }
4344
4345 ectx.ec_instr = INSTRUCTIONS(dfunc);
4346 }
4347
4348 // Following errors are in the function, not the caller.
4349 // Commands behave like vim9script.
4350 estack_push_ufunc(ufunc, 1);
4351 current_sctx = ufunc->uf_script_ctx;
4352 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4353
4354 // Use a specific location for storing error messages to be converted to an
4355 // exception.
4356 saved_msg_list = msg_list;
4357 msg_list = &private_msg_list;
4358
4359 // Do turn errors into exceptions.
4360 suppress_errthrow = FALSE;
4361
4362 // Do not delete the function while executing it.
4363 ++ufunc->uf_calls;
4364
4365 // When ":silent!" was used before calling then we still abort the
4366 // function. If ":silent!" is used in the function then we don't.
4367 emsg_silent_def = emsg_silent;
4368 did_emsg_def = 0;
4369
4370 ectx.ec_where.wt_index = 0;
4371 ectx.ec_where.wt_variable = FALSE;
4372
4373 // Execute the instructions until done.
4374 ret = exec_instructions(&ectx);
4375 if (ret == OK)
4376 {
4377 // function finished, get result from the stack.
4378 if (ufunc->uf_ret_type == &t_void)
4379 {
4380 rettv->v_type = VAR_VOID;
4381 }
4382 else
4383 {
4384 tv = STACK_TV_BOT(-1);
4385 *rettv = *tv;
4386 tv->v_type = VAR_UNKNOWN;
4387 }
4388 }
4389
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004390 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004391 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4392 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004393
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004394 // Deal with any remaining closures, they may be in use somewhere.
4395 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004396 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004397 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004398 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4399 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004400
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004401 estack_pop();
4402 current_sctx = save_current_sctx;
4403
Bram Moolenaarc970e422021-03-17 15:03:04 +01004404 // TODO: when is it safe to delete the function if it is no longer used?
4405 --ufunc->uf_calls;
4406
Bram Moolenaar352134b2020-10-17 22:04:08 +02004407 if (*msg_list != NULL && saved_msg_list != NULL)
4408 {
4409 msglist_T **plist = saved_msg_list;
4410
4411 // Append entries from the current msg_list (uncaught exceptions) to
4412 // the saved msg_list.
4413 while (*plist != NULL)
4414 plist = &(*plist)->next;
4415
4416 *plist = *msg_list;
4417 }
4418 msg_list = saved_msg_list;
4419
Bram Moolenaar4c137212021-04-19 16:48:48 +02004420 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004421 {
4422 cmdmod.cmod_filter_regmatch.regprog = NULL;
4423 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004424 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004425 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004426 emsg_silent_def = save_emsg_silent_def;
4427 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004428
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004429failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004430 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4432 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004435 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004436
Bram Moolenaar0186e582021-01-10 18:33:11 +01004437 while (ectx.ec_outer != NULL)
4438 {
4439 outer_T *up = ectx.ec_outer->out_up_is_copy
4440 ? NULL : ectx.ec_outer->out_up;
4441
4442 vim_free(ectx.ec_outer);
4443 ectx.ec_outer = up;
4444 }
4445
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004446 // Not sure if this is necessary.
4447 suppress_errthrow = save_suppress_errthrow;
4448
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004449 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004450 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004451 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004452 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 return ret;
4454}
4455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004457 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4458 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4459 * "pfx" is prefixed to every line.
4460 */
4461 static void
4462list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4463{
4464 int line_idx = 0;
4465 int prev_current = 0;
4466 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004467 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004468
4469 for (current = 0; current < instr_count; ++current)
4470 {
4471 isn_T *iptr = &instr[current];
4472 char *line;
4473
4474 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004475 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004476 while (line_idx < iptr->isn_lnum
4477 && line_idx < ufunc->uf_lines.ga_len)
4478 {
4479 if (current > prev_current)
4480 {
4481 msg_puts("\n\n");
4482 prev_current = current;
4483 }
4484 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4485 if (line != NULL)
4486 msg(line);
4487 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004488 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4489 {
4490 int first_def_arg = ufunc->uf_args.ga_len
4491 - ufunc->uf_def_args.ga_len;
4492
4493 if (def_arg_idx > 0)
4494 msg_puts("\n\n");
4495 msg_start();
4496 msg_puts(" ");
4497 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4498 first_def_arg + def_arg_idx]);
4499 msg_puts(" = ");
4500 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4501 msg_clr_eos();
4502 msg_end();
4503 }
4504 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004505
4506 switch (iptr->isn_type)
4507 {
4508 case ISN_EXEC:
4509 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4510 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004511 case ISN_LEGACY_EVAL:
4512 smsg("%s%4d EVAL legacy %s", pfx, current,
4513 iptr->isn_arg.string);
4514 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004515 case ISN_REDIRSTART:
4516 smsg("%s%4d REDIR", pfx, current);
4517 break;
4518 case ISN_REDIREND:
4519 smsg("%s%4d REDIR END%s", pfx, current,
4520 iptr->isn_arg.number ? " append" : "");
4521 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004522 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004523#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004524 smsg("%s%4d CEXPR pre %s", pfx, current,
4525 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004526#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004527 break;
4528 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004529#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004530 {
4531 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4532
4533 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4534 cexpr_get_auname(cer->cer_cmdidx),
4535 cer->cer_forceit ? "!" : "",
4536 cer->cer_cmdline);
4537 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004538#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004539 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004540 case ISN_INSTR:
4541 {
4542 smsg("%s%4d INSTR", pfx, current);
4543 list_instructions(" ", iptr->isn_arg.instr,
4544 INT_MAX, NULL);
4545 msg(" -------------");
4546 }
4547 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004548 case ISN_SUBSTITUTE:
4549 {
4550 subs_T *subs = &iptr->isn_arg.subs;
4551
4552 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4553 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4554 msg(" -------------");
4555 }
4556 break;
4557 case ISN_EXECCONCAT:
4558 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4559 (varnumber_T)iptr->isn_arg.number);
4560 break;
4561 case ISN_ECHO:
4562 {
4563 echo_T *echo = &iptr->isn_arg.echo;
4564
4565 smsg("%s%4d %s %d", pfx, current,
4566 echo->echo_with_white ? "ECHO" : "ECHON",
4567 echo->echo_count);
4568 }
4569 break;
4570 case ISN_EXECUTE:
4571 smsg("%s%4d EXECUTE %lld", pfx, current,
4572 (varnumber_T)(iptr->isn_arg.number));
4573 break;
4574 case ISN_ECHOMSG:
4575 smsg("%s%4d ECHOMSG %lld", pfx, current,
4576 (varnumber_T)(iptr->isn_arg.number));
4577 break;
4578 case ISN_ECHOERR:
4579 smsg("%s%4d ECHOERR %lld", pfx, current,
4580 (varnumber_T)(iptr->isn_arg.number));
4581 break;
4582 case ISN_LOAD:
4583 {
4584 if (iptr->isn_arg.number < 0)
4585 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4586 (varnumber_T)(iptr->isn_arg.number
4587 + STACK_FRAME_SIZE));
4588 else
4589 smsg("%s%4d LOAD $%lld", pfx, current,
4590 (varnumber_T)(iptr->isn_arg.number));
4591 }
4592 break;
4593 case ISN_LOADOUTER:
4594 {
4595 if (iptr->isn_arg.number < 0)
4596 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4597 iptr->isn_arg.outer.outer_depth,
4598 iptr->isn_arg.outer.outer_idx
4599 + STACK_FRAME_SIZE);
4600 else
4601 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4602 iptr->isn_arg.outer.outer_depth,
4603 iptr->isn_arg.outer.outer_idx);
4604 }
4605 break;
4606 case ISN_LOADV:
4607 smsg("%s%4d LOADV v:%s", pfx, current,
4608 get_vim_var_name(iptr->isn_arg.number));
4609 break;
4610 case ISN_LOADSCRIPT:
4611 {
4612 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4613 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4614 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4615 + sref->sref_idx;
4616
4617 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4618 sv->sv_name,
4619 sref->sref_idx,
4620 si->sn_name);
4621 }
4622 break;
4623 case ISN_LOADS:
4624 {
4625 scriptitem_T *si = SCRIPT_ITEM(
4626 iptr->isn_arg.loadstore.ls_sid);
4627
4628 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4629 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4630 }
4631 break;
4632 case ISN_LOADAUTO:
4633 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4634 break;
4635 case ISN_LOADG:
4636 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4637 break;
4638 case ISN_LOADB:
4639 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4640 break;
4641 case ISN_LOADW:
4642 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4643 break;
4644 case ISN_LOADT:
4645 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4646 break;
4647 case ISN_LOADGDICT:
4648 smsg("%s%4d LOAD g:", pfx, current);
4649 break;
4650 case ISN_LOADBDICT:
4651 smsg("%s%4d LOAD b:", pfx, current);
4652 break;
4653 case ISN_LOADWDICT:
4654 smsg("%s%4d LOAD w:", pfx, current);
4655 break;
4656 case ISN_LOADTDICT:
4657 smsg("%s%4d LOAD t:", pfx, current);
4658 break;
4659 case ISN_LOADOPT:
4660 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4661 break;
4662 case ISN_LOADENV:
4663 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4664 break;
4665 case ISN_LOADREG:
4666 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4667 break;
4668
4669 case ISN_STORE:
4670 if (iptr->isn_arg.number < 0)
4671 smsg("%s%4d STORE arg[%lld]", pfx, current,
4672 iptr->isn_arg.number + STACK_FRAME_SIZE);
4673 else
4674 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4675 break;
4676 case ISN_STOREOUTER:
4677 {
4678 if (iptr->isn_arg.number < 0)
4679 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4680 iptr->isn_arg.outer.outer_depth,
4681 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4682 else
4683 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4684 iptr->isn_arg.outer.outer_depth,
4685 iptr->isn_arg.outer.outer_idx);
4686 }
4687 break;
4688 case ISN_STOREV:
4689 smsg("%s%4d STOREV v:%s", pfx, current,
4690 get_vim_var_name(iptr->isn_arg.number));
4691 break;
4692 case ISN_STOREAUTO:
4693 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4694 break;
4695 case ISN_STOREG:
4696 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4697 break;
4698 case ISN_STOREB:
4699 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4700 break;
4701 case ISN_STOREW:
4702 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4703 break;
4704 case ISN_STORET:
4705 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4706 break;
4707 case ISN_STORES:
4708 {
4709 scriptitem_T *si = SCRIPT_ITEM(
4710 iptr->isn_arg.loadstore.ls_sid);
4711
4712 smsg("%s%4d STORES %s in %s", pfx, current,
4713 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4714 }
4715 break;
4716 case ISN_STORESCRIPT:
4717 {
4718 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4719 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4720 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4721 + sref->sref_idx;
4722
4723 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4724 sv->sv_name,
4725 sref->sref_idx,
4726 si->sn_name);
4727 }
4728 break;
4729 case ISN_STOREOPT:
4730 smsg("%s%4d STOREOPT &%s", pfx, current,
4731 iptr->isn_arg.storeopt.so_name);
4732 break;
4733 case ISN_STOREENV:
4734 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4735 break;
4736 case ISN_STOREREG:
4737 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4738 break;
4739 case ISN_STORENR:
4740 smsg("%s%4d STORE %lld in $%d", pfx, current,
4741 iptr->isn_arg.storenr.stnr_val,
4742 iptr->isn_arg.storenr.stnr_idx);
4743 break;
4744
4745 case ISN_STOREINDEX:
4746 smsg("%s%4d STOREINDEX %s", pfx, current,
4747 vartype_name(iptr->isn_arg.vartype));
4748 break;
4749
4750 case ISN_STORERANGE:
4751 smsg("%s%4d STORERANGE", pfx, current);
4752 break;
4753
4754 // constants
4755 case ISN_PUSHNR:
4756 smsg("%s%4d PUSHNR %lld", pfx, current,
4757 (varnumber_T)(iptr->isn_arg.number));
4758 break;
4759 case ISN_PUSHBOOL:
4760 case ISN_PUSHSPEC:
4761 smsg("%s%4d PUSH %s", pfx, current,
4762 get_var_special_name(iptr->isn_arg.number));
4763 break;
4764 case ISN_PUSHF:
4765#ifdef FEAT_FLOAT
4766 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4767#endif
4768 break;
4769 case ISN_PUSHS:
4770 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4771 break;
4772 case ISN_PUSHBLOB:
4773 {
4774 char_u *r;
4775 char_u numbuf[NUMBUFLEN];
4776 char_u *tofree;
4777
4778 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4779 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4780 vim_free(tofree);
4781 }
4782 break;
4783 case ISN_PUSHFUNC:
4784 {
4785 char *name = (char *)iptr->isn_arg.string;
4786
4787 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4788 name == NULL ? "[none]" : name);
4789 }
4790 break;
4791 case ISN_PUSHCHANNEL:
4792#ifdef FEAT_JOB_CHANNEL
4793 {
4794 channel_T *channel = iptr->isn_arg.channel;
4795
4796 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4797 channel == NULL ? 0 : channel->ch_id);
4798 }
4799#endif
4800 break;
4801 case ISN_PUSHJOB:
4802#ifdef FEAT_JOB_CHANNEL
4803 {
4804 typval_T tv;
4805 char_u *name;
4806
4807 tv.v_type = VAR_JOB;
4808 tv.vval.v_job = iptr->isn_arg.job;
4809 name = tv_get_string(&tv);
4810 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4811 }
4812#endif
4813 break;
4814 case ISN_PUSHEXC:
4815 smsg("%s%4d PUSH v:exception", pfx, current);
4816 break;
4817 case ISN_UNLET:
4818 smsg("%s%4d UNLET%s %s", pfx, current,
4819 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4820 iptr->isn_arg.unlet.ul_name);
4821 break;
4822 case ISN_UNLETENV:
4823 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4824 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4825 iptr->isn_arg.unlet.ul_name);
4826 break;
4827 case ISN_UNLETINDEX:
4828 smsg("%s%4d UNLETINDEX", pfx, current);
4829 break;
4830 case ISN_UNLETRANGE:
4831 smsg("%s%4d UNLETRANGE", pfx, current);
4832 break;
4833 case ISN_LOCKCONST:
4834 smsg("%s%4d LOCKCONST", pfx, current);
4835 break;
4836 case ISN_NEWLIST:
4837 smsg("%s%4d NEWLIST size %lld", pfx, current,
4838 (varnumber_T)(iptr->isn_arg.number));
4839 break;
4840 case ISN_NEWDICT:
4841 smsg("%s%4d NEWDICT size %lld", pfx, current,
4842 (varnumber_T)(iptr->isn_arg.number));
4843 break;
4844
4845 // function call
4846 case ISN_BCALL:
4847 {
4848 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4849
4850 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4851 internal_func_name(cbfunc->cbf_idx),
4852 cbfunc->cbf_argcount);
4853 }
4854 break;
4855 case ISN_DCALL:
4856 {
4857 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4858 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4859 + cdfunc->cdf_idx;
4860
4861 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
4862 df->df_ufunc->uf_name_exp != NULL
4863 ? df->df_ufunc->uf_name_exp
4864 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4865 }
4866 break;
4867 case ISN_UCALL:
4868 {
4869 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4870
4871 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
4872 cufunc->cuf_name, cufunc->cuf_argcount);
4873 }
4874 break;
4875 case ISN_PCALL:
4876 {
4877 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4878
4879 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
4880 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4881 }
4882 break;
4883 case ISN_PCALL_END:
4884 smsg("%s%4d PCALL end", pfx, current);
4885 break;
4886 case ISN_RETURN:
4887 smsg("%s%4d RETURN", pfx, current);
4888 break;
4889 case ISN_RETURN_ZERO:
4890 smsg("%s%4d RETURN 0", pfx, current);
4891 break;
4892 case ISN_FUNCREF:
4893 {
4894 funcref_T *funcref = &iptr->isn_arg.funcref;
4895 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4896 + funcref->fr_func;
4897
4898 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
4899 }
4900 break;
4901
4902 case ISN_NEWFUNC:
4903 {
4904 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4905
4906 smsg("%s%4d NEWFUNC %s %s", pfx, current,
4907 newfunc->nf_lambda, newfunc->nf_global);
4908 }
4909 break;
4910
4911 case ISN_DEF:
4912 {
4913 char_u *name = iptr->isn_arg.string;
4914
4915 smsg("%s%4d DEF %s", pfx, current,
4916 name == NULL ? (char_u *)"" : name);
4917 }
4918 break;
4919
4920 case ISN_JUMP:
4921 {
4922 char *when = "?";
4923
4924 switch (iptr->isn_arg.jump.jump_when)
4925 {
4926 case JUMP_ALWAYS:
4927 when = "JUMP";
4928 break;
4929 case JUMP_AND_KEEP_IF_TRUE:
4930 when = "JUMP_AND_KEEP_IF_TRUE";
4931 break;
4932 case JUMP_IF_FALSE:
4933 when = "JUMP_IF_FALSE";
4934 break;
4935 case JUMP_AND_KEEP_IF_FALSE:
4936 when = "JUMP_AND_KEEP_IF_FALSE";
4937 break;
4938 case JUMP_IF_COND_FALSE:
4939 when = "JUMP_IF_COND_FALSE";
4940 break;
4941 case JUMP_IF_COND_TRUE:
4942 when = "JUMP_IF_COND_TRUE";
4943 break;
4944 }
4945 smsg("%s%4d %s -> %d", pfx, current, when,
4946 iptr->isn_arg.jump.jump_where);
4947 }
4948 break;
4949
4950 case ISN_JUMP_IF_ARG_SET:
4951 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
4952 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4953 iptr->isn_arg.jump.jump_where);
4954 break;
4955
4956 case ISN_FOR:
4957 {
4958 forloop_T *forloop = &iptr->isn_arg.forloop;
4959
4960 smsg("%s%4d FOR $%d -> %d", pfx, current,
4961 forloop->for_idx, forloop->for_end);
4962 }
4963 break;
4964
4965 case ISN_TRY:
4966 {
4967 try_T *try = &iptr->isn_arg.try;
4968
4969 if (try->try_ref->try_finally == 0)
4970 smsg("%s%4d TRY catch -> %d, endtry -> %d",
4971 pfx, current,
4972 try->try_ref->try_catch,
4973 try->try_ref->try_endtry);
4974 else
4975 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4976 pfx, current,
4977 try->try_ref->try_catch,
4978 try->try_ref->try_finally,
4979 try->try_ref->try_endtry);
4980 }
4981 break;
4982 case ISN_CATCH:
4983 // TODO
4984 smsg("%s%4d CATCH", pfx, current);
4985 break;
4986 case ISN_TRYCONT:
4987 {
4988 trycont_T *trycont = &iptr->isn_arg.trycont;
4989
4990 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
4991 trycont->tct_levels,
4992 trycont->tct_levels == 1 ? "" : "s",
4993 trycont->tct_where);
4994 }
4995 break;
4996 case ISN_FINALLY:
4997 smsg("%s%4d FINALLY", pfx, current);
4998 break;
4999 case ISN_ENDTRY:
5000 smsg("%s%4d ENDTRY", pfx, current);
5001 break;
5002 case ISN_THROW:
5003 smsg("%s%4d THROW", pfx, current);
5004 break;
5005
5006 // expression operations on number
5007 case ISN_OPNR:
5008 case ISN_OPFLOAT:
5009 case ISN_OPANY:
5010 {
5011 char *what;
5012 char *ins;
5013
5014 switch (iptr->isn_arg.op.op_type)
5015 {
5016 case EXPR_MULT: what = "*"; break;
5017 case EXPR_DIV: what = "/"; break;
5018 case EXPR_REM: what = "%"; break;
5019 case EXPR_SUB: what = "-"; break;
5020 case EXPR_ADD: what = "+"; break;
5021 default: what = "???"; break;
5022 }
5023 switch (iptr->isn_type)
5024 {
5025 case ISN_OPNR: ins = "OPNR"; break;
5026 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5027 case ISN_OPANY: ins = "OPANY"; break;
5028 default: ins = "???"; break;
5029 }
5030 smsg("%s%4d %s %s", pfx, current, ins, what);
5031 }
5032 break;
5033
5034 case ISN_COMPAREBOOL:
5035 case ISN_COMPARESPECIAL:
5036 case ISN_COMPARENR:
5037 case ISN_COMPAREFLOAT:
5038 case ISN_COMPARESTRING:
5039 case ISN_COMPAREBLOB:
5040 case ISN_COMPARELIST:
5041 case ISN_COMPAREDICT:
5042 case ISN_COMPAREFUNC:
5043 case ISN_COMPAREANY:
5044 {
5045 char *p;
5046 char buf[10];
5047 char *type;
5048
5049 switch (iptr->isn_arg.op.op_type)
5050 {
5051 case EXPR_EQUAL: p = "=="; break;
5052 case EXPR_NEQUAL: p = "!="; break;
5053 case EXPR_GREATER: p = ">"; break;
5054 case EXPR_GEQUAL: p = ">="; break;
5055 case EXPR_SMALLER: p = "<"; break;
5056 case EXPR_SEQUAL: p = "<="; break;
5057 case EXPR_MATCH: p = "=~"; break;
5058 case EXPR_IS: p = "is"; break;
5059 case EXPR_ISNOT: p = "isnot"; break;
5060 case EXPR_NOMATCH: p = "!~"; break;
5061 default: p = "???"; break;
5062 }
5063 STRCPY(buf, p);
5064 if (iptr->isn_arg.op.op_ic == TRUE)
5065 strcat(buf, "?");
5066 switch(iptr->isn_type)
5067 {
5068 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5069 case ISN_COMPARESPECIAL:
5070 type = "COMPARESPECIAL"; break;
5071 case ISN_COMPARENR: type = "COMPARENR"; break;
5072 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5073 case ISN_COMPARESTRING:
5074 type = "COMPARESTRING"; break;
5075 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5076 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5077 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5078 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5079 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5080 default: type = "???"; break;
5081 }
5082
5083 smsg("%s%4d %s %s", pfx, current, type, buf);
5084 }
5085 break;
5086
5087 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5088 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5089
5090 // expression operations
5091 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5092 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5093 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5094 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5095 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5096 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5097 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5098 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5099 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5100 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5101 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5102 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5103 pfx, current, iptr->isn_arg.number); break;
5104 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5105 pfx, current, iptr->isn_arg.number); break;
5106 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5107 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5108 iptr->isn_arg.string); break;
5109 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5110
5111 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5112 case ISN_CHECKTYPE:
5113 {
5114 checktype_T *ct = &iptr->isn_arg.type;
5115 char *tofree;
5116
5117 if (ct->ct_arg_idx == 0)
5118 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5119 type_name(ct->ct_type, &tofree),
5120 (int)ct->ct_off);
5121 else
5122 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5123 type_name(ct->ct_type, &tofree),
5124 (int)ct->ct_off,
5125 (int)ct->ct_arg_idx);
5126 vim_free(tofree);
5127 break;
5128 }
5129 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5130 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5131 iptr->isn_arg.checklen.cl_min_len);
5132 break;
5133 case ISN_SETTYPE:
5134 {
5135 char *tofree;
5136
5137 smsg("%s%4d SETTYPE %s", pfx, current,
5138 type_name(iptr->isn_arg.type.ct_type, &tofree));
5139 vim_free(tofree);
5140 break;
5141 }
5142 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005143 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5144 smsg("%s%4d INVERT %d (!val)", pfx, current,
5145 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005146 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005147 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5148 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005149 break;
5150 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005151 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005152 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005153 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5154 pfx, current,
5155 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005156 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005157 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5158 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005159 break;
5160 case ISN_PUT:
5161 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5162 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005163 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005164 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5165 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005166 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005167 else
5168 smsg("%s%4d PUT %c %ld", pfx, current,
5169 iptr->isn_arg.put.put_regname,
5170 (long)iptr->isn_arg.put.put_lnum);
5171 break;
5172
5173 // TODO: summarize modifiers
5174 case ISN_CMDMOD:
5175 {
5176 char_u *buf;
5177 size_t len = produce_cmdmods(
5178 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5179
5180 buf = alloc(len + 1);
5181 if (buf != NULL)
5182 {
5183 (void)produce_cmdmods(
5184 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5185 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5186 vim_free(buf);
5187 }
5188 break;
5189 }
5190 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5191
5192 case ISN_PROF_START:
5193 smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum);
5194 break;
5195
5196 case ISN_PROF_END:
5197 smsg("%s%4d PROFILE END", pfx, current);
5198 break;
5199
5200 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5201 iptr->isn_arg.unpack.unp_count,
5202 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5203 break;
5204 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5205 iptr->isn_arg.shuffle.shfl_item,
5206 iptr->isn_arg.shuffle.shfl_up);
5207 break;
5208 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5209
5210 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5211 return;
5212 }
5213
5214 out_flush(); // output one line at a time
5215 ui_breakcheck();
5216 if (got_int)
5217 break;
5218 }
5219}
5220
5221/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005222 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005223 * We don't really need this at runtime, but we do have tests that require it,
5224 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 */
5226 void
5227ex_disassemble(exarg_T *eap)
5228{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005229 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005230 char_u *fname;
5231 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232 dfunc_T *dfunc;
5233 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005234 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005235 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005237 if (STRNCMP(arg, "<lambda>", 8) == 0)
5238 {
5239 arg += 8;
5240 (void)getdigits(&arg);
5241 fname = vim_strnsave(eap->arg, arg - eap->arg);
5242 }
5243 else
5244 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005245 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005246 if (fname == NULL)
5247 {
5248 semsg(_(e_invarg2), eap->arg);
5249 return;
5250 }
5251
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005252 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005253 if (ufunc == NULL)
5254 {
5255 char_u *p = untrans_function_name(fname);
5256
5257 if (p != NULL)
5258 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005259 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005260 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005261 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005262 if (ufunc == NULL)
5263 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005264 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 return;
5266 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005267 if (func_needs_compiling(ufunc, eap->forceit)
5268 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005269 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005270 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005272 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273 return;
5274 }
5275 if (ufunc->uf_name_exp != NULL)
5276 msg((char *)ufunc->uf_name_exp);
5277 else
5278 msg((char *)ufunc->uf_name);
5279
5280 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005281#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005282 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
5283 instr_count = eap->forceit ? dfunc->df_instr_prof_count
5284 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005285#else
5286 instr = dfunc->df_instr;
5287 instr_count = dfunc->df_instr_count;
5288#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289
Bram Moolenaar4c137212021-04-19 16:48:48 +02005290 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291}
5292
5293/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005294 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 * list, etc. Mostly like what JavaScript does, except that empty list and
5296 * empty dictionary are FALSE.
5297 */
5298 int
5299tv2bool(typval_T *tv)
5300{
5301 switch (tv->v_type)
5302 {
5303 case VAR_NUMBER:
5304 return tv->vval.v_number != 0;
5305 case VAR_FLOAT:
5306#ifdef FEAT_FLOAT
5307 return tv->vval.v_float != 0.0;
5308#else
5309 break;
5310#endif
5311 case VAR_PARTIAL:
5312 return tv->vval.v_partial != NULL;
5313 case VAR_FUNC:
5314 case VAR_STRING:
5315 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5316 case VAR_LIST:
5317 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5318 case VAR_DICT:
5319 return tv->vval.v_dict != NULL
5320 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5321 case VAR_BOOL:
5322 case VAR_SPECIAL:
5323 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5324 case VAR_JOB:
5325#ifdef FEAT_JOB_CHANNEL
5326 return tv->vval.v_job != NULL;
5327#else
5328 break;
5329#endif
5330 case VAR_CHANNEL:
5331#ifdef FEAT_JOB_CHANNEL
5332 return tv->vval.v_channel != NULL;
5333#else
5334 break;
5335#endif
5336 case VAR_BLOB:
5337 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5338 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005339 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005341 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 break;
5343 }
5344 return FALSE;
5345}
5346
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005347 void
5348emsg_using_string_as(typval_T *tv, int as_number)
5349{
5350 semsg(_(as_number ? e_using_string_as_number_str
5351 : e_using_string_as_bool_str),
5352 tv->vval.v_string == NULL
5353 ? (char_u *)"" : tv->vval.v_string);
5354}
5355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005356/*
5357 * If "tv" is a string give an error and return FAIL.
5358 */
5359 int
5360check_not_string(typval_T *tv)
5361{
5362 if (tv->v_type == VAR_STRING)
5363 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005364 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 clear_tv(tv);
5366 return FAIL;
5367 }
5368 return OK;
5369}
5370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371
5372#endif // FEAT_EVAL