blob: 026a9ee45d88a0adeb6f727e3f86c219d35bc533 [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 Moolenaar701cc6c2021-04-10 13:33:48 +02001300 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001301 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1302 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001303 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001304 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001305 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001306 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001307 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001308 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001309 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001310
Bram Moolenaar09689a02020-05-09 22:50:08 +02001311 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001312 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001313 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1314 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001315 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001316 {
1317 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001318 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001319 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001322 // If depth of calling is getting too high, don't execute the function.
1323 orig_funcdepth = funcdepth_get();
1324 if (funcdepth_increment() == FAIL)
1325 return FAIL;
1326
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001327 CLEAR_FIELD(funclocal);
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001328 CLEAR_FIELD(ectx);
1329 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1330 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1331 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001332 {
1333 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001334 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001337 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02001339 idx = argc - ufunc->uf_args.ga_len;
1340 if (idx > 0 && ufunc->uf_va_name == NULL)
1341 {
1342 if (idx == 1)
1343 emsg(_(e_one_argument_too_many));
1344 else
1345 semsg(_(e_nr_arguments_too_many), idx);
Bram Moolenaarc4297692021-04-10 20:46:48 +02001346 goto failed_early;
Bram Moolenaarbb8a7ce2021-04-10 20:10:26 +02001347 }
1348
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001349 // Put arguments on the stack, but no more than what the function expects.
1350 // A lambda can be called with more arguments than it uses.
1351 for (idx = 0; idx < argc
1352 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1353 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 {
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001355 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
1356 && argv[idx].v_type == VAR_SPECIAL
1357 && argv[idx].vval.v_number == VVAL_NONE)
1358 {
1359 // Use the default value.
1360 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1361 }
1362 else
1363 {
1364 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
1365 && check_typval_arg_type(
1366 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
1367 goto failed_early;
1368 copy_tv(&argv[idx], STACK_TV_BOT(0));
1369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 ++ectx.ec_stack.ga_len;
1371 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001372
1373 // Turn varargs into a list. Empty list if no args.
1374 if (ufunc->uf_va_name != NULL)
1375 {
1376 int vararg_count = argc - ufunc->uf_args.ga_len;
1377
1378 if (vararg_count < 0)
1379 vararg_count = 0;
1380 else
1381 argc -= vararg_count;
1382 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001383 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001384
1385 // Check the type of the list items.
1386 tv = STACK_TV_BOT(-1);
1387 if (ufunc->uf_va_type != NULL
Bram Moolenaar2a389082021-04-09 20:24:31 +02001388 && ufunc->uf_va_type != &t_list_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001389 && ufunc->uf_va_type->tt_member != &t_any
1390 && tv->vval.v_list != NULL)
1391 {
1392 type_T *expected = ufunc->uf_va_type->tt_member;
1393 listitem_T *li = tv->vval.v_list->lv_first;
1394
1395 for (idx = 0; idx < vararg_count; ++idx)
1396 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001397 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001398 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001399 goto failed_early;
1400 li = li->li_next;
1401 }
1402 }
1403
Bram Moolenaar23e03252020-04-12 22:22:31 +02001404 if (defcount > 0)
1405 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001406 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001407 --ectx.ec_stack.ga_len;
1408 }
1409
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001410 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001411 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001412 if (defcount > 0)
1413 for (idx = 0; idx < defcount; ++idx)
1414 {
1415 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1416 ++ectx.ec_stack.ga_len;
1417 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001418 if (ufunc->uf_va_name != NULL)
Bram Moolenaarc970e422021-03-17 15:03:04 +01001419 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420
1421 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001422 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1423 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001425 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001426 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1427 + ufunc->uf_dfunc_idx;
1428 ufunc_T *base_ufunc = dfunc->df_ufunc;
1429
1430 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1431 // by copy_func().
1432 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001433 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001434 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1435 if (ectx.ec_outer == NULL)
1436 goto failed_early;
1437 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001438 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001439 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1440 {
1441 if (current_ectx->ec_outer != NULL)
1442 *ectx.ec_outer = *current_ectx->ec_outer;
1443 }
1444 else
1445 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001446 }
1447 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001448 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1449 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001450 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001451 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 // dummy frame entries
1454 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1455 {
1456 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1457 ++ectx.ec_stack.ga_len;
1458 }
1459
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001460 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001461 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001462 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1463 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001465 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001466 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001467 ectx.ec_stack.ga_len += dfunc->df_varcount;
1468 if (dfunc->df_has_closure)
1469 {
1470 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1471 STACK_TV_VAR(idx)->vval.v_number = 0;
1472 ++ectx.ec_stack.ga_len;
1473 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001474
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001475 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001476 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001477
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001478 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001479 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001480 estack_push_ufunc(ufunc, 1);
1481 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001482 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1483
Bram Moolenaar352134b2020-10-17 22:04:08 +02001484 // Use a specific location for storing error messages to be converted to an
1485 // exception.
1486 saved_msg_list = msg_list;
1487 msg_list = &private_msg_list;
1488
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001489 // Do turn errors into exceptions.
1490 suppress_errthrow = FALSE;
1491
Bram Moolenaarc970e422021-03-17 15:03:04 +01001492 // Do not delete the function while executing it.
1493 ++ufunc->uf_calls;
1494
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001495 // When ":silent!" was used before calling then we still abort the
1496 // function. If ":silent!" is used in the function then we don't.
1497 emsg_silent_def = emsg_silent;
1498 did_emsg_def = 0;
1499
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001500 where.wt_index = 0;
1501 where.wt_variable = FALSE;
1502
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001503 // Start execution at the first instruction.
1504 ectx.ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 for (;;)
1507 {
1508 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001509
Bram Moolenaar270d0382020-05-15 21:42:53 +02001510 if (++breakcheck_count >= 100)
1511 {
1512 line_breakcheck();
1513 breakcheck_count = 0;
1514 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001515 if (got_int)
1516 {
1517 // Turn CTRL-C into an exception.
1518 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001519 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001520 goto failed;
1521 did_throw = TRUE;
1522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523
Bram Moolenaara26b9702020-04-18 19:53:28 +02001524 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1525 {
1526 // Turn an error message into an exception.
1527 did_emsg = FALSE;
1528 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1529 goto failed;
1530 did_throw = TRUE;
1531 *msg_list = NULL;
1532 }
1533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534 if (did_throw && !ectx.ec_in_catch)
1535 {
1536 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001537 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538
1539 // An exception jumps to the first catch, finally, or returns from
1540 // the current function.
1541 if (trystack->ga_len > 0)
1542 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001543 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 {
1545 // jump to ":catch" or ":finally"
1546 ectx.ec_in_catch = TRUE;
1547 ectx.ec_iidx = trycmd->tcd_catch_idx;
1548 }
1549 else
1550 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001551 // Not inside try or need to return from current functions.
1552 // Push a dummy return value.
1553 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1554 goto failed;
1555 tv = STACK_TV_BOT(0);
1556 tv->v_type = VAR_NUMBER;
1557 tv->vval.v_number = 0;
1558 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001559 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001561 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001562 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001563 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1564 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 goto done;
1566 }
1567
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001568 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001569 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 }
1571 continue;
1572 }
1573
1574 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1575 switch (iptr->isn_type)
1576 {
1577 // execute Ex command line
1578 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001579 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001580 source_cookie_T cookie;
1581
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001582 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001583 // Pass getsourceline to get an error for a missing ":end"
1584 // command.
1585 CLEAR_FIELD(cookie);
1586 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1587 if (do_cmdline(iptr->isn_arg.string,
1588 getsourceline, &cookie,
1589 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1590 == FAIL
1591 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001592 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 break;
1595
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001596 // execute Ex command from pieces on the stack
1597 case ISN_EXECCONCAT:
1598 {
1599 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001600 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001601 int pass;
1602 int i;
1603 char_u *cmd = NULL;
1604 char_u *str;
1605
1606 for (pass = 1; pass <= 2; ++pass)
1607 {
1608 for (i = 0; i < count; ++i)
1609 {
1610 tv = STACK_TV_BOT(i - count);
1611 str = tv->vval.v_string;
1612 if (str != NULL && *str != NUL)
1613 {
1614 if (pass == 2)
1615 STRCPY(cmd + len, str);
1616 len += STRLEN(str);
1617 }
1618 if (pass == 2)
1619 clear_tv(tv);
1620 }
1621 if (pass == 1)
1622 {
1623 cmd = alloc(len + 1);
1624 if (cmd == NULL)
1625 goto failed;
1626 len = 0;
1627 }
1628 }
1629
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001630 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001631 do_cmdline_cmd(cmd);
1632 vim_free(cmd);
1633 }
1634 break;
1635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 // execute :echo {string} ...
1637 case ISN_ECHO:
1638 {
1639 int count = iptr->isn_arg.echo.echo_count;
1640 int atstart = TRUE;
1641 int needclr = TRUE;
1642
1643 for (idx = 0; idx < count; ++idx)
1644 {
1645 tv = STACK_TV_BOT(idx - count);
1646 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1647 &atstart, &needclr);
1648 clear_tv(tv);
1649 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001650 if (needclr)
1651 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 ectx.ec_stack.ga_len -= count;
1653 }
1654 break;
1655
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001656 // :execute {string} ...
1657 // :echomsg {string} ...
1658 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001659 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001660 case ISN_ECHOMSG:
1661 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001662 {
1663 int count = iptr->isn_arg.number;
1664 garray_T ga;
1665 char_u buf[NUMBUFLEN];
1666 char_u *p;
1667 int len;
1668 int failed = FALSE;
1669
1670 ga_init2(&ga, 1, 80);
1671 for (idx = 0; idx < count; ++idx)
1672 {
1673 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001674 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001675 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001676 if (tv->v_type == VAR_CHANNEL
1677 || tv->v_type == VAR_JOB)
1678 {
1679 SOURCING_LNUM = iptr->isn_lnum;
1680 emsg(_(e_inval_string));
1681 break;
1682 }
1683 else
1684 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001685 }
1686 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001687 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001688
1689 len = (int)STRLEN(p);
1690 if (ga_grow(&ga, len + 2) == FAIL)
1691 failed = TRUE;
1692 else
1693 {
1694 if (ga.ga_len > 0)
1695 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1696 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1697 ga.ga_len += len;
1698 }
1699 clear_tv(tv);
1700 }
1701 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001702 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001703 {
1704 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001705 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001706 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001707
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001708 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001709 {
1710 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001711 {
1712 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001713 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001714 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001715 {
1716 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001717 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001718 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001719 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001720 else
1721 {
1722 msg_sb_eol();
1723 if (iptr->isn_type == ISN_ECHOMSG)
1724 {
1725 msg_attr(ga.ga_data, echo_attr);
1726 out_flush();
1727 }
1728 else
1729 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001730 SOURCING_LNUM = iptr->isn_lnum;
1731 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001732 }
1733 }
1734 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001735 ga_clear(&ga);
1736 }
1737 break;
1738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 // load local variable or argument
1740 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001741 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 goto failed;
1743 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1744 ++ectx.ec_stack.ga_len;
1745 break;
1746
1747 // load v: variable
1748 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001749 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 goto failed;
1751 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1752 ++ectx.ec_stack.ga_len;
1753 break;
1754
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001755 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 case ISN_LOADSCRIPT:
1757 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001758 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 svar_T *sv;
1760
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001761 sv = get_script_svar(sref, &ectx);
1762 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001763 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001764 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001765 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 goto failed;
1767 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1768 ++ectx.ec_stack.ga_len;
1769 }
1770 break;
1771
1772 // load s: variable in old script
1773 case ISN_LOADS:
1774 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001775 hashtab_T *ht = &SCRIPT_VARS(
1776 iptr->isn_arg.loadstore.ls_sid);
1777 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 if (di == NULL)
1781 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001782 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001783 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001784 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 }
1786 else
1787 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001788 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 goto failed;
1790 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1791 ++ectx.ec_stack.ga_len;
1792 }
1793 }
1794 break;
1795
Bram Moolenaard3aac292020-04-19 14:32:17 +02001796 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001798 case ISN_LOADB:
1799 case ISN_LOADW:
1800 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001802 dictitem_T *di = NULL;
1803 hashtab_T *ht = NULL;
1804 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001805
Bram Moolenaard3aac292020-04-19 14:32:17 +02001806 switch (iptr->isn_type)
1807 {
1808 case ISN_LOADG:
1809 ht = get_globvar_ht();
1810 namespace = 'g';
1811 break;
1812 case ISN_LOADB:
1813 ht = &curbuf->b_vars->dv_hashtab;
1814 namespace = 'b';
1815 break;
1816 case ISN_LOADW:
1817 ht = &curwin->w_vars->dv_hashtab;
1818 namespace = 'w';
1819 break;
1820 case ISN_LOADT:
1821 ht = &curtab->tp_vars->dv_hashtab;
1822 namespace = 't';
1823 break;
1824 default: // Cannot reach here
1825 goto failed;
1826 }
1827 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 if (di == NULL)
1830 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001832 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001833 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001834 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 }
1836 else
1837 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001838 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 goto failed;
1840 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1841 ++ectx.ec_stack.ga_len;
1842 }
1843 }
1844 break;
1845
Bram Moolenaar03290b82020-12-19 16:30:44 +01001846 // load autoload variable
1847 case ISN_LOADAUTO:
1848 {
1849 char_u *name = iptr->isn_arg.string;
1850
1851 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1852 goto failed;
1853 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001854 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001855 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001856 goto on_error;
1857 ++ectx.ec_stack.ga_len;
1858 }
1859 break;
1860
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001861 // load g:/b:/w:/t: namespace
1862 case ISN_LOADGDICT:
1863 case ISN_LOADBDICT:
1864 case ISN_LOADWDICT:
1865 case ISN_LOADTDICT:
1866 {
1867 dict_T *d = NULL;
1868
1869 switch (iptr->isn_type)
1870 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001871 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1872 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1873 case ISN_LOADWDICT: d = curwin->w_vars; break;
1874 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001875 default: // Cannot reach here
1876 goto failed;
1877 }
1878 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1879 goto failed;
1880 tv = STACK_TV_BOT(0);
1881 tv->v_type = VAR_DICT;
1882 tv->v_lock = 0;
1883 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001884 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001885 ++ectx.ec_stack.ga_len;
1886 }
1887 break;
1888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 // load &option
1890 case ISN_LOADOPT:
1891 {
1892 typval_T optval;
1893 char_u *name = iptr->isn_arg.string;
1894
Bram Moolenaara8c17702020-04-01 21:17:24 +02001895 // This is not expected to fail, name is checked during
1896 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001897 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001899 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001900 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 *STACK_TV_BOT(0) = optval;
1902 ++ectx.ec_stack.ga_len;
1903 }
1904 break;
1905
1906 // load $ENV
1907 case ISN_LOADENV:
1908 {
1909 typval_T optval;
1910 char_u *name = iptr->isn_arg.string;
1911
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;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001914 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001915 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 *STACK_TV_BOT(0) = optval;
1917 ++ectx.ec_stack.ga_len;
1918 }
1919 break;
1920
1921 // load @register
1922 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001923 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 goto failed;
1925 tv = STACK_TV_BOT(0);
1926 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001927 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001928 // This may result in NULL, which should be equivalent to an
1929 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 tv->vval.v_string = get_reg_contents(
1931 iptr->isn_arg.number, GREG_EXPR_SRC);
1932 ++ectx.ec_stack.ga_len;
1933 break;
1934
1935 // store local variable
1936 case ISN_STORE:
1937 --ectx.ec_stack.ga_len;
1938 tv = STACK_TV_VAR(iptr->isn_arg.number);
1939 clear_tv(tv);
1940 *tv = *STACK_TV_BOT(0);
1941 break;
1942
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001943 // store s: variable in old script
1944 case ISN_STORES:
1945 {
1946 hashtab_T *ht = &SCRIPT_VARS(
1947 iptr->isn_arg.loadstore.ls_sid);
1948 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001949 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001950
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001951 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001952 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001953 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001954 else
1955 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001956 SOURCING_LNUM = iptr->isn_lnum;
1957 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001958 {
1959 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001960 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001961 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001962 clear_tv(&di->di_tv);
1963 di->di_tv = *STACK_TV_BOT(0);
1964 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001965 }
1966 break;
1967
1968 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 case ISN_STORESCRIPT:
1970 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001971 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1972 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001974 sv = get_script_svar(sref, &ectx);
1975 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001976 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 --ectx.ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001978
1979 // "const" and "final" are checked at compile time, locking
1980 // the value needs to be checked here.
1981 SOURCING_LNUM = iptr->isn_lnum;
1982 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001983 {
1984 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001985 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001986 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 clear_tv(sv->sv_tv);
1989 *sv->sv_tv = *STACK_TV_BOT(0);
1990 }
1991 break;
1992
1993 // store option
1994 case ISN_STOREOPT:
1995 {
1996 long n = 0;
1997 char_u *s = NULL;
1998 char *msg;
1999
2000 --ectx.ec_stack.ga_len;
2001 tv = STACK_TV_BOT(0);
2002 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002003 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002005 if (s == NULL)
2006 s = (char_u *)"";
2007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002009 // must be VAR_NUMBER, CHECKTYPE makes sure
2010 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2012 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002013 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 if (msg != NULL)
2015 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002016 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002018 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 }
2021 break;
2022
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002023 // store $ENV
2024 case ISN_STOREENV:
2025 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002026 tv = STACK_TV_BOT(0);
2027 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2028 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002029 break;
2030
2031 // store @r
2032 case ISN_STOREREG:
2033 {
2034 int reg = iptr->isn_arg.number;
2035
2036 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002037 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002038 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002039 tv_get_string(tv), -1, FALSE);
2040 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002041 }
2042 break;
2043
2044 // store v: variable
2045 case ISN_STOREV:
2046 --ectx.ec_stack.ga_len;
2047 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2048 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002049 // should not happen, type is checked when compiling
2050 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002051 break;
2052
Bram Moolenaard3aac292020-04-19 14:32:17 +02002053 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002055 case ISN_STOREB:
2056 case ISN_STOREW:
2057 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002059 dictitem_T *di;
2060 hashtab_T *ht;
2061 char_u *name = iptr->isn_arg.string + 2;
2062
Bram Moolenaard3aac292020-04-19 14:32:17 +02002063 switch (iptr->isn_type)
2064 {
2065 case ISN_STOREG:
2066 ht = get_globvar_ht();
2067 break;
2068 case ISN_STOREB:
2069 ht = &curbuf->b_vars->dv_hashtab;
2070 break;
2071 case ISN_STOREW:
2072 ht = &curwin->w_vars->dv_hashtab;
2073 break;
2074 case ISN_STORET:
2075 ht = &curtab->tp_vars->dv_hashtab;
2076 break;
2077 default: // Cannot reach here
2078 goto failed;
2079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080
2081 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002082 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002084 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 else
2086 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002087 SOURCING_LNUM = iptr->isn_lnum;
2088 if (var_check_permission(di, name) == FAIL)
2089 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 clear_tv(&di->di_tv);
2091 di->di_tv = *STACK_TV_BOT(0);
2092 }
2093 }
2094 break;
2095
Bram Moolenaar03290b82020-12-19 16:30:44 +01002096 // store an autoload variable
2097 case ISN_STOREAUTO:
2098 SOURCING_LNUM = iptr->isn_lnum;
2099 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2100 clear_tv(STACK_TV_BOT(-1));
2101 --ectx.ec_stack.ga_len;
2102 break;
2103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 // store number in local variable
2105 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002106 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 clear_tv(tv);
2108 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002109 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 break;
2111
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002112 // store value in list or dict variable
2113 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002114 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002115 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002116 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002117 typval_T *tv_dest = STACK_TV_BOT(-1);
2118 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002119
Bram Moolenaar752fc692021-01-04 21:57:11 +01002120 // Stack contains:
2121 // -3 value to be stored
2122 // -2 index
2123 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002124 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002125 SOURCING_LNUM = iptr->isn_lnum;
2126 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002127 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002128 dest_type = tv_dest->v_type;
2129 if (dest_type == VAR_DICT)
2130 status = do_2string(tv_idx, TRUE);
2131 else if (dest_type == VAR_LIST
2132 && tv_idx->v_type != VAR_NUMBER)
2133 {
2134 emsg(_(e_number_exp));
2135 status = FAIL;
2136 }
2137 }
2138 else if (dest_type != tv_dest->v_type)
2139 {
2140 // just in case, should be OK
2141 semsg(_(e_expected_str_but_got_str),
2142 vartype_name(dest_type),
2143 vartype_name(tv_dest->v_type));
2144 status = FAIL;
2145 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002146
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002147 if (status == OK && dest_type == VAR_LIST)
2148 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002149 long lidx = (long)tv_idx->vval.v_number;
2150 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002151
2152 if (list == NULL)
2153 {
2154 emsg(_(e_list_not_set));
2155 goto on_error;
2156 }
2157 if (lidx < 0 && list->lv_len + lidx >= 0)
2158 // negative index is relative to the end
2159 lidx = list->lv_len + lidx;
2160 if (lidx < 0 || lidx > list->lv_len)
2161 {
2162 semsg(_(e_listidx), lidx);
2163 goto on_error;
2164 }
2165 if (lidx < list->lv_len)
2166 {
2167 listitem_T *li = list_find(list, lidx);
2168
2169 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002170 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002171 goto on_error;
2172 // overwrite existing list item
2173 clear_tv(&li->li_tv);
2174 li->li_tv = *tv;
2175 }
2176 else
2177 {
2178 if (error_if_locked(list->lv_lock,
2179 e_cannot_change_list))
2180 goto on_error;
2181 // append to list, only fails when out of memory
2182 if (list_append_tv(list, tv) == FAIL)
2183 goto failed;
2184 clear_tv(tv);
2185 }
2186 }
2187 else if (status == OK && dest_type == VAR_DICT)
2188 {
2189 char_u *key = tv_idx->vval.v_string;
2190 dict_T *dict = tv_dest->vval.v_dict;
2191 dictitem_T *di;
2192
2193 SOURCING_LNUM = iptr->isn_lnum;
2194 if (dict == NULL)
2195 {
2196 emsg(_(e_dictionary_not_set));
2197 goto on_error;
2198 }
2199 if (key == NULL)
2200 key = (char_u *)"";
2201 di = dict_find(dict, key, -1);
2202 if (di != NULL)
2203 {
2204 if (error_if_locked(di->di_tv.v_lock,
2205 e_cannot_change_dict_item))
2206 goto on_error;
2207 // overwrite existing value
2208 clear_tv(&di->di_tv);
2209 di->di_tv = *tv;
2210 }
2211 else
2212 {
2213 if (error_if_locked(dict->dv_lock,
2214 e_cannot_change_dict))
2215 goto on_error;
2216 // add to dict, only fails when out of memory
2217 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2218 goto failed;
2219 clear_tv(tv);
2220 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002221 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002222 else if (status == OK && dest_type == VAR_BLOB)
2223 {
2224 // TODO
2225 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002226 else
2227 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002228 status = FAIL;
2229 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002230 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002231
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002232 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002233 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002234 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002235 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002236 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002237 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002238 goto on_error;
2239 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002240 }
2241 break;
2242
Bram Moolenaar68452172021-04-12 21:21:02 +02002243 // store value in blob range
2244 case ISN_STORERANGE:
2245 {
2246 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2247 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2248 typval_T *tv_dest = STACK_TV_BOT(-1);
2249 int status = OK;
2250
2251 // Stack contains:
2252 // -4 value to be stored
2253 // -3 first index or "none"
2254 // -2 second index or "none"
2255 // -1 destination blob
2256 tv = STACK_TV_BOT(-4);
2257 if (tv_dest->v_type != VAR_BLOB)
2258 {
2259 status = FAIL;
2260 emsg(_(e_blob_required));
2261 }
2262 else
2263 {
2264 varnumber_T n1;
2265 varnumber_T n2;
2266 int error = FALSE;
2267
2268 n1 = tv_get_number_chk(tv_idx1, &error);
2269 if (error)
2270 status = FAIL;
2271 else
2272 {
2273 if (tv_idx2->v_type == VAR_SPECIAL
2274 && tv_idx2->vval.v_number == VVAL_NONE)
2275 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2276 else
2277 n2 = tv_get_number_chk(tv_idx2, &error);
2278 if (error)
2279 status = FAIL;
2280 else
2281 status = blob_set_range(tv_dest->vval.v_blob,
2282 n1, n2, tv);
2283 }
2284 }
2285
2286 clear_tv(tv_idx1);
2287 clear_tv(tv_idx2);
2288 clear_tv(tv_dest);
2289 ectx.ec_stack.ga_len -= 4;
2290 clear_tv(tv);
2291
2292 if (status == FAIL)
2293 goto on_error;
2294 }
2295 break;
2296
Bram Moolenaar0186e582021-01-10 18:33:11 +01002297 // load or store variable or argument from outer scope
2298 case ISN_LOADOUTER:
2299 case ISN_STOREOUTER:
2300 {
2301 int depth = iptr->isn_arg.outer.outer_depth;
2302 outer_T *outer = ectx.ec_outer;
2303
2304 while (depth > 1 && outer != NULL)
2305 {
2306 outer = outer->out_up;
2307 --depth;
2308 }
2309 if (outer == NULL)
2310 {
2311 SOURCING_LNUM = iptr->isn_lnum;
2312 iemsg("LOADOUTER depth more than scope levels");
2313 goto failed;
2314 }
2315 tv = ((typval_T *)outer->out_stack->ga_data)
2316 + outer->out_frame_idx + STACK_FRAME_SIZE
2317 + iptr->isn_arg.outer.outer_idx;
2318 if (iptr->isn_type == ISN_LOADOUTER)
2319 {
2320 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2321 goto failed;
2322 copy_tv(tv, STACK_TV_BOT(0));
2323 ++ectx.ec_stack.ga_len;
2324 }
2325 else
2326 {
2327 --ectx.ec_stack.ga_len;
2328 clear_tv(tv);
2329 *tv = *STACK_TV_BOT(0);
2330 }
2331 }
2332 break;
2333
Bram Moolenaar752fc692021-01-04 21:57:11 +01002334 // unlet item in list or dict variable
2335 case ISN_UNLETINDEX:
2336 {
2337 typval_T *tv_idx = STACK_TV_BOT(-2);
2338 typval_T *tv_dest = STACK_TV_BOT(-1);
2339 int status = OK;
2340
2341 // Stack contains:
2342 // -2 index
2343 // -1 dict or list
2344 if (tv_dest->v_type == VAR_DICT)
2345 {
2346 // unlet a dict item, index must be a string
2347 if (tv_idx->v_type != VAR_STRING)
2348 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002349 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002350 semsg(_(e_expected_str_but_got_str),
2351 vartype_name(VAR_STRING),
2352 vartype_name(tv_idx->v_type));
2353 status = FAIL;
2354 }
2355 else
2356 {
2357 dict_T *d = tv_dest->vval.v_dict;
2358 char_u *key = tv_idx->vval.v_string;
2359 dictitem_T *di = NULL;
2360
2361 if (key == NULL)
2362 key = (char_u *)"";
2363 if (d != NULL)
2364 di = dict_find(d, key, (int)STRLEN(key));
2365 if (di == NULL)
2366 {
2367 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002368 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002369 semsg(_(e_dictkey), key);
2370 status = FAIL;
2371 }
2372 else
2373 {
2374 // TODO: check for dict or item locked
2375 dictitem_remove(d, di);
2376 }
2377 }
2378 }
2379 else if (tv_dest->v_type == VAR_LIST)
2380 {
2381 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002382 SOURCING_LNUM = iptr->isn_lnum;
2383 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002384 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002385 status = FAIL;
2386 }
2387 else
2388 {
2389 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002390 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002391 listitem_T *li = NULL;
2392
2393 li = list_find(l, n);
2394 if (li == NULL)
2395 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002396 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002397 semsg(_(e_listidx), n);
2398 status = FAIL;
2399 }
2400 else
2401 // TODO: check for list or item locked
2402 listitem_remove(l, li);
2403 }
2404 }
2405 else
2406 {
2407 status = FAIL;
2408 semsg(_(e_cannot_index_str),
2409 vartype_name(tv_dest->v_type));
2410 }
2411
2412 clear_tv(tv_idx);
2413 clear_tv(tv_dest);
2414 ectx.ec_stack.ga_len -= 2;
2415 if (status == FAIL)
2416 goto on_error;
2417 }
2418 break;
2419
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002420 // unlet range of items in list variable
2421 case ISN_UNLETRANGE:
2422 {
2423 // Stack contains:
2424 // -3 index1
2425 // -2 index2
2426 // -1 dict or list
2427 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2428 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2429 typval_T *tv_dest = STACK_TV_BOT(-1);
2430 int status = OK;
2431
2432 if (tv_dest->v_type == VAR_LIST)
2433 {
2434 // indexes must be a number
2435 SOURCING_LNUM = iptr->isn_lnum;
2436 if (check_for_number(tv_idx1) == FAIL
2437 || check_for_number(tv_idx2) == FAIL)
2438 {
2439 status = FAIL;
2440 }
2441 else
2442 {
2443 list_T *l = tv_dest->vval.v_list;
2444 long n1 = (long)tv_idx1->vval.v_number;
2445 long n2 = (long)tv_idx2->vval.v_number;
2446 listitem_T *li;
2447
2448 li = list_find_index(l, &n1);
2449 if (li == NULL
2450 || list_unlet_range(l, li, NULL, n1,
2451 TRUE, n2) == FAIL)
2452 status = FAIL;
2453 }
2454 }
2455 else
2456 {
2457 status = FAIL;
2458 SOURCING_LNUM = iptr->isn_lnum;
2459 semsg(_(e_cannot_index_str),
2460 vartype_name(tv_dest->v_type));
2461 }
2462
2463 clear_tv(tv_idx1);
2464 clear_tv(tv_idx2);
2465 clear_tv(tv_dest);
2466 ectx.ec_stack.ga_len -= 3;
2467 if (status == FAIL)
2468 goto on_error;
2469 }
2470 break;
2471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 // push constant
2473 case ISN_PUSHNR:
2474 case ISN_PUSHBOOL:
2475 case ISN_PUSHSPEC:
2476 case ISN_PUSHF:
2477 case ISN_PUSHS:
2478 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002479 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002480 case ISN_PUSHCHANNEL:
2481 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002482 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 goto failed;
2484 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002485 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 ++ectx.ec_stack.ga_len;
2487 switch (iptr->isn_type)
2488 {
2489 case ISN_PUSHNR:
2490 tv->v_type = VAR_NUMBER;
2491 tv->vval.v_number = iptr->isn_arg.number;
2492 break;
2493 case ISN_PUSHBOOL:
2494 tv->v_type = VAR_BOOL;
2495 tv->vval.v_number = iptr->isn_arg.number;
2496 break;
2497 case ISN_PUSHSPEC:
2498 tv->v_type = VAR_SPECIAL;
2499 tv->vval.v_number = iptr->isn_arg.number;
2500 break;
2501#ifdef FEAT_FLOAT
2502 case ISN_PUSHF:
2503 tv->v_type = VAR_FLOAT;
2504 tv->vval.v_float = iptr->isn_arg.fnumber;
2505 break;
2506#endif
2507 case ISN_PUSHBLOB:
2508 blob_copy(iptr->isn_arg.blob, tv);
2509 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002510 case ISN_PUSHFUNC:
2511 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002512 if (iptr->isn_arg.string == NULL)
2513 tv->vval.v_string = NULL;
2514 else
2515 tv->vval.v_string =
2516 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002517 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002518 case ISN_PUSHCHANNEL:
2519#ifdef FEAT_JOB_CHANNEL
2520 tv->v_type = VAR_CHANNEL;
2521 tv->vval.v_channel = iptr->isn_arg.channel;
2522 if (tv->vval.v_channel != NULL)
2523 ++tv->vval.v_channel->ch_refcount;
2524#endif
2525 break;
2526 case ISN_PUSHJOB:
2527#ifdef FEAT_JOB_CHANNEL
2528 tv->v_type = VAR_JOB;
2529 tv->vval.v_job = iptr->isn_arg.job;
2530 if (tv->vval.v_job != NULL)
2531 ++tv->vval.v_job->jv_refcount;
2532#endif
2533 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 default:
2535 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002536 tv->vval.v_string = vim_strsave(
2537 iptr->isn_arg.string == NULL
2538 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 }
2540 break;
2541
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002542 case ISN_UNLET:
2543 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2544 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002545 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002546 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002547 case ISN_UNLETENV:
2548 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2549 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002550
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002551 case ISN_LOCKCONST:
2552 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2553 break;
2554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 // create a list from items on the stack; uses a single allocation
2556 // for the list header and the items
2557 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002558 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2559 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 break;
2561
2562 // create a dict from items on the stack
2563 case ISN_NEWDICT:
2564 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002565 int count = iptr->isn_arg.number;
2566 dict_T *dict = dict_alloc();
2567 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002568 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569
2570 if (dict == NULL)
2571 goto failed;
2572 for (idx = 0; idx < count; ++idx)
2573 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002574 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002576 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002577 key = tv->vval.v_string == NULL
2578 ? (char_u *)"" : tv->vval.v_string;
2579 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002580 if (item != NULL)
2581 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002582 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002583 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002584 dict_unref(dict);
2585 goto on_error;
2586 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002587 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 clear_tv(tv);
2589 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002590 {
2591 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2595 item->di_tv.v_lock = 0;
2596 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002597 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002598 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002599 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 }
2603
2604 if (count > 0)
2605 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002606 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 goto failed;
2608 else
2609 ++ectx.ec_stack.ga_len;
2610 tv = STACK_TV_BOT(-1);
2611 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002612 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 tv->vval.v_dict = dict;
2614 ++dict->dv_refcount;
2615 }
2616 break;
2617
2618 // call a :def function
2619 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002620 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002621 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2622 NULL,
2623 iptr->isn_arg.dfunc.cdf_argcount,
2624 &funclocal,
2625 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002626 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 break;
2628
2629 // call a builtin function
2630 case ISN_BCALL:
2631 SOURCING_LNUM = iptr->isn_lnum;
2632 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2633 iptr->isn_arg.bfunc.cbf_argcount,
2634 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002635 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 break;
2637
2638 // call a funcref or partial
2639 case ISN_PCALL:
2640 {
2641 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2642 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002643 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644
2645 SOURCING_LNUM = iptr->isn_lnum;
2646 if (pfunc->cpf_top)
2647 {
2648 // funcref is above the arguments
2649 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2650 }
2651 else
2652 {
2653 // Get the funcref from the stack.
2654 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002655 partial_tv = *STACK_TV_BOT(0);
2656 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002658 r = call_partial(tv, pfunc->cpf_argcount,
2659 &funclocal, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002660 if (tv == &partial_tv)
2661 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002663 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 }
2665 break;
2666
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002667 case ISN_PCALL_END:
2668 // PCALL finished, arguments have been consumed and replaced by
2669 // the return value. Now clear the funcref from the stack,
2670 // and move the return value in its place.
2671 --ectx.ec_stack.ga_len;
2672 clear_tv(STACK_TV_BOT(-1));
2673 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2674 break;
2675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 // call a user defined function or funcref/partial
2677 case ISN_UCALL:
2678 {
2679 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2680
2681 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002682 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
2683 &funclocal, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002684 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 }
2686 break;
2687
2688 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002689 case ISN_RETURN_ZERO:
2690 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2691 goto failed;
2692 tv = STACK_TV_BOT(0);
2693 ++ectx.ec_stack.ga_len;
2694 tv->v_type = VAR_NUMBER;
2695 tv->vval.v_number = 0;
2696 tv->v_lock = 0;
2697 // FALLTHROUGH
2698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 case ISN_RETURN:
2700 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002701 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002702 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002703
2704 if (trystack->ga_len > 0)
2705 trycmd = ((trycmd_T *)trystack->ga_data)
2706 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002707 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002708 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002710 // jump to ":finally" or ":endtry"
2711 if (trycmd->tcd_finally_idx != 0)
2712 ectx.ec_iidx = trycmd->tcd_finally_idx;
2713 else
2714 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 trycmd->tcd_return = TRUE;
2716 }
2717 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002718 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 }
2720 break;
2721
2722 // push a function reference to a compiled function
2723 case ISN_FUNCREF:
2724 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002725 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2726 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2727 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 if (pt == NULL)
2730 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002731 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002732 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002733 vim_free(pt);
2734 goto failed;
2735 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002736 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2737 &ectx) == FAIL)
2738 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 tv = STACK_TV_BOT(0);
2741 ++ectx.ec_stack.ga_len;
2742 tv->vval.v_partial = pt;
2743 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002744 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 }
2746 break;
2747
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002748 // Create a global function from a lambda.
2749 case ISN_NEWFUNC:
2750 {
2751 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2752
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002753 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002754 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002755 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002756 }
2757 break;
2758
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002759 // List functions
2760 case ISN_DEF:
2761 if (iptr->isn_arg.string == NULL)
2762 list_functions(NULL);
2763 else
2764 {
2765 exarg_T ea;
2766
2767 CLEAR_FIELD(ea);
2768 ea.cmd = ea.arg = iptr->isn_arg.string;
2769 define_function(&ea, NULL);
2770 }
2771 break;
2772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 // jump if a condition is met
2774 case ISN_JUMP:
2775 {
2776 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002777 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 int jump = TRUE;
2779
2780 if (when != JUMP_ALWAYS)
2781 {
2782 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002783 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002784 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002785 || when == JUMP_IF_COND_TRUE)
2786 {
2787 SOURCING_LNUM = iptr->isn_lnum;
2788 jump = tv_get_bool_chk(tv, &error);
2789 if (error)
2790 goto on_error;
2791 }
2792 else
2793 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002795 || when == JUMP_AND_KEEP_IF_FALSE
2796 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002798 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 {
2800 // drop the value from the stack
2801 clear_tv(tv);
2802 --ectx.ec_stack.ga_len;
2803 }
2804 }
2805 if (jump)
2806 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2807 }
2808 break;
2809
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002810 // Jump if an argument with a default value was already set and not
2811 // v:none.
2812 case ISN_JUMP_IF_ARG_SET:
2813 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2814 if (tv->v_type != VAR_UNKNOWN
2815 && !(tv->v_type == VAR_SPECIAL
2816 && tv->vval.v_number == VVAL_NONE))
2817 ectx.ec_iidx = iptr->isn_arg.jumparg.jump_where;
2818 break;
2819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 // top of a for loop
2821 case ISN_FOR:
2822 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002823 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 typval_T *idxtv =
2825 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2826
Bram Moolenaar270d0382020-05-15 21:42:53 +02002827 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 goto failed;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002829 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002830 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002831 list_T *list = ltv->vval.v_list;
2832
2833 // push the next item from the list
2834 ++idxtv->vval.v_number;
2835 if (list == NULL
2836 || idxtv->vval.v_number >= list->lv_len)
2837 {
2838 // past the end of the list, jump to "endfor"
2839 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2840 may_restore_cmdmod(&funclocal);
2841 }
2842 else if (list->lv_first == &range_list_item)
2843 {
2844 // non-materialized range() list
2845 tv = STACK_TV_BOT(0);
2846 tv->v_type = VAR_NUMBER;
2847 tv->v_lock = 0;
2848 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 list, idxtv->vval.v_number, NULL);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002850 ++ectx.ec_stack.ga_len;
2851 }
2852 else
2853 {
2854 listitem_T *li = list_find(list,
2855 idxtv->vval.v_number);
2856
2857 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2858 ++ectx.ec_stack.ga_len;
2859 }
2860 }
2861 else if (ltv->v_type == VAR_STRING)
2862 {
2863 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002864
2865 // Push the next character from the string. The index
2866 // is for the last byte of the previous character.
2867 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002868 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002869 {
2870 // past the end of the string, jump to "endfor"
2871 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2872 may_restore_cmdmod(&funclocal);
2873 }
2874 else
2875 {
2876 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2877
2878 tv = STACK_TV_BOT(0);
2879 tv->v_type = VAR_STRING;
2880 tv->vval.v_string = vim_strnsave(
2881 str + idxtv->vval.v_number, clen);
2882 ++ectx.ec_stack.ga_len;
2883 idxtv->vval.v_number += clen - 1;
2884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 }
2886 else
2887 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002888 // TODO: support Blob
2889 semsg(_(e_for_loop_on_str_not_supported),
2890 vartype_name(ltv->v_type));
2891 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 }
2893 }
2894 break;
2895
2896 // start of ":try" block
2897 case ISN_TRY:
2898 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002899 trycmd_T *trycmd = NULL;
2900
Bram Moolenaar270d0382020-05-15 21:42:53 +02002901 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 goto failed;
2903 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2904 + ectx.ec_trystack.ga_len;
2905 ++ectx.ec_trystack.ga_len;
2906 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002907 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002908 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002909 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002910 trycmd->tcd_catch_idx =
2911 iptr->isn_arg.try.try_ref->try_catch;
2912 trycmd->tcd_finally_idx =
2913 iptr->isn_arg.try.try_ref->try_finally;
2914 trycmd->tcd_endtry_idx =
2915 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 }
2917 break;
2918
2919 case ISN_PUSHEXC:
2920 if (current_exception == NULL)
2921 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002922 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 iemsg("Evaluating catch while current_exception is NULL");
2924 goto failed;
2925 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002926 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 goto failed;
2928 tv = STACK_TV_BOT(0);
2929 ++ectx.ec_stack.ga_len;
2930 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002931 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 tv->vval.v_string = vim_strsave(
2933 (char_u *)current_exception->value);
2934 break;
2935
2936 case ISN_CATCH:
2937 {
2938 garray_T *trystack = &ectx.ec_trystack;
2939
Bram Moolenaara91a7132021-03-25 21:12:15 +01002940 may_restore_cmdmod(&funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 if (trystack->ga_len > 0)
2942 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002943 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 + trystack->ga_len - 1;
2945 trycmd->tcd_caught = TRUE;
2946 }
2947 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002948 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 catch_exception(current_exception);
2950 }
2951 break;
2952
Bram Moolenaarc150c092021-02-13 15:02:46 +01002953 case ISN_TRYCONT:
2954 {
2955 garray_T *trystack = &ectx.ec_trystack;
2956 trycont_T *trycont = &iptr->isn_arg.trycont;
2957 int i;
2958 trycmd_T *trycmd;
2959 int iidx = trycont->tct_where;
2960
2961 if (trystack->ga_len < trycont->tct_levels)
2962 {
2963 siemsg("TRYCONT: expected %d levels, found %d",
2964 trycont->tct_levels, trystack->ga_len);
2965 goto failed;
2966 }
2967 // Make :endtry jump to any outer try block and the last
2968 // :endtry inside the loop to the loop start.
2969 for (i = trycont->tct_levels; i > 0; --i)
2970 {
2971 trycmd = ((trycmd_T *)trystack->ga_data)
2972 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002973 // Add one to tcd_cont to be able to jump to
2974 // instruction with index zero.
2975 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002976 iidx = trycmd->tcd_finally_idx == 0
2977 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002978 }
2979 // jump to :finally or :endtry of current try statement
2980 ectx.ec_iidx = iidx;
2981 }
2982 break;
2983
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002984 case ISN_FINALLY:
2985 {
2986 garray_T *trystack = &ectx.ec_trystack;
2987 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2988 + trystack->ga_len - 1;
2989
2990 // Reset the index to avoid a return statement jumps here
2991 // again.
2992 trycmd->tcd_finally_idx = 0;
2993 break;
2994 }
2995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 // end of ":try" block
2997 case ISN_ENDTRY:
2998 {
2999 garray_T *trystack = &ectx.ec_trystack;
3000
3001 if (trystack->ga_len > 0)
3002 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003003 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 --trystack->ga_len;
3006 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02003007 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 trycmd = ((trycmd_T *)trystack->ga_data)
3009 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003010 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 {
3012 // discard the exception
3013 if (caught_stack == current_exception)
3014 caught_stack = caught_stack->caught;
3015 discard_current_exception();
3016 }
3017
3018 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003019 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003020
3021 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
3022 {
3023 --ectx.ec_stack.ga_len;
3024 clear_tv(STACK_TV_BOT(0));
3025 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003026 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003027 // handling :continue: jump to outer try block or
3028 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003029 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 }
3031 }
3032 break;
3033
3034 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003035 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003036 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003037
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003038 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3039 {
3040 // throwing an exception while using "silent!" causes
3041 // the function to abort but not display an error.
3042 tv = STACK_TV_BOT(-1);
3043 clear_tv(tv);
3044 tv->v_type = VAR_NUMBER;
3045 tv->vval.v_number = 0;
3046 goto done;
3047 }
3048 --ectx.ec_stack.ga_len;
3049 tv = STACK_TV_BOT(0);
3050 if (tv->vval.v_string == NULL
3051 || *skipwhite(tv->vval.v_string) == NUL)
3052 {
3053 vim_free(tv->vval.v_string);
3054 SOURCING_LNUM = iptr->isn_lnum;
3055 emsg(_(e_throw_with_empty_string));
3056 goto failed;
3057 }
3058
3059 // Inside a "catch" we need to first discard the caught
3060 // exception.
3061 if (trystack->ga_len > 0)
3062 {
3063 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3064 + trystack->ga_len - 1;
3065 if (trycmd->tcd_caught && current_exception != NULL)
3066 {
3067 // discard the exception
3068 if (caught_stack == current_exception)
3069 caught_stack = caught_stack->caught;
3070 discard_current_exception();
3071 trycmd->tcd_caught = FALSE;
3072 }
3073 }
3074
3075 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3076 == FAIL)
3077 {
3078 vim_free(tv->vval.v_string);
3079 goto failed;
3080 }
3081 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 break;
3084
3085 // compare with special values
3086 case ISN_COMPAREBOOL:
3087 case ISN_COMPARESPECIAL:
3088 {
3089 typval_T *tv1 = STACK_TV_BOT(-2);
3090 typval_T *tv2 = STACK_TV_BOT(-1);
3091 varnumber_T arg1 = tv1->vval.v_number;
3092 varnumber_T arg2 = tv2->vval.v_number;
3093 int res;
3094
3095 switch (iptr->isn_arg.op.op_type)
3096 {
3097 case EXPR_EQUAL: res = arg1 == arg2; break;
3098 case EXPR_NEQUAL: res = arg1 != arg2; break;
3099 default: res = 0; break;
3100 }
3101
3102 --ectx.ec_stack.ga_len;
3103 tv1->v_type = VAR_BOOL;
3104 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3105 }
3106 break;
3107
3108 // Operation with two number arguments
3109 case ISN_OPNR:
3110 case ISN_COMPARENR:
3111 {
3112 typval_T *tv1 = STACK_TV_BOT(-2);
3113 typval_T *tv2 = STACK_TV_BOT(-1);
3114 varnumber_T arg1 = tv1->vval.v_number;
3115 varnumber_T arg2 = tv2->vval.v_number;
3116 varnumber_T res;
3117
3118 switch (iptr->isn_arg.op.op_type)
3119 {
3120 case EXPR_MULT: res = arg1 * arg2; break;
3121 case EXPR_DIV: res = arg1 / arg2; break;
3122 case EXPR_REM: res = arg1 % arg2; break;
3123 case EXPR_SUB: res = arg1 - arg2; break;
3124 case EXPR_ADD: res = arg1 + arg2; break;
3125
3126 case EXPR_EQUAL: res = arg1 == arg2; break;
3127 case EXPR_NEQUAL: res = arg1 != arg2; break;
3128 case EXPR_GREATER: res = arg1 > arg2; break;
3129 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3130 case EXPR_SMALLER: res = arg1 < arg2; break;
3131 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3132 default: res = 0; break;
3133 }
3134
3135 --ectx.ec_stack.ga_len;
3136 if (iptr->isn_type == ISN_COMPARENR)
3137 {
3138 tv1->v_type = VAR_BOOL;
3139 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3140 }
3141 else
3142 tv1->vval.v_number = res;
3143 }
3144 break;
3145
3146 // Computation with two float arguments
3147 case ISN_OPFLOAT:
3148 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003149#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 {
3151 typval_T *tv1 = STACK_TV_BOT(-2);
3152 typval_T *tv2 = STACK_TV_BOT(-1);
3153 float_T arg1 = tv1->vval.v_float;
3154 float_T arg2 = tv2->vval.v_float;
3155 float_T res = 0;
3156 int cmp = FALSE;
3157
3158 switch (iptr->isn_arg.op.op_type)
3159 {
3160 case EXPR_MULT: res = arg1 * arg2; break;
3161 case EXPR_DIV: res = arg1 / arg2; break;
3162 case EXPR_SUB: res = arg1 - arg2; break;
3163 case EXPR_ADD: res = arg1 + arg2; break;
3164
3165 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3166 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3167 case EXPR_GREATER: cmp = arg1 > arg2; break;
3168 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3169 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3170 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3171 default: cmp = 0; break;
3172 }
3173 --ectx.ec_stack.ga_len;
3174 if (iptr->isn_type == ISN_COMPAREFLOAT)
3175 {
3176 tv1->v_type = VAR_BOOL;
3177 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3178 }
3179 else
3180 tv1->vval.v_float = res;
3181 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003182#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 break;
3184
3185 case ISN_COMPARELIST:
3186 {
3187 typval_T *tv1 = STACK_TV_BOT(-2);
3188 typval_T *tv2 = STACK_TV_BOT(-1);
3189 list_T *arg1 = tv1->vval.v_list;
3190 list_T *arg2 = tv2->vval.v_list;
3191 int cmp = FALSE;
3192 int ic = iptr->isn_arg.op.op_ic;
3193
3194 switch (iptr->isn_arg.op.op_type)
3195 {
3196 case EXPR_EQUAL: cmp =
3197 list_equal(arg1, arg2, ic, FALSE); break;
3198 case EXPR_NEQUAL: cmp =
3199 !list_equal(arg1, arg2, ic, FALSE); break;
3200 case EXPR_IS: cmp = arg1 == arg2; break;
3201 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3202 default: cmp = 0; break;
3203 }
3204 --ectx.ec_stack.ga_len;
3205 clear_tv(tv1);
3206 clear_tv(tv2);
3207 tv1->v_type = VAR_BOOL;
3208 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3209 }
3210 break;
3211
3212 case ISN_COMPAREBLOB:
3213 {
3214 typval_T *tv1 = STACK_TV_BOT(-2);
3215 typval_T *tv2 = STACK_TV_BOT(-1);
3216 blob_T *arg1 = tv1->vval.v_blob;
3217 blob_T *arg2 = tv2->vval.v_blob;
3218 int cmp = FALSE;
3219
3220 switch (iptr->isn_arg.op.op_type)
3221 {
3222 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3223 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3224 case EXPR_IS: cmp = arg1 == arg2; break;
3225 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3226 default: cmp = 0; break;
3227 }
3228 --ectx.ec_stack.ga_len;
3229 clear_tv(tv1);
3230 clear_tv(tv2);
3231 tv1->v_type = VAR_BOOL;
3232 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3233 }
3234 break;
3235
3236 // TODO: handle separately
3237 case ISN_COMPARESTRING:
3238 case ISN_COMPAREDICT:
3239 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 case ISN_COMPAREANY:
3241 {
3242 typval_T *tv1 = STACK_TV_BOT(-2);
3243 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003244 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 int ic = iptr->isn_arg.op.op_ic;
3246
Bram Moolenaareb26f432020-09-14 16:50:05 +02003247 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003248 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 --ectx.ec_stack.ga_len;
3251 }
3252 break;
3253
3254 case ISN_ADDLIST:
3255 case ISN_ADDBLOB:
3256 {
3257 typval_T *tv1 = STACK_TV_BOT(-2);
3258 typval_T *tv2 = STACK_TV_BOT(-1);
3259
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003260 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 if (iptr->isn_type == ISN_ADDLIST)
3262 eval_addlist(tv1, tv2);
3263 else
3264 eval_addblob(tv1, tv2);
3265 clear_tv(tv2);
3266 --ectx.ec_stack.ga_len;
3267 }
3268 break;
3269
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003270 case ISN_LISTAPPEND:
3271 {
3272 typval_T *tv1 = STACK_TV_BOT(-2);
3273 typval_T *tv2 = STACK_TV_BOT(-1);
3274 list_T *l = tv1->vval.v_list;
3275
3276 // add an item to a list
3277 if (l == NULL)
3278 {
3279 SOURCING_LNUM = iptr->isn_lnum;
3280 emsg(_(e_cannot_add_to_null_list));
3281 goto on_error;
3282 }
3283 if (list_append_tv(l, tv2) == FAIL)
3284 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003285 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003286 --ectx.ec_stack.ga_len;
3287 }
3288 break;
3289
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003290 case ISN_BLOBAPPEND:
3291 {
3292 typval_T *tv1 = STACK_TV_BOT(-2);
3293 typval_T *tv2 = STACK_TV_BOT(-1);
3294 blob_T *b = tv1->vval.v_blob;
3295 int error = FALSE;
3296 varnumber_T n;
3297
3298 // add a number to a blob
3299 if (b == NULL)
3300 {
3301 SOURCING_LNUM = iptr->isn_lnum;
3302 emsg(_(e_cannot_add_to_null_blob));
3303 goto on_error;
3304 }
3305 n = tv_get_number_chk(tv2, &error);
3306 if (error)
3307 goto on_error;
3308 ga_append(&b->bv_ga, (int)n);
3309 --ectx.ec_stack.ga_len;
3310 }
3311 break;
3312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 // Computation with two arguments of unknown type
3314 case ISN_OPANY:
3315 {
3316 typval_T *tv1 = STACK_TV_BOT(-2);
3317 typval_T *tv2 = STACK_TV_BOT(-1);
3318 varnumber_T n1, n2;
3319#ifdef FEAT_FLOAT
3320 float_T f1 = 0, f2 = 0;
3321#endif
3322 int error = FALSE;
3323
3324 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3325 {
3326 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3327 {
3328 eval_addlist(tv1, tv2);
3329 clear_tv(tv2);
3330 --ectx.ec_stack.ga_len;
3331 break;
3332 }
3333 else if (tv1->v_type == VAR_BLOB
3334 && tv2->v_type == VAR_BLOB)
3335 {
3336 eval_addblob(tv1, tv2);
3337 clear_tv(tv2);
3338 --ectx.ec_stack.ga_len;
3339 break;
3340 }
3341 }
3342#ifdef FEAT_FLOAT
3343 if (tv1->v_type == VAR_FLOAT)
3344 {
3345 f1 = tv1->vval.v_float;
3346 n1 = 0;
3347 }
3348 else
3349#endif
3350 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003351 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 n1 = tv_get_number_chk(tv1, &error);
3353 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003354 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355#ifdef FEAT_FLOAT
3356 if (tv2->v_type == VAR_FLOAT)
3357 f1 = n1;
3358#endif
3359 }
3360#ifdef FEAT_FLOAT
3361 if (tv2->v_type == VAR_FLOAT)
3362 {
3363 f2 = tv2->vval.v_float;
3364 n2 = 0;
3365 }
3366 else
3367#endif
3368 {
3369 n2 = tv_get_number_chk(tv2, &error);
3370 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003371 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372#ifdef FEAT_FLOAT
3373 if (tv1->v_type == VAR_FLOAT)
3374 f2 = n2;
3375#endif
3376 }
3377#ifdef FEAT_FLOAT
3378 // if there is a float on either side the result is a float
3379 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3380 {
3381 switch (iptr->isn_arg.op.op_type)
3382 {
3383 case EXPR_MULT: f1 = f1 * f2; break;
3384 case EXPR_DIV: f1 = f1 / f2; break;
3385 case EXPR_SUB: f1 = f1 - f2; break;
3386 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003387 default: SOURCING_LNUM = iptr->isn_lnum;
3388 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003389 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 }
3391 clear_tv(tv1);
3392 clear_tv(tv2);
3393 tv1->v_type = VAR_FLOAT;
3394 tv1->vval.v_float = f1;
3395 --ectx.ec_stack.ga_len;
3396 }
3397 else
3398#endif
3399 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003400 int failed = FALSE;
3401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 switch (iptr->isn_arg.op.op_type)
3403 {
3404 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003405 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3406 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003407 goto on_error;
3408 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 case EXPR_SUB: n1 = n1 - n2; break;
3410 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003411 default: n1 = num_modulus(n1, n2, &failed);
3412 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003413 goto on_error;
3414 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 }
3416 clear_tv(tv1);
3417 clear_tv(tv2);
3418 tv1->v_type = VAR_NUMBER;
3419 tv1->vval.v_number = n1;
3420 --ectx.ec_stack.ga_len;
3421 }
3422 }
3423 break;
3424
3425 case ISN_CONCAT:
3426 {
3427 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3428 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3429 char_u *res;
3430
3431 res = concat_str(str1, str2);
3432 clear_tv(STACK_TV_BOT(-2));
3433 clear_tv(STACK_TV_BOT(-1));
3434 --ectx.ec_stack.ga_len;
3435 STACK_TV_BOT(-1)->vval.v_string = res;
3436 }
3437 break;
3438
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003439 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003440 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003441 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003442 int is_slice = iptr->isn_type == ISN_STRSLICE;
3443 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003444 char_u *res;
3445
3446 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003447 // string slice: string is at stack-3, first index at
3448 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003449 if (is_slice)
3450 {
3451 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003452 n1 = tv->vval.v_number;
3453 }
3454
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003455 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003456 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003457
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003458 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003459 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003460 if (is_slice)
3461 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003462 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003463 else
3464 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003465 // single character (including composing characters).
3466 // If the index is too big or negative the result is
3467 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003468 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003469 vim_free(tv->vval.v_string);
3470 tv->vval.v_string = res;
3471 }
3472 break;
3473
3474 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003475 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003476 case ISN_BLOBINDEX:
3477 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003479 int is_slice = iptr->isn_type == ISN_LISTSLICE
3480 || iptr->isn_type == ISN_BLOBSLICE;
3481 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3482 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003483 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003484 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485
3486 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003487 // list slice: list is at stack-3, indexes at stack-2 and
3488 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003489 // Same for blob.
3490 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491
3492 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003493 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003495
3496 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 {
Bram Moolenaared591872020-08-15 22:14:53 +02003498 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003499 n1 = tv->vval.v_number;
3500 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 }
Bram Moolenaared591872020-08-15 22:14:53 +02003502
3503 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003504 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003505 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003506 if (is_blob)
3507 {
3508 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3509 n1, n2, FALSE, tv) == FAIL)
3510 goto on_error;
3511 }
3512 else
3513 {
3514 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3515 n1, n2, FALSE, tv, TRUE) == FAIL)
3516 goto on_error;
3517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 }
3519 break;
3520
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003521 case ISN_ANYINDEX:
3522 case ISN_ANYSLICE:
3523 {
3524 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3525 typval_T *var1, *var2;
3526 int res;
3527
3528 // index: composite is at stack-2, index at stack-1
3529 // slice: composite is at stack-3, indexes at stack-2 and
3530 // stack-1
3531 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003532 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003533 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3534 goto on_error;
3535 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3536 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003537 res = eval_index_inner(tv, is_slice, var1, var2,
3538 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003539 clear_tv(var1);
3540 if (is_slice)
3541 clear_tv(var2);
3542 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3543 if (res == FAIL)
3544 goto on_error;
3545 }
3546 break;
3547
Bram Moolenaar9af78762020-06-16 11:34:42 +02003548 case ISN_SLICE:
3549 {
3550 list_T *list;
3551 int count = iptr->isn_arg.number;
3552
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003553 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003554 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003555 list = tv->vval.v_list;
3556
3557 // no error for short list, expect it to be checked earlier
3558 if (list != NULL && list->lv_len >= count)
3559 {
3560 list_T *newlist = list_slice(list,
3561 count, list->lv_len - 1);
3562
3563 if (newlist != NULL)
3564 {
3565 list_unref(list);
3566 tv->vval.v_list = newlist;
3567 ++newlist->lv_refcount;
3568 }
3569 }
3570 }
3571 break;
3572
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003573 case ISN_GETITEM:
3574 {
3575 listitem_T *li;
3576 int index = iptr->isn_arg.number;
3577
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003578 // Get list item: list is at stack-1, push item.
3579 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003580 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003581 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003582
3583 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3584 goto failed;
3585 ++ectx.ec_stack.ga_len;
3586 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003587
3588 // Useful when used in unpack assignment. Reset at
3589 // ISN_DROP.
3590 where.wt_index = index + 1;
3591 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003592 }
3593 break;
3594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 case ISN_MEMBER:
3596 {
3597 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003598 char_u *key;
3599 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003600 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003601
3602 // dict member: dict is at stack-2, key at stack-1
3603 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003604 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003605 dict = tv->vval.v_dict;
3606
3607 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003608 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003609 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003610 if (key == NULL)
3611 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003612
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003613 if ((di = dict_find(dict, key, -1)) == NULL)
3614 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003615 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003616 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003617
3618 // If :silent! is used we will continue, make sure the
3619 // stack contents makes sense.
3620 clear_tv(tv);
3621 --ectx.ec_stack.ga_len;
3622 tv = STACK_TV_BOT(-1);
3623 clear_tv(tv);
3624 tv->v_type = VAR_NUMBER;
3625 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003626 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003627 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003628 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003629 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003630 // Clear the dict only after getting the item, to avoid
3631 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003632 tv = STACK_TV_BOT(-1);
3633 temp_tv = *tv;
3634 copy_tv(&di->di_tv, tv);
3635 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003636 }
3637 break;
3638
3639 // dict member with string key
3640 case ISN_STRINGMEMBER:
3641 {
3642 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003644 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645
3646 tv = STACK_TV_BOT(-1);
3647 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3648 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003649 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003651 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 }
3653 dict = tv->vval.v_dict;
3654
3655 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3656 == NULL)
3657 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003658 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003660 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003662 // Clear the dict after getting the item, to avoid that it
3663 // make the item invalid.
3664 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003666 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 }
3668 break;
3669
3670 case ISN_NEGATENR:
3671 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003672 if (tv->v_type != VAR_NUMBER
3673#ifdef FEAT_FLOAT
3674 && tv->v_type != VAR_FLOAT
3675#endif
3676 )
3677 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003678 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003679 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003680 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003681 }
3682#ifdef FEAT_FLOAT
3683 if (tv->v_type == VAR_FLOAT)
3684 tv->vval.v_float = -tv->vval.v_float;
3685 else
3686#endif
3687 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 break;
3689
3690 case ISN_CHECKNR:
3691 {
3692 int error = FALSE;
3693
3694 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003695 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003697 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 (void)tv_get_number_chk(tv, &error);
3699 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003700 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 }
3702 break;
3703
3704 case ISN_CHECKTYPE:
3705 {
3706 checktype_T *ct = &iptr->isn_arg.type;
3707
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003708 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003709 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003710 if (!where.wt_variable)
3711 where.wt_index = ct->ct_arg_idx;
3712 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003713 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003714 if (!where.wt_variable)
3715 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003716
3717 // number 0 is FALSE, number 1 is TRUE
3718 if (tv->v_type == VAR_NUMBER
3719 && ct->ct_type->tt_type == VAR_BOOL
3720 && (tv->vval.v_number == 0
3721 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003723 tv->v_type = VAR_BOOL;
3724 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003725 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 }
3727 }
3728 break;
3729
Bram Moolenaar9af78762020-06-16 11:34:42 +02003730 case ISN_CHECKLEN:
3731 {
3732 int min_len = iptr->isn_arg.checklen.cl_min_len;
3733 list_T *list = NULL;
3734
3735 tv = STACK_TV_BOT(-1);
3736 if (tv->v_type == VAR_LIST)
3737 list = tv->vval.v_list;
3738 if (list == NULL || list->lv_len < min_len
3739 || (list->lv_len > min_len
3740 && !iptr->isn_arg.checklen.cl_more_OK))
3741 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003742 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003743 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003744 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003745 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003746 }
3747 }
3748 break;
3749
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003750 case ISN_SETTYPE:
3751 {
3752 checktype_T *ct = &iptr->isn_arg.type;
3753
3754 tv = STACK_TV_BOT(-1);
3755 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3756 {
3757 free_type(tv->vval.v_dict->dv_type);
3758 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3759 }
3760 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3761 {
3762 free_type(tv->vval.v_list->lv_type);
3763 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3764 }
3765 }
3766 break;
3767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003769 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 {
3771 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003772 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773
3774 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003775 if (iptr->isn_type == ISN_2BOOL)
3776 {
3777 n = tv2bool(tv);
3778 if (iptr->isn_arg.number) // invert
3779 n = !n;
3780 }
3781 else
3782 {
3783 SOURCING_LNUM = iptr->isn_lnum;
3784 n = tv_get_bool_chk(tv, &error);
3785 if (error)
3786 goto on_error;
3787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 clear_tv(tv);
3789 tv->v_type = VAR_BOOL;
3790 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3791 }
3792 break;
3793
3794 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003795 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003796 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003797 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3798 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3799 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 break;
3801
Bram Moolenaar08597872020-12-10 19:43:40 +01003802 case ISN_RANGE:
3803 {
3804 exarg_T ea;
3805 char *errormsg;
3806
Bram Moolenaarece0b872021-01-08 20:40:45 +01003807 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003808 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003809 ea.addr_type = ADDR_LINES;
3810 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003811 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003812 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003813 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003814
3815 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3816 goto failed;
3817 ++ectx.ec_stack.ga_len;
3818 tv = STACK_TV_BOT(-1);
3819 tv->v_type = VAR_NUMBER;
3820 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003821 if (ea.addr_count == 0)
3822 tv->vval.v_number = curwin->w_cursor.lnum;
3823 else
3824 tv->vval.v_number = ea.line2;
3825 }
3826 break;
3827
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003828 case ISN_PUT:
3829 {
3830 int regname = iptr->isn_arg.put.put_regname;
3831 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3832 char_u *expr = NULL;
3833 int dir = FORWARD;
3834
Bram Moolenaar08597872020-12-10 19:43:40 +01003835 if (lnum < -2)
3836 {
3837 // line number was put on the stack by ISN_RANGE
3838 tv = STACK_TV_BOT(-1);
3839 curwin->w_cursor.lnum = tv->vval.v_number;
3840 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3841 dir = BACKWARD;
3842 --ectx.ec_stack.ga_len;
3843 }
3844 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003845 // :put! above cursor
3846 dir = BACKWARD;
3847 else if (lnum >= 0)
3848 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003849
3850 if (regname == '=')
3851 {
3852 tv = STACK_TV_BOT(-1);
3853 if (tv->v_type == VAR_STRING)
3854 expr = tv->vval.v_string;
3855 else
3856 {
3857 expr = typval2string(tv, TRUE); // allocates value
3858 clear_tv(tv);
3859 }
3860 --ectx.ec_stack.ga_len;
3861 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003862 check_cursor();
3863 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3864 vim_free(expr);
3865 }
3866 break;
3867
Bram Moolenaar02194d22020-10-24 23:08:38 +02003868 case ISN_CMDMOD:
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003869 funclocal.floc_save_cmdmod = cmdmod;
3870 funclocal.floc_restore_cmdmod = TRUE;
3871 funclocal.floc_restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003872 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3873 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003874 break;
3875
Bram Moolenaar02194d22020-10-24 23:08:38 +02003876 case ISN_CMDMOD_REV:
3877 // filter regprog is owned by the instruction, don't free it
3878 cmdmod.cmod_filter_regmatch.regprog = NULL;
3879 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003880 cmdmod = funclocal.floc_save_cmdmod;
3881 funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003882 break;
3883
Bram Moolenaar792f7862020-11-23 08:31:18 +01003884 case ISN_UNPACK:
3885 {
3886 int count = iptr->isn_arg.unpack.unp_count;
3887 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3888 list_T *l;
3889 listitem_T *li;
3890 int i;
3891
3892 // Check there is a valid list to unpack.
3893 tv = STACK_TV_BOT(-1);
3894 if (tv->v_type != VAR_LIST)
3895 {
3896 SOURCING_LNUM = iptr->isn_lnum;
3897 emsg(_(e_for_argument_must_be_sequence_of_lists));
3898 goto on_error;
3899 }
3900 l = tv->vval.v_list;
3901 if (l == NULL
3902 || l->lv_len < (semicolon ? count - 1 : count))
3903 {
3904 SOURCING_LNUM = iptr->isn_lnum;
3905 emsg(_(e_list_value_does_not_have_enough_items));
3906 goto on_error;
3907 }
3908 else if (!semicolon && l->lv_len > count)
3909 {
3910 SOURCING_LNUM = iptr->isn_lnum;
3911 emsg(_(e_list_value_has_more_items_than_targets));
3912 goto on_error;
3913 }
3914
3915 CHECK_LIST_MATERIALIZE(l);
3916 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3917 goto failed;
3918 ectx.ec_stack.ga_len += count - 1;
3919
3920 // Variable after semicolon gets a list with the remaining
3921 // items.
3922 if (semicolon)
3923 {
3924 list_T *rem_list =
3925 list_alloc_with_items(l->lv_len - count + 1);
3926
3927 if (rem_list == NULL)
3928 goto failed;
3929 tv = STACK_TV_BOT(-count);
3930 tv->vval.v_list = rem_list;
3931 ++rem_list->lv_refcount;
3932 tv->v_lock = 0;
3933 li = l->lv_first;
3934 for (i = 0; i < count - 1; ++i)
3935 li = li->li_next;
3936 for (i = 0; li != NULL; ++i)
3937 {
3938 list_set_item(rem_list, i, &li->li_tv);
3939 li = li->li_next;
3940 }
3941 --count;
3942 }
3943
3944 // Produce the values in reverse order, first item last.
3945 li = l->lv_first;
3946 for (i = 0; i < count; ++i)
3947 {
3948 tv = STACK_TV_BOT(-i - 1);
3949 copy_tv(&li->li_tv, tv);
3950 li = li->li_next;
3951 }
3952
3953 list_unref(l);
3954 }
3955 break;
3956
Bram Moolenaarb2049902021-01-24 12:53:53 +01003957 case ISN_PROF_START:
3958 case ISN_PROF_END:
3959 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003960#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003961 funccall_T cookie;
3962 ufunc_T *cur_ufunc =
3963 (((dfunc_T *)def_functions.ga_data)
3964 + ectx.ec_dfunc_idx)->df_ufunc;
3965
3966 cookie.func = cur_ufunc;
3967 if (iptr->isn_type == ISN_PROF_START)
3968 {
3969 func_line_start(&cookie, iptr->isn_lnum);
3970 // if we get here the instruction is executed
3971 func_line_exec(&cookie);
3972 }
3973 else
3974 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003975#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003976 }
3977 break;
3978
Bram Moolenaar389df252020-07-09 21:20:47 +02003979 case ISN_SHUFFLE:
3980 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003981 typval_T tmp_tv;
3982 int item = iptr->isn_arg.shuffle.shfl_item;
3983 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003984
3985 tmp_tv = *STACK_TV_BOT(-item);
3986 for ( ; up > 0 && item > 1; --up)
3987 {
3988 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3989 --item;
3990 }
3991 *STACK_TV_BOT(-item) = tmp_tv;
3992 }
3993 break;
3994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 case ISN_DROP:
3996 --ectx.ec_stack.ga_len;
3997 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003998 where.wt_index = 0;
3999 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 break;
4001 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004002 continue;
4003
Bram Moolenaard032f342020-07-18 18:13:02 +02004004func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004005 // Restore previous function. If the frame pointer is where we started
4006 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02004007 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004008 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004009
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004010 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004011 // only fails when out of memory
4012 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004013 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004014
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004015on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004016 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004017 // If "emsg_silent" is set then ignore the error, unless it was set
4018 // when calling the function.
4019 if (did_emsg_cumul + did_emsg == did_emsg_before
4020 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004021 {
4022 // If a sequence of instructions causes an error while ":silent!"
4023 // was used, restore the stack length and jump ahead to restoring
4024 // the cmdmod.
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004025 if (funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004026 {
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004027 while (ectx.ec_stack.ga_len
4028 > funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004029 {
4030 --ectx.ec_stack.ga_len;
4031 clear_tv(STACK_TV_BOT(0));
4032 }
4033 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
4034 ++ectx.ec_iidx;
4035 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004036 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004037 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004038on_fatal_error:
4039 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004040 // If we are not inside a try-catch started here, abort execution.
4041 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004042 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 }
4044
4045done:
4046 // function finished, get result from the stack.
Bram Moolenaar90193e62021-04-04 20:49:50 +02004047 if (ufunc->uf_ret_type == &t_void)
4048 {
4049 rettv->v_type = VAR_VOID;
4050 }
4051 else
4052 {
4053 tv = STACK_TV_BOT(-1);
4054 *rettv = *tv;
4055 tv->v_type = VAR_UNKNOWN;
4056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 ret = OK;
4058
4059failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004060 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004061 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004062 func_return(&funclocal, &ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004063
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004064 // Deal with any remaining closures, they may be in use somewhere.
4065 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004066 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004067 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004068 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4069 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004070
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004071 estack_pop();
4072 current_sctx = save_current_sctx;
4073
Bram Moolenaarc970e422021-03-17 15:03:04 +01004074 // TODO: when is it safe to delete the function if it is no longer used?
4075 --ufunc->uf_calls;
4076
Bram Moolenaar352134b2020-10-17 22:04:08 +02004077 if (*msg_list != NULL && saved_msg_list != NULL)
4078 {
4079 msglist_T **plist = saved_msg_list;
4080
4081 // Append entries from the current msg_list (uncaught exceptions) to
4082 // the saved msg_list.
4083 while (*plist != NULL)
4084 plist = &(*plist)->next;
4085
4086 *plist = *msg_list;
4087 }
4088 msg_list = saved_msg_list;
4089
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004090 if (funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004091 {
4092 cmdmod.cmod_filter_regmatch.regprog = NULL;
4093 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004094 cmdmod = funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004095 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004096 emsg_silent_def = save_emsg_silent_def;
4097 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004098
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004099failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004100 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4102 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004105 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004106
Bram Moolenaar0186e582021-01-10 18:33:11 +01004107 while (ectx.ec_outer != NULL)
4108 {
4109 outer_T *up = ectx.ec_outer->out_up_is_copy
4110 ? NULL : ectx.ec_outer->out_up;
4111
4112 vim_free(ectx.ec_outer);
4113 ectx.ec_outer = up;
4114 }
4115
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004116 // Not sure if this is necessary.
4117 suppress_errthrow = save_suppress_errthrow;
4118
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004119 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004120 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004121 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004122 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 return ret;
4124}
4125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004127 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01004128 * We don't really need this at runtime, but we do have tests that require it,
4129 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 */
4131 void
4132ex_disassemble(exarg_T *eap)
4133{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004134 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004135 char_u *fname;
4136 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 dfunc_T *dfunc;
4138 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004139 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 int current;
4141 int line_idx = 0;
4142 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004143 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
Bram Moolenaarbfd65582020-07-13 18:18:00 +02004145 if (STRNCMP(arg, "<lambda>", 8) == 0)
4146 {
4147 arg += 8;
4148 (void)getdigits(&arg);
4149 fname = vim_strnsave(eap->arg, arg - eap->arg);
4150 }
4151 else
4152 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004153 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01004154 if (fname == NULL)
4155 {
4156 semsg(_(e_invarg2), eap->arg);
4157 return;
4158 }
4159
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004160 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004161 if (ufunc == NULL)
4162 {
4163 char_u *p = untrans_function_name(fname);
4164
4165 if (p != NULL)
4166 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004167 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004168 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01004169 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 if (ufunc == NULL)
4171 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004172 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 return;
4174 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01004175 if (func_needs_compiling(ufunc, eap->forceit)
4176 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02004177 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004178 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004180 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 return;
4182 }
4183 if (ufunc->uf_name_exp != NULL)
4184 msg((char *)ufunc->uf_name_exp);
4185 else
4186 msg((char *)ufunc->uf_name);
4187
4188 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004189#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004190 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
4191 instr_count = eap->forceit ? dfunc->df_instr_prof_count
4192 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004193#else
4194 instr = dfunc->df_instr;
4195 instr_count = dfunc->df_instr_count;
4196#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004197 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 {
4199 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004200 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201
4202 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
4203 {
4204 if (current > prev_current)
4205 {
4206 msg_puts("\n\n");
4207 prev_current = current;
4208 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004209 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4210 if (line != NULL)
4211 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 }
4213
4214 switch (iptr->isn_type)
4215 {
4216 case ISN_EXEC:
4217 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
4218 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004219 case ISN_EXECCONCAT:
4220 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004221 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004222 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 case ISN_ECHO:
4224 {
4225 echo_T *echo = &iptr->isn_arg.echo;
4226
4227 smsg("%4d %s %d", current,
4228 echo->echo_with_white ? "ECHO" : "ECHON",
4229 echo->echo_count);
4230 }
4231 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004232 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01004233 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004234 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01004235 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004236 case ISN_ECHOMSG:
4237 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004238 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004239 break;
4240 case ISN_ECHOERR:
4241 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004242 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004243 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004245 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004246 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004247 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004248 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004249 + STACK_FRAME_SIZE));
4250 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004251 smsg("%4d LOAD $%lld", current,
4252 (varnumber_T)(iptr->isn_arg.number));
4253 }
4254 break;
4255 case ISN_LOADOUTER:
4256 {
4257 if (iptr->isn_arg.number < 0)
4258 smsg("%4d LOADOUTER level %d arg[%d]", current,
4259 iptr->isn_arg.outer.outer_depth,
4260 iptr->isn_arg.outer.outer_idx
4261 + STACK_FRAME_SIZE);
4262 else
4263 smsg("%4d LOADOUTER level %d $%d", current,
4264 iptr->isn_arg.outer.outer_depth,
4265 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 break;
4268 case ISN_LOADV:
4269 smsg("%4d LOADV v:%s", current,
4270 get_vim_var_name(iptr->isn_arg.number));
4271 break;
4272 case ISN_LOADSCRIPT:
4273 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004274 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4275 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004277 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004279 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4280 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004281 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004282 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 }
4284 break;
4285 case ISN_LOADS:
4286 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004287 scriptitem_T *si = SCRIPT_ITEM(
4288 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289
4290 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004291 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 }
4293 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004294 case ISN_LOADAUTO:
4295 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4296 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 case ISN_LOADG:
4298 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4299 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004300 case ISN_LOADB:
4301 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4302 break;
4303 case ISN_LOADW:
4304 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4305 break;
4306 case ISN_LOADT:
4307 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4308 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004309 case ISN_LOADGDICT:
4310 smsg("%4d LOAD g:", current);
4311 break;
4312 case ISN_LOADBDICT:
4313 smsg("%4d LOAD b:", current);
4314 break;
4315 case ISN_LOADWDICT:
4316 smsg("%4d LOAD w:", current);
4317 break;
4318 case ISN_LOADTDICT:
4319 smsg("%4d LOAD t:", current);
4320 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 case ISN_LOADOPT:
4322 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4323 break;
4324 case ISN_LOADENV:
4325 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4326 break;
4327 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004328 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 break;
4330
4331 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004332 if (iptr->isn_arg.number < 0)
4333 smsg("%4d STORE arg[%lld]", current,
4334 iptr->isn_arg.number + STACK_FRAME_SIZE);
4335 else
4336 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4337 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004338 case ISN_STOREOUTER:
4339 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004340 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004341 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4342 iptr->isn_arg.outer.outer_depth,
4343 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004344 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004345 smsg("%4d STOREOUTER level %d $%d", current,
4346 iptr->isn_arg.outer.outer_depth,
4347 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004350 case ISN_STOREV:
4351 smsg("%4d STOREV v:%s", current,
4352 get_vim_var_name(iptr->isn_arg.number));
4353 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004354 case ISN_STOREAUTO:
4355 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4356 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004358 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4359 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004360 case ISN_STOREB:
4361 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4362 break;
4363 case ISN_STOREW:
4364 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4365 break;
4366 case ISN_STORET:
4367 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4368 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004369 case ISN_STORES:
4370 {
4371 scriptitem_T *si = SCRIPT_ITEM(
4372 iptr->isn_arg.loadstore.ls_sid);
4373
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004374 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004375 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 break;
4378 case ISN_STORESCRIPT:
4379 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004380 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4381 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004383 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004385 smsg("%4d STORESCRIPT %s-%d in %s", current,
4386 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004387 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004388 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 }
4390 break;
4391 case ISN_STOREOPT:
4392 smsg("%4d STOREOPT &%s", current,
4393 iptr->isn_arg.storeopt.so_name);
4394 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004395 case ISN_STOREENV:
4396 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4397 break;
4398 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004399 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004400 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 case ISN_STORENR:
4402 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004403 iptr->isn_arg.storenr.stnr_val,
4404 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 break;
4406
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004407 case ISN_STOREINDEX:
4408 switch (iptr->isn_arg.vartype)
4409 {
4410 case VAR_LIST:
4411 smsg("%4d STORELIST", current);
4412 break;
4413 case VAR_DICT:
4414 smsg("%4d STOREDICT", current);
4415 break;
4416 case VAR_ANY:
4417 smsg("%4d STOREINDEX", current);
4418 break;
4419 default: break;
4420 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004421 break;
4422
Bram Moolenaar68452172021-04-12 21:21:02 +02004423 case ISN_STORERANGE:
4424 smsg("%4d STORERANGE", current);
4425 break;
4426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 // constants
4428 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004429 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004430 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 break;
4432 case ISN_PUSHBOOL:
4433 case ISN_PUSHSPEC:
4434 smsg("%4d PUSH %s", current,
4435 get_var_special_name(iptr->isn_arg.number));
4436 break;
4437 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004438#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004440#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 break;
4442 case ISN_PUSHS:
4443 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4444 break;
4445 case ISN_PUSHBLOB:
4446 {
4447 char_u *r;
4448 char_u numbuf[NUMBUFLEN];
4449 char_u *tofree;
4450
4451 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004452 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 vim_free(tofree);
4454 }
4455 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004456 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004457 {
4458 char *name = (char *)iptr->isn_arg.string;
4459
4460 smsg("%4d PUSHFUNC \"%s\"", current,
4461 name == NULL ? "[none]" : name);
4462 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004463 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004464 case ISN_PUSHCHANNEL:
4465#ifdef FEAT_JOB_CHANNEL
4466 {
4467 channel_T *channel = iptr->isn_arg.channel;
4468
4469 smsg("%4d PUSHCHANNEL %d", current,
4470 channel == NULL ? 0 : channel->ch_id);
4471 }
4472#endif
4473 break;
4474 case ISN_PUSHJOB:
4475#ifdef FEAT_JOB_CHANNEL
4476 {
4477 typval_T tv;
4478 char_u *name;
4479
4480 tv.v_type = VAR_JOB;
4481 tv.vval.v_job = iptr->isn_arg.job;
4482 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004483 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004484 }
4485#endif
4486 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 case ISN_PUSHEXC:
4488 smsg("%4d PUSH v:exception", current);
4489 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004490 case ISN_UNLET:
4491 smsg("%4d UNLET%s %s", current,
4492 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4493 iptr->isn_arg.unlet.ul_name);
4494 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004495 case ISN_UNLETENV:
4496 smsg("%4d UNLETENV%s $%s", current,
4497 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4498 iptr->isn_arg.unlet.ul_name);
4499 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004500 case ISN_UNLETINDEX:
4501 smsg("%4d UNLETINDEX", current);
4502 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004503 case ISN_UNLETRANGE:
4504 smsg("%4d UNLETRANGE", current);
4505 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004506 case ISN_LOCKCONST:
4507 smsg("%4d LOCKCONST", current);
4508 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004510 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004511 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 break;
4513 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004514 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004515 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 break;
4517
4518 // function call
4519 case ISN_BCALL:
4520 {
4521 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4522
4523 smsg("%4d BCALL %s(argc %d)", current,
4524 internal_func_name(cbfunc->cbf_idx),
4525 cbfunc->cbf_argcount);
4526 }
4527 break;
4528 case ISN_DCALL:
4529 {
4530 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4531 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4532 + cdfunc->cdf_idx;
4533
4534 smsg("%4d DCALL %s(argc %d)", current,
4535 df->df_ufunc->uf_name_exp != NULL
4536 ? df->df_ufunc->uf_name_exp
4537 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4538 }
4539 break;
4540 case ISN_UCALL:
4541 {
4542 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4543
4544 smsg("%4d UCALL %s(argc %d)", current,
4545 cufunc->cuf_name, cufunc->cuf_argcount);
4546 }
4547 break;
4548 case ISN_PCALL:
4549 {
4550 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4551
4552 smsg("%4d PCALL%s (argc %d)", current,
4553 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4554 }
4555 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004556 case ISN_PCALL_END:
4557 smsg("%4d PCALL end", current);
4558 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 case ISN_RETURN:
4560 smsg("%4d RETURN", current);
4561 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004562 case ISN_RETURN_ZERO:
4563 smsg("%4d RETURN 0", current);
4564 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 case ISN_FUNCREF:
4566 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004567 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004569 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004571 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 }
4573 break;
4574
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004575 case ISN_NEWFUNC:
4576 {
4577 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4578
4579 smsg("%4d NEWFUNC %s %s", current,
4580 newfunc->nf_lambda, newfunc->nf_global);
4581 }
4582 break;
4583
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004584 case ISN_DEF:
4585 {
4586 char_u *name = iptr->isn_arg.string;
4587
4588 smsg("%4d DEF %s", current,
4589 name == NULL ? (char_u *)"" : name);
4590 }
4591 break;
4592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 case ISN_JUMP:
4594 {
4595 char *when = "?";
4596
4597 switch (iptr->isn_arg.jump.jump_when)
4598 {
4599 case JUMP_ALWAYS:
4600 when = "JUMP";
4601 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 case JUMP_AND_KEEP_IF_TRUE:
4603 when = "JUMP_AND_KEEP_IF_TRUE";
4604 break;
4605 case JUMP_IF_FALSE:
4606 when = "JUMP_IF_FALSE";
4607 break;
4608 case JUMP_AND_KEEP_IF_FALSE:
4609 when = "JUMP_AND_KEEP_IF_FALSE";
4610 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004611 case JUMP_IF_COND_FALSE:
4612 when = "JUMP_IF_COND_FALSE";
4613 break;
4614 case JUMP_IF_COND_TRUE:
4615 when = "JUMP_IF_COND_TRUE";
4616 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004618 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 iptr->isn_arg.jump.jump_where);
4620 }
4621 break;
4622
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004623 case ISN_JUMP_IF_ARG_SET:
4624 smsg("%4d JUMP_IF_ARG_SET arg[%d] -> %d", current,
4625 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4626 iptr->isn_arg.jump.jump_where);
4627 break;
4628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 case ISN_FOR:
4630 {
4631 forloop_T *forloop = &iptr->isn_arg.forloop;
4632
4633 smsg("%4d FOR $%d -> %d", current,
4634 forloop->for_idx, forloop->for_end);
4635 }
4636 break;
4637
4638 case ISN_TRY:
4639 {
4640 try_T *try = &iptr->isn_arg.try;
4641
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004642 if (try->try_ref->try_finally == 0)
4643 smsg("%4d TRY catch -> %d, endtry -> %d",
4644 current,
4645 try->try_ref->try_catch,
4646 try->try_ref->try_endtry);
4647 else
4648 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4649 current,
4650 try->try_ref->try_catch,
4651 try->try_ref->try_finally,
4652 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 }
4654 break;
4655 case ISN_CATCH:
4656 // TODO
4657 smsg("%4d CATCH", current);
4658 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004659 case ISN_TRYCONT:
4660 {
4661 trycont_T *trycont = &iptr->isn_arg.trycont;
4662
4663 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4664 trycont->tct_levels,
4665 trycont->tct_levels == 1 ? "" : "s",
4666 trycont->tct_where);
4667 }
4668 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004669 case ISN_FINALLY:
4670 smsg("%4d FINALLY", current);
4671 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 case ISN_ENDTRY:
4673 smsg("%4d ENDTRY", current);
4674 break;
4675 case ISN_THROW:
4676 smsg("%4d THROW", current);
4677 break;
4678
4679 // expression operations on number
4680 case ISN_OPNR:
4681 case ISN_OPFLOAT:
4682 case ISN_OPANY:
4683 {
4684 char *what;
4685 char *ins;
4686
4687 switch (iptr->isn_arg.op.op_type)
4688 {
4689 case EXPR_MULT: what = "*"; break;
4690 case EXPR_DIV: what = "/"; break;
4691 case EXPR_REM: what = "%"; break;
4692 case EXPR_SUB: what = "-"; break;
4693 case EXPR_ADD: what = "+"; break;
4694 default: what = "???"; break;
4695 }
4696 switch (iptr->isn_type)
4697 {
4698 case ISN_OPNR: ins = "OPNR"; break;
4699 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4700 case ISN_OPANY: ins = "OPANY"; break;
4701 default: ins = "???"; break;
4702 }
4703 smsg("%4d %s %s", current, ins, what);
4704 }
4705 break;
4706
4707 case ISN_COMPAREBOOL:
4708 case ISN_COMPARESPECIAL:
4709 case ISN_COMPARENR:
4710 case ISN_COMPAREFLOAT:
4711 case ISN_COMPARESTRING:
4712 case ISN_COMPAREBLOB:
4713 case ISN_COMPARELIST:
4714 case ISN_COMPAREDICT:
4715 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 case ISN_COMPAREANY:
4717 {
4718 char *p;
4719 char buf[10];
4720 char *type;
4721
4722 switch (iptr->isn_arg.op.op_type)
4723 {
4724 case EXPR_EQUAL: p = "=="; break;
4725 case EXPR_NEQUAL: p = "!="; break;
4726 case EXPR_GREATER: p = ">"; break;
4727 case EXPR_GEQUAL: p = ">="; break;
4728 case EXPR_SMALLER: p = "<"; break;
4729 case EXPR_SEQUAL: p = "<="; break;
4730 case EXPR_MATCH: p = "=~"; break;
4731 case EXPR_IS: p = "is"; break;
4732 case EXPR_ISNOT: p = "isnot"; break;
4733 case EXPR_NOMATCH: p = "!~"; break;
4734 default: p = "???"; break;
4735 }
4736 STRCPY(buf, p);
4737 if (iptr->isn_arg.op.op_ic == TRUE)
4738 strcat(buf, "?");
4739 switch(iptr->isn_type)
4740 {
4741 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4742 case ISN_COMPARESPECIAL:
4743 type = "COMPARESPECIAL"; break;
4744 case ISN_COMPARENR: type = "COMPARENR"; break;
4745 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4746 case ISN_COMPARESTRING:
4747 type = "COMPARESTRING"; break;
4748 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4749 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4750 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4751 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4753 default: type = "???"; break;
4754 }
4755
4756 smsg("%4d %s %s", current, type, buf);
4757 }
4758 break;
4759
4760 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4761 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4762
4763 // expression operations
4764 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004765 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004766 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004767 case ISN_BLOBINDEX: smsg("%4d BLOBINDEX", current); break;
4768 case ISN_BLOBSLICE: smsg("%4d BLOBSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004769 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004770 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004771 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004772 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4774 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004775 case ISN_SLICE: smsg("%4d SLICE %lld",
4776 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 case ISN_GETITEM: smsg("%4d ITEM %lld",
4778 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004779 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4780 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 iptr->isn_arg.string); break;
4782 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4783
4784 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004785 case ISN_CHECKTYPE:
4786 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004787 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004788 char *tofree;
4789
Bram Moolenaare32e5162021-01-21 20:21:29 +01004790 if (ct->ct_arg_idx == 0)
4791 smsg("%4d CHECKTYPE %s stack[%d]", current,
4792 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004793 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004794 else
4795 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4796 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004797 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004798 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004799 vim_free(tofree);
4800 break;
4801 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004802 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4803 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4804 iptr->isn_arg.checklen.cl_min_len);
4805 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004806 case ISN_SETTYPE:
4807 {
4808 char *tofree;
4809
4810 smsg("%4d SETTYPE %s", current,
4811 type_name(iptr->isn_arg.type.ct_type, &tofree));
4812 vim_free(tofree);
4813 break;
4814 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004815 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 case ISN_2BOOL: if (iptr->isn_arg.number)
4817 smsg("%4d INVERT (!val)", current);
4818 else
4819 smsg("%4d 2BOOL (!!val)", current);
4820 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004821 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004822 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004823 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004824 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004825 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004826 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004827 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4828 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004829 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004830 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4831 smsg("%4d PUT %c above range",
4832 current, iptr->isn_arg.put.put_regname);
4833 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4834 smsg("%4d PUT %c range",
4835 current, iptr->isn_arg.put.put_regname);
4836 else
4837 smsg("%4d PUT %c %ld", current,
4838 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004839 (long)iptr->isn_arg.put.put_lnum);
4840 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841
Bram Moolenaar02194d22020-10-24 23:08:38 +02004842 // TODO: summarize modifiers
4843 case ISN_CMDMOD:
4844 {
4845 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004846 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004847 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4848
4849 buf = alloc(len + 1);
4850 if (buf != NULL)
4851 {
4852 (void)produce_cmdmods(
4853 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4854 smsg("%4d CMDMOD %s", current, buf);
4855 vim_free(buf);
4856 }
4857 break;
4858 }
4859 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004860
Bram Moolenaarb2049902021-01-24 12:53:53 +01004861 case ISN_PROF_START:
4862 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4863 break;
4864
4865 case ISN_PROF_END:
4866 smsg("%4d PROFILE END", current);
4867 break;
4868
Bram Moolenaar792f7862020-11-23 08:31:18 +01004869 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4870 iptr->isn_arg.unpack.unp_count,
4871 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4872 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004873 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4874 iptr->isn_arg.shuffle.shfl_item,
4875 iptr->isn_arg.shuffle.shfl_up);
4876 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 case ISN_DROP: smsg("%4d DROP", current); break;
4878 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004879
4880 out_flush(); // output one line at a time
4881 ui_breakcheck();
4882 if (got_int)
4883 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885}
4886
4887/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004888 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 * list, etc. Mostly like what JavaScript does, except that empty list and
4890 * empty dictionary are FALSE.
4891 */
4892 int
4893tv2bool(typval_T *tv)
4894{
4895 switch (tv->v_type)
4896 {
4897 case VAR_NUMBER:
4898 return tv->vval.v_number != 0;
4899 case VAR_FLOAT:
4900#ifdef FEAT_FLOAT
4901 return tv->vval.v_float != 0.0;
4902#else
4903 break;
4904#endif
4905 case VAR_PARTIAL:
4906 return tv->vval.v_partial != NULL;
4907 case VAR_FUNC:
4908 case VAR_STRING:
4909 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4910 case VAR_LIST:
4911 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4912 case VAR_DICT:
4913 return tv->vval.v_dict != NULL
4914 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4915 case VAR_BOOL:
4916 case VAR_SPECIAL:
4917 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4918 case VAR_JOB:
4919#ifdef FEAT_JOB_CHANNEL
4920 return tv->vval.v_job != NULL;
4921#else
4922 break;
4923#endif
4924 case VAR_CHANNEL:
4925#ifdef FEAT_JOB_CHANNEL
4926 return tv->vval.v_channel != NULL;
4927#else
4928 break;
4929#endif
4930 case VAR_BLOB:
4931 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4932 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004933 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 case VAR_VOID:
4935 break;
4936 }
4937 return FALSE;
4938}
4939
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004940 void
4941emsg_using_string_as(typval_T *tv, int as_number)
4942{
4943 semsg(_(as_number ? e_using_string_as_number_str
4944 : e_using_string_as_bool_str),
4945 tv->vval.v_string == NULL
4946 ? (char_u *)"" : tv->vval.v_string);
4947}
4948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949/*
4950 * If "tv" is a string give an error and return FAIL.
4951 */
4952 int
4953check_not_string(typval_T *tv)
4954{
4955 if (tv->v_type == VAR_STRING)
4956 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004957 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 clear_tv(tv);
4959 return FAIL;
4960 }
4961 return OK;
4962}
4963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964
4965#endif // FEAT_EVAL