blob: 9b7f87b6c13aade84b98fee9e25fb248eeb83ed9 [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
37
38// A stack is used to store:
39// - arguments passed to a :def function
40// - info about the calling function, to use when returning
41// - local variables
42// - temporary values
43//
44// In detail (FP == Frame Pointer):
45// arg1 first argument from caller (if present)
46// arg2 second argument from caller (if present)
47// extra_arg1 any missing optional argument default value
48// FP -> cur_func calling function
49// current previous instruction pointer
50// frame_ptr previous Frame Pointer
51// var1 space for local variable
52// var2 space for local variable
53// .... fixed space for max. number of local variables
54// temp temporary values
55// .... flexible space for temporary values (can grow big)
56
57/*
58 * Execution context.
59 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010060struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020062 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063
Bram Moolenaar0186e582021-01-10 18:33:11 +010064 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 garray_T ec_trystack; // stack of trycmd_T values
67 int ec_in_catch; // when TRUE in catch or finally block
68
69 int ec_dfunc_idx; // current function index
70 isn_T *ec_instr; // array with instructions
71 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020072
73 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010074};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
Bram Moolenaar12d26532021-02-19 19:13:21 +010076#ifdef FEAT_PROFILE
77// stack of profinfo_T used when profiling.
78static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
79#endif
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020082#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 +010083
Bram Moolenaar418f1df2020-08-12 21:34:49 +020084 void
85to_string_error(vartype_T vartype)
86{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020087 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020088}
89
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 */
93 static int
94ufunc_argcount(ufunc_T *ufunc)
95{
96 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
97}
98
99/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200100 * Create a new list from "count" items at the bottom of the stack.
101 * When "count" is zero an empty list is added to the stack.
102 */
103 static int
104exe_newlist(int count, ectx_T *ectx)
105{
106 list_T *list = list_alloc_with_items(count);
107 int idx;
108 typval_T *tv;
109
110 if (list == NULL)
111 return FAIL;
112 for (idx = 0; idx < count; ++idx)
113 list_set_item(list, idx, STACK_TV_BOT(idx - count));
114
115 if (count > 0)
116 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200117 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200118 return FAIL;
119 else
120 ++ectx->ec_stack.ga_len;
121 tv = STACK_TV_BOT(-1);
122 tv->v_type = VAR_LIST;
123 tv->vval.v_list = list;
124 ++list->lv_refcount;
125 return OK;
126}
127
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100128// Data local to a function.
129// On a function call, if not empty, is saved on the stack and restored when
130// returning.
131typedef struct {
132 int floc_restore_cmdmod;
133 cmdmod_T floc_save_cmdmod;
134 int floc_restore_cmdmod_stacklen;
135} funclocal_T;
136
Bram Moolenaarfe270812020-04-11 22:31:27 +0200137/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100139 * This adds a stack frame and sets the instruction pointer to the start of the
140 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100141 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 *
143 * Stack has:
144 * - current arguments (already there)
145 * - omitted optional argument (default values) added here
146 * - stack frame:
147 * - pointer to calling function
148 * - Index of next instruction in calling function
149 * - previous frame pointer
150 * - reserved space for local variables
151 */
152 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100153call_dfunc(
154 int cdf_idx,
155 partial_T *pt,
156 int argcount_arg,
157 funclocal_T *funclocal,
158 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100160 int argcount = argcount_arg;
161 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
162 ufunc_T *ufunc = dfunc->df_ufunc;
163 int arg_to_add;
164 int vararg_count = 0;
165 int varcount;
166 int idx;
167 estack_T *entry;
168 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169
170 if (dfunc->df_deleted)
171 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100172 // don't use ufunc->uf_name, it may have been freed
173 emsg_funcname(e_func_deleted,
174 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175 return FAIL;
176 }
177
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100178#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100179 if (do_profiling == PROF_YES)
180 {
181 if (ga_grow(&profile_info_ga, 1) == OK)
182 {
183 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
184 + profile_info_ga.ga_len;
185 ++profile_info_ga.ga_len;
186 CLEAR_POINTER(info);
187 profile_may_start_func(info, ufunc,
188 (((dfunc_T *)def_functions.ga_data)
189 + ectx->ec_dfunc_idx)->df_ufunc);
190 }
191
192 // Profiling might be enabled/disabled along the way. This should not
193 // fail, since the function was compiled before and toggling profiling
194 // doesn't change any errors.
195 if (func_needs_compiling(ufunc, PROFILING(ufunc))
196 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100197 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100198 return FAIL;
199 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100200#endif
201
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200202 if (ufunc->uf_va_name != NULL)
203 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200204 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200205 // Stack at time of call with 2 varargs:
206 // normal_arg
207 // optional_arg
208 // vararg_1
209 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200210 // After creating the list:
211 // normal_arg
212 // optional_arg
213 // vararg-list
214 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 // After creating the list
217 // normal_arg
218 // (space for optional_arg)
219 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200220 vararg_count = argcount - ufunc->uf_args.ga_len;
221 if (vararg_count < 0)
222 vararg_count = 0;
223 else
224 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200225 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200226 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200227
228 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200229 }
230
Bram Moolenaarfe270812020-04-11 22:31:27 +0200231 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200232 if (arg_to_add < 0)
233 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200234 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200235 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200236 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200237 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200238 return FAIL;
239 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200240
241 // Reserve space for:
242 // - missing arguments
243 // - stack frame
244 // - local variables
245 // - if needed: a counter for number of closures created in
246 // ectx->ec_funcrefs.
247 varcount = dfunc->df_varcount + dfunc->df_has_closure;
248 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
249 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250 return FAIL;
251
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100252 // If depth of calling is getting too high, don't execute the function.
253 if (funcdepth_increment() == FAIL)
254 return FAIL;
255
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100256 // Only make a copy of funclocal if it contains something to restore.
257 if (funclocal->floc_restore_cmdmod)
258 {
259 floc = ALLOC_ONE(funclocal_T);
260 if (floc == NULL)
261 return FAIL;
262 *floc = *funclocal;
263 funclocal->floc_restore_cmdmod = FALSE;
264 }
265
Bram Moolenaarfe270812020-04-11 22:31:27 +0200266 // Move the vararg-list to below the missing optional arguments.
267 if (vararg_count > 0 && arg_to_add > 0)
268 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100269
270 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200271 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200273 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
275 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100276 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
277 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100278 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100279 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200281 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282
283 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200284 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200286 if (dfunc->df_has_closure)
287 {
288 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
289
290 tv->v_type = VAR_NUMBER;
291 tv->vval.v_number = 0;
292 }
293 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100295 if (pt != NULL || ufunc->uf_partial != NULL
296 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100297 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100298 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
299
300 if (outer == NULL)
301 return FAIL;
302 if (pt != NULL)
303 {
304 *outer = pt->pt_outer;
305 outer->out_up_is_copy = TRUE;
306 }
307 else if (ufunc->uf_partial != NULL)
308 {
309 *outer = ufunc->uf_partial->pt_outer;
310 outer->out_up_is_copy = TRUE;
311 }
312 else
313 {
314 outer->out_stack = &ectx->ec_stack;
315 outer->out_frame_idx = ectx->ec_frame_idx;
316 outer->out_up = ectx->ec_outer;
317 }
318 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100319 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100320 else
321 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100322
Bram Moolenaarc970e422021-03-17 15:03:04 +0100323 ++ufunc->uf_calls;
324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325 // Set execution state to the start of the called function.
326 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100327 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100328 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200329 if (entry != NULL)
330 {
331 // Set the script context to the script where the function was defined.
332 // TODO: save more than the SID?
333 entry->es_save_sid = current_sctx.sc_sid;
334 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
335 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100336
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200337 // Start execution at the first instruction.
338 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100339
340 return OK;
341}
342
343// Get pointer to item in the stack.
344#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
345
346/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200347 * Used when returning from a function: Check if any closure is still
348 * referenced. If so then move the arguments and variables to a separate piece
349 * of stack to be used when the closure is called.
350 * When "free_arguments" is TRUE the arguments are to be freed.
351 * Returns FAIL when out of memory.
352 */
353 static int
354handle_closure_in_use(ectx_T *ectx, int free_arguments)
355{
356 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
357 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200358 int argcount;
359 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200360 int idx;
361 typval_T *tv;
362 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200363 garray_T *gap = &ectx->ec_funcrefs;
364 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200365
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200366 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200367 return OK; // function was freed
368 if (dfunc->df_has_closure == 0)
369 return OK; // no closures
370 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
371 closure_count = tv->vval.v_number;
372 if (closure_count == 0)
373 return OK; // no funcrefs created
374
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200375 argcount = ufunc_argcount(dfunc->df_ufunc);
376 top = ectx->ec_frame_idx - argcount;
377
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200378 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200379 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200380 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200381 partial_T *pt;
382 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200383
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200384 if (off < 0)
385 continue; // count is off or already done
386 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200387 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200388 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200389 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200390 int i;
391
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200392 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200393 // unreferenced on return.
394 for (i = 0; i < dfunc->df_varcount; ++i)
395 {
396 typval_T *stv = STACK_TV(ectx->ec_frame_idx
397 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200398 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200399 --refcount;
400 }
401 if (refcount > 1)
402 {
403 closure_in_use = TRUE;
404 break;
405 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200406 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 }
408
409 if (closure_in_use)
410 {
411 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
412 typval_T *stack;
413
414 // A closure is using the arguments and/or local variables.
415 // Move them to the called function.
416 if (funcstack == NULL)
417 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200418 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
419 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200420 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
421 funcstack->fs_ga.ga_data = stack;
422 if (stack == NULL)
423 {
424 vim_free(funcstack);
425 return FAIL;
426 }
427
428 // Move or copy the arguments.
429 for (idx = 0; idx < argcount; ++idx)
430 {
431 tv = STACK_TV(top + idx);
432 if (free_arguments)
433 {
434 *(stack + idx) = *tv;
435 tv->v_type = VAR_UNKNOWN;
436 }
437 else
438 copy_tv(tv, stack + idx);
439 }
440 // Move the local variables.
441 for (idx = 0; idx < dfunc->df_varcount; ++idx)
442 {
443 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200444
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200445 // A partial created for a local function, that is also used as a
446 // local variable, has a reference count for the variable, thus
447 // will never go down to zero. When all these refcounts are one
448 // then the funcstack is unused. We need to count how many we have
449 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200450 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
451 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200452 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200453
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200454 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200455 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
456 gap->ga_len - closure_count + i])
457 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200458 }
459
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200460 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200461 tv->v_type = VAR_UNKNOWN;
462 }
463
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200464 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200465 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200466 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
467 - closure_count + idx];
468 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200469 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200470 ++funcstack->fs_refcount;
471 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100472 pt->pt_outer.out_stack = &funcstack->fs_ga;
473 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
474 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200475 }
476 }
477 }
478
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200479 for (idx = 0; idx < closure_count; ++idx)
480 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
481 - closure_count + idx]);
482 gap->ga_len -= closure_count;
483 if (gap->ga_len == 0)
484 ga_clear(gap);
485
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200486 return OK;
487}
488
489/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200490 * Called when a partial is freed or its reference count goes down to one. The
491 * funcstack may be the only reference to the partials in the local variables.
492 * Go over all of them, the funcref and can be freed if all partials
493 * referencing the funcstack have a reference count of one.
494 */
495 void
496funcstack_check_refcount(funcstack_T *funcstack)
497{
498 int i;
499 garray_T *gap = &funcstack->fs_ga;
500 int done = 0;
501
502 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
503 return;
504 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
505 {
506 typval_T *tv = ((typval_T *)gap->ga_data) + i;
507
508 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
509 && tv->vval.v_partial->pt_funcstack == funcstack
510 && tv->vval.v_partial->pt_refcount == 1)
511 ++done;
512 }
513 if (done == funcstack->fs_min_refcount)
514 {
515 typval_T *stack = gap->ga_data;
516
517 // All partials referencing the funcstack have a reference count of
518 // one, thus the funcstack is no longer of use.
519 for (i = 0; i < gap->ga_len; ++i)
520 clear_tv(stack + i);
521 vim_free(stack);
522 vim_free(funcstack);
523 }
524}
525
526/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 * Return from the current function.
528 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200529 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100530func_return(funclocal_T *funclocal, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100533 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200534 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
535 + ectx->ec_dfunc_idx;
536 int argcount = ufunc_argcount(dfunc->df_ufunc);
537 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200538 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100539 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
540 + STACK_FRAME_FUNC_OFF)->vval.v_number;
541 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
542 + prev_dfunc_idx;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100543 funclocal_T *floc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544
Bram Moolenaar12d26532021-02-19 19:13:21 +0100545#ifdef FEAT_PROFILE
546 if (do_profiling == PROF_YES)
547 {
548 ufunc_T *caller = prev_dfunc->df_ufunc;
549
550 if (dfunc->df_ufunc->uf_profiling
551 || (caller != NULL && caller->uf_profiling))
552 {
553 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
554 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
555 --profile_info_ga.ga_len;
556 }
557 }
558#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100559 // TODO: when is it safe to delete the function when it is no longer used?
560 --dfunc->df_ufunc->uf_calls;
561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200563 entry = estack_pop();
564 if (entry != NULL)
565 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200567 if (handle_closure_in_use(ectx, TRUE) == FAIL)
568 return FAIL;
569
570 // Clear the arguments.
571 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
572 clear_tv(STACK_TV(idx));
573
574 // Clear local variables and temp values, but not the return value.
575 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100576 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100578
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100579 // The return value should be on top of the stack. However, when aborting
580 // it may not be there and ec_frame_idx is the top of the stack.
581 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100582 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100583 ret_idx = 0;
584
Bram Moolenaar0186e582021-01-10 18:33:11 +0100585 vim_free(ectx->ec_outer);
586
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100587 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100588 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100589 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
590 + STACK_FRAME_IIDX_OFF)->vval.v_number;
591 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
592 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100593 floc = (void *)STACK_TV(ectx->ec_frame_idx
594 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200595 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
597 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100598 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100599
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100600 if (floc == NULL)
601 funclocal->floc_restore_cmdmod = FALSE;
602 else
603 {
604 *funclocal = *floc;
605 vim_free(floc);
606 }
607
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100608 if (ret_idx > 0)
609 {
610 // Reset the stack to the position before the call, with a spot for the
611 // return value, moved there from above the frame.
612 ectx->ec_stack.ga_len = top + 1;
613 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
614 }
615 else
616 // Reset the stack to the position before the call.
617 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200618
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100619 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200620 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621}
622
623#undef STACK_TV
624
625/*
626 * Prepare arguments and rettv for calling a builtin or user function.
627 */
628 static int
629call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
630{
631 int idx;
632 typval_T *tv;
633
634 // Move arguments from bottom of the stack to argvars[] and add terminator.
635 for (idx = 0; idx < argcount; ++idx)
636 argvars[idx] = *STACK_TV_BOT(idx - argcount);
637 argvars[argcount].v_type = VAR_UNKNOWN;
638
639 // Result replaces the arguments on the stack.
640 if (argcount > 0)
641 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200642 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 return FAIL;
644 else
645 ++ectx->ec_stack.ga_len;
646
647 // Default return value is zero.
648 tv = STACK_TV_BOT(-1);
649 tv->v_type = VAR_NUMBER;
650 tv->vval.v_number = 0;
651
652 return OK;
653}
654
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200655// Ugly global to avoid passing the execution context around through many
656// layers.
657static ectx_T *current_ectx = NULL;
658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659/*
660 * Call a builtin function by index.
661 */
662 static int
663call_bfunc(int func_idx, int argcount, ectx_T *ectx)
664{
665 typval_T argvars[MAX_FUNC_ARGS];
666 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100667 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200668 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669
670 if (call_prepare(argcount, argvars, ectx) == FAIL)
671 return FAIL;
672
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200673 // Call the builtin function. Set "current_ectx" so that when it
674 // recursively invokes call_def_function() a closure context can be set.
675 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200677 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678
679 // Clear the arguments.
680 for (idx = 0; idx < argcount; ++idx)
681 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200682
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100683 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200684 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 return OK;
686}
687
688/*
689 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100690 * If the function is compiled this will add a stack frame and set the
691 * instruction pointer at the start of the function.
692 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100693 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100694 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 */
696 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100697call_ufunc(
698 ufunc_T *ufunc,
699 partial_T *pt,
700 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100701 funclocal_T *funclocal,
Bram Moolenaar0186e582021-01-10 18:33:11 +0100702 ectx_T *ectx,
703 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704{
705 typval_T argvars[MAX_FUNC_ARGS];
706 funcexe_T funcexe;
707 int error;
708 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100709 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100710#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100711 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100712#else
713# define profiling FALSE
714#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715
Bram Moolenaarb2049902021-01-24 12:53:53 +0100716 if (func_needs_compiling(ufunc, profiling)
717 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200718 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200719 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100720 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100721 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100722 if (error != FCERR_UNKNOWN)
723 {
724 if (error == FCERR_TOOMANY)
725 semsg(_(e_toomanyarg), ufunc->uf_name);
726 else
727 semsg(_(e_toofewarg), ufunc->uf_name);
728 return FAIL;
729 }
730
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100731 // The function has been compiled, can call it quickly. For a function
732 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100733 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100734 if (iptr != NULL)
735 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100736 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100737 iptr->isn_type = ISN_DCALL;
738 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
739 iptr->isn_arg.dfunc.cdf_argcount = argcount;
740 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100741 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, funclocal, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 if (call_prepare(argcount, argvars, ectx) == FAIL)
745 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200746 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 funcexe.evaluate = TRUE;
748
749 // Call the user function. Result goes in last position on the stack.
750 // TODO: add selfdict if there is one
751 error = call_user_func_check(ufunc, argcount, argvars,
752 STACK_TV_BOT(-1), &funcexe, NULL);
753
754 // Clear the arguments.
755 for (idx = 0; idx < argcount; ++idx)
756 clear_tv(&argvars[idx]);
757
758 if (error != FCERR_NONE)
759 {
760 user_func_error(error, ufunc->uf_name);
761 return FAIL;
762 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100763 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200764 // Error other than from calling the function itself.
765 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 return OK;
767}
768
769/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100770 * If command modifiers were applied restore them.
771 */
772 static void
773may_restore_cmdmod(funclocal_T *funclocal)
774{
775 if (funclocal->floc_restore_cmdmod)
776 {
777 cmdmod.cmod_filter_regmatch.regprog = NULL;
778 undo_cmdmod(&cmdmod);
779 cmdmod = funclocal->floc_save_cmdmod;
780 funclocal->floc_restore_cmdmod = FALSE;
781 }
782}
783
784/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200785 * Return TRUE if an error was given or CTRL-C was pressed.
786 */
787 static int
788vim9_aborting(int prev_called_emsg)
789{
790 return called_emsg > prev_called_emsg || got_int || did_throw;
791}
792
793/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 * Execute a function by "name".
795 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100796 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 * Returns FAIL if not found without an error message.
798 */
799 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100800call_by_name(
801 char_u *name,
802 int argcount,
803 funclocal_T *funclocal,
804 ectx_T *ectx,
805 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806{
807 ufunc_T *ufunc;
808
809 if (builtin_function(name, -1))
810 {
811 int func_idx = find_internal_func(name);
812
813 if (func_idx < 0)
814 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200815 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816 return FAIL;
817 return call_bfunc(func_idx, argcount, ectx);
818 }
819
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200820 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200821
822 if (ufunc == NULL)
823 {
824 int called_emsg_before = called_emsg;
825
826 if (script_autoload(name, TRUE))
827 // loaded a package, search for the function again
828 ufunc = find_func(name, FALSE, NULL);
829 if (vim9_aborting(called_emsg_before))
830 return FAIL; // bail out if loading the script caused an error
831 }
832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100834 {
835 if (ufunc->uf_arg_types != NULL)
836 {
837 int i;
838 typval_T *argv = STACK_TV_BOT(0) - argcount;
839
840 // The function can change at runtime, check that the argument
841 // types are correct.
842 for (i = 0; i < argcount; ++i)
843 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100844 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100845
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100846 if (i < ufunc->uf_args.ga_len)
847 type = ufunc->uf_arg_types[i];
848 else if (ufunc->uf_va_type != NULL)
849 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100850 if (type != NULL && check_typval_arg_type(type,
851 &argv[i], i + 1) == FAIL)
852 return FAIL;
853 }
854 }
855
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100856 return call_ufunc(ufunc, NULL, argcount, funclocal, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858
859 return FAIL;
860}
861
862 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100863call_partial(
864 typval_T *tv,
865 int argcount_arg,
866 funclocal_T *funclocal,
867 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200869 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200870 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100872 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873
874 if (tv->v_type == VAR_PARTIAL)
875 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200876 partial_T *pt = tv->vval.v_partial;
877 int i;
878
879 if (pt->pt_argc > 0)
880 {
881 // Make space for arguments from the partial, shift the "argcount"
882 // arguments up.
883 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
884 return FAIL;
885 for (i = 1; i <= argcount; ++i)
886 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
887 ectx->ec_stack.ga_len += pt->pt_argc;
888 argcount += pt->pt_argc;
889
890 // copy the arguments from the partial onto the stack
891 for (i = 0; i < pt->pt_argc; ++i)
892 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894
895 if (pt->pt_func != NULL)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100896 return call_ufunc(pt->pt_func, pt, argcount, funclocal, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 name = pt->pt_name;
899 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200900 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200902 if (name != NULL)
903 {
904 char_u fname_buf[FLEN_FIXED + 1];
905 char_u *tofree = NULL;
906 int error = FCERR_NONE;
907 char_u *fname;
908
909 // May need to translate <SNR>123_ to K_SNR.
910 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
911 if (error != FCERR_NONE)
912 res = FAIL;
913 else
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100914 res = call_by_name(fname, argcount, funclocal, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200915 vim_free(tofree);
916 }
917
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100918 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919 {
920 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200921 semsg(_(e_unknownfunc),
922 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 return FAIL;
924 }
925 return OK;
926}
927
928/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200929 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
930 * TRUE.
931 */
932 static int
933error_if_locked(int lock, char *error)
934{
935 if (lock & (VAR_LOCKED | VAR_FIXED))
936 {
937 emsg(_(error));
938 return TRUE;
939 }
940 return FALSE;
941}
942
943/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100944 * Give an error if "tv" is not a number and return FAIL.
945 */
946 static int
947check_for_number(typval_T *tv)
948{
949 if (tv->v_type != VAR_NUMBER)
950 {
951 semsg(_(e_expected_str_but_got_str),
952 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
953 return FAIL;
954 }
955 return OK;
956}
957
958/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100959 * Store "tv" in variable "name".
960 * This is for s: and g: variables.
961 */
962 static void
963store_var(char_u *name, typval_T *tv)
964{
965 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200966 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100967
Bram Moolenaard877a572021-04-01 19:42:48 +0200968 if (tv->v_lock)
969 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100970 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +0200971 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100972 restore_funccal();
973}
974
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100975/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100976 * Convert "tv" to a string.
977 * Return FAIL if not allowed.
978 */
979 static int
980do_2string(typval_T *tv, int is_2string_any)
981{
982 if (tv->v_type != VAR_STRING)
983 {
984 char_u *str;
985
986 if (is_2string_any)
987 {
988 switch (tv->v_type)
989 {
990 case VAR_SPECIAL:
991 case VAR_BOOL:
992 case VAR_NUMBER:
993 case VAR_FLOAT:
994 case VAR_BLOB: break;
995 default: to_string_error(tv->v_type);
996 return FAIL;
997 }
998 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100999 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001000 clear_tv(tv);
1001 tv->v_type = VAR_STRING;
1002 tv->vval.v_string = str;
1003 }
1004 return OK;
1005}
1006
1007/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001008 * When the value of "sv" is a null list of dict, allocate it.
1009 */
1010 static void
1011allocate_if_null(typval_T *tv)
1012{
1013 switch (tv->v_type)
1014 {
1015 case VAR_LIST:
1016 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001017 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001018 break;
1019 case VAR_DICT:
1020 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001021 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001022 break;
1023 default:
1024 break;
1025 }
1026}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001027
Bram Moolenaare7525c52021-01-09 13:20:37 +01001028/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001029 * Return the character "str[index]" where "index" is the character index,
1030 * including composing characters.
1031 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001032 */
1033 char_u *
1034char_from_string(char_u *str, varnumber_T index)
1035{
1036 size_t nbyte = 0;
1037 varnumber_T nchar = index;
1038 size_t slen;
1039
1040 if (str == NULL)
1041 return NULL;
1042 slen = STRLEN(str);
1043
Bram Moolenaarff871402021-03-26 13:34:05 +01001044 // Do the same as for a list: a negative index counts from the end.
1045 // Optimization to check the first byte to be below 0x80 (and no composing
1046 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001047 if (index < 0)
1048 {
1049 int clen = 0;
1050
1051 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001052 {
1053 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1054 ++nbyte;
1055 else if (enc_utf8)
1056 nbyte += utfc_ptr2len(str + nbyte);
1057 else
1058 nbyte += mb_ptr2len(str + nbyte);
1059 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001060 nchar = clen + index;
1061 if (nchar < 0)
1062 // unlike list: index out of range results in empty string
1063 return NULL;
1064 }
1065
1066 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001067 {
1068 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1069 ++nbyte;
1070 else if (enc_utf8)
1071 nbyte += utfc_ptr2len(str + nbyte);
1072 else
1073 nbyte += mb_ptr2len(str + nbyte);
1074 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001075 if (nbyte >= slen)
1076 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001077 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001078}
1079
1080/*
1081 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001082 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001083 * If going over the end return "str_len".
1084 * If "idx" is negative count from the end, -1 is the last character.
1085 * When going over the start return -1.
1086 */
1087 static long
1088char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1089{
1090 varnumber_T nchar = idx;
1091 size_t nbyte = 0;
1092
1093 if (nchar >= 0)
1094 {
1095 while (nchar > 0 && nbyte < str_len)
1096 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001097 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001098 --nchar;
1099 }
1100 }
1101 else
1102 {
1103 nbyte = str_len;
1104 while (nchar < 0 && nbyte > 0)
1105 {
1106 --nbyte;
1107 nbyte -= mb_head_off(str, str + nbyte);
1108 ++nchar;
1109 }
1110 if (nchar < 0)
1111 return -1;
1112 }
1113 return (long)nbyte;
1114}
1115
1116/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001117 * Return the slice "str[first : last]" using character indexes. Composing
1118 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001119 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001120 * Return NULL when the result is empty.
1121 */
1122 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001123string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001124{
1125 long start_byte, end_byte;
1126 size_t slen;
1127
1128 if (str == NULL)
1129 return NULL;
1130 slen = STRLEN(str);
1131 start_byte = char_idx2byte(str, slen, first);
1132 if (start_byte < 0)
1133 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001134 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001135 end_byte = (long)slen;
1136 else
1137 {
1138 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001139 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001140 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001141 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001142 }
1143
1144 if (start_byte >= (long)slen || end_byte <= start_byte)
1145 return NULL;
1146 return vim_strnsave(str + start_byte, end_byte - start_byte);
1147}
1148
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001149 static svar_T *
1150get_script_svar(scriptref_T *sref, ectx_T *ectx)
1151{
1152 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1153 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1154 + ectx->ec_dfunc_idx;
1155 svar_T *sv;
1156
1157 if (sref->sref_seq != si->sn_script_seq)
1158 {
1159 // The script was reloaded after the function was
1160 // compiled, the script_idx may not be valid.
1161 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1162 dfunc->df_ufunc->uf_name_exp);
1163 return NULL;
1164 }
1165 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1166 if (!equal_type(sv->sv_type, sref->sref_type))
1167 {
1168 emsg(_(e_script_variable_type_changed));
1169 return NULL;
1170 }
1171 return sv;
1172}
1173
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001174/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 * Execute a function by "name".
1176 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001177 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 */
1179 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001180call_eval_func(
1181 char_u *name,
1182 int argcount,
1183 funclocal_T *funclocal,
1184 ectx_T *ectx,
1185 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186{
Bram Moolenaared677f52020-08-12 16:38:10 +02001187 int called_emsg_before = called_emsg;
1188 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001190 res = call_by_name(name, argcount, funclocal, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001191 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001193 dictitem_T *v;
1194
1195 v = find_var(name, NULL, FALSE);
1196 if (v == NULL)
1197 {
1198 semsg(_(e_unknownfunc), name);
1199 return FAIL;
1200 }
1201 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1202 {
1203 semsg(_(e_unknownfunc), name);
1204 return FAIL;
1205 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001206 return call_partial(&v->di_tv, argcount, funclocal, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001208 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209}
1210
1211/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001212 * When a function reference is used, fill a partial with the information
1213 * needed, especially when it is used as a closure.
1214 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001215 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001216fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1217{
1218 pt->pt_func = ufunc;
1219 pt->pt_refcount = 1;
1220
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001221 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001222 {
1223 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1224 + ectx->ec_dfunc_idx;
1225
1226 // The closure needs to find arguments and local
1227 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001228 pt->pt_outer.out_stack = &ectx->ec_stack;
1229 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1230 pt->pt_outer.out_up = ectx->ec_outer;
1231 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001232
1233 // If this function returns and the closure is still
1234 // being used, we need to make a copy of the context
1235 // (arguments and local variables). Store a reference
1236 // to the partial so we can handle that.
1237 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1238 {
1239 vim_free(pt);
1240 return FAIL;
1241 }
1242 // Extra variable keeps the count of closures created
1243 // in the current function call.
1244 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1245 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1246
1247 ((partial_T **)ectx->ec_funcrefs.ga_data)
1248 [ectx->ec_funcrefs.ga_len] = pt;
1249 ++pt->pt_refcount;
1250 ++ectx->ec_funcrefs.ga_len;
1251 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001252 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001253 return OK;
1254}
1255
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001256
Bram Moolenaarf112f302020-12-20 17:47:52 +01001257/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 * Call a "def" function from old Vim script.
1259 * Return OK or FAIL.
1260 */
1261 int
1262call_def_function(
1263 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001264 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001266 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 typval_T *rettv) // return value
1268{
1269 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001270 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001271 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 typval_T *tv;
1273 int idx;
1274 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001275 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001276 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001277 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001278 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001279 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001280 msglist_T **saved_msg_list = NULL;
1281 msglist_T *private_msg_list = NULL;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001282 funclocal_T funclocal;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001283 int save_emsg_silent_def = emsg_silent_def;
1284 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001285 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001286 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001287 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288
1289// Get pointer to item in the stack.
1290#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1291
1292// Get pointer to item at the bottom of the stack, -1 is the bottom.
1293#undef STACK_TV_BOT
1294#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1295
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001296// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001297#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 +01001298
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001299 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001300 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1301 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001302 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001303 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001304 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001305 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001306 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001307 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001308 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001309
Bram Moolenaar09689a02020-05-09 22:50:08 +02001310 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001311 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001312 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1313 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001314 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001315 {
1316 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001317 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001318 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001321 // If depth of calling is getting too high, don't execute the function.
1322 orig_funcdepth = funcdepth_get();
1323 if (funcdepth_increment() == FAIL)
1324 return FAIL;
1325
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001326 CLEAR_FIELD(funclocal);
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001327 CLEAR_FIELD(ectx);
1328 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1329 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1330 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001331 {
1332 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001333 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001336 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001338 // Put arguments on the stack, but no more than what the function expects.
1339 // A lambda can be called with more arguments than it uses.
1340 for (idx = 0; idx < argc
1341 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1342 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 {
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001344 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
1345 && argv[idx].v_type == VAR_SPECIAL
1346 && argv[idx].vval.v_number == VVAL_NONE)
1347 {
1348 // Use the default value.
1349 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1350 }
1351 else
1352 {
1353 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
1354 && check_typval_arg_type(
1355 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
1356 goto failed_early;
1357 copy_tv(&argv[idx], STACK_TV_BOT(0));
1358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 ++ectx.ec_stack.ga_len;
1360 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001361
1362 // Turn varargs into a list. Empty list if no args.
1363 if (ufunc->uf_va_name != NULL)
1364 {
1365 int vararg_count = argc - ufunc->uf_args.ga_len;
1366
1367 if (vararg_count < 0)
1368 vararg_count = 0;
1369 else
1370 argc -= vararg_count;
1371 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001372 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001373
1374 // Check the type of the list items.
1375 tv = STACK_TV_BOT(-1);
1376 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001377 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001378 && ufunc->uf_va_type->tt_member != &t_any
1379 && tv->vval.v_list != NULL)
1380 {
1381 type_T *expected = ufunc->uf_va_type->tt_member;
1382 listitem_T *li = tv->vval.v_list->lv_first;
1383
1384 for (idx = 0; idx < vararg_count; ++idx)
1385 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001386 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001387 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001388 goto failed_early;
1389 li = li->li_next;
1390 }
1391 }
1392
Bram Moolenaar23e03252020-04-12 22:22:31 +02001393 if (defcount > 0)
1394 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001395 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001396 --ectx.ec_stack.ga_len;
1397 }
1398
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001399 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001400 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001401 if (defcount > 0)
1402 for (idx = 0; idx < defcount; ++idx)
1403 {
1404 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1405 ++ectx.ec_stack.ga_len;
1406 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001407 if (ufunc->uf_va_name != NULL)
Bram Moolenaarc970e422021-03-17 15:03:04 +01001408 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409
1410 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001411 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1412 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001414 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001415 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1416 + ufunc->uf_dfunc_idx;
1417 ufunc_T *base_ufunc = dfunc->df_ufunc;
1418
1419 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1420 // by copy_func().
1421 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001422 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001423 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1424 if (ectx.ec_outer == NULL)
1425 goto failed_early;
1426 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001427 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001428 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1429 {
1430 if (current_ectx->ec_outer != NULL)
1431 *ectx.ec_outer = *current_ectx->ec_outer;
1432 }
1433 else
1434 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001435 }
1436 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001437 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1438 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001439 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001440 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 // dummy frame entries
1443 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1444 {
1445 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1446 ++ectx.ec_stack.ga_len;
1447 }
1448
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001449 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001450 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001451 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1452 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001454 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001455 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001456 ectx.ec_stack.ga_len += dfunc->df_varcount;
1457 if (dfunc->df_has_closure)
1458 {
1459 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1460 STACK_TV_VAR(idx)->vval.v_number = 0;
1461 ++ectx.ec_stack.ga_len;
1462 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001463
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001464 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001465 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001466
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001467 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001468 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001469 estack_push_ufunc(ufunc, 1);
1470 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001471 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1472
Bram Moolenaar352134b2020-10-17 22:04:08 +02001473 // Use a specific location for storing error messages to be converted to an
1474 // exception.
1475 saved_msg_list = msg_list;
1476 msg_list = &private_msg_list;
1477
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001478 // Do turn errors into exceptions.
1479 suppress_errthrow = FALSE;
1480
Bram Moolenaarc970e422021-03-17 15:03:04 +01001481 // Do not delete the function while executing it.
1482 ++ufunc->uf_calls;
1483
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001484 // When ":silent!" was used before calling then we still abort the
1485 // function. If ":silent!" is used in the function then we don't.
1486 emsg_silent_def = emsg_silent;
1487 did_emsg_def = 0;
1488
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001489 where.wt_index = 0;
1490 where.wt_variable = FALSE;
1491
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001492 // Start execution at the first instruction.
1493 ectx.ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 for (;;)
1496 {
1497 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001498
Bram Moolenaar270d0382020-05-15 21:42:53 +02001499 if (++breakcheck_count >= 100)
1500 {
1501 line_breakcheck();
1502 breakcheck_count = 0;
1503 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001504 if (got_int)
1505 {
1506 // Turn CTRL-C into an exception.
1507 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001508 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001509 goto failed;
1510 did_throw = TRUE;
1511 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512
Bram Moolenaara26b9702020-04-18 19:53:28 +02001513 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1514 {
1515 // Turn an error message into an exception.
1516 did_emsg = FALSE;
1517 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1518 goto failed;
1519 did_throw = TRUE;
1520 *msg_list = NULL;
1521 }
1522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 if (did_throw && !ectx.ec_in_catch)
1524 {
1525 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001526 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527
1528 // An exception jumps to the first catch, finally, or returns from
1529 // the current function.
1530 if (trystack->ga_len > 0)
1531 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001532 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 {
1534 // jump to ":catch" or ":finally"
1535 ectx.ec_in_catch = TRUE;
1536 ectx.ec_iidx = trycmd->tcd_catch_idx;
1537 }
1538 else
1539 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001540 // Not inside try or need to return from current functions.
1541 // Push a dummy return value.
1542 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1543 goto failed;
1544 tv = STACK_TV_BOT(0);
1545 tv->v_type = VAR_NUMBER;
1546 tv->vval.v_number = 0;
1547 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001548 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001550 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001551 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001552 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1553 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 goto done;
1555 }
1556
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001557 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001558 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 }
1560 continue;
1561 }
1562
1563 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1564 switch (iptr->isn_type)
1565 {
1566 // execute Ex command line
1567 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001568 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001569 source_cookie_T cookie;
1570
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001571 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001572 // Pass getsourceline to get an error for a missing ":end"
1573 // command.
1574 CLEAR_FIELD(cookie);
1575 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1576 if (do_cmdline(iptr->isn_arg.string,
1577 getsourceline, &cookie,
1578 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1579 == FAIL
1580 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001581 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 break;
1584
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001585 // execute Ex command from pieces on the stack
1586 case ISN_EXECCONCAT:
1587 {
1588 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001589 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001590 int pass;
1591 int i;
1592 char_u *cmd = NULL;
1593 char_u *str;
1594
1595 for (pass = 1; pass <= 2; ++pass)
1596 {
1597 for (i = 0; i < count; ++i)
1598 {
1599 tv = STACK_TV_BOT(i - count);
1600 str = tv->vval.v_string;
1601 if (str != NULL && *str != NUL)
1602 {
1603 if (pass == 2)
1604 STRCPY(cmd + len, str);
1605 len += STRLEN(str);
1606 }
1607 if (pass == 2)
1608 clear_tv(tv);
1609 }
1610 if (pass == 1)
1611 {
1612 cmd = alloc(len + 1);
1613 if (cmd == NULL)
1614 goto failed;
1615 len = 0;
1616 }
1617 }
1618
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001619 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001620 do_cmdline_cmd(cmd);
1621 vim_free(cmd);
1622 }
1623 break;
1624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 // execute :echo {string} ...
1626 case ISN_ECHO:
1627 {
1628 int count = iptr->isn_arg.echo.echo_count;
1629 int atstart = TRUE;
1630 int needclr = TRUE;
1631
1632 for (idx = 0; idx < count; ++idx)
1633 {
1634 tv = STACK_TV_BOT(idx - count);
1635 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1636 &atstart, &needclr);
1637 clear_tv(tv);
1638 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001639 if (needclr)
1640 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 ectx.ec_stack.ga_len -= count;
1642 }
1643 break;
1644
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001645 // :execute {string} ...
1646 // :echomsg {string} ...
1647 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001648 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001649 case ISN_ECHOMSG:
1650 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001651 {
1652 int count = iptr->isn_arg.number;
1653 garray_T ga;
1654 char_u buf[NUMBUFLEN];
1655 char_u *p;
1656 int len;
1657 int failed = FALSE;
1658
1659 ga_init2(&ga, 1, 80);
1660 for (idx = 0; idx < count; ++idx)
1661 {
1662 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001663 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001664 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001665 if (tv->v_type == VAR_CHANNEL
1666 || tv->v_type == VAR_JOB)
1667 {
1668 SOURCING_LNUM = iptr->isn_lnum;
1669 emsg(_(e_inval_string));
1670 break;
1671 }
1672 else
1673 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001674 }
1675 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001676 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001677
1678 len = (int)STRLEN(p);
1679 if (ga_grow(&ga, len + 2) == FAIL)
1680 failed = TRUE;
1681 else
1682 {
1683 if (ga.ga_len > 0)
1684 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1685 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1686 ga.ga_len += len;
1687 }
1688 clear_tv(tv);
1689 }
1690 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001691 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001692 {
1693 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001694 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001695 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001696
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001697 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001698 {
1699 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001700 {
1701 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001702 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001703 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001704 {
1705 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001706 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001707 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001708 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001709 else
1710 {
1711 msg_sb_eol();
1712 if (iptr->isn_type == ISN_ECHOMSG)
1713 {
1714 msg_attr(ga.ga_data, echo_attr);
1715 out_flush();
1716 }
1717 else
1718 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001719 SOURCING_LNUM = iptr->isn_lnum;
1720 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001721 }
1722 }
1723 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001724 ga_clear(&ga);
1725 }
1726 break;
1727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 // load local variable or argument
1729 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001730 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 goto failed;
1732 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1733 ++ectx.ec_stack.ga_len;
1734 break;
1735
1736 // load v: variable
1737 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001738 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 goto failed;
1740 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1741 ++ectx.ec_stack.ga_len;
1742 break;
1743
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001744 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 case ISN_LOADSCRIPT:
1746 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001747 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 svar_T *sv;
1749
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001750 sv = get_script_svar(sref, &ectx);
1751 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001752 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001753 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001754 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 goto failed;
1756 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1757 ++ectx.ec_stack.ga_len;
1758 }
1759 break;
1760
1761 // load s: variable in old script
1762 case ISN_LOADS:
1763 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001764 hashtab_T *ht = &SCRIPT_VARS(
1765 iptr->isn_arg.loadstore.ls_sid);
1766 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 if (di == NULL)
1770 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001771 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001772 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001773 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 }
1775 else
1776 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001777 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 goto failed;
1779 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1780 ++ectx.ec_stack.ga_len;
1781 }
1782 }
1783 break;
1784
Bram Moolenaard3aac292020-04-19 14:32:17 +02001785 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001787 case ISN_LOADB:
1788 case ISN_LOADW:
1789 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001791 dictitem_T *di = NULL;
1792 hashtab_T *ht = NULL;
1793 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001794
Bram Moolenaard3aac292020-04-19 14:32:17 +02001795 switch (iptr->isn_type)
1796 {
1797 case ISN_LOADG:
1798 ht = get_globvar_ht();
1799 namespace = 'g';
1800 break;
1801 case ISN_LOADB:
1802 ht = &curbuf->b_vars->dv_hashtab;
1803 namespace = 'b';
1804 break;
1805 case ISN_LOADW:
1806 ht = &curwin->w_vars->dv_hashtab;
1807 namespace = 'w';
1808 break;
1809 case ISN_LOADT:
1810 ht = &curtab->tp_vars->dv_hashtab;
1811 namespace = 't';
1812 break;
1813 default: // Cannot reach here
1814 goto failed;
1815 }
1816 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 if (di == NULL)
1819 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001820 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001821 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001822 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001823 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 }
1825 else
1826 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001827 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 goto failed;
1829 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1830 ++ectx.ec_stack.ga_len;
1831 }
1832 }
1833 break;
1834
Bram Moolenaar03290b82020-12-19 16:30:44 +01001835 // load autoload variable
1836 case ISN_LOADAUTO:
1837 {
1838 char_u *name = iptr->isn_arg.string;
1839
1840 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1841 goto failed;
1842 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001843 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001844 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001845 goto on_error;
1846 ++ectx.ec_stack.ga_len;
1847 }
1848 break;
1849
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001850 // load g:/b:/w:/t: namespace
1851 case ISN_LOADGDICT:
1852 case ISN_LOADBDICT:
1853 case ISN_LOADWDICT:
1854 case ISN_LOADTDICT:
1855 {
1856 dict_T *d = NULL;
1857
1858 switch (iptr->isn_type)
1859 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001860 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1861 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1862 case ISN_LOADWDICT: d = curwin->w_vars; break;
1863 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001864 default: // Cannot reach here
1865 goto failed;
1866 }
1867 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1868 goto failed;
1869 tv = STACK_TV_BOT(0);
1870 tv->v_type = VAR_DICT;
1871 tv->v_lock = 0;
1872 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001873 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001874 ++ectx.ec_stack.ga_len;
1875 }
1876 break;
1877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 // load &option
1879 case ISN_LOADOPT:
1880 {
1881 typval_T optval;
1882 char_u *name = iptr->isn_arg.string;
1883
Bram Moolenaara8c17702020-04-01 21:17:24 +02001884 // This is not expected to fail, name is checked during
1885 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001886 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001888 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001889 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 *STACK_TV_BOT(0) = optval;
1891 ++ectx.ec_stack.ga_len;
1892 }
1893 break;
1894
1895 // load $ENV
1896 case ISN_LOADENV:
1897 {
1898 typval_T optval;
1899 char_u *name = iptr->isn_arg.string;
1900
Bram Moolenaar270d0382020-05-15 21:42:53 +02001901 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001903 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001904 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 *STACK_TV_BOT(0) = optval;
1906 ++ectx.ec_stack.ga_len;
1907 }
1908 break;
1909
1910 // load @register
1911 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001912 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 goto failed;
1914 tv = STACK_TV_BOT(0);
1915 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001916 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001917 // This may result in NULL, which should be equivalent to an
1918 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 tv->vval.v_string = get_reg_contents(
1920 iptr->isn_arg.number, GREG_EXPR_SRC);
1921 ++ectx.ec_stack.ga_len;
1922 break;
1923
1924 // store local variable
1925 case ISN_STORE:
1926 --ectx.ec_stack.ga_len;
1927 tv = STACK_TV_VAR(iptr->isn_arg.number);
1928 clear_tv(tv);
1929 *tv = *STACK_TV_BOT(0);
1930 break;
1931
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001932 // store s: variable in old script
1933 case ISN_STORES:
1934 {
1935 hashtab_T *ht = &SCRIPT_VARS(
1936 iptr->isn_arg.loadstore.ls_sid);
1937 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001938 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001939
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001940 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001941 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001942 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001943 else
1944 {
1945 clear_tv(&di->di_tv);
1946 di->di_tv = *STACK_TV_BOT(0);
1947 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001948 }
1949 break;
1950
1951 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 case ISN_STORESCRIPT:
1953 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001954 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1955 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001957 sv = get_script_svar(sref, &ectx);
1958 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001959 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 --ectx.ec_stack.ga_len;
1961 clear_tv(sv->sv_tv);
1962 *sv->sv_tv = *STACK_TV_BOT(0);
1963 }
1964 break;
1965
1966 // store option
1967 case ISN_STOREOPT:
1968 {
1969 long n = 0;
1970 char_u *s = NULL;
1971 char *msg;
1972
1973 --ectx.ec_stack.ga_len;
1974 tv = STACK_TV_BOT(0);
1975 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001976 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001978 if (s == NULL)
1979 s = (char_u *)"";
1980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001982 // must be VAR_NUMBER, CHECKTYPE makes sure
1983 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1985 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001986 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 if (msg != NULL)
1988 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001989 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001991 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 }
1994 break;
1995
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001996 // store $ENV
1997 case ISN_STOREENV:
1998 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001999 tv = STACK_TV_BOT(0);
2000 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2001 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002002 break;
2003
2004 // store @r
2005 case ISN_STOREREG:
2006 {
2007 int reg = iptr->isn_arg.number;
2008
2009 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002010 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002011 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002012 tv_get_string(tv), -1, FALSE);
2013 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002014 }
2015 break;
2016
2017 // store v: variable
2018 case ISN_STOREV:
2019 --ectx.ec_stack.ga_len;
2020 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2021 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002022 // should not happen, type is checked when compiling
2023 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002024 break;
2025
Bram Moolenaard3aac292020-04-19 14:32:17 +02002026 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002028 case ISN_STOREB:
2029 case ISN_STOREW:
2030 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002032 dictitem_T *di;
2033 hashtab_T *ht;
2034 char_u *name = iptr->isn_arg.string + 2;
2035
Bram Moolenaard3aac292020-04-19 14:32:17 +02002036 switch (iptr->isn_type)
2037 {
2038 case ISN_STOREG:
2039 ht = get_globvar_ht();
2040 break;
2041 case ISN_STOREB:
2042 ht = &curbuf->b_vars->dv_hashtab;
2043 break;
2044 case ISN_STOREW:
2045 ht = &curwin->w_vars->dv_hashtab;
2046 break;
2047 case ISN_STORET:
2048 ht = &curtab->tp_vars->dv_hashtab;
2049 break;
2050 default: // Cannot reach here
2051 goto failed;
2052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053
2054 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002055 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002057 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 else
2059 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002060 SOURCING_LNUM = iptr->isn_lnum;
2061 if (var_check_permission(di, name) == FAIL)
2062 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 clear_tv(&di->di_tv);
2064 di->di_tv = *STACK_TV_BOT(0);
2065 }
2066 }
2067 break;
2068
Bram Moolenaar03290b82020-12-19 16:30:44 +01002069 // store an autoload variable
2070 case ISN_STOREAUTO:
2071 SOURCING_LNUM = iptr->isn_lnum;
2072 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2073 clear_tv(STACK_TV_BOT(-1));
2074 --ectx.ec_stack.ga_len;
2075 break;
2076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077 // store number in local variable
2078 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002079 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 clear_tv(tv);
2081 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002082 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 break;
2084
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002085 // store value in list or dict variable
2086 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002087 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002088 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002089 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002090 typval_T *tv_dest = STACK_TV_BOT(-1);
2091 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002092
Bram Moolenaar752fc692021-01-04 21:57:11 +01002093 // Stack contains:
2094 // -3 value to be stored
2095 // -2 index
2096 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002097 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002098 SOURCING_LNUM = iptr->isn_lnum;
2099 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002100 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002101 dest_type = tv_dest->v_type;
2102 if (dest_type == VAR_DICT)
2103 status = do_2string(tv_idx, TRUE);
2104 else if (dest_type == VAR_LIST
2105 && tv_idx->v_type != VAR_NUMBER)
2106 {
2107 emsg(_(e_number_exp));
2108 status = FAIL;
2109 }
2110 }
2111 else if (dest_type != tv_dest->v_type)
2112 {
2113 // just in case, should be OK
2114 semsg(_(e_expected_str_but_got_str),
2115 vartype_name(dest_type),
2116 vartype_name(tv_dest->v_type));
2117 status = FAIL;
2118 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002119
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002120 if (status == OK && dest_type == VAR_LIST)
2121 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002122 long lidx = (long)tv_idx->vval.v_number;
2123 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002124
2125 if (list == NULL)
2126 {
2127 emsg(_(e_list_not_set));
2128 goto on_error;
2129 }
2130 if (lidx < 0 && list->lv_len + lidx >= 0)
2131 // negative index is relative to the end
2132 lidx = list->lv_len + lidx;
2133 if (lidx < 0 || lidx > list->lv_len)
2134 {
2135 semsg(_(e_listidx), lidx);
2136 goto on_error;
2137 }
2138 if (lidx < list->lv_len)
2139 {
2140 listitem_T *li = list_find(list, lidx);
2141
2142 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002143 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002144 goto on_error;
2145 // overwrite existing list item
2146 clear_tv(&li->li_tv);
2147 li->li_tv = *tv;
2148 }
2149 else
2150 {
2151 if (error_if_locked(list->lv_lock,
2152 e_cannot_change_list))
2153 goto on_error;
2154 // append to list, only fails when out of memory
2155 if (list_append_tv(list, tv) == FAIL)
2156 goto failed;
2157 clear_tv(tv);
2158 }
2159 }
2160 else if (status == OK && dest_type == VAR_DICT)
2161 {
2162 char_u *key = tv_idx->vval.v_string;
2163 dict_T *dict = tv_dest->vval.v_dict;
2164 dictitem_T *di;
2165
2166 SOURCING_LNUM = iptr->isn_lnum;
2167 if (dict == NULL)
2168 {
2169 emsg(_(e_dictionary_not_set));
2170 goto on_error;
2171 }
2172 if (key == NULL)
2173 key = (char_u *)"";
2174 di = dict_find(dict, key, -1);
2175 if (di != NULL)
2176 {
2177 if (error_if_locked(di->di_tv.v_lock,
2178 e_cannot_change_dict_item))
2179 goto on_error;
2180 // overwrite existing value
2181 clear_tv(&di->di_tv);
2182 di->di_tv = *tv;
2183 }
2184 else
2185 {
2186 if (error_if_locked(dict->dv_lock,
2187 e_cannot_change_dict))
2188 goto on_error;
2189 // add to dict, only fails when out of memory
2190 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2191 goto failed;
2192 clear_tv(tv);
2193 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002194 }
2195 else
2196 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002197 status = FAIL;
2198 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002199 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002200
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002201 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002202 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002203 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002204 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002205 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002206 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002207 goto on_error;
2208 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002209 }
2210 break;
2211
Bram Moolenaar0186e582021-01-10 18:33:11 +01002212 // load or store variable or argument from outer scope
2213 case ISN_LOADOUTER:
2214 case ISN_STOREOUTER:
2215 {
2216 int depth = iptr->isn_arg.outer.outer_depth;
2217 outer_T *outer = ectx.ec_outer;
2218
2219 while (depth > 1 && outer != NULL)
2220 {
2221 outer = outer->out_up;
2222 --depth;
2223 }
2224 if (outer == NULL)
2225 {
2226 SOURCING_LNUM = iptr->isn_lnum;
2227 iemsg("LOADOUTER depth more than scope levels");
2228 goto failed;
2229 }
2230 tv = ((typval_T *)outer->out_stack->ga_data)
2231 + outer->out_frame_idx + STACK_FRAME_SIZE
2232 + iptr->isn_arg.outer.outer_idx;
2233 if (iptr->isn_type == ISN_LOADOUTER)
2234 {
2235 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2236 goto failed;
2237 copy_tv(tv, STACK_TV_BOT(0));
2238 ++ectx.ec_stack.ga_len;
2239 }
2240 else
2241 {
2242 --ectx.ec_stack.ga_len;
2243 clear_tv(tv);
2244 *tv = *STACK_TV_BOT(0);
2245 }
2246 }
2247 break;
2248
Bram Moolenaar752fc692021-01-04 21:57:11 +01002249 // unlet item in list or dict variable
2250 case ISN_UNLETINDEX:
2251 {
2252 typval_T *tv_idx = STACK_TV_BOT(-2);
2253 typval_T *tv_dest = STACK_TV_BOT(-1);
2254 int status = OK;
2255
2256 // Stack contains:
2257 // -2 index
2258 // -1 dict or list
2259 if (tv_dest->v_type == VAR_DICT)
2260 {
2261 // unlet a dict item, index must be a string
2262 if (tv_idx->v_type != VAR_STRING)
2263 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002264 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002265 semsg(_(e_expected_str_but_got_str),
2266 vartype_name(VAR_STRING),
2267 vartype_name(tv_idx->v_type));
2268 status = FAIL;
2269 }
2270 else
2271 {
2272 dict_T *d = tv_dest->vval.v_dict;
2273 char_u *key = tv_idx->vval.v_string;
2274 dictitem_T *di = NULL;
2275
2276 if (key == NULL)
2277 key = (char_u *)"";
2278 if (d != NULL)
2279 di = dict_find(d, key, (int)STRLEN(key));
2280 if (di == NULL)
2281 {
2282 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002283 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002284 semsg(_(e_dictkey), key);
2285 status = FAIL;
2286 }
2287 else
2288 {
2289 // TODO: check for dict or item locked
2290 dictitem_remove(d, di);
2291 }
2292 }
2293 }
2294 else if (tv_dest->v_type == VAR_LIST)
2295 {
2296 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002297 SOURCING_LNUM = iptr->isn_lnum;
2298 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002299 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002300 status = FAIL;
2301 }
2302 else
2303 {
2304 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002305 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002306 listitem_T *li = NULL;
2307
2308 li = list_find(l, n);
2309 if (li == NULL)
2310 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002311 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002312 semsg(_(e_listidx), n);
2313 status = FAIL;
2314 }
2315 else
2316 // TODO: check for list or item locked
2317 listitem_remove(l, li);
2318 }
2319 }
2320 else
2321 {
2322 status = FAIL;
2323 semsg(_(e_cannot_index_str),
2324 vartype_name(tv_dest->v_type));
2325 }
2326
2327 clear_tv(tv_idx);
2328 clear_tv(tv_dest);
2329 ectx.ec_stack.ga_len -= 2;
2330 if (status == FAIL)
2331 goto on_error;
2332 }
2333 break;
2334
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002335 // unlet range of items in list variable
2336 case ISN_UNLETRANGE:
2337 {
2338 // Stack contains:
2339 // -3 index1
2340 // -2 index2
2341 // -1 dict or list
2342 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2343 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2344 typval_T *tv_dest = STACK_TV_BOT(-1);
2345 int status = OK;
2346
2347 if (tv_dest->v_type == VAR_LIST)
2348 {
2349 // indexes must be a number
2350 SOURCING_LNUM = iptr->isn_lnum;
2351 if (check_for_number(tv_idx1) == FAIL
2352 || check_for_number(tv_idx2) == FAIL)
2353 {
2354 status = FAIL;
2355 }
2356 else
2357 {
2358 list_T *l = tv_dest->vval.v_list;
2359 long n1 = (long)tv_idx1->vval.v_number;
2360 long n2 = (long)tv_idx2->vval.v_number;
2361 listitem_T *li;
2362
2363 li = list_find_index(l, &n1);
2364 if (li == NULL
2365 || list_unlet_range(l, li, NULL, n1,
2366 TRUE, n2) == FAIL)
2367 status = FAIL;
2368 }
2369 }
2370 else
2371 {
2372 status = FAIL;
2373 SOURCING_LNUM = iptr->isn_lnum;
2374 semsg(_(e_cannot_index_str),
2375 vartype_name(tv_dest->v_type));
2376 }
2377
2378 clear_tv(tv_idx1);
2379 clear_tv(tv_idx2);
2380 clear_tv(tv_dest);
2381 ectx.ec_stack.ga_len -= 3;
2382 if (status == FAIL)
2383 goto on_error;
2384 }
2385 break;
2386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 // push constant
2388 case ISN_PUSHNR:
2389 case ISN_PUSHBOOL:
2390 case ISN_PUSHSPEC:
2391 case ISN_PUSHF:
2392 case ISN_PUSHS:
2393 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002394 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002395 case ISN_PUSHCHANNEL:
2396 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002397 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 goto failed;
2399 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002400 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 ++ectx.ec_stack.ga_len;
2402 switch (iptr->isn_type)
2403 {
2404 case ISN_PUSHNR:
2405 tv->v_type = VAR_NUMBER;
2406 tv->vval.v_number = iptr->isn_arg.number;
2407 break;
2408 case ISN_PUSHBOOL:
2409 tv->v_type = VAR_BOOL;
2410 tv->vval.v_number = iptr->isn_arg.number;
2411 break;
2412 case ISN_PUSHSPEC:
2413 tv->v_type = VAR_SPECIAL;
2414 tv->vval.v_number = iptr->isn_arg.number;
2415 break;
2416#ifdef FEAT_FLOAT
2417 case ISN_PUSHF:
2418 tv->v_type = VAR_FLOAT;
2419 tv->vval.v_float = iptr->isn_arg.fnumber;
2420 break;
2421#endif
2422 case ISN_PUSHBLOB:
2423 blob_copy(iptr->isn_arg.blob, tv);
2424 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002425 case ISN_PUSHFUNC:
2426 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002427 if (iptr->isn_arg.string == NULL)
2428 tv->vval.v_string = NULL;
2429 else
2430 tv->vval.v_string =
2431 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002432 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002433 case ISN_PUSHCHANNEL:
2434#ifdef FEAT_JOB_CHANNEL
2435 tv->v_type = VAR_CHANNEL;
2436 tv->vval.v_channel = iptr->isn_arg.channel;
2437 if (tv->vval.v_channel != NULL)
2438 ++tv->vval.v_channel->ch_refcount;
2439#endif
2440 break;
2441 case ISN_PUSHJOB:
2442#ifdef FEAT_JOB_CHANNEL
2443 tv->v_type = VAR_JOB;
2444 tv->vval.v_job = iptr->isn_arg.job;
2445 if (tv->vval.v_job != NULL)
2446 ++tv->vval.v_job->jv_refcount;
2447#endif
2448 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 default:
2450 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002451 tv->vval.v_string = vim_strsave(
2452 iptr->isn_arg.string == NULL
2453 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 }
2455 break;
2456
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002457 case ISN_UNLET:
2458 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2459 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002460 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002461 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002462 case ISN_UNLETENV:
2463 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2464 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002465
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002466 case ISN_LOCKCONST:
2467 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2468 break;
2469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 // create a list from items on the stack; uses a single allocation
2471 // for the list header and the items
2472 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002473 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2474 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 break;
2476
2477 // create a dict from items on the stack
2478 case ISN_NEWDICT:
2479 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002480 int count = iptr->isn_arg.number;
2481 dict_T *dict = dict_alloc();
2482 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002483 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484
2485 if (dict == NULL)
2486 goto failed;
2487 for (idx = 0; idx < count; ++idx)
2488 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002489 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002491 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002492 key = tv->vval.v_string == NULL
2493 ? (char_u *)"" : tv->vval.v_string;
2494 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002495 if (item != NULL)
2496 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002497 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002498 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002499 dict_unref(dict);
2500 goto on_error;
2501 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002502 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 clear_tv(tv);
2504 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002505 {
2506 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2510 item->di_tv.v_lock = 0;
2511 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002512 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002513 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002514 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 }
2518
2519 if (count > 0)
2520 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002521 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 goto failed;
2523 else
2524 ++ectx.ec_stack.ga_len;
2525 tv = STACK_TV_BOT(-1);
2526 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002527 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 tv->vval.v_dict = dict;
2529 ++dict->dv_refcount;
2530 }
2531 break;
2532
2533 // call a :def function
2534 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002535 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002536 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2537 NULL,
2538 iptr->isn_arg.dfunc.cdf_argcount,
2539 &funclocal,
2540 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002541 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 break;
2543
2544 // call a builtin function
2545 case ISN_BCALL:
2546 SOURCING_LNUM = iptr->isn_lnum;
2547 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2548 iptr->isn_arg.bfunc.cbf_argcount,
2549 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002550 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 break;
2552
2553 // call a funcref or partial
2554 case ISN_PCALL:
2555 {
2556 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2557 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002558 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559
2560 SOURCING_LNUM = iptr->isn_lnum;
2561 if (pfunc->cpf_top)
2562 {
2563 // funcref is above the arguments
2564 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2565 }
2566 else
2567 {
2568 // Get the funcref from the stack.
2569 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002570 partial_tv = *STACK_TV_BOT(0);
2571 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002573 r = call_partial(tv, pfunc->cpf_argcount,
2574 &funclocal, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002575 if (tv == &partial_tv)
2576 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002578 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 }
2580 break;
2581
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002582 case ISN_PCALL_END:
2583 // PCALL finished, arguments have been consumed and replaced by
2584 // the return value. Now clear the funcref from the stack,
2585 // and move the return value in its place.
2586 --ectx.ec_stack.ga_len;
2587 clear_tv(STACK_TV_BOT(-1));
2588 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2589 break;
2590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 // call a user defined function or funcref/partial
2592 case ISN_UCALL:
2593 {
2594 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2595
2596 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002597 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
2598 &funclocal, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002599 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 }
2601 break;
2602
2603 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002604 case ISN_RETURN_ZERO:
2605 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2606 goto failed;
2607 tv = STACK_TV_BOT(0);
2608 ++ectx.ec_stack.ga_len;
2609 tv->v_type = VAR_NUMBER;
2610 tv->vval.v_number = 0;
2611 tv->v_lock = 0;
2612 // FALLTHROUGH
2613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 case ISN_RETURN:
2615 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002616 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002617 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002618
2619 if (trystack->ga_len > 0)
2620 trycmd = ((trycmd_T *)trystack->ga_data)
2621 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002622 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002623 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002625 // jump to ":finally" or ":endtry"
2626 if (trycmd->tcd_finally_idx != 0)
2627 ectx.ec_iidx = trycmd->tcd_finally_idx;
2628 else
2629 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 trycmd->tcd_return = TRUE;
2631 }
2632 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002633 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 }
2635 break;
2636
2637 // push a function reference to a compiled function
2638 case ISN_FUNCREF:
2639 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002640 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2641 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2642 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 if (pt == NULL)
2645 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002646 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002647 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002648 vim_free(pt);
2649 goto failed;
2650 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002651 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2652 &ectx) == FAIL)
2653 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 tv = STACK_TV_BOT(0);
2656 ++ectx.ec_stack.ga_len;
2657 tv->vval.v_partial = pt;
2658 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002659 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 }
2661 break;
2662
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002663 // Create a global function from a lambda.
2664 case ISN_NEWFUNC:
2665 {
2666 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2667
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002668 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002669 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002670 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002671 }
2672 break;
2673
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002674 // List functions
2675 case ISN_DEF:
2676 if (iptr->isn_arg.string == NULL)
2677 list_functions(NULL);
2678 else
2679 {
2680 exarg_T ea;
2681
2682 CLEAR_FIELD(ea);
2683 ea.cmd = ea.arg = iptr->isn_arg.string;
2684 define_function(&ea, NULL);
2685 }
2686 break;
2687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 // jump if a condition is met
2689 case ISN_JUMP:
2690 {
2691 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002692 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 int jump = TRUE;
2694
2695 if (when != JUMP_ALWAYS)
2696 {
2697 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002698 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002699 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002700 || when == JUMP_IF_COND_TRUE)
2701 {
2702 SOURCING_LNUM = iptr->isn_lnum;
2703 jump = tv_get_bool_chk(tv, &error);
2704 if (error)
2705 goto on_error;
2706 }
2707 else
2708 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002710 || when == JUMP_AND_KEEP_IF_FALSE
2711 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002713 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 {
2715 // drop the value from the stack
2716 clear_tv(tv);
2717 --ectx.ec_stack.ga_len;
2718 }
2719 }
2720 if (jump)
2721 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2722 }
2723 break;
2724
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002725 // Jump if an argument with a default value was already set and not
2726 // v:none.
2727 case ISN_JUMP_IF_ARG_SET:
2728 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2729 if (tv->v_type != VAR_UNKNOWN
2730 && !(tv->v_type == VAR_SPECIAL
2731 && tv->vval.v_number == VVAL_NONE))
2732 ectx.ec_iidx = iptr->isn_arg.jumparg.jump_where;
2733 break;
2734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 // top of a for loop
2736 case ISN_FOR:
2737 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002738 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 typval_T *idxtv =
2740 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2741
Bram Moolenaar270d0382020-05-15 21:42:53 +02002742 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 goto failed;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002744 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002745 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002746 list_T *list = ltv->vval.v_list;
2747
2748 // push the next item from the list
2749 ++idxtv->vval.v_number;
2750 if (list == NULL
2751 || idxtv->vval.v_number >= list->lv_len)
2752 {
2753 // past the end of the list, jump to "endfor"
2754 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2755 may_restore_cmdmod(&funclocal);
2756 }
2757 else if (list->lv_first == &range_list_item)
2758 {
2759 // non-materialized range() list
2760 tv = STACK_TV_BOT(0);
2761 tv->v_type = VAR_NUMBER;
2762 tv->v_lock = 0;
2763 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 list, idxtv->vval.v_number, NULL);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002765 ++ectx.ec_stack.ga_len;
2766 }
2767 else
2768 {
2769 listitem_T *li = list_find(list,
2770 idxtv->vval.v_number);
2771
2772 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2773 ++ectx.ec_stack.ga_len;
2774 }
2775 }
2776 else if (ltv->v_type == VAR_STRING)
2777 {
2778 char_u *str = ltv->vval.v_string;
2779 int len = str == NULL ? 0 : (int)STRLEN(str);
2780
2781 // Push the next character from the string. The index
2782 // is for the last byte of the previous character.
2783 ++idxtv->vval.v_number;
2784 if (idxtv->vval.v_number >= len)
2785 {
2786 // past the end of the string, jump to "endfor"
2787 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2788 may_restore_cmdmod(&funclocal);
2789 }
2790 else
2791 {
2792 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2793
2794 tv = STACK_TV_BOT(0);
2795 tv->v_type = VAR_STRING;
2796 tv->vval.v_string = vim_strnsave(
2797 str + idxtv->vval.v_number, clen);
2798 ++ectx.ec_stack.ga_len;
2799 idxtv->vval.v_number += clen - 1;
2800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 }
2802 else
2803 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002804 // TODO: support Blob
2805 semsg(_(e_for_loop_on_str_not_supported),
2806 vartype_name(ltv->v_type));
2807 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 }
2809 }
2810 break;
2811
2812 // start of ":try" block
2813 case ISN_TRY:
2814 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002815 trycmd_T *trycmd = NULL;
2816
Bram Moolenaar270d0382020-05-15 21:42:53 +02002817 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 goto failed;
2819 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2820 + ectx.ec_trystack.ga_len;
2821 ++ectx.ec_trystack.ga_len;
2822 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002823 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002824 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002825 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002826 trycmd->tcd_catch_idx =
2827 iptr->isn_arg.try.try_ref->try_catch;
2828 trycmd->tcd_finally_idx =
2829 iptr->isn_arg.try.try_ref->try_finally;
2830 trycmd->tcd_endtry_idx =
2831 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 }
2833 break;
2834
2835 case ISN_PUSHEXC:
2836 if (current_exception == NULL)
2837 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002838 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 iemsg("Evaluating catch while current_exception is NULL");
2840 goto failed;
2841 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002842 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 goto failed;
2844 tv = STACK_TV_BOT(0);
2845 ++ectx.ec_stack.ga_len;
2846 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002847 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 tv->vval.v_string = vim_strsave(
2849 (char_u *)current_exception->value);
2850 break;
2851
2852 case ISN_CATCH:
2853 {
2854 garray_T *trystack = &ectx.ec_trystack;
2855
Bram Moolenaara91a7132021-03-25 21:12:15 +01002856 may_restore_cmdmod(&funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 if (trystack->ga_len > 0)
2858 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002859 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 + trystack->ga_len - 1;
2861 trycmd->tcd_caught = TRUE;
2862 }
2863 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002864 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 catch_exception(current_exception);
2866 }
2867 break;
2868
Bram Moolenaarc150c092021-02-13 15:02:46 +01002869 case ISN_TRYCONT:
2870 {
2871 garray_T *trystack = &ectx.ec_trystack;
2872 trycont_T *trycont = &iptr->isn_arg.trycont;
2873 int i;
2874 trycmd_T *trycmd;
2875 int iidx = trycont->tct_where;
2876
2877 if (trystack->ga_len < trycont->tct_levels)
2878 {
2879 siemsg("TRYCONT: expected %d levels, found %d",
2880 trycont->tct_levels, trystack->ga_len);
2881 goto failed;
2882 }
2883 // Make :endtry jump to any outer try block and the last
2884 // :endtry inside the loop to the loop start.
2885 for (i = trycont->tct_levels; i > 0; --i)
2886 {
2887 trycmd = ((trycmd_T *)trystack->ga_data)
2888 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002889 // Add one to tcd_cont to be able to jump to
2890 // instruction with index zero.
2891 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002892 iidx = trycmd->tcd_finally_idx == 0
2893 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002894 }
2895 // jump to :finally or :endtry of current try statement
2896 ectx.ec_iidx = iidx;
2897 }
2898 break;
2899
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002900 case ISN_FINALLY:
2901 {
2902 garray_T *trystack = &ectx.ec_trystack;
2903 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2904 + trystack->ga_len - 1;
2905
2906 // Reset the index to avoid a return statement jumps here
2907 // again.
2908 trycmd->tcd_finally_idx = 0;
2909 break;
2910 }
2911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 // end of ":try" block
2913 case ISN_ENDTRY:
2914 {
2915 garray_T *trystack = &ectx.ec_trystack;
2916
2917 if (trystack->ga_len > 0)
2918 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002919 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 --trystack->ga_len;
2922 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002923 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 trycmd = ((trycmd_T *)trystack->ga_data)
2925 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002926 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 {
2928 // discard the exception
2929 if (caught_stack == current_exception)
2930 caught_stack = caught_stack->caught;
2931 discard_current_exception();
2932 }
2933
2934 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002935 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002936
2937 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2938 {
2939 --ectx.ec_stack.ga_len;
2940 clear_tv(STACK_TV_BOT(0));
2941 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002942 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002943 // handling :continue: jump to outer try block or
2944 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002945 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 }
2947 }
2948 break;
2949
2950 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002951 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002952 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002953
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002954 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2955 {
2956 // throwing an exception while using "silent!" causes
2957 // the function to abort but not display an error.
2958 tv = STACK_TV_BOT(-1);
2959 clear_tv(tv);
2960 tv->v_type = VAR_NUMBER;
2961 tv->vval.v_number = 0;
2962 goto done;
2963 }
2964 --ectx.ec_stack.ga_len;
2965 tv = STACK_TV_BOT(0);
2966 if (tv->vval.v_string == NULL
2967 || *skipwhite(tv->vval.v_string) == NUL)
2968 {
2969 vim_free(tv->vval.v_string);
2970 SOURCING_LNUM = iptr->isn_lnum;
2971 emsg(_(e_throw_with_empty_string));
2972 goto failed;
2973 }
2974
2975 // Inside a "catch" we need to first discard the caught
2976 // exception.
2977 if (trystack->ga_len > 0)
2978 {
2979 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2980 + trystack->ga_len - 1;
2981 if (trycmd->tcd_caught && current_exception != NULL)
2982 {
2983 // discard the exception
2984 if (caught_stack == current_exception)
2985 caught_stack = caught_stack->caught;
2986 discard_current_exception();
2987 trycmd->tcd_caught = FALSE;
2988 }
2989 }
2990
2991 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2992 == FAIL)
2993 {
2994 vim_free(tv->vval.v_string);
2995 goto failed;
2996 }
2997 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 break;
3000
3001 // compare with special values
3002 case ISN_COMPAREBOOL:
3003 case ISN_COMPARESPECIAL:
3004 {
3005 typval_T *tv1 = STACK_TV_BOT(-2);
3006 typval_T *tv2 = STACK_TV_BOT(-1);
3007 varnumber_T arg1 = tv1->vval.v_number;
3008 varnumber_T arg2 = tv2->vval.v_number;
3009 int res;
3010
3011 switch (iptr->isn_arg.op.op_type)
3012 {
3013 case EXPR_EQUAL: res = arg1 == arg2; break;
3014 case EXPR_NEQUAL: res = arg1 != arg2; break;
3015 default: res = 0; break;
3016 }
3017
3018 --ectx.ec_stack.ga_len;
3019 tv1->v_type = VAR_BOOL;
3020 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3021 }
3022 break;
3023
3024 // Operation with two number arguments
3025 case ISN_OPNR:
3026 case ISN_COMPARENR:
3027 {
3028 typval_T *tv1 = STACK_TV_BOT(-2);
3029 typval_T *tv2 = STACK_TV_BOT(-1);
3030 varnumber_T arg1 = tv1->vval.v_number;
3031 varnumber_T arg2 = tv2->vval.v_number;
3032 varnumber_T res;
3033
3034 switch (iptr->isn_arg.op.op_type)
3035 {
3036 case EXPR_MULT: res = arg1 * arg2; break;
3037 case EXPR_DIV: res = arg1 / arg2; break;
3038 case EXPR_REM: res = arg1 % arg2; break;
3039 case EXPR_SUB: res = arg1 - arg2; break;
3040 case EXPR_ADD: res = arg1 + arg2; break;
3041
3042 case EXPR_EQUAL: res = arg1 == arg2; break;
3043 case EXPR_NEQUAL: res = arg1 != arg2; break;
3044 case EXPR_GREATER: res = arg1 > arg2; break;
3045 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3046 case EXPR_SMALLER: res = arg1 < arg2; break;
3047 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3048 default: res = 0; break;
3049 }
3050
3051 --ectx.ec_stack.ga_len;
3052 if (iptr->isn_type == ISN_COMPARENR)
3053 {
3054 tv1->v_type = VAR_BOOL;
3055 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3056 }
3057 else
3058 tv1->vval.v_number = res;
3059 }
3060 break;
3061
3062 // Computation with two float arguments
3063 case ISN_OPFLOAT:
3064 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003065#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 {
3067 typval_T *tv1 = STACK_TV_BOT(-2);
3068 typval_T *tv2 = STACK_TV_BOT(-1);
3069 float_T arg1 = tv1->vval.v_float;
3070 float_T arg2 = tv2->vval.v_float;
3071 float_T res = 0;
3072 int cmp = FALSE;
3073
3074 switch (iptr->isn_arg.op.op_type)
3075 {
3076 case EXPR_MULT: res = arg1 * arg2; break;
3077 case EXPR_DIV: res = arg1 / arg2; break;
3078 case EXPR_SUB: res = arg1 - arg2; break;
3079 case EXPR_ADD: res = arg1 + arg2; break;
3080
3081 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3082 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3083 case EXPR_GREATER: cmp = arg1 > arg2; break;
3084 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3085 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3086 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3087 default: cmp = 0; break;
3088 }
3089 --ectx.ec_stack.ga_len;
3090 if (iptr->isn_type == ISN_COMPAREFLOAT)
3091 {
3092 tv1->v_type = VAR_BOOL;
3093 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3094 }
3095 else
3096 tv1->vval.v_float = res;
3097 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003098#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 break;
3100
3101 case ISN_COMPARELIST:
3102 {
3103 typval_T *tv1 = STACK_TV_BOT(-2);
3104 typval_T *tv2 = STACK_TV_BOT(-1);
3105 list_T *arg1 = tv1->vval.v_list;
3106 list_T *arg2 = tv2->vval.v_list;
3107 int cmp = FALSE;
3108 int ic = iptr->isn_arg.op.op_ic;
3109
3110 switch (iptr->isn_arg.op.op_type)
3111 {
3112 case EXPR_EQUAL: cmp =
3113 list_equal(arg1, arg2, ic, FALSE); break;
3114 case EXPR_NEQUAL: cmp =
3115 !list_equal(arg1, arg2, ic, FALSE); break;
3116 case EXPR_IS: cmp = arg1 == arg2; break;
3117 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3118 default: cmp = 0; break;
3119 }
3120 --ectx.ec_stack.ga_len;
3121 clear_tv(tv1);
3122 clear_tv(tv2);
3123 tv1->v_type = VAR_BOOL;
3124 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3125 }
3126 break;
3127
3128 case ISN_COMPAREBLOB:
3129 {
3130 typval_T *tv1 = STACK_TV_BOT(-2);
3131 typval_T *tv2 = STACK_TV_BOT(-1);
3132 blob_T *arg1 = tv1->vval.v_blob;
3133 blob_T *arg2 = tv2->vval.v_blob;
3134 int cmp = FALSE;
3135
3136 switch (iptr->isn_arg.op.op_type)
3137 {
3138 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3139 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3140 case EXPR_IS: cmp = arg1 == arg2; break;
3141 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3142 default: cmp = 0; break;
3143 }
3144 --ectx.ec_stack.ga_len;
3145 clear_tv(tv1);
3146 clear_tv(tv2);
3147 tv1->v_type = VAR_BOOL;
3148 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3149 }
3150 break;
3151
3152 // TODO: handle separately
3153 case ISN_COMPARESTRING:
3154 case ISN_COMPAREDICT:
3155 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 case ISN_COMPAREANY:
3157 {
3158 typval_T *tv1 = STACK_TV_BOT(-2);
3159 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003160 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 int ic = iptr->isn_arg.op.op_ic;
3162
Bram Moolenaareb26f432020-09-14 16:50:05 +02003163 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003164 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 --ectx.ec_stack.ga_len;
3167 }
3168 break;
3169
3170 case ISN_ADDLIST:
3171 case ISN_ADDBLOB:
3172 {
3173 typval_T *tv1 = STACK_TV_BOT(-2);
3174 typval_T *tv2 = STACK_TV_BOT(-1);
3175
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003176 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 if (iptr->isn_type == ISN_ADDLIST)
3178 eval_addlist(tv1, tv2);
3179 else
3180 eval_addblob(tv1, tv2);
3181 clear_tv(tv2);
3182 --ectx.ec_stack.ga_len;
3183 }
3184 break;
3185
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003186 case ISN_LISTAPPEND:
3187 {
3188 typval_T *tv1 = STACK_TV_BOT(-2);
3189 typval_T *tv2 = STACK_TV_BOT(-1);
3190 list_T *l = tv1->vval.v_list;
3191
3192 // add an item to a list
3193 if (l == NULL)
3194 {
3195 SOURCING_LNUM = iptr->isn_lnum;
3196 emsg(_(e_cannot_add_to_null_list));
3197 goto on_error;
3198 }
3199 if (list_append_tv(l, tv2) == FAIL)
3200 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003201 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003202 --ectx.ec_stack.ga_len;
3203 }
3204 break;
3205
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003206 case ISN_BLOBAPPEND:
3207 {
3208 typval_T *tv1 = STACK_TV_BOT(-2);
3209 typval_T *tv2 = STACK_TV_BOT(-1);
3210 blob_T *b = tv1->vval.v_blob;
3211 int error = FALSE;
3212 varnumber_T n;
3213
3214 // add a number to a blob
3215 if (b == NULL)
3216 {
3217 SOURCING_LNUM = iptr->isn_lnum;
3218 emsg(_(e_cannot_add_to_null_blob));
3219 goto on_error;
3220 }
3221 n = tv_get_number_chk(tv2, &error);
3222 if (error)
3223 goto on_error;
3224 ga_append(&b->bv_ga, (int)n);
3225 --ectx.ec_stack.ga_len;
3226 }
3227 break;
3228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 // Computation with two arguments of unknown type
3230 case ISN_OPANY:
3231 {
3232 typval_T *tv1 = STACK_TV_BOT(-2);
3233 typval_T *tv2 = STACK_TV_BOT(-1);
3234 varnumber_T n1, n2;
3235#ifdef FEAT_FLOAT
3236 float_T f1 = 0, f2 = 0;
3237#endif
3238 int error = FALSE;
3239
3240 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3241 {
3242 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3243 {
3244 eval_addlist(tv1, tv2);
3245 clear_tv(tv2);
3246 --ectx.ec_stack.ga_len;
3247 break;
3248 }
3249 else if (tv1->v_type == VAR_BLOB
3250 && tv2->v_type == VAR_BLOB)
3251 {
3252 eval_addblob(tv1, tv2);
3253 clear_tv(tv2);
3254 --ectx.ec_stack.ga_len;
3255 break;
3256 }
3257 }
3258#ifdef FEAT_FLOAT
3259 if (tv1->v_type == VAR_FLOAT)
3260 {
3261 f1 = tv1->vval.v_float;
3262 n1 = 0;
3263 }
3264 else
3265#endif
3266 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003267 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 n1 = tv_get_number_chk(tv1, &error);
3269 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003270 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271#ifdef FEAT_FLOAT
3272 if (tv2->v_type == VAR_FLOAT)
3273 f1 = n1;
3274#endif
3275 }
3276#ifdef FEAT_FLOAT
3277 if (tv2->v_type == VAR_FLOAT)
3278 {
3279 f2 = tv2->vval.v_float;
3280 n2 = 0;
3281 }
3282 else
3283#endif
3284 {
3285 n2 = tv_get_number_chk(tv2, &error);
3286 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003287 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288#ifdef FEAT_FLOAT
3289 if (tv1->v_type == VAR_FLOAT)
3290 f2 = n2;
3291#endif
3292 }
3293#ifdef FEAT_FLOAT
3294 // if there is a float on either side the result is a float
3295 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3296 {
3297 switch (iptr->isn_arg.op.op_type)
3298 {
3299 case EXPR_MULT: f1 = f1 * f2; break;
3300 case EXPR_DIV: f1 = f1 / f2; break;
3301 case EXPR_SUB: f1 = f1 - f2; break;
3302 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003303 default: SOURCING_LNUM = iptr->isn_lnum;
3304 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003305 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 }
3307 clear_tv(tv1);
3308 clear_tv(tv2);
3309 tv1->v_type = VAR_FLOAT;
3310 tv1->vval.v_float = f1;
3311 --ectx.ec_stack.ga_len;
3312 }
3313 else
3314#endif
3315 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003316 int failed = FALSE;
3317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 switch (iptr->isn_arg.op.op_type)
3319 {
3320 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003321 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3322 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003323 goto on_error;
3324 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 case EXPR_SUB: n1 = n1 - n2; break;
3326 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003327 default: n1 = num_modulus(n1, n2, &failed);
3328 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003329 goto on_error;
3330 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 }
3332 clear_tv(tv1);
3333 clear_tv(tv2);
3334 tv1->v_type = VAR_NUMBER;
3335 tv1->vval.v_number = n1;
3336 --ectx.ec_stack.ga_len;
3337 }
3338 }
3339 break;
3340
3341 case ISN_CONCAT:
3342 {
3343 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3344 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3345 char_u *res;
3346
3347 res = concat_str(str1, str2);
3348 clear_tv(STACK_TV_BOT(-2));
3349 clear_tv(STACK_TV_BOT(-1));
3350 --ectx.ec_stack.ga_len;
3351 STACK_TV_BOT(-1)->vval.v_string = res;
3352 }
3353 break;
3354
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003355 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003356 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003357 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003358 int is_slice = iptr->isn_type == ISN_STRSLICE;
3359 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003360 char_u *res;
3361
3362 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003363 // string slice: string is at stack-3, first index at
3364 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003365 if (is_slice)
3366 {
3367 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003368 n1 = tv->vval.v_number;
3369 }
3370
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003371 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003372 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003373
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003374 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003375 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003376 if (is_slice)
3377 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003378 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003379 else
3380 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003381 // single character (including composing characters).
3382 // If the index is too big or negative the result is
3383 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003384 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003385 vim_free(tv->vval.v_string);
3386 tv->vval.v_string = res;
3387 }
3388 break;
3389
3390 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003391 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 {
Bram Moolenaared591872020-08-15 22:14:53 +02003393 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003395 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396
3397 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003398 // list slice: list is at stack-3, indexes at stack-2 and
3399 // stack-1
3400 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 list = tv->vval.v_list;
3402
3403 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003404 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003406
3407 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 {
Bram Moolenaared591872020-08-15 22:14:53 +02003409 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003410 n1 = tv->vval.v_number;
3411 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 }
Bram Moolenaared591872020-08-15 22:14:53 +02003413
3414 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003415 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003416 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003417 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3418 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003419 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 }
3421 break;
3422
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003423 case ISN_ANYINDEX:
3424 case ISN_ANYSLICE:
3425 {
3426 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3427 typval_T *var1, *var2;
3428 int res;
3429
3430 // index: composite is at stack-2, index at stack-1
3431 // slice: composite is at stack-3, indexes at stack-2 and
3432 // stack-1
3433 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003434 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003435 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3436 goto on_error;
3437 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3438 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003439 res = eval_index_inner(tv, is_slice, var1, var2,
3440 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003441 clear_tv(var1);
3442 if (is_slice)
3443 clear_tv(var2);
3444 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3445 if (res == FAIL)
3446 goto on_error;
3447 }
3448 break;
3449
Bram Moolenaar9af78762020-06-16 11:34:42 +02003450 case ISN_SLICE:
3451 {
3452 list_T *list;
3453 int count = iptr->isn_arg.number;
3454
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003455 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003456 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003457 list = tv->vval.v_list;
3458
3459 // no error for short list, expect it to be checked earlier
3460 if (list != NULL && list->lv_len >= count)
3461 {
3462 list_T *newlist = list_slice(list,
3463 count, list->lv_len - 1);
3464
3465 if (newlist != NULL)
3466 {
3467 list_unref(list);
3468 tv->vval.v_list = newlist;
3469 ++newlist->lv_refcount;
3470 }
3471 }
3472 }
3473 break;
3474
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003475 case ISN_GETITEM:
3476 {
3477 listitem_T *li;
3478 int index = iptr->isn_arg.number;
3479
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003480 // Get list item: list is at stack-1, push item.
3481 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003482 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003483 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003484
3485 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3486 goto failed;
3487 ++ectx.ec_stack.ga_len;
3488 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003489
3490 // Useful when used in unpack assignment. Reset at
3491 // ISN_DROP.
3492 where.wt_index = index + 1;
3493 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003494 }
3495 break;
3496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 case ISN_MEMBER:
3498 {
3499 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003500 char_u *key;
3501 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003502 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003503
3504 // dict member: dict is at stack-2, key at stack-1
3505 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003506 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003507 dict = tv->vval.v_dict;
3508
3509 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003510 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003511 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003512 if (key == NULL)
3513 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003514
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003515 if ((di = dict_find(dict, key, -1)) == NULL)
3516 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003518 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003519
3520 // If :silent! is used we will continue, make sure the
3521 // stack contents makes sense.
3522 clear_tv(tv);
3523 --ectx.ec_stack.ga_len;
3524 tv = STACK_TV_BOT(-1);
3525 clear_tv(tv);
3526 tv->v_type = VAR_NUMBER;
3527 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003528 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003529 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003530 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003531 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003532 // Clear the dict only after getting the item, to avoid
3533 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003534 tv = STACK_TV_BOT(-1);
3535 temp_tv = *tv;
3536 copy_tv(&di->di_tv, tv);
3537 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003538 }
3539 break;
3540
3541 // dict member with string key
3542 case ISN_STRINGMEMBER:
3543 {
3544 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003546 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547
3548 tv = STACK_TV_BOT(-1);
3549 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3550 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003551 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003553 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 }
3555 dict = tv->vval.v_dict;
3556
3557 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3558 == NULL)
3559 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003560 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003562 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003564 // Clear the dict after getting the item, to avoid that it
3565 // make the item invalid.
3566 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003568 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 }
3570 break;
3571
3572 case ISN_NEGATENR:
3573 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003574 if (tv->v_type != VAR_NUMBER
3575#ifdef FEAT_FLOAT
3576 && tv->v_type != VAR_FLOAT
3577#endif
3578 )
3579 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003580 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003581 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003582 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003583 }
3584#ifdef FEAT_FLOAT
3585 if (tv->v_type == VAR_FLOAT)
3586 tv->vval.v_float = -tv->vval.v_float;
3587 else
3588#endif
3589 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 break;
3591
3592 case ISN_CHECKNR:
3593 {
3594 int error = FALSE;
3595
3596 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003597 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003599 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 (void)tv_get_number_chk(tv, &error);
3601 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003602 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 }
3604 break;
3605
3606 case ISN_CHECKTYPE:
3607 {
3608 checktype_T *ct = &iptr->isn_arg.type;
3609
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003610 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003611 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003612 if (!where.wt_variable)
3613 where.wt_index = ct->ct_arg_idx;
3614 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003615 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003616 if (!where.wt_variable)
3617 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003618
3619 // number 0 is FALSE, number 1 is TRUE
3620 if (tv->v_type == VAR_NUMBER
3621 && ct->ct_type->tt_type == VAR_BOOL
3622 && (tv->vval.v_number == 0
3623 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003625 tv->v_type = VAR_BOOL;
3626 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003627 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 }
3629 }
3630 break;
3631
Bram Moolenaar9af78762020-06-16 11:34:42 +02003632 case ISN_CHECKLEN:
3633 {
3634 int min_len = iptr->isn_arg.checklen.cl_min_len;
3635 list_T *list = NULL;
3636
3637 tv = STACK_TV_BOT(-1);
3638 if (tv->v_type == VAR_LIST)
3639 list = tv->vval.v_list;
3640 if (list == NULL || list->lv_len < min_len
3641 || (list->lv_len > min_len
3642 && !iptr->isn_arg.checklen.cl_more_OK))
3643 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003644 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003645 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003646 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003647 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003648 }
3649 }
3650 break;
3651
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003652 case ISN_SETTYPE:
3653 {
3654 checktype_T *ct = &iptr->isn_arg.type;
3655
3656 tv = STACK_TV_BOT(-1);
3657 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3658 {
3659 free_type(tv->vval.v_dict->dv_type);
3660 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3661 }
3662 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3663 {
3664 free_type(tv->vval.v_list->lv_type);
3665 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3666 }
3667 }
3668 break;
3669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003671 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 {
3673 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003674 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675
3676 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003677 if (iptr->isn_type == ISN_2BOOL)
3678 {
3679 n = tv2bool(tv);
3680 if (iptr->isn_arg.number) // invert
3681 n = !n;
3682 }
3683 else
3684 {
3685 SOURCING_LNUM = iptr->isn_lnum;
3686 n = tv_get_bool_chk(tv, &error);
3687 if (error)
3688 goto on_error;
3689 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 clear_tv(tv);
3691 tv->v_type = VAR_BOOL;
3692 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3693 }
3694 break;
3695
3696 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003697 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003698 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003699 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3700 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3701 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 break;
3703
Bram Moolenaar08597872020-12-10 19:43:40 +01003704 case ISN_RANGE:
3705 {
3706 exarg_T ea;
3707 char *errormsg;
3708
Bram Moolenaarece0b872021-01-08 20:40:45 +01003709 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003710 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003711 ea.addr_type = ADDR_LINES;
3712 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003713 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003714 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003715 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003716
3717 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3718 goto failed;
3719 ++ectx.ec_stack.ga_len;
3720 tv = STACK_TV_BOT(-1);
3721 tv->v_type = VAR_NUMBER;
3722 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003723 if (ea.addr_count == 0)
3724 tv->vval.v_number = curwin->w_cursor.lnum;
3725 else
3726 tv->vval.v_number = ea.line2;
3727 }
3728 break;
3729
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003730 case ISN_PUT:
3731 {
3732 int regname = iptr->isn_arg.put.put_regname;
3733 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3734 char_u *expr = NULL;
3735 int dir = FORWARD;
3736
Bram Moolenaar08597872020-12-10 19:43:40 +01003737 if (lnum < -2)
3738 {
3739 // line number was put on the stack by ISN_RANGE
3740 tv = STACK_TV_BOT(-1);
3741 curwin->w_cursor.lnum = tv->vval.v_number;
3742 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3743 dir = BACKWARD;
3744 --ectx.ec_stack.ga_len;
3745 }
3746 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003747 // :put! above cursor
3748 dir = BACKWARD;
3749 else if (lnum >= 0)
3750 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003751
3752 if (regname == '=')
3753 {
3754 tv = STACK_TV_BOT(-1);
3755 if (tv->v_type == VAR_STRING)
3756 expr = tv->vval.v_string;
3757 else
3758 {
3759 expr = typval2string(tv, TRUE); // allocates value
3760 clear_tv(tv);
3761 }
3762 --ectx.ec_stack.ga_len;
3763 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003764 check_cursor();
3765 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3766 vim_free(expr);
3767 }
3768 break;
3769
Bram Moolenaar02194d22020-10-24 23:08:38 +02003770 case ISN_CMDMOD:
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003771 funclocal.floc_save_cmdmod = cmdmod;
3772 funclocal.floc_restore_cmdmod = TRUE;
3773 funclocal.floc_restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003774 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3775 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003776 break;
3777
Bram Moolenaar02194d22020-10-24 23:08:38 +02003778 case ISN_CMDMOD_REV:
3779 // filter regprog is owned by the instruction, don't free it
3780 cmdmod.cmod_filter_regmatch.regprog = NULL;
3781 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003782 cmdmod = funclocal.floc_save_cmdmod;
3783 funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003784 break;
3785
Bram Moolenaar792f7862020-11-23 08:31:18 +01003786 case ISN_UNPACK:
3787 {
3788 int count = iptr->isn_arg.unpack.unp_count;
3789 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3790 list_T *l;
3791 listitem_T *li;
3792 int i;
3793
3794 // Check there is a valid list to unpack.
3795 tv = STACK_TV_BOT(-1);
3796 if (tv->v_type != VAR_LIST)
3797 {
3798 SOURCING_LNUM = iptr->isn_lnum;
3799 emsg(_(e_for_argument_must_be_sequence_of_lists));
3800 goto on_error;
3801 }
3802 l = tv->vval.v_list;
3803 if (l == NULL
3804 || l->lv_len < (semicolon ? count - 1 : count))
3805 {
3806 SOURCING_LNUM = iptr->isn_lnum;
3807 emsg(_(e_list_value_does_not_have_enough_items));
3808 goto on_error;
3809 }
3810 else if (!semicolon && l->lv_len > count)
3811 {
3812 SOURCING_LNUM = iptr->isn_lnum;
3813 emsg(_(e_list_value_has_more_items_than_targets));
3814 goto on_error;
3815 }
3816
3817 CHECK_LIST_MATERIALIZE(l);
3818 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3819 goto failed;
3820 ectx.ec_stack.ga_len += count - 1;
3821
3822 // Variable after semicolon gets a list with the remaining
3823 // items.
3824 if (semicolon)
3825 {
3826 list_T *rem_list =
3827 list_alloc_with_items(l->lv_len - count + 1);
3828
3829 if (rem_list == NULL)
3830 goto failed;
3831 tv = STACK_TV_BOT(-count);
3832 tv->vval.v_list = rem_list;
3833 ++rem_list->lv_refcount;
3834 tv->v_lock = 0;
3835 li = l->lv_first;
3836 for (i = 0; i < count - 1; ++i)
3837 li = li->li_next;
3838 for (i = 0; li != NULL; ++i)
3839 {
3840 list_set_item(rem_list, i, &li->li_tv);
3841 li = li->li_next;
3842 }
3843 --count;
3844 }
3845
3846 // Produce the values in reverse order, first item last.
3847 li = l->lv_first;
3848 for (i = 0; i < count; ++i)
3849 {
3850 tv = STACK_TV_BOT(-i - 1);
3851 copy_tv(&li->li_tv, tv);
3852 li = li->li_next;
3853 }
3854
3855 list_unref(l);
3856 }
3857 break;
3858
Bram Moolenaarb2049902021-01-24 12:53:53 +01003859 case ISN_PROF_START:
3860 case ISN_PROF_END:
3861 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003862#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003863 funccall_T cookie;
3864 ufunc_T *cur_ufunc =
3865 (((dfunc_T *)def_functions.ga_data)
3866 + ectx.ec_dfunc_idx)->df_ufunc;
3867
3868 cookie.func = cur_ufunc;
3869 if (iptr->isn_type == ISN_PROF_START)
3870 {
3871 func_line_start(&cookie, iptr->isn_lnum);
3872 // if we get here the instruction is executed
3873 func_line_exec(&cookie);
3874 }
3875 else
3876 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003877#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003878 }
3879 break;
3880
Bram Moolenaar389df252020-07-09 21:20:47 +02003881 case ISN_SHUFFLE:
3882 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003883 typval_T tmp_tv;
3884 int item = iptr->isn_arg.shuffle.shfl_item;
3885 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003886
3887 tmp_tv = *STACK_TV_BOT(-item);
3888 for ( ; up > 0 && item > 1; --up)
3889 {
3890 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3891 --item;
3892 }
3893 *STACK_TV_BOT(-item) = tmp_tv;
3894 }
3895 break;
3896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 case ISN_DROP:
3898 --ectx.ec_stack.ga_len;
3899 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003900 where.wt_index = 0;
3901 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 break;
3903 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003904 continue;
3905
Bram Moolenaard032f342020-07-18 18:13:02 +02003906func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003907 // Restore previous function. If the frame pointer is where we started
3908 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003909 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003910 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003911
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003912 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003913 // only fails when out of memory
3914 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003915 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003916
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003917on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003918 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003919 // If "emsg_silent" is set then ignore the error, unless it was set
3920 // when calling the function.
3921 if (did_emsg_cumul + did_emsg == did_emsg_before
3922 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003923 {
3924 // If a sequence of instructions causes an error while ":silent!"
3925 // was used, restore the stack length and jump ahead to restoring
3926 // the cmdmod.
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003927 if (funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003928 {
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003929 while (ectx.ec_stack.ga_len
3930 > funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003931 {
3932 --ectx.ec_stack.ga_len;
3933 clear_tv(STACK_TV_BOT(0));
3934 }
3935 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3936 ++ectx.ec_iidx;
3937 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003938 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003939 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003940on_fatal_error:
3941 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003942 // If we are not inside a try-catch started here, abort execution.
3943 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003944 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 }
3946
3947done:
3948 // function finished, get result from the stack.
3949 tv = STACK_TV_BOT(-1);
3950 *rettv = *tv;
3951 tv->v_type = VAR_UNKNOWN;
3952 ret = OK;
3953
3954failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003955 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003956 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003957 func_return(&funclocal, &ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003958
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003959 // Deal with any remaining closures, they may be in use somewhere.
3960 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003961 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003962 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003963 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3964 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003965
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003966 estack_pop();
3967 current_sctx = save_current_sctx;
3968
Bram Moolenaarc970e422021-03-17 15:03:04 +01003969 // TODO: when is it safe to delete the function if it is no longer used?
3970 --ufunc->uf_calls;
3971
Bram Moolenaar352134b2020-10-17 22:04:08 +02003972 if (*msg_list != NULL && saved_msg_list != NULL)
3973 {
3974 msglist_T **plist = saved_msg_list;
3975
3976 // Append entries from the current msg_list (uncaught exceptions) to
3977 // the saved msg_list.
3978 while (*plist != NULL)
3979 plist = &(*plist)->next;
3980
3981 *plist = *msg_list;
3982 }
3983 msg_list = saved_msg_list;
3984
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003985 if (funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02003986 {
3987 cmdmod.cmod_filter_regmatch.regprog = NULL;
3988 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003989 cmdmod = funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003990 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003991 emsg_silent_def = save_emsg_silent_def;
3992 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003993
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003994failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003995 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3997 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004000 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004001
Bram Moolenaar0186e582021-01-10 18:33:11 +01004002 while (ectx.ec_outer != NULL)
4003 {
4004 outer_T *up = ectx.ec_outer->out_up_is_copy
4005 ? NULL : ectx.ec_outer->out_up;
4006
4007 vim_free(ectx.ec_outer);
4008 ectx.ec_outer = up;
4009 }
4010
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004011 // Not sure if this is necessary.
4012 suppress_errthrow = save_suppress_errthrow;
4013
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004014 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004015 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004016 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004017 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 return ret;
4019}
4020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004022 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01004023 * We don't really need this at runtime, but we do have tests that require it,
4024 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 */
4026 void
4027ex_disassemble(exarg_T *eap)
4028{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004029 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004030 char_u *fname;
4031 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 dfunc_T *dfunc;
4033 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004034 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 int current;
4036 int line_idx = 0;
4037 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004038 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039
Bram Moolenaarbfd65582020-07-13 18:18:00 +02004040 if (STRNCMP(arg, "<lambda>", 8) == 0)
4041 {
4042 arg += 8;
4043 (void)getdigits(&arg);
4044 fname = vim_strnsave(eap->arg, arg - eap->arg);
4045 }
4046 else
4047 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004048 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004049 if (fname == NULL)
4050 {
4051 semsg(_(e_invarg2), eap->arg);
4052 return;
4053 }
4054
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004055 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004056 if (ufunc == NULL)
4057 {
4058 char_u *p = untrans_function_name(fname);
4059
4060 if (p != NULL)
4061 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004062 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004063 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004064 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 if (ufunc == NULL)
4066 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004067 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 return;
4069 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01004070 if (func_needs_compiling(ufunc, eap->forceit)
4071 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02004072 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004073 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004075 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 return;
4077 }
4078 if (ufunc->uf_name_exp != NULL)
4079 msg((char *)ufunc->uf_name_exp);
4080 else
4081 msg((char *)ufunc->uf_name);
4082
4083 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004084#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004085 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
4086 instr_count = eap->forceit ? dfunc->df_instr_prof_count
4087 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004088#else
4089 instr = dfunc->df_instr;
4090 instr_count = dfunc->df_instr_count;
4091#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004092 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 {
4094 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004095 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096
4097 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
4098 {
4099 if (current > prev_current)
4100 {
4101 msg_puts("\n\n");
4102 prev_current = current;
4103 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004104 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4105 if (line != NULL)
4106 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 }
4108
4109 switch (iptr->isn_type)
4110 {
4111 case ISN_EXEC:
4112 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
4113 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004114 case ISN_EXECCONCAT:
4115 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004116 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004117 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 case ISN_ECHO:
4119 {
4120 echo_T *echo = &iptr->isn_arg.echo;
4121
4122 smsg("%4d %s %d", current,
4123 echo->echo_with_white ? "ECHO" : "ECHON",
4124 echo->echo_count);
4125 }
4126 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004127 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01004128 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004129 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01004130 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004131 case ISN_ECHOMSG:
4132 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004133 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004134 break;
4135 case ISN_ECHOERR:
4136 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004137 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004138 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004140 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004141 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004142 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004143 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004144 + STACK_FRAME_SIZE));
4145 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004146 smsg("%4d LOAD $%lld", current,
4147 (varnumber_T)(iptr->isn_arg.number));
4148 }
4149 break;
4150 case ISN_LOADOUTER:
4151 {
4152 if (iptr->isn_arg.number < 0)
4153 smsg("%4d LOADOUTER level %d arg[%d]", current,
4154 iptr->isn_arg.outer.outer_depth,
4155 iptr->isn_arg.outer.outer_idx
4156 + STACK_FRAME_SIZE);
4157 else
4158 smsg("%4d LOADOUTER level %d $%d", current,
4159 iptr->isn_arg.outer.outer_depth,
4160 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 break;
4163 case ISN_LOADV:
4164 smsg("%4d LOADV v:%s", current,
4165 get_vim_var_name(iptr->isn_arg.number));
4166 break;
4167 case ISN_LOADSCRIPT:
4168 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004169 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4170 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004172 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004174 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4175 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004176 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004177 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 }
4179 break;
4180 case ISN_LOADS:
4181 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004182 scriptitem_T *si = SCRIPT_ITEM(
4183 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184
4185 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004186 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 }
4188 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004189 case ISN_LOADAUTO:
4190 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4191 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 case ISN_LOADG:
4193 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4194 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004195 case ISN_LOADB:
4196 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4197 break;
4198 case ISN_LOADW:
4199 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4200 break;
4201 case ISN_LOADT:
4202 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4203 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004204 case ISN_LOADGDICT:
4205 smsg("%4d LOAD g:", current);
4206 break;
4207 case ISN_LOADBDICT:
4208 smsg("%4d LOAD b:", current);
4209 break;
4210 case ISN_LOADWDICT:
4211 smsg("%4d LOAD w:", current);
4212 break;
4213 case ISN_LOADTDICT:
4214 smsg("%4d LOAD t:", current);
4215 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 case ISN_LOADOPT:
4217 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4218 break;
4219 case ISN_LOADENV:
4220 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4221 break;
4222 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004223 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 break;
4225
4226 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004227 if (iptr->isn_arg.number < 0)
4228 smsg("%4d STORE arg[%lld]", current,
4229 iptr->isn_arg.number + STACK_FRAME_SIZE);
4230 else
4231 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4232 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004233 case ISN_STOREOUTER:
4234 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004235 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004236 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4237 iptr->isn_arg.outer.outer_depth,
4238 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004239 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004240 smsg("%4d STOREOUTER level %d $%d", current,
4241 iptr->isn_arg.outer.outer_depth,
4242 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004245 case ISN_STOREV:
4246 smsg("%4d STOREV v:%s", current,
4247 get_vim_var_name(iptr->isn_arg.number));
4248 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004249 case ISN_STOREAUTO:
4250 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4251 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004253 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4254 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004255 case ISN_STOREB:
4256 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4257 break;
4258 case ISN_STOREW:
4259 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4260 break;
4261 case ISN_STORET:
4262 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4263 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004264 case ISN_STORES:
4265 {
4266 scriptitem_T *si = SCRIPT_ITEM(
4267 iptr->isn_arg.loadstore.ls_sid);
4268
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004269 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004270 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 break;
4273 case ISN_STORESCRIPT:
4274 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004275 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4276 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004278 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004280 smsg("%4d STORESCRIPT %s-%d in %s", current,
4281 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004282 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004283 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 }
4285 break;
4286 case ISN_STOREOPT:
4287 smsg("%4d STOREOPT &%s", current,
4288 iptr->isn_arg.storeopt.so_name);
4289 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004290 case ISN_STOREENV:
4291 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4292 break;
4293 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004294 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004295 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 case ISN_STORENR:
4297 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004298 iptr->isn_arg.storenr.stnr_val,
4299 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 break;
4301
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004302 case ISN_STOREINDEX:
4303 switch (iptr->isn_arg.vartype)
4304 {
4305 case VAR_LIST:
4306 smsg("%4d STORELIST", current);
4307 break;
4308 case VAR_DICT:
4309 smsg("%4d STOREDICT", current);
4310 break;
4311 case VAR_ANY:
4312 smsg("%4d STOREINDEX", current);
4313 break;
4314 default: break;
4315 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004316 break;
4317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 // constants
4319 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004320 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004321 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 break;
4323 case ISN_PUSHBOOL:
4324 case ISN_PUSHSPEC:
4325 smsg("%4d PUSH %s", current,
4326 get_var_special_name(iptr->isn_arg.number));
4327 break;
4328 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004329#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004331#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 break;
4333 case ISN_PUSHS:
4334 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4335 break;
4336 case ISN_PUSHBLOB:
4337 {
4338 char_u *r;
4339 char_u numbuf[NUMBUFLEN];
4340 char_u *tofree;
4341
4342 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004343 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 vim_free(tofree);
4345 }
4346 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004347 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004348 {
4349 char *name = (char *)iptr->isn_arg.string;
4350
4351 smsg("%4d PUSHFUNC \"%s\"", current,
4352 name == NULL ? "[none]" : name);
4353 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004354 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004355 case ISN_PUSHCHANNEL:
4356#ifdef FEAT_JOB_CHANNEL
4357 {
4358 channel_T *channel = iptr->isn_arg.channel;
4359
4360 smsg("%4d PUSHCHANNEL %d", current,
4361 channel == NULL ? 0 : channel->ch_id);
4362 }
4363#endif
4364 break;
4365 case ISN_PUSHJOB:
4366#ifdef FEAT_JOB_CHANNEL
4367 {
4368 typval_T tv;
4369 char_u *name;
4370
4371 tv.v_type = VAR_JOB;
4372 tv.vval.v_job = iptr->isn_arg.job;
4373 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004374 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004375 }
4376#endif
4377 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 case ISN_PUSHEXC:
4379 smsg("%4d PUSH v:exception", current);
4380 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004381 case ISN_UNLET:
4382 smsg("%4d UNLET%s %s", current,
4383 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4384 iptr->isn_arg.unlet.ul_name);
4385 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004386 case ISN_UNLETENV:
4387 smsg("%4d UNLETENV%s $%s", current,
4388 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4389 iptr->isn_arg.unlet.ul_name);
4390 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004391 case ISN_UNLETINDEX:
4392 smsg("%4d UNLETINDEX", current);
4393 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004394 case ISN_UNLETRANGE:
4395 smsg("%4d UNLETRANGE", current);
4396 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004397 case ISN_LOCKCONST:
4398 smsg("%4d LOCKCONST", current);
4399 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004401 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004402 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 break;
4404 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004405 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004406 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 break;
4408
4409 // function call
4410 case ISN_BCALL:
4411 {
4412 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4413
4414 smsg("%4d BCALL %s(argc %d)", current,
4415 internal_func_name(cbfunc->cbf_idx),
4416 cbfunc->cbf_argcount);
4417 }
4418 break;
4419 case ISN_DCALL:
4420 {
4421 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4422 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4423 + cdfunc->cdf_idx;
4424
4425 smsg("%4d DCALL %s(argc %d)", current,
4426 df->df_ufunc->uf_name_exp != NULL
4427 ? df->df_ufunc->uf_name_exp
4428 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4429 }
4430 break;
4431 case ISN_UCALL:
4432 {
4433 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4434
4435 smsg("%4d UCALL %s(argc %d)", current,
4436 cufunc->cuf_name, cufunc->cuf_argcount);
4437 }
4438 break;
4439 case ISN_PCALL:
4440 {
4441 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4442
4443 smsg("%4d PCALL%s (argc %d)", current,
4444 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4445 }
4446 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004447 case ISN_PCALL_END:
4448 smsg("%4d PCALL end", current);
4449 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 case ISN_RETURN:
4451 smsg("%4d RETURN", current);
4452 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004453 case ISN_RETURN_ZERO:
4454 smsg("%4d RETURN 0", current);
4455 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 case ISN_FUNCREF:
4457 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004458 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004460 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004462 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 }
4464 break;
4465
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004466 case ISN_NEWFUNC:
4467 {
4468 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4469
4470 smsg("%4d NEWFUNC %s %s", current,
4471 newfunc->nf_lambda, newfunc->nf_global);
4472 }
4473 break;
4474
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004475 case ISN_DEF:
4476 {
4477 char_u *name = iptr->isn_arg.string;
4478
4479 smsg("%4d DEF %s", current,
4480 name == NULL ? (char_u *)"" : name);
4481 }
4482 break;
4483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 case ISN_JUMP:
4485 {
4486 char *when = "?";
4487
4488 switch (iptr->isn_arg.jump.jump_when)
4489 {
4490 case JUMP_ALWAYS:
4491 when = "JUMP";
4492 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 case JUMP_AND_KEEP_IF_TRUE:
4494 when = "JUMP_AND_KEEP_IF_TRUE";
4495 break;
4496 case JUMP_IF_FALSE:
4497 when = "JUMP_IF_FALSE";
4498 break;
4499 case JUMP_AND_KEEP_IF_FALSE:
4500 when = "JUMP_AND_KEEP_IF_FALSE";
4501 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004502 case JUMP_IF_COND_FALSE:
4503 when = "JUMP_IF_COND_FALSE";
4504 break;
4505 case JUMP_IF_COND_TRUE:
4506 when = "JUMP_IF_COND_TRUE";
4507 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004509 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 iptr->isn_arg.jump.jump_where);
4511 }
4512 break;
4513
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004514 case ISN_JUMP_IF_ARG_SET:
4515 smsg("%4d JUMP_IF_ARG_SET arg[%d] -> %d", current,
4516 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4517 iptr->isn_arg.jump.jump_where);
4518 break;
4519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 case ISN_FOR:
4521 {
4522 forloop_T *forloop = &iptr->isn_arg.forloop;
4523
4524 smsg("%4d FOR $%d -> %d", current,
4525 forloop->for_idx, forloop->for_end);
4526 }
4527 break;
4528
4529 case ISN_TRY:
4530 {
4531 try_T *try = &iptr->isn_arg.try;
4532
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004533 if (try->try_ref->try_finally == 0)
4534 smsg("%4d TRY catch -> %d, endtry -> %d",
4535 current,
4536 try->try_ref->try_catch,
4537 try->try_ref->try_endtry);
4538 else
4539 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4540 current,
4541 try->try_ref->try_catch,
4542 try->try_ref->try_finally,
4543 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 }
4545 break;
4546 case ISN_CATCH:
4547 // TODO
4548 smsg("%4d CATCH", current);
4549 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004550 case ISN_TRYCONT:
4551 {
4552 trycont_T *trycont = &iptr->isn_arg.trycont;
4553
4554 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4555 trycont->tct_levels,
4556 trycont->tct_levels == 1 ? "" : "s",
4557 trycont->tct_where);
4558 }
4559 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004560 case ISN_FINALLY:
4561 smsg("%4d FINALLY", current);
4562 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 case ISN_ENDTRY:
4564 smsg("%4d ENDTRY", current);
4565 break;
4566 case ISN_THROW:
4567 smsg("%4d THROW", current);
4568 break;
4569
4570 // expression operations on number
4571 case ISN_OPNR:
4572 case ISN_OPFLOAT:
4573 case ISN_OPANY:
4574 {
4575 char *what;
4576 char *ins;
4577
4578 switch (iptr->isn_arg.op.op_type)
4579 {
4580 case EXPR_MULT: what = "*"; break;
4581 case EXPR_DIV: what = "/"; break;
4582 case EXPR_REM: what = "%"; break;
4583 case EXPR_SUB: what = "-"; break;
4584 case EXPR_ADD: what = "+"; break;
4585 default: what = "???"; break;
4586 }
4587 switch (iptr->isn_type)
4588 {
4589 case ISN_OPNR: ins = "OPNR"; break;
4590 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4591 case ISN_OPANY: ins = "OPANY"; break;
4592 default: ins = "???"; break;
4593 }
4594 smsg("%4d %s %s", current, ins, what);
4595 }
4596 break;
4597
4598 case ISN_COMPAREBOOL:
4599 case ISN_COMPARESPECIAL:
4600 case ISN_COMPARENR:
4601 case ISN_COMPAREFLOAT:
4602 case ISN_COMPARESTRING:
4603 case ISN_COMPAREBLOB:
4604 case ISN_COMPARELIST:
4605 case ISN_COMPAREDICT:
4606 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 case ISN_COMPAREANY:
4608 {
4609 char *p;
4610 char buf[10];
4611 char *type;
4612
4613 switch (iptr->isn_arg.op.op_type)
4614 {
4615 case EXPR_EQUAL: p = "=="; break;
4616 case EXPR_NEQUAL: p = "!="; break;
4617 case EXPR_GREATER: p = ">"; break;
4618 case EXPR_GEQUAL: p = ">="; break;
4619 case EXPR_SMALLER: p = "<"; break;
4620 case EXPR_SEQUAL: p = "<="; break;
4621 case EXPR_MATCH: p = "=~"; break;
4622 case EXPR_IS: p = "is"; break;
4623 case EXPR_ISNOT: p = "isnot"; break;
4624 case EXPR_NOMATCH: p = "!~"; break;
4625 default: p = "???"; break;
4626 }
4627 STRCPY(buf, p);
4628 if (iptr->isn_arg.op.op_ic == TRUE)
4629 strcat(buf, "?");
4630 switch(iptr->isn_type)
4631 {
4632 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4633 case ISN_COMPARESPECIAL:
4634 type = "COMPARESPECIAL"; break;
4635 case ISN_COMPARENR: type = "COMPARENR"; break;
4636 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4637 case ISN_COMPARESTRING:
4638 type = "COMPARESTRING"; break;
4639 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4640 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4641 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4642 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4644 default: type = "???"; break;
4645 }
4646
4647 smsg("%4d %s %s", current, type, buf);
4648 }
4649 break;
4650
4651 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4652 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4653
4654 // expression operations
4655 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004656 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004657 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004658 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004659 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004660 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004661 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004662 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4663 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004664 case ISN_SLICE: smsg("%4d SLICE %lld",
4665 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004666 case ISN_GETITEM: smsg("%4d ITEM %lld",
4667 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004668 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4669 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 iptr->isn_arg.string); break;
4671 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4672
4673 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004674 case ISN_CHECKTYPE:
4675 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004676 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004677 char *tofree;
4678
Bram Moolenaare32e5162021-01-21 20:21:29 +01004679 if (ct->ct_arg_idx == 0)
4680 smsg("%4d CHECKTYPE %s stack[%d]", current,
4681 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004682 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004683 else
4684 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4685 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004686 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004687 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004688 vim_free(tofree);
4689 break;
4690 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004691 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4692 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4693 iptr->isn_arg.checklen.cl_min_len);
4694 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004695 case ISN_SETTYPE:
4696 {
4697 char *tofree;
4698
4699 smsg("%4d SETTYPE %s", current,
4700 type_name(iptr->isn_arg.type.ct_type, &tofree));
4701 vim_free(tofree);
4702 break;
4703 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004704 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 case ISN_2BOOL: if (iptr->isn_arg.number)
4706 smsg("%4d INVERT (!val)", current);
4707 else
4708 smsg("%4d 2BOOL (!!val)", current);
4709 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004710 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004711 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004712 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004713 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004714 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004715 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004716 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4717 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004718 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004719 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4720 smsg("%4d PUT %c above range",
4721 current, iptr->isn_arg.put.put_regname);
4722 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4723 smsg("%4d PUT %c range",
4724 current, iptr->isn_arg.put.put_regname);
4725 else
4726 smsg("%4d PUT %c %ld", current,
4727 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004728 (long)iptr->isn_arg.put.put_lnum);
4729 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
Bram Moolenaar02194d22020-10-24 23:08:38 +02004731 // TODO: summarize modifiers
4732 case ISN_CMDMOD:
4733 {
4734 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004735 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004736 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4737
4738 buf = alloc(len + 1);
4739 if (buf != NULL)
4740 {
4741 (void)produce_cmdmods(
4742 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4743 smsg("%4d CMDMOD %s", current, buf);
4744 vim_free(buf);
4745 }
4746 break;
4747 }
4748 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004749
Bram Moolenaarb2049902021-01-24 12:53:53 +01004750 case ISN_PROF_START:
4751 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4752 break;
4753
4754 case ISN_PROF_END:
4755 smsg("%4d PROFILE END", current);
4756 break;
4757
Bram Moolenaar792f7862020-11-23 08:31:18 +01004758 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4759 iptr->isn_arg.unpack.unp_count,
4760 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4761 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004762 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4763 iptr->isn_arg.shuffle.shfl_item,
4764 iptr->isn_arg.shuffle.shfl_up);
4765 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 case ISN_DROP: smsg("%4d DROP", current); break;
4767 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004768
4769 out_flush(); // output one line at a time
4770 ui_breakcheck();
4771 if (got_int)
4772 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774}
4775
4776/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004777 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 * list, etc. Mostly like what JavaScript does, except that empty list and
4779 * empty dictionary are FALSE.
4780 */
4781 int
4782tv2bool(typval_T *tv)
4783{
4784 switch (tv->v_type)
4785 {
4786 case VAR_NUMBER:
4787 return tv->vval.v_number != 0;
4788 case VAR_FLOAT:
4789#ifdef FEAT_FLOAT
4790 return tv->vval.v_float != 0.0;
4791#else
4792 break;
4793#endif
4794 case VAR_PARTIAL:
4795 return tv->vval.v_partial != NULL;
4796 case VAR_FUNC:
4797 case VAR_STRING:
4798 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4799 case VAR_LIST:
4800 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4801 case VAR_DICT:
4802 return tv->vval.v_dict != NULL
4803 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4804 case VAR_BOOL:
4805 case VAR_SPECIAL:
4806 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4807 case VAR_JOB:
4808#ifdef FEAT_JOB_CHANNEL
4809 return tv->vval.v_job != NULL;
4810#else
4811 break;
4812#endif
4813 case VAR_CHANNEL:
4814#ifdef FEAT_JOB_CHANNEL
4815 return tv->vval.v_channel != NULL;
4816#else
4817 break;
4818#endif
4819 case VAR_BLOB:
4820 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4821 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004822 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 case VAR_VOID:
4824 break;
4825 }
4826 return FALSE;
4827}
4828
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004829 void
4830emsg_using_string_as(typval_T *tv, int as_number)
4831{
4832 semsg(_(as_number ? e_using_string_as_number_str
4833 : e_using_string_as_bool_str),
4834 tv->vval.v_string == NULL
4835 ? (char_u *)"" : tv->vval.v_string);
4836}
4837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838/*
4839 * If "tv" is a string give an error and return FAIL.
4840 */
4841 int
4842check_not_string(typval_T *tv)
4843{
4844 if (tv->v_type == VAR_STRING)
4845 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004846 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 clear_tv(tv);
4848 return FAIL;
4849 }
4850 return OK;
4851}
4852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853
4854#endif // FEAT_EVAL