blob: 1232b35bb52f8ce310aa2201eb20fc20c98ffbeb [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200140 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
155 static void
156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200168static garray_T dict_stack = GA_EMPTY;
169
170/*
171 * Put a value on the dict stack. This consumes "tv".
172 */
173 static int
174dict_stack_save(typval_T *tv)
175{
176 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000177 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200178 if (ga_grow(&dict_stack, 1) == FAIL)
179 return FAIL;
180 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
181 ++dict_stack.ga_len;
182 return OK;
183}
184
185/*
186 * Get the typval at top of the dict stack.
187 */
188 static typval_T *
189dict_stack_get_tv(void)
190{
191 if (dict_stack.ga_len == 0)
192 return NULL;
193 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
194}
195
196/*
197 * Get the dict at top of the dict stack.
198 */
199 static dict_T *
200dict_stack_get_dict(void)
201{
202 typval_T *tv;
203
204 if (dict_stack.ga_len == 0)
205 return NULL;
206 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
207 if (tv->v_type == VAR_DICT)
208 return tv->vval.v_dict;
209 return NULL;
210}
211
212/*
213 * Drop an item from the dict stack.
214 */
215 static void
216dict_stack_drop(void)
217{
218 if (dict_stack.ga_len == 0)
219 {
220 iemsg("Dict stack underflow");
221 return;
222 }
223 --dict_stack.ga_len;
224 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
225}
226
227/*
228 * Drop items from the dict stack until the length is equal to "len".
229 */
230 static void
231dict_stack_clear(int len)
232{
233 while (dict_stack.ga_len > len)
234 dict_stack_drop();
235}
236
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100239 * This adds a stack frame and sets the instruction pointer to the start of the
240 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200241 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 *
243 * Stack has:
244 * - current arguments (already there)
245 * - omitted optional argument (default values) added here
246 * - stack frame:
247 * - pointer to calling function
248 * - Index of next instruction in calling function
249 * - previous frame pointer
250 * - reserved space for local variables
251 */
252 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100253call_dfunc(
254 int cdf_idx,
255 partial_T *pt,
256 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100257 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100259 int argcount = argcount_arg;
260 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
261 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200262 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100263 int arg_to_add;
264 int vararg_count = 0;
265 int varcount;
266 int idx;
267 estack_T *entry;
268 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200269 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270
271 if (dfunc->df_deleted)
272 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100273 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000274 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100275 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 return FAIL;
277 }
278
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100279#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100280 if (do_profiling == PROF_YES)
281 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200282 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100283 {
284 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
285 + profile_info_ga.ga_len;
286 ++profile_info_ga.ga_len;
287 CLEAR_POINTER(info);
288 profile_may_start_func(info, ufunc,
289 (((dfunc_T *)def_functions.ga_data)
290 + ectx->ec_dfunc_idx)->df_ufunc);
291 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100292 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100293#endif
294
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200295 // Update uf_has_breakpoint if needed.
296 update_has_breakpoint(ufunc);
297
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200298 // When debugging and using "cont" switches to the not-debugged
299 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200300 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
301 {
302 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
303
304 // compile_def_function() may cause def_functions.ga_data to change
305 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
306 }
307 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200308 {
309 if (did_emsg_cumul + did_emsg == did_emsg_before)
310 semsg(_(e_function_is_not_compiled_str),
311 printable_func_name(ufunc));
312 return FAIL;
313 }
314
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200315 if (ufunc->uf_va_name != NULL)
316 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200317 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200318 // Stack at time of call with 2 varargs:
319 // normal_arg
320 // optional_arg
321 // vararg_1
322 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200323 // After creating the list:
324 // normal_arg
325 // optional_arg
326 // vararg-list
327 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200328 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200329 // After creating the list
330 // normal_arg
331 // (space for optional_arg)
332 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200333 vararg_count = argcount - ufunc->uf_args.ga_len;
334 if (vararg_count < 0)
335 vararg_count = 0;
336 else
337 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200338 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200339 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200340
341 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200342 }
343
Bram Moolenaarfe270812020-04-11 22:31:27 +0200344 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200345 if (arg_to_add < 0)
346 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200347 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200348 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200349 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200350 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200351 return FAIL;
352 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200353
354 // Reserve space for:
355 // - missing arguments
356 // - stack frame
357 // - local variables
358 // - if needed: a counter for number of closures created in
359 // ectx->ec_funcrefs.
360 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200361 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 return FAIL;
363
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100364 // If depth of calling is getting too high, don't execute the function.
365 if (funcdepth_increment() == FAIL)
366 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200367 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100368
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100369 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200370 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100371 {
372 floc = ALLOC_ONE(funclocal_T);
373 if (floc == NULL)
374 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200375 *floc = ectx->ec_funclocal;
376 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100377 }
378
Bram Moolenaarfe270812020-04-11 22:31:27 +0200379 // Move the vararg-list to below the missing optional arguments.
380 if (vararg_count > 0 && arg_to_add > 0)
381 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100382
383 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200384 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200385 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200386 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387
388 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100389 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
390 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200391 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200392 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
393 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100395 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200396 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397
398 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200399 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000400 {
401 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
402
403 tv->v_type = VAR_NUMBER;
404 tv->vval.v_number = 0;
405 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200406 if (dfunc->df_has_closure)
407 {
408 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
409
410 tv->v_type = VAR_NUMBER;
411 tv->vval.v_number = 0;
412 }
413 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100415 if (pt != NULL || ufunc->uf_partial != NULL
416 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100417 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200418 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100419
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200420 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100421 return FAIL;
422 if (pt != NULL)
423 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200424 ref->or_outer = &pt->pt_outer;
425 ++pt->pt_refcount;
426 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100427 }
428 else if (ufunc->uf_partial != NULL)
429 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200430 ref->or_outer = &ufunc->uf_partial->pt_outer;
431 ++ufunc->uf_partial->pt_refcount;
432 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100433 }
434 else
435 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200436 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200437 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200438 {
439 vim_free(ref);
440 return FAIL;
441 }
442 ref->or_outer_allocated = TRUE;
443 ref->or_outer->out_stack = &ectx->ec_stack;
444 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
445 if (ectx->ec_outer_ref != NULL)
446 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100447 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200448 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100449 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100450 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200451 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100452
Bram Moolenaarc970e422021-03-17 15:03:04 +0100453 ++ufunc->uf_calls;
454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 // Set execution state to the start of the called function.
456 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100457 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100458 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200459 if (entry != NULL)
460 {
461 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200462 // Save the current context so it can be restored on return.
463 entry->es_save_sctx = current_sctx;
464 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200465 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100466
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200467 // Start execution at the first instruction.
468 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469
470 return OK;
471}
472
473// Get pointer to item in the stack.
474#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
475
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000476// Double linked list of funcstack_T in use.
477static funcstack_T *first_funcstack = NULL;
478
479 static void
480add_funcstack_to_list(funcstack_T *funcstack)
481{
482 // Link in list of funcstacks.
483 if (first_funcstack != NULL)
484 first_funcstack->fs_prev = funcstack;
485 funcstack->fs_next = first_funcstack;
486 funcstack->fs_prev = NULL;
487 first_funcstack = funcstack;
488}
489
490 static void
491remove_funcstack_from_list(funcstack_T *funcstack)
492{
493 if (funcstack->fs_prev == NULL)
494 first_funcstack = funcstack->fs_next;
495 else
496 funcstack->fs_prev->fs_next = funcstack->fs_next;
497 if (funcstack->fs_next != NULL)
498 funcstack->fs_next->fs_prev = funcstack->fs_prev;
499}
500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200502 * Used when returning from a function: Check if any closure is still
503 * referenced. If so then move the arguments and variables to a separate piece
504 * of stack to be used when the closure is called.
505 * When "free_arguments" is TRUE the arguments are to be freed.
506 * Returns FAIL when out of memory.
507 */
508 static int
509handle_closure_in_use(ectx_T *ectx, int free_arguments)
510{
511 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
512 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200513 int argcount;
514 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515 int idx;
516 typval_T *tv;
517 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200518 garray_T *gap = &ectx->ec_funcrefs;
519 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200520
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200521 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200522 return OK; // function was freed
523 if (dfunc->df_has_closure == 0)
524 return OK; // no closures
525 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
526 closure_count = tv->vval.v_number;
527 if (closure_count == 0)
528 return OK; // no funcrefs created
529
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200530 argcount = ufunc_argcount(dfunc->df_ufunc);
531 top = ectx->ec_frame_idx - argcount;
532
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200533 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200535 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200536 partial_T *pt;
537 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200538
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200539 if (off < 0)
540 continue; // count is off or already done
541 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200542 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200543 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200544 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200545 int i;
546
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000547 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200548 // unreferenced on return.
549 for (i = 0; i < dfunc->df_varcount; ++i)
550 {
551 typval_T *stv = STACK_TV(ectx->ec_frame_idx
552 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200553 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200554 --refcount;
555 }
556 if (refcount > 1)
557 {
558 closure_in_use = TRUE;
559 break;
560 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200561 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200562 }
563
564 if (closure_in_use)
565 {
566 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
567 typval_T *stack;
568
569 // A closure is using the arguments and/or local variables.
570 // Move them to the called function.
571 if (funcstack == NULL)
572 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000573
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200574 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
575 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200576 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
577 funcstack->fs_ga.ga_data = stack;
578 if (stack == NULL)
579 {
580 vim_free(funcstack);
581 return FAIL;
582 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000583 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200584
585 // Move or copy the arguments.
586 for (idx = 0; idx < argcount; ++idx)
587 {
588 tv = STACK_TV(top + idx);
589 if (free_arguments)
590 {
591 *(stack + idx) = *tv;
592 tv->v_type = VAR_UNKNOWN;
593 }
594 else
595 copy_tv(tv, stack + idx);
596 }
597 // Move the local variables.
598 for (idx = 0; idx < dfunc->df_varcount; ++idx)
599 {
600 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200601
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200602 // A partial created for a local function, that is also used as a
603 // local variable, has a reference count for the variable, thus
604 // will never go down to zero. When all these refcounts are one
605 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000606 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200607 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
608 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200609 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200610
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200611 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200612 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
613 gap->ga_len - closure_count + i])
614 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200615 }
616
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200617 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200618 tv->v_type = VAR_UNKNOWN;
619 }
620
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200621 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200623 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
624 - closure_count + idx];
625 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200627 ++funcstack->fs_refcount;
628 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100629 pt->pt_outer.out_stack = &funcstack->fs_ga;
630 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200631 }
632 }
633 }
634
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200635 for (idx = 0; idx < closure_count; ++idx)
636 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
637 - closure_count + idx]);
638 gap->ga_len -= closure_count;
639 if (gap->ga_len == 0)
640 ga_clear(gap);
641
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200642 return OK;
643}
644
645/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200646 * Called when a partial is freed or its reference count goes down to one. The
647 * funcstack may be the only reference to the partials in the local variables.
648 * Go over all of them, the funcref and can be freed if all partials
649 * referencing the funcstack have a reference count of one.
650 */
651 void
652funcstack_check_refcount(funcstack_T *funcstack)
653{
654 int i;
655 garray_T *gap = &funcstack->fs_ga;
656 int done = 0;
657
658 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
659 return;
660 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
661 {
662 typval_T *tv = ((typval_T *)gap->ga_data) + i;
663
664 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
665 && tv->vval.v_partial->pt_funcstack == funcstack
666 && tv->vval.v_partial->pt_refcount == 1)
667 ++done;
668 }
669 if (done == funcstack->fs_min_refcount)
670 {
671 typval_T *stack = gap->ga_data;
672
673 // All partials referencing the funcstack have a reference count of
674 // one, thus the funcstack is no longer of use.
675 for (i = 0; i < gap->ga_len; ++i)
676 clear_tv(stack + i);
677 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000678 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200679 vim_free(funcstack);
680 }
681}
682
683/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000684 * For garbage collecting: set references in all variables referenced by
685 * all funcstacks.
686 */
687 int
688set_ref_in_funcstacks(int copyID)
689{
690 funcstack_T *funcstack;
691
692 for (funcstack = first_funcstack; funcstack != NULL;
693 funcstack = funcstack->fs_next)
694 {
695 typval_T *stack = funcstack->fs_ga.ga_data;
696 int i;
697
698 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
699 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
700 return TRUE; // abort
701 }
702 return FALSE;
703}
704
705/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 * Return from the current function.
707 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200709func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100712 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200713 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
714 + ectx->ec_dfunc_idx;
715 int argcount = ufunc_argcount(dfunc->df_ufunc);
716 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200717 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100718 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
719 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200720 funclocal_T *floc;
721#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100722 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
723 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724
Bram Moolenaar12d26532021-02-19 19:13:21 +0100725 if (do_profiling == PROF_YES)
726 {
727 ufunc_T *caller = prev_dfunc->df_ufunc;
728
729 if (dfunc->df_ufunc->uf_profiling
730 || (caller != NULL && caller->uf_profiling))
731 {
732 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
733 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
734 --profile_info_ga.ga_len;
735 }
736 }
737#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000738
739 // No check for uf_refcount being zero, cannot think of a way that would
740 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100741 --dfunc->df_ufunc->uf_calls;
742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200744 entry = estack_pop();
745 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200746 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200748 if (handle_closure_in_use(ectx, TRUE) == FAIL)
749 return FAIL;
750
751 // Clear the arguments.
752 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
753 clear_tv(STACK_TV(idx));
754
755 // Clear local variables and temp values, but not the return value.
756 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100757 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100759
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100760 // The return value should be on top of the stack. However, when aborting
761 // it may not be there and ec_frame_idx is the top of the stack.
762 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100763 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100764 ret_idx = 0;
765
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200766 if (ectx->ec_outer_ref != NULL)
767 {
768 if (ectx->ec_outer_ref->or_outer_allocated)
769 vim_free(ectx->ec_outer_ref->or_outer);
770 partial_unref(ectx->ec_outer_ref->or_partial);
771 vim_free(ectx->ec_outer_ref);
772 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100773
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100774 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100775 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100776 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
777 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200778 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
779 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200780 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100781 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100782 floc = (void *)STACK_TV(ectx->ec_frame_idx
783 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200784 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100785 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
786 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200787
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100788 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200789 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100790 else
791 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200792 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100793 vim_free(floc);
794 }
795
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100796 if (ret_idx > 0)
797 {
798 // Reset the stack to the position before the call, with a spot for the
799 // return value, moved there from above the frame.
800 ectx->ec_stack.ga_len = top + 1;
801 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
802 }
803 else
804 // Reset the stack to the position before the call.
805 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200806
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100807 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200808 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200809 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810}
811
812#undef STACK_TV
813
814/*
815 * Prepare arguments and rettv for calling a builtin or user function.
816 */
817 static int
818call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
819{
820 int idx;
821 typval_T *tv;
822
823 // Move arguments from bottom of the stack to argvars[] and add terminator.
824 for (idx = 0; idx < argcount; ++idx)
825 argvars[idx] = *STACK_TV_BOT(idx - argcount);
826 argvars[argcount].v_type = VAR_UNKNOWN;
827
828 // Result replaces the arguments on the stack.
829 if (argcount > 0)
830 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200831 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 return FAIL;
833 else
834 ++ectx->ec_stack.ga_len;
835
836 // Default return value is zero.
837 tv = STACK_TV_BOT(-1);
838 tv->v_type = VAR_NUMBER;
839 tv->vval.v_number = 0;
840
841 return OK;
842}
843
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200844// Ugly global to avoid passing the execution context around through many
845// layers.
846static ectx_T *current_ectx = NULL;
847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848/*
849 * Call a builtin function by index.
850 */
851 static int
852call_bfunc(int func_idx, int argcount, ectx_T *ectx)
853{
854 typval_T argvars[MAX_FUNC_ARGS];
855 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100856 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200857 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200858 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859
860 if (call_prepare(argcount, argvars, ectx) == FAIL)
861 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200862 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200864 // Call the builtin function. Set "current_ectx" so that when it
865 // recursively invokes call_def_function() a closure context can be set.
866 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200868 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200869 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870
871 // Clear the arguments.
872 for (idx = 0; idx < argcount; ++idx)
873 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200874
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100875 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200876 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 return OK;
878}
879
880/*
881 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100882 * If the function is compiled this will add a stack frame and set the
883 * instruction pointer at the start of the function.
884 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200885 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100886 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 */
888 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100889call_ufunc(
890 ufunc_T *ufunc,
891 partial_T *pt,
892 int argcount,
893 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200894 isn_T *iptr,
895 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896{
897 typval_T argvars[MAX_FUNC_ARGS];
898 funcexe_T funcexe;
899 int error;
900 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100901 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200902 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903
Bram Moolenaare99d4222021-06-13 14:01:26 +0200904 if (func_needs_compiling(ufunc, compile_type)
905 && compile_def_function(ufunc, FALSE, compile_type, NULL)
906 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200907 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200908 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100909 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100910 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100911 if (error != FCERR_UNKNOWN)
912 {
913 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000914 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100915 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000916 semsg(_(e_not_enough_arguments_for_function_str),
917 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100918 return FAIL;
919 }
920
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100921 // The function has been compiled, can call it quickly. For a function
922 // that was defined later: we can call it directly next time.
923 if (iptr != NULL)
924 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100925 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100926 iptr->isn_type = ISN_DCALL;
927 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
928 iptr->isn_arg.dfunc.cdf_argcount = argcount;
929 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200930 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932
933 if (call_prepare(argcount, argvars, ectx) == FAIL)
934 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200935 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000936 funcexe.fe_evaluate = TRUE;
937 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938
939 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000941 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942
943 // Clear the arguments.
944 for (idx = 0; idx < argcount; ++idx)
945 clear_tv(&argvars[idx]);
946
947 if (error != FCERR_NONE)
948 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000949 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 return FAIL;
951 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100952 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200953 // Error other than from calling the function itself.
954 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 return OK;
956}
957
958/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100959 * If command modifiers were applied restore them.
960 */
961 static void
962may_restore_cmdmod(funclocal_T *funclocal)
963{
964 if (funclocal->floc_restore_cmdmod)
965 {
966 cmdmod.cmod_filter_regmatch.regprog = NULL;
967 undo_cmdmod(&cmdmod);
968 cmdmod = funclocal->floc_save_cmdmod;
969 funclocal->floc_restore_cmdmod = FALSE;
970 }
971}
972
973/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200974 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
975 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200976 */
977 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200978vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200979{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200980 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200981}
982
983/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 * Execute a function by "name".
985 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100986 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 * Returns FAIL if not found without an error message.
988 */
989 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100990call_by_name(
991 char_u *name,
992 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100993 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200994 isn_T *iptr,
995 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996{
997 ufunc_T *ufunc;
998
999 if (builtin_function(name, -1))
1000 {
1001 int func_idx = find_internal_func(name);
1002
1003 if (func_idx < 0)
1004 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001005 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 return FAIL;
1007 return call_bfunc(func_idx, argcount, ectx);
1008 }
1009
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001010 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001011
1012 if (ufunc == NULL)
1013 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001014 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001015
1016 if (script_autoload(name, TRUE))
1017 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001018 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001019
1020 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001021 return FAIL; // bail out if loading the script caused an error
1022 }
1023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001025 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001026 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001027 {
1028 int i;
1029 typval_T *argv = STACK_TV_BOT(0) - argcount;
1030
1031 // The function can change at runtime, check that the argument
1032 // types are correct.
1033 for (i = 0; i < argcount; ++i)
1034 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001035 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001036
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001037 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001038 type = ufunc->uf_arg_types[i];
1039 else if (ufunc->uf_va_type != NULL)
1040 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001041 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001042 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001043 return FAIL;
1044 }
1045 }
1046
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001047 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049
1050 return FAIL;
1051}
1052
1053 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001054call_partial(
1055 typval_T *tv,
1056 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001057 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001059 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001060 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001062 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001063 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064
1065 if (tv->v_type == VAR_PARTIAL)
1066 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001067 partial_T *pt = tv->vval.v_partial;
1068 int i;
1069
1070 if (pt->pt_argc > 0)
1071 {
1072 // Make space for arguments from the partial, shift the "argcount"
1073 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001074 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001075 return FAIL;
1076 for (i = 1; i <= argcount; ++i)
1077 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1078 ectx->ec_stack.ga_len += pt->pt_argc;
1079 argcount += pt->pt_argc;
1080
1081 // copy the arguments from the partial onto the stack
1082 for (i = 0; i < pt->pt_argc; ++i)
1083 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1084 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001085 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086
1087 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001088 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090 name = pt->pt_name;
1091 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001092 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001094 if (name != NULL)
1095 {
1096 char_u fname_buf[FLEN_FIXED + 1];
1097 char_u *tofree = NULL;
1098 int error = FCERR_NONE;
1099 char_u *fname;
1100
1101 // May need to translate <SNR>123_ to K_SNR.
1102 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1103 if (error != FCERR_NONE)
1104 res = FAIL;
1105 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001106 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001107 vim_free(tofree);
1108 }
1109
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001110 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 {
1112 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001113 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001114 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 return FAIL;
1116 }
1117 return OK;
1118}
1119
1120/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001121 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1122 * TRUE.
1123 */
1124 static int
1125error_if_locked(int lock, char *error)
1126{
1127 if (lock & (VAR_LOCKED | VAR_FIXED))
1128 {
1129 emsg(_(error));
1130 return TRUE;
1131 }
1132 return FALSE;
1133}
1134
1135/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001136 * Give an error if "tv" is not a number and return FAIL.
1137 */
1138 static int
1139check_for_number(typval_T *tv)
1140{
1141 if (tv->v_type != VAR_NUMBER)
1142 {
1143 semsg(_(e_expected_str_but_got_str),
1144 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1145 return FAIL;
1146 }
1147 return OK;
1148}
1149
1150/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001151 * Store "tv" in variable "name".
1152 * This is for s: and g: variables.
1153 */
1154 static void
1155store_var(char_u *name, typval_T *tv)
1156{
1157 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001158 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001159
Bram Moolenaard877a572021-04-01 19:42:48 +02001160 if (tv->v_lock)
1161 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001162 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001163 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001164 restore_funccal();
1165}
1166
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001167/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001168 * Convert "tv" to a string.
1169 * Return FAIL if not allowed.
1170 */
1171 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001172do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001173{
1174 if (tv->v_type != VAR_STRING)
1175 {
1176 char_u *str;
1177
1178 if (is_2string_any)
1179 {
1180 switch (tv->v_type)
1181 {
1182 case VAR_SPECIAL:
1183 case VAR_BOOL:
1184 case VAR_NUMBER:
1185 case VAR_FLOAT:
1186 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001187
1188 case VAR_LIST:
1189 if (tolerant)
1190 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001191 char_u *s, *e, *p;
1192 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001193
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001194 ga_init2(&ga, sizeof(char_u *), 1);
1195
1196 // Convert to NL separated items, then
1197 // escape the items and replace the NL with
1198 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001199 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001200 if (str == NULL)
1201 return FAIL;
1202 s = str;
1203 while ((e = vim_strchr(s, '\n')) != NULL)
1204 {
1205 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001206 p = vim_strsave_fnameescape(s,
1207 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001208 if (p != NULL)
1209 {
1210 ga_concat(&ga, p);
1211 ga_concat(&ga, (char_u *)" ");
1212 vim_free(p);
1213 }
1214 s = e + 1;
1215 }
1216 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001217 clear_tv(tv);
1218 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001219 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001220 return OK;
1221 }
1222 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001223 default: to_string_error(tv->v_type);
1224 return FAIL;
1225 }
1226 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001227 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001228 clear_tv(tv);
1229 tv->v_type = VAR_STRING;
1230 tv->vval.v_string = str;
1231 }
1232 return OK;
1233}
1234
1235/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001236 * When the value of "sv" is a null list of dict, allocate it.
1237 */
1238 static void
1239allocate_if_null(typval_T *tv)
1240{
1241 switch (tv->v_type)
1242 {
1243 case VAR_LIST:
1244 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001245 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001246 break;
1247 case VAR_DICT:
1248 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001249 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001250 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001251 case VAR_BLOB:
1252 if (tv->vval.v_blob == NULL)
1253 (void)rettv_blob_alloc(tv);
1254 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001255 default:
1256 break;
1257 }
1258}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001259
Bram Moolenaare7525c52021-01-09 13:20:37 +01001260/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001261 * Return the character "str[index]" where "index" is the character index,
1262 * including composing characters.
1263 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001264 */
1265 char_u *
1266char_from_string(char_u *str, varnumber_T index)
1267{
1268 size_t nbyte = 0;
1269 varnumber_T nchar = index;
1270 size_t slen;
1271
1272 if (str == NULL)
1273 return NULL;
1274 slen = STRLEN(str);
1275
Bram Moolenaarff871402021-03-26 13:34:05 +01001276 // Do the same as for a list: a negative index counts from the end.
1277 // Optimization to check the first byte to be below 0x80 (and no composing
1278 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001279 if (index < 0)
1280 {
1281 int clen = 0;
1282
1283 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001284 {
1285 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1286 ++nbyte;
1287 else if (enc_utf8)
1288 nbyte += utfc_ptr2len(str + nbyte);
1289 else
1290 nbyte += mb_ptr2len(str + nbyte);
1291 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001292 nchar = clen + index;
1293 if (nchar < 0)
1294 // unlike list: index out of range results in empty string
1295 return NULL;
1296 }
1297
1298 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001299 {
1300 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1301 ++nbyte;
1302 else if (enc_utf8)
1303 nbyte += utfc_ptr2len(str + nbyte);
1304 else
1305 nbyte += mb_ptr2len(str + nbyte);
1306 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001307 if (nbyte >= slen)
1308 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001309 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001310}
1311
1312/*
1313 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001314 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001315 * If going over the end return "str_len".
1316 * If "idx" is negative count from the end, -1 is the last character.
1317 * When going over the start return -1.
1318 */
1319 static long
1320char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1321{
1322 varnumber_T nchar = idx;
1323 size_t nbyte = 0;
1324
1325 if (nchar >= 0)
1326 {
1327 while (nchar > 0 && nbyte < str_len)
1328 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001329 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001330 --nchar;
1331 }
1332 }
1333 else
1334 {
1335 nbyte = str_len;
1336 while (nchar < 0 && nbyte > 0)
1337 {
1338 --nbyte;
1339 nbyte -= mb_head_off(str, str + nbyte);
1340 ++nchar;
1341 }
1342 if (nchar < 0)
1343 return -1;
1344 }
1345 return (long)nbyte;
1346}
1347
1348/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001349 * Return the slice "str[first : last]" using character indexes. Composing
1350 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001351 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001352 * Return NULL when the result is empty.
1353 */
1354 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001355string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001356{
1357 long start_byte, end_byte;
1358 size_t slen;
1359
1360 if (str == NULL)
1361 return NULL;
1362 slen = STRLEN(str);
1363 start_byte = char_idx2byte(str, slen, first);
1364 if (start_byte < 0)
1365 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001366 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001367 end_byte = (long)slen;
1368 else
1369 {
1370 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001371 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001372 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001373 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001374 }
1375
1376 if (start_byte >= (long)slen || end_byte <= start_byte)
1377 return NULL;
1378 return vim_strnsave(str + start_byte, end_byte - start_byte);
1379}
1380
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001381/*
1382 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1383 * When "dfunc_idx" is negative don't give an error.
1384 * Returns NULL for an error.
1385 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001386 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001387get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001388{
1389 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001390 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1391 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001392 svar_T *sv;
1393
1394 if (sref->sref_seq != si->sn_script_seq)
1395 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001396 // The script was reloaded after the function was compiled, the
1397 // script_idx may not be valid.
1398 if (dfunc != NULL)
1399 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1400 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001401 return NULL;
1402 }
1403 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001404 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001405 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001406 if (dfunc != NULL)
1407 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001408 return NULL;
1409 }
1410 return sv;
1411}
1412
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001413/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001414 * Function passed to do_cmdline() for splitting a script joined by NL
1415 * characters.
1416 */
1417 static char_u *
1418get_split_sourceline(
1419 int c UNUSED,
1420 void *cookie,
1421 int indent UNUSED,
1422 getline_opt_T options UNUSED)
1423{
1424 source_cookie_T *sp = (source_cookie_T *)cookie;
1425 char_u *p;
1426 char_u *line;
1427
1428 if (*sp->nextline == NUL)
1429 return NULL;
1430 p = vim_strchr(sp->nextline, '\n');
1431 if (p == NULL)
1432 {
1433 line = vim_strsave(sp->nextline);
1434 sp->nextline += STRLEN(sp->nextline);
1435 }
1436 else
1437 {
1438 line = vim_strnsave(sp->nextline, p - sp->nextline);
1439 sp->nextline = p + 1;
1440 }
1441 return line;
1442}
1443
1444/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 * Execute a function by "name".
1446 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001447 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 */
1449 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001450call_eval_func(
1451 char_u *name,
1452 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001453 ectx_T *ectx,
1454 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455{
Bram Moolenaared677f52020-08-12 16:38:10 +02001456 int called_emsg_before = called_emsg;
1457 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001459 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001460 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001462 dictitem_T *v;
1463
1464 v = find_var(name, NULL, FALSE);
1465 if (v == NULL)
1466 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001467 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001468 return FAIL;
1469 }
1470 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1471 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001472 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001473 return FAIL;
1474 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001475 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001477 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478}
1479
1480/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001481 * When a function reference is used, fill a partial with the information
1482 * needed, especially when it is used as a closure.
1483 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001484 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001485fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1486{
1487 pt->pt_func = ufunc;
1488 pt->pt_refcount = 1;
1489
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001490 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001491 {
1492 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1493 + ectx->ec_dfunc_idx;
1494
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001495 // The closure may need to find arguments and local variables in the
1496 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001497 pt->pt_outer.out_stack = &ectx->ec_stack;
1498 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001499 if (ectx->ec_outer_ref != NULL)
1500 {
1501 // The current context already has a context, link to that one.
1502 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1503 if (ectx->ec_outer_ref->or_partial != NULL)
1504 {
1505 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1506 ++pt->pt_outer.out_up_partial->pt_refcount;
1507 }
1508 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001509
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001510 // If this function returns and the closure is still being used, we
1511 // need to make a copy of the context (arguments and local variables).
1512 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001513 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001514 {
1515 vim_free(pt);
1516 return FAIL;
1517 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001518 // Extra variable keeps the count of closures created in the current
1519 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001520 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1521 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1522
1523 ((partial_T **)ectx->ec_funcrefs.ga_data)
1524 [ectx->ec_funcrefs.ga_len] = pt;
1525 ++pt->pt_refcount;
1526 ++ectx->ec_funcrefs.ga_len;
1527 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001528 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001529 return OK;
1530}
1531
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001532/*
1533 * Execute iptr->isn_arg.string as an Ex command.
1534 */
1535 static int
1536exec_command(isn_T *iptr)
1537{
1538 source_cookie_T cookie;
1539
1540 SOURCING_LNUM = iptr->isn_lnum;
1541 // Pass getsourceline to get an error for a missing ":end"
1542 // command.
1543 CLEAR_FIELD(cookie);
1544 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1545 if (do_cmdline(iptr->isn_arg.string,
1546 getsourceline, &cookie,
1547 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1548 || did_emsg)
1549 return FAIL;
1550 return OK;
1551}
1552
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001553// used for v_instr of typval of VAR_INSTR
1554struct instr_S {
1555 ectx_T *instr_ectx;
1556 isn_T *instr_instr;
1557};
1558
Bram Moolenaar4c137212021-04-19 16:48:48 +02001559// used for substitute_instr
1560typedef struct subs_expr_S {
1561 ectx_T *subs_ectx;
1562 isn_T *subs_instr;
1563 int subs_status;
1564} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565
1566// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001567#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568
1569// Get pointer to item at the bottom of the stack, -1 is the bottom.
1570#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001571#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 +01001572
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001573// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001574#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 +01001575
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001576// Set when calling do_debug().
1577static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001578static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001579
1580/*
1581 * When debugging lookup "name" and return the typeval.
1582 * When not found return NULL.
1583 */
1584 typval_T *
1585lookup_debug_var(char_u *name)
1586{
1587 int idx;
1588 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001589 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001590 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001591 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001592
1593 if (ectx == NULL)
1594 return NULL;
1595 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1596
1597 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001598 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001599 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001600 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001601 return STACK_TV_VAR(idx);
1602 }
1603
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001604 // Go through argument names.
1605 ufunc = dfunc->df_ufunc;
1606 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1607 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1608 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1609 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1610 - varargs_off + idx);
1611 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1612 return STACK_TV(ectx->ec_frame_idx - 1);
1613
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001614 return NULL;
1615}
1616
Bram Moolenaar26a44842021-09-02 18:49:06 +02001617/*
1618 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1619 * breakpoint was set in that function or when there is any expression.
1620 */
1621 int
1622may_break_in_function(ufunc_T *ufunc)
1623{
1624 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1625}
1626
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001627 static void
1628handle_debug(isn_T *iptr, ectx_T *ectx)
1629{
1630 char_u *line;
1631 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1632 + ectx->ec_dfunc_idx)->df_ufunc;
1633 isn_T *ni;
1634 int end_lnum = iptr->isn_lnum;
1635 garray_T ga;
1636 int lnum;
1637
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001638 if (ex_nesting_level > debug_break_level)
1639 {
1640 linenr_T breakpoint;
1641
Bram Moolenaar26a44842021-09-02 18:49:06 +02001642 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001643 return;
1644
1645 // check for the next breakpoint if needed
1646 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001647 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001648 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1649 return;
1650 }
1651
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001652 SOURCING_LNUM = iptr->isn_lnum;
1653 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001654 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001655
1656 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1657 if (ni->isn_type == ISN_DEBUG
1658 || ni->isn_type == ISN_RETURN
1659 || ni->isn_type == ISN_RETURN_VOID)
1660 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001661 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001662 break;
1663 }
1664
1665 if (end_lnum > iptr->isn_lnum)
1666 {
1667 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001668 for (lnum = iptr->isn_lnum; lnum < end_lnum
1669 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001670 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001671 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001672
Bram Moolenaar303215d2021-07-07 20:10:43 +02001673 if (p == NULL)
1674 continue; // left over from continuation line
1675 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001676 if (*p == '#')
1677 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001678 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001679 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1680 if (STRNCMP(p, "def ", 4) == 0)
1681 break;
1682 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001683 line = ga_concat_strings(&ga, " ");
1684 vim_free(ga.ga_data);
1685 }
1686 else
1687 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001688
Dominique Pelle6e969552021-06-17 13:53:41 +02001689 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001690 debug_context = NULL;
1691
1692 if (end_lnum > iptr->isn_lnum)
1693 vim_free(line);
1694}
1695
Bram Moolenaar4c137212021-04-19 16:48:48 +02001696/*
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001697 * Store a value in a list or dict variable.
1698 * Returns OK, FAIL or NOTDONE (uncatchable error).
1699 */
1700 static int
1701execute_storeindex(isn_T *iptr, ectx_T *ectx)
1702{
1703 vartype_T dest_type = iptr->isn_arg.vartype;
1704 typval_T *tv;
1705 typval_T *tv_idx = STACK_TV_BOT(-2);
1706 typval_T *tv_dest = STACK_TV_BOT(-1);
1707 int status = OK;
1708
1709 // Stack contains:
1710 // -3 value to be stored
1711 // -2 index
1712 // -1 dict or list
1713 tv = STACK_TV_BOT(-3);
1714 SOURCING_LNUM = iptr->isn_lnum;
1715 if (dest_type == VAR_ANY)
1716 {
1717 dest_type = tv_dest->v_type;
1718 if (dest_type == VAR_DICT)
1719 status = do_2string(tv_idx, TRUE, FALSE);
1720 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1721 {
1722 emsg(_(e_number_expected));
1723 status = FAIL;
1724 }
1725 }
1726 else if (dest_type != tv_dest->v_type)
1727 {
1728 // just in case, should be OK
1729 semsg(_(e_expected_str_but_got_str),
1730 vartype_name(dest_type),
1731 vartype_name(tv_dest->v_type));
1732 status = FAIL;
1733 }
1734
1735 if (status == OK && dest_type == VAR_LIST)
1736 {
1737 long lidx = (long)tv_idx->vval.v_number;
1738 list_T *list = tv_dest->vval.v_list;
1739
1740 if (list == NULL)
1741 {
1742 emsg(_(e_list_not_set));
1743 return FAIL;
1744 }
1745 if (lidx < 0 && list->lv_len + lidx >= 0)
1746 // negative index is relative to the end
1747 lidx = list->lv_len + lidx;
1748 if (lidx < 0 || lidx > list->lv_len)
1749 {
1750 semsg(_(e_list_index_out_of_range_nr), lidx);
1751 return FAIL;
1752 }
1753 if (lidx < list->lv_len)
1754 {
1755 listitem_T *li = list_find(list, lidx);
1756
1757 if (error_if_locked(li->li_tv.v_lock, e_cannot_change_list_item))
1758 return FAIL;
1759 // overwrite existing list item
1760 clear_tv(&li->li_tv);
1761 li->li_tv = *tv;
1762 }
1763 else
1764 {
1765 if (error_if_locked(list->lv_lock, e_cannot_change_list))
1766 return FAIL;
1767 // append to list, only fails when out of memory
1768 if (list_append_tv(list, tv) == FAIL)
1769 return NOTDONE;
1770 clear_tv(tv);
1771 }
1772 }
1773 else if (status == OK && dest_type == VAR_DICT)
1774 {
1775 char_u *key = tv_idx->vval.v_string;
1776 dict_T *dict = tv_dest->vval.v_dict;
1777 dictitem_T *di;
1778
1779 SOURCING_LNUM = iptr->isn_lnum;
1780 if (dict == NULL)
1781 {
1782 emsg(_(e_dictionary_not_set));
1783 return FAIL;
1784 }
1785 if (key == NULL)
1786 key = (char_u *)"";
1787 di = dict_find(dict, key, -1);
1788 if (di != NULL)
1789 {
1790 if (error_if_locked(di->di_tv.v_lock, e_cannot_change_dict_item))
1791 return FAIL;
1792 // overwrite existing value
1793 clear_tv(&di->di_tv);
1794 di->di_tv = *tv;
1795 }
1796 else
1797 {
1798 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1799 return FAIL;
1800 // add to dict, only fails when out of memory
1801 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1802 return NOTDONE;
1803 clear_tv(tv);
1804 }
1805 }
1806 else if (status == OK && dest_type == VAR_BLOB)
1807 {
1808 long lidx = (long)tv_idx->vval.v_number;
1809 blob_T *blob = tv_dest->vval.v_blob;
1810 varnumber_T nr;
1811 int error = FALSE;
1812 int len;
1813
1814 if (blob == NULL)
1815 {
1816 emsg(_(e_blob_not_set));
1817 return FAIL;
1818 }
1819 len = blob_len(blob);
1820 if (lidx < 0 && len + lidx >= 0)
1821 // negative index is relative to the end
1822 lidx = len + lidx;
1823
1824 // Can add one byte at the end.
1825 if (lidx < 0 || lidx > len)
1826 {
1827 semsg(_(e_blob_index_out_of_range_nr), lidx);
1828 return FAIL;
1829 }
1830 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1831 return FAIL;
1832 nr = tv_get_number_chk(tv, &error);
1833 if (error)
1834 return FAIL;
1835 blob_set_append(blob, lidx, nr);
1836 }
1837 else
1838 {
1839 status = FAIL;
1840 semsg(_(e_cannot_index_str), vartype_name(dest_type));
1841 }
1842
1843 clear_tv(tv_idx);
1844 clear_tv(tv_dest);
1845 ectx->ec_stack.ga_len -= 3;
1846 if (status == FAIL)
1847 {
1848 clear_tv(tv);
1849 return FAIL;
1850 }
1851 return OK;
1852}
1853
1854/*
1855 * Store a value in a blob range.
1856 */
1857 static int
1858execute_storerange(isn_T *iptr, ectx_T *ectx)
1859{
1860 typval_T *tv;
1861 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1862 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1863 typval_T *tv_dest = STACK_TV_BOT(-1);
1864 int status = OK;
1865
1866 // Stack contains:
1867 // -4 value to be stored
1868 // -3 first index or "none"
1869 // -2 second index or "none"
1870 // -1 destination list or blob
1871 tv = STACK_TV_BOT(-4);
1872 if (tv_dest->v_type == VAR_LIST)
1873 {
1874 long n1;
1875 long n2;
1876 int error = FALSE;
1877
1878 SOURCING_LNUM = iptr->isn_lnum;
1879 n1 = (long)tv_get_number_chk(tv_idx1, &error);
1880 if (error)
1881 status = FAIL;
1882 else
1883 {
1884 if (tv_idx2->v_type == VAR_SPECIAL
1885 && tv_idx2->vval.v_number == VVAL_NONE)
1886 n2 = list_len(tv_dest->vval.v_list) - 1;
1887 else
1888 n2 = (long)tv_get_number_chk(tv_idx2, &error);
1889 if (error)
1890 status = FAIL;
1891 else
1892 {
1893 listitem_T *li1 = check_range_index_one(
1894 tv_dest->vval.v_list, &n1, FALSE);
1895
1896 if (li1 == NULL)
1897 status = FAIL;
1898 else
1899 {
1900 status = check_range_index_two(
1901 tv_dest->vval.v_list,
1902 &n1, li1, &n2, FALSE);
1903 if (status != FAIL)
1904 status = list_assign_range(
1905 tv_dest->vval.v_list,
1906 tv->vval.v_list,
1907 n1,
1908 n2,
1909 tv_idx2->v_type == VAR_SPECIAL,
1910 (char_u *)"=",
1911 (char_u *)"[unknown]");
1912 }
1913 }
1914 }
1915 }
1916 else if (tv_dest->v_type == VAR_BLOB)
1917 {
1918 varnumber_T n1;
1919 varnumber_T n2;
1920 int error = FALSE;
1921
1922 n1 = tv_get_number_chk(tv_idx1, &error);
1923 if (error)
1924 status = FAIL;
1925 else
1926 {
1927 if (tv_idx2->v_type == VAR_SPECIAL
1928 && tv_idx2->vval.v_number == VVAL_NONE)
1929 n2 = blob_len(tv_dest->vval.v_blob) - 1;
1930 else
1931 n2 = tv_get_number_chk(tv_idx2, &error);
1932 if (error)
1933 status = FAIL;
1934 else
1935 {
1936 long bloblen = blob_len(tv_dest->vval.v_blob);
1937
1938 if (check_blob_index(bloblen,
1939 n1, FALSE) == FAIL
1940 || check_blob_range(bloblen,
1941 n1, n2, FALSE) == FAIL)
1942 status = FAIL;
1943 else
1944 status = blob_set_range(
1945 tv_dest->vval.v_blob, n1, n2, tv);
1946 }
1947 }
1948 }
1949 else
1950 {
1951 status = FAIL;
1952 emsg(_(e_blob_required));
1953 }
1954
1955 clear_tv(tv_idx1);
1956 clear_tv(tv_idx2);
1957 clear_tv(tv_dest);
1958 ectx->ec_stack.ga_len -= 4;
1959 clear_tv(tv);
1960
1961 return status;
1962}
1963
1964/*
1965 * Unlet item in list or dict variable.
1966 */
1967 static int
1968execute_unletindex(isn_T *iptr, ectx_T *ectx)
1969{
1970 typval_T *tv_idx = STACK_TV_BOT(-2);
1971 typval_T *tv_dest = STACK_TV_BOT(-1);
1972 int status = OK;
1973
1974 // Stack contains:
1975 // -2 index
1976 // -1 dict or list
1977 if (tv_dest->v_type == VAR_DICT)
1978 {
1979 // unlet a dict item, index must be a string
1980 if (tv_idx->v_type != VAR_STRING)
1981 {
1982 SOURCING_LNUM = iptr->isn_lnum;
1983 semsg(_(e_expected_str_but_got_str),
1984 vartype_name(VAR_STRING),
1985 vartype_name(tv_idx->v_type));
1986 status = FAIL;
1987 }
1988 else
1989 {
1990 dict_T *d = tv_dest->vval.v_dict;
1991 char_u *key = tv_idx->vval.v_string;
1992 dictitem_T *di = NULL;
1993
1994 if (d != NULL && value_check_lock(
1995 d->dv_lock, NULL, FALSE))
1996 status = FAIL;
1997 else
1998 {
1999 SOURCING_LNUM = iptr->isn_lnum;
2000 if (key == NULL)
2001 key = (char_u *)"";
2002 if (d != NULL)
2003 di = dict_find(d, key, (int)STRLEN(key));
2004 if (di == NULL)
2005 {
2006 // NULL dict is equivalent to empty dict
2007 semsg(_(e_key_not_present_in_dictionary),
2008 key);
2009 status = FAIL;
2010 }
2011 else if (var_check_fixed(di->di_flags,
2012 NULL, FALSE)
2013 || var_check_ro(di->di_flags,
2014 NULL, FALSE))
2015 status = FAIL;
2016 else
2017 dictitem_remove(d, di);
2018 }
2019 }
2020 }
2021 else if (tv_dest->v_type == VAR_LIST)
2022 {
2023 // unlet a List item, index must be a number
2024 SOURCING_LNUM = iptr->isn_lnum;
2025 if (check_for_number(tv_idx) == FAIL)
2026 {
2027 status = FAIL;
2028 }
2029 else
2030 {
2031 list_T *l = tv_dest->vval.v_list;
2032 long n = (long)tv_idx->vval.v_number;
2033
2034 if (l != NULL && value_check_lock(
2035 l->lv_lock, NULL, FALSE))
2036 status = FAIL;
2037 else
2038 {
2039 listitem_T *li = list_find(l, n);
2040
2041 if (li == NULL)
2042 {
2043 SOURCING_LNUM = iptr->isn_lnum;
2044 semsg(_(e_list_index_out_of_range_nr), n);
2045 status = FAIL;
2046 }
2047 else if (value_check_lock(li->li_tv.v_lock,
2048 NULL, FALSE))
2049 status = FAIL;
2050 else
2051 listitem_remove(l, li);
2052 }
2053 }
2054 }
2055 else
2056 {
2057 status = FAIL;
2058 semsg(_(e_cannot_index_str),
2059 vartype_name(tv_dest->v_type));
2060 }
2061
2062 clear_tv(tv_idx);
2063 clear_tv(tv_dest);
2064 ectx->ec_stack.ga_len -= 2;
2065
2066 return status;
2067}
2068
2069/*
2070 * Unlet a range of items in a list variable.
2071 */
2072 static int
2073execute_unletrange(isn_T *iptr, ectx_T *ectx)
2074{
2075 // Stack contains:
2076 // -3 index1
2077 // -2 index2
2078 // -1 dict or list
2079 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2080 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2081 typval_T *tv_dest = STACK_TV_BOT(-1);
2082 int status = OK;
2083
2084 if (tv_dest->v_type == VAR_LIST)
2085 {
2086 // indexes must be a number
2087 SOURCING_LNUM = iptr->isn_lnum;
2088 if (check_for_number(tv_idx1) == FAIL
2089 || (tv_idx2->v_type != VAR_SPECIAL
2090 && check_for_number(tv_idx2) == FAIL))
2091 {
2092 status = FAIL;
2093 }
2094 else
2095 {
2096 list_T *l = tv_dest->vval.v_list;
2097 long n1 = (long)tv_idx1->vval.v_number;
2098 long n2 = tv_idx2->v_type == VAR_SPECIAL
2099 ? 0 : (long)tv_idx2->vval.v_number;
2100 listitem_T *li;
2101
2102 li = list_find_index(l, &n1);
2103 if (li == NULL)
2104 status = FAIL;
2105 else
2106 {
2107 if (n1 < 0)
2108 n1 = list_idx_of_item(l, li);
2109 if (n2 < 0)
2110 {
2111 listitem_T *li2 = list_find(l, n2);
2112
2113 if (li2 == NULL)
2114 status = FAIL;
2115 else
2116 n2 = list_idx_of_item(l, li2);
2117 }
2118 if (status != FAIL
2119 && tv_idx2->v_type != VAR_SPECIAL
2120 && n2 < n1)
2121 {
2122 semsg(_(e_list_index_out_of_range_nr), n2);
2123 status = FAIL;
2124 }
2125 if (status != FAIL
2126 && list_unlet_range(l, li, NULL, n1,
2127 tv_idx2->v_type != VAR_SPECIAL, n2)
2128 == FAIL)
2129 status = FAIL;
2130 }
2131 }
2132 }
2133 else
2134 {
2135 status = FAIL;
2136 SOURCING_LNUM = iptr->isn_lnum;
2137 semsg(_(e_cannot_index_str),
2138 vartype_name(tv_dest->v_type));
2139 }
2140
2141 clear_tv(tv_idx1);
2142 clear_tv(tv_idx2);
2143 clear_tv(tv_dest);
2144 ectx->ec_stack.ga_len -= 3;
2145
2146 return status;
2147}
2148
2149/*
2150 * Top of a for loop.
2151 */
2152 static int
2153execute_for(isn_T *iptr, ectx_T *ectx)
2154{
2155 typval_T *tv;
2156 typval_T *ltv = STACK_TV_BOT(-1);
2157 typval_T *idxtv =
2158 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2159
2160 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2161 return FAIL;
2162 if (ltv->v_type == VAR_LIST)
2163 {
2164 list_T *list = ltv->vval.v_list;
2165
2166 // push the next item from the list
2167 ++idxtv->vval.v_number;
2168 if (list == NULL
2169 || idxtv->vval.v_number >= list->lv_len)
2170 {
2171 // past the end of the list, jump to "endfor"
2172 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2173 may_restore_cmdmod(&ectx->ec_funclocal);
2174 }
2175 else if (list->lv_first == &range_list_item)
2176 {
2177 // non-materialized range() list
2178 tv = STACK_TV_BOT(0);
2179 tv->v_type = VAR_NUMBER;
2180 tv->v_lock = 0;
2181 tv->vval.v_number = list_find_nr(
2182 list, idxtv->vval.v_number, NULL);
2183 ++ectx->ec_stack.ga_len;
2184 }
2185 else
2186 {
2187 listitem_T *li = list_find(list,
2188 idxtv->vval.v_number);
2189
2190 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2191 ++ectx->ec_stack.ga_len;
2192 }
2193 }
2194 else if (ltv->v_type == VAR_STRING)
2195 {
2196 char_u *str = ltv->vval.v_string;
2197
2198 // The index is for the last byte of the previous
2199 // character.
2200 ++idxtv->vval.v_number;
2201 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2202 {
2203 // past the end of the string, jump to "endfor"
2204 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2205 may_restore_cmdmod(&ectx->ec_funclocal);
2206 }
2207 else
2208 {
2209 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2210
2211 // Push the next character from the string.
2212 tv = STACK_TV_BOT(0);
2213 tv->v_type = VAR_STRING;
2214 tv->vval.v_string = vim_strnsave(
2215 str + idxtv->vval.v_number, clen);
2216 ++ectx->ec_stack.ga_len;
2217 idxtv->vval.v_number += clen - 1;
2218 }
2219 }
2220 else if (ltv->v_type == VAR_BLOB)
2221 {
2222 blob_T *blob = ltv->vval.v_blob;
2223
2224 // When we get here the first time make a copy of the
2225 // blob, so that the iteration still works when it is
2226 // changed.
2227 if (idxtv->vval.v_number == -1 && blob != NULL)
2228 {
2229 blob_copy(blob, ltv);
2230 blob_unref(blob);
2231 blob = ltv->vval.v_blob;
2232 }
2233
2234 // The index is for the previous byte.
2235 ++idxtv->vval.v_number;
2236 if (blob == NULL
2237 || idxtv->vval.v_number >= blob_len(blob))
2238 {
2239 // past the end of the blob, 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 // Push the next byte from the blob.
2246 tv = STACK_TV_BOT(0);
2247 tv->v_type = VAR_NUMBER;
2248 tv->vval.v_number = blob_get(blob,
2249 idxtv->vval.v_number);
2250 ++ectx->ec_stack.ga_len;
2251 }
2252 }
2253 else
2254 {
2255 semsg(_(e_for_loop_on_str_not_supported),
2256 vartype_name(ltv->v_type));
2257 return FAIL;
2258 }
2259 return OK;
2260}
2261
2262/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002263 * Load instruction for w:/b:/g:/t: variable.
2264 * "isn_type" is used instead of "iptr->isn_type".
2265 */
2266 static int
2267load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2268{
2269 dictitem_T *di = NULL;
2270 hashtab_T *ht = NULL;
2271 char namespace;
2272
2273 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2274 return NOTDONE;
2275 switch (isn_type)
2276 {
2277 case ISN_LOADG:
2278 ht = get_globvar_ht();
2279 namespace = 'g';
2280 break;
2281 case ISN_LOADB:
2282 ht = &curbuf->b_vars->dv_hashtab;
2283 namespace = 'b';
2284 break;
2285 case ISN_LOADW:
2286 ht = &curwin->w_vars->dv_hashtab;
2287 namespace = 'w';
2288 break;
2289 case ISN_LOADT:
2290 ht = &curtab->tp_vars->dv_hashtab;
2291 namespace = 't';
2292 break;
2293 default: // Cannot reach here
2294 return NOTDONE;
2295 }
2296 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2297
2298 if (di == NULL && ht == get_globvar_ht()
2299 && vim_strchr(iptr->isn_arg.string,
2300 AUTOLOAD_CHAR) != NULL)
2301 {
2302 // Global variable has an autoload name, may still need
2303 // to load the script.
2304 if (script_autoload(iptr->isn_arg.string, FALSE))
2305 di = find_var_in_ht(ht, 0,
2306 iptr->isn_arg.string, TRUE);
2307 if (did_emsg)
2308 return FAIL;
2309 }
2310
2311 if (di == NULL)
2312 {
2313 SOURCING_LNUM = iptr->isn_lnum;
2314 if (vim_strchr(iptr->isn_arg.string,
2315 AUTOLOAD_CHAR) != NULL)
2316 // no check if the item exists in the script but
2317 // isn't exported, it is too complicated
2318 semsg(_(e_item_not_found_in_script_str),
2319 iptr->isn_arg.string);
2320 else
2321 semsg(_(e_undefined_variable_char_str),
2322 namespace, iptr->isn_arg.string);
2323 return FAIL;
2324 }
2325 else
2326 {
2327 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2328 ++ectx->ec_stack.ga_len;
2329 }
2330 return OK;
2331}
2332
2333/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002334 * Execute instructions in execution context "ectx".
2335 * Return OK or FAIL;
2336 */
2337 static int
2338exec_instructions(ectx_T *ectx)
2339{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002340 int ret = FAIL;
2341 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002342 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002343
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002344 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002345 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002346
Bram Moolenaarff652882021-05-16 15:24:49 +02002347 // Only catch exceptions in this instruction list.
2348 ectx->ec_trylevel_at_start = trylevel;
2349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 for (;;)
2351 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002352 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002354 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002355
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002356 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002357 {
2358 line_breakcheck();
2359 breakcheck_count = 0;
2360 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002361 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002362 {
2363 // Turn CTRL-C into an exception.
2364 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002365 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002366 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002367 did_throw = TRUE;
2368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002370 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002371 {
2372 // Turn an error message into an exception.
2373 did_emsg = FALSE;
2374 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002375 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002376 did_throw = TRUE;
2377 *msg_list = NULL;
2378 }
2379
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002380 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002382 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002383 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002384 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385
2386 // An exception jumps to the first catch, finally, or returns from
2387 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002388 while (index > 0)
2389 {
2390 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002391 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002392 break;
2393 // In the catch and finally block of this try we have to go up
2394 // one level.
2395 --index;
2396 trycmd = NULL;
2397 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002398 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002400 if (trycmd->tcd_in_catch)
2401 {
2402 // exception inside ":catch", jump to ":finally" once
2403 ectx->ec_iidx = trycmd->tcd_finally_idx;
2404 trycmd->tcd_finally_idx = 0;
2405 }
2406 else
2407 // jump to first ":catch"
2408 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002409 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002410 did_throw = FALSE; // don't come back here until :endtry
2411 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 }
2413 else
2414 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002415 // Not inside try or need to return from current functions.
2416 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002417 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002418 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002419 tv = STACK_TV_BOT(0);
2420 tv->v_type = VAR_NUMBER;
2421 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002422 ++ectx->ec_stack.ga_len;
2423 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002425 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002426 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002427 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002428 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 goto done;
2430 }
2431
Bram Moolenaar4c137212021-04-19 16:48:48 +02002432 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002433 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 }
2435 continue;
2436 }
2437
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002438 /*
2439 * Big switch on the instruction. Most compilers will be turning this
2440 * into an efficient lookup table, since the "case" values are an enum
2441 * with sequential numbers. It may look ugly, but it should be the
2442 * most efficient way.
2443 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002444 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 switch (iptr->isn_type)
2446 {
2447 // execute Ex command line
2448 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002449 if (exec_command(iptr) == FAIL)
2450 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 break;
2452
Bram Moolenaar20677332021-06-06 17:02:53 +02002453 // execute Ex command line split at NL characters.
2454 case ISN_EXEC_SPLIT:
2455 {
2456 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002457 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002458
2459 SOURCING_LNUM = iptr->isn_lnum;
2460 CLEAR_FIELD(cookie);
2461 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2462 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002463 line = get_split_sourceline(0, &cookie, 0, 0);
2464 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002465 get_split_sourceline, &cookie,
2466 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2467 == FAIL
2468 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002469 {
2470 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002471 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002472 }
2473 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002474 }
2475 break;
2476
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002477 // execute Ex command line that is only a range
2478 case ISN_EXECRANGE:
2479 {
2480 exarg_T ea;
2481 char *error = NULL;
2482
2483 CLEAR_FIELD(ea);
2484 ea.cmdidx = CMD_SIZE;
2485 ea.addr_type = ADDR_LINES;
2486 ea.cmd = iptr->isn_arg.string;
2487 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002488 if (ea.cmd == NULL)
2489 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002490 if (error == NULL)
2491 error = ex_range_without_command(&ea);
2492 if (error != NULL)
2493 {
2494 SOURCING_LNUM = iptr->isn_lnum;
2495 emsg(error);
2496 goto on_error;
2497 }
2498 }
2499 break;
2500
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002501 // Evaluate an expression with legacy syntax, push it onto the
2502 // stack.
2503 case ISN_LEGACY_EVAL:
2504 {
2505 char_u *arg = iptr->isn_arg.string;
2506 int res;
2507 int save_flags = cmdmod.cmod_flags;
2508
Bram Moolenaar35578162021-08-02 19:10:38 +02002509 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002510 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002511 tv = STACK_TV_BOT(0);
2512 init_tv(tv);
2513 cmdmod.cmod_flags |= CMOD_LEGACY;
2514 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2515 cmdmod.cmod_flags = save_flags;
2516 if (res == FAIL)
2517 goto on_error;
2518 ++ectx->ec_stack.ga_len;
2519 }
2520 break;
2521
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002522 // push typeval VAR_INSTR with instructions to be executed
2523 case ISN_INSTR:
2524 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002525 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002526 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002527 tv = STACK_TV_BOT(0);
2528 tv->vval.v_instr = ALLOC_ONE(instr_T);
2529 if (tv->vval.v_instr == NULL)
2530 goto on_error;
2531 ++ectx->ec_stack.ga_len;
2532
2533 tv->v_type = VAR_INSTR;
2534 tv->vval.v_instr->instr_ectx = ectx;
2535 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2536 }
2537 break;
2538
Bram Moolenaar4c137212021-04-19 16:48:48 +02002539 // execute :substitute with an expression
2540 case ISN_SUBSTITUTE:
2541 {
2542 subs_T *subs = &iptr->isn_arg.subs;
2543 source_cookie_T cookie;
2544 struct subs_expr_S *save_instr = substitute_instr;
2545 struct subs_expr_S subs_instr;
2546 int res;
2547
2548 subs_instr.subs_ectx = ectx;
2549 subs_instr.subs_instr = subs->subs_instr;
2550 subs_instr.subs_status = OK;
2551 substitute_instr = &subs_instr;
2552
2553 SOURCING_LNUM = iptr->isn_lnum;
2554 // This is very much like ISN_EXEC
2555 CLEAR_FIELD(cookie);
2556 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2557 res = do_cmdline(subs->subs_cmd,
2558 getsourceline, &cookie,
2559 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2560 substitute_instr = save_instr;
2561
2562 if (res == FAIL || did_emsg
2563 || subs_instr.subs_status == FAIL)
2564 goto on_error;
2565 }
2566 break;
2567
2568 case ISN_FINISH:
2569 goto done;
2570
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002571 case ISN_REDIRSTART:
2572 // create a dummy entry for var_redir_str()
2573 if (alloc_redir_lval() == FAIL)
2574 goto on_error;
2575
2576 // The output is stored in growarray "redir_ga" until
2577 // redirection ends.
2578 init_redir_ga();
2579 redir_vname = 1;
2580 break;
2581
2582 case ISN_REDIREND:
2583 {
2584 char_u *res = get_clear_redir_ga();
2585
2586 // End redirection, put redirected text on the stack.
2587 clear_redir_lval();
2588 redir_vname = 0;
2589
Bram Moolenaar35578162021-08-02 19:10:38 +02002590 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002591 {
2592 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002593 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002594 }
2595 tv = STACK_TV_BOT(0);
2596 tv->v_type = VAR_STRING;
2597 tv->vval.v_string = res;
2598 ++ectx->ec_stack.ga_len;
2599 }
2600 break;
2601
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002602 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002603#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002604 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2605 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002606#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002607 break;
2608
2609 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002610#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002611 {
2612 exarg_T ea;
2613 int res;
2614
2615 CLEAR_FIELD(ea);
2616 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2617 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2618 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2619 --ectx->ec_stack.ga_len;
2620 tv = STACK_TV_BOT(0);
2621 res = cexpr_core(&ea, tv);
2622 clear_tv(tv);
2623 if (res == FAIL)
2624 goto on_error;
2625 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002626#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002627 break;
2628
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002629 // execute Ex command from pieces on the stack
2630 case ISN_EXECCONCAT:
2631 {
2632 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002633 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002634 int pass;
2635 int i;
2636 char_u *cmd = NULL;
2637 char_u *str;
2638
2639 for (pass = 1; pass <= 2; ++pass)
2640 {
2641 for (i = 0; i < count; ++i)
2642 {
2643 tv = STACK_TV_BOT(i - count);
2644 str = tv->vval.v_string;
2645 if (str != NULL && *str != NUL)
2646 {
2647 if (pass == 2)
2648 STRCPY(cmd + len, str);
2649 len += STRLEN(str);
2650 }
2651 if (pass == 2)
2652 clear_tv(tv);
2653 }
2654 if (pass == 1)
2655 {
2656 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002657 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002658 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002659 len = 0;
2660 }
2661 }
2662
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002663 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002664 do_cmdline_cmd(cmd);
2665 vim_free(cmd);
2666 }
2667 break;
2668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 // execute :echo {string} ...
2670 case ISN_ECHO:
2671 {
2672 int count = iptr->isn_arg.echo.echo_count;
2673 int atstart = TRUE;
2674 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002675 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676
2677 for (idx = 0; idx < count; ++idx)
2678 {
2679 tv = STACK_TV_BOT(idx - count);
2680 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2681 &atstart, &needclr);
2682 clear_tv(tv);
2683 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002684 if (needclr)
2685 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002686 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 }
2688 break;
2689
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002690 // :execute {string} ...
2691 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002692 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002693 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002694 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002695 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002696 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002697 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002698 {
2699 int count = iptr->isn_arg.number;
2700 garray_T ga;
2701 char_u buf[NUMBUFLEN];
2702 char_u *p;
2703 int len;
2704 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002705 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002706
2707 ga_init2(&ga, 1, 80);
2708 for (idx = 0; idx < count; ++idx)
2709 {
2710 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002711 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002712 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002713 if (tv->v_type == VAR_CHANNEL
2714 || tv->v_type == VAR_JOB)
2715 {
2716 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002717 semsg(_(e_using_invalid_value_as_string_str),
2718 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002719 break;
2720 }
2721 else
2722 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002723 }
2724 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002725 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002726
2727 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002728 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002729 failed = TRUE;
2730 else
2731 {
2732 if (ga.ga_len > 0)
2733 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2734 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2735 ga.ga_len += len;
2736 }
2737 clear_tv(tv);
2738 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002739 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002740 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002741 {
2742 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002743 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002744 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002745
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002746 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002747 {
2748 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002749 {
2750 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002751 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002752 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002753 {
2754 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002755 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002756 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002757 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002758 else
2759 {
2760 msg_sb_eol();
2761 if (iptr->isn_type == ISN_ECHOMSG)
2762 {
2763 msg_attr(ga.ga_data, echo_attr);
2764 out_flush();
2765 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002766 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2767 {
2768 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2769 TRUE);
2770 ui_write((char_u *)"\r\n", 2, TRUE);
2771 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002772 else
2773 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002774 SOURCING_LNUM = iptr->isn_lnum;
2775 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002776 }
2777 }
2778 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002779 ga_clear(&ga);
2780 }
2781 break;
2782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 // load local variable or argument
2784 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002785 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002786 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002788 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 break;
2790
2791 // load v: variable
2792 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002793 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002794 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002796 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 break;
2798
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002799 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 case ISN_LOADSCRIPT:
2801 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002802 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 svar_T *sv;
2804
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002805 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002806 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002807 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002808 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002809 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002810 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002812 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 }
2814 break;
2815
2816 // load s: variable in old script
2817 case ISN_LOADS:
2818 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002819 hashtab_T *ht = &SCRIPT_VARS(
2820 iptr->isn_arg.loadstore.ls_sid);
2821 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 if (di == NULL)
2825 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002826 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002827 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002828 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 }
2830 else
2831 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002832 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002833 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002835 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 }
2837 }
2838 break;
2839
Bram Moolenaard3aac292020-04-19 14:32:17 +02002840 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002842 case ISN_LOADB:
2843 case ISN_LOADW:
2844 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002846 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002847
Bram Moolenaar06b77222022-01-25 15:51:56 +00002848 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002849 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002850 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002851 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 break;
2855
Bram Moolenaar03290b82020-12-19 16:30:44 +01002856 // load autoload variable
2857 case ISN_LOADAUTO:
2858 {
2859 char_u *name = iptr->isn_arg.string;
2860
Bram Moolenaar35578162021-08-02 19:10:38 +02002861 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002862 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002863 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002864 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002865 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002866 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002867 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002868 }
2869 break;
2870
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002871 // load g:/b:/w:/t: namespace
2872 case ISN_LOADGDICT:
2873 case ISN_LOADBDICT:
2874 case ISN_LOADWDICT:
2875 case ISN_LOADTDICT:
2876 {
2877 dict_T *d = NULL;
2878
2879 switch (iptr->isn_type)
2880 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002881 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2882 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2883 case ISN_LOADWDICT: d = curwin->w_vars; break;
2884 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002885 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002886 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002887 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002888 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002889 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002890 tv = STACK_TV_BOT(0);
2891 tv->v_type = VAR_DICT;
2892 tv->v_lock = 0;
2893 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002894 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002895 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002896 }
2897 break;
2898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 // load &option
2900 case ISN_LOADOPT:
2901 {
2902 typval_T optval;
2903 char_u *name = iptr->isn_arg.string;
2904
Bram Moolenaara8c17702020-04-01 21:17:24 +02002905 // This is not expected to fail, name is checked during
2906 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002907 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002908 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002909 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002910 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002912 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 }
2914 break;
2915
2916 // load $ENV
2917 case ISN_LOADENV:
2918 {
2919 typval_T optval;
2920 char_u *name = iptr->isn_arg.string;
2921
Bram Moolenaar35578162021-08-02 19:10:38 +02002922 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002923 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002924 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002925 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002927 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 }
2929 break;
2930
2931 // load @register
2932 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002933 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002934 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 tv = STACK_TV_BOT(0);
2936 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002937 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002938 // This may result in NULL, which should be equivalent to an
2939 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 tv->vval.v_string = get_reg_contents(
2941 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002942 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 break;
2944
2945 // store local variable
2946 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002947 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 tv = STACK_TV_VAR(iptr->isn_arg.number);
2949 clear_tv(tv);
2950 *tv = *STACK_TV_BOT(0);
2951 break;
2952
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002953 // store s: variable in old script
2954 case ISN_STORES:
2955 {
2956 hashtab_T *ht = &SCRIPT_VARS(
2957 iptr->isn_arg.loadstore.ls_sid);
2958 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002959 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002960
Bram Moolenaar4c137212021-04-19 16:48:48 +02002961 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002962 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002963 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002964 else
2965 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002966 SOURCING_LNUM = iptr->isn_lnum;
2967 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002968 {
2969 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002970 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002971 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002972 clear_tv(&di->di_tv);
2973 di->di_tv = *STACK_TV_BOT(0);
2974 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002975 }
2976 break;
2977
2978 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 case ISN_STORESCRIPT:
2980 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002981 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2982 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002984 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002985 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002986 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002987 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002988
2989 // "const" and "final" are checked at compile time, locking
2990 // the value needs to be checked here.
2991 SOURCING_LNUM = iptr->isn_lnum;
2992 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002993 {
2994 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002995 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002996 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 clear_tv(sv->sv_tv);
2999 *sv->sv_tv = *STACK_TV_BOT(0);
3000 }
3001 break;
3002
3003 // store option
3004 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003005 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003007 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3008 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 long n = 0;
3010 char_u *s = NULL;
3011 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003012 char_u numbuf[NUMBUFLEN];
3013 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014
Bram Moolenaar4c137212021-04-19 16:48:48 +02003015 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 tv = STACK_TV_BOT(0);
3017 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003018 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003020 if (s == NULL)
3021 s = (char_u *)"";
3022 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003023 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3024 {
3025 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003026 // If the option can be set to a function reference or
3027 // a lambda and the passed value is a function
3028 // reference, then convert it to the name (string) of
3029 // the function reference.
3030 s = tv2string(tv, &tofree, numbuf, 0);
3031 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003032 {
3033 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003034 goto on_error;
3035 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003038 // must be VAR_NUMBER, CHECKTYPE makes sure
3039 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003040 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003041 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003042 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 if (msg != NULL)
3044 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003045 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003047 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 }
3050 break;
3051
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003052 // store $ENV
3053 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003054 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003055 tv = STACK_TV_BOT(0);
3056 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3057 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003058 break;
3059
3060 // store @r
3061 case ISN_STOREREG:
3062 {
3063 int reg = iptr->isn_arg.number;
3064
Bram Moolenaar4c137212021-04-19 16:48:48 +02003065 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003066 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003067 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003068 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003069 }
3070 break;
3071
3072 // store v: variable
3073 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003074 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003075 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3076 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003077 // should not happen, type is checked when compiling
3078 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003079 break;
3080
Bram Moolenaard3aac292020-04-19 14:32:17 +02003081 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003083 case ISN_STOREB:
3084 case ISN_STOREW:
3085 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003087 dictitem_T *di;
3088 hashtab_T *ht;
3089 char_u *name = iptr->isn_arg.string + 2;
3090
Bram Moolenaard3aac292020-04-19 14:32:17 +02003091 switch (iptr->isn_type)
3092 {
3093 case ISN_STOREG:
3094 ht = get_globvar_ht();
3095 break;
3096 case ISN_STOREB:
3097 ht = &curbuf->b_vars->dv_hashtab;
3098 break;
3099 case ISN_STOREW:
3100 ht = &curwin->w_vars->dv_hashtab;
3101 break;
3102 case ISN_STORET:
3103 ht = &curtab->tp_vars->dv_hashtab;
3104 break;
3105 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003106 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108
Bram Moolenaar4c137212021-04-19 16:48:48 +02003109 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003110 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003112 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 else
3114 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003115 SOURCING_LNUM = iptr->isn_lnum;
3116 if (var_check_permission(di, name) == FAIL)
3117 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 clear_tv(&di->di_tv);
3119 di->di_tv = *STACK_TV_BOT(0);
3120 }
3121 }
3122 break;
3123
Bram Moolenaar03290b82020-12-19 16:30:44 +01003124 // store an autoload variable
3125 case ISN_STOREAUTO:
3126 SOURCING_LNUM = iptr->isn_lnum;
3127 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3128 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003129 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003130 break;
3131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 // store number in local variable
3133 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003134 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 clear_tv(tv);
3136 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003137 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 break;
3139
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003140 // store value in list or dict variable
3141 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003142 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003143 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003144
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003145 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003146 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003147 if (res == NOTDONE)
3148 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003149 }
3150 break;
3151
Bram Moolenaar68452172021-04-12 21:21:02 +02003152 // store value in blob range
3153 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003154 if (execute_storerange(iptr, ectx) == FAIL)
3155 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003156 break;
3157
Bram Moolenaar0186e582021-01-10 18:33:11 +01003158 // load or store variable or argument from outer scope
3159 case ISN_LOADOUTER:
3160 case ISN_STOREOUTER:
3161 {
3162 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003163 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3164 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003165
3166 while (depth > 1 && outer != NULL)
3167 {
3168 outer = outer->out_up;
3169 --depth;
3170 }
3171 if (outer == NULL)
3172 {
3173 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003174 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3175 || ectx->ec_outer_ref == NULL)
3176 // Possibly :def function called from legacy
3177 // context.
3178 emsg(_(e_closure_called_from_invalid_context));
3179 else
3180 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003181 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003182 }
3183 tv = ((typval_T *)outer->out_stack->ga_data)
3184 + outer->out_frame_idx + STACK_FRAME_SIZE
3185 + iptr->isn_arg.outer.outer_idx;
3186 if (iptr->isn_type == ISN_LOADOUTER)
3187 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003188 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003189 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003190 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003191 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003192 }
3193 else
3194 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003196 clear_tv(tv);
3197 *tv = *STACK_TV_BOT(0);
3198 }
3199 }
3200 break;
3201
Bram Moolenaar752fc692021-01-04 21:57:11 +01003202 // unlet item in list or dict variable
3203 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003204 if (execute_unletindex(iptr, ectx) == FAIL)
3205 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003206 break;
3207
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003208 // unlet range of items in list variable
3209 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003210 if (execute_unletrange(iptr, ectx) == FAIL)
3211 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003212 break;
3213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 // push constant
3215 case ISN_PUSHNR:
3216 case ISN_PUSHBOOL:
3217 case ISN_PUSHSPEC:
3218 case ISN_PUSHF:
3219 case ISN_PUSHS:
3220 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003221 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003222 case ISN_PUSHCHANNEL:
3223 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003224 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003225 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003227 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003228 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 switch (iptr->isn_type)
3230 {
3231 case ISN_PUSHNR:
3232 tv->v_type = VAR_NUMBER;
3233 tv->vval.v_number = iptr->isn_arg.number;
3234 break;
3235 case ISN_PUSHBOOL:
3236 tv->v_type = VAR_BOOL;
3237 tv->vval.v_number = iptr->isn_arg.number;
3238 break;
3239 case ISN_PUSHSPEC:
3240 tv->v_type = VAR_SPECIAL;
3241 tv->vval.v_number = iptr->isn_arg.number;
3242 break;
3243#ifdef FEAT_FLOAT
3244 case ISN_PUSHF:
3245 tv->v_type = VAR_FLOAT;
3246 tv->vval.v_float = iptr->isn_arg.fnumber;
3247 break;
3248#endif
3249 case ISN_PUSHBLOB:
3250 blob_copy(iptr->isn_arg.blob, tv);
3251 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003252 case ISN_PUSHFUNC:
3253 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003254 if (iptr->isn_arg.string == NULL)
3255 tv->vval.v_string = NULL;
3256 else
3257 tv->vval.v_string =
3258 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003259 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003260 case ISN_PUSHCHANNEL:
3261#ifdef FEAT_JOB_CHANNEL
3262 tv->v_type = VAR_CHANNEL;
3263 tv->vval.v_channel = iptr->isn_arg.channel;
3264 if (tv->vval.v_channel != NULL)
3265 ++tv->vval.v_channel->ch_refcount;
3266#endif
3267 break;
3268 case ISN_PUSHJOB:
3269#ifdef FEAT_JOB_CHANNEL
3270 tv->v_type = VAR_JOB;
3271 tv->vval.v_job = iptr->isn_arg.job;
3272 if (tv->vval.v_job != NULL)
3273 ++tv->vval.v_job->jv_refcount;
3274#endif
3275 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 default:
3277 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003278 tv->vval.v_string = vim_strsave(
3279 iptr->isn_arg.string == NULL
3280 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 }
3282 break;
3283
Bram Moolenaar06b77222022-01-25 15:51:56 +00003284 case ISN_AUTOLOAD:
3285 {
3286 char_u *name = iptr->isn_arg.string;
3287
3288 (void)script_autoload(name, FALSE);
3289 if (find_func(name, TRUE))
3290 {
3291 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3292 goto theend;
3293 tv = STACK_TV_BOT(0);
3294 tv->v_lock = 0;
3295 ++ectx->ec_stack.ga_len;
3296 tv->v_type = VAR_FUNC;
3297 tv->vval.v_string = vim_strsave(name);
3298 }
3299 else
3300 {
3301 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3302
3303 if (res == NOTDONE)
3304 goto theend;
3305 if (res == FAIL)
3306 goto on_error;
3307 }
3308 }
3309 break;
3310
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003311 case ISN_UNLET:
3312 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3313 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003314 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003315 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003316 case ISN_UNLETENV:
3317 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3318 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003319
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003320 case ISN_LOCKUNLOCK:
3321 {
3322 typval_T *lval_root_save = lval_root;
3323 int res;
3324
3325 // Stack has the local variable, argument the whole :lock
3326 // or :unlock command, like ISN_EXEC.
3327 --ectx->ec_stack.ga_len;
3328 lval_root = STACK_TV_BOT(0);
3329 res = exec_command(iptr);
3330 clear_tv(lval_root);
3331 lval_root = lval_root_save;
3332 if (res == FAIL)
3333 goto on_error;
3334 }
3335 break;
3336
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003337 case ISN_LOCKCONST:
3338 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3339 break;
3340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 // create a list from items on the stack; uses a single allocation
3342 // for the list header and the items
3343 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003344 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003345 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 break;
3347
3348 // create a dict from items on the stack
3349 case ISN_NEWDICT:
3350 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003351 int count = iptr->isn_arg.number;
3352 dict_T *dict = dict_alloc();
3353 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003354 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003355 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003357 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003358 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 for (idx = 0; idx < count; ++idx)
3360 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003361 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003363 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003364 key = tv->vval.v_string == NULL
3365 ? (char_u *)"" : tv->vval.v_string;
3366 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003367 if (item != NULL)
3368 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003369 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar74409f62022-01-01 15:58:22 +00003370 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003371 dict_unref(dict);
3372 goto on_error;
3373 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003374 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003376 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003377 {
3378 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003379 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3382 item->di_tv.v_lock = 0;
3383 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003384 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003385 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003386 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003387 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 }
3390
3391 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003392 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003393 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003394 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003396 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 tv = STACK_TV_BOT(-1);
3398 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003399 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 tv->vval.v_dict = dict;
3401 ++dict->dv_refcount;
3402 }
3403 break;
3404
3405 // call a :def function
3406 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003407 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003408 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3409 NULL,
3410 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003411 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003412 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 break;
3414
3415 // call a builtin function
3416 case ISN_BCALL:
3417 SOURCING_LNUM = iptr->isn_lnum;
3418 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3419 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003420 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003421 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 break;
3423
3424 // call a funcref or partial
3425 case ISN_PCALL:
3426 {
3427 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3428 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003429 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430
3431 SOURCING_LNUM = iptr->isn_lnum;
3432 if (pfunc->cpf_top)
3433 {
3434 // funcref is above the arguments
3435 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3436 }
3437 else
3438 {
3439 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003440 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003441 partial_tv = *STACK_TV_BOT(0);
3442 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003444 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003445 if (tv == &partial_tv)
3446 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003448 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 }
3450 break;
3451
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003452 case ISN_PCALL_END:
3453 // PCALL finished, arguments have been consumed and replaced by
3454 // the return value. Now clear the funcref from the stack,
3455 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003456 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003457 clear_tv(STACK_TV_BOT(-1));
3458 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3459 break;
3460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 // call a user defined function or funcref/partial
3462 case ISN_UCALL:
3463 {
3464 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3465
3466 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003467 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003468 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003469 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 }
3471 break;
3472
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003473 // return from a :def function call without a value
3474 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003475 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003476 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003477 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003478 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003479 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003480 tv->vval.v_number = 0;
3481 tv->v_lock = 0;
3482 // FALLTHROUGH
3483
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003484 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 case ISN_RETURN:
3486 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003487 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003488 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003489
3490 if (trystack->ga_len > 0)
3491 trycmd = ((trycmd_T *)trystack->ga_data)
3492 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003493 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003494 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003496 // jump to ":finally" or ":endtry"
3497 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003498 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003499 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003500 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 trycmd->tcd_return = TRUE;
3502 }
3503 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003504 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 }
3506 break;
3507
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003508 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 case ISN_FUNCREF:
3510 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003511 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003512 ufunc_T *ufunc;
3513 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003516 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003517 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003518 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003519 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003520 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003521 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003522 if (funcref->fr_func_name == NULL)
3523 {
3524 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3525 + funcref->fr_dfunc_idx;
3526
3527 ufunc = pt_dfunc->df_ufunc;
3528 }
3529 else
3530 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003531 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003532 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003533 if (ufunc == NULL)
3534 {
3535 SOURCING_LNUM = iptr->isn_lnum;
3536 emsg(_(e_function_reference_invalid));
3537 goto theend;
3538 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003539 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003540 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003542 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 tv->vval.v_partial = pt;
3544 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003545 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 }
3547 break;
3548
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003549 // Create a global function from a lambda.
3550 case ISN_NEWFUNC:
3551 {
3552 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3553
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003554 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003555 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003556 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003557 }
3558 break;
3559
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003560 // List functions
3561 case ISN_DEF:
3562 if (iptr->isn_arg.string == NULL)
3563 list_functions(NULL);
3564 else
3565 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003566 exarg_T ea;
3567 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003568
3569 CLEAR_FIELD(ea);
3570 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003571 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3572 define_function(&ea, NULL, &lines_to_free);
3573 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003574 }
3575 break;
3576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 // jump if a condition is met
3578 case ISN_JUMP:
3579 {
3580 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003581 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 int jump = TRUE;
3583
3584 if (when != JUMP_ALWAYS)
3585 {
3586 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003587 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003588 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003589 || when == JUMP_IF_COND_TRUE)
3590 {
3591 SOURCING_LNUM = iptr->isn_lnum;
3592 jump = tv_get_bool_chk(tv, &error);
3593 if (error)
3594 goto on_error;
3595 }
3596 else
3597 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003599 || when == JUMP_AND_KEEP_IF_FALSE
3600 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003602 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 {
3604 // drop the value from the stack
3605 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003606 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 }
3608 }
3609 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003610 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 }
3612 break;
3613
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003614 // Jump if an argument with a default value was already set and not
3615 // v:none.
3616 case ISN_JUMP_IF_ARG_SET:
3617 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3618 if (tv->v_type != VAR_UNKNOWN
3619 && !(tv->v_type == VAR_SPECIAL
3620 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003621 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003622 break;
3623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 // top of a for loop
3625 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003626 if (execute_for(iptr, ectx) == FAIL)
3627 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 break;
3629
3630 // start of ":try" block
3631 case ISN_TRY:
3632 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003633 trycmd_T *trycmd = NULL;
3634
Bram Moolenaar35578162021-08-02 19:10:38 +02003635 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003636 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003637 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3638 + ectx->ec_trystack.ga_len;
3639 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003641 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003642 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3643 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003644 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003645 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003646 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003647 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003648 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003649 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 }
3651 break;
3652
3653 case ISN_PUSHEXC:
3654 if (current_exception == NULL)
3655 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003656 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003658 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003660 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003661 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003663 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003665 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 tv->vval.v_string = vim_strsave(
3667 (char_u *)current_exception->value);
3668 break;
3669
3670 case ISN_CATCH:
3671 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003672 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673
Bram Moolenaar4c137212021-04-19 16:48:48 +02003674 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 if (trystack->ga_len > 0)
3676 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003677 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 + trystack->ga_len - 1;
3679 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003680 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 }
3682 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003683 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 catch_exception(current_exception);
3685 }
3686 break;
3687
Bram Moolenaarc150c092021-02-13 15:02:46 +01003688 case ISN_TRYCONT:
3689 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003691 trycont_T *trycont = &iptr->isn_arg.trycont;
3692 int i;
3693 trycmd_T *trycmd;
3694 int iidx = trycont->tct_where;
3695
3696 if (trystack->ga_len < trycont->tct_levels)
3697 {
3698 siemsg("TRYCONT: expected %d levels, found %d",
3699 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003700 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003701 }
3702 // Make :endtry jump to any outer try block and the last
3703 // :endtry inside the loop to the loop start.
3704 for (i = trycont->tct_levels; i > 0; --i)
3705 {
3706 trycmd = ((trycmd_T *)trystack->ga_data)
3707 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003708 // Add one to tcd_cont to be able to jump to
3709 // instruction with index zero.
3710 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003711 iidx = trycmd->tcd_finally_idx == 0
3712 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003713 }
3714 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003715 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003716 }
3717 break;
3718
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003719 case ISN_FINALLY:
3720 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003721 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003722 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3723 + trystack->ga_len - 1;
3724
3725 // Reset the index to avoid a return statement jumps here
3726 // again.
3727 trycmd->tcd_finally_idx = 0;
3728 break;
3729 }
3730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 // end of ":try" block
3732 case ISN_ENDTRY:
3733 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003734 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735
3736 if (trystack->ga_len > 0)
3737 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003738 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 --trystack->ga_len;
3741 --trylevel;
3742 trycmd = ((trycmd_T *)trystack->ga_data)
3743 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003744 if (trycmd->tcd_did_throw)
3745 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003746 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 {
3748 // discard the exception
3749 if (caught_stack == current_exception)
3750 caught_stack = caught_stack->caught;
3751 discard_current_exception();
3752 }
3753
3754 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003755 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003756
Bram Moolenaar4c137212021-04-19 16:48:48 +02003757 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003758 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003759 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003760 clear_tv(STACK_TV_BOT(0));
3761 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003762 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003763 // handling :continue: jump to outer try block or
3764 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003765 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 }
3767 }
3768 break;
3769
3770 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003771 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003772 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003773
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003774 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3775 {
3776 // throwing an exception while using "silent!" causes
3777 // the function to abort but not display an error.
3778 tv = STACK_TV_BOT(-1);
3779 clear_tv(tv);
3780 tv->v_type = VAR_NUMBER;
3781 tv->vval.v_number = 0;
3782 goto done;
3783 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003785 tv = STACK_TV_BOT(0);
3786 if (tv->vval.v_string == NULL
3787 || *skipwhite(tv->vval.v_string) == NUL)
3788 {
3789 vim_free(tv->vval.v_string);
3790 SOURCING_LNUM = iptr->isn_lnum;
3791 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003792 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003793 }
3794
3795 // Inside a "catch" we need to first discard the caught
3796 // exception.
3797 if (trystack->ga_len > 0)
3798 {
3799 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3800 + trystack->ga_len - 1;
3801 if (trycmd->tcd_caught && current_exception != NULL)
3802 {
3803 // discard the exception
3804 if (caught_stack == current_exception)
3805 caught_stack = caught_stack->caught;
3806 discard_current_exception();
3807 trycmd->tcd_caught = FALSE;
3808 }
3809 }
3810
3811 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3812 == FAIL)
3813 {
3814 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003815 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003816 }
3817 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 break;
3820
3821 // compare with special values
3822 case ISN_COMPAREBOOL:
3823 case ISN_COMPARESPECIAL:
3824 {
3825 typval_T *tv1 = STACK_TV_BOT(-2);
3826 typval_T *tv2 = STACK_TV_BOT(-1);
3827 varnumber_T arg1 = tv1->vval.v_number;
3828 varnumber_T arg2 = tv2->vval.v_number;
3829 int res;
3830
3831 switch (iptr->isn_arg.op.op_type)
3832 {
3833 case EXPR_EQUAL: res = arg1 == arg2; break;
3834 case EXPR_NEQUAL: res = arg1 != arg2; break;
3835 default: res = 0; break;
3836 }
3837
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 tv1->v_type = VAR_BOOL;
3840 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3841 }
3842 break;
3843
3844 // Operation with two number arguments
3845 case ISN_OPNR:
3846 case ISN_COMPARENR:
3847 {
3848 typval_T *tv1 = STACK_TV_BOT(-2);
3849 typval_T *tv2 = STACK_TV_BOT(-1);
3850 varnumber_T arg1 = tv1->vval.v_number;
3851 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003852 varnumber_T res = 0;
3853 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854
3855 switch (iptr->isn_arg.op.op_type)
3856 {
3857 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003858 case EXPR_DIV: if (arg2 == 0)
3859 div_zero = TRUE;
3860 else
3861 res = arg1 / arg2;
3862 break;
3863 case EXPR_REM: if (arg2 == 0)
3864 div_zero = TRUE;
3865 else
3866 res = arg1 % arg2;
3867 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 case EXPR_SUB: res = arg1 - arg2; break;
3869 case EXPR_ADD: res = arg1 + arg2; break;
3870
3871 case EXPR_EQUAL: res = arg1 == arg2; break;
3872 case EXPR_NEQUAL: res = arg1 != arg2; break;
3873 case EXPR_GREATER: res = arg1 > arg2; break;
3874 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3875 case EXPR_SMALLER: res = arg1 < arg2; break;
3876 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003877 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 }
3879
Bram Moolenaar4c137212021-04-19 16:48:48 +02003880 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 if (iptr->isn_type == ISN_COMPARENR)
3882 {
3883 tv1->v_type = VAR_BOOL;
3884 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3885 }
3886 else
3887 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003888 if (div_zero)
3889 {
3890 SOURCING_LNUM = iptr->isn_lnum;
3891 emsg(_(e_divide_by_zero));
3892 goto on_error;
3893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 }
3895 break;
3896
3897 // Computation with two float arguments
3898 case ISN_OPFLOAT:
3899 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003900#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 {
3902 typval_T *tv1 = STACK_TV_BOT(-2);
3903 typval_T *tv2 = STACK_TV_BOT(-1);
3904 float_T arg1 = tv1->vval.v_float;
3905 float_T arg2 = tv2->vval.v_float;
3906 float_T res = 0;
3907 int cmp = FALSE;
3908
3909 switch (iptr->isn_arg.op.op_type)
3910 {
3911 case EXPR_MULT: res = arg1 * arg2; break;
3912 case EXPR_DIV: res = arg1 / arg2; break;
3913 case EXPR_SUB: res = arg1 - arg2; break;
3914 case EXPR_ADD: res = arg1 + arg2; break;
3915
3916 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3917 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3918 case EXPR_GREATER: cmp = arg1 > arg2; break;
3919 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3920 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3921 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3922 default: cmp = 0; break;
3923 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003924 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 if (iptr->isn_type == ISN_COMPAREFLOAT)
3926 {
3927 tv1->v_type = VAR_BOOL;
3928 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3929 }
3930 else
3931 tv1->vval.v_float = res;
3932 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003933#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 break;
3935
3936 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00003937 case ISN_COMPAREDICT:
3938 case ISN_COMPAREFUNC:
3939 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 case ISN_COMPAREBLOB:
3941 {
3942 typval_T *tv1 = STACK_TV_BOT(-2);
3943 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00003944 exprtype_T exprtype = iptr->isn_arg.op.op_type;
3945 int ic = iptr->isn_arg.op.op_ic;
3946 int res = FALSE;
3947 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948
Bram Moolenaar265f8112021-12-19 12:33:05 +00003949 SOURCING_LNUM = iptr->isn_lnum;
3950 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00003952 status = typval_compare_list(tv1, tv2,
3953 exprtype, ic, &res);
3954 }
3955 else if (iptr->isn_type == ISN_COMPAREDICT)
3956 {
3957 status = typval_compare_dict(tv1, tv2,
3958 exprtype, ic, &res);
3959 }
3960 else if (iptr->isn_type == ISN_COMPAREFUNC)
3961 {
3962 status = typval_compare_func(tv1, tv2,
3963 exprtype, ic, &res);
3964 }
3965 else if (iptr->isn_type == ISN_COMPARESTRING)
3966 {
3967 status = typval_compare_string(tv1, tv2,
3968 exprtype, ic, &res);
3969 }
3970 else
3971 {
3972 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003974 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 clear_tv(tv1);
3976 clear_tv(tv2);
3977 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003978 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3979 if (status == FAIL)
3980 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 }
3982 break;
3983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 case ISN_COMPAREANY:
3985 {
3986 typval_T *tv1 = STACK_TV_BOT(-2);
3987 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003988 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003990 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991
Bram Moolenaareb26f432020-09-14 16:50:05 +02003992 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003993 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003995 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003996 if (status == FAIL)
3997 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 }
3999 break;
4000
4001 case ISN_ADDLIST:
4002 case ISN_ADDBLOB:
4003 {
4004 typval_T *tv1 = STACK_TV_BOT(-2);
4005 typval_T *tv2 = STACK_TV_BOT(-1);
4006
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004007 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004009 {
4010 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4011 && tv1->vval.v_list != NULL)
4012 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4013 NULL);
4014 else
4015 eval_addlist(tv1, tv2);
4016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 else
4018 eval_addblob(tv1, tv2);
4019 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004020 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 }
4022 break;
4023
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004024 case ISN_LISTAPPEND:
4025 {
4026 typval_T *tv1 = STACK_TV_BOT(-2);
4027 typval_T *tv2 = STACK_TV_BOT(-1);
4028 list_T *l = tv1->vval.v_list;
4029
4030 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004031 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004032 if (l == NULL)
4033 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004034 emsg(_(e_cannot_add_to_null_list));
4035 goto on_error;
4036 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004037 if (value_check_lock(l->lv_lock, NULL, FALSE))
4038 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004039 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004040 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004041 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004042 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004043 }
4044 break;
4045
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004046 case ISN_BLOBAPPEND:
4047 {
4048 typval_T *tv1 = STACK_TV_BOT(-2);
4049 typval_T *tv2 = STACK_TV_BOT(-1);
4050 blob_T *b = tv1->vval.v_blob;
4051 int error = FALSE;
4052 varnumber_T n;
4053
4054 // add a number to a blob
4055 if (b == NULL)
4056 {
4057 SOURCING_LNUM = iptr->isn_lnum;
4058 emsg(_(e_cannot_add_to_null_blob));
4059 goto on_error;
4060 }
4061 n = tv_get_number_chk(tv2, &error);
4062 if (error)
4063 goto on_error;
4064 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004065 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004066 }
4067 break;
4068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 // Computation with two arguments of unknown type
4070 case ISN_OPANY:
4071 {
4072 typval_T *tv1 = STACK_TV_BOT(-2);
4073 typval_T *tv2 = STACK_TV_BOT(-1);
4074 varnumber_T n1, n2;
4075#ifdef FEAT_FLOAT
4076 float_T f1 = 0, f2 = 0;
4077#endif
4078 int error = FALSE;
4079
4080 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4081 {
4082 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4083 {
4084 eval_addlist(tv1, tv2);
4085 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004086 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 break;
4088 }
4089 else if (tv1->v_type == VAR_BLOB
4090 && tv2->v_type == VAR_BLOB)
4091 {
4092 eval_addblob(tv1, tv2);
4093 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004094 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 break;
4096 }
4097 }
4098#ifdef FEAT_FLOAT
4099 if (tv1->v_type == VAR_FLOAT)
4100 {
4101 f1 = tv1->vval.v_float;
4102 n1 = 0;
4103 }
4104 else
4105#endif
4106 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004107 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 n1 = tv_get_number_chk(tv1, &error);
4109 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004110 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111#ifdef FEAT_FLOAT
4112 if (tv2->v_type == VAR_FLOAT)
4113 f1 = n1;
4114#endif
4115 }
4116#ifdef FEAT_FLOAT
4117 if (tv2->v_type == VAR_FLOAT)
4118 {
4119 f2 = tv2->vval.v_float;
4120 n2 = 0;
4121 }
4122 else
4123#endif
4124 {
4125 n2 = tv_get_number_chk(tv2, &error);
4126 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004127 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128#ifdef FEAT_FLOAT
4129 if (tv1->v_type == VAR_FLOAT)
4130 f2 = n2;
4131#endif
4132 }
4133#ifdef FEAT_FLOAT
4134 // if there is a float on either side the result is a float
4135 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4136 {
4137 switch (iptr->isn_arg.op.op_type)
4138 {
4139 case EXPR_MULT: f1 = f1 * f2; break;
4140 case EXPR_DIV: f1 = f1 / f2; break;
4141 case EXPR_SUB: f1 = f1 - f2; break;
4142 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004143 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004144 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004145 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 }
4147 clear_tv(tv1);
4148 clear_tv(tv2);
4149 tv1->v_type = VAR_FLOAT;
4150 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004151 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 }
4153 else
4154#endif
4155 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004156 int failed = FALSE;
4157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 switch (iptr->isn_arg.op.op_type)
4159 {
4160 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004161 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4162 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004163 goto on_error;
4164 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 case EXPR_SUB: n1 = n1 - n2; break;
4166 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004167 default: n1 = num_modulus(n1, n2, &failed);
4168 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004169 goto on_error;
4170 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 }
4172 clear_tv(tv1);
4173 clear_tv(tv2);
4174 tv1->v_type = VAR_NUMBER;
4175 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004176 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 }
4178 }
4179 break;
4180
4181 case ISN_CONCAT:
4182 {
4183 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4184 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4185 char_u *res;
4186
4187 res = concat_str(str1, str2);
4188 clear_tv(STACK_TV_BOT(-2));
4189 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004190 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 STACK_TV_BOT(-1)->vval.v_string = res;
4192 }
4193 break;
4194
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004195 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004196 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004197 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004198 int is_slice = iptr->isn_type == ISN_STRSLICE;
4199 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004200 char_u *res;
4201
4202 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004203 // string slice: string is at stack-3, first index at
4204 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004205 if (is_slice)
4206 {
4207 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004208 n1 = tv->vval.v_number;
4209 }
4210
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004211 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004212 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004213
Bram Moolenaar4c137212021-04-19 16:48:48 +02004214 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004215 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004216 if (is_slice)
4217 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004218 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004219 else
4220 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004221 // single character (including composing characters).
4222 // If the index is too big or negative the result is
4223 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004224 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004225 vim_free(tv->vval.v_string);
4226 tv->vval.v_string = res;
4227 }
4228 break;
4229
4230 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004231 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004232 case ISN_BLOBINDEX:
4233 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004235 int is_slice = iptr->isn_type == ISN_LISTSLICE
4236 || iptr->isn_type == ISN_BLOBSLICE;
4237 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4238 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004239 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004240 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241
4242 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004243 // list slice: list is at stack-3, indexes at stack-2 and
4244 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004245 // Same for blob.
4246 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247
4248 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004249 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004251
4252 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 {
Bram Moolenaared591872020-08-15 22:14:53 +02004254 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004255 n1 = tv->vval.v_number;
4256 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 }
Bram Moolenaared591872020-08-15 22:14:53 +02004258
Bram Moolenaar4c137212021-04-19 16:48:48 +02004259 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004260 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004261 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004262 if (is_blob)
4263 {
4264 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4265 n1, n2, FALSE, tv) == FAIL)
4266 goto on_error;
4267 }
4268 else
4269 {
4270 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4271 n1, n2, FALSE, tv, TRUE) == FAIL)
4272 goto on_error;
4273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 }
4275 break;
4276
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004277 case ISN_ANYINDEX:
4278 case ISN_ANYSLICE:
4279 {
4280 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4281 typval_T *var1, *var2;
4282 int res;
4283
4284 // index: composite is at stack-2, index at stack-1
4285 // slice: composite is at stack-3, indexes at stack-2 and
4286 // stack-1
4287 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004288 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004289 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4290 goto on_error;
4291 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4292 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004293 res = eval_index_inner(tv, is_slice, var1, var2,
4294 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004295 clear_tv(var1);
4296 if (is_slice)
4297 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004298 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004299 if (res == FAIL)
4300 goto on_error;
4301 }
4302 break;
4303
Bram Moolenaar9af78762020-06-16 11:34:42 +02004304 case ISN_SLICE:
4305 {
4306 list_T *list;
4307 int count = iptr->isn_arg.number;
4308
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004309 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004310 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004311 list = tv->vval.v_list;
4312
4313 // no error for short list, expect it to be checked earlier
4314 if (list != NULL && list->lv_len >= count)
4315 {
4316 list_T *newlist = list_slice(list,
4317 count, list->lv_len - 1);
4318
4319 if (newlist != NULL)
4320 {
4321 list_unref(list);
4322 tv->vval.v_list = newlist;
4323 ++newlist->lv_refcount;
4324 }
4325 }
4326 }
4327 break;
4328
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004329 case ISN_GETITEM:
4330 {
4331 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004332 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004333
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004334 // Get list item: list is at stack-1, push item.
4335 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004336 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4337 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004338
Bram Moolenaar35578162021-08-02 19:10:38 +02004339 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004340 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004341 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004342 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004343
4344 // Useful when used in unpack assignment. Reset at
4345 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004346 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004347 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004348 }
4349 break;
4350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 case ISN_MEMBER:
4352 {
4353 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004354 char_u *key;
4355 dictitem_T *di;
4356
4357 // dict member: dict is at stack-2, key at stack-1
4358 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004359 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004360 dict = tv->vval.v_dict;
4361
4362 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004363 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004364 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004365 if (key == NULL)
4366 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004367
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004368 if ((di = dict_find(dict, key, -1)) == NULL)
4369 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004370 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004371 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004372
4373 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004374 // stack contents makes sense and the dict stack is
4375 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004376 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004377 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004378 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004379 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004380 tv->v_type = VAR_NUMBER;
4381 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004382 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004383 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004384 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004386 // Put the dict used on the dict stack, it might be used by
4387 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004388 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004389 if (dict_stack_save(tv) == FAIL)
4390 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004391 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004392 }
4393 break;
4394
4395 // dict member with string key
4396 case ISN_STRINGMEMBER:
4397 {
4398 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 dictitem_T *di;
4400
4401 tv = STACK_TV_BOT(-1);
4402 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4403 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004404 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004405 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004406 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 }
4408 dict = tv->vval.v_dict;
4409
4410 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4411 == NULL)
4412 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004413 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004414 semsg(_(e_key_not_present_in_dictionary), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004415 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004417 // Put the dict used on the dict stack, it might be used by
4418 // a dict function later.
4419 if (dict_stack_save(tv) == FAIL)
4420 goto on_fatal_error;
4421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004423 }
4424 break;
4425
4426 case ISN_CLEARDICT:
4427 dict_stack_drop();
4428 break;
4429
4430 case ISN_USEDICT:
4431 {
4432 typval_T *dict_tv = dict_stack_get_tv();
4433
4434 // Turn "dict.Func" into a partial for "Func" bound to
4435 // "dict". Don't do this when "Func" is already a partial
4436 // that was bound explicitly (pt_auto is FALSE).
4437 tv = STACK_TV_BOT(-1);
4438 if (dict_tv != NULL
4439 && dict_tv->v_type == VAR_DICT
4440 && dict_tv->vval.v_dict != NULL
4441 && (tv->v_type == VAR_FUNC
4442 || (tv->v_type == VAR_PARTIAL
4443 && (tv->vval.v_partial->pt_auto
4444 || tv->vval.v_partial->pt_dict == NULL))))
4445 dict_tv->vval.v_dict =
4446 make_partial(dict_tv->vval.v_dict, tv);
4447 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 }
4449 break;
4450
4451 case ISN_NEGATENR:
4452 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004453 if (tv->v_type != VAR_NUMBER
4454#ifdef FEAT_FLOAT
4455 && tv->v_type != VAR_FLOAT
4456#endif
4457 )
4458 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004459 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004460 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004461 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004462 }
4463#ifdef FEAT_FLOAT
4464 if (tv->v_type == VAR_FLOAT)
4465 tv->vval.v_float = -tv->vval.v_float;
4466 else
4467#endif
4468 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 break;
4470
4471 case ISN_CHECKNR:
4472 {
4473 int error = FALSE;
4474
4475 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004476 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004478 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 (void)tv_get_number_chk(tv, &error);
4480 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004481 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 }
4483 break;
4484
4485 case ISN_CHECKTYPE:
4486 {
4487 checktype_T *ct = &iptr->isn_arg.type;
4488
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004489 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004490 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004491 if (!ectx->ec_where.wt_variable)
4492 ectx->ec_where.wt_index = ct->ct_arg_idx;
4493 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4494 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004495 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004496 if (!ectx->ec_where.wt_variable)
4497 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004498
4499 // number 0 is FALSE, number 1 is TRUE
4500 if (tv->v_type == VAR_NUMBER
4501 && ct->ct_type->tt_type == VAR_BOOL
4502 && (tv->vval.v_number == 0
4503 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004505 tv->v_type = VAR_BOOL;
4506 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004507 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 }
4509 }
4510 break;
4511
Bram Moolenaar9af78762020-06-16 11:34:42 +02004512 case ISN_CHECKLEN:
4513 {
4514 int min_len = iptr->isn_arg.checklen.cl_min_len;
4515 list_T *list = NULL;
4516
4517 tv = STACK_TV_BOT(-1);
4518 if (tv->v_type == VAR_LIST)
4519 list = tv->vval.v_list;
4520 if (list == NULL || list->lv_len < min_len
4521 || (list->lv_len > min_len
4522 && !iptr->isn_arg.checklen.cl_more_OK))
4523 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004524 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004525 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004526 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004527 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004528 }
4529 }
4530 break;
4531
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004532 case ISN_SETTYPE:
4533 {
4534 checktype_T *ct = &iptr->isn_arg.type;
4535
4536 tv = STACK_TV_BOT(-1);
4537 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4538 {
4539 free_type(tv->vval.v_dict->dv_type);
4540 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4541 }
4542 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4543 {
4544 free_type(tv->vval.v_list->lv_type);
4545 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4546 }
4547 }
4548 break;
4549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004551 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 {
4553 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004554 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004556 if (iptr->isn_type == ISN_2BOOL)
4557 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004558 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004559 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004560 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004561 n = !n;
4562 }
4563 else
4564 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004565 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004566 SOURCING_LNUM = iptr->isn_lnum;
4567 n = tv_get_bool_chk(tv, &error);
4568 if (error)
4569 goto on_error;
4570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 clear_tv(tv);
4572 tv->v_type = VAR_BOOL;
4573 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4574 }
4575 break;
4576
4577 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004578 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004579 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004580 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4581 iptr->isn_type == ISN_2STRING_ANY,
4582 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004583 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 break;
4585
Bram Moolenaar08597872020-12-10 19:43:40 +01004586 case ISN_RANGE:
4587 {
4588 exarg_T ea;
4589 char *errormsg;
4590
Bram Moolenaarece0b872021-01-08 20:40:45 +01004591 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004592 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004593 ea.addr_type = ADDR_LINES;
4594 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004595 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004596 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004597 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004598
Bram Moolenaar35578162021-08-02 19:10:38 +02004599 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004600 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004601 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004602 tv = STACK_TV_BOT(-1);
4603 tv->v_type = VAR_NUMBER;
4604 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004605 if (ea.addr_count == 0)
4606 tv->vval.v_number = curwin->w_cursor.lnum;
4607 else
4608 tv->vval.v_number = ea.line2;
4609 }
4610 break;
4611
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004612 case ISN_PUT:
4613 {
4614 int regname = iptr->isn_arg.put.put_regname;
4615 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4616 char_u *expr = NULL;
4617 int dir = FORWARD;
4618
Bram Moolenaar08597872020-12-10 19:43:40 +01004619 if (lnum < -2)
4620 {
4621 // line number was put on the stack by ISN_RANGE
4622 tv = STACK_TV_BOT(-1);
4623 curwin->w_cursor.lnum = tv->vval.v_number;
4624 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4625 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004626 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004627 }
4628 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004629 // :put! above cursor
4630 dir = BACKWARD;
4631 else if (lnum >= 0)
4632 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004633
4634 if (regname == '=')
4635 {
4636 tv = STACK_TV_BOT(-1);
4637 if (tv->v_type == VAR_STRING)
4638 expr = tv->vval.v_string;
4639 else
4640 {
4641 expr = typval2string(tv, TRUE); // allocates value
4642 clear_tv(tv);
4643 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004644 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004645 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004646 check_cursor();
4647 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4648 vim_free(expr);
4649 }
4650 break;
4651
Bram Moolenaar02194d22020-10-24 23:08:38 +02004652 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004653 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4654 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4655 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4656 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004657 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4658 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004659 break;
4660
Bram Moolenaar02194d22020-10-24 23:08:38 +02004661 case ISN_CMDMOD_REV:
4662 // filter regprog is owned by the instruction, don't free it
4663 cmdmod.cmod_filter_regmatch.regprog = NULL;
4664 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004665 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4666 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004667 break;
4668
Bram Moolenaar792f7862020-11-23 08:31:18 +01004669 case ISN_UNPACK:
4670 {
4671 int count = iptr->isn_arg.unpack.unp_count;
4672 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4673 list_T *l;
4674 listitem_T *li;
4675 int i;
4676
4677 // Check there is a valid list to unpack.
4678 tv = STACK_TV_BOT(-1);
4679 if (tv->v_type != VAR_LIST)
4680 {
4681 SOURCING_LNUM = iptr->isn_lnum;
4682 emsg(_(e_for_argument_must_be_sequence_of_lists));
4683 goto on_error;
4684 }
4685 l = tv->vval.v_list;
4686 if (l == NULL
4687 || l->lv_len < (semicolon ? count - 1 : count))
4688 {
4689 SOURCING_LNUM = iptr->isn_lnum;
4690 emsg(_(e_list_value_does_not_have_enough_items));
4691 goto on_error;
4692 }
4693 else if (!semicolon && l->lv_len > count)
4694 {
4695 SOURCING_LNUM = iptr->isn_lnum;
4696 emsg(_(e_list_value_has_more_items_than_targets));
4697 goto on_error;
4698 }
4699
4700 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004701 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004702 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004703 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004704
4705 // Variable after semicolon gets a list with the remaining
4706 // items.
4707 if (semicolon)
4708 {
4709 list_T *rem_list =
4710 list_alloc_with_items(l->lv_len - count + 1);
4711
4712 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004713 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004714 tv = STACK_TV_BOT(-count);
4715 tv->vval.v_list = rem_list;
4716 ++rem_list->lv_refcount;
4717 tv->v_lock = 0;
4718 li = l->lv_first;
4719 for (i = 0; i < count - 1; ++i)
4720 li = li->li_next;
4721 for (i = 0; li != NULL; ++i)
4722 {
4723 list_set_item(rem_list, i, &li->li_tv);
4724 li = li->li_next;
4725 }
4726 --count;
4727 }
4728
4729 // Produce the values in reverse order, first item last.
4730 li = l->lv_first;
4731 for (i = 0; i < count; ++i)
4732 {
4733 tv = STACK_TV_BOT(-i - 1);
4734 copy_tv(&li->li_tv, tv);
4735 li = li->li_next;
4736 }
4737
4738 list_unref(l);
4739 }
4740 break;
4741
Bram Moolenaarb2049902021-01-24 12:53:53 +01004742 case ISN_PROF_START:
4743 case ISN_PROF_END:
4744 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004745#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004746 funccall_T cookie;
4747 ufunc_T *cur_ufunc =
4748 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004749 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004750
4751 cookie.func = cur_ufunc;
4752 if (iptr->isn_type == ISN_PROF_START)
4753 {
4754 func_line_start(&cookie, iptr->isn_lnum);
4755 // if we get here the instruction is executed
4756 func_line_exec(&cookie);
4757 }
4758 else
4759 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004760#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004761 }
4762 break;
4763
Bram Moolenaare99d4222021-06-13 14:01:26 +02004764 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004765 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004766 break;
4767
Bram Moolenaar389df252020-07-09 21:20:47 +02004768 case ISN_SHUFFLE:
4769 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004770 typval_T tmp_tv;
4771 int item = iptr->isn_arg.shuffle.shfl_item;
4772 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004773
4774 tmp_tv = *STACK_TV_BOT(-item);
4775 for ( ; up > 0 && item > 1; --up)
4776 {
4777 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4778 --item;
4779 }
4780 *STACK_TV_BOT(-item) = tmp_tv;
4781 }
4782 break;
4783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004785 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004787 ectx->ec_where.wt_index = 0;
4788 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 break;
4790 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004791 continue;
4792
Bram Moolenaard032f342020-07-18 18:13:02 +02004793func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004794 // Restore previous function. If the frame pointer is where we started
4795 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004796 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004797 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004798
Bram Moolenaar4c137212021-04-19 16:48:48 +02004799 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004800 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004801 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004802 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004803
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004804on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004805 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004806 // If "emsg_silent" is set then ignore the error, unless it was set
4807 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004808 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004809 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004810 {
4811 // If a sequence of instructions causes an error while ":silent!"
4812 // was used, restore the stack length and jump ahead to restoring
4813 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004814 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004815 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004816 while (ectx->ec_stack.ga_len
4817 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004818 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004819 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004820 clear_tv(STACK_TV_BOT(0));
4821 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004822 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4823 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004824 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004825 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004826 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004827on_fatal_error:
4828 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004829 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004830 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004831 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 }
4833
4834done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004835 ret = OK;
4836theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004837 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004838 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4839 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004840}
4841
4842/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004843 * Execute the instructions from a VAR_INSTR typeval and put the result in
4844 * "rettv".
4845 * Return OK or FAIL.
4846 */
4847 int
4848exe_typval_instr(typval_T *tv, typval_T *rettv)
4849{
4850 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4851 isn_T *save_instr = ectx->ec_instr;
4852 int save_iidx = ectx->ec_iidx;
4853 int res;
4854
4855 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4856 res = exec_instructions(ectx);
4857 if (res == OK)
4858 {
4859 *rettv = *STACK_TV_BOT(-1);
4860 --ectx->ec_stack.ga_len;
4861 }
4862
4863 ectx->ec_instr = save_instr;
4864 ectx->ec_iidx = save_iidx;
4865
4866 return res;
4867}
4868
4869/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004870 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4871 * "substitute_instr".
4872 */
4873 char_u *
4874exe_substitute_instr(void)
4875{
4876 ectx_T *ectx = substitute_instr->subs_ectx;
4877 isn_T *save_instr = ectx->ec_instr;
4878 int save_iidx = ectx->ec_iidx;
4879 char_u *res;
4880
4881 ectx->ec_instr = substitute_instr->subs_instr;
4882 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004883 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004884 typval_T *tv = STACK_TV_BOT(-1);
4885
Bram Moolenaar27523602021-06-05 21:36:19 +02004886 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004887 --ectx->ec_stack.ga_len;
4888 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004889 }
4890 else
4891 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004892 substitute_instr->subs_status = FAIL;
4893 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895
Bram Moolenaar4c137212021-04-19 16:48:48 +02004896 ectx->ec_instr = save_instr;
4897 ectx->ec_iidx = save_iidx;
4898
4899 return res;
4900}
4901
4902/*
4903 * Call a "def" function from old Vim script.
4904 * Return OK or FAIL.
4905 */
4906 int
4907call_def_function(
4908 ufunc_T *ufunc,
4909 int argc_arg, // nr of arguments
4910 typval_T *argv, // arguments
4911 partial_T *partial, // optional partial for context
4912 typval_T *rettv) // return value
4913{
4914 ectx_T ectx; // execution context
4915 int argc = argc_arg;
4916 typval_T *tv;
4917 int idx;
4918 int ret = FAIL;
4919 int defcount = ufunc->uf_args.ga_len - argc;
4920 sctx_T save_current_sctx = current_sctx;
4921 int did_emsg_before = did_emsg_cumul + did_emsg;
4922 int save_suppress_errthrow = suppress_errthrow;
4923 msglist_T **saved_msg_list = NULL;
4924 msglist_T *private_msg_list = NULL;
4925 int save_emsg_silent_def = emsg_silent_def;
4926 int save_did_emsg_def = did_emsg_def;
4927 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004928 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004929
4930// Get pointer to item in the stack.
4931#undef STACK_TV
4932#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4933
4934// Get pointer to item at the bottom of the stack, -1 is the bottom.
4935#undef STACK_TV_BOT
4936#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4937
4938// Get pointer to a local variable on the stack. Negative for arguments.
4939#undef STACK_TV_VAR
4940#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4941
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004942 // Update uf_has_breakpoint if needed.
4943 update_has_breakpoint(ufunc);
4944
Bram Moolenaar4c137212021-04-19 16:48:48 +02004945 if (ufunc->uf_def_status == UF_NOT_COMPILED
4946 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004947 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4948 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004949 == FAIL))
4950 {
4951 if (did_emsg_cumul + did_emsg == did_emsg_before)
4952 semsg(_(e_function_is_not_compiled_str),
4953 printable_func_name(ufunc));
4954 return FAIL;
4955 }
4956
4957 {
4958 // Check the function was really compiled.
4959 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4960 + ufunc->uf_dfunc_idx;
4961 if (INSTRUCTIONS(dfunc) == NULL)
4962 {
4963 iemsg("using call_def_function() on not compiled function");
4964 return FAIL;
4965 }
4966 }
4967
4968 // If depth of calling is getting too high, don't execute the function.
4969 orig_funcdepth = funcdepth_get();
4970 if (funcdepth_increment() == FAIL)
4971 return FAIL;
4972
4973 CLEAR_FIELD(ectx);
4974 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4975 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004976 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004977 {
4978 funcdepth_decrement();
4979 return FAIL;
4980 }
4981 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4982 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4983 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004984 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004985
4986 idx = argc - ufunc->uf_args.ga_len;
4987 if (idx > 0 && ufunc->uf_va_name == NULL)
4988 {
4989 if (idx == 1)
4990 emsg(_(e_one_argument_too_many));
4991 else
4992 semsg(_(e_nr_arguments_too_many), idx);
4993 goto failed_early;
4994 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004995 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4996 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004997 {
4998 if (idx == -1)
4999 emsg(_(e_one_argument_too_few));
5000 else
5001 semsg(_(e_nr_arguments_too_few), -idx);
5002 goto failed_early;
5003 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005004
5005 // Put arguments on the stack, but no more than what the function expects.
5006 // A lambda can be called with more arguments than it uses.
5007 for (idx = 0; idx < argc
5008 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5009 ++idx)
5010 {
5011 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5012 && argv[idx].v_type == VAR_SPECIAL
5013 && argv[idx].vval.v_number == VVAL_NONE)
5014 {
5015 // Use the default value.
5016 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5017 }
5018 else
5019 {
5020 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5021 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005022 ufunc->uf_arg_types[idx], &argv[idx],
5023 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005024 goto failed_early;
5025 copy_tv(&argv[idx], STACK_TV_BOT(0));
5026 }
5027 ++ectx.ec_stack.ga_len;
5028 }
5029
5030 // Turn varargs into a list. Empty list if no args.
5031 if (ufunc->uf_va_name != NULL)
5032 {
5033 int vararg_count = argc - ufunc->uf_args.ga_len;
5034
5035 if (vararg_count < 0)
5036 vararg_count = 0;
5037 else
5038 argc -= vararg_count;
5039 if (exe_newlist(vararg_count, &ectx) == FAIL)
5040 goto failed_early;
5041
5042 // Check the type of the list items.
5043 tv = STACK_TV_BOT(-1);
5044 if (ufunc->uf_va_type != NULL
5045 && ufunc->uf_va_type != &t_list_any
5046 && ufunc->uf_va_type->tt_member != &t_any
5047 && tv->vval.v_list != NULL)
5048 {
5049 type_T *expected = ufunc->uf_va_type->tt_member;
5050 listitem_T *li = tv->vval.v_list->lv_first;
5051
5052 for (idx = 0; idx < vararg_count; ++idx)
5053 {
5054 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005055 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005056 goto failed_early;
5057 li = li->li_next;
5058 }
5059 }
5060
5061 if (defcount > 0)
5062 // Move varargs list to below missing default arguments.
5063 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5064 --ectx.ec_stack.ga_len;
5065 }
5066
5067 // Make space for omitted arguments, will store default value below.
5068 // Any varargs list goes after them.
5069 if (defcount > 0)
5070 for (idx = 0; idx < defcount; ++idx)
5071 {
5072 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5073 ++ectx.ec_stack.ga_len;
5074 }
5075 if (ufunc->uf_va_name != NULL)
5076 ++ectx.ec_stack.ga_len;
5077
5078 // Frame pointer points to just after arguments.
5079 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5080 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5081
5082 {
5083 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5084 + ufunc->uf_dfunc_idx;
5085 ufunc_T *base_ufunc = dfunc->df_ufunc;
5086
5087 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5088 // by copy_func().
5089 if (partial != NULL || base_ufunc->uf_partial != NULL)
5090 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005091 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5092 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005093 goto failed_early;
5094 if (partial != NULL)
5095 {
5096 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
5097 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005098 if (current_ectx->ec_outer_ref != NULL
5099 && current_ectx->ec_outer_ref->or_outer != NULL)
5100 ectx.ec_outer_ref->or_outer =
5101 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005102 }
5103 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005104 {
5105 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
5106 ++partial->pt_refcount;
5107 ectx.ec_outer_ref->or_partial = partial;
5108 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005109 }
5110 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005111 {
5112 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5113 ++base_ufunc->uf_partial->pt_refcount;
5114 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5115 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005116 }
5117 }
5118
5119 // dummy frame entries
5120 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5121 {
5122 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5123 ++ectx.ec_stack.ga_len;
5124 }
5125
5126 {
5127 // Reserve space for local variables and any closure reference count.
5128 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5129 + ufunc->uf_dfunc_idx;
5130
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005131 // Initialize variables to zero. That avoids having to generate
5132 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005133 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005134 {
5135 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5136 STACK_TV_VAR(idx)->vval.v_number = 0;
5137 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005138 ectx.ec_stack.ga_len += dfunc->df_varcount;
5139 if (dfunc->df_has_closure)
5140 {
5141 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5142 STACK_TV_VAR(idx)->vval.v_number = 0;
5143 ++ectx.ec_stack.ga_len;
5144 }
5145
5146 ectx.ec_instr = INSTRUCTIONS(dfunc);
5147 }
5148
5149 // Following errors are in the function, not the caller.
5150 // Commands behave like vim9script.
5151 estack_push_ufunc(ufunc, 1);
5152 current_sctx = ufunc->uf_script_ctx;
5153 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5154
5155 // Use a specific location for storing error messages to be converted to an
5156 // exception.
5157 saved_msg_list = msg_list;
5158 msg_list = &private_msg_list;
5159
5160 // Do turn errors into exceptions.
5161 suppress_errthrow = FALSE;
5162
5163 // Do not delete the function while executing it.
5164 ++ufunc->uf_calls;
5165
5166 // When ":silent!" was used before calling then we still abort the
5167 // function. If ":silent!" is used in the function then we don't.
5168 emsg_silent_def = emsg_silent;
5169 did_emsg_def = 0;
5170
5171 ectx.ec_where.wt_index = 0;
5172 ectx.ec_where.wt_variable = FALSE;
5173
5174 // Execute the instructions until done.
5175 ret = exec_instructions(&ectx);
5176 if (ret == OK)
5177 {
5178 // function finished, get result from the stack.
5179 if (ufunc->uf_ret_type == &t_void)
5180 {
5181 rettv->v_type = VAR_VOID;
5182 }
5183 else
5184 {
5185 tv = STACK_TV_BOT(-1);
5186 *rettv = *tv;
5187 tv->v_type = VAR_UNKNOWN;
5188 }
5189 }
5190
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005191 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005192 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5193 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005194
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005195 // Deal with any remaining closures, they may be in use somewhere.
5196 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005197 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005198 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005199 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005200 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005201
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005202 estack_pop();
5203 current_sctx = save_current_sctx;
5204
Bram Moolenaar2336c372021-12-06 15:06:54 +00005205 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5206 // Function was unreferenced while being used, free it now.
5207 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005208
Bram Moolenaar352134b2020-10-17 22:04:08 +02005209 if (*msg_list != NULL && saved_msg_list != NULL)
5210 {
5211 msglist_T **plist = saved_msg_list;
5212
5213 // Append entries from the current msg_list (uncaught exceptions) to
5214 // the saved msg_list.
5215 while (*plist != NULL)
5216 plist = &(*plist)->next;
5217
5218 *plist = *msg_list;
5219 }
5220 msg_list = saved_msg_list;
5221
Bram Moolenaar4c137212021-04-19 16:48:48 +02005222 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005223 {
5224 cmdmod.cmod_filter_regmatch.regprog = NULL;
5225 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005226 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005227 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005228 emsg_silent_def = save_emsg_silent_def;
5229 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005230
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005231failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005232 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5234 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005235 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005238 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005239 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005240 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005241 if (ectx.ec_outer_ref->or_outer_allocated)
5242 vim_free(ectx.ec_outer_ref->or_outer);
5243 partial_unref(ectx.ec_outer_ref->or_partial);
5244 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005245 }
5246
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005247 // Not sure if this is necessary.
5248 suppress_errthrow = save_suppress_errthrow;
5249
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005250 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5251 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005252 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005253 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005254 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 return ret;
5256}
5257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005259 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5260 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5261 * "pfx" is prefixed to every line.
5262 */
5263 static void
5264list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5265{
5266 int line_idx = 0;
5267 int prev_current = 0;
5268 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005269 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005270
5271 for (current = 0; current < instr_count; ++current)
5272 {
5273 isn_T *iptr = &instr[current];
5274 char *line;
5275
5276 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005277 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005278 while (line_idx < iptr->isn_lnum
5279 && line_idx < ufunc->uf_lines.ga_len)
5280 {
5281 if (current > prev_current)
5282 {
5283 msg_puts("\n\n");
5284 prev_current = current;
5285 }
5286 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5287 if (line != NULL)
5288 msg(line);
5289 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005290 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5291 {
5292 int first_def_arg = ufunc->uf_args.ga_len
5293 - ufunc->uf_def_args.ga_len;
5294
5295 if (def_arg_idx > 0)
5296 msg_puts("\n\n");
5297 msg_start();
5298 msg_puts(" ");
5299 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5300 first_def_arg + def_arg_idx]);
5301 msg_puts(" = ");
5302 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5303 msg_clr_eos();
5304 msg_end();
5305 }
5306 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005307
5308 switch (iptr->isn_type)
5309 {
5310 case ISN_EXEC:
5311 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5312 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005313 case ISN_EXEC_SPLIT:
5314 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5315 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005316 case ISN_EXECRANGE:
5317 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5318 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005319 case ISN_LEGACY_EVAL:
5320 smsg("%s%4d EVAL legacy %s", pfx, current,
5321 iptr->isn_arg.string);
5322 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005323 case ISN_REDIRSTART:
5324 smsg("%s%4d REDIR", pfx, current);
5325 break;
5326 case ISN_REDIREND:
5327 smsg("%s%4d REDIR END%s", pfx, current,
5328 iptr->isn_arg.number ? " append" : "");
5329 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005330 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005331#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005332 smsg("%s%4d CEXPR pre %s", pfx, current,
5333 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005334#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005335 break;
5336 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005337#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005338 {
5339 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5340
5341 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5342 cexpr_get_auname(cer->cer_cmdidx),
5343 cer->cer_forceit ? "!" : "",
5344 cer->cer_cmdline);
5345 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005346#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005347 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005348 case ISN_INSTR:
5349 {
5350 smsg("%s%4d INSTR", pfx, current);
5351 list_instructions(" ", iptr->isn_arg.instr,
5352 INT_MAX, NULL);
5353 msg(" -------------");
5354 }
5355 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005356 case ISN_SUBSTITUTE:
5357 {
5358 subs_T *subs = &iptr->isn_arg.subs;
5359
5360 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5361 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5362 msg(" -------------");
5363 }
5364 break;
5365 case ISN_EXECCONCAT:
5366 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5367 (varnumber_T)iptr->isn_arg.number);
5368 break;
5369 case ISN_ECHO:
5370 {
5371 echo_T *echo = &iptr->isn_arg.echo;
5372
5373 smsg("%s%4d %s %d", pfx, current,
5374 echo->echo_with_white ? "ECHO" : "ECHON",
5375 echo->echo_count);
5376 }
5377 break;
5378 case ISN_EXECUTE:
5379 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005380 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005381 break;
5382 case ISN_ECHOMSG:
5383 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005384 (varnumber_T)(iptr->isn_arg.number));
5385 break;
5386 case ISN_ECHOCONSOLE:
5387 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5388 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005389 break;
5390 case ISN_ECHOERR:
5391 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005392 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005393 break;
5394 case ISN_LOAD:
5395 {
5396 if (iptr->isn_arg.number < 0)
5397 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5398 (varnumber_T)(iptr->isn_arg.number
5399 + STACK_FRAME_SIZE));
5400 else
5401 smsg("%s%4d LOAD $%lld", pfx, current,
5402 (varnumber_T)(iptr->isn_arg.number));
5403 }
5404 break;
5405 case ISN_LOADOUTER:
5406 {
5407 if (iptr->isn_arg.number < 0)
5408 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5409 iptr->isn_arg.outer.outer_depth,
5410 iptr->isn_arg.outer.outer_idx
5411 + STACK_FRAME_SIZE);
5412 else
5413 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5414 iptr->isn_arg.outer.outer_depth,
5415 iptr->isn_arg.outer.outer_idx);
5416 }
5417 break;
5418 case ISN_LOADV:
5419 smsg("%s%4d LOADV v:%s", pfx, current,
5420 get_vim_var_name(iptr->isn_arg.number));
5421 break;
5422 case ISN_LOADSCRIPT:
5423 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005424 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5425 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5426 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005427
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005428 sv = get_script_svar(sref, -1);
5429 if (sv == NULL)
5430 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5431 pfx, current, si->sn_name);
5432 else
5433 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005434 sv->sv_name,
5435 sref->sref_idx,
5436 si->sn_name);
5437 }
5438 break;
5439 case ISN_LOADS:
5440 {
5441 scriptitem_T *si = SCRIPT_ITEM(
5442 iptr->isn_arg.loadstore.ls_sid);
5443
5444 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5445 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5446 }
5447 break;
5448 case ISN_LOADAUTO:
5449 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5450 break;
5451 case ISN_LOADG:
5452 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5453 break;
5454 case ISN_LOADB:
5455 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5456 break;
5457 case ISN_LOADW:
5458 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5459 break;
5460 case ISN_LOADT:
5461 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5462 break;
5463 case ISN_LOADGDICT:
5464 smsg("%s%4d LOAD g:", pfx, current);
5465 break;
5466 case ISN_LOADBDICT:
5467 smsg("%s%4d LOAD b:", pfx, current);
5468 break;
5469 case ISN_LOADWDICT:
5470 smsg("%s%4d LOAD w:", pfx, current);
5471 break;
5472 case ISN_LOADTDICT:
5473 smsg("%s%4d LOAD t:", pfx, current);
5474 break;
5475 case ISN_LOADOPT:
5476 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5477 break;
5478 case ISN_LOADENV:
5479 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5480 break;
5481 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005482 smsg("%s%4d LOADREG @%c", pfx, current,
5483 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005484 break;
5485
5486 case ISN_STORE:
5487 if (iptr->isn_arg.number < 0)
5488 smsg("%s%4d STORE arg[%lld]", pfx, current,
5489 iptr->isn_arg.number + STACK_FRAME_SIZE);
5490 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005491 smsg("%s%4d STORE $%lld", pfx, current,
5492 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005493 break;
5494 case ISN_STOREOUTER:
5495 {
5496 if (iptr->isn_arg.number < 0)
5497 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5498 iptr->isn_arg.outer.outer_depth,
5499 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5500 else
5501 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5502 iptr->isn_arg.outer.outer_depth,
5503 iptr->isn_arg.outer.outer_idx);
5504 }
5505 break;
5506 case ISN_STOREV:
5507 smsg("%s%4d STOREV v:%s", pfx, current,
5508 get_vim_var_name(iptr->isn_arg.number));
5509 break;
5510 case ISN_STOREAUTO:
5511 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5512 break;
5513 case ISN_STOREG:
5514 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5515 break;
5516 case ISN_STOREB:
5517 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5518 break;
5519 case ISN_STOREW:
5520 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5521 break;
5522 case ISN_STORET:
5523 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5524 break;
5525 case ISN_STORES:
5526 {
5527 scriptitem_T *si = SCRIPT_ITEM(
5528 iptr->isn_arg.loadstore.ls_sid);
5529
5530 smsg("%s%4d STORES %s in %s", pfx, current,
5531 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5532 }
5533 break;
5534 case ISN_STORESCRIPT:
5535 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005536 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5537 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5538 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005539
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005540 sv = get_script_svar(sref, -1);
5541 if (sv == NULL)
5542 smsg("%s%4d STORESCRIPT [deleted] in %s",
5543 pfx, current, si->sn_name);
5544 else
5545 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005546 sv->sv_name,
5547 sref->sref_idx,
5548 si->sn_name);
5549 }
5550 break;
5551 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005552 case ISN_STOREFUNCOPT:
5553 smsg("%s%4d %s &%s", pfx, current,
5554 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005555 iptr->isn_arg.storeopt.so_name);
5556 break;
5557 case ISN_STOREENV:
5558 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5559 break;
5560 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005561 smsg("%s%4d STOREREG @%c", pfx, current,
5562 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005563 break;
5564 case ISN_STORENR:
5565 smsg("%s%4d STORE %lld in $%d", pfx, current,
5566 iptr->isn_arg.storenr.stnr_val,
5567 iptr->isn_arg.storenr.stnr_idx);
5568 break;
5569
5570 case ISN_STOREINDEX:
5571 smsg("%s%4d STOREINDEX %s", pfx, current,
5572 vartype_name(iptr->isn_arg.vartype));
5573 break;
5574
5575 case ISN_STORERANGE:
5576 smsg("%s%4d STORERANGE", pfx, current);
5577 break;
5578
5579 // constants
5580 case ISN_PUSHNR:
5581 smsg("%s%4d PUSHNR %lld", pfx, current,
5582 (varnumber_T)(iptr->isn_arg.number));
5583 break;
5584 case ISN_PUSHBOOL:
5585 case ISN_PUSHSPEC:
5586 smsg("%s%4d PUSH %s", pfx, current,
5587 get_var_special_name(iptr->isn_arg.number));
5588 break;
5589 case ISN_PUSHF:
5590#ifdef FEAT_FLOAT
5591 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5592#endif
5593 break;
5594 case ISN_PUSHS:
5595 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5596 break;
5597 case ISN_PUSHBLOB:
5598 {
5599 char_u *r;
5600 char_u numbuf[NUMBUFLEN];
5601 char_u *tofree;
5602
5603 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5604 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5605 vim_free(tofree);
5606 }
5607 break;
5608 case ISN_PUSHFUNC:
5609 {
5610 char *name = (char *)iptr->isn_arg.string;
5611
5612 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5613 name == NULL ? "[none]" : name);
5614 }
5615 break;
5616 case ISN_PUSHCHANNEL:
5617#ifdef FEAT_JOB_CHANNEL
5618 {
5619 channel_T *channel = iptr->isn_arg.channel;
5620
5621 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5622 channel == NULL ? 0 : channel->ch_id);
5623 }
5624#endif
5625 break;
5626 case ISN_PUSHJOB:
5627#ifdef FEAT_JOB_CHANNEL
5628 {
5629 typval_T tv;
5630 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005631 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005632
5633 tv.v_type = VAR_JOB;
5634 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005635 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005636 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5637 }
5638#endif
5639 break;
5640 case ISN_PUSHEXC:
5641 smsg("%s%4d PUSH v:exception", pfx, current);
5642 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005643 case ISN_AUTOLOAD:
5644 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5645 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005646 case ISN_UNLET:
5647 smsg("%s%4d UNLET%s %s", pfx, current,
5648 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5649 iptr->isn_arg.unlet.ul_name);
5650 break;
5651 case ISN_UNLETENV:
5652 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5653 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5654 iptr->isn_arg.unlet.ul_name);
5655 break;
5656 case ISN_UNLETINDEX:
5657 smsg("%s%4d UNLETINDEX", pfx, current);
5658 break;
5659 case ISN_UNLETRANGE:
5660 smsg("%s%4d UNLETRANGE", pfx, current);
5661 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005662 case ISN_LOCKUNLOCK:
5663 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5664 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005665 case ISN_LOCKCONST:
5666 smsg("%s%4d LOCKCONST", pfx, current);
5667 break;
5668 case ISN_NEWLIST:
5669 smsg("%s%4d NEWLIST size %lld", pfx, current,
5670 (varnumber_T)(iptr->isn_arg.number));
5671 break;
5672 case ISN_NEWDICT:
5673 smsg("%s%4d NEWDICT size %lld", pfx, current,
5674 (varnumber_T)(iptr->isn_arg.number));
5675 break;
5676
5677 // function call
5678 case ISN_BCALL:
5679 {
5680 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5681
5682 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5683 internal_func_name(cbfunc->cbf_idx),
5684 cbfunc->cbf_argcount);
5685 }
5686 break;
5687 case ISN_DCALL:
5688 {
5689 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5690 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5691 + cdfunc->cdf_idx;
5692
5693 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005694 printable_func_name(df->df_ufunc),
5695 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005696 }
5697 break;
5698 case ISN_UCALL:
5699 {
5700 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5701
5702 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5703 cufunc->cuf_name, cufunc->cuf_argcount);
5704 }
5705 break;
5706 case ISN_PCALL:
5707 {
5708 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5709
5710 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5711 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5712 }
5713 break;
5714 case ISN_PCALL_END:
5715 smsg("%s%4d PCALL end", pfx, current);
5716 break;
5717 case ISN_RETURN:
5718 smsg("%s%4d RETURN", pfx, current);
5719 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005720 case ISN_RETURN_VOID:
5721 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005722 break;
5723 case ISN_FUNCREF:
5724 {
5725 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005726 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005727
Bram Moolenaar38453522021-11-28 22:00:12 +00005728 if (funcref->fr_func_name == NULL)
5729 {
5730 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5731 + funcref->fr_dfunc_idx;
5732 name = df->df_ufunc->uf_name;
5733 }
5734 else
5735 name = funcref->fr_func_name;
5736 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005737 }
5738 break;
5739
5740 case ISN_NEWFUNC:
5741 {
5742 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5743
5744 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5745 newfunc->nf_lambda, newfunc->nf_global);
5746 }
5747 break;
5748
5749 case ISN_DEF:
5750 {
5751 char_u *name = iptr->isn_arg.string;
5752
5753 smsg("%s%4d DEF %s", pfx, current,
5754 name == NULL ? (char_u *)"" : name);
5755 }
5756 break;
5757
5758 case ISN_JUMP:
5759 {
5760 char *when = "?";
5761
5762 switch (iptr->isn_arg.jump.jump_when)
5763 {
5764 case JUMP_ALWAYS:
5765 when = "JUMP";
5766 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005767 case JUMP_NEVER:
5768 iemsg("JUMP_NEVER should not be used");
5769 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005770 case JUMP_AND_KEEP_IF_TRUE:
5771 when = "JUMP_AND_KEEP_IF_TRUE";
5772 break;
5773 case JUMP_IF_FALSE:
5774 when = "JUMP_IF_FALSE";
5775 break;
5776 case JUMP_AND_KEEP_IF_FALSE:
5777 when = "JUMP_AND_KEEP_IF_FALSE";
5778 break;
5779 case JUMP_IF_COND_FALSE:
5780 when = "JUMP_IF_COND_FALSE";
5781 break;
5782 case JUMP_IF_COND_TRUE:
5783 when = "JUMP_IF_COND_TRUE";
5784 break;
5785 }
5786 smsg("%s%4d %s -> %d", pfx, current, when,
5787 iptr->isn_arg.jump.jump_where);
5788 }
5789 break;
5790
5791 case ISN_JUMP_IF_ARG_SET:
5792 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5793 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5794 iptr->isn_arg.jump.jump_where);
5795 break;
5796
5797 case ISN_FOR:
5798 {
5799 forloop_T *forloop = &iptr->isn_arg.forloop;
5800
5801 smsg("%s%4d FOR $%d -> %d", pfx, current,
5802 forloop->for_idx, forloop->for_end);
5803 }
5804 break;
5805
5806 case ISN_TRY:
5807 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005808 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005809
5810 if (try->try_ref->try_finally == 0)
5811 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5812 pfx, current,
5813 try->try_ref->try_catch,
5814 try->try_ref->try_endtry);
5815 else
5816 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5817 pfx, current,
5818 try->try_ref->try_catch,
5819 try->try_ref->try_finally,
5820 try->try_ref->try_endtry);
5821 }
5822 break;
5823 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005824 smsg("%s%4d CATCH", pfx, current);
5825 break;
5826 case ISN_TRYCONT:
5827 {
5828 trycont_T *trycont = &iptr->isn_arg.trycont;
5829
5830 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5831 trycont->tct_levels,
5832 trycont->tct_levels == 1 ? "" : "s",
5833 trycont->tct_where);
5834 }
5835 break;
5836 case ISN_FINALLY:
5837 smsg("%s%4d FINALLY", pfx, current);
5838 break;
5839 case ISN_ENDTRY:
5840 smsg("%s%4d ENDTRY", pfx, current);
5841 break;
5842 case ISN_THROW:
5843 smsg("%s%4d THROW", pfx, current);
5844 break;
5845
5846 // expression operations on number
5847 case ISN_OPNR:
5848 case ISN_OPFLOAT:
5849 case ISN_OPANY:
5850 {
5851 char *what;
5852 char *ins;
5853
5854 switch (iptr->isn_arg.op.op_type)
5855 {
5856 case EXPR_MULT: what = "*"; break;
5857 case EXPR_DIV: what = "/"; break;
5858 case EXPR_REM: what = "%"; break;
5859 case EXPR_SUB: what = "-"; break;
5860 case EXPR_ADD: what = "+"; break;
5861 default: what = "???"; break;
5862 }
5863 switch (iptr->isn_type)
5864 {
5865 case ISN_OPNR: ins = "OPNR"; break;
5866 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5867 case ISN_OPANY: ins = "OPANY"; break;
5868 default: ins = "???"; break;
5869 }
5870 smsg("%s%4d %s %s", pfx, current, ins, what);
5871 }
5872 break;
5873
5874 case ISN_COMPAREBOOL:
5875 case ISN_COMPARESPECIAL:
5876 case ISN_COMPARENR:
5877 case ISN_COMPAREFLOAT:
5878 case ISN_COMPARESTRING:
5879 case ISN_COMPAREBLOB:
5880 case ISN_COMPARELIST:
5881 case ISN_COMPAREDICT:
5882 case ISN_COMPAREFUNC:
5883 case ISN_COMPAREANY:
5884 {
5885 char *p;
5886 char buf[10];
5887 char *type;
5888
5889 switch (iptr->isn_arg.op.op_type)
5890 {
5891 case EXPR_EQUAL: p = "=="; break;
5892 case EXPR_NEQUAL: p = "!="; break;
5893 case EXPR_GREATER: p = ">"; break;
5894 case EXPR_GEQUAL: p = ">="; break;
5895 case EXPR_SMALLER: p = "<"; break;
5896 case EXPR_SEQUAL: p = "<="; break;
5897 case EXPR_MATCH: p = "=~"; break;
5898 case EXPR_IS: p = "is"; break;
5899 case EXPR_ISNOT: p = "isnot"; break;
5900 case EXPR_NOMATCH: p = "!~"; break;
5901 default: p = "???"; break;
5902 }
5903 STRCPY(buf, p);
5904 if (iptr->isn_arg.op.op_ic == TRUE)
5905 strcat(buf, "?");
5906 switch(iptr->isn_type)
5907 {
5908 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5909 case ISN_COMPARESPECIAL:
5910 type = "COMPARESPECIAL"; break;
5911 case ISN_COMPARENR: type = "COMPARENR"; break;
5912 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5913 case ISN_COMPARESTRING:
5914 type = "COMPARESTRING"; break;
5915 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5916 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5917 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5918 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5919 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5920 default: type = "???"; break;
5921 }
5922
5923 smsg("%s%4d %s %s", pfx, current, type, buf);
5924 }
5925 break;
5926
5927 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5928 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5929
5930 // expression operations
5931 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5932 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5933 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5934 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5935 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5936 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5937 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5938 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5939 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5940 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5941 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5942 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005943 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005944 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5945 iptr->isn_arg.getitem.gi_index,
5946 iptr->isn_arg.getitem.gi_with_op ?
5947 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005948 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5949 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5950 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005951 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5952 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5953
Bram Moolenaar4c137212021-04-19 16:48:48 +02005954 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5955
5956 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5957 case ISN_CHECKTYPE:
5958 {
5959 checktype_T *ct = &iptr->isn_arg.type;
5960 char *tofree;
5961
5962 if (ct->ct_arg_idx == 0)
5963 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5964 type_name(ct->ct_type, &tofree),
5965 (int)ct->ct_off);
5966 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005967 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5968 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005969 type_name(ct->ct_type, &tofree),
5970 (int)ct->ct_off,
5971 (int)ct->ct_arg_idx);
5972 vim_free(tofree);
5973 break;
5974 }
5975 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5976 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5977 iptr->isn_arg.checklen.cl_min_len);
5978 break;
5979 case ISN_SETTYPE:
5980 {
5981 char *tofree;
5982
5983 smsg("%s%4d SETTYPE %s", pfx, current,
5984 type_name(iptr->isn_arg.type.ct_type, &tofree));
5985 vim_free(tofree);
5986 break;
5987 }
5988 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005989 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5990 smsg("%s%4d INVERT %d (!val)", pfx, current,
5991 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005992 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005993 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5994 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005995 break;
5996 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005997 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005998 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005999 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6000 pfx, current,
6001 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006002 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006003 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6004 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006005 break;
6006 case ISN_PUT:
6007 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6008 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006009 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006010 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6011 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006012 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006013 else
6014 smsg("%s%4d PUT %c %ld", pfx, current,
6015 iptr->isn_arg.put.put_regname,
6016 (long)iptr->isn_arg.put.put_lnum);
6017 break;
6018
Bram Moolenaar4c137212021-04-19 16:48:48 +02006019 case ISN_CMDMOD:
6020 {
6021 char_u *buf;
6022 size_t len = produce_cmdmods(
6023 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6024
6025 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006026 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006027 {
6028 (void)produce_cmdmods(
6029 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6030 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6031 vim_free(buf);
6032 }
6033 break;
6034 }
6035 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6036
6037 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006038 smsg("%s%4d PROFILE START line %d", pfx, current,
6039 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006040 break;
6041
6042 case ISN_PROF_END:
6043 smsg("%s%4d PROFILE END", pfx, current);
6044 break;
6045
Bram Moolenaare99d4222021-06-13 14:01:26 +02006046 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006047 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6048 iptr->isn_arg.debug.dbg_break_lnum + 1,
6049 iptr->isn_lnum,
6050 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006051 break;
6052
Bram Moolenaar4c137212021-04-19 16:48:48 +02006053 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6054 iptr->isn_arg.unpack.unp_count,
6055 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6056 break;
6057 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6058 iptr->isn_arg.shuffle.shfl_item,
6059 iptr->isn_arg.shuffle.shfl_up);
6060 break;
6061 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6062
6063 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6064 return;
6065 }
6066
6067 out_flush(); // output one line at a time
6068 ui_breakcheck();
6069 if (got_int)
6070 break;
6071 }
6072}
6073
6074/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006075 * Handle command line completion for the :disassemble command.
6076 */
6077 void
6078set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6079{
6080 char_u *p;
6081
6082 // Default: expand user functions, "debug" and "profile"
6083 xp->xp_context = EXPAND_DISASSEMBLE;
6084 xp->xp_pattern = arg;
6085
6086 // first argument already typed: only user function names
6087 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6088 {
6089 xp->xp_context = EXPAND_USER_FUNC;
6090 xp->xp_pattern = skipwhite(p);
6091 }
6092}
6093
6094/*
6095 * Function given to ExpandGeneric() to obtain the list of :disassemble
6096 * arguments.
6097 */
6098 char_u *
6099get_disassemble_argument(expand_T *xp, int idx)
6100{
6101 if (idx == 0)
6102 return (char_u *)"debug";
6103 if (idx == 1)
6104 return (char_u *)"profile";
6105 return get_user_func_name(xp, idx - 2);
6106}
6107
6108/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006109 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006110 * We don't really need this at runtime, but we do have tests that require it,
6111 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 */
6113 void
6114ex_disassemble(exarg_T *eap)
6115{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006116 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006117 char_u *fname;
6118 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119 dfunc_T *dfunc;
6120 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006121 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006122 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006123 compiletype_T compile_type = CT_NONE;
6124
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006125 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006126 {
6127 compile_type = CT_PROFILE;
6128 arg = skipwhite(arg + 7);
6129 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006130 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006131 {
6132 compile_type = CT_DEBUG;
6133 arg = skipwhite(arg + 5);
6134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006136 if (STRNCMP(arg, "<lambda>", 8) == 0)
6137 {
6138 arg += 8;
6139 (void)getdigits(&arg);
6140 fname = vim_strnsave(eap->arg, arg - eap->arg);
6141 }
6142 else
6143 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006144 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006145 if (fname == NULL)
6146 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006147 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006148 return;
6149 }
6150
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006151 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006152 if (ufunc == NULL)
6153 {
6154 char_u *p = untrans_function_name(fname);
6155
6156 if (p != NULL)
6157 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006158 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006159 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006160 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006161 if (ufunc == NULL)
6162 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006163 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006164 return;
6165 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006166 if (func_needs_compiling(ufunc, compile_type)
6167 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006168 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006169 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006170 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006171 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172 return;
6173 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006174 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175
6176 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006177 switch (compile_type)
6178 {
6179 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006180#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006181 instr = dfunc->df_instr_prof;
6182 instr_count = dfunc->df_instr_prof_count;
6183 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006184#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006185 // FALLTHROUGH
6186 case CT_NONE:
6187 instr = dfunc->df_instr;
6188 instr_count = dfunc->df_instr_count;
6189 break;
6190 case CT_DEBUG:
6191 instr = dfunc->df_instr_debug;
6192 instr_count = dfunc->df_instr_debug_count;
6193 break;
6194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195
Bram Moolenaar4c137212021-04-19 16:48:48 +02006196 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197}
6198
6199/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006200 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006201 * list, etc. Mostly like what JavaScript does, except that empty list and
6202 * empty dictionary are FALSE.
6203 */
6204 int
6205tv2bool(typval_T *tv)
6206{
6207 switch (tv->v_type)
6208 {
6209 case VAR_NUMBER:
6210 return tv->vval.v_number != 0;
6211 case VAR_FLOAT:
6212#ifdef FEAT_FLOAT
6213 return tv->vval.v_float != 0.0;
6214#else
6215 break;
6216#endif
6217 case VAR_PARTIAL:
6218 return tv->vval.v_partial != NULL;
6219 case VAR_FUNC:
6220 case VAR_STRING:
6221 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6222 case VAR_LIST:
6223 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6224 case VAR_DICT:
6225 return tv->vval.v_dict != NULL
6226 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6227 case VAR_BOOL:
6228 case VAR_SPECIAL:
6229 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6230 case VAR_JOB:
6231#ifdef FEAT_JOB_CHANNEL
6232 return tv->vval.v_job != NULL;
6233#else
6234 break;
6235#endif
6236 case VAR_CHANNEL:
6237#ifdef FEAT_JOB_CHANNEL
6238 return tv->vval.v_channel != NULL;
6239#else
6240 break;
6241#endif
6242 case VAR_BLOB:
6243 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6244 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006245 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006247 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248 break;
6249 }
6250 return FALSE;
6251}
6252
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006253 void
6254emsg_using_string_as(typval_T *tv, int as_number)
6255{
6256 semsg(_(as_number ? e_using_string_as_number_str
6257 : e_using_string_as_bool_str),
6258 tv->vval.v_string == NULL
6259 ? (char_u *)"" : tv->vval.v_string);
6260}
6261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262/*
6263 * If "tv" is a string give an error and return FAIL.
6264 */
6265 int
6266check_not_string(typval_T *tv)
6267{
6268 if (tv->v_type == VAR_STRING)
6269 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006270 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 clear_tv(tv);
6272 return FAIL;
6273 }
6274 return OK;
6275}
6276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277
6278#endif // FEAT_EVAL