blob: 4d24eb96eb72cab8569271bf52ecac10f23cee3f [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 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000155 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200156update_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 Moolenaar139575d2022-03-15 19:29:30 +0000287 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288
289 if (dfunc->df_deleted)
290 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100291 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000292 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100293 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 return FAIL;
295 }
296
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100297#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100298 if (do_profiling == PROF_YES)
299 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200300 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100301 {
302 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
303 + profile_info_ga.ga_len;
304 ++profile_info_ga.ga_len;
305 CLEAR_POINTER(info);
306 profile_may_start_func(info, ufunc,
307 (((dfunc_T *)def_functions.ga_data)
308 + ectx->ec_dfunc_idx)->df_ufunc);
309 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100310 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100311#endif
312
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200313 // When debugging and using "cont" switches to the not-debugged
314 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000315 compile_type = get_compile_type(ufunc);
316 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200317 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000318 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200319
320 // compile_def_function() may cause def_functions.ga_data to change
321 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
322 }
323 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200324 {
325 if (did_emsg_cumul + did_emsg == did_emsg_before)
326 semsg(_(e_function_is_not_compiled_str),
327 printable_func_name(ufunc));
328 return FAIL;
329 }
330
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200331 if (ufunc->uf_va_name != NULL)
332 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200333 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200334 // Stack at time of call with 2 varargs:
335 // normal_arg
336 // optional_arg
337 // vararg_1
338 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200339 // After creating the list:
340 // normal_arg
341 // optional_arg
342 // vararg-list
343 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200344 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200345 // After creating the list
346 // normal_arg
347 // (space for optional_arg)
348 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200349 vararg_count = argcount - ufunc->uf_args.ga_len;
350 if (vararg_count < 0)
351 vararg_count = 0;
352 else
353 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200354 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200355 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200356
357 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200358 }
359
Bram Moolenaarfe270812020-04-11 22:31:27 +0200360 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200361 if (arg_to_add < 0)
362 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200363 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200364 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200365 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200366 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200367 return FAIL;
368 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000369 else if (arg_to_add > ufunc->uf_def_args.ga_len)
370 {
371 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
372
373 if (missing == 1)
374 emsg(_(e_one_argument_too_few));
375 else
376 semsg(_(e_nr_arguments_too_few), missing);
377 return FAIL;
378 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200379
380 // Reserve space for:
381 // - missing arguments
382 // - stack frame
383 // - local variables
384 // - if needed: a counter for number of closures created in
385 // ectx->ec_funcrefs.
386 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200387 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100388 return FAIL;
389
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100390 // If depth of calling is getting too high, don't execute the function.
391 if (funcdepth_increment() == FAIL)
392 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200393 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100394
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100395 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200396 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100397 {
398 floc = ALLOC_ONE(funclocal_T);
399 if (floc == NULL)
400 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200401 *floc = ectx->ec_funclocal;
402 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100403 }
404
Bram Moolenaarfe270812020-04-11 22:31:27 +0200405 // Move the vararg-list to below the missing optional arguments.
406 if (vararg_count > 0 && arg_to_add > 0)
407 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100408
409 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200410 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200411 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200412 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413
414 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100415 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
416 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200417 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200418 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
419 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100420 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100421 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200422 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423
424 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200425 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000426 {
427 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
428
429 tv->v_type = VAR_NUMBER;
430 tv->vval.v_number = 0;
431 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200432 if (dfunc->df_has_closure)
433 {
434 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
435
436 tv->v_type = VAR_NUMBER;
437 tv->vval.v_number = 0;
438 }
439 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100440
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100441 if (pt != NULL || ufunc->uf_partial != NULL
442 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100443 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200444 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100445
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200446 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100447 return FAIL;
448 if (pt != NULL)
449 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000450 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200451 ++pt->pt_refcount;
452 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100453 }
454 else if (ufunc->uf_partial != NULL)
455 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000456 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200457 ++ufunc->uf_partial->pt_refcount;
458 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100459 }
460 else
461 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200462 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200463 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200464 {
465 vim_free(ref);
466 return FAIL;
467 }
468 ref->or_outer_allocated = TRUE;
469 ref->or_outer->out_stack = &ectx->ec_stack;
470 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
471 if (ectx->ec_outer_ref != NULL)
472 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100473 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200474 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100475 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100476 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200477 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100478
Bram Moolenaarc970e422021-03-17 15:03:04 +0100479 ++ufunc->uf_calls;
480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 // Set execution state to the start of the called function.
482 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100483 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100484 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200485 if (entry != NULL)
486 {
487 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200488 // Save the current context so it can be restored on return.
489 entry->es_save_sctx = current_sctx;
490 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200491 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100492
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200493 // Start execution at the first instruction.
494 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495
496 return OK;
497}
498
499// Get pointer to item in the stack.
500#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
501
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000502// Double linked list of funcstack_T in use.
503static funcstack_T *first_funcstack = NULL;
504
505 static void
506add_funcstack_to_list(funcstack_T *funcstack)
507{
508 // Link in list of funcstacks.
509 if (first_funcstack != NULL)
510 first_funcstack->fs_prev = funcstack;
511 funcstack->fs_next = first_funcstack;
512 funcstack->fs_prev = NULL;
513 first_funcstack = funcstack;
514}
515
516 static void
517remove_funcstack_from_list(funcstack_T *funcstack)
518{
519 if (funcstack->fs_prev == NULL)
520 first_funcstack = funcstack->fs_next;
521 else
522 funcstack->fs_prev->fs_next = funcstack->fs_next;
523 if (funcstack->fs_next != NULL)
524 funcstack->fs_next->fs_prev = funcstack->fs_prev;
525}
526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200528 * Used when returning from a function: Check if any closure is still
529 * referenced. If so then move the arguments and variables to a separate piece
530 * of stack to be used when the closure is called.
531 * When "free_arguments" is TRUE the arguments are to be freed.
532 * Returns FAIL when out of memory.
533 */
534 static int
535handle_closure_in_use(ectx_T *ectx, int free_arguments)
536{
537 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
538 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200539 int argcount;
540 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200541 int idx;
542 typval_T *tv;
543 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200544 garray_T *gap = &ectx->ec_funcrefs;
545 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200546
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200547 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200548 return OK; // function was freed
549 if (dfunc->df_has_closure == 0)
550 return OK; // no closures
551 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
552 closure_count = tv->vval.v_number;
553 if (closure_count == 0)
554 return OK; // no funcrefs created
555
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200556 argcount = ufunc_argcount(dfunc->df_ufunc);
557 top = ectx->ec_frame_idx - argcount;
558
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200559 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200560 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200561 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200562 partial_T *pt;
563 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200564
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200565 if (off < 0)
566 continue; // count is off or already done
567 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200568 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200569 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200570 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200571 int i;
572
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000573 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200574 // unreferenced on return.
575 for (i = 0; i < dfunc->df_varcount; ++i)
576 {
577 typval_T *stv = STACK_TV(ectx->ec_frame_idx
578 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200579 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200580 --refcount;
581 }
582 if (refcount > 1)
583 {
584 closure_in_use = TRUE;
585 break;
586 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200587 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200588 }
589
590 if (closure_in_use)
591 {
592 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
593 typval_T *stack;
594
595 // A closure is using the arguments and/or local variables.
596 // Move them to the called function.
597 if (funcstack == NULL)
598 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000599
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200600 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
601 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200602 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
603 funcstack->fs_ga.ga_data = stack;
604 if (stack == NULL)
605 {
606 vim_free(funcstack);
607 return FAIL;
608 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000609 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200610
611 // Move or copy the arguments.
612 for (idx = 0; idx < argcount; ++idx)
613 {
614 tv = STACK_TV(top + idx);
615 if (free_arguments)
616 {
617 *(stack + idx) = *tv;
618 tv->v_type = VAR_UNKNOWN;
619 }
620 else
621 copy_tv(tv, stack + idx);
622 }
623 // Move the local variables.
624 for (idx = 0; idx < dfunc->df_varcount; ++idx)
625 {
626 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200627
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200628 // A partial created for a local function, that is also used as a
629 // local variable, has a reference count for the variable, thus
630 // will never go down to zero. When all these refcounts are one
631 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000632 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200633 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
634 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200635 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200636
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200637 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200638 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
639 gap->ga_len - closure_count + i])
640 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200641 }
642
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200643 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200644 tv->v_type = VAR_UNKNOWN;
645 }
646
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200647 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200648 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200649 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
650 - closure_count + idx];
651 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200652 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200653 ++funcstack->fs_refcount;
654 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100655 pt->pt_outer.out_stack = &funcstack->fs_ga;
656 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200657 }
658 }
659 }
660
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200661 for (idx = 0; idx < closure_count; ++idx)
662 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
663 - closure_count + idx]);
664 gap->ga_len -= closure_count;
665 if (gap->ga_len == 0)
666 ga_clear(gap);
667
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200668 return OK;
669}
670
671/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200672 * Called when a partial is freed or its reference count goes down to one. The
673 * funcstack may be the only reference to the partials in the local variables.
674 * Go over all of them, the funcref and can be freed if all partials
675 * referencing the funcstack have a reference count of one.
676 */
677 void
678funcstack_check_refcount(funcstack_T *funcstack)
679{
680 int i;
681 garray_T *gap = &funcstack->fs_ga;
682 int done = 0;
683
684 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
685 return;
686 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
687 {
688 typval_T *tv = ((typval_T *)gap->ga_data) + i;
689
690 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
691 && tv->vval.v_partial->pt_funcstack == funcstack
692 && tv->vval.v_partial->pt_refcount == 1)
693 ++done;
694 }
695 if (done == funcstack->fs_min_refcount)
696 {
697 typval_T *stack = gap->ga_data;
698
699 // All partials referencing the funcstack have a reference count of
700 // one, thus the funcstack is no longer of use.
701 for (i = 0; i < gap->ga_len; ++i)
702 clear_tv(stack + i);
703 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000704 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200705 vim_free(funcstack);
706 }
707}
708
709/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000710 * For garbage collecting: set references in all variables referenced by
711 * all funcstacks.
712 */
713 int
714set_ref_in_funcstacks(int copyID)
715{
716 funcstack_T *funcstack;
717
718 for (funcstack = first_funcstack; funcstack != NULL;
719 funcstack = funcstack->fs_next)
720 {
721 typval_T *stack = funcstack->fs_ga.ga_data;
722 int i;
723
724 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
725 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
726 return TRUE; // abort
727 }
728 return FALSE;
729}
730
731/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 * Return from the current function.
733 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200734 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200735func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100738 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200739 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
740 + ectx->ec_dfunc_idx;
741 int argcount = ufunc_argcount(dfunc->df_ufunc);
742 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200743 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100744 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
745 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200746 funclocal_T *floc;
747#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100748 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
749 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750
Bram Moolenaar12d26532021-02-19 19:13:21 +0100751 if (do_profiling == PROF_YES)
752 {
753 ufunc_T *caller = prev_dfunc->df_ufunc;
754
755 if (dfunc->df_ufunc->uf_profiling
756 || (caller != NULL && caller->uf_profiling))
757 {
758 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
759 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
760 --profile_info_ga.ga_len;
761 }
762 }
763#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000764
765 // No check for uf_refcount being zero, cannot think of a way that would
766 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100767 --dfunc->df_ufunc->uf_calls;
768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200770 entry = estack_pop();
771 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200772 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200774 if (handle_closure_in_use(ectx, TRUE) == FAIL)
775 return FAIL;
776
777 // Clear the arguments.
778 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
779 clear_tv(STACK_TV(idx));
780
781 // Clear local variables and temp values, but not the return value.
782 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100783 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100785
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100786 // The return value should be on top of the stack. However, when aborting
787 // it may not be there and ec_frame_idx is the top of the stack.
788 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100789 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100790 ret_idx = 0;
791
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200792 if (ectx->ec_outer_ref != NULL)
793 {
794 if (ectx->ec_outer_ref->or_outer_allocated)
795 vim_free(ectx->ec_outer_ref->or_outer);
796 partial_unref(ectx->ec_outer_ref->or_partial);
797 vim_free(ectx->ec_outer_ref);
798 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100799
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100800 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100801 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100802 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
803 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200804 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
805 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200806 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100807 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100808 floc = (void *)STACK_TV(ectx->ec_frame_idx
809 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200810 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100811 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
812 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200813
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100814 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200815 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100816 else
817 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200818 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100819 vim_free(floc);
820 }
821
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100822 if (ret_idx > 0)
823 {
824 // Reset the stack to the position before the call, with a spot for the
825 // return value, moved there from above the frame.
826 ectx->ec_stack.ga_len = top + 1;
827 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
828 }
829 else
830 // Reset the stack to the position before the call.
831 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200832
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100833 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200834 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200835 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836}
837
838#undef STACK_TV
839
840/*
841 * Prepare arguments and rettv for calling a builtin or user function.
842 */
843 static int
844call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
845{
846 int idx;
847 typval_T *tv;
848
849 // Move arguments from bottom of the stack to argvars[] and add terminator.
850 for (idx = 0; idx < argcount; ++idx)
851 argvars[idx] = *STACK_TV_BOT(idx - argcount);
852 argvars[argcount].v_type = VAR_UNKNOWN;
853
854 // Result replaces the arguments on the stack.
855 if (argcount > 0)
856 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200857 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 return FAIL;
859 else
860 ++ectx->ec_stack.ga_len;
861
862 // Default return value is zero.
863 tv = STACK_TV_BOT(-1);
864 tv->v_type = VAR_NUMBER;
865 tv->vval.v_number = 0;
866
867 return OK;
868}
869
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200870// Ugly global to avoid passing the execution context around through many
871// layers.
872static ectx_T *current_ectx = NULL;
873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874/*
875 * Call a builtin function by index.
876 */
877 static int
878call_bfunc(int func_idx, int argcount, ectx_T *ectx)
879{
880 typval_T argvars[MAX_FUNC_ARGS];
881 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100882 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200883 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200884 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885
886 if (call_prepare(argcount, argvars, ectx) == FAIL)
887 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200888 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200890 // Call the builtin function. Set "current_ectx" so that when it
891 // recursively invokes call_def_function() a closure context can be set.
892 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200894 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200895 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896
897 // Clear the arguments.
898 for (idx = 0; idx < argcount; ++idx)
899 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200900
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100901 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200902 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903 return OK;
904}
905
906/*
907 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100908 * If the function is compiled this will add a stack frame and set the
909 * instruction pointer at the start of the function.
910 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200911 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100912 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913 */
914 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100915call_ufunc(
916 ufunc_T *ufunc,
917 partial_T *pt,
918 int argcount,
919 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200920 isn_T *iptr,
921 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922{
923 typval_T argvars[MAX_FUNC_ARGS];
924 funcexe_T funcexe;
925 int error;
926 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100927 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000928 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929
Bram Moolenaare99d4222021-06-13 14:01:26 +0200930 if (func_needs_compiling(ufunc, compile_type)
931 && compile_def_function(ufunc, FALSE, compile_type, NULL)
932 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200933 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200934 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100935 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100936 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100937 if (error != FCERR_UNKNOWN)
938 {
939 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000940 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100941 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000942 semsg(_(e_not_enough_arguments_for_function_str),
943 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100944 return FAIL;
945 }
946
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100947 // The function has been compiled, can call it quickly. For a function
948 // that was defined later: we can call it directly next time.
949 if (iptr != NULL)
950 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100951 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100952 iptr->isn_type = ISN_DCALL;
953 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
954 iptr->isn_arg.dfunc.cdf_argcount = argcount;
955 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200956 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100957 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958
959 if (call_prepare(argcount, argvars, ectx) == FAIL)
960 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200961 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000962 funcexe.fe_evaluate = TRUE;
963 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964
965 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000967 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968
969 // Clear the arguments.
970 for (idx = 0; idx < argcount; ++idx)
971 clear_tv(&argvars[idx]);
972
973 if (error != FCERR_NONE)
974 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000975 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 return FAIL;
977 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100978 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200979 // Error other than from calling the function itself.
980 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 return OK;
982}
983
984/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100985 * If command modifiers were applied restore them.
986 */
987 static void
988may_restore_cmdmod(funclocal_T *funclocal)
989{
990 if (funclocal->floc_restore_cmdmod)
991 {
992 cmdmod.cmod_filter_regmatch.regprog = NULL;
993 undo_cmdmod(&cmdmod);
994 cmdmod = funclocal->floc_save_cmdmod;
995 funclocal->floc_restore_cmdmod = FALSE;
996 }
997}
998
999/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001000 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1001 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001002 */
1003 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001004vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001005{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001006 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001007}
1008
1009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 * Execute a function by "name".
1011 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001012 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 * Returns FAIL if not found without an error message.
1014 */
1015 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001016call_by_name(
1017 char_u *name,
1018 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001019 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001020 isn_T *iptr,
1021 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022{
1023 ufunc_T *ufunc;
1024
1025 if (builtin_function(name, -1))
1026 {
1027 int func_idx = find_internal_func(name);
1028
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001029 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001031 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 return FAIL;
1033 return call_bfunc(func_idx, argcount, ectx);
1034 }
1035
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001036 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001037
1038 if (ufunc == NULL)
1039 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001040 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001041
1042 if (script_autoload(name, TRUE))
1043 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001044 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001045
1046 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001047 return FAIL; // bail out if loading the script caused an error
1048 }
1049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001051 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001052 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001053 {
1054 int i;
1055 typval_T *argv = STACK_TV_BOT(0) - argcount;
1056
1057 // The function can change at runtime, check that the argument
1058 // types are correct.
1059 for (i = 0; i < argcount; ++i)
1060 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001061 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001062
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001063 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001064 type = ufunc->uf_arg_types[i];
1065 else if (ufunc->uf_va_type != NULL)
1066 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001067 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001068 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001069 return FAIL;
1070 }
1071 }
1072
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001073 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
1076 return FAIL;
1077}
1078
1079 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001080call_partial(
1081 typval_T *tv,
1082 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001083 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001085 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001086 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001088 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001089 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090
1091 if (tv->v_type == VAR_PARTIAL)
1092 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001093 partial_T *pt = tv->vval.v_partial;
1094 int i;
1095
1096 if (pt->pt_argc > 0)
1097 {
1098 // Make space for arguments from the partial, shift the "argcount"
1099 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001100 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001101 return FAIL;
1102 for (i = 1; i <= argcount; ++i)
1103 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1104 ectx->ec_stack.ga_len += pt->pt_argc;
1105 argcount += pt->pt_argc;
1106
1107 // copy the arguments from the partial onto the stack
1108 for (i = 0; i < pt->pt_argc; ++i)
1109 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1110 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001111 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112
1113 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001114 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 name = pt->pt_name;
1117 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001118 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001120 if (name != NULL)
1121 {
1122 char_u fname_buf[FLEN_FIXED + 1];
1123 char_u *tofree = NULL;
1124 int error = FCERR_NONE;
1125 char_u *fname;
1126
1127 // May need to translate <SNR>123_ to K_SNR.
1128 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1129 if (error != FCERR_NONE)
1130 res = FAIL;
1131 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001132 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001133 vim_free(tofree);
1134 }
1135
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001136 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 {
1138 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001139 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001140 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 return FAIL;
1142 }
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001147 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1148 * TRUE.
1149 */
1150 static int
1151error_if_locked(int lock, char *error)
1152{
1153 if (lock & (VAR_LOCKED | VAR_FIXED))
1154 {
1155 emsg(_(error));
1156 return TRUE;
1157 }
1158 return FALSE;
1159}
1160
1161/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001162 * Give an error if "tv" is not a number and return FAIL.
1163 */
1164 static int
1165check_for_number(typval_T *tv)
1166{
1167 if (tv->v_type != VAR_NUMBER)
1168 {
1169 semsg(_(e_expected_str_but_got_str),
1170 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1171 return FAIL;
1172 }
1173 return OK;
1174}
1175
1176/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001177 * Store "tv" in variable "name".
1178 * This is for s: and g: variables.
1179 */
1180 static void
1181store_var(char_u *name, typval_T *tv)
1182{
1183 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001184 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001185
Bram Moolenaard877a572021-04-01 19:42:48 +02001186 if (tv->v_lock)
1187 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001188 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001189 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001190 restore_funccal();
1191}
1192
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001193/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001194 * Convert "tv" to a string.
1195 * Return FAIL if not allowed.
1196 */
1197 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001198do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001199{
1200 if (tv->v_type != VAR_STRING)
1201 {
1202 char_u *str;
1203
1204 if (is_2string_any)
1205 {
1206 switch (tv->v_type)
1207 {
1208 case VAR_SPECIAL:
1209 case VAR_BOOL:
1210 case VAR_NUMBER:
1211 case VAR_FLOAT:
1212 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001213
1214 case VAR_LIST:
1215 if (tolerant)
1216 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001217 char_u *s, *e, *p;
1218 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001219
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001220 ga_init2(&ga, sizeof(char_u *), 1);
1221
1222 // Convert to NL separated items, then
1223 // escape the items and replace the NL with
1224 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001225 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001226 if (str == NULL)
1227 return FAIL;
1228 s = str;
1229 while ((e = vim_strchr(s, '\n')) != NULL)
1230 {
1231 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001232 p = vim_strsave_fnameescape(s,
1233 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001234 if (p != NULL)
1235 {
1236 ga_concat(&ga, p);
1237 ga_concat(&ga, (char_u *)" ");
1238 vim_free(p);
1239 }
1240 s = e + 1;
1241 }
1242 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001243 clear_tv(tv);
1244 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001245 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001246 return OK;
1247 }
1248 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001249 default: to_string_error(tv->v_type);
1250 return FAIL;
1251 }
1252 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001253 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001254 clear_tv(tv);
1255 tv->v_type = VAR_STRING;
1256 tv->vval.v_string = str;
1257 }
1258 return OK;
1259}
1260
1261/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001262 * When the value of "sv" is a null list of dict, allocate it.
1263 */
1264 static void
1265allocate_if_null(typval_T *tv)
1266{
1267 switch (tv->v_type)
1268 {
1269 case VAR_LIST:
1270 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001271 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001272 break;
1273 case VAR_DICT:
1274 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001275 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001276 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001277 case VAR_BLOB:
1278 if (tv->vval.v_blob == NULL)
1279 (void)rettv_blob_alloc(tv);
1280 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001281 default:
1282 break;
1283 }
1284}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001285
Bram Moolenaare7525c52021-01-09 13:20:37 +01001286/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001287 * Return the character "str[index]" where "index" is the character index,
1288 * including composing characters.
1289 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001290 */
1291 char_u *
1292char_from_string(char_u *str, varnumber_T index)
1293{
1294 size_t nbyte = 0;
1295 varnumber_T nchar = index;
1296 size_t slen;
1297
1298 if (str == NULL)
1299 return NULL;
1300 slen = STRLEN(str);
1301
Bram Moolenaarff871402021-03-26 13:34:05 +01001302 // Do the same as for a list: a negative index counts from the end.
1303 // Optimization to check the first byte to be below 0x80 (and no composing
1304 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001305 if (index < 0)
1306 {
1307 int clen = 0;
1308
1309 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001310 {
1311 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1312 ++nbyte;
1313 else if (enc_utf8)
1314 nbyte += utfc_ptr2len(str + nbyte);
1315 else
1316 nbyte += mb_ptr2len(str + nbyte);
1317 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001318 nchar = clen + index;
1319 if (nchar < 0)
1320 // unlike list: index out of range results in empty string
1321 return NULL;
1322 }
1323
1324 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001325 {
1326 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1327 ++nbyte;
1328 else if (enc_utf8)
1329 nbyte += utfc_ptr2len(str + nbyte);
1330 else
1331 nbyte += mb_ptr2len(str + nbyte);
1332 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001333 if (nbyte >= slen)
1334 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001335 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001336}
1337
1338/*
1339 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001340 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001341 * If going over the end return "str_len".
1342 * If "idx" is negative count from the end, -1 is the last character.
1343 * When going over the start return -1.
1344 */
1345 static long
1346char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1347{
1348 varnumber_T nchar = idx;
1349 size_t nbyte = 0;
1350
1351 if (nchar >= 0)
1352 {
1353 while (nchar > 0 && nbyte < str_len)
1354 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001355 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001356 --nchar;
1357 }
1358 }
1359 else
1360 {
1361 nbyte = str_len;
1362 while (nchar < 0 && nbyte > 0)
1363 {
1364 --nbyte;
1365 nbyte -= mb_head_off(str, str + nbyte);
1366 ++nchar;
1367 }
1368 if (nchar < 0)
1369 return -1;
1370 }
1371 return (long)nbyte;
1372}
1373
1374/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001375 * Return the slice "str[first : last]" using character indexes. Composing
1376 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001377 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001378 * Return NULL when the result is empty.
1379 */
1380 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001381string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001382{
1383 long start_byte, end_byte;
1384 size_t slen;
1385
1386 if (str == NULL)
1387 return NULL;
1388 slen = STRLEN(str);
1389 start_byte = char_idx2byte(str, slen, first);
1390 if (start_byte < 0)
1391 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001392 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001393 end_byte = (long)slen;
1394 else
1395 {
1396 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001397 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001398 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001399 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001400 }
1401
1402 if (start_byte >= (long)slen || end_byte <= start_byte)
1403 return NULL;
1404 return vim_strnsave(str + start_byte, end_byte - start_byte);
1405}
1406
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001407/*
1408 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1409 * When "dfunc_idx" is negative don't give an error.
1410 * Returns NULL for an error.
1411 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001412 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001413get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001414{
1415 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001416 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1417 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001418 svar_T *sv;
1419
1420 if (sref->sref_seq != si->sn_script_seq)
1421 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001422 // The script was reloaded after the function was compiled, the
1423 // script_idx may not be valid.
1424 if (dfunc != NULL)
1425 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1426 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001427 return NULL;
1428 }
1429 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001430 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001431 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001432 if (dfunc != NULL)
1433 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001434 return NULL;
1435 }
1436 return sv;
1437}
1438
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001439/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001440 * Function passed to do_cmdline() for splitting a script joined by NL
1441 * characters.
1442 */
1443 static char_u *
1444get_split_sourceline(
1445 int c UNUSED,
1446 void *cookie,
1447 int indent UNUSED,
1448 getline_opt_T options UNUSED)
1449{
1450 source_cookie_T *sp = (source_cookie_T *)cookie;
1451 char_u *p;
1452 char_u *line;
1453
Bram Moolenaar20677332021-06-06 17:02:53 +02001454 p = vim_strchr(sp->nextline, '\n');
1455 if (p == NULL)
1456 {
1457 line = vim_strsave(sp->nextline);
1458 sp->nextline += STRLEN(sp->nextline);
1459 }
1460 else
1461 {
1462 line = vim_strnsave(sp->nextline, p - sp->nextline);
1463 sp->nextline = p + 1;
1464 }
1465 return line;
1466}
1467
1468/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 * Execute a function by "name".
1470 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001471 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 */
1473 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001474call_eval_func(
1475 char_u *name,
1476 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001477 ectx_T *ectx,
1478 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479{
Bram Moolenaared677f52020-08-12 16:38:10 +02001480 int called_emsg_before = called_emsg;
1481 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001483 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001484 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001486 dictitem_T *v;
1487
1488 v = find_var(name, NULL, FALSE);
1489 if (v == NULL)
1490 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001491 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001492 return FAIL;
1493 }
1494 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1495 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001496 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001497 return FAIL;
1498 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001499 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001501 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502}
1503
1504/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001505 * When a function reference is used, fill a partial with the information
1506 * needed, especially when it is used as a closure.
1507 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001508 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001509fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1510{
1511 pt->pt_func = ufunc;
1512 pt->pt_refcount = 1;
1513
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001514 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001515 {
1516 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1517 + ectx->ec_dfunc_idx;
1518
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001519 // The closure may need to find arguments and local variables in the
1520 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001521 pt->pt_outer.out_stack = &ectx->ec_stack;
1522 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001523 if (ectx->ec_outer_ref != NULL)
1524 {
1525 // The current context already has a context, link to that one.
1526 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1527 if (ectx->ec_outer_ref->or_partial != NULL)
1528 {
1529 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1530 ++pt->pt_outer.out_up_partial->pt_refcount;
1531 }
1532 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001533
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001534 // If this function returns and the closure is still being used, we
1535 // need to make a copy of the context (arguments and local variables).
1536 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001537 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001538 {
1539 vim_free(pt);
1540 return FAIL;
1541 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001542 // Extra variable keeps the count of closures created in the current
1543 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001544 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1545 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1546
1547 ((partial_T **)ectx->ec_funcrefs.ga_data)
1548 [ectx->ec_funcrefs.ga_len] = pt;
1549 ++pt->pt_refcount;
1550 ++ectx->ec_funcrefs.ga_len;
1551 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001552 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001553 return OK;
1554}
1555
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001556/*
1557 * Execute iptr->isn_arg.string as an Ex command.
1558 */
1559 static int
1560exec_command(isn_T *iptr)
1561{
1562 source_cookie_T cookie;
1563
1564 SOURCING_LNUM = iptr->isn_lnum;
1565 // Pass getsourceline to get an error for a missing ":end"
1566 // command.
1567 CLEAR_FIELD(cookie);
1568 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1569 if (do_cmdline(iptr->isn_arg.string,
1570 getsourceline, &cookie,
1571 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1572 || did_emsg)
1573 return FAIL;
1574 return OK;
1575}
1576
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001577// used for v_instr of typval of VAR_INSTR
1578struct instr_S {
1579 ectx_T *instr_ectx;
1580 isn_T *instr_instr;
1581};
1582
Bram Moolenaar4c137212021-04-19 16:48:48 +02001583// used for substitute_instr
1584typedef struct subs_expr_S {
1585 ectx_T *subs_ectx;
1586 isn_T *subs_instr;
1587 int subs_status;
1588} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589
1590// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001591#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
1593// Get pointer to item at the bottom of the stack, -1 is the bottom.
1594#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001595#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 +01001596
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001597// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001598#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 +01001599
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001600// Set when calling do_debug().
1601static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001602static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001603
1604/*
1605 * When debugging lookup "name" and return the typeval.
1606 * When not found return NULL.
1607 */
1608 typval_T *
1609lookup_debug_var(char_u *name)
1610{
1611 int idx;
1612 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001613 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001614 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001615 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001616
1617 if (ectx == NULL)
1618 return NULL;
1619 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1620
1621 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001622 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001623 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001624 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1625
1626 // the variable name may be NULL when not available in this block
1627 if (varname != NULL && STRCMP(varname, 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)
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001914 status = FAIL; // cannot happen?
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001915 else
1916 {
1917 listitem_T *li1 = check_range_index_one(
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001918 tv_dest->vval.v_list, &n1, FALSE);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001919
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
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002001 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002002 if (tv_dest->v_type == VAR_DICT)
2003 {
2004 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002005 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002006 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002007 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 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002023 if (tv_idx->v_type == VAR_STRING)
2024 {
2025 key = tv_idx->vval.v_string;
2026 if (key == NULL)
2027 key = (char_u *)"";
2028 }
2029 else
2030 {
2031 key = tv_get_string(tv_idx);
2032 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002033 if (d != NULL)
2034 di = dict_find(d, key, (int)STRLEN(key));
2035 if (di == NULL)
2036 {
2037 // NULL dict is equivalent to empty dict
2038 semsg(_(e_key_not_present_in_dictionary),
2039 key);
2040 status = FAIL;
2041 }
2042 else if (var_check_fixed(di->di_flags,
2043 NULL, FALSE)
2044 || var_check_ro(di->di_flags,
2045 NULL, FALSE))
2046 status = FAIL;
2047 else
2048 dictitem_remove(d, di);
2049 }
2050 }
2051 }
2052 else if (tv_dest->v_type == VAR_LIST)
2053 {
2054 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002055 if (check_for_number(tv_idx) == FAIL)
2056 {
2057 status = FAIL;
2058 }
2059 else
2060 {
2061 list_T *l = tv_dest->vval.v_list;
2062 long n = (long)tv_idx->vval.v_number;
2063
2064 if (l != NULL && value_check_lock(
2065 l->lv_lock, NULL, FALSE))
2066 status = FAIL;
2067 else
2068 {
2069 listitem_T *li = list_find(l, n);
2070
2071 if (li == NULL)
2072 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002073 semsg(_(e_list_index_out_of_range_nr), n);
2074 status = FAIL;
2075 }
2076 else if (value_check_lock(li->li_tv.v_lock,
2077 NULL, FALSE))
2078 status = FAIL;
2079 else
2080 listitem_remove(l, li);
2081 }
2082 }
2083 }
2084 else
2085 {
2086 status = FAIL;
2087 semsg(_(e_cannot_index_str),
2088 vartype_name(tv_dest->v_type));
2089 }
2090
2091 clear_tv(tv_idx);
2092 clear_tv(tv_dest);
2093 ectx->ec_stack.ga_len -= 2;
2094
2095 return status;
2096}
2097
2098/*
2099 * Unlet a range of items in a list variable.
2100 */
2101 static int
2102execute_unletrange(isn_T *iptr, ectx_T *ectx)
2103{
2104 // Stack contains:
2105 // -3 index1
2106 // -2 index2
2107 // -1 dict or list
2108 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2109 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2110 typval_T *tv_dest = STACK_TV_BOT(-1);
2111 int status = OK;
2112
2113 if (tv_dest->v_type == VAR_LIST)
2114 {
2115 // indexes must be a number
2116 SOURCING_LNUM = iptr->isn_lnum;
2117 if (check_for_number(tv_idx1) == FAIL
2118 || (tv_idx2->v_type != VAR_SPECIAL
2119 && check_for_number(tv_idx2) == FAIL))
2120 {
2121 status = FAIL;
2122 }
2123 else
2124 {
2125 list_T *l = tv_dest->vval.v_list;
2126 long n1 = (long)tv_idx1->vval.v_number;
2127 long n2 = tv_idx2->v_type == VAR_SPECIAL
2128 ? 0 : (long)tv_idx2->vval.v_number;
2129 listitem_T *li;
2130
2131 li = list_find_index(l, &n1);
2132 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002133 {
2134 semsg(_(e_list_index_out_of_range_nr),
2135 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002136 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002137 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002138 else
2139 {
2140 if (n1 < 0)
2141 n1 = list_idx_of_item(l, li);
2142 if (n2 < 0)
2143 {
2144 listitem_T *li2 = list_find(l, n2);
2145
2146 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002147 {
2148 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002149 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002150 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002151 else
2152 n2 = list_idx_of_item(l, li2);
2153 }
2154 if (status != FAIL
2155 && tv_idx2->v_type != VAR_SPECIAL
2156 && n2 < n1)
2157 {
2158 semsg(_(e_list_index_out_of_range_nr), n2);
2159 status = FAIL;
2160 }
2161 if (status != FAIL
2162 && list_unlet_range(l, li, NULL, n1,
2163 tv_idx2->v_type != VAR_SPECIAL, n2)
2164 == FAIL)
2165 status = FAIL;
2166 }
2167 }
2168 }
2169 else
2170 {
2171 status = FAIL;
2172 SOURCING_LNUM = iptr->isn_lnum;
2173 semsg(_(e_cannot_index_str),
2174 vartype_name(tv_dest->v_type));
2175 }
2176
2177 clear_tv(tv_idx1);
2178 clear_tv(tv_idx2);
2179 clear_tv(tv_dest);
2180 ectx->ec_stack.ga_len -= 3;
2181
2182 return status;
2183}
2184
2185/*
2186 * Top of a for loop.
2187 */
2188 static int
2189execute_for(isn_T *iptr, ectx_T *ectx)
2190{
2191 typval_T *tv;
2192 typval_T *ltv = STACK_TV_BOT(-1);
2193 typval_T *idxtv =
2194 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2195
2196 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2197 return FAIL;
2198 if (ltv->v_type == VAR_LIST)
2199 {
2200 list_T *list = ltv->vval.v_list;
2201
2202 // push the next item from the list
2203 ++idxtv->vval.v_number;
2204 if (list == NULL
2205 || idxtv->vval.v_number >= list->lv_len)
2206 {
2207 // past the end of the list, jump to "endfor"
2208 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2209 may_restore_cmdmod(&ectx->ec_funclocal);
2210 }
2211 else if (list->lv_first == &range_list_item)
2212 {
2213 // non-materialized range() list
2214 tv = STACK_TV_BOT(0);
2215 tv->v_type = VAR_NUMBER;
2216 tv->v_lock = 0;
2217 tv->vval.v_number = list_find_nr(
2218 list, idxtv->vval.v_number, NULL);
2219 ++ectx->ec_stack.ga_len;
2220 }
2221 else
2222 {
2223 listitem_T *li = list_find(list,
2224 idxtv->vval.v_number);
2225
2226 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2227 ++ectx->ec_stack.ga_len;
2228 }
2229 }
2230 else if (ltv->v_type == VAR_STRING)
2231 {
2232 char_u *str = ltv->vval.v_string;
2233
2234 // The index is for the last byte of the previous
2235 // character.
2236 ++idxtv->vval.v_number;
2237 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2238 {
2239 // past the end of the string, jump to "endfor"
2240 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2241 may_restore_cmdmod(&ectx->ec_funclocal);
2242 }
2243 else
2244 {
2245 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2246
2247 // Push the next character from the string.
2248 tv = STACK_TV_BOT(0);
2249 tv->v_type = VAR_STRING;
2250 tv->vval.v_string = vim_strnsave(
2251 str + idxtv->vval.v_number, clen);
2252 ++ectx->ec_stack.ga_len;
2253 idxtv->vval.v_number += clen - 1;
2254 }
2255 }
2256 else if (ltv->v_type == VAR_BLOB)
2257 {
2258 blob_T *blob = ltv->vval.v_blob;
2259
2260 // When we get here the first time make a copy of the
2261 // blob, so that the iteration still works when it is
2262 // changed.
2263 if (idxtv->vval.v_number == -1 && blob != NULL)
2264 {
2265 blob_copy(blob, ltv);
2266 blob_unref(blob);
2267 blob = ltv->vval.v_blob;
2268 }
2269
2270 // The index is for the previous byte.
2271 ++idxtv->vval.v_number;
2272 if (blob == NULL
2273 || idxtv->vval.v_number >= blob_len(blob))
2274 {
2275 // past the end of the blob, jump to "endfor"
2276 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2277 may_restore_cmdmod(&ectx->ec_funclocal);
2278 }
2279 else
2280 {
2281 // Push the next byte from the blob.
2282 tv = STACK_TV_BOT(0);
2283 tv->v_type = VAR_NUMBER;
2284 tv->vval.v_number = blob_get(blob,
2285 idxtv->vval.v_number);
2286 ++ectx->ec_stack.ga_len;
2287 }
2288 }
2289 else
2290 {
2291 semsg(_(e_for_loop_on_str_not_supported),
2292 vartype_name(ltv->v_type));
2293 return FAIL;
2294 }
2295 return OK;
2296}
2297
2298/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002299 * Load instruction for w:/b:/g:/t: variable.
2300 * "isn_type" is used instead of "iptr->isn_type".
2301 */
2302 static int
2303load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2304{
2305 dictitem_T *di = NULL;
2306 hashtab_T *ht = NULL;
2307 char namespace;
2308
2309 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2310 return NOTDONE;
2311 switch (isn_type)
2312 {
2313 case ISN_LOADG:
2314 ht = get_globvar_ht();
2315 namespace = 'g';
2316 break;
2317 case ISN_LOADB:
2318 ht = &curbuf->b_vars->dv_hashtab;
2319 namespace = 'b';
2320 break;
2321 case ISN_LOADW:
2322 ht = &curwin->w_vars->dv_hashtab;
2323 namespace = 'w';
2324 break;
2325 case ISN_LOADT:
2326 ht = &curtab->tp_vars->dv_hashtab;
2327 namespace = 't';
2328 break;
2329 default: // Cannot reach here
2330 return NOTDONE;
2331 }
2332 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2333
Bram Moolenaar06b77222022-01-25 15:51:56 +00002334 if (di == NULL)
2335 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002336 if (isn_type == ISN_LOADG)
2337 {
2338 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2339
2340 // g:Something could be a function
2341 if (ufunc != NULL)
2342 {
2343 typval_T *tv = STACK_TV_BOT(0);
2344
2345 ++ectx->ec_stack.ga_len;
2346 tv->v_type = VAR_FUNC;
2347 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2348 if (tv->vval.v_string == NULL)
2349 return FAIL;
2350 STRCPY(tv->vval.v_string, "g:");
2351 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2352 return OK;
2353 }
2354 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002355 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002356 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002357 // no check if the item exists in the script but
2358 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002359 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002360 else
2361 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002362 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002363 return FAIL;
2364 }
2365 else
2366 {
2367 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2368 ++ectx->ec_stack.ga_len;
2369 }
2370 return OK;
2371}
2372
2373/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002374 * Execute instructions in execution context "ectx".
2375 * Return OK or FAIL;
2376 */
2377 static int
2378exec_instructions(ectx_T *ectx)
2379{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002380 int ret = FAIL;
2381 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002382 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002383
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002384 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002385 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002386
Bram Moolenaarff652882021-05-16 15:24:49 +02002387 // Only catch exceptions in this instruction list.
2388 ectx->ec_trylevel_at_start = trylevel;
2389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 for (;;)
2391 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002392 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002394 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002395
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002396 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002397 {
2398 line_breakcheck();
2399 breakcheck_count = 0;
2400 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002401 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002402 {
2403 // Turn CTRL-C into an exception.
2404 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002405 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002406 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002407 did_throw = TRUE;
2408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002410 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002411 {
2412 // Turn an error message into an exception.
2413 did_emsg = FALSE;
2414 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002415 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002416 did_throw = TRUE;
2417 *msg_list = NULL;
2418 }
2419
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002420 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002422 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002423 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002424 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425
2426 // An exception jumps to the first catch, finally, or returns from
2427 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002428 while (index > 0)
2429 {
2430 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002431 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002432 break;
2433 // In the catch and finally block of this try we have to go up
2434 // one level.
2435 --index;
2436 trycmd = NULL;
2437 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002438 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002440 if (trycmd->tcd_in_catch)
2441 {
2442 // exception inside ":catch", jump to ":finally" once
2443 ectx->ec_iidx = trycmd->tcd_finally_idx;
2444 trycmd->tcd_finally_idx = 0;
2445 }
2446 else
2447 // jump to first ":catch"
2448 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002449 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002450 did_throw = FALSE; // don't come back here until :endtry
2451 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 }
2453 else
2454 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002455 // Not inside try or need to return from current functions.
2456 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002457 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002458 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002459 tv = STACK_TV_BOT(0);
2460 tv->v_type = VAR_NUMBER;
2461 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002462 ++ectx->ec_stack.ga_len;
2463 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002465 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002466 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002467 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002468 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 goto done;
2470 }
2471
Bram Moolenaar4c137212021-04-19 16:48:48 +02002472 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002473 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 }
2475 continue;
2476 }
2477
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002478 /*
2479 * Big switch on the instruction. Most compilers will be turning this
2480 * into an efficient lookup table, since the "case" values are an enum
2481 * with sequential numbers. It may look ugly, but it should be the
2482 * most efficient way.
2483 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002484 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 switch (iptr->isn_type)
2486 {
2487 // execute Ex command line
2488 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002489 if (exec_command(iptr) == FAIL)
2490 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 break;
2492
Bram Moolenaar20677332021-06-06 17:02:53 +02002493 // execute Ex command line split at NL characters.
2494 case ISN_EXEC_SPLIT:
2495 {
2496 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002497 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002498
2499 SOURCING_LNUM = iptr->isn_lnum;
2500 CLEAR_FIELD(cookie);
2501 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2502 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002503 line = get_split_sourceline(0, &cookie, 0, 0);
2504 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002505 get_split_sourceline, &cookie,
2506 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2507 == FAIL
2508 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002509 {
2510 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002511 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002512 }
2513 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002514 }
2515 break;
2516
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002517 // execute Ex command line that is only a range
2518 case ISN_EXECRANGE:
2519 {
2520 exarg_T ea;
2521 char *error = NULL;
2522
2523 CLEAR_FIELD(ea);
2524 ea.cmdidx = CMD_SIZE;
2525 ea.addr_type = ADDR_LINES;
2526 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002527 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002528 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002529 if (ea.cmd == NULL)
2530 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002531 // error is always NULL when using ADDR_LINES
2532 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002533 if (error != NULL)
2534 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002535 emsg(error);
2536 goto on_error;
2537 }
2538 }
2539 break;
2540
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002541 // Evaluate an expression with legacy syntax, push it onto the
2542 // stack.
2543 case ISN_LEGACY_EVAL:
2544 {
2545 char_u *arg = iptr->isn_arg.string;
2546 int res;
2547 int save_flags = cmdmod.cmod_flags;
2548
Bram Moolenaar35578162021-08-02 19:10:38 +02002549 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002550 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002551 tv = STACK_TV_BOT(0);
2552 init_tv(tv);
2553 cmdmod.cmod_flags |= CMOD_LEGACY;
2554 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2555 cmdmod.cmod_flags = save_flags;
2556 if (res == FAIL)
2557 goto on_error;
2558 ++ectx->ec_stack.ga_len;
2559 }
2560 break;
2561
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002562 // push typeval VAR_INSTR with instructions to be executed
2563 case ISN_INSTR:
2564 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002565 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002566 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002567 tv = STACK_TV_BOT(0);
2568 tv->vval.v_instr = ALLOC_ONE(instr_T);
2569 if (tv->vval.v_instr == NULL)
2570 goto on_error;
2571 ++ectx->ec_stack.ga_len;
2572
2573 tv->v_type = VAR_INSTR;
2574 tv->vval.v_instr->instr_ectx = ectx;
2575 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2576 }
2577 break;
2578
Bram Moolenaar4c137212021-04-19 16:48:48 +02002579 // execute :substitute with an expression
2580 case ISN_SUBSTITUTE:
2581 {
2582 subs_T *subs = &iptr->isn_arg.subs;
2583 source_cookie_T cookie;
2584 struct subs_expr_S *save_instr = substitute_instr;
2585 struct subs_expr_S subs_instr;
2586 int res;
2587
2588 subs_instr.subs_ectx = ectx;
2589 subs_instr.subs_instr = subs->subs_instr;
2590 subs_instr.subs_status = OK;
2591 substitute_instr = &subs_instr;
2592
2593 SOURCING_LNUM = iptr->isn_lnum;
2594 // This is very much like ISN_EXEC
2595 CLEAR_FIELD(cookie);
2596 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2597 res = do_cmdline(subs->subs_cmd,
2598 getsourceline, &cookie,
2599 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2600 substitute_instr = save_instr;
2601
2602 if (res == FAIL || did_emsg
2603 || subs_instr.subs_status == FAIL)
2604 goto on_error;
2605 }
2606 break;
2607
2608 case ISN_FINISH:
2609 goto done;
2610
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002611 case ISN_REDIRSTART:
2612 // create a dummy entry for var_redir_str()
2613 if (alloc_redir_lval() == FAIL)
2614 goto on_error;
2615
2616 // The output is stored in growarray "redir_ga" until
2617 // redirection ends.
2618 init_redir_ga();
2619 redir_vname = 1;
2620 break;
2621
2622 case ISN_REDIREND:
2623 {
2624 char_u *res = get_clear_redir_ga();
2625
2626 // End redirection, put redirected text on the stack.
2627 clear_redir_lval();
2628 redir_vname = 0;
2629
Bram Moolenaar35578162021-08-02 19:10:38 +02002630 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002631 {
2632 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002633 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002634 }
2635 tv = STACK_TV_BOT(0);
2636 tv->v_type = VAR_STRING;
2637 tv->vval.v_string = res;
2638 ++ectx->ec_stack.ga_len;
2639 }
2640 break;
2641
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002642 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002643#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002644 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2645 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002646#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002647 break;
2648
2649 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002650#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002651 {
2652 exarg_T ea;
2653 int res;
2654
2655 CLEAR_FIELD(ea);
2656 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2657 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2658 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2659 --ectx->ec_stack.ga_len;
2660 tv = STACK_TV_BOT(0);
2661 res = cexpr_core(&ea, tv);
2662 clear_tv(tv);
2663 if (res == FAIL)
2664 goto on_error;
2665 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002666#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002667 break;
2668
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002669 // execute Ex command from pieces on the stack
2670 case ISN_EXECCONCAT:
2671 {
2672 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002673 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002674 int pass;
2675 int i;
2676 char_u *cmd = NULL;
2677 char_u *str;
2678
2679 for (pass = 1; pass <= 2; ++pass)
2680 {
2681 for (i = 0; i < count; ++i)
2682 {
2683 tv = STACK_TV_BOT(i - count);
2684 str = tv->vval.v_string;
2685 if (str != NULL && *str != NUL)
2686 {
2687 if (pass == 2)
2688 STRCPY(cmd + len, str);
2689 len += STRLEN(str);
2690 }
2691 if (pass == 2)
2692 clear_tv(tv);
2693 }
2694 if (pass == 1)
2695 {
2696 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002697 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002698 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002699 len = 0;
2700 }
2701 }
2702
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002703 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002704 do_cmdline_cmd(cmd);
2705 vim_free(cmd);
2706 }
2707 break;
2708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 // execute :echo {string} ...
2710 case ISN_ECHO:
2711 {
2712 int count = iptr->isn_arg.echo.echo_count;
2713 int atstart = TRUE;
2714 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002715 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716
2717 for (idx = 0; idx < count; ++idx)
2718 {
2719 tv = STACK_TV_BOT(idx - count);
2720 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2721 &atstart, &needclr);
2722 clear_tv(tv);
2723 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002724 if (needclr)
2725 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002726 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 }
2728 break;
2729
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002730 // :execute {string} ...
2731 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002732 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002733 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002734 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002735 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002736 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002737 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002738 {
2739 int count = iptr->isn_arg.number;
2740 garray_T ga;
2741 char_u buf[NUMBUFLEN];
2742 char_u *p;
2743 int len;
2744 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002745 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002746
2747 ga_init2(&ga, 1, 80);
2748 for (idx = 0; idx < count; ++idx)
2749 {
2750 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002751 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002752 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002753 if (tv->v_type == VAR_CHANNEL
2754 || tv->v_type == VAR_JOB)
2755 {
2756 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002757 semsg(_(e_using_invalid_value_as_string_str),
2758 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002759 break;
2760 }
2761 else
2762 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002763 }
2764 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002765 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002766
2767 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002768 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002769 failed = TRUE;
2770 else
2771 {
2772 if (ga.ga_len > 0)
2773 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2774 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2775 ga.ga_len += len;
2776 }
2777 clear_tv(tv);
2778 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002779 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002780 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002781 {
2782 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002783 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002784 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002785
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002786 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002787 {
2788 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002789 {
2790 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002791 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002792 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002793 {
2794 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002795 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002796 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002797 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002798 else
2799 {
2800 msg_sb_eol();
2801 if (iptr->isn_type == ISN_ECHOMSG)
2802 {
2803 msg_attr(ga.ga_data, echo_attr);
2804 out_flush();
2805 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002806 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2807 {
2808 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2809 TRUE);
2810 ui_write((char_u *)"\r\n", 2, TRUE);
2811 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002812 else
2813 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002814 SOURCING_LNUM = iptr->isn_lnum;
2815 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002816 }
2817 }
2818 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002819 ga_clear(&ga);
2820 }
2821 break;
2822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 // load local variable or argument
2824 case ISN_LOAD:
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(STACK_TV_VAR(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
2831 // load v: variable
2832 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002833 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002834 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002836 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 break;
2838
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002839 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 case ISN_LOADSCRIPT:
2841 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002842 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 svar_T *sv;
2844
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002845 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002846 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002847 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002848 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002849 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002850 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002852 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 }
2854 break;
2855
2856 // load s: variable in old script
2857 case ISN_LOADS:
2858 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002859 hashtab_T *ht = &SCRIPT_VARS(
2860 iptr->isn_arg.loadstore.ls_sid);
2861 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002863
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 if (di == NULL)
2865 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002866 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002867 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002868 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 }
2870 else
2871 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002872 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002873 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002875 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 }
2877 }
2878 break;
2879
Bram Moolenaard3aac292020-04-19 14:32:17 +02002880 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002882 case ISN_LOADB:
2883 case ISN_LOADW:
2884 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002886 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002887
Bram Moolenaar06b77222022-01-25 15:51:56 +00002888 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002889 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002890 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002891 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 break;
2895
Bram Moolenaar03290b82020-12-19 16:30:44 +01002896 // load autoload variable
2897 case ISN_LOADAUTO:
2898 {
2899 char_u *name = iptr->isn_arg.string;
2900
Bram Moolenaar35578162021-08-02 19:10:38 +02002901 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002902 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002903 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002904 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002905 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002906 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002907 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002908 }
2909 break;
2910
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002911 // load g:/b:/w:/t: namespace
2912 case ISN_LOADGDICT:
2913 case ISN_LOADBDICT:
2914 case ISN_LOADWDICT:
2915 case ISN_LOADTDICT:
2916 {
2917 dict_T *d = NULL;
2918
2919 switch (iptr->isn_type)
2920 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002921 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2922 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2923 case ISN_LOADWDICT: d = curwin->w_vars; break;
2924 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002925 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002926 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002927 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002928 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002929 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002930 tv = STACK_TV_BOT(0);
2931 tv->v_type = VAR_DICT;
2932 tv->v_lock = 0;
2933 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002934 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002935 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002936 }
2937 break;
2938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 // load &option
2940 case ISN_LOADOPT:
2941 {
2942 typval_T optval;
2943 char_u *name = iptr->isn_arg.string;
2944
Bram Moolenaara8c17702020-04-01 21:17:24 +02002945 // This is not expected to fail, name is checked during
2946 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002947 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002948 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002949 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002950 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002952 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 }
2954 break;
2955
2956 // load $ENV
2957 case ISN_LOADENV:
2958 {
2959 typval_T optval;
2960 char_u *name = iptr->isn_arg.string;
2961
Bram Moolenaar35578162021-08-02 19:10:38 +02002962 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002963 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002964 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002965 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002967 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 }
2969 break;
2970
2971 // load @register
2972 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002973 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002974 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 tv = STACK_TV_BOT(0);
2976 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002977 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002978 // This may result in NULL, which should be equivalent to an
2979 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 tv->vval.v_string = get_reg_contents(
2981 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002982 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 break;
2984
2985 // store local variable
2986 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002987 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 tv = STACK_TV_VAR(iptr->isn_arg.number);
2989 clear_tv(tv);
2990 *tv = *STACK_TV_BOT(0);
2991 break;
2992
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002993 // store s: variable in old script
2994 case ISN_STORES:
2995 {
2996 hashtab_T *ht = &SCRIPT_VARS(
2997 iptr->isn_arg.loadstore.ls_sid);
2998 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002999 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003000
Bram Moolenaar4c137212021-04-19 16:48:48 +02003001 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003002 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003003 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003004 else
3005 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003006 SOURCING_LNUM = iptr->isn_lnum;
3007 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003008 {
3009 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003010 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003011 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003012 clear_tv(&di->di_tv);
3013 di->di_tv = *STACK_TV_BOT(0);
3014 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003015 }
3016 break;
3017
3018 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 case ISN_STORESCRIPT:
3020 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003021 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3022 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003024 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003025 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003026 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003028
3029 // "const" and "final" are checked at compile time, locking
3030 // the value needs to be checked here.
3031 SOURCING_LNUM = iptr->isn_lnum;
3032 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003033 {
3034 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003035 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003036 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 clear_tv(sv->sv_tv);
3039 *sv->sv_tv = *STACK_TV_BOT(0);
3040 }
3041 break;
3042
3043 // store option
3044 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003045 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003047 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3048 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 long n = 0;
3050 char_u *s = NULL;
3051 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003052 char_u numbuf[NUMBUFLEN];
3053 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054
Bram Moolenaar4c137212021-04-19 16:48:48 +02003055 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 tv = STACK_TV_BOT(0);
3057 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003058 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003060 if (s == NULL)
3061 s = (char_u *)"";
3062 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003063 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3064 {
3065 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003066 // If the option can be set to a function reference or
3067 // a lambda and the passed value is a function
3068 // reference, then convert it to the name (string) of
3069 // the function reference.
3070 s = tv2string(tv, &tofree, numbuf, 0);
3071 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003072 {
3073 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003074 goto on_error;
3075 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003078 // must be VAR_NUMBER, CHECKTYPE makes sure
3079 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003080 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003081 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003082 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 if (msg != NULL)
3084 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003085 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003087 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 }
3090 break;
3091
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003092 // store $ENV
3093 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003094 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003095 tv = STACK_TV_BOT(0);
3096 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3097 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003098 break;
3099
3100 // store @r
3101 case ISN_STOREREG:
3102 {
3103 int reg = iptr->isn_arg.number;
3104
Bram Moolenaar4c137212021-04-19 16:48:48 +02003105 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003106 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003107 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003108 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003109 }
3110 break;
3111
3112 // store v: variable
3113 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003114 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003115 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3116 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003117 // should not happen, type is checked when compiling
3118 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003119 break;
3120
Bram Moolenaard3aac292020-04-19 14:32:17 +02003121 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003123 case ISN_STOREB:
3124 case ISN_STOREW:
3125 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003127 dictitem_T *di;
3128 hashtab_T *ht;
3129 char_u *name = iptr->isn_arg.string + 2;
3130
Bram Moolenaard3aac292020-04-19 14:32:17 +02003131 switch (iptr->isn_type)
3132 {
3133 case ISN_STOREG:
3134 ht = get_globvar_ht();
3135 break;
3136 case ISN_STOREB:
3137 ht = &curbuf->b_vars->dv_hashtab;
3138 break;
3139 case ISN_STOREW:
3140 ht = &curwin->w_vars->dv_hashtab;
3141 break;
3142 case ISN_STORET:
3143 ht = &curtab->tp_vars->dv_hashtab;
3144 break;
3145 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003146 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148
Bram Moolenaar4c137212021-04-19 16:48:48 +02003149 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003150 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003152 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 else
3154 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003155 SOURCING_LNUM = iptr->isn_lnum;
3156 if (var_check_permission(di, name) == FAIL)
3157 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 clear_tv(&di->di_tv);
3159 di->di_tv = *STACK_TV_BOT(0);
3160 }
3161 }
3162 break;
3163
Bram Moolenaar03290b82020-12-19 16:30:44 +01003164 // store an autoload variable
3165 case ISN_STOREAUTO:
3166 SOURCING_LNUM = iptr->isn_lnum;
3167 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3168 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003169 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003170 break;
3171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 // store number in local variable
3173 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003174 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 clear_tv(tv);
3176 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003177 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 break;
3179
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003180 // store value in list or dict variable
3181 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003182 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003183 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003184
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003185 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003186 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003187 if (res == NOTDONE)
3188 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003189 }
3190 break;
3191
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003192 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003193 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003194 if (execute_storerange(iptr, ectx) == FAIL)
3195 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003196 break;
3197
Bram Moolenaar0186e582021-01-10 18:33:11 +01003198 // load or store variable or argument from outer scope
3199 case ISN_LOADOUTER:
3200 case ISN_STOREOUTER:
3201 {
3202 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003203 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3204 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003205
3206 while (depth > 1 && outer != NULL)
3207 {
3208 outer = outer->out_up;
3209 --depth;
3210 }
3211 if (outer == NULL)
3212 {
3213 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003214 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3215 || ectx->ec_outer_ref == NULL)
3216 // Possibly :def function called from legacy
3217 // context.
3218 emsg(_(e_closure_called_from_invalid_context));
3219 else
3220 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003221 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003222 }
3223 tv = ((typval_T *)outer->out_stack->ga_data)
3224 + outer->out_frame_idx + STACK_FRAME_SIZE
3225 + iptr->isn_arg.outer.outer_idx;
3226 if (iptr->isn_type == ISN_LOADOUTER)
3227 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003228 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003229 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003230 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003231 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003232 }
3233 else
3234 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003235 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003236 clear_tv(tv);
3237 *tv = *STACK_TV_BOT(0);
3238 }
3239 }
3240 break;
3241
Bram Moolenaar752fc692021-01-04 21:57:11 +01003242 // unlet item in list or dict variable
3243 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003244 if (execute_unletindex(iptr, ectx) == FAIL)
3245 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003246 break;
3247
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003248 // unlet range of items in list variable
3249 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003250 if (execute_unletrange(iptr, ectx) == FAIL)
3251 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003252 break;
3253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 // push constant
3255 case ISN_PUSHNR:
3256 case ISN_PUSHBOOL:
3257 case ISN_PUSHSPEC:
3258 case ISN_PUSHF:
3259 case ISN_PUSHS:
3260 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003261 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003262 case ISN_PUSHCHANNEL:
3263 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003264 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003265 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003267 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003268 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 switch (iptr->isn_type)
3270 {
3271 case ISN_PUSHNR:
3272 tv->v_type = VAR_NUMBER;
3273 tv->vval.v_number = iptr->isn_arg.number;
3274 break;
3275 case ISN_PUSHBOOL:
3276 tv->v_type = VAR_BOOL;
3277 tv->vval.v_number = iptr->isn_arg.number;
3278 break;
3279 case ISN_PUSHSPEC:
3280 tv->v_type = VAR_SPECIAL;
3281 tv->vval.v_number = iptr->isn_arg.number;
3282 break;
3283#ifdef FEAT_FLOAT
3284 case ISN_PUSHF:
3285 tv->v_type = VAR_FLOAT;
3286 tv->vval.v_float = iptr->isn_arg.fnumber;
3287 break;
3288#endif
3289 case ISN_PUSHBLOB:
3290 blob_copy(iptr->isn_arg.blob, tv);
3291 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003292 case ISN_PUSHFUNC:
3293 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003294 if (iptr->isn_arg.string == NULL)
3295 tv->vval.v_string = NULL;
3296 else
3297 tv->vval.v_string =
3298 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003299 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003300 case ISN_PUSHCHANNEL:
3301#ifdef FEAT_JOB_CHANNEL
3302 tv->v_type = VAR_CHANNEL;
3303 tv->vval.v_channel = iptr->isn_arg.channel;
3304 if (tv->vval.v_channel != NULL)
3305 ++tv->vval.v_channel->ch_refcount;
3306#endif
3307 break;
3308 case ISN_PUSHJOB:
3309#ifdef FEAT_JOB_CHANNEL
3310 tv->v_type = VAR_JOB;
3311 tv->vval.v_job = iptr->isn_arg.job;
3312 if (tv->vval.v_job != NULL)
3313 ++tv->vval.v_job->jv_refcount;
3314#endif
3315 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 default:
3317 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003318 tv->vval.v_string = iptr->isn_arg.string == NULL
3319 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 }
3321 break;
3322
Bram Moolenaar06b77222022-01-25 15:51:56 +00003323 case ISN_AUTOLOAD:
3324 {
3325 char_u *name = iptr->isn_arg.string;
3326
3327 (void)script_autoload(name, FALSE);
3328 if (find_func(name, TRUE))
3329 {
3330 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3331 goto theend;
3332 tv = STACK_TV_BOT(0);
3333 tv->v_lock = 0;
3334 ++ectx->ec_stack.ga_len;
3335 tv->v_type = VAR_FUNC;
3336 tv->vval.v_string = vim_strsave(name);
3337 }
3338 else
3339 {
3340 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3341
3342 if (res == NOTDONE)
3343 goto theend;
3344 if (res == FAIL)
3345 goto on_error;
3346 }
3347 }
3348 break;
3349
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003350 case ISN_UNLET:
3351 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3352 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003353 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003354 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003355 case ISN_UNLETENV:
3356 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3357 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003358
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003359 case ISN_LOCKUNLOCK:
3360 {
3361 typval_T *lval_root_save = lval_root;
3362 int res;
3363
3364 // Stack has the local variable, argument the whole :lock
3365 // or :unlock command, like ISN_EXEC.
3366 --ectx->ec_stack.ga_len;
3367 lval_root = STACK_TV_BOT(0);
3368 res = exec_command(iptr);
3369 clear_tv(lval_root);
3370 lval_root = lval_root_save;
3371 if (res == FAIL)
3372 goto on_error;
3373 }
3374 break;
3375
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003376 case ISN_LOCKCONST:
3377 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3378 break;
3379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 // create a list from items on the stack; uses a single allocation
3381 // for the list header and the items
3382 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003383 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003384 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 break;
3386
3387 // create a dict from items on the stack
3388 case ISN_NEWDICT:
3389 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003390 int count = iptr->isn_arg.number;
3391 dict_T *dict = dict_alloc();
3392 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003393 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003394 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003396 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003397 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 for (idx = 0; idx < count; ++idx)
3399 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003400 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003402 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003403 key = tv->vval.v_string == NULL
3404 ? (char_u *)"" : tv->vval.v_string;
3405 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003406 if (item != NULL)
3407 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003408 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar74409f62022-01-01 15:58:22 +00003409 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003410 dict_unref(dict);
3411 goto on_error;
3412 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003413 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003415 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003416 {
3417 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003418 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3421 item->di_tv.v_lock = 0;
3422 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003423 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003424 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003425 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003426 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 }
3429
3430 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003431 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003432 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003433 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003435 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 tv = STACK_TV_BOT(-1);
3437 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003438 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 tv->vval.v_dict = dict;
3440 ++dict->dv_refcount;
3441 }
3442 break;
3443
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003444 // create a partial with NULL value
3445 case ISN_NEWPARTIAL:
3446 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3447 goto theend;
3448 ++ectx->ec_stack.ga_len;
3449 tv = STACK_TV_BOT(-1);
3450 tv->v_type = VAR_PARTIAL;
3451 tv->v_lock = 0;
3452 tv->vval.v_partial = NULL;
3453 break;
3454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 // call a :def function
3456 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003457 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003458 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3459 NULL,
3460 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003461 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003462 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 break;
3464
3465 // call a builtin function
3466 case ISN_BCALL:
3467 SOURCING_LNUM = iptr->isn_lnum;
3468 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3469 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003470 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003471 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 break;
3473
3474 // call a funcref or partial
3475 case ISN_PCALL:
3476 {
3477 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3478 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003479 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480
3481 SOURCING_LNUM = iptr->isn_lnum;
3482 if (pfunc->cpf_top)
3483 {
3484 // funcref is above the arguments
3485 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3486 }
3487 else
3488 {
3489 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003490 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003491 partial_tv = *STACK_TV_BOT(0);
3492 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003494 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003495 if (tv == &partial_tv)
3496 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003498 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 }
3500 break;
3501
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003502 case ISN_PCALL_END:
3503 // PCALL finished, arguments have been consumed and replaced by
3504 // the return value. Now clear the funcref from the stack,
3505 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003506 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003507 clear_tv(STACK_TV_BOT(-1));
3508 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3509 break;
3510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 // call a user defined function or funcref/partial
3512 case ISN_UCALL:
3513 {
3514 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3515
3516 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003517 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003518 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003519 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 }
3521 break;
3522
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003523 // return from a :def function call without a value
3524 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003525 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003526 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003527 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003528 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003529 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003530 tv->vval.v_number = 0;
3531 tv->v_lock = 0;
3532 // FALLTHROUGH
3533
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003534 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 case ISN_RETURN:
3536 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003537 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003538 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003539
3540 if (trystack->ga_len > 0)
3541 trycmd = ((trycmd_T *)trystack->ga_data)
3542 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003543 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003544 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003546 // jump to ":finally" or ":endtry"
3547 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003548 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003549 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003550 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 trycmd->tcd_return = TRUE;
3552 }
3553 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003554 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 }
3556 break;
3557
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003558 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 case ISN_FUNCREF:
3560 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003561 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003562 ufunc_T *ufunc;
3563 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003566 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003567 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003568 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003569 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003570 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003571 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003572 if (funcref->fr_func_name == NULL)
3573 {
3574 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3575 + funcref->fr_dfunc_idx;
3576
3577 ufunc = pt_dfunc->df_ufunc;
3578 }
3579 else
3580 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003581 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003582 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003583 if (ufunc == NULL)
3584 {
3585 SOURCING_LNUM = iptr->isn_lnum;
3586 iemsg("ufunc unexpectedly NULL for FUNCREF");
3587 goto theend;
3588 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003589 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003590 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003592 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 tv->vval.v_partial = pt;
3594 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003595 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 }
3597 break;
3598
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003599 // Create a global function from a lambda.
3600 case ISN_NEWFUNC:
3601 {
3602 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3603
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003604 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003605 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003606 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003607 }
3608 break;
3609
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003610 // List functions
3611 case ISN_DEF:
3612 if (iptr->isn_arg.string == NULL)
3613 list_functions(NULL);
3614 else
3615 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003616 exarg_T ea;
3617 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003618
3619 CLEAR_FIELD(ea);
3620 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003621 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3622 define_function(&ea, NULL, &lines_to_free);
3623 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003624 }
3625 break;
3626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 // jump if a condition is met
3628 case ISN_JUMP:
3629 {
3630 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003631 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 int jump = TRUE;
3633
3634 if (when != JUMP_ALWAYS)
3635 {
3636 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003637 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003638 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003639 || when == JUMP_IF_COND_TRUE)
3640 {
3641 SOURCING_LNUM = iptr->isn_lnum;
3642 jump = tv_get_bool_chk(tv, &error);
3643 if (error)
3644 goto on_error;
3645 }
3646 else
3647 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003649 || when == JUMP_AND_KEEP_IF_FALSE
3650 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003652 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 {
3654 // drop the value from the stack
3655 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003656 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 }
3658 }
3659 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003660 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
3662 break;
3663
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003664 // Jump if an argument with a default value was already set and not
3665 // v:none.
3666 case ISN_JUMP_IF_ARG_SET:
3667 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3668 if (tv->v_type != VAR_UNKNOWN
3669 && !(tv->v_type == VAR_SPECIAL
3670 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003671 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003672 break;
3673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 // top of a for loop
3675 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003676 if (execute_for(iptr, ectx) == FAIL)
3677 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 break;
3679
3680 // start of ":try" block
3681 case ISN_TRY:
3682 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003683 trycmd_T *trycmd = NULL;
3684
Bram Moolenaar35578162021-08-02 19:10:38 +02003685 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003686 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003687 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3688 + ectx->ec_trystack.ga_len;
3689 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003691 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003692 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3693 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003694 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003695 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003696 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003697 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003698 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003699 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 }
3701 break;
3702
3703 case ISN_PUSHEXC:
3704 if (current_exception == NULL)
3705 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003706 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003708 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003710 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003711 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003713 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003715 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 tv->vval.v_string = vim_strsave(
3717 (char_u *)current_exception->value);
3718 break;
3719
3720 case ISN_CATCH:
3721 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003722 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723
Bram Moolenaar4c137212021-04-19 16:48:48 +02003724 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 if (trystack->ga_len > 0)
3726 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003727 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 + trystack->ga_len - 1;
3729 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003730 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 }
3732 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003733 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 catch_exception(current_exception);
3735 }
3736 break;
3737
Bram Moolenaarc150c092021-02-13 15:02:46 +01003738 case ISN_TRYCONT:
3739 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003740 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003741 trycont_T *trycont = &iptr->isn_arg.trycont;
3742 int i;
3743 trycmd_T *trycmd;
3744 int iidx = trycont->tct_where;
3745
3746 if (trystack->ga_len < trycont->tct_levels)
3747 {
3748 siemsg("TRYCONT: expected %d levels, found %d",
3749 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003750 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003751 }
3752 // Make :endtry jump to any outer try block and the last
3753 // :endtry inside the loop to the loop start.
3754 for (i = trycont->tct_levels; i > 0; --i)
3755 {
3756 trycmd = ((trycmd_T *)trystack->ga_data)
3757 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003758 // Add one to tcd_cont to be able to jump to
3759 // instruction with index zero.
3760 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003761 iidx = trycmd->tcd_finally_idx == 0
3762 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003763 }
3764 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003765 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003766 }
3767 break;
3768
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003769 case ISN_FINALLY:
3770 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003771 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003772 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3773 + trystack->ga_len - 1;
3774
3775 // Reset the index to avoid a return statement jumps here
3776 // again.
3777 trycmd->tcd_finally_idx = 0;
3778 break;
3779 }
3780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 // end of ":try" block
3782 case ISN_ENDTRY:
3783 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785
3786 if (trystack->ga_len > 0)
3787 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003788 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 --trystack->ga_len;
3791 --trylevel;
3792 trycmd = ((trycmd_T *)trystack->ga_data)
3793 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003794 if (trycmd->tcd_did_throw)
3795 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003796 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 {
3798 // discard the exception
3799 if (caught_stack == current_exception)
3800 caught_stack = caught_stack->caught;
3801 discard_current_exception();
3802 }
3803
3804 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003805 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003806
Bram Moolenaar4c137212021-04-19 16:48:48 +02003807 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003808 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003809 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003810 clear_tv(STACK_TV_BOT(0));
3811 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003812 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003813 // handling :continue: jump to outer try block or
3814 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003815 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 }
3817 }
3818 break;
3819
3820 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003821 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003822 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003823
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003824 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3825 {
3826 // throwing an exception while using "silent!" causes
3827 // the function to abort but not display an error.
3828 tv = STACK_TV_BOT(-1);
3829 clear_tv(tv);
3830 tv->v_type = VAR_NUMBER;
3831 tv->vval.v_number = 0;
3832 goto done;
3833 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003834 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003835 tv = STACK_TV_BOT(0);
3836 if (tv->vval.v_string == NULL
3837 || *skipwhite(tv->vval.v_string) == NUL)
3838 {
3839 vim_free(tv->vval.v_string);
3840 SOURCING_LNUM = iptr->isn_lnum;
3841 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003842 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003843 }
3844
3845 // Inside a "catch" we need to first discard the caught
3846 // exception.
3847 if (trystack->ga_len > 0)
3848 {
3849 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3850 + trystack->ga_len - 1;
3851 if (trycmd->tcd_caught && current_exception != NULL)
3852 {
3853 // discard the exception
3854 if (caught_stack == current_exception)
3855 caught_stack = caught_stack->caught;
3856 discard_current_exception();
3857 trycmd->tcd_caught = FALSE;
3858 }
3859 }
3860
Bram Moolenaar90a57162022-02-12 14:23:17 +00003861 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003862 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3863 == FAIL)
3864 {
3865 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003866 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003867 }
3868 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 break;
3871
3872 // compare with special values
3873 case ISN_COMPAREBOOL:
3874 case ISN_COMPARESPECIAL:
3875 {
3876 typval_T *tv1 = STACK_TV_BOT(-2);
3877 typval_T *tv2 = STACK_TV_BOT(-1);
3878 varnumber_T arg1 = tv1->vval.v_number;
3879 varnumber_T arg2 = tv2->vval.v_number;
3880 int res;
3881
3882 switch (iptr->isn_arg.op.op_type)
3883 {
3884 case EXPR_EQUAL: res = arg1 == arg2; break;
3885 case EXPR_NEQUAL: res = arg1 != arg2; break;
3886 default: res = 0; break;
3887 }
3888
Bram Moolenaar4c137212021-04-19 16:48:48 +02003889 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 tv1->v_type = VAR_BOOL;
3891 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3892 }
3893 break;
3894
Bram Moolenaar7a222242022-03-01 19:23:24 +00003895 case ISN_COMPARENULL:
3896 {
3897 typval_T *tv1 = STACK_TV_BOT(-2);
3898 typval_T *tv2 = STACK_TV_BOT(-1);
3899 int res;
3900
3901 res = typval_compare_null(tv1, tv2);
3902 if (res == MAYBE)
3903 goto on_error;
3904 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3905 res = !res;
3906 clear_tv(tv1);
3907 clear_tv(tv2);
3908 --ectx->ec_stack.ga_len;
3909 tv1->v_type = VAR_BOOL;
3910 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3911 }
3912 break;
3913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 // Operation with two number arguments
3915 case ISN_OPNR:
3916 case ISN_COMPARENR:
3917 {
3918 typval_T *tv1 = STACK_TV_BOT(-2);
3919 typval_T *tv2 = STACK_TV_BOT(-1);
3920 varnumber_T arg1 = tv1->vval.v_number;
3921 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003922 varnumber_T res = 0;
3923 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924
3925 switch (iptr->isn_arg.op.op_type)
3926 {
3927 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003928 case EXPR_DIV: if (arg2 == 0)
3929 div_zero = TRUE;
3930 else
3931 res = arg1 / arg2;
3932 break;
3933 case EXPR_REM: if (arg2 == 0)
3934 div_zero = TRUE;
3935 else
3936 res = arg1 % arg2;
3937 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 case EXPR_SUB: res = arg1 - arg2; break;
3939 case EXPR_ADD: res = arg1 + arg2; break;
3940
3941 case EXPR_EQUAL: res = arg1 == arg2; break;
3942 case EXPR_NEQUAL: res = arg1 != arg2; break;
3943 case EXPR_GREATER: res = arg1 > arg2; break;
3944 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3945 case EXPR_SMALLER: res = arg1 < arg2; break;
3946 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003947 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
3949
Bram Moolenaar4c137212021-04-19 16:48:48 +02003950 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 if (iptr->isn_type == ISN_COMPARENR)
3952 {
3953 tv1->v_type = VAR_BOOL;
3954 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3955 }
3956 else
3957 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003958 if (div_zero)
3959 {
3960 SOURCING_LNUM = iptr->isn_lnum;
3961 emsg(_(e_divide_by_zero));
3962 goto on_error;
3963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 }
3965 break;
3966
3967 // Computation with two float arguments
3968 case ISN_OPFLOAT:
3969 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003970#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 {
3972 typval_T *tv1 = STACK_TV_BOT(-2);
3973 typval_T *tv2 = STACK_TV_BOT(-1);
3974 float_T arg1 = tv1->vval.v_float;
3975 float_T arg2 = tv2->vval.v_float;
3976 float_T res = 0;
3977 int cmp = FALSE;
3978
3979 switch (iptr->isn_arg.op.op_type)
3980 {
3981 case EXPR_MULT: res = arg1 * arg2; break;
3982 case EXPR_DIV: res = arg1 / arg2; break;
3983 case EXPR_SUB: res = arg1 - arg2; break;
3984 case EXPR_ADD: res = arg1 + arg2; break;
3985
3986 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3987 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3988 case EXPR_GREATER: cmp = arg1 > arg2; break;
3989 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3990 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3991 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3992 default: cmp = 0; break;
3993 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003994 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 if (iptr->isn_type == ISN_COMPAREFLOAT)
3996 {
3997 tv1->v_type = VAR_BOOL;
3998 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3999 }
4000 else
4001 tv1->vval.v_float = res;
4002 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004003#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 break;
4005
4006 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004007 case ISN_COMPAREDICT:
4008 case ISN_COMPAREFUNC:
4009 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 case ISN_COMPAREBLOB:
4011 {
4012 typval_T *tv1 = STACK_TV_BOT(-2);
4013 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004014 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4015 int ic = iptr->isn_arg.op.op_ic;
4016 int res = FALSE;
4017 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018
Bram Moolenaar265f8112021-12-19 12:33:05 +00004019 SOURCING_LNUM = iptr->isn_lnum;
4020 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004022 status = typval_compare_list(tv1, tv2,
4023 exprtype, ic, &res);
4024 }
4025 else if (iptr->isn_type == ISN_COMPAREDICT)
4026 {
4027 status = typval_compare_dict(tv1, tv2,
4028 exprtype, ic, &res);
4029 }
4030 else if (iptr->isn_type == ISN_COMPAREFUNC)
4031 {
4032 status = typval_compare_func(tv1, tv2,
4033 exprtype, ic, &res);
4034 }
4035 else if (iptr->isn_type == ISN_COMPARESTRING)
4036 {
4037 status = typval_compare_string(tv1, tv2,
4038 exprtype, ic, &res);
4039 }
4040 else
4041 {
4042 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004044 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 clear_tv(tv1);
4046 clear_tv(tv2);
4047 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004048 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4049 if (status == FAIL)
4050 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 }
4052 break;
4053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 case ISN_COMPAREANY:
4055 {
4056 typval_T *tv1 = STACK_TV_BOT(-2);
4057 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004058 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004060 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061
Bram Moolenaareb26f432020-09-14 16:50:05 +02004062 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004063 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004065 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004066 if (status == FAIL)
4067 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 }
4069 break;
4070
4071 case ISN_ADDLIST:
4072 case ISN_ADDBLOB:
4073 {
4074 typval_T *tv1 = STACK_TV_BOT(-2);
4075 typval_T *tv2 = STACK_TV_BOT(-1);
4076
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004077 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004079 {
4080 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4081 && tv1->vval.v_list != NULL)
4082 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4083 NULL);
4084 else
4085 eval_addlist(tv1, tv2);
4086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 else
4088 eval_addblob(tv1, tv2);
4089 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004090 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 }
4092 break;
4093
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004094 case ISN_LISTAPPEND:
4095 {
4096 typval_T *tv1 = STACK_TV_BOT(-2);
4097 typval_T *tv2 = STACK_TV_BOT(-1);
4098 list_T *l = tv1->vval.v_list;
4099
4100 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004101 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004102 if (l == NULL)
4103 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004104 emsg(_(e_cannot_add_to_null_list));
4105 goto on_error;
4106 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004107 if (value_check_lock(l->lv_lock, NULL, FALSE))
4108 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004109 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004110 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004111 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004112 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004113 }
4114 break;
4115
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004116 case ISN_BLOBAPPEND:
4117 {
4118 typval_T *tv1 = STACK_TV_BOT(-2);
4119 typval_T *tv2 = STACK_TV_BOT(-1);
4120 blob_T *b = tv1->vval.v_blob;
4121 int error = FALSE;
4122 varnumber_T n;
4123
4124 // add a number to a blob
4125 if (b == NULL)
4126 {
4127 SOURCING_LNUM = iptr->isn_lnum;
4128 emsg(_(e_cannot_add_to_null_blob));
4129 goto on_error;
4130 }
4131 n = tv_get_number_chk(tv2, &error);
4132 if (error)
4133 goto on_error;
4134 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004135 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004136 }
4137 break;
4138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 // Computation with two arguments of unknown type
4140 case ISN_OPANY:
4141 {
4142 typval_T *tv1 = STACK_TV_BOT(-2);
4143 typval_T *tv2 = STACK_TV_BOT(-1);
4144 varnumber_T n1, n2;
4145#ifdef FEAT_FLOAT
4146 float_T f1 = 0, f2 = 0;
4147#endif
4148 int error = FALSE;
4149
4150 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4151 {
4152 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4153 {
4154 eval_addlist(tv1, tv2);
4155 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004156 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 break;
4158 }
4159 else if (tv1->v_type == VAR_BLOB
4160 && tv2->v_type == VAR_BLOB)
4161 {
4162 eval_addblob(tv1, tv2);
4163 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004164 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 break;
4166 }
4167 }
4168#ifdef FEAT_FLOAT
4169 if (tv1->v_type == VAR_FLOAT)
4170 {
4171 f1 = tv1->vval.v_float;
4172 n1 = 0;
4173 }
4174 else
4175#endif
4176 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004177 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 n1 = tv_get_number_chk(tv1, &error);
4179 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004180 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181#ifdef FEAT_FLOAT
4182 if (tv2->v_type == VAR_FLOAT)
4183 f1 = n1;
4184#endif
4185 }
4186#ifdef FEAT_FLOAT
4187 if (tv2->v_type == VAR_FLOAT)
4188 {
4189 f2 = tv2->vval.v_float;
4190 n2 = 0;
4191 }
4192 else
4193#endif
4194 {
4195 n2 = tv_get_number_chk(tv2, &error);
4196 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004197 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198#ifdef FEAT_FLOAT
4199 if (tv1->v_type == VAR_FLOAT)
4200 f2 = n2;
4201#endif
4202 }
4203#ifdef FEAT_FLOAT
4204 // if there is a float on either side the result is a float
4205 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4206 {
4207 switch (iptr->isn_arg.op.op_type)
4208 {
4209 case EXPR_MULT: f1 = f1 * f2; break;
4210 case EXPR_DIV: f1 = f1 / f2; break;
4211 case EXPR_SUB: f1 = f1 - f2; break;
4212 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004213 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004214 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004215 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 }
4217 clear_tv(tv1);
4218 clear_tv(tv2);
4219 tv1->v_type = VAR_FLOAT;
4220 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004221 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 }
4223 else
4224#endif
4225 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004226 int failed = FALSE;
4227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 switch (iptr->isn_arg.op.op_type)
4229 {
4230 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004231 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4232 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004233 goto on_error;
4234 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 case EXPR_SUB: n1 = n1 - n2; break;
4236 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004237 default: n1 = num_modulus(n1, n2, &failed);
4238 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004239 goto on_error;
4240 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 }
4242 clear_tv(tv1);
4243 clear_tv(tv2);
4244 tv1->v_type = VAR_NUMBER;
4245 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004246 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 }
4248 }
4249 break;
4250
4251 case ISN_CONCAT:
4252 {
4253 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4254 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4255 char_u *res;
4256
4257 res = concat_str(str1, str2);
4258 clear_tv(STACK_TV_BOT(-2));
4259 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004260 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 STACK_TV_BOT(-1)->vval.v_string = res;
4262 }
4263 break;
4264
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004265 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004266 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004267 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004268 int is_slice = iptr->isn_type == ISN_STRSLICE;
4269 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004270 char_u *res;
4271
4272 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004273 // string slice: string is at stack-3, first index at
4274 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004275 if (is_slice)
4276 {
4277 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004278 n1 = tv->vval.v_number;
4279 }
4280
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004281 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004282 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004283
Bram Moolenaar4c137212021-04-19 16:48:48 +02004284 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004285 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004286 if (is_slice)
4287 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004288 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004289 else
4290 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004291 // single character (including composing characters).
4292 // If the index is too big or negative the result is
4293 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004294 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004295 vim_free(tv->vval.v_string);
4296 tv->vval.v_string = res;
4297 }
4298 break;
4299
4300 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004301 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004302 case ISN_BLOBINDEX:
4303 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004305 int is_slice = iptr->isn_type == ISN_LISTSLICE
4306 || iptr->isn_type == ISN_BLOBSLICE;
4307 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4308 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004309 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004310 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311
4312 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004313 // list slice: list is at stack-3, indexes at stack-2 and
4314 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004315 // Same for blob.
4316 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317
4318 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004319 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004321
4322 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 {
Bram Moolenaared591872020-08-15 22:14:53 +02004324 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004325 n1 = tv->vval.v_number;
4326 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 }
Bram Moolenaared591872020-08-15 22:14:53 +02004328
Bram Moolenaar4c137212021-04-19 16:48:48 +02004329 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004330 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004332 if (is_blob)
4333 {
4334 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4335 n1, n2, FALSE, tv) == FAIL)
4336 goto on_error;
4337 }
4338 else
4339 {
4340 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4341 n1, n2, FALSE, tv, TRUE) == FAIL)
4342 goto on_error;
4343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 }
4345 break;
4346
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004347 case ISN_ANYINDEX:
4348 case ISN_ANYSLICE:
4349 {
4350 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4351 typval_T *var1, *var2;
4352 int res;
4353
4354 // index: composite is at stack-2, index at stack-1
4355 // slice: composite is at stack-3, indexes at stack-2 and
4356 // stack-1
4357 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004358 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004359 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4360 goto on_error;
4361 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4362 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004363 res = eval_index_inner(tv, is_slice, var1, var2,
4364 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004365 clear_tv(var1);
4366 if (is_slice)
4367 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004368 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004369 if (res == FAIL)
4370 goto on_error;
4371 }
4372 break;
4373
Bram Moolenaar9af78762020-06-16 11:34:42 +02004374 case ISN_SLICE:
4375 {
4376 list_T *list;
4377 int count = iptr->isn_arg.number;
4378
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004379 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004380 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004381 list = tv->vval.v_list;
4382
4383 // no error for short list, expect it to be checked earlier
4384 if (list != NULL && list->lv_len >= count)
4385 {
4386 list_T *newlist = list_slice(list,
4387 count, list->lv_len - 1);
4388
4389 if (newlist != NULL)
4390 {
4391 list_unref(list);
4392 tv->vval.v_list = newlist;
4393 ++newlist->lv_refcount;
4394 }
4395 }
4396 }
4397 break;
4398
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004399 case ISN_GETITEM:
4400 {
4401 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004402 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004403
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004404 // Get list item: list is at stack-1, push item.
4405 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004406 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4407 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004408
Bram Moolenaar35578162021-08-02 19:10:38 +02004409 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004410 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004411 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004412 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004413
4414 // Useful when used in unpack assignment. Reset at
4415 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004416 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004417 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004418 }
4419 break;
4420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 case ISN_MEMBER:
4422 {
4423 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004424 char_u *key;
4425 dictitem_T *di;
4426
4427 // dict member: dict is at stack-2, key at stack-1
4428 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004429 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004430 dict = tv->vval.v_dict;
4431
4432 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004433 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004434 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004435 if (key == NULL)
4436 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004437
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004438 if ((di = dict_find(dict, key, -1)) == NULL)
4439 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004440 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004441 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004442
4443 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004444 // stack contents makes sense and the dict stack is
4445 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004446 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004447 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004448 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004449 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004450 tv->v_type = VAR_NUMBER;
4451 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004452 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004453 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004454 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004455 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004456 // Put the dict used on the dict stack, it might be used by
4457 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004458 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004459 if (dict_stack_save(tv) == FAIL)
4460 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004461 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004462 }
4463 break;
4464
4465 // dict member with string key
4466 case ISN_STRINGMEMBER:
4467 {
4468 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 dictitem_T *di;
4470
4471 tv = STACK_TV_BOT(-1);
4472 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4473 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004474 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004475 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004476 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 }
4478 dict = tv->vval.v_dict;
4479
4480 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4481 == NULL)
4482 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004483 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004484 semsg(_(e_key_not_present_in_dictionary),
4485 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004486 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004488 // Put the dict used on the dict stack, it might be used by
4489 // a dict function later.
4490 if (dict_stack_save(tv) == FAIL)
4491 goto on_fatal_error;
4492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004494 }
4495 break;
4496
4497 case ISN_CLEARDICT:
4498 dict_stack_drop();
4499 break;
4500
4501 case ISN_USEDICT:
4502 {
4503 typval_T *dict_tv = dict_stack_get_tv();
4504
4505 // Turn "dict.Func" into a partial for "Func" bound to
4506 // "dict". Don't do this when "Func" is already a partial
4507 // that was bound explicitly (pt_auto is FALSE).
4508 tv = STACK_TV_BOT(-1);
4509 if (dict_tv != NULL
4510 && dict_tv->v_type == VAR_DICT
4511 && dict_tv->vval.v_dict != NULL
4512 && (tv->v_type == VAR_FUNC
4513 || (tv->v_type == VAR_PARTIAL
4514 && (tv->vval.v_partial->pt_auto
4515 || tv->vval.v_partial->pt_dict == NULL))))
4516 dict_tv->vval.v_dict =
4517 make_partial(dict_tv->vval.v_dict, tv);
4518 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 }
4520 break;
4521
4522 case ISN_NEGATENR:
4523 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004524 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004525#ifdef FEAT_FLOAT
4526 if (tv->v_type == VAR_FLOAT)
4527 tv->vval.v_float = -tv->vval.v_float;
4528 else
4529#endif
4530 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 break;
4532
4533 case ISN_CHECKNR:
4534 {
4535 int error = FALSE;
4536
4537 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004538 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004540 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 (void)tv_get_number_chk(tv, &error);
4542 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004543 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 }
4545 break;
4546
4547 case ISN_CHECKTYPE:
4548 {
4549 checktype_T *ct = &iptr->isn_arg.type;
4550
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004551 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004552 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004553 if (!ectx->ec_where.wt_variable)
4554 ectx->ec_where.wt_index = ct->ct_arg_idx;
4555 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4556 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004557 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004558 if (!ectx->ec_where.wt_variable)
4559 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004560
4561 // number 0 is FALSE, number 1 is TRUE
4562 if (tv->v_type == VAR_NUMBER
4563 && ct->ct_type->tt_type == VAR_BOOL
4564 && (tv->vval.v_number == 0
4565 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004567 tv->v_type = VAR_BOOL;
4568 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004569 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 }
4571 }
4572 break;
4573
Bram Moolenaar9af78762020-06-16 11:34:42 +02004574 case ISN_CHECKLEN:
4575 {
4576 int min_len = iptr->isn_arg.checklen.cl_min_len;
4577 list_T *list = NULL;
4578
4579 tv = STACK_TV_BOT(-1);
4580 if (tv->v_type == VAR_LIST)
4581 list = tv->vval.v_list;
4582 if (list == NULL || list->lv_len < min_len
4583 || (list->lv_len > min_len
4584 && !iptr->isn_arg.checklen.cl_more_OK))
4585 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004586 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004587 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004588 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004589 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004590 }
4591 }
4592 break;
4593
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004594 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004595 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004596 break;
4597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004599 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 {
4601 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004602 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004604 if (iptr->isn_type == ISN_2BOOL)
4605 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004606 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004607 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004608 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004609 n = !n;
4610 }
4611 else
4612 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004613 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004614 SOURCING_LNUM = iptr->isn_lnum;
4615 n = tv_get_bool_chk(tv, &error);
4616 if (error)
4617 goto on_error;
4618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 clear_tv(tv);
4620 tv->v_type = VAR_BOOL;
4621 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4622 }
4623 break;
4624
4625 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004626 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004627 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004628 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4629 iptr->isn_type == ISN_2STRING_ANY,
4630 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004631 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 break;
4633
Bram Moolenaar08597872020-12-10 19:43:40 +01004634 case ISN_RANGE:
4635 {
4636 exarg_T ea;
4637 char *errormsg;
4638
Bram Moolenaarece0b872021-01-08 20:40:45 +01004639 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004640 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004641 ea.addr_type = ADDR_LINES;
4642 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004643 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004644 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004645 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004646
Bram Moolenaar35578162021-08-02 19:10:38 +02004647 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004648 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004649 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004650 tv = STACK_TV_BOT(-1);
4651 tv->v_type = VAR_NUMBER;
4652 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004653 if (ea.addr_count == 0)
4654 tv->vval.v_number = curwin->w_cursor.lnum;
4655 else
4656 tv->vval.v_number = ea.line2;
4657 }
4658 break;
4659
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004660 case ISN_PUT:
4661 {
4662 int regname = iptr->isn_arg.put.put_regname;
4663 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4664 char_u *expr = NULL;
4665 int dir = FORWARD;
4666
Bram Moolenaar08597872020-12-10 19:43:40 +01004667 if (lnum < -2)
4668 {
4669 // line number was put on the stack by ISN_RANGE
4670 tv = STACK_TV_BOT(-1);
4671 curwin->w_cursor.lnum = tv->vval.v_number;
4672 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4673 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004674 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004675 }
4676 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004677 // :put! above cursor
4678 dir = BACKWARD;
4679 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004680 {
4681 curwin->w_cursor.lnum = lnum;
4682 if (lnum == 0)
4683 // check_cursor() below will move to line 1
4684 dir = BACKWARD;
4685 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004686
4687 if (regname == '=')
4688 {
4689 tv = STACK_TV_BOT(-1);
4690 if (tv->v_type == VAR_STRING)
4691 expr = tv->vval.v_string;
4692 else
4693 {
4694 expr = typval2string(tv, TRUE); // allocates value
4695 clear_tv(tv);
4696 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004697 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004698 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004699 check_cursor();
4700 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4701 vim_free(expr);
4702 }
4703 break;
4704
Bram Moolenaar02194d22020-10-24 23:08:38 +02004705 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004706 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4707 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4708 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4709 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004710 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4711 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004712 break;
4713
Bram Moolenaar02194d22020-10-24 23:08:38 +02004714 case ISN_CMDMOD_REV:
4715 // filter regprog is owned by the instruction, don't free it
4716 cmdmod.cmod_filter_regmatch.regprog = NULL;
4717 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004718 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4719 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004720 break;
4721
Bram Moolenaar792f7862020-11-23 08:31:18 +01004722 case ISN_UNPACK:
4723 {
4724 int count = iptr->isn_arg.unpack.unp_count;
4725 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4726 list_T *l;
4727 listitem_T *li;
4728 int i;
4729
4730 // Check there is a valid list to unpack.
4731 tv = STACK_TV_BOT(-1);
4732 if (tv->v_type != VAR_LIST)
4733 {
4734 SOURCING_LNUM = iptr->isn_lnum;
4735 emsg(_(e_for_argument_must_be_sequence_of_lists));
4736 goto on_error;
4737 }
4738 l = tv->vval.v_list;
4739 if (l == NULL
4740 || l->lv_len < (semicolon ? count - 1 : count))
4741 {
4742 SOURCING_LNUM = iptr->isn_lnum;
4743 emsg(_(e_list_value_does_not_have_enough_items));
4744 goto on_error;
4745 }
4746 else if (!semicolon && l->lv_len > count)
4747 {
4748 SOURCING_LNUM = iptr->isn_lnum;
4749 emsg(_(e_list_value_has_more_items_than_targets));
4750 goto on_error;
4751 }
4752
4753 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004754 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004755 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004756 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004757
4758 // Variable after semicolon gets a list with the remaining
4759 // items.
4760 if (semicolon)
4761 {
4762 list_T *rem_list =
4763 list_alloc_with_items(l->lv_len - count + 1);
4764
4765 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004766 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004767 tv = STACK_TV_BOT(-count);
4768 tv->vval.v_list = rem_list;
4769 ++rem_list->lv_refcount;
4770 tv->v_lock = 0;
4771 li = l->lv_first;
4772 for (i = 0; i < count - 1; ++i)
4773 li = li->li_next;
4774 for (i = 0; li != NULL; ++i)
4775 {
4776 list_set_item(rem_list, i, &li->li_tv);
4777 li = li->li_next;
4778 }
4779 --count;
4780 }
4781
4782 // Produce the values in reverse order, first item last.
4783 li = l->lv_first;
4784 for (i = 0; i < count; ++i)
4785 {
4786 tv = STACK_TV_BOT(-i - 1);
4787 copy_tv(&li->li_tv, tv);
4788 li = li->li_next;
4789 }
4790
4791 list_unref(l);
4792 }
4793 break;
4794
Bram Moolenaarb2049902021-01-24 12:53:53 +01004795 case ISN_PROF_START:
4796 case ISN_PROF_END:
4797 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004798#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004799 funccall_T cookie;
4800 ufunc_T *cur_ufunc =
4801 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004802 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004803
4804 cookie.func = cur_ufunc;
4805 if (iptr->isn_type == ISN_PROF_START)
4806 {
4807 func_line_start(&cookie, iptr->isn_lnum);
4808 // if we get here the instruction is executed
4809 func_line_exec(&cookie);
4810 }
4811 else
4812 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004813#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004814 }
4815 break;
4816
Bram Moolenaare99d4222021-06-13 14:01:26 +02004817 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004818 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004819 break;
4820
Bram Moolenaar389df252020-07-09 21:20:47 +02004821 case ISN_SHUFFLE:
4822 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004823 typval_T tmp_tv;
4824 int item = iptr->isn_arg.shuffle.shfl_item;
4825 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004826
4827 tmp_tv = *STACK_TV_BOT(-item);
4828 for ( ; up > 0 && item > 1; --up)
4829 {
4830 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4831 --item;
4832 }
4833 *STACK_TV_BOT(-item) = tmp_tv;
4834 }
4835 break;
4836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004838 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004840 ectx->ec_where.wt_index = 0;
4841 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 break;
4843 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004844 continue;
4845
Bram Moolenaard032f342020-07-18 18:13:02 +02004846func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004847 // Restore previous function. If the frame pointer is where we started
4848 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004849 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004850 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004851
Bram Moolenaar4c137212021-04-19 16:48:48 +02004852 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004853 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004854 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004855 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004856
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004857on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004858 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004859 // If "emsg_silent" is set then ignore the error, unless it was set
4860 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004861 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004862 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004863 {
4864 // If a sequence of instructions causes an error while ":silent!"
4865 // was used, restore the stack length and jump ahead to restoring
4866 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004867 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004868 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004869 while (ectx->ec_stack.ga_len
4870 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004871 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004872 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004873 clear_tv(STACK_TV_BOT(0));
4874 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004875 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4876 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004877 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004878 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004879 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004880on_fatal_error:
4881 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004882 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004883 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004884 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 }
4886
4887done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004888 ret = OK;
4889theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004890 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004891 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4892 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004893}
4894
4895/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004896 * Execute the instructions from a VAR_INSTR typeval and put the result in
4897 * "rettv".
4898 * Return OK or FAIL.
4899 */
4900 int
4901exe_typval_instr(typval_T *tv, typval_T *rettv)
4902{
4903 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4904 isn_T *save_instr = ectx->ec_instr;
4905 int save_iidx = ectx->ec_iidx;
4906 int res;
4907
4908 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4909 res = exec_instructions(ectx);
4910 if (res == OK)
4911 {
4912 *rettv = *STACK_TV_BOT(-1);
4913 --ectx->ec_stack.ga_len;
4914 }
4915
4916 ectx->ec_instr = save_instr;
4917 ectx->ec_iidx = save_iidx;
4918
4919 return res;
4920}
4921
4922/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004923 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4924 * "substitute_instr".
4925 */
4926 char_u *
4927exe_substitute_instr(void)
4928{
4929 ectx_T *ectx = substitute_instr->subs_ectx;
4930 isn_T *save_instr = ectx->ec_instr;
4931 int save_iidx = ectx->ec_iidx;
4932 char_u *res;
4933
4934 ectx->ec_instr = substitute_instr->subs_instr;
4935 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004936 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004937 typval_T *tv = STACK_TV_BOT(-1);
4938
Bram Moolenaar27523602021-06-05 21:36:19 +02004939 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004940 --ectx->ec_stack.ga_len;
4941 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004942 }
4943 else
4944 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004945 substitute_instr->subs_status = FAIL;
4946 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948
Bram Moolenaar4c137212021-04-19 16:48:48 +02004949 ectx->ec_instr = save_instr;
4950 ectx->ec_iidx = save_iidx;
4951
4952 return res;
4953}
4954
4955/*
4956 * Call a "def" function from old Vim script.
4957 * Return OK or FAIL.
4958 */
4959 int
4960call_def_function(
4961 ufunc_T *ufunc,
4962 int argc_arg, // nr of arguments
4963 typval_T *argv, // arguments
4964 partial_T *partial, // optional partial for context
4965 typval_T *rettv) // return value
4966{
4967 ectx_T ectx; // execution context
4968 int argc = argc_arg;
4969 typval_T *tv;
4970 int idx;
4971 int ret = FAIL;
4972 int defcount = ufunc->uf_args.ga_len - argc;
4973 sctx_T save_current_sctx = current_sctx;
4974 int did_emsg_before = did_emsg_cumul + did_emsg;
4975 int save_suppress_errthrow = suppress_errthrow;
4976 msglist_T **saved_msg_list = NULL;
4977 msglist_T *private_msg_list = NULL;
4978 int save_emsg_silent_def = emsg_silent_def;
4979 int save_did_emsg_def = did_emsg_def;
4980 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004981 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004982
4983// Get pointer to item in the stack.
4984#undef STACK_TV
4985#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4986
4987// Get pointer to item at the bottom of the stack, -1 is the bottom.
4988#undef STACK_TV_BOT
4989#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4990
4991// Get pointer to a local variable on the stack. Negative for arguments.
4992#undef STACK_TV_VAR
4993#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4994
4995 if (ufunc->uf_def_status == UF_NOT_COMPILED
4996 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00004997 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
4998 && compile_def_function(ufunc, FALSE,
4999 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005000 {
5001 if (did_emsg_cumul + did_emsg == did_emsg_before)
5002 semsg(_(e_function_is_not_compiled_str),
5003 printable_func_name(ufunc));
5004 return FAIL;
5005 }
5006
5007 {
5008 // Check the function was really compiled.
5009 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5010 + ufunc->uf_dfunc_idx;
5011 if (INSTRUCTIONS(dfunc) == NULL)
5012 {
5013 iemsg("using call_def_function() on not compiled function");
5014 return FAIL;
5015 }
5016 }
5017
5018 // If depth of calling is getting too high, don't execute the function.
5019 orig_funcdepth = funcdepth_get();
5020 if (funcdepth_increment() == FAIL)
5021 return FAIL;
5022
5023 CLEAR_FIELD(ectx);
5024 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5025 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005026 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005027 {
5028 funcdepth_decrement();
5029 return FAIL;
5030 }
5031 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5032 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5033 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005034 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005035
5036 idx = argc - ufunc->uf_args.ga_len;
5037 if (idx > 0 && ufunc->uf_va_name == NULL)
5038 {
5039 if (idx == 1)
5040 emsg(_(e_one_argument_too_many));
5041 else
5042 semsg(_(e_nr_arguments_too_many), idx);
5043 goto failed_early;
5044 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005045 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5046 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005047 {
5048 if (idx == -1)
5049 emsg(_(e_one_argument_too_few));
5050 else
5051 semsg(_(e_nr_arguments_too_few), -idx);
5052 goto failed_early;
5053 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005054
5055 // Put arguments on the stack, but no more than what the function expects.
5056 // A lambda can be called with more arguments than it uses.
5057 for (idx = 0; idx < argc
5058 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5059 ++idx)
5060 {
5061 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5062 && argv[idx].v_type == VAR_SPECIAL
5063 && argv[idx].vval.v_number == VVAL_NONE)
5064 {
5065 // Use the default value.
5066 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5067 }
5068 else
5069 {
5070 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5071 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005072 ufunc->uf_arg_types[idx], &argv[idx],
5073 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005074 goto failed_early;
5075 copy_tv(&argv[idx], STACK_TV_BOT(0));
5076 }
5077 ++ectx.ec_stack.ga_len;
5078 }
5079
5080 // Turn varargs into a list. Empty list if no args.
5081 if (ufunc->uf_va_name != NULL)
5082 {
5083 int vararg_count = argc - ufunc->uf_args.ga_len;
5084
5085 if (vararg_count < 0)
5086 vararg_count = 0;
5087 else
5088 argc -= vararg_count;
5089 if (exe_newlist(vararg_count, &ectx) == FAIL)
5090 goto failed_early;
5091
5092 // Check the type of the list items.
5093 tv = STACK_TV_BOT(-1);
5094 if (ufunc->uf_va_type != NULL
5095 && ufunc->uf_va_type != &t_list_any
5096 && ufunc->uf_va_type->tt_member != &t_any
5097 && tv->vval.v_list != NULL)
5098 {
5099 type_T *expected = ufunc->uf_va_type->tt_member;
5100 listitem_T *li = tv->vval.v_list->lv_first;
5101
5102 for (idx = 0; idx < vararg_count; ++idx)
5103 {
5104 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005105 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005106 goto failed_early;
5107 li = li->li_next;
5108 }
5109 }
5110
5111 if (defcount > 0)
5112 // Move varargs list to below missing default arguments.
5113 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5114 --ectx.ec_stack.ga_len;
5115 }
5116
5117 // Make space for omitted arguments, will store default value below.
5118 // Any varargs list goes after them.
5119 if (defcount > 0)
5120 for (idx = 0; idx < defcount; ++idx)
5121 {
5122 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5123 ++ectx.ec_stack.ga_len;
5124 }
5125 if (ufunc->uf_va_name != NULL)
5126 ++ectx.ec_stack.ga_len;
5127
5128 // Frame pointer points to just after arguments.
5129 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5130 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5131
5132 {
5133 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5134 + ufunc->uf_dfunc_idx;
5135 ufunc_T *base_ufunc = dfunc->df_ufunc;
5136
5137 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5138 // by copy_func().
5139 if (partial != NULL || base_ufunc->uf_partial != NULL)
5140 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005141 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5142 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005143 goto failed_early;
5144 if (partial != NULL)
5145 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005146 outer_T *outer = get_pt_outer(partial);
5147
5148 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005149 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005150 if (current_ectx != NULL)
5151 {
5152 if (current_ectx->ec_outer_ref != NULL
5153 && current_ectx->ec_outer_ref->or_outer != NULL)
5154 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005155 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005156 }
5157 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005158 }
5159 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005160 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005161 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005162 ++partial->pt_refcount;
5163 ectx.ec_outer_ref->or_partial = partial;
5164 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005165 }
5166 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005167 {
5168 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5169 ++base_ufunc->uf_partial->pt_refcount;
5170 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5171 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005172 }
5173 }
5174
5175 // dummy frame entries
5176 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5177 {
5178 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5179 ++ectx.ec_stack.ga_len;
5180 }
5181
5182 {
5183 // Reserve space for local variables and any closure reference count.
5184 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5185 + ufunc->uf_dfunc_idx;
5186
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005187 // Initialize variables to zero. That avoids having to generate
5188 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005189 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005190 {
5191 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5192 STACK_TV_VAR(idx)->vval.v_number = 0;
5193 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005194 ectx.ec_stack.ga_len += dfunc->df_varcount;
5195 if (dfunc->df_has_closure)
5196 {
5197 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5198 STACK_TV_VAR(idx)->vval.v_number = 0;
5199 ++ectx.ec_stack.ga_len;
5200 }
5201
5202 ectx.ec_instr = INSTRUCTIONS(dfunc);
5203 }
5204
5205 // Following errors are in the function, not the caller.
5206 // Commands behave like vim9script.
5207 estack_push_ufunc(ufunc, 1);
5208 current_sctx = ufunc->uf_script_ctx;
5209 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5210
5211 // Use a specific location for storing error messages to be converted to an
5212 // exception.
5213 saved_msg_list = msg_list;
5214 msg_list = &private_msg_list;
5215
5216 // Do turn errors into exceptions.
5217 suppress_errthrow = FALSE;
5218
5219 // Do not delete the function while executing it.
5220 ++ufunc->uf_calls;
5221
5222 // When ":silent!" was used before calling then we still abort the
5223 // function. If ":silent!" is used in the function then we don't.
5224 emsg_silent_def = emsg_silent;
5225 did_emsg_def = 0;
5226
5227 ectx.ec_where.wt_index = 0;
5228 ectx.ec_where.wt_variable = FALSE;
5229
5230 // Execute the instructions until done.
5231 ret = exec_instructions(&ectx);
5232 if (ret == OK)
5233 {
5234 // function finished, get result from the stack.
5235 if (ufunc->uf_ret_type == &t_void)
5236 {
5237 rettv->v_type = VAR_VOID;
5238 }
5239 else
5240 {
5241 tv = STACK_TV_BOT(-1);
5242 *rettv = *tv;
5243 tv->v_type = VAR_UNKNOWN;
5244 }
5245 }
5246
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005247 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005248 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5249 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005250
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005251 // Deal with any remaining closures, they may be in use somewhere.
5252 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005253 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005254 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005255 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005256 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005257
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005258 estack_pop();
5259 current_sctx = save_current_sctx;
5260
Bram Moolenaar2336c372021-12-06 15:06:54 +00005261 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5262 // Function was unreferenced while being used, free it now.
5263 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005264
Bram Moolenaar352134b2020-10-17 22:04:08 +02005265 if (*msg_list != NULL && saved_msg_list != NULL)
5266 {
5267 msglist_T **plist = saved_msg_list;
5268
5269 // Append entries from the current msg_list (uncaught exceptions) to
5270 // the saved msg_list.
5271 while (*plist != NULL)
5272 plist = &(*plist)->next;
5273
5274 *plist = *msg_list;
5275 }
5276 msg_list = saved_msg_list;
5277
Bram Moolenaar4c137212021-04-19 16:48:48 +02005278 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005279 {
5280 cmdmod.cmod_filter_regmatch.regprog = NULL;
5281 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005282 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005283 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005284 emsg_silent_def = save_emsg_silent_def;
5285 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005286
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005287failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005288 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5290 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005291 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005294 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005295 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005296 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005297 if (ectx.ec_outer_ref->or_outer_allocated)
5298 vim_free(ectx.ec_outer_ref->or_outer);
5299 partial_unref(ectx.ec_outer_ref->or_partial);
5300 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005301 }
5302
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005303 // Not sure if this is necessary.
5304 suppress_errthrow = save_suppress_errthrow;
5305
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005306 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5307 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005308 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005309 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005310 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005311 return ret;
5312}
5313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005314/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005315 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5316 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5317 * "pfx" is prefixed to every line.
5318 */
5319 static void
5320list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5321{
5322 int line_idx = 0;
5323 int prev_current = 0;
5324 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005325 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005326
5327 for (current = 0; current < instr_count; ++current)
5328 {
5329 isn_T *iptr = &instr[current];
5330 char *line;
5331
5332 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005333 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005334 while (line_idx < iptr->isn_lnum
5335 && line_idx < ufunc->uf_lines.ga_len)
5336 {
5337 if (current > prev_current)
5338 {
5339 msg_puts("\n\n");
5340 prev_current = current;
5341 }
5342 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5343 if (line != NULL)
5344 msg(line);
5345 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005346 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5347 {
5348 int first_def_arg = ufunc->uf_args.ga_len
5349 - ufunc->uf_def_args.ga_len;
5350
5351 if (def_arg_idx > 0)
5352 msg_puts("\n\n");
5353 msg_start();
5354 msg_puts(" ");
5355 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5356 first_def_arg + def_arg_idx]);
5357 msg_puts(" = ");
5358 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5359 msg_clr_eos();
5360 msg_end();
5361 }
5362 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005363
5364 switch (iptr->isn_type)
5365 {
5366 case ISN_EXEC:
5367 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5368 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005369 case ISN_EXEC_SPLIT:
5370 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5371 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005372 case ISN_EXECRANGE:
5373 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5374 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005375 case ISN_LEGACY_EVAL:
5376 smsg("%s%4d EVAL legacy %s", pfx, current,
5377 iptr->isn_arg.string);
5378 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005379 case ISN_REDIRSTART:
5380 smsg("%s%4d REDIR", pfx, current);
5381 break;
5382 case ISN_REDIREND:
5383 smsg("%s%4d REDIR END%s", pfx, current,
5384 iptr->isn_arg.number ? " append" : "");
5385 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005386 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005387#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005388 smsg("%s%4d CEXPR pre %s", pfx, current,
5389 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005390#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005391 break;
5392 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005393#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005394 {
5395 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5396
5397 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5398 cexpr_get_auname(cer->cer_cmdidx),
5399 cer->cer_forceit ? "!" : "",
5400 cer->cer_cmdline);
5401 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005402#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005403 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005404 case ISN_INSTR:
5405 {
5406 smsg("%s%4d INSTR", pfx, current);
5407 list_instructions(" ", iptr->isn_arg.instr,
5408 INT_MAX, NULL);
5409 msg(" -------------");
5410 }
5411 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005412 case ISN_SUBSTITUTE:
5413 {
5414 subs_T *subs = &iptr->isn_arg.subs;
5415
5416 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5417 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5418 msg(" -------------");
5419 }
5420 break;
5421 case ISN_EXECCONCAT:
5422 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5423 (varnumber_T)iptr->isn_arg.number);
5424 break;
5425 case ISN_ECHO:
5426 {
5427 echo_T *echo = &iptr->isn_arg.echo;
5428
5429 smsg("%s%4d %s %d", pfx, current,
5430 echo->echo_with_white ? "ECHO" : "ECHON",
5431 echo->echo_count);
5432 }
5433 break;
5434 case ISN_EXECUTE:
5435 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005436 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005437 break;
5438 case ISN_ECHOMSG:
5439 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005440 (varnumber_T)(iptr->isn_arg.number));
5441 break;
5442 case ISN_ECHOCONSOLE:
5443 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5444 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005445 break;
5446 case ISN_ECHOERR:
5447 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005448 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005449 break;
5450 case ISN_LOAD:
5451 {
5452 if (iptr->isn_arg.number < 0)
5453 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5454 (varnumber_T)(iptr->isn_arg.number
5455 + STACK_FRAME_SIZE));
5456 else
5457 smsg("%s%4d LOAD $%lld", pfx, current,
5458 (varnumber_T)(iptr->isn_arg.number));
5459 }
5460 break;
5461 case ISN_LOADOUTER:
5462 {
5463 if (iptr->isn_arg.number < 0)
5464 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5465 iptr->isn_arg.outer.outer_depth,
5466 iptr->isn_arg.outer.outer_idx
5467 + STACK_FRAME_SIZE);
5468 else
5469 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5470 iptr->isn_arg.outer.outer_depth,
5471 iptr->isn_arg.outer.outer_idx);
5472 }
5473 break;
5474 case ISN_LOADV:
5475 smsg("%s%4d LOADV v:%s", pfx, current,
5476 get_vim_var_name(iptr->isn_arg.number));
5477 break;
5478 case ISN_LOADSCRIPT:
5479 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005480 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5481 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5482 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005483
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005484 sv = get_script_svar(sref, -1);
5485 if (sv == NULL)
5486 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5487 pfx, current, si->sn_name);
5488 else
5489 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005490 sv->sv_name,
5491 sref->sref_idx,
5492 si->sn_name);
5493 }
5494 break;
5495 case ISN_LOADS:
5496 {
5497 scriptitem_T *si = SCRIPT_ITEM(
5498 iptr->isn_arg.loadstore.ls_sid);
5499
5500 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5501 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5502 }
5503 break;
5504 case ISN_LOADAUTO:
5505 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5506 break;
5507 case ISN_LOADG:
5508 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5509 break;
5510 case ISN_LOADB:
5511 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5512 break;
5513 case ISN_LOADW:
5514 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5515 break;
5516 case ISN_LOADT:
5517 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5518 break;
5519 case ISN_LOADGDICT:
5520 smsg("%s%4d LOAD g:", pfx, current);
5521 break;
5522 case ISN_LOADBDICT:
5523 smsg("%s%4d LOAD b:", pfx, current);
5524 break;
5525 case ISN_LOADWDICT:
5526 smsg("%s%4d LOAD w:", pfx, current);
5527 break;
5528 case ISN_LOADTDICT:
5529 smsg("%s%4d LOAD t:", pfx, current);
5530 break;
5531 case ISN_LOADOPT:
5532 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5533 break;
5534 case ISN_LOADENV:
5535 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5536 break;
5537 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005538 smsg("%s%4d LOADREG @%c", pfx, current,
5539 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005540 break;
5541
5542 case ISN_STORE:
5543 if (iptr->isn_arg.number < 0)
5544 smsg("%s%4d STORE arg[%lld]", pfx, current,
5545 iptr->isn_arg.number + STACK_FRAME_SIZE);
5546 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005547 smsg("%s%4d STORE $%lld", pfx, current,
5548 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005549 break;
5550 case ISN_STOREOUTER:
5551 {
5552 if (iptr->isn_arg.number < 0)
5553 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5554 iptr->isn_arg.outer.outer_depth,
5555 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5556 else
5557 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5558 iptr->isn_arg.outer.outer_depth,
5559 iptr->isn_arg.outer.outer_idx);
5560 }
5561 break;
5562 case ISN_STOREV:
5563 smsg("%s%4d STOREV v:%s", pfx, current,
5564 get_vim_var_name(iptr->isn_arg.number));
5565 break;
5566 case ISN_STOREAUTO:
5567 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5568 break;
5569 case ISN_STOREG:
5570 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5571 break;
5572 case ISN_STOREB:
5573 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5574 break;
5575 case ISN_STOREW:
5576 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5577 break;
5578 case ISN_STORET:
5579 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5580 break;
5581 case ISN_STORES:
5582 {
5583 scriptitem_T *si = SCRIPT_ITEM(
5584 iptr->isn_arg.loadstore.ls_sid);
5585
5586 smsg("%s%4d STORES %s in %s", pfx, current,
5587 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5588 }
5589 break;
5590 case ISN_STORESCRIPT:
5591 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005592 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5593 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5594 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005595
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005596 sv = get_script_svar(sref, -1);
5597 if (sv == NULL)
5598 smsg("%s%4d STORESCRIPT [deleted] in %s",
5599 pfx, current, si->sn_name);
5600 else
5601 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005602 sv->sv_name,
5603 sref->sref_idx,
5604 si->sn_name);
5605 }
5606 break;
5607 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005608 case ISN_STOREFUNCOPT:
5609 smsg("%s%4d %s &%s", pfx, current,
5610 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005611 iptr->isn_arg.storeopt.so_name);
5612 break;
5613 case ISN_STOREENV:
5614 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5615 break;
5616 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005617 smsg("%s%4d STOREREG @%c", pfx, current,
5618 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005619 break;
5620 case ISN_STORENR:
5621 smsg("%s%4d STORE %lld in $%d", pfx, current,
5622 iptr->isn_arg.storenr.stnr_val,
5623 iptr->isn_arg.storenr.stnr_idx);
5624 break;
5625
5626 case ISN_STOREINDEX:
5627 smsg("%s%4d STOREINDEX %s", pfx, current,
5628 vartype_name(iptr->isn_arg.vartype));
5629 break;
5630
5631 case ISN_STORERANGE:
5632 smsg("%s%4d STORERANGE", pfx, current);
5633 break;
5634
5635 // constants
5636 case ISN_PUSHNR:
5637 smsg("%s%4d PUSHNR %lld", pfx, current,
5638 (varnumber_T)(iptr->isn_arg.number));
5639 break;
5640 case ISN_PUSHBOOL:
5641 case ISN_PUSHSPEC:
5642 smsg("%s%4d PUSH %s", pfx, current,
5643 get_var_special_name(iptr->isn_arg.number));
5644 break;
5645 case ISN_PUSHF:
5646#ifdef FEAT_FLOAT
5647 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5648#endif
5649 break;
5650 case ISN_PUSHS:
5651 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5652 break;
5653 case ISN_PUSHBLOB:
5654 {
5655 char_u *r;
5656 char_u numbuf[NUMBUFLEN];
5657 char_u *tofree;
5658
5659 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5660 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5661 vim_free(tofree);
5662 }
5663 break;
5664 case ISN_PUSHFUNC:
5665 {
5666 char *name = (char *)iptr->isn_arg.string;
5667
5668 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5669 name == NULL ? "[none]" : name);
5670 }
5671 break;
5672 case ISN_PUSHCHANNEL:
5673#ifdef FEAT_JOB_CHANNEL
5674 {
5675 channel_T *channel = iptr->isn_arg.channel;
5676
5677 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5678 channel == NULL ? 0 : channel->ch_id);
5679 }
5680#endif
5681 break;
5682 case ISN_PUSHJOB:
5683#ifdef FEAT_JOB_CHANNEL
5684 {
5685 typval_T tv;
5686 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005687 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005688
5689 tv.v_type = VAR_JOB;
5690 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005691 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005692 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5693 }
5694#endif
5695 break;
5696 case ISN_PUSHEXC:
5697 smsg("%s%4d PUSH v:exception", pfx, current);
5698 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005699 case ISN_AUTOLOAD:
5700 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5701 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005702 case ISN_UNLET:
5703 smsg("%s%4d UNLET%s %s", pfx, current,
5704 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5705 iptr->isn_arg.unlet.ul_name);
5706 break;
5707 case ISN_UNLETENV:
5708 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5709 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5710 iptr->isn_arg.unlet.ul_name);
5711 break;
5712 case ISN_UNLETINDEX:
5713 smsg("%s%4d UNLETINDEX", pfx, current);
5714 break;
5715 case ISN_UNLETRANGE:
5716 smsg("%s%4d UNLETRANGE", pfx, current);
5717 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005718 case ISN_LOCKUNLOCK:
5719 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5720 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005721 case ISN_LOCKCONST:
5722 smsg("%s%4d LOCKCONST", pfx, current);
5723 break;
5724 case ISN_NEWLIST:
5725 smsg("%s%4d NEWLIST size %lld", pfx, current,
5726 (varnumber_T)(iptr->isn_arg.number));
5727 break;
5728 case ISN_NEWDICT:
5729 smsg("%s%4d NEWDICT size %lld", pfx, current,
5730 (varnumber_T)(iptr->isn_arg.number));
5731 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005732 case ISN_NEWPARTIAL:
5733 smsg("%s%4d NEWPARTIAL", pfx, current);
5734 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005735
5736 // function call
5737 case ISN_BCALL:
5738 {
5739 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5740
5741 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5742 internal_func_name(cbfunc->cbf_idx),
5743 cbfunc->cbf_argcount);
5744 }
5745 break;
5746 case ISN_DCALL:
5747 {
5748 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5749 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5750 + cdfunc->cdf_idx;
5751
5752 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005753 printable_func_name(df->df_ufunc),
5754 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005755 }
5756 break;
5757 case ISN_UCALL:
5758 {
5759 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5760
5761 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5762 cufunc->cuf_name, cufunc->cuf_argcount);
5763 }
5764 break;
5765 case ISN_PCALL:
5766 {
5767 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5768
5769 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5770 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5771 }
5772 break;
5773 case ISN_PCALL_END:
5774 smsg("%s%4d PCALL end", pfx, current);
5775 break;
5776 case ISN_RETURN:
5777 smsg("%s%4d RETURN", pfx, current);
5778 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005779 case ISN_RETURN_VOID:
5780 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005781 break;
5782 case ISN_FUNCREF:
5783 {
5784 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005785 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005786
Bram Moolenaar38453522021-11-28 22:00:12 +00005787 if (funcref->fr_func_name == NULL)
5788 {
5789 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5790 + funcref->fr_dfunc_idx;
5791 name = df->df_ufunc->uf_name;
5792 }
5793 else
5794 name = funcref->fr_func_name;
5795 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005796 }
5797 break;
5798
5799 case ISN_NEWFUNC:
5800 {
5801 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5802
5803 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5804 newfunc->nf_lambda, newfunc->nf_global);
5805 }
5806 break;
5807
5808 case ISN_DEF:
5809 {
5810 char_u *name = iptr->isn_arg.string;
5811
5812 smsg("%s%4d DEF %s", pfx, current,
5813 name == NULL ? (char_u *)"" : name);
5814 }
5815 break;
5816
5817 case ISN_JUMP:
5818 {
5819 char *when = "?";
5820
5821 switch (iptr->isn_arg.jump.jump_when)
5822 {
5823 case JUMP_ALWAYS:
5824 when = "JUMP";
5825 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005826 case JUMP_NEVER:
5827 iemsg("JUMP_NEVER should not be used");
5828 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005829 case JUMP_AND_KEEP_IF_TRUE:
5830 when = "JUMP_AND_KEEP_IF_TRUE";
5831 break;
5832 case JUMP_IF_FALSE:
5833 when = "JUMP_IF_FALSE";
5834 break;
5835 case JUMP_AND_KEEP_IF_FALSE:
5836 when = "JUMP_AND_KEEP_IF_FALSE";
5837 break;
5838 case JUMP_IF_COND_FALSE:
5839 when = "JUMP_IF_COND_FALSE";
5840 break;
5841 case JUMP_IF_COND_TRUE:
5842 when = "JUMP_IF_COND_TRUE";
5843 break;
5844 }
5845 smsg("%s%4d %s -> %d", pfx, current, when,
5846 iptr->isn_arg.jump.jump_where);
5847 }
5848 break;
5849
5850 case ISN_JUMP_IF_ARG_SET:
5851 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5852 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5853 iptr->isn_arg.jump.jump_where);
5854 break;
5855
5856 case ISN_FOR:
5857 {
5858 forloop_T *forloop = &iptr->isn_arg.forloop;
5859
5860 smsg("%s%4d FOR $%d -> %d", pfx, current,
5861 forloop->for_idx, forloop->for_end);
5862 }
5863 break;
5864
5865 case ISN_TRY:
5866 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005867 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005868
5869 if (try->try_ref->try_finally == 0)
5870 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5871 pfx, current,
5872 try->try_ref->try_catch,
5873 try->try_ref->try_endtry);
5874 else
5875 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5876 pfx, current,
5877 try->try_ref->try_catch,
5878 try->try_ref->try_finally,
5879 try->try_ref->try_endtry);
5880 }
5881 break;
5882 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005883 smsg("%s%4d CATCH", pfx, current);
5884 break;
5885 case ISN_TRYCONT:
5886 {
5887 trycont_T *trycont = &iptr->isn_arg.trycont;
5888
5889 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5890 trycont->tct_levels,
5891 trycont->tct_levels == 1 ? "" : "s",
5892 trycont->tct_where);
5893 }
5894 break;
5895 case ISN_FINALLY:
5896 smsg("%s%4d FINALLY", pfx, current);
5897 break;
5898 case ISN_ENDTRY:
5899 smsg("%s%4d ENDTRY", pfx, current);
5900 break;
5901 case ISN_THROW:
5902 smsg("%s%4d THROW", pfx, current);
5903 break;
5904
5905 // expression operations on number
5906 case ISN_OPNR:
5907 case ISN_OPFLOAT:
5908 case ISN_OPANY:
5909 {
5910 char *what;
5911 char *ins;
5912
5913 switch (iptr->isn_arg.op.op_type)
5914 {
5915 case EXPR_MULT: what = "*"; break;
5916 case EXPR_DIV: what = "/"; break;
5917 case EXPR_REM: what = "%"; break;
5918 case EXPR_SUB: what = "-"; break;
5919 case EXPR_ADD: what = "+"; break;
5920 default: what = "???"; break;
5921 }
5922 switch (iptr->isn_type)
5923 {
5924 case ISN_OPNR: ins = "OPNR"; break;
5925 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5926 case ISN_OPANY: ins = "OPANY"; break;
5927 default: ins = "???"; break;
5928 }
5929 smsg("%s%4d %s %s", pfx, current, ins, what);
5930 }
5931 break;
5932
5933 case ISN_COMPAREBOOL:
5934 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00005935 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005936 case ISN_COMPARENR:
5937 case ISN_COMPAREFLOAT:
5938 case ISN_COMPARESTRING:
5939 case ISN_COMPAREBLOB:
5940 case ISN_COMPARELIST:
5941 case ISN_COMPAREDICT:
5942 case ISN_COMPAREFUNC:
5943 case ISN_COMPAREANY:
5944 {
5945 char *p;
5946 char buf[10];
5947 char *type;
5948
5949 switch (iptr->isn_arg.op.op_type)
5950 {
5951 case EXPR_EQUAL: p = "=="; break;
5952 case EXPR_NEQUAL: p = "!="; break;
5953 case EXPR_GREATER: p = ">"; break;
5954 case EXPR_GEQUAL: p = ">="; break;
5955 case EXPR_SMALLER: p = "<"; break;
5956 case EXPR_SEQUAL: p = "<="; break;
5957 case EXPR_MATCH: p = "=~"; break;
5958 case EXPR_IS: p = "is"; break;
5959 case EXPR_ISNOT: p = "isnot"; break;
5960 case EXPR_NOMATCH: p = "!~"; break;
5961 default: p = "???"; break;
5962 }
5963 STRCPY(buf, p);
5964 if (iptr->isn_arg.op.op_ic == TRUE)
5965 strcat(buf, "?");
5966 switch(iptr->isn_type)
5967 {
5968 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5969 case ISN_COMPARESPECIAL:
5970 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00005971 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005972 case ISN_COMPARENR: type = "COMPARENR"; break;
5973 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5974 case ISN_COMPARESTRING:
5975 type = "COMPARESTRING"; break;
5976 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5977 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5978 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5979 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5980 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5981 default: type = "???"; break;
5982 }
5983
5984 smsg("%s%4d %s %s", pfx, current, type, buf);
5985 }
5986 break;
5987
5988 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5989 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5990
5991 // expression operations
5992 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5993 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5994 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5995 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5996 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5997 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5998 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5999 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6000 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6001 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6002 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6003 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006004 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006005 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6006 iptr->isn_arg.getitem.gi_index,
6007 iptr->isn_arg.getitem.gi_with_op ?
6008 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006009 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6010 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6011 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006012 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6013 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6014
Bram Moolenaar4c137212021-04-19 16:48:48 +02006015 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6016
6017 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6018 case ISN_CHECKTYPE:
6019 {
6020 checktype_T *ct = &iptr->isn_arg.type;
6021 char *tofree;
6022
6023 if (ct->ct_arg_idx == 0)
6024 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6025 type_name(ct->ct_type, &tofree),
6026 (int)ct->ct_off);
6027 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006028 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6029 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006030 type_name(ct->ct_type, &tofree),
6031 (int)ct->ct_off,
6032 (int)ct->ct_arg_idx);
6033 vim_free(tofree);
6034 break;
6035 }
6036 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6037 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6038 iptr->isn_arg.checklen.cl_min_len);
6039 break;
6040 case ISN_SETTYPE:
6041 {
6042 char *tofree;
6043
6044 smsg("%s%4d SETTYPE %s", pfx, current,
6045 type_name(iptr->isn_arg.type.ct_type, &tofree));
6046 vim_free(tofree);
6047 break;
6048 }
6049 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006050 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6051 smsg("%s%4d INVERT %d (!val)", pfx, current,
6052 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006053 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006054 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6055 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006056 break;
6057 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006058 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006059 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006060 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6061 pfx, current,
6062 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006063 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006064 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6065 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006066 break;
6067 case ISN_PUT:
6068 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6069 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006070 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006071 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6072 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006073 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006074 else
6075 smsg("%s%4d PUT %c %ld", pfx, current,
6076 iptr->isn_arg.put.put_regname,
6077 (long)iptr->isn_arg.put.put_lnum);
6078 break;
6079
Bram Moolenaar4c137212021-04-19 16:48:48 +02006080 case ISN_CMDMOD:
6081 {
6082 char_u *buf;
6083 size_t len = produce_cmdmods(
6084 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6085
6086 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006087 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006088 {
6089 (void)produce_cmdmods(
6090 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6091 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6092 vim_free(buf);
6093 }
6094 break;
6095 }
6096 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6097
6098 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006099 smsg("%s%4d PROFILE START line %d", pfx, current,
6100 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006101 break;
6102
6103 case ISN_PROF_END:
6104 smsg("%s%4d PROFILE END", pfx, current);
6105 break;
6106
Bram Moolenaare99d4222021-06-13 14:01:26 +02006107 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006108 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6109 iptr->isn_arg.debug.dbg_break_lnum + 1,
6110 iptr->isn_lnum,
6111 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006112 break;
6113
Bram Moolenaar4c137212021-04-19 16:48:48 +02006114 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6115 iptr->isn_arg.unpack.unp_count,
6116 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6117 break;
6118 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6119 iptr->isn_arg.shuffle.shfl_item,
6120 iptr->isn_arg.shuffle.shfl_up);
6121 break;
6122 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6123
6124 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6125 return;
6126 }
6127
6128 out_flush(); // output one line at a time
6129 ui_breakcheck();
6130 if (got_int)
6131 break;
6132 }
6133}
6134
6135/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006136 * Handle command line completion for the :disassemble command.
6137 */
6138 void
6139set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6140{
6141 char_u *p;
6142
6143 // Default: expand user functions, "debug" and "profile"
6144 xp->xp_context = EXPAND_DISASSEMBLE;
6145 xp->xp_pattern = arg;
6146
6147 // first argument already typed: only user function names
6148 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6149 {
6150 xp->xp_context = EXPAND_USER_FUNC;
6151 xp->xp_pattern = skipwhite(p);
6152 }
6153}
6154
6155/*
6156 * Function given to ExpandGeneric() to obtain the list of :disassemble
6157 * arguments.
6158 */
6159 char_u *
6160get_disassemble_argument(expand_T *xp, int idx)
6161{
6162 if (idx == 0)
6163 return (char_u *)"debug";
6164 if (idx == 1)
6165 return (char_u *)"profile";
6166 return get_user_func_name(xp, idx - 2);
6167}
6168
6169/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006170 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006171 * We don't really need this at runtime, but we do have tests that require it,
6172 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173 */
6174 void
6175ex_disassemble(exarg_T *eap)
6176{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006177 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006178 char_u *fname;
6179 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006180 dfunc_T *dfunc;
6181 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006182 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006183 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006184 compiletype_T compile_type = CT_NONE;
6185
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006186 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006187 {
6188 compile_type = CT_PROFILE;
6189 arg = skipwhite(arg + 7);
6190 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006191 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006192 {
6193 compile_type = CT_DEBUG;
6194 arg = skipwhite(arg + 5);
6195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006197 if (STRNCMP(arg, "<lambda>", 8) == 0)
6198 {
6199 arg += 8;
6200 (void)getdigits(&arg);
6201 fname = vim_strnsave(eap->arg, arg - eap->arg);
6202 }
6203 else
6204 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006205 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006206 if (fname == NULL)
6207 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006208 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006209 return;
6210 }
6211
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006212 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006213 if (ufunc == NULL)
6214 {
6215 char_u *p = untrans_function_name(fname);
6216
6217 if (p != NULL)
6218 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006219 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006220 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006221 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 if (ufunc == NULL)
6223 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006224 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225 return;
6226 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006227 if (func_needs_compiling(ufunc, compile_type)
6228 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006229 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006230 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006232 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 return;
6234 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006235 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236
6237 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006238 switch (compile_type)
6239 {
6240 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006241#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006242 instr = dfunc->df_instr_prof;
6243 instr_count = dfunc->df_instr_prof_count;
6244 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006245#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006246 // FALLTHROUGH
6247 case CT_NONE:
6248 instr = dfunc->df_instr;
6249 instr_count = dfunc->df_instr_count;
6250 break;
6251 case CT_DEBUG:
6252 instr = dfunc->df_instr_debug;
6253 instr_count = dfunc->df_instr_debug_count;
6254 break;
6255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256
Bram Moolenaar4c137212021-04-19 16:48:48 +02006257 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258}
6259
6260/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006261 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262 * list, etc. Mostly like what JavaScript does, except that empty list and
6263 * empty dictionary are FALSE.
6264 */
6265 int
6266tv2bool(typval_T *tv)
6267{
6268 switch (tv->v_type)
6269 {
6270 case VAR_NUMBER:
6271 return tv->vval.v_number != 0;
6272 case VAR_FLOAT:
6273#ifdef FEAT_FLOAT
6274 return tv->vval.v_float != 0.0;
6275#else
6276 break;
6277#endif
6278 case VAR_PARTIAL:
6279 return tv->vval.v_partial != NULL;
6280 case VAR_FUNC:
6281 case VAR_STRING:
6282 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6283 case VAR_LIST:
6284 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6285 case VAR_DICT:
6286 return tv->vval.v_dict != NULL
6287 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6288 case VAR_BOOL:
6289 case VAR_SPECIAL:
6290 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6291 case VAR_JOB:
6292#ifdef FEAT_JOB_CHANNEL
6293 return tv->vval.v_job != NULL;
6294#else
6295 break;
6296#endif
6297 case VAR_CHANNEL:
6298#ifdef FEAT_JOB_CHANNEL
6299 return tv->vval.v_channel != NULL;
6300#else
6301 break;
6302#endif
6303 case VAR_BLOB:
6304 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6305 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006306 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006308 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 break;
6310 }
6311 return FALSE;
6312}
6313
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006314 void
6315emsg_using_string_as(typval_T *tv, int as_number)
6316{
6317 semsg(_(as_number ? e_using_string_as_number_str
6318 : e_using_string_as_bool_str),
6319 tv->vval.v_string == NULL
6320 ? (char_u *)"" : tv->vval.v_string);
6321}
6322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323/*
6324 * If "tv" is a string give an error and return FAIL.
6325 */
6326 int
6327check_not_string(typval_T *tv)
6328{
6329 if (tv->v_type == VAR_STRING)
6330 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006331 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332 clear_tv(tv);
6333 return FAIL;
6334 }
6335 return OK;
6336}
6337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338
6339#endif // FEAT_EVAL