blob: e870a279c03fd4d69feffa9deab238962272b573 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010029 int tcd_catch_idx; // instruction of the first :catch or :finally
30 int tcd_finally_idx; // instruction of the :finally block or zero
31 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010032 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010033 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_return; // when TRUE return from end of :finally
35} trycmd_T;
36
Bram Moolenaar4c137212021-04-19 16:48:48 +020037// Data local to a function.
38// On a function call, if not empty, is saved on the stack and restored when
39// returning.
40typedef struct {
41 int floc_restore_cmdmod;
42 cmdmod_T floc_save_cmdmod;
43 int floc_restore_cmdmod_stacklen;
44} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010045
46// A stack is used to store:
47// - arguments passed to a :def function
48// - info about the calling function, to use when returning
49// - local variables
50// - temporary values
51//
52// In detail (FP == Frame Pointer):
53// arg1 first argument from caller (if present)
54// arg2 second argument from caller (if present)
55// extra_arg1 any missing optional argument default value
56// FP -> cur_func calling function
57// current previous instruction pointer
58// frame_ptr previous Frame Pointer
59// var1 space for local variable
60// var2 space for local variable
61// .... fixed space for max. number of local variables
62// temp temporary values
63// .... flexible space for temporary values (can grow big)
64
65/*
66 * Execution context.
67 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010068struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010069 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020070 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020071 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072
Bram Moolenaar0186e582021-01-10 18:33:11 +010073 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020074 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076 garray_T ec_trystack; // stack of trycmd_T values
77 int ec_in_catch; // when TRUE in catch or finally block
78
79 int ec_dfunc_idx; // current function index
80 isn_T *ec_instr; // array with instructions
81 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020082
83 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020084
85 int ec_did_emsg_before;
86 int ec_trylevel_at_start;
87 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010088};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010089
Bram Moolenaar12d26532021-02-19 19:13:21 +010090#ifdef FEAT_PROFILE
91// stack of profinfo_T used when profiling.
92static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
93#endif
94
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020096#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097
Bram Moolenaar418f1df2020-08-12 21:34:49 +020098 void
99to_string_error(vartype_T vartype)
100{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200101 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200102}
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100105 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106 */
107 static int
108ufunc_argcount(ufunc_T *ufunc)
109{
110 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100144 * This adds a stack frame and sets the instruction pointer to the start of the
145 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100146 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147 *
148 * Stack has:
149 * - current arguments (already there)
150 * - omitted optional argument (default values) added here
151 * - stack frame:
152 * - pointer to calling function
153 * - Index of next instruction in calling function
154 * - previous frame pointer
155 * - reserved space for local variables
156 */
157 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100158call_dfunc(
159 int cdf_idx,
160 partial_T *pt,
161 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100162 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100164 int argcount = argcount_arg;
165 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
166 ufunc_T *ufunc = dfunc->df_ufunc;
167 int arg_to_add;
168 int vararg_count = 0;
169 int varcount;
170 int idx;
171 estack_T *entry;
172 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100176 // don't use ufunc->uf_name, it may have been freed
177 emsg_funcname(e_func_deleted,
178 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 return FAIL;
180 }
181
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100182#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100183 if (do_profiling == PROF_YES)
184 {
185 if (ga_grow(&profile_info_ga, 1) == OK)
186 {
187 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
188 + profile_info_ga.ga_len;
189 ++profile_info_ga.ga_len;
190 CLEAR_POINTER(info);
191 profile_may_start_func(info, ufunc,
192 (((dfunc_T *)def_functions.ga_data)
193 + ectx->ec_dfunc_idx)->df_ufunc);
194 }
195
196 // Profiling might be enabled/disabled along the way. This should not
197 // fail, since the function was compiled before and toggling profiling
198 // doesn't change any errors.
199 if (func_needs_compiling(ufunc, PROFILING(ufunc))
200 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100201 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100202 return FAIL;
203 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100204#endif
205
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200206 if (ufunc->uf_va_name != NULL)
207 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200208 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200209 // Stack at time of call with 2 varargs:
210 // normal_arg
211 // optional_arg
212 // vararg_1
213 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200214 // After creating the list:
215 // normal_arg
216 // optional_arg
217 // vararg-list
218 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200219 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200220 // After creating the list
221 // normal_arg
222 // (space for optional_arg)
223 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200224 vararg_count = argcount - ufunc->uf_args.ga_len;
225 if (vararg_count < 0)
226 vararg_count = 0;
227 else
228 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200229 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200230 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200231
232 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200233 }
234
Bram Moolenaarfe270812020-04-11 22:31:27 +0200235 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200236 if (arg_to_add < 0)
237 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200238 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200239 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200240 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200241 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200242 return FAIL;
243 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200244
245 // Reserve space for:
246 // - missing arguments
247 // - stack frame
248 // - local variables
249 // - if needed: a counter for number of closures created in
250 // ectx->ec_funcrefs.
251 varcount = dfunc->df_varcount + dfunc->df_has_closure;
252 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
253 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254 return FAIL;
255
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100256 // If depth of calling is getting too high, don't execute the function.
257 if (funcdepth_increment() == FAIL)
258 return FAIL;
259
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100260 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200261 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100262 {
263 floc = ALLOC_ONE(funclocal_T);
264 if (floc == NULL)
265 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200266 *floc = ectx->ec_funclocal;
267 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100268 }
269
Bram Moolenaarfe270812020-04-11 22:31:27 +0200270 // Move the vararg-list to below the missing optional arguments.
271 if (vararg_count > 0 && arg_to_add > 0)
272 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100273
274 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200275 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200276 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200277 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278
279 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
281 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200282 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100283 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100284 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100285 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200286 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287
288 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200289 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200291 if (dfunc->df_has_closure)
292 {
293 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
294
295 tv->v_type = VAR_NUMBER;
296 tv->vval.v_number = 0;
297 }
298 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100300 if (pt != NULL || ufunc->uf_partial != NULL
301 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100302 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100303 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
304
305 if (outer == NULL)
306 return FAIL;
307 if (pt != NULL)
308 {
309 *outer = pt->pt_outer;
310 outer->out_up_is_copy = TRUE;
311 }
312 else if (ufunc->uf_partial != NULL)
313 {
314 *outer = ufunc->uf_partial->pt_outer;
315 outer->out_up_is_copy = TRUE;
316 }
317 else
318 {
319 outer->out_stack = &ectx->ec_stack;
320 outer->out_frame_idx = ectx->ec_frame_idx;
321 outer->out_up = ectx->ec_outer;
322 }
323 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100324 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100325 else
326 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100327
Bram Moolenaarc970e422021-03-17 15:03:04 +0100328 ++ufunc->uf_calls;
329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100330 // Set execution state to the start of the called function.
331 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100332 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100333 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200334 if (entry != NULL)
335 {
336 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200337 // Save the current context so it can be restored on return.
338 entry->es_save_sctx = current_sctx;
339 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200340 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100341
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200342 // Start execution at the first instruction.
343 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344
345 return OK;
346}
347
348// Get pointer to item in the stack.
349#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
350
351/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200352 * Used when returning from a function: Check if any closure is still
353 * referenced. If so then move the arguments and variables to a separate piece
354 * of stack to be used when the closure is called.
355 * When "free_arguments" is TRUE the arguments are to be freed.
356 * Returns FAIL when out of memory.
357 */
358 static int
359handle_closure_in_use(ectx_T *ectx, int free_arguments)
360{
361 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
362 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200363 int argcount;
364 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200365 int idx;
366 typval_T *tv;
367 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200368 garray_T *gap = &ectx->ec_funcrefs;
369 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200370
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200371 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200372 return OK; // function was freed
373 if (dfunc->df_has_closure == 0)
374 return OK; // no closures
375 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
376 closure_count = tv->vval.v_number;
377 if (closure_count == 0)
378 return OK; // no funcrefs created
379
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200380 argcount = ufunc_argcount(dfunc->df_ufunc);
381 top = ectx->ec_frame_idx - argcount;
382
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200383 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200384 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200385 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200386 partial_T *pt;
387 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200389 if (off < 0)
390 continue; // count is off or already done
391 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200392 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200393 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200394 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200395 int i;
396
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200397 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200398 // unreferenced on return.
399 for (i = 0; i < dfunc->df_varcount; ++i)
400 {
401 typval_T *stv = STACK_TV(ectx->ec_frame_idx
402 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200403 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200404 --refcount;
405 }
406 if (refcount > 1)
407 {
408 closure_in_use = TRUE;
409 break;
410 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200411 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200412 }
413
414 if (closure_in_use)
415 {
416 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
417 typval_T *stack;
418
419 // A closure is using the arguments and/or local variables.
420 // Move them to the called function.
421 if (funcstack == NULL)
422 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200423 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
424 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200425 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
426 funcstack->fs_ga.ga_data = stack;
427 if (stack == NULL)
428 {
429 vim_free(funcstack);
430 return FAIL;
431 }
432
433 // Move or copy the arguments.
434 for (idx = 0; idx < argcount; ++idx)
435 {
436 tv = STACK_TV(top + idx);
437 if (free_arguments)
438 {
439 *(stack + idx) = *tv;
440 tv->v_type = VAR_UNKNOWN;
441 }
442 else
443 copy_tv(tv, stack + idx);
444 }
445 // Move the local variables.
446 for (idx = 0; idx < dfunc->df_varcount; ++idx)
447 {
448 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200449
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200450 // A partial created for a local function, that is also used as a
451 // local variable, has a reference count for the variable, thus
452 // will never go down to zero. When all these refcounts are one
453 // then the funcstack is unused. We need to count how many we have
454 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200455 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
456 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200457 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200458
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200459 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200460 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
461 gap->ga_len - closure_count + i])
462 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200463 }
464
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200465 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 tv->v_type = VAR_UNKNOWN;
467 }
468
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200469 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200470 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200471 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
472 - closure_count + idx];
473 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200474 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200475 ++funcstack->fs_refcount;
476 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100477 pt->pt_outer.out_stack = &funcstack->fs_ga;
478 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
479 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200480 }
481 }
482 }
483
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200484 for (idx = 0; idx < closure_count; ++idx)
485 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
486 - closure_count + idx]);
487 gap->ga_len -= closure_count;
488 if (gap->ga_len == 0)
489 ga_clear(gap);
490
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200491 return OK;
492}
493
494/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200495 * Called when a partial is freed or its reference count goes down to one. The
496 * funcstack may be the only reference to the partials in the local variables.
497 * Go over all of them, the funcref and can be freed if all partials
498 * referencing the funcstack have a reference count of one.
499 */
500 void
501funcstack_check_refcount(funcstack_T *funcstack)
502{
503 int i;
504 garray_T *gap = &funcstack->fs_ga;
505 int done = 0;
506
507 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
508 return;
509 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
510 {
511 typval_T *tv = ((typval_T *)gap->ga_data) + i;
512
513 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
514 && tv->vval.v_partial->pt_funcstack == funcstack
515 && tv->vval.v_partial->pt_refcount == 1)
516 ++done;
517 }
518 if (done == funcstack->fs_min_refcount)
519 {
520 typval_T *stack = gap->ga_data;
521
522 // All partials referencing the funcstack have a reference count of
523 // one, thus the funcstack is no longer of use.
524 for (i = 0; i < gap->ga_len; ++i)
525 clear_tv(stack + i);
526 vim_free(stack);
527 vim_free(funcstack);
528 }
529}
530
531/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 * Return from the current function.
533 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200534 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200535func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100538 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200539 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
540 + ectx->ec_dfunc_idx;
541 int argcount = ufunc_argcount(dfunc->df_ufunc);
542 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200543 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100544 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
545 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200546 funclocal_T *floc;
547#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100548 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
549 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550
Bram Moolenaar12d26532021-02-19 19:13:21 +0100551 if (do_profiling == PROF_YES)
552 {
553 ufunc_T *caller = prev_dfunc->df_ufunc;
554
555 if (dfunc->df_ufunc->uf_profiling
556 || (caller != NULL && caller->uf_profiling))
557 {
558 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
559 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
560 --profile_info_ga.ga_len;
561 }
562 }
563#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100564 // TODO: when is it safe to delete the function when it is no longer used?
565 --dfunc->df_ufunc->uf_calls;
566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200568 entry = estack_pop();
569 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200570 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200572 if (handle_closure_in_use(ectx, TRUE) == FAIL)
573 return FAIL;
574
575 // Clear the arguments.
576 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
577 clear_tv(STACK_TV(idx));
578
579 // Clear local variables and temp values, but not the return value.
580 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100581 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100583
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100584 // The return value should be on top of the stack. However, when aborting
585 // it may not be there and ec_frame_idx is the top of the stack.
586 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100587 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100588 ret_idx = 0;
589
Bram Moolenaar0186e582021-01-10 18:33:11 +0100590 vim_free(ectx->ec_outer);
591
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100592 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100593 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100594 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
595 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200596 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
597 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100598 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
599 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100600 floc = (void *)STACK_TV(ectx->ec_frame_idx
601 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200602 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100603 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
604 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200605
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100606 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200607 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100608 else
609 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200610 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100611 vim_free(floc);
612 }
613
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100614 if (ret_idx > 0)
615 {
616 // Reset the stack to the position before the call, with a spot for the
617 // return value, moved there from above the frame.
618 ectx->ec_stack.ga_len = top + 1;
619 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
620 }
621 else
622 // Reset the stack to the position before the call.
623 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200624
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100625 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627}
628
629#undef STACK_TV
630
631/*
632 * Prepare arguments and rettv for calling a builtin or user function.
633 */
634 static int
635call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
636{
637 int idx;
638 typval_T *tv;
639
640 // Move arguments from bottom of the stack to argvars[] and add terminator.
641 for (idx = 0; idx < argcount; ++idx)
642 argvars[idx] = *STACK_TV_BOT(idx - argcount);
643 argvars[argcount].v_type = VAR_UNKNOWN;
644
645 // Result replaces the arguments on the stack.
646 if (argcount > 0)
647 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200648 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 return FAIL;
650 else
651 ++ectx->ec_stack.ga_len;
652
653 // Default return value is zero.
654 tv = STACK_TV_BOT(-1);
655 tv->v_type = VAR_NUMBER;
656 tv->vval.v_number = 0;
657
658 return OK;
659}
660
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200661// Ugly global to avoid passing the execution context around through many
662// layers.
663static ectx_T *current_ectx = NULL;
664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665/*
666 * Call a builtin function by index.
667 */
668 static int
669call_bfunc(int func_idx, int argcount, ectx_T *ectx)
670{
671 typval_T argvars[MAX_FUNC_ARGS];
672 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100673 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200674 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 if (call_prepare(argcount, argvars, ectx) == FAIL)
677 return FAIL;
678
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200679 // Call the builtin function. Set "current_ectx" so that when it
680 // recursively invokes call_def_function() a closure context can be set.
681 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200683 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684
685 // Clear the arguments.
686 for (idx = 0; idx < argcount; ++idx)
687 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200688
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100689 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200690 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 return OK;
692}
693
694/*
695 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100696 * If the function is compiled this will add a stack frame and set the
697 * instruction pointer at the start of the function.
698 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100699 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100700 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 */
702 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100703call_ufunc(
704 ufunc_T *ufunc,
705 partial_T *pt,
706 int argcount,
707 ectx_T *ectx,
708 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709{
710 typval_T argvars[MAX_FUNC_ARGS];
711 funcexe_T funcexe;
712 int error;
713 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100714 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100715#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100716 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100717#else
718# define profiling FALSE
719#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720
Bram Moolenaarb2049902021-01-24 12:53:53 +0100721 if (func_needs_compiling(ufunc, profiling)
722 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200723 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200724 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100725 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100726 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100727 if (error != FCERR_UNKNOWN)
728 {
729 if (error == FCERR_TOOMANY)
730 semsg(_(e_toomanyarg), ufunc->uf_name);
731 else
732 semsg(_(e_toofewarg), ufunc->uf_name);
733 return FAIL;
734 }
735
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100736 // The function has been compiled, can call it quickly. For a function
737 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100738 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100739 if (iptr != NULL)
740 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100741 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100742 iptr->isn_type = ISN_DCALL;
743 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
744 iptr->isn_arg.dfunc.cdf_argcount = argcount;
745 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200746 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748
749 if (call_prepare(argcount, argvars, ectx) == FAIL)
750 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200751 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 funcexe.evaluate = TRUE;
753
754 // Call the user function. Result goes in last position on the stack.
755 // TODO: add selfdict if there is one
756 error = call_user_func_check(ufunc, argcount, argvars,
757 STACK_TV_BOT(-1), &funcexe, NULL);
758
759 // Clear the arguments.
760 for (idx = 0; idx < argcount; ++idx)
761 clear_tv(&argvars[idx]);
762
763 if (error != FCERR_NONE)
764 {
765 user_func_error(error, ufunc->uf_name);
766 return FAIL;
767 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100768 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200769 // Error other than from calling the function itself.
770 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 return OK;
772}
773
774/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100775 * If command modifiers were applied restore them.
776 */
777 static void
778may_restore_cmdmod(funclocal_T *funclocal)
779{
780 if (funclocal->floc_restore_cmdmod)
781 {
782 cmdmod.cmod_filter_regmatch.regprog = NULL;
783 undo_cmdmod(&cmdmod);
784 cmdmod = funclocal->floc_save_cmdmod;
785 funclocal->floc_restore_cmdmod = FALSE;
786 }
787}
788
789/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200790 * Return TRUE if an error was given or CTRL-C was pressed.
791 */
792 static int
793vim9_aborting(int prev_called_emsg)
794{
795 return called_emsg > prev_called_emsg || got_int || did_throw;
796}
797
798/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 * Execute a function by "name".
800 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100801 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802 * Returns FAIL if not found without an error message.
803 */
804 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100805call_by_name(
806 char_u *name,
807 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100808 ectx_T *ectx,
809 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810{
811 ufunc_T *ufunc;
812
813 if (builtin_function(name, -1))
814 {
815 int func_idx = find_internal_func(name);
816
817 if (func_idx < 0)
818 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200819 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 return FAIL;
821 return call_bfunc(func_idx, argcount, ectx);
822 }
823
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200824 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200825
826 if (ufunc == NULL)
827 {
828 int called_emsg_before = called_emsg;
829
830 if (script_autoload(name, TRUE))
831 // loaded a package, search for the function again
832 ufunc = find_func(name, FALSE, NULL);
833 if (vim9_aborting(called_emsg_before))
834 return FAIL; // bail out if loading the script caused an error
835 }
836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100838 {
839 if (ufunc->uf_arg_types != NULL)
840 {
841 int i;
842 typval_T *argv = STACK_TV_BOT(0) - argcount;
843
844 // The function can change at runtime, check that the argument
845 // types are correct.
846 for (i = 0; i < argcount; ++i)
847 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100848 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100849
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100850 if (i < ufunc->uf_args.ga_len)
851 type = ufunc->uf_arg_types[i];
852 else if (ufunc->uf_va_type != NULL)
853 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100854 if (type != NULL && check_typval_arg_type(type,
855 &argv[i], i + 1) == FAIL)
856 return FAIL;
857 }
858 }
859
Bram Moolenaar4c137212021-04-19 16:48:48 +0200860 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862
863 return FAIL;
864}
865
866 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100867call_partial(
868 typval_T *tv,
869 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100870 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200872 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200873 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100875 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876
877 if (tv->v_type == VAR_PARTIAL)
878 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200879 partial_T *pt = tv->vval.v_partial;
880 int i;
881
882 if (pt->pt_argc > 0)
883 {
884 // Make space for arguments from the partial, shift the "argcount"
885 // arguments up.
886 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
887 return FAIL;
888 for (i = 1; i <= argcount; ++i)
889 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
890 ectx->ec_stack.ga_len += pt->pt_argc;
891 argcount += pt->pt_argc;
892
893 // copy the arguments from the partial onto the stack
894 for (i = 0; i < pt->pt_argc; ++i)
895 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897
898 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200899 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 name = pt->pt_name;
902 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200903 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200905 if (name != NULL)
906 {
907 char_u fname_buf[FLEN_FIXED + 1];
908 char_u *tofree = NULL;
909 int error = FCERR_NONE;
910 char_u *fname;
911
912 // May need to translate <SNR>123_ to K_SNR.
913 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
914 if (error != FCERR_NONE)
915 res = FAIL;
916 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200917 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200918 vim_free(tofree);
919 }
920
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100921 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 {
923 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200924 semsg(_(e_unknownfunc),
925 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 return FAIL;
927 }
928 return OK;
929}
930
931/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200932 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
933 * TRUE.
934 */
935 static int
936error_if_locked(int lock, char *error)
937{
938 if (lock & (VAR_LOCKED | VAR_FIXED))
939 {
940 emsg(_(error));
941 return TRUE;
942 }
943 return FALSE;
944}
945
946/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100947 * Give an error if "tv" is not a number and return FAIL.
948 */
949 static int
950check_for_number(typval_T *tv)
951{
952 if (tv->v_type != VAR_NUMBER)
953 {
954 semsg(_(e_expected_str_but_got_str),
955 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
956 return FAIL;
957 }
958 return OK;
959}
960
961/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100962 * Store "tv" in variable "name".
963 * This is for s: and g: variables.
964 */
965 static void
966store_var(char_u *name, typval_T *tv)
967{
968 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200969 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100970
Bram Moolenaard877a572021-04-01 19:42:48 +0200971 if (tv->v_lock)
972 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100973 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +0200974 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100975 restore_funccal();
976}
977
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100978/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100979 * Convert "tv" to a string.
980 * Return FAIL if not allowed.
981 */
982 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200983do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100984{
985 if (tv->v_type != VAR_STRING)
986 {
987 char_u *str;
988
989 if (is_2string_any)
990 {
991 switch (tv->v_type)
992 {
993 case VAR_SPECIAL:
994 case VAR_BOOL:
995 case VAR_NUMBER:
996 case VAR_FLOAT:
997 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +0200998
999 case VAR_LIST:
1000 if (tolerant)
1001 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001002 char_u *s, *e, *p;
1003 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001004
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001005 ga_init2(&ga, sizeof(char_u *), 1);
1006
1007 // Convert to NL separated items, then
1008 // escape the items and replace the NL with
1009 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001010 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001011 if (str == NULL)
1012 return FAIL;
1013 s = str;
1014 while ((e = vim_strchr(s, '\n')) != NULL)
1015 {
1016 *e = NUL;
1017 p = vim_strsave_fnameescape(s, FALSE);
1018 if (p != NULL)
1019 {
1020 ga_concat(&ga, p);
1021 ga_concat(&ga, (char_u *)" ");
1022 vim_free(p);
1023 }
1024 s = e + 1;
1025 }
1026 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001027 clear_tv(tv);
1028 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001029 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001030 return OK;
1031 }
1032 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001033 default: to_string_error(tv->v_type);
1034 return FAIL;
1035 }
1036 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001037 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001038 clear_tv(tv);
1039 tv->v_type = VAR_STRING;
1040 tv->vval.v_string = str;
1041 }
1042 return OK;
1043}
1044
1045/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001046 * When the value of "sv" is a null list of dict, allocate it.
1047 */
1048 static void
1049allocate_if_null(typval_T *tv)
1050{
1051 switch (tv->v_type)
1052 {
1053 case VAR_LIST:
1054 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001055 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001056 break;
1057 case VAR_DICT:
1058 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001059 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001060 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001061 case VAR_BLOB:
1062 if (tv->vval.v_blob == NULL)
1063 (void)rettv_blob_alloc(tv);
1064 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001065 default:
1066 break;
1067 }
1068}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001069
Bram Moolenaare7525c52021-01-09 13:20:37 +01001070/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001071 * Return the character "str[index]" where "index" is the character index,
1072 * including composing characters.
1073 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001074 */
1075 char_u *
1076char_from_string(char_u *str, varnumber_T index)
1077{
1078 size_t nbyte = 0;
1079 varnumber_T nchar = index;
1080 size_t slen;
1081
1082 if (str == NULL)
1083 return NULL;
1084 slen = STRLEN(str);
1085
Bram Moolenaarff871402021-03-26 13:34:05 +01001086 // Do the same as for a list: a negative index counts from the end.
1087 // Optimization to check the first byte to be below 0x80 (and no composing
1088 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001089 if (index < 0)
1090 {
1091 int clen = 0;
1092
1093 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001094 {
1095 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1096 ++nbyte;
1097 else if (enc_utf8)
1098 nbyte += utfc_ptr2len(str + nbyte);
1099 else
1100 nbyte += mb_ptr2len(str + nbyte);
1101 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001102 nchar = clen + index;
1103 if (nchar < 0)
1104 // unlike list: index out of range results in empty string
1105 return NULL;
1106 }
1107
1108 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001109 {
1110 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1111 ++nbyte;
1112 else if (enc_utf8)
1113 nbyte += utfc_ptr2len(str + nbyte);
1114 else
1115 nbyte += mb_ptr2len(str + nbyte);
1116 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001117 if (nbyte >= slen)
1118 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001119 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001120}
1121
1122/*
1123 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001124 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001125 * If going over the end return "str_len".
1126 * If "idx" is negative count from the end, -1 is the last character.
1127 * When going over the start return -1.
1128 */
1129 static long
1130char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1131{
1132 varnumber_T nchar = idx;
1133 size_t nbyte = 0;
1134
1135 if (nchar >= 0)
1136 {
1137 while (nchar > 0 && nbyte < str_len)
1138 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001139 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001140 --nchar;
1141 }
1142 }
1143 else
1144 {
1145 nbyte = str_len;
1146 while (nchar < 0 && nbyte > 0)
1147 {
1148 --nbyte;
1149 nbyte -= mb_head_off(str, str + nbyte);
1150 ++nchar;
1151 }
1152 if (nchar < 0)
1153 return -1;
1154 }
1155 return (long)nbyte;
1156}
1157
1158/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001159 * Return the slice "str[first : last]" using character indexes. Composing
1160 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001161 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001162 * Return NULL when the result is empty.
1163 */
1164 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001165string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001166{
1167 long start_byte, end_byte;
1168 size_t slen;
1169
1170 if (str == NULL)
1171 return NULL;
1172 slen = STRLEN(str);
1173 start_byte = char_idx2byte(str, slen, first);
1174 if (start_byte < 0)
1175 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001176 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001177 end_byte = (long)slen;
1178 else
1179 {
1180 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001181 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001182 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001183 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001184 }
1185
1186 if (start_byte >= (long)slen || end_byte <= start_byte)
1187 return NULL;
1188 return vim_strnsave(str + start_byte, end_byte - start_byte);
1189}
1190
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001191 static svar_T *
1192get_script_svar(scriptref_T *sref, ectx_T *ectx)
1193{
1194 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1195 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1196 + ectx->ec_dfunc_idx;
1197 svar_T *sv;
1198
1199 if (sref->sref_seq != si->sn_script_seq)
1200 {
1201 // The script was reloaded after the function was
1202 // compiled, the script_idx may not be valid.
1203 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1204 dfunc->df_ufunc->uf_name_exp);
1205 return NULL;
1206 }
1207 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1208 if (!equal_type(sv->sv_type, sref->sref_type))
1209 {
1210 emsg(_(e_script_variable_type_changed));
1211 return NULL;
1212 }
1213 return sv;
1214}
1215
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001216/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 * Execute a function by "name".
1218 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001219 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 */
1221 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001222call_eval_func(
1223 char_u *name,
1224 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001225 ectx_T *ectx,
1226 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227{
Bram Moolenaared677f52020-08-12 16:38:10 +02001228 int called_emsg_before = called_emsg;
1229 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230
Bram Moolenaar4c137212021-04-19 16:48:48 +02001231 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001232 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001234 dictitem_T *v;
1235
1236 v = find_var(name, NULL, FALSE);
1237 if (v == NULL)
1238 {
1239 semsg(_(e_unknownfunc), name);
1240 return FAIL;
1241 }
1242 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1243 {
1244 semsg(_(e_unknownfunc), name);
1245 return FAIL;
1246 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001247 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001249 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250}
1251
1252/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001253 * When a function reference is used, fill a partial with the information
1254 * needed, especially when it is used as a closure.
1255 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001256 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001257fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1258{
1259 pt->pt_func = ufunc;
1260 pt->pt_refcount = 1;
1261
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001262 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001263 {
1264 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1265 + ectx->ec_dfunc_idx;
1266
1267 // The closure needs to find arguments and local
1268 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001269 pt->pt_outer.out_stack = &ectx->ec_stack;
1270 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1271 pt->pt_outer.out_up = ectx->ec_outer;
1272 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001273
1274 // If this function returns and the closure is still
1275 // being used, we need to make a copy of the context
1276 // (arguments and local variables). Store a reference
1277 // to the partial so we can handle that.
1278 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1279 {
1280 vim_free(pt);
1281 return FAIL;
1282 }
1283 // Extra variable keeps the count of closures created
1284 // in the current function call.
1285 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1286 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1287
1288 ((partial_T **)ectx->ec_funcrefs.ga_data)
1289 [ectx->ec_funcrefs.ga_len] = pt;
1290 ++pt->pt_refcount;
1291 ++ectx->ec_funcrefs.ga_len;
1292 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001293 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001294 return OK;
1295}
1296
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001297// used for v_instr of typval of VAR_INSTR
1298struct instr_S {
1299 ectx_T *instr_ectx;
1300 isn_T *instr_instr;
1301};
1302
Bram Moolenaar4c137212021-04-19 16:48:48 +02001303// used for substitute_instr
1304typedef struct subs_expr_S {
1305 ectx_T *subs_ectx;
1306 isn_T *subs_instr;
1307 int subs_status;
1308} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309
1310// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001311#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
1313// Get pointer to item at the bottom of the stack, -1 is the bottom.
1314#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001315#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 +01001316
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001317// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001318#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 +01001319
Bram Moolenaar4c137212021-04-19 16:48:48 +02001320/*
1321 * Execute instructions in execution context "ectx".
1322 * Return OK or FAIL;
1323 */
1324 static int
1325exec_instructions(ectx_T *ectx)
1326{
1327 int breakcheck_count = 0;
1328 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001329 int ret = FAIL;
1330 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001331
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001332 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001333 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001334
Bram Moolenaarff652882021-05-16 15:24:49 +02001335 // Only catch exceptions in this instruction list.
1336 ectx->ec_trylevel_at_start = trylevel;
1337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 for (;;)
1339 {
1340 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001341
Bram Moolenaar270d0382020-05-15 21:42:53 +02001342 if (++breakcheck_count >= 100)
1343 {
1344 line_breakcheck();
1345 breakcheck_count = 0;
1346 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001347 if (got_int)
1348 {
1349 // Turn CTRL-C into an exception.
1350 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001351 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001352 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001353 did_throw = TRUE;
1354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355
Bram Moolenaara26b9702020-04-18 19:53:28 +02001356 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1357 {
1358 // Turn an error message into an exception.
1359 did_emsg = FALSE;
1360 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001361 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001362 did_throw = TRUE;
1363 *msg_list = NULL;
1364 }
1365
Bram Moolenaar4c137212021-04-19 16:48:48 +02001366 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001368 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001369 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370
1371 // An exception jumps to the first catch, finally, or returns from
1372 // the current function.
1373 if (trystack->ga_len > 0)
1374 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001375 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 {
1377 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001378 ectx->ec_in_catch = TRUE;
1379 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 }
1381 else
1382 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001383 // Not inside try or need to return from current functions.
1384 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001385 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001386 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001387 tv = STACK_TV_BOT(0);
1388 tv->v_type = VAR_NUMBER;
1389 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001390 ++ectx->ec_stack.ga_len;
1391 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001393 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001394 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001395 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001396 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 goto done;
1398 }
1399
Bram Moolenaar4c137212021-04-19 16:48:48 +02001400 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001401 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 }
1403 continue;
1404 }
1405
Bram Moolenaar4c137212021-04-19 16:48:48 +02001406 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 switch (iptr->isn_type)
1408 {
1409 // execute Ex command line
1410 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001411 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001412 source_cookie_T cookie;
1413
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001414 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001415 // Pass getsourceline to get an error for a missing ":end"
1416 // command.
1417 CLEAR_FIELD(cookie);
1418 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1419 if (do_cmdline(iptr->isn_arg.string,
1420 getsourceline, &cookie,
1421 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1422 == FAIL
1423 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001424 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 break;
1427
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001428 // Evaluate an expression with legacy syntax, push it onto the
1429 // stack.
1430 case ISN_LEGACY_EVAL:
1431 {
1432 char_u *arg = iptr->isn_arg.string;
1433 int res;
1434 int save_flags = cmdmod.cmod_flags;
1435
1436 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001437 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001438 tv = STACK_TV_BOT(0);
1439 init_tv(tv);
1440 cmdmod.cmod_flags |= CMOD_LEGACY;
1441 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1442 cmdmod.cmod_flags = save_flags;
1443 if (res == FAIL)
1444 goto on_error;
1445 ++ectx->ec_stack.ga_len;
1446 }
1447 break;
1448
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001449 // push typeval VAR_INSTR with instructions to be executed
1450 case ISN_INSTR:
1451 {
1452 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001453 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001454 tv = STACK_TV_BOT(0);
1455 tv->vval.v_instr = ALLOC_ONE(instr_T);
1456 if (tv->vval.v_instr == NULL)
1457 goto on_error;
1458 ++ectx->ec_stack.ga_len;
1459
1460 tv->v_type = VAR_INSTR;
1461 tv->vval.v_instr->instr_ectx = ectx;
1462 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1463 }
1464 break;
1465
Bram Moolenaar4c137212021-04-19 16:48:48 +02001466 // execute :substitute with an expression
1467 case ISN_SUBSTITUTE:
1468 {
1469 subs_T *subs = &iptr->isn_arg.subs;
1470 source_cookie_T cookie;
1471 struct subs_expr_S *save_instr = substitute_instr;
1472 struct subs_expr_S subs_instr;
1473 int res;
1474
1475 subs_instr.subs_ectx = ectx;
1476 subs_instr.subs_instr = subs->subs_instr;
1477 subs_instr.subs_status = OK;
1478 substitute_instr = &subs_instr;
1479
1480 SOURCING_LNUM = iptr->isn_lnum;
1481 // This is very much like ISN_EXEC
1482 CLEAR_FIELD(cookie);
1483 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1484 res = do_cmdline(subs->subs_cmd,
1485 getsourceline, &cookie,
1486 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1487 substitute_instr = save_instr;
1488
1489 if (res == FAIL || did_emsg
1490 || subs_instr.subs_status == FAIL)
1491 goto on_error;
1492 }
1493 break;
1494
1495 case ISN_FINISH:
1496 goto done;
1497
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001498 case ISN_REDIRSTART:
1499 // create a dummy entry for var_redir_str()
1500 if (alloc_redir_lval() == FAIL)
1501 goto on_error;
1502
1503 // The output is stored in growarray "redir_ga" until
1504 // redirection ends.
1505 init_redir_ga();
1506 redir_vname = 1;
1507 break;
1508
1509 case ISN_REDIREND:
1510 {
1511 char_u *res = get_clear_redir_ga();
1512
1513 // End redirection, put redirected text on the stack.
1514 clear_redir_lval();
1515 redir_vname = 0;
1516
1517 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1518 {
1519 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001520 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001521 }
1522 tv = STACK_TV_BOT(0);
1523 tv->v_type = VAR_STRING;
1524 tv->vval.v_string = res;
1525 ++ectx->ec_stack.ga_len;
1526 }
1527 break;
1528
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001529 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001530#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001531 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1532 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001533#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001534 break;
1535
1536 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001537#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001538 {
1539 exarg_T ea;
1540 int res;
1541
1542 CLEAR_FIELD(ea);
1543 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1544 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1545 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1546 --ectx->ec_stack.ga_len;
1547 tv = STACK_TV_BOT(0);
1548 res = cexpr_core(&ea, tv);
1549 clear_tv(tv);
1550 if (res == FAIL)
1551 goto on_error;
1552 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001553#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001554 break;
1555
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001556 // execute Ex command from pieces on the stack
1557 case ISN_EXECCONCAT:
1558 {
1559 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001560 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001561 int pass;
1562 int i;
1563 char_u *cmd = NULL;
1564 char_u *str;
1565
1566 for (pass = 1; pass <= 2; ++pass)
1567 {
1568 for (i = 0; i < count; ++i)
1569 {
1570 tv = STACK_TV_BOT(i - count);
1571 str = tv->vval.v_string;
1572 if (str != NULL && *str != NUL)
1573 {
1574 if (pass == 2)
1575 STRCPY(cmd + len, str);
1576 len += STRLEN(str);
1577 }
1578 if (pass == 2)
1579 clear_tv(tv);
1580 }
1581 if (pass == 1)
1582 {
1583 cmd = alloc(len + 1);
1584 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001585 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001586 len = 0;
1587 }
1588 }
1589
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001590 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001591 do_cmdline_cmd(cmd);
1592 vim_free(cmd);
1593 }
1594 break;
1595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 // execute :echo {string} ...
1597 case ISN_ECHO:
1598 {
1599 int count = iptr->isn_arg.echo.echo_count;
1600 int atstart = TRUE;
1601 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001602 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603
1604 for (idx = 0; idx < count; ++idx)
1605 {
1606 tv = STACK_TV_BOT(idx - count);
1607 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1608 &atstart, &needclr);
1609 clear_tv(tv);
1610 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001611 if (needclr)
1612 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001613 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 }
1615 break;
1616
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001617 // :execute {string} ...
1618 // :echomsg {string} ...
1619 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001620 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001621 case ISN_ECHOMSG:
1622 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001623 {
1624 int count = iptr->isn_arg.number;
1625 garray_T ga;
1626 char_u buf[NUMBUFLEN];
1627 char_u *p;
1628 int len;
1629 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001630 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001631
1632 ga_init2(&ga, 1, 80);
1633 for (idx = 0; idx < count; ++idx)
1634 {
1635 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001636 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001637 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001638 if (tv->v_type == VAR_CHANNEL
1639 || tv->v_type == VAR_JOB)
1640 {
1641 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001642 semsg(_(e_using_invalid_value_as_string_str),
1643 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001644 break;
1645 }
1646 else
1647 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001648 }
1649 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001650 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001651
1652 len = (int)STRLEN(p);
1653 if (ga_grow(&ga, len + 2) == FAIL)
1654 failed = TRUE;
1655 else
1656 {
1657 if (ga.ga_len > 0)
1658 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1659 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1660 ga.ga_len += len;
1661 }
1662 clear_tv(tv);
1663 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001664 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001665 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001666 {
1667 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001668 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001669 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001670
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001671 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001672 {
1673 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001674 {
1675 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001676 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001677 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001678 {
1679 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001680 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001681 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001682 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001683 else
1684 {
1685 msg_sb_eol();
1686 if (iptr->isn_type == ISN_ECHOMSG)
1687 {
1688 msg_attr(ga.ga_data, echo_attr);
1689 out_flush();
1690 }
1691 else
1692 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001693 SOURCING_LNUM = iptr->isn_lnum;
1694 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001695 }
1696 }
1697 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001698 ga_clear(&ga);
1699 }
1700 break;
1701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 // load local variable or argument
1703 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001704 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001705 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001707 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 break;
1709
1710 // load v: variable
1711 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001712 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001713 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001715 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 break;
1717
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001718 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 case ISN_LOADSCRIPT:
1720 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001721 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 svar_T *sv;
1723
Bram Moolenaar4c137212021-04-19 16:48:48 +02001724 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001725 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001726 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001727 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001728 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001729 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001731 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 }
1733 break;
1734
1735 // load s: variable in old script
1736 case ISN_LOADS:
1737 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001738 hashtab_T *ht = &SCRIPT_VARS(
1739 iptr->isn_arg.loadstore.ls_sid);
1740 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 if (di == NULL)
1744 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001745 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001746 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001747 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 }
1749 else
1750 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001751 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001752 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001754 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 }
1756 }
1757 break;
1758
Bram Moolenaard3aac292020-04-19 14:32:17 +02001759 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001761 case ISN_LOADB:
1762 case ISN_LOADW:
1763 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001765 dictitem_T *di = NULL;
1766 hashtab_T *ht = NULL;
1767 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001768
Bram Moolenaard3aac292020-04-19 14:32:17 +02001769 switch (iptr->isn_type)
1770 {
1771 case ISN_LOADG:
1772 ht = get_globvar_ht();
1773 namespace = 'g';
1774 break;
1775 case ISN_LOADB:
1776 ht = &curbuf->b_vars->dv_hashtab;
1777 namespace = 'b';
1778 break;
1779 case ISN_LOADW:
1780 ht = &curwin->w_vars->dv_hashtab;
1781 namespace = 'w';
1782 break;
1783 case ISN_LOADT:
1784 ht = &curtab->tp_vars->dv_hashtab;
1785 namespace = 't';
1786 break;
1787 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001788 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001789 }
1790 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 if (di == NULL)
1793 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001794 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001795 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001796 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001797 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 }
1799 else
1800 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001801 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001802 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001804 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 }
1806 }
1807 break;
1808
Bram Moolenaar03290b82020-12-19 16:30:44 +01001809 // load autoload variable
1810 case ISN_LOADAUTO:
1811 {
1812 char_u *name = iptr->isn_arg.string;
1813
Bram Moolenaar4c137212021-04-19 16:48:48 +02001814 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001815 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001816 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001817 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001818 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001819 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001820 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001821 }
1822 break;
1823
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001824 // load g:/b:/w:/t: namespace
1825 case ISN_LOADGDICT:
1826 case ISN_LOADBDICT:
1827 case ISN_LOADWDICT:
1828 case ISN_LOADTDICT:
1829 {
1830 dict_T *d = NULL;
1831
1832 switch (iptr->isn_type)
1833 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001834 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1835 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1836 case ISN_LOADWDICT: d = curwin->w_vars; break;
1837 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001838 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001839 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001840 }
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 Moolenaar2f8ce0a2020-07-19 19:47:35 +02001843 tv = STACK_TV_BOT(0);
1844 tv->v_type = VAR_DICT;
1845 tv->v_lock = 0;
1846 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001847 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001848 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001849 }
1850 break;
1851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 // load &option
1853 case ISN_LOADOPT:
1854 {
1855 typval_T optval;
1856 char_u *name = iptr->isn_arg.string;
1857
Bram Moolenaara8c17702020-04-01 21:17:24 +02001858 // This is not expected to fail, name is checked during
1859 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001860 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001861 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001862 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001863 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001865 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 }
1867 break;
1868
1869 // load $ENV
1870 case ISN_LOADENV:
1871 {
1872 typval_T optval;
1873 char_u *name = iptr->isn_arg.string;
1874
Bram Moolenaar4c137212021-04-19 16:48:48 +02001875 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001876 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001877 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001878 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001880 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 }
1882 break;
1883
1884 // load @register
1885 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001886 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001887 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 tv = STACK_TV_BOT(0);
1889 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001890 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001891 // This may result in NULL, which should be equivalent to an
1892 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 tv->vval.v_string = get_reg_contents(
1894 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001895 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 break;
1897
1898 // store local variable
1899 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001900 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 tv = STACK_TV_VAR(iptr->isn_arg.number);
1902 clear_tv(tv);
1903 *tv = *STACK_TV_BOT(0);
1904 break;
1905
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001906 // store s: variable in old script
1907 case ISN_STORES:
1908 {
1909 hashtab_T *ht = &SCRIPT_VARS(
1910 iptr->isn_arg.loadstore.ls_sid);
1911 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001912 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001913
Bram Moolenaar4c137212021-04-19 16:48:48 +02001914 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001915 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001916 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001917 else
1918 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001919 SOURCING_LNUM = iptr->isn_lnum;
1920 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001921 {
1922 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001923 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001924 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001925 clear_tv(&di->di_tv);
1926 di->di_tv = *STACK_TV_BOT(0);
1927 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001928 }
1929 break;
1930
1931 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 case ISN_STORESCRIPT:
1933 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001934 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1935 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936
Bram Moolenaar4c137212021-04-19 16:48:48 +02001937 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001938 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001939 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001940 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001941
1942 // "const" and "final" are checked at compile time, locking
1943 // the value needs to be checked here.
1944 SOURCING_LNUM = iptr->isn_lnum;
1945 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001946 {
1947 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001948 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001949 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 clear_tv(sv->sv_tv);
1952 *sv->sv_tv = *STACK_TV_BOT(0);
1953 }
1954 break;
1955
1956 // store option
1957 case ISN_STOREOPT:
1958 {
1959 long n = 0;
1960 char_u *s = NULL;
1961 char *msg;
1962
Bram Moolenaar4c137212021-04-19 16:48:48 +02001963 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 tv = STACK_TV_BOT(0);
1965 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001966 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001968 if (s == NULL)
1969 s = (char_u *)"";
1970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001972 // must be VAR_NUMBER, CHECKTYPE makes sure
1973 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1975 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001976 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 if (msg != NULL)
1978 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001979 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001981 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 }
1984 break;
1985
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001986 // store $ENV
1987 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001988 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001989 tv = STACK_TV_BOT(0);
1990 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1991 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001992 break;
1993
1994 // store @r
1995 case ISN_STOREREG:
1996 {
1997 int reg = iptr->isn_arg.number;
1998
Bram Moolenaar4c137212021-04-19 16:48:48 +02001999 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002000 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002001 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002002 tv_get_string(tv), -1, FALSE);
2003 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002004 }
2005 break;
2006
2007 // store v: variable
2008 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002009 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002010 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2011 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002012 // should not happen, type is checked when compiling
2013 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002014 break;
2015
Bram Moolenaard3aac292020-04-19 14:32:17 +02002016 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002018 case ISN_STOREB:
2019 case ISN_STOREW:
2020 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002022 dictitem_T *di;
2023 hashtab_T *ht;
2024 char_u *name = iptr->isn_arg.string + 2;
2025
Bram Moolenaard3aac292020-04-19 14:32:17 +02002026 switch (iptr->isn_type)
2027 {
2028 case ISN_STOREG:
2029 ht = get_globvar_ht();
2030 break;
2031 case ISN_STOREB:
2032 ht = &curbuf->b_vars->dv_hashtab;
2033 break;
2034 case ISN_STOREW:
2035 ht = &curwin->w_vars->dv_hashtab;
2036 break;
2037 case ISN_STORET:
2038 ht = &curtab->tp_vars->dv_hashtab;
2039 break;
2040 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002041 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043
Bram Moolenaar4c137212021-04-19 16:48:48 +02002044 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002045 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002047 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 else
2049 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002050 SOURCING_LNUM = iptr->isn_lnum;
2051 if (var_check_permission(di, name) == FAIL)
2052 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 clear_tv(&di->di_tv);
2054 di->di_tv = *STACK_TV_BOT(0);
2055 }
2056 }
2057 break;
2058
Bram Moolenaar03290b82020-12-19 16:30:44 +01002059 // store an autoload variable
2060 case ISN_STOREAUTO:
2061 SOURCING_LNUM = iptr->isn_lnum;
2062 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2063 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002064 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002065 break;
2066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 // store number in local variable
2068 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002069 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 clear_tv(tv);
2071 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002072 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 break;
2074
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002075 // store value in list or dict variable
2076 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002077 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002078 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002079 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002080 typval_T *tv_dest = STACK_TV_BOT(-1);
2081 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002082
Bram Moolenaar752fc692021-01-04 21:57:11 +01002083 // Stack contains:
2084 // -3 value to be stored
2085 // -2 index
2086 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002087 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002088 SOURCING_LNUM = iptr->isn_lnum;
2089 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002090 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002091 dest_type = tv_dest->v_type;
2092 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002093 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002094 else if (dest_type == VAR_LIST
2095 && tv_idx->v_type != VAR_NUMBER)
2096 {
2097 emsg(_(e_number_exp));
2098 status = FAIL;
2099 }
2100 }
2101 else if (dest_type != tv_dest->v_type)
2102 {
2103 // just in case, should be OK
2104 semsg(_(e_expected_str_but_got_str),
2105 vartype_name(dest_type),
2106 vartype_name(tv_dest->v_type));
2107 status = FAIL;
2108 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002109
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002110 if (status == OK && dest_type == VAR_LIST)
2111 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002112 long lidx = (long)tv_idx->vval.v_number;
2113 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002114
2115 if (list == NULL)
2116 {
2117 emsg(_(e_list_not_set));
2118 goto on_error;
2119 }
2120 if (lidx < 0 && list->lv_len + lidx >= 0)
2121 // negative index is relative to the end
2122 lidx = list->lv_len + lidx;
2123 if (lidx < 0 || lidx > list->lv_len)
2124 {
2125 semsg(_(e_listidx), lidx);
2126 goto on_error;
2127 }
2128 if (lidx < list->lv_len)
2129 {
2130 listitem_T *li = list_find(list, lidx);
2131
2132 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002133 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002134 goto on_error;
2135 // overwrite existing list item
2136 clear_tv(&li->li_tv);
2137 li->li_tv = *tv;
2138 }
2139 else
2140 {
2141 if (error_if_locked(list->lv_lock,
2142 e_cannot_change_list))
2143 goto on_error;
2144 // append to list, only fails when out of memory
2145 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002146 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002147 clear_tv(tv);
2148 }
2149 }
2150 else if (status == OK && dest_type == VAR_DICT)
2151 {
2152 char_u *key = tv_idx->vval.v_string;
2153 dict_T *dict = tv_dest->vval.v_dict;
2154 dictitem_T *di;
2155
2156 SOURCING_LNUM = iptr->isn_lnum;
2157 if (dict == NULL)
2158 {
2159 emsg(_(e_dictionary_not_set));
2160 goto on_error;
2161 }
2162 if (key == NULL)
2163 key = (char_u *)"";
2164 di = dict_find(dict, key, -1);
2165 if (di != NULL)
2166 {
2167 if (error_if_locked(di->di_tv.v_lock,
2168 e_cannot_change_dict_item))
2169 goto on_error;
2170 // overwrite existing value
2171 clear_tv(&di->di_tv);
2172 di->di_tv = *tv;
2173 }
2174 else
2175 {
2176 if (error_if_locked(dict->dv_lock,
2177 e_cannot_change_dict))
2178 goto on_error;
2179 // add to dict, only fails when out of memory
2180 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002181 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002182 clear_tv(tv);
2183 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002184 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002185 else if (status == OK && dest_type == VAR_BLOB)
2186 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002187 long lidx = (long)tv_idx->vval.v_number;
2188 blob_T *blob = tv_dest->vval.v_blob;
2189 varnumber_T nr;
2190 int error = FALSE;
2191 int len;
2192
2193 if (blob == NULL)
2194 {
2195 emsg(_(e_blob_not_set));
2196 goto on_error;
2197 }
2198 len = blob_len(blob);
2199 if (lidx < 0 && len + lidx >= 0)
2200 // negative index is relative to the end
2201 lidx = len + lidx;
2202
2203 // Can add one byte at the end.
2204 if (lidx < 0 || lidx > len)
2205 {
2206 semsg(_(e_blobidx), lidx);
2207 goto on_error;
2208 }
2209 if (value_check_lock(blob->bv_lock,
2210 (char_u *)"blob", FALSE))
2211 goto on_error;
2212 nr = tv_get_number_chk(tv, &error);
2213 if (error)
2214 goto on_error;
2215 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002216 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002217 else
2218 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002219 status = FAIL;
2220 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002221 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002222
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002223 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002224 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002225 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002226 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002227 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002228 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002229 goto on_error;
2230 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002231 }
2232 break;
2233
Bram Moolenaar68452172021-04-12 21:21:02 +02002234 // store value in blob range
2235 case ISN_STORERANGE:
2236 {
2237 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2238 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2239 typval_T *tv_dest = STACK_TV_BOT(-1);
2240 int status = OK;
2241
2242 // Stack contains:
2243 // -4 value to be stored
2244 // -3 first index or "none"
2245 // -2 second index or "none"
2246 // -1 destination blob
2247 tv = STACK_TV_BOT(-4);
2248 if (tv_dest->v_type != VAR_BLOB)
2249 {
2250 status = FAIL;
2251 emsg(_(e_blob_required));
2252 }
2253 else
2254 {
2255 varnumber_T n1;
2256 varnumber_T n2;
2257 int error = FALSE;
2258
2259 n1 = tv_get_number_chk(tv_idx1, &error);
2260 if (error)
2261 status = FAIL;
2262 else
2263 {
2264 if (tv_idx2->v_type == VAR_SPECIAL
2265 && tv_idx2->vval.v_number == VVAL_NONE)
2266 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2267 else
2268 n2 = tv_get_number_chk(tv_idx2, &error);
2269 if (error)
2270 status = FAIL;
2271 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002272 {
2273 long bloblen = blob_len(tv_dest->vval.v_blob);
2274
2275 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002276 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002277 || check_blob_range(bloblen,
2278 n1, n2, FALSE) == FAIL)
2279 status = FAIL;
2280 else
2281 status = blob_set_range(
2282 tv_dest->vval.v_blob, n1, n2, tv);
2283 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002284 }
2285 }
2286
2287 clear_tv(tv_idx1);
2288 clear_tv(tv_idx2);
2289 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002290 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002291 clear_tv(tv);
2292
2293 if (status == FAIL)
2294 goto on_error;
2295 }
2296 break;
2297
Bram Moolenaar0186e582021-01-10 18:33:11 +01002298 // load or store variable or argument from outer scope
2299 case ISN_LOADOUTER:
2300 case ISN_STOREOUTER:
2301 {
2302 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002303 outer_T *outer = ectx->ec_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002304
2305 while (depth > 1 && outer != NULL)
2306 {
2307 outer = outer->out_up;
2308 --depth;
2309 }
2310 if (outer == NULL)
2311 {
2312 SOURCING_LNUM = iptr->isn_lnum;
2313 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002314 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002315 }
2316 tv = ((typval_T *)outer->out_stack->ga_data)
2317 + outer->out_frame_idx + STACK_FRAME_SIZE
2318 + iptr->isn_arg.outer.outer_idx;
2319 if (iptr->isn_type == ISN_LOADOUTER)
2320 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002321 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002322 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002323 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002324 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002325 }
2326 else
2327 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002328 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002329 clear_tv(tv);
2330 *tv = *STACK_TV_BOT(0);
2331 }
2332 }
2333 break;
2334
Bram Moolenaar752fc692021-01-04 21:57:11 +01002335 // unlet item in list or dict variable
2336 case ISN_UNLETINDEX:
2337 {
2338 typval_T *tv_idx = STACK_TV_BOT(-2);
2339 typval_T *tv_dest = STACK_TV_BOT(-1);
2340 int status = OK;
2341
2342 // Stack contains:
2343 // -2 index
2344 // -1 dict or list
2345 if (tv_dest->v_type == VAR_DICT)
2346 {
2347 // unlet a dict item, index must be a string
2348 if (tv_idx->v_type != VAR_STRING)
2349 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002350 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002351 semsg(_(e_expected_str_but_got_str),
2352 vartype_name(VAR_STRING),
2353 vartype_name(tv_idx->v_type));
2354 status = FAIL;
2355 }
2356 else
2357 {
2358 dict_T *d = tv_dest->vval.v_dict;
2359 char_u *key = tv_idx->vval.v_string;
2360 dictitem_T *di = NULL;
2361
2362 if (key == NULL)
2363 key = (char_u *)"";
2364 if (d != NULL)
2365 di = dict_find(d, key, (int)STRLEN(key));
2366 if (di == NULL)
2367 {
2368 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002369 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002370 semsg(_(e_dictkey), key);
2371 status = FAIL;
2372 }
2373 else
2374 {
2375 // TODO: check for dict or item locked
2376 dictitem_remove(d, di);
2377 }
2378 }
2379 }
2380 else if (tv_dest->v_type == VAR_LIST)
2381 {
2382 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002383 SOURCING_LNUM = iptr->isn_lnum;
2384 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002385 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002386 status = FAIL;
2387 }
2388 else
2389 {
2390 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002391 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002392 listitem_T *li = NULL;
2393
2394 li = list_find(l, n);
2395 if (li == NULL)
2396 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002397 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002398 semsg(_(e_listidx), n);
2399 status = FAIL;
2400 }
2401 else
2402 // TODO: check for list or item locked
2403 listitem_remove(l, li);
2404 }
2405 }
2406 else
2407 {
2408 status = FAIL;
2409 semsg(_(e_cannot_index_str),
2410 vartype_name(tv_dest->v_type));
2411 }
2412
2413 clear_tv(tv_idx);
2414 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002415 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002416 if (status == FAIL)
2417 goto on_error;
2418 }
2419 break;
2420
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002421 // unlet range of items in list variable
2422 case ISN_UNLETRANGE:
2423 {
2424 // Stack contains:
2425 // -3 index1
2426 // -2 index2
2427 // -1 dict or list
2428 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2429 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2430 typval_T *tv_dest = STACK_TV_BOT(-1);
2431 int status = OK;
2432
2433 if (tv_dest->v_type == VAR_LIST)
2434 {
2435 // indexes must be a number
2436 SOURCING_LNUM = iptr->isn_lnum;
2437 if (check_for_number(tv_idx1) == FAIL
2438 || check_for_number(tv_idx2) == FAIL)
2439 {
2440 status = FAIL;
2441 }
2442 else
2443 {
2444 list_T *l = tv_dest->vval.v_list;
2445 long n1 = (long)tv_idx1->vval.v_number;
2446 long n2 = (long)tv_idx2->vval.v_number;
2447 listitem_T *li;
2448
2449 li = list_find_index(l, &n1);
2450 if (li == NULL
2451 || list_unlet_range(l, li, NULL, n1,
2452 TRUE, n2) == FAIL)
2453 status = FAIL;
2454 }
2455 }
2456 else
2457 {
2458 status = FAIL;
2459 SOURCING_LNUM = iptr->isn_lnum;
2460 semsg(_(e_cannot_index_str),
2461 vartype_name(tv_dest->v_type));
2462 }
2463
2464 clear_tv(tv_idx1);
2465 clear_tv(tv_idx2);
2466 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002467 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002468 if (status == FAIL)
2469 goto on_error;
2470 }
2471 break;
2472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 // push constant
2474 case ISN_PUSHNR:
2475 case ISN_PUSHBOOL:
2476 case ISN_PUSHSPEC:
2477 case ISN_PUSHF:
2478 case ISN_PUSHS:
2479 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002480 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002481 case ISN_PUSHCHANNEL:
2482 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002483 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002484 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002486 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002487 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 switch (iptr->isn_type)
2489 {
2490 case ISN_PUSHNR:
2491 tv->v_type = VAR_NUMBER;
2492 tv->vval.v_number = iptr->isn_arg.number;
2493 break;
2494 case ISN_PUSHBOOL:
2495 tv->v_type = VAR_BOOL;
2496 tv->vval.v_number = iptr->isn_arg.number;
2497 break;
2498 case ISN_PUSHSPEC:
2499 tv->v_type = VAR_SPECIAL;
2500 tv->vval.v_number = iptr->isn_arg.number;
2501 break;
2502#ifdef FEAT_FLOAT
2503 case ISN_PUSHF:
2504 tv->v_type = VAR_FLOAT;
2505 tv->vval.v_float = iptr->isn_arg.fnumber;
2506 break;
2507#endif
2508 case ISN_PUSHBLOB:
2509 blob_copy(iptr->isn_arg.blob, tv);
2510 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002511 case ISN_PUSHFUNC:
2512 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002513 if (iptr->isn_arg.string == NULL)
2514 tv->vval.v_string = NULL;
2515 else
2516 tv->vval.v_string =
2517 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002518 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002519 case ISN_PUSHCHANNEL:
2520#ifdef FEAT_JOB_CHANNEL
2521 tv->v_type = VAR_CHANNEL;
2522 tv->vval.v_channel = iptr->isn_arg.channel;
2523 if (tv->vval.v_channel != NULL)
2524 ++tv->vval.v_channel->ch_refcount;
2525#endif
2526 break;
2527 case ISN_PUSHJOB:
2528#ifdef FEAT_JOB_CHANNEL
2529 tv->v_type = VAR_JOB;
2530 tv->vval.v_job = iptr->isn_arg.job;
2531 if (tv->vval.v_job != NULL)
2532 ++tv->vval.v_job->jv_refcount;
2533#endif
2534 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 default:
2536 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002537 tv->vval.v_string = vim_strsave(
2538 iptr->isn_arg.string == NULL
2539 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 }
2541 break;
2542
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002543 case ISN_UNLET:
2544 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2545 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002546 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002547 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002548 case ISN_UNLETENV:
2549 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2550 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002551
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002552 case ISN_LOCKCONST:
2553 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2554 break;
2555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 // create a list from items on the stack; uses a single allocation
2557 // for the list header and the items
2558 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002559 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002560 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 break;
2562
2563 // create a dict from items on the stack
2564 case ISN_NEWDICT:
2565 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002566 int count = iptr->isn_arg.number;
2567 dict_T *dict = dict_alloc();
2568 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002569 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002570 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571
2572 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002573 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 for (idx = 0; idx < count; ++idx)
2575 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002576 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002578 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002579 key = tv->vval.v_string == NULL
2580 ? (char_u *)"" : tv->vval.v_string;
2581 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002582 if (item != NULL)
2583 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002584 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002585 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002586 dict_unref(dict);
2587 goto on_error;
2588 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002589 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 clear_tv(tv);
2591 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002592 {
2593 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002594 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2597 item->di_tv.v_lock = 0;
2598 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002599 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002600 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002601 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002602 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 }
2605
2606 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002607 ectx->ec_stack.ga_len -= 2 * count - 1;
2608 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002609 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002611 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 tv = STACK_TV_BOT(-1);
2613 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002614 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 tv->vval.v_dict = dict;
2616 ++dict->dv_refcount;
2617 }
2618 break;
2619
2620 // call a :def function
2621 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002622 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002623 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2624 NULL,
2625 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002626 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002627 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 break;
2629
2630 // call a builtin function
2631 case ISN_BCALL:
2632 SOURCING_LNUM = iptr->isn_lnum;
2633 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2634 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002635 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002636 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 break;
2638
2639 // call a funcref or partial
2640 case ISN_PCALL:
2641 {
2642 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2643 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002644 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645
2646 SOURCING_LNUM = iptr->isn_lnum;
2647 if (pfunc->cpf_top)
2648 {
2649 // funcref is above the arguments
2650 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2651 }
2652 else
2653 {
2654 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002655 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002656 partial_tv = *STACK_TV_BOT(0);
2657 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002659 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002660 if (tv == &partial_tv)
2661 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002663 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 }
2665 break;
2666
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002667 case ISN_PCALL_END:
2668 // PCALL finished, arguments have been consumed and replaced by
2669 // the return value. Now clear the funcref from the stack,
2670 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002671 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002672 clear_tv(STACK_TV_BOT(-1));
2673 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2674 break;
2675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 // call a user defined function or funcref/partial
2677 case ISN_UCALL:
2678 {
2679 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2680
2681 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002682 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002683 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002684 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 }
2686 break;
2687
2688 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002689 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002690 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002691 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002692 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002693 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002694 tv->v_type = VAR_NUMBER;
2695 tv->vval.v_number = 0;
2696 tv->v_lock = 0;
2697 // FALLTHROUGH
2698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 case ISN_RETURN:
2700 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002701 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002702 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002703
2704 if (trystack->ga_len > 0)
2705 trycmd = ((trycmd_T *)trystack->ga_data)
2706 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002707 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002708 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002710 // jump to ":finally" or ":endtry"
2711 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002712 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002713 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002714 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 trycmd->tcd_return = TRUE;
2716 }
2717 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002718 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 }
2720 break;
2721
2722 // push a function reference to a compiled function
2723 case ISN_FUNCREF:
2724 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002725 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2726 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2727 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002730 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002731 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002732 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002733 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002734 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002735 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002736 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002737 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002738 goto theend;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002741 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 tv->vval.v_partial = pt;
2743 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002744 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 }
2746 break;
2747
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002748 // Create a global function from a lambda.
2749 case ISN_NEWFUNC:
2750 {
2751 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2752
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002753 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002754 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002755 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002756 }
2757 break;
2758
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002759 // List functions
2760 case ISN_DEF:
2761 if (iptr->isn_arg.string == NULL)
2762 list_functions(NULL);
2763 else
2764 {
2765 exarg_T ea;
2766
2767 CLEAR_FIELD(ea);
2768 ea.cmd = ea.arg = iptr->isn_arg.string;
2769 define_function(&ea, NULL);
2770 }
2771 break;
2772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 // jump if a condition is met
2774 case ISN_JUMP:
2775 {
2776 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002777 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 int jump = TRUE;
2779
2780 if (when != JUMP_ALWAYS)
2781 {
2782 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002783 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002784 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002785 || when == JUMP_IF_COND_TRUE)
2786 {
2787 SOURCING_LNUM = iptr->isn_lnum;
2788 jump = tv_get_bool_chk(tv, &error);
2789 if (error)
2790 goto on_error;
2791 }
2792 else
2793 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002795 || when == JUMP_AND_KEEP_IF_FALSE
2796 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002798 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 {
2800 // drop the value from the stack
2801 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002802 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 }
2804 }
2805 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002806 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 }
2808 break;
2809
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002810 // Jump if an argument with a default value was already set and not
2811 // v:none.
2812 case ISN_JUMP_IF_ARG_SET:
2813 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2814 if (tv->v_type != VAR_UNKNOWN
2815 && !(tv->v_type == VAR_SPECIAL
2816 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002817 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002818 break;
2819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 // top of a for loop
2821 case ISN_FOR:
2822 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002823 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 typval_T *idxtv =
2825 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2826
Bram Moolenaar4c137212021-04-19 16:48:48 +02002827 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002828 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002829 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002830 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002831 list_T *list = ltv->vval.v_list;
2832
2833 // push the next item from the list
2834 ++idxtv->vval.v_number;
2835 if (list == NULL
2836 || idxtv->vval.v_number >= list->lv_len)
2837 {
2838 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002839 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2840 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002841 }
2842 else if (list->lv_first == &range_list_item)
2843 {
2844 // non-materialized range() list
2845 tv = STACK_TV_BOT(0);
2846 tv->v_type = VAR_NUMBER;
2847 tv->v_lock = 0;
2848 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002850 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002851 }
2852 else
2853 {
2854 listitem_T *li = list_find(list,
2855 idxtv->vval.v_number);
2856
2857 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002858 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002859 }
2860 }
2861 else if (ltv->v_type == VAR_STRING)
2862 {
2863 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002864
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002865 // The index is for the last byte of the previous
2866 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002867 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002868 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002869 {
2870 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002871 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2872 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002873 }
2874 else
2875 {
2876 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2877
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002878 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002879 tv = STACK_TV_BOT(0);
2880 tv->v_type = VAR_STRING;
2881 tv->vval.v_string = vim_strnsave(
2882 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002883 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002884 idxtv->vval.v_number += clen - 1;
2885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002887 else if (ltv->v_type == VAR_BLOB)
2888 {
2889 blob_T *blob = ltv->vval.v_blob;
2890
2891 // When we get here the first time make a copy of the
2892 // blob, so that the iteration still works when it is
2893 // changed.
2894 if (idxtv->vval.v_number == -1 && blob != NULL)
2895 {
2896 blob_copy(blob, ltv);
2897 blob_unref(blob);
2898 blob = ltv->vval.v_blob;
2899 }
2900
2901 // The index is for the previous byte.
2902 ++idxtv->vval.v_number;
2903 if (blob == NULL
2904 || idxtv->vval.v_number >= blob_len(blob))
2905 {
2906 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002907 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2908 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002909 }
2910 else
2911 {
2912 // Push the next byte from the blob.
2913 tv = STACK_TV_BOT(0);
2914 tv->v_type = VAR_NUMBER;
2915 tv->vval.v_number = blob_get(blob,
2916 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002917 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002918 }
2919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 else
2921 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002922 semsg(_(e_for_loop_on_str_not_supported),
2923 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002924 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 }
2926 }
2927 break;
2928
2929 // start of ":try" block
2930 case ISN_TRY:
2931 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002932 trycmd_T *trycmd = NULL;
2933
Bram Moolenaar4c137212021-04-19 16:48:48 +02002934 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002935 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
2937 + ectx->ec_trystack.ga_len;
2938 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002940 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002941 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
2942 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002943 trycmd->tcd_catch_idx =
2944 iptr->isn_arg.try.try_ref->try_catch;
2945 trycmd->tcd_finally_idx =
2946 iptr->isn_arg.try.try_ref->try_finally;
2947 trycmd->tcd_endtry_idx =
2948 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 }
2950 break;
2951
2952 case ISN_PUSHEXC:
2953 if (current_exception == NULL)
2954 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002955 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002957 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002959 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002960 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002962 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002964 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 tv->vval.v_string = vim_strsave(
2966 (char_u *)current_exception->value);
2967 break;
2968
2969 case ISN_CATCH:
2970 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002971 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972
Bram Moolenaar4c137212021-04-19 16:48:48 +02002973 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 if (trystack->ga_len > 0)
2975 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002976 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 + trystack->ga_len - 1;
2978 trycmd->tcd_caught = TRUE;
2979 }
2980 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002981 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 catch_exception(current_exception);
2983 }
2984 break;
2985
Bram Moolenaarc150c092021-02-13 15:02:46 +01002986 case ISN_TRYCONT:
2987 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002988 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002989 trycont_T *trycont = &iptr->isn_arg.trycont;
2990 int i;
2991 trycmd_T *trycmd;
2992 int iidx = trycont->tct_where;
2993
2994 if (trystack->ga_len < trycont->tct_levels)
2995 {
2996 siemsg("TRYCONT: expected %d levels, found %d",
2997 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002998 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002999 }
3000 // Make :endtry jump to any outer try block and the last
3001 // :endtry inside the loop to the loop start.
3002 for (i = trycont->tct_levels; i > 0; --i)
3003 {
3004 trycmd = ((trycmd_T *)trystack->ga_data)
3005 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003006 // Add one to tcd_cont to be able to jump to
3007 // instruction with index zero.
3008 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003009 iidx = trycmd->tcd_finally_idx == 0
3010 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003011 }
3012 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003013 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003014 }
3015 break;
3016
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003017 case ISN_FINALLY:
3018 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003019 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003020 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3021 + trystack->ga_len - 1;
3022
3023 // Reset the index to avoid a return statement jumps here
3024 // again.
3025 trycmd->tcd_finally_idx = 0;
3026 break;
3027 }
3028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 // end of ":try" block
3030 case ISN_ENDTRY:
3031 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003032 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033
3034 if (trystack->ga_len > 0)
3035 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003036 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 --trystack->ga_len;
3039 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003040 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 trycmd = ((trycmd_T *)trystack->ga_data)
3042 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003043 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 {
3045 // discard the exception
3046 if (caught_stack == current_exception)
3047 caught_stack = caught_stack->caught;
3048 discard_current_exception();
3049 }
3050
3051 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003052 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003053
Bram Moolenaar4c137212021-04-19 16:48:48 +02003054 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003055 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003056 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003057 clear_tv(STACK_TV_BOT(0));
3058 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003059 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003060 // handling :continue: jump to outer try block or
3061 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003062 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 }
3064 }
3065 break;
3066
3067 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003068 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003069 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003070
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003071 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3072 {
3073 // throwing an exception while using "silent!" causes
3074 // the function to abort but not display an error.
3075 tv = STACK_TV_BOT(-1);
3076 clear_tv(tv);
3077 tv->v_type = VAR_NUMBER;
3078 tv->vval.v_number = 0;
3079 goto done;
3080 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003081 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003082 tv = STACK_TV_BOT(0);
3083 if (tv->vval.v_string == NULL
3084 || *skipwhite(tv->vval.v_string) == NUL)
3085 {
3086 vim_free(tv->vval.v_string);
3087 SOURCING_LNUM = iptr->isn_lnum;
3088 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003089 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003090 }
3091
3092 // Inside a "catch" we need to first discard the caught
3093 // exception.
3094 if (trystack->ga_len > 0)
3095 {
3096 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3097 + trystack->ga_len - 1;
3098 if (trycmd->tcd_caught && current_exception != NULL)
3099 {
3100 // discard the exception
3101 if (caught_stack == current_exception)
3102 caught_stack = caught_stack->caught;
3103 discard_current_exception();
3104 trycmd->tcd_caught = FALSE;
3105 }
3106 }
3107
3108 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3109 == FAIL)
3110 {
3111 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003112 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003113 }
3114 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 break;
3117
3118 // compare with special values
3119 case ISN_COMPAREBOOL:
3120 case ISN_COMPARESPECIAL:
3121 {
3122 typval_T *tv1 = STACK_TV_BOT(-2);
3123 typval_T *tv2 = STACK_TV_BOT(-1);
3124 varnumber_T arg1 = tv1->vval.v_number;
3125 varnumber_T arg2 = tv2->vval.v_number;
3126 int res;
3127
3128 switch (iptr->isn_arg.op.op_type)
3129 {
3130 case EXPR_EQUAL: res = arg1 == arg2; break;
3131 case EXPR_NEQUAL: res = arg1 != arg2; break;
3132 default: res = 0; break;
3133 }
3134
Bram Moolenaar4c137212021-04-19 16:48:48 +02003135 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 tv1->v_type = VAR_BOOL;
3137 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3138 }
3139 break;
3140
3141 // Operation with two number arguments
3142 case ISN_OPNR:
3143 case ISN_COMPARENR:
3144 {
3145 typval_T *tv1 = STACK_TV_BOT(-2);
3146 typval_T *tv2 = STACK_TV_BOT(-1);
3147 varnumber_T arg1 = tv1->vval.v_number;
3148 varnumber_T arg2 = tv2->vval.v_number;
3149 varnumber_T res;
3150
3151 switch (iptr->isn_arg.op.op_type)
3152 {
3153 case EXPR_MULT: res = arg1 * arg2; break;
3154 case EXPR_DIV: res = arg1 / arg2; break;
3155 case EXPR_REM: res = arg1 % arg2; break;
3156 case EXPR_SUB: res = arg1 - arg2; break;
3157 case EXPR_ADD: res = arg1 + arg2; break;
3158
3159 case EXPR_EQUAL: res = arg1 == arg2; break;
3160 case EXPR_NEQUAL: res = arg1 != arg2; break;
3161 case EXPR_GREATER: res = arg1 > arg2; break;
3162 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3163 case EXPR_SMALLER: res = arg1 < arg2; break;
3164 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3165 default: res = 0; break;
3166 }
3167
Bram Moolenaar4c137212021-04-19 16:48:48 +02003168 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 if (iptr->isn_type == ISN_COMPARENR)
3170 {
3171 tv1->v_type = VAR_BOOL;
3172 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3173 }
3174 else
3175 tv1->vval.v_number = res;
3176 }
3177 break;
3178
3179 // Computation with two float arguments
3180 case ISN_OPFLOAT:
3181 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003182#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 {
3184 typval_T *tv1 = STACK_TV_BOT(-2);
3185 typval_T *tv2 = STACK_TV_BOT(-1);
3186 float_T arg1 = tv1->vval.v_float;
3187 float_T arg2 = tv2->vval.v_float;
3188 float_T res = 0;
3189 int cmp = FALSE;
3190
3191 switch (iptr->isn_arg.op.op_type)
3192 {
3193 case EXPR_MULT: res = arg1 * arg2; break;
3194 case EXPR_DIV: res = arg1 / arg2; break;
3195 case EXPR_SUB: res = arg1 - arg2; break;
3196 case EXPR_ADD: res = arg1 + arg2; break;
3197
3198 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3199 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3200 case EXPR_GREATER: cmp = arg1 > arg2; break;
3201 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3202 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3203 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3204 default: cmp = 0; break;
3205 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003206 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 if (iptr->isn_type == ISN_COMPAREFLOAT)
3208 {
3209 tv1->v_type = VAR_BOOL;
3210 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3211 }
3212 else
3213 tv1->vval.v_float = res;
3214 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003215#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 break;
3217
3218 case ISN_COMPARELIST:
3219 {
3220 typval_T *tv1 = STACK_TV_BOT(-2);
3221 typval_T *tv2 = STACK_TV_BOT(-1);
3222 list_T *arg1 = tv1->vval.v_list;
3223 list_T *arg2 = tv2->vval.v_list;
3224 int cmp = FALSE;
3225 int ic = iptr->isn_arg.op.op_ic;
3226
3227 switch (iptr->isn_arg.op.op_type)
3228 {
3229 case EXPR_EQUAL: cmp =
3230 list_equal(arg1, arg2, ic, FALSE); break;
3231 case EXPR_NEQUAL: cmp =
3232 !list_equal(arg1, arg2, ic, FALSE); break;
3233 case EXPR_IS: cmp = arg1 == arg2; break;
3234 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3235 default: cmp = 0; break;
3236 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003237 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 clear_tv(tv1);
3239 clear_tv(tv2);
3240 tv1->v_type = VAR_BOOL;
3241 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3242 }
3243 break;
3244
3245 case ISN_COMPAREBLOB:
3246 {
3247 typval_T *tv1 = STACK_TV_BOT(-2);
3248 typval_T *tv2 = STACK_TV_BOT(-1);
3249 blob_T *arg1 = tv1->vval.v_blob;
3250 blob_T *arg2 = tv2->vval.v_blob;
3251 int cmp = FALSE;
3252
3253 switch (iptr->isn_arg.op.op_type)
3254 {
3255 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3256 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3257 case EXPR_IS: cmp = arg1 == arg2; break;
3258 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3259 default: cmp = 0; break;
3260 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003261 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 clear_tv(tv1);
3263 clear_tv(tv2);
3264 tv1->v_type = VAR_BOOL;
3265 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3266 }
3267 break;
3268
3269 // TODO: handle separately
3270 case ISN_COMPARESTRING:
3271 case ISN_COMPAREDICT:
3272 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 case ISN_COMPAREANY:
3274 {
3275 typval_T *tv1 = STACK_TV_BOT(-2);
3276 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003277 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 int ic = iptr->isn_arg.op.op_ic;
3279
Bram Moolenaareb26f432020-09-14 16:50:05 +02003280 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003281 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003283 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 }
3285 break;
3286
3287 case ISN_ADDLIST:
3288 case ISN_ADDBLOB:
3289 {
3290 typval_T *tv1 = STACK_TV_BOT(-2);
3291 typval_T *tv2 = STACK_TV_BOT(-1);
3292
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003293 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 if (iptr->isn_type == ISN_ADDLIST)
3295 eval_addlist(tv1, tv2);
3296 else
3297 eval_addblob(tv1, tv2);
3298 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003299 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 }
3301 break;
3302
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003303 case ISN_LISTAPPEND:
3304 {
3305 typval_T *tv1 = STACK_TV_BOT(-2);
3306 typval_T *tv2 = STACK_TV_BOT(-1);
3307 list_T *l = tv1->vval.v_list;
3308
3309 // add an item to a list
3310 if (l == NULL)
3311 {
3312 SOURCING_LNUM = iptr->isn_lnum;
3313 emsg(_(e_cannot_add_to_null_list));
3314 goto on_error;
3315 }
3316 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003317 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003318 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003319 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003320 }
3321 break;
3322
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003323 case ISN_BLOBAPPEND:
3324 {
3325 typval_T *tv1 = STACK_TV_BOT(-2);
3326 typval_T *tv2 = STACK_TV_BOT(-1);
3327 blob_T *b = tv1->vval.v_blob;
3328 int error = FALSE;
3329 varnumber_T n;
3330
3331 // add a number to a blob
3332 if (b == NULL)
3333 {
3334 SOURCING_LNUM = iptr->isn_lnum;
3335 emsg(_(e_cannot_add_to_null_blob));
3336 goto on_error;
3337 }
3338 n = tv_get_number_chk(tv2, &error);
3339 if (error)
3340 goto on_error;
3341 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003342 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003343 }
3344 break;
3345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 // Computation with two arguments of unknown type
3347 case ISN_OPANY:
3348 {
3349 typval_T *tv1 = STACK_TV_BOT(-2);
3350 typval_T *tv2 = STACK_TV_BOT(-1);
3351 varnumber_T n1, n2;
3352#ifdef FEAT_FLOAT
3353 float_T f1 = 0, f2 = 0;
3354#endif
3355 int error = FALSE;
3356
3357 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3358 {
3359 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3360 {
3361 eval_addlist(tv1, tv2);
3362 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003363 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 break;
3365 }
3366 else if (tv1->v_type == VAR_BLOB
3367 && tv2->v_type == VAR_BLOB)
3368 {
3369 eval_addblob(tv1, tv2);
3370 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003371 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 break;
3373 }
3374 }
3375#ifdef FEAT_FLOAT
3376 if (tv1->v_type == VAR_FLOAT)
3377 {
3378 f1 = tv1->vval.v_float;
3379 n1 = 0;
3380 }
3381 else
3382#endif
3383 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003384 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 n1 = tv_get_number_chk(tv1, &error);
3386 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003387 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388#ifdef FEAT_FLOAT
3389 if (tv2->v_type == VAR_FLOAT)
3390 f1 = n1;
3391#endif
3392 }
3393#ifdef FEAT_FLOAT
3394 if (tv2->v_type == VAR_FLOAT)
3395 {
3396 f2 = tv2->vval.v_float;
3397 n2 = 0;
3398 }
3399 else
3400#endif
3401 {
3402 n2 = tv_get_number_chk(tv2, &error);
3403 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003404 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405#ifdef FEAT_FLOAT
3406 if (tv1->v_type == VAR_FLOAT)
3407 f2 = n2;
3408#endif
3409 }
3410#ifdef FEAT_FLOAT
3411 // if there is a float on either side the result is a float
3412 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3413 {
3414 switch (iptr->isn_arg.op.op_type)
3415 {
3416 case EXPR_MULT: f1 = f1 * f2; break;
3417 case EXPR_DIV: f1 = f1 / f2; break;
3418 case EXPR_SUB: f1 = f1 - f2; break;
3419 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003420 default: SOURCING_LNUM = iptr->isn_lnum;
3421 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003422 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 }
3424 clear_tv(tv1);
3425 clear_tv(tv2);
3426 tv1->v_type = VAR_FLOAT;
3427 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003428 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 }
3430 else
3431#endif
3432 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003433 int failed = FALSE;
3434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 switch (iptr->isn_arg.op.op_type)
3436 {
3437 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003438 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3439 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003440 goto on_error;
3441 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 case EXPR_SUB: n1 = n1 - n2; break;
3443 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003444 default: n1 = num_modulus(n1, n2, &failed);
3445 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003446 goto on_error;
3447 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 }
3449 clear_tv(tv1);
3450 clear_tv(tv2);
3451 tv1->v_type = VAR_NUMBER;
3452 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003453 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 }
3455 }
3456 break;
3457
3458 case ISN_CONCAT:
3459 {
3460 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3461 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3462 char_u *res;
3463
3464 res = concat_str(str1, str2);
3465 clear_tv(STACK_TV_BOT(-2));
3466 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003467 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 STACK_TV_BOT(-1)->vval.v_string = res;
3469 }
3470 break;
3471
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003472 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003473 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003474 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003475 int is_slice = iptr->isn_type == ISN_STRSLICE;
3476 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003477 char_u *res;
3478
3479 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003480 // string slice: string is at stack-3, first index at
3481 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003482 if (is_slice)
3483 {
3484 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003485 n1 = tv->vval.v_number;
3486 }
3487
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003488 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003489 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003490
Bram Moolenaar4c137212021-04-19 16:48:48 +02003491 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003492 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003493 if (is_slice)
3494 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003495 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003496 else
3497 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003498 // single character (including composing characters).
3499 // If the index is too big or negative the result is
3500 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003501 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003502 vim_free(tv->vval.v_string);
3503 tv->vval.v_string = res;
3504 }
3505 break;
3506
3507 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003508 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003509 case ISN_BLOBINDEX:
3510 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003512 int is_slice = iptr->isn_type == ISN_LISTSLICE
3513 || iptr->isn_type == ISN_BLOBSLICE;
3514 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3515 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003516 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003517 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518
3519 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003520 // list slice: list is at stack-3, indexes at stack-2 and
3521 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003522 // Same for blob.
3523 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524
3525 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003526 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003528
3529 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 {
Bram Moolenaared591872020-08-15 22:14:53 +02003531 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003532 n1 = tv->vval.v_number;
3533 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 }
Bram Moolenaared591872020-08-15 22:14:53 +02003535
Bram Moolenaar4c137212021-04-19 16:48:48 +02003536 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003537 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003538 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003539 if (is_blob)
3540 {
3541 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3542 n1, n2, FALSE, tv) == FAIL)
3543 goto on_error;
3544 }
3545 else
3546 {
3547 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3548 n1, n2, FALSE, tv, TRUE) == FAIL)
3549 goto on_error;
3550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 }
3552 break;
3553
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003554 case ISN_ANYINDEX:
3555 case ISN_ANYSLICE:
3556 {
3557 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3558 typval_T *var1, *var2;
3559 int res;
3560
3561 // index: composite is at stack-2, index at stack-1
3562 // slice: composite is at stack-3, indexes at stack-2 and
3563 // stack-1
3564 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003565 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003566 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3567 goto on_error;
3568 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3569 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003570 res = eval_index_inner(tv, is_slice, var1, var2,
3571 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003572 clear_tv(var1);
3573 if (is_slice)
3574 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003575 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003576 if (res == FAIL)
3577 goto on_error;
3578 }
3579 break;
3580
Bram Moolenaar9af78762020-06-16 11:34:42 +02003581 case ISN_SLICE:
3582 {
3583 list_T *list;
3584 int count = iptr->isn_arg.number;
3585
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003586 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003587 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003588 list = tv->vval.v_list;
3589
3590 // no error for short list, expect it to be checked earlier
3591 if (list != NULL && list->lv_len >= count)
3592 {
3593 list_T *newlist = list_slice(list,
3594 count, list->lv_len - 1);
3595
3596 if (newlist != NULL)
3597 {
3598 list_unref(list);
3599 tv->vval.v_list = newlist;
3600 ++newlist->lv_refcount;
3601 }
3602 }
3603 }
3604 break;
3605
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003606 case ISN_GETITEM:
3607 {
3608 listitem_T *li;
3609 int index = iptr->isn_arg.number;
3610
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003611 // Get list item: list is at stack-1, push item.
3612 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003613 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003614 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003615
Bram Moolenaar4c137212021-04-19 16:48:48 +02003616 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003617 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003618 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003619 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003620
3621 // Useful when used in unpack assignment. Reset at
3622 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003623 ectx->ec_where.wt_index = index + 1;
3624 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003625 }
3626 break;
3627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 case ISN_MEMBER:
3629 {
3630 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003631 char_u *key;
3632 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003633 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003634
3635 // dict member: dict is at stack-2, key at stack-1
3636 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003637 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003638 dict = tv->vval.v_dict;
3639
3640 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003641 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003642 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003643 if (key == NULL)
3644 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003645
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003646 if ((di = dict_find(dict, key, -1)) == NULL)
3647 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003648 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003649 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003650
3651 // If :silent! is used we will continue, make sure the
3652 // stack contents makes sense.
3653 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003654 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003655 tv = STACK_TV_BOT(-1);
3656 clear_tv(tv);
3657 tv->v_type = VAR_NUMBER;
3658 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003659 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003660 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003661 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003662 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003663 // Clear the dict only after getting the item, to avoid
3664 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003665 tv = STACK_TV_BOT(-1);
3666 temp_tv = *tv;
3667 copy_tv(&di->di_tv, tv);
3668 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003669 }
3670 break;
3671
3672 // dict member with string key
3673 case ISN_STRINGMEMBER:
3674 {
3675 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003677 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678
3679 tv = STACK_TV_BOT(-1);
3680 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3681 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003682 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003684 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 }
3686 dict = tv->vval.v_dict;
3687
3688 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3689 == NULL)
3690 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003691 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003693 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003695 // Clear the dict after getting the item, to avoid that it
3696 // make the item invalid.
3697 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003699 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 }
3701 break;
3702
3703 case ISN_NEGATENR:
3704 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003705 if (tv->v_type != VAR_NUMBER
3706#ifdef FEAT_FLOAT
3707 && tv->v_type != VAR_FLOAT
3708#endif
3709 )
3710 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003711 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003712 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003713 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003714 }
3715#ifdef FEAT_FLOAT
3716 if (tv->v_type == VAR_FLOAT)
3717 tv->vval.v_float = -tv->vval.v_float;
3718 else
3719#endif
3720 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 break;
3722
3723 case ISN_CHECKNR:
3724 {
3725 int error = FALSE;
3726
3727 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003728 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003730 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 (void)tv_get_number_chk(tv, &error);
3732 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003733 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 }
3735 break;
3736
3737 case ISN_CHECKTYPE:
3738 {
3739 checktype_T *ct = &iptr->isn_arg.type;
3740
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003741 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003742 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003743 if (!ectx->ec_where.wt_variable)
3744 ectx->ec_where.wt_index = ct->ct_arg_idx;
3745 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3746 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003747 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003748 if (!ectx->ec_where.wt_variable)
3749 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003750
3751 // number 0 is FALSE, number 1 is TRUE
3752 if (tv->v_type == VAR_NUMBER
3753 && ct->ct_type->tt_type == VAR_BOOL
3754 && (tv->vval.v_number == 0
3755 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003757 tv->v_type = VAR_BOOL;
3758 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003759 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 }
3761 }
3762 break;
3763
Bram Moolenaar9af78762020-06-16 11:34:42 +02003764 case ISN_CHECKLEN:
3765 {
3766 int min_len = iptr->isn_arg.checklen.cl_min_len;
3767 list_T *list = NULL;
3768
3769 tv = STACK_TV_BOT(-1);
3770 if (tv->v_type == VAR_LIST)
3771 list = tv->vval.v_list;
3772 if (list == NULL || list->lv_len < min_len
3773 || (list->lv_len > min_len
3774 && !iptr->isn_arg.checklen.cl_more_OK))
3775 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003777 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003778 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003779 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003780 }
3781 }
3782 break;
3783
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003784 case ISN_SETTYPE:
3785 {
3786 checktype_T *ct = &iptr->isn_arg.type;
3787
3788 tv = STACK_TV_BOT(-1);
3789 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3790 {
3791 free_type(tv->vval.v_dict->dv_type);
3792 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3793 }
3794 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3795 {
3796 free_type(tv->vval.v_list->lv_type);
3797 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3798 }
3799 }
3800 break;
3801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003803 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 {
3805 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003806 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003808 if (iptr->isn_type == ISN_2BOOL)
3809 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003810 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003811 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003812 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003813 n = !n;
3814 }
3815 else
3816 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003817 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003818 SOURCING_LNUM = iptr->isn_lnum;
3819 n = tv_get_bool_chk(tv, &error);
3820 if (error)
3821 goto on_error;
3822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 clear_tv(tv);
3824 tv->v_type = VAR_BOOL;
3825 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3826 }
3827 break;
3828
3829 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003830 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003832 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3833 iptr->isn_type == ISN_2STRING_ANY,
3834 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003835 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 break;
3837
Bram Moolenaar08597872020-12-10 19:43:40 +01003838 case ISN_RANGE:
3839 {
3840 exarg_T ea;
3841 char *errormsg;
3842
Bram Moolenaarece0b872021-01-08 20:40:45 +01003843 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003844 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003845 ea.addr_type = ADDR_LINES;
3846 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003847 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003848 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003849 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003850
Bram Moolenaar4c137212021-04-19 16:48:48 +02003851 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003852 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003853 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003854 tv = STACK_TV_BOT(-1);
3855 tv->v_type = VAR_NUMBER;
3856 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003857 if (ea.addr_count == 0)
3858 tv->vval.v_number = curwin->w_cursor.lnum;
3859 else
3860 tv->vval.v_number = ea.line2;
3861 }
3862 break;
3863
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003864 case ISN_PUT:
3865 {
3866 int regname = iptr->isn_arg.put.put_regname;
3867 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3868 char_u *expr = NULL;
3869 int dir = FORWARD;
3870
Bram Moolenaar08597872020-12-10 19:43:40 +01003871 if (lnum < -2)
3872 {
3873 // line number was put on the stack by ISN_RANGE
3874 tv = STACK_TV_BOT(-1);
3875 curwin->w_cursor.lnum = tv->vval.v_number;
3876 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3877 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003878 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003879 }
3880 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003881 // :put! above cursor
3882 dir = BACKWARD;
3883 else if (lnum >= 0)
3884 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003885
3886 if (regname == '=')
3887 {
3888 tv = STACK_TV_BOT(-1);
3889 if (tv->v_type == VAR_STRING)
3890 expr = tv->vval.v_string;
3891 else
3892 {
3893 expr = typval2string(tv, TRUE); // allocates value
3894 clear_tv(tv);
3895 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003896 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003897 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003898 check_cursor();
3899 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3900 vim_free(expr);
3901 }
3902 break;
3903
Bram Moolenaar02194d22020-10-24 23:08:38 +02003904 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003905 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3906 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3907 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3908 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003909 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3910 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003911 break;
3912
Bram Moolenaar02194d22020-10-24 23:08:38 +02003913 case ISN_CMDMOD_REV:
3914 // filter regprog is owned by the instruction, don't free it
3915 cmdmod.cmod_filter_regmatch.regprog = NULL;
3916 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003917 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
3918 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003919 break;
3920
Bram Moolenaar792f7862020-11-23 08:31:18 +01003921 case ISN_UNPACK:
3922 {
3923 int count = iptr->isn_arg.unpack.unp_count;
3924 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3925 list_T *l;
3926 listitem_T *li;
3927 int i;
3928
3929 // Check there is a valid list to unpack.
3930 tv = STACK_TV_BOT(-1);
3931 if (tv->v_type != VAR_LIST)
3932 {
3933 SOURCING_LNUM = iptr->isn_lnum;
3934 emsg(_(e_for_argument_must_be_sequence_of_lists));
3935 goto on_error;
3936 }
3937 l = tv->vval.v_list;
3938 if (l == NULL
3939 || l->lv_len < (semicolon ? count - 1 : count))
3940 {
3941 SOURCING_LNUM = iptr->isn_lnum;
3942 emsg(_(e_list_value_does_not_have_enough_items));
3943 goto on_error;
3944 }
3945 else if (!semicolon && l->lv_len > count)
3946 {
3947 SOURCING_LNUM = iptr->isn_lnum;
3948 emsg(_(e_list_value_has_more_items_than_targets));
3949 goto on_error;
3950 }
3951
3952 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003953 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003954 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003955 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003956
3957 // Variable after semicolon gets a list with the remaining
3958 // items.
3959 if (semicolon)
3960 {
3961 list_T *rem_list =
3962 list_alloc_with_items(l->lv_len - count + 1);
3963
3964 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003965 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003966 tv = STACK_TV_BOT(-count);
3967 tv->vval.v_list = rem_list;
3968 ++rem_list->lv_refcount;
3969 tv->v_lock = 0;
3970 li = l->lv_first;
3971 for (i = 0; i < count - 1; ++i)
3972 li = li->li_next;
3973 for (i = 0; li != NULL; ++i)
3974 {
3975 list_set_item(rem_list, i, &li->li_tv);
3976 li = li->li_next;
3977 }
3978 --count;
3979 }
3980
3981 // Produce the values in reverse order, first item last.
3982 li = l->lv_first;
3983 for (i = 0; i < count; ++i)
3984 {
3985 tv = STACK_TV_BOT(-i - 1);
3986 copy_tv(&li->li_tv, tv);
3987 li = li->li_next;
3988 }
3989
3990 list_unref(l);
3991 }
3992 break;
3993
Bram Moolenaarb2049902021-01-24 12:53:53 +01003994 case ISN_PROF_START:
3995 case ISN_PROF_END:
3996 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003997#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003998 funccall_T cookie;
3999 ufunc_T *cur_ufunc =
4000 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004001 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004002
4003 cookie.func = cur_ufunc;
4004 if (iptr->isn_type == ISN_PROF_START)
4005 {
4006 func_line_start(&cookie, iptr->isn_lnum);
4007 // if we get here the instruction is executed
4008 func_line_exec(&cookie);
4009 }
4010 else
4011 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004012#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004013 }
4014 break;
4015
Bram Moolenaar389df252020-07-09 21:20:47 +02004016 case ISN_SHUFFLE:
4017 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004018 typval_T tmp_tv;
4019 int item = iptr->isn_arg.shuffle.shfl_item;
4020 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004021
4022 tmp_tv = *STACK_TV_BOT(-item);
4023 for ( ; up > 0 && item > 1; --up)
4024 {
4025 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4026 --item;
4027 }
4028 *STACK_TV_BOT(-item) = tmp_tv;
4029 }
4030 break;
4031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004033 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004035 ectx->ec_where.wt_index = 0;
4036 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 break;
4038 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004039 continue;
4040
Bram Moolenaard032f342020-07-18 18:13:02 +02004041func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004042 // Restore previous function. If the frame pointer is where we started
4043 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004044 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004045 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004046
Bram Moolenaar4c137212021-04-19 16:48:48 +02004047 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004048 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004049 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004050 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004051
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004052on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004053 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004054 // If "emsg_silent" is set then ignore the error, unless it was set
4055 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004056 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004057 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004058 {
4059 // If a sequence of instructions causes an error while ":silent!"
4060 // was used, restore the stack length and jump ahead to restoring
4061 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004062 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004063 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004064 while (ectx->ec_stack.ga_len
4065 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004066 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004067 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004068 clear_tv(STACK_TV_BOT(0));
4069 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004070 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4071 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004072 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004073 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004074 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004075on_fatal_error:
4076 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004077 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004078 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004079 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 }
4081
4082done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004083 ret = OK;
4084theend:
4085 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4086 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004087}
4088
4089/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004090 * Execute the instructions from a VAR_INSTR typeval and put the result in
4091 * "rettv".
4092 * Return OK or FAIL.
4093 */
4094 int
4095exe_typval_instr(typval_T *tv, typval_T *rettv)
4096{
4097 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4098 isn_T *save_instr = ectx->ec_instr;
4099 int save_iidx = ectx->ec_iidx;
4100 int res;
4101
4102 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4103 res = exec_instructions(ectx);
4104 if (res == OK)
4105 {
4106 *rettv = *STACK_TV_BOT(-1);
4107 --ectx->ec_stack.ga_len;
4108 }
4109
4110 ectx->ec_instr = save_instr;
4111 ectx->ec_iidx = save_iidx;
4112
4113 return res;
4114}
4115
4116/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004117 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4118 * "substitute_instr".
4119 */
4120 char_u *
4121exe_substitute_instr(void)
4122{
4123 ectx_T *ectx = substitute_instr->subs_ectx;
4124 isn_T *save_instr = ectx->ec_instr;
4125 int save_iidx = ectx->ec_iidx;
4126 char_u *res;
4127
4128 ectx->ec_instr = substitute_instr->subs_instr;
4129 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004130 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004131 typval_T *tv = STACK_TV_BOT(-1);
4132
Bram Moolenaar27523602021-06-05 21:36:19 +02004133 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004134 --ectx->ec_stack.ga_len;
4135 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004136 }
4137 else
4138 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004139 substitute_instr->subs_status = FAIL;
4140 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
Bram Moolenaar4c137212021-04-19 16:48:48 +02004143 ectx->ec_instr = save_instr;
4144 ectx->ec_iidx = save_iidx;
4145
4146 return res;
4147}
4148
4149/*
4150 * Call a "def" function from old Vim script.
4151 * Return OK or FAIL.
4152 */
4153 int
4154call_def_function(
4155 ufunc_T *ufunc,
4156 int argc_arg, // nr of arguments
4157 typval_T *argv, // arguments
4158 partial_T *partial, // optional partial for context
4159 typval_T *rettv) // return value
4160{
4161 ectx_T ectx; // execution context
4162 int argc = argc_arg;
4163 typval_T *tv;
4164 int idx;
4165 int ret = FAIL;
4166 int defcount = ufunc->uf_args.ga_len - argc;
4167 sctx_T save_current_sctx = current_sctx;
4168 int did_emsg_before = did_emsg_cumul + did_emsg;
4169 int save_suppress_errthrow = suppress_errthrow;
4170 msglist_T **saved_msg_list = NULL;
4171 msglist_T *private_msg_list = NULL;
4172 int save_emsg_silent_def = emsg_silent_def;
4173 int save_did_emsg_def = did_emsg_def;
4174 int orig_funcdepth;
4175
4176// Get pointer to item in the stack.
4177#undef STACK_TV
4178#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4179
4180// Get pointer to item at the bottom of the stack, -1 is the bottom.
4181#undef STACK_TV_BOT
4182#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4183
4184// Get pointer to a local variable on the stack. Negative for arguments.
4185#undef STACK_TV_VAR
4186#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4187
4188 if (ufunc->uf_def_status == UF_NOT_COMPILED
4189 || ufunc->uf_def_status == UF_COMPILE_ERROR
4190 || (func_needs_compiling(ufunc, PROFILING(ufunc))
4191 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
4192 == FAIL))
4193 {
4194 if (did_emsg_cumul + did_emsg == did_emsg_before)
4195 semsg(_(e_function_is_not_compiled_str),
4196 printable_func_name(ufunc));
4197 return FAIL;
4198 }
4199
4200 {
4201 // Check the function was really compiled.
4202 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4203 + ufunc->uf_dfunc_idx;
4204 if (INSTRUCTIONS(dfunc) == NULL)
4205 {
4206 iemsg("using call_def_function() on not compiled function");
4207 return FAIL;
4208 }
4209 }
4210
4211 // If depth of calling is getting too high, don't execute the function.
4212 orig_funcdepth = funcdepth_get();
4213 if (funcdepth_increment() == FAIL)
4214 return FAIL;
4215
4216 CLEAR_FIELD(ectx);
4217 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4218 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4219 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4220 {
4221 funcdepth_decrement();
4222 return FAIL;
4223 }
4224 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4225 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4226 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004227
4228 idx = argc - ufunc->uf_args.ga_len;
4229 if (idx > 0 && ufunc->uf_va_name == NULL)
4230 {
4231 if (idx == 1)
4232 emsg(_(e_one_argument_too_many));
4233 else
4234 semsg(_(e_nr_arguments_too_many), idx);
4235 goto failed_early;
4236 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004237 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4238 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004239 {
4240 if (idx == -1)
4241 emsg(_(e_one_argument_too_few));
4242 else
4243 semsg(_(e_nr_arguments_too_few), -idx);
4244 goto failed_early;
4245 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004246
4247 // Put arguments on the stack, but no more than what the function expects.
4248 // A lambda can be called with more arguments than it uses.
4249 for (idx = 0; idx < argc
4250 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4251 ++idx)
4252 {
4253 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4254 && argv[idx].v_type == VAR_SPECIAL
4255 && argv[idx].vval.v_number == VVAL_NONE)
4256 {
4257 // Use the default value.
4258 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4259 }
4260 else
4261 {
4262 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4263 && check_typval_arg_type(
4264 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4265 goto failed_early;
4266 copy_tv(&argv[idx], STACK_TV_BOT(0));
4267 }
4268 ++ectx.ec_stack.ga_len;
4269 }
4270
4271 // Turn varargs into a list. Empty list if no args.
4272 if (ufunc->uf_va_name != NULL)
4273 {
4274 int vararg_count = argc - ufunc->uf_args.ga_len;
4275
4276 if (vararg_count < 0)
4277 vararg_count = 0;
4278 else
4279 argc -= vararg_count;
4280 if (exe_newlist(vararg_count, &ectx) == FAIL)
4281 goto failed_early;
4282
4283 // Check the type of the list items.
4284 tv = STACK_TV_BOT(-1);
4285 if (ufunc->uf_va_type != NULL
4286 && ufunc->uf_va_type != &t_list_any
4287 && ufunc->uf_va_type->tt_member != &t_any
4288 && tv->vval.v_list != NULL)
4289 {
4290 type_T *expected = ufunc->uf_va_type->tt_member;
4291 listitem_T *li = tv->vval.v_list->lv_first;
4292
4293 for (idx = 0; idx < vararg_count; ++idx)
4294 {
4295 if (check_typval_arg_type(expected, &li->li_tv,
4296 argc + idx + 1) == FAIL)
4297 goto failed_early;
4298 li = li->li_next;
4299 }
4300 }
4301
4302 if (defcount > 0)
4303 // Move varargs list to below missing default arguments.
4304 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4305 --ectx.ec_stack.ga_len;
4306 }
4307
4308 // Make space for omitted arguments, will store default value below.
4309 // Any varargs list goes after them.
4310 if (defcount > 0)
4311 for (idx = 0; idx < defcount; ++idx)
4312 {
4313 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4314 ++ectx.ec_stack.ga_len;
4315 }
4316 if (ufunc->uf_va_name != NULL)
4317 ++ectx.ec_stack.ga_len;
4318
4319 // Frame pointer points to just after arguments.
4320 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4321 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4322
4323 {
4324 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4325 + ufunc->uf_dfunc_idx;
4326 ufunc_T *base_ufunc = dfunc->df_ufunc;
4327
4328 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4329 // by copy_func().
4330 if (partial != NULL || base_ufunc->uf_partial != NULL)
4331 {
4332 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
4333 if (ectx.ec_outer == NULL)
4334 goto failed_early;
4335 if (partial != NULL)
4336 {
4337 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4338 {
4339 if (current_ectx->ec_outer != NULL)
4340 *ectx.ec_outer = *current_ectx->ec_outer;
4341 }
4342 else
4343 *ectx.ec_outer = partial->pt_outer;
4344 }
4345 else
4346 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
4347 ectx.ec_outer->out_up_is_copy = TRUE;
4348 }
4349 }
4350
4351 // dummy frame entries
4352 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4353 {
4354 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4355 ++ectx.ec_stack.ga_len;
4356 }
4357
4358 {
4359 // Reserve space for local variables and any closure reference count.
4360 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4361 + ufunc->uf_dfunc_idx;
4362
4363 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4364 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4365 ectx.ec_stack.ga_len += dfunc->df_varcount;
4366 if (dfunc->df_has_closure)
4367 {
4368 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4369 STACK_TV_VAR(idx)->vval.v_number = 0;
4370 ++ectx.ec_stack.ga_len;
4371 }
4372
4373 ectx.ec_instr = INSTRUCTIONS(dfunc);
4374 }
4375
4376 // Following errors are in the function, not the caller.
4377 // Commands behave like vim9script.
4378 estack_push_ufunc(ufunc, 1);
4379 current_sctx = ufunc->uf_script_ctx;
4380 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4381
4382 // Use a specific location for storing error messages to be converted to an
4383 // exception.
4384 saved_msg_list = msg_list;
4385 msg_list = &private_msg_list;
4386
4387 // Do turn errors into exceptions.
4388 suppress_errthrow = FALSE;
4389
4390 // Do not delete the function while executing it.
4391 ++ufunc->uf_calls;
4392
4393 // When ":silent!" was used before calling then we still abort the
4394 // function. If ":silent!" is used in the function then we don't.
4395 emsg_silent_def = emsg_silent;
4396 did_emsg_def = 0;
4397
4398 ectx.ec_where.wt_index = 0;
4399 ectx.ec_where.wt_variable = FALSE;
4400
4401 // Execute the instructions until done.
4402 ret = exec_instructions(&ectx);
4403 if (ret == OK)
4404 {
4405 // function finished, get result from the stack.
4406 if (ufunc->uf_ret_type == &t_void)
4407 {
4408 rettv->v_type = VAR_VOID;
4409 }
4410 else
4411 {
4412 tv = STACK_TV_BOT(-1);
4413 *rettv = *tv;
4414 tv->v_type = VAR_UNKNOWN;
4415 }
4416 }
4417
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004418 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004419 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4420 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004421
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004422 // Deal with any remaining closures, they may be in use somewhere.
4423 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004424 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004425 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004426 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4427 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004428
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004429 estack_pop();
4430 current_sctx = save_current_sctx;
4431
Bram Moolenaarc970e422021-03-17 15:03:04 +01004432 // TODO: when is it safe to delete the function if it is no longer used?
4433 --ufunc->uf_calls;
4434
Bram Moolenaar352134b2020-10-17 22:04:08 +02004435 if (*msg_list != NULL && saved_msg_list != NULL)
4436 {
4437 msglist_T **plist = saved_msg_list;
4438
4439 // Append entries from the current msg_list (uncaught exceptions) to
4440 // the saved msg_list.
4441 while (*plist != NULL)
4442 plist = &(*plist)->next;
4443
4444 *plist = *msg_list;
4445 }
4446 msg_list = saved_msg_list;
4447
Bram Moolenaar4c137212021-04-19 16:48:48 +02004448 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004449 {
4450 cmdmod.cmod_filter_regmatch.regprog = NULL;
4451 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004452 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004453 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004454 emsg_silent_def = save_emsg_silent_def;
4455 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004456
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004457failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004458 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4460 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004463 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004464
Bram Moolenaar0186e582021-01-10 18:33:11 +01004465 while (ectx.ec_outer != NULL)
4466 {
4467 outer_T *up = ectx.ec_outer->out_up_is_copy
4468 ? NULL : ectx.ec_outer->out_up;
4469
4470 vim_free(ectx.ec_outer);
4471 ectx.ec_outer = up;
4472 }
4473
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004474 // Not sure if this is necessary.
4475 suppress_errthrow = save_suppress_errthrow;
4476
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004477 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004478 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004479 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004480 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 return ret;
4482}
4483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004485 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4486 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4487 * "pfx" is prefixed to every line.
4488 */
4489 static void
4490list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4491{
4492 int line_idx = 0;
4493 int prev_current = 0;
4494 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004495 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004496
4497 for (current = 0; current < instr_count; ++current)
4498 {
4499 isn_T *iptr = &instr[current];
4500 char *line;
4501
4502 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004503 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004504 while (line_idx < iptr->isn_lnum
4505 && line_idx < ufunc->uf_lines.ga_len)
4506 {
4507 if (current > prev_current)
4508 {
4509 msg_puts("\n\n");
4510 prev_current = current;
4511 }
4512 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4513 if (line != NULL)
4514 msg(line);
4515 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004516 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4517 {
4518 int first_def_arg = ufunc->uf_args.ga_len
4519 - ufunc->uf_def_args.ga_len;
4520
4521 if (def_arg_idx > 0)
4522 msg_puts("\n\n");
4523 msg_start();
4524 msg_puts(" ");
4525 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4526 first_def_arg + def_arg_idx]);
4527 msg_puts(" = ");
4528 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4529 msg_clr_eos();
4530 msg_end();
4531 }
4532 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004533
4534 switch (iptr->isn_type)
4535 {
4536 case ISN_EXEC:
4537 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4538 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004539 case ISN_LEGACY_EVAL:
4540 smsg("%s%4d EVAL legacy %s", pfx, current,
4541 iptr->isn_arg.string);
4542 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004543 case ISN_REDIRSTART:
4544 smsg("%s%4d REDIR", pfx, current);
4545 break;
4546 case ISN_REDIREND:
4547 smsg("%s%4d REDIR END%s", pfx, current,
4548 iptr->isn_arg.number ? " append" : "");
4549 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004550 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004551#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004552 smsg("%s%4d CEXPR pre %s", pfx, current,
4553 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004554#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004555 break;
4556 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004557#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004558 {
4559 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4560
4561 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4562 cexpr_get_auname(cer->cer_cmdidx),
4563 cer->cer_forceit ? "!" : "",
4564 cer->cer_cmdline);
4565 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004566#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004567 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004568 case ISN_INSTR:
4569 {
4570 smsg("%s%4d INSTR", pfx, current);
4571 list_instructions(" ", iptr->isn_arg.instr,
4572 INT_MAX, NULL);
4573 msg(" -------------");
4574 }
4575 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004576 case ISN_SUBSTITUTE:
4577 {
4578 subs_T *subs = &iptr->isn_arg.subs;
4579
4580 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4581 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4582 msg(" -------------");
4583 }
4584 break;
4585 case ISN_EXECCONCAT:
4586 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4587 (varnumber_T)iptr->isn_arg.number);
4588 break;
4589 case ISN_ECHO:
4590 {
4591 echo_T *echo = &iptr->isn_arg.echo;
4592
4593 smsg("%s%4d %s %d", pfx, current,
4594 echo->echo_with_white ? "ECHO" : "ECHON",
4595 echo->echo_count);
4596 }
4597 break;
4598 case ISN_EXECUTE:
4599 smsg("%s%4d EXECUTE %lld", pfx, current,
4600 (varnumber_T)(iptr->isn_arg.number));
4601 break;
4602 case ISN_ECHOMSG:
4603 smsg("%s%4d ECHOMSG %lld", pfx, current,
4604 (varnumber_T)(iptr->isn_arg.number));
4605 break;
4606 case ISN_ECHOERR:
4607 smsg("%s%4d ECHOERR %lld", pfx, current,
4608 (varnumber_T)(iptr->isn_arg.number));
4609 break;
4610 case ISN_LOAD:
4611 {
4612 if (iptr->isn_arg.number < 0)
4613 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4614 (varnumber_T)(iptr->isn_arg.number
4615 + STACK_FRAME_SIZE));
4616 else
4617 smsg("%s%4d LOAD $%lld", pfx, current,
4618 (varnumber_T)(iptr->isn_arg.number));
4619 }
4620 break;
4621 case ISN_LOADOUTER:
4622 {
4623 if (iptr->isn_arg.number < 0)
4624 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4625 iptr->isn_arg.outer.outer_depth,
4626 iptr->isn_arg.outer.outer_idx
4627 + STACK_FRAME_SIZE);
4628 else
4629 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4630 iptr->isn_arg.outer.outer_depth,
4631 iptr->isn_arg.outer.outer_idx);
4632 }
4633 break;
4634 case ISN_LOADV:
4635 smsg("%s%4d LOADV v:%s", pfx, current,
4636 get_vim_var_name(iptr->isn_arg.number));
4637 break;
4638 case ISN_LOADSCRIPT:
4639 {
4640 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4641 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4642 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4643 + sref->sref_idx;
4644
4645 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4646 sv->sv_name,
4647 sref->sref_idx,
4648 si->sn_name);
4649 }
4650 break;
4651 case ISN_LOADS:
4652 {
4653 scriptitem_T *si = SCRIPT_ITEM(
4654 iptr->isn_arg.loadstore.ls_sid);
4655
4656 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4657 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4658 }
4659 break;
4660 case ISN_LOADAUTO:
4661 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4662 break;
4663 case ISN_LOADG:
4664 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4665 break;
4666 case ISN_LOADB:
4667 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4668 break;
4669 case ISN_LOADW:
4670 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4671 break;
4672 case ISN_LOADT:
4673 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4674 break;
4675 case ISN_LOADGDICT:
4676 smsg("%s%4d LOAD g:", pfx, current);
4677 break;
4678 case ISN_LOADBDICT:
4679 smsg("%s%4d LOAD b:", pfx, current);
4680 break;
4681 case ISN_LOADWDICT:
4682 smsg("%s%4d LOAD w:", pfx, current);
4683 break;
4684 case ISN_LOADTDICT:
4685 smsg("%s%4d LOAD t:", pfx, current);
4686 break;
4687 case ISN_LOADOPT:
4688 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4689 break;
4690 case ISN_LOADENV:
4691 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4692 break;
4693 case ISN_LOADREG:
4694 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4695 break;
4696
4697 case ISN_STORE:
4698 if (iptr->isn_arg.number < 0)
4699 smsg("%s%4d STORE arg[%lld]", pfx, current,
4700 iptr->isn_arg.number + STACK_FRAME_SIZE);
4701 else
4702 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4703 break;
4704 case ISN_STOREOUTER:
4705 {
4706 if (iptr->isn_arg.number < 0)
4707 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4708 iptr->isn_arg.outer.outer_depth,
4709 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4710 else
4711 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4712 iptr->isn_arg.outer.outer_depth,
4713 iptr->isn_arg.outer.outer_idx);
4714 }
4715 break;
4716 case ISN_STOREV:
4717 smsg("%s%4d STOREV v:%s", pfx, current,
4718 get_vim_var_name(iptr->isn_arg.number));
4719 break;
4720 case ISN_STOREAUTO:
4721 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4722 break;
4723 case ISN_STOREG:
4724 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4725 break;
4726 case ISN_STOREB:
4727 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4728 break;
4729 case ISN_STOREW:
4730 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4731 break;
4732 case ISN_STORET:
4733 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4734 break;
4735 case ISN_STORES:
4736 {
4737 scriptitem_T *si = SCRIPT_ITEM(
4738 iptr->isn_arg.loadstore.ls_sid);
4739
4740 smsg("%s%4d STORES %s in %s", pfx, current,
4741 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4742 }
4743 break;
4744 case ISN_STORESCRIPT:
4745 {
4746 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4747 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4748 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4749 + sref->sref_idx;
4750
4751 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4752 sv->sv_name,
4753 sref->sref_idx,
4754 si->sn_name);
4755 }
4756 break;
4757 case ISN_STOREOPT:
4758 smsg("%s%4d STOREOPT &%s", pfx, current,
4759 iptr->isn_arg.storeopt.so_name);
4760 break;
4761 case ISN_STOREENV:
4762 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4763 break;
4764 case ISN_STOREREG:
4765 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4766 break;
4767 case ISN_STORENR:
4768 smsg("%s%4d STORE %lld in $%d", pfx, current,
4769 iptr->isn_arg.storenr.stnr_val,
4770 iptr->isn_arg.storenr.stnr_idx);
4771 break;
4772
4773 case ISN_STOREINDEX:
4774 smsg("%s%4d STOREINDEX %s", pfx, current,
4775 vartype_name(iptr->isn_arg.vartype));
4776 break;
4777
4778 case ISN_STORERANGE:
4779 smsg("%s%4d STORERANGE", pfx, current);
4780 break;
4781
4782 // constants
4783 case ISN_PUSHNR:
4784 smsg("%s%4d PUSHNR %lld", pfx, current,
4785 (varnumber_T)(iptr->isn_arg.number));
4786 break;
4787 case ISN_PUSHBOOL:
4788 case ISN_PUSHSPEC:
4789 smsg("%s%4d PUSH %s", pfx, current,
4790 get_var_special_name(iptr->isn_arg.number));
4791 break;
4792 case ISN_PUSHF:
4793#ifdef FEAT_FLOAT
4794 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4795#endif
4796 break;
4797 case ISN_PUSHS:
4798 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4799 break;
4800 case ISN_PUSHBLOB:
4801 {
4802 char_u *r;
4803 char_u numbuf[NUMBUFLEN];
4804 char_u *tofree;
4805
4806 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4807 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4808 vim_free(tofree);
4809 }
4810 break;
4811 case ISN_PUSHFUNC:
4812 {
4813 char *name = (char *)iptr->isn_arg.string;
4814
4815 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4816 name == NULL ? "[none]" : name);
4817 }
4818 break;
4819 case ISN_PUSHCHANNEL:
4820#ifdef FEAT_JOB_CHANNEL
4821 {
4822 channel_T *channel = iptr->isn_arg.channel;
4823
4824 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4825 channel == NULL ? 0 : channel->ch_id);
4826 }
4827#endif
4828 break;
4829 case ISN_PUSHJOB:
4830#ifdef FEAT_JOB_CHANNEL
4831 {
4832 typval_T tv;
4833 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004834 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02004835
4836 tv.v_type = VAR_JOB;
4837 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004838 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004839 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4840 }
4841#endif
4842 break;
4843 case ISN_PUSHEXC:
4844 smsg("%s%4d PUSH v:exception", pfx, current);
4845 break;
4846 case ISN_UNLET:
4847 smsg("%s%4d UNLET%s %s", pfx, current,
4848 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4849 iptr->isn_arg.unlet.ul_name);
4850 break;
4851 case ISN_UNLETENV:
4852 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4853 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4854 iptr->isn_arg.unlet.ul_name);
4855 break;
4856 case ISN_UNLETINDEX:
4857 smsg("%s%4d UNLETINDEX", pfx, current);
4858 break;
4859 case ISN_UNLETRANGE:
4860 smsg("%s%4d UNLETRANGE", pfx, current);
4861 break;
4862 case ISN_LOCKCONST:
4863 smsg("%s%4d LOCKCONST", pfx, current);
4864 break;
4865 case ISN_NEWLIST:
4866 smsg("%s%4d NEWLIST size %lld", pfx, current,
4867 (varnumber_T)(iptr->isn_arg.number));
4868 break;
4869 case ISN_NEWDICT:
4870 smsg("%s%4d NEWDICT size %lld", pfx, current,
4871 (varnumber_T)(iptr->isn_arg.number));
4872 break;
4873
4874 // function call
4875 case ISN_BCALL:
4876 {
4877 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4878
4879 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4880 internal_func_name(cbfunc->cbf_idx),
4881 cbfunc->cbf_argcount);
4882 }
4883 break;
4884 case ISN_DCALL:
4885 {
4886 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4887 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4888 + cdfunc->cdf_idx;
4889
4890 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
4891 df->df_ufunc->uf_name_exp != NULL
4892 ? df->df_ufunc->uf_name_exp
4893 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4894 }
4895 break;
4896 case ISN_UCALL:
4897 {
4898 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4899
4900 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
4901 cufunc->cuf_name, cufunc->cuf_argcount);
4902 }
4903 break;
4904 case ISN_PCALL:
4905 {
4906 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4907
4908 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
4909 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4910 }
4911 break;
4912 case ISN_PCALL_END:
4913 smsg("%s%4d PCALL end", pfx, current);
4914 break;
4915 case ISN_RETURN:
4916 smsg("%s%4d RETURN", pfx, current);
4917 break;
4918 case ISN_RETURN_ZERO:
4919 smsg("%s%4d RETURN 0", pfx, current);
4920 break;
4921 case ISN_FUNCREF:
4922 {
4923 funcref_T *funcref = &iptr->isn_arg.funcref;
4924 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4925 + funcref->fr_func;
4926
4927 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
4928 }
4929 break;
4930
4931 case ISN_NEWFUNC:
4932 {
4933 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4934
4935 smsg("%s%4d NEWFUNC %s %s", pfx, current,
4936 newfunc->nf_lambda, newfunc->nf_global);
4937 }
4938 break;
4939
4940 case ISN_DEF:
4941 {
4942 char_u *name = iptr->isn_arg.string;
4943
4944 smsg("%s%4d DEF %s", pfx, current,
4945 name == NULL ? (char_u *)"" : name);
4946 }
4947 break;
4948
4949 case ISN_JUMP:
4950 {
4951 char *when = "?";
4952
4953 switch (iptr->isn_arg.jump.jump_when)
4954 {
4955 case JUMP_ALWAYS:
4956 when = "JUMP";
4957 break;
4958 case JUMP_AND_KEEP_IF_TRUE:
4959 when = "JUMP_AND_KEEP_IF_TRUE";
4960 break;
4961 case JUMP_IF_FALSE:
4962 when = "JUMP_IF_FALSE";
4963 break;
4964 case JUMP_AND_KEEP_IF_FALSE:
4965 when = "JUMP_AND_KEEP_IF_FALSE";
4966 break;
4967 case JUMP_IF_COND_FALSE:
4968 when = "JUMP_IF_COND_FALSE";
4969 break;
4970 case JUMP_IF_COND_TRUE:
4971 when = "JUMP_IF_COND_TRUE";
4972 break;
4973 }
4974 smsg("%s%4d %s -> %d", pfx, current, when,
4975 iptr->isn_arg.jump.jump_where);
4976 }
4977 break;
4978
4979 case ISN_JUMP_IF_ARG_SET:
4980 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
4981 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4982 iptr->isn_arg.jump.jump_where);
4983 break;
4984
4985 case ISN_FOR:
4986 {
4987 forloop_T *forloop = &iptr->isn_arg.forloop;
4988
4989 smsg("%s%4d FOR $%d -> %d", pfx, current,
4990 forloop->for_idx, forloop->for_end);
4991 }
4992 break;
4993
4994 case ISN_TRY:
4995 {
4996 try_T *try = &iptr->isn_arg.try;
4997
4998 if (try->try_ref->try_finally == 0)
4999 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5000 pfx, current,
5001 try->try_ref->try_catch,
5002 try->try_ref->try_endtry);
5003 else
5004 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5005 pfx, current,
5006 try->try_ref->try_catch,
5007 try->try_ref->try_finally,
5008 try->try_ref->try_endtry);
5009 }
5010 break;
5011 case ISN_CATCH:
5012 // TODO
5013 smsg("%s%4d CATCH", pfx, current);
5014 break;
5015 case ISN_TRYCONT:
5016 {
5017 trycont_T *trycont = &iptr->isn_arg.trycont;
5018
5019 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5020 trycont->tct_levels,
5021 trycont->tct_levels == 1 ? "" : "s",
5022 trycont->tct_where);
5023 }
5024 break;
5025 case ISN_FINALLY:
5026 smsg("%s%4d FINALLY", pfx, current);
5027 break;
5028 case ISN_ENDTRY:
5029 smsg("%s%4d ENDTRY", pfx, current);
5030 break;
5031 case ISN_THROW:
5032 smsg("%s%4d THROW", pfx, current);
5033 break;
5034
5035 // expression operations on number
5036 case ISN_OPNR:
5037 case ISN_OPFLOAT:
5038 case ISN_OPANY:
5039 {
5040 char *what;
5041 char *ins;
5042
5043 switch (iptr->isn_arg.op.op_type)
5044 {
5045 case EXPR_MULT: what = "*"; break;
5046 case EXPR_DIV: what = "/"; break;
5047 case EXPR_REM: what = "%"; break;
5048 case EXPR_SUB: what = "-"; break;
5049 case EXPR_ADD: what = "+"; break;
5050 default: what = "???"; break;
5051 }
5052 switch (iptr->isn_type)
5053 {
5054 case ISN_OPNR: ins = "OPNR"; break;
5055 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5056 case ISN_OPANY: ins = "OPANY"; break;
5057 default: ins = "???"; break;
5058 }
5059 smsg("%s%4d %s %s", pfx, current, ins, what);
5060 }
5061 break;
5062
5063 case ISN_COMPAREBOOL:
5064 case ISN_COMPARESPECIAL:
5065 case ISN_COMPARENR:
5066 case ISN_COMPAREFLOAT:
5067 case ISN_COMPARESTRING:
5068 case ISN_COMPAREBLOB:
5069 case ISN_COMPARELIST:
5070 case ISN_COMPAREDICT:
5071 case ISN_COMPAREFUNC:
5072 case ISN_COMPAREANY:
5073 {
5074 char *p;
5075 char buf[10];
5076 char *type;
5077
5078 switch (iptr->isn_arg.op.op_type)
5079 {
5080 case EXPR_EQUAL: p = "=="; break;
5081 case EXPR_NEQUAL: p = "!="; break;
5082 case EXPR_GREATER: p = ">"; break;
5083 case EXPR_GEQUAL: p = ">="; break;
5084 case EXPR_SMALLER: p = "<"; break;
5085 case EXPR_SEQUAL: p = "<="; break;
5086 case EXPR_MATCH: p = "=~"; break;
5087 case EXPR_IS: p = "is"; break;
5088 case EXPR_ISNOT: p = "isnot"; break;
5089 case EXPR_NOMATCH: p = "!~"; break;
5090 default: p = "???"; break;
5091 }
5092 STRCPY(buf, p);
5093 if (iptr->isn_arg.op.op_ic == TRUE)
5094 strcat(buf, "?");
5095 switch(iptr->isn_type)
5096 {
5097 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5098 case ISN_COMPARESPECIAL:
5099 type = "COMPARESPECIAL"; break;
5100 case ISN_COMPARENR: type = "COMPARENR"; break;
5101 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5102 case ISN_COMPARESTRING:
5103 type = "COMPARESTRING"; break;
5104 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5105 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5106 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5107 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5108 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5109 default: type = "???"; break;
5110 }
5111
5112 smsg("%s%4d %s %s", pfx, current, type, buf);
5113 }
5114 break;
5115
5116 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5117 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5118
5119 // expression operations
5120 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5121 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5122 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5123 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5124 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5125 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5126 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5127 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5128 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5129 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5130 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5131 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5132 pfx, current, iptr->isn_arg.number); break;
5133 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5134 pfx, current, iptr->isn_arg.number); break;
5135 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5136 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5137 iptr->isn_arg.string); break;
5138 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5139
5140 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5141 case ISN_CHECKTYPE:
5142 {
5143 checktype_T *ct = &iptr->isn_arg.type;
5144 char *tofree;
5145
5146 if (ct->ct_arg_idx == 0)
5147 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5148 type_name(ct->ct_type, &tofree),
5149 (int)ct->ct_off);
5150 else
5151 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5152 type_name(ct->ct_type, &tofree),
5153 (int)ct->ct_off,
5154 (int)ct->ct_arg_idx);
5155 vim_free(tofree);
5156 break;
5157 }
5158 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5159 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5160 iptr->isn_arg.checklen.cl_min_len);
5161 break;
5162 case ISN_SETTYPE:
5163 {
5164 char *tofree;
5165
5166 smsg("%s%4d SETTYPE %s", pfx, current,
5167 type_name(iptr->isn_arg.type.ct_type, &tofree));
5168 vim_free(tofree);
5169 break;
5170 }
5171 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005172 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5173 smsg("%s%4d INVERT %d (!val)", pfx, current,
5174 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005175 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005176 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5177 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005178 break;
5179 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005180 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005181 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005182 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5183 pfx, current,
5184 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005185 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005186 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5187 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005188 break;
5189 case ISN_PUT:
5190 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5191 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005192 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005193 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5194 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005195 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005196 else
5197 smsg("%s%4d PUT %c %ld", pfx, current,
5198 iptr->isn_arg.put.put_regname,
5199 (long)iptr->isn_arg.put.put_lnum);
5200 break;
5201
5202 // TODO: summarize modifiers
5203 case ISN_CMDMOD:
5204 {
5205 char_u *buf;
5206 size_t len = produce_cmdmods(
5207 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5208
5209 buf = alloc(len + 1);
5210 if (buf != NULL)
5211 {
5212 (void)produce_cmdmods(
5213 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5214 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5215 vim_free(buf);
5216 }
5217 break;
5218 }
5219 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5220
5221 case ISN_PROF_START:
5222 smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum);
5223 break;
5224
5225 case ISN_PROF_END:
5226 smsg("%s%4d PROFILE END", pfx, current);
5227 break;
5228
5229 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5230 iptr->isn_arg.unpack.unp_count,
5231 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5232 break;
5233 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5234 iptr->isn_arg.shuffle.shfl_item,
5235 iptr->isn_arg.shuffle.shfl_up);
5236 break;
5237 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5238
5239 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5240 return;
5241 }
5242
5243 out_flush(); // output one line at a time
5244 ui_breakcheck();
5245 if (got_int)
5246 break;
5247 }
5248}
5249
5250/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005251 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005252 * We don't really need this at runtime, but we do have tests that require it,
5253 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 */
5255 void
5256ex_disassemble(exarg_T *eap)
5257{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005258 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005259 char_u *fname;
5260 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261 dfunc_T *dfunc;
5262 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005263 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005264 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005266 if (STRNCMP(arg, "<lambda>", 8) == 0)
5267 {
5268 arg += 8;
5269 (void)getdigits(&arg);
5270 fname = vim_strnsave(eap->arg, arg - eap->arg);
5271 }
5272 else
5273 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005274 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005275 if (fname == NULL)
5276 {
5277 semsg(_(e_invarg2), eap->arg);
5278 return;
5279 }
5280
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005281 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005282 if (ufunc == NULL)
5283 {
5284 char_u *p = untrans_function_name(fname);
5285
5286 if (p != NULL)
5287 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005288 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005289 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005290 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 if (ufunc == NULL)
5292 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005293 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 return;
5295 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005296 if (func_needs_compiling(ufunc, eap->forceit)
5297 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005298 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005299 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005301 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302 return;
5303 }
5304 if (ufunc->uf_name_exp != NULL)
5305 msg((char *)ufunc->uf_name_exp);
5306 else
5307 msg((char *)ufunc->uf_name);
5308
5309 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005310#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005311 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
5312 instr_count = eap->forceit ? dfunc->df_instr_prof_count
5313 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005314#else
5315 instr = dfunc->df_instr;
5316 instr_count = dfunc->df_instr_count;
5317#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318
Bram Moolenaar4c137212021-04-19 16:48:48 +02005319 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320}
5321
5322/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005323 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324 * list, etc. Mostly like what JavaScript does, except that empty list and
5325 * empty dictionary are FALSE.
5326 */
5327 int
5328tv2bool(typval_T *tv)
5329{
5330 switch (tv->v_type)
5331 {
5332 case VAR_NUMBER:
5333 return tv->vval.v_number != 0;
5334 case VAR_FLOAT:
5335#ifdef FEAT_FLOAT
5336 return tv->vval.v_float != 0.0;
5337#else
5338 break;
5339#endif
5340 case VAR_PARTIAL:
5341 return tv->vval.v_partial != NULL;
5342 case VAR_FUNC:
5343 case VAR_STRING:
5344 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5345 case VAR_LIST:
5346 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5347 case VAR_DICT:
5348 return tv->vval.v_dict != NULL
5349 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5350 case VAR_BOOL:
5351 case VAR_SPECIAL:
5352 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5353 case VAR_JOB:
5354#ifdef FEAT_JOB_CHANNEL
5355 return tv->vval.v_job != NULL;
5356#else
5357 break;
5358#endif
5359 case VAR_CHANNEL:
5360#ifdef FEAT_JOB_CHANNEL
5361 return tv->vval.v_channel != NULL;
5362#else
5363 break;
5364#endif
5365 case VAR_BLOB:
5366 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5367 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005368 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005370 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371 break;
5372 }
5373 return FALSE;
5374}
5375
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005376 void
5377emsg_using_string_as(typval_T *tv, int as_number)
5378{
5379 semsg(_(as_number ? e_using_string_as_number_str
5380 : e_using_string_as_bool_str),
5381 tv->vval.v_string == NULL
5382 ? (char_u *)"" : tv->vval.v_string);
5383}
5384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005385/*
5386 * If "tv" is a string give an error and return FAIL.
5387 */
5388 int
5389check_not_string(typval_T *tv)
5390{
5391 if (tv->v_type == VAR_STRING)
5392 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005393 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 clear_tv(tv);
5395 return FAIL;
5396 }
5397 return OK;
5398}
5399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400
5401#endif // FEAT_EVAL