blob: 1c5dd74da0643d5f36a21e3e9536f483fe1b6f21 [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 Moolenaarcd1cda22022-02-16 21:48:25 +0000370 else if (arg_to_add > ufunc->uf_def_args.ga_len)
371 {
372 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
373
374 if (missing == 1)
375 emsg(_(e_one_argument_too_few));
376 else
377 semsg(_(e_nr_arguments_too_few), missing);
378 return FAIL;
379 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200380
381 // Reserve space for:
382 // - missing arguments
383 // - stack frame
384 // - local variables
385 // - if needed: a counter for number of closures created in
386 // ectx->ec_funcrefs.
387 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200388 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389 return FAIL;
390
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100391 // If depth of calling is getting too high, don't execute the function.
392 if (funcdepth_increment() == FAIL)
393 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200394 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100395
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100396 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200397 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 {
399 floc = ALLOC_ONE(funclocal_T);
400 if (floc == NULL)
401 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200402 *floc = ectx->ec_funclocal;
403 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100404 }
405
Bram Moolenaarfe270812020-04-11 22:31:27 +0200406 // Move the vararg-list to below the missing optional arguments.
407 if (vararg_count > 0 && arg_to_add > 0)
408 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100409
410 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200411 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200412 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200413 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414
415 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100416 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
417 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200418 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200419 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
420 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100421 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100422 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100424
425 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200426 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000427 {
428 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
429
430 tv->v_type = VAR_NUMBER;
431 tv->vval.v_number = 0;
432 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200433 if (dfunc->df_has_closure)
434 {
435 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
436
437 tv->v_type = VAR_NUMBER;
438 tv->vval.v_number = 0;
439 }
440 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100442 if (pt != NULL || ufunc->uf_partial != NULL
443 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100444 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200445 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100446
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200447 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100448 return FAIL;
449 if (pt != NULL)
450 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000451 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200452 ++pt->pt_refcount;
453 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100454 }
455 else if (ufunc->uf_partial != NULL)
456 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000457 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200458 ++ufunc->uf_partial->pt_refcount;
459 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100460 }
461 else
462 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200463 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200464 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200465 {
466 vim_free(ref);
467 return FAIL;
468 }
469 ref->or_outer_allocated = TRUE;
470 ref->or_outer->out_stack = &ectx->ec_stack;
471 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
472 if (ectx->ec_outer_ref != NULL)
473 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100474 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200475 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100476 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100477 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200478 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100479
Bram Moolenaarc970e422021-03-17 15:03:04 +0100480 ++ufunc->uf_calls;
481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 // Set execution state to the start of the called function.
483 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100484 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100485 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200486 if (entry != NULL)
487 {
488 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200489 // Save the current context so it can be restored on return.
490 entry->es_save_sctx = current_sctx;
491 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200492 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100493
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200494 // Start execution at the first instruction.
495 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496
497 return OK;
498}
499
500// Get pointer to item in the stack.
501#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
502
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000503// Double linked list of funcstack_T in use.
504static funcstack_T *first_funcstack = NULL;
505
506 static void
507add_funcstack_to_list(funcstack_T *funcstack)
508{
509 // Link in list of funcstacks.
510 if (first_funcstack != NULL)
511 first_funcstack->fs_prev = funcstack;
512 funcstack->fs_next = first_funcstack;
513 funcstack->fs_prev = NULL;
514 first_funcstack = funcstack;
515}
516
517 static void
518remove_funcstack_from_list(funcstack_T *funcstack)
519{
520 if (funcstack->fs_prev == NULL)
521 first_funcstack = funcstack->fs_next;
522 else
523 funcstack->fs_prev->fs_next = funcstack->fs_next;
524 if (funcstack->fs_next != NULL)
525 funcstack->fs_next->fs_prev = funcstack->fs_prev;
526}
527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200529 * Used when returning from a function: Check if any closure is still
530 * referenced. If so then move the arguments and variables to a separate piece
531 * of stack to be used when the closure is called.
532 * When "free_arguments" is TRUE the arguments are to be freed.
533 * Returns FAIL when out of memory.
534 */
535 static int
536handle_closure_in_use(ectx_T *ectx, int free_arguments)
537{
538 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
539 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200540 int argcount;
541 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200542 int idx;
543 typval_T *tv;
544 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200545 garray_T *gap = &ectx->ec_funcrefs;
546 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200547
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200548 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200549 return OK; // function was freed
550 if (dfunc->df_has_closure == 0)
551 return OK; // no closures
552 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
553 closure_count = tv->vval.v_number;
554 if (closure_count == 0)
555 return OK; // no funcrefs created
556
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200557 argcount = ufunc_argcount(dfunc->df_ufunc);
558 top = ectx->ec_frame_idx - argcount;
559
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200560 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200561 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200562 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200563 partial_T *pt;
564 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200565
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200566 if (off < 0)
567 continue; // count is off or already done
568 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200569 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200570 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200571 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200572 int i;
573
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000574 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200575 // unreferenced on return.
576 for (i = 0; i < dfunc->df_varcount; ++i)
577 {
578 typval_T *stv = STACK_TV(ectx->ec_frame_idx
579 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200580 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200581 --refcount;
582 }
583 if (refcount > 1)
584 {
585 closure_in_use = TRUE;
586 break;
587 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200588 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200589 }
590
591 if (closure_in_use)
592 {
593 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
594 typval_T *stack;
595
596 // A closure is using the arguments and/or local variables.
597 // Move them to the called function.
598 if (funcstack == NULL)
599 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000600
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200601 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
602 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200603 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
604 funcstack->fs_ga.ga_data = stack;
605 if (stack == NULL)
606 {
607 vim_free(funcstack);
608 return FAIL;
609 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000610 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200611
612 // Move or copy the arguments.
613 for (idx = 0; idx < argcount; ++idx)
614 {
615 tv = STACK_TV(top + idx);
616 if (free_arguments)
617 {
618 *(stack + idx) = *tv;
619 tv->v_type = VAR_UNKNOWN;
620 }
621 else
622 copy_tv(tv, stack + idx);
623 }
624 // Move the local variables.
625 for (idx = 0; idx < dfunc->df_varcount; ++idx)
626 {
627 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200628
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200629 // A partial created for a local function, that is also used as a
630 // local variable, has a reference count for the variable, thus
631 // will never go down to zero. When all these refcounts are one
632 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000633 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200634 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
635 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200636 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200637
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200638 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200639 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
640 gap->ga_len - closure_count + i])
641 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200642 }
643
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200644 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200645 tv->v_type = VAR_UNKNOWN;
646 }
647
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200648 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200649 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200650 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
651 - closure_count + idx];
652 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200653 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200654 ++funcstack->fs_refcount;
655 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100656 pt->pt_outer.out_stack = &funcstack->fs_ga;
657 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200658 }
659 }
660 }
661
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200662 for (idx = 0; idx < closure_count; ++idx)
663 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
664 - closure_count + idx]);
665 gap->ga_len -= closure_count;
666 if (gap->ga_len == 0)
667 ga_clear(gap);
668
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200669 return OK;
670}
671
672/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200673 * Called when a partial is freed or its reference count goes down to one. The
674 * funcstack may be the only reference to the partials in the local variables.
675 * Go over all of them, the funcref and can be freed if all partials
676 * referencing the funcstack have a reference count of one.
677 */
678 void
679funcstack_check_refcount(funcstack_T *funcstack)
680{
681 int i;
682 garray_T *gap = &funcstack->fs_ga;
683 int done = 0;
684
685 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
686 return;
687 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
688 {
689 typval_T *tv = ((typval_T *)gap->ga_data) + i;
690
691 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
692 && tv->vval.v_partial->pt_funcstack == funcstack
693 && tv->vval.v_partial->pt_refcount == 1)
694 ++done;
695 }
696 if (done == funcstack->fs_min_refcount)
697 {
698 typval_T *stack = gap->ga_data;
699
700 // All partials referencing the funcstack have a reference count of
701 // one, thus the funcstack is no longer of use.
702 for (i = 0; i < gap->ga_len; ++i)
703 clear_tv(stack + i);
704 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000705 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200706 vim_free(funcstack);
707 }
708}
709
710/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000711 * For garbage collecting: set references in all variables referenced by
712 * all funcstacks.
713 */
714 int
715set_ref_in_funcstacks(int copyID)
716{
717 funcstack_T *funcstack;
718
719 for (funcstack = first_funcstack; funcstack != NULL;
720 funcstack = funcstack->fs_next)
721 {
722 typval_T *stack = funcstack->fs_ga.ga_data;
723 int i;
724
725 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
726 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
727 return TRUE; // abort
728 }
729 return FALSE;
730}
731
732/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 * Return from the current function.
734 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200735 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200736func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100739 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200740 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
741 + ectx->ec_dfunc_idx;
742 int argcount = ufunc_argcount(dfunc->df_ufunc);
743 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200744 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100745 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
746 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200747 funclocal_T *floc;
748#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100749 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
750 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751
Bram Moolenaar12d26532021-02-19 19:13:21 +0100752 if (do_profiling == PROF_YES)
753 {
754 ufunc_T *caller = prev_dfunc->df_ufunc;
755
756 if (dfunc->df_ufunc->uf_profiling
757 || (caller != NULL && caller->uf_profiling))
758 {
759 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
760 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
761 --profile_info_ga.ga_len;
762 }
763 }
764#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000765
766 // No check for uf_refcount being zero, cannot think of a way that would
767 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100768 --dfunc->df_ufunc->uf_calls;
769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200771 entry = estack_pop();
772 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200773 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200775 if (handle_closure_in_use(ectx, TRUE) == FAIL)
776 return FAIL;
777
778 // Clear the arguments.
779 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
780 clear_tv(STACK_TV(idx));
781
782 // Clear local variables and temp values, but not the return value.
783 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100784 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100786
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100787 // The return value should be on top of the stack. However, when aborting
788 // it may not be there and ec_frame_idx is the top of the stack.
789 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100790 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100791 ret_idx = 0;
792
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200793 if (ectx->ec_outer_ref != NULL)
794 {
795 if (ectx->ec_outer_ref->or_outer_allocated)
796 vim_free(ectx->ec_outer_ref->or_outer);
797 partial_unref(ectx->ec_outer_ref->or_partial);
798 vim_free(ectx->ec_outer_ref);
799 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100800
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100801 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100802 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100803 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
804 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200805 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
806 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200807 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100808 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100809 floc = (void *)STACK_TV(ectx->ec_frame_idx
810 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200811 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100812 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
813 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200814
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100815 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200816 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100817 else
818 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200819 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100820 vim_free(floc);
821 }
822
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100823 if (ret_idx > 0)
824 {
825 // Reset the stack to the position before the call, with a spot for the
826 // return value, moved there from above the frame.
827 ectx->ec_stack.ga_len = top + 1;
828 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
829 }
830 else
831 // Reset the stack to the position before the call.
832 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200833
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100834 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200835 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200836 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837}
838
839#undef STACK_TV
840
841/*
842 * Prepare arguments and rettv for calling a builtin or user function.
843 */
844 static int
845call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
846{
847 int idx;
848 typval_T *tv;
849
850 // Move arguments from bottom of the stack to argvars[] and add terminator.
851 for (idx = 0; idx < argcount; ++idx)
852 argvars[idx] = *STACK_TV_BOT(idx - argcount);
853 argvars[argcount].v_type = VAR_UNKNOWN;
854
855 // Result replaces the arguments on the stack.
856 if (argcount > 0)
857 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200858 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859 return FAIL;
860 else
861 ++ectx->ec_stack.ga_len;
862
863 // Default return value is zero.
864 tv = STACK_TV_BOT(-1);
865 tv->v_type = VAR_NUMBER;
866 tv->vval.v_number = 0;
867
868 return OK;
869}
870
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200871// Ugly global to avoid passing the execution context around through many
872// layers.
873static ectx_T *current_ectx = NULL;
874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875/*
876 * Call a builtin function by index.
877 */
878 static int
879call_bfunc(int func_idx, int argcount, ectx_T *ectx)
880{
881 typval_T argvars[MAX_FUNC_ARGS];
882 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100883 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200884 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200885 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886
887 if (call_prepare(argcount, argvars, ectx) == FAIL)
888 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200889 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200891 // Call the builtin function. Set "current_ectx" so that when it
892 // recursively invokes call_def_function() a closure context can be set.
893 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200895 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200896 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897
898 // Clear the arguments.
899 for (idx = 0; idx < argcount; ++idx)
900 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200901
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100902 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200903 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 return OK;
905}
906
907/*
908 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100909 * If the function is compiled this will add a stack frame and set the
910 * instruction pointer at the start of the function.
911 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200912 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100913 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 */
915 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100916call_ufunc(
917 ufunc_T *ufunc,
918 partial_T *pt,
919 int argcount,
920 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200921 isn_T *iptr,
922 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923{
924 typval_T argvars[MAX_FUNC_ARGS];
925 funcexe_T funcexe;
926 int error;
927 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100928 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200929 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930
Bram Moolenaare99d4222021-06-13 14:01:26 +0200931 if (func_needs_compiling(ufunc, compile_type)
932 && compile_def_function(ufunc, FALSE, compile_type, NULL)
933 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200934 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200935 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100936 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100937 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100938 if (error != FCERR_UNKNOWN)
939 {
940 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000941 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100942 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000943 semsg(_(e_not_enough_arguments_for_function_str),
944 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100945 return FAIL;
946 }
947
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100948 // The function has been compiled, can call it quickly. For a function
949 // that was defined later: we can call it directly next time.
950 if (iptr != NULL)
951 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100952 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100953 iptr->isn_type = ISN_DCALL;
954 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
955 iptr->isn_arg.dfunc.cdf_argcount = argcount;
956 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200957 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 if (call_prepare(argcount, argvars, ectx) == FAIL)
961 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200962 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000963 funcexe.fe_evaluate = TRUE;
964 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965
966 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000968 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969
970 // Clear the arguments.
971 for (idx = 0; idx < argcount; ++idx)
972 clear_tv(&argvars[idx]);
973
974 if (error != FCERR_NONE)
975 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000976 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 return FAIL;
978 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100979 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200980 // Error other than from calling the function itself.
981 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 return OK;
983}
984
985/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100986 * If command modifiers were applied restore them.
987 */
988 static void
989may_restore_cmdmod(funclocal_T *funclocal)
990{
991 if (funclocal->floc_restore_cmdmod)
992 {
993 cmdmod.cmod_filter_regmatch.regprog = NULL;
994 undo_cmdmod(&cmdmod);
995 cmdmod = funclocal->floc_save_cmdmod;
996 funclocal->floc_restore_cmdmod = FALSE;
997 }
998}
999
1000/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001001 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1002 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001003 */
1004 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001005vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001006{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001007 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001008}
1009
1010/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 * Execute a function by "name".
1012 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001013 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 * Returns FAIL if not found without an error message.
1015 */
1016 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001017call_by_name(
1018 char_u *name,
1019 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001020 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001021 isn_T *iptr,
1022 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023{
1024 ufunc_T *ufunc;
1025
1026 if (builtin_function(name, -1))
1027 {
1028 int func_idx = find_internal_func(name);
1029
1030 if (func_idx < 0)
1031 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001032 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 return FAIL;
1034 return call_bfunc(func_idx, argcount, ectx);
1035 }
1036
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001037 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001038
1039 if (ufunc == NULL)
1040 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001041 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001042
1043 if (script_autoload(name, TRUE))
1044 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001045 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001046
1047 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001048 return FAIL; // bail out if loading the script caused an error
1049 }
1050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001052 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001053 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001054 {
1055 int i;
1056 typval_T *argv = STACK_TV_BOT(0) - argcount;
1057
1058 // The function can change at runtime, check that the argument
1059 // types are correct.
1060 for (i = 0; i < argcount; ++i)
1061 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001062 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001063
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001064 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001065 type = ufunc->uf_arg_types[i];
1066 else if (ufunc->uf_va_type != NULL)
1067 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001068 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001069 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001070 return FAIL;
1071 }
1072 }
1073
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001074 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076
1077 return FAIL;
1078}
1079
1080 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001081call_partial(
1082 typval_T *tv,
1083 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001084 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001086 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001087 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001089 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001090 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091
1092 if (tv->v_type == VAR_PARTIAL)
1093 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001094 partial_T *pt = tv->vval.v_partial;
1095 int i;
1096
1097 if (pt->pt_argc > 0)
1098 {
1099 // Make space for arguments from the partial, shift the "argcount"
1100 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001101 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001102 return FAIL;
1103 for (i = 1; i <= argcount; ++i)
1104 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1105 ectx->ec_stack.ga_len += pt->pt_argc;
1106 argcount += pt->pt_argc;
1107
1108 // copy the arguments from the partial onto the stack
1109 for (i = 0; i < pt->pt_argc; ++i)
1110 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1111 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001112 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113
1114 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001115 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 name = pt->pt_name;
1118 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001119 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001121 if (name != NULL)
1122 {
1123 char_u fname_buf[FLEN_FIXED + 1];
1124 char_u *tofree = NULL;
1125 int error = FCERR_NONE;
1126 char_u *fname;
1127
1128 // May need to translate <SNR>123_ to K_SNR.
1129 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1130 if (error != FCERR_NONE)
1131 res = FAIL;
1132 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001133 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001134 vim_free(tofree);
1135 }
1136
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001137 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 {
1139 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001140 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001141 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 return FAIL;
1143 }
1144 return OK;
1145}
1146
1147/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001148 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1149 * TRUE.
1150 */
1151 static int
1152error_if_locked(int lock, char *error)
1153{
1154 if (lock & (VAR_LOCKED | VAR_FIXED))
1155 {
1156 emsg(_(error));
1157 return TRUE;
1158 }
1159 return FALSE;
1160}
1161
1162/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001163 * Give an error if "tv" is not a number and return FAIL.
1164 */
1165 static int
1166check_for_number(typval_T *tv)
1167{
1168 if (tv->v_type != VAR_NUMBER)
1169 {
1170 semsg(_(e_expected_str_but_got_str),
1171 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1172 return FAIL;
1173 }
1174 return OK;
1175}
1176
1177/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001178 * Store "tv" in variable "name".
1179 * This is for s: and g: variables.
1180 */
1181 static void
1182store_var(char_u *name, typval_T *tv)
1183{
1184 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001185 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001186
Bram Moolenaard877a572021-04-01 19:42:48 +02001187 if (tv->v_lock)
1188 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001189 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001190 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001191 restore_funccal();
1192}
1193
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001194/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001195 * Convert "tv" to a string.
1196 * Return FAIL if not allowed.
1197 */
1198 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001199do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001200{
1201 if (tv->v_type != VAR_STRING)
1202 {
1203 char_u *str;
1204
1205 if (is_2string_any)
1206 {
1207 switch (tv->v_type)
1208 {
1209 case VAR_SPECIAL:
1210 case VAR_BOOL:
1211 case VAR_NUMBER:
1212 case VAR_FLOAT:
1213 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001214
1215 case VAR_LIST:
1216 if (tolerant)
1217 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001218 char_u *s, *e, *p;
1219 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001220
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001221 ga_init2(&ga, sizeof(char_u *), 1);
1222
1223 // Convert to NL separated items, then
1224 // escape the items and replace the NL with
1225 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001226 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001227 if (str == NULL)
1228 return FAIL;
1229 s = str;
1230 while ((e = vim_strchr(s, '\n')) != NULL)
1231 {
1232 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001233 p = vim_strsave_fnameescape(s,
1234 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001235 if (p != NULL)
1236 {
1237 ga_concat(&ga, p);
1238 ga_concat(&ga, (char_u *)" ");
1239 vim_free(p);
1240 }
1241 s = e + 1;
1242 }
1243 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001244 clear_tv(tv);
1245 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001246 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001247 return OK;
1248 }
1249 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001250 default: to_string_error(tv->v_type);
1251 return FAIL;
1252 }
1253 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001254 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001255 clear_tv(tv);
1256 tv->v_type = VAR_STRING;
1257 tv->vval.v_string = str;
1258 }
1259 return OK;
1260}
1261
1262/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001263 * When the value of "sv" is a null list of dict, allocate it.
1264 */
1265 static void
1266allocate_if_null(typval_T *tv)
1267{
1268 switch (tv->v_type)
1269 {
1270 case VAR_LIST:
1271 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001272 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001273 break;
1274 case VAR_DICT:
1275 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001276 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001277 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001278 case VAR_BLOB:
1279 if (tv->vval.v_blob == NULL)
1280 (void)rettv_blob_alloc(tv);
1281 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001282 default:
1283 break;
1284 }
1285}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001286
Bram Moolenaare7525c52021-01-09 13:20:37 +01001287/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001288 * Return the character "str[index]" where "index" is the character index,
1289 * including composing characters.
1290 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001291 */
1292 char_u *
1293char_from_string(char_u *str, varnumber_T index)
1294{
1295 size_t nbyte = 0;
1296 varnumber_T nchar = index;
1297 size_t slen;
1298
1299 if (str == NULL)
1300 return NULL;
1301 slen = STRLEN(str);
1302
Bram Moolenaarff871402021-03-26 13:34:05 +01001303 // Do the same as for a list: a negative index counts from the end.
1304 // Optimization to check the first byte to be below 0x80 (and no composing
1305 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001306 if (index < 0)
1307 {
1308 int clen = 0;
1309
1310 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001311 {
1312 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1313 ++nbyte;
1314 else if (enc_utf8)
1315 nbyte += utfc_ptr2len(str + nbyte);
1316 else
1317 nbyte += mb_ptr2len(str + nbyte);
1318 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001319 nchar = clen + index;
1320 if (nchar < 0)
1321 // unlike list: index out of range results in empty string
1322 return NULL;
1323 }
1324
1325 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001326 {
1327 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1328 ++nbyte;
1329 else if (enc_utf8)
1330 nbyte += utfc_ptr2len(str + nbyte);
1331 else
1332 nbyte += mb_ptr2len(str + nbyte);
1333 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001334 if (nbyte >= slen)
1335 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001336 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001337}
1338
1339/*
1340 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001341 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001342 * If going over the end return "str_len".
1343 * If "idx" is negative count from the end, -1 is the last character.
1344 * When going over the start return -1.
1345 */
1346 static long
1347char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1348{
1349 varnumber_T nchar = idx;
1350 size_t nbyte = 0;
1351
1352 if (nchar >= 0)
1353 {
1354 while (nchar > 0 && nbyte < str_len)
1355 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001356 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001357 --nchar;
1358 }
1359 }
1360 else
1361 {
1362 nbyte = str_len;
1363 while (nchar < 0 && nbyte > 0)
1364 {
1365 --nbyte;
1366 nbyte -= mb_head_off(str, str + nbyte);
1367 ++nchar;
1368 }
1369 if (nchar < 0)
1370 return -1;
1371 }
1372 return (long)nbyte;
1373}
1374
1375/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001376 * Return the slice "str[first : last]" using character indexes. Composing
1377 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001378 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001379 * Return NULL when the result is empty.
1380 */
1381 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001382string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001383{
1384 long start_byte, end_byte;
1385 size_t slen;
1386
1387 if (str == NULL)
1388 return NULL;
1389 slen = STRLEN(str);
1390 start_byte = char_idx2byte(str, slen, first);
1391 if (start_byte < 0)
1392 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001393 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001394 end_byte = (long)slen;
1395 else
1396 {
1397 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001398 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001399 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001400 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001401 }
1402
1403 if (start_byte >= (long)slen || end_byte <= start_byte)
1404 return NULL;
1405 return vim_strnsave(str + start_byte, end_byte - start_byte);
1406}
1407
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001408/*
1409 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1410 * When "dfunc_idx" is negative don't give an error.
1411 * Returns NULL for an error.
1412 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001413 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001414get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001415{
1416 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001417 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1418 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001419 svar_T *sv;
1420
1421 if (sref->sref_seq != si->sn_script_seq)
1422 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001423 // The script was reloaded after the function was compiled, the
1424 // script_idx may not be valid.
1425 if (dfunc != NULL)
1426 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1427 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001428 return NULL;
1429 }
1430 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001431 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001432 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001433 if (dfunc != NULL)
1434 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001435 return NULL;
1436 }
1437 return sv;
1438}
1439
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001440/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001441 * Function passed to do_cmdline() for splitting a script joined by NL
1442 * characters.
1443 */
1444 static char_u *
1445get_split_sourceline(
1446 int c UNUSED,
1447 void *cookie,
1448 int indent UNUSED,
1449 getline_opt_T options UNUSED)
1450{
1451 source_cookie_T *sp = (source_cookie_T *)cookie;
1452 char_u *p;
1453 char_u *line;
1454
1455 if (*sp->nextline == NUL)
1456 return NULL;
1457 p = vim_strchr(sp->nextline, '\n');
1458 if (p == NULL)
1459 {
1460 line = vim_strsave(sp->nextline);
1461 sp->nextline += STRLEN(sp->nextline);
1462 }
1463 else
1464 {
1465 line = vim_strnsave(sp->nextline, p - sp->nextline);
1466 sp->nextline = p + 1;
1467 }
1468 return line;
1469}
1470
1471/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 * Execute a function by "name".
1473 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001474 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 */
1476 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001477call_eval_func(
1478 char_u *name,
1479 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001480 ectx_T *ectx,
1481 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482{
Bram Moolenaared677f52020-08-12 16:38:10 +02001483 int called_emsg_before = called_emsg;
1484 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001486 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001487 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001489 dictitem_T *v;
1490
1491 v = find_var(name, NULL, FALSE);
1492 if (v == NULL)
1493 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001494 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001495 return FAIL;
1496 }
1497 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1498 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001499 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001500 return FAIL;
1501 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001502 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001504 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505}
1506
1507/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001508 * When a function reference is used, fill a partial with the information
1509 * needed, especially when it is used as a closure.
1510 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001511 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001512fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1513{
1514 pt->pt_func = ufunc;
1515 pt->pt_refcount = 1;
1516
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001517 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001518 {
1519 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1520 + ectx->ec_dfunc_idx;
1521
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001522 // The closure may need to find arguments and local variables in the
1523 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001524 pt->pt_outer.out_stack = &ectx->ec_stack;
1525 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001526 if (ectx->ec_outer_ref != NULL)
1527 {
1528 // The current context already has a context, link to that one.
1529 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1530 if (ectx->ec_outer_ref->or_partial != NULL)
1531 {
1532 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1533 ++pt->pt_outer.out_up_partial->pt_refcount;
1534 }
1535 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001536
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001537 // If this function returns and the closure is still being used, we
1538 // need to make a copy of the context (arguments and local variables).
1539 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001540 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001541 {
1542 vim_free(pt);
1543 return FAIL;
1544 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001545 // Extra variable keeps the count of closures created in the current
1546 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001547 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1548 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1549
1550 ((partial_T **)ectx->ec_funcrefs.ga_data)
1551 [ectx->ec_funcrefs.ga_len] = pt;
1552 ++pt->pt_refcount;
1553 ++ectx->ec_funcrefs.ga_len;
1554 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001555 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001556 return OK;
1557}
1558
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001559/*
1560 * Execute iptr->isn_arg.string as an Ex command.
1561 */
1562 static int
1563exec_command(isn_T *iptr)
1564{
1565 source_cookie_T cookie;
1566
1567 SOURCING_LNUM = iptr->isn_lnum;
1568 // Pass getsourceline to get an error for a missing ":end"
1569 // command.
1570 CLEAR_FIELD(cookie);
1571 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1572 if (do_cmdline(iptr->isn_arg.string,
1573 getsourceline, &cookie,
1574 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1575 || did_emsg)
1576 return FAIL;
1577 return OK;
1578}
1579
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001580// used for v_instr of typval of VAR_INSTR
1581struct instr_S {
1582 ectx_T *instr_ectx;
1583 isn_T *instr_instr;
1584};
1585
Bram Moolenaar4c137212021-04-19 16:48:48 +02001586// used for substitute_instr
1587typedef struct subs_expr_S {
1588 ectx_T *subs_ectx;
1589 isn_T *subs_instr;
1590 int subs_status;
1591} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
1593// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001594#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595
1596// Get pointer to item at the bottom of the stack, -1 is the bottom.
1597#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001598#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 +01001599
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001600// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001601#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 +01001602
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001603// Set when calling do_debug().
1604static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001605static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001606
1607/*
1608 * When debugging lookup "name" and return the typeval.
1609 * When not found return NULL.
1610 */
1611 typval_T *
1612lookup_debug_var(char_u *name)
1613{
1614 int idx;
1615 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001616 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001617 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001618 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001619
1620 if (ectx == NULL)
1621 return NULL;
1622 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1623
1624 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001625 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001626 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001627 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001628 return STACK_TV_VAR(idx);
1629 }
1630
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001631 // Go through argument names.
1632 ufunc = dfunc->df_ufunc;
1633 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1634 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1635 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1636 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1637 - varargs_off + idx);
1638 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1639 return STACK_TV(ectx->ec_frame_idx - 1);
1640
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001641 return NULL;
1642}
1643
Bram Moolenaar26a44842021-09-02 18:49:06 +02001644/*
1645 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1646 * breakpoint was set in that function or when there is any expression.
1647 */
1648 int
1649may_break_in_function(ufunc_T *ufunc)
1650{
1651 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1652}
1653
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001654 static void
1655handle_debug(isn_T *iptr, ectx_T *ectx)
1656{
1657 char_u *line;
1658 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1659 + ectx->ec_dfunc_idx)->df_ufunc;
1660 isn_T *ni;
1661 int end_lnum = iptr->isn_lnum;
1662 garray_T ga;
1663 int lnum;
1664
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001665 if (ex_nesting_level > debug_break_level)
1666 {
1667 linenr_T breakpoint;
1668
Bram Moolenaar26a44842021-09-02 18:49:06 +02001669 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001670 return;
1671
1672 // check for the next breakpoint if needed
1673 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001674 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001675 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1676 return;
1677 }
1678
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001679 SOURCING_LNUM = iptr->isn_lnum;
1680 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001681 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001682
1683 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1684 if (ni->isn_type == ISN_DEBUG
1685 || ni->isn_type == ISN_RETURN
1686 || ni->isn_type == ISN_RETURN_VOID)
1687 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001688 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001689 break;
1690 }
1691
1692 if (end_lnum > iptr->isn_lnum)
1693 {
1694 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001695 for (lnum = iptr->isn_lnum; lnum < end_lnum
1696 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001697 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001698 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001699
Bram Moolenaar303215d2021-07-07 20:10:43 +02001700 if (p == NULL)
1701 continue; // left over from continuation line
1702 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001703 if (*p == '#')
1704 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001705 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001706 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1707 if (STRNCMP(p, "def ", 4) == 0)
1708 break;
1709 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001710 line = ga_concat_strings(&ga, " ");
1711 vim_free(ga.ga_data);
1712 }
1713 else
1714 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001715
Dominique Pelle6e969552021-06-17 13:53:41 +02001716 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001717 debug_context = NULL;
1718
1719 if (end_lnum > iptr->isn_lnum)
1720 vim_free(line);
1721}
1722
Bram Moolenaar4c137212021-04-19 16:48:48 +02001723/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001724 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001725 * Returns OK, FAIL or NOTDONE (uncatchable error).
1726 */
1727 static int
1728execute_storeindex(isn_T *iptr, ectx_T *ectx)
1729{
1730 vartype_T dest_type = iptr->isn_arg.vartype;
1731 typval_T *tv;
1732 typval_T *tv_idx = STACK_TV_BOT(-2);
1733 typval_T *tv_dest = STACK_TV_BOT(-1);
1734 int status = OK;
1735
1736 // Stack contains:
1737 // -3 value to be stored
1738 // -2 index
1739 // -1 dict or list
1740 tv = STACK_TV_BOT(-3);
1741 SOURCING_LNUM = iptr->isn_lnum;
1742 if (dest_type == VAR_ANY)
1743 {
1744 dest_type = tv_dest->v_type;
1745 if (dest_type == VAR_DICT)
1746 status = do_2string(tv_idx, TRUE, FALSE);
1747 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1748 {
1749 emsg(_(e_number_expected));
1750 status = FAIL;
1751 }
1752 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001753
1754 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001755 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001756 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001757 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001758 long lidx = (long)tv_idx->vval.v_number;
1759 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001760
Bram Moolenaare08be092022-02-17 13:08:26 +00001761 if (list == NULL)
1762 {
1763 emsg(_(e_list_not_set));
1764 return FAIL;
1765 }
1766 if (lidx < 0 && list->lv_len + lidx >= 0)
1767 // negative index is relative to the end
1768 lidx = list->lv_len + lidx;
1769 if (lidx < 0 || lidx > list->lv_len)
1770 {
1771 semsg(_(e_list_index_out_of_range_nr), lidx);
1772 return FAIL;
1773 }
1774 if (lidx < list->lv_len)
1775 {
1776 listitem_T *li = list_find(list, lidx);
1777
1778 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001779 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001780 return FAIL;
1781 // overwrite existing list item
1782 clear_tv(&li->li_tv);
1783 li->li_tv = *tv;
1784 }
1785 else
1786 {
1787 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1788 return FAIL;
1789 // append to list, only fails when out of memory
1790 if (list_append_tv(list, tv) == FAIL)
1791 return NOTDONE;
1792 clear_tv(tv);
1793 }
1794 }
1795 else if (dest_type == VAR_DICT)
1796 {
1797 char_u *key = tv_idx->vval.v_string;
1798 dict_T *dict = tv_dest->vval.v_dict;
1799 dictitem_T *di;
1800
1801 SOURCING_LNUM = iptr->isn_lnum;
1802 if (dict == NULL)
1803 {
1804 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001805 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001806 }
1807 if (key == NULL)
1808 key = (char_u *)"";
1809 di = dict_find(dict, key, -1);
1810 if (di != NULL)
1811 {
1812 if (error_if_locked(di->di_tv.v_lock,
1813 e_cannot_change_dict_item))
1814 return FAIL;
1815 // overwrite existing value
1816 clear_tv(&di->di_tv);
1817 di->di_tv = *tv;
1818 }
1819 else
1820 {
1821 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1822 return FAIL;
1823 // add to dict, only fails when out of memory
1824 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1825 return NOTDONE;
1826 clear_tv(tv);
1827 }
1828 }
1829 else if (dest_type == VAR_BLOB)
1830 {
1831 long lidx = (long)tv_idx->vval.v_number;
1832 blob_T *blob = tv_dest->vval.v_blob;
1833 varnumber_T nr;
1834 int error = FALSE;
1835 int len;
1836
1837 if (blob == NULL)
1838 {
1839 emsg(_(e_blob_not_set));
1840 return FAIL;
1841 }
1842 len = blob_len(blob);
1843 if (lidx < 0 && len + lidx >= 0)
1844 // negative index is relative to the end
1845 lidx = len + lidx;
1846
1847 // Can add one byte at the end.
1848 if (lidx < 0 || lidx > len)
1849 {
1850 semsg(_(e_blob_index_out_of_range_nr), lidx);
1851 return FAIL;
1852 }
1853 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1854 return FAIL;
1855 nr = tv_get_number_chk(tv, &error);
1856 if (error)
1857 return FAIL;
1858 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001859 }
1860 else
1861 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001862 status = FAIL;
1863 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001864 }
1865 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001866
1867 clear_tv(tv_idx);
1868 clear_tv(tv_dest);
1869 ectx->ec_stack.ga_len -= 3;
1870 if (status == FAIL)
1871 {
1872 clear_tv(tv);
1873 return FAIL;
1874 }
1875 return OK;
1876}
1877
1878/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001879 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001880 */
1881 static int
1882execute_storerange(isn_T *iptr, ectx_T *ectx)
1883{
1884 typval_T *tv;
1885 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1886 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1887 typval_T *tv_dest = STACK_TV_BOT(-1);
1888 int status = OK;
1889
1890 // Stack contains:
1891 // -4 value to be stored
1892 // -3 first index or "none"
1893 // -2 second index or "none"
1894 // -1 destination list or blob
1895 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001896 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001897 if (tv_dest->v_type == VAR_LIST)
1898 {
1899 long n1;
1900 long n2;
1901 int error = FALSE;
1902
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001903 n1 = (long)tv_get_number_chk(tv_idx1, &error);
1904 if (error)
1905 status = FAIL;
1906 else
1907 {
1908 if (tv_idx2->v_type == VAR_SPECIAL
1909 && tv_idx2->vval.v_number == VVAL_NONE)
1910 n2 = list_len(tv_dest->vval.v_list) - 1;
1911 else
1912 n2 = (long)tv_get_number_chk(tv_idx2, &error);
1913 if (error)
1914 status = FAIL;
1915 else
1916 {
1917 listitem_T *li1 = check_range_index_one(
1918 tv_dest->vval.v_list, &n1, FALSE);
1919
1920 if (li1 == NULL)
1921 status = FAIL;
1922 else
1923 {
1924 status = check_range_index_two(
1925 tv_dest->vval.v_list,
1926 &n1, li1, &n2, FALSE);
1927 if (status != FAIL)
1928 status = list_assign_range(
1929 tv_dest->vval.v_list,
1930 tv->vval.v_list,
1931 n1,
1932 n2,
1933 tv_idx2->v_type == VAR_SPECIAL,
1934 (char_u *)"=",
1935 (char_u *)"[unknown]");
1936 }
1937 }
1938 }
1939 }
1940 else if (tv_dest->v_type == VAR_BLOB)
1941 {
1942 varnumber_T n1;
1943 varnumber_T n2;
1944 int error = FALSE;
1945
1946 n1 = tv_get_number_chk(tv_idx1, &error);
1947 if (error)
1948 status = FAIL;
1949 else
1950 {
1951 if (tv_idx2->v_type == VAR_SPECIAL
1952 && tv_idx2->vval.v_number == VVAL_NONE)
1953 n2 = blob_len(tv_dest->vval.v_blob) - 1;
1954 else
1955 n2 = tv_get_number_chk(tv_idx2, &error);
1956 if (error)
1957 status = FAIL;
1958 else
1959 {
1960 long bloblen = blob_len(tv_dest->vval.v_blob);
1961
1962 if (check_blob_index(bloblen,
1963 n1, FALSE) == FAIL
1964 || check_blob_range(bloblen,
1965 n1, n2, FALSE) == FAIL)
1966 status = FAIL;
1967 else
1968 status = blob_set_range(
1969 tv_dest->vval.v_blob, n1, n2, tv);
1970 }
1971 }
1972 }
1973 else
1974 {
1975 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001976 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001977 }
1978
1979 clear_tv(tv_idx1);
1980 clear_tv(tv_idx2);
1981 clear_tv(tv_dest);
1982 ectx->ec_stack.ga_len -= 4;
1983 clear_tv(tv);
1984
1985 return status;
1986}
1987
1988/*
1989 * Unlet item in list or dict variable.
1990 */
1991 static int
1992execute_unletindex(isn_T *iptr, ectx_T *ectx)
1993{
1994 typval_T *tv_idx = STACK_TV_BOT(-2);
1995 typval_T *tv_dest = STACK_TV_BOT(-1);
1996 int status = OK;
1997
1998 // Stack contains:
1999 // -2 index
2000 // -1 dict or list
2001 if (tv_dest->v_type == VAR_DICT)
2002 {
2003 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002004 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002005 {
2006 SOURCING_LNUM = iptr->isn_lnum;
2007 semsg(_(e_expected_str_but_got_str),
2008 vartype_name(VAR_STRING),
2009 vartype_name(tv_idx->v_type));
2010 status = FAIL;
2011 }
2012 else
2013 {
2014 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002015 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002016 dictitem_T *di = NULL;
2017
2018 if (d != NULL && value_check_lock(
2019 d->dv_lock, NULL, FALSE))
2020 status = FAIL;
2021 else
2022 {
2023 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002024 if (tv_idx->v_type == VAR_STRING)
2025 {
2026 key = tv_idx->vval.v_string;
2027 if (key == NULL)
2028 key = (char_u *)"";
2029 }
2030 else
2031 {
2032 key = tv_get_string(tv_idx);
2033 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002034 if (d != NULL)
2035 di = dict_find(d, key, (int)STRLEN(key));
2036 if (di == NULL)
2037 {
2038 // NULL dict is equivalent to empty dict
2039 semsg(_(e_key_not_present_in_dictionary),
2040 key);
2041 status = FAIL;
2042 }
2043 else if (var_check_fixed(di->di_flags,
2044 NULL, FALSE)
2045 || var_check_ro(di->di_flags,
2046 NULL, FALSE))
2047 status = FAIL;
2048 else
2049 dictitem_remove(d, di);
2050 }
2051 }
2052 }
2053 else if (tv_dest->v_type == VAR_LIST)
2054 {
2055 // unlet a List item, index must be a number
2056 SOURCING_LNUM = iptr->isn_lnum;
2057 if (check_for_number(tv_idx) == FAIL)
2058 {
2059 status = FAIL;
2060 }
2061 else
2062 {
2063 list_T *l = tv_dest->vval.v_list;
2064 long n = (long)tv_idx->vval.v_number;
2065
2066 if (l != NULL && value_check_lock(
2067 l->lv_lock, NULL, FALSE))
2068 status = FAIL;
2069 else
2070 {
2071 listitem_T *li = list_find(l, n);
2072
2073 if (li == NULL)
2074 {
2075 SOURCING_LNUM = iptr->isn_lnum;
2076 semsg(_(e_list_index_out_of_range_nr), n);
2077 status = FAIL;
2078 }
2079 else if (value_check_lock(li->li_tv.v_lock,
2080 NULL, FALSE))
2081 status = FAIL;
2082 else
2083 listitem_remove(l, li);
2084 }
2085 }
2086 }
2087 else
2088 {
2089 status = FAIL;
2090 semsg(_(e_cannot_index_str),
2091 vartype_name(tv_dest->v_type));
2092 }
2093
2094 clear_tv(tv_idx);
2095 clear_tv(tv_dest);
2096 ectx->ec_stack.ga_len -= 2;
2097
2098 return status;
2099}
2100
2101/*
2102 * Unlet a range of items in a list variable.
2103 */
2104 static int
2105execute_unletrange(isn_T *iptr, ectx_T *ectx)
2106{
2107 // Stack contains:
2108 // -3 index1
2109 // -2 index2
2110 // -1 dict or list
2111 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2112 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2113 typval_T *tv_dest = STACK_TV_BOT(-1);
2114 int status = OK;
2115
2116 if (tv_dest->v_type == VAR_LIST)
2117 {
2118 // indexes must be a number
2119 SOURCING_LNUM = iptr->isn_lnum;
2120 if (check_for_number(tv_idx1) == FAIL
2121 || (tv_idx2->v_type != VAR_SPECIAL
2122 && check_for_number(tv_idx2) == FAIL))
2123 {
2124 status = FAIL;
2125 }
2126 else
2127 {
2128 list_T *l = tv_dest->vval.v_list;
2129 long n1 = (long)tv_idx1->vval.v_number;
2130 long n2 = tv_idx2->v_type == VAR_SPECIAL
2131 ? 0 : (long)tv_idx2->vval.v_number;
2132 listitem_T *li;
2133
2134 li = list_find_index(l, &n1);
2135 if (li == NULL)
2136 status = FAIL;
2137 else
2138 {
2139 if (n1 < 0)
2140 n1 = list_idx_of_item(l, li);
2141 if (n2 < 0)
2142 {
2143 listitem_T *li2 = list_find(l, n2);
2144
2145 if (li2 == NULL)
2146 status = FAIL;
2147 else
2148 n2 = list_idx_of_item(l, li2);
2149 }
2150 if (status != FAIL
2151 && tv_idx2->v_type != VAR_SPECIAL
2152 && n2 < n1)
2153 {
2154 semsg(_(e_list_index_out_of_range_nr), n2);
2155 status = FAIL;
2156 }
2157 if (status != FAIL
2158 && list_unlet_range(l, li, NULL, n1,
2159 tv_idx2->v_type != VAR_SPECIAL, n2)
2160 == FAIL)
2161 status = FAIL;
2162 }
2163 }
2164 }
2165 else
2166 {
2167 status = FAIL;
2168 SOURCING_LNUM = iptr->isn_lnum;
2169 semsg(_(e_cannot_index_str),
2170 vartype_name(tv_dest->v_type));
2171 }
2172
2173 clear_tv(tv_idx1);
2174 clear_tv(tv_idx2);
2175 clear_tv(tv_dest);
2176 ectx->ec_stack.ga_len -= 3;
2177
2178 return status;
2179}
2180
2181/*
2182 * Top of a for loop.
2183 */
2184 static int
2185execute_for(isn_T *iptr, ectx_T *ectx)
2186{
2187 typval_T *tv;
2188 typval_T *ltv = STACK_TV_BOT(-1);
2189 typval_T *idxtv =
2190 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2191
2192 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2193 return FAIL;
2194 if (ltv->v_type == VAR_LIST)
2195 {
2196 list_T *list = ltv->vval.v_list;
2197
2198 // push the next item from the list
2199 ++idxtv->vval.v_number;
2200 if (list == NULL
2201 || idxtv->vval.v_number >= list->lv_len)
2202 {
2203 // past the end of the list, jump to "endfor"
2204 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2205 may_restore_cmdmod(&ectx->ec_funclocal);
2206 }
2207 else if (list->lv_first == &range_list_item)
2208 {
2209 // non-materialized range() list
2210 tv = STACK_TV_BOT(0);
2211 tv->v_type = VAR_NUMBER;
2212 tv->v_lock = 0;
2213 tv->vval.v_number = list_find_nr(
2214 list, idxtv->vval.v_number, NULL);
2215 ++ectx->ec_stack.ga_len;
2216 }
2217 else
2218 {
2219 listitem_T *li = list_find(list,
2220 idxtv->vval.v_number);
2221
2222 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2223 ++ectx->ec_stack.ga_len;
2224 }
2225 }
2226 else if (ltv->v_type == VAR_STRING)
2227 {
2228 char_u *str = ltv->vval.v_string;
2229
2230 // The index is for the last byte of the previous
2231 // character.
2232 ++idxtv->vval.v_number;
2233 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2234 {
2235 // past the end of the string, jump to "endfor"
2236 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2237 may_restore_cmdmod(&ectx->ec_funclocal);
2238 }
2239 else
2240 {
2241 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2242
2243 // Push the next character from the string.
2244 tv = STACK_TV_BOT(0);
2245 tv->v_type = VAR_STRING;
2246 tv->vval.v_string = vim_strnsave(
2247 str + idxtv->vval.v_number, clen);
2248 ++ectx->ec_stack.ga_len;
2249 idxtv->vval.v_number += clen - 1;
2250 }
2251 }
2252 else if (ltv->v_type == VAR_BLOB)
2253 {
2254 blob_T *blob = ltv->vval.v_blob;
2255
2256 // When we get here the first time make a copy of the
2257 // blob, so that the iteration still works when it is
2258 // changed.
2259 if (idxtv->vval.v_number == -1 && blob != NULL)
2260 {
2261 blob_copy(blob, ltv);
2262 blob_unref(blob);
2263 blob = ltv->vval.v_blob;
2264 }
2265
2266 // The index is for the previous byte.
2267 ++idxtv->vval.v_number;
2268 if (blob == NULL
2269 || idxtv->vval.v_number >= blob_len(blob))
2270 {
2271 // past the end of the blob, jump to "endfor"
2272 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2273 may_restore_cmdmod(&ectx->ec_funclocal);
2274 }
2275 else
2276 {
2277 // Push the next byte from the blob.
2278 tv = STACK_TV_BOT(0);
2279 tv->v_type = VAR_NUMBER;
2280 tv->vval.v_number = blob_get(blob,
2281 idxtv->vval.v_number);
2282 ++ectx->ec_stack.ga_len;
2283 }
2284 }
2285 else
2286 {
2287 semsg(_(e_for_loop_on_str_not_supported),
2288 vartype_name(ltv->v_type));
2289 return FAIL;
2290 }
2291 return OK;
2292}
2293
2294/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002295 * Load instruction for w:/b:/g:/t: variable.
2296 * "isn_type" is used instead of "iptr->isn_type".
2297 */
2298 static int
2299load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2300{
2301 dictitem_T *di = NULL;
2302 hashtab_T *ht = NULL;
2303 char namespace;
2304
2305 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2306 return NOTDONE;
2307 switch (isn_type)
2308 {
2309 case ISN_LOADG:
2310 ht = get_globvar_ht();
2311 namespace = 'g';
2312 break;
2313 case ISN_LOADB:
2314 ht = &curbuf->b_vars->dv_hashtab;
2315 namespace = 'b';
2316 break;
2317 case ISN_LOADW:
2318 ht = &curwin->w_vars->dv_hashtab;
2319 namespace = 'w';
2320 break;
2321 case ISN_LOADT:
2322 ht = &curtab->tp_vars->dv_hashtab;
2323 namespace = 't';
2324 break;
2325 default: // Cannot reach here
2326 return NOTDONE;
2327 }
2328 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2329
2330 if (di == NULL && ht == get_globvar_ht()
2331 && vim_strchr(iptr->isn_arg.string,
2332 AUTOLOAD_CHAR) != NULL)
2333 {
2334 // Global variable has an autoload name, may still need
2335 // to load the script.
2336 if (script_autoload(iptr->isn_arg.string, FALSE))
2337 di = find_var_in_ht(ht, 0,
2338 iptr->isn_arg.string, TRUE);
2339 if (did_emsg)
2340 return FAIL;
2341 }
2342
2343 if (di == NULL)
2344 {
2345 SOURCING_LNUM = iptr->isn_lnum;
2346 if (vim_strchr(iptr->isn_arg.string,
2347 AUTOLOAD_CHAR) != NULL)
2348 // no check if the item exists in the script but
2349 // isn't exported, it is too complicated
2350 semsg(_(e_item_not_found_in_script_str),
2351 iptr->isn_arg.string);
2352 else
2353 semsg(_(e_undefined_variable_char_str),
2354 namespace, iptr->isn_arg.string);
2355 return FAIL;
2356 }
2357 else
2358 {
2359 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2360 ++ectx->ec_stack.ga_len;
2361 }
2362 return OK;
2363}
2364
2365/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002366 * Execute instructions in execution context "ectx".
2367 * Return OK or FAIL;
2368 */
2369 static int
2370exec_instructions(ectx_T *ectx)
2371{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002372 int ret = FAIL;
2373 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002374 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002375
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002376 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002377 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002378
Bram Moolenaarff652882021-05-16 15:24:49 +02002379 // Only catch exceptions in this instruction list.
2380 ectx->ec_trylevel_at_start = trylevel;
2381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 for (;;)
2383 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002384 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002386 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002387
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002388 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002389 {
2390 line_breakcheck();
2391 breakcheck_count = 0;
2392 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002393 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002394 {
2395 // Turn CTRL-C into an exception.
2396 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002397 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002398 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002399 did_throw = TRUE;
2400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002402 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002403 {
2404 // Turn an error message into an exception.
2405 did_emsg = FALSE;
2406 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002407 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002408 did_throw = TRUE;
2409 *msg_list = NULL;
2410 }
2411
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002412 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002414 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002415 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002416 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417
2418 // An exception jumps to the first catch, finally, or returns from
2419 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002420 while (index > 0)
2421 {
2422 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002423 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002424 break;
2425 // In the catch and finally block of this try we have to go up
2426 // one level.
2427 --index;
2428 trycmd = NULL;
2429 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002430 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002432 if (trycmd->tcd_in_catch)
2433 {
2434 // exception inside ":catch", jump to ":finally" once
2435 ectx->ec_iidx = trycmd->tcd_finally_idx;
2436 trycmd->tcd_finally_idx = 0;
2437 }
2438 else
2439 // jump to first ":catch"
2440 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002441 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002442 did_throw = FALSE; // don't come back here until :endtry
2443 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 }
2445 else
2446 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002447 // Not inside try or need to return from current functions.
2448 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002449 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002450 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002451 tv = STACK_TV_BOT(0);
2452 tv->v_type = VAR_NUMBER;
2453 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002454 ++ectx->ec_stack.ga_len;
2455 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002457 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002458 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002459 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002460 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 goto done;
2462 }
2463
Bram Moolenaar4c137212021-04-19 16:48:48 +02002464 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002465 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 }
2467 continue;
2468 }
2469
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002470 /*
2471 * Big switch on the instruction. Most compilers will be turning this
2472 * into an efficient lookup table, since the "case" values are an enum
2473 * with sequential numbers. It may look ugly, but it should be the
2474 * most efficient way.
2475 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002476 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 switch (iptr->isn_type)
2478 {
2479 // execute Ex command line
2480 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002481 if (exec_command(iptr) == FAIL)
2482 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 break;
2484
Bram Moolenaar20677332021-06-06 17:02:53 +02002485 // execute Ex command line split at NL characters.
2486 case ISN_EXEC_SPLIT:
2487 {
2488 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002489 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002490
2491 SOURCING_LNUM = iptr->isn_lnum;
2492 CLEAR_FIELD(cookie);
2493 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2494 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002495 line = get_split_sourceline(0, &cookie, 0, 0);
2496 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002497 get_split_sourceline, &cookie,
2498 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2499 == FAIL
2500 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002501 {
2502 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002503 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002504 }
2505 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002506 }
2507 break;
2508
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002509 // execute Ex command line that is only a range
2510 case ISN_EXECRANGE:
2511 {
2512 exarg_T ea;
2513 char *error = NULL;
2514
2515 CLEAR_FIELD(ea);
2516 ea.cmdidx = CMD_SIZE;
2517 ea.addr_type = ADDR_LINES;
2518 ea.cmd = iptr->isn_arg.string;
2519 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002520 if (ea.cmd == NULL)
2521 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002522 if (error == NULL)
2523 error = ex_range_without_command(&ea);
2524 if (error != NULL)
2525 {
2526 SOURCING_LNUM = iptr->isn_lnum;
2527 emsg(error);
2528 goto on_error;
2529 }
2530 }
2531 break;
2532
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002533 // Evaluate an expression with legacy syntax, push it onto the
2534 // stack.
2535 case ISN_LEGACY_EVAL:
2536 {
2537 char_u *arg = iptr->isn_arg.string;
2538 int res;
2539 int save_flags = cmdmod.cmod_flags;
2540
Bram Moolenaar35578162021-08-02 19:10:38 +02002541 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002542 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002543 tv = STACK_TV_BOT(0);
2544 init_tv(tv);
2545 cmdmod.cmod_flags |= CMOD_LEGACY;
2546 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2547 cmdmod.cmod_flags = save_flags;
2548 if (res == FAIL)
2549 goto on_error;
2550 ++ectx->ec_stack.ga_len;
2551 }
2552 break;
2553
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002554 // push typeval VAR_INSTR with instructions to be executed
2555 case ISN_INSTR:
2556 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002557 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002558 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002559 tv = STACK_TV_BOT(0);
2560 tv->vval.v_instr = ALLOC_ONE(instr_T);
2561 if (tv->vval.v_instr == NULL)
2562 goto on_error;
2563 ++ectx->ec_stack.ga_len;
2564
2565 tv->v_type = VAR_INSTR;
2566 tv->vval.v_instr->instr_ectx = ectx;
2567 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2568 }
2569 break;
2570
Bram Moolenaar4c137212021-04-19 16:48:48 +02002571 // execute :substitute with an expression
2572 case ISN_SUBSTITUTE:
2573 {
2574 subs_T *subs = &iptr->isn_arg.subs;
2575 source_cookie_T cookie;
2576 struct subs_expr_S *save_instr = substitute_instr;
2577 struct subs_expr_S subs_instr;
2578 int res;
2579
2580 subs_instr.subs_ectx = ectx;
2581 subs_instr.subs_instr = subs->subs_instr;
2582 subs_instr.subs_status = OK;
2583 substitute_instr = &subs_instr;
2584
2585 SOURCING_LNUM = iptr->isn_lnum;
2586 // This is very much like ISN_EXEC
2587 CLEAR_FIELD(cookie);
2588 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2589 res = do_cmdline(subs->subs_cmd,
2590 getsourceline, &cookie,
2591 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2592 substitute_instr = save_instr;
2593
2594 if (res == FAIL || did_emsg
2595 || subs_instr.subs_status == FAIL)
2596 goto on_error;
2597 }
2598 break;
2599
2600 case ISN_FINISH:
2601 goto done;
2602
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002603 case ISN_REDIRSTART:
2604 // create a dummy entry for var_redir_str()
2605 if (alloc_redir_lval() == FAIL)
2606 goto on_error;
2607
2608 // The output is stored in growarray "redir_ga" until
2609 // redirection ends.
2610 init_redir_ga();
2611 redir_vname = 1;
2612 break;
2613
2614 case ISN_REDIREND:
2615 {
2616 char_u *res = get_clear_redir_ga();
2617
2618 // End redirection, put redirected text on the stack.
2619 clear_redir_lval();
2620 redir_vname = 0;
2621
Bram Moolenaar35578162021-08-02 19:10:38 +02002622 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002623 {
2624 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002625 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002626 }
2627 tv = STACK_TV_BOT(0);
2628 tv->v_type = VAR_STRING;
2629 tv->vval.v_string = res;
2630 ++ectx->ec_stack.ga_len;
2631 }
2632 break;
2633
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002634 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002635#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002636 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2637 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002638#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002639 break;
2640
2641 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002642#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002643 {
2644 exarg_T ea;
2645 int res;
2646
2647 CLEAR_FIELD(ea);
2648 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2649 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2650 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2651 --ectx->ec_stack.ga_len;
2652 tv = STACK_TV_BOT(0);
2653 res = cexpr_core(&ea, tv);
2654 clear_tv(tv);
2655 if (res == FAIL)
2656 goto on_error;
2657 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002658#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002659 break;
2660
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002661 // execute Ex command from pieces on the stack
2662 case ISN_EXECCONCAT:
2663 {
2664 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002665 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002666 int pass;
2667 int i;
2668 char_u *cmd = NULL;
2669 char_u *str;
2670
2671 for (pass = 1; pass <= 2; ++pass)
2672 {
2673 for (i = 0; i < count; ++i)
2674 {
2675 tv = STACK_TV_BOT(i - count);
2676 str = tv->vval.v_string;
2677 if (str != NULL && *str != NUL)
2678 {
2679 if (pass == 2)
2680 STRCPY(cmd + len, str);
2681 len += STRLEN(str);
2682 }
2683 if (pass == 2)
2684 clear_tv(tv);
2685 }
2686 if (pass == 1)
2687 {
2688 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002689 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002690 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002691 len = 0;
2692 }
2693 }
2694
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002695 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002696 do_cmdline_cmd(cmd);
2697 vim_free(cmd);
2698 }
2699 break;
2700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 // execute :echo {string} ...
2702 case ISN_ECHO:
2703 {
2704 int count = iptr->isn_arg.echo.echo_count;
2705 int atstart = TRUE;
2706 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002707 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708
2709 for (idx = 0; idx < count; ++idx)
2710 {
2711 tv = STACK_TV_BOT(idx - count);
2712 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2713 &atstart, &needclr);
2714 clear_tv(tv);
2715 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002716 if (needclr)
2717 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002718 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 }
2720 break;
2721
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002722 // :execute {string} ...
2723 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002724 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002725 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002726 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002727 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002728 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002729 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002730 {
2731 int count = iptr->isn_arg.number;
2732 garray_T ga;
2733 char_u buf[NUMBUFLEN];
2734 char_u *p;
2735 int len;
2736 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002737 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002738
2739 ga_init2(&ga, 1, 80);
2740 for (idx = 0; idx < count; ++idx)
2741 {
2742 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002743 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002744 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002745 if (tv->v_type == VAR_CHANNEL
2746 || tv->v_type == VAR_JOB)
2747 {
2748 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002749 semsg(_(e_using_invalid_value_as_string_str),
2750 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002751 break;
2752 }
2753 else
2754 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002755 }
2756 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002757 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002758
2759 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002760 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002761 failed = TRUE;
2762 else
2763 {
2764 if (ga.ga_len > 0)
2765 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2766 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2767 ga.ga_len += len;
2768 }
2769 clear_tv(tv);
2770 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002771 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002772 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002773 {
2774 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002775 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002776 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002777
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002778 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002779 {
2780 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002781 {
2782 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002783 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002784 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002785 {
2786 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002787 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002788 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002789 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002790 else
2791 {
2792 msg_sb_eol();
2793 if (iptr->isn_type == ISN_ECHOMSG)
2794 {
2795 msg_attr(ga.ga_data, echo_attr);
2796 out_flush();
2797 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002798 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2799 {
2800 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2801 TRUE);
2802 ui_write((char_u *)"\r\n", 2, TRUE);
2803 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002804 else
2805 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002806 SOURCING_LNUM = iptr->isn_lnum;
2807 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002808 }
2809 }
2810 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002811 ga_clear(&ga);
2812 }
2813 break;
2814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 // load local variable or argument
2816 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002817 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002818 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002820 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 break;
2822
2823 // load v: variable
2824 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002825 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002826 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002828 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 break;
2830
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002831 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 case ISN_LOADSCRIPT:
2833 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002834 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 svar_T *sv;
2836
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002837 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002838 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002839 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002840 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002841 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002842 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002844 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 }
2846 break;
2847
2848 // load s: variable in old script
2849 case ISN_LOADS:
2850 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002851 hashtab_T *ht = &SCRIPT_VARS(
2852 iptr->isn_arg.loadstore.ls_sid);
2853 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 if (di == NULL)
2857 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002858 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002859 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002860 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 }
2862 else
2863 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002864 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002865 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002867 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 }
2869 }
2870 break;
2871
Bram Moolenaard3aac292020-04-19 14:32:17 +02002872 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002874 case ISN_LOADB:
2875 case ISN_LOADW:
2876 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002878 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002879
Bram Moolenaar06b77222022-01-25 15:51:56 +00002880 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002881 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002882 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002883 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 break;
2887
Bram Moolenaar03290b82020-12-19 16:30:44 +01002888 // load autoload variable
2889 case ISN_LOADAUTO:
2890 {
2891 char_u *name = iptr->isn_arg.string;
2892
Bram Moolenaar35578162021-08-02 19:10:38 +02002893 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002894 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002895 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002896 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002897 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002898 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002899 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002900 }
2901 break;
2902
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002903 // load g:/b:/w:/t: namespace
2904 case ISN_LOADGDICT:
2905 case ISN_LOADBDICT:
2906 case ISN_LOADWDICT:
2907 case ISN_LOADTDICT:
2908 {
2909 dict_T *d = NULL;
2910
2911 switch (iptr->isn_type)
2912 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002913 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2914 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2915 case ISN_LOADWDICT: d = curwin->w_vars; break;
2916 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002917 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002918 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002919 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002920 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002921 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002922 tv = STACK_TV_BOT(0);
2923 tv->v_type = VAR_DICT;
2924 tv->v_lock = 0;
2925 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002926 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002927 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002928 }
2929 break;
2930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 // load &option
2932 case ISN_LOADOPT:
2933 {
2934 typval_T optval;
2935 char_u *name = iptr->isn_arg.string;
2936
Bram Moolenaara8c17702020-04-01 21:17:24 +02002937 // This is not expected to fail, name is checked during
2938 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002939 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002940 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002941 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002942 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002944 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 }
2946 break;
2947
2948 // load $ENV
2949 case ISN_LOADENV:
2950 {
2951 typval_T optval;
2952 char_u *name = iptr->isn_arg.string;
2953
Bram Moolenaar35578162021-08-02 19:10:38 +02002954 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002955 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002956 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002957 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002959 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 }
2961 break;
2962
2963 // load @register
2964 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002965 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002966 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 tv = STACK_TV_BOT(0);
2968 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002969 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002970 // This may result in NULL, which should be equivalent to an
2971 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 tv->vval.v_string = get_reg_contents(
2973 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002974 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 break;
2976
2977 // store local variable
2978 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002979 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 tv = STACK_TV_VAR(iptr->isn_arg.number);
2981 clear_tv(tv);
2982 *tv = *STACK_TV_BOT(0);
2983 break;
2984
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002985 // store s: variable in old script
2986 case ISN_STORES:
2987 {
2988 hashtab_T *ht = &SCRIPT_VARS(
2989 iptr->isn_arg.loadstore.ls_sid);
2990 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002991 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002992
Bram Moolenaar4c137212021-04-19 16:48:48 +02002993 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002994 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002995 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002996 else
2997 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002998 SOURCING_LNUM = iptr->isn_lnum;
2999 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003000 {
3001 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003002 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003003 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003004 clear_tv(&di->di_tv);
3005 di->di_tv = *STACK_TV_BOT(0);
3006 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003007 }
3008 break;
3009
3010 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 case ISN_STORESCRIPT:
3012 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003013 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3014 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003016 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003017 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003018 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003019 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003020
3021 // "const" and "final" are checked at compile time, locking
3022 // the value needs to be checked here.
3023 SOURCING_LNUM = iptr->isn_lnum;
3024 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003025 {
3026 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003027 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003028 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 clear_tv(sv->sv_tv);
3031 *sv->sv_tv = *STACK_TV_BOT(0);
3032 }
3033 break;
3034
3035 // store option
3036 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003037 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003039 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3040 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 long n = 0;
3042 char_u *s = NULL;
3043 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003044 char_u numbuf[NUMBUFLEN];
3045 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046
Bram Moolenaar4c137212021-04-19 16:48:48 +02003047 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 tv = STACK_TV_BOT(0);
3049 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003050 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003052 if (s == NULL)
3053 s = (char_u *)"";
3054 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003055 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3056 {
3057 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003058 // If the option can be set to a function reference or
3059 // a lambda and the passed value is a function
3060 // reference, then convert it to the name (string) of
3061 // the function reference.
3062 s = tv2string(tv, &tofree, numbuf, 0);
3063 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003064 {
3065 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003066 goto on_error;
3067 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003070 // must be VAR_NUMBER, CHECKTYPE makes sure
3071 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003072 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003073 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003074 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 if (msg != NULL)
3076 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003077 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003079 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 }
3082 break;
3083
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003084 // store $ENV
3085 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003086 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003087 tv = STACK_TV_BOT(0);
3088 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3089 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003090 break;
3091
3092 // store @r
3093 case ISN_STOREREG:
3094 {
3095 int reg = iptr->isn_arg.number;
3096
Bram Moolenaar4c137212021-04-19 16:48:48 +02003097 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003098 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003099 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003100 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003101 }
3102 break;
3103
3104 // store v: variable
3105 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003107 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3108 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003109 // should not happen, type is checked when compiling
3110 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003111 break;
3112
Bram Moolenaard3aac292020-04-19 14:32:17 +02003113 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003115 case ISN_STOREB:
3116 case ISN_STOREW:
3117 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003119 dictitem_T *di;
3120 hashtab_T *ht;
3121 char_u *name = iptr->isn_arg.string + 2;
3122
Bram Moolenaard3aac292020-04-19 14:32:17 +02003123 switch (iptr->isn_type)
3124 {
3125 case ISN_STOREG:
3126 ht = get_globvar_ht();
3127 break;
3128 case ISN_STOREB:
3129 ht = &curbuf->b_vars->dv_hashtab;
3130 break;
3131 case ISN_STOREW:
3132 ht = &curwin->w_vars->dv_hashtab;
3133 break;
3134 case ISN_STORET:
3135 ht = &curtab->tp_vars->dv_hashtab;
3136 break;
3137 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003138 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140
Bram Moolenaar4c137212021-04-19 16:48:48 +02003141 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003142 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003144 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 else
3146 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003147 SOURCING_LNUM = iptr->isn_lnum;
3148 if (var_check_permission(di, name) == FAIL)
3149 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 clear_tv(&di->di_tv);
3151 di->di_tv = *STACK_TV_BOT(0);
3152 }
3153 }
3154 break;
3155
Bram Moolenaar03290b82020-12-19 16:30:44 +01003156 // store an autoload variable
3157 case ISN_STOREAUTO:
3158 SOURCING_LNUM = iptr->isn_lnum;
3159 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3160 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003161 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003162 break;
3163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 // store number in local variable
3165 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003166 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 clear_tv(tv);
3168 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003169 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 break;
3171
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003172 // store value in list or dict variable
3173 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003174 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003175 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003176
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003177 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003178 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003179 if (res == NOTDONE)
3180 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003181 }
3182 break;
3183
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003184 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003185 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003186 if (execute_storerange(iptr, ectx) == FAIL)
3187 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003188 break;
3189
Bram Moolenaar0186e582021-01-10 18:33:11 +01003190 // load or store variable or argument from outer scope
3191 case ISN_LOADOUTER:
3192 case ISN_STOREOUTER:
3193 {
3194 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003195 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3196 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003197
3198 while (depth > 1 && outer != NULL)
3199 {
3200 outer = outer->out_up;
3201 --depth;
3202 }
3203 if (outer == NULL)
3204 {
3205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003206 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3207 || ectx->ec_outer_ref == NULL)
3208 // Possibly :def function called from legacy
3209 // context.
3210 emsg(_(e_closure_called_from_invalid_context));
3211 else
3212 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003213 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003214 }
3215 tv = ((typval_T *)outer->out_stack->ga_data)
3216 + outer->out_frame_idx + STACK_FRAME_SIZE
3217 + iptr->isn_arg.outer.outer_idx;
3218 if (iptr->isn_type == ISN_LOADOUTER)
3219 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003220 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003221 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003222 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003223 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003224 }
3225 else
3226 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003227 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003228 clear_tv(tv);
3229 *tv = *STACK_TV_BOT(0);
3230 }
3231 }
3232 break;
3233
Bram Moolenaar752fc692021-01-04 21:57:11 +01003234 // unlet item in list or dict variable
3235 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003236 if (execute_unletindex(iptr, ectx) == FAIL)
3237 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003238 break;
3239
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003240 // unlet range of items in list variable
3241 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003242 if (execute_unletrange(iptr, ectx) == FAIL)
3243 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003244 break;
3245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 // push constant
3247 case ISN_PUSHNR:
3248 case ISN_PUSHBOOL:
3249 case ISN_PUSHSPEC:
3250 case ISN_PUSHF:
3251 case ISN_PUSHS:
3252 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003253 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003254 case ISN_PUSHCHANNEL:
3255 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003256 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003257 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003259 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003260 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 switch (iptr->isn_type)
3262 {
3263 case ISN_PUSHNR:
3264 tv->v_type = VAR_NUMBER;
3265 tv->vval.v_number = iptr->isn_arg.number;
3266 break;
3267 case ISN_PUSHBOOL:
3268 tv->v_type = VAR_BOOL;
3269 tv->vval.v_number = iptr->isn_arg.number;
3270 break;
3271 case ISN_PUSHSPEC:
3272 tv->v_type = VAR_SPECIAL;
3273 tv->vval.v_number = iptr->isn_arg.number;
3274 break;
3275#ifdef FEAT_FLOAT
3276 case ISN_PUSHF:
3277 tv->v_type = VAR_FLOAT;
3278 tv->vval.v_float = iptr->isn_arg.fnumber;
3279 break;
3280#endif
3281 case ISN_PUSHBLOB:
3282 blob_copy(iptr->isn_arg.blob, tv);
3283 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003284 case ISN_PUSHFUNC:
3285 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003286 if (iptr->isn_arg.string == NULL)
3287 tv->vval.v_string = NULL;
3288 else
3289 tv->vval.v_string =
3290 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003291 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003292 case ISN_PUSHCHANNEL:
3293#ifdef FEAT_JOB_CHANNEL
3294 tv->v_type = VAR_CHANNEL;
3295 tv->vval.v_channel = iptr->isn_arg.channel;
3296 if (tv->vval.v_channel != NULL)
3297 ++tv->vval.v_channel->ch_refcount;
3298#endif
3299 break;
3300 case ISN_PUSHJOB:
3301#ifdef FEAT_JOB_CHANNEL
3302 tv->v_type = VAR_JOB;
3303 tv->vval.v_job = iptr->isn_arg.job;
3304 if (tv->vval.v_job != NULL)
3305 ++tv->vval.v_job->jv_refcount;
3306#endif
3307 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 default:
3309 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003310 tv->vval.v_string = vim_strsave(
3311 iptr->isn_arg.string == NULL
3312 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 }
3314 break;
3315
Bram Moolenaar06b77222022-01-25 15:51:56 +00003316 case ISN_AUTOLOAD:
3317 {
3318 char_u *name = iptr->isn_arg.string;
3319
3320 (void)script_autoload(name, FALSE);
3321 if (find_func(name, TRUE))
3322 {
3323 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3324 goto theend;
3325 tv = STACK_TV_BOT(0);
3326 tv->v_lock = 0;
3327 ++ectx->ec_stack.ga_len;
3328 tv->v_type = VAR_FUNC;
3329 tv->vval.v_string = vim_strsave(name);
3330 }
3331 else
3332 {
3333 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3334
3335 if (res == NOTDONE)
3336 goto theend;
3337 if (res == FAIL)
3338 goto on_error;
3339 }
3340 }
3341 break;
3342
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003343 case ISN_UNLET:
3344 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3345 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003346 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003347 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003348 case ISN_UNLETENV:
3349 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3350 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003351
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003352 case ISN_LOCKUNLOCK:
3353 {
3354 typval_T *lval_root_save = lval_root;
3355 int res;
3356
3357 // Stack has the local variable, argument the whole :lock
3358 // or :unlock command, like ISN_EXEC.
3359 --ectx->ec_stack.ga_len;
3360 lval_root = STACK_TV_BOT(0);
3361 res = exec_command(iptr);
3362 clear_tv(lval_root);
3363 lval_root = lval_root_save;
3364 if (res == FAIL)
3365 goto on_error;
3366 }
3367 break;
3368
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003369 case ISN_LOCKCONST:
3370 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3371 break;
3372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 // create a list from items on the stack; uses a single allocation
3374 // for the list header and the items
3375 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003376 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003377 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 break;
3379
3380 // create a dict from items on the stack
3381 case ISN_NEWDICT:
3382 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003383 int count = iptr->isn_arg.number;
3384 dict_T *dict = dict_alloc();
3385 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003386 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003387 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003389 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003390 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 for (idx = 0; idx < count; ++idx)
3392 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003393 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003395 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003396 key = tv->vval.v_string == NULL
3397 ? (char_u *)"" : tv->vval.v_string;
3398 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003399 if (item != NULL)
3400 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003401 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar74409f62022-01-01 15:58:22 +00003402 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003403 dict_unref(dict);
3404 goto on_error;
3405 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003406 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003408 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003409 {
3410 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003411 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3414 item->di_tv.v_lock = 0;
3415 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003416 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003417 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003418 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003419 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 }
3422
3423 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003424 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003425 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003426 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003428 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 tv = STACK_TV_BOT(-1);
3430 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003431 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 tv->vval.v_dict = dict;
3433 ++dict->dv_refcount;
3434 }
3435 break;
3436
3437 // call a :def function
3438 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003439 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003440 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3441 NULL,
3442 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003443 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003444 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 break;
3446
3447 // call a builtin function
3448 case ISN_BCALL:
3449 SOURCING_LNUM = iptr->isn_lnum;
3450 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3451 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003452 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003453 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 break;
3455
3456 // call a funcref or partial
3457 case ISN_PCALL:
3458 {
3459 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3460 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003461 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462
3463 SOURCING_LNUM = iptr->isn_lnum;
3464 if (pfunc->cpf_top)
3465 {
3466 // funcref is above the arguments
3467 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3468 }
3469 else
3470 {
3471 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003472 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003473 partial_tv = *STACK_TV_BOT(0);
3474 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003476 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003477 if (tv == &partial_tv)
3478 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003480 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 }
3482 break;
3483
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003484 case ISN_PCALL_END:
3485 // PCALL finished, arguments have been consumed and replaced by
3486 // the return value. Now clear the funcref from the stack,
3487 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003488 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003489 clear_tv(STACK_TV_BOT(-1));
3490 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3491 break;
3492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 // call a user defined function or funcref/partial
3494 case ISN_UCALL:
3495 {
3496 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3497
3498 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003499 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003500 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003501 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 }
3503 break;
3504
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003505 // return from a :def function call without a value
3506 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003507 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003508 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003509 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003510 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003511 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003512 tv->vval.v_number = 0;
3513 tv->v_lock = 0;
3514 // FALLTHROUGH
3515
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003516 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 case ISN_RETURN:
3518 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003519 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003520 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003521
3522 if (trystack->ga_len > 0)
3523 trycmd = ((trycmd_T *)trystack->ga_data)
3524 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003525 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003526 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003528 // jump to ":finally" or ":endtry"
3529 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003530 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003531 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003532 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 trycmd->tcd_return = TRUE;
3534 }
3535 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003536 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 }
3538 break;
3539
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003540 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 case ISN_FUNCREF:
3542 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003543 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003544 ufunc_T *ufunc;
3545 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003548 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003549 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003550 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003551 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003552 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003553 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003554 if (funcref->fr_func_name == NULL)
3555 {
3556 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3557 + funcref->fr_dfunc_idx;
3558
3559 ufunc = pt_dfunc->df_ufunc;
3560 }
3561 else
3562 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003563 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003564 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003565 if (ufunc == NULL)
3566 {
3567 SOURCING_LNUM = iptr->isn_lnum;
3568 emsg(_(e_function_reference_invalid));
3569 goto theend;
3570 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003571 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003572 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003574 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 tv->vval.v_partial = pt;
3576 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003577 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 }
3579 break;
3580
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003581 // Create a global function from a lambda.
3582 case ISN_NEWFUNC:
3583 {
3584 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3585
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003586 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003587 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003588 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003589 }
3590 break;
3591
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003592 // List functions
3593 case ISN_DEF:
3594 if (iptr->isn_arg.string == NULL)
3595 list_functions(NULL);
3596 else
3597 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003598 exarg_T ea;
3599 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003600
3601 CLEAR_FIELD(ea);
3602 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003603 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3604 define_function(&ea, NULL, &lines_to_free);
3605 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003606 }
3607 break;
3608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 // jump if a condition is met
3610 case ISN_JUMP:
3611 {
3612 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003613 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 int jump = TRUE;
3615
3616 if (when != JUMP_ALWAYS)
3617 {
3618 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003619 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003620 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003621 || when == JUMP_IF_COND_TRUE)
3622 {
3623 SOURCING_LNUM = iptr->isn_lnum;
3624 jump = tv_get_bool_chk(tv, &error);
3625 if (error)
3626 goto on_error;
3627 }
3628 else
3629 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003631 || when == JUMP_AND_KEEP_IF_FALSE
3632 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003634 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 {
3636 // drop the value from the stack
3637 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003638 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 }
3640 }
3641 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003642 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 }
3644 break;
3645
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003646 // Jump if an argument with a default value was already set and not
3647 // v:none.
3648 case ISN_JUMP_IF_ARG_SET:
3649 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3650 if (tv->v_type != VAR_UNKNOWN
3651 && !(tv->v_type == VAR_SPECIAL
3652 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003653 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003654 break;
3655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 // top of a for loop
3657 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003658 if (execute_for(iptr, ectx) == FAIL)
3659 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 break;
3661
3662 // start of ":try" block
3663 case ISN_TRY:
3664 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003665 trycmd_T *trycmd = NULL;
3666
Bram Moolenaar35578162021-08-02 19:10:38 +02003667 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003668 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003669 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3670 + ectx->ec_trystack.ga_len;
3671 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003673 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003674 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3675 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003676 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003677 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003678 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003679 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003680 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003681 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 }
3683 break;
3684
3685 case ISN_PUSHEXC:
3686 if (current_exception == NULL)
3687 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003688 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003690 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003692 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003693 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003695 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003697 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 tv->vval.v_string = vim_strsave(
3699 (char_u *)current_exception->value);
3700 break;
3701
3702 case ISN_CATCH:
3703 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003704 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705
Bram Moolenaar4c137212021-04-19 16:48:48 +02003706 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 if (trystack->ga_len > 0)
3708 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003709 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 + trystack->ga_len - 1;
3711 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003712 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 }
3714 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003715 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 catch_exception(current_exception);
3717 }
3718 break;
3719
Bram Moolenaarc150c092021-02-13 15:02:46 +01003720 case ISN_TRYCONT:
3721 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003722 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003723 trycont_T *trycont = &iptr->isn_arg.trycont;
3724 int i;
3725 trycmd_T *trycmd;
3726 int iidx = trycont->tct_where;
3727
3728 if (trystack->ga_len < trycont->tct_levels)
3729 {
3730 siemsg("TRYCONT: expected %d levels, found %d",
3731 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003732 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003733 }
3734 // Make :endtry jump to any outer try block and the last
3735 // :endtry inside the loop to the loop start.
3736 for (i = trycont->tct_levels; i > 0; --i)
3737 {
3738 trycmd = ((trycmd_T *)trystack->ga_data)
3739 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003740 // Add one to tcd_cont to be able to jump to
3741 // instruction with index zero.
3742 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003743 iidx = trycmd->tcd_finally_idx == 0
3744 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003745 }
3746 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003747 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003748 }
3749 break;
3750
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003751 case ISN_FINALLY:
3752 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003753 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003754 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3755 + trystack->ga_len - 1;
3756
3757 // Reset the index to avoid a return statement jumps here
3758 // again.
3759 trycmd->tcd_finally_idx = 0;
3760 break;
3761 }
3762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 // end of ":try" block
3764 case ISN_ENDTRY:
3765 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003766 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767
3768 if (trystack->ga_len > 0)
3769 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003770 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 --trystack->ga_len;
3773 --trylevel;
3774 trycmd = ((trycmd_T *)trystack->ga_data)
3775 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003776 if (trycmd->tcd_did_throw)
3777 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003778 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 {
3780 // discard the exception
3781 if (caught_stack == current_exception)
3782 caught_stack = caught_stack->caught;
3783 discard_current_exception();
3784 }
3785
3786 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003787 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003788
Bram Moolenaar4c137212021-04-19 16:48:48 +02003789 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003790 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003791 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003792 clear_tv(STACK_TV_BOT(0));
3793 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003794 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003795 // handling :continue: jump to outer try block or
3796 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003797 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
3799 }
3800 break;
3801
3802 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003803 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003804 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003805
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003806 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3807 {
3808 // throwing an exception while using "silent!" causes
3809 // the function to abort but not display an error.
3810 tv = STACK_TV_BOT(-1);
3811 clear_tv(tv);
3812 tv->v_type = VAR_NUMBER;
3813 tv->vval.v_number = 0;
3814 goto done;
3815 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003816 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003817 tv = STACK_TV_BOT(0);
3818 if (tv->vval.v_string == NULL
3819 || *skipwhite(tv->vval.v_string) == NUL)
3820 {
3821 vim_free(tv->vval.v_string);
3822 SOURCING_LNUM = iptr->isn_lnum;
3823 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003824 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003825 }
3826
3827 // Inside a "catch" we need to first discard the caught
3828 // exception.
3829 if (trystack->ga_len > 0)
3830 {
3831 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3832 + trystack->ga_len - 1;
3833 if (trycmd->tcd_caught && current_exception != NULL)
3834 {
3835 // discard the exception
3836 if (caught_stack == current_exception)
3837 caught_stack = caught_stack->caught;
3838 discard_current_exception();
3839 trycmd->tcd_caught = FALSE;
3840 }
3841 }
3842
Bram Moolenaar90a57162022-02-12 14:23:17 +00003843 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003844 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3845 == FAIL)
3846 {
3847 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003848 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003849 }
3850 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 break;
3853
3854 // compare with special values
3855 case ISN_COMPAREBOOL:
3856 case ISN_COMPARESPECIAL:
3857 {
3858 typval_T *tv1 = STACK_TV_BOT(-2);
3859 typval_T *tv2 = STACK_TV_BOT(-1);
3860 varnumber_T arg1 = tv1->vval.v_number;
3861 varnumber_T arg2 = tv2->vval.v_number;
3862 int res;
3863
3864 switch (iptr->isn_arg.op.op_type)
3865 {
3866 case EXPR_EQUAL: res = arg1 == arg2; break;
3867 case EXPR_NEQUAL: res = arg1 != arg2; break;
3868 default: res = 0; break;
3869 }
3870
Bram Moolenaar4c137212021-04-19 16:48:48 +02003871 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 tv1->v_type = VAR_BOOL;
3873 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3874 }
3875 break;
3876
3877 // Operation with two number arguments
3878 case ISN_OPNR:
3879 case ISN_COMPARENR:
3880 {
3881 typval_T *tv1 = STACK_TV_BOT(-2);
3882 typval_T *tv2 = STACK_TV_BOT(-1);
3883 varnumber_T arg1 = tv1->vval.v_number;
3884 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003885 varnumber_T res = 0;
3886 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887
3888 switch (iptr->isn_arg.op.op_type)
3889 {
3890 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003891 case EXPR_DIV: if (arg2 == 0)
3892 div_zero = TRUE;
3893 else
3894 res = arg1 / arg2;
3895 break;
3896 case EXPR_REM: if (arg2 == 0)
3897 div_zero = TRUE;
3898 else
3899 res = arg1 % arg2;
3900 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 case EXPR_SUB: res = arg1 - arg2; break;
3902 case EXPR_ADD: res = arg1 + arg2; break;
3903
3904 case EXPR_EQUAL: res = arg1 == arg2; break;
3905 case EXPR_NEQUAL: res = arg1 != arg2; break;
3906 case EXPR_GREATER: res = arg1 > arg2; break;
3907 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3908 case EXPR_SMALLER: res = arg1 < arg2; break;
3909 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003910 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 }
3912
Bram Moolenaar4c137212021-04-19 16:48:48 +02003913 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 if (iptr->isn_type == ISN_COMPARENR)
3915 {
3916 tv1->v_type = VAR_BOOL;
3917 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3918 }
3919 else
3920 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003921 if (div_zero)
3922 {
3923 SOURCING_LNUM = iptr->isn_lnum;
3924 emsg(_(e_divide_by_zero));
3925 goto on_error;
3926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 }
3928 break;
3929
3930 // Computation with two float arguments
3931 case ISN_OPFLOAT:
3932 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003933#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 {
3935 typval_T *tv1 = STACK_TV_BOT(-2);
3936 typval_T *tv2 = STACK_TV_BOT(-1);
3937 float_T arg1 = tv1->vval.v_float;
3938 float_T arg2 = tv2->vval.v_float;
3939 float_T res = 0;
3940 int cmp = FALSE;
3941
3942 switch (iptr->isn_arg.op.op_type)
3943 {
3944 case EXPR_MULT: res = arg1 * arg2; break;
3945 case EXPR_DIV: res = arg1 / arg2; break;
3946 case EXPR_SUB: res = arg1 - arg2; break;
3947 case EXPR_ADD: res = arg1 + arg2; break;
3948
3949 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3950 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3951 case EXPR_GREATER: cmp = arg1 > arg2; break;
3952 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3953 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3954 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3955 default: cmp = 0; break;
3956 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003957 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 if (iptr->isn_type == ISN_COMPAREFLOAT)
3959 {
3960 tv1->v_type = VAR_BOOL;
3961 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3962 }
3963 else
3964 tv1->vval.v_float = res;
3965 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003966#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 break;
3968
3969 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00003970 case ISN_COMPAREDICT:
3971 case ISN_COMPAREFUNC:
3972 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 case ISN_COMPAREBLOB:
3974 {
3975 typval_T *tv1 = STACK_TV_BOT(-2);
3976 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00003977 exprtype_T exprtype = iptr->isn_arg.op.op_type;
3978 int ic = iptr->isn_arg.op.op_ic;
3979 int res = FALSE;
3980 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981
Bram Moolenaar265f8112021-12-19 12:33:05 +00003982 SOURCING_LNUM = iptr->isn_lnum;
3983 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00003985 status = typval_compare_list(tv1, tv2,
3986 exprtype, ic, &res);
3987 }
3988 else if (iptr->isn_type == ISN_COMPAREDICT)
3989 {
3990 status = typval_compare_dict(tv1, tv2,
3991 exprtype, ic, &res);
3992 }
3993 else if (iptr->isn_type == ISN_COMPAREFUNC)
3994 {
3995 status = typval_compare_func(tv1, tv2,
3996 exprtype, ic, &res);
3997 }
3998 else if (iptr->isn_type == ISN_COMPARESTRING)
3999 {
4000 status = typval_compare_string(tv1, tv2,
4001 exprtype, ic, &res);
4002 }
4003 else
4004 {
4005 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004007 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 clear_tv(tv1);
4009 clear_tv(tv2);
4010 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004011 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4012 if (status == FAIL)
4013 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 }
4015 break;
4016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 case ISN_COMPAREANY:
4018 {
4019 typval_T *tv1 = STACK_TV_BOT(-2);
4020 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004021 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004023 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024
Bram Moolenaareb26f432020-09-14 16:50:05 +02004025 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004026 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004028 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004029 if (status == FAIL)
4030 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 }
4032 break;
4033
4034 case ISN_ADDLIST:
4035 case ISN_ADDBLOB:
4036 {
4037 typval_T *tv1 = STACK_TV_BOT(-2);
4038 typval_T *tv2 = STACK_TV_BOT(-1);
4039
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004040 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004042 {
4043 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4044 && tv1->vval.v_list != NULL)
4045 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4046 NULL);
4047 else
4048 eval_addlist(tv1, tv2);
4049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 else
4051 eval_addblob(tv1, tv2);
4052 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004053 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 }
4055 break;
4056
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004057 case ISN_LISTAPPEND:
4058 {
4059 typval_T *tv1 = STACK_TV_BOT(-2);
4060 typval_T *tv2 = STACK_TV_BOT(-1);
4061 list_T *l = tv1->vval.v_list;
4062
4063 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004064 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004065 if (l == NULL)
4066 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004067 emsg(_(e_cannot_add_to_null_list));
4068 goto on_error;
4069 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004070 if (value_check_lock(l->lv_lock, NULL, FALSE))
4071 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004072 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004073 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004074 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004075 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004076 }
4077 break;
4078
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004079 case ISN_BLOBAPPEND:
4080 {
4081 typval_T *tv1 = STACK_TV_BOT(-2);
4082 typval_T *tv2 = STACK_TV_BOT(-1);
4083 blob_T *b = tv1->vval.v_blob;
4084 int error = FALSE;
4085 varnumber_T n;
4086
4087 // add a number to a blob
4088 if (b == NULL)
4089 {
4090 SOURCING_LNUM = iptr->isn_lnum;
4091 emsg(_(e_cannot_add_to_null_blob));
4092 goto on_error;
4093 }
4094 n = tv_get_number_chk(tv2, &error);
4095 if (error)
4096 goto on_error;
4097 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004098 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004099 }
4100 break;
4101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 // Computation with two arguments of unknown type
4103 case ISN_OPANY:
4104 {
4105 typval_T *tv1 = STACK_TV_BOT(-2);
4106 typval_T *tv2 = STACK_TV_BOT(-1);
4107 varnumber_T n1, n2;
4108#ifdef FEAT_FLOAT
4109 float_T f1 = 0, f2 = 0;
4110#endif
4111 int error = FALSE;
4112
4113 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4114 {
4115 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4116 {
4117 eval_addlist(tv1, tv2);
4118 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004119 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 break;
4121 }
4122 else if (tv1->v_type == VAR_BLOB
4123 && tv2->v_type == VAR_BLOB)
4124 {
4125 eval_addblob(tv1, tv2);
4126 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004127 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 break;
4129 }
4130 }
4131#ifdef FEAT_FLOAT
4132 if (tv1->v_type == VAR_FLOAT)
4133 {
4134 f1 = tv1->vval.v_float;
4135 n1 = 0;
4136 }
4137 else
4138#endif
4139 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004140 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 n1 = tv_get_number_chk(tv1, &error);
4142 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004143 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144#ifdef FEAT_FLOAT
4145 if (tv2->v_type == VAR_FLOAT)
4146 f1 = n1;
4147#endif
4148 }
4149#ifdef FEAT_FLOAT
4150 if (tv2->v_type == VAR_FLOAT)
4151 {
4152 f2 = tv2->vval.v_float;
4153 n2 = 0;
4154 }
4155 else
4156#endif
4157 {
4158 n2 = tv_get_number_chk(tv2, &error);
4159 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004160 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161#ifdef FEAT_FLOAT
4162 if (tv1->v_type == VAR_FLOAT)
4163 f2 = n2;
4164#endif
4165 }
4166#ifdef FEAT_FLOAT
4167 // if there is a float on either side the result is a float
4168 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4169 {
4170 switch (iptr->isn_arg.op.op_type)
4171 {
4172 case EXPR_MULT: f1 = f1 * f2; break;
4173 case EXPR_DIV: f1 = f1 / f2; break;
4174 case EXPR_SUB: f1 = f1 - f2; break;
4175 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004176 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004177 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004178 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 }
4180 clear_tv(tv1);
4181 clear_tv(tv2);
4182 tv1->v_type = VAR_FLOAT;
4183 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004184 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 }
4186 else
4187#endif
4188 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004189 int failed = FALSE;
4190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 switch (iptr->isn_arg.op.op_type)
4192 {
4193 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004194 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4195 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004196 goto on_error;
4197 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 case EXPR_SUB: n1 = n1 - n2; break;
4199 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004200 default: n1 = num_modulus(n1, n2, &failed);
4201 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004202 goto on_error;
4203 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 }
4205 clear_tv(tv1);
4206 clear_tv(tv2);
4207 tv1->v_type = VAR_NUMBER;
4208 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004209 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 }
4211 }
4212 break;
4213
4214 case ISN_CONCAT:
4215 {
4216 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4217 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4218 char_u *res;
4219
4220 res = concat_str(str1, str2);
4221 clear_tv(STACK_TV_BOT(-2));
4222 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004223 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 STACK_TV_BOT(-1)->vval.v_string = res;
4225 }
4226 break;
4227
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004228 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004229 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004230 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004231 int is_slice = iptr->isn_type == ISN_STRSLICE;
4232 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004233 char_u *res;
4234
4235 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004236 // string slice: string is at stack-3, first index at
4237 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004238 if (is_slice)
4239 {
4240 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004241 n1 = tv->vval.v_number;
4242 }
4243
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004244 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004245 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004246
Bram Moolenaar4c137212021-04-19 16:48:48 +02004247 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004248 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004249 if (is_slice)
4250 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004251 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004252 else
4253 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004254 // single character (including composing characters).
4255 // If the index is too big or negative the result is
4256 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004257 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004258 vim_free(tv->vval.v_string);
4259 tv->vval.v_string = res;
4260 }
4261 break;
4262
4263 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004264 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004265 case ISN_BLOBINDEX:
4266 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004268 int is_slice = iptr->isn_type == ISN_LISTSLICE
4269 || iptr->isn_type == ISN_BLOBSLICE;
4270 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4271 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004272 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004273 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274
4275 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004276 // list slice: list is at stack-3, indexes at stack-2 and
4277 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004278 // Same for blob.
4279 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280
4281 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004282 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004284
4285 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 {
Bram Moolenaared591872020-08-15 22:14:53 +02004287 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004288 n1 = tv->vval.v_number;
4289 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 }
Bram Moolenaared591872020-08-15 22:14:53 +02004291
Bram Moolenaar4c137212021-04-19 16:48:48 +02004292 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004293 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004294 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004295 if (is_blob)
4296 {
4297 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4298 n1, n2, FALSE, tv) == FAIL)
4299 goto on_error;
4300 }
4301 else
4302 {
4303 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4304 n1, n2, FALSE, tv, TRUE) == FAIL)
4305 goto on_error;
4306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 }
4308 break;
4309
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004310 case ISN_ANYINDEX:
4311 case ISN_ANYSLICE:
4312 {
4313 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4314 typval_T *var1, *var2;
4315 int res;
4316
4317 // index: composite is at stack-2, index at stack-1
4318 // slice: composite is at stack-3, indexes at stack-2 and
4319 // stack-1
4320 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004321 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004322 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4323 goto on_error;
4324 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4325 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004326 res = eval_index_inner(tv, is_slice, var1, var2,
4327 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004328 clear_tv(var1);
4329 if (is_slice)
4330 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004331 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004332 if (res == FAIL)
4333 goto on_error;
4334 }
4335 break;
4336
Bram Moolenaar9af78762020-06-16 11:34:42 +02004337 case ISN_SLICE:
4338 {
4339 list_T *list;
4340 int count = iptr->isn_arg.number;
4341
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004342 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004343 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004344 list = tv->vval.v_list;
4345
4346 // no error for short list, expect it to be checked earlier
4347 if (list != NULL && list->lv_len >= count)
4348 {
4349 list_T *newlist = list_slice(list,
4350 count, list->lv_len - 1);
4351
4352 if (newlist != NULL)
4353 {
4354 list_unref(list);
4355 tv->vval.v_list = newlist;
4356 ++newlist->lv_refcount;
4357 }
4358 }
4359 }
4360 break;
4361
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004362 case ISN_GETITEM:
4363 {
4364 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004365 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004366
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004367 // Get list item: list is at stack-1, push item.
4368 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004369 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4370 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004371
Bram Moolenaar35578162021-08-02 19:10:38 +02004372 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004373 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004374 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004375 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004376
4377 // Useful when used in unpack assignment. Reset at
4378 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004379 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004380 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004381 }
4382 break;
4383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 case ISN_MEMBER:
4385 {
4386 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004387 char_u *key;
4388 dictitem_T *di;
4389
4390 // dict member: dict is at stack-2, key at stack-1
4391 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004392 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004393 dict = tv->vval.v_dict;
4394
4395 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004396 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004397 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004398 if (key == NULL)
4399 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004400
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004401 if ((di = dict_find(dict, key, -1)) == NULL)
4402 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004403 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004404 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004405
4406 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004407 // stack contents makes sense and the dict stack is
4408 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004409 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004410 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004411 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004412 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004413 tv->v_type = VAR_NUMBER;
4414 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004415 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004416 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004417 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004418 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004419 // Put the dict used on the dict stack, it might be used by
4420 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004421 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004422 if (dict_stack_save(tv) == FAIL)
4423 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004424 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004425 }
4426 break;
4427
4428 // dict member with string key
4429 case ISN_STRINGMEMBER:
4430 {
4431 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 dictitem_T *di;
4433
4434 tv = STACK_TV_BOT(-1);
4435 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4436 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004437 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004438 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004439 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 }
4441 dict = tv->vval.v_dict;
4442
4443 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4444 == NULL)
4445 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004446 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004447 semsg(_(e_key_not_present_in_dictionary),
4448 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004449 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004451 // Put the dict used on the dict stack, it might be used by
4452 // a dict function later.
4453 if (dict_stack_save(tv) == FAIL)
4454 goto on_fatal_error;
4455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004457 }
4458 break;
4459
4460 case ISN_CLEARDICT:
4461 dict_stack_drop();
4462 break;
4463
4464 case ISN_USEDICT:
4465 {
4466 typval_T *dict_tv = dict_stack_get_tv();
4467
4468 // Turn "dict.Func" into a partial for "Func" bound to
4469 // "dict". Don't do this when "Func" is already a partial
4470 // that was bound explicitly (pt_auto is FALSE).
4471 tv = STACK_TV_BOT(-1);
4472 if (dict_tv != NULL
4473 && dict_tv->v_type == VAR_DICT
4474 && dict_tv->vval.v_dict != NULL
4475 && (tv->v_type == VAR_FUNC
4476 || (tv->v_type == VAR_PARTIAL
4477 && (tv->vval.v_partial->pt_auto
4478 || tv->vval.v_partial->pt_dict == NULL))))
4479 dict_tv->vval.v_dict =
4480 make_partial(dict_tv->vval.v_dict, tv);
4481 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 }
4483 break;
4484
4485 case ISN_NEGATENR:
4486 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004487 if (tv->v_type != VAR_NUMBER
4488#ifdef FEAT_FLOAT
4489 && tv->v_type != VAR_FLOAT
4490#endif
4491 )
4492 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004493 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004494 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004495 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004496 }
4497#ifdef FEAT_FLOAT
4498 if (tv->v_type == VAR_FLOAT)
4499 tv->vval.v_float = -tv->vval.v_float;
4500 else
4501#endif
4502 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 break;
4504
4505 case ISN_CHECKNR:
4506 {
4507 int error = FALSE;
4508
4509 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004510 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004512 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 (void)tv_get_number_chk(tv, &error);
4514 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004515 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 }
4517 break;
4518
4519 case ISN_CHECKTYPE:
4520 {
4521 checktype_T *ct = &iptr->isn_arg.type;
4522
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004523 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004524 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004525 if (!ectx->ec_where.wt_variable)
4526 ectx->ec_where.wt_index = ct->ct_arg_idx;
4527 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4528 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004529 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004530 if (!ectx->ec_where.wt_variable)
4531 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004532
4533 // number 0 is FALSE, number 1 is TRUE
4534 if (tv->v_type == VAR_NUMBER
4535 && ct->ct_type->tt_type == VAR_BOOL
4536 && (tv->vval.v_number == 0
4537 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004539 tv->v_type = VAR_BOOL;
4540 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004541 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 }
4543 }
4544 break;
4545
Bram Moolenaar9af78762020-06-16 11:34:42 +02004546 case ISN_CHECKLEN:
4547 {
4548 int min_len = iptr->isn_arg.checklen.cl_min_len;
4549 list_T *list = NULL;
4550
4551 tv = STACK_TV_BOT(-1);
4552 if (tv->v_type == VAR_LIST)
4553 list = tv->vval.v_list;
4554 if (list == NULL || list->lv_len < min_len
4555 || (list->lv_len > min_len
4556 && !iptr->isn_arg.checklen.cl_more_OK))
4557 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004558 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004559 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004560 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004561 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004562 }
4563 }
4564 break;
4565
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004566 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004567 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004568 break;
4569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004571 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 {
4573 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004574 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004576 if (iptr->isn_type == ISN_2BOOL)
4577 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004578 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004579 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004580 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004581 n = !n;
4582 }
4583 else
4584 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004585 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004586 SOURCING_LNUM = iptr->isn_lnum;
4587 n = tv_get_bool_chk(tv, &error);
4588 if (error)
4589 goto on_error;
4590 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 clear_tv(tv);
4592 tv->v_type = VAR_BOOL;
4593 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4594 }
4595 break;
4596
4597 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004598 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004599 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004600 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4601 iptr->isn_type == ISN_2STRING_ANY,
4602 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004603 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 break;
4605
Bram Moolenaar08597872020-12-10 19:43:40 +01004606 case ISN_RANGE:
4607 {
4608 exarg_T ea;
4609 char *errormsg;
4610
Bram Moolenaarece0b872021-01-08 20:40:45 +01004611 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004612 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004613 ea.addr_type = ADDR_LINES;
4614 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004615 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004616 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004617 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004618
Bram Moolenaar35578162021-08-02 19:10:38 +02004619 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004620 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004621 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004622 tv = STACK_TV_BOT(-1);
4623 tv->v_type = VAR_NUMBER;
4624 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004625 if (ea.addr_count == 0)
4626 tv->vval.v_number = curwin->w_cursor.lnum;
4627 else
4628 tv->vval.v_number = ea.line2;
4629 }
4630 break;
4631
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004632 case ISN_PUT:
4633 {
4634 int regname = iptr->isn_arg.put.put_regname;
4635 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4636 char_u *expr = NULL;
4637 int dir = FORWARD;
4638
Bram Moolenaar08597872020-12-10 19:43:40 +01004639 if (lnum < -2)
4640 {
4641 // line number was put on the stack by ISN_RANGE
4642 tv = STACK_TV_BOT(-1);
4643 curwin->w_cursor.lnum = tv->vval.v_number;
4644 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4645 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004646 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004647 }
4648 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004649 // :put! above cursor
4650 dir = BACKWARD;
4651 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004652 {
4653 curwin->w_cursor.lnum = lnum;
4654 if (lnum == 0)
4655 // check_cursor() below will move to line 1
4656 dir = BACKWARD;
4657 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004658
4659 if (regname == '=')
4660 {
4661 tv = STACK_TV_BOT(-1);
4662 if (tv->v_type == VAR_STRING)
4663 expr = tv->vval.v_string;
4664 else
4665 {
4666 expr = typval2string(tv, TRUE); // allocates value
4667 clear_tv(tv);
4668 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004669 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004670 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004671 check_cursor();
4672 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4673 vim_free(expr);
4674 }
4675 break;
4676
Bram Moolenaar02194d22020-10-24 23:08:38 +02004677 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004678 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4679 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4680 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4681 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004682 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4683 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004684 break;
4685
Bram Moolenaar02194d22020-10-24 23:08:38 +02004686 case ISN_CMDMOD_REV:
4687 // filter regprog is owned by the instruction, don't free it
4688 cmdmod.cmod_filter_regmatch.regprog = NULL;
4689 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004690 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4691 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004692 break;
4693
Bram Moolenaar792f7862020-11-23 08:31:18 +01004694 case ISN_UNPACK:
4695 {
4696 int count = iptr->isn_arg.unpack.unp_count;
4697 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4698 list_T *l;
4699 listitem_T *li;
4700 int i;
4701
4702 // Check there is a valid list to unpack.
4703 tv = STACK_TV_BOT(-1);
4704 if (tv->v_type != VAR_LIST)
4705 {
4706 SOURCING_LNUM = iptr->isn_lnum;
4707 emsg(_(e_for_argument_must_be_sequence_of_lists));
4708 goto on_error;
4709 }
4710 l = tv->vval.v_list;
4711 if (l == NULL
4712 || l->lv_len < (semicolon ? count - 1 : count))
4713 {
4714 SOURCING_LNUM = iptr->isn_lnum;
4715 emsg(_(e_list_value_does_not_have_enough_items));
4716 goto on_error;
4717 }
4718 else if (!semicolon && l->lv_len > count)
4719 {
4720 SOURCING_LNUM = iptr->isn_lnum;
4721 emsg(_(e_list_value_has_more_items_than_targets));
4722 goto on_error;
4723 }
4724
4725 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004726 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004727 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004728 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004729
4730 // Variable after semicolon gets a list with the remaining
4731 // items.
4732 if (semicolon)
4733 {
4734 list_T *rem_list =
4735 list_alloc_with_items(l->lv_len - count + 1);
4736
4737 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004738 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004739 tv = STACK_TV_BOT(-count);
4740 tv->vval.v_list = rem_list;
4741 ++rem_list->lv_refcount;
4742 tv->v_lock = 0;
4743 li = l->lv_first;
4744 for (i = 0; i < count - 1; ++i)
4745 li = li->li_next;
4746 for (i = 0; li != NULL; ++i)
4747 {
4748 list_set_item(rem_list, i, &li->li_tv);
4749 li = li->li_next;
4750 }
4751 --count;
4752 }
4753
4754 // Produce the values in reverse order, first item last.
4755 li = l->lv_first;
4756 for (i = 0; i < count; ++i)
4757 {
4758 tv = STACK_TV_BOT(-i - 1);
4759 copy_tv(&li->li_tv, tv);
4760 li = li->li_next;
4761 }
4762
4763 list_unref(l);
4764 }
4765 break;
4766
Bram Moolenaarb2049902021-01-24 12:53:53 +01004767 case ISN_PROF_START:
4768 case ISN_PROF_END:
4769 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004770#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004771 funccall_T cookie;
4772 ufunc_T *cur_ufunc =
4773 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004774 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004775
4776 cookie.func = cur_ufunc;
4777 if (iptr->isn_type == ISN_PROF_START)
4778 {
4779 func_line_start(&cookie, iptr->isn_lnum);
4780 // if we get here the instruction is executed
4781 func_line_exec(&cookie);
4782 }
4783 else
4784 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004785#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004786 }
4787 break;
4788
Bram Moolenaare99d4222021-06-13 14:01:26 +02004789 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004790 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004791 break;
4792
Bram Moolenaar389df252020-07-09 21:20:47 +02004793 case ISN_SHUFFLE:
4794 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004795 typval_T tmp_tv;
4796 int item = iptr->isn_arg.shuffle.shfl_item;
4797 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004798
4799 tmp_tv = *STACK_TV_BOT(-item);
4800 for ( ; up > 0 && item > 1; --up)
4801 {
4802 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4803 --item;
4804 }
4805 *STACK_TV_BOT(-item) = tmp_tv;
4806 }
4807 break;
4808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004810 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004812 ectx->ec_where.wt_index = 0;
4813 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 break;
4815 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004816 continue;
4817
Bram Moolenaard032f342020-07-18 18:13:02 +02004818func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004819 // Restore previous function. If the frame pointer is where we started
4820 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004821 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004822 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004823
Bram Moolenaar4c137212021-04-19 16:48:48 +02004824 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004825 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004826 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004827 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004828
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004829on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004830 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004831 // If "emsg_silent" is set then ignore the error, unless it was set
4832 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004833 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004834 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004835 {
4836 // If a sequence of instructions causes an error while ":silent!"
4837 // was used, restore the stack length and jump ahead to restoring
4838 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004839 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004840 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004841 while (ectx->ec_stack.ga_len
4842 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004843 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004844 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004845 clear_tv(STACK_TV_BOT(0));
4846 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004847 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4848 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004849 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004850 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004851 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004852on_fatal_error:
4853 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004854 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004855 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004856 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 }
4858
4859done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004860 ret = OK;
4861theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004862 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004863 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4864 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004865}
4866
4867/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004868 * Execute the instructions from a VAR_INSTR typeval and put the result in
4869 * "rettv".
4870 * Return OK or FAIL.
4871 */
4872 int
4873exe_typval_instr(typval_T *tv, typval_T *rettv)
4874{
4875 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4876 isn_T *save_instr = ectx->ec_instr;
4877 int save_iidx = ectx->ec_iidx;
4878 int res;
4879
4880 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4881 res = exec_instructions(ectx);
4882 if (res == OK)
4883 {
4884 *rettv = *STACK_TV_BOT(-1);
4885 --ectx->ec_stack.ga_len;
4886 }
4887
4888 ectx->ec_instr = save_instr;
4889 ectx->ec_iidx = save_iidx;
4890
4891 return res;
4892}
4893
4894/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004895 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4896 * "substitute_instr".
4897 */
4898 char_u *
4899exe_substitute_instr(void)
4900{
4901 ectx_T *ectx = substitute_instr->subs_ectx;
4902 isn_T *save_instr = ectx->ec_instr;
4903 int save_iidx = ectx->ec_iidx;
4904 char_u *res;
4905
4906 ectx->ec_instr = substitute_instr->subs_instr;
4907 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004908 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004909 typval_T *tv = STACK_TV_BOT(-1);
4910
Bram Moolenaar27523602021-06-05 21:36:19 +02004911 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004912 --ectx->ec_stack.ga_len;
4913 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004914 }
4915 else
4916 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004917 substitute_instr->subs_status = FAIL;
4918 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920
Bram Moolenaar4c137212021-04-19 16:48:48 +02004921 ectx->ec_instr = save_instr;
4922 ectx->ec_iidx = save_iidx;
4923
4924 return res;
4925}
4926
4927/*
4928 * Call a "def" function from old Vim script.
4929 * Return OK or FAIL.
4930 */
4931 int
4932call_def_function(
4933 ufunc_T *ufunc,
4934 int argc_arg, // nr of arguments
4935 typval_T *argv, // arguments
4936 partial_T *partial, // optional partial for context
4937 typval_T *rettv) // return value
4938{
4939 ectx_T ectx; // execution context
4940 int argc = argc_arg;
4941 typval_T *tv;
4942 int idx;
4943 int ret = FAIL;
4944 int defcount = ufunc->uf_args.ga_len - argc;
4945 sctx_T save_current_sctx = current_sctx;
4946 int did_emsg_before = did_emsg_cumul + did_emsg;
4947 int save_suppress_errthrow = suppress_errthrow;
4948 msglist_T **saved_msg_list = NULL;
4949 msglist_T *private_msg_list = NULL;
4950 int save_emsg_silent_def = emsg_silent_def;
4951 int save_did_emsg_def = did_emsg_def;
4952 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004953 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004954
4955// Get pointer to item in the stack.
4956#undef STACK_TV
4957#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4958
4959// Get pointer to item at the bottom of the stack, -1 is the bottom.
4960#undef STACK_TV_BOT
4961#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4962
4963// Get pointer to a local variable on the stack. Negative for arguments.
4964#undef STACK_TV_VAR
4965#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4966
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004967 // Update uf_has_breakpoint if needed.
4968 update_has_breakpoint(ufunc);
4969
Bram Moolenaar4c137212021-04-19 16:48:48 +02004970 if (ufunc->uf_def_status == UF_NOT_COMPILED
4971 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004972 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4973 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004974 == FAIL))
4975 {
4976 if (did_emsg_cumul + did_emsg == did_emsg_before)
4977 semsg(_(e_function_is_not_compiled_str),
4978 printable_func_name(ufunc));
4979 return FAIL;
4980 }
4981
4982 {
4983 // Check the function was really compiled.
4984 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4985 + ufunc->uf_dfunc_idx;
4986 if (INSTRUCTIONS(dfunc) == NULL)
4987 {
4988 iemsg("using call_def_function() on not compiled function");
4989 return FAIL;
4990 }
4991 }
4992
4993 // If depth of calling is getting too high, don't execute the function.
4994 orig_funcdepth = funcdepth_get();
4995 if (funcdepth_increment() == FAIL)
4996 return FAIL;
4997
4998 CLEAR_FIELD(ectx);
4999 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5000 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005001 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005002 {
5003 funcdepth_decrement();
5004 return FAIL;
5005 }
5006 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5007 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5008 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005009 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005010
5011 idx = argc - ufunc->uf_args.ga_len;
5012 if (idx > 0 && ufunc->uf_va_name == NULL)
5013 {
5014 if (idx == 1)
5015 emsg(_(e_one_argument_too_many));
5016 else
5017 semsg(_(e_nr_arguments_too_many), idx);
5018 goto failed_early;
5019 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005020 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5021 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005022 {
5023 if (idx == -1)
5024 emsg(_(e_one_argument_too_few));
5025 else
5026 semsg(_(e_nr_arguments_too_few), -idx);
5027 goto failed_early;
5028 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005029
5030 // Put arguments on the stack, but no more than what the function expects.
5031 // A lambda can be called with more arguments than it uses.
5032 for (idx = 0; idx < argc
5033 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5034 ++idx)
5035 {
5036 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5037 && argv[idx].v_type == VAR_SPECIAL
5038 && argv[idx].vval.v_number == VVAL_NONE)
5039 {
5040 // Use the default value.
5041 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5042 }
5043 else
5044 {
5045 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5046 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005047 ufunc->uf_arg_types[idx], &argv[idx],
5048 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005049 goto failed_early;
5050 copy_tv(&argv[idx], STACK_TV_BOT(0));
5051 }
5052 ++ectx.ec_stack.ga_len;
5053 }
5054
5055 // Turn varargs into a list. Empty list if no args.
5056 if (ufunc->uf_va_name != NULL)
5057 {
5058 int vararg_count = argc - ufunc->uf_args.ga_len;
5059
5060 if (vararg_count < 0)
5061 vararg_count = 0;
5062 else
5063 argc -= vararg_count;
5064 if (exe_newlist(vararg_count, &ectx) == FAIL)
5065 goto failed_early;
5066
5067 // Check the type of the list items.
5068 tv = STACK_TV_BOT(-1);
5069 if (ufunc->uf_va_type != NULL
5070 && ufunc->uf_va_type != &t_list_any
5071 && ufunc->uf_va_type->tt_member != &t_any
5072 && tv->vval.v_list != NULL)
5073 {
5074 type_T *expected = ufunc->uf_va_type->tt_member;
5075 listitem_T *li = tv->vval.v_list->lv_first;
5076
5077 for (idx = 0; idx < vararg_count; ++idx)
5078 {
5079 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005080 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005081 goto failed_early;
5082 li = li->li_next;
5083 }
5084 }
5085
5086 if (defcount > 0)
5087 // Move varargs list to below missing default arguments.
5088 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5089 --ectx.ec_stack.ga_len;
5090 }
5091
5092 // Make space for omitted arguments, will store default value below.
5093 // Any varargs list goes after them.
5094 if (defcount > 0)
5095 for (idx = 0; idx < defcount; ++idx)
5096 {
5097 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5098 ++ectx.ec_stack.ga_len;
5099 }
5100 if (ufunc->uf_va_name != NULL)
5101 ++ectx.ec_stack.ga_len;
5102
5103 // Frame pointer points to just after arguments.
5104 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5105 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5106
5107 {
5108 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5109 + ufunc->uf_dfunc_idx;
5110 ufunc_T *base_ufunc = dfunc->df_ufunc;
5111
5112 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5113 // by copy_func().
5114 if (partial != NULL || base_ufunc->uf_partial != NULL)
5115 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005116 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5117 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005118 goto failed_early;
5119 if (partial != NULL)
5120 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005121 outer_T *outer = get_pt_outer(partial);
5122
5123 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005124 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005125 if (current_ectx != NULL)
5126 {
5127 if (current_ectx->ec_outer_ref != NULL
5128 && current_ectx->ec_outer_ref->or_outer != NULL)
5129 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005130 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005131 }
5132 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005133 }
5134 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005135 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005136 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005137 ++partial->pt_refcount;
5138 ectx.ec_outer_ref->or_partial = partial;
5139 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005140 }
5141 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005142 {
5143 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5144 ++base_ufunc->uf_partial->pt_refcount;
5145 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5146 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005147 }
5148 }
5149
5150 // dummy frame entries
5151 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5152 {
5153 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5154 ++ectx.ec_stack.ga_len;
5155 }
5156
5157 {
5158 // Reserve space for local variables and any closure reference count.
5159 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5160 + ufunc->uf_dfunc_idx;
5161
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005162 // Initialize variables to zero. That avoids having to generate
5163 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005164 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005165 {
5166 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5167 STACK_TV_VAR(idx)->vval.v_number = 0;
5168 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005169 ectx.ec_stack.ga_len += dfunc->df_varcount;
5170 if (dfunc->df_has_closure)
5171 {
5172 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5173 STACK_TV_VAR(idx)->vval.v_number = 0;
5174 ++ectx.ec_stack.ga_len;
5175 }
5176
5177 ectx.ec_instr = INSTRUCTIONS(dfunc);
5178 }
5179
5180 // Following errors are in the function, not the caller.
5181 // Commands behave like vim9script.
5182 estack_push_ufunc(ufunc, 1);
5183 current_sctx = ufunc->uf_script_ctx;
5184 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5185
5186 // Use a specific location for storing error messages to be converted to an
5187 // exception.
5188 saved_msg_list = msg_list;
5189 msg_list = &private_msg_list;
5190
5191 // Do turn errors into exceptions.
5192 suppress_errthrow = FALSE;
5193
5194 // Do not delete the function while executing it.
5195 ++ufunc->uf_calls;
5196
5197 // When ":silent!" was used before calling then we still abort the
5198 // function. If ":silent!" is used in the function then we don't.
5199 emsg_silent_def = emsg_silent;
5200 did_emsg_def = 0;
5201
5202 ectx.ec_where.wt_index = 0;
5203 ectx.ec_where.wt_variable = FALSE;
5204
5205 // Execute the instructions until done.
5206 ret = exec_instructions(&ectx);
5207 if (ret == OK)
5208 {
5209 // function finished, get result from the stack.
5210 if (ufunc->uf_ret_type == &t_void)
5211 {
5212 rettv->v_type = VAR_VOID;
5213 }
5214 else
5215 {
5216 tv = STACK_TV_BOT(-1);
5217 *rettv = *tv;
5218 tv->v_type = VAR_UNKNOWN;
5219 }
5220 }
5221
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005222 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005223 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5224 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005225
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005226 // Deal with any remaining closures, they may be in use somewhere.
5227 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005228 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005229 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005230 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005231 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005232
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005233 estack_pop();
5234 current_sctx = save_current_sctx;
5235
Bram Moolenaar2336c372021-12-06 15:06:54 +00005236 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5237 // Function was unreferenced while being used, free it now.
5238 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005239
Bram Moolenaar352134b2020-10-17 22:04:08 +02005240 if (*msg_list != NULL && saved_msg_list != NULL)
5241 {
5242 msglist_T **plist = saved_msg_list;
5243
5244 // Append entries from the current msg_list (uncaught exceptions) to
5245 // the saved msg_list.
5246 while (*plist != NULL)
5247 plist = &(*plist)->next;
5248
5249 *plist = *msg_list;
5250 }
5251 msg_list = saved_msg_list;
5252
Bram Moolenaar4c137212021-04-19 16:48:48 +02005253 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005254 {
5255 cmdmod.cmod_filter_regmatch.regprog = NULL;
5256 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005257 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005258 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005259 emsg_silent_def = save_emsg_silent_def;
5260 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005261
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005262failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005263 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5265 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005266 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005269 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005270 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005271 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005272 if (ectx.ec_outer_ref->or_outer_allocated)
5273 vim_free(ectx.ec_outer_ref->or_outer);
5274 partial_unref(ectx.ec_outer_ref->or_partial);
5275 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005276 }
5277
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005278 // Not sure if this is necessary.
5279 suppress_errthrow = save_suppress_errthrow;
5280
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005281 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5282 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005283 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005284 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005285 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286 return ret;
5287}
5288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005290 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5291 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5292 * "pfx" is prefixed to every line.
5293 */
5294 static void
5295list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5296{
5297 int line_idx = 0;
5298 int prev_current = 0;
5299 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005300 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005301
5302 for (current = 0; current < instr_count; ++current)
5303 {
5304 isn_T *iptr = &instr[current];
5305 char *line;
5306
5307 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005308 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005309 while (line_idx < iptr->isn_lnum
5310 && line_idx < ufunc->uf_lines.ga_len)
5311 {
5312 if (current > prev_current)
5313 {
5314 msg_puts("\n\n");
5315 prev_current = current;
5316 }
5317 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5318 if (line != NULL)
5319 msg(line);
5320 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005321 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5322 {
5323 int first_def_arg = ufunc->uf_args.ga_len
5324 - ufunc->uf_def_args.ga_len;
5325
5326 if (def_arg_idx > 0)
5327 msg_puts("\n\n");
5328 msg_start();
5329 msg_puts(" ");
5330 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5331 first_def_arg + def_arg_idx]);
5332 msg_puts(" = ");
5333 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5334 msg_clr_eos();
5335 msg_end();
5336 }
5337 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005338
5339 switch (iptr->isn_type)
5340 {
5341 case ISN_EXEC:
5342 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5343 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005344 case ISN_EXEC_SPLIT:
5345 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5346 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005347 case ISN_EXECRANGE:
5348 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5349 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005350 case ISN_LEGACY_EVAL:
5351 smsg("%s%4d EVAL legacy %s", pfx, current,
5352 iptr->isn_arg.string);
5353 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005354 case ISN_REDIRSTART:
5355 smsg("%s%4d REDIR", pfx, current);
5356 break;
5357 case ISN_REDIREND:
5358 smsg("%s%4d REDIR END%s", pfx, current,
5359 iptr->isn_arg.number ? " append" : "");
5360 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005361 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005362#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005363 smsg("%s%4d CEXPR pre %s", pfx, current,
5364 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005365#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005366 break;
5367 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005368#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005369 {
5370 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5371
5372 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5373 cexpr_get_auname(cer->cer_cmdidx),
5374 cer->cer_forceit ? "!" : "",
5375 cer->cer_cmdline);
5376 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005377#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005378 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005379 case ISN_INSTR:
5380 {
5381 smsg("%s%4d INSTR", pfx, current);
5382 list_instructions(" ", iptr->isn_arg.instr,
5383 INT_MAX, NULL);
5384 msg(" -------------");
5385 }
5386 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005387 case ISN_SUBSTITUTE:
5388 {
5389 subs_T *subs = &iptr->isn_arg.subs;
5390
5391 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5392 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5393 msg(" -------------");
5394 }
5395 break;
5396 case ISN_EXECCONCAT:
5397 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5398 (varnumber_T)iptr->isn_arg.number);
5399 break;
5400 case ISN_ECHO:
5401 {
5402 echo_T *echo = &iptr->isn_arg.echo;
5403
5404 smsg("%s%4d %s %d", pfx, current,
5405 echo->echo_with_white ? "ECHO" : "ECHON",
5406 echo->echo_count);
5407 }
5408 break;
5409 case ISN_EXECUTE:
5410 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005411 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005412 break;
5413 case ISN_ECHOMSG:
5414 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005415 (varnumber_T)(iptr->isn_arg.number));
5416 break;
5417 case ISN_ECHOCONSOLE:
5418 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5419 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005420 break;
5421 case ISN_ECHOERR:
5422 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005423 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005424 break;
5425 case ISN_LOAD:
5426 {
5427 if (iptr->isn_arg.number < 0)
5428 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5429 (varnumber_T)(iptr->isn_arg.number
5430 + STACK_FRAME_SIZE));
5431 else
5432 smsg("%s%4d LOAD $%lld", pfx, current,
5433 (varnumber_T)(iptr->isn_arg.number));
5434 }
5435 break;
5436 case ISN_LOADOUTER:
5437 {
5438 if (iptr->isn_arg.number < 0)
5439 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5440 iptr->isn_arg.outer.outer_depth,
5441 iptr->isn_arg.outer.outer_idx
5442 + STACK_FRAME_SIZE);
5443 else
5444 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5445 iptr->isn_arg.outer.outer_depth,
5446 iptr->isn_arg.outer.outer_idx);
5447 }
5448 break;
5449 case ISN_LOADV:
5450 smsg("%s%4d LOADV v:%s", pfx, current,
5451 get_vim_var_name(iptr->isn_arg.number));
5452 break;
5453 case ISN_LOADSCRIPT:
5454 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005455 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5456 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5457 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005458
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005459 sv = get_script_svar(sref, -1);
5460 if (sv == NULL)
5461 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5462 pfx, current, si->sn_name);
5463 else
5464 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005465 sv->sv_name,
5466 sref->sref_idx,
5467 si->sn_name);
5468 }
5469 break;
5470 case ISN_LOADS:
5471 {
5472 scriptitem_T *si = SCRIPT_ITEM(
5473 iptr->isn_arg.loadstore.ls_sid);
5474
5475 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5476 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5477 }
5478 break;
5479 case ISN_LOADAUTO:
5480 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5481 break;
5482 case ISN_LOADG:
5483 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5484 break;
5485 case ISN_LOADB:
5486 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5487 break;
5488 case ISN_LOADW:
5489 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5490 break;
5491 case ISN_LOADT:
5492 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5493 break;
5494 case ISN_LOADGDICT:
5495 smsg("%s%4d LOAD g:", pfx, current);
5496 break;
5497 case ISN_LOADBDICT:
5498 smsg("%s%4d LOAD b:", pfx, current);
5499 break;
5500 case ISN_LOADWDICT:
5501 smsg("%s%4d LOAD w:", pfx, current);
5502 break;
5503 case ISN_LOADTDICT:
5504 smsg("%s%4d LOAD t:", pfx, current);
5505 break;
5506 case ISN_LOADOPT:
5507 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5508 break;
5509 case ISN_LOADENV:
5510 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5511 break;
5512 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005513 smsg("%s%4d LOADREG @%c", pfx, current,
5514 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005515 break;
5516
5517 case ISN_STORE:
5518 if (iptr->isn_arg.number < 0)
5519 smsg("%s%4d STORE arg[%lld]", pfx, current,
5520 iptr->isn_arg.number + STACK_FRAME_SIZE);
5521 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005522 smsg("%s%4d STORE $%lld", pfx, current,
5523 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005524 break;
5525 case ISN_STOREOUTER:
5526 {
5527 if (iptr->isn_arg.number < 0)
5528 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5529 iptr->isn_arg.outer.outer_depth,
5530 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5531 else
5532 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5533 iptr->isn_arg.outer.outer_depth,
5534 iptr->isn_arg.outer.outer_idx);
5535 }
5536 break;
5537 case ISN_STOREV:
5538 smsg("%s%4d STOREV v:%s", pfx, current,
5539 get_vim_var_name(iptr->isn_arg.number));
5540 break;
5541 case ISN_STOREAUTO:
5542 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5543 break;
5544 case ISN_STOREG:
5545 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5546 break;
5547 case ISN_STOREB:
5548 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5549 break;
5550 case ISN_STOREW:
5551 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5552 break;
5553 case ISN_STORET:
5554 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5555 break;
5556 case ISN_STORES:
5557 {
5558 scriptitem_T *si = SCRIPT_ITEM(
5559 iptr->isn_arg.loadstore.ls_sid);
5560
5561 smsg("%s%4d STORES %s in %s", pfx, current,
5562 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5563 }
5564 break;
5565 case ISN_STORESCRIPT:
5566 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005567 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5568 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5569 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005570
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005571 sv = get_script_svar(sref, -1);
5572 if (sv == NULL)
5573 smsg("%s%4d STORESCRIPT [deleted] in %s",
5574 pfx, current, si->sn_name);
5575 else
5576 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005577 sv->sv_name,
5578 sref->sref_idx,
5579 si->sn_name);
5580 }
5581 break;
5582 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005583 case ISN_STOREFUNCOPT:
5584 smsg("%s%4d %s &%s", pfx, current,
5585 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005586 iptr->isn_arg.storeopt.so_name);
5587 break;
5588 case ISN_STOREENV:
5589 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5590 break;
5591 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005592 smsg("%s%4d STOREREG @%c", pfx, current,
5593 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005594 break;
5595 case ISN_STORENR:
5596 smsg("%s%4d STORE %lld in $%d", pfx, current,
5597 iptr->isn_arg.storenr.stnr_val,
5598 iptr->isn_arg.storenr.stnr_idx);
5599 break;
5600
5601 case ISN_STOREINDEX:
5602 smsg("%s%4d STOREINDEX %s", pfx, current,
5603 vartype_name(iptr->isn_arg.vartype));
5604 break;
5605
5606 case ISN_STORERANGE:
5607 smsg("%s%4d STORERANGE", pfx, current);
5608 break;
5609
5610 // constants
5611 case ISN_PUSHNR:
5612 smsg("%s%4d PUSHNR %lld", pfx, current,
5613 (varnumber_T)(iptr->isn_arg.number));
5614 break;
5615 case ISN_PUSHBOOL:
5616 case ISN_PUSHSPEC:
5617 smsg("%s%4d PUSH %s", pfx, current,
5618 get_var_special_name(iptr->isn_arg.number));
5619 break;
5620 case ISN_PUSHF:
5621#ifdef FEAT_FLOAT
5622 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5623#endif
5624 break;
5625 case ISN_PUSHS:
5626 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5627 break;
5628 case ISN_PUSHBLOB:
5629 {
5630 char_u *r;
5631 char_u numbuf[NUMBUFLEN];
5632 char_u *tofree;
5633
5634 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5635 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5636 vim_free(tofree);
5637 }
5638 break;
5639 case ISN_PUSHFUNC:
5640 {
5641 char *name = (char *)iptr->isn_arg.string;
5642
5643 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5644 name == NULL ? "[none]" : name);
5645 }
5646 break;
5647 case ISN_PUSHCHANNEL:
5648#ifdef FEAT_JOB_CHANNEL
5649 {
5650 channel_T *channel = iptr->isn_arg.channel;
5651
5652 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5653 channel == NULL ? 0 : channel->ch_id);
5654 }
5655#endif
5656 break;
5657 case ISN_PUSHJOB:
5658#ifdef FEAT_JOB_CHANNEL
5659 {
5660 typval_T tv;
5661 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005662 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005663
5664 tv.v_type = VAR_JOB;
5665 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005666 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005667 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5668 }
5669#endif
5670 break;
5671 case ISN_PUSHEXC:
5672 smsg("%s%4d PUSH v:exception", pfx, current);
5673 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005674 case ISN_AUTOLOAD:
5675 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5676 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005677 case ISN_UNLET:
5678 smsg("%s%4d UNLET%s %s", pfx, current,
5679 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5680 iptr->isn_arg.unlet.ul_name);
5681 break;
5682 case ISN_UNLETENV:
5683 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5684 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5685 iptr->isn_arg.unlet.ul_name);
5686 break;
5687 case ISN_UNLETINDEX:
5688 smsg("%s%4d UNLETINDEX", pfx, current);
5689 break;
5690 case ISN_UNLETRANGE:
5691 smsg("%s%4d UNLETRANGE", pfx, current);
5692 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005693 case ISN_LOCKUNLOCK:
5694 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5695 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005696 case ISN_LOCKCONST:
5697 smsg("%s%4d LOCKCONST", pfx, current);
5698 break;
5699 case ISN_NEWLIST:
5700 smsg("%s%4d NEWLIST size %lld", pfx, current,
5701 (varnumber_T)(iptr->isn_arg.number));
5702 break;
5703 case ISN_NEWDICT:
5704 smsg("%s%4d NEWDICT size %lld", pfx, current,
5705 (varnumber_T)(iptr->isn_arg.number));
5706 break;
5707
5708 // function call
5709 case ISN_BCALL:
5710 {
5711 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5712
5713 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5714 internal_func_name(cbfunc->cbf_idx),
5715 cbfunc->cbf_argcount);
5716 }
5717 break;
5718 case ISN_DCALL:
5719 {
5720 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5721 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5722 + cdfunc->cdf_idx;
5723
5724 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005725 printable_func_name(df->df_ufunc),
5726 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005727 }
5728 break;
5729 case ISN_UCALL:
5730 {
5731 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5732
5733 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5734 cufunc->cuf_name, cufunc->cuf_argcount);
5735 }
5736 break;
5737 case ISN_PCALL:
5738 {
5739 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5740
5741 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5742 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5743 }
5744 break;
5745 case ISN_PCALL_END:
5746 smsg("%s%4d PCALL end", pfx, current);
5747 break;
5748 case ISN_RETURN:
5749 smsg("%s%4d RETURN", pfx, current);
5750 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005751 case ISN_RETURN_VOID:
5752 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005753 break;
5754 case ISN_FUNCREF:
5755 {
5756 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005757 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005758
Bram Moolenaar38453522021-11-28 22:00:12 +00005759 if (funcref->fr_func_name == NULL)
5760 {
5761 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5762 + funcref->fr_dfunc_idx;
5763 name = df->df_ufunc->uf_name;
5764 }
5765 else
5766 name = funcref->fr_func_name;
5767 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005768 }
5769 break;
5770
5771 case ISN_NEWFUNC:
5772 {
5773 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5774
5775 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5776 newfunc->nf_lambda, newfunc->nf_global);
5777 }
5778 break;
5779
5780 case ISN_DEF:
5781 {
5782 char_u *name = iptr->isn_arg.string;
5783
5784 smsg("%s%4d DEF %s", pfx, current,
5785 name == NULL ? (char_u *)"" : name);
5786 }
5787 break;
5788
5789 case ISN_JUMP:
5790 {
5791 char *when = "?";
5792
5793 switch (iptr->isn_arg.jump.jump_when)
5794 {
5795 case JUMP_ALWAYS:
5796 when = "JUMP";
5797 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005798 case JUMP_NEVER:
5799 iemsg("JUMP_NEVER should not be used");
5800 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005801 case JUMP_AND_KEEP_IF_TRUE:
5802 when = "JUMP_AND_KEEP_IF_TRUE";
5803 break;
5804 case JUMP_IF_FALSE:
5805 when = "JUMP_IF_FALSE";
5806 break;
5807 case JUMP_AND_KEEP_IF_FALSE:
5808 when = "JUMP_AND_KEEP_IF_FALSE";
5809 break;
5810 case JUMP_IF_COND_FALSE:
5811 when = "JUMP_IF_COND_FALSE";
5812 break;
5813 case JUMP_IF_COND_TRUE:
5814 when = "JUMP_IF_COND_TRUE";
5815 break;
5816 }
5817 smsg("%s%4d %s -> %d", pfx, current, when,
5818 iptr->isn_arg.jump.jump_where);
5819 }
5820 break;
5821
5822 case ISN_JUMP_IF_ARG_SET:
5823 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5824 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5825 iptr->isn_arg.jump.jump_where);
5826 break;
5827
5828 case ISN_FOR:
5829 {
5830 forloop_T *forloop = &iptr->isn_arg.forloop;
5831
5832 smsg("%s%4d FOR $%d -> %d", pfx, current,
5833 forloop->for_idx, forloop->for_end);
5834 }
5835 break;
5836
5837 case ISN_TRY:
5838 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005839 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005840
5841 if (try->try_ref->try_finally == 0)
5842 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5843 pfx, current,
5844 try->try_ref->try_catch,
5845 try->try_ref->try_endtry);
5846 else
5847 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5848 pfx, current,
5849 try->try_ref->try_catch,
5850 try->try_ref->try_finally,
5851 try->try_ref->try_endtry);
5852 }
5853 break;
5854 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005855 smsg("%s%4d CATCH", pfx, current);
5856 break;
5857 case ISN_TRYCONT:
5858 {
5859 trycont_T *trycont = &iptr->isn_arg.trycont;
5860
5861 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5862 trycont->tct_levels,
5863 trycont->tct_levels == 1 ? "" : "s",
5864 trycont->tct_where);
5865 }
5866 break;
5867 case ISN_FINALLY:
5868 smsg("%s%4d FINALLY", pfx, current);
5869 break;
5870 case ISN_ENDTRY:
5871 smsg("%s%4d ENDTRY", pfx, current);
5872 break;
5873 case ISN_THROW:
5874 smsg("%s%4d THROW", pfx, current);
5875 break;
5876
5877 // expression operations on number
5878 case ISN_OPNR:
5879 case ISN_OPFLOAT:
5880 case ISN_OPANY:
5881 {
5882 char *what;
5883 char *ins;
5884
5885 switch (iptr->isn_arg.op.op_type)
5886 {
5887 case EXPR_MULT: what = "*"; break;
5888 case EXPR_DIV: what = "/"; break;
5889 case EXPR_REM: what = "%"; break;
5890 case EXPR_SUB: what = "-"; break;
5891 case EXPR_ADD: what = "+"; break;
5892 default: what = "???"; break;
5893 }
5894 switch (iptr->isn_type)
5895 {
5896 case ISN_OPNR: ins = "OPNR"; break;
5897 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5898 case ISN_OPANY: ins = "OPANY"; break;
5899 default: ins = "???"; break;
5900 }
5901 smsg("%s%4d %s %s", pfx, current, ins, what);
5902 }
5903 break;
5904
5905 case ISN_COMPAREBOOL:
5906 case ISN_COMPARESPECIAL:
5907 case ISN_COMPARENR:
5908 case ISN_COMPAREFLOAT:
5909 case ISN_COMPARESTRING:
5910 case ISN_COMPAREBLOB:
5911 case ISN_COMPARELIST:
5912 case ISN_COMPAREDICT:
5913 case ISN_COMPAREFUNC:
5914 case ISN_COMPAREANY:
5915 {
5916 char *p;
5917 char buf[10];
5918 char *type;
5919
5920 switch (iptr->isn_arg.op.op_type)
5921 {
5922 case EXPR_EQUAL: p = "=="; break;
5923 case EXPR_NEQUAL: p = "!="; break;
5924 case EXPR_GREATER: p = ">"; break;
5925 case EXPR_GEQUAL: p = ">="; break;
5926 case EXPR_SMALLER: p = "<"; break;
5927 case EXPR_SEQUAL: p = "<="; break;
5928 case EXPR_MATCH: p = "=~"; break;
5929 case EXPR_IS: p = "is"; break;
5930 case EXPR_ISNOT: p = "isnot"; break;
5931 case EXPR_NOMATCH: p = "!~"; break;
5932 default: p = "???"; break;
5933 }
5934 STRCPY(buf, p);
5935 if (iptr->isn_arg.op.op_ic == TRUE)
5936 strcat(buf, "?");
5937 switch(iptr->isn_type)
5938 {
5939 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5940 case ISN_COMPARESPECIAL:
5941 type = "COMPARESPECIAL"; break;
5942 case ISN_COMPARENR: type = "COMPARENR"; break;
5943 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5944 case ISN_COMPARESTRING:
5945 type = "COMPARESTRING"; break;
5946 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5947 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5948 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5949 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5950 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5951 default: type = "???"; break;
5952 }
5953
5954 smsg("%s%4d %s %s", pfx, current, type, buf);
5955 }
5956 break;
5957
5958 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5959 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5960
5961 // expression operations
5962 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5963 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5964 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5965 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5966 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5967 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5968 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5969 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5970 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5971 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5972 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5973 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005974 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005975 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5976 iptr->isn_arg.getitem.gi_index,
5977 iptr->isn_arg.getitem.gi_with_op ?
5978 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005979 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5980 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5981 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005982 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5983 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5984
Bram Moolenaar4c137212021-04-19 16:48:48 +02005985 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5986
5987 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5988 case ISN_CHECKTYPE:
5989 {
5990 checktype_T *ct = &iptr->isn_arg.type;
5991 char *tofree;
5992
5993 if (ct->ct_arg_idx == 0)
5994 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5995 type_name(ct->ct_type, &tofree),
5996 (int)ct->ct_off);
5997 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005998 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5999 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006000 type_name(ct->ct_type, &tofree),
6001 (int)ct->ct_off,
6002 (int)ct->ct_arg_idx);
6003 vim_free(tofree);
6004 break;
6005 }
6006 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6007 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6008 iptr->isn_arg.checklen.cl_min_len);
6009 break;
6010 case ISN_SETTYPE:
6011 {
6012 char *tofree;
6013
6014 smsg("%s%4d SETTYPE %s", pfx, current,
6015 type_name(iptr->isn_arg.type.ct_type, &tofree));
6016 vim_free(tofree);
6017 break;
6018 }
6019 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006020 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6021 smsg("%s%4d INVERT %d (!val)", pfx, current,
6022 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006023 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006024 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6025 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006026 break;
6027 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006028 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006029 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006030 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6031 pfx, current,
6032 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006033 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006034 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6035 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006036 break;
6037 case ISN_PUT:
6038 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6039 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006040 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006041 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6042 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006043 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006044 else
6045 smsg("%s%4d PUT %c %ld", pfx, current,
6046 iptr->isn_arg.put.put_regname,
6047 (long)iptr->isn_arg.put.put_lnum);
6048 break;
6049
Bram Moolenaar4c137212021-04-19 16:48:48 +02006050 case ISN_CMDMOD:
6051 {
6052 char_u *buf;
6053 size_t len = produce_cmdmods(
6054 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6055
6056 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006057 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006058 {
6059 (void)produce_cmdmods(
6060 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6061 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6062 vim_free(buf);
6063 }
6064 break;
6065 }
6066 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6067
6068 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006069 smsg("%s%4d PROFILE START line %d", pfx, current,
6070 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006071 break;
6072
6073 case ISN_PROF_END:
6074 smsg("%s%4d PROFILE END", pfx, current);
6075 break;
6076
Bram Moolenaare99d4222021-06-13 14:01:26 +02006077 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006078 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6079 iptr->isn_arg.debug.dbg_break_lnum + 1,
6080 iptr->isn_lnum,
6081 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006082 break;
6083
Bram Moolenaar4c137212021-04-19 16:48:48 +02006084 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6085 iptr->isn_arg.unpack.unp_count,
6086 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6087 break;
6088 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6089 iptr->isn_arg.shuffle.shfl_item,
6090 iptr->isn_arg.shuffle.shfl_up);
6091 break;
6092 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6093
6094 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6095 return;
6096 }
6097
6098 out_flush(); // output one line at a time
6099 ui_breakcheck();
6100 if (got_int)
6101 break;
6102 }
6103}
6104
6105/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006106 * Handle command line completion for the :disassemble command.
6107 */
6108 void
6109set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6110{
6111 char_u *p;
6112
6113 // Default: expand user functions, "debug" and "profile"
6114 xp->xp_context = EXPAND_DISASSEMBLE;
6115 xp->xp_pattern = arg;
6116
6117 // first argument already typed: only user function names
6118 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6119 {
6120 xp->xp_context = EXPAND_USER_FUNC;
6121 xp->xp_pattern = skipwhite(p);
6122 }
6123}
6124
6125/*
6126 * Function given to ExpandGeneric() to obtain the list of :disassemble
6127 * arguments.
6128 */
6129 char_u *
6130get_disassemble_argument(expand_T *xp, int idx)
6131{
6132 if (idx == 0)
6133 return (char_u *)"debug";
6134 if (idx == 1)
6135 return (char_u *)"profile";
6136 return get_user_func_name(xp, idx - 2);
6137}
6138
6139/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006140 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006141 * We don't really need this at runtime, but we do have tests that require it,
6142 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143 */
6144 void
6145ex_disassemble(exarg_T *eap)
6146{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006147 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006148 char_u *fname;
6149 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150 dfunc_T *dfunc;
6151 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006152 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006153 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006154 compiletype_T compile_type = CT_NONE;
6155
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006156 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006157 {
6158 compile_type = CT_PROFILE;
6159 arg = skipwhite(arg + 7);
6160 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006161 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006162 {
6163 compile_type = CT_DEBUG;
6164 arg = skipwhite(arg + 5);
6165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006167 if (STRNCMP(arg, "<lambda>", 8) == 0)
6168 {
6169 arg += 8;
6170 (void)getdigits(&arg);
6171 fname = vim_strnsave(eap->arg, arg - eap->arg);
6172 }
6173 else
6174 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006175 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006176 if (fname == NULL)
6177 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006178 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006179 return;
6180 }
6181
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006182 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006183 if (ufunc == NULL)
6184 {
6185 char_u *p = untrans_function_name(fname);
6186
6187 if (p != NULL)
6188 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006189 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006190 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006191 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192 if (ufunc == NULL)
6193 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006194 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195 return;
6196 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006197 if (func_needs_compiling(ufunc, compile_type)
6198 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006199 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006200 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006201 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006202 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006203 return;
6204 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006205 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206
6207 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006208 switch (compile_type)
6209 {
6210 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006211#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006212 instr = dfunc->df_instr_prof;
6213 instr_count = dfunc->df_instr_prof_count;
6214 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006215#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006216 // FALLTHROUGH
6217 case CT_NONE:
6218 instr = dfunc->df_instr;
6219 instr_count = dfunc->df_instr_count;
6220 break;
6221 case CT_DEBUG:
6222 instr = dfunc->df_instr_debug;
6223 instr_count = dfunc->df_instr_debug_count;
6224 break;
6225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226
Bram Moolenaar4c137212021-04-19 16:48:48 +02006227 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006228}
6229
6230/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006231 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 * list, etc. Mostly like what JavaScript does, except that empty list and
6233 * empty dictionary are FALSE.
6234 */
6235 int
6236tv2bool(typval_T *tv)
6237{
6238 switch (tv->v_type)
6239 {
6240 case VAR_NUMBER:
6241 return tv->vval.v_number != 0;
6242 case VAR_FLOAT:
6243#ifdef FEAT_FLOAT
6244 return tv->vval.v_float != 0.0;
6245#else
6246 break;
6247#endif
6248 case VAR_PARTIAL:
6249 return tv->vval.v_partial != NULL;
6250 case VAR_FUNC:
6251 case VAR_STRING:
6252 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6253 case VAR_LIST:
6254 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6255 case VAR_DICT:
6256 return tv->vval.v_dict != NULL
6257 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6258 case VAR_BOOL:
6259 case VAR_SPECIAL:
6260 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6261 case VAR_JOB:
6262#ifdef FEAT_JOB_CHANNEL
6263 return tv->vval.v_job != NULL;
6264#else
6265 break;
6266#endif
6267 case VAR_CHANNEL:
6268#ifdef FEAT_JOB_CHANNEL
6269 return tv->vval.v_channel != NULL;
6270#else
6271 break;
6272#endif
6273 case VAR_BLOB:
6274 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6275 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006276 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006278 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279 break;
6280 }
6281 return FALSE;
6282}
6283
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006284 void
6285emsg_using_string_as(typval_T *tv, int as_number)
6286{
6287 semsg(_(as_number ? e_using_string_as_number_str
6288 : e_using_string_as_bool_str),
6289 tv->vval.v_string == NULL
6290 ? (char_u *)"" : tv->vval.v_string);
6291}
6292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293/*
6294 * If "tv" is a string give an error and return FAIL.
6295 */
6296 int
6297check_not_string(typval_T *tv)
6298{
6299 if (tv->v_type == VAR_STRING)
6300 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006301 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 clear_tv(tv);
6303 return FAIL;
6304 }
6305 return OK;
6306}
6307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308
6309#endif // FEAT_EVAL