blob: 29cd5fc67ae3a2b075545e6ac8ec42713356a67a [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 Moolenaar8a7d6542020-01-26 15:56:19 +01001298 for (;;)
1299 {
1300 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001301
Bram Moolenaar270d0382020-05-15 21:42:53 +02001302 if (++breakcheck_count >= 100)
1303 {
1304 line_breakcheck();
1305 breakcheck_count = 0;
1306 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001307 if (got_int)
1308 {
1309 // Turn CTRL-C into an exception.
1310 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001311 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001312 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001313 did_throw = TRUE;
1314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315
Bram Moolenaara26b9702020-04-18 19:53:28 +02001316 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1317 {
1318 // Turn an error message into an exception.
1319 did_emsg = FALSE;
1320 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001321 return FAIL;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001322 did_throw = TRUE;
1323 *msg_list = NULL;
1324 }
1325
Bram Moolenaar4c137212021-04-19 16:48:48 +02001326 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001328 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001329 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330
1331 // An exception jumps to the first catch, finally, or returns from
1332 // the current function.
1333 if (trystack->ga_len > 0)
1334 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001335 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 {
1337 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001338 ectx->ec_in_catch = TRUE;
1339 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 }
1341 else
1342 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001343 // Not inside try or need to return from current functions.
1344 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001345 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1346 return FAIL;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001347 tv = STACK_TV_BOT(0);
1348 tv->v_type = VAR_NUMBER;
1349 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001350 ++ectx->ec_stack.ga_len;
1351 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001353 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001354 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001355 if (handle_closure_in_use(ectx, FALSE) == FAIL)
1356 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 goto done;
1358 }
1359
Bram Moolenaar4c137212021-04-19 16:48:48 +02001360 if (func_return(ectx) == FAIL)
1361 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 }
1363 continue;
1364 }
1365
Bram Moolenaar4c137212021-04-19 16:48:48 +02001366 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 switch (iptr->isn_type)
1368 {
1369 // execute Ex command line
1370 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001371 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001372 source_cookie_T cookie;
1373
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001374 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001375 // Pass getsourceline to get an error for a missing ":end"
1376 // command.
1377 CLEAR_FIELD(cookie);
1378 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1379 if (do_cmdline(iptr->isn_arg.string,
1380 getsourceline, &cookie,
1381 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1382 == FAIL
1383 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001384 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 break;
1387
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001388 // push typeval VAR_INSTR with instructions to be executed
1389 case ISN_INSTR:
1390 {
1391 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1392 return FAIL;
1393 tv = STACK_TV_BOT(0);
1394 tv->vval.v_instr = ALLOC_ONE(instr_T);
1395 if (tv->vval.v_instr == NULL)
1396 goto on_error;
1397 ++ectx->ec_stack.ga_len;
1398
1399 tv->v_type = VAR_INSTR;
1400 tv->vval.v_instr->instr_ectx = ectx;
1401 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1402 }
1403 break;
1404
Bram Moolenaar4c137212021-04-19 16:48:48 +02001405 // execute :substitute with an expression
1406 case ISN_SUBSTITUTE:
1407 {
1408 subs_T *subs = &iptr->isn_arg.subs;
1409 source_cookie_T cookie;
1410 struct subs_expr_S *save_instr = substitute_instr;
1411 struct subs_expr_S subs_instr;
1412 int res;
1413
1414 subs_instr.subs_ectx = ectx;
1415 subs_instr.subs_instr = subs->subs_instr;
1416 subs_instr.subs_status = OK;
1417 substitute_instr = &subs_instr;
1418
1419 SOURCING_LNUM = iptr->isn_lnum;
1420 // This is very much like ISN_EXEC
1421 CLEAR_FIELD(cookie);
1422 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1423 res = do_cmdline(subs->subs_cmd,
1424 getsourceline, &cookie,
1425 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1426 substitute_instr = save_instr;
1427
1428 if (res == FAIL || did_emsg
1429 || subs_instr.subs_status == FAIL)
1430 goto on_error;
1431 }
1432 break;
1433
1434 case ISN_FINISH:
1435 goto done;
1436
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001437 case ISN_REDIRSTART:
1438 // create a dummy entry for var_redir_str()
1439 if (alloc_redir_lval() == FAIL)
1440 goto on_error;
1441
1442 // The output is stored in growarray "redir_ga" until
1443 // redirection ends.
1444 init_redir_ga();
1445 redir_vname = 1;
1446 break;
1447
1448 case ISN_REDIREND:
1449 {
1450 char_u *res = get_clear_redir_ga();
1451
1452 // End redirection, put redirected text on the stack.
1453 clear_redir_lval();
1454 redir_vname = 0;
1455
1456 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1457 {
1458 vim_free(res);
1459 return FAIL;
1460 }
1461 tv = STACK_TV_BOT(0);
1462 tv->v_type = VAR_STRING;
1463 tv->vval.v_string = res;
1464 ++ectx->ec_stack.ga_len;
1465 }
1466 break;
1467
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001468 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001469#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001470 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1471 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001472#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001473 break;
1474
1475 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001476#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001477 {
1478 exarg_T ea;
1479 int res;
1480
1481 CLEAR_FIELD(ea);
1482 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1483 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1484 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1485 --ectx->ec_stack.ga_len;
1486 tv = STACK_TV_BOT(0);
1487 res = cexpr_core(&ea, tv);
1488 clear_tv(tv);
1489 if (res == FAIL)
1490 goto on_error;
1491 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001492#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001493 break;
1494
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001495 // execute Ex command from pieces on the stack
1496 case ISN_EXECCONCAT:
1497 {
1498 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001499 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001500 int pass;
1501 int i;
1502 char_u *cmd = NULL;
1503 char_u *str;
1504
1505 for (pass = 1; pass <= 2; ++pass)
1506 {
1507 for (i = 0; i < count; ++i)
1508 {
1509 tv = STACK_TV_BOT(i - count);
1510 str = tv->vval.v_string;
1511 if (str != NULL && *str != NUL)
1512 {
1513 if (pass == 2)
1514 STRCPY(cmd + len, str);
1515 len += STRLEN(str);
1516 }
1517 if (pass == 2)
1518 clear_tv(tv);
1519 }
1520 if (pass == 1)
1521 {
1522 cmd = alloc(len + 1);
1523 if (cmd == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001524 return FAIL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001525 len = 0;
1526 }
1527 }
1528
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001529 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001530 do_cmdline_cmd(cmd);
1531 vim_free(cmd);
1532 }
1533 break;
1534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 // execute :echo {string} ...
1536 case ISN_ECHO:
1537 {
1538 int count = iptr->isn_arg.echo.echo_count;
1539 int atstart = TRUE;
1540 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001541 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
1543 for (idx = 0; idx < count; ++idx)
1544 {
1545 tv = STACK_TV_BOT(idx - count);
1546 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1547 &atstart, &needclr);
1548 clear_tv(tv);
1549 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001550 if (needclr)
1551 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001552 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 }
1554 break;
1555
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001556 // :execute {string} ...
1557 // :echomsg {string} ...
1558 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001559 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001560 case ISN_ECHOMSG:
1561 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001562 {
1563 int count = iptr->isn_arg.number;
1564 garray_T ga;
1565 char_u buf[NUMBUFLEN];
1566 char_u *p;
1567 int len;
1568 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001569 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001570
1571 ga_init2(&ga, 1, 80);
1572 for (idx = 0; idx < count; ++idx)
1573 {
1574 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001575 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001576 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001577 if (tv->v_type == VAR_CHANNEL
1578 || tv->v_type == VAR_JOB)
1579 {
1580 SOURCING_LNUM = iptr->isn_lnum;
1581 emsg(_(e_inval_string));
1582 break;
1583 }
1584 else
1585 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001586 }
1587 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001588 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001589
1590 len = (int)STRLEN(p);
1591 if (ga_grow(&ga, len + 2) == FAIL)
1592 failed = TRUE;
1593 else
1594 {
1595 if (ga.ga_len > 0)
1596 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1597 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1598 ga.ga_len += len;
1599 }
1600 clear_tv(tv);
1601 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001602 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001603 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001604 {
1605 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001606 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001607 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001608
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001609 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001610 {
1611 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001612 {
1613 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001614 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001615 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001616 {
1617 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001618 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001619 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001620 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001621 else
1622 {
1623 msg_sb_eol();
1624 if (iptr->isn_type == ISN_ECHOMSG)
1625 {
1626 msg_attr(ga.ga_data, echo_attr);
1627 out_flush();
1628 }
1629 else
1630 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001631 SOURCING_LNUM = iptr->isn_lnum;
1632 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001633 }
1634 }
1635 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001636 ga_clear(&ga);
1637 }
1638 break;
1639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 // load local variable or argument
1641 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001642 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1643 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001645 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 break;
1647
1648 // load v: variable
1649 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001650 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1651 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001653 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 break;
1655
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001656 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 case ISN_LOADSCRIPT:
1658 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001659 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 svar_T *sv;
1661
Bram Moolenaar4c137212021-04-19 16:48:48 +02001662 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001663 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001664 return FAIL;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001665 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001666 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1667 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001669 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 }
1671 break;
1672
1673 // load s: variable in old script
1674 case ISN_LOADS:
1675 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001676 hashtab_T *ht = &SCRIPT_VARS(
1677 iptr->isn_arg.loadstore.ls_sid);
1678 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 if (di == NULL)
1682 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001683 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001684 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001685 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 }
1687 else
1688 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001689 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1690 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001692 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 }
1694 }
1695 break;
1696
Bram Moolenaard3aac292020-04-19 14:32:17 +02001697 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001699 case ISN_LOADB:
1700 case ISN_LOADW:
1701 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001703 dictitem_T *di = NULL;
1704 hashtab_T *ht = NULL;
1705 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001706
Bram Moolenaard3aac292020-04-19 14:32:17 +02001707 switch (iptr->isn_type)
1708 {
1709 case ISN_LOADG:
1710 ht = get_globvar_ht();
1711 namespace = 'g';
1712 break;
1713 case ISN_LOADB:
1714 ht = &curbuf->b_vars->dv_hashtab;
1715 namespace = 'b';
1716 break;
1717 case ISN_LOADW:
1718 ht = &curwin->w_vars->dv_hashtab;
1719 namespace = 'w';
1720 break;
1721 case ISN_LOADT:
1722 ht = &curtab->tp_vars->dv_hashtab;
1723 namespace = 't';
1724 break;
1725 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001726 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001727 }
1728 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 if (di == NULL)
1731 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001732 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001733 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001734 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001735 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 }
1737 else
1738 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001739 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1740 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001742 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 }
1744 }
1745 break;
1746
Bram Moolenaar03290b82020-12-19 16:30:44 +01001747 // load autoload variable
1748 case ISN_LOADAUTO:
1749 {
1750 char_u *name = iptr->isn_arg.string;
1751
Bram Moolenaar4c137212021-04-19 16:48:48 +02001752 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1753 return FAIL;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001754 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001755 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001756 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001757 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001758 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001759 }
1760 break;
1761
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001762 // load g:/b:/w:/t: namespace
1763 case ISN_LOADGDICT:
1764 case ISN_LOADBDICT:
1765 case ISN_LOADWDICT:
1766 case ISN_LOADTDICT:
1767 {
1768 dict_T *d = NULL;
1769
1770 switch (iptr->isn_type)
1771 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001772 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1773 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1774 case ISN_LOADWDICT: d = curwin->w_vars; break;
1775 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001776 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001777 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001778 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001779 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1780 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001781 tv = STACK_TV_BOT(0);
1782 tv->v_type = VAR_DICT;
1783 tv->v_lock = 0;
1784 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001785 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001786 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001787 }
1788 break;
1789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 // load &option
1791 case ISN_LOADOPT:
1792 {
1793 typval_T optval;
1794 char_u *name = iptr->isn_arg.string;
1795
Bram Moolenaara8c17702020-04-01 21:17:24 +02001796 // This is not expected to fail, name is checked during
1797 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001798 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1799 return FAIL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001800 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001801 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001803 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 }
1805 break;
1806
1807 // load $ENV
1808 case ISN_LOADENV:
1809 {
1810 typval_T optval;
1811 char_u *name = iptr->isn_arg.string;
1812
Bram Moolenaar4c137212021-04-19 16:48:48 +02001813 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1814 return FAIL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001815 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001816 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001818 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 }
1820 break;
1821
1822 // load @register
1823 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001824 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1825 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 tv = STACK_TV_BOT(0);
1827 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001828 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001829 // This may result in NULL, which should be equivalent to an
1830 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 tv->vval.v_string = get_reg_contents(
1832 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001833 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 break;
1835
1836 // store local variable
1837 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001838 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 tv = STACK_TV_VAR(iptr->isn_arg.number);
1840 clear_tv(tv);
1841 *tv = *STACK_TV_BOT(0);
1842 break;
1843
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001844 // store s: variable in old script
1845 case ISN_STORES:
1846 {
1847 hashtab_T *ht = &SCRIPT_VARS(
1848 iptr->isn_arg.loadstore.ls_sid);
1849 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001850 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001851
Bram Moolenaar4c137212021-04-19 16:48:48 +02001852 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001853 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001854 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001855 else
1856 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001857 SOURCING_LNUM = iptr->isn_lnum;
1858 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001859 {
1860 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001861 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001862 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001863 clear_tv(&di->di_tv);
1864 di->di_tv = *STACK_TV_BOT(0);
1865 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001866 }
1867 break;
1868
1869 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 case ISN_STORESCRIPT:
1871 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001872 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1873 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874
Bram Moolenaar4c137212021-04-19 16:48:48 +02001875 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001876 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001877 return FAIL;
1878 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001879
1880 // "const" and "final" are checked at compile time, locking
1881 // the value needs to be checked here.
1882 SOURCING_LNUM = iptr->isn_lnum;
1883 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001884 {
1885 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001886 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001887 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 clear_tv(sv->sv_tv);
1890 *sv->sv_tv = *STACK_TV_BOT(0);
1891 }
1892 break;
1893
1894 // store option
1895 case ISN_STOREOPT:
1896 {
1897 long n = 0;
1898 char_u *s = NULL;
1899 char *msg;
1900
Bram Moolenaar4c137212021-04-19 16:48:48 +02001901 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 tv = STACK_TV_BOT(0);
1903 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001904 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001906 if (s == NULL)
1907 s = (char_u *)"";
1908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001910 // must be VAR_NUMBER, CHECKTYPE makes sure
1911 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1913 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001914 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 if (msg != NULL)
1916 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001917 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001919 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 }
1922 break;
1923
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001924 // store $ENV
1925 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001926 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001927 tv = STACK_TV_BOT(0);
1928 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1929 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001930 break;
1931
1932 // store @r
1933 case ISN_STOREREG:
1934 {
1935 int reg = iptr->isn_arg.number;
1936
Bram Moolenaar4c137212021-04-19 16:48:48 +02001937 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001938 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001939 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001940 tv_get_string(tv), -1, FALSE);
1941 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001942 }
1943 break;
1944
1945 // store v: variable
1946 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001947 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001948 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1949 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001950 // should not happen, type is checked when compiling
1951 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001952 break;
1953
Bram Moolenaard3aac292020-04-19 14:32:17 +02001954 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001956 case ISN_STOREB:
1957 case ISN_STOREW:
1958 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001960 dictitem_T *di;
1961 hashtab_T *ht;
1962 char_u *name = iptr->isn_arg.string + 2;
1963
Bram Moolenaard3aac292020-04-19 14:32:17 +02001964 switch (iptr->isn_type)
1965 {
1966 case ISN_STOREG:
1967 ht = get_globvar_ht();
1968 break;
1969 case ISN_STOREB:
1970 ht = &curbuf->b_vars->dv_hashtab;
1971 break;
1972 case ISN_STOREW:
1973 ht = &curwin->w_vars->dv_hashtab;
1974 break;
1975 case ISN_STORET:
1976 ht = &curtab->tp_vars->dv_hashtab;
1977 break;
1978 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001979 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981
Bram Moolenaar4c137212021-04-19 16:48:48 +02001982 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001983 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001985 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 else
1987 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001988 SOURCING_LNUM = iptr->isn_lnum;
1989 if (var_check_permission(di, name) == FAIL)
1990 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 clear_tv(&di->di_tv);
1992 di->di_tv = *STACK_TV_BOT(0);
1993 }
1994 }
1995 break;
1996
Bram Moolenaar03290b82020-12-19 16:30:44 +01001997 // store an autoload variable
1998 case ISN_STOREAUTO:
1999 SOURCING_LNUM = iptr->isn_lnum;
2000 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2001 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002002 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002003 break;
2004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 // store number in local variable
2006 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002007 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 clear_tv(tv);
2009 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002010 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 break;
2012
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002013 // store value in list or dict variable
2014 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002015 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002016 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002017 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002018 typval_T *tv_dest = STACK_TV_BOT(-1);
2019 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002020
Bram Moolenaar752fc692021-01-04 21:57:11 +01002021 // Stack contains:
2022 // -3 value to be stored
2023 // -2 index
2024 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002025 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002026 SOURCING_LNUM = iptr->isn_lnum;
2027 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002028 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002029 dest_type = tv_dest->v_type;
2030 if (dest_type == VAR_DICT)
2031 status = do_2string(tv_idx, TRUE);
2032 else if (dest_type == VAR_LIST
2033 && tv_idx->v_type != VAR_NUMBER)
2034 {
2035 emsg(_(e_number_exp));
2036 status = FAIL;
2037 }
2038 }
2039 else if (dest_type != tv_dest->v_type)
2040 {
2041 // just in case, should be OK
2042 semsg(_(e_expected_str_but_got_str),
2043 vartype_name(dest_type),
2044 vartype_name(tv_dest->v_type));
2045 status = FAIL;
2046 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002047
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002048 if (status == OK && dest_type == VAR_LIST)
2049 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002050 long lidx = (long)tv_idx->vval.v_number;
2051 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002052
2053 if (list == NULL)
2054 {
2055 emsg(_(e_list_not_set));
2056 goto on_error;
2057 }
2058 if (lidx < 0 && list->lv_len + lidx >= 0)
2059 // negative index is relative to the end
2060 lidx = list->lv_len + lidx;
2061 if (lidx < 0 || lidx > list->lv_len)
2062 {
2063 semsg(_(e_listidx), lidx);
2064 goto on_error;
2065 }
2066 if (lidx < list->lv_len)
2067 {
2068 listitem_T *li = list_find(list, lidx);
2069
2070 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002071 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002072 goto on_error;
2073 // overwrite existing list item
2074 clear_tv(&li->li_tv);
2075 li->li_tv = *tv;
2076 }
2077 else
2078 {
2079 if (error_if_locked(list->lv_lock,
2080 e_cannot_change_list))
2081 goto on_error;
2082 // append to list, only fails when out of memory
2083 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002084 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002085 clear_tv(tv);
2086 }
2087 }
2088 else if (status == OK && dest_type == VAR_DICT)
2089 {
2090 char_u *key = tv_idx->vval.v_string;
2091 dict_T *dict = tv_dest->vval.v_dict;
2092 dictitem_T *di;
2093
2094 SOURCING_LNUM = iptr->isn_lnum;
2095 if (dict == NULL)
2096 {
2097 emsg(_(e_dictionary_not_set));
2098 goto on_error;
2099 }
2100 if (key == NULL)
2101 key = (char_u *)"";
2102 di = dict_find(dict, key, -1);
2103 if (di != NULL)
2104 {
2105 if (error_if_locked(di->di_tv.v_lock,
2106 e_cannot_change_dict_item))
2107 goto on_error;
2108 // overwrite existing value
2109 clear_tv(&di->di_tv);
2110 di->di_tv = *tv;
2111 }
2112 else
2113 {
2114 if (error_if_locked(dict->dv_lock,
2115 e_cannot_change_dict))
2116 goto on_error;
2117 // add to dict, only fails when out of memory
2118 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002119 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002120 clear_tv(tv);
2121 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002122 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002123 else if (status == OK && dest_type == VAR_BLOB)
2124 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002125 long lidx = (long)tv_idx->vval.v_number;
2126 blob_T *blob = tv_dest->vval.v_blob;
2127 varnumber_T nr;
2128 int error = FALSE;
2129 int len;
2130
2131 if (blob == NULL)
2132 {
2133 emsg(_(e_blob_not_set));
2134 goto on_error;
2135 }
2136 len = blob_len(blob);
2137 if (lidx < 0 && len + lidx >= 0)
2138 // negative index is relative to the end
2139 lidx = len + lidx;
2140
2141 // Can add one byte at the end.
2142 if (lidx < 0 || lidx > len)
2143 {
2144 semsg(_(e_blobidx), lidx);
2145 goto on_error;
2146 }
2147 if (value_check_lock(blob->bv_lock,
2148 (char_u *)"blob", FALSE))
2149 goto on_error;
2150 nr = tv_get_number_chk(tv, &error);
2151 if (error)
2152 goto on_error;
2153 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002154 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002155 else
2156 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002157 status = FAIL;
2158 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002159 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002160
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002161 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002162 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002163 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002164 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002165 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002166 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002167 goto on_error;
2168 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002169 }
2170 break;
2171
Bram Moolenaar68452172021-04-12 21:21:02 +02002172 // store value in blob range
2173 case ISN_STORERANGE:
2174 {
2175 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2176 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2177 typval_T *tv_dest = STACK_TV_BOT(-1);
2178 int status = OK;
2179
2180 // Stack contains:
2181 // -4 value to be stored
2182 // -3 first index or "none"
2183 // -2 second index or "none"
2184 // -1 destination blob
2185 tv = STACK_TV_BOT(-4);
2186 if (tv_dest->v_type != VAR_BLOB)
2187 {
2188 status = FAIL;
2189 emsg(_(e_blob_required));
2190 }
2191 else
2192 {
2193 varnumber_T n1;
2194 varnumber_T n2;
2195 int error = FALSE;
2196
2197 n1 = tv_get_number_chk(tv_idx1, &error);
2198 if (error)
2199 status = FAIL;
2200 else
2201 {
2202 if (tv_idx2->v_type == VAR_SPECIAL
2203 && tv_idx2->vval.v_number == VVAL_NONE)
2204 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2205 else
2206 n2 = tv_get_number_chk(tv_idx2, &error);
2207 if (error)
2208 status = FAIL;
2209 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002210 {
2211 long bloblen = blob_len(tv_dest->vval.v_blob);
2212
2213 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002214 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002215 || check_blob_range(bloblen,
2216 n1, n2, FALSE) == FAIL)
2217 status = FAIL;
2218 else
2219 status = blob_set_range(
2220 tv_dest->vval.v_blob, n1, n2, tv);
2221 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002222 }
2223 }
2224
2225 clear_tv(tv_idx1);
2226 clear_tv(tv_idx2);
2227 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002228 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002229 clear_tv(tv);
2230
2231 if (status == FAIL)
2232 goto on_error;
2233 }
2234 break;
2235
Bram Moolenaar0186e582021-01-10 18:33:11 +01002236 // load or store variable or argument from outer scope
2237 case ISN_LOADOUTER:
2238 case ISN_STOREOUTER:
2239 {
2240 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002241 outer_T *outer = ectx->ec_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002242
2243 while (depth > 1 && outer != NULL)
2244 {
2245 outer = outer->out_up;
2246 --depth;
2247 }
2248 if (outer == NULL)
2249 {
2250 SOURCING_LNUM = iptr->isn_lnum;
2251 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002252 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002253 }
2254 tv = ((typval_T *)outer->out_stack->ga_data)
2255 + outer->out_frame_idx + STACK_FRAME_SIZE
2256 + iptr->isn_arg.outer.outer_idx;
2257 if (iptr->isn_type == ISN_LOADOUTER)
2258 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002259 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2260 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002261 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002262 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002263 }
2264 else
2265 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002266 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002267 clear_tv(tv);
2268 *tv = *STACK_TV_BOT(0);
2269 }
2270 }
2271 break;
2272
Bram Moolenaar752fc692021-01-04 21:57:11 +01002273 // unlet item in list or dict variable
2274 case ISN_UNLETINDEX:
2275 {
2276 typval_T *tv_idx = STACK_TV_BOT(-2);
2277 typval_T *tv_dest = STACK_TV_BOT(-1);
2278 int status = OK;
2279
2280 // Stack contains:
2281 // -2 index
2282 // -1 dict or list
2283 if (tv_dest->v_type == VAR_DICT)
2284 {
2285 // unlet a dict item, index must be a string
2286 if (tv_idx->v_type != VAR_STRING)
2287 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002288 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002289 semsg(_(e_expected_str_but_got_str),
2290 vartype_name(VAR_STRING),
2291 vartype_name(tv_idx->v_type));
2292 status = FAIL;
2293 }
2294 else
2295 {
2296 dict_T *d = tv_dest->vval.v_dict;
2297 char_u *key = tv_idx->vval.v_string;
2298 dictitem_T *di = NULL;
2299
2300 if (key == NULL)
2301 key = (char_u *)"";
2302 if (d != NULL)
2303 di = dict_find(d, key, (int)STRLEN(key));
2304 if (di == NULL)
2305 {
2306 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002307 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002308 semsg(_(e_dictkey), key);
2309 status = FAIL;
2310 }
2311 else
2312 {
2313 // TODO: check for dict or item locked
2314 dictitem_remove(d, di);
2315 }
2316 }
2317 }
2318 else if (tv_dest->v_type == VAR_LIST)
2319 {
2320 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002321 SOURCING_LNUM = iptr->isn_lnum;
2322 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002323 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002324 status = FAIL;
2325 }
2326 else
2327 {
2328 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002329 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002330 listitem_T *li = NULL;
2331
2332 li = list_find(l, n);
2333 if (li == NULL)
2334 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002335 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002336 semsg(_(e_listidx), n);
2337 status = FAIL;
2338 }
2339 else
2340 // TODO: check for list or item locked
2341 listitem_remove(l, li);
2342 }
2343 }
2344 else
2345 {
2346 status = FAIL;
2347 semsg(_(e_cannot_index_str),
2348 vartype_name(tv_dest->v_type));
2349 }
2350
2351 clear_tv(tv_idx);
2352 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002353 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002354 if (status == FAIL)
2355 goto on_error;
2356 }
2357 break;
2358
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002359 // unlet range of items in list variable
2360 case ISN_UNLETRANGE:
2361 {
2362 // Stack contains:
2363 // -3 index1
2364 // -2 index2
2365 // -1 dict or list
2366 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2367 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2368 typval_T *tv_dest = STACK_TV_BOT(-1);
2369 int status = OK;
2370
2371 if (tv_dest->v_type == VAR_LIST)
2372 {
2373 // indexes must be a number
2374 SOURCING_LNUM = iptr->isn_lnum;
2375 if (check_for_number(tv_idx1) == FAIL
2376 || check_for_number(tv_idx2) == FAIL)
2377 {
2378 status = FAIL;
2379 }
2380 else
2381 {
2382 list_T *l = tv_dest->vval.v_list;
2383 long n1 = (long)tv_idx1->vval.v_number;
2384 long n2 = (long)tv_idx2->vval.v_number;
2385 listitem_T *li;
2386
2387 li = list_find_index(l, &n1);
2388 if (li == NULL
2389 || list_unlet_range(l, li, NULL, n1,
2390 TRUE, n2) == FAIL)
2391 status = FAIL;
2392 }
2393 }
2394 else
2395 {
2396 status = FAIL;
2397 SOURCING_LNUM = iptr->isn_lnum;
2398 semsg(_(e_cannot_index_str),
2399 vartype_name(tv_dest->v_type));
2400 }
2401
2402 clear_tv(tv_idx1);
2403 clear_tv(tv_idx2);
2404 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002405 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002406 if (status == FAIL)
2407 goto on_error;
2408 }
2409 break;
2410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 // push constant
2412 case ISN_PUSHNR:
2413 case ISN_PUSHBOOL:
2414 case ISN_PUSHSPEC:
2415 case ISN_PUSHF:
2416 case ISN_PUSHS:
2417 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002418 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002419 case ISN_PUSHCHANNEL:
2420 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002421 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2422 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002424 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002425 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 switch (iptr->isn_type)
2427 {
2428 case ISN_PUSHNR:
2429 tv->v_type = VAR_NUMBER;
2430 tv->vval.v_number = iptr->isn_arg.number;
2431 break;
2432 case ISN_PUSHBOOL:
2433 tv->v_type = VAR_BOOL;
2434 tv->vval.v_number = iptr->isn_arg.number;
2435 break;
2436 case ISN_PUSHSPEC:
2437 tv->v_type = VAR_SPECIAL;
2438 tv->vval.v_number = iptr->isn_arg.number;
2439 break;
2440#ifdef FEAT_FLOAT
2441 case ISN_PUSHF:
2442 tv->v_type = VAR_FLOAT;
2443 tv->vval.v_float = iptr->isn_arg.fnumber;
2444 break;
2445#endif
2446 case ISN_PUSHBLOB:
2447 blob_copy(iptr->isn_arg.blob, tv);
2448 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002449 case ISN_PUSHFUNC:
2450 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002451 if (iptr->isn_arg.string == NULL)
2452 tv->vval.v_string = NULL;
2453 else
2454 tv->vval.v_string =
2455 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002456 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002457 case ISN_PUSHCHANNEL:
2458#ifdef FEAT_JOB_CHANNEL
2459 tv->v_type = VAR_CHANNEL;
2460 tv->vval.v_channel = iptr->isn_arg.channel;
2461 if (tv->vval.v_channel != NULL)
2462 ++tv->vval.v_channel->ch_refcount;
2463#endif
2464 break;
2465 case ISN_PUSHJOB:
2466#ifdef FEAT_JOB_CHANNEL
2467 tv->v_type = VAR_JOB;
2468 tv->vval.v_job = iptr->isn_arg.job;
2469 if (tv->vval.v_job != NULL)
2470 ++tv->vval.v_job->jv_refcount;
2471#endif
2472 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 default:
2474 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002475 tv->vval.v_string = vim_strsave(
2476 iptr->isn_arg.string == NULL
2477 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 }
2479 break;
2480
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002481 case ISN_UNLET:
2482 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2483 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002484 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002485 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002486 case ISN_UNLETENV:
2487 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2488 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002489
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002490 case ISN_LOCKCONST:
2491 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2492 break;
2493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 // create a list from items on the stack; uses a single allocation
2495 // for the list header and the items
2496 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002497 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
2498 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 break;
2500
2501 // create a dict from items on the stack
2502 case ISN_NEWDICT:
2503 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002504 int count = iptr->isn_arg.number;
2505 dict_T *dict = dict_alloc();
2506 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002507 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002508 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509
2510 if (dict == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002511 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 for (idx = 0; idx < count; ++idx)
2513 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002514 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002516 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002517 key = tv->vval.v_string == NULL
2518 ? (char_u *)"" : tv->vval.v_string;
2519 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002520 if (item != NULL)
2521 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002522 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002523 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002524 dict_unref(dict);
2525 goto on_error;
2526 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002527 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 clear_tv(tv);
2529 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002530 {
2531 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002532 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002533 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2535 item->di_tv.v_lock = 0;
2536 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002537 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002538 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002539 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002540 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 }
2543
2544 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002545 ectx->ec_stack.ga_len -= 2 * count - 1;
2546 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2547 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002549 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 tv = STACK_TV_BOT(-1);
2551 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002552 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 tv->vval.v_dict = dict;
2554 ++dict->dv_refcount;
2555 }
2556 break;
2557
2558 // call a :def function
2559 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002560 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002561 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2562 NULL,
2563 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002564 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002565 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 break;
2567
2568 // call a builtin function
2569 case ISN_BCALL:
2570 SOURCING_LNUM = iptr->isn_lnum;
2571 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2572 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002573 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002574 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 break;
2576
2577 // call a funcref or partial
2578 case ISN_PCALL:
2579 {
2580 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2581 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002582 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583
2584 SOURCING_LNUM = iptr->isn_lnum;
2585 if (pfunc->cpf_top)
2586 {
2587 // funcref is above the arguments
2588 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2589 }
2590 else
2591 {
2592 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002593 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002594 partial_tv = *STACK_TV_BOT(0);
2595 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002597 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002598 if (tv == &partial_tv)
2599 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002601 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 }
2603 break;
2604
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002605 case ISN_PCALL_END:
2606 // PCALL finished, arguments have been consumed and replaced by
2607 // the return value. Now clear the funcref from the stack,
2608 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002609 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002610 clear_tv(STACK_TV_BOT(-1));
2611 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2612 break;
2613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 // call a user defined function or funcref/partial
2615 case ISN_UCALL:
2616 {
2617 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2618
2619 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002620 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002621 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002622 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 }
2624 break;
2625
2626 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002627 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002628 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2629 return FAIL;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002630 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002631 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002632 tv->v_type = VAR_NUMBER;
2633 tv->vval.v_number = 0;
2634 tv->v_lock = 0;
2635 // FALLTHROUGH
2636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 case ISN_RETURN:
2638 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002639 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002640 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002641
2642 if (trystack->ga_len > 0)
2643 trycmd = ((trycmd_T *)trystack->ga_data)
2644 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002645 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002646 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002648 // jump to ":finally" or ":endtry"
2649 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002650 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002651 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002652 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 trycmd->tcd_return = TRUE;
2654 }
2655 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002656 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 }
2658 break;
2659
2660 // push a function reference to a compiled function
2661 case ISN_FUNCREF:
2662 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002663 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2664 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2665 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 if (pt == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002668 return FAIL;
2669 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002670 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002671 vim_free(pt);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002672 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002673 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002674 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002675 ectx) == FAIL)
2676 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002679 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 tv->vval.v_partial = pt;
2681 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002682 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 }
2684 break;
2685
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002686 // Create a global function from a lambda.
2687 case ISN_NEWFUNC:
2688 {
2689 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2690
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002691 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002692 ectx) == FAIL)
2693 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002694 }
2695 break;
2696
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002697 // List functions
2698 case ISN_DEF:
2699 if (iptr->isn_arg.string == NULL)
2700 list_functions(NULL);
2701 else
2702 {
2703 exarg_T ea;
2704
2705 CLEAR_FIELD(ea);
2706 ea.cmd = ea.arg = iptr->isn_arg.string;
2707 define_function(&ea, NULL);
2708 }
2709 break;
2710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 // jump if a condition is met
2712 case ISN_JUMP:
2713 {
2714 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002715 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 int jump = TRUE;
2717
2718 if (when != JUMP_ALWAYS)
2719 {
2720 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002721 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002722 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002723 || when == JUMP_IF_COND_TRUE)
2724 {
2725 SOURCING_LNUM = iptr->isn_lnum;
2726 jump = tv_get_bool_chk(tv, &error);
2727 if (error)
2728 goto on_error;
2729 }
2730 else
2731 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002733 || when == JUMP_AND_KEEP_IF_FALSE
2734 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002736 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 {
2738 // drop the value from the stack
2739 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002740 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 }
2742 }
2743 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002744 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 }
2746 break;
2747
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002748 // Jump if an argument with a default value was already set and not
2749 // v:none.
2750 case ISN_JUMP_IF_ARG_SET:
2751 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2752 if (tv->v_type != VAR_UNKNOWN
2753 && !(tv->v_type == VAR_SPECIAL
2754 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002755 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002756 break;
2757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 // top of a for loop
2759 case ISN_FOR:
2760 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002761 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 typval_T *idxtv =
2763 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2764
Bram Moolenaar4c137212021-04-19 16:48:48 +02002765 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2766 return FAIL;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002767 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002768 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002769 list_T *list = ltv->vval.v_list;
2770
2771 // push the next item from the list
2772 ++idxtv->vval.v_number;
2773 if (list == NULL
2774 || idxtv->vval.v_number >= list->lv_len)
2775 {
2776 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002777 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2778 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002779 }
2780 else if (list->lv_first == &range_list_item)
2781 {
2782 // non-materialized range() list
2783 tv = STACK_TV_BOT(0);
2784 tv->v_type = VAR_NUMBER;
2785 tv->v_lock = 0;
2786 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002788 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002789 }
2790 else
2791 {
2792 listitem_T *li = list_find(list,
2793 idxtv->vval.v_number);
2794
2795 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002796 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002797 }
2798 }
2799 else if (ltv->v_type == VAR_STRING)
2800 {
2801 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002802
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002803 // The index is for the last byte of the previous
2804 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002805 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002806 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002807 {
2808 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002809 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2810 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002811 }
2812 else
2813 {
2814 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2815
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002816 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002817 tv = STACK_TV_BOT(0);
2818 tv->v_type = VAR_STRING;
2819 tv->vval.v_string = vim_strnsave(
2820 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002821 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002822 idxtv->vval.v_number += clen - 1;
2823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002825 else if (ltv->v_type == VAR_BLOB)
2826 {
2827 blob_T *blob = ltv->vval.v_blob;
2828
2829 // When we get here the first time make a copy of the
2830 // blob, so that the iteration still works when it is
2831 // changed.
2832 if (idxtv->vval.v_number == -1 && blob != NULL)
2833 {
2834 blob_copy(blob, ltv);
2835 blob_unref(blob);
2836 blob = ltv->vval.v_blob;
2837 }
2838
2839 // The index is for the previous byte.
2840 ++idxtv->vval.v_number;
2841 if (blob == NULL
2842 || idxtv->vval.v_number >= blob_len(blob))
2843 {
2844 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002845 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2846 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002847 }
2848 else
2849 {
2850 // Push the next byte from the blob.
2851 tv = STACK_TV_BOT(0);
2852 tv->v_type = VAR_NUMBER;
2853 tv->vval.v_number = blob_get(blob,
2854 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002855 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002856 }
2857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 else
2859 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002860 semsg(_(e_for_loop_on_str_not_supported),
2861 vartype_name(ltv->v_type));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002862 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 }
2864 }
2865 break;
2866
2867 // start of ":try" block
2868 case ISN_TRY:
2869 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002870 trycmd_T *trycmd = NULL;
2871
Bram Moolenaar4c137212021-04-19 16:48:48 +02002872 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
2873 return FAIL;
2874 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
2875 + ectx->ec_trystack.ga_len;
2876 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002878 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002879 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
2880 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002881 trycmd->tcd_catch_idx =
2882 iptr->isn_arg.try.try_ref->try_catch;
2883 trycmd->tcd_finally_idx =
2884 iptr->isn_arg.try.try_ref->try_finally;
2885 trycmd->tcd_endtry_idx =
2886 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 }
2888 break;
2889
2890 case ISN_PUSHEXC:
2891 if (current_exception == NULL)
2892 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002893 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002895 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002897 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2898 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002900 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002902 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 tv->vval.v_string = vim_strsave(
2904 (char_u *)current_exception->value);
2905 break;
2906
2907 case ISN_CATCH:
2908 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002909 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910
Bram Moolenaar4c137212021-04-19 16:48:48 +02002911 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 if (trystack->ga_len > 0)
2913 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002914 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 + trystack->ga_len - 1;
2916 trycmd->tcd_caught = TRUE;
2917 }
2918 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002919 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 catch_exception(current_exception);
2921 }
2922 break;
2923
Bram Moolenaarc150c092021-02-13 15:02:46 +01002924 case ISN_TRYCONT:
2925 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002926 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002927 trycont_T *trycont = &iptr->isn_arg.trycont;
2928 int i;
2929 trycmd_T *trycmd;
2930 int iidx = trycont->tct_where;
2931
2932 if (trystack->ga_len < trycont->tct_levels)
2933 {
2934 siemsg("TRYCONT: expected %d levels, found %d",
2935 trycont->tct_levels, trystack->ga_len);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 return FAIL;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002937 }
2938 // Make :endtry jump to any outer try block and the last
2939 // :endtry inside the loop to the loop start.
2940 for (i = trycont->tct_levels; i > 0; --i)
2941 {
2942 trycmd = ((trycmd_T *)trystack->ga_data)
2943 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002944 // Add one to tcd_cont to be able to jump to
2945 // instruction with index zero.
2946 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002947 iidx = trycmd->tcd_finally_idx == 0
2948 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002949 }
2950 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02002951 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002952 }
2953 break;
2954
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002955 case ISN_FINALLY:
2956 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002957 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002958 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2959 + trystack->ga_len - 1;
2960
2961 // Reset the index to avoid a return statement jumps here
2962 // again.
2963 trycmd->tcd_finally_idx = 0;
2964 break;
2965 }
2966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 // end of ":try" block
2968 case ISN_ENDTRY:
2969 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002970 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971
2972 if (trystack->ga_len > 0)
2973 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002974 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 --trystack->ga_len;
2977 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002978 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 trycmd = ((trycmd_T *)trystack->ga_data)
2980 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002981 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 {
2983 // discard the exception
2984 if (caught_stack == current_exception)
2985 caught_stack = caught_stack->caught;
2986 discard_current_exception();
2987 }
2988
2989 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002990 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002991
Bram Moolenaar4c137212021-04-19 16:48:48 +02002992 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01002993 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002994 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002995 clear_tv(STACK_TV_BOT(0));
2996 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002997 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002998 // handling :continue: jump to outer try block or
2999 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003000 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 }
3002 }
3003 break;
3004
3005 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003006 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003007 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003008
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003009 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3010 {
3011 // throwing an exception while using "silent!" causes
3012 // the function to abort but not display an error.
3013 tv = STACK_TV_BOT(-1);
3014 clear_tv(tv);
3015 tv->v_type = VAR_NUMBER;
3016 tv->vval.v_number = 0;
3017 goto done;
3018 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003019 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003020 tv = STACK_TV_BOT(0);
3021 if (tv->vval.v_string == NULL
3022 || *skipwhite(tv->vval.v_string) == NUL)
3023 {
3024 vim_free(tv->vval.v_string);
3025 SOURCING_LNUM = iptr->isn_lnum;
3026 emsg(_(e_throw_with_empty_string));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003028 }
3029
3030 // Inside a "catch" we need to first discard the caught
3031 // exception.
3032 if (trystack->ga_len > 0)
3033 {
3034 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3035 + trystack->ga_len - 1;
3036 if (trycmd->tcd_caught && current_exception != NULL)
3037 {
3038 // discard the exception
3039 if (caught_stack == current_exception)
3040 caught_stack = caught_stack->caught;
3041 discard_current_exception();
3042 trycmd->tcd_caught = FALSE;
3043 }
3044 }
3045
3046 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3047 == FAIL)
3048 {
3049 vim_free(tv->vval.v_string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003050 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003051 }
3052 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 break;
3055
3056 // compare with special values
3057 case ISN_COMPAREBOOL:
3058 case ISN_COMPARESPECIAL:
3059 {
3060 typval_T *tv1 = STACK_TV_BOT(-2);
3061 typval_T *tv2 = STACK_TV_BOT(-1);
3062 varnumber_T arg1 = tv1->vval.v_number;
3063 varnumber_T arg2 = tv2->vval.v_number;
3064 int res;
3065
3066 switch (iptr->isn_arg.op.op_type)
3067 {
3068 case EXPR_EQUAL: res = arg1 == arg2; break;
3069 case EXPR_NEQUAL: res = arg1 != arg2; break;
3070 default: res = 0; break;
3071 }
3072
Bram Moolenaar4c137212021-04-19 16:48:48 +02003073 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 tv1->v_type = VAR_BOOL;
3075 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3076 }
3077 break;
3078
3079 // Operation with two number arguments
3080 case ISN_OPNR:
3081 case ISN_COMPARENR:
3082 {
3083 typval_T *tv1 = STACK_TV_BOT(-2);
3084 typval_T *tv2 = STACK_TV_BOT(-1);
3085 varnumber_T arg1 = tv1->vval.v_number;
3086 varnumber_T arg2 = tv2->vval.v_number;
3087 varnumber_T res;
3088
3089 switch (iptr->isn_arg.op.op_type)
3090 {
3091 case EXPR_MULT: res = arg1 * arg2; break;
3092 case EXPR_DIV: res = arg1 / arg2; break;
3093 case EXPR_REM: res = arg1 % arg2; break;
3094 case EXPR_SUB: res = arg1 - arg2; break;
3095 case EXPR_ADD: res = arg1 + arg2; break;
3096
3097 case EXPR_EQUAL: res = arg1 == arg2; break;
3098 case EXPR_NEQUAL: res = arg1 != arg2; break;
3099 case EXPR_GREATER: res = arg1 > arg2; break;
3100 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3101 case EXPR_SMALLER: res = arg1 < arg2; break;
3102 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3103 default: res = 0; break;
3104 }
3105
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 if (iptr->isn_type == ISN_COMPARENR)
3108 {
3109 tv1->v_type = VAR_BOOL;
3110 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3111 }
3112 else
3113 tv1->vval.v_number = res;
3114 }
3115 break;
3116
3117 // Computation with two float arguments
3118 case ISN_OPFLOAT:
3119 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003120#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 {
3122 typval_T *tv1 = STACK_TV_BOT(-2);
3123 typval_T *tv2 = STACK_TV_BOT(-1);
3124 float_T arg1 = tv1->vval.v_float;
3125 float_T arg2 = tv2->vval.v_float;
3126 float_T res = 0;
3127 int cmp = FALSE;
3128
3129 switch (iptr->isn_arg.op.op_type)
3130 {
3131 case EXPR_MULT: res = arg1 * arg2; break;
3132 case EXPR_DIV: res = arg1 / arg2; break;
3133 case EXPR_SUB: res = arg1 - arg2; break;
3134 case EXPR_ADD: res = arg1 + arg2; break;
3135
3136 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3137 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3138 case EXPR_GREATER: cmp = arg1 > arg2; break;
3139 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3140 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3141 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3142 default: cmp = 0; break;
3143 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 if (iptr->isn_type == ISN_COMPAREFLOAT)
3146 {
3147 tv1->v_type = VAR_BOOL;
3148 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3149 }
3150 else
3151 tv1->vval.v_float = res;
3152 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003153#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 break;
3155
3156 case ISN_COMPARELIST:
3157 {
3158 typval_T *tv1 = STACK_TV_BOT(-2);
3159 typval_T *tv2 = STACK_TV_BOT(-1);
3160 list_T *arg1 = tv1->vval.v_list;
3161 list_T *arg2 = tv2->vval.v_list;
3162 int cmp = FALSE;
3163 int ic = iptr->isn_arg.op.op_ic;
3164
3165 switch (iptr->isn_arg.op.op_type)
3166 {
3167 case EXPR_EQUAL: cmp =
3168 list_equal(arg1, arg2, ic, FALSE); break;
3169 case EXPR_NEQUAL: cmp =
3170 !list_equal(arg1, arg2, ic, FALSE); break;
3171 case EXPR_IS: cmp = arg1 == arg2; break;
3172 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3173 default: cmp = 0; break;
3174 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003175 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 clear_tv(tv1);
3177 clear_tv(tv2);
3178 tv1->v_type = VAR_BOOL;
3179 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3180 }
3181 break;
3182
3183 case ISN_COMPAREBLOB:
3184 {
3185 typval_T *tv1 = STACK_TV_BOT(-2);
3186 typval_T *tv2 = STACK_TV_BOT(-1);
3187 blob_T *arg1 = tv1->vval.v_blob;
3188 blob_T *arg2 = tv2->vval.v_blob;
3189 int cmp = FALSE;
3190
3191 switch (iptr->isn_arg.op.op_type)
3192 {
3193 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3194 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3195 case EXPR_IS: cmp = arg1 == arg2; break;
3196 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3197 default: cmp = 0; break;
3198 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003199 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 clear_tv(tv1);
3201 clear_tv(tv2);
3202 tv1->v_type = VAR_BOOL;
3203 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3204 }
3205 break;
3206
3207 // TODO: handle separately
3208 case ISN_COMPARESTRING:
3209 case ISN_COMPAREDICT:
3210 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 case ISN_COMPAREANY:
3212 {
3213 typval_T *tv1 = STACK_TV_BOT(-2);
3214 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003215 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 int ic = iptr->isn_arg.op.op_ic;
3217
Bram Moolenaareb26f432020-09-14 16:50:05 +02003218 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003219 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003221 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 }
3223 break;
3224
3225 case ISN_ADDLIST:
3226 case ISN_ADDBLOB:
3227 {
3228 typval_T *tv1 = STACK_TV_BOT(-2);
3229 typval_T *tv2 = STACK_TV_BOT(-1);
3230
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003231 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 if (iptr->isn_type == ISN_ADDLIST)
3233 eval_addlist(tv1, tv2);
3234 else
3235 eval_addblob(tv1, tv2);
3236 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003237 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 }
3239 break;
3240
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003241 case ISN_LISTAPPEND:
3242 {
3243 typval_T *tv1 = STACK_TV_BOT(-2);
3244 typval_T *tv2 = STACK_TV_BOT(-1);
3245 list_T *l = tv1->vval.v_list;
3246
3247 // add an item to a list
3248 if (l == NULL)
3249 {
3250 SOURCING_LNUM = iptr->isn_lnum;
3251 emsg(_(e_cannot_add_to_null_list));
3252 goto on_error;
3253 }
3254 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003255 return FAIL;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003256 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003257 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003258 }
3259 break;
3260
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003261 case ISN_BLOBAPPEND:
3262 {
3263 typval_T *tv1 = STACK_TV_BOT(-2);
3264 typval_T *tv2 = STACK_TV_BOT(-1);
3265 blob_T *b = tv1->vval.v_blob;
3266 int error = FALSE;
3267 varnumber_T n;
3268
3269 // add a number to a blob
3270 if (b == NULL)
3271 {
3272 SOURCING_LNUM = iptr->isn_lnum;
3273 emsg(_(e_cannot_add_to_null_blob));
3274 goto on_error;
3275 }
3276 n = tv_get_number_chk(tv2, &error);
3277 if (error)
3278 goto on_error;
3279 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003280 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003281 }
3282 break;
3283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 // Computation with two arguments of unknown type
3285 case ISN_OPANY:
3286 {
3287 typval_T *tv1 = STACK_TV_BOT(-2);
3288 typval_T *tv2 = STACK_TV_BOT(-1);
3289 varnumber_T n1, n2;
3290#ifdef FEAT_FLOAT
3291 float_T f1 = 0, f2 = 0;
3292#endif
3293 int error = FALSE;
3294
3295 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3296 {
3297 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3298 {
3299 eval_addlist(tv1, tv2);
3300 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003301 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 break;
3303 }
3304 else if (tv1->v_type == VAR_BLOB
3305 && tv2->v_type == VAR_BLOB)
3306 {
3307 eval_addblob(tv1, tv2);
3308 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003309 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 break;
3311 }
3312 }
3313#ifdef FEAT_FLOAT
3314 if (tv1->v_type == VAR_FLOAT)
3315 {
3316 f1 = tv1->vval.v_float;
3317 n1 = 0;
3318 }
3319 else
3320#endif
3321 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003322 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 n1 = tv_get_number_chk(tv1, &error);
3324 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003325 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326#ifdef FEAT_FLOAT
3327 if (tv2->v_type == VAR_FLOAT)
3328 f1 = n1;
3329#endif
3330 }
3331#ifdef FEAT_FLOAT
3332 if (tv2->v_type == VAR_FLOAT)
3333 {
3334 f2 = tv2->vval.v_float;
3335 n2 = 0;
3336 }
3337 else
3338#endif
3339 {
3340 n2 = tv_get_number_chk(tv2, &error);
3341 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003342 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343#ifdef FEAT_FLOAT
3344 if (tv1->v_type == VAR_FLOAT)
3345 f2 = n2;
3346#endif
3347 }
3348#ifdef FEAT_FLOAT
3349 // if there is a float on either side the result is a float
3350 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3351 {
3352 switch (iptr->isn_arg.op.op_type)
3353 {
3354 case EXPR_MULT: f1 = f1 * f2; break;
3355 case EXPR_DIV: f1 = f1 / f2; break;
3356 case EXPR_SUB: f1 = f1 - f2; break;
3357 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003358 default: SOURCING_LNUM = iptr->isn_lnum;
3359 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003360 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 }
3362 clear_tv(tv1);
3363 clear_tv(tv2);
3364 tv1->v_type = VAR_FLOAT;
3365 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003366 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 }
3368 else
3369#endif
3370 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003371 int failed = FALSE;
3372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 switch (iptr->isn_arg.op.op_type)
3374 {
3375 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003376 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3377 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003378 goto on_error;
3379 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 case EXPR_SUB: n1 = n1 - n2; break;
3381 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003382 default: n1 = num_modulus(n1, n2, &failed);
3383 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003384 goto on_error;
3385 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 }
3387 clear_tv(tv1);
3388 clear_tv(tv2);
3389 tv1->v_type = VAR_NUMBER;
3390 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003391 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 }
3393 }
3394 break;
3395
3396 case ISN_CONCAT:
3397 {
3398 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3399 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3400 char_u *res;
3401
3402 res = concat_str(str1, str2);
3403 clear_tv(STACK_TV_BOT(-2));
3404 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003405 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 STACK_TV_BOT(-1)->vval.v_string = res;
3407 }
3408 break;
3409
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003410 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003411 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003412 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003413 int is_slice = iptr->isn_type == ISN_STRSLICE;
3414 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003415 char_u *res;
3416
3417 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003418 // string slice: string is at stack-3, first index at
3419 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003420 if (is_slice)
3421 {
3422 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003423 n1 = tv->vval.v_number;
3424 }
3425
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003426 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003427 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003428
Bram Moolenaar4c137212021-04-19 16:48:48 +02003429 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003430 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003431 if (is_slice)
3432 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003433 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003434 else
3435 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003436 // single character (including composing characters).
3437 // If the index is too big or negative the result is
3438 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003439 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003440 vim_free(tv->vval.v_string);
3441 tv->vval.v_string = res;
3442 }
3443 break;
3444
3445 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003446 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003447 case ISN_BLOBINDEX:
3448 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003450 int is_slice = iptr->isn_type == ISN_LISTSLICE
3451 || iptr->isn_type == ISN_BLOBSLICE;
3452 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3453 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003454 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003455 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456
3457 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003458 // list slice: list is at stack-3, indexes at stack-2 and
3459 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003460 // Same for blob.
3461 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462
3463 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003464 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003466
3467 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 {
Bram Moolenaared591872020-08-15 22:14:53 +02003469 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003470 n1 = tv->vval.v_number;
3471 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 }
Bram Moolenaared591872020-08-15 22:14:53 +02003473
Bram Moolenaar4c137212021-04-19 16:48:48 +02003474 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003475 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003476 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003477 if (is_blob)
3478 {
3479 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3480 n1, n2, FALSE, tv) == FAIL)
3481 goto on_error;
3482 }
3483 else
3484 {
3485 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3486 n1, n2, FALSE, tv, TRUE) == FAIL)
3487 goto on_error;
3488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 }
3490 break;
3491
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003492 case ISN_ANYINDEX:
3493 case ISN_ANYSLICE:
3494 {
3495 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3496 typval_T *var1, *var2;
3497 int res;
3498
3499 // index: composite is at stack-2, index at stack-1
3500 // slice: composite is at stack-3, indexes at stack-2 and
3501 // stack-1
3502 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003503 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003504 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3505 goto on_error;
3506 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3507 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003508 res = eval_index_inner(tv, is_slice, var1, var2,
3509 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003510 clear_tv(var1);
3511 if (is_slice)
3512 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003513 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003514 if (res == FAIL)
3515 goto on_error;
3516 }
3517 break;
3518
Bram Moolenaar9af78762020-06-16 11:34:42 +02003519 case ISN_SLICE:
3520 {
3521 list_T *list;
3522 int count = iptr->isn_arg.number;
3523
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003524 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003525 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003526 list = tv->vval.v_list;
3527
3528 // no error for short list, expect it to be checked earlier
3529 if (list != NULL && list->lv_len >= count)
3530 {
3531 list_T *newlist = list_slice(list,
3532 count, list->lv_len - 1);
3533
3534 if (newlist != NULL)
3535 {
3536 list_unref(list);
3537 tv->vval.v_list = newlist;
3538 ++newlist->lv_refcount;
3539 }
3540 }
3541 }
3542 break;
3543
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003544 case ISN_GETITEM:
3545 {
3546 listitem_T *li;
3547 int index = iptr->isn_arg.number;
3548
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003549 // Get list item: list is at stack-1, push item.
3550 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003551 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003552 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003553
Bram Moolenaar4c137212021-04-19 16:48:48 +02003554 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3555 return FAIL;
3556 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003557 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003558
3559 // Useful when used in unpack assignment. Reset at
3560 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003561 ectx->ec_where.wt_index = index + 1;
3562 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003563 }
3564 break;
3565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 case ISN_MEMBER:
3567 {
3568 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003569 char_u *key;
3570 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003571 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003572
3573 // dict member: dict is at stack-2, key at stack-1
3574 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003575 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003576 dict = tv->vval.v_dict;
3577
3578 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003579 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003580 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003581 if (key == NULL)
3582 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003583
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003584 if ((di = dict_find(dict, key, -1)) == NULL)
3585 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003586 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003587 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003588
3589 // If :silent! is used we will continue, make sure the
3590 // stack contents makes sense.
3591 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003592 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003593 tv = STACK_TV_BOT(-1);
3594 clear_tv(tv);
3595 tv->v_type = VAR_NUMBER;
3596 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003597 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003598 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003599 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003600 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003601 // Clear the dict only after getting the item, to avoid
3602 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003603 tv = STACK_TV_BOT(-1);
3604 temp_tv = *tv;
3605 copy_tv(&di->di_tv, tv);
3606 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003607 }
3608 break;
3609
3610 // dict member with string key
3611 case ISN_STRINGMEMBER:
3612 {
3613 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003615 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616
3617 tv = STACK_TV_BOT(-1);
3618 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3619 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003620 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003622 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 }
3624 dict = tv->vval.v_dict;
3625
3626 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3627 == NULL)
3628 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003629 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003631 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003633 // Clear the dict after getting the item, to avoid that it
3634 // make the item invalid.
3635 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003637 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 }
3639 break;
3640
3641 case ISN_NEGATENR:
3642 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003643 if (tv->v_type != VAR_NUMBER
3644#ifdef FEAT_FLOAT
3645 && tv->v_type != VAR_FLOAT
3646#endif
3647 )
3648 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003649 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003650 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003651 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003652 }
3653#ifdef FEAT_FLOAT
3654 if (tv->v_type == VAR_FLOAT)
3655 tv->vval.v_float = -tv->vval.v_float;
3656 else
3657#endif
3658 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 break;
3660
3661 case ISN_CHECKNR:
3662 {
3663 int error = FALSE;
3664
3665 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003666 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003668 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 (void)tv_get_number_chk(tv, &error);
3670 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003671 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 }
3673 break;
3674
3675 case ISN_CHECKTYPE:
3676 {
3677 checktype_T *ct = &iptr->isn_arg.type;
3678
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003679 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003680 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003681 if (!ectx->ec_where.wt_variable)
3682 ectx->ec_where.wt_index = ct->ct_arg_idx;
3683 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3684 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003685 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003686 if (!ectx->ec_where.wt_variable)
3687 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003688
3689 // number 0 is FALSE, number 1 is TRUE
3690 if (tv->v_type == VAR_NUMBER
3691 && ct->ct_type->tt_type == VAR_BOOL
3692 && (tv->vval.v_number == 0
3693 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003695 tv->v_type = VAR_BOOL;
3696 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003697 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 }
3699 }
3700 break;
3701
Bram Moolenaar9af78762020-06-16 11:34:42 +02003702 case ISN_CHECKLEN:
3703 {
3704 int min_len = iptr->isn_arg.checklen.cl_min_len;
3705 list_T *list = NULL;
3706
3707 tv = STACK_TV_BOT(-1);
3708 if (tv->v_type == VAR_LIST)
3709 list = tv->vval.v_list;
3710 if (list == NULL || list->lv_len < min_len
3711 || (list->lv_len > min_len
3712 && !iptr->isn_arg.checklen.cl_more_OK))
3713 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003714 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003715 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003716 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003717 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003718 }
3719 }
3720 break;
3721
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003722 case ISN_SETTYPE:
3723 {
3724 checktype_T *ct = &iptr->isn_arg.type;
3725
3726 tv = STACK_TV_BOT(-1);
3727 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3728 {
3729 free_type(tv->vval.v_dict->dv_type);
3730 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3731 }
3732 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3733 {
3734 free_type(tv->vval.v_list->lv_type);
3735 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3736 }
3737 }
3738 break;
3739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003741 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 {
3743 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003744 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745
3746 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003747 if (iptr->isn_type == ISN_2BOOL)
3748 {
3749 n = tv2bool(tv);
3750 if (iptr->isn_arg.number) // invert
3751 n = !n;
3752 }
3753 else
3754 {
3755 SOURCING_LNUM = iptr->isn_lnum;
3756 n = tv_get_bool_chk(tv, &error);
3757 if (error)
3758 goto on_error;
3759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 clear_tv(tv);
3761 tv->v_type = VAR_BOOL;
3762 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3763 }
3764 break;
3765
3766 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003767 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003768 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003769 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3770 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3771 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 break;
3773
Bram Moolenaar08597872020-12-10 19:43:40 +01003774 case ISN_RANGE:
3775 {
3776 exarg_T ea;
3777 char *errormsg;
3778
Bram Moolenaarece0b872021-01-08 20:40:45 +01003779 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003780 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003781 ea.addr_type = ADDR_LINES;
3782 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003783 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003784 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003785 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003786
Bram Moolenaar4c137212021-04-19 16:48:48 +02003787 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3788 return FAIL;
3789 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003790 tv = STACK_TV_BOT(-1);
3791 tv->v_type = VAR_NUMBER;
3792 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003793 if (ea.addr_count == 0)
3794 tv->vval.v_number = curwin->w_cursor.lnum;
3795 else
3796 tv->vval.v_number = ea.line2;
3797 }
3798 break;
3799
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003800 case ISN_PUT:
3801 {
3802 int regname = iptr->isn_arg.put.put_regname;
3803 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3804 char_u *expr = NULL;
3805 int dir = FORWARD;
3806
Bram Moolenaar08597872020-12-10 19:43:40 +01003807 if (lnum < -2)
3808 {
3809 // line number was put on the stack by ISN_RANGE
3810 tv = STACK_TV_BOT(-1);
3811 curwin->w_cursor.lnum = tv->vval.v_number;
3812 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3813 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003814 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003815 }
3816 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003817 // :put! above cursor
3818 dir = BACKWARD;
3819 else if (lnum >= 0)
3820 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003821
3822 if (regname == '=')
3823 {
3824 tv = STACK_TV_BOT(-1);
3825 if (tv->v_type == VAR_STRING)
3826 expr = tv->vval.v_string;
3827 else
3828 {
3829 expr = typval2string(tv, TRUE); // allocates value
3830 clear_tv(tv);
3831 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003832 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003833 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003834 check_cursor();
3835 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3836 vim_free(expr);
3837 }
3838 break;
3839
Bram Moolenaar02194d22020-10-24 23:08:38 +02003840 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003841 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3842 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3843 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3844 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003845 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3846 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003847 break;
3848
Bram Moolenaar02194d22020-10-24 23:08:38 +02003849 case ISN_CMDMOD_REV:
3850 // filter regprog is owned by the instruction, don't free it
3851 cmdmod.cmod_filter_regmatch.regprog = NULL;
3852 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003853 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
3854 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003855 break;
3856
Bram Moolenaar792f7862020-11-23 08:31:18 +01003857 case ISN_UNPACK:
3858 {
3859 int count = iptr->isn_arg.unpack.unp_count;
3860 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3861 list_T *l;
3862 listitem_T *li;
3863 int i;
3864
3865 // Check there is a valid list to unpack.
3866 tv = STACK_TV_BOT(-1);
3867 if (tv->v_type != VAR_LIST)
3868 {
3869 SOURCING_LNUM = iptr->isn_lnum;
3870 emsg(_(e_for_argument_must_be_sequence_of_lists));
3871 goto on_error;
3872 }
3873 l = tv->vval.v_list;
3874 if (l == NULL
3875 || l->lv_len < (semicolon ? count - 1 : count))
3876 {
3877 SOURCING_LNUM = iptr->isn_lnum;
3878 emsg(_(e_list_value_does_not_have_enough_items));
3879 goto on_error;
3880 }
3881 else if (!semicolon && l->lv_len > count)
3882 {
3883 SOURCING_LNUM = iptr->isn_lnum;
3884 emsg(_(e_list_value_has_more_items_than_targets));
3885 goto on_error;
3886 }
3887
3888 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003889 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
3890 return FAIL;
3891 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003892
3893 // Variable after semicolon gets a list with the remaining
3894 // items.
3895 if (semicolon)
3896 {
3897 list_T *rem_list =
3898 list_alloc_with_items(l->lv_len - count + 1);
3899
3900 if (rem_list == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003901 return FAIL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003902 tv = STACK_TV_BOT(-count);
3903 tv->vval.v_list = rem_list;
3904 ++rem_list->lv_refcount;
3905 tv->v_lock = 0;
3906 li = l->lv_first;
3907 for (i = 0; i < count - 1; ++i)
3908 li = li->li_next;
3909 for (i = 0; li != NULL; ++i)
3910 {
3911 list_set_item(rem_list, i, &li->li_tv);
3912 li = li->li_next;
3913 }
3914 --count;
3915 }
3916
3917 // Produce the values in reverse order, first item last.
3918 li = l->lv_first;
3919 for (i = 0; i < count; ++i)
3920 {
3921 tv = STACK_TV_BOT(-i - 1);
3922 copy_tv(&li->li_tv, tv);
3923 li = li->li_next;
3924 }
3925
3926 list_unref(l);
3927 }
3928 break;
3929
Bram Moolenaarb2049902021-01-24 12:53:53 +01003930 case ISN_PROF_START:
3931 case ISN_PROF_END:
3932 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003933#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003934 funccall_T cookie;
3935 ufunc_T *cur_ufunc =
3936 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003937 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003938
3939 cookie.func = cur_ufunc;
3940 if (iptr->isn_type == ISN_PROF_START)
3941 {
3942 func_line_start(&cookie, iptr->isn_lnum);
3943 // if we get here the instruction is executed
3944 func_line_exec(&cookie);
3945 }
3946 else
3947 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003948#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003949 }
3950 break;
3951
Bram Moolenaar389df252020-07-09 21:20:47 +02003952 case ISN_SHUFFLE:
3953 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003954 typval_T tmp_tv;
3955 int item = iptr->isn_arg.shuffle.shfl_item;
3956 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003957
3958 tmp_tv = *STACK_TV_BOT(-item);
3959 for ( ; up > 0 && item > 1; --up)
3960 {
3961 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3962 --item;
3963 }
3964 *STACK_TV_BOT(-item) = tmp_tv;
3965 }
3966 break;
3967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003969 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003971 ectx->ec_where.wt_index = 0;
3972 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 break;
3974 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003975 continue;
3976
Bram Moolenaard032f342020-07-18 18:13:02 +02003977func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003978 // Restore previous function. If the frame pointer is where we started
3979 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003980 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003981 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003982
Bram Moolenaar4c137212021-04-19 16:48:48 +02003983 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003984 // only fails when out of memory
Bram Moolenaar4c137212021-04-19 16:48:48 +02003985 return FAIL;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003986 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003987
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003988on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003989 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003990 // If "emsg_silent" is set then ignore the error, unless it was set
3991 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003992 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003993 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003994 {
3995 // If a sequence of instructions causes an error while ":silent!"
3996 // was used, restore the stack length and jump ahead to restoring
3997 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003998 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003999 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004000 while (ectx->ec_stack.ga_len
4001 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004002 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004003 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004004 clear_tv(STACK_TV_BOT(0));
4005 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004006 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4007 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004008 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004009 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004010 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004011on_fatal_error:
4012 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004013 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004014 if (trylevel <= ectx->ec_trylevel_at_start)
4015 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 }
4017
4018done:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004019 return OK;
4020}
4021
4022/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004023 * Execute the instructions from a VAR_INSTR typeval and put the result in
4024 * "rettv".
4025 * Return OK or FAIL.
4026 */
4027 int
4028exe_typval_instr(typval_T *tv, typval_T *rettv)
4029{
4030 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4031 isn_T *save_instr = ectx->ec_instr;
4032 int save_iidx = ectx->ec_iidx;
4033 int res;
4034
4035 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4036 res = exec_instructions(ectx);
4037 if (res == OK)
4038 {
4039 *rettv = *STACK_TV_BOT(-1);
4040 --ectx->ec_stack.ga_len;
4041 }
4042
4043 ectx->ec_instr = save_instr;
4044 ectx->ec_iidx = save_iidx;
4045
4046 return res;
4047}
4048
4049/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004050 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4051 * "substitute_instr".
4052 */
4053 char_u *
4054exe_substitute_instr(void)
4055{
4056 ectx_T *ectx = substitute_instr->subs_ectx;
4057 isn_T *save_instr = ectx->ec_instr;
4058 int save_iidx = ectx->ec_iidx;
4059 char_u *res;
4060
4061 ectx->ec_instr = substitute_instr->subs_instr;
4062 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004063 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004064 typval_T *tv = STACK_TV_BOT(-1);
4065
4066 res = vim_strsave(tv_get_string(tv));
4067 --ectx->ec_stack.ga_len;
4068 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004069 }
4070 else
4071 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004072 substitute_instr->subs_status = FAIL;
4073 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075
Bram Moolenaar4c137212021-04-19 16:48:48 +02004076 ectx->ec_instr = save_instr;
4077 ectx->ec_iidx = save_iidx;
4078
4079 return res;
4080}
4081
4082/*
4083 * Call a "def" function from old Vim script.
4084 * Return OK or FAIL.
4085 */
4086 int
4087call_def_function(
4088 ufunc_T *ufunc,
4089 int argc_arg, // nr of arguments
4090 typval_T *argv, // arguments
4091 partial_T *partial, // optional partial for context
4092 typval_T *rettv) // return value
4093{
4094 ectx_T ectx; // execution context
4095 int argc = argc_arg;
4096 typval_T *tv;
4097 int idx;
4098 int ret = FAIL;
4099 int defcount = ufunc->uf_args.ga_len - argc;
4100 sctx_T save_current_sctx = current_sctx;
4101 int did_emsg_before = did_emsg_cumul + did_emsg;
4102 int save_suppress_errthrow = suppress_errthrow;
4103 msglist_T **saved_msg_list = NULL;
4104 msglist_T *private_msg_list = NULL;
4105 int save_emsg_silent_def = emsg_silent_def;
4106 int save_did_emsg_def = did_emsg_def;
4107 int orig_funcdepth;
4108
4109// Get pointer to item in the stack.
4110#undef STACK_TV
4111#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4112
4113// Get pointer to item at the bottom of the stack, -1 is the bottom.
4114#undef STACK_TV_BOT
4115#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4116
4117// Get pointer to a local variable on the stack. Negative for arguments.
4118#undef STACK_TV_VAR
4119#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4120
4121 if (ufunc->uf_def_status == UF_NOT_COMPILED
4122 || ufunc->uf_def_status == UF_COMPILE_ERROR
4123 || (func_needs_compiling(ufunc, PROFILING(ufunc))
4124 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
4125 == FAIL))
4126 {
4127 if (did_emsg_cumul + did_emsg == did_emsg_before)
4128 semsg(_(e_function_is_not_compiled_str),
4129 printable_func_name(ufunc));
4130 return FAIL;
4131 }
4132
4133 {
4134 // Check the function was really compiled.
4135 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4136 + ufunc->uf_dfunc_idx;
4137 if (INSTRUCTIONS(dfunc) == NULL)
4138 {
4139 iemsg("using call_def_function() on not compiled function");
4140 return FAIL;
4141 }
4142 }
4143
4144 // If depth of calling is getting too high, don't execute the function.
4145 orig_funcdepth = funcdepth_get();
4146 if (funcdepth_increment() == FAIL)
4147 return FAIL;
4148
4149 CLEAR_FIELD(ectx);
4150 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4151 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4152 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4153 {
4154 funcdepth_decrement();
4155 return FAIL;
4156 }
4157 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4158 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4159 ectx.ec_did_emsg_before = did_emsg_before;
4160 ectx.ec_trylevel_at_start = trylevel;
4161
4162 idx = argc - ufunc->uf_args.ga_len;
4163 if (idx > 0 && ufunc->uf_va_name == NULL)
4164 {
4165 if (idx == 1)
4166 emsg(_(e_one_argument_too_many));
4167 else
4168 semsg(_(e_nr_arguments_too_many), idx);
4169 goto failed_early;
4170 }
4171
4172 // Put arguments on the stack, but no more than what the function expects.
4173 // A lambda can be called with more arguments than it uses.
4174 for (idx = 0; idx < argc
4175 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4176 ++idx)
4177 {
4178 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4179 && argv[idx].v_type == VAR_SPECIAL
4180 && argv[idx].vval.v_number == VVAL_NONE)
4181 {
4182 // Use the default value.
4183 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4184 }
4185 else
4186 {
4187 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4188 && check_typval_arg_type(
4189 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4190 goto failed_early;
4191 copy_tv(&argv[idx], STACK_TV_BOT(0));
4192 }
4193 ++ectx.ec_stack.ga_len;
4194 }
4195
4196 // Turn varargs into a list. Empty list if no args.
4197 if (ufunc->uf_va_name != NULL)
4198 {
4199 int vararg_count = argc - ufunc->uf_args.ga_len;
4200
4201 if (vararg_count < 0)
4202 vararg_count = 0;
4203 else
4204 argc -= vararg_count;
4205 if (exe_newlist(vararg_count, &ectx) == FAIL)
4206 goto failed_early;
4207
4208 // Check the type of the list items.
4209 tv = STACK_TV_BOT(-1);
4210 if (ufunc->uf_va_type != NULL
4211 && ufunc->uf_va_type != &t_list_any
4212 && ufunc->uf_va_type->tt_member != &t_any
4213 && tv->vval.v_list != NULL)
4214 {
4215 type_T *expected = ufunc->uf_va_type->tt_member;
4216 listitem_T *li = tv->vval.v_list->lv_first;
4217
4218 for (idx = 0; idx < vararg_count; ++idx)
4219 {
4220 if (check_typval_arg_type(expected, &li->li_tv,
4221 argc + idx + 1) == FAIL)
4222 goto failed_early;
4223 li = li->li_next;
4224 }
4225 }
4226
4227 if (defcount > 0)
4228 // Move varargs list to below missing default arguments.
4229 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4230 --ectx.ec_stack.ga_len;
4231 }
4232
4233 // Make space for omitted arguments, will store default value below.
4234 // Any varargs list goes after them.
4235 if (defcount > 0)
4236 for (idx = 0; idx < defcount; ++idx)
4237 {
4238 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4239 ++ectx.ec_stack.ga_len;
4240 }
4241 if (ufunc->uf_va_name != NULL)
4242 ++ectx.ec_stack.ga_len;
4243
4244 // Frame pointer points to just after arguments.
4245 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4246 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4247
4248 {
4249 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4250 + ufunc->uf_dfunc_idx;
4251 ufunc_T *base_ufunc = dfunc->df_ufunc;
4252
4253 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4254 // by copy_func().
4255 if (partial != NULL || base_ufunc->uf_partial != NULL)
4256 {
4257 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
4258 if (ectx.ec_outer == NULL)
4259 goto failed_early;
4260 if (partial != NULL)
4261 {
4262 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4263 {
4264 if (current_ectx->ec_outer != NULL)
4265 *ectx.ec_outer = *current_ectx->ec_outer;
4266 }
4267 else
4268 *ectx.ec_outer = partial->pt_outer;
4269 }
4270 else
4271 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
4272 ectx.ec_outer->out_up_is_copy = TRUE;
4273 }
4274 }
4275
4276 // dummy frame entries
4277 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4278 {
4279 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4280 ++ectx.ec_stack.ga_len;
4281 }
4282
4283 {
4284 // Reserve space for local variables and any closure reference count.
4285 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4286 + ufunc->uf_dfunc_idx;
4287
4288 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4289 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4290 ectx.ec_stack.ga_len += dfunc->df_varcount;
4291 if (dfunc->df_has_closure)
4292 {
4293 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4294 STACK_TV_VAR(idx)->vval.v_number = 0;
4295 ++ectx.ec_stack.ga_len;
4296 }
4297
4298 ectx.ec_instr = INSTRUCTIONS(dfunc);
4299 }
4300
4301 // Following errors are in the function, not the caller.
4302 // Commands behave like vim9script.
4303 estack_push_ufunc(ufunc, 1);
4304 current_sctx = ufunc->uf_script_ctx;
4305 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4306
4307 // Use a specific location for storing error messages to be converted to an
4308 // exception.
4309 saved_msg_list = msg_list;
4310 msg_list = &private_msg_list;
4311
4312 // Do turn errors into exceptions.
4313 suppress_errthrow = FALSE;
4314
4315 // Do not delete the function while executing it.
4316 ++ufunc->uf_calls;
4317
4318 // When ":silent!" was used before calling then we still abort the
4319 // function. If ":silent!" is used in the function then we don't.
4320 emsg_silent_def = emsg_silent;
4321 did_emsg_def = 0;
4322
4323 ectx.ec_where.wt_index = 0;
4324 ectx.ec_where.wt_variable = FALSE;
4325
4326 // Execute the instructions until done.
4327 ret = exec_instructions(&ectx);
4328 if (ret == OK)
4329 {
4330 // function finished, get result from the stack.
4331 if (ufunc->uf_ret_type == &t_void)
4332 {
4333 rettv->v_type = VAR_VOID;
4334 }
4335 else
4336 {
4337 tv = STACK_TV_BOT(-1);
4338 *rettv = *tv;
4339 tv->v_type = VAR_UNKNOWN;
4340 }
4341 }
4342
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004343 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004344 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4345 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004346
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004347 // Deal with any remaining closures, they may be in use somewhere.
4348 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004349 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004350 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004351 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4352 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004353
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004354 estack_pop();
4355 current_sctx = save_current_sctx;
4356
Bram Moolenaarc970e422021-03-17 15:03:04 +01004357 // TODO: when is it safe to delete the function if it is no longer used?
4358 --ufunc->uf_calls;
4359
Bram Moolenaar352134b2020-10-17 22:04:08 +02004360 if (*msg_list != NULL && saved_msg_list != NULL)
4361 {
4362 msglist_T **plist = saved_msg_list;
4363
4364 // Append entries from the current msg_list (uncaught exceptions) to
4365 // the saved msg_list.
4366 while (*plist != NULL)
4367 plist = &(*plist)->next;
4368
4369 *plist = *msg_list;
4370 }
4371 msg_list = saved_msg_list;
4372
Bram Moolenaar4c137212021-04-19 16:48:48 +02004373 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004374 {
4375 cmdmod.cmod_filter_regmatch.regprog = NULL;
4376 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004377 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004378 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004379 emsg_silent_def = save_emsg_silent_def;
4380 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004381
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004382failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004383 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4385 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004388 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004389
Bram Moolenaar0186e582021-01-10 18:33:11 +01004390 while (ectx.ec_outer != NULL)
4391 {
4392 outer_T *up = ectx.ec_outer->out_up_is_copy
4393 ? NULL : ectx.ec_outer->out_up;
4394
4395 vim_free(ectx.ec_outer);
4396 ectx.ec_outer = up;
4397 }
4398
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004399 // Not sure if this is necessary.
4400 suppress_errthrow = save_suppress_errthrow;
4401
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004402 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004403 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004404 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004405 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 return ret;
4407}
4408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004410 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4411 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4412 * "pfx" is prefixed to every line.
4413 */
4414 static void
4415list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4416{
4417 int line_idx = 0;
4418 int prev_current = 0;
4419 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004420 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004421
4422 for (current = 0; current < instr_count; ++current)
4423 {
4424 isn_T *iptr = &instr[current];
4425 char *line;
4426
4427 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004428 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004429 while (line_idx < iptr->isn_lnum
4430 && line_idx < ufunc->uf_lines.ga_len)
4431 {
4432 if (current > prev_current)
4433 {
4434 msg_puts("\n\n");
4435 prev_current = current;
4436 }
4437 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4438 if (line != NULL)
4439 msg(line);
4440 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004441 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4442 {
4443 int first_def_arg = ufunc->uf_args.ga_len
4444 - ufunc->uf_def_args.ga_len;
4445
4446 if (def_arg_idx > 0)
4447 msg_puts("\n\n");
4448 msg_start();
4449 msg_puts(" ");
4450 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4451 first_def_arg + def_arg_idx]);
4452 msg_puts(" = ");
4453 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4454 msg_clr_eos();
4455 msg_end();
4456 }
4457 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004458
4459 switch (iptr->isn_type)
4460 {
4461 case ISN_EXEC:
4462 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4463 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004464 case ISN_REDIRSTART:
4465 smsg("%s%4d REDIR", pfx, current);
4466 break;
4467 case ISN_REDIREND:
4468 smsg("%s%4d REDIR END%s", pfx, current,
4469 iptr->isn_arg.number ? " append" : "");
4470 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004471 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004472#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004473 smsg("%s%4d CEXPR pre %s", pfx, current,
4474 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004475#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004476 break;
4477 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004478#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004479 {
4480 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4481
4482 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4483 cexpr_get_auname(cer->cer_cmdidx),
4484 cer->cer_forceit ? "!" : "",
4485 cer->cer_cmdline);
4486 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004487#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004488 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004489 case ISN_INSTR:
4490 {
4491 smsg("%s%4d INSTR", pfx, current);
4492 list_instructions(" ", iptr->isn_arg.instr,
4493 INT_MAX, NULL);
4494 msg(" -------------");
4495 }
4496 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004497 case ISN_SUBSTITUTE:
4498 {
4499 subs_T *subs = &iptr->isn_arg.subs;
4500
4501 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4502 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4503 msg(" -------------");
4504 }
4505 break;
4506 case ISN_EXECCONCAT:
4507 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4508 (varnumber_T)iptr->isn_arg.number);
4509 break;
4510 case ISN_ECHO:
4511 {
4512 echo_T *echo = &iptr->isn_arg.echo;
4513
4514 smsg("%s%4d %s %d", pfx, current,
4515 echo->echo_with_white ? "ECHO" : "ECHON",
4516 echo->echo_count);
4517 }
4518 break;
4519 case ISN_EXECUTE:
4520 smsg("%s%4d EXECUTE %lld", pfx, current,
4521 (varnumber_T)(iptr->isn_arg.number));
4522 break;
4523 case ISN_ECHOMSG:
4524 smsg("%s%4d ECHOMSG %lld", pfx, current,
4525 (varnumber_T)(iptr->isn_arg.number));
4526 break;
4527 case ISN_ECHOERR:
4528 smsg("%s%4d ECHOERR %lld", pfx, current,
4529 (varnumber_T)(iptr->isn_arg.number));
4530 break;
4531 case ISN_LOAD:
4532 {
4533 if (iptr->isn_arg.number < 0)
4534 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4535 (varnumber_T)(iptr->isn_arg.number
4536 + STACK_FRAME_SIZE));
4537 else
4538 smsg("%s%4d LOAD $%lld", pfx, current,
4539 (varnumber_T)(iptr->isn_arg.number));
4540 }
4541 break;
4542 case ISN_LOADOUTER:
4543 {
4544 if (iptr->isn_arg.number < 0)
4545 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4546 iptr->isn_arg.outer.outer_depth,
4547 iptr->isn_arg.outer.outer_idx
4548 + STACK_FRAME_SIZE);
4549 else
4550 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4551 iptr->isn_arg.outer.outer_depth,
4552 iptr->isn_arg.outer.outer_idx);
4553 }
4554 break;
4555 case ISN_LOADV:
4556 smsg("%s%4d LOADV v:%s", pfx, current,
4557 get_vim_var_name(iptr->isn_arg.number));
4558 break;
4559 case ISN_LOADSCRIPT:
4560 {
4561 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4562 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4563 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4564 + sref->sref_idx;
4565
4566 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4567 sv->sv_name,
4568 sref->sref_idx,
4569 si->sn_name);
4570 }
4571 break;
4572 case ISN_LOADS:
4573 {
4574 scriptitem_T *si = SCRIPT_ITEM(
4575 iptr->isn_arg.loadstore.ls_sid);
4576
4577 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4578 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4579 }
4580 break;
4581 case ISN_LOADAUTO:
4582 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4583 break;
4584 case ISN_LOADG:
4585 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4586 break;
4587 case ISN_LOADB:
4588 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4589 break;
4590 case ISN_LOADW:
4591 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4592 break;
4593 case ISN_LOADT:
4594 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4595 break;
4596 case ISN_LOADGDICT:
4597 smsg("%s%4d LOAD g:", pfx, current);
4598 break;
4599 case ISN_LOADBDICT:
4600 smsg("%s%4d LOAD b:", pfx, current);
4601 break;
4602 case ISN_LOADWDICT:
4603 smsg("%s%4d LOAD w:", pfx, current);
4604 break;
4605 case ISN_LOADTDICT:
4606 smsg("%s%4d LOAD t:", pfx, current);
4607 break;
4608 case ISN_LOADOPT:
4609 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4610 break;
4611 case ISN_LOADENV:
4612 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4613 break;
4614 case ISN_LOADREG:
4615 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4616 break;
4617
4618 case ISN_STORE:
4619 if (iptr->isn_arg.number < 0)
4620 smsg("%s%4d STORE arg[%lld]", pfx, current,
4621 iptr->isn_arg.number + STACK_FRAME_SIZE);
4622 else
4623 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4624 break;
4625 case ISN_STOREOUTER:
4626 {
4627 if (iptr->isn_arg.number < 0)
4628 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4629 iptr->isn_arg.outer.outer_depth,
4630 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4631 else
4632 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4633 iptr->isn_arg.outer.outer_depth,
4634 iptr->isn_arg.outer.outer_idx);
4635 }
4636 break;
4637 case ISN_STOREV:
4638 smsg("%s%4d STOREV v:%s", pfx, current,
4639 get_vim_var_name(iptr->isn_arg.number));
4640 break;
4641 case ISN_STOREAUTO:
4642 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4643 break;
4644 case ISN_STOREG:
4645 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4646 break;
4647 case ISN_STOREB:
4648 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4649 break;
4650 case ISN_STOREW:
4651 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4652 break;
4653 case ISN_STORET:
4654 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4655 break;
4656 case ISN_STORES:
4657 {
4658 scriptitem_T *si = SCRIPT_ITEM(
4659 iptr->isn_arg.loadstore.ls_sid);
4660
4661 smsg("%s%4d STORES %s in %s", pfx, current,
4662 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4663 }
4664 break;
4665 case ISN_STORESCRIPT:
4666 {
4667 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4668 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4669 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4670 + sref->sref_idx;
4671
4672 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4673 sv->sv_name,
4674 sref->sref_idx,
4675 si->sn_name);
4676 }
4677 break;
4678 case ISN_STOREOPT:
4679 smsg("%s%4d STOREOPT &%s", pfx, current,
4680 iptr->isn_arg.storeopt.so_name);
4681 break;
4682 case ISN_STOREENV:
4683 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4684 break;
4685 case ISN_STOREREG:
4686 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4687 break;
4688 case ISN_STORENR:
4689 smsg("%s%4d STORE %lld in $%d", pfx, current,
4690 iptr->isn_arg.storenr.stnr_val,
4691 iptr->isn_arg.storenr.stnr_idx);
4692 break;
4693
4694 case ISN_STOREINDEX:
4695 smsg("%s%4d STOREINDEX %s", pfx, current,
4696 vartype_name(iptr->isn_arg.vartype));
4697 break;
4698
4699 case ISN_STORERANGE:
4700 smsg("%s%4d STORERANGE", pfx, current);
4701 break;
4702
4703 // constants
4704 case ISN_PUSHNR:
4705 smsg("%s%4d PUSHNR %lld", pfx, current,
4706 (varnumber_T)(iptr->isn_arg.number));
4707 break;
4708 case ISN_PUSHBOOL:
4709 case ISN_PUSHSPEC:
4710 smsg("%s%4d PUSH %s", pfx, current,
4711 get_var_special_name(iptr->isn_arg.number));
4712 break;
4713 case ISN_PUSHF:
4714#ifdef FEAT_FLOAT
4715 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4716#endif
4717 break;
4718 case ISN_PUSHS:
4719 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4720 break;
4721 case ISN_PUSHBLOB:
4722 {
4723 char_u *r;
4724 char_u numbuf[NUMBUFLEN];
4725 char_u *tofree;
4726
4727 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4728 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4729 vim_free(tofree);
4730 }
4731 break;
4732 case ISN_PUSHFUNC:
4733 {
4734 char *name = (char *)iptr->isn_arg.string;
4735
4736 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4737 name == NULL ? "[none]" : name);
4738 }
4739 break;
4740 case ISN_PUSHCHANNEL:
4741#ifdef FEAT_JOB_CHANNEL
4742 {
4743 channel_T *channel = iptr->isn_arg.channel;
4744
4745 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4746 channel == NULL ? 0 : channel->ch_id);
4747 }
4748#endif
4749 break;
4750 case ISN_PUSHJOB:
4751#ifdef FEAT_JOB_CHANNEL
4752 {
4753 typval_T tv;
4754 char_u *name;
4755
4756 tv.v_type = VAR_JOB;
4757 tv.vval.v_job = iptr->isn_arg.job;
4758 name = tv_get_string(&tv);
4759 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4760 }
4761#endif
4762 break;
4763 case ISN_PUSHEXC:
4764 smsg("%s%4d PUSH v:exception", pfx, current);
4765 break;
4766 case ISN_UNLET:
4767 smsg("%s%4d UNLET%s %s", pfx, current,
4768 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4769 iptr->isn_arg.unlet.ul_name);
4770 break;
4771 case ISN_UNLETENV:
4772 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4773 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4774 iptr->isn_arg.unlet.ul_name);
4775 break;
4776 case ISN_UNLETINDEX:
4777 smsg("%s%4d UNLETINDEX", pfx, current);
4778 break;
4779 case ISN_UNLETRANGE:
4780 smsg("%s%4d UNLETRANGE", pfx, current);
4781 break;
4782 case ISN_LOCKCONST:
4783 smsg("%s%4d LOCKCONST", pfx, current);
4784 break;
4785 case ISN_NEWLIST:
4786 smsg("%s%4d NEWLIST size %lld", pfx, current,
4787 (varnumber_T)(iptr->isn_arg.number));
4788 break;
4789 case ISN_NEWDICT:
4790 smsg("%s%4d NEWDICT size %lld", pfx, current,
4791 (varnumber_T)(iptr->isn_arg.number));
4792 break;
4793
4794 // function call
4795 case ISN_BCALL:
4796 {
4797 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4798
4799 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4800 internal_func_name(cbfunc->cbf_idx),
4801 cbfunc->cbf_argcount);
4802 }
4803 break;
4804 case ISN_DCALL:
4805 {
4806 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4807 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4808 + cdfunc->cdf_idx;
4809
4810 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
4811 df->df_ufunc->uf_name_exp != NULL
4812 ? df->df_ufunc->uf_name_exp
4813 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4814 }
4815 break;
4816 case ISN_UCALL:
4817 {
4818 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4819
4820 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
4821 cufunc->cuf_name, cufunc->cuf_argcount);
4822 }
4823 break;
4824 case ISN_PCALL:
4825 {
4826 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4827
4828 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
4829 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4830 }
4831 break;
4832 case ISN_PCALL_END:
4833 smsg("%s%4d PCALL end", pfx, current);
4834 break;
4835 case ISN_RETURN:
4836 smsg("%s%4d RETURN", pfx, current);
4837 break;
4838 case ISN_RETURN_ZERO:
4839 smsg("%s%4d RETURN 0", pfx, current);
4840 break;
4841 case ISN_FUNCREF:
4842 {
4843 funcref_T *funcref = &iptr->isn_arg.funcref;
4844 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4845 + funcref->fr_func;
4846
4847 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
4848 }
4849 break;
4850
4851 case ISN_NEWFUNC:
4852 {
4853 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4854
4855 smsg("%s%4d NEWFUNC %s %s", pfx, current,
4856 newfunc->nf_lambda, newfunc->nf_global);
4857 }
4858 break;
4859
4860 case ISN_DEF:
4861 {
4862 char_u *name = iptr->isn_arg.string;
4863
4864 smsg("%s%4d DEF %s", pfx, current,
4865 name == NULL ? (char_u *)"" : name);
4866 }
4867 break;
4868
4869 case ISN_JUMP:
4870 {
4871 char *when = "?";
4872
4873 switch (iptr->isn_arg.jump.jump_when)
4874 {
4875 case JUMP_ALWAYS:
4876 when = "JUMP";
4877 break;
4878 case JUMP_AND_KEEP_IF_TRUE:
4879 when = "JUMP_AND_KEEP_IF_TRUE";
4880 break;
4881 case JUMP_IF_FALSE:
4882 when = "JUMP_IF_FALSE";
4883 break;
4884 case JUMP_AND_KEEP_IF_FALSE:
4885 when = "JUMP_AND_KEEP_IF_FALSE";
4886 break;
4887 case JUMP_IF_COND_FALSE:
4888 when = "JUMP_IF_COND_FALSE";
4889 break;
4890 case JUMP_IF_COND_TRUE:
4891 when = "JUMP_IF_COND_TRUE";
4892 break;
4893 }
4894 smsg("%s%4d %s -> %d", pfx, current, when,
4895 iptr->isn_arg.jump.jump_where);
4896 }
4897 break;
4898
4899 case ISN_JUMP_IF_ARG_SET:
4900 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
4901 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4902 iptr->isn_arg.jump.jump_where);
4903 break;
4904
4905 case ISN_FOR:
4906 {
4907 forloop_T *forloop = &iptr->isn_arg.forloop;
4908
4909 smsg("%s%4d FOR $%d -> %d", pfx, current,
4910 forloop->for_idx, forloop->for_end);
4911 }
4912 break;
4913
4914 case ISN_TRY:
4915 {
4916 try_T *try = &iptr->isn_arg.try;
4917
4918 if (try->try_ref->try_finally == 0)
4919 smsg("%s%4d TRY catch -> %d, endtry -> %d",
4920 pfx, current,
4921 try->try_ref->try_catch,
4922 try->try_ref->try_endtry);
4923 else
4924 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4925 pfx, current,
4926 try->try_ref->try_catch,
4927 try->try_ref->try_finally,
4928 try->try_ref->try_endtry);
4929 }
4930 break;
4931 case ISN_CATCH:
4932 // TODO
4933 smsg("%s%4d CATCH", pfx, current);
4934 break;
4935 case ISN_TRYCONT:
4936 {
4937 trycont_T *trycont = &iptr->isn_arg.trycont;
4938
4939 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
4940 trycont->tct_levels,
4941 trycont->tct_levels == 1 ? "" : "s",
4942 trycont->tct_where);
4943 }
4944 break;
4945 case ISN_FINALLY:
4946 smsg("%s%4d FINALLY", pfx, current);
4947 break;
4948 case ISN_ENDTRY:
4949 smsg("%s%4d ENDTRY", pfx, current);
4950 break;
4951 case ISN_THROW:
4952 smsg("%s%4d THROW", pfx, current);
4953 break;
4954
4955 // expression operations on number
4956 case ISN_OPNR:
4957 case ISN_OPFLOAT:
4958 case ISN_OPANY:
4959 {
4960 char *what;
4961 char *ins;
4962
4963 switch (iptr->isn_arg.op.op_type)
4964 {
4965 case EXPR_MULT: what = "*"; break;
4966 case EXPR_DIV: what = "/"; break;
4967 case EXPR_REM: what = "%"; break;
4968 case EXPR_SUB: what = "-"; break;
4969 case EXPR_ADD: what = "+"; break;
4970 default: what = "???"; break;
4971 }
4972 switch (iptr->isn_type)
4973 {
4974 case ISN_OPNR: ins = "OPNR"; break;
4975 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4976 case ISN_OPANY: ins = "OPANY"; break;
4977 default: ins = "???"; break;
4978 }
4979 smsg("%s%4d %s %s", pfx, current, ins, what);
4980 }
4981 break;
4982
4983 case ISN_COMPAREBOOL:
4984 case ISN_COMPARESPECIAL:
4985 case ISN_COMPARENR:
4986 case ISN_COMPAREFLOAT:
4987 case ISN_COMPARESTRING:
4988 case ISN_COMPAREBLOB:
4989 case ISN_COMPARELIST:
4990 case ISN_COMPAREDICT:
4991 case ISN_COMPAREFUNC:
4992 case ISN_COMPAREANY:
4993 {
4994 char *p;
4995 char buf[10];
4996 char *type;
4997
4998 switch (iptr->isn_arg.op.op_type)
4999 {
5000 case EXPR_EQUAL: p = "=="; break;
5001 case EXPR_NEQUAL: p = "!="; break;
5002 case EXPR_GREATER: p = ">"; break;
5003 case EXPR_GEQUAL: p = ">="; break;
5004 case EXPR_SMALLER: p = "<"; break;
5005 case EXPR_SEQUAL: p = "<="; break;
5006 case EXPR_MATCH: p = "=~"; break;
5007 case EXPR_IS: p = "is"; break;
5008 case EXPR_ISNOT: p = "isnot"; break;
5009 case EXPR_NOMATCH: p = "!~"; break;
5010 default: p = "???"; break;
5011 }
5012 STRCPY(buf, p);
5013 if (iptr->isn_arg.op.op_ic == TRUE)
5014 strcat(buf, "?");
5015 switch(iptr->isn_type)
5016 {
5017 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5018 case ISN_COMPARESPECIAL:
5019 type = "COMPARESPECIAL"; break;
5020 case ISN_COMPARENR: type = "COMPARENR"; break;
5021 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5022 case ISN_COMPARESTRING:
5023 type = "COMPARESTRING"; break;
5024 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5025 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5026 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5027 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5028 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5029 default: type = "???"; break;
5030 }
5031
5032 smsg("%s%4d %s %s", pfx, current, type, buf);
5033 }
5034 break;
5035
5036 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5037 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5038
5039 // expression operations
5040 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5041 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5042 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5043 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5044 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5045 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5046 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5047 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5048 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5049 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5050 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5051 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5052 pfx, current, iptr->isn_arg.number); break;
5053 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5054 pfx, current, iptr->isn_arg.number); break;
5055 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5056 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5057 iptr->isn_arg.string); break;
5058 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5059
5060 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5061 case ISN_CHECKTYPE:
5062 {
5063 checktype_T *ct = &iptr->isn_arg.type;
5064 char *tofree;
5065
5066 if (ct->ct_arg_idx == 0)
5067 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5068 type_name(ct->ct_type, &tofree),
5069 (int)ct->ct_off);
5070 else
5071 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5072 type_name(ct->ct_type, &tofree),
5073 (int)ct->ct_off,
5074 (int)ct->ct_arg_idx);
5075 vim_free(tofree);
5076 break;
5077 }
5078 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5079 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5080 iptr->isn_arg.checklen.cl_min_len);
5081 break;
5082 case ISN_SETTYPE:
5083 {
5084 char *tofree;
5085
5086 smsg("%s%4d SETTYPE %s", pfx, current,
5087 type_name(iptr->isn_arg.type.ct_type, &tofree));
5088 vim_free(tofree);
5089 break;
5090 }
5091 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
5092 case ISN_2BOOL: if (iptr->isn_arg.number)
5093 smsg("%s%4d INVERT (!val)", pfx, current);
5094 else
5095 smsg("%s%4d 2BOOL (!!val)", pfx, current);
5096 break;
5097 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
5098 (varnumber_T)(iptr->isn_arg.number));
5099 break;
5100 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]", pfx, current,
5101 (varnumber_T)(iptr->isn_arg.number));
5102 break;
5103 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current, iptr->isn_arg.string);
5104 break;
5105 case ISN_PUT:
5106 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5107 smsg("%s%4d PUT %c above range",
5108 pfx, current, iptr->isn_arg.put.put_regname);
5109 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5110 smsg("%s%4d PUT %c range",
5111 pfx, current, iptr->isn_arg.put.put_regname);
5112 else
5113 smsg("%s%4d PUT %c %ld", pfx, current,
5114 iptr->isn_arg.put.put_regname,
5115 (long)iptr->isn_arg.put.put_lnum);
5116 break;
5117
5118 // TODO: summarize modifiers
5119 case ISN_CMDMOD:
5120 {
5121 char_u *buf;
5122 size_t len = produce_cmdmods(
5123 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5124
5125 buf = alloc(len + 1);
5126 if (buf != NULL)
5127 {
5128 (void)produce_cmdmods(
5129 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5130 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5131 vim_free(buf);
5132 }
5133 break;
5134 }
5135 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5136
5137 case ISN_PROF_START:
5138 smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum);
5139 break;
5140
5141 case ISN_PROF_END:
5142 smsg("%s%4d PROFILE END", pfx, current);
5143 break;
5144
5145 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5146 iptr->isn_arg.unpack.unp_count,
5147 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5148 break;
5149 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5150 iptr->isn_arg.shuffle.shfl_item,
5151 iptr->isn_arg.shuffle.shfl_up);
5152 break;
5153 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5154
5155 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5156 return;
5157 }
5158
5159 out_flush(); // output one line at a time
5160 ui_breakcheck();
5161 if (got_int)
5162 break;
5163 }
5164}
5165
5166/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005167 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005168 * We don't really need this at runtime, but we do have tests that require it,
5169 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 */
5171 void
5172ex_disassemble(exarg_T *eap)
5173{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005174 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005175 char_u *fname;
5176 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 dfunc_T *dfunc;
5178 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005179 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005180 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005182 if (STRNCMP(arg, "<lambda>", 8) == 0)
5183 {
5184 arg += 8;
5185 (void)getdigits(&arg);
5186 fname = vim_strnsave(eap->arg, arg - eap->arg);
5187 }
5188 else
5189 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005190 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005191 if (fname == NULL)
5192 {
5193 semsg(_(e_invarg2), eap->arg);
5194 return;
5195 }
5196
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005197 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005198 if (ufunc == NULL)
5199 {
5200 char_u *p = untrans_function_name(fname);
5201
5202 if (p != NULL)
5203 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005204 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005205 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005206 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 if (ufunc == NULL)
5208 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005209 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 return;
5211 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005212 if (func_needs_compiling(ufunc, eap->forceit)
5213 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005214 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005215 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005217 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 return;
5219 }
5220 if (ufunc->uf_name_exp != NULL)
5221 msg((char *)ufunc->uf_name_exp);
5222 else
5223 msg((char *)ufunc->uf_name);
5224
5225 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005226#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005227 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
5228 instr_count = eap->forceit ? dfunc->df_instr_prof_count
5229 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005230#else
5231 instr = dfunc->df_instr;
5232 instr_count = dfunc->df_instr_count;
5233#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234
Bram Moolenaar4c137212021-04-19 16:48:48 +02005235 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236}
5237
5238/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005239 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 * list, etc. Mostly like what JavaScript does, except that empty list and
5241 * empty dictionary are FALSE.
5242 */
5243 int
5244tv2bool(typval_T *tv)
5245{
5246 switch (tv->v_type)
5247 {
5248 case VAR_NUMBER:
5249 return tv->vval.v_number != 0;
5250 case VAR_FLOAT:
5251#ifdef FEAT_FLOAT
5252 return tv->vval.v_float != 0.0;
5253#else
5254 break;
5255#endif
5256 case VAR_PARTIAL:
5257 return tv->vval.v_partial != NULL;
5258 case VAR_FUNC:
5259 case VAR_STRING:
5260 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5261 case VAR_LIST:
5262 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5263 case VAR_DICT:
5264 return tv->vval.v_dict != NULL
5265 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5266 case VAR_BOOL:
5267 case VAR_SPECIAL:
5268 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5269 case VAR_JOB:
5270#ifdef FEAT_JOB_CHANNEL
5271 return tv->vval.v_job != NULL;
5272#else
5273 break;
5274#endif
5275 case VAR_CHANNEL:
5276#ifdef FEAT_JOB_CHANNEL
5277 return tv->vval.v_channel != NULL;
5278#else
5279 break;
5280#endif
5281 case VAR_BLOB:
5282 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5283 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005284 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005286 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005287 break;
5288 }
5289 return FALSE;
5290}
5291
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005292 void
5293emsg_using_string_as(typval_T *tv, int as_number)
5294{
5295 semsg(_(as_number ? e_using_string_as_number_str
5296 : e_using_string_as_bool_str),
5297 tv->vval.v_string == NULL
5298 ? (char_u *)"" : tv->vval.v_string);
5299}
5300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301/*
5302 * If "tv" is a string give an error and return FAIL.
5303 */
5304 int
5305check_not_string(typval_T *tv)
5306{
5307 if (tv->v_type == VAR_STRING)
5308 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005309 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 clear_tv(tv);
5311 return FAIL;
5312 }
5313 return OK;
5314}
5315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316
5317#endif // FEAT_EVAL