blob: 81041bbeae137a09c8fbcb5b9683796c21654d34 [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 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001945 SOURCING_LNUM = iptr->isn_lnum;
1946 if (var_check_permission(di, name) == FAIL)
1947 goto on_error;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001948 clear_tv(&di->di_tv);
1949 di->di_tv = *STACK_TV_BOT(0);
1950 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001951 }
1952 break;
1953
1954 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 case ISN_STORESCRIPT:
1956 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001957 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1958 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001960 sv = get_script_svar(sref, &ectx);
1961 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001962 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 --ectx.ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001964
1965 // "const" and "final" are checked at compile time, locking
1966 // the value needs to be checked here.
1967 SOURCING_LNUM = iptr->isn_lnum;
1968 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
1969 goto on_error;
1970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 clear_tv(sv->sv_tv);
1972 *sv->sv_tv = *STACK_TV_BOT(0);
1973 }
1974 break;
1975
1976 // store option
1977 case ISN_STOREOPT:
1978 {
1979 long n = 0;
1980 char_u *s = NULL;
1981 char *msg;
1982
1983 --ectx.ec_stack.ga_len;
1984 tv = STACK_TV_BOT(0);
1985 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001986 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001988 if (s == NULL)
1989 s = (char_u *)"";
1990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001992 // must be VAR_NUMBER, CHECKTYPE makes sure
1993 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1995 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001996 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 if (msg != NULL)
1998 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001999 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 }
2004 break;
2005
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002006 // store $ENV
2007 case ISN_STOREENV:
2008 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002009 tv = STACK_TV_BOT(0);
2010 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2011 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002012 break;
2013
2014 // store @r
2015 case ISN_STOREREG:
2016 {
2017 int reg = iptr->isn_arg.number;
2018
2019 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002020 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002021 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002022 tv_get_string(tv), -1, FALSE);
2023 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002024 }
2025 break;
2026
2027 // store v: variable
2028 case ISN_STOREV:
2029 --ectx.ec_stack.ga_len;
2030 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2031 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002032 // should not happen, type is checked when compiling
2033 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002034 break;
2035
Bram Moolenaard3aac292020-04-19 14:32:17 +02002036 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002038 case ISN_STOREB:
2039 case ISN_STOREW:
2040 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002042 dictitem_T *di;
2043 hashtab_T *ht;
2044 char_u *name = iptr->isn_arg.string + 2;
2045
Bram Moolenaard3aac292020-04-19 14:32:17 +02002046 switch (iptr->isn_type)
2047 {
2048 case ISN_STOREG:
2049 ht = get_globvar_ht();
2050 break;
2051 case ISN_STOREB:
2052 ht = &curbuf->b_vars->dv_hashtab;
2053 break;
2054 case ISN_STOREW:
2055 ht = &curwin->w_vars->dv_hashtab;
2056 break;
2057 case ISN_STORET:
2058 ht = &curtab->tp_vars->dv_hashtab;
2059 break;
2060 default: // Cannot reach here
2061 goto failed;
2062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063
2064 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002065 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002067 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 else
2069 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002070 SOURCING_LNUM = iptr->isn_lnum;
2071 if (var_check_permission(di, name) == FAIL)
2072 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 clear_tv(&di->di_tv);
2074 di->di_tv = *STACK_TV_BOT(0);
2075 }
2076 }
2077 break;
2078
Bram Moolenaar03290b82020-12-19 16:30:44 +01002079 // store an autoload variable
2080 case ISN_STOREAUTO:
2081 SOURCING_LNUM = iptr->isn_lnum;
2082 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2083 clear_tv(STACK_TV_BOT(-1));
2084 --ectx.ec_stack.ga_len;
2085 break;
2086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 // store number in local variable
2088 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002089 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 clear_tv(tv);
2091 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002092 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 break;
2094
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002095 // store value in list or dict variable
2096 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002097 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002098 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002099 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002100 typval_T *tv_dest = STACK_TV_BOT(-1);
2101 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002102
Bram Moolenaar752fc692021-01-04 21:57:11 +01002103 // Stack contains:
2104 // -3 value to be stored
2105 // -2 index
2106 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002107 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002108 SOURCING_LNUM = iptr->isn_lnum;
2109 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002110 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002111 dest_type = tv_dest->v_type;
2112 if (dest_type == VAR_DICT)
2113 status = do_2string(tv_idx, TRUE);
2114 else if (dest_type == VAR_LIST
2115 && tv_idx->v_type != VAR_NUMBER)
2116 {
2117 emsg(_(e_number_exp));
2118 status = FAIL;
2119 }
2120 }
2121 else if (dest_type != tv_dest->v_type)
2122 {
2123 // just in case, should be OK
2124 semsg(_(e_expected_str_but_got_str),
2125 vartype_name(dest_type),
2126 vartype_name(tv_dest->v_type));
2127 status = FAIL;
2128 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002129
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002130 if (status == OK && dest_type == VAR_LIST)
2131 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002132 long lidx = (long)tv_idx->vval.v_number;
2133 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002134
2135 if (list == NULL)
2136 {
2137 emsg(_(e_list_not_set));
2138 goto on_error;
2139 }
2140 if (lidx < 0 && list->lv_len + lidx >= 0)
2141 // negative index is relative to the end
2142 lidx = list->lv_len + lidx;
2143 if (lidx < 0 || lidx > list->lv_len)
2144 {
2145 semsg(_(e_listidx), lidx);
2146 goto on_error;
2147 }
2148 if (lidx < list->lv_len)
2149 {
2150 listitem_T *li = list_find(list, lidx);
2151
2152 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002153 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002154 goto on_error;
2155 // overwrite existing list item
2156 clear_tv(&li->li_tv);
2157 li->li_tv = *tv;
2158 }
2159 else
2160 {
2161 if (error_if_locked(list->lv_lock,
2162 e_cannot_change_list))
2163 goto on_error;
2164 // append to list, only fails when out of memory
2165 if (list_append_tv(list, tv) == FAIL)
2166 goto failed;
2167 clear_tv(tv);
2168 }
2169 }
2170 else if (status == OK && dest_type == VAR_DICT)
2171 {
2172 char_u *key = tv_idx->vval.v_string;
2173 dict_T *dict = tv_dest->vval.v_dict;
2174 dictitem_T *di;
2175
2176 SOURCING_LNUM = iptr->isn_lnum;
2177 if (dict == NULL)
2178 {
2179 emsg(_(e_dictionary_not_set));
2180 goto on_error;
2181 }
2182 if (key == NULL)
2183 key = (char_u *)"";
2184 di = dict_find(dict, key, -1);
2185 if (di != NULL)
2186 {
2187 if (error_if_locked(di->di_tv.v_lock,
2188 e_cannot_change_dict_item))
2189 goto on_error;
2190 // overwrite existing value
2191 clear_tv(&di->di_tv);
2192 di->di_tv = *tv;
2193 }
2194 else
2195 {
2196 if (error_if_locked(dict->dv_lock,
2197 e_cannot_change_dict))
2198 goto on_error;
2199 // add to dict, only fails when out of memory
2200 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2201 goto failed;
2202 clear_tv(tv);
2203 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002204 }
2205 else
2206 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002207 status = FAIL;
2208 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002209 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002210
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002211 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002212 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002213 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002214 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002215 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002216 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002217 goto on_error;
2218 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002219 }
2220 break;
2221
Bram Moolenaar0186e582021-01-10 18:33:11 +01002222 // load or store variable or argument from outer scope
2223 case ISN_LOADOUTER:
2224 case ISN_STOREOUTER:
2225 {
2226 int depth = iptr->isn_arg.outer.outer_depth;
2227 outer_T *outer = ectx.ec_outer;
2228
2229 while (depth > 1 && outer != NULL)
2230 {
2231 outer = outer->out_up;
2232 --depth;
2233 }
2234 if (outer == NULL)
2235 {
2236 SOURCING_LNUM = iptr->isn_lnum;
2237 iemsg("LOADOUTER depth more than scope levels");
2238 goto failed;
2239 }
2240 tv = ((typval_T *)outer->out_stack->ga_data)
2241 + outer->out_frame_idx + STACK_FRAME_SIZE
2242 + iptr->isn_arg.outer.outer_idx;
2243 if (iptr->isn_type == ISN_LOADOUTER)
2244 {
2245 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2246 goto failed;
2247 copy_tv(tv, STACK_TV_BOT(0));
2248 ++ectx.ec_stack.ga_len;
2249 }
2250 else
2251 {
2252 --ectx.ec_stack.ga_len;
2253 clear_tv(tv);
2254 *tv = *STACK_TV_BOT(0);
2255 }
2256 }
2257 break;
2258
Bram Moolenaar752fc692021-01-04 21:57:11 +01002259 // unlet item in list or dict variable
2260 case ISN_UNLETINDEX:
2261 {
2262 typval_T *tv_idx = STACK_TV_BOT(-2);
2263 typval_T *tv_dest = STACK_TV_BOT(-1);
2264 int status = OK;
2265
2266 // Stack contains:
2267 // -2 index
2268 // -1 dict or list
2269 if (tv_dest->v_type == VAR_DICT)
2270 {
2271 // unlet a dict item, index must be a string
2272 if (tv_idx->v_type != VAR_STRING)
2273 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002274 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002275 semsg(_(e_expected_str_but_got_str),
2276 vartype_name(VAR_STRING),
2277 vartype_name(tv_idx->v_type));
2278 status = FAIL;
2279 }
2280 else
2281 {
2282 dict_T *d = tv_dest->vval.v_dict;
2283 char_u *key = tv_idx->vval.v_string;
2284 dictitem_T *di = NULL;
2285
2286 if (key == NULL)
2287 key = (char_u *)"";
2288 if (d != NULL)
2289 di = dict_find(d, key, (int)STRLEN(key));
2290 if (di == NULL)
2291 {
2292 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002293 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002294 semsg(_(e_dictkey), key);
2295 status = FAIL;
2296 }
2297 else
2298 {
2299 // TODO: check for dict or item locked
2300 dictitem_remove(d, di);
2301 }
2302 }
2303 }
2304 else if (tv_dest->v_type == VAR_LIST)
2305 {
2306 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002307 SOURCING_LNUM = iptr->isn_lnum;
2308 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002309 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002310 status = FAIL;
2311 }
2312 else
2313 {
2314 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002315 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002316 listitem_T *li = NULL;
2317
2318 li = list_find(l, n);
2319 if (li == NULL)
2320 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002321 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002322 semsg(_(e_listidx), n);
2323 status = FAIL;
2324 }
2325 else
2326 // TODO: check for list or item locked
2327 listitem_remove(l, li);
2328 }
2329 }
2330 else
2331 {
2332 status = FAIL;
2333 semsg(_(e_cannot_index_str),
2334 vartype_name(tv_dest->v_type));
2335 }
2336
2337 clear_tv(tv_idx);
2338 clear_tv(tv_dest);
2339 ectx.ec_stack.ga_len -= 2;
2340 if (status == FAIL)
2341 goto on_error;
2342 }
2343 break;
2344
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002345 // unlet range of items in list variable
2346 case ISN_UNLETRANGE:
2347 {
2348 // Stack contains:
2349 // -3 index1
2350 // -2 index2
2351 // -1 dict or list
2352 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2353 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2354 typval_T *tv_dest = STACK_TV_BOT(-1);
2355 int status = OK;
2356
2357 if (tv_dest->v_type == VAR_LIST)
2358 {
2359 // indexes must be a number
2360 SOURCING_LNUM = iptr->isn_lnum;
2361 if (check_for_number(tv_idx1) == FAIL
2362 || check_for_number(tv_idx2) == FAIL)
2363 {
2364 status = FAIL;
2365 }
2366 else
2367 {
2368 list_T *l = tv_dest->vval.v_list;
2369 long n1 = (long)tv_idx1->vval.v_number;
2370 long n2 = (long)tv_idx2->vval.v_number;
2371 listitem_T *li;
2372
2373 li = list_find_index(l, &n1);
2374 if (li == NULL
2375 || list_unlet_range(l, li, NULL, n1,
2376 TRUE, n2) == FAIL)
2377 status = FAIL;
2378 }
2379 }
2380 else
2381 {
2382 status = FAIL;
2383 SOURCING_LNUM = iptr->isn_lnum;
2384 semsg(_(e_cannot_index_str),
2385 vartype_name(tv_dest->v_type));
2386 }
2387
2388 clear_tv(tv_idx1);
2389 clear_tv(tv_idx2);
2390 clear_tv(tv_dest);
2391 ectx.ec_stack.ga_len -= 3;
2392 if (status == FAIL)
2393 goto on_error;
2394 }
2395 break;
2396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 // push constant
2398 case ISN_PUSHNR:
2399 case ISN_PUSHBOOL:
2400 case ISN_PUSHSPEC:
2401 case ISN_PUSHF:
2402 case ISN_PUSHS:
2403 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002404 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002405 case ISN_PUSHCHANNEL:
2406 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002407 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 goto failed;
2409 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002410 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 ++ectx.ec_stack.ga_len;
2412 switch (iptr->isn_type)
2413 {
2414 case ISN_PUSHNR:
2415 tv->v_type = VAR_NUMBER;
2416 tv->vval.v_number = iptr->isn_arg.number;
2417 break;
2418 case ISN_PUSHBOOL:
2419 tv->v_type = VAR_BOOL;
2420 tv->vval.v_number = iptr->isn_arg.number;
2421 break;
2422 case ISN_PUSHSPEC:
2423 tv->v_type = VAR_SPECIAL;
2424 tv->vval.v_number = iptr->isn_arg.number;
2425 break;
2426#ifdef FEAT_FLOAT
2427 case ISN_PUSHF:
2428 tv->v_type = VAR_FLOAT;
2429 tv->vval.v_float = iptr->isn_arg.fnumber;
2430 break;
2431#endif
2432 case ISN_PUSHBLOB:
2433 blob_copy(iptr->isn_arg.blob, tv);
2434 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002435 case ISN_PUSHFUNC:
2436 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002437 if (iptr->isn_arg.string == NULL)
2438 tv->vval.v_string = NULL;
2439 else
2440 tv->vval.v_string =
2441 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002442 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002443 case ISN_PUSHCHANNEL:
2444#ifdef FEAT_JOB_CHANNEL
2445 tv->v_type = VAR_CHANNEL;
2446 tv->vval.v_channel = iptr->isn_arg.channel;
2447 if (tv->vval.v_channel != NULL)
2448 ++tv->vval.v_channel->ch_refcount;
2449#endif
2450 break;
2451 case ISN_PUSHJOB:
2452#ifdef FEAT_JOB_CHANNEL
2453 tv->v_type = VAR_JOB;
2454 tv->vval.v_job = iptr->isn_arg.job;
2455 if (tv->vval.v_job != NULL)
2456 ++tv->vval.v_job->jv_refcount;
2457#endif
2458 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 default:
2460 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002461 tv->vval.v_string = vim_strsave(
2462 iptr->isn_arg.string == NULL
2463 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 }
2465 break;
2466
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002467 case ISN_UNLET:
2468 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2469 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002470 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002471 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002472 case ISN_UNLETENV:
2473 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2474 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002475
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002476 case ISN_LOCKCONST:
2477 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2478 break;
2479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 // create a list from items on the stack; uses a single allocation
2481 // for the list header and the items
2482 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002483 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2484 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 break;
2486
2487 // create a dict from items on the stack
2488 case ISN_NEWDICT:
2489 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002490 int count = iptr->isn_arg.number;
2491 dict_T *dict = dict_alloc();
2492 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002493 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494
2495 if (dict == NULL)
2496 goto failed;
2497 for (idx = 0; idx < count; ++idx)
2498 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002499 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002501 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002502 key = tv->vval.v_string == NULL
2503 ? (char_u *)"" : tv->vval.v_string;
2504 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002505 if (item != NULL)
2506 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002507 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002508 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002509 dict_unref(dict);
2510 goto on_error;
2511 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002512 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 clear_tv(tv);
2514 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002515 {
2516 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2520 item->di_tv.v_lock = 0;
2521 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002522 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002523 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002524 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 }
2528
2529 if (count > 0)
2530 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002531 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 goto failed;
2533 else
2534 ++ectx.ec_stack.ga_len;
2535 tv = STACK_TV_BOT(-1);
2536 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002537 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 tv->vval.v_dict = dict;
2539 ++dict->dv_refcount;
2540 }
2541 break;
2542
2543 // call a :def function
2544 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002545 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002546 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2547 NULL,
2548 iptr->isn_arg.dfunc.cdf_argcount,
2549 &funclocal,
2550 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002551 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 break;
2553
2554 // call a builtin function
2555 case ISN_BCALL:
2556 SOURCING_LNUM = iptr->isn_lnum;
2557 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2558 iptr->isn_arg.bfunc.cbf_argcount,
2559 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002560 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 break;
2562
2563 // call a funcref or partial
2564 case ISN_PCALL:
2565 {
2566 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2567 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002568 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569
2570 SOURCING_LNUM = iptr->isn_lnum;
2571 if (pfunc->cpf_top)
2572 {
2573 // funcref is above the arguments
2574 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2575 }
2576 else
2577 {
2578 // Get the funcref from the stack.
2579 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002580 partial_tv = *STACK_TV_BOT(0);
2581 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002583 r = call_partial(tv, pfunc->cpf_argcount,
2584 &funclocal, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002585 if (tv == &partial_tv)
2586 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002588 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 }
2590 break;
2591
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002592 case ISN_PCALL_END:
2593 // PCALL finished, arguments have been consumed and replaced by
2594 // the return value. Now clear the funcref from the stack,
2595 // and move the return value in its place.
2596 --ectx.ec_stack.ga_len;
2597 clear_tv(STACK_TV_BOT(-1));
2598 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2599 break;
2600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 // call a user defined function or funcref/partial
2602 case ISN_UCALL:
2603 {
2604 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2605
2606 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002607 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
2608 &funclocal, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002609 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 }
2611 break;
2612
2613 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002614 case ISN_RETURN_ZERO:
2615 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2616 goto failed;
2617 tv = STACK_TV_BOT(0);
2618 ++ectx.ec_stack.ga_len;
2619 tv->v_type = VAR_NUMBER;
2620 tv->vval.v_number = 0;
2621 tv->v_lock = 0;
2622 // FALLTHROUGH
2623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 case ISN_RETURN:
2625 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002626 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002627 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002628
2629 if (trystack->ga_len > 0)
2630 trycmd = ((trycmd_T *)trystack->ga_data)
2631 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002632 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002633 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002635 // jump to ":finally" or ":endtry"
2636 if (trycmd->tcd_finally_idx != 0)
2637 ectx.ec_iidx = trycmd->tcd_finally_idx;
2638 else
2639 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 trycmd->tcd_return = TRUE;
2641 }
2642 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002643 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 }
2645 break;
2646
2647 // push a function reference to a compiled function
2648 case ISN_FUNCREF:
2649 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002650 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2651 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2652 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 if (pt == NULL)
2655 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002656 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002657 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002658 vim_free(pt);
2659 goto failed;
2660 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002661 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2662 &ectx) == FAIL)
2663 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 tv = STACK_TV_BOT(0);
2666 ++ectx.ec_stack.ga_len;
2667 tv->vval.v_partial = pt;
2668 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002669 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 }
2671 break;
2672
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002673 // Create a global function from a lambda.
2674 case ISN_NEWFUNC:
2675 {
2676 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2677
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002678 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002679 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002680 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002681 }
2682 break;
2683
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002684 // List functions
2685 case ISN_DEF:
2686 if (iptr->isn_arg.string == NULL)
2687 list_functions(NULL);
2688 else
2689 {
2690 exarg_T ea;
2691
2692 CLEAR_FIELD(ea);
2693 ea.cmd = ea.arg = iptr->isn_arg.string;
2694 define_function(&ea, NULL);
2695 }
2696 break;
2697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 // jump if a condition is met
2699 case ISN_JUMP:
2700 {
2701 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002702 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 int jump = TRUE;
2704
2705 if (when != JUMP_ALWAYS)
2706 {
2707 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002708 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002709 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002710 || when == JUMP_IF_COND_TRUE)
2711 {
2712 SOURCING_LNUM = iptr->isn_lnum;
2713 jump = tv_get_bool_chk(tv, &error);
2714 if (error)
2715 goto on_error;
2716 }
2717 else
2718 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002720 || when == JUMP_AND_KEEP_IF_FALSE
2721 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002723 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 {
2725 // drop the value from the stack
2726 clear_tv(tv);
2727 --ectx.ec_stack.ga_len;
2728 }
2729 }
2730 if (jump)
2731 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2732 }
2733 break;
2734
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002735 // Jump if an argument with a default value was already set and not
2736 // v:none.
2737 case ISN_JUMP_IF_ARG_SET:
2738 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2739 if (tv->v_type != VAR_UNKNOWN
2740 && !(tv->v_type == VAR_SPECIAL
2741 && tv->vval.v_number == VVAL_NONE))
2742 ectx.ec_iidx = iptr->isn_arg.jumparg.jump_where;
2743 break;
2744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 // top of a for loop
2746 case ISN_FOR:
2747 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002748 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 typval_T *idxtv =
2750 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2751
Bram Moolenaar270d0382020-05-15 21:42:53 +02002752 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 goto failed;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002754 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002755 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002756 list_T *list = ltv->vval.v_list;
2757
2758 // push the next item from the list
2759 ++idxtv->vval.v_number;
2760 if (list == NULL
2761 || idxtv->vval.v_number >= list->lv_len)
2762 {
2763 // past the end of the list, jump to "endfor"
2764 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2765 may_restore_cmdmod(&funclocal);
2766 }
2767 else if (list->lv_first == &range_list_item)
2768 {
2769 // non-materialized range() list
2770 tv = STACK_TV_BOT(0);
2771 tv->v_type = VAR_NUMBER;
2772 tv->v_lock = 0;
2773 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 list, idxtv->vval.v_number, NULL);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002775 ++ectx.ec_stack.ga_len;
2776 }
2777 else
2778 {
2779 listitem_T *li = list_find(list,
2780 idxtv->vval.v_number);
2781
2782 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2783 ++ectx.ec_stack.ga_len;
2784 }
2785 }
2786 else if (ltv->v_type == VAR_STRING)
2787 {
2788 char_u *str = ltv->vval.v_string;
2789 int len = str == NULL ? 0 : (int)STRLEN(str);
2790
2791 // Push the next character from the string. The index
2792 // is for the last byte of the previous character.
2793 ++idxtv->vval.v_number;
2794 if (idxtv->vval.v_number >= len)
2795 {
2796 // past the end of the string, jump to "endfor"
2797 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2798 may_restore_cmdmod(&funclocal);
2799 }
2800 else
2801 {
2802 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2803
2804 tv = STACK_TV_BOT(0);
2805 tv->v_type = VAR_STRING;
2806 tv->vval.v_string = vim_strnsave(
2807 str + idxtv->vval.v_number, clen);
2808 ++ectx.ec_stack.ga_len;
2809 idxtv->vval.v_number += clen - 1;
2810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 }
2812 else
2813 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002814 // TODO: support Blob
2815 semsg(_(e_for_loop_on_str_not_supported),
2816 vartype_name(ltv->v_type));
2817 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 }
2819 }
2820 break;
2821
2822 // start of ":try" block
2823 case ISN_TRY:
2824 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002825 trycmd_T *trycmd = NULL;
2826
Bram Moolenaar270d0382020-05-15 21:42:53 +02002827 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 goto failed;
2829 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2830 + ectx.ec_trystack.ga_len;
2831 ++ectx.ec_trystack.ga_len;
2832 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002833 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002834 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002835 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002836 trycmd->tcd_catch_idx =
2837 iptr->isn_arg.try.try_ref->try_catch;
2838 trycmd->tcd_finally_idx =
2839 iptr->isn_arg.try.try_ref->try_finally;
2840 trycmd->tcd_endtry_idx =
2841 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 }
2843 break;
2844
2845 case ISN_PUSHEXC:
2846 if (current_exception == NULL)
2847 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002848 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 iemsg("Evaluating catch while current_exception is NULL");
2850 goto failed;
2851 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002852 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 goto failed;
2854 tv = STACK_TV_BOT(0);
2855 ++ectx.ec_stack.ga_len;
2856 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002857 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 tv->vval.v_string = vim_strsave(
2859 (char_u *)current_exception->value);
2860 break;
2861
2862 case ISN_CATCH:
2863 {
2864 garray_T *trystack = &ectx.ec_trystack;
2865
Bram Moolenaara91a7132021-03-25 21:12:15 +01002866 may_restore_cmdmod(&funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 if (trystack->ga_len > 0)
2868 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002869 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 + trystack->ga_len - 1;
2871 trycmd->tcd_caught = TRUE;
2872 }
2873 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002874 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 catch_exception(current_exception);
2876 }
2877 break;
2878
Bram Moolenaarc150c092021-02-13 15:02:46 +01002879 case ISN_TRYCONT:
2880 {
2881 garray_T *trystack = &ectx.ec_trystack;
2882 trycont_T *trycont = &iptr->isn_arg.trycont;
2883 int i;
2884 trycmd_T *trycmd;
2885 int iidx = trycont->tct_where;
2886
2887 if (trystack->ga_len < trycont->tct_levels)
2888 {
2889 siemsg("TRYCONT: expected %d levels, found %d",
2890 trycont->tct_levels, trystack->ga_len);
2891 goto failed;
2892 }
2893 // Make :endtry jump to any outer try block and the last
2894 // :endtry inside the loop to the loop start.
2895 for (i = trycont->tct_levels; i > 0; --i)
2896 {
2897 trycmd = ((trycmd_T *)trystack->ga_data)
2898 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002899 // Add one to tcd_cont to be able to jump to
2900 // instruction with index zero.
2901 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002902 iidx = trycmd->tcd_finally_idx == 0
2903 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002904 }
2905 // jump to :finally or :endtry of current try statement
2906 ectx.ec_iidx = iidx;
2907 }
2908 break;
2909
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002910 case ISN_FINALLY:
2911 {
2912 garray_T *trystack = &ectx.ec_trystack;
2913 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2914 + trystack->ga_len - 1;
2915
2916 // Reset the index to avoid a return statement jumps here
2917 // again.
2918 trycmd->tcd_finally_idx = 0;
2919 break;
2920 }
2921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 // end of ":try" block
2923 case ISN_ENDTRY:
2924 {
2925 garray_T *trystack = &ectx.ec_trystack;
2926
2927 if (trystack->ga_len > 0)
2928 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002929 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 --trystack->ga_len;
2932 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002933 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 trycmd = ((trycmd_T *)trystack->ga_data)
2935 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002936 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 {
2938 // discard the exception
2939 if (caught_stack == current_exception)
2940 caught_stack = caught_stack->caught;
2941 discard_current_exception();
2942 }
2943
2944 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002945 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002946
2947 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2948 {
2949 --ectx.ec_stack.ga_len;
2950 clear_tv(STACK_TV_BOT(0));
2951 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002952 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002953 // handling :continue: jump to outer try block or
2954 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002955 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 }
2957 }
2958 break;
2959
2960 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002961 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002962 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002963
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002964 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2965 {
2966 // throwing an exception while using "silent!" causes
2967 // the function to abort but not display an error.
2968 tv = STACK_TV_BOT(-1);
2969 clear_tv(tv);
2970 tv->v_type = VAR_NUMBER;
2971 tv->vval.v_number = 0;
2972 goto done;
2973 }
2974 --ectx.ec_stack.ga_len;
2975 tv = STACK_TV_BOT(0);
2976 if (tv->vval.v_string == NULL
2977 || *skipwhite(tv->vval.v_string) == NUL)
2978 {
2979 vim_free(tv->vval.v_string);
2980 SOURCING_LNUM = iptr->isn_lnum;
2981 emsg(_(e_throw_with_empty_string));
2982 goto failed;
2983 }
2984
2985 // Inside a "catch" we need to first discard the caught
2986 // exception.
2987 if (trystack->ga_len > 0)
2988 {
2989 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2990 + trystack->ga_len - 1;
2991 if (trycmd->tcd_caught && current_exception != NULL)
2992 {
2993 // discard the exception
2994 if (caught_stack == current_exception)
2995 caught_stack = caught_stack->caught;
2996 discard_current_exception();
2997 trycmd->tcd_caught = FALSE;
2998 }
2999 }
3000
3001 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3002 == FAIL)
3003 {
3004 vim_free(tv->vval.v_string);
3005 goto failed;
3006 }
3007 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 break;
3010
3011 // compare with special values
3012 case ISN_COMPAREBOOL:
3013 case ISN_COMPARESPECIAL:
3014 {
3015 typval_T *tv1 = STACK_TV_BOT(-2);
3016 typval_T *tv2 = STACK_TV_BOT(-1);
3017 varnumber_T arg1 = tv1->vval.v_number;
3018 varnumber_T arg2 = tv2->vval.v_number;
3019 int res;
3020
3021 switch (iptr->isn_arg.op.op_type)
3022 {
3023 case EXPR_EQUAL: res = arg1 == arg2; break;
3024 case EXPR_NEQUAL: res = arg1 != arg2; break;
3025 default: res = 0; break;
3026 }
3027
3028 --ectx.ec_stack.ga_len;
3029 tv1->v_type = VAR_BOOL;
3030 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3031 }
3032 break;
3033
3034 // Operation with two number arguments
3035 case ISN_OPNR:
3036 case ISN_COMPARENR:
3037 {
3038 typval_T *tv1 = STACK_TV_BOT(-2);
3039 typval_T *tv2 = STACK_TV_BOT(-1);
3040 varnumber_T arg1 = tv1->vval.v_number;
3041 varnumber_T arg2 = tv2->vval.v_number;
3042 varnumber_T res;
3043
3044 switch (iptr->isn_arg.op.op_type)
3045 {
3046 case EXPR_MULT: res = arg1 * arg2; break;
3047 case EXPR_DIV: res = arg1 / arg2; break;
3048 case EXPR_REM: res = arg1 % arg2; break;
3049 case EXPR_SUB: res = arg1 - arg2; break;
3050 case EXPR_ADD: res = arg1 + arg2; break;
3051
3052 case EXPR_EQUAL: res = arg1 == arg2; break;
3053 case EXPR_NEQUAL: res = arg1 != arg2; break;
3054 case EXPR_GREATER: res = arg1 > arg2; break;
3055 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3056 case EXPR_SMALLER: res = arg1 < arg2; break;
3057 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3058 default: res = 0; break;
3059 }
3060
3061 --ectx.ec_stack.ga_len;
3062 if (iptr->isn_type == ISN_COMPARENR)
3063 {
3064 tv1->v_type = VAR_BOOL;
3065 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3066 }
3067 else
3068 tv1->vval.v_number = res;
3069 }
3070 break;
3071
3072 // Computation with two float arguments
3073 case ISN_OPFLOAT:
3074 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003075#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 {
3077 typval_T *tv1 = STACK_TV_BOT(-2);
3078 typval_T *tv2 = STACK_TV_BOT(-1);
3079 float_T arg1 = tv1->vval.v_float;
3080 float_T arg2 = tv2->vval.v_float;
3081 float_T res = 0;
3082 int cmp = FALSE;
3083
3084 switch (iptr->isn_arg.op.op_type)
3085 {
3086 case EXPR_MULT: res = arg1 * arg2; break;
3087 case EXPR_DIV: res = arg1 / arg2; break;
3088 case EXPR_SUB: res = arg1 - arg2; break;
3089 case EXPR_ADD: res = arg1 + arg2; break;
3090
3091 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3092 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3093 case EXPR_GREATER: cmp = arg1 > arg2; break;
3094 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3095 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3096 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3097 default: cmp = 0; break;
3098 }
3099 --ectx.ec_stack.ga_len;
3100 if (iptr->isn_type == ISN_COMPAREFLOAT)
3101 {
3102 tv1->v_type = VAR_BOOL;
3103 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3104 }
3105 else
3106 tv1->vval.v_float = res;
3107 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003108#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 break;
3110
3111 case ISN_COMPARELIST:
3112 {
3113 typval_T *tv1 = STACK_TV_BOT(-2);
3114 typval_T *tv2 = STACK_TV_BOT(-1);
3115 list_T *arg1 = tv1->vval.v_list;
3116 list_T *arg2 = tv2->vval.v_list;
3117 int cmp = FALSE;
3118 int ic = iptr->isn_arg.op.op_ic;
3119
3120 switch (iptr->isn_arg.op.op_type)
3121 {
3122 case EXPR_EQUAL: cmp =
3123 list_equal(arg1, arg2, ic, FALSE); break;
3124 case EXPR_NEQUAL: cmp =
3125 !list_equal(arg1, arg2, ic, FALSE); break;
3126 case EXPR_IS: cmp = arg1 == arg2; break;
3127 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3128 default: cmp = 0; break;
3129 }
3130 --ectx.ec_stack.ga_len;
3131 clear_tv(tv1);
3132 clear_tv(tv2);
3133 tv1->v_type = VAR_BOOL;
3134 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3135 }
3136 break;
3137
3138 case ISN_COMPAREBLOB:
3139 {
3140 typval_T *tv1 = STACK_TV_BOT(-2);
3141 typval_T *tv2 = STACK_TV_BOT(-1);
3142 blob_T *arg1 = tv1->vval.v_blob;
3143 blob_T *arg2 = tv2->vval.v_blob;
3144 int cmp = FALSE;
3145
3146 switch (iptr->isn_arg.op.op_type)
3147 {
3148 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3149 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3150 case EXPR_IS: cmp = arg1 == arg2; break;
3151 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3152 default: cmp = 0; break;
3153 }
3154 --ectx.ec_stack.ga_len;
3155 clear_tv(tv1);
3156 clear_tv(tv2);
3157 tv1->v_type = VAR_BOOL;
3158 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3159 }
3160 break;
3161
3162 // TODO: handle separately
3163 case ISN_COMPARESTRING:
3164 case ISN_COMPAREDICT:
3165 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 case ISN_COMPAREANY:
3167 {
3168 typval_T *tv1 = STACK_TV_BOT(-2);
3169 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003170 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 int ic = iptr->isn_arg.op.op_ic;
3172
Bram Moolenaareb26f432020-09-14 16:50:05 +02003173 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003174 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 --ectx.ec_stack.ga_len;
3177 }
3178 break;
3179
3180 case ISN_ADDLIST:
3181 case ISN_ADDBLOB:
3182 {
3183 typval_T *tv1 = STACK_TV_BOT(-2);
3184 typval_T *tv2 = STACK_TV_BOT(-1);
3185
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003186 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 if (iptr->isn_type == ISN_ADDLIST)
3188 eval_addlist(tv1, tv2);
3189 else
3190 eval_addblob(tv1, tv2);
3191 clear_tv(tv2);
3192 --ectx.ec_stack.ga_len;
3193 }
3194 break;
3195
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003196 case ISN_LISTAPPEND:
3197 {
3198 typval_T *tv1 = STACK_TV_BOT(-2);
3199 typval_T *tv2 = STACK_TV_BOT(-1);
3200 list_T *l = tv1->vval.v_list;
3201
3202 // add an item to a list
3203 if (l == NULL)
3204 {
3205 SOURCING_LNUM = iptr->isn_lnum;
3206 emsg(_(e_cannot_add_to_null_list));
3207 goto on_error;
3208 }
3209 if (list_append_tv(l, tv2) == FAIL)
3210 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003211 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003212 --ectx.ec_stack.ga_len;
3213 }
3214 break;
3215
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003216 case ISN_BLOBAPPEND:
3217 {
3218 typval_T *tv1 = STACK_TV_BOT(-2);
3219 typval_T *tv2 = STACK_TV_BOT(-1);
3220 blob_T *b = tv1->vval.v_blob;
3221 int error = FALSE;
3222 varnumber_T n;
3223
3224 // add a number to a blob
3225 if (b == NULL)
3226 {
3227 SOURCING_LNUM = iptr->isn_lnum;
3228 emsg(_(e_cannot_add_to_null_blob));
3229 goto on_error;
3230 }
3231 n = tv_get_number_chk(tv2, &error);
3232 if (error)
3233 goto on_error;
3234 ga_append(&b->bv_ga, (int)n);
3235 --ectx.ec_stack.ga_len;
3236 }
3237 break;
3238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 // Computation with two arguments of unknown type
3240 case ISN_OPANY:
3241 {
3242 typval_T *tv1 = STACK_TV_BOT(-2);
3243 typval_T *tv2 = STACK_TV_BOT(-1);
3244 varnumber_T n1, n2;
3245#ifdef FEAT_FLOAT
3246 float_T f1 = 0, f2 = 0;
3247#endif
3248 int error = FALSE;
3249
3250 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3251 {
3252 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3253 {
3254 eval_addlist(tv1, tv2);
3255 clear_tv(tv2);
3256 --ectx.ec_stack.ga_len;
3257 break;
3258 }
3259 else if (tv1->v_type == VAR_BLOB
3260 && tv2->v_type == VAR_BLOB)
3261 {
3262 eval_addblob(tv1, tv2);
3263 clear_tv(tv2);
3264 --ectx.ec_stack.ga_len;
3265 break;
3266 }
3267 }
3268#ifdef FEAT_FLOAT
3269 if (tv1->v_type == VAR_FLOAT)
3270 {
3271 f1 = tv1->vval.v_float;
3272 n1 = 0;
3273 }
3274 else
3275#endif
3276 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003277 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 n1 = tv_get_number_chk(tv1, &error);
3279 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003280 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281#ifdef FEAT_FLOAT
3282 if (tv2->v_type == VAR_FLOAT)
3283 f1 = n1;
3284#endif
3285 }
3286#ifdef FEAT_FLOAT
3287 if (tv2->v_type == VAR_FLOAT)
3288 {
3289 f2 = tv2->vval.v_float;
3290 n2 = 0;
3291 }
3292 else
3293#endif
3294 {
3295 n2 = tv_get_number_chk(tv2, &error);
3296 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003297 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298#ifdef FEAT_FLOAT
3299 if (tv1->v_type == VAR_FLOAT)
3300 f2 = n2;
3301#endif
3302 }
3303#ifdef FEAT_FLOAT
3304 // if there is a float on either side the result is a float
3305 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3306 {
3307 switch (iptr->isn_arg.op.op_type)
3308 {
3309 case EXPR_MULT: f1 = f1 * f2; break;
3310 case EXPR_DIV: f1 = f1 / f2; break;
3311 case EXPR_SUB: f1 = f1 - f2; break;
3312 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003313 default: SOURCING_LNUM = iptr->isn_lnum;
3314 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003315 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 }
3317 clear_tv(tv1);
3318 clear_tv(tv2);
3319 tv1->v_type = VAR_FLOAT;
3320 tv1->vval.v_float = f1;
3321 --ectx.ec_stack.ga_len;
3322 }
3323 else
3324#endif
3325 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003326 int failed = FALSE;
3327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 switch (iptr->isn_arg.op.op_type)
3329 {
3330 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003331 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3332 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003333 goto on_error;
3334 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 case EXPR_SUB: n1 = n1 - n2; break;
3336 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003337 default: n1 = num_modulus(n1, n2, &failed);
3338 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003339 goto on_error;
3340 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 }
3342 clear_tv(tv1);
3343 clear_tv(tv2);
3344 tv1->v_type = VAR_NUMBER;
3345 tv1->vval.v_number = n1;
3346 --ectx.ec_stack.ga_len;
3347 }
3348 }
3349 break;
3350
3351 case ISN_CONCAT:
3352 {
3353 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3354 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3355 char_u *res;
3356
3357 res = concat_str(str1, str2);
3358 clear_tv(STACK_TV_BOT(-2));
3359 clear_tv(STACK_TV_BOT(-1));
3360 --ectx.ec_stack.ga_len;
3361 STACK_TV_BOT(-1)->vval.v_string = res;
3362 }
3363 break;
3364
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003365 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003366 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003367 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003368 int is_slice = iptr->isn_type == ISN_STRSLICE;
3369 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003370 char_u *res;
3371
3372 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003373 // string slice: string is at stack-3, first index at
3374 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003375 if (is_slice)
3376 {
3377 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003378 n1 = tv->vval.v_number;
3379 }
3380
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003381 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003382 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003383
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003384 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003385 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003386 if (is_slice)
3387 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003388 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003389 else
3390 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003391 // single character (including composing characters).
3392 // If the index is too big or negative the result is
3393 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003394 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003395 vim_free(tv->vval.v_string);
3396 tv->vval.v_string = res;
3397 }
3398 break;
3399
3400 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003401 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 {
Bram Moolenaared591872020-08-15 22:14:53 +02003403 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003405 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406
3407 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003408 // list slice: list is at stack-3, indexes at stack-2 and
3409 // stack-1
3410 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 list = tv->vval.v_list;
3412
3413 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003414 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003416
3417 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 {
Bram Moolenaared591872020-08-15 22:14:53 +02003419 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003420 n1 = tv->vval.v_number;
3421 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 }
Bram Moolenaared591872020-08-15 22:14:53 +02003423
3424 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003425 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003426 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003427 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3428 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003429 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 }
3431 break;
3432
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003433 case ISN_ANYINDEX:
3434 case ISN_ANYSLICE:
3435 {
3436 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3437 typval_T *var1, *var2;
3438 int res;
3439
3440 // index: composite is at stack-2, index at stack-1
3441 // slice: composite is at stack-3, indexes at stack-2 and
3442 // stack-1
3443 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003444 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003445 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3446 goto on_error;
3447 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3448 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003449 res = eval_index_inner(tv, is_slice, var1, var2,
3450 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003451 clear_tv(var1);
3452 if (is_slice)
3453 clear_tv(var2);
3454 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3455 if (res == FAIL)
3456 goto on_error;
3457 }
3458 break;
3459
Bram Moolenaar9af78762020-06-16 11:34:42 +02003460 case ISN_SLICE:
3461 {
3462 list_T *list;
3463 int count = iptr->isn_arg.number;
3464
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003465 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003466 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003467 list = tv->vval.v_list;
3468
3469 // no error for short list, expect it to be checked earlier
3470 if (list != NULL && list->lv_len >= count)
3471 {
3472 list_T *newlist = list_slice(list,
3473 count, list->lv_len - 1);
3474
3475 if (newlist != NULL)
3476 {
3477 list_unref(list);
3478 tv->vval.v_list = newlist;
3479 ++newlist->lv_refcount;
3480 }
3481 }
3482 }
3483 break;
3484
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003485 case ISN_GETITEM:
3486 {
3487 listitem_T *li;
3488 int index = iptr->isn_arg.number;
3489
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003490 // Get list item: list is at stack-1, push item.
3491 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003492 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003493 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003494
3495 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3496 goto failed;
3497 ++ectx.ec_stack.ga_len;
3498 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003499
3500 // Useful when used in unpack assignment. Reset at
3501 // ISN_DROP.
3502 where.wt_index = index + 1;
3503 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003504 }
3505 break;
3506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 case ISN_MEMBER:
3508 {
3509 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003510 char_u *key;
3511 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003512 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003513
3514 // dict member: dict is at stack-2, key at stack-1
3515 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003516 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003517 dict = tv->vval.v_dict;
3518
3519 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003520 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003521 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003522 if (key == NULL)
3523 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003524
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003525 if ((di = dict_find(dict, key, -1)) == NULL)
3526 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003527 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003528 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003529
3530 // If :silent! is used we will continue, make sure the
3531 // stack contents makes sense.
3532 clear_tv(tv);
3533 --ectx.ec_stack.ga_len;
3534 tv = STACK_TV_BOT(-1);
3535 clear_tv(tv);
3536 tv->v_type = VAR_NUMBER;
3537 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003538 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003539 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003540 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003541 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003542 // Clear the dict only after getting the item, to avoid
3543 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003544 tv = STACK_TV_BOT(-1);
3545 temp_tv = *tv;
3546 copy_tv(&di->di_tv, tv);
3547 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003548 }
3549 break;
3550
3551 // dict member with string key
3552 case ISN_STRINGMEMBER:
3553 {
3554 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003556 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557
3558 tv = STACK_TV_BOT(-1);
3559 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3560 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003563 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 }
3565 dict = tv->vval.v_dict;
3566
3567 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3568 == NULL)
3569 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003570 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003572 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003574 // Clear the dict after getting the item, to avoid that it
3575 // make the item invalid.
3576 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003578 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 }
3580 break;
3581
3582 case ISN_NEGATENR:
3583 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003584 if (tv->v_type != VAR_NUMBER
3585#ifdef FEAT_FLOAT
3586 && tv->v_type != VAR_FLOAT
3587#endif
3588 )
3589 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003590 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003591 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003592 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003593 }
3594#ifdef FEAT_FLOAT
3595 if (tv->v_type == VAR_FLOAT)
3596 tv->vval.v_float = -tv->vval.v_float;
3597 else
3598#endif
3599 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 break;
3601
3602 case ISN_CHECKNR:
3603 {
3604 int error = FALSE;
3605
3606 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003607 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003609 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 (void)tv_get_number_chk(tv, &error);
3611 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003612 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 }
3614 break;
3615
3616 case ISN_CHECKTYPE:
3617 {
3618 checktype_T *ct = &iptr->isn_arg.type;
3619
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003620 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003621 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003622 if (!where.wt_variable)
3623 where.wt_index = ct->ct_arg_idx;
3624 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003625 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003626 if (!where.wt_variable)
3627 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003628
3629 // number 0 is FALSE, number 1 is TRUE
3630 if (tv->v_type == VAR_NUMBER
3631 && ct->ct_type->tt_type == VAR_BOOL
3632 && (tv->vval.v_number == 0
3633 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003635 tv->v_type = VAR_BOOL;
3636 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003637 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 }
3639 }
3640 break;
3641
Bram Moolenaar9af78762020-06-16 11:34:42 +02003642 case ISN_CHECKLEN:
3643 {
3644 int min_len = iptr->isn_arg.checklen.cl_min_len;
3645 list_T *list = NULL;
3646
3647 tv = STACK_TV_BOT(-1);
3648 if (tv->v_type == VAR_LIST)
3649 list = tv->vval.v_list;
3650 if (list == NULL || list->lv_len < min_len
3651 || (list->lv_len > min_len
3652 && !iptr->isn_arg.checklen.cl_more_OK))
3653 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003654 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003655 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003656 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003657 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003658 }
3659 }
3660 break;
3661
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003662 case ISN_SETTYPE:
3663 {
3664 checktype_T *ct = &iptr->isn_arg.type;
3665
3666 tv = STACK_TV_BOT(-1);
3667 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3668 {
3669 free_type(tv->vval.v_dict->dv_type);
3670 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3671 }
3672 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3673 {
3674 free_type(tv->vval.v_list->lv_type);
3675 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3676 }
3677 }
3678 break;
3679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003681 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 {
3683 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003684 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685
3686 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003687 if (iptr->isn_type == ISN_2BOOL)
3688 {
3689 n = tv2bool(tv);
3690 if (iptr->isn_arg.number) // invert
3691 n = !n;
3692 }
3693 else
3694 {
3695 SOURCING_LNUM = iptr->isn_lnum;
3696 n = tv_get_bool_chk(tv, &error);
3697 if (error)
3698 goto on_error;
3699 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 clear_tv(tv);
3701 tv->v_type = VAR_BOOL;
3702 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3703 }
3704 break;
3705
3706 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003707 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003708 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003709 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3710 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3711 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 break;
3713
Bram Moolenaar08597872020-12-10 19:43:40 +01003714 case ISN_RANGE:
3715 {
3716 exarg_T ea;
3717 char *errormsg;
3718
Bram Moolenaarece0b872021-01-08 20:40:45 +01003719 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003720 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003721 ea.addr_type = ADDR_LINES;
3722 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003723 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003724 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003725 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003726
3727 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3728 goto failed;
3729 ++ectx.ec_stack.ga_len;
3730 tv = STACK_TV_BOT(-1);
3731 tv->v_type = VAR_NUMBER;
3732 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003733 if (ea.addr_count == 0)
3734 tv->vval.v_number = curwin->w_cursor.lnum;
3735 else
3736 tv->vval.v_number = ea.line2;
3737 }
3738 break;
3739
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003740 case ISN_PUT:
3741 {
3742 int regname = iptr->isn_arg.put.put_regname;
3743 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3744 char_u *expr = NULL;
3745 int dir = FORWARD;
3746
Bram Moolenaar08597872020-12-10 19:43:40 +01003747 if (lnum < -2)
3748 {
3749 // line number was put on the stack by ISN_RANGE
3750 tv = STACK_TV_BOT(-1);
3751 curwin->w_cursor.lnum = tv->vval.v_number;
3752 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3753 dir = BACKWARD;
3754 --ectx.ec_stack.ga_len;
3755 }
3756 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003757 // :put! above cursor
3758 dir = BACKWARD;
3759 else if (lnum >= 0)
3760 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003761
3762 if (regname == '=')
3763 {
3764 tv = STACK_TV_BOT(-1);
3765 if (tv->v_type == VAR_STRING)
3766 expr = tv->vval.v_string;
3767 else
3768 {
3769 expr = typval2string(tv, TRUE); // allocates value
3770 clear_tv(tv);
3771 }
3772 --ectx.ec_stack.ga_len;
3773 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003774 check_cursor();
3775 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3776 vim_free(expr);
3777 }
3778 break;
3779
Bram Moolenaar02194d22020-10-24 23:08:38 +02003780 case ISN_CMDMOD:
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003781 funclocal.floc_save_cmdmod = cmdmod;
3782 funclocal.floc_restore_cmdmod = TRUE;
3783 funclocal.floc_restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003784 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3785 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003786 break;
3787
Bram Moolenaar02194d22020-10-24 23:08:38 +02003788 case ISN_CMDMOD_REV:
3789 // filter regprog is owned by the instruction, don't free it
3790 cmdmod.cmod_filter_regmatch.regprog = NULL;
3791 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003792 cmdmod = funclocal.floc_save_cmdmod;
3793 funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003794 break;
3795
Bram Moolenaar792f7862020-11-23 08:31:18 +01003796 case ISN_UNPACK:
3797 {
3798 int count = iptr->isn_arg.unpack.unp_count;
3799 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3800 list_T *l;
3801 listitem_T *li;
3802 int i;
3803
3804 // Check there is a valid list to unpack.
3805 tv = STACK_TV_BOT(-1);
3806 if (tv->v_type != VAR_LIST)
3807 {
3808 SOURCING_LNUM = iptr->isn_lnum;
3809 emsg(_(e_for_argument_must_be_sequence_of_lists));
3810 goto on_error;
3811 }
3812 l = tv->vval.v_list;
3813 if (l == NULL
3814 || l->lv_len < (semicolon ? count - 1 : count))
3815 {
3816 SOURCING_LNUM = iptr->isn_lnum;
3817 emsg(_(e_list_value_does_not_have_enough_items));
3818 goto on_error;
3819 }
3820 else if (!semicolon && l->lv_len > count)
3821 {
3822 SOURCING_LNUM = iptr->isn_lnum;
3823 emsg(_(e_list_value_has_more_items_than_targets));
3824 goto on_error;
3825 }
3826
3827 CHECK_LIST_MATERIALIZE(l);
3828 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3829 goto failed;
3830 ectx.ec_stack.ga_len += count - 1;
3831
3832 // Variable after semicolon gets a list with the remaining
3833 // items.
3834 if (semicolon)
3835 {
3836 list_T *rem_list =
3837 list_alloc_with_items(l->lv_len - count + 1);
3838
3839 if (rem_list == NULL)
3840 goto failed;
3841 tv = STACK_TV_BOT(-count);
3842 tv->vval.v_list = rem_list;
3843 ++rem_list->lv_refcount;
3844 tv->v_lock = 0;
3845 li = l->lv_first;
3846 for (i = 0; i < count - 1; ++i)
3847 li = li->li_next;
3848 for (i = 0; li != NULL; ++i)
3849 {
3850 list_set_item(rem_list, i, &li->li_tv);
3851 li = li->li_next;
3852 }
3853 --count;
3854 }
3855
3856 // Produce the values in reverse order, first item last.
3857 li = l->lv_first;
3858 for (i = 0; i < count; ++i)
3859 {
3860 tv = STACK_TV_BOT(-i - 1);
3861 copy_tv(&li->li_tv, tv);
3862 li = li->li_next;
3863 }
3864
3865 list_unref(l);
3866 }
3867 break;
3868
Bram Moolenaarb2049902021-01-24 12:53:53 +01003869 case ISN_PROF_START:
3870 case ISN_PROF_END:
3871 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003872#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003873 funccall_T cookie;
3874 ufunc_T *cur_ufunc =
3875 (((dfunc_T *)def_functions.ga_data)
3876 + ectx.ec_dfunc_idx)->df_ufunc;
3877
3878 cookie.func = cur_ufunc;
3879 if (iptr->isn_type == ISN_PROF_START)
3880 {
3881 func_line_start(&cookie, iptr->isn_lnum);
3882 // if we get here the instruction is executed
3883 func_line_exec(&cookie);
3884 }
3885 else
3886 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003887#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003888 }
3889 break;
3890
Bram Moolenaar389df252020-07-09 21:20:47 +02003891 case ISN_SHUFFLE:
3892 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003893 typval_T tmp_tv;
3894 int item = iptr->isn_arg.shuffle.shfl_item;
3895 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003896
3897 tmp_tv = *STACK_TV_BOT(-item);
3898 for ( ; up > 0 && item > 1; --up)
3899 {
3900 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3901 --item;
3902 }
3903 *STACK_TV_BOT(-item) = tmp_tv;
3904 }
3905 break;
3906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 case ISN_DROP:
3908 --ectx.ec_stack.ga_len;
3909 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003910 where.wt_index = 0;
3911 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 break;
3913 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003914 continue;
3915
Bram Moolenaard032f342020-07-18 18:13:02 +02003916func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003917 // Restore previous function. If the frame pointer is where we started
3918 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003919 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003920 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003921
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003922 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003923 // only fails when out of memory
3924 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003925 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003926
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003927on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003928 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003929 // If "emsg_silent" is set then ignore the error, unless it was set
3930 // when calling the function.
3931 if (did_emsg_cumul + did_emsg == did_emsg_before
3932 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003933 {
3934 // If a sequence of instructions causes an error while ":silent!"
3935 // was used, restore the stack length and jump ahead to restoring
3936 // the cmdmod.
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003937 if (funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003938 {
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003939 while (ectx.ec_stack.ga_len
3940 > funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003941 {
3942 --ectx.ec_stack.ga_len;
3943 clear_tv(STACK_TV_BOT(0));
3944 }
3945 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3946 ++ectx.ec_iidx;
3947 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003948 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003949 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003950on_fatal_error:
3951 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003952 // If we are not inside a try-catch started here, abort execution.
3953 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003954 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 }
3956
3957done:
3958 // function finished, get result from the stack.
3959 tv = STACK_TV_BOT(-1);
3960 *rettv = *tv;
3961 tv->v_type = VAR_UNKNOWN;
3962 ret = OK;
3963
3964failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003965 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003966 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003967 func_return(&funclocal, &ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003968
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003969 // Deal with any remaining closures, they may be in use somewhere.
3970 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003971 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003972 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003973 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3974 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003975
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003976 estack_pop();
3977 current_sctx = save_current_sctx;
3978
Bram Moolenaarc970e422021-03-17 15:03:04 +01003979 // TODO: when is it safe to delete the function if it is no longer used?
3980 --ufunc->uf_calls;
3981
Bram Moolenaar352134b2020-10-17 22:04:08 +02003982 if (*msg_list != NULL && saved_msg_list != NULL)
3983 {
3984 msglist_T **plist = saved_msg_list;
3985
3986 // Append entries from the current msg_list (uncaught exceptions) to
3987 // the saved msg_list.
3988 while (*plist != NULL)
3989 plist = &(*plist)->next;
3990
3991 *plist = *msg_list;
3992 }
3993 msg_list = saved_msg_list;
3994
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003995 if (funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02003996 {
3997 cmdmod.cmod_filter_regmatch.regprog = NULL;
3998 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003999 cmdmod = funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004000 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004001 emsg_silent_def = save_emsg_silent_def;
4002 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004003
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004004failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004005 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4007 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004010 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004011
Bram Moolenaar0186e582021-01-10 18:33:11 +01004012 while (ectx.ec_outer != NULL)
4013 {
4014 outer_T *up = ectx.ec_outer->out_up_is_copy
4015 ? NULL : ectx.ec_outer->out_up;
4016
4017 vim_free(ectx.ec_outer);
4018 ectx.ec_outer = up;
4019 }
4020
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004021 // Not sure if this is necessary.
4022 suppress_errthrow = save_suppress_errthrow;
4023
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004024 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004025 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004026 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004027 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 return ret;
4029}
4030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004032 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01004033 * We don't really need this at runtime, but we do have tests that require it,
4034 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 */
4036 void
4037ex_disassemble(exarg_T *eap)
4038{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004039 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004040 char_u *fname;
4041 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 dfunc_T *dfunc;
4043 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004044 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 int current;
4046 int line_idx = 0;
4047 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004048 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049
Bram Moolenaarbfd65582020-07-13 18:18:00 +02004050 if (STRNCMP(arg, "<lambda>", 8) == 0)
4051 {
4052 arg += 8;
4053 (void)getdigits(&arg);
4054 fname = vim_strnsave(eap->arg, arg - eap->arg);
4055 }
4056 else
4057 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004058 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004059 if (fname == NULL)
4060 {
4061 semsg(_(e_invarg2), eap->arg);
4062 return;
4063 }
4064
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004065 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004066 if (ufunc == NULL)
4067 {
4068 char_u *p = untrans_function_name(fname);
4069
4070 if (p != NULL)
4071 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004072 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004073 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004074 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 if (ufunc == NULL)
4076 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004077 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 return;
4079 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01004080 if (func_needs_compiling(ufunc, eap->forceit)
4081 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02004082 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004083 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004085 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 return;
4087 }
4088 if (ufunc->uf_name_exp != NULL)
4089 msg((char *)ufunc->uf_name_exp);
4090 else
4091 msg((char *)ufunc->uf_name);
4092
4093 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004094#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004095 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
4096 instr_count = eap->forceit ? dfunc->df_instr_prof_count
4097 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004098#else
4099 instr = dfunc->df_instr;
4100 instr_count = dfunc->df_instr_count;
4101#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004102 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 {
4104 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004105 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106
4107 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
4108 {
4109 if (current > prev_current)
4110 {
4111 msg_puts("\n\n");
4112 prev_current = current;
4113 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004114 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4115 if (line != NULL)
4116 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 }
4118
4119 switch (iptr->isn_type)
4120 {
4121 case ISN_EXEC:
4122 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
4123 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004124 case ISN_EXECCONCAT:
4125 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004126 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004127 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 case ISN_ECHO:
4129 {
4130 echo_T *echo = &iptr->isn_arg.echo;
4131
4132 smsg("%4d %s %d", current,
4133 echo->echo_with_white ? "ECHO" : "ECHON",
4134 echo->echo_count);
4135 }
4136 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004137 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01004138 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004139 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01004140 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004141 case ISN_ECHOMSG:
4142 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004143 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004144 break;
4145 case ISN_ECHOERR:
4146 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004147 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004148 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004150 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004151 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004152 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004153 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004154 + STACK_FRAME_SIZE));
4155 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004156 smsg("%4d LOAD $%lld", current,
4157 (varnumber_T)(iptr->isn_arg.number));
4158 }
4159 break;
4160 case ISN_LOADOUTER:
4161 {
4162 if (iptr->isn_arg.number < 0)
4163 smsg("%4d LOADOUTER level %d arg[%d]", current,
4164 iptr->isn_arg.outer.outer_depth,
4165 iptr->isn_arg.outer.outer_idx
4166 + STACK_FRAME_SIZE);
4167 else
4168 smsg("%4d LOADOUTER level %d $%d", current,
4169 iptr->isn_arg.outer.outer_depth,
4170 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 break;
4173 case ISN_LOADV:
4174 smsg("%4d LOADV v:%s", current,
4175 get_vim_var_name(iptr->isn_arg.number));
4176 break;
4177 case ISN_LOADSCRIPT:
4178 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004179 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4180 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004182 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004184 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4185 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004186 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004187 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 }
4189 break;
4190 case ISN_LOADS:
4191 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004192 scriptitem_T *si = SCRIPT_ITEM(
4193 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194
4195 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004196 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 }
4198 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004199 case ISN_LOADAUTO:
4200 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4201 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 case ISN_LOADG:
4203 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4204 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004205 case ISN_LOADB:
4206 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4207 break;
4208 case ISN_LOADW:
4209 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4210 break;
4211 case ISN_LOADT:
4212 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4213 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004214 case ISN_LOADGDICT:
4215 smsg("%4d LOAD g:", current);
4216 break;
4217 case ISN_LOADBDICT:
4218 smsg("%4d LOAD b:", current);
4219 break;
4220 case ISN_LOADWDICT:
4221 smsg("%4d LOAD w:", current);
4222 break;
4223 case ISN_LOADTDICT:
4224 smsg("%4d LOAD t:", current);
4225 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 case ISN_LOADOPT:
4227 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4228 break;
4229 case ISN_LOADENV:
4230 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4231 break;
4232 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004233 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 break;
4235
4236 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004237 if (iptr->isn_arg.number < 0)
4238 smsg("%4d STORE arg[%lld]", current,
4239 iptr->isn_arg.number + STACK_FRAME_SIZE);
4240 else
4241 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4242 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004243 case ISN_STOREOUTER:
4244 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004245 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004246 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4247 iptr->isn_arg.outer.outer_depth,
4248 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004249 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004250 smsg("%4d STOREOUTER level %d $%d", current,
4251 iptr->isn_arg.outer.outer_depth,
4252 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004255 case ISN_STOREV:
4256 smsg("%4d STOREV v:%s", current,
4257 get_vim_var_name(iptr->isn_arg.number));
4258 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004259 case ISN_STOREAUTO:
4260 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4261 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004263 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4264 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004265 case ISN_STOREB:
4266 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4267 break;
4268 case ISN_STOREW:
4269 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4270 break;
4271 case ISN_STORET:
4272 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4273 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004274 case ISN_STORES:
4275 {
4276 scriptitem_T *si = SCRIPT_ITEM(
4277 iptr->isn_arg.loadstore.ls_sid);
4278
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004279 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004280 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 break;
4283 case ISN_STORESCRIPT:
4284 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004285 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4286 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004288 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004290 smsg("%4d STORESCRIPT %s-%d in %s", current,
4291 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004292 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004293 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 }
4295 break;
4296 case ISN_STOREOPT:
4297 smsg("%4d STOREOPT &%s", current,
4298 iptr->isn_arg.storeopt.so_name);
4299 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004300 case ISN_STOREENV:
4301 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4302 break;
4303 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004304 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004305 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 case ISN_STORENR:
4307 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004308 iptr->isn_arg.storenr.stnr_val,
4309 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 break;
4311
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004312 case ISN_STOREINDEX:
4313 switch (iptr->isn_arg.vartype)
4314 {
4315 case VAR_LIST:
4316 smsg("%4d STORELIST", current);
4317 break;
4318 case VAR_DICT:
4319 smsg("%4d STOREDICT", current);
4320 break;
4321 case VAR_ANY:
4322 smsg("%4d STOREINDEX", current);
4323 break;
4324 default: break;
4325 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004326 break;
4327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 // constants
4329 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004330 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004331 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 break;
4333 case ISN_PUSHBOOL:
4334 case ISN_PUSHSPEC:
4335 smsg("%4d PUSH %s", current,
4336 get_var_special_name(iptr->isn_arg.number));
4337 break;
4338 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004339#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004341#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 break;
4343 case ISN_PUSHS:
4344 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4345 break;
4346 case ISN_PUSHBLOB:
4347 {
4348 char_u *r;
4349 char_u numbuf[NUMBUFLEN];
4350 char_u *tofree;
4351
4352 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004353 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 vim_free(tofree);
4355 }
4356 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004357 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004358 {
4359 char *name = (char *)iptr->isn_arg.string;
4360
4361 smsg("%4d PUSHFUNC \"%s\"", current,
4362 name == NULL ? "[none]" : name);
4363 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004364 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004365 case ISN_PUSHCHANNEL:
4366#ifdef FEAT_JOB_CHANNEL
4367 {
4368 channel_T *channel = iptr->isn_arg.channel;
4369
4370 smsg("%4d PUSHCHANNEL %d", current,
4371 channel == NULL ? 0 : channel->ch_id);
4372 }
4373#endif
4374 break;
4375 case ISN_PUSHJOB:
4376#ifdef FEAT_JOB_CHANNEL
4377 {
4378 typval_T tv;
4379 char_u *name;
4380
4381 tv.v_type = VAR_JOB;
4382 tv.vval.v_job = iptr->isn_arg.job;
4383 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004384 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004385 }
4386#endif
4387 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 case ISN_PUSHEXC:
4389 smsg("%4d PUSH v:exception", current);
4390 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004391 case ISN_UNLET:
4392 smsg("%4d UNLET%s %s", current,
4393 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4394 iptr->isn_arg.unlet.ul_name);
4395 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004396 case ISN_UNLETENV:
4397 smsg("%4d UNLETENV%s $%s", current,
4398 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4399 iptr->isn_arg.unlet.ul_name);
4400 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004401 case ISN_UNLETINDEX:
4402 smsg("%4d UNLETINDEX", current);
4403 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004404 case ISN_UNLETRANGE:
4405 smsg("%4d UNLETRANGE", current);
4406 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004407 case ISN_LOCKCONST:
4408 smsg("%4d LOCKCONST", current);
4409 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004411 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004412 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 break;
4414 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004415 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004416 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 break;
4418
4419 // function call
4420 case ISN_BCALL:
4421 {
4422 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4423
4424 smsg("%4d BCALL %s(argc %d)", current,
4425 internal_func_name(cbfunc->cbf_idx),
4426 cbfunc->cbf_argcount);
4427 }
4428 break;
4429 case ISN_DCALL:
4430 {
4431 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4432 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4433 + cdfunc->cdf_idx;
4434
4435 smsg("%4d DCALL %s(argc %d)", current,
4436 df->df_ufunc->uf_name_exp != NULL
4437 ? df->df_ufunc->uf_name_exp
4438 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4439 }
4440 break;
4441 case ISN_UCALL:
4442 {
4443 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4444
4445 smsg("%4d UCALL %s(argc %d)", current,
4446 cufunc->cuf_name, cufunc->cuf_argcount);
4447 }
4448 break;
4449 case ISN_PCALL:
4450 {
4451 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4452
4453 smsg("%4d PCALL%s (argc %d)", current,
4454 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4455 }
4456 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004457 case ISN_PCALL_END:
4458 smsg("%4d PCALL end", current);
4459 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 case ISN_RETURN:
4461 smsg("%4d RETURN", current);
4462 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004463 case ISN_RETURN_ZERO:
4464 smsg("%4d RETURN 0", current);
4465 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 case ISN_FUNCREF:
4467 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004468 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004470 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004472 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 }
4474 break;
4475
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004476 case ISN_NEWFUNC:
4477 {
4478 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4479
4480 smsg("%4d NEWFUNC %s %s", current,
4481 newfunc->nf_lambda, newfunc->nf_global);
4482 }
4483 break;
4484
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004485 case ISN_DEF:
4486 {
4487 char_u *name = iptr->isn_arg.string;
4488
4489 smsg("%4d DEF %s", current,
4490 name == NULL ? (char_u *)"" : name);
4491 }
4492 break;
4493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 case ISN_JUMP:
4495 {
4496 char *when = "?";
4497
4498 switch (iptr->isn_arg.jump.jump_when)
4499 {
4500 case JUMP_ALWAYS:
4501 when = "JUMP";
4502 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 case JUMP_AND_KEEP_IF_TRUE:
4504 when = "JUMP_AND_KEEP_IF_TRUE";
4505 break;
4506 case JUMP_IF_FALSE:
4507 when = "JUMP_IF_FALSE";
4508 break;
4509 case JUMP_AND_KEEP_IF_FALSE:
4510 when = "JUMP_AND_KEEP_IF_FALSE";
4511 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004512 case JUMP_IF_COND_FALSE:
4513 when = "JUMP_IF_COND_FALSE";
4514 break;
4515 case JUMP_IF_COND_TRUE:
4516 when = "JUMP_IF_COND_TRUE";
4517 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004519 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 iptr->isn_arg.jump.jump_where);
4521 }
4522 break;
4523
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004524 case ISN_JUMP_IF_ARG_SET:
4525 smsg("%4d JUMP_IF_ARG_SET arg[%d] -> %d", current,
4526 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4527 iptr->isn_arg.jump.jump_where);
4528 break;
4529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 case ISN_FOR:
4531 {
4532 forloop_T *forloop = &iptr->isn_arg.forloop;
4533
4534 smsg("%4d FOR $%d -> %d", current,
4535 forloop->for_idx, forloop->for_end);
4536 }
4537 break;
4538
4539 case ISN_TRY:
4540 {
4541 try_T *try = &iptr->isn_arg.try;
4542
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004543 if (try->try_ref->try_finally == 0)
4544 smsg("%4d TRY catch -> %d, endtry -> %d",
4545 current,
4546 try->try_ref->try_catch,
4547 try->try_ref->try_endtry);
4548 else
4549 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4550 current,
4551 try->try_ref->try_catch,
4552 try->try_ref->try_finally,
4553 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 }
4555 break;
4556 case ISN_CATCH:
4557 // TODO
4558 smsg("%4d CATCH", current);
4559 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004560 case ISN_TRYCONT:
4561 {
4562 trycont_T *trycont = &iptr->isn_arg.trycont;
4563
4564 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4565 trycont->tct_levels,
4566 trycont->tct_levels == 1 ? "" : "s",
4567 trycont->tct_where);
4568 }
4569 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004570 case ISN_FINALLY:
4571 smsg("%4d FINALLY", current);
4572 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 case ISN_ENDTRY:
4574 smsg("%4d ENDTRY", current);
4575 break;
4576 case ISN_THROW:
4577 smsg("%4d THROW", current);
4578 break;
4579
4580 // expression operations on number
4581 case ISN_OPNR:
4582 case ISN_OPFLOAT:
4583 case ISN_OPANY:
4584 {
4585 char *what;
4586 char *ins;
4587
4588 switch (iptr->isn_arg.op.op_type)
4589 {
4590 case EXPR_MULT: what = "*"; break;
4591 case EXPR_DIV: what = "/"; break;
4592 case EXPR_REM: what = "%"; break;
4593 case EXPR_SUB: what = "-"; break;
4594 case EXPR_ADD: what = "+"; break;
4595 default: what = "???"; break;
4596 }
4597 switch (iptr->isn_type)
4598 {
4599 case ISN_OPNR: ins = "OPNR"; break;
4600 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4601 case ISN_OPANY: ins = "OPANY"; break;
4602 default: ins = "???"; break;
4603 }
4604 smsg("%4d %s %s", current, ins, what);
4605 }
4606 break;
4607
4608 case ISN_COMPAREBOOL:
4609 case ISN_COMPARESPECIAL:
4610 case ISN_COMPARENR:
4611 case ISN_COMPAREFLOAT:
4612 case ISN_COMPARESTRING:
4613 case ISN_COMPAREBLOB:
4614 case ISN_COMPARELIST:
4615 case ISN_COMPAREDICT:
4616 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 case ISN_COMPAREANY:
4618 {
4619 char *p;
4620 char buf[10];
4621 char *type;
4622
4623 switch (iptr->isn_arg.op.op_type)
4624 {
4625 case EXPR_EQUAL: p = "=="; break;
4626 case EXPR_NEQUAL: p = "!="; break;
4627 case EXPR_GREATER: p = ">"; break;
4628 case EXPR_GEQUAL: p = ">="; break;
4629 case EXPR_SMALLER: p = "<"; break;
4630 case EXPR_SEQUAL: p = "<="; break;
4631 case EXPR_MATCH: p = "=~"; break;
4632 case EXPR_IS: p = "is"; break;
4633 case EXPR_ISNOT: p = "isnot"; break;
4634 case EXPR_NOMATCH: p = "!~"; break;
4635 default: p = "???"; break;
4636 }
4637 STRCPY(buf, p);
4638 if (iptr->isn_arg.op.op_ic == TRUE)
4639 strcat(buf, "?");
4640 switch(iptr->isn_type)
4641 {
4642 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4643 case ISN_COMPARESPECIAL:
4644 type = "COMPARESPECIAL"; break;
4645 case ISN_COMPARENR: type = "COMPARENR"; break;
4646 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4647 case ISN_COMPARESTRING:
4648 type = "COMPARESTRING"; break;
4649 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4650 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4651 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4652 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4654 default: type = "???"; break;
4655 }
4656
4657 smsg("%4d %s %s", current, type, buf);
4658 }
4659 break;
4660
4661 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4662 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4663
4664 // expression operations
4665 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004666 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004667 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004668 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004669 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004670 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004671 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004672 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4673 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004674 case ISN_SLICE: smsg("%4d SLICE %lld",
4675 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004676 case ISN_GETITEM: smsg("%4d ITEM %lld",
4677 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004678 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4679 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 iptr->isn_arg.string); break;
4681 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4682
4683 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004684 case ISN_CHECKTYPE:
4685 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004686 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004687 char *tofree;
4688
Bram Moolenaare32e5162021-01-21 20:21:29 +01004689 if (ct->ct_arg_idx == 0)
4690 smsg("%4d CHECKTYPE %s stack[%d]", current,
4691 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004692 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004693 else
4694 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4695 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004696 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004697 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004698 vim_free(tofree);
4699 break;
4700 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004701 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4702 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4703 iptr->isn_arg.checklen.cl_min_len);
4704 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004705 case ISN_SETTYPE:
4706 {
4707 char *tofree;
4708
4709 smsg("%4d SETTYPE %s", current,
4710 type_name(iptr->isn_arg.type.ct_type, &tofree));
4711 vim_free(tofree);
4712 break;
4713 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004714 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 case ISN_2BOOL: if (iptr->isn_arg.number)
4716 smsg("%4d INVERT (!val)", current);
4717 else
4718 smsg("%4d 2BOOL (!!val)", current);
4719 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004720 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004721 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004722 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004723 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004724 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004725 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004726 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4727 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004728 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004729 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4730 smsg("%4d PUT %c above range",
4731 current, iptr->isn_arg.put.put_regname);
4732 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4733 smsg("%4d PUT %c range",
4734 current, iptr->isn_arg.put.put_regname);
4735 else
4736 smsg("%4d PUT %c %ld", current,
4737 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004738 (long)iptr->isn_arg.put.put_lnum);
4739 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740
Bram Moolenaar02194d22020-10-24 23:08:38 +02004741 // TODO: summarize modifiers
4742 case ISN_CMDMOD:
4743 {
4744 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004745 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004746 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4747
4748 buf = alloc(len + 1);
4749 if (buf != NULL)
4750 {
4751 (void)produce_cmdmods(
4752 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4753 smsg("%4d CMDMOD %s", current, buf);
4754 vim_free(buf);
4755 }
4756 break;
4757 }
4758 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004759
Bram Moolenaarb2049902021-01-24 12:53:53 +01004760 case ISN_PROF_START:
4761 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4762 break;
4763
4764 case ISN_PROF_END:
4765 smsg("%4d PROFILE END", current);
4766 break;
4767
Bram Moolenaar792f7862020-11-23 08:31:18 +01004768 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4769 iptr->isn_arg.unpack.unp_count,
4770 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4771 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004772 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4773 iptr->isn_arg.shuffle.shfl_item,
4774 iptr->isn_arg.shuffle.shfl_up);
4775 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 case ISN_DROP: smsg("%4d DROP", current); break;
4777 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004778
4779 out_flush(); // output one line at a time
4780 ui_breakcheck();
4781 if (got_int)
4782 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784}
4785
4786/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004787 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 * list, etc. Mostly like what JavaScript does, except that empty list and
4789 * empty dictionary are FALSE.
4790 */
4791 int
4792tv2bool(typval_T *tv)
4793{
4794 switch (tv->v_type)
4795 {
4796 case VAR_NUMBER:
4797 return tv->vval.v_number != 0;
4798 case VAR_FLOAT:
4799#ifdef FEAT_FLOAT
4800 return tv->vval.v_float != 0.0;
4801#else
4802 break;
4803#endif
4804 case VAR_PARTIAL:
4805 return tv->vval.v_partial != NULL;
4806 case VAR_FUNC:
4807 case VAR_STRING:
4808 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4809 case VAR_LIST:
4810 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4811 case VAR_DICT:
4812 return tv->vval.v_dict != NULL
4813 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4814 case VAR_BOOL:
4815 case VAR_SPECIAL:
4816 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4817 case VAR_JOB:
4818#ifdef FEAT_JOB_CHANNEL
4819 return tv->vval.v_job != NULL;
4820#else
4821 break;
4822#endif
4823 case VAR_CHANNEL:
4824#ifdef FEAT_JOB_CHANNEL
4825 return tv->vval.v_channel != NULL;
4826#else
4827 break;
4828#endif
4829 case VAR_BLOB:
4830 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4831 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004832 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 case VAR_VOID:
4834 break;
4835 }
4836 return FALSE;
4837}
4838
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004839 void
4840emsg_using_string_as(typval_T *tv, int as_number)
4841{
4842 semsg(_(as_number ? e_using_string_as_number_str
4843 : e_using_string_as_bool_str),
4844 tv->vval.v_string == NULL
4845 ? (char_u *)"" : tv->vval.v_string);
4846}
4847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848/*
4849 * If "tv" is a string give an error and return FAIL.
4850 */
4851 int
4852check_not_string(typval_T *tv)
4853{
4854 if (tv->v_type == VAR_STRING)
4855 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004856 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 clear_tv(tv);
4858 return FAIL;
4859 }
4860 return OK;
4861}
4862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863
4864#endif // FEAT_EVAL