blob: 1412d08365804bb809e7e958f2d8d3f280d89338 [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
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
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 Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#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 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200140 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
155 static void
156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200168static garray_T dict_stack = GA_EMPTY;
169
170/*
171 * Put a value on the dict stack. This consumes "tv".
172 */
173 static int
174dict_stack_save(typval_T *tv)
175{
176 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000177 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200178 if (ga_grow(&dict_stack, 1) == FAIL)
179 return FAIL;
180 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
181 ++dict_stack.ga_len;
182 return OK;
183}
184
185/*
186 * Get the typval at top of the dict stack.
187 */
188 static typval_T *
189dict_stack_get_tv(void)
190{
191 if (dict_stack.ga_len == 0)
192 return NULL;
193 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
194}
195
196/*
197 * Get the dict at top of the dict stack.
198 */
199 static dict_T *
200dict_stack_get_dict(void)
201{
202 typval_T *tv;
203
204 if (dict_stack.ga_len == 0)
205 return NULL;
206 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
207 if (tv->v_type == VAR_DICT)
208 return tv->vval.v_dict;
209 return NULL;
210}
211
212/*
213 * Drop an item from the dict stack.
214 */
215 static void
216dict_stack_drop(void)
217{
218 if (dict_stack.ga_len == 0)
219 {
220 iemsg("Dict stack underflow");
221 return;
222 }
223 --dict_stack.ga_len;
224 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
225}
226
227/*
228 * Drop items from the dict stack until the length is equal to "len".
229 */
230 static void
231dict_stack_clear(int len)
232{
233 while (dict_stack.ga_len > len)
234 dict_stack_drop();
235}
236
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200237/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000238 * Get a pointer to useful "pt_outer" of "pt".
239 */
240 static outer_T *
241get_pt_outer(partial_T *pt)
242{
243 partial_T *ptref = pt->pt_outer_partial;
244
245 if (ptref == NULL)
246 return &pt->pt_outer;
247
248 // partial using partial (recursively)
249 while (ptref->pt_outer_partial != NULL)
250 ptref = ptref->pt_outer_partial;
251 return &ptref->pt_outer;
252}
253
254/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100256 * This adds a stack frame and sets the instruction pointer to the start of the
257 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200258 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 *
260 * Stack has:
261 * - current arguments (already there)
262 * - omitted optional argument (default values) added here
263 * - stack frame:
264 * - pointer to calling function
265 * - Index of next instruction in calling function
266 * - previous frame pointer
267 * - reserved space for local variables
268 */
269 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100270call_dfunc(
271 int cdf_idx,
272 partial_T *pt,
273 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100274 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100276 int argcount = argcount_arg;
277 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
278 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200279 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100280 int arg_to_add;
281 int vararg_count = 0;
282 int varcount;
283 int idx;
284 estack_T *entry;
285 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200286 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287
288 if (dfunc->df_deleted)
289 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100290 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000291 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100292 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 return FAIL;
294 }
295
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100296#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100297 if (do_profiling == PROF_YES)
298 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200299 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100300 {
301 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
302 + profile_info_ga.ga_len;
303 ++profile_info_ga.ga_len;
304 CLEAR_POINTER(info);
305 profile_may_start_func(info, ufunc,
306 (((dfunc_T *)def_functions.ga_data)
307 + ectx->ec_dfunc_idx)->df_ufunc);
308 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100309 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100310#endif
311
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200312 // Update uf_has_breakpoint if needed.
313 update_has_breakpoint(ufunc);
314
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200315 // When debugging and using "cont" switches to the not-debugged
316 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200317 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
318 {
319 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
320
321 // compile_def_function() may cause def_functions.ga_data to change
322 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
323 }
324 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200325 {
326 if (did_emsg_cumul + did_emsg == did_emsg_before)
327 semsg(_(e_function_is_not_compiled_str),
328 printable_func_name(ufunc));
329 return FAIL;
330 }
331
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200332 if (ufunc->uf_va_name != NULL)
333 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200334 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200335 // Stack at time of call with 2 varargs:
336 // normal_arg
337 // optional_arg
338 // vararg_1
339 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200340 // After creating the list:
341 // normal_arg
342 // optional_arg
343 // vararg-list
344 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200345 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200346 // After creating the list
347 // normal_arg
348 // (space for optional_arg)
349 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200350 vararg_count = argcount - ufunc->uf_args.ga_len;
351 if (vararg_count < 0)
352 vararg_count = 0;
353 else
354 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200355 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200356 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200357
358 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200359 }
360
Bram Moolenaarfe270812020-04-11 22:31:27 +0200361 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200362 if (arg_to_add < 0)
363 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200364 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200365 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200366 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200367 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200368 return FAIL;
369 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200370
371 // Reserve space for:
372 // - missing arguments
373 // - stack frame
374 // - local variables
375 // - if needed: a counter for number of closures created in
376 // ectx->ec_funcrefs.
377 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200378 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 return FAIL;
380
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100381 // If depth of calling is getting too high, don't execute the function.
382 if (funcdepth_increment() == FAIL)
383 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200384 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100385
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100386 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200387 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100388 {
389 floc = ALLOC_ONE(funclocal_T);
390 if (floc == NULL)
391 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200392 *floc = ectx->ec_funclocal;
393 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394 }
395
Bram Moolenaarfe270812020-04-11 22:31:27 +0200396 // Move the vararg-list to below the missing optional arguments.
397 if (vararg_count > 0 && arg_to_add > 0)
398 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100399
400 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200401 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200402 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200403 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
405 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100406 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
407 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200408 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200409 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
410 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100411 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100412 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200413 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414
415 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200416 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000417 {
418 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
419
420 tv->v_type = VAR_NUMBER;
421 tv->vval.v_number = 0;
422 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200423 if (dfunc->df_has_closure)
424 {
425 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
426
427 tv->v_type = VAR_NUMBER;
428 tv->vval.v_number = 0;
429 }
430 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100432 if (pt != NULL || ufunc->uf_partial != NULL
433 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100434 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200435 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100436
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200437 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100438 return FAIL;
439 if (pt != NULL)
440 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000441 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200442 ++pt->pt_refcount;
443 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100444 }
445 else if (ufunc->uf_partial != NULL)
446 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000447 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200448 ++ufunc->uf_partial->pt_refcount;
449 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100450 }
451 else
452 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200453 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200454 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200455 {
456 vim_free(ref);
457 return FAIL;
458 }
459 ref->or_outer_allocated = TRUE;
460 ref->or_outer->out_stack = &ectx->ec_stack;
461 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
462 if (ectx->ec_outer_ref != NULL)
463 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100464 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200465 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100466 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100467 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200468 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100469
Bram Moolenaarc970e422021-03-17 15:03:04 +0100470 ++ufunc->uf_calls;
471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 // Set execution state to the start of the called function.
473 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100474 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100475 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200476 if (entry != NULL)
477 {
478 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200479 // Save the current context so it can be restored on return.
480 entry->es_save_sctx = current_sctx;
481 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200482 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100483
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200484 // Start execution at the first instruction.
485 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486
487 return OK;
488}
489
490// Get pointer to item in the stack.
491#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
492
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000493// Double linked list of funcstack_T in use.
494static funcstack_T *first_funcstack = NULL;
495
496 static void
497add_funcstack_to_list(funcstack_T *funcstack)
498{
499 // Link in list of funcstacks.
500 if (first_funcstack != NULL)
501 first_funcstack->fs_prev = funcstack;
502 funcstack->fs_next = first_funcstack;
503 funcstack->fs_prev = NULL;
504 first_funcstack = funcstack;
505}
506
507 static void
508remove_funcstack_from_list(funcstack_T *funcstack)
509{
510 if (funcstack->fs_prev == NULL)
511 first_funcstack = funcstack->fs_next;
512 else
513 funcstack->fs_prev->fs_next = funcstack->fs_next;
514 if (funcstack->fs_next != NULL)
515 funcstack->fs_next->fs_prev = funcstack->fs_prev;
516}
517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200519 * Used when returning from a function: Check if any closure is still
520 * referenced. If so then move the arguments and variables to a separate piece
521 * of stack to be used when the closure is called.
522 * When "free_arguments" is TRUE the arguments are to be freed.
523 * Returns FAIL when out of memory.
524 */
525 static int
526handle_closure_in_use(ectx_T *ectx, int free_arguments)
527{
528 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
529 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200530 int argcount;
531 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200532 int idx;
533 typval_T *tv;
534 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200535 garray_T *gap = &ectx->ec_funcrefs;
536 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200537
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200538 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 return OK; // function was freed
540 if (dfunc->df_has_closure == 0)
541 return OK; // no closures
542 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
543 closure_count = tv->vval.v_number;
544 if (closure_count == 0)
545 return OK; // no funcrefs created
546
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200547 argcount = ufunc_argcount(dfunc->df_ufunc);
548 top = ectx->ec_frame_idx - argcount;
549
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200550 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200551 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200552 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200553 partial_T *pt;
554 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200555
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200556 if (off < 0)
557 continue; // count is off or already done
558 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200559 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200560 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200561 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200562 int i;
563
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000564 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200565 // unreferenced on return.
566 for (i = 0; i < dfunc->df_varcount; ++i)
567 {
568 typval_T *stv = STACK_TV(ectx->ec_frame_idx
569 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200570 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200571 --refcount;
572 }
573 if (refcount > 1)
574 {
575 closure_in_use = TRUE;
576 break;
577 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200578 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200579 }
580
581 if (closure_in_use)
582 {
583 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
584 typval_T *stack;
585
586 // A closure is using the arguments and/or local variables.
587 // Move them to the called function.
588 if (funcstack == NULL)
589 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000590
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200591 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
592 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200593 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
594 funcstack->fs_ga.ga_data = stack;
595 if (stack == NULL)
596 {
597 vim_free(funcstack);
598 return FAIL;
599 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000600 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200601
602 // Move or copy the arguments.
603 for (idx = 0; idx < argcount; ++idx)
604 {
605 tv = STACK_TV(top + idx);
606 if (free_arguments)
607 {
608 *(stack + idx) = *tv;
609 tv->v_type = VAR_UNKNOWN;
610 }
611 else
612 copy_tv(tv, stack + idx);
613 }
614 // Move the local variables.
615 for (idx = 0; idx < dfunc->df_varcount; ++idx)
616 {
617 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200618
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200619 // A partial created for a local function, that is also used as a
620 // local variable, has a reference count for the variable, thus
621 // will never go down to zero. When all these refcounts are one
622 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000623 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200624 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
625 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200626 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200627
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200628 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200629 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
630 gap->ga_len - closure_count + i])
631 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200632 }
633
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200634 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200635 tv->v_type = VAR_UNKNOWN;
636 }
637
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200638 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200639 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200640 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
641 - closure_count + idx];
642 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200643 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200644 ++funcstack->fs_refcount;
645 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100646 pt->pt_outer.out_stack = &funcstack->fs_ga;
647 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200648 }
649 }
650 }
651
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200652 for (idx = 0; idx < closure_count; ++idx)
653 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
654 - closure_count + idx]);
655 gap->ga_len -= closure_count;
656 if (gap->ga_len == 0)
657 ga_clear(gap);
658
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200659 return OK;
660}
661
662/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200663 * Called when a partial is freed or its reference count goes down to one. The
664 * funcstack may be the only reference to the partials in the local variables.
665 * Go over all of them, the funcref and can be freed if all partials
666 * referencing the funcstack have a reference count of one.
667 */
668 void
669funcstack_check_refcount(funcstack_T *funcstack)
670{
671 int i;
672 garray_T *gap = &funcstack->fs_ga;
673 int done = 0;
674
675 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
676 return;
677 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
678 {
679 typval_T *tv = ((typval_T *)gap->ga_data) + i;
680
681 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
682 && tv->vval.v_partial->pt_funcstack == funcstack
683 && tv->vval.v_partial->pt_refcount == 1)
684 ++done;
685 }
686 if (done == funcstack->fs_min_refcount)
687 {
688 typval_T *stack = gap->ga_data;
689
690 // All partials referencing the funcstack have a reference count of
691 // one, thus the funcstack is no longer of use.
692 for (i = 0; i < gap->ga_len; ++i)
693 clear_tv(stack + i);
694 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000695 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200696 vim_free(funcstack);
697 }
698}
699
700/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000701 * For garbage collecting: set references in all variables referenced by
702 * all funcstacks.
703 */
704 int
705set_ref_in_funcstacks(int copyID)
706{
707 funcstack_T *funcstack;
708
709 for (funcstack = first_funcstack; funcstack != NULL;
710 funcstack = funcstack->fs_next)
711 {
712 typval_T *stack = funcstack->fs_ga.ga_data;
713 int i;
714
715 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
716 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
717 return TRUE; // abort
718 }
719 return FALSE;
720}
721
722/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 * Return from the current function.
724 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200725 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200726func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100729 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200730 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
731 + ectx->ec_dfunc_idx;
732 int argcount = ufunc_argcount(dfunc->df_ufunc);
733 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200734 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100735 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
736 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200737 funclocal_T *floc;
738#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100739 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
740 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741
Bram Moolenaar12d26532021-02-19 19:13:21 +0100742 if (do_profiling == PROF_YES)
743 {
744 ufunc_T *caller = prev_dfunc->df_ufunc;
745
746 if (dfunc->df_ufunc->uf_profiling
747 || (caller != NULL && caller->uf_profiling))
748 {
749 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
750 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
751 --profile_info_ga.ga_len;
752 }
753 }
754#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000755
756 // No check for uf_refcount being zero, cannot think of a way that would
757 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100758 --dfunc->df_ufunc->uf_calls;
759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200761 entry = estack_pop();
762 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200763 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200765 if (handle_closure_in_use(ectx, TRUE) == FAIL)
766 return FAIL;
767
768 // Clear the arguments.
769 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
770 clear_tv(STACK_TV(idx));
771
772 // Clear local variables and temp values, but not the return value.
773 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100774 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100776
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100777 // The return value should be on top of the stack. However, when aborting
778 // it may not be there and ec_frame_idx is the top of the stack.
779 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100780 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100781 ret_idx = 0;
782
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200783 if (ectx->ec_outer_ref != NULL)
784 {
785 if (ectx->ec_outer_ref->or_outer_allocated)
786 vim_free(ectx->ec_outer_ref->or_outer);
787 partial_unref(ectx->ec_outer_ref->or_partial);
788 vim_free(ectx->ec_outer_ref);
789 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100790
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100791 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100792 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100793 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
794 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200795 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
796 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200797 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100798 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100799 floc = (void *)STACK_TV(ectx->ec_frame_idx
800 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200801 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100802 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
803 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200804
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100805 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200806 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100807 else
808 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200809 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100810 vim_free(floc);
811 }
812
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100813 if (ret_idx > 0)
814 {
815 // Reset the stack to the position before the call, with a spot for the
816 // return value, moved there from above the frame.
817 ectx->ec_stack.ga_len = top + 1;
818 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
819 }
820 else
821 // Reset the stack to the position before the call.
822 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200823
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100824 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200825 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200826 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827}
828
829#undef STACK_TV
830
831/*
832 * Prepare arguments and rettv for calling a builtin or user function.
833 */
834 static int
835call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
836{
837 int idx;
838 typval_T *tv;
839
840 // Move arguments from bottom of the stack to argvars[] and add terminator.
841 for (idx = 0; idx < argcount; ++idx)
842 argvars[idx] = *STACK_TV_BOT(idx - argcount);
843 argvars[argcount].v_type = VAR_UNKNOWN;
844
845 // Result replaces the arguments on the stack.
846 if (argcount > 0)
847 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200848 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849 return FAIL;
850 else
851 ++ectx->ec_stack.ga_len;
852
853 // Default return value is zero.
854 tv = STACK_TV_BOT(-1);
855 tv->v_type = VAR_NUMBER;
856 tv->vval.v_number = 0;
857
858 return OK;
859}
860
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200861// Ugly global to avoid passing the execution context around through many
862// layers.
863static ectx_T *current_ectx = NULL;
864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865/*
866 * Call a builtin function by index.
867 */
868 static int
869call_bfunc(int func_idx, int argcount, ectx_T *ectx)
870{
871 typval_T argvars[MAX_FUNC_ARGS];
872 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100873 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200874 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200875 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876
877 if (call_prepare(argcount, argvars, ectx) == FAIL)
878 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200879 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200881 // Call the builtin function. Set "current_ectx" so that when it
882 // recursively invokes call_def_function() a closure context can be set.
883 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200885 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200886 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
888 // Clear the arguments.
889 for (idx = 0; idx < argcount; ++idx)
890 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200891
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100892 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200893 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 return OK;
895}
896
897/*
898 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100899 * If the function is compiled this will add a stack frame and set the
900 * instruction pointer at the start of the function.
901 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200902 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100903 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 */
905 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100906call_ufunc(
907 ufunc_T *ufunc,
908 partial_T *pt,
909 int argcount,
910 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200911 isn_T *iptr,
912 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913{
914 typval_T argvars[MAX_FUNC_ARGS];
915 funcexe_T funcexe;
916 int error;
917 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100918 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200919 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920
Bram Moolenaare99d4222021-06-13 14:01:26 +0200921 if (func_needs_compiling(ufunc, compile_type)
922 && compile_def_function(ufunc, FALSE, compile_type, NULL)
923 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200924 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200925 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100926 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100927 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100928 if (error != FCERR_UNKNOWN)
929 {
930 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000931 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100932 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000933 semsg(_(e_not_enough_arguments_for_function_str),
934 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100935 return FAIL;
936 }
937
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100938 // The function has been compiled, can call it quickly. For a function
939 // that was defined later: we can call it directly next time.
940 if (iptr != NULL)
941 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100942 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100943 iptr->isn_type = ISN_DCALL;
944 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
945 iptr->isn_arg.dfunc.cdf_argcount = argcount;
946 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200947 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
950 if (call_prepare(argcount, argvars, ectx) == FAIL)
951 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200952 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000953 funcexe.fe_evaluate = TRUE;
954 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955
956 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000958 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 // Clear the arguments.
961 for (idx = 0; idx < argcount; ++idx)
962 clear_tv(&argvars[idx]);
963
964 if (error != FCERR_NONE)
965 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000966 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 return FAIL;
968 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100969 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200970 // Error other than from calling the function itself.
971 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 return OK;
973}
974
975/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100976 * If command modifiers were applied restore them.
977 */
978 static void
979may_restore_cmdmod(funclocal_T *funclocal)
980{
981 if (funclocal->floc_restore_cmdmod)
982 {
983 cmdmod.cmod_filter_regmatch.regprog = NULL;
984 undo_cmdmod(&cmdmod);
985 cmdmod = funclocal->floc_save_cmdmod;
986 funclocal->floc_restore_cmdmod = FALSE;
987 }
988}
989
990/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200991 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
992 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200993 */
994 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200995vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200996{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200997 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200998}
999
1000/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 * Execute a function by "name".
1002 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001003 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 * Returns FAIL if not found without an error message.
1005 */
1006 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001007call_by_name(
1008 char_u *name,
1009 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001010 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001011 isn_T *iptr,
1012 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013{
1014 ufunc_T *ufunc;
1015
1016 if (builtin_function(name, -1))
1017 {
1018 int func_idx = find_internal_func(name);
1019
1020 if (func_idx < 0)
1021 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001022 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 return FAIL;
1024 return call_bfunc(func_idx, argcount, ectx);
1025 }
1026
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001027 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001028
1029 if (ufunc == NULL)
1030 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001031 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001032
1033 if (script_autoload(name, TRUE))
1034 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001035 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001036
1037 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001038 return FAIL; // bail out if loading the script caused an error
1039 }
1040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001042 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001043 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001044 {
1045 int i;
1046 typval_T *argv = STACK_TV_BOT(0) - argcount;
1047
1048 // The function can change at runtime, check that the argument
1049 // types are correct.
1050 for (i = 0; i < argcount; ++i)
1051 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001052 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001053
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001054 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001055 type = ufunc->uf_arg_types[i];
1056 else if (ufunc->uf_va_type != NULL)
1057 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001058 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001059 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001060 return FAIL;
1061 }
1062 }
1063
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001064 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066
1067 return FAIL;
1068}
1069
1070 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001071call_partial(
1072 typval_T *tv,
1073 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001074 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001076 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001077 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001079 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001080 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081
1082 if (tv->v_type == VAR_PARTIAL)
1083 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001084 partial_T *pt = tv->vval.v_partial;
1085 int i;
1086
1087 if (pt->pt_argc > 0)
1088 {
1089 // Make space for arguments from the partial, shift the "argcount"
1090 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001091 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001092 return FAIL;
1093 for (i = 1; i <= argcount; ++i)
1094 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1095 ectx->ec_stack.ga_len += pt->pt_argc;
1096 argcount += pt->pt_argc;
1097
1098 // copy the arguments from the partial onto the stack
1099 for (i = 0; i < pt->pt_argc; ++i)
1100 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1101 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001102 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103
1104 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001105 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 name = pt->pt_name;
1108 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001109 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001111 if (name != NULL)
1112 {
1113 char_u fname_buf[FLEN_FIXED + 1];
1114 char_u *tofree = NULL;
1115 int error = FCERR_NONE;
1116 char_u *fname;
1117
1118 // May need to translate <SNR>123_ to K_SNR.
1119 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1120 if (error != FCERR_NONE)
1121 res = FAIL;
1122 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001123 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001124 vim_free(tofree);
1125 }
1126
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001127 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 {
1129 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001130 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001131 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 return FAIL;
1133 }
1134 return OK;
1135}
1136
1137/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001138 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1139 * TRUE.
1140 */
1141 static int
1142error_if_locked(int lock, char *error)
1143{
1144 if (lock & (VAR_LOCKED | VAR_FIXED))
1145 {
1146 emsg(_(error));
1147 return TRUE;
1148 }
1149 return FALSE;
1150}
1151
1152/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001153 * Give an error if "tv" is not a number and return FAIL.
1154 */
1155 static int
1156check_for_number(typval_T *tv)
1157{
1158 if (tv->v_type != VAR_NUMBER)
1159 {
1160 semsg(_(e_expected_str_but_got_str),
1161 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1162 return FAIL;
1163 }
1164 return OK;
1165}
1166
1167/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001168 * Store "tv" in variable "name".
1169 * This is for s: and g: variables.
1170 */
1171 static void
1172store_var(char_u *name, typval_T *tv)
1173{
1174 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001175 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001176
Bram Moolenaard877a572021-04-01 19:42:48 +02001177 if (tv->v_lock)
1178 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001179 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001180 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001181 restore_funccal();
1182}
1183
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001184/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001185 * Convert "tv" to a string.
1186 * Return FAIL if not allowed.
1187 */
1188 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001189do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001190{
1191 if (tv->v_type != VAR_STRING)
1192 {
1193 char_u *str;
1194
1195 if (is_2string_any)
1196 {
1197 switch (tv->v_type)
1198 {
1199 case VAR_SPECIAL:
1200 case VAR_BOOL:
1201 case VAR_NUMBER:
1202 case VAR_FLOAT:
1203 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001204
1205 case VAR_LIST:
1206 if (tolerant)
1207 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001208 char_u *s, *e, *p;
1209 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001210
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001211 ga_init2(&ga, sizeof(char_u *), 1);
1212
1213 // Convert to NL separated items, then
1214 // escape the items and replace the NL with
1215 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001216 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001217 if (str == NULL)
1218 return FAIL;
1219 s = str;
1220 while ((e = vim_strchr(s, '\n')) != NULL)
1221 {
1222 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001223 p = vim_strsave_fnameescape(s,
1224 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001225 if (p != NULL)
1226 {
1227 ga_concat(&ga, p);
1228 ga_concat(&ga, (char_u *)" ");
1229 vim_free(p);
1230 }
1231 s = e + 1;
1232 }
1233 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001234 clear_tv(tv);
1235 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001236 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001237 return OK;
1238 }
1239 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001240 default: to_string_error(tv->v_type);
1241 return FAIL;
1242 }
1243 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001244 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001245 clear_tv(tv);
1246 tv->v_type = VAR_STRING;
1247 tv->vval.v_string = str;
1248 }
1249 return OK;
1250}
1251
1252/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001253 * When the value of "sv" is a null list of dict, allocate it.
1254 */
1255 static void
1256allocate_if_null(typval_T *tv)
1257{
1258 switch (tv->v_type)
1259 {
1260 case VAR_LIST:
1261 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001262 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001263 break;
1264 case VAR_DICT:
1265 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001266 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001267 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001268 case VAR_BLOB:
1269 if (tv->vval.v_blob == NULL)
1270 (void)rettv_blob_alloc(tv);
1271 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001272 default:
1273 break;
1274 }
1275}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001276
Bram Moolenaare7525c52021-01-09 13:20:37 +01001277/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001278 * Return the character "str[index]" where "index" is the character index,
1279 * including composing characters.
1280 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001281 */
1282 char_u *
1283char_from_string(char_u *str, varnumber_T index)
1284{
1285 size_t nbyte = 0;
1286 varnumber_T nchar = index;
1287 size_t slen;
1288
1289 if (str == NULL)
1290 return NULL;
1291 slen = STRLEN(str);
1292
Bram Moolenaarff871402021-03-26 13:34:05 +01001293 // Do the same as for a list: a negative index counts from the end.
1294 // Optimization to check the first byte to be below 0x80 (and no composing
1295 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001296 if (index < 0)
1297 {
1298 int clen = 0;
1299
1300 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001301 {
1302 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1303 ++nbyte;
1304 else if (enc_utf8)
1305 nbyte += utfc_ptr2len(str + nbyte);
1306 else
1307 nbyte += mb_ptr2len(str + nbyte);
1308 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001309 nchar = clen + index;
1310 if (nchar < 0)
1311 // unlike list: index out of range results in empty string
1312 return NULL;
1313 }
1314
1315 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001316 {
1317 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1318 ++nbyte;
1319 else if (enc_utf8)
1320 nbyte += utfc_ptr2len(str + nbyte);
1321 else
1322 nbyte += mb_ptr2len(str + nbyte);
1323 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001324 if (nbyte >= slen)
1325 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001326 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001327}
1328
1329/*
1330 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001331 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001332 * If going over the end return "str_len".
1333 * If "idx" is negative count from the end, -1 is the last character.
1334 * When going over the start return -1.
1335 */
1336 static long
1337char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1338{
1339 varnumber_T nchar = idx;
1340 size_t nbyte = 0;
1341
1342 if (nchar >= 0)
1343 {
1344 while (nchar > 0 && nbyte < str_len)
1345 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001346 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001347 --nchar;
1348 }
1349 }
1350 else
1351 {
1352 nbyte = str_len;
1353 while (nchar < 0 && nbyte > 0)
1354 {
1355 --nbyte;
1356 nbyte -= mb_head_off(str, str + nbyte);
1357 ++nchar;
1358 }
1359 if (nchar < 0)
1360 return -1;
1361 }
1362 return (long)nbyte;
1363}
1364
1365/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001366 * Return the slice "str[first : last]" using character indexes. Composing
1367 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001368 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001369 * Return NULL when the result is empty.
1370 */
1371 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001372string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001373{
1374 long start_byte, end_byte;
1375 size_t slen;
1376
1377 if (str == NULL)
1378 return NULL;
1379 slen = STRLEN(str);
1380 start_byte = char_idx2byte(str, slen, first);
1381 if (start_byte < 0)
1382 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001383 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001384 end_byte = (long)slen;
1385 else
1386 {
1387 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001388 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001389 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001390 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001391 }
1392
1393 if (start_byte >= (long)slen || end_byte <= start_byte)
1394 return NULL;
1395 return vim_strnsave(str + start_byte, end_byte - start_byte);
1396}
1397
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001398/*
1399 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1400 * When "dfunc_idx" is negative don't give an error.
1401 * Returns NULL for an error.
1402 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001403 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001404get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001405{
1406 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001407 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1408 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001409 svar_T *sv;
1410
1411 if (sref->sref_seq != si->sn_script_seq)
1412 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001413 // The script was reloaded after the function was compiled, the
1414 // script_idx may not be valid.
1415 if (dfunc != NULL)
1416 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1417 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001418 return NULL;
1419 }
1420 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001421 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001422 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001423 if (dfunc != NULL)
1424 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001425 return NULL;
1426 }
1427 return sv;
1428}
1429
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001430/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001431 * Function passed to do_cmdline() for splitting a script joined by NL
1432 * characters.
1433 */
1434 static char_u *
1435get_split_sourceline(
1436 int c UNUSED,
1437 void *cookie,
1438 int indent UNUSED,
1439 getline_opt_T options UNUSED)
1440{
1441 source_cookie_T *sp = (source_cookie_T *)cookie;
1442 char_u *p;
1443 char_u *line;
1444
1445 if (*sp->nextline == NUL)
1446 return NULL;
1447 p = vim_strchr(sp->nextline, '\n');
1448 if (p == NULL)
1449 {
1450 line = vim_strsave(sp->nextline);
1451 sp->nextline += STRLEN(sp->nextline);
1452 }
1453 else
1454 {
1455 line = vim_strnsave(sp->nextline, p - sp->nextline);
1456 sp->nextline = p + 1;
1457 }
1458 return line;
1459}
1460
1461/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 * Execute a function by "name".
1463 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001464 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 */
1466 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001467call_eval_func(
1468 char_u *name,
1469 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001470 ectx_T *ectx,
1471 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472{
Bram Moolenaared677f52020-08-12 16:38:10 +02001473 int called_emsg_before = called_emsg;
1474 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001476 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001477 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001479 dictitem_T *v;
1480
1481 v = find_var(name, NULL, FALSE);
1482 if (v == NULL)
1483 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001484 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001485 return FAIL;
1486 }
1487 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1488 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001489 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001490 return FAIL;
1491 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001492 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001494 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495}
1496
1497/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001498 * When a function reference is used, fill a partial with the information
1499 * needed, especially when it is used as a closure.
1500 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001501 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001502fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1503{
1504 pt->pt_func = ufunc;
1505 pt->pt_refcount = 1;
1506
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001507 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001508 {
1509 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1510 + ectx->ec_dfunc_idx;
1511
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001512 // The closure may need to find arguments and local variables in the
1513 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001514 pt->pt_outer.out_stack = &ectx->ec_stack;
1515 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001516 if (ectx->ec_outer_ref != NULL)
1517 {
1518 // The current context already has a context, link to that one.
1519 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1520 if (ectx->ec_outer_ref->or_partial != NULL)
1521 {
1522 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1523 ++pt->pt_outer.out_up_partial->pt_refcount;
1524 }
1525 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001526
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001527 // If this function returns and the closure is still being used, we
1528 // need to make a copy of the context (arguments and local variables).
1529 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001530 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001531 {
1532 vim_free(pt);
1533 return FAIL;
1534 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001535 // Extra variable keeps the count of closures created in the current
1536 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001537 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1538 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1539
1540 ((partial_T **)ectx->ec_funcrefs.ga_data)
1541 [ectx->ec_funcrefs.ga_len] = pt;
1542 ++pt->pt_refcount;
1543 ++ectx->ec_funcrefs.ga_len;
1544 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001545 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001546 return OK;
1547}
1548
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001549/*
1550 * Execute iptr->isn_arg.string as an Ex command.
1551 */
1552 static int
1553exec_command(isn_T *iptr)
1554{
1555 source_cookie_T cookie;
1556
1557 SOURCING_LNUM = iptr->isn_lnum;
1558 // Pass getsourceline to get an error for a missing ":end"
1559 // command.
1560 CLEAR_FIELD(cookie);
1561 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1562 if (do_cmdline(iptr->isn_arg.string,
1563 getsourceline, &cookie,
1564 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1565 || did_emsg)
1566 return FAIL;
1567 return OK;
1568}
1569
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001570// used for v_instr of typval of VAR_INSTR
1571struct instr_S {
1572 ectx_T *instr_ectx;
1573 isn_T *instr_instr;
1574};
1575
Bram Moolenaar4c137212021-04-19 16:48:48 +02001576// used for substitute_instr
1577typedef struct subs_expr_S {
1578 ectx_T *subs_ectx;
1579 isn_T *subs_instr;
1580 int subs_status;
1581} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582
1583// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001584#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585
1586// Get pointer to item at the bottom of the stack, -1 is the bottom.
1587#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001588#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 +01001589
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001590// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001591#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 +01001592
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001593// Set when calling do_debug().
1594static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001595static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001596
1597/*
1598 * When debugging lookup "name" and return the typeval.
1599 * When not found return NULL.
1600 */
1601 typval_T *
1602lookup_debug_var(char_u *name)
1603{
1604 int idx;
1605 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001606 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001607 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001608 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001609
1610 if (ectx == NULL)
1611 return NULL;
1612 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1613
1614 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001615 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001616 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001617 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001618 return STACK_TV_VAR(idx);
1619 }
1620
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001621 // Go through argument names.
1622 ufunc = dfunc->df_ufunc;
1623 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1624 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1625 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1626 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1627 - varargs_off + idx);
1628 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1629 return STACK_TV(ectx->ec_frame_idx - 1);
1630
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001631 return NULL;
1632}
1633
Bram Moolenaar26a44842021-09-02 18:49:06 +02001634/*
1635 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1636 * breakpoint was set in that function or when there is any expression.
1637 */
1638 int
1639may_break_in_function(ufunc_T *ufunc)
1640{
1641 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1642}
1643
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001644 static void
1645handle_debug(isn_T *iptr, ectx_T *ectx)
1646{
1647 char_u *line;
1648 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1649 + ectx->ec_dfunc_idx)->df_ufunc;
1650 isn_T *ni;
1651 int end_lnum = iptr->isn_lnum;
1652 garray_T ga;
1653 int lnum;
1654
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001655 if (ex_nesting_level > debug_break_level)
1656 {
1657 linenr_T breakpoint;
1658
Bram Moolenaar26a44842021-09-02 18:49:06 +02001659 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001660 return;
1661
1662 // check for the next breakpoint if needed
1663 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001664 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001665 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1666 return;
1667 }
1668
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001669 SOURCING_LNUM = iptr->isn_lnum;
1670 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001671 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001672
1673 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1674 if (ni->isn_type == ISN_DEBUG
1675 || ni->isn_type == ISN_RETURN
1676 || ni->isn_type == ISN_RETURN_VOID)
1677 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001678 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001679 break;
1680 }
1681
1682 if (end_lnum > iptr->isn_lnum)
1683 {
1684 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001685 for (lnum = iptr->isn_lnum; lnum < end_lnum
1686 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001687 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001688 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001689
Bram Moolenaar303215d2021-07-07 20:10:43 +02001690 if (p == NULL)
1691 continue; // left over from continuation line
1692 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001693 if (*p == '#')
1694 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001695 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001696 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1697 if (STRNCMP(p, "def ", 4) == 0)
1698 break;
1699 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001700 line = ga_concat_strings(&ga, " ");
1701 vim_free(ga.ga_data);
1702 }
1703 else
1704 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001705
Dominique Pelle6e969552021-06-17 13:53:41 +02001706 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001707 debug_context = NULL;
1708
1709 if (end_lnum > iptr->isn_lnum)
1710 vim_free(line);
1711}
1712
Bram Moolenaar4c137212021-04-19 16:48:48 +02001713/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001714 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001715 * Returns OK, FAIL or NOTDONE (uncatchable error).
1716 */
1717 static int
1718execute_storeindex(isn_T *iptr, ectx_T *ectx)
1719{
1720 vartype_T dest_type = iptr->isn_arg.vartype;
1721 typval_T *tv;
1722 typval_T *tv_idx = STACK_TV_BOT(-2);
1723 typval_T *tv_dest = STACK_TV_BOT(-1);
1724 int status = OK;
1725
1726 // Stack contains:
1727 // -3 value to be stored
1728 // -2 index
1729 // -1 dict or list
1730 tv = STACK_TV_BOT(-3);
1731 SOURCING_LNUM = iptr->isn_lnum;
1732 if (dest_type == VAR_ANY)
1733 {
1734 dest_type = tv_dest->v_type;
1735 if (dest_type == VAR_DICT)
1736 status = do_2string(tv_idx, TRUE, FALSE);
1737 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1738 {
1739 emsg(_(e_number_expected));
1740 status = FAIL;
1741 }
1742 }
1743 else if (dest_type != tv_dest->v_type)
1744 {
1745 // just in case, should be OK
1746 semsg(_(e_expected_str_but_got_str),
1747 vartype_name(dest_type),
1748 vartype_name(tv_dest->v_type));
1749 status = FAIL;
1750 }
1751
1752 if (status == OK && dest_type == VAR_LIST)
1753 {
1754 long lidx = (long)tv_idx->vval.v_number;
1755 list_T *list = tv_dest->vval.v_list;
1756
1757 if (list == NULL)
1758 {
1759 emsg(_(e_list_not_set));
1760 return FAIL;
1761 }
1762 if (lidx < 0 && list->lv_len + lidx >= 0)
1763 // negative index is relative to the end
1764 lidx = list->lv_len + lidx;
1765 if (lidx < 0 || lidx > list->lv_len)
1766 {
1767 semsg(_(e_list_index_out_of_range_nr), lidx);
1768 return FAIL;
1769 }
1770 if (lidx < list->lv_len)
1771 {
1772 listitem_T *li = list_find(list, lidx);
1773
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001774 if (error_if_locked(li->li_tv.v_lock,
1775 e_cannot_change_locked_list_item))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001776 return FAIL;
1777 // overwrite existing list item
1778 clear_tv(&li->li_tv);
1779 li->li_tv = *tv;
1780 }
1781 else
1782 {
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001783 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001784 return FAIL;
1785 // append to list, only fails when out of memory
1786 if (list_append_tv(list, tv) == FAIL)
1787 return NOTDONE;
1788 clear_tv(tv);
1789 }
1790 }
1791 else if (status == OK && dest_type == VAR_DICT)
1792 {
1793 char_u *key = tv_idx->vval.v_string;
1794 dict_T *dict = tv_dest->vval.v_dict;
1795 dictitem_T *di;
1796
1797 SOURCING_LNUM = iptr->isn_lnum;
1798 if (dict == NULL)
1799 {
1800 emsg(_(e_dictionary_not_set));
1801 return FAIL;
1802 }
1803 if (key == NULL)
1804 key = (char_u *)"";
1805 di = dict_find(dict, key, -1);
1806 if (di != NULL)
1807 {
1808 if (error_if_locked(di->di_tv.v_lock, e_cannot_change_dict_item))
1809 return FAIL;
1810 // overwrite existing value
1811 clear_tv(&di->di_tv);
1812 di->di_tv = *tv;
1813 }
1814 else
1815 {
1816 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1817 return FAIL;
1818 // add to dict, only fails when out of memory
1819 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1820 return NOTDONE;
1821 clear_tv(tv);
1822 }
1823 }
1824 else if (status == OK && dest_type == VAR_BLOB)
1825 {
1826 long lidx = (long)tv_idx->vval.v_number;
1827 blob_T *blob = tv_dest->vval.v_blob;
1828 varnumber_T nr;
1829 int error = FALSE;
1830 int len;
1831
1832 if (blob == NULL)
1833 {
1834 emsg(_(e_blob_not_set));
1835 return FAIL;
1836 }
1837 len = blob_len(blob);
1838 if (lidx < 0 && len + lidx >= 0)
1839 // negative index is relative to the end
1840 lidx = len + lidx;
1841
1842 // Can add one byte at the end.
1843 if (lidx < 0 || lidx > len)
1844 {
1845 semsg(_(e_blob_index_out_of_range_nr), lidx);
1846 return FAIL;
1847 }
1848 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1849 return FAIL;
1850 nr = tv_get_number_chk(tv, &error);
1851 if (error)
1852 return FAIL;
1853 blob_set_append(blob, lidx, nr);
1854 }
1855 else
1856 {
1857 status = FAIL;
1858 semsg(_(e_cannot_index_str), vartype_name(dest_type));
1859 }
1860
1861 clear_tv(tv_idx);
1862 clear_tv(tv_dest);
1863 ectx->ec_stack.ga_len -= 3;
1864 if (status == FAIL)
1865 {
1866 clear_tv(tv);
1867 return FAIL;
1868 }
1869 return OK;
1870}
1871
1872/*
1873 * Store a value in a blob range.
1874 */
1875 static int
1876execute_storerange(isn_T *iptr, ectx_T *ectx)
1877{
1878 typval_T *tv;
1879 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1880 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1881 typval_T *tv_dest = STACK_TV_BOT(-1);
1882 int status = OK;
1883
1884 // Stack contains:
1885 // -4 value to be stored
1886 // -3 first index or "none"
1887 // -2 second index or "none"
1888 // -1 destination list or blob
1889 tv = STACK_TV_BOT(-4);
1890 if (tv_dest->v_type == VAR_LIST)
1891 {
1892 long n1;
1893 long n2;
1894 int error = FALSE;
1895
1896 SOURCING_LNUM = iptr->isn_lnum;
1897 n1 = (long)tv_get_number_chk(tv_idx1, &error);
1898 if (error)
1899 status = FAIL;
1900 else
1901 {
1902 if (tv_idx2->v_type == VAR_SPECIAL
1903 && tv_idx2->vval.v_number == VVAL_NONE)
1904 n2 = list_len(tv_dest->vval.v_list) - 1;
1905 else
1906 n2 = (long)tv_get_number_chk(tv_idx2, &error);
1907 if (error)
1908 status = FAIL;
1909 else
1910 {
1911 listitem_T *li1 = check_range_index_one(
1912 tv_dest->vval.v_list, &n1, FALSE);
1913
1914 if (li1 == NULL)
1915 status = FAIL;
1916 else
1917 {
1918 status = check_range_index_two(
1919 tv_dest->vval.v_list,
1920 &n1, li1, &n2, FALSE);
1921 if (status != FAIL)
1922 status = list_assign_range(
1923 tv_dest->vval.v_list,
1924 tv->vval.v_list,
1925 n1,
1926 n2,
1927 tv_idx2->v_type == VAR_SPECIAL,
1928 (char_u *)"=",
1929 (char_u *)"[unknown]");
1930 }
1931 }
1932 }
1933 }
1934 else if (tv_dest->v_type == VAR_BLOB)
1935 {
1936 varnumber_T n1;
1937 varnumber_T n2;
1938 int error = FALSE;
1939
1940 n1 = tv_get_number_chk(tv_idx1, &error);
1941 if (error)
1942 status = FAIL;
1943 else
1944 {
1945 if (tv_idx2->v_type == VAR_SPECIAL
1946 && tv_idx2->vval.v_number == VVAL_NONE)
1947 n2 = blob_len(tv_dest->vval.v_blob) - 1;
1948 else
1949 n2 = tv_get_number_chk(tv_idx2, &error);
1950 if (error)
1951 status = FAIL;
1952 else
1953 {
1954 long bloblen = blob_len(tv_dest->vval.v_blob);
1955
1956 if (check_blob_index(bloblen,
1957 n1, FALSE) == FAIL
1958 || check_blob_range(bloblen,
1959 n1, n2, FALSE) == FAIL)
1960 status = FAIL;
1961 else
1962 status = blob_set_range(
1963 tv_dest->vval.v_blob, n1, n2, tv);
1964 }
1965 }
1966 }
1967 else
1968 {
1969 status = FAIL;
1970 emsg(_(e_blob_required));
1971 }
1972
1973 clear_tv(tv_idx1);
1974 clear_tv(tv_idx2);
1975 clear_tv(tv_dest);
1976 ectx->ec_stack.ga_len -= 4;
1977 clear_tv(tv);
1978
1979 return status;
1980}
1981
1982/*
1983 * Unlet item in list or dict variable.
1984 */
1985 static int
1986execute_unletindex(isn_T *iptr, ectx_T *ectx)
1987{
1988 typval_T *tv_idx = STACK_TV_BOT(-2);
1989 typval_T *tv_dest = STACK_TV_BOT(-1);
1990 int status = OK;
1991
1992 // Stack contains:
1993 // -2 index
1994 // -1 dict or list
1995 if (tv_dest->v_type == VAR_DICT)
1996 {
1997 // unlet a dict item, index must be a string
1998 if (tv_idx->v_type != VAR_STRING)
1999 {
2000 SOURCING_LNUM = iptr->isn_lnum;
2001 semsg(_(e_expected_str_but_got_str),
2002 vartype_name(VAR_STRING),
2003 vartype_name(tv_idx->v_type));
2004 status = FAIL;
2005 }
2006 else
2007 {
2008 dict_T *d = tv_dest->vval.v_dict;
2009 char_u *key = tv_idx->vval.v_string;
2010 dictitem_T *di = NULL;
2011
2012 if (d != NULL && value_check_lock(
2013 d->dv_lock, NULL, FALSE))
2014 status = FAIL;
2015 else
2016 {
2017 SOURCING_LNUM = iptr->isn_lnum;
2018 if (key == NULL)
2019 key = (char_u *)"";
2020 if (d != NULL)
2021 di = dict_find(d, key, (int)STRLEN(key));
2022 if (di == NULL)
2023 {
2024 // NULL dict is equivalent to empty dict
2025 semsg(_(e_key_not_present_in_dictionary),
2026 key);
2027 status = FAIL;
2028 }
2029 else if (var_check_fixed(di->di_flags,
2030 NULL, FALSE)
2031 || var_check_ro(di->di_flags,
2032 NULL, FALSE))
2033 status = FAIL;
2034 else
2035 dictitem_remove(d, di);
2036 }
2037 }
2038 }
2039 else if (tv_dest->v_type == VAR_LIST)
2040 {
2041 // unlet a List item, index must be a number
2042 SOURCING_LNUM = iptr->isn_lnum;
2043 if (check_for_number(tv_idx) == FAIL)
2044 {
2045 status = FAIL;
2046 }
2047 else
2048 {
2049 list_T *l = tv_dest->vval.v_list;
2050 long n = (long)tv_idx->vval.v_number;
2051
2052 if (l != NULL && value_check_lock(
2053 l->lv_lock, NULL, FALSE))
2054 status = FAIL;
2055 else
2056 {
2057 listitem_T *li = list_find(l, n);
2058
2059 if (li == NULL)
2060 {
2061 SOURCING_LNUM = iptr->isn_lnum;
2062 semsg(_(e_list_index_out_of_range_nr), n);
2063 status = FAIL;
2064 }
2065 else if (value_check_lock(li->li_tv.v_lock,
2066 NULL, FALSE))
2067 status = FAIL;
2068 else
2069 listitem_remove(l, li);
2070 }
2071 }
2072 }
2073 else
2074 {
2075 status = FAIL;
2076 semsg(_(e_cannot_index_str),
2077 vartype_name(tv_dest->v_type));
2078 }
2079
2080 clear_tv(tv_idx);
2081 clear_tv(tv_dest);
2082 ectx->ec_stack.ga_len -= 2;
2083
2084 return status;
2085}
2086
2087/*
2088 * Unlet a range of items in a list variable.
2089 */
2090 static int
2091execute_unletrange(isn_T *iptr, ectx_T *ectx)
2092{
2093 // Stack contains:
2094 // -3 index1
2095 // -2 index2
2096 // -1 dict or list
2097 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2098 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2099 typval_T *tv_dest = STACK_TV_BOT(-1);
2100 int status = OK;
2101
2102 if (tv_dest->v_type == VAR_LIST)
2103 {
2104 // indexes must be a number
2105 SOURCING_LNUM = iptr->isn_lnum;
2106 if (check_for_number(tv_idx1) == FAIL
2107 || (tv_idx2->v_type != VAR_SPECIAL
2108 && check_for_number(tv_idx2) == FAIL))
2109 {
2110 status = FAIL;
2111 }
2112 else
2113 {
2114 list_T *l = tv_dest->vval.v_list;
2115 long n1 = (long)tv_idx1->vval.v_number;
2116 long n2 = tv_idx2->v_type == VAR_SPECIAL
2117 ? 0 : (long)tv_idx2->vval.v_number;
2118 listitem_T *li;
2119
2120 li = list_find_index(l, &n1);
2121 if (li == NULL)
2122 status = FAIL;
2123 else
2124 {
2125 if (n1 < 0)
2126 n1 = list_idx_of_item(l, li);
2127 if (n2 < 0)
2128 {
2129 listitem_T *li2 = list_find(l, n2);
2130
2131 if (li2 == NULL)
2132 status = FAIL;
2133 else
2134 n2 = list_idx_of_item(l, li2);
2135 }
2136 if (status != FAIL
2137 && tv_idx2->v_type != VAR_SPECIAL
2138 && n2 < n1)
2139 {
2140 semsg(_(e_list_index_out_of_range_nr), n2);
2141 status = FAIL;
2142 }
2143 if (status != FAIL
2144 && list_unlet_range(l, li, NULL, n1,
2145 tv_idx2->v_type != VAR_SPECIAL, n2)
2146 == FAIL)
2147 status = FAIL;
2148 }
2149 }
2150 }
2151 else
2152 {
2153 status = FAIL;
2154 SOURCING_LNUM = iptr->isn_lnum;
2155 semsg(_(e_cannot_index_str),
2156 vartype_name(tv_dest->v_type));
2157 }
2158
2159 clear_tv(tv_idx1);
2160 clear_tv(tv_idx2);
2161 clear_tv(tv_dest);
2162 ectx->ec_stack.ga_len -= 3;
2163
2164 return status;
2165}
2166
2167/*
2168 * Top of a for loop.
2169 */
2170 static int
2171execute_for(isn_T *iptr, ectx_T *ectx)
2172{
2173 typval_T *tv;
2174 typval_T *ltv = STACK_TV_BOT(-1);
2175 typval_T *idxtv =
2176 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2177
2178 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2179 return FAIL;
2180 if (ltv->v_type == VAR_LIST)
2181 {
2182 list_T *list = ltv->vval.v_list;
2183
2184 // push the next item from the list
2185 ++idxtv->vval.v_number;
2186 if (list == NULL
2187 || idxtv->vval.v_number >= list->lv_len)
2188 {
2189 // past the end of the list, jump to "endfor"
2190 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2191 may_restore_cmdmod(&ectx->ec_funclocal);
2192 }
2193 else if (list->lv_first == &range_list_item)
2194 {
2195 // non-materialized range() list
2196 tv = STACK_TV_BOT(0);
2197 tv->v_type = VAR_NUMBER;
2198 tv->v_lock = 0;
2199 tv->vval.v_number = list_find_nr(
2200 list, idxtv->vval.v_number, NULL);
2201 ++ectx->ec_stack.ga_len;
2202 }
2203 else
2204 {
2205 listitem_T *li = list_find(list,
2206 idxtv->vval.v_number);
2207
2208 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2209 ++ectx->ec_stack.ga_len;
2210 }
2211 }
2212 else if (ltv->v_type == VAR_STRING)
2213 {
2214 char_u *str = ltv->vval.v_string;
2215
2216 // The index is for the last byte of the previous
2217 // character.
2218 ++idxtv->vval.v_number;
2219 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2220 {
2221 // past the end of the string, jump to "endfor"
2222 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2223 may_restore_cmdmod(&ectx->ec_funclocal);
2224 }
2225 else
2226 {
2227 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2228
2229 // Push the next character from the string.
2230 tv = STACK_TV_BOT(0);
2231 tv->v_type = VAR_STRING;
2232 tv->vval.v_string = vim_strnsave(
2233 str + idxtv->vval.v_number, clen);
2234 ++ectx->ec_stack.ga_len;
2235 idxtv->vval.v_number += clen - 1;
2236 }
2237 }
2238 else if (ltv->v_type == VAR_BLOB)
2239 {
2240 blob_T *blob = ltv->vval.v_blob;
2241
2242 // When we get here the first time make a copy of the
2243 // blob, so that the iteration still works when it is
2244 // changed.
2245 if (idxtv->vval.v_number == -1 && blob != NULL)
2246 {
2247 blob_copy(blob, ltv);
2248 blob_unref(blob);
2249 blob = ltv->vval.v_blob;
2250 }
2251
2252 // The index is for the previous byte.
2253 ++idxtv->vval.v_number;
2254 if (blob == NULL
2255 || idxtv->vval.v_number >= blob_len(blob))
2256 {
2257 // past the end of the blob, jump to "endfor"
2258 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2259 may_restore_cmdmod(&ectx->ec_funclocal);
2260 }
2261 else
2262 {
2263 // Push the next byte from the blob.
2264 tv = STACK_TV_BOT(0);
2265 tv->v_type = VAR_NUMBER;
2266 tv->vval.v_number = blob_get(blob,
2267 idxtv->vval.v_number);
2268 ++ectx->ec_stack.ga_len;
2269 }
2270 }
2271 else
2272 {
2273 semsg(_(e_for_loop_on_str_not_supported),
2274 vartype_name(ltv->v_type));
2275 return FAIL;
2276 }
2277 return OK;
2278}
2279
2280/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002281 * Load instruction for w:/b:/g:/t: variable.
2282 * "isn_type" is used instead of "iptr->isn_type".
2283 */
2284 static int
2285load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2286{
2287 dictitem_T *di = NULL;
2288 hashtab_T *ht = NULL;
2289 char namespace;
2290
2291 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2292 return NOTDONE;
2293 switch (isn_type)
2294 {
2295 case ISN_LOADG:
2296 ht = get_globvar_ht();
2297 namespace = 'g';
2298 break;
2299 case ISN_LOADB:
2300 ht = &curbuf->b_vars->dv_hashtab;
2301 namespace = 'b';
2302 break;
2303 case ISN_LOADW:
2304 ht = &curwin->w_vars->dv_hashtab;
2305 namespace = 'w';
2306 break;
2307 case ISN_LOADT:
2308 ht = &curtab->tp_vars->dv_hashtab;
2309 namespace = 't';
2310 break;
2311 default: // Cannot reach here
2312 return NOTDONE;
2313 }
2314 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2315
2316 if (di == NULL && ht == get_globvar_ht()
2317 && vim_strchr(iptr->isn_arg.string,
2318 AUTOLOAD_CHAR) != NULL)
2319 {
2320 // Global variable has an autoload name, may still need
2321 // to load the script.
2322 if (script_autoload(iptr->isn_arg.string, FALSE))
2323 di = find_var_in_ht(ht, 0,
2324 iptr->isn_arg.string, TRUE);
2325 if (did_emsg)
2326 return FAIL;
2327 }
2328
2329 if (di == NULL)
2330 {
2331 SOURCING_LNUM = iptr->isn_lnum;
2332 if (vim_strchr(iptr->isn_arg.string,
2333 AUTOLOAD_CHAR) != NULL)
2334 // no check if the item exists in the script but
2335 // isn't exported, it is too complicated
2336 semsg(_(e_item_not_found_in_script_str),
2337 iptr->isn_arg.string);
2338 else
2339 semsg(_(e_undefined_variable_char_str),
2340 namespace, iptr->isn_arg.string);
2341 return FAIL;
2342 }
2343 else
2344 {
2345 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2346 ++ectx->ec_stack.ga_len;
2347 }
2348 return OK;
2349}
2350
2351/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002352 * Execute instructions in execution context "ectx".
2353 * Return OK or FAIL;
2354 */
2355 static int
2356exec_instructions(ectx_T *ectx)
2357{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002358 int ret = FAIL;
2359 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002360 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002361
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002362 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002363 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002364
Bram Moolenaarff652882021-05-16 15:24:49 +02002365 // Only catch exceptions in this instruction list.
2366 ectx->ec_trylevel_at_start = trylevel;
2367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 for (;;)
2369 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002370 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002372 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002373
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002374 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002375 {
2376 line_breakcheck();
2377 breakcheck_count = 0;
2378 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002379 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002380 {
2381 // Turn CTRL-C into an exception.
2382 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002383 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002384 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002385 did_throw = TRUE;
2386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002388 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002389 {
2390 // Turn an error message into an exception.
2391 did_emsg = FALSE;
2392 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002393 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002394 did_throw = TRUE;
2395 *msg_list = NULL;
2396 }
2397
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002398 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002400 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002401 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002402 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403
2404 // An exception jumps to the first catch, finally, or returns from
2405 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002406 while (index > 0)
2407 {
2408 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002409 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002410 break;
2411 // In the catch and finally block of this try we have to go up
2412 // one level.
2413 --index;
2414 trycmd = NULL;
2415 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002416 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002418 if (trycmd->tcd_in_catch)
2419 {
2420 // exception inside ":catch", jump to ":finally" once
2421 ectx->ec_iidx = trycmd->tcd_finally_idx;
2422 trycmd->tcd_finally_idx = 0;
2423 }
2424 else
2425 // jump to first ":catch"
2426 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002427 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002428 did_throw = FALSE; // don't come back here until :endtry
2429 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 }
2431 else
2432 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002433 // Not inside try or need to return from current functions.
2434 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002435 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002436 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002437 tv = STACK_TV_BOT(0);
2438 tv->v_type = VAR_NUMBER;
2439 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002440 ++ectx->ec_stack.ga_len;
2441 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002443 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002444 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002445 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002446 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 goto done;
2448 }
2449
Bram Moolenaar4c137212021-04-19 16:48:48 +02002450 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002451 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 }
2453 continue;
2454 }
2455
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002456 /*
2457 * Big switch on the instruction. Most compilers will be turning this
2458 * into an efficient lookup table, since the "case" values are an enum
2459 * with sequential numbers. It may look ugly, but it should be the
2460 * most efficient way.
2461 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002462 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 switch (iptr->isn_type)
2464 {
2465 // execute Ex command line
2466 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002467 if (exec_command(iptr) == FAIL)
2468 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 break;
2470
Bram Moolenaar20677332021-06-06 17:02:53 +02002471 // execute Ex command line split at NL characters.
2472 case ISN_EXEC_SPLIT:
2473 {
2474 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002475 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002476
2477 SOURCING_LNUM = iptr->isn_lnum;
2478 CLEAR_FIELD(cookie);
2479 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2480 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002481 line = get_split_sourceline(0, &cookie, 0, 0);
2482 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002483 get_split_sourceline, &cookie,
2484 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2485 == FAIL
2486 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002487 {
2488 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002489 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002490 }
2491 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002492 }
2493 break;
2494
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002495 // execute Ex command line that is only a range
2496 case ISN_EXECRANGE:
2497 {
2498 exarg_T ea;
2499 char *error = NULL;
2500
2501 CLEAR_FIELD(ea);
2502 ea.cmdidx = CMD_SIZE;
2503 ea.addr_type = ADDR_LINES;
2504 ea.cmd = iptr->isn_arg.string;
2505 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002506 if (ea.cmd == NULL)
2507 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002508 if (error == NULL)
2509 error = ex_range_without_command(&ea);
2510 if (error != NULL)
2511 {
2512 SOURCING_LNUM = iptr->isn_lnum;
2513 emsg(error);
2514 goto on_error;
2515 }
2516 }
2517 break;
2518
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002519 // Evaluate an expression with legacy syntax, push it onto the
2520 // stack.
2521 case ISN_LEGACY_EVAL:
2522 {
2523 char_u *arg = iptr->isn_arg.string;
2524 int res;
2525 int save_flags = cmdmod.cmod_flags;
2526
Bram Moolenaar35578162021-08-02 19:10:38 +02002527 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002528 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002529 tv = STACK_TV_BOT(0);
2530 init_tv(tv);
2531 cmdmod.cmod_flags |= CMOD_LEGACY;
2532 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2533 cmdmod.cmod_flags = save_flags;
2534 if (res == FAIL)
2535 goto on_error;
2536 ++ectx->ec_stack.ga_len;
2537 }
2538 break;
2539
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002540 // push typeval VAR_INSTR with instructions to be executed
2541 case ISN_INSTR:
2542 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002543 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002544 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002545 tv = STACK_TV_BOT(0);
2546 tv->vval.v_instr = ALLOC_ONE(instr_T);
2547 if (tv->vval.v_instr == NULL)
2548 goto on_error;
2549 ++ectx->ec_stack.ga_len;
2550
2551 tv->v_type = VAR_INSTR;
2552 tv->vval.v_instr->instr_ectx = ectx;
2553 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2554 }
2555 break;
2556
Bram Moolenaar4c137212021-04-19 16:48:48 +02002557 // execute :substitute with an expression
2558 case ISN_SUBSTITUTE:
2559 {
2560 subs_T *subs = &iptr->isn_arg.subs;
2561 source_cookie_T cookie;
2562 struct subs_expr_S *save_instr = substitute_instr;
2563 struct subs_expr_S subs_instr;
2564 int res;
2565
2566 subs_instr.subs_ectx = ectx;
2567 subs_instr.subs_instr = subs->subs_instr;
2568 subs_instr.subs_status = OK;
2569 substitute_instr = &subs_instr;
2570
2571 SOURCING_LNUM = iptr->isn_lnum;
2572 // This is very much like ISN_EXEC
2573 CLEAR_FIELD(cookie);
2574 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2575 res = do_cmdline(subs->subs_cmd,
2576 getsourceline, &cookie,
2577 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2578 substitute_instr = save_instr;
2579
2580 if (res == FAIL || did_emsg
2581 || subs_instr.subs_status == FAIL)
2582 goto on_error;
2583 }
2584 break;
2585
2586 case ISN_FINISH:
2587 goto done;
2588
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002589 case ISN_REDIRSTART:
2590 // create a dummy entry for var_redir_str()
2591 if (alloc_redir_lval() == FAIL)
2592 goto on_error;
2593
2594 // The output is stored in growarray "redir_ga" until
2595 // redirection ends.
2596 init_redir_ga();
2597 redir_vname = 1;
2598 break;
2599
2600 case ISN_REDIREND:
2601 {
2602 char_u *res = get_clear_redir_ga();
2603
2604 // End redirection, put redirected text on the stack.
2605 clear_redir_lval();
2606 redir_vname = 0;
2607
Bram Moolenaar35578162021-08-02 19:10:38 +02002608 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002609 {
2610 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002611 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002612 }
2613 tv = STACK_TV_BOT(0);
2614 tv->v_type = VAR_STRING;
2615 tv->vval.v_string = res;
2616 ++ectx->ec_stack.ga_len;
2617 }
2618 break;
2619
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002620 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002621#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002622 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2623 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002624#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002625 break;
2626
2627 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002628#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002629 {
2630 exarg_T ea;
2631 int res;
2632
2633 CLEAR_FIELD(ea);
2634 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2635 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2636 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2637 --ectx->ec_stack.ga_len;
2638 tv = STACK_TV_BOT(0);
2639 res = cexpr_core(&ea, tv);
2640 clear_tv(tv);
2641 if (res == FAIL)
2642 goto on_error;
2643 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002644#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002645 break;
2646
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002647 // execute Ex command from pieces on the stack
2648 case ISN_EXECCONCAT:
2649 {
2650 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002651 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002652 int pass;
2653 int i;
2654 char_u *cmd = NULL;
2655 char_u *str;
2656
2657 for (pass = 1; pass <= 2; ++pass)
2658 {
2659 for (i = 0; i < count; ++i)
2660 {
2661 tv = STACK_TV_BOT(i - count);
2662 str = tv->vval.v_string;
2663 if (str != NULL && *str != NUL)
2664 {
2665 if (pass == 2)
2666 STRCPY(cmd + len, str);
2667 len += STRLEN(str);
2668 }
2669 if (pass == 2)
2670 clear_tv(tv);
2671 }
2672 if (pass == 1)
2673 {
2674 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002675 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002676 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002677 len = 0;
2678 }
2679 }
2680
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002681 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002682 do_cmdline_cmd(cmd);
2683 vim_free(cmd);
2684 }
2685 break;
2686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 // execute :echo {string} ...
2688 case ISN_ECHO:
2689 {
2690 int count = iptr->isn_arg.echo.echo_count;
2691 int atstart = TRUE;
2692 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002693 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694
2695 for (idx = 0; idx < count; ++idx)
2696 {
2697 tv = STACK_TV_BOT(idx - count);
2698 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2699 &atstart, &needclr);
2700 clear_tv(tv);
2701 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002702 if (needclr)
2703 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002704 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 }
2706 break;
2707
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002708 // :execute {string} ...
2709 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002710 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002711 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002712 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002713 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002714 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002715 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002716 {
2717 int count = iptr->isn_arg.number;
2718 garray_T ga;
2719 char_u buf[NUMBUFLEN];
2720 char_u *p;
2721 int len;
2722 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002723 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002724
2725 ga_init2(&ga, 1, 80);
2726 for (idx = 0; idx < count; ++idx)
2727 {
2728 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002729 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002730 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002731 if (tv->v_type == VAR_CHANNEL
2732 || tv->v_type == VAR_JOB)
2733 {
2734 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002735 semsg(_(e_using_invalid_value_as_string_str),
2736 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002737 break;
2738 }
2739 else
2740 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002741 }
2742 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002743 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002744
2745 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002746 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002747 failed = TRUE;
2748 else
2749 {
2750 if (ga.ga_len > 0)
2751 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2752 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2753 ga.ga_len += len;
2754 }
2755 clear_tv(tv);
2756 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002757 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002758 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002759 {
2760 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002761 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002762 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002763
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002764 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002765 {
2766 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002767 {
2768 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002769 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002770 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002771 {
2772 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002773 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002774 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002775 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002776 else
2777 {
2778 msg_sb_eol();
2779 if (iptr->isn_type == ISN_ECHOMSG)
2780 {
2781 msg_attr(ga.ga_data, echo_attr);
2782 out_flush();
2783 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002784 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2785 {
2786 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2787 TRUE);
2788 ui_write((char_u *)"\r\n", 2, TRUE);
2789 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002790 else
2791 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002792 SOURCING_LNUM = iptr->isn_lnum;
2793 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002794 }
2795 }
2796 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002797 ga_clear(&ga);
2798 }
2799 break;
2800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 // load local variable or argument
2802 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002803 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002804 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002806 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 break;
2808
2809 // load v: variable
2810 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002811 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002812 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002814 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 break;
2816
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002817 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 case ISN_LOADSCRIPT:
2819 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002820 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 svar_T *sv;
2822
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002823 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002824 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002825 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002826 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002827 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002828 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002830 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 }
2832 break;
2833
2834 // load s: variable in old script
2835 case ISN_LOADS:
2836 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002837 hashtab_T *ht = &SCRIPT_VARS(
2838 iptr->isn_arg.loadstore.ls_sid);
2839 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 if (di == NULL)
2843 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002844 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002845 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002846 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 }
2848 else
2849 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002850 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002851 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002853 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 }
2855 }
2856 break;
2857
Bram Moolenaard3aac292020-04-19 14:32:17 +02002858 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002860 case ISN_LOADB:
2861 case ISN_LOADW:
2862 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002864 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002865
Bram Moolenaar06b77222022-01-25 15:51:56 +00002866 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002867 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002868 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002869 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 break;
2873
Bram Moolenaar03290b82020-12-19 16:30:44 +01002874 // load autoload variable
2875 case ISN_LOADAUTO:
2876 {
2877 char_u *name = iptr->isn_arg.string;
2878
Bram Moolenaar35578162021-08-02 19:10:38 +02002879 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002880 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002881 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002882 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002883 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002884 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002885 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002886 }
2887 break;
2888
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002889 // load g:/b:/w:/t: namespace
2890 case ISN_LOADGDICT:
2891 case ISN_LOADBDICT:
2892 case ISN_LOADWDICT:
2893 case ISN_LOADTDICT:
2894 {
2895 dict_T *d = NULL;
2896
2897 switch (iptr->isn_type)
2898 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002899 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2900 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2901 case ISN_LOADWDICT: d = curwin->w_vars; break;
2902 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002903 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002904 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002905 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002906 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002907 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002908 tv = STACK_TV_BOT(0);
2909 tv->v_type = VAR_DICT;
2910 tv->v_lock = 0;
2911 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002912 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002913 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002914 }
2915 break;
2916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 // load &option
2918 case ISN_LOADOPT:
2919 {
2920 typval_T optval;
2921 char_u *name = iptr->isn_arg.string;
2922
Bram Moolenaara8c17702020-04-01 21:17:24 +02002923 // This is not expected to fail, name is checked during
2924 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002925 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002926 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002927 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002928 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002930 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 }
2932 break;
2933
2934 // load $ENV
2935 case ISN_LOADENV:
2936 {
2937 typval_T optval;
2938 char_u *name = iptr->isn_arg.string;
2939
Bram Moolenaar35578162021-08-02 19:10:38 +02002940 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002941 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002942 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002943 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002945 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 }
2947 break;
2948
2949 // load @register
2950 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002951 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002952 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 tv = STACK_TV_BOT(0);
2954 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002955 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002956 // This may result in NULL, which should be equivalent to an
2957 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 tv->vval.v_string = get_reg_contents(
2959 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002960 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 break;
2962
2963 // store local variable
2964 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 tv = STACK_TV_VAR(iptr->isn_arg.number);
2967 clear_tv(tv);
2968 *tv = *STACK_TV_BOT(0);
2969 break;
2970
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002971 // store s: variable in old script
2972 case ISN_STORES:
2973 {
2974 hashtab_T *ht = &SCRIPT_VARS(
2975 iptr->isn_arg.loadstore.ls_sid);
2976 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002977 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002978
Bram Moolenaar4c137212021-04-19 16:48:48 +02002979 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002980 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002981 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002982 else
2983 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002984 SOURCING_LNUM = iptr->isn_lnum;
2985 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002986 {
2987 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002988 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002989 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002990 clear_tv(&di->di_tv);
2991 di->di_tv = *STACK_TV_BOT(0);
2992 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002993 }
2994 break;
2995
2996 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 case ISN_STORESCRIPT:
2998 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002999 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3000 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003002 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003003 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003004 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003005 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003006
3007 // "const" and "final" are checked at compile time, locking
3008 // the value needs to be checked here.
3009 SOURCING_LNUM = iptr->isn_lnum;
3010 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003011 {
3012 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003013 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003014 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 clear_tv(sv->sv_tv);
3017 *sv->sv_tv = *STACK_TV_BOT(0);
3018 }
3019 break;
3020
3021 // store option
3022 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003023 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003025 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3026 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 long n = 0;
3028 char_u *s = NULL;
3029 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003030 char_u numbuf[NUMBUFLEN];
3031 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032
Bram Moolenaar4c137212021-04-19 16:48:48 +02003033 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 tv = STACK_TV_BOT(0);
3035 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003036 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003038 if (s == NULL)
3039 s = (char_u *)"";
3040 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003041 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3042 {
3043 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003044 // If the option can be set to a function reference or
3045 // a lambda and the passed value is a function
3046 // reference, then convert it to the name (string) of
3047 // the function reference.
3048 s = tv2string(tv, &tofree, numbuf, 0);
3049 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003050 {
3051 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003052 goto on_error;
3053 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003056 // must be VAR_NUMBER, CHECKTYPE makes sure
3057 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003058 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003059 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003060 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 if (msg != NULL)
3062 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003063 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003065 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 }
3068 break;
3069
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003070 // store $ENV
3071 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003072 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003073 tv = STACK_TV_BOT(0);
3074 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3075 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003076 break;
3077
3078 // store @r
3079 case ISN_STOREREG:
3080 {
3081 int reg = iptr->isn_arg.number;
3082
Bram Moolenaar4c137212021-04-19 16:48:48 +02003083 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003084 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003085 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003086 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003087 }
3088 break;
3089
3090 // store v: variable
3091 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003092 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003093 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3094 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003095 // should not happen, type is checked when compiling
3096 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003097 break;
3098
Bram Moolenaard3aac292020-04-19 14:32:17 +02003099 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003101 case ISN_STOREB:
3102 case ISN_STOREW:
3103 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003105 dictitem_T *di;
3106 hashtab_T *ht;
3107 char_u *name = iptr->isn_arg.string + 2;
3108
Bram Moolenaard3aac292020-04-19 14:32:17 +02003109 switch (iptr->isn_type)
3110 {
3111 case ISN_STOREG:
3112 ht = get_globvar_ht();
3113 break;
3114 case ISN_STOREB:
3115 ht = &curbuf->b_vars->dv_hashtab;
3116 break;
3117 case ISN_STOREW:
3118 ht = &curwin->w_vars->dv_hashtab;
3119 break;
3120 case ISN_STORET:
3121 ht = &curtab->tp_vars->dv_hashtab;
3122 break;
3123 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003124 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126
Bram Moolenaar4c137212021-04-19 16:48:48 +02003127 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003128 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003130 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 else
3132 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003133 SOURCING_LNUM = iptr->isn_lnum;
3134 if (var_check_permission(di, name) == FAIL)
3135 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 clear_tv(&di->di_tv);
3137 di->di_tv = *STACK_TV_BOT(0);
3138 }
3139 }
3140 break;
3141
Bram Moolenaar03290b82020-12-19 16:30:44 +01003142 // store an autoload variable
3143 case ISN_STOREAUTO:
3144 SOURCING_LNUM = iptr->isn_lnum;
3145 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3146 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003147 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003148 break;
3149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 // store number in local variable
3151 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003152 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 clear_tv(tv);
3154 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003155 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 break;
3157
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003158 // store value in list or dict variable
3159 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003160 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003161 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003162
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003163 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003164 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003165 if (res == NOTDONE)
3166 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003167 }
3168 break;
3169
Bram Moolenaar68452172021-04-12 21:21:02 +02003170 // store value in blob range
3171 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003172 if (execute_storerange(iptr, ectx) == FAIL)
3173 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003174 break;
3175
Bram Moolenaar0186e582021-01-10 18:33:11 +01003176 // load or store variable or argument from outer scope
3177 case ISN_LOADOUTER:
3178 case ISN_STOREOUTER:
3179 {
3180 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003181 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3182 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003183
3184 while (depth > 1 && outer != NULL)
3185 {
3186 outer = outer->out_up;
3187 --depth;
3188 }
3189 if (outer == NULL)
3190 {
3191 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003192 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3193 || ectx->ec_outer_ref == NULL)
3194 // Possibly :def function called from legacy
3195 // context.
3196 emsg(_(e_closure_called_from_invalid_context));
3197 else
3198 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003199 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003200 }
3201 tv = ((typval_T *)outer->out_stack->ga_data)
3202 + outer->out_frame_idx + STACK_FRAME_SIZE
3203 + iptr->isn_arg.outer.outer_idx;
3204 if (iptr->isn_type == ISN_LOADOUTER)
3205 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003206 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003207 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003208 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003209 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003210 }
3211 else
3212 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003213 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003214 clear_tv(tv);
3215 *tv = *STACK_TV_BOT(0);
3216 }
3217 }
3218 break;
3219
Bram Moolenaar752fc692021-01-04 21:57:11 +01003220 // unlet item in list or dict variable
3221 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003222 if (execute_unletindex(iptr, ectx) == FAIL)
3223 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003224 break;
3225
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003226 // unlet range of items in list variable
3227 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003228 if (execute_unletrange(iptr, ectx) == FAIL)
3229 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003230 break;
3231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 // push constant
3233 case ISN_PUSHNR:
3234 case ISN_PUSHBOOL:
3235 case ISN_PUSHSPEC:
3236 case ISN_PUSHF:
3237 case ISN_PUSHS:
3238 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003239 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003240 case ISN_PUSHCHANNEL:
3241 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003242 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003243 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003245 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003246 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 switch (iptr->isn_type)
3248 {
3249 case ISN_PUSHNR:
3250 tv->v_type = VAR_NUMBER;
3251 tv->vval.v_number = iptr->isn_arg.number;
3252 break;
3253 case ISN_PUSHBOOL:
3254 tv->v_type = VAR_BOOL;
3255 tv->vval.v_number = iptr->isn_arg.number;
3256 break;
3257 case ISN_PUSHSPEC:
3258 tv->v_type = VAR_SPECIAL;
3259 tv->vval.v_number = iptr->isn_arg.number;
3260 break;
3261#ifdef FEAT_FLOAT
3262 case ISN_PUSHF:
3263 tv->v_type = VAR_FLOAT;
3264 tv->vval.v_float = iptr->isn_arg.fnumber;
3265 break;
3266#endif
3267 case ISN_PUSHBLOB:
3268 blob_copy(iptr->isn_arg.blob, tv);
3269 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003270 case ISN_PUSHFUNC:
3271 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003272 if (iptr->isn_arg.string == NULL)
3273 tv->vval.v_string = NULL;
3274 else
3275 tv->vval.v_string =
3276 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003277 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003278 case ISN_PUSHCHANNEL:
3279#ifdef FEAT_JOB_CHANNEL
3280 tv->v_type = VAR_CHANNEL;
3281 tv->vval.v_channel = iptr->isn_arg.channel;
3282 if (tv->vval.v_channel != NULL)
3283 ++tv->vval.v_channel->ch_refcount;
3284#endif
3285 break;
3286 case ISN_PUSHJOB:
3287#ifdef FEAT_JOB_CHANNEL
3288 tv->v_type = VAR_JOB;
3289 tv->vval.v_job = iptr->isn_arg.job;
3290 if (tv->vval.v_job != NULL)
3291 ++tv->vval.v_job->jv_refcount;
3292#endif
3293 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 default:
3295 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003296 tv->vval.v_string = vim_strsave(
3297 iptr->isn_arg.string == NULL
3298 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 }
3300 break;
3301
Bram Moolenaar06b77222022-01-25 15:51:56 +00003302 case ISN_AUTOLOAD:
3303 {
3304 char_u *name = iptr->isn_arg.string;
3305
3306 (void)script_autoload(name, FALSE);
3307 if (find_func(name, TRUE))
3308 {
3309 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3310 goto theend;
3311 tv = STACK_TV_BOT(0);
3312 tv->v_lock = 0;
3313 ++ectx->ec_stack.ga_len;
3314 tv->v_type = VAR_FUNC;
3315 tv->vval.v_string = vim_strsave(name);
3316 }
3317 else
3318 {
3319 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3320
3321 if (res == NOTDONE)
3322 goto theend;
3323 if (res == FAIL)
3324 goto on_error;
3325 }
3326 }
3327 break;
3328
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003329 case ISN_UNLET:
3330 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3331 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003332 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003333 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003334 case ISN_UNLETENV:
3335 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3336 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003337
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003338 case ISN_LOCKUNLOCK:
3339 {
3340 typval_T *lval_root_save = lval_root;
3341 int res;
3342
3343 // Stack has the local variable, argument the whole :lock
3344 // or :unlock command, like ISN_EXEC.
3345 --ectx->ec_stack.ga_len;
3346 lval_root = STACK_TV_BOT(0);
3347 res = exec_command(iptr);
3348 clear_tv(lval_root);
3349 lval_root = lval_root_save;
3350 if (res == FAIL)
3351 goto on_error;
3352 }
3353 break;
3354
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003355 case ISN_LOCKCONST:
3356 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3357 break;
3358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 // create a list from items on the stack; uses a single allocation
3360 // for the list header and the items
3361 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003362 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003363 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 break;
3365
3366 // create a dict from items on the stack
3367 case ISN_NEWDICT:
3368 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003369 int count = iptr->isn_arg.number;
3370 dict_T *dict = dict_alloc();
3371 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003372 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003373 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003375 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003376 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 for (idx = 0; idx < count; ++idx)
3378 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003379 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003381 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003382 key = tv->vval.v_string == NULL
3383 ? (char_u *)"" : tv->vval.v_string;
3384 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003385 if (item != NULL)
3386 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003387 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar74409f62022-01-01 15:58:22 +00003388 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003389 dict_unref(dict);
3390 goto on_error;
3391 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003392 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003394 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003395 {
3396 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003397 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3400 item->di_tv.v_lock = 0;
3401 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003402 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003403 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003404 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003405 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 }
3408
3409 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003410 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003411 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003412 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003414 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 tv = STACK_TV_BOT(-1);
3416 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003417 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 tv->vval.v_dict = dict;
3419 ++dict->dv_refcount;
3420 }
3421 break;
3422
3423 // call a :def function
3424 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003425 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003426 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3427 NULL,
3428 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003429 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003430 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 break;
3432
3433 // call a builtin function
3434 case ISN_BCALL:
3435 SOURCING_LNUM = iptr->isn_lnum;
3436 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3437 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003438 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003439 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 break;
3441
3442 // call a funcref or partial
3443 case ISN_PCALL:
3444 {
3445 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3446 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003447 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448
3449 SOURCING_LNUM = iptr->isn_lnum;
3450 if (pfunc->cpf_top)
3451 {
3452 // funcref is above the arguments
3453 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3454 }
3455 else
3456 {
3457 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003458 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003459 partial_tv = *STACK_TV_BOT(0);
3460 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003462 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003463 if (tv == &partial_tv)
3464 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003466 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 }
3468 break;
3469
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003470 case ISN_PCALL_END:
3471 // PCALL finished, arguments have been consumed and replaced by
3472 // the return value. Now clear the funcref from the stack,
3473 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003474 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003475 clear_tv(STACK_TV_BOT(-1));
3476 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3477 break;
3478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 // call a user defined function or funcref/partial
3480 case ISN_UCALL:
3481 {
3482 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3483
3484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003485 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003486 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003487 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 }
3489 break;
3490
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003491 // return from a :def function call without a value
3492 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003493 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003494 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003495 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003496 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003497 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003498 tv->vval.v_number = 0;
3499 tv->v_lock = 0;
3500 // FALLTHROUGH
3501
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003502 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 case ISN_RETURN:
3504 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003505 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003506 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003507
3508 if (trystack->ga_len > 0)
3509 trycmd = ((trycmd_T *)trystack->ga_data)
3510 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003511 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003514 // jump to ":finally" or ":endtry"
3515 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003516 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003517 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003518 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 trycmd->tcd_return = TRUE;
3520 }
3521 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003522 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 }
3524 break;
3525
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003526 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 case ISN_FUNCREF:
3528 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003529 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003530 ufunc_T *ufunc;
3531 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003534 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003535 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003536 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003537 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003538 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003539 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003540 if (funcref->fr_func_name == NULL)
3541 {
3542 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3543 + funcref->fr_dfunc_idx;
3544
3545 ufunc = pt_dfunc->df_ufunc;
3546 }
3547 else
3548 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003549 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003550 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003551 if (ufunc == NULL)
3552 {
3553 SOURCING_LNUM = iptr->isn_lnum;
3554 emsg(_(e_function_reference_invalid));
3555 goto theend;
3556 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003557 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003558 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003560 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 tv->vval.v_partial = pt;
3562 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003563 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 }
3565 break;
3566
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003567 // Create a global function from a lambda.
3568 case ISN_NEWFUNC:
3569 {
3570 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3571
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003572 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003573 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003574 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003575 }
3576 break;
3577
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003578 // List functions
3579 case ISN_DEF:
3580 if (iptr->isn_arg.string == NULL)
3581 list_functions(NULL);
3582 else
3583 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003584 exarg_T ea;
3585 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003586
3587 CLEAR_FIELD(ea);
3588 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003589 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3590 define_function(&ea, NULL, &lines_to_free);
3591 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003592 }
3593 break;
3594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 // jump if a condition is met
3596 case ISN_JUMP:
3597 {
3598 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003599 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 int jump = TRUE;
3601
3602 if (when != JUMP_ALWAYS)
3603 {
3604 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003605 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003606 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003607 || when == JUMP_IF_COND_TRUE)
3608 {
3609 SOURCING_LNUM = iptr->isn_lnum;
3610 jump = tv_get_bool_chk(tv, &error);
3611 if (error)
3612 goto on_error;
3613 }
3614 else
3615 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003617 || when == JUMP_AND_KEEP_IF_FALSE
3618 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003620 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 {
3622 // drop the value from the stack
3623 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003624 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 }
3626 }
3627 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003628 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 }
3630 break;
3631
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003632 // Jump if an argument with a default value was already set and not
3633 // v:none.
3634 case ISN_JUMP_IF_ARG_SET:
3635 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3636 if (tv->v_type != VAR_UNKNOWN
3637 && !(tv->v_type == VAR_SPECIAL
3638 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003639 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003640 break;
3641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 // top of a for loop
3643 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003644 if (execute_for(iptr, ectx) == FAIL)
3645 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 break;
3647
3648 // start of ":try" block
3649 case ISN_TRY:
3650 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003651 trycmd_T *trycmd = NULL;
3652
Bram Moolenaar35578162021-08-02 19:10:38 +02003653 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003654 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003655 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3656 + ectx->ec_trystack.ga_len;
3657 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003659 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003660 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3661 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003662 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003663 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003664 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003665 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003666 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003667 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 }
3669 break;
3670
3671 case ISN_PUSHEXC:
3672 if (current_exception == NULL)
3673 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003674 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003676 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003678 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003679 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003681 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003683 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 tv->vval.v_string = vim_strsave(
3685 (char_u *)current_exception->value);
3686 break;
3687
3688 case ISN_CATCH:
3689 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691
Bram Moolenaar4c137212021-04-19 16:48:48 +02003692 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 if (trystack->ga_len > 0)
3694 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003695 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 + trystack->ga_len - 1;
3697 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003698 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 }
3700 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003701 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 catch_exception(current_exception);
3703 }
3704 break;
3705
Bram Moolenaarc150c092021-02-13 15:02:46 +01003706 case ISN_TRYCONT:
3707 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003708 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003709 trycont_T *trycont = &iptr->isn_arg.trycont;
3710 int i;
3711 trycmd_T *trycmd;
3712 int iidx = trycont->tct_where;
3713
3714 if (trystack->ga_len < trycont->tct_levels)
3715 {
3716 siemsg("TRYCONT: expected %d levels, found %d",
3717 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003718 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003719 }
3720 // Make :endtry jump to any outer try block and the last
3721 // :endtry inside the loop to the loop start.
3722 for (i = trycont->tct_levels; i > 0; --i)
3723 {
3724 trycmd = ((trycmd_T *)trystack->ga_data)
3725 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003726 // Add one to tcd_cont to be able to jump to
3727 // instruction with index zero.
3728 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003729 iidx = trycmd->tcd_finally_idx == 0
3730 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003731 }
3732 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003733 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003734 }
3735 break;
3736
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003737 case ISN_FINALLY:
3738 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003739 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003740 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3741 + trystack->ga_len - 1;
3742
3743 // Reset the index to avoid a return statement jumps here
3744 // again.
3745 trycmd->tcd_finally_idx = 0;
3746 break;
3747 }
3748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 // end of ":try" block
3750 case ISN_ENDTRY:
3751 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003752 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753
3754 if (trystack->ga_len > 0)
3755 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003756 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 --trystack->ga_len;
3759 --trylevel;
3760 trycmd = ((trycmd_T *)trystack->ga_data)
3761 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003762 if (trycmd->tcd_did_throw)
3763 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003764 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 {
3766 // discard the exception
3767 if (caught_stack == current_exception)
3768 caught_stack = caught_stack->caught;
3769 discard_current_exception();
3770 }
3771
3772 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003773 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003774
Bram Moolenaar4c137212021-04-19 16:48:48 +02003775 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003776 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003777 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003778 clear_tv(STACK_TV_BOT(0));
3779 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003780 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003781 // handling :continue: jump to outer try block or
3782 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003783 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 }
3785 }
3786 break;
3787
3788 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003789 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003790 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003791
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003792 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3793 {
3794 // throwing an exception while using "silent!" causes
3795 // the function to abort but not display an error.
3796 tv = STACK_TV_BOT(-1);
3797 clear_tv(tv);
3798 tv->v_type = VAR_NUMBER;
3799 tv->vval.v_number = 0;
3800 goto done;
3801 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003802 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003803 tv = STACK_TV_BOT(0);
3804 if (tv->vval.v_string == NULL
3805 || *skipwhite(tv->vval.v_string) == NUL)
3806 {
3807 vim_free(tv->vval.v_string);
3808 SOURCING_LNUM = iptr->isn_lnum;
3809 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003810 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003811 }
3812
3813 // Inside a "catch" we need to first discard the caught
3814 // exception.
3815 if (trystack->ga_len > 0)
3816 {
3817 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3818 + trystack->ga_len - 1;
3819 if (trycmd->tcd_caught && current_exception != NULL)
3820 {
3821 // discard the exception
3822 if (caught_stack == current_exception)
3823 caught_stack = caught_stack->caught;
3824 discard_current_exception();
3825 trycmd->tcd_caught = FALSE;
3826 }
3827 }
3828
3829 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3830 == FAIL)
3831 {
3832 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003833 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003834 }
3835 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 break;
3838
3839 // compare with special values
3840 case ISN_COMPAREBOOL:
3841 case ISN_COMPARESPECIAL:
3842 {
3843 typval_T *tv1 = STACK_TV_BOT(-2);
3844 typval_T *tv2 = STACK_TV_BOT(-1);
3845 varnumber_T arg1 = tv1->vval.v_number;
3846 varnumber_T arg2 = tv2->vval.v_number;
3847 int res;
3848
3849 switch (iptr->isn_arg.op.op_type)
3850 {
3851 case EXPR_EQUAL: res = arg1 == arg2; break;
3852 case EXPR_NEQUAL: res = arg1 != arg2; break;
3853 default: res = 0; break;
3854 }
3855
Bram Moolenaar4c137212021-04-19 16:48:48 +02003856 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 tv1->v_type = VAR_BOOL;
3858 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3859 }
3860 break;
3861
3862 // Operation with two number arguments
3863 case ISN_OPNR:
3864 case ISN_COMPARENR:
3865 {
3866 typval_T *tv1 = STACK_TV_BOT(-2);
3867 typval_T *tv2 = STACK_TV_BOT(-1);
3868 varnumber_T arg1 = tv1->vval.v_number;
3869 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003870 varnumber_T res = 0;
3871 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872
3873 switch (iptr->isn_arg.op.op_type)
3874 {
3875 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003876 case EXPR_DIV: if (arg2 == 0)
3877 div_zero = TRUE;
3878 else
3879 res = arg1 / arg2;
3880 break;
3881 case EXPR_REM: if (arg2 == 0)
3882 div_zero = TRUE;
3883 else
3884 res = arg1 % arg2;
3885 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 case EXPR_SUB: res = arg1 - arg2; break;
3887 case EXPR_ADD: res = arg1 + arg2; break;
3888
3889 case EXPR_EQUAL: res = arg1 == arg2; break;
3890 case EXPR_NEQUAL: res = arg1 != arg2; break;
3891 case EXPR_GREATER: res = arg1 > arg2; break;
3892 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3893 case EXPR_SMALLER: res = arg1 < arg2; break;
3894 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003895 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 }
3897
Bram Moolenaar4c137212021-04-19 16:48:48 +02003898 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 if (iptr->isn_type == ISN_COMPARENR)
3900 {
3901 tv1->v_type = VAR_BOOL;
3902 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3903 }
3904 else
3905 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003906 if (div_zero)
3907 {
3908 SOURCING_LNUM = iptr->isn_lnum;
3909 emsg(_(e_divide_by_zero));
3910 goto on_error;
3911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 }
3913 break;
3914
3915 // Computation with two float arguments
3916 case ISN_OPFLOAT:
3917 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003918#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 {
3920 typval_T *tv1 = STACK_TV_BOT(-2);
3921 typval_T *tv2 = STACK_TV_BOT(-1);
3922 float_T arg1 = tv1->vval.v_float;
3923 float_T arg2 = tv2->vval.v_float;
3924 float_T res = 0;
3925 int cmp = FALSE;
3926
3927 switch (iptr->isn_arg.op.op_type)
3928 {
3929 case EXPR_MULT: res = arg1 * arg2; break;
3930 case EXPR_DIV: res = arg1 / arg2; break;
3931 case EXPR_SUB: res = arg1 - arg2; break;
3932 case EXPR_ADD: res = arg1 + arg2; break;
3933
3934 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3935 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3936 case EXPR_GREATER: cmp = arg1 > arg2; break;
3937 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3938 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3939 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3940 default: cmp = 0; break;
3941 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003942 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 if (iptr->isn_type == ISN_COMPAREFLOAT)
3944 {
3945 tv1->v_type = VAR_BOOL;
3946 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3947 }
3948 else
3949 tv1->vval.v_float = res;
3950 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003951#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 break;
3953
3954 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00003955 case ISN_COMPAREDICT:
3956 case ISN_COMPAREFUNC:
3957 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 case ISN_COMPAREBLOB:
3959 {
3960 typval_T *tv1 = STACK_TV_BOT(-2);
3961 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00003962 exprtype_T exprtype = iptr->isn_arg.op.op_type;
3963 int ic = iptr->isn_arg.op.op_ic;
3964 int res = FALSE;
3965 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966
Bram Moolenaar265f8112021-12-19 12:33:05 +00003967 SOURCING_LNUM = iptr->isn_lnum;
3968 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00003970 status = typval_compare_list(tv1, tv2,
3971 exprtype, ic, &res);
3972 }
3973 else if (iptr->isn_type == ISN_COMPAREDICT)
3974 {
3975 status = typval_compare_dict(tv1, tv2,
3976 exprtype, ic, &res);
3977 }
3978 else if (iptr->isn_type == ISN_COMPAREFUNC)
3979 {
3980 status = typval_compare_func(tv1, tv2,
3981 exprtype, ic, &res);
3982 }
3983 else if (iptr->isn_type == ISN_COMPARESTRING)
3984 {
3985 status = typval_compare_string(tv1, tv2,
3986 exprtype, ic, &res);
3987 }
3988 else
3989 {
3990 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003992 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 clear_tv(tv1);
3994 clear_tv(tv2);
3995 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003996 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3997 if (status == FAIL)
3998 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 }
4000 break;
4001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 case ISN_COMPAREANY:
4003 {
4004 typval_T *tv1 = STACK_TV_BOT(-2);
4005 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004006 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004008 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009
Bram Moolenaareb26f432020-09-14 16:50:05 +02004010 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004011 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004013 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004014 if (status == FAIL)
4015 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 }
4017 break;
4018
4019 case ISN_ADDLIST:
4020 case ISN_ADDBLOB:
4021 {
4022 typval_T *tv1 = STACK_TV_BOT(-2);
4023 typval_T *tv2 = STACK_TV_BOT(-1);
4024
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004025 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004027 {
4028 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4029 && tv1->vval.v_list != NULL)
4030 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4031 NULL);
4032 else
4033 eval_addlist(tv1, tv2);
4034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 else
4036 eval_addblob(tv1, tv2);
4037 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004038 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 }
4040 break;
4041
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004042 case ISN_LISTAPPEND:
4043 {
4044 typval_T *tv1 = STACK_TV_BOT(-2);
4045 typval_T *tv2 = STACK_TV_BOT(-1);
4046 list_T *l = tv1->vval.v_list;
4047
4048 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004049 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004050 if (l == NULL)
4051 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004052 emsg(_(e_cannot_add_to_null_list));
4053 goto on_error;
4054 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004055 if (value_check_lock(l->lv_lock, NULL, FALSE))
4056 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004057 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004058 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004059 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004060 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004061 }
4062 break;
4063
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004064 case ISN_BLOBAPPEND:
4065 {
4066 typval_T *tv1 = STACK_TV_BOT(-2);
4067 typval_T *tv2 = STACK_TV_BOT(-1);
4068 blob_T *b = tv1->vval.v_blob;
4069 int error = FALSE;
4070 varnumber_T n;
4071
4072 // add a number to a blob
4073 if (b == NULL)
4074 {
4075 SOURCING_LNUM = iptr->isn_lnum;
4076 emsg(_(e_cannot_add_to_null_blob));
4077 goto on_error;
4078 }
4079 n = tv_get_number_chk(tv2, &error);
4080 if (error)
4081 goto on_error;
4082 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004083 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004084 }
4085 break;
4086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 // Computation with two arguments of unknown type
4088 case ISN_OPANY:
4089 {
4090 typval_T *tv1 = STACK_TV_BOT(-2);
4091 typval_T *tv2 = STACK_TV_BOT(-1);
4092 varnumber_T n1, n2;
4093#ifdef FEAT_FLOAT
4094 float_T f1 = 0, f2 = 0;
4095#endif
4096 int error = FALSE;
4097
4098 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4099 {
4100 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4101 {
4102 eval_addlist(tv1, tv2);
4103 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004104 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 break;
4106 }
4107 else if (tv1->v_type == VAR_BLOB
4108 && tv2->v_type == VAR_BLOB)
4109 {
4110 eval_addblob(tv1, tv2);
4111 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004112 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 break;
4114 }
4115 }
4116#ifdef FEAT_FLOAT
4117 if (tv1->v_type == VAR_FLOAT)
4118 {
4119 f1 = tv1->vval.v_float;
4120 n1 = 0;
4121 }
4122 else
4123#endif
4124 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004125 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 n1 = tv_get_number_chk(tv1, &error);
4127 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004128 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129#ifdef FEAT_FLOAT
4130 if (tv2->v_type == VAR_FLOAT)
4131 f1 = n1;
4132#endif
4133 }
4134#ifdef FEAT_FLOAT
4135 if (tv2->v_type == VAR_FLOAT)
4136 {
4137 f2 = tv2->vval.v_float;
4138 n2 = 0;
4139 }
4140 else
4141#endif
4142 {
4143 n2 = tv_get_number_chk(tv2, &error);
4144 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004145 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146#ifdef FEAT_FLOAT
4147 if (tv1->v_type == VAR_FLOAT)
4148 f2 = n2;
4149#endif
4150 }
4151#ifdef FEAT_FLOAT
4152 // if there is a float on either side the result is a float
4153 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4154 {
4155 switch (iptr->isn_arg.op.op_type)
4156 {
4157 case EXPR_MULT: f1 = f1 * f2; break;
4158 case EXPR_DIV: f1 = f1 / f2; break;
4159 case EXPR_SUB: f1 = f1 - f2; break;
4160 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004161 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004162 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004163 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 }
4165 clear_tv(tv1);
4166 clear_tv(tv2);
4167 tv1->v_type = VAR_FLOAT;
4168 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004169 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 }
4171 else
4172#endif
4173 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004174 int failed = FALSE;
4175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 switch (iptr->isn_arg.op.op_type)
4177 {
4178 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004179 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4180 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004181 goto on_error;
4182 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 case EXPR_SUB: n1 = n1 - n2; break;
4184 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004185 default: n1 = num_modulus(n1, n2, &failed);
4186 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004187 goto on_error;
4188 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 }
4190 clear_tv(tv1);
4191 clear_tv(tv2);
4192 tv1->v_type = VAR_NUMBER;
4193 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004194 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 }
4196 }
4197 break;
4198
4199 case ISN_CONCAT:
4200 {
4201 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4202 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4203 char_u *res;
4204
4205 res = concat_str(str1, str2);
4206 clear_tv(STACK_TV_BOT(-2));
4207 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004208 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 STACK_TV_BOT(-1)->vval.v_string = res;
4210 }
4211 break;
4212
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004213 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004214 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004215 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004216 int is_slice = iptr->isn_type == ISN_STRSLICE;
4217 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004218 char_u *res;
4219
4220 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004221 // string slice: string is at stack-3, first index at
4222 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004223 if (is_slice)
4224 {
4225 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004226 n1 = tv->vval.v_number;
4227 }
4228
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004229 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004230 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004231
Bram Moolenaar4c137212021-04-19 16:48:48 +02004232 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004233 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004234 if (is_slice)
4235 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004236 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004237 else
4238 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004239 // single character (including composing characters).
4240 // If the index is too big or negative the result is
4241 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004242 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004243 vim_free(tv->vval.v_string);
4244 tv->vval.v_string = res;
4245 }
4246 break;
4247
4248 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004249 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004250 case ISN_BLOBINDEX:
4251 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004253 int is_slice = iptr->isn_type == ISN_LISTSLICE
4254 || iptr->isn_type == ISN_BLOBSLICE;
4255 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4256 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004257 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004258 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259
4260 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004261 // list slice: list is at stack-3, indexes at stack-2 and
4262 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004263 // Same for blob.
4264 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265
4266 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004267 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004269
4270 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 {
Bram Moolenaared591872020-08-15 22:14:53 +02004272 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004273 n1 = tv->vval.v_number;
4274 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 }
Bram Moolenaared591872020-08-15 22:14:53 +02004276
Bram Moolenaar4c137212021-04-19 16:48:48 +02004277 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004278 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004279 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004280 if (is_blob)
4281 {
4282 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4283 n1, n2, FALSE, tv) == FAIL)
4284 goto on_error;
4285 }
4286 else
4287 {
4288 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4289 n1, n2, FALSE, tv, TRUE) == FAIL)
4290 goto on_error;
4291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 }
4293 break;
4294
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004295 case ISN_ANYINDEX:
4296 case ISN_ANYSLICE:
4297 {
4298 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4299 typval_T *var1, *var2;
4300 int res;
4301
4302 // index: composite is at stack-2, index at stack-1
4303 // slice: composite is at stack-3, indexes at stack-2 and
4304 // stack-1
4305 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004306 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004307 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4308 goto on_error;
4309 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4310 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004311 res = eval_index_inner(tv, is_slice, var1, var2,
4312 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004313 clear_tv(var1);
4314 if (is_slice)
4315 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004316 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004317 if (res == FAIL)
4318 goto on_error;
4319 }
4320 break;
4321
Bram Moolenaar9af78762020-06-16 11:34:42 +02004322 case ISN_SLICE:
4323 {
4324 list_T *list;
4325 int count = iptr->isn_arg.number;
4326
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004327 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004328 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004329 list = tv->vval.v_list;
4330
4331 // no error for short list, expect it to be checked earlier
4332 if (list != NULL && list->lv_len >= count)
4333 {
4334 list_T *newlist = list_slice(list,
4335 count, list->lv_len - 1);
4336
4337 if (newlist != NULL)
4338 {
4339 list_unref(list);
4340 tv->vval.v_list = newlist;
4341 ++newlist->lv_refcount;
4342 }
4343 }
4344 }
4345 break;
4346
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004347 case ISN_GETITEM:
4348 {
4349 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004350 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004351
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004352 // Get list item: list is at stack-1, push item.
4353 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004354 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4355 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004356
Bram Moolenaar35578162021-08-02 19:10:38 +02004357 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004358 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004359 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004360 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004361
4362 // Useful when used in unpack assignment. Reset at
4363 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004364 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004365 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004366 }
4367 break;
4368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 case ISN_MEMBER:
4370 {
4371 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004372 char_u *key;
4373 dictitem_T *di;
4374
4375 // dict member: dict is at stack-2, key at stack-1
4376 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004377 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004378 dict = tv->vval.v_dict;
4379
4380 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004381 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004382 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004383 if (key == NULL)
4384 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004385
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004386 if ((di = dict_find(dict, key, -1)) == NULL)
4387 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004388 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004389 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004390
4391 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004392 // stack contents makes sense and the dict stack is
4393 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004394 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004395 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004396 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004397 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004398 tv->v_type = VAR_NUMBER;
4399 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004400 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004401 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004402 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004403 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004404 // Put the dict used on the dict stack, it might be used by
4405 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004406 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004407 if (dict_stack_save(tv) == FAIL)
4408 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004409 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004410 }
4411 break;
4412
4413 // dict member with string key
4414 case ISN_STRINGMEMBER:
4415 {
4416 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 dictitem_T *di;
4418
4419 tv = STACK_TV_BOT(-1);
4420 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4421 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004422 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004423 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004424 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 }
4426 dict = tv->vval.v_dict;
4427
4428 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4429 == NULL)
4430 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004431 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004432 semsg(_(e_key_not_present_in_dictionary),
4433 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004434 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004436 // Put the dict used on the dict stack, it might be used by
4437 // a dict function later.
4438 if (dict_stack_save(tv) == FAIL)
4439 goto on_fatal_error;
4440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004442 }
4443 break;
4444
4445 case ISN_CLEARDICT:
4446 dict_stack_drop();
4447 break;
4448
4449 case ISN_USEDICT:
4450 {
4451 typval_T *dict_tv = dict_stack_get_tv();
4452
4453 // Turn "dict.Func" into a partial for "Func" bound to
4454 // "dict". Don't do this when "Func" is already a partial
4455 // that was bound explicitly (pt_auto is FALSE).
4456 tv = STACK_TV_BOT(-1);
4457 if (dict_tv != NULL
4458 && dict_tv->v_type == VAR_DICT
4459 && dict_tv->vval.v_dict != NULL
4460 && (tv->v_type == VAR_FUNC
4461 || (tv->v_type == VAR_PARTIAL
4462 && (tv->vval.v_partial->pt_auto
4463 || tv->vval.v_partial->pt_dict == NULL))))
4464 dict_tv->vval.v_dict =
4465 make_partial(dict_tv->vval.v_dict, tv);
4466 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 }
4468 break;
4469
4470 case ISN_NEGATENR:
4471 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004472 if (tv->v_type != VAR_NUMBER
4473#ifdef FEAT_FLOAT
4474 && tv->v_type != VAR_FLOAT
4475#endif
4476 )
4477 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004478 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004479 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004480 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004481 }
4482#ifdef FEAT_FLOAT
4483 if (tv->v_type == VAR_FLOAT)
4484 tv->vval.v_float = -tv->vval.v_float;
4485 else
4486#endif
4487 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 break;
4489
4490 case ISN_CHECKNR:
4491 {
4492 int error = FALSE;
4493
4494 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004495 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004497 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 (void)tv_get_number_chk(tv, &error);
4499 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004500 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 }
4502 break;
4503
4504 case ISN_CHECKTYPE:
4505 {
4506 checktype_T *ct = &iptr->isn_arg.type;
4507
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004508 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004509 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004510 if (!ectx->ec_where.wt_variable)
4511 ectx->ec_where.wt_index = ct->ct_arg_idx;
4512 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4513 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004514 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004515 if (!ectx->ec_where.wt_variable)
4516 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004517
4518 // number 0 is FALSE, number 1 is TRUE
4519 if (tv->v_type == VAR_NUMBER
4520 && ct->ct_type->tt_type == VAR_BOOL
4521 && (tv->vval.v_number == 0
4522 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004524 tv->v_type = VAR_BOOL;
4525 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004526 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 }
4528 }
4529 break;
4530
Bram Moolenaar9af78762020-06-16 11:34:42 +02004531 case ISN_CHECKLEN:
4532 {
4533 int min_len = iptr->isn_arg.checklen.cl_min_len;
4534 list_T *list = NULL;
4535
4536 tv = STACK_TV_BOT(-1);
4537 if (tv->v_type == VAR_LIST)
4538 list = tv->vval.v_list;
4539 if (list == NULL || list->lv_len < min_len
4540 || (list->lv_len > min_len
4541 && !iptr->isn_arg.checklen.cl_more_OK))
4542 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004543 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004544 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004545 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004546 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004547 }
4548 }
4549 break;
4550
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004551 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004552 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004553 break;
4554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004556 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 {
4558 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004559 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004561 if (iptr->isn_type == ISN_2BOOL)
4562 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004563 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004564 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004565 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004566 n = !n;
4567 }
4568 else
4569 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004570 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004571 SOURCING_LNUM = iptr->isn_lnum;
4572 n = tv_get_bool_chk(tv, &error);
4573 if (error)
4574 goto on_error;
4575 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 clear_tv(tv);
4577 tv->v_type = VAR_BOOL;
4578 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4579 }
4580 break;
4581
4582 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004583 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004584 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004585 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4586 iptr->isn_type == ISN_2STRING_ANY,
4587 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004588 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 break;
4590
Bram Moolenaar08597872020-12-10 19:43:40 +01004591 case ISN_RANGE:
4592 {
4593 exarg_T ea;
4594 char *errormsg;
4595
Bram Moolenaarece0b872021-01-08 20:40:45 +01004596 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004597 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004598 ea.addr_type = ADDR_LINES;
4599 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004600 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004601 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004602 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004603
Bram Moolenaar35578162021-08-02 19:10:38 +02004604 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004605 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004606 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004607 tv = STACK_TV_BOT(-1);
4608 tv->v_type = VAR_NUMBER;
4609 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004610 if (ea.addr_count == 0)
4611 tv->vval.v_number = curwin->w_cursor.lnum;
4612 else
4613 tv->vval.v_number = ea.line2;
4614 }
4615 break;
4616
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004617 case ISN_PUT:
4618 {
4619 int regname = iptr->isn_arg.put.put_regname;
4620 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4621 char_u *expr = NULL;
4622 int dir = FORWARD;
4623
Bram Moolenaar08597872020-12-10 19:43:40 +01004624 if (lnum < -2)
4625 {
4626 // line number was put on the stack by ISN_RANGE
4627 tv = STACK_TV_BOT(-1);
4628 curwin->w_cursor.lnum = tv->vval.v_number;
4629 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4630 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004631 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004632 }
4633 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004634 // :put! above cursor
4635 dir = BACKWARD;
4636 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004637 {
4638 curwin->w_cursor.lnum = lnum;
4639 if (lnum == 0)
4640 // check_cursor() below will move to line 1
4641 dir = BACKWARD;
4642 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004643
4644 if (regname == '=')
4645 {
4646 tv = STACK_TV_BOT(-1);
4647 if (tv->v_type == VAR_STRING)
4648 expr = tv->vval.v_string;
4649 else
4650 {
4651 expr = typval2string(tv, TRUE); // allocates value
4652 clear_tv(tv);
4653 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004654 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004655 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004656 check_cursor();
4657 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4658 vim_free(expr);
4659 }
4660 break;
4661
Bram Moolenaar02194d22020-10-24 23:08:38 +02004662 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004663 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4664 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4665 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4666 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004667 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4668 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004669 break;
4670
Bram Moolenaar02194d22020-10-24 23:08:38 +02004671 case ISN_CMDMOD_REV:
4672 // filter regprog is owned by the instruction, don't free it
4673 cmdmod.cmod_filter_regmatch.regprog = NULL;
4674 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004675 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4676 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004677 break;
4678
Bram Moolenaar792f7862020-11-23 08:31:18 +01004679 case ISN_UNPACK:
4680 {
4681 int count = iptr->isn_arg.unpack.unp_count;
4682 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4683 list_T *l;
4684 listitem_T *li;
4685 int i;
4686
4687 // Check there is a valid list to unpack.
4688 tv = STACK_TV_BOT(-1);
4689 if (tv->v_type != VAR_LIST)
4690 {
4691 SOURCING_LNUM = iptr->isn_lnum;
4692 emsg(_(e_for_argument_must_be_sequence_of_lists));
4693 goto on_error;
4694 }
4695 l = tv->vval.v_list;
4696 if (l == NULL
4697 || l->lv_len < (semicolon ? count - 1 : count))
4698 {
4699 SOURCING_LNUM = iptr->isn_lnum;
4700 emsg(_(e_list_value_does_not_have_enough_items));
4701 goto on_error;
4702 }
4703 else if (!semicolon && l->lv_len > count)
4704 {
4705 SOURCING_LNUM = iptr->isn_lnum;
4706 emsg(_(e_list_value_has_more_items_than_targets));
4707 goto on_error;
4708 }
4709
4710 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004711 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004712 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004713 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004714
4715 // Variable after semicolon gets a list with the remaining
4716 // items.
4717 if (semicolon)
4718 {
4719 list_T *rem_list =
4720 list_alloc_with_items(l->lv_len - count + 1);
4721
4722 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004723 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004724 tv = STACK_TV_BOT(-count);
4725 tv->vval.v_list = rem_list;
4726 ++rem_list->lv_refcount;
4727 tv->v_lock = 0;
4728 li = l->lv_first;
4729 for (i = 0; i < count - 1; ++i)
4730 li = li->li_next;
4731 for (i = 0; li != NULL; ++i)
4732 {
4733 list_set_item(rem_list, i, &li->li_tv);
4734 li = li->li_next;
4735 }
4736 --count;
4737 }
4738
4739 // Produce the values in reverse order, first item last.
4740 li = l->lv_first;
4741 for (i = 0; i < count; ++i)
4742 {
4743 tv = STACK_TV_BOT(-i - 1);
4744 copy_tv(&li->li_tv, tv);
4745 li = li->li_next;
4746 }
4747
4748 list_unref(l);
4749 }
4750 break;
4751
Bram Moolenaarb2049902021-01-24 12:53:53 +01004752 case ISN_PROF_START:
4753 case ISN_PROF_END:
4754 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004755#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004756 funccall_T cookie;
4757 ufunc_T *cur_ufunc =
4758 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004759 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004760
4761 cookie.func = cur_ufunc;
4762 if (iptr->isn_type == ISN_PROF_START)
4763 {
4764 func_line_start(&cookie, iptr->isn_lnum);
4765 // if we get here the instruction is executed
4766 func_line_exec(&cookie);
4767 }
4768 else
4769 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004770#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004771 }
4772 break;
4773
Bram Moolenaare99d4222021-06-13 14:01:26 +02004774 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004775 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004776 break;
4777
Bram Moolenaar389df252020-07-09 21:20:47 +02004778 case ISN_SHUFFLE:
4779 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004780 typval_T tmp_tv;
4781 int item = iptr->isn_arg.shuffle.shfl_item;
4782 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004783
4784 tmp_tv = *STACK_TV_BOT(-item);
4785 for ( ; up > 0 && item > 1; --up)
4786 {
4787 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4788 --item;
4789 }
4790 *STACK_TV_BOT(-item) = tmp_tv;
4791 }
4792 break;
4793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004795 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004797 ectx->ec_where.wt_index = 0;
4798 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 break;
4800 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004801 continue;
4802
Bram Moolenaard032f342020-07-18 18:13:02 +02004803func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004804 // Restore previous function. If the frame pointer is where we started
4805 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004806 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004807 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004808
Bram Moolenaar4c137212021-04-19 16:48:48 +02004809 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004810 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004811 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004812 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004813
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004814on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004815 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004816 // If "emsg_silent" is set then ignore the error, unless it was set
4817 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004818 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004819 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004820 {
4821 // If a sequence of instructions causes an error while ":silent!"
4822 // was used, restore the stack length and jump ahead to restoring
4823 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004824 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004825 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004826 while (ectx->ec_stack.ga_len
4827 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004828 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004829 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004830 clear_tv(STACK_TV_BOT(0));
4831 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004832 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4833 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004834 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004835 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004836 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004837on_fatal_error:
4838 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004839 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004840 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004841 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 }
4843
4844done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004845 ret = OK;
4846theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004847 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004848 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4849 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850}
4851
4852/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004853 * Execute the instructions from a VAR_INSTR typeval and put the result in
4854 * "rettv".
4855 * Return OK or FAIL.
4856 */
4857 int
4858exe_typval_instr(typval_T *tv, typval_T *rettv)
4859{
4860 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4861 isn_T *save_instr = ectx->ec_instr;
4862 int save_iidx = ectx->ec_iidx;
4863 int res;
4864
4865 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4866 res = exec_instructions(ectx);
4867 if (res == OK)
4868 {
4869 *rettv = *STACK_TV_BOT(-1);
4870 --ectx->ec_stack.ga_len;
4871 }
4872
4873 ectx->ec_instr = save_instr;
4874 ectx->ec_iidx = save_iidx;
4875
4876 return res;
4877}
4878
4879/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004880 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4881 * "substitute_instr".
4882 */
4883 char_u *
4884exe_substitute_instr(void)
4885{
4886 ectx_T *ectx = substitute_instr->subs_ectx;
4887 isn_T *save_instr = ectx->ec_instr;
4888 int save_iidx = ectx->ec_iidx;
4889 char_u *res;
4890
4891 ectx->ec_instr = substitute_instr->subs_instr;
4892 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004893 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004894 typval_T *tv = STACK_TV_BOT(-1);
4895
Bram Moolenaar27523602021-06-05 21:36:19 +02004896 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004897 --ectx->ec_stack.ga_len;
4898 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004899 }
4900 else
4901 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004902 substitute_instr->subs_status = FAIL;
4903 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905
Bram Moolenaar4c137212021-04-19 16:48:48 +02004906 ectx->ec_instr = save_instr;
4907 ectx->ec_iidx = save_iidx;
4908
4909 return res;
4910}
4911
4912/*
4913 * Call a "def" function from old Vim script.
4914 * Return OK or FAIL.
4915 */
4916 int
4917call_def_function(
4918 ufunc_T *ufunc,
4919 int argc_arg, // nr of arguments
4920 typval_T *argv, // arguments
4921 partial_T *partial, // optional partial for context
4922 typval_T *rettv) // return value
4923{
4924 ectx_T ectx; // execution context
4925 int argc = argc_arg;
4926 typval_T *tv;
4927 int idx;
4928 int ret = FAIL;
4929 int defcount = ufunc->uf_args.ga_len - argc;
4930 sctx_T save_current_sctx = current_sctx;
4931 int did_emsg_before = did_emsg_cumul + did_emsg;
4932 int save_suppress_errthrow = suppress_errthrow;
4933 msglist_T **saved_msg_list = NULL;
4934 msglist_T *private_msg_list = NULL;
4935 int save_emsg_silent_def = emsg_silent_def;
4936 int save_did_emsg_def = did_emsg_def;
4937 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004938 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004939
4940// Get pointer to item in the stack.
4941#undef STACK_TV
4942#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4943
4944// Get pointer to item at the bottom of the stack, -1 is the bottom.
4945#undef STACK_TV_BOT
4946#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4947
4948// Get pointer to a local variable on the stack. Negative for arguments.
4949#undef STACK_TV_VAR
4950#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4951
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004952 // Update uf_has_breakpoint if needed.
4953 update_has_breakpoint(ufunc);
4954
Bram Moolenaar4c137212021-04-19 16:48:48 +02004955 if (ufunc->uf_def_status == UF_NOT_COMPILED
4956 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004957 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4958 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004959 == FAIL))
4960 {
4961 if (did_emsg_cumul + did_emsg == did_emsg_before)
4962 semsg(_(e_function_is_not_compiled_str),
4963 printable_func_name(ufunc));
4964 return FAIL;
4965 }
4966
4967 {
4968 // Check the function was really compiled.
4969 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4970 + ufunc->uf_dfunc_idx;
4971 if (INSTRUCTIONS(dfunc) == NULL)
4972 {
4973 iemsg("using call_def_function() on not compiled function");
4974 return FAIL;
4975 }
4976 }
4977
4978 // If depth of calling is getting too high, don't execute the function.
4979 orig_funcdepth = funcdepth_get();
4980 if (funcdepth_increment() == FAIL)
4981 return FAIL;
4982
4983 CLEAR_FIELD(ectx);
4984 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4985 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004986 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004987 {
4988 funcdepth_decrement();
4989 return FAIL;
4990 }
4991 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4992 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4993 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004994 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004995
4996 idx = argc - ufunc->uf_args.ga_len;
4997 if (idx > 0 && ufunc->uf_va_name == NULL)
4998 {
4999 if (idx == 1)
5000 emsg(_(e_one_argument_too_many));
5001 else
5002 semsg(_(e_nr_arguments_too_many), idx);
5003 goto failed_early;
5004 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005005 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5006 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005007 {
5008 if (idx == -1)
5009 emsg(_(e_one_argument_too_few));
5010 else
5011 semsg(_(e_nr_arguments_too_few), -idx);
5012 goto failed_early;
5013 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005014
5015 // Put arguments on the stack, but no more than what the function expects.
5016 // A lambda can be called with more arguments than it uses.
5017 for (idx = 0; idx < argc
5018 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5019 ++idx)
5020 {
5021 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5022 && argv[idx].v_type == VAR_SPECIAL
5023 && argv[idx].vval.v_number == VVAL_NONE)
5024 {
5025 // Use the default value.
5026 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5027 }
5028 else
5029 {
5030 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5031 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005032 ufunc->uf_arg_types[idx], &argv[idx],
5033 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005034 goto failed_early;
5035 copy_tv(&argv[idx], STACK_TV_BOT(0));
5036 }
5037 ++ectx.ec_stack.ga_len;
5038 }
5039
5040 // Turn varargs into a list. Empty list if no args.
5041 if (ufunc->uf_va_name != NULL)
5042 {
5043 int vararg_count = argc - ufunc->uf_args.ga_len;
5044
5045 if (vararg_count < 0)
5046 vararg_count = 0;
5047 else
5048 argc -= vararg_count;
5049 if (exe_newlist(vararg_count, &ectx) == FAIL)
5050 goto failed_early;
5051
5052 // Check the type of the list items.
5053 tv = STACK_TV_BOT(-1);
5054 if (ufunc->uf_va_type != NULL
5055 && ufunc->uf_va_type != &t_list_any
5056 && ufunc->uf_va_type->tt_member != &t_any
5057 && tv->vval.v_list != NULL)
5058 {
5059 type_T *expected = ufunc->uf_va_type->tt_member;
5060 listitem_T *li = tv->vval.v_list->lv_first;
5061
5062 for (idx = 0; idx < vararg_count; ++idx)
5063 {
5064 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005065 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005066 goto failed_early;
5067 li = li->li_next;
5068 }
5069 }
5070
5071 if (defcount > 0)
5072 // Move varargs list to below missing default arguments.
5073 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5074 --ectx.ec_stack.ga_len;
5075 }
5076
5077 // Make space for omitted arguments, will store default value below.
5078 // Any varargs list goes after them.
5079 if (defcount > 0)
5080 for (idx = 0; idx < defcount; ++idx)
5081 {
5082 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5083 ++ectx.ec_stack.ga_len;
5084 }
5085 if (ufunc->uf_va_name != NULL)
5086 ++ectx.ec_stack.ga_len;
5087
5088 // Frame pointer points to just after arguments.
5089 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5090 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5091
5092 {
5093 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5094 + ufunc->uf_dfunc_idx;
5095 ufunc_T *base_ufunc = dfunc->df_ufunc;
5096
5097 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5098 // by copy_func().
5099 if (partial != NULL || base_ufunc->uf_partial != NULL)
5100 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005101 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5102 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005103 goto failed_early;
5104 if (partial != NULL)
5105 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005106 outer_T *outer = get_pt_outer(partial);
5107
5108 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005109 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005110 if (current_ectx != NULL)
5111 {
5112 if (current_ectx->ec_outer_ref != NULL
5113 && current_ectx->ec_outer_ref->or_outer != NULL)
5114 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005115 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005116 }
5117 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005118 }
5119 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005120 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005121 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005122 ++partial->pt_refcount;
5123 ectx.ec_outer_ref->or_partial = partial;
5124 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005125 }
5126 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005127 {
5128 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5129 ++base_ufunc->uf_partial->pt_refcount;
5130 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5131 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005132 }
5133 }
5134
5135 // dummy frame entries
5136 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5137 {
5138 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5139 ++ectx.ec_stack.ga_len;
5140 }
5141
5142 {
5143 // Reserve space for local variables and any closure reference count.
5144 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5145 + ufunc->uf_dfunc_idx;
5146
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005147 // Initialize variables to zero. That avoids having to generate
5148 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005149 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005150 {
5151 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5152 STACK_TV_VAR(idx)->vval.v_number = 0;
5153 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005154 ectx.ec_stack.ga_len += dfunc->df_varcount;
5155 if (dfunc->df_has_closure)
5156 {
5157 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5158 STACK_TV_VAR(idx)->vval.v_number = 0;
5159 ++ectx.ec_stack.ga_len;
5160 }
5161
5162 ectx.ec_instr = INSTRUCTIONS(dfunc);
5163 }
5164
5165 // Following errors are in the function, not the caller.
5166 // Commands behave like vim9script.
5167 estack_push_ufunc(ufunc, 1);
5168 current_sctx = ufunc->uf_script_ctx;
5169 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5170
5171 // Use a specific location for storing error messages to be converted to an
5172 // exception.
5173 saved_msg_list = msg_list;
5174 msg_list = &private_msg_list;
5175
5176 // Do turn errors into exceptions.
5177 suppress_errthrow = FALSE;
5178
5179 // Do not delete the function while executing it.
5180 ++ufunc->uf_calls;
5181
5182 // When ":silent!" was used before calling then we still abort the
5183 // function. If ":silent!" is used in the function then we don't.
5184 emsg_silent_def = emsg_silent;
5185 did_emsg_def = 0;
5186
5187 ectx.ec_where.wt_index = 0;
5188 ectx.ec_where.wt_variable = FALSE;
5189
5190 // Execute the instructions until done.
5191 ret = exec_instructions(&ectx);
5192 if (ret == OK)
5193 {
5194 // function finished, get result from the stack.
5195 if (ufunc->uf_ret_type == &t_void)
5196 {
5197 rettv->v_type = VAR_VOID;
5198 }
5199 else
5200 {
5201 tv = STACK_TV_BOT(-1);
5202 *rettv = *tv;
5203 tv->v_type = VAR_UNKNOWN;
5204 }
5205 }
5206
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005207 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005208 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5209 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005210
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005211 // Deal with any remaining closures, they may be in use somewhere.
5212 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005213 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005214 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005215 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005216 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005217
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005218 estack_pop();
5219 current_sctx = save_current_sctx;
5220
Bram Moolenaar2336c372021-12-06 15:06:54 +00005221 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5222 // Function was unreferenced while being used, free it now.
5223 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005224
Bram Moolenaar352134b2020-10-17 22:04:08 +02005225 if (*msg_list != NULL && saved_msg_list != NULL)
5226 {
5227 msglist_T **plist = saved_msg_list;
5228
5229 // Append entries from the current msg_list (uncaught exceptions) to
5230 // the saved msg_list.
5231 while (*plist != NULL)
5232 plist = &(*plist)->next;
5233
5234 *plist = *msg_list;
5235 }
5236 msg_list = saved_msg_list;
5237
Bram Moolenaar4c137212021-04-19 16:48:48 +02005238 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005239 {
5240 cmdmod.cmod_filter_regmatch.regprog = NULL;
5241 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005242 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005243 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005244 emsg_silent_def = save_emsg_silent_def;
5245 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005246
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005247failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005248 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005249 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5250 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005251 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005254 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005255 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005256 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005257 if (ectx.ec_outer_ref->or_outer_allocated)
5258 vim_free(ectx.ec_outer_ref->or_outer);
5259 partial_unref(ectx.ec_outer_ref->or_partial);
5260 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005261 }
5262
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005263 // Not sure if this is necessary.
5264 suppress_errthrow = save_suppress_errthrow;
5265
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005266 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5267 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005268 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005269 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005270 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 return ret;
5272}
5273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005275 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5276 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5277 * "pfx" is prefixed to every line.
5278 */
5279 static void
5280list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5281{
5282 int line_idx = 0;
5283 int prev_current = 0;
5284 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005285 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005286
5287 for (current = 0; current < instr_count; ++current)
5288 {
5289 isn_T *iptr = &instr[current];
5290 char *line;
5291
5292 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005293 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005294 while (line_idx < iptr->isn_lnum
5295 && line_idx < ufunc->uf_lines.ga_len)
5296 {
5297 if (current > prev_current)
5298 {
5299 msg_puts("\n\n");
5300 prev_current = current;
5301 }
5302 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5303 if (line != NULL)
5304 msg(line);
5305 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005306 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5307 {
5308 int first_def_arg = ufunc->uf_args.ga_len
5309 - ufunc->uf_def_args.ga_len;
5310
5311 if (def_arg_idx > 0)
5312 msg_puts("\n\n");
5313 msg_start();
5314 msg_puts(" ");
5315 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5316 first_def_arg + def_arg_idx]);
5317 msg_puts(" = ");
5318 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5319 msg_clr_eos();
5320 msg_end();
5321 }
5322 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005323
5324 switch (iptr->isn_type)
5325 {
5326 case ISN_EXEC:
5327 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5328 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005329 case ISN_EXEC_SPLIT:
5330 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5331 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005332 case ISN_EXECRANGE:
5333 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5334 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005335 case ISN_LEGACY_EVAL:
5336 smsg("%s%4d EVAL legacy %s", pfx, current,
5337 iptr->isn_arg.string);
5338 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005339 case ISN_REDIRSTART:
5340 smsg("%s%4d REDIR", pfx, current);
5341 break;
5342 case ISN_REDIREND:
5343 smsg("%s%4d REDIR END%s", pfx, current,
5344 iptr->isn_arg.number ? " append" : "");
5345 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005346 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005347#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005348 smsg("%s%4d CEXPR pre %s", pfx, current,
5349 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005350#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005351 break;
5352 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005353#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005354 {
5355 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5356
5357 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5358 cexpr_get_auname(cer->cer_cmdidx),
5359 cer->cer_forceit ? "!" : "",
5360 cer->cer_cmdline);
5361 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005362#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005363 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005364 case ISN_INSTR:
5365 {
5366 smsg("%s%4d INSTR", pfx, current);
5367 list_instructions(" ", iptr->isn_arg.instr,
5368 INT_MAX, NULL);
5369 msg(" -------------");
5370 }
5371 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005372 case ISN_SUBSTITUTE:
5373 {
5374 subs_T *subs = &iptr->isn_arg.subs;
5375
5376 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5377 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5378 msg(" -------------");
5379 }
5380 break;
5381 case ISN_EXECCONCAT:
5382 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5383 (varnumber_T)iptr->isn_arg.number);
5384 break;
5385 case ISN_ECHO:
5386 {
5387 echo_T *echo = &iptr->isn_arg.echo;
5388
5389 smsg("%s%4d %s %d", pfx, current,
5390 echo->echo_with_white ? "ECHO" : "ECHON",
5391 echo->echo_count);
5392 }
5393 break;
5394 case ISN_EXECUTE:
5395 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005396 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005397 break;
5398 case ISN_ECHOMSG:
5399 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005400 (varnumber_T)(iptr->isn_arg.number));
5401 break;
5402 case ISN_ECHOCONSOLE:
5403 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5404 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005405 break;
5406 case ISN_ECHOERR:
5407 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005408 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005409 break;
5410 case ISN_LOAD:
5411 {
5412 if (iptr->isn_arg.number < 0)
5413 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5414 (varnumber_T)(iptr->isn_arg.number
5415 + STACK_FRAME_SIZE));
5416 else
5417 smsg("%s%4d LOAD $%lld", pfx, current,
5418 (varnumber_T)(iptr->isn_arg.number));
5419 }
5420 break;
5421 case ISN_LOADOUTER:
5422 {
5423 if (iptr->isn_arg.number < 0)
5424 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5425 iptr->isn_arg.outer.outer_depth,
5426 iptr->isn_arg.outer.outer_idx
5427 + STACK_FRAME_SIZE);
5428 else
5429 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5430 iptr->isn_arg.outer.outer_depth,
5431 iptr->isn_arg.outer.outer_idx);
5432 }
5433 break;
5434 case ISN_LOADV:
5435 smsg("%s%4d LOADV v:%s", pfx, current,
5436 get_vim_var_name(iptr->isn_arg.number));
5437 break;
5438 case ISN_LOADSCRIPT:
5439 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005440 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5441 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5442 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005443
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005444 sv = get_script_svar(sref, -1);
5445 if (sv == NULL)
5446 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5447 pfx, current, si->sn_name);
5448 else
5449 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005450 sv->sv_name,
5451 sref->sref_idx,
5452 si->sn_name);
5453 }
5454 break;
5455 case ISN_LOADS:
5456 {
5457 scriptitem_T *si = SCRIPT_ITEM(
5458 iptr->isn_arg.loadstore.ls_sid);
5459
5460 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5461 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5462 }
5463 break;
5464 case ISN_LOADAUTO:
5465 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5466 break;
5467 case ISN_LOADG:
5468 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5469 break;
5470 case ISN_LOADB:
5471 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5472 break;
5473 case ISN_LOADW:
5474 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5475 break;
5476 case ISN_LOADT:
5477 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5478 break;
5479 case ISN_LOADGDICT:
5480 smsg("%s%4d LOAD g:", pfx, current);
5481 break;
5482 case ISN_LOADBDICT:
5483 smsg("%s%4d LOAD b:", pfx, current);
5484 break;
5485 case ISN_LOADWDICT:
5486 smsg("%s%4d LOAD w:", pfx, current);
5487 break;
5488 case ISN_LOADTDICT:
5489 smsg("%s%4d LOAD t:", pfx, current);
5490 break;
5491 case ISN_LOADOPT:
5492 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5493 break;
5494 case ISN_LOADENV:
5495 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5496 break;
5497 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005498 smsg("%s%4d LOADREG @%c", pfx, current,
5499 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005500 break;
5501
5502 case ISN_STORE:
5503 if (iptr->isn_arg.number < 0)
5504 smsg("%s%4d STORE arg[%lld]", pfx, current,
5505 iptr->isn_arg.number + STACK_FRAME_SIZE);
5506 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005507 smsg("%s%4d STORE $%lld", pfx, current,
5508 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005509 break;
5510 case ISN_STOREOUTER:
5511 {
5512 if (iptr->isn_arg.number < 0)
5513 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5514 iptr->isn_arg.outer.outer_depth,
5515 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5516 else
5517 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5518 iptr->isn_arg.outer.outer_depth,
5519 iptr->isn_arg.outer.outer_idx);
5520 }
5521 break;
5522 case ISN_STOREV:
5523 smsg("%s%4d STOREV v:%s", pfx, current,
5524 get_vim_var_name(iptr->isn_arg.number));
5525 break;
5526 case ISN_STOREAUTO:
5527 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5528 break;
5529 case ISN_STOREG:
5530 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5531 break;
5532 case ISN_STOREB:
5533 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5534 break;
5535 case ISN_STOREW:
5536 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5537 break;
5538 case ISN_STORET:
5539 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5540 break;
5541 case ISN_STORES:
5542 {
5543 scriptitem_T *si = SCRIPT_ITEM(
5544 iptr->isn_arg.loadstore.ls_sid);
5545
5546 smsg("%s%4d STORES %s in %s", pfx, current,
5547 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5548 }
5549 break;
5550 case ISN_STORESCRIPT:
5551 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005552 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5553 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5554 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005555
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005556 sv = get_script_svar(sref, -1);
5557 if (sv == NULL)
5558 smsg("%s%4d STORESCRIPT [deleted] in %s",
5559 pfx, current, si->sn_name);
5560 else
5561 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005562 sv->sv_name,
5563 sref->sref_idx,
5564 si->sn_name);
5565 }
5566 break;
5567 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005568 case ISN_STOREFUNCOPT:
5569 smsg("%s%4d %s &%s", pfx, current,
5570 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005571 iptr->isn_arg.storeopt.so_name);
5572 break;
5573 case ISN_STOREENV:
5574 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5575 break;
5576 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005577 smsg("%s%4d STOREREG @%c", pfx, current,
5578 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005579 break;
5580 case ISN_STORENR:
5581 smsg("%s%4d STORE %lld in $%d", pfx, current,
5582 iptr->isn_arg.storenr.stnr_val,
5583 iptr->isn_arg.storenr.stnr_idx);
5584 break;
5585
5586 case ISN_STOREINDEX:
5587 smsg("%s%4d STOREINDEX %s", pfx, current,
5588 vartype_name(iptr->isn_arg.vartype));
5589 break;
5590
5591 case ISN_STORERANGE:
5592 smsg("%s%4d STORERANGE", pfx, current);
5593 break;
5594
5595 // constants
5596 case ISN_PUSHNR:
5597 smsg("%s%4d PUSHNR %lld", pfx, current,
5598 (varnumber_T)(iptr->isn_arg.number));
5599 break;
5600 case ISN_PUSHBOOL:
5601 case ISN_PUSHSPEC:
5602 smsg("%s%4d PUSH %s", pfx, current,
5603 get_var_special_name(iptr->isn_arg.number));
5604 break;
5605 case ISN_PUSHF:
5606#ifdef FEAT_FLOAT
5607 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5608#endif
5609 break;
5610 case ISN_PUSHS:
5611 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5612 break;
5613 case ISN_PUSHBLOB:
5614 {
5615 char_u *r;
5616 char_u numbuf[NUMBUFLEN];
5617 char_u *tofree;
5618
5619 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5620 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5621 vim_free(tofree);
5622 }
5623 break;
5624 case ISN_PUSHFUNC:
5625 {
5626 char *name = (char *)iptr->isn_arg.string;
5627
5628 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5629 name == NULL ? "[none]" : name);
5630 }
5631 break;
5632 case ISN_PUSHCHANNEL:
5633#ifdef FEAT_JOB_CHANNEL
5634 {
5635 channel_T *channel = iptr->isn_arg.channel;
5636
5637 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5638 channel == NULL ? 0 : channel->ch_id);
5639 }
5640#endif
5641 break;
5642 case ISN_PUSHJOB:
5643#ifdef FEAT_JOB_CHANNEL
5644 {
5645 typval_T tv;
5646 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005647 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005648
5649 tv.v_type = VAR_JOB;
5650 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005651 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005652 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5653 }
5654#endif
5655 break;
5656 case ISN_PUSHEXC:
5657 smsg("%s%4d PUSH v:exception", pfx, current);
5658 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005659 case ISN_AUTOLOAD:
5660 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5661 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005662 case ISN_UNLET:
5663 smsg("%s%4d UNLET%s %s", pfx, current,
5664 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5665 iptr->isn_arg.unlet.ul_name);
5666 break;
5667 case ISN_UNLETENV:
5668 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5669 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5670 iptr->isn_arg.unlet.ul_name);
5671 break;
5672 case ISN_UNLETINDEX:
5673 smsg("%s%4d UNLETINDEX", pfx, current);
5674 break;
5675 case ISN_UNLETRANGE:
5676 smsg("%s%4d UNLETRANGE", pfx, current);
5677 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005678 case ISN_LOCKUNLOCK:
5679 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5680 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005681 case ISN_LOCKCONST:
5682 smsg("%s%4d LOCKCONST", pfx, current);
5683 break;
5684 case ISN_NEWLIST:
5685 smsg("%s%4d NEWLIST size %lld", pfx, current,
5686 (varnumber_T)(iptr->isn_arg.number));
5687 break;
5688 case ISN_NEWDICT:
5689 smsg("%s%4d NEWDICT size %lld", pfx, current,
5690 (varnumber_T)(iptr->isn_arg.number));
5691 break;
5692
5693 // function call
5694 case ISN_BCALL:
5695 {
5696 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5697
5698 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5699 internal_func_name(cbfunc->cbf_idx),
5700 cbfunc->cbf_argcount);
5701 }
5702 break;
5703 case ISN_DCALL:
5704 {
5705 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5706 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5707 + cdfunc->cdf_idx;
5708
5709 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005710 printable_func_name(df->df_ufunc),
5711 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005712 }
5713 break;
5714 case ISN_UCALL:
5715 {
5716 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5717
5718 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5719 cufunc->cuf_name, cufunc->cuf_argcount);
5720 }
5721 break;
5722 case ISN_PCALL:
5723 {
5724 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5725
5726 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5727 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5728 }
5729 break;
5730 case ISN_PCALL_END:
5731 smsg("%s%4d PCALL end", pfx, current);
5732 break;
5733 case ISN_RETURN:
5734 smsg("%s%4d RETURN", pfx, current);
5735 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005736 case ISN_RETURN_VOID:
5737 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005738 break;
5739 case ISN_FUNCREF:
5740 {
5741 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005742 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005743
Bram Moolenaar38453522021-11-28 22:00:12 +00005744 if (funcref->fr_func_name == NULL)
5745 {
5746 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5747 + funcref->fr_dfunc_idx;
5748 name = df->df_ufunc->uf_name;
5749 }
5750 else
5751 name = funcref->fr_func_name;
5752 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005753 }
5754 break;
5755
5756 case ISN_NEWFUNC:
5757 {
5758 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5759
5760 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5761 newfunc->nf_lambda, newfunc->nf_global);
5762 }
5763 break;
5764
5765 case ISN_DEF:
5766 {
5767 char_u *name = iptr->isn_arg.string;
5768
5769 smsg("%s%4d DEF %s", pfx, current,
5770 name == NULL ? (char_u *)"" : name);
5771 }
5772 break;
5773
5774 case ISN_JUMP:
5775 {
5776 char *when = "?";
5777
5778 switch (iptr->isn_arg.jump.jump_when)
5779 {
5780 case JUMP_ALWAYS:
5781 when = "JUMP";
5782 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005783 case JUMP_NEVER:
5784 iemsg("JUMP_NEVER should not be used");
5785 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005786 case JUMP_AND_KEEP_IF_TRUE:
5787 when = "JUMP_AND_KEEP_IF_TRUE";
5788 break;
5789 case JUMP_IF_FALSE:
5790 when = "JUMP_IF_FALSE";
5791 break;
5792 case JUMP_AND_KEEP_IF_FALSE:
5793 when = "JUMP_AND_KEEP_IF_FALSE";
5794 break;
5795 case JUMP_IF_COND_FALSE:
5796 when = "JUMP_IF_COND_FALSE";
5797 break;
5798 case JUMP_IF_COND_TRUE:
5799 when = "JUMP_IF_COND_TRUE";
5800 break;
5801 }
5802 smsg("%s%4d %s -> %d", pfx, current, when,
5803 iptr->isn_arg.jump.jump_where);
5804 }
5805 break;
5806
5807 case ISN_JUMP_IF_ARG_SET:
5808 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5809 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5810 iptr->isn_arg.jump.jump_where);
5811 break;
5812
5813 case ISN_FOR:
5814 {
5815 forloop_T *forloop = &iptr->isn_arg.forloop;
5816
5817 smsg("%s%4d FOR $%d -> %d", pfx, current,
5818 forloop->for_idx, forloop->for_end);
5819 }
5820 break;
5821
5822 case ISN_TRY:
5823 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005824 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005825
5826 if (try->try_ref->try_finally == 0)
5827 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5828 pfx, current,
5829 try->try_ref->try_catch,
5830 try->try_ref->try_endtry);
5831 else
5832 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5833 pfx, current,
5834 try->try_ref->try_catch,
5835 try->try_ref->try_finally,
5836 try->try_ref->try_endtry);
5837 }
5838 break;
5839 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005840 smsg("%s%4d CATCH", pfx, current);
5841 break;
5842 case ISN_TRYCONT:
5843 {
5844 trycont_T *trycont = &iptr->isn_arg.trycont;
5845
5846 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5847 trycont->tct_levels,
5848 trycont->tct_levels == 1 ? "" : "s",
5849 trycont->tct_where);
5850 }
5851 break;
5852 case ISN_FINALLY:
5853 smsg("%s%4d FINALLY", pfx, current);
5854 break;
5855 case ISN_ENDTRY:
5856 smsg("%s%4d ENDTRY", pfx, current);
5857 break;
5858 case ISN_THROW:
5859 smsg("%s%4d THROW", pfx, current);
5860 break;
5861
5862 // expression operations on number
5863 case ISN_OPNR:
5864 case ISN_OPFLOAT:
5865 case ISN_OPANY:
5866 {
5867 char *what;
5868 char *ins;
5869
5870 switch (iptr->isn_arg.op.op_type)
5871 {
5872 case EXPR_MULT: what = "*"; break;
5873 case EXPR_DIV: what = "/"; break;
5874 case EXPR_REM: what = "%"; break;
5875 case EXPR_SUB: what = "-"; break;
5876 case EXPR_ADD: what = "+"; break;
5877 default: what = "???"; break;
5878 }
5879 switch (iptr->isn_type)
5880 {
5881 case ISN_OPNR: ins = "OPNR"; break;
5882 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5883 case ISN_OPANY: ins = "OPANY"; break;
5884 default: ins = "???"; break;
5885 }
5886 smsg("%s%4d %s %s", pfx, current, ins, what);
5887 }
5888 break;
5889
5890 case ISN_COMPAREBOOL:
5891 case ISN_COMPARESPECIAL:
5892 case ISN_COMPARENR:
5893 case ISN_COMPAREFLOAT:
5894 case ISN_COMPARESTRING:
5895 case ISN_COMPAREBLOB:
5896 case ISN_COMPARELIST:
5897 case ISN_COMPAREDICT:
5898 case ISN_COMPAREFUNC:
5899 case ISN_COMPAREANY:
5900 {
5901 char *p;
5902 char buf[10];
5903 char *type;
5904
5905 switch (iptr->isn_arg.op.op_type)
5906 {
5907 case EXPR_EQUAL: p = "=="; break;
5908 case EXPR_NEQUAL: p = "!="; break;
5909 case EXPR_GREATER: p = ">"; break;
5910 case EXPR_GEQUAL: p = ">="; break;
5911 case EXPR_SMALLER: p = "<"; break;
5912 case EXPR_SEQUAL: p = "<="; break;
5913 case EXPR_MATCH: p = "=~"; break;
5914 case EXPR_IS: p = "is"; break;
5915 case EXPR_ISNOT: p = "isnot"; break;
5916 case EXPR_NOMATCH: p = "!~"; break;
5917 default: p = "???"; break;
5918 }
5919 STRCPY(buf, p);
5920 if (iptr->isn_arg.op.op_ic == TRUE)
5921 strcat(buf, "?");
5922 switch(iptr->isn_type)
5923 {
5924 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5925 case ISN_COMPARESPECIAL:
5926 type = "COMPARESPECIAL"; break;
5927 case ISN_COMPARENR: type = "COMPARENR"; break;
5928 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5929 case ISN_COMPARESTRING:
5930 type = "COMPARESTRING"; break;
5931 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5932 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5933 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5934 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5935 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5936 default: type = "???"; break;
5937 }
5938
5939 smsg("%s%4d %s %s", pfx, current, type, buf);
5940 }
5941 break;
5942
5943 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5944 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5945
5946 // expression operations
5947 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5948 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5949 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5950 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5951 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5952 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5953 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5954 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5955 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5956 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5957 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5958 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005959 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005960 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5961 iptr->isn_arg.getitem.gi_index,
5962 iptr->isn_arg.getitem.gi_with_op ?
5963 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005964 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5965 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5966 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005967 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5968 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5969
Bram Moolenaar4c137212021-04-19 16:48:48 +02005970 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5971
5972 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5973 case ISN_CHECKTYPE:
5974 {
5975 checktype_T *ct = &iptr->isn_arg.type;
5976 char *tofree;
5977
5978 if (ct->ct_arg_idx == 0)
5979 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5980 type_name(ct->ct_type, &tofree),
5981 (int)ct->ct_off);
5982 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005983 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5984 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005985 type_name(ct->ct_type, &tofree),
5986 (int)ct->ct_off,
5987 (int)ct->ct_arg_idx);
5988 vim_free(tofree);
5989 break;
5990 }
5991 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5992 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5993 iptr->isn_arg.checklen.cl_min_len);
5994 break;
5995 case ISN_SETTYPE:
5996 {
5997 char *tofree;
5998
5999 smsg("%s%4d SETTYPE %s", pfx, current,
6000 type_name(iptr->isn_arg.type.ct_type, &tofree));
6001 vim_free(tofree);
6002 break;
6003 }
6004 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006005 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6006 smsg("%s%4d INVERT %d (!val)", pfx, current,
6007 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006008 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006009 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6010 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006011 break;
6012 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006013 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006014 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006015 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6016 pfx, current,
6017 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006018 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006019 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6020 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006021 break;
6022 case ISN_PUT:
6023 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6024 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006025 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006026 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6027 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006028 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006029 else
6030 smsg("%s%4d PUT %c %ld", pfx, current,
6031 iptr->isn_arg.put.put_regname,
6032 (long)iptr->isn_arg.put.put_lnum);
6033 break;
6034
Bram Moolenaar4c137212021-04-19 16:48:48 +02006035 case ISN_CMDMOD:
6036 {
6037 char_u *buf;
6038 size_t len = produce_cmdmods(
6039 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6040
6041 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006042 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006043 {
6044 (void)produce_cmdmods(
6045 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6046 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6047 vim_free(buf);
6048 }
6049 break;
6050 }
6051 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6052
6053 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006054 smsg("%s%4d PROFILE START line %d", pfx, current,
6055 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006056 break;
6057
6058 case ISN_PROF_END:
6059 smsg("%s%4d PROFILE END", pfx, current);
6060 break;
6061
Bram Moolenaare99d4222021-06-13 14:01:26 +02006062 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006063 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6064 iptr->isn_arg.debug.dbg_break_lnum + 1,
6065 iptr->isn_lnum,
6066 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006067 break;
6068
Bram Moolenaar4c137212021-04-19 16:48:48 +02006069 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6070 iptr->isn_arg.unpack.unp_count,
6071 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6072 break;
6073 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6074 iptr->isn_arg.shuffle.shfl_item,
6075 iptr->isn_arg.shuffle.shfl_up);
6076 break;
6077 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6078
6079 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6080 return;
6081 }
6082
6083 out_flush(); // output one line at a time
6084 ui_breakcheck();
6085 if (got_int)
6086 break;
6087 }
6088}
6089
6090/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006091 * Handle command line completion for the :disassemble command.
6092 */
6093 void
6094set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6095{
6096 char_u *p;
6097
6098 // Default: expand user functions, "debug" and "profile"
6099 xp->xp_context = EXPAND_DISASSEMBLE;
6100 xp->xp_pattern = arg;
6101
6102 // first argument already typed: only user function names
6103 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6104 {
6105 xp->xp_context = EXPAND_USER_FUNC;
6106 xp->xp_pattern = skipwhite(p);
6107 }
6108}
6109
6110/*
6111 * Function given to ExpandGeneric() to obtain the list of :disassemble
6112 * arguments.
6113 */
6114 char_u *
6115get_disassemble_argument(expand_T *xp, int idx)
6116{
6117 if (idx == 0)
6118 return (char_u *)"debug";
6119 if (idx == 1)
6120 return (char_u *)"profile";
6121 return get_user_func_name(xp, idx - 2);
6122}
6123
6124/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006125 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006126 * We don't really need this at runtime, but we do have tests that require it,
6127 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006128 */
6129 void
6130ex_disassemble(exarg_T *eap)
6131{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006132 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006133 char_u *fname;
6134 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135 dfunc_T *dfunc;
6136 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006137 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006138 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006139 compiletype_T compile_type = CT_NONE;
6140
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006141 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006142 {
6143 compile_type = CT_PROFILE;
6144 arg = skipwhite(arg + 7);
6145 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006146 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006147 {
6148 compile_type = CT_DEBUG;
6149 arg = skipwhite(arg + 5);
6150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006152 if (STRNCMP(arg, "<lambda>", 8) == 0)
6153 {
6154 arg += 8;
6155 (void)getdigits(&arg);
6156 fname = vim_strnsave(eap->arg, arg - eap->arg);
6157 }
6158 else
6159 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006160 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006161 if (fname == NULL)
6162 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006163 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006164 return;
6165 }
6166
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006167 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006168 if (ufunc == NULL)
6169 {
6170 char_u *p = untrans_function_name(fname);
6171
6172 if (p != NULL)
6173 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006174 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006175 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006176 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 if (ufunc == NULL)
6178 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006179 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006180 return;
6181 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006182 if (func_needs_compiling(ufunc, compile_type)
6183 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006184 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006185 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006187 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 return;
6189 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006190 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191
6192 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006193 switch (compile_type)
6194 {
6195 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006196#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006197 instr = dfunc->df_instr_prof;
6198 instr_count = dfunc->df_instr_prof_count;
6199 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006200#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006201 // FALLTHROUGH
6202 case CT_NONE:
6203 instr = dfunc->df_instr;
6204 instr_count = dfunc->df_instr_count;
6205 break;
6206 case CT_DEBUG:
6207 instr = dfunc->df_instr_debug;
6208 instr_count = dfunc->df_instr_debug_count;
6209 break;
6210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211
Bram Moolenaar4c137212021-04-19 16:48:48 +02006212 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213}
6214
6215/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006216 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217 * list, etc. Mostly like what JavaScript does, except that empty list and
6218 * empty dictionary are FALSE.
6219 */
6220 int
6221tv2bool(typval_T *tv)
6222{
6223 switch (tv->v_type)
6224 {
6225 case VAR_NUMBER:
6226 return tv->vval.v_number != 0;
6227 case VAR_FLOAT:
6228#ifdef FEAT_FLOAT
6229 return tv->vval.v_float != 0.0;
6230#else
6231 break;
6232#endif
6233 case VAR_PARTIAL:
6234 return tv->vval.v_partial != NULL;
6235 case VAR_FUNC:
6236 case VAR_STRING:
6237 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6238 case VAR_LIST:
6239 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6240 case VAR_DICT:
6241 return tv->vval.v_dict != NULL
6242 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6243 case VAR_BOOL:
6244 case VAR_SPECIAL:
6245 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6246 case VAR_JOB:
6247#ifdef FEAT_JOB_CHANNEL
6248 return tv->vval.v_job != NULL;
6249#else
6250 break;
6251#endif
6252 case VAR_CHANNEL:
6253#ifdef FEAT_JOB_CHANNEL
6254 return tv->vval.v_channel != NULL;
6255#else
6256 break;
6257#endif
6258 case VAR_BLOB:
6259 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6260 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006261 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006263 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264 break;
6265 }
6266 return FALSE;
6267}
6268
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006269 void
6270emsg_using_string_as(typval_T *tv, int as_number)
6271{
6272 semsg(_(as_number ? e_using_string_as_number_str
6273 : e_using_string_as_bool_str),
6274 tv->vval.v_string == NULL
6275 ? (char_u *)"" : tv->vval.v_string);
6276}
6277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278/*
6279 * If "tv" is a string give an error and return FAIL.
6280 */
6281 int
6282check_not_string(typval_T *tv)
6283{
6284 if (tv->v_type == VAR_STRING)
6285 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006286 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 clear_tv(tv);
6288 return FAIL;
6289 }
6290 return OK;
6291}
6292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293
6294#endif // FEAT_EVAL