blob: 7f56597b3084ed5f18643624b08b6d1dce52741c [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
983do_2string(typval_T *tv, int is_2string_any)
984{
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;
998 default: to_string_error(tv->v_type);
999 return FAIL;
1000 }
1001 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001002 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001003 clear_tv(tv);
1004 tv->v_type = VAR_STRING;
1005 tv->vval.v_string = str;
1006 }
1007 return OK;
1008}
1009
1010/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001011 * When the value of "sv" is a null list of dict, allocate it.
1012 */
1013 static void
1014allocate_if_null(typval_T *tv)
1015{
1016 switch (tv->v_type)
1017 {
1018 case VAR_LIST:
1019 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001020 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001021 break;
1022 case VAR_DICT:
1023 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001024 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001025 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001026 case VAR_BLOB:
1027 if (tv->vval.v_blob == NULL)
1028 (void)rettv_blob_alloc(tv);
1029 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001030 default:
1031 break;
1032 }
1033}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001034
Bram Moolenaare7525c52021-01-09 13:20:37 +01001035/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001036 * Return the character "str[index]" where "index" is the character index,
1037 * including composing characters.
1038 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001039 */
1040 char_u *
1041char_from_string(char_u *str, varnumber_T index)
1042{
1043 size_t nbyte = 0;
1044 varnumber_T nchar = index;
1045 size_t slen;
1046
1047 if (str == NULL)
1048 return NULL;
1049 slen = STRLEN(str);
1050
Bram Moolenaarff871402021-03-26 13:34:05 +01001051 // Do the same as for a list: a negative index counts from the end.
1052 // Optimization to check the first byte to be below 0x80 (and no composing
1053 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001054 if (index < 0)
1055 {
1056 int clen = 0;
1057
1058 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001059 {
1060 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1061 ++nbyte;
1062 else if (enc_utf8)
1063 nbyte += utfc_ptr2len(str + nbyte);
1064 else
1065 nbyte += mb_ptr2len(str + nbyte);
1066 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001067 nchar = clen + index;
1068 if (nchar < 0)
1069 // unlike list: index out of range results in empty string
1070 return NULL;
1071 }
1072
1073 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001074 {
1075 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1076 ++nbyte;
1077 else if (enc_utf8)
1078 nbyte += utfc_ptr2len(str + nbyte);
1079 else
1080 nbyte += mb_ptr2len(str + nbyte);
1081 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001082 if (nbyte >= slen)
1083 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001084 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001085}
1086
1087/*
1088 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001089 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001090 * If going over the end return "str_len".
1091 * If "idx" is negative count from the end, -1 is the last character.
1092 * When going over the start return -1.
1093 */
1094 static long
1095char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1096{
1097 varnumber_T nchar = idx;
1098 size_t nbyte = 0;
1099
1100 if (nchar >= 0)
1101 {
1102 while (nchar > 0 && nbyte < str_len)
1103 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001104 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001105 --nchar;
1106 }
1107 }
1108 else
1109 {
1110 nbyte = str_len;
1111 while (nchar < 0 && nbyte > 0)
1112 {
1113 --nbyte;
1114 nbyte -= mb_head_off(str, str + nbyte);
1115 ++nchar;
1116 }
1117 if (nchar < 0)
1118 return -1;
1119 }
1120 return (long)nbyte;
1121}
1122
1123/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001124 * Return the slice "str[first : last]" using character indexes. Composing
1125 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001126 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001127 * Return NULL when the result is empty.
1128 */
1129 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001130string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001131{
1132 long start_byte, end_byte;
1133 size_t slen;
1134
1135 if (str == NULL)
1136 return NULL;
1137 slen = STRLEN(str);
1138 start_byte = char_idx2byte(str, slen, first);
1139 if (start_byte < 0)
1140 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001141 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001142 end_byte = (long)slen;
1143 else
1144 {
1145 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001146 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001147 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001148 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001149 }
1150
1151 if (start_byte >= (long)slen || end_byte <= start_byte)
1152 return NULL;
1153 return vim_strnsave(str + start_byte, end_byte - start_byte);
1154}
1155
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001156 static svar_T *
1157get_script_svar(scriptref_T *sref, ectx_T *ectx)
1158{
1159 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1160 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1161 + ectx->ec_dfunc_idx;
1162 svar_T *sv;
1163
1164 if (sref->sref_seq != si->sn_script_seq)
1165 {
1166 // The script was reloaded after the function was
1167 // compiled, the script_idx may not be valid.
1168 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1169 dfunc->df_ufunc->uf_name_exp);
1170 return NULL;
1171 }
1172 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1173 if (!equal_type(sv->sv_type, sref->sref_type))
1174 {
1175 emsg(_(e_script_variable_type_changed));
1176 return NULL;
1177 }
1178 return sv;
1179}
1180
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001181/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 * Execute a function by "name".
1183 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001184 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 */
1186 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001187call_eval_func(
1188 char_u *name,
1189 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001190 ectx_T *ectx,
1191 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192{
Bram Moolenaared677f52020-08-12 16:38:10 +02001193 int called_emsg_before = called_emsg;
1194 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195
Bram Moolenaar4c137212021-04-19 16:48:48 +02001196 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001197 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001199 dictitem_T *v;
1200
1201 v = find_var(name, NULL, FALSE);
1202 if (v == NULL)
1203 {
1204 semsg(_(e_unknownfunc), name);
1205 return FAIL;
1206 }
1207 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1208 {
1209 semsg(_(e_unknownfunc), name);
1210 return FAIL;
1211 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001212 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001214 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215}
1216
1217/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001218 * When a function reference is used, fill a partial with the information
1219 * needed, especially when it is used as a closure.
1220 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001221 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001222fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1223{
1224 pt->pt_func = ufunc;
1225 pt->pt_refcount = 1;
1226
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001227 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001228 {
1229 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1230 + ectx->ec_dfunc_idx;
1231
1232 // The closure needs to find arguments and local
1233 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001234 pt->pt_outer.out_stack = &ectx->ec_stack;
1235 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1236 pt->pt_outer.out_up = ectx->ec_outer;
1237 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001238
1239 // If this function returns and the closure is still
1240 // being used, we need to make a copy of the context
1241 // (arguments and local variables). Store a reference
1242 // to the partial so we can handle that.
1243 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1244 {
1245 vim_free(pt);
1246 return FAIL;
1247 }
1248 // Extra variable keeps the count of closures created
1249 // in the current function call.
1250 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1251 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1252
1253 ((partial_T **)ectx->ec_funcrefs.ga_data)
1254 [ectx->ec_funcrefs.ga_len] = pt;
1255 ++pt->pt_refcount;
1256 ++ectx->ec_funcrefs.ga_len;
1257 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001258 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001259 return OK;
1260}
1261
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001262// used for v_instr of typval of VAR_INSTR
1263struct instr_S {
1264 ectx_T *instr_ectx;
1265 isn_T *instr_instr;
1266};
1267
Bram Moolenaar4c137212021-04-19 16:48:48 +02001268// used for substitute_instr
1269typedef struct subs_expr_S {
1270 ectx_T *subs_ectx;
1271 isn_T *subs_instr;
1272 int subs_status;
1273} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274
1275// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001276#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277
1278// Get pointer to item at the bottom of the stack, -1 is the bottom.
1279#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001280#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 +01001281
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001282// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001283#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 +01001284
Bram Moolenaar4c137212021-04-19 16:48:48 +02001285/*
1286 * Execute instructions in execution context "ectx".
1287 * Return OK or FAIL;
1288 */
1289 static int
1290exec_instructions(ectx_T *ectx)
1291{
1292 int breakcheck_count = 0;
1293 typval_T *tv;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001294
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001295 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001296 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001297
Bram Moolenaarff652882021-05-16 15:24:49 +02001298 // Only catch exceptions in this instruction list.
1299 ectx->ec_trylevel_at_start = trylevel;
1300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 for (;;)
1302 {
1303 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001304
Bram Moolenaar270d0382020-05-15 21:42:53 +02001305 if (++breakcheck_count >= 100)
1306 {
1307 line_breakcheck();
1308 breakcheck_count = 0;
1309 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001310 if (got_int)
1311 {
1312 // Turn CTRL-C into an exception.
1313 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001314 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001315 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001316 did_throw = TRUE;
1317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318
Bram Moolenaara26b9702020-04-18 19:53:28 +02001319 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1320 {
1321 // Turn an error message into an exception.
1322 did_emsg = FALSE;
1323 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001324 return FAIL;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001325 did_throw = TRUE;
1326 *msg_list = NULL;
1327 }
1328
Bram Moolenaar4c137212021-04-19 16:48:48 +02001329 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001331 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001332 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333
1334 // An exception jumps to the first catch, finally, or returns from
1335 // the current function.
1336 if (trystack->ga_len > 0)
1337 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001338 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 {
1340 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001341 ectx->ec_in_catch = TRUE;
1342 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 }
1344 else
1345 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001346 // Not inside try or need to return from current functions.
1347 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001348 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1349 return FAIL;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001350 tv = STACK_TV_BOT(0);
1351 tv->v_type = VAR_NUMBER;
1352 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001353 ++ectx->ec_stack.ga_len;
1354 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001356 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001357 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001358 if (handle_closure_in_use(ectx, FALSE) == FAIL)
1359 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 goto done;
1361 }
1362
Bram Moolenaar4c137212021-04-19 16:48:48 +02001363 if (func_return(ectx) == FAIL)
1364 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 }
1366 continue;
1367 }
1368
Bram Moolenaar4c137212021-04-19 16:48:48 +02001369 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 switch (iptr->isn_type)
1371 {
1372 // execute Ex command line
1373 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001374 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001375 source_cookie_T cookie;
1376
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001377 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001378 // Pass getsourceline to get an error for a missing ":end"
1379 // command.
1380 CLEAR_FIELD(cookie);
1381 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1382 if (do_cmdline(iptr->isn_arg.string,
1383 getsourceline, &cookie,
1384 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1385 == FAIL
1386 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001387 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 break;
1390
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001391 // Evaluate an expression with legacy syntax, push it onto the
1392 // stack.
1393 case ISN_LEGACY_EVAL:
1394 {
1395 char_u *arg = iptr->isn_arg.string;
1396 int res;
1397 int save_flags = cmdmod.cmod_flags;
1398
1399 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1400 return FAIL;
1401 tv = STACK_TV_BOT(0);
1402 init_tv(tv);
1403 cmdmod.cmod_flags |= CMOD_LEGACY;
1404 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1405 cmdmod.cmod_flags = save_flags;
1406 if (res == FAIL)
1407 goto on_error;
1408 ++ectx->ec_stack.ga_len;
1409 }
1410 break;
1411
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001412 // push typeval VAR_INSTR with instructions to be executed
1413 case ISN_INSTR:
1414 {
1415 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1416 return FAIL;
1417 tv = STACK_TV_BOT(0);
1418 tv->vval.v_instr = ALLOC_ONE(instr_T);
1419 if (tv->vval.v_instr == NULL)
1420 goto on_error;
1421 ++ectx->ec_stack.ga_len;
1422
1423 tv->v_type = VAR_INSTR;
1424 tv->vval.v_instr->instr_ectx = ectx;
1425 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1426 }
1427 break;
1428
Bram Moolenaar4c137212021-04-19 16:48:48 +02001429 // execute :substitute with an expression
1430 case ISN_SUBSTITUTE:
1431 {
1432 subs_T *subs = &iptr->isn_arg.subs;
1433 source_cookie_T cookie;
1434 struct subs_expr_S *save_instr = substitute_instr;
1435 struct subs_expr_S subs_instr;
1436 int res;
1437
1438 subs_instr.subs_ectx = ectx;
1439 subs_instr.subs_instr = subs->subs_instr;
1440 subs_instr.subs_status = OK;
1441 substitute_instr = &subs_instr;
1442
1443 SOURCING_LNUM = iptr->isn_lnum;
1444 // This is very much like ISN_EXEC
1445 CLEAR_FIELD(cookie);
1446 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1447 res = do_cmdline(subs->subs_cmd,
1448 getsourceline, &cookie,
1449 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1450 substitute_instr = save_instr;
1451
1452 if (res == FAIL || did_emsg
1453 || subs_instr.subs_status == FAIL)
1454 goto on_error;
1455 }
1456 break;
1457
1458 case ISN_FINISH:
1459 goto done;
1460
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001461 case ISN_REDIRSTART:
1462 // create a dummy entry for var_redir_str()
1463 if (alloc_redir_lval() == FAIL)
1464 goto on_error;
1465
1466 // The output is stored in growarray "redir_ga" until
1467 // redirection ends.
1468 init_redir_ga();
1469 redir_vname = 1;
1470 break;
1471
1472 case ISN_REDIREND:
1473 {
1474 char_u *res = get_clear_redir_ga();
1475
1476 // End redirection, put redirected text on the stack.
1477 clear_redir_lval();
1478 redir_vname = 0;
1479
1480 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1481 {
1482 vim_free(res);
1483 return FAIL;
1484 }
1485 tv = STACK_TV_BOT(0);
1486 tv->v_type = VAR_STRING;
1487 tv->vval.v_string = res;
1488 ++ectx->ec_stack.ga_len;
1489 }
1490 break;
1491
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001492 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001493#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001494 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1495 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001496#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001497 break;
1498
1499 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001500#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001501 {
1502 exarg_T ea;
1503 int res;
1504
1505 CLEAR_FIELD(ea);
1506 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1507 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1508 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1509 --ectx->ec_stack.ga_len;
1510 tv = STACK_TV_BOT(0);
1511 res = cexpr_core(&ea, tv);
1512 clear_tv(tv);
1513 if (res == FAIL)
1514 goto on_error;
1515 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001516#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001517 break;
1518
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001519 // execute Ex command from pieces on the stack
1520 case ISN_EXECCONCAT:
1521 {
1522 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001523 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001524 int pass;
1525 int i;
1526 char_u *cmd = NULL;
1527 char_u *str;
1528
1529 for (pass = 1; pass <= 2; ++pass)
1530 {
1531 for (i = 0; i < count; ++i)
1532 {
1533 tv = STACK_TV_BOT(i - count);
1534 str = tv->vval.v_string;
1535 if (str != NULL && *str != NUL)
1536 {
1537 if (pass == 2)
1538 STRCPY(cmd + len, str);
1539 len += STRLEN(str);
1540 }
1541 if (pass == 2)
1542 clear_tv(tv);
1543 }
1544 if (pass == 1)
1545 {
1546 cmd = alloc(len + 1);
1547 if (cmd == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001548 return FAIL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001549 len = 0;
1550 }
1551 }
1552
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001553 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001554 do_cmdline_cmd(cmd);
1555 vim_free(cmd);
1556 }
1557 break;
1558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 // execute :echo {string} ...
1560 case ISN_ECHO:
1561 {
1562 int count = iptr->isn_arg.echo.echo_count;
1563 int atstart = TRUE;
1564 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001565 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566
1567 for (idx = 0; idx < count; ++idx)
1568 {
1569 tv = STACK_TV_BOT(idx - count);
1570 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1571 &atstart, &needclr);
1572 clear_tv(tv);
1573 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001574 if (needclr)
1575 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001576 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 }
1578 break;
1579
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001580 // :execute {string} ...
1581 // :echomsg {string} ...
1582 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001583 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001584 case ISN_ECHOMSG:
1585 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001586 {
1587 int count = iptr->isn_arg.number;
1588 garray_T ga;
1589 char_u buf[NUMBUFLEN];
1590 char_u *p;
1591 int len;
1592 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001593 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001594
1595 ga_init2(&ga, 1, 80);
1596 for (idx = 0; idx < count; ++idx)
1597 {
1598 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001599 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001600 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001601 if (tv->v_type == VAR_CHANNEL
1602 || tv->v_type == VAR_JOB)
1603 {
1604 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001605 semsg(_(e_using_invalid_value_as_string_str),
1606 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001607 break;
1608 }
1609 else
1610 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001611 }
1612 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001613 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001614
1615 len = (int)STRLEN(p);
1616 if (ga_grow(&ga, len + 2) == FAIL)
1617 failed = TRUE;
1618 else
1619 {
1620 if (ga.ga_len > 0)
1621 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1622 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1623 ga.ga_len += len;
1624 }
1625 clear_tv(tv);
1626 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001627 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001628 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001629 {
1630 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001631 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001632 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001633
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001634 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001635 {
1636 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001637 {
1638 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001639 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001640 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001641 {
1642 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001643 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001644 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001645 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001646 else
1647 {
1648 msg_sb_eol();
1649 if (iptr->isn_type == ISN_ECHOMSG)
1650 {
1651 msg_attr(ga.ga_data, echo_attr);
1652 out_flush();
1653 }
1654 else
1655 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001656 SOURCING_LNUM = iptr->isn_lnum;
1657 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001658 }
1659 }
1660 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001661 ga_clear(&ga);
1662 }
1663 break;
1664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 // load local variable or argument
1666 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001667 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1668 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001670 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 break;
1672
1673 // load v: variable
1674 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001675 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1676 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001678 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 break;
1680
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001681 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 case ISN_LOADSCRIPT:
1683 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001684 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 svar_T *sv;
1686
Bram Moolenaar4c137212021-04-19 16:48:48 +02001687 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001688 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001689 return FAIL;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001690 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001691 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1692 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001694 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 }
1696 break;
1697
1698 // load s: variable in old script
1699 case ISN_LOADS:
1700 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001701 hashtab_T *ht = &SCRIPT_VARS(
1702 iptr->isn_arg.loadstore.ls_sid);
1703 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 if (di == NULL)
1707 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001708 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001709 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001710 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 }
1712 else
1713 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001714 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1715 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001717 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 }
1719 }
1720 break;
1721
Bram Moolenaard3aac292020-04-19 14:32:17 +02001722 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001724 case ISN_LOADB:
1725 case ISN_LOADW:
1726 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001728 dictitem_T *di = NULL;
1729 hashtab_T *ht = NULL;
1730 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001731
Bram Moolenaard3aac292020-04-19 14:32:17 +02001732 switch (iptr->isn_type)
1733 {
1734 case ISN_LOADG:
1735 ht = get_globvar_ht();
1736 namespace = 'g';
1737 break;
1738 case ISN_LOADB:
1739 ht = &curbuf->b_vars->dv_hashtab;
1740 namespace = 'b';
1741 break;
1742 case ISN_LOADW:
1743 ht = &curwin->w_vars->dv_hashtab;
1744 namespace = 'w';
1745 break;
1746 case ISN_LOADT:
1747 ht = &curtab->tp_vars->dv_hashtab;
1748 namespace = 't';
1749 break;
1750 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001751 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001752 }
1753 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 if (di == NULL)
1756 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001757 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001758 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001759 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001760 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 }
1762 else
1763 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001764 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1765 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001767 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 }
1769 }
1770 break;
1771
Bram Moolenaar03290b82020-12-19 16:30:44 +01001772 // load autoload variable
1773 case ISN_LOADAUTO:
1774 {
1775 char_u *name = iptr->isn_arg.string;
1776
Bram Moolenaar4c137212021-04-19 16:48:48 +02001777 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1778 return FAIL;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001779 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001780 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001781 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001782 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001783 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001784 }
1785 break;
1786
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001787 // load g:/b:/w:/t: namespace
1788 case ISN_LOADGDICT:
1789 case ISN_LOADBDICT:
1790 case ISN_LOADWDICT:
1791 case ISN_LOADTDICT:
1792 {
1793 dict_T *d = NULL;
1794
1795 switch (iptr->isn_type)
1796 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001797 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1798 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1799 case ISN_LOADWDICT: d = curwin->w_vars; break;
1800 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001801 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001802 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001803 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001804 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1805 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001806 tv = STACK_TV_BOT(0);
1807 tv->v_type = VAR_DICT;
1808 tv->v_lock = 0;
1809 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001810 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001811 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001812 }
1813 break;
1814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 // load &option
1816 case ISN_LOADOPT:
1817 {
1818 typval_T optval;
1819 char_u *name = iptr->isn_arg.string;
1820
Bram Moolenaara8c17702020-04-01 21:17:24 +02001821 // This is not expected to fail, name is checked during
1822 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001823 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1824 return FAIL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001825 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001826 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001828 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 }
1830 break;
1831
1832 // load $ENV
1833 case ISN_LOADENV:
1834 {
1835 typval_T optval;
1836 char_u *name = iptr->isn_arg.string;
1837
Bram Moolenaar4c137212021-04-19 16:48:48 +02001838 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1839 return FAIL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001840 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001841 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001843 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 }
1845 break;
1846
1847 // load @register
1848 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001849 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1850 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 tv = STACK_TV_BOT(0);
1852 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001853 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001854 // This may result in NULL, which should be equivalent to an
1855 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 tv->vval.v_string = get_reg_contents(
1857 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001858 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 break;
1860
1861 // store local variable
1862 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001863 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 tv = STACK_TV_VAR(iptr->isn_arg.number);
1865 clear_tv(tv);
1866 *tv = *STACK_TV_BOT(0);
1867 break;
1868
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001869 // store s: variable in old script
1870 case ISN_STORES:
1871 {
1872 hashtab_T *ht = &SCRIPT_VARS(
1873 iptr->isn_arg.loadstore.ls_sid);
1874 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001875 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001876
Bram Moolenaar4c137212021-04-19 16:48:48 +02001877 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001878 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001879 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001880 else
1881 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001882 SOURCING_LNUM = iptr->isn_lnum;
1883 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001884 {
1885 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001886 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001887 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001888 clear_tv(&di->di_tv);
1889 di->di_tv = *STACK_TV_BOT(0);
1890 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001891 }
1892 break;
1893
1894 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 case ISN_STORESCRIPT:
1896 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001897 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1898 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899
Bram Moolenaar4c137212021-04-19 16:48:48 +02001900 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001901 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001902 return FAIL;
1903 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001904
1905 // "const" and "final" are checked at compile time, locking
1906 // the value needs to be checked here.
1907 SOURCING_LNUM = iptr->isn_lnum;
1908 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001909 {
1910 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001911 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001912 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 clear_tv(sv->sv_tv);
1915 *sv->sv_tv = *STACK_TV_BOT(0);
1916 }
1917 break;
1918
1919 // store option
1920 case ISN_STOREOPT:
1921 {
1922 long n = 0;
1923 char_u *s = NULL;
1924 char *msg;
1925
Bram Moolenaar4c137212021-04-19 16:48:48 +02001926 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 tv = STACK_TV_BOT(0);
1928 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001929 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001931 if (s == NULL)
1932 s = (char_u *)"";
1933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001935 // must be VAR_NUMBER, CHECKTYPE makes sure
1936 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1938 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001939 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 if (msg != NULL)
1941 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001942 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001944 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 }
1947 break;
1948
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001949 // store $ENV
1950 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001951 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001952 tv = STACK_TV_BOT(0);
1953 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1954 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001955 break;
1956
1957 // store @r
1958 case ISN_STOREREG:
1959 {
1960 int reg = iptr->isn_arg.number;
1961
Bram Moolenaar4c137212021-04-19 16:48:48 +02001962 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001963 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001964 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001965 tv_get_string(tv), -1, FALSE);
1966 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001967 }
1968 break;
1969
1970 // store v: variable
1971 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001972 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001973 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1974 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001975 // should not happen, type is checked when compiling
1976 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001977 break;
1978
Bram Moolenaard3aac292020-04-19 14:32:17 +02001979 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001981 case ISN_STOREB:
1982 case ISN_STOREW:
1983 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001985 dictitem_T *di;
1986 hashtab_T *ht;
1987 char_u *name = iptr->isn_arg.string + 2;
1988
Bram Moolenaard3aac292020-04-19 14:32:17 +02001989 switch (iptr->isn_type)
1990 {
1991 case ISN_STOREG:
1992 ht = get_globvar_ht();
1993 break;
1994 case ISN_STOREB:
1995 ht = &curbuf->b_vars->dv_hashtab;
1996 break;
1997 case ISN_STOREW:
1998 ht = &curwin->w_vars->dv_hashtab;
1999 break;
2000 case ISN_STORET:
2001 ht = &curtab->tp_vars->dv_hashtab;
2002 break;
2003 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02002004 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006
Bram Moolenaar4c137212021-04-19 16:48:48 +02002007 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002008 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002010 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 else
2012 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002013 SOURCING_LNUM = iptr->isn_lnum;
2014 if (var_check_permission(di, name) == FAIL)
2015 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 clear_tv(&di->di_tv);
2017 di->di_tv = *STACK_TV_BOT(0);
2018 }
2019 }
2020 break;
2021
Bram Moolenaar03290b82020-12-19 16:30:44 +01002022 // store an autoload variable
2023 case ISN_STOREAUTO:
2024 SOURCING_LNUM = iptr->isn_lnum;
2025 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2026 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002027 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002028 break;
2029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 // store number in local variable
2031 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002032 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 clear_tv(tv);
2034 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002035 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 break;
2037
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002038 // store value in list or dict variable
2039 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002040 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002041 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002042 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002043 typval_T *tv_dest = STACK_TV_BOT(-1);
2044 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002045
Bram Moolenaar752fc692021-01-04 21:57:11 +01002046 // Stack contains:
2047 // -3 value to be stored
2048 // -2 index
2049 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002050 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002051 SOURCING_LNUM = iptr->isn_lnum;
2052 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002053 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002054 dest_type = tv_dest->v_type;
2055 if (dest_type == VAR_DICT)
2056 status = do_2string(tv_idx, TRUE);
2057 else if (dest_type == VAR_LIST
2058 && tv_idx->v_type != VAR_NUMBER)
2059 {
2060 emsg(_(e_number_exp));
2061 status = FAIL;
2062 }
2063 }
2064 else if (dest_type != tv_dest->v_type)
2065 {
2066 // just in case, should be OK
2067 semsg(_(e_expected_str_but_got_str),
2068 vartype_name(dest_type),
2069 vartype_name(tv_dest->v_type));
2070 status = FAIL;
2071 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002072
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002073 if (status == OK && dest_type == VAR_LIST)
2074 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002075 long lidx = (long)tv_idx->vval.v_number;
2076 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002077
2078 if (list == NULL)
2079 {
2080 emsg(_(e_list_not_set));
2081 goto on_error;
2082 }
2083 if (lidx < 0 && list->lv_len + lidx >= 0)
2084 // negative index is relative to the end
2085 lidx = list->lv_len + lidx;
2086 if (lidx < 0 || lidx > list->lv_len)
2087 {
2088 semsg(_(e_listidx), lidx);
2089 goto on_error;
2090 }
2091 if (lidx < list->lv_len)
2092 {
2093 listitem_T *li = list_find(list, lidx);
2094
2095 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002096 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002097 goto on_error;
2098 // overwrite existing list item
2099 clear_tv(&li->li_tv);
2100 li->li_tv = *tv;
2101 }
2102 else
2103 {
2104 if (error_if_locked(list->lv_lock,
2105 e_cannot_change_list))
2106 goto on_error;
2107 // append to list, only fails when out of memory
2108 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002109 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002110 clear_tv(tv);
2111 }
2112 }
2113 else if (status == OK && dest_type == VAR_DICT)
2114 {
2115 char_u *key = tv_idx->vval.v_string;
2116 dict_T *dict = tv_dest->vval.v_dict;
2117 dictitem_T *di;
2118
2119 SOURCING_LNUM = iptr->isn_lnum;
2120 if (dict == NULL)
2121 {
2122 emsg(_(e_dictionary_not_set));
2123 goto on_error;
2124 }
2125 if (key == NULL)
2126 key = (char_u *)"";
2127 di = dict_find(dict, key, -1);
2128 if (di != NULL)
2129 {
2130 if (error_if_locked(di->di_tv.v_lock,
2131 e_cannot_change_dict_item))
2132 goto on_error;
2133 // overwrite existing value
2134 clear_tv(&di->di_tv);
2135 di->di_tv = *tv;
2136 }
2137 else
2138 {
2139 if (error_if_locked(dict->dv_lock,
2140 e_cannot_change_dict))
2141 goto on_error;
2142 // add to dict, only fails when out of memory
2143 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002144 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002145 clear_tv(tv);
2146 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002147 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002148 else if (status == OK && dest_type == VAR_BLOB)
2149 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002150 long lidx = (long)tv_idx->vval.v_number;
2151 blob_T *blob = tv_dest->vval.v_blob;
2152 varnumber_T nr;
2153 int error = FALSE;
2154 int len;
2155
2156 if (blob == NULL)
2157 {
2158 emsg(_(e_blob_not_set));
2159 goto on_error;
2160 }
2161 len = blob_len(blob);
2162 if (lidx < 0 && len + lidx >= 0)
2163 // negative index is relative to the end
2164 lidx = len + lidx;
2165
2166 // Can add one byte at the end.
2167 if (lidx < 0 || lidx > len)
2168 {
2169 semsg(_(e_blobidx), lidx);
2170 goto on_error;
2171 }
2172 if (value_check_lock(blob->bv_lock,
2173 (char_u *)"blob", FALSE))
2174 goto on_error;
2175 nr = tv_get_number_chk(tv, &error);
2176 if (error)
2177 goto on_error;
2178 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002179 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002180 else
2181 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002182 status = FAIL;
2183 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002184 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002185
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002186 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002187 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002188 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002189 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002190 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002191 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002192 goto on_error;
2193 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002194 }
2195 break;
2196
Bram Moolenaar68452172021-04-12 21:21:02 +02002197 // store value in blob range
2198 case ISN_STORERANGE:
2199 {
2200 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2201 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2202 typval_T *tv_dest = STACK_TV_BOT(-1);
2203 int status = OK;
2204
2205 // Stack contains:
2206 // -4 value to be stored
2207 // -3 first index or "none"
2208 // -2 second index or "none"
2209 // -1 destination blob
2210 tv = STACK_TV_BOT(-4);
2211 if (tv_dest->v_type != VAR_BLOB)
2212 {
2213 status = FAIL;
2214 emsg(_(e_blob_required));
2215 }
2216 else
2217 {
2218 varnumber_T n1;
2219 varnumber_T n2;
2220 int error = FALSE;
2221
2222 n1 = tv_get_number_chk(tv_idx1, &error);
2223 if (error)
2224 status = FAIL;
2225 else
2226 {
2227 if (tv_idx2->v_type == VAR_SPECIAL
2228 && tv_idx2->vval.v_number == VVAL_NONE)
2229 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2230 else
2231 n2 = tv_get_number_chk(tv_idx2, &error);
2232 if (error)
2233 status = FAIL;
2234 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002235 {
2236 long bloblen = blob_len(tv_dest->vval.v_blob);
2237
2238 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002239 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002240 || check_blob_range(bloblen,
2241 n1, n2, FALSE) == FAIL)
2242 status = FAIL;
2243 else
2244 status = blob_set_range(
2245 tv_dest->vval.v_blob, n1, n2, tv);
2246 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002247 }
2248 }
2249
2250 clear_tv(tv_idx1);
2251 clear_tv(tv_idx2);
2252 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002253 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002254 clear_tv(tv);
2255
2256 if (status == FAIL)
2257 goto on_error;
2258 }
2259 break;
2260
Bram Moolenaar0186e582021-01-10 18:33:11 +01002261 // load or store variable or argument from outer scope
2262 case ISN_LOADOUTER:
2263 case ISN_STOREOUTER:
2264 {
2265 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002266 outer_T *outer = ectx->ec_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002267
2268 while (depth > 1 && outer != NULL)
2269 {
2270 outer = outer->out_up;
2271 --depth;
2272 }
2273 if (outer == NULL)
2274 {
2275 SOURCING_LNUM = iptr->isn_lnum;
2276 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002277 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002278 }
2279 tv = ((typval_T *)outer->out_stack->ga_data)
2280 + outer->out_frame_idx + STACK_FRAME_SIZE
2281 + iptr->isn_arg.outer.outer_idx;
2282 if (iptr->isn_type == ISN_LOADOUTER)
2283 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002284 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2285 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002286 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002287 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002288 }
2289 else
2290 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002291 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002292 clear_tv(tv);
2293 *tv = *STACK_TV_BOT(0);
2294 }
2295 }
2296 break;
2297
Bram Moolenaar752fc692021-01-04 21:57:11 +01002298 // unlet item in list or dict variable
2299 case ISN_UNLETINDEX:
2300 {
2301 typval_T *tv_idx = STACK_TV_BOT(-2);
2302 typval_T *tv_dest = STACK_TV_BOT(-1);
2303 int status = OK;
2304
2305 // Stack contains:
2306 // -2 index
2307 // -1 dict or list
2308 if (tv_dest->v_type == VAR_DICT)
2309 {
2310 // unlet a dict item, index must be a string
2311 if (tv_idx->v_type != VAR_STRING)
2312 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002313 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002314 semsg(_(e_expected_str_but_got_str),
2315 vartype_name(VAR_STRING),
2316 vartype_name(tv_idx->v_type));
2317 status = FAIL;
2318 }
2319 else
2320 {
2321 dict_T *d = tv_dest->vval.v_dict;
2322 char_u *key = tv_idx->vval.v_string;
2323 dictitem_T *di = NULL;
2324
2325 if (key == NULL)
2326 key = (char_u *)"";
2327 if (d != NULL)
2328 di = dict_find(d, key, (int)STRLEN(key));
2329 if (di == NULL)
2330 {
2331 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002333 semsg(_(e_dictkey), key);
2334 status = FAIL;
2335 }
2336 else
2337 {
2338 // TODO: check for dict or item locked
2339 dictitem_remove(d, di);
2340 }
2341 }
2342 }
2343 else if (tv_dest->v_type == VAR_LIST)
2344 {
2345 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002346 SOURCING_LNUM = iptr->isn_lnum;
2347 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002348 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002349 status = FAIL;
2350 }
2351 else
2352 {
2353 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002354 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002355 listitem_T *li = NULL;
2356
2357 li = list_find(l, n);
2358 if (li == NULL)
2359 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002360 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002361 semsg(_(e_listidx), n);
2362 status = FAIL;
2363 }
2364 else
2365 // TODO: check for list or item locked
2366 listitem_remove(l, li);
2367 }
2368 }
2369 else
2370 {
2371 status = FAIL;
2372 semsg(_(e_cannot_index_str),
2373 vartype_name(tv_dest->v_type));
2374 }
2375
2376 clear_tv(tv_idx);
2377 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002378 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002379 if (status == FAIL)
2380 goto on_error;
2381 }
2382 break;
2383
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002384 // unlet range of items in list variable
2385 case ISN_UNLETRANGE:
2386 {
2387 // Stack contains:
2388 // -3 index1
2389 // -2 index2
2390 // -1 dict or list
2391 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2392 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2393 typval_T *tv_dest = STACK_TV_BOT(-1);
2394 int status = OK;
2395
2396 if (tv_dest->v_type == VAR_LIST)
2397 {
2398 // indexes must be a number
2399 SOURCING_LNUM = iptr->isn_lnum;
2400 if (check_for_number(tv_idx1) == FAIL
2401 || check_for_number(tv_idx2) == FAIL)
2402 {
2403 status = FAIL;
2404 }
2405 else
2406 {
2407 list_T *l = tv_dest->vval.v_list;
2408 long n1 = (long)tv_idx1->vval.v_number;
2409 long n2 = (long)tv_idx2->vval.v_number;
2410 listitem_T *li;
2411
2412 li = list_find_index(l, &n1);
2413 if (li == NULL
2414 || list_unlet_range(l, li, NULL, n1,
2415 TRUE, n2) == FAIL)
2416 status = FAIL;
2417 }
2418 }
2419 else
2420 {
2421 status = FAIL;
2422 SOURCING_LNUM = iptr->isn_lnum;
2423 semsg(_(e_cannot_index_str),
2424 vartype_name(tv_dest->v_type));
2425 }
2426
2427 clear_tv(tv_idx1);
2428 clear_tv(tv_idx2);
2429 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002430 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002431 if (status == FAIL)
2432 goto on_error;
2433 }
2434 break;
2435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 // push constant
2437 case ISN_PUSHNR:
2438 case ISN_PUSHBOOL:
2439 case ISN_PUSHSPEC:
2440 case ISN_PUSHF:
2441 case ISN_PUSHS:
2442 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002443 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002444 case ISN_PUSHCHANNEL:
2445 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002446 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2447 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002449 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002450 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 switch (iptr->isn_type)
2452 {
2453 case ISN_PUSHNR:
2454 tv->v_type = VAR_NUMBER;
2455 tv->vval.v_number = iptr->isn_arg.number;
2456 break;
2457 case ISN_PUSHBOOL:
2458 tv->v_type = VAR_BOOL;
2459 tv->vval.v_number = iptr->isn_arg.number;
2460 break;
2461 case ISN_PUSHSPEC:
2462 tv->v_type = VAR_SPECIAL;
2463 tv->vval.v_number = iptr->isn_arg.number;
2464 break;
2465#ifdef FEAT_FLOAT
2466 case ISN_PUSHF:
2467 tv->v_type = VAR_FLOAT;
2468 tv->vval.v_float = iptr->isn_arg.fnumber;
2469 break;
2470#endif
2471 case ISN_PUSHBLOB:
2472 blob_copy(iptr->isn_arg.blob, tv);
2473 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002474 case ISN_PUSHFUNC:
2475 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002476 if (iptr->isn_arg.string == NULL)
2477 tv->vval.v_string = NULL;
2478 else
2479 tv->vval.v_string =
2480 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002481 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002482 case ISN_PUSHCHANNEL:
2483#ifdef FEAT_JOB_CHANNEL
2484 tv->v_type = VAR_CHANNEL;
2485 tv->vval.v_channel = iptr->isn_arg.channel;
2486 if (tv->vval.v_channel != NULL)
2487 ++tv->vval.v_channel->ch_refcount;
2488#endif
2489 break;
2490 case ISN_PUSHJOB:
2491#ifdef FEAT_JOB_CHANNEL
2492 tv->v_type = VAR_JOB;
2493 tv->vval.v_job = iptr->isn_arg.job;
2494 if (tv->vval.v_job != NULL)
2495 ++tv->vval.v_job->jv_refcount;
2496#endif
2497 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 default:
2499 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002500 tv->vval.v_string = vim_strsave(
2501 iptr->isn_arg.string == NULL
2502 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 }
2504 break;
2505
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002506 case ISN_UNLET:
2507 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2508 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002509 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002510 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002511 case ISN_UNLETENV:
2512 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2513 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002514
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002515 case ISN_LOCKCONST:
2516 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2517 break;
2518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 // create a list from items on the stack; uses a single allocation
2520 // for the list header and the items
2521 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002522 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
2523 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 break;
2525
2526 // create a dict from items on the stack
2527 case ISN_NEWDICT:
2528 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002529 int count = iptr->isn_arg.number;
2530 dict_T *dict = dict_alloc();
2531 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002532 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002533 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534
2535 if (dict == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002536 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 for (idx = 0; idx < count; ++idx)
2538 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002539 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002541 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002542 key = tv->vval.v_string == NULL
2543 ? (char_u *)"" : tv->vval.v_string;
2544 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002545 if (item != NULL)
2546 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002547 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002548 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002549 dict_unref(dict);
2550 goto on_error;
2551 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002552 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 clear_tv(tv);
2554 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002555 {
2556 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002557 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002558 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2560 item->di_tv.v_lock = 0;
2561 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002562 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002563 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002564 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002565 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 }
2568
2569 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002570 ectx->ec_stack.ga_len -= 2 * count - 1;
2571 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2572 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002574 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 tv = STACK_TV_BOT(-1);
2576 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002577 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 tv->vval.v_dict = dict;
2579 ++dict->dv_refcount;
2580 }
2581 break;
2582
2583 // call a :def function
2584 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002585 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002586 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2587 NULL,
2588 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002589 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002590 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 break;
2592
2593 // call a builtin function
2594 case ISN_BCALL:
2595 SOURCING_LNUM = iptr->isn_lnum;
2596 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2597 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002598 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002599 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 break;
2601
2602 // call a funcref or partial
2603 case ISN_PCALL:
2604 {
2605 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2606 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002607 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608
2609 SOURCING_LNUM = iptr->isn_lnum;
2610 if (pfunc->cpf_top)
2611 {
2612 // funcref is above the arguments
2613 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2614 }
2615 else
2616 {
2617 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002618 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002619 partial_tv = *STACK_TV_BOT(0);
2620 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002622 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002623 if (tv == &partial_tv)
2624 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002626 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 }
2628 break;
2629
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002630 case ISN_PCALL_END:
2631 // PCALL finished, arguments have been consumed and replaced by
2632 // the return value. Now clear the funcref from the stack,
2633 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002634 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002635 clear_tv(STACK_TV_BOT(-1));
2636 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2637 break;
2638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 // call a user defined function or funcref/partial
2640 case ISN_UCALL:
2641 {
2642 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2643
2644 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002645 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002646 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002647 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 }
2649 break;
2650
2651 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002652 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002653 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2654 return FAIL;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002655 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002656 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002657 tv->v_type = VAR_NUMBER;
2658 tv->vval.v_number = 0;
2659 tv->v_lock = 0;
2660 // FALLTHROUGH
2661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 case ISN_RETURN:
2663 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002664 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002665 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002666
2667 if (trystack->ga_len > 0)
2668 trycmd = ((trycmd_T *)trystack->ga_data)
2669 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002670 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002671 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002673 // jump to ":finally" or ":endtry"
2674 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002675 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002676 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002677 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 trycmd->tcd_return = TRUE;
2679 }
2680 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002681 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 }
2683 break;
2684
2685 // push a function reference to a compiled function
2686 case ISN_FUNCREF:
2687 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002688 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2689 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2690 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 if (pt == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002693 return FAIL;
2694 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002695 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002696 vim_free(pt);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002697 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002698 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002699 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002700 ectx) == FAIL)
2701 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002704 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 tv->vval.v_partial = pt;
2706 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002707 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 }
2709 break;
2710
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002711 // Create a global function from a lambda.
2712 case ISN_NEWFUNC:
2713 {
2714 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2715
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002716 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002717 ectx) == FAIL)
2718 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002719 }
2720 break;
2721
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002722 // List functions
2723 case ISN_DEF:
2724 if (iptr->isn_arg.string == NULL)
2725 list_functions(NULL);
2726 else
2727 {
2728 exarg_T ea;
2729
2730 CLEAR_FIELD(ea);
2731 ea.cmd = ea.arg = iptr->isn_arg.string;
2732 define_function(&ea, NULL);
2733 }
2734 break;
2735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 // jump if a condition is met
2737 case ISN_JUMP:
2738 {
2739 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002740 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 int jump = TRUE;
2742
2743 if (when != JUMP_ALWAYS)
2744 {
2745 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002746 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002747 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002748 || when == JUMP_IF_COND_TRUE)
2749 {
2750 SOURCING_LNUM = iptr->isn_lnum;
2751 jump = tv_get_bool_chk(tv, &error);
2752 if (error)
2753 goto on_error;
2754 }
2755 else
2756 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002758 || when == JUMP_AND_KEEP_IF_FALSE
2759 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002761 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 {
2763 // drop the value from the stack
2764 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002765 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 }
2767 }
2768 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002769 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 }
2771 break;
2772
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002773 // Jump if an argument with a default value was already set and not
2774 // v:none.
2775 case ISN_JUMP_IF_ARG_SET:
2776 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2777 if (tv->v_type != VAR_UNKNOWN
2778 && !(tv->v_type == VAR_SPECIAL
2779 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002780 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002781 break;
2782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 // top of a for loop
2784 case ISN_FOR:
2785 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002786 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 typval_T *idxtv =
2788 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2789
Bram Moolenaar4c137212021-04-19 16:48:48 +02002790 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2791 return FAIL;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002792 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002793 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002794 list_T *list = ltv->vval.v_list;
2795
2796 // push the next item from the list
2797 ++idxtv->vval.v_number;
2798 if (list == NULL
2799 || idxtv->vval.v_number >= list->lv_len)
2800 {
2801 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002802 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2803 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002804 }
2805 else if (list->lv_first == &range_list_item)
2806 {
2807 // non-materialized range() list
2808 tv = STACK_TV_BOT(0);
2809 tv->v_type = VAR_NUMBER;
2810 tv->v_lock = 0;
2811 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002813 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002814 }
2815 else
2816 {
2817 listitem_T *li = list_find(list,
2818 idxtv->vval.v_number);
2819
2820 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002821 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002822 }
2823 }
2824 else if (ltv->v_type == VAR_STRING)
2825 {
2826 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002827
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002828 // The index is for the last byte of the previous
2829 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002830 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002831 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002832 {
2833 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002834 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2835 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002836 }
2837 else
2838 {
2839 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2840
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002841 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002842 tv = STACK_TV_BOT(0);
2843 tv->v_type = VAR_STRING;
2844 tv->vval.v_string = vim_strnsave(
2845 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002846 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002847 idxtv->vval.v_number += clen - 1;
2848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002850 else if (ltv->v_type == VAR_BLOB)
2851 {
2852 blob_T *blob = ltv->vval.v_blob;
2853
2854 // When we get here the first time make a copy of the
2855 // blob, so that the iteration still works when it is
2856 // changed.
2857 if (idxtv->vval.v_number == -1 && blob != NULL)
2858 {
2859 blob_copy(blob, ltv);
2860 blob_unref(blob);
2861 blob = ltv->vval.v_blob;
2862 }
2863
2864 // The index is for the previous byte.
2865 ++idxtv->vval.v_number;
2866 if (blob == NULL
2867 || idxtv->vval.v_number >= blob_len(blob))
2868 {
2869 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002870 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2871 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002872 }
2873 else
2874 {
2875 // Push the next byte from the blob.
2876 tv = STACK_TV_BOT(0);
2877 tv->v_type = VAR_NUMBER;
2878 tv->vval.v_number = blob_get(blob,
2879 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002880 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002881 }
2882 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 else
2884 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002885 semsg(_(e_for_loop_on_str_not_supported),
2886 vartype_name(ltv->v_type));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002887 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 }
2889 }
2890 break;
2891
2892 // start of ":try" block
2893 case ISN_TRY:
2894 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002895 trycmd_T *trycmd = NULL;
2896
Bram Moolenaar4c137212021-04-19 16:48:48 +02002897 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
2898 return FAIL;
2899 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
2900 + ectx->ec_trystack.ga_len;
2901 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002903 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002904 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
2905 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002906 trycmd->tcd_catch_idx =
2907 iptr->isn_arg.try.try_ref->try_catch;
2908 trycmd->tcd_finally_idx =
2909 iptr->isn_arg.try.try_ref->try_finally;
2910 trycmd->tcd_endtry_idx =
2911 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 }
2913 break;
2914
2915 case ISN_PUSHEXC:
2916 if (current_exception == NULL)
2917 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002918 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002920 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002922 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2923 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002925 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002927 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 tv->vval.v_string = vim_strsave(
2929 (char_u *)current_exception->value);
2930 break;
2931
2932 case ISN_CATCH:
2933 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002934 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 if (trystack->ga_len > 0)
2938 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002939 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 + trystack->ga_len - 1;
2941 trycmd->tcd_caught = TRUE;
2942 }
2943 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002944 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 catch_exception(current_exception);
2946 }
2947 break;
2948
Bram Moolenaarc150c092021-02-13 15:02:46 +01002949 case ISN_TRYCONT:
2950 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002951 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002952 trycont_T *trycont = &iptr->isn_arg.trycont;
2953 int i;
2954 trycmd_T *trycmd;
2955 int iidx = trycont->tct_where;
2956
2957 if (trystack->ga_len < trycont->tct_levels)
2958 {
2959 siemsg("TRYCONT: expected %d levels, found %d",
2960 trycont->tct_levels, trystack->ga_len);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002961 return FAIL;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002962 }
2963 // Make :endtry jump to any outer try block and the last
2964 // :endtry inside the loop to the loop start.
2965 for (i = trycont->tct_levels; i > 0; --i)
2966 {
2967 trycmd = ((trycmd_T *)trystack->ga_data)
2968 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002969 // Add one to tcd_cont to be able to jump to
2970 // instruction with index zero.
2971 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002972 iidx = trycmd->tcd_finally_idx == 0
2973 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002974 }
2975 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02002976 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002977 }
2978 break;
2979
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002980 case ISN_FINALLY:
2981 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002982 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002983 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2984 + trystack->ga_len - 1;
2985
2986 // Reset the index to avoid a return statement jumps here
2987 // again.
2988 trycmd->tcd_finally_idx = 0;
2989 break;
2990 }
2991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 // end of ":try" block
2993 case ISN_ENDTRY:
2994 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002995 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996
2997 if (trystack->ga_len > 0)
2998 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002999 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 --trystack->ga_len;
3002 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003003 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 trycmd = ((trycmd_T *)trystack->ga_data)
3005 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003006 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 {
3008 // discard the exception
3009 if (caught_stack == current_exception)
3010 caught_stack = caught_stack->caught;
3011 discard_current_exception();
3012 }
3013
3014 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003015 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003016
Bram Moolenaar4c137212021-04-19 16:48:48 +02003017 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003018 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003019 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003020 clear_tv(STACK_TV_BOT(0));
3021 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003022 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003023 // handling :continue: jump to outer try block or
3024 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003025 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
3027 }
3028 break;
3029
3030 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003031 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003032 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003033
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003034 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3035 {
3036 // throwing an exception while using "silent!" causes
3037 // the function to abort but not display an error.
3038 tv = STACK_TV_BOT(-1);
3039 clear_tv(tv);
3040 tv->v_type = VAR_NUMBER;
3041 tv->vval.v_number = 0;
3042 goto done;
3043 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003044 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003045 tv = STACK_TV_BOT(0);
3046 if (tv->vval.v_string == NULL
3047 || *skipwhite(tv->vval.v_string) == NUL)
3048 {
3049 vim_free(tv->vval.v_string);
3050 SOURCING_LNUM = iptr->isn_lnum;
3051 emsg(_(e_throw_with_empty_string));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003052 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003053 }
3054
3055 // Inside a "catch" we need to first discard the caught
3056 // exception.
3057 if (trystack->ga_len > 0)
3058 {
3059 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3060 + trystack->ga_len - 1;
3061 if (trycmd->tcd_caught && current_exception != NULL)
3062 {
3063 // discard the exception
3064 if (caught_stack == current_exception)
3065 caught_stack = caught_stack->caught;
3066 discard_current_exception();
3067 trycmd->tcd_caught = FALSE;
3068 }
3069 }
3070
3071 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3072 == FAIL)
3073 {
3074 vim_free(tv->vval.v_string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003075 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003076 }
3077 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 break;
3080
3081 // compare with special values
3082 case ISN_COMPAREBOOL:
3083 case ISN_COMPARESPECIAL:
3084 {
3085 typval_T *tv1 = STACK_TV_BOT(-2);
3086 typval_T *tv2 = STACK_TV_BOT(-1);
3087 varnumber_T arg1 = tv1->vval.v_number;
3088 varnumber_T arg2 = tv2->vval.v_number;
3089 int res;
3090
3091 switch (iptr->isn_arg.op.op_type)
3092 {
3093 case EXPR_EQUAL: res = arg1 == arg2; break;
3094 case EXPR_NEQUAL: res = arg1 != arg2; break;
3095 default: res = 0; break;
3096 }
3097
Bram Moolenaar4c137212021-04-19 16:48:48 +02003098 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 tv1->v_type = VAR_BOOL;
3100 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3101 }
3102 break;
3103
3104 // Operation with two number arguments
3105 case ISN_OPNR:
3106 case ISN_COMPARENR:
3107 {
3108 typval_T *tv1 = STACK_TV_BOT(-2);
3109 typval_T *tv2 = STACK_TV_BOT(-1);
3110 varnumber_T arg1 = tv1->vval.v_number;
3111 varnumber_T arg2 = tv2->vval.v_number;
3112 varnumber_T res;
3113
3114 switch (iptr->isn_arg.op.op_type)
3115 {
3116 case EXPR_MULT: res = arg1 * arg2; break;
3117 case EXPR_DIV: res = arg1 / arg2; break;
3118 case EXPR_REM: res = arg1 % arg2; break;
3119 case EXPR_SUB: res = arg1 - arg2; break;
3120 case EXPR_ADD: res = arg1 + arg2; break;
3121
3122 case EXPR_EQUAL: res = arg1 == arg2; break;
3123 case EXPR_NEQUAL: res = arg1 != arg2; break;
3124 case EXPR_GREATER: res = arg1 > arg2; break;
3125 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3126 case EXPR_SMALLER: res = arg1 < arg2; break;
3127 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3128 default: res = 0; break;
3129 }
3130
Bram Moolenaar4c137212021-04-19 16:48:48 +02003131 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 if (iptr->isn_type == ISN_COMPARENR)
3133 {
3134 tv1->v_type = VAR_BOOL;
3135 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3136 }
3137 else
3138 tv1->vval.v_number = res;
3139 }
3140 break;
3141
3142 // Computation with two float arguments
3143 case ISN_OPFLOAT:
3144 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003145#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
3147 typval_T *tv1 = STACK_TV_BOT(-2);
3148 typval_T *tv2 = STACK_TV_BOT(-1);
3149 float_T arg1 = tv1->vval.v_float;
3150 float_T arg2 = tv2->vval.v_float;
3151 float_T res = 0;
3152 int cmp = FALSE;
3153
3154 switch (iptr->isn_arg.op.op_type)
3155 {
3156 case EXPR_MULT: res = arg1 * arg2; break;
3157 case EXPR_DIV: res = arg1 / arg2; break;
3158 case EXPR_SUB: res = arg1 - arg2; break;
3159 case EXPR_ADD: res = arg1 + arg2; break;
3160
3161 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3162 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3163 case EXPR_GREATER: cmp = arg1 > arg2; break;
3164 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3165 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3166 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3167 default: cmp = 0; break;
3168 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003169 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 if (iptr->isn_type == ISN_COMPAREFLOAT)
3171 {
3172 tv1->v_type = VAR_BOOL;
3173 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3174 }
3175 else
3176 tv1->vval.v_float = res;
3177 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003178#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 break;
3180
3181 case ISN_COMPARELIST:
3182 {
3183 typval_T *tv1 = STACK_TV_BOT(-2);
3184 typval_T *tv2 = STACK_TV_BOT(-1);
3185 list_T *arg1 = tv1->vval.v_list;
3186 list_T *arg2 = tv2->vval.v_list;
3187 int cmp = FALSE;
3188 int ic = iptr->isn_arg.op.op_ic;
3189
3190 switch (iptr->isn_arg.op.op_type)
3191 {
3192 case EXPR_EQUAL: cmp =
3193 list_equal(arg1, arg2, ic, FALSE); break;
3194 case EXPR_NEQUAL: cmp =
3195 !list_equal(arg1, arg2, ic, FALSE); break;
3196 case EXPR_IS: cmp = arg1 == arg2; break;
3197 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3198 default: cmp = 0; break;
3199 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003200 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 clear_tv(tv1);
3202 clear_tv(tv2);
3203 tv1->v_type = VAR_BOOL;
3204 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3205 }
3206 break;
3207
3208 case ISN_COMPAREBLOB:
3209 {
3210 typval_T *tv1 = STACK_TV_BOT(-2);
3211 typval_T *tv2 = STACK_TV_BOT(-1);
3212 blob_T *arg1 = tv1->vval.v_blob;
3213 blob_T *arg2 = tv2->vval.v_blob;
3214 int cmp = FALSE;
3215
3216 switch (iptr->isn_arg.op.op_type)
3217 {
3218 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3219 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3220 case EXPR_IS: cmp = arg1 == arg2; break;
3221 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3222 default: cmp = 0; break;
3223 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003224 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 clear_tv(tv1);
3226 clear_tv(tv2);
3227 tv1->v_type = VAR_BOOL;
3228 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3229 }
3230 break;
3231
3232 // TODO: handle separately
3233 case ISN_COMPARESTRING:
3234 case ISN_COMPAREDICT:
3235 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 case ISN_COMPAREANY:
3237 {
3238 typval_T *tv1 = STACK_TV_BOT(-2);
3239 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003240 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 int ic = iptr->isn_arg.op.op_ic;
3242
Bram Moolenaareb26f432020-09-14 16:50:05 +02003243 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003244 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003246 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 }
3248 break;
3249
3250 case ISN_ADDLIST:
3251 case ISN_ADDBLOB:
3252 {
3253 typval_T *tv1 = STACK_TV_BOT(-2);
3254 typval_T *tv2 = STACK_TV_BOT(-1);
3255
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003256 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 if (iptr->isn_type == ISN_ADDLIST)
3258 eval_addlist(tv1, tv2);
3259 else
3260 eval_addblob(tv1, tv2);
3261 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003262 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 }
3264 break;
3265
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003266 case ISN_LISTAPPEND:
3267 {
3268 typval_T *tv1 = STACK_TV_BOT(-2);
3269 typval_T *tv2 = STACK_TV_BOT(-1);
3270 list_T *l = tv1->vval.v_list;
3271
3272 // add an item to a list
3273 if (l == NULL)
3274 {
3275 SOURCING_LNUM = iptr->isn_lnum;
3276 emsg(_(e_cannot_add_to_null_list));
3277 goto on_error;
3278 }
3279 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003280 return FAIL;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003281 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003282 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003283 }
3284 break;
3285
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003286 case ISN_BLOBAPPEND:
3287 {
3288 typval_T *tv1 = STACK_TV_BOT(-2);
3289 typval_T *tv2 = STACK_TV_BOT(-1);
3290 blob_T *b = tv1->vval.v_blob;
3291 int error = FALSE;
3292 varnumber_T n;
3293
3294 // add a number to a blob
3295 if (b == NULL)
3296 {
3297 SOURCING_LNUM = iptr->isn_lnum;
3298 emsg(_(e_cannot_add_to_null_blob));
3299 goto on_error;
3300 }
3301 n = tv_get_number_chk(tv2, &error);
3302 if (error)
3303 goto on_error;
3304 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003305 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003306 }
3307 break;
3308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 // Computation with two arguments of unknown type
3310 case ISN_OPANY:
3311 {
3312 typval_T *tv1 = STACK_TV_BOT(-2);
3313 typval_T *tv2 = STACK_TV_BOT(-1);
3314 varnumber_T n1, n2;
3315#ifdef FEAT_FLOAT
3316 float_T f1 = 0, f2 = 0;
3317#endif
3318 int error = FALSE;
3319
3320 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3321 {
3322 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3323 {
3324 eval_addlist(tv1, tv2);
3325 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003326 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 break;
3328 }
3329 else if (tv1->v_type == VAR_BLOB
3330 && tv2->v_type == VAR_BLOB)
3331 {
3332 eval_addblob(tv1, tv2);
3333 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003334 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 break;
3336 }
3337 }
3338#ifdef FEAT_FLOAT
3339 if (tv1->v_type == VAR_FLOAT)
3340 {
3341 f1 = tv1->vval.v_float;
3342 n1 = 0;
3343 }
3344 else
3345#endif
3346 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003347 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 n1 = tv_get_number_chk(tv1, &error);
3349 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003350 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351#ifdef FEAT_FLOAT
3352 if (tv2->v_type == VAR_FLOAT)
3353 f1 = n1;
3354#endif
3355 }
3356#ifdef FEAT_FLOAT
3357 if (tv2->v_type == VAR_FLOAT)
3358 {
3359 f2 = tv2->vval.v_float;
3360 n2 = 0;
3361 }
3362 else
3363#endif
3364 {
3365 n2 = tv_get_number_chk(tv2, &error);
3366 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003367 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368#ifdef FEAT_FLOAT
3369 if (tv1->v_type == VAR_FLOAT)
3370 f2 = n2;
3371#endif
3372 }
3373#ifdef FEAT_FLOAT
3374 // if there is a float on either side the result is a float
3375 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3376 {
3377 switch (iptr->isn_arg.op.op_type)
3378 {
3379 case EXPR_MULT: f1 = f1 * f2; break;
3380 case EXPR_DIV: f1 = f1 / f2; break;
3381 case EXPR_SUB: f1 = f1 - f2; break;
3382 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003383 default: SOURCING_LNUM = iptr->isn_lnum;
3384 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003385 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 }
3387 clear_tv(tv1);
3388 clear_tv(tv2);
3389 tv1->v_type = VAR_FLOAT;
3390 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003391 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 }
3393 else
3394#endif
3395 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003396 int failed = FALSE;
3397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 switch (iptr->isn_arg.op.op_type)
3399 {
3400 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003401 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3402 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003403 goto on_error;
3404 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 case EXPR_SUB: n1 = n1 - n2; break;
3406 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003407 default: n1 = num_modulus(n1, n2, &failed);
3408 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003409 goto on_error;
3410 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 }
3412 clear_tv(tv1);
3413 clear_tv(tv2);
3414 tv1->v_type = VAR_NUMBER;
3415 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 }
3418 }
3419 break;
3420
3421 case ISN_CONCAT:
3422 {
3423 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3424 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3425 char_u *res;
3426
3427 res = concat_str(str1, str2);
3428 clear_tv(STACK_TV_BOT(-2));
3429 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003430 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 STACK_TV_BOT(-1)->vval.v_string = res;
3432 }
3433 break;
3434
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003435 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003436 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003437 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003438 int is_slice = iptr->isn_type == ISN_STRSLICE;
3439 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003440 char_u *res;
3441
3442 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003443 // string slice: string is at stack-3, first index at
3444 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003445 if (is_slice)
3446 {
3447 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003448 n1 = tv->vval.v_number;
3449 }
3450
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003451 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003452 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003453
Bram Moolenaar4c137212021-04-19 16:48:48 +02003454 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003455 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003456 if (is_slice)
3457 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003458 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003459 else
3460 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003461 // single character (including composing characters).
3462 // If the index is too big or negative the result is
3463 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003464 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003465 vim_free(tv->vval.v_string);
3466 tv->vval.v_string = res;
3467 }
3468 break;
3469
3470 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003471 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003472 case ISN_BLOBINDEX:
3473 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003475 int is_slice = iptr->isn_type == ISN_LISTSLICE
3476 || iptr->isn_type == ISN_BLOBSLICE;
3477 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3478 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003479 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003480 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481
3482 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003483 // list slice: list is at stack-3, indexes at stack-2 and
3484 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003485 // Same for blob.
3486 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487
3488 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003489 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003491
3492 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 {
Bram Moolenaared591872020-08-15 22:14:53 +02003494 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003495 n1 = tv->vval.v_number;
3496 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 }
Bram Moolenaared591872020-08-15 22:14:53 +02003498
Bram Moolenaar4c137212021-04-19 16:48:48 +02003499 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003500 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003501 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003502 if (is_blob)
3503 {
3504 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3505 n1, n2, FALSE, tv) == FAIL)
3506 goto on_error;
3507 }
3508 else
3509 {
3510 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3511 n1, n2, FALSE, tv, TRUE) == FAIL)
3512 goto on_error;
3513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 }
3515 break;
3516
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003517 case ISN_ANYINDEX:
3518 case ISN_ANYSLICE:
3519 {
3520 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3521 typval_T *var1, *var2;
3522 int res;
3523
3524 // index: composite is at stack-2, index at stack-1
3525 // slice: composite is at stack-3, indexes at stack-2 and
3526 // stack-1
3527 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003528 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003529 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3530 goto on_error;
3531 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3532 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003533 res = eval_index_inner(tv, is_slice, var1, var2,
3534 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003535 clear_tv(var1);
3536 if (is_slice)
3537 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003538 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003539 if (res == FAIL)
3540 goto on_error;
3541 }
3542 break;
3543
Bram Moolenaar9af78762020-06-16 11:34:42 +02003544 case ISN_SLICE:
3545 {
3546 list_T *list;
3547 int count = iptr->isn_arg.number;
3548
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003549 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003550 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003551 list = tv->vval.v_list;
3552
3553 // no error for short list, expect it to be checked earlier
3554 if (list != NULL && list->lv_len >= count)
3555 {
3556 list_T *newlist = list_slice(list,
3557 count, list->lv_len - 1);
3558
3559 if (newlist != NULL)
3560 {
3561 list_unref(list);
3562 tv->vval.v_list = newlist;
3563 ++newlist->lv_refcount;
3564 }
3565 }
3566 }
3567 break;
3568
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003569 case ISN_GETITEM:
3570 {
3571 listitem_T *li;
3572 int index = iptr->isn_arg.number;
3573
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003574 // Get list item: list is at stack-1, push item.
3575 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003576 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003577 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003578
Bram Moolenaar4c137212021-04-19 16:48:48 +02003579 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3580 return FAIL;
3581 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003582 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003583
3584 // Useful when used in unpack assignment. Reset at
3585 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003586 ectx->ec_where.wt_index = index + 1;
3587 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003588 }
3589 break;
3590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 case ISN_MEMBER:
3592 {
3593 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003594 char_u *key;
3595 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003596 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003597
3598 // dict member: dict is at stack-2, key at stack-1
3599 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003600 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003601 dict = tv->vval.v_dict;
3602
3603 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003604 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003605 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003606 if (key == NULL)
3607 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003608
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003609 if ((di = dict_find(dict, key, -1)) == NULL)
3610 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003611 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003612 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003613
3614 // If :silent! is used we will continue, make sure the
3615 // stack contents makes sense.
3616 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003617 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003618 tv = STACK_TV_BOT(-1);
3619 clear_tv(tv);
3620 tv->v_type = VAR_NUMBER;
3621 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003622 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003623 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003624 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003626 // Clear the dict only after getting the item, to avoid
3627 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003628 tv = STACK_TV_BOT(-1);
3629 temp_tv = *tv;
3630 copy_tv(&di->di_tv, tv);
3631 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003632 }
3633 break;
3634
3635 // dict member with string key
3636 case ISN_STRINGMEMBER:
3637 {
3638 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003640 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641
3642 tv = STACK_TV_BOT(-1);
3643 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3644 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003645 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003647 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 }
3649 dict = tv->vval.v_dict;
3650
3651 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3652 == NULL)
3653 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003654 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003656 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003658 // Clear the dict after getting the item, to avoid that it
3659 // make the item invalid.
3660 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003662 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
3664 break;
3665
3666 case ISN_NEGATENR:
3667 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003668 if (tv->v_type != VAR_NUMBER
3669#ifdef FEAT_FLOAT
3670 && tv->v_type != VAR_FLOAT
3671#endif
3672 )
3673 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003674 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003675 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003676 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003677 }
3678#ifdef FEAT_FLOAT
3679 if (tv->v_type == VAR_FLOAT)
3680 tv->vval.v_float = -tv->vval.v_float;
3681 else
3682#endif
3683 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 break;
3685
3686 case ISN_CHECKNR:
3687 {
3688 int error = FALSE;
3689
3690 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003691 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003693 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 (void)tv_get_number_chk(tv, &error);
3695 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003696 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 }
3698 break;
3699
3700 case ISN_CHECKTYPE:
3701 {
3702 checktype_T *ct = &iptr->isn_arg.type;
3703
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003704 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003705 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003706 if (!ectx->ec_where.wt_variable)
3707 ectx->ec_where.wt_index = ct->ct_arg_idx;
3708 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3709 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003710 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003711 if (!ectx->ec_where.wt_variable)
3712 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003713
3714 // number 0 is FALSE, number 1 is TRUE
3715 if (tv->v_type == VAR_NUMBER
3716 && ct->ct_type->tt_type == VAR_BOOL
3717 && (tv->vval.v_number == 0
3718 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003720 tv->v_type = VAR_BOOL;
3721 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003722 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 }
3724 }
3725 break;
3726
Bram Moolenaar9af78762020-06-16 11:34:42 +02003727 case ISN_CHECKLEN:
3728 {
3729 int min_len = iptr->isn_arg.checklen.cl_min_len;
3730 list_T *list = NULL;
3731
3732 tv = STACK_TV_BOT(-1);
3733 if (tv->v_type == VAR_LIST)
3734 list = tv->vval.v_list;
3735 if (list == NULL || list->lv_len < min_len
3736 || (list->lv_len > min_len
3737 && !iptr->isn_arg.checklen.cl_more_OK))
3738 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003739 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003740 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003741 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003742 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003743 }
3744 }
3745 break;
3746
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003747 case ISN_SETTYPE:
3748 {
3749 checktype_T *ct = &iptr->isn_arg.type;
3750
3751 tv = STACK_TV_BOT(-1);
3752 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3753 {
3754 free_type(tv->vval.v_dict->dv_type);
3755 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3756 }
3757 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3758 {
3759 free_type(tv->vval.v_list->lv_type);
3760 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3761 }
3762 }
3763 break;
3764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003766 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 {
3768 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003769 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770
3771 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003772 if (iptr->isn_type == ISN_2BOOL)
3773 {
3774 n = tv2bool(tv);
3775 if (iptr->isn_arg.number) // invert
3776 n = !n;
3777 }
3778 else
3779 {
3780 SOURCING_LNUM = iptr->isn_lnum;
3781 n = tv_get_bool_chk(tv, &error);
3782 if (error)
3783 goto on_error;
3784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 clear_tv(tv);
3786 tv->v_type = VAR_BOOL;
3787 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3788 }
3789 break;
3790
3791 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003792 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003793 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003794 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3795 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3796 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 break;
3798
Bram Moolenaar08597872020-12-10 19:43:40 +01003799 case ISN_RANGE:
3800 {
3801 exarg_T ea;
3802 char *errormsg;
3803
Bram Moolenaarece0b872021-01-08 20:40:45 +01003804 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003805 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003806 ea.addr_type = ADDR_LINES;
3807 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003808 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003809 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003810 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003811
Bram Moolenaar4c137212021-04-19 16:48:48 +02003812 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3813 return FAIL;
3814 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003815 tv = STACK_TV_BOT(-1);
3816 tv->v_type = VAR_NUMBER;
3817 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003818 if (ea.addr_count == 0)
3819 tv->vval.v_number = curwin->w_cursor.lnum;
3820 else
3821 tv->vval.v_number = ea.line2;
3822 }
3823 break;
3824
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003825 case ISN_PUT:
3826 {
3827 int regname = iptr->isn_arg.put.put_regname;
3828 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3829 char_u *expr = NULL;
3830 int dir = FORWARD;
3831
Bram Moolenaar08597872020-12-10 19:43:40 +01003832 if (lnum < -2)
3833 {
3834 // line number was put on the stack by ISN_RANGE
3835 tv = STACK_TV_BOT(-1);
3836 curwin->w_cursor.lnum = tv->vval.v_number;
3837 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3838 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003840 }
3841 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003842 // :put! above cursor
3843 dir = BACKWARD;
3844 else if (lnum >= 0)
3845 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003846
3847 if (regname == '=')
3848 {
3849 tv = STACK_TV_BOT(-1);
3850 if (tv->v_type == VAR_STRING)
3851 expr = tv->vval.v_string;
3852 else
3853 {
3854 expr = typval2string(tv, TRUE); // allocates value
3855 clear_tv(tv);
3856 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003857 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003858 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003859 check_cursor();
3860 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3861 vim_free(expr);
3862 }
3863 break;
3864
Bram Moolenaar02194d22020-10-24 23:08:38 +02003865 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003866 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3867 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3868 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3869 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003870 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3871 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003872 break;
3873
Bram Moolenaar02194d22020-10-24 23:08:38 +02003874 case ISN_CMDMOD_REV:
3875 // filter regprog is owned by the instruction, don't free it
3876 cmdmod.cmod_filter_regmatch.regprog = NULL;
3877 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003878 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
3879 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003880 break;
3881
Bram Moolenaar792f7862020-11-23 08:31:18 +01003882 case ISN_UNPACK:
3883 {
3884 int count = iptr->isn_arg.unpack.unp_count;
3885 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3886 list_T *l;
3887 listitem_T *li;
3888 int i;
3889
3890 // Check there is a valid list to unpack.
3891 tv = STACK_TV_BOT(-1);
3892 if (tv->v_type != VAR_LIST)
3893 {
3894 SOURCING_LNUM = iptr->isn_lnum;
3895 emsg(_(e_for_argument_must_be_sequence_of_lists));
3896 goto on_error;
3897 }
3898 l = tv->vval.v_list;
3899 if (l == NULL
3900 || l->lv_len < (semicolon ? count - 1 : count))
3901 {
3902 SOURCING_LNUM = iptr->isn_lnum;
3903 emsg(_(e_list_value_does_not_have_enough_items));
3904 goto on_error;
3905 }
3906 else if (!semicolon && l->lv_len > count)
3907 {
3908 SOURCING_LNUM = iptr->isn_lnum;
3909 emsg(_(e_list_value_has_more_items_than_targets));
3910 goto on_error;
3911 }
3912
3913 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003914 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
3915 return FAIL;
3916 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003917
3918 // Variable after semicolon gets a list with the remaining
3919 // items.
3920 if (semicolon)
3921 {
3922 list_T *rem_list =
3923 list_alloc_with_items(l->lv_len - count + 1);
3924
3925 if (rem_list == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003926 return FAIL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003927 tv = STACK_TV_BOT(-count);
3928 tv->vval.v_list = rem_list;
3929 ++rem_list->lv_refcount;
3930 tv->v_lock = 0;
3931 li = l->lv_first;
3932 for (i = 0; i < count - 1; ++i)
3933 li = li->li_next;
3934 for (i = 0; li != NULL; ++i)
3935 {
3936 list_set_item(rem_list, i, &li->li_tv);
3937 li = li->li_next;
3938 }
3939 --count;
3940 }
3941
3942 // Produce the values in reverse order, first item last.
3943 li = l->lv_first;
3944 for (i = 0; i < count; ++i)
3945 {
3946 tv = STACK_TV_BOT(-i - 1);
3947 copy_tv(&li->li_tv, tv);
3948 li = li->li_next;
3949 }
3950
3951 list_unref(l);
3952 }
3953 break;
3954
Bram Moolenaarb2049902021-01-24 12:53:53 +01003955 case ISN_PROF_START:
3956 case ISN_PROF_END:
3957 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003958#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003959 funccall_T cookie;
3960 ufunc_T *cur_ufunc =
3961 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003962 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003963
3964 cookie.func = cur_ufunc;
3965 if (iptr->isn_type == ISN_PROF_START)
3966 {
3967 func_line_start(&cookie, iptr->isn_lnum);
3968 // if we get here the instruction is executed
3969 func_line_exec(&cookie);
3970 }
3971 else
3972 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003973#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003974 }
3975 break;
3976
Bram Moolenaar389df252020-07-09 21:20:47 +02003977 case ISN_SHUFFLE:
3978 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003979 typval_T tmp_tv;
3980 int item = iptr->isn_arg.shuffle.shfl_item;
3981 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003982
3983 tmp_tv = *STACK_TV_BOT(-item);
3984 for ( ; up > 0 && item > 1; --up)
3985 {
3986 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3987 --item;
3988 }
3989 *STACK_TV_BOT(-item) = tmp_tv;
3990 }
3991 break;
3992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003994 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003996 ectx->ec_where.wt_index = 0;
3997 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 break;
3999 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004000 continue;
4001
Bram Moolenaard032f342020-07-18 18:13:02 +02004002func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004003 // Restore previous function. If the frame pointer is where we started
4004 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004005 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004006 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004007
Bram Moolenaar4c137212021-04-19 16:48:48 +02004008 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004009 // only fails when out of memory
Bram Moolenaar4c137212021-04-19 16:48:48 +02004010 return FAIL;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004011 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004012
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004013on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004014 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004015 // If "emsg_silent" is set then ignore the error, unless it was set
4016 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004017 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004018 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004019 {
4020 // If a sequence of instructions causes an error while ":silent!"
4021 // was used, restore the stack length and jump ahead to restoring
4022 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004023 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004024 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004025 while (ectx->ec_stack.ga_len
4026 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004027 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004028 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004029 clear_tv(STACK_TV_BOT(0));
4030 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004031 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4032 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004033 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004034 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004035 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004036on_fatal_error:
4037 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004038 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004039 if (trylevel <= ectx->ec_trylevel_at_start)
4040 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 }
4042
4043done:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004044 return OK;
4045}
4046
4047/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004048 * Execute the instructions from a VAR_INSTR typeval and put the result in
4049 * "rettv".
4050 * Return OK or FAIL.
4051 */
4052 int
4053exe_typval_instr(typval_T *tv, typval_T *rettv)
4054{
4055 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4056 isn_T *save_instr = ectx->ec_instr;
4057 int save_iidx = ectx->ec_iidx;
4058 int res;
4059
4060 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4061 res = exec_instructions(ectx);
4062 if (res == OK)
4063 {
4064 *rettv = *STACK_TV_BOT(-1);
4065 --ectx->ec_stack.ga_len;
4066 }
4067
4068 ectx->ec_instr = save_instr;
4069 ectx->ec_iidx = save_iidx;
4070
4071 return res;
4072}
4073
4074/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004075 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4076 * "substitute_instr".
4077 */
4078 char_u *
4079exe_substitute_instr(void)
4080{
4081 ectx_T *ectx = substitute_instr->subs_ectx;
4082 isn_T *save_instr = ectx->ec_instr;
4083 int save_iidx = ectx->ec_iidx;
4084 char_u *res;
4085
4086 ectx->ec_instr = substitute_instr->subs_instr;
4087 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004088 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004089 typval_T *tv = STACK_TV_BOT(-1);
4090
4091 res = vim_strsave(tv_get_string(tv));
4092 --ectx->ec_stack.ga_len;
4093 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004094 }
4095 else
4096 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004097 substitute_instr->subs_status = FAIL;
4098 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100
Bram Moolenaar4c137212021-04-19 16:48:48 +02004101 ectx->ec_instr = save_instr;
4102 ectx->ec_iidx = save_iidx;
4103
4104 return res;
4105}
4106
4107/*
4108 * Call a "def" function from old Vim script.
4109 * Return OK or FAIL.
4110 */
4111 int
4112call_def_function(
4113 ufunc_T *ufunc,
4114 int argc_arg, // nr of arguments
4115 typval_T *argv, // arguments
4116 partial_T *partial, // optional partial for context
4117 typval_T *rettv) // return value
4118{
4119 ectx_T ectx; // execution context
4120 int argc = argc_arg;
4121 typval_T *tv;
4122 int idx;
4123 int ret = FAIL;
4124 int defcount = ufunc->uf_args.ga_len - argc;
4125 sctx_T save_current_sctx = current_sctx;
4126 int did_emsg_before = did_emsg_cumul + did_emsg;
4127 int save_suppress_errthrow = suppress_errthrow;
4128 msglist_T **saved_msg_list = NULL;
4129 msglist_T *private_msg_list = NULL;
4130 int save_emsg_silent_def = emsg_silent_def;
4131 int save_did_emsg_def = did_emsg_def;
4132 int orig_funcdepth;
4133
4134// Get pointer to item in the stack.
4135#undef STACK_TV
4136#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4137
4138// Get pointer to item at the bottom of the stack, -1 is the bottom.
4139#undef STACK_TV_BOT
4140#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4141
4142// Get pointer to a local variable on the stack. Negative for arguments.
4143#undef STACK_TV_VAR
4144#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4145
4146 if (ufunc->uf_def_status == UF_NOT_COMPILED
4147 || ufunc->uf_def_status == UF_COMPILE_ERROR
4148 || (func_needs_compiling(ufunc, PROFILING(ufunc))
4149 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
4150 == FAIL))
4151 {
4152 if (did_emsg_cumul + did_emsg == did_emsg_before)
4153 semsg(_(e_function_is_not_compiled_str),
4154 printable_func_name(ufunc));
4155 return FAIL;
4156 }
4157
4158 {
4159 // Check the function was really compiled.
4160 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4161 + ufunc->uf_dfunc_idx;
4162 if (INSTRUCTIONS(dfunc) == NULL)
4163 {
4164 iemsg("using call_def_function() on not compiled function");
4165 return FAIL;
4166 }
4167 }
4168
4169 // If depth of calling is getting too high, don't execute the function.
4170 orig_funcdepth = funcdepth_get();
4171 if (funcdepth_increment() == FAIL)
4172 return FAIL;
4173
4174 CLEAR_FIELD(ectx);
4175 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4176 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4177 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4178 {
4179 funcdepth_decrement();
4180 return FAIL;
4181 }
4182 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4183 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4184 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004185
4186 idx = argc - ufunc->uf_args.ga_len;
4187 if (idx > 0 && ufunc->uf_va_name == NULL)
4188 {
4189 if (idx == 1)
4190 emsg(_(e_one_argument_too_many));
4191 else
4192 semsg(_(e_nr_arguments_too_many), idx);
4193 goto failed_early;
4194 }
4195
4196 // Put arguments on the stack, but no more than what the function expects.
4197 // A lambda can be called with more arguments than it uses.
4198 for (idx = 0; idx < argc
4199 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4200 ++idx)
4201 {
4202 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4203 && argv[idx].v_type == VAR_SPECIAL
4204 && argv[idx].vval.v_number == VVAL_NONE)
4205 {
4206 // Use the default value.
4207 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4208 }
4209 else
4210 {
4211 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4212 && check_typval_arg_type(
4213 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4214 goto failed_early;
4215 copy_tv(&argv[idx], STACK_TV_BOT(0));
4216 }
4217 ++ectx.ec_stack.ga_len;
4218 }
4219
4220 // Turn varargs into a list. Empty list if no args.
4221 if (ufunc->uf_va_name != NULL)
4222 {
4223 int vararg_count = argc - ufunc->uf_args.ga_len;
4224
4225 if (vararg_count < 0)
4226 vararg_count = 0;
4227 else
4228 argc -= vararg_count;
4229 if (exe_newlist(vararg_count, &ectx) == FAIL)
4230 goto failed_early;
4231
4232 // Check the type of the list items.
4233 tv = STACK_TV_BOT(-1);
4234 if (ufunc->uf_va_type != NULL
4235 && ufunc->uf_va_type != &t_list_any
4236 && ufunc->uf_va_type->tt_member != &t_any
4237 && tv->vval.v_list != NULL)
4238 {
4239 type_T *expected = ufunc->uf_va_type->tt_member;
4240 listitem_T *li = tv->vval.v_list->lv_first;
4241
4242 for (idx = 0; idx < vararg_count; ++idx)
4243 {
4244 if (check_typval_arg_type(expected, &li->li_tv,
4245 argc + idx + 1) == FAIL)
4246 goto failed_early;
4247 li = li->li_next;
4248 }
4249 }
4250
4251 if (defcount > 0)
4252 // Move varargs list to below missing default arguments.
4253 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4254 --ectx.ec_stack.ga_len;
4255 }
4256
4257 // Make space for omitted arguments, will store default value below.
4258 // Any varargs list goes after them.
4259 if (defcount > 0)
4260 for (idx = 0; idx < defcount; ++idx)
4261 {
4262 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4263 ++ectx.ec_stack.ga_len;
4264 }
4265 if (ufunc->uf_va_name != NULL)
4266 ++ectx.ec_stack.ga_len;
4267
4268 // Frame pointer points to just after arguments.
4269 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4270 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4271
4272 {
4273 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4274 + ufunc->uf_dfunc_idx;
4275 ufunc_T *base_ufunc = dfunc->df_ufunc;
4276
4277 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4278 // by copy_func().
4279 if (partial != NULL || base_ufunc->uf_partial != NULL)
4280 {
4281 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
4282 if (ectx.ec_outer == NULL)
4283 goto failed_early;
4284 if (partial != NULL)
4285 {
4286 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4287 {
4288 if (current_ectx->ec_outer != NULL)
4289 *ectx.ec_outer = *current_ectx->ec_outer;
4290 }
4291 else
4292 *ectx.ec_outer = partial->pt_outer;
4293 }
4294 else
4295 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
4296 ectx.ec_outer->out_up_is_copy = TRUE;
4297 }
4298 }
4299
4300 // dummy frame entries
4301 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4302 {
4303 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4304 ++ectx.ec_stack.ga_len;
4305 }
4306
4307 {
4308 // Reserve space for local variables and any closure reference count.
4309 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4310 + ufunc->uf_dfunc_idx;
4311
4312 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4313 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4314 ectx.ec_stack.ga_len += dfunc->df_varcount;
4315 if (dfunc->df_has_closure)
4316 {
4317 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4318 STACK_TV_VAR(idx)->vval.v_number = 0;
4319 ++ectx.ec_stack.ga_len;
4320 }
4321
4322 ectx.ec_instr = INSTRUCTIONS(dfunc);
4323 }
4324
4325 // Following errors are in the function, not the caller.
4326 // Commands behave like vim9script.
4327 estack_push_ufunc(ufunc, 1);
4328 current_sctx = ufunc->uf_script_ctx;
4329 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4330
4331 // Use a specific location for storing error messages to be converted to an
4332 // exception.
4333 saved_msg_list = msg_list;
4334 msg_list = &private_msg_list;
4335
4336 // Do turn errors into exceptions.
4337 suppress_errthrow = FALSE;
4338
4339 // Do not delete the function while executing it.
4340 ++ufunc->uf_calls;
4341
4342 // When ":silent!" was used before calling then we still abort the
4343 // function. If ":silent!" is used in the function then we don't.
4344 emsg_silent_def = emsg_silent;
4345 did_emsg_def = 0;
4346
4347 ectx.ec_where.wt_index = 0;
4348 ectx.ec_where.wt_variable = FALSE;
4349
4350 // Execute the instructions until done.
4351 ret = exec_instructions(&ectx);
4352 if (ret == OK)
4353 {
4354 // function finished, get result from the stack.
4355 if (ufunc->uf_ret_type == &t_void)
4356 {
4357 rettv->v_type = VAR_VOID;
4358 }
4359 else
4360 {
4361 tv = STACK_TV_BOT(-1);
4362 *rettv = *tv;
4363 tv->v_type = VAR_UNKNOWN;
4364 }
4365 }
4366
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004367 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004368 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4369 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004370
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004371 // Deal with any remaining closures, they may be in use somewhere.
4372 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004373 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004374 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004375 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4376 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004377
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004378 estack_pop();
4379 current_sctx = save_current_sctx;
4380
Bram Moolenaarc970e422021-03-17 15:03:04 +01004381 // TODO: when is it safe to delete the function if it is no longer used?
4382 --ufunc->uf_calls;
4383
Bram Moolenaar352134b2020-10-17 22:04:08 +02004384 if (*msg_list != NULL && saved_msg_list != NULL)
4385 {
4386 msglist_T **plist = saved_msg_list;
4387
4388 // Append entries from the current msg_list (uncaught exceptions) to
4389 // the saved msg_list.
4390 while (*plist != NULL)
4391 plist = &(*plist)->next;
4392
4393 *plist = *msg_list;
4394 }
4395 msg_list = saved_msg_list;
4396
Bram Moolenaar4c137212021-04-19 16:48:48 +02004397 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004398 {
4399 cmdmod.cmod_filter_regmatch.regprog = NULL;
4400 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004401 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004402 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004403 emsg_silent_def = save_emsg_silent_def;
4404 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004405
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004406failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004407 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4409 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004412 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004413
Bram Moolenaar0186e582021-01-10 18:33:11 +01004414 while (ectx.ec_outer != NULL)
4415 {
4416 outer_T *up = ectx.ec_outer->out_up_is_copy
4417 ? NULL : ectx.ec_outer->out_up;
4418
4419 vim_free(ectx.ec_outer);
4420 ectx.ec_outer = up;
4421 }
4422
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004423 // Not sure if this is necessary.
4424 suppress_errthrow = save_suppress_errthrow;
4425
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004426 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004427 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004428 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004429 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 return ret;
4431}
4432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004434 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4435 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4436 * "pfx" is prefixed to every line.
4437 */
4438 static void
4439list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4440{
4441 int line_idx = 0;
4442 int prev_current = 0;
4443 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004444 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004445
4446 for (current = 0; current < instr_count; ++current)
4447 {
4448 isn_T *iptr = &instr[current];
4449 char *line;
4450
4451 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004452 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004453 while (line_idx < iptr->isn_lnum
4454 && line_idx < ufunc->uf_lines.ga_len)
4455 {
4456 if (current > prev_current)
4457 {
4458 msg_puts("\n\n");
4459 prev_current = current;
4460 }
4461 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4462 if (line != NULL)
4463 msg(line);
4464 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004465 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4466 {
4467 int first_def_arg = ufunc->uf_args.ga_len
4468 - ufunc->uf_def_args.ga_len;
4469
4470 if (def_arg_idx > 0)
4471 msg_puts("\n\n");
4472 msg_start();
4473 msg_puts(" ");
4474 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4475 first_def_arg + def_arg_idx]);
4476 msg_puts(" = ");
4477 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4478 msg_clr_eos();
4479 msg_end();
4480 }
4481 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004482
4483 switch (iptr->isn_type)
4484 {
4485 case ISN_EXEC:
4486 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4487 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004488 case ISN_LEGACY_EVAL:
4489 smsg("%s%4d EVAL legacy %s", pfx, current,
4490 iptr->isn_arg.string);
4491 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004492 case ISN_REDIRSTART:
4493 smsg("%s%4d REDIR", pfx, current);
4494 break;
4495 case ISN_REDIREND:
4496 smsg("%s%4d REDIR END%s", pfx, current,
4497 iptr->isn_arg.number ? " append" : "");
4498 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004499 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004500#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004501 smsg("%s%4d CEXPR pre %s", pfx, current,
4502 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004503#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004504 break;
4505 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004506#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004507 {
4508 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4509
4510 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4511 cexpr_get_auname(cer->cer_cmdidx),
4512 cer->cer_forceit ? "!" : "",
4513 cer->cer_cmdline);
4514 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004515#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004516 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004517 case ISN_INSTR:
4518 {
4519 smsg("%s%4d INSTR", pfx, current);
4520 list_instructions(" ", iptr->isn_arg.instr,
4521 INT_MAX, NULL);
4522 msg(" -------------");
4523 }
4524 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004525 case ISN_SUBSTITUTE:
4526 {
4527 subs_T *subs = &iptr->isn_arg.subs;
4528
4529 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4530 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4531 msg(" -------------");
4532 }
4533 break;
4534 case ISN_EXECCONCAT:
4535 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4536 (varnumber_T)iptr->isn_arg.number);
4537 break;
4538 case ISN_ECHO:
4539 {
4540 echo_T *echo = &iptr->isn_arg.echo;
4541
4542 smsg("%s%4d %s %d", pfx, current,
4543 echo->echo_with_white ? "ECHO" : "ECHON",
4544 echo->echo_count);
4545 }
4546 break;
4547 case ISN_EXECUTE:
4548 smsg("%s%4d EXECUTE %lld", pfx, current,
4549 (varnumber_T)(iptr->isn_arg.number));
4550 break;
4551 case ISN_ECHOMSG:
4552 smsg("%s%4d ECHOMSG %lld", pfx, current,
4553 (varnumber_T)(iptr->isn_arg.number));
4554 break;
4555 case ISN_ECHOERR:
4556 smsg("%s%4d ECHOERR %lld", pfx, current,
4557 (varnumber_T)(iptr->isn_arg.number));
4558 break;
4559 case ISN_LOAD:
4560 {
4561 if (iptr->isn_arg.number < 0)
4562 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4563 (varnumber_T)(iptr->isn_arg.number
4564 + STACK_FRAME_SIZE));
4565 else
4566 smsg("%s%4d LOAD $%lld", pfx, current,
4567 (varnumber_T)(iptr->isn_arg.number));
4568 }
4569 break;
4570 case ISN_LOADOUTER:
4571 {
4572 if (iptr->isn_arg.number < 0)
4573 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4574 iptr->isn_arg.outer.outer_depth,
4575 iptr->isn_arg.outer.outer_idx
4576 + STACK_FRAME_SIZE);
4577 else
4578 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4579 iptr->isn_arg.outer.outer_depth,
4580 iptr->isn_arg.outer.outer_idx);
4581 }
4582 break;
4583 case ISN_LOADV:
4584 smsg("%s%4d LOADV v:%s", pfx, current,
4585 get_vim_var_name(iptr->isn_arg.number));
4586 break;
4587 case ISN_LOADSCRIPT:
4588 {
4589 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4590 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4591 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4592 + sref->sref_idx;
4593
4594 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4595 sv->sv_name,
4596 sref->sref_idx,
4597 si->sn_name);
4598 }
4599 break;
4600 case ISN_LOADS:
4601 {
4602 scriptitem_T *si = SCRIPT_ITEM(
4603 iptr->isn_arg.loadstore.ls_sid);
4604
4605 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4606 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4607 }
4608 break;
4609 case ISN_LOADAUTO:
4610 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4611 break;
4612 case ISN_LOADG:
4613 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4614 break;
4615 case ISN_LOADB:
4616 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4617 break;
4618 case ISN_LOADW:
4619 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4620 break;
4621 case ISN_LOADT:
4622 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4623 break;
4624 case ISN_LOADGDICT:
4625 smsg("%s%4d LOAD g:", pfx, current);
4626 break;
4627 case ISN_LOADBDICT:
4628 smsg("%s%4d LOAD b:", pfx, current);
4629 break;
4630 case ISN_LOADWDICT:
4631 smsg("%s%4d LOAD w:", pfx, current);
4632 break;
4633 case ISN_LOADTDICT:
4634 smsg("%s%4d LOAD t:", pfx, current);
4635 break;
4636 case ISN_LOADOPT:
4637 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4638 break;
4639 case ISN_LOADENV:
4640 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4641 break;
4642 case ISN_LOADREG:
4643 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4644 break;
4645
4646 case ISN_STORE:
4647 if (iptr->isn_arg.number < 0)
4648 smsg("%s%4d STORE arg[%lld]", pfx, current,
4649 iptr->isn_arg.number + STACK_FRAME_SIZE);
4650 else
4651 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4652 break;
4653 case ISN_STOREOUTER:
4654 {
4655 if (iptr->isn_arg.number < 0)
4656 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4657 iptr->isn_arg.outer.outer_depth,
4658 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4659 else
4660 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4661 iptr->isn_arg.outer.outer_depth,
4662 iptr->isn_arg.outer.outer_idx);
4663 }
4664 break;
4665 case ISN_STOREV:
4666 smsg("%s%4d STOREV v:%s", pfx, current,
4667 get_vim_var_name(iptr->isn_arg.number));
4668 break;
4669 case ISN_STOREAUTO:
4670 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4671 break;
4672 case ISN_STOREG:
4673 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4674 break;
4675 case ISN_STOREB:
4676 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4677 break;
4678 case ISN_STOREW:
4679 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4680 break;
4681 case ISN_STORET:
4682 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4683 break;
4684 case ISN_STORES:
4685 {
4686 scriptitem_T *si = SCRIPT_ITEM(
4687 iptr->isn_arg.loadstore.ls_sid);
4688
4689 smsg("%s%4d STORES %s in %s", pfx, current,
4690 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4691 }
4692 break;
4693 case ISN_STORESCRIPT:
4694 {
4695 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4696 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4697 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4698 + sref->sref_idx;
4699
4700 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4701 sv->sv_name,
4702 sref->sref_idx,
4703 si->sn_name);
4704 }
4705 break;
4706 case ISN_STOREOPT:
4707 smsg("%s%4d STOREOPT &%s", pfx, current,
4708 iptr->isn_arg.storeopt.so_name);
4709 break;
4710 case ISN_STOREENV:
4711 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4712 break;
4713 case ISN_STOREREG:
4714 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4715 break;
4716 case ISN_STORENR:
4717 smsg("%s%4d STORE %lld in $%d", pfx, current,
4718 iptr->isn_arg.storenr.stnr_val,
4719 iptr->isn_arg.storenr.stnr_idx);
4720 break;
4721
4722 case ISN_STOREINDEX:
4723 smsg("%s%4d STOREINDEX %s", pfx, current,
4724 vartype_name(iptr->isn_arg.vartype));
4725 break;
4726
4727 case ISN_STORERANGE:
4728 smsg("%s%4d STORERANGE", pfx, current);
4729 break;
4730
4731 // constants
4732 case ISN_PUSHNR:
4733 smsg("%s%4d PUSHNR %lld", pfx, current,
4734 (varnumber_T)(iptr->isn_arg.number));
4735 break;
4736 case ISN_PUSHBOOL:
4737 case ISN_PUSHSPEC:
4738 smsg("%s%4d PUSH %s", pfx, current,
4739 get_var_special_name(iptr->isn_arg.number));
4740 break;
4741 case ISN_PUSHF:
4742#ifdef FEAT_FLOAT
4743 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4744#endif
4745 break;
4746 case ISN_PUSHS:
4747 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4748 break;
4749 case ISN_PUSHBLOB:
4750 {
4751 char_u *r;
4752 char_u numbuf[NUMBUFLEN];
4753 char_u *tofree;
4754
4755 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4756 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4757 vim_free(tofree);
4758 }
4759 break;
4760 case ISN_PUSHFUNC:
4761 {
4762 char *name = (char *)iptr->isn_arg.string;
4763
4764 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4765 name == NULL ? "[none]" : name);
4766 }
4767 break;
4768 case ISN_PUSHCHANNEL:
4769#ifdef FEAT_JOB_CHANNEL
4770 {
4771 channel_T *channel = iptr->isn_arg.channel;
4772
4773 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4774 channel == NULL ? 0 : channel->ch_id);
4775 }
4776#endif
4777 break;
4778 case ISN_PUSHJOB:
4779#ifdef FEAT_JOB_CHANNEL
4780 {
4781 typval_T tv;
4782 char_u *name;
4783
4784 tv.v_type = VAR_JOB;
4785 tv.vval.v_job = iptr->isn_arg.job;
4786 name = tv_get_string(&tv);
4787 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4788 }
4789#endif
4790 break;
4791 case ISN_PUSHEXC:
4792 smsg("%s%4d PUSH v:exception", pfx, current);
4793 break;
4794 case ISN_UNLET:
4795 smsg("%s%4d UNLET%s %s", pfx, current,
4796 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4797 iptr->isn_arg.unlet.ul_name);
4798 break;
4799 case ISN_UNLETENV:
4800 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4801 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4802 iptr->isn_arg.unlet.ul_name);
4803 break;
4804 case ISN_UNLETINDEX:
4805 smsg("%s%4d UNLETINDEX", pfx, current);
4806 break;
4807 case ISN_UNLETRANGE:
4808 smsg("%s%4d UNLETRANGE", pfx, current);
4809 break;
4810 case ISN_LOCKCONST:
4811 smsg("%s%4d LOCKCONST", pfx, current);
4812 break;
4813 case ISN_NEWLIST:
4814 smsg("%s%4d NEWLIST size %lld", pfx, current,
4815 (varnumber_T)(iptr->isn_arg.number));
4816 break;
4817 case ISN_NEWDICT:
4818 smsg("%s%4d NEWDICT size %lld", pfx, current,
4819 (varnumber_T)(iptr->isn_arg.number));
4820 break;
4821
4822 // function call
4823 case ISN_BCALL:
4824 {
4825 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4826
4827 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4828 internal_func_name(cbfunc->cbf_idx),
4829 cbfunc->cbf_argcount);
4830 }
4831 break;
4832 case ISN_DCALL:
4833 {
4834 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4835 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4836 + cdfunc->cdf_idx;
4837
4838 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
4839 df->df_ufunc->uf_name_exp != NULL
4840 ? df->df_ufunc->uf_name_exp
4841 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4842 }
4843 break;
4844 case ISN_UCALL:
4845 {
4846 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4847
4848 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
4849 cufunc->cuf_name, cufunc->cuf_argcount);
4850 }
4851 break;
4852 case ISN_PCALL:
4853 {
4854 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4855
4856 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
4857 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4858 }
4859 break;
4860 case ISN_PCALL_END:
4861 smsg("%s%4d PCALL end", pfx, current);
4862 break;
4863 case ISN_RETURN:
4864 smsg("%s%4d RETURN", pfx, current);
4865 break;
4866 case ISN_RETURN_ZERO:
4867 smsg("%s%4d RETURN 0", pfx, current);
4868 break;
4869 case ISN_FUNCREF:
4870 {
4871 funcref_T *funcref = &iptr->isn_arg.funcref;
4872 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4873 + funcref->fr_func;
4874
4875 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
4876 }
4877 break;
4878
4879 case ISN_NEWFUNC:
4880 {
4881 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4882
4883 smsg("%s%4d NEWFUNC %s %s", pfx, current,
4884 newfunc->nf_lambda, newfunc->nf_global);
4885 }
4886 break;
4887
4888 case ISN_DEF:
4889 {
4890 char_u *name = iptr->isn_arg.string;
4891
4892 smsg("%s%4d DEF %s", pfx, current,
4893 name == NULL ? (char_u *)"" : name);
4894 }
4895 break;
4896
4897 case ISN_JUMP:
4898 {
4899 char *when = "?";
4900
4901 switch (iptr->isn_arg.jump.jump_when)
4902 {
4903 case JUMP_ALWAYS:
4904 when = "JUMP";
4905 break;
4906 case JUMP_AND_KEEP_IF_TRUE:
4907 when = "JUMP_AND_KEEP_IF_TRUE";
4908 break;
4909 case JUMP_IF_FALSE:
4910 when = "JUMP_IF_FALSE";
4911 break;
4912 case JUMP_AND_KEEP_IF_FALSE:
4913 when = "JUMP_AND_KEEP_IF_FALSE";
4914 break;
4915 case JUMP_IF_COND_FALSE:
4916 when = "JUMP_IF_COND_FALSE";
4917 break;
4918 case JUMP_IF_COND_TRUE:
4919 when = "JUMP_IF_COND_TRUE";
4920 break;
4921 }
4922 smsg("%s%4d %s -> %d", pfx, current, when,
4923 iptr->isn_arg.jump.jump_where);
4924 }
4925 break;
4926
4927 case ISN_JUMP_IF_ARG_SET:
4928 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
4929 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4930 iptr->isn_arg.jump.jump_where);
4931 break;
4932
4933 case ISN_FOR:
4934 {
4935 forloop_T *forloop = &iptr->isn_arg.forloop;
4936
4937 smsg("%s%4d FOR $%d -> %d", pfx, current,
4938 forloop->for_idx, forloop->for_end);
4939 }
4940 break;
4941
4942 case ISN_TRY:
4943 {
4944 try_T *try = &iptr->isn_arg.try;
4945
4946 if (try->try_ref->try_finally == 0)
4947 smsg("%s%4d TRY catch -> %d, endtry -> %d",
4948 pfx, current,
4949 try->try_ref->try_catch,
4950 try->try_ref->try_endtry);
4951 else
4952 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4953 pfx, current,
4954 try->try_ref->try_catch,
4955 try->try_ref->try_finally,
4956 try->try_ref->try_endtry);
4957 }
4958 break;
4959 case ISN_CATCH:
4960 // TODO
4961 smsg("%s%4d CATCH", pfx, current);
4962 break;
4963 case ISN_TRYCONT:
4964 {
4965 trycont_T *trycont = &iptr->isn_arg.trycont;
4966
4967 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
4968 trycont->tct_levels,
4969 trycont->tct_levels == 1 ? "" : "s",
4970 trycont->tct_where);
4971 }
4972 break;
4973 case ISN_FINALLY:
4974 smsg("%s%4d FINALLY", pfx, current);
4975 break;
4976 case ISN_ENDTRY:
4977 smsg("%s%4d ENDTRY", pfx, current);
4978 break;
4979 case ISN_THROW:
4980 smsg("%s%4d THROW", pfx, current);
4981 break;
4982
4983 // expression operations on number
4984 case ISN_OPNR:
4985 case ISN_OPFLOAT:
4986 case ISN_OPANY:
4987 {
4988 char *what;
4989 char *ins;
4990
4991 switch (iptr->isn_arg.op.op_type)
4992 {
4993 case EXPR_MULT: what = "*"; break;
4994 case EXPR_DIV: what = "/"; break;
4995 case EXPR_REM: what = "%"; break;
4996 case EXPR_SUB: what = "-"; break;
4997 case EXPR_ADD: what = "+"; break;
4998 default: what = "???"; break;
4999 }
5000 switch (iptr->isn_type)
5001 {
5002 case ISN_OPNR: ins = "OPNR"; break;
5003 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5004 case ISN_OPANY: ins = "OPANY"; break;
5005 default: ins = "???"; break;
5006 }
5007 smsg("%s%4d %s %s", pfx, current, ins, what);
5008 }
5009 break;
5010
5011 case ISN_COMPAREBOOL:
5012 case ISN_COMPARESPECIAL:
5013 case ISN_COMPARENR:
5014 case ISN_COMPAREFLOAT:
5015 case ISN_COMPARESTRING:
5016 case ISN_COMPAREBLOB:
5017 case ISN_COMPARELIST:
5018 case ISN_COMPAREDICT:
5019 case ISN_COMPAREFUNC:
5020 case ISN_COMPAREANY:
5021 {
5022 char *p;
5023 char buf[10];
5024 char *type;
5025
5026 switch (iptr->isn_arg.op.op_type)
5027 {
5028 case EXPR_EQUAL: p = "=="; break;
5029 case EXPR_NEQUAL: p = "!="; break;
5030 case EXPR_GREATER: p = ">"; break;
5031 case EXPR_GEQUAL: p = ">="; break;
5032 case EXPR_SMALLER: p = "<"; break;
5033 case EXPR_SEQUAL: p = "<="; break;
5034 case EXPR_MATCH: p = "=~"; break;
5035 case EXPR_IS: p = "is"; break;
5036 case EXPR_ISNOT: p = "isnot"; break;
5037 case EXPR_NOMATCH: p = "!~"; break;
5038 default: p = "???"; break;
5039 }
5040 STRCPY(buf, p);
5041 if (iptr->isn_arg.op.op_ic == TRUE)
5042 strcat(buf, "?");
5043 switch(iptr->isn_type)
5044 {
5045 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5046 case ISN_COMPARESPECIAL:
5047 type = "COMPARESPECIAL"; break;
5048 case ISN_COMPARENR: type = "COMPARENR"; break;
5049 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5050 case ISN_COMPARESTRING:
5051 type = "COMPARESTRING"; break;
5052 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5053 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5054 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5055 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5056 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5057 default: type = "???"; break;
5058 }
5059
5060 smsg("%s%4d %s %s", pfx, current, type, buf);
5061 }
5062 break;
5063
5064 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5065 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5066
5067 // expression operations
5068 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5069 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5070 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5071 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5072 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5073 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5074 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5075 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5076 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5077 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5078 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5079 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5080 pfx, current, iptr->isn_arg.number); break;
5081 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5082 pfx, current, iptr->isn_arg.number); break;
5083 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5084 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5085 iptr->isn_arg.string); break;
5086 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5087
5088 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5089 case ISN_CHECKTYPE:
5090 {
5091 checktype_T *ct = &iptr->isn_arg.type;
5092 char *tofree;
5093
5094 if (ct->ct_arg_idx == 0)
5095 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5096 type_name(ct->ct_type, &tofree),
5097 (int)ct->ct_off);
5098 else
5099 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5100 type_name(ct->ct_type, &tofree),
5101 (int)ct->ct_off,
5102 (int)ct->ct_arg_idx);
5103 vim_free(tofree);
5104 break;
5105 }
5106 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5107 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5108 iptr->isn_arg.checklen.cl_min_len);
5109 break;
5110 case ISN_SETTYPE:
5111 {
5112 char *tofree;
5113
5114 smsg("%s%4d SETTYPE %s", pfx, current,
5115 type_name(iptr->isn_arg.type.ct_type, &tofree));
5116 vim_free(tofree);
5117 break;
5118 }
5119 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
5120 case ISN_2BOOL: if (iptr->isn_arg.number)
5121 smsg("%s%4d INVERT (!val)", pfx, current);
5122 else
5123 smsg("%s%4d 2BOOL (!!val)", pfx, current);
5124 break;
5125 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
5126 (varnumber_T)(iptr->isn_arg.number));
5127 break;
5128 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]", pfx, current,
5129 (varnumber_T)(iptr->isn_arg.number));
5130 break;
5131 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current, iptr->isn_arg.string);
5132 break;
5133 case ISN_PUT:
5134 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5135 smsg("%s%4d PUT %c above range",
5136 pfx, current, iptr->isn_arg.put.put_regname);
5137 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5138 smsg("%s%4d PUT %c range",
5139 pfx, current, iptr->isn_arg.put.put_regname);
5140 else
5141 smsg("%s%4d PUT %c %ld", pfx, current,
5142 iptr->isn_arg.put.put_regname,
5143 (long)iptr->isn_arg.put.put_lnum);
5144 break;
5145
5146 // TODO: summarize modifiers
5147 case ISN_CMDMOD:
5148 {
5149 char_u *buf;
5150 size_t len = produce_cmdmods(
5151 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5152
5153 buf = alloc(len + 1);
5154 if (buf != NULL)
5155 {
5156 (void)produce_cmdmods(
5157 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5158 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5159 vim_free(buf);
5160 }
5161 break;
5162 }
5163 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5164
5165 case ISN_PROF_START:
5166 smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum);
5167 break;
5168
5169 case ISN_PROF_END:
5170 smsg("%s%4d PROFILE END", pfx, current);
5171 break;
5172
5173 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5174 iptr->isn_arg.unpack.unp_count,
5175 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5176 break;
5177 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5178 iptr->isn_arg.shuffle.shfl_item,
5179 iptr->isn_arg.shuffle.shfl_up);
5180 break;
5181 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5182
5183 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5184 return;
5185 }
5186
5187 out_flush(); // output one line at a time
5188 ui_breakcheck();
5189 if (got_int)
5190 break;
5191 }
5192}
5193
5194/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005195 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005196 * We don't really need this at runtime, but we do have tests that require it,
5197 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 */
5199 void
5200ex_disassemble(exarg_T *eap)
5201{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005202 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005203 char_u *fname;
5204 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 dfunc_T *dfunc;
5206 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005207 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005208 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005210 if (STRNCMP(arg, "<lambda>", 8) == 0)
5211 {
5212 arg += 8;
5213 (void)getdigits(&arg);
5214 fname = vim_strnsave(eap->arg, arg - eap->arg);
5215 }
5216 else
5217 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005218 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005219 if (fname == NULL)
5220 {
5221 semsg(_(e_invarg2), eap->arg);
5222 return;
5223 }
5224
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005225 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005226 if (ufunc == NULL)
5227 {
5228 char_u *p = untrans_function_name(fname);
5229
5230 if (p != NULL)
5231 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005232 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005233 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005234 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 if (ufunc == NULL)
5236 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005237 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238 return;
5239 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005240 if (func_needs_compiling(ufunc, eap->forceit)
5241 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005242 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005243 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005245 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246 return;
5247 }
5248 if (ufunc->uf_name_exp != NULL)
5249 msg((char *)ufunc->uf_name_exp);
5250 else
5251 msg((char *)ufunc->uf_name);
5252
5253 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005254#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005255 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
5256 instr_count = eap->forceit ? dfunc->df_instr_prof_count
5257 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005258#else
5259 instr = dfunc->df_instr;
5260 instr_count = dfunc->df_instr_count;
5261#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005262
Bram Moolenaar4c137212021-04-19 16:48:48 +02005263 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264}
5265
5266/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005267 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268 * list, etc. Mostly like what JavaScript does, except that empty list and
5269 * empty dictionary are FALSE.
5270 */
5271 int
5272tv2bool(typval_T *tv)
5273{
5274 switch (tv->v_type)
5275 {
5276 case VAR_NUMBER:
5277 return tv->vval.v_number != 0;
5278 case VAR_FLOAT:
5279#ifdef FEAT_FLOAT
5280 return tv->vval.v_float != 0.0;
5281#else
5282 break;
5283#endif
5284 case VAR_PARTIAL:
5285 return tv->vval.v_partial != NULL;
5286 case VAR_FUNC:
5287 case VAR_STRING:
5288 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5289 case VAR_LIST:
5290 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5291 case VAR_DICT:
5292 return tv->vval.v_dict != NULL
5293 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5294 case VAR_BOOL:
5295 case VAR_SPECIAL:
5296 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5297 case VAR_JOB:
5298#ifdef FEAT_JOB_CHANNEL
5299 return tv->vval.v_job != NULL;
5300#else
5301 break;
5302#endif
5303 case VAR_CHANNEL:
5304#ifdef FEAT_JOB_CHANNEL
5305 return tv->vval.v_channel != NULL;
5306#else
5307 break;
5308#endif
5309 case VAR_BLOB:
5310 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5311 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005312 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005314 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315 break;
5316 }
5317 return FALSE;
5318}
5319
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005320 void
5321emsg_using_string_as(typval_T *tv, int as_number)
5322{
5323 semsg(_(as_number ? e_using_string_as_number_str
5324 : e_using_string_as_bool_str),
5325 tv->vval.v_string == NULL
5326 ? (char_u *)"" : tv->vval.v_string);
5327}
5328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329/*
5330 * If "tv" is a string give an error and return FAIL.
5331 */
5332 int
5333check_not_string(typval_T *tv)
5334{
5335 if (tv->v_type == VAR_STRING)
5336 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005337 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 clear_tv(tv);
5339 return FAIL;
5340 }
5341 return OK;
5342}
5343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005344
5345#endif // FEAT_EVAL