blob: 72cbb9044d8aacc7b15c7dcb4063b061ed419b1f [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)
177 ga_init2(&dict_stack, (int)sizeof(typval_T), 10);
178 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
274 emsg_funcname(e_func_deleted,
275 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 Moolenaar8a7d6542020-01-26 15:56:19 +0100400 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200401 if (dfunc->df_has_closure)
402 {
403 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
404
405 tv->v_type = VAR_NUMBER;
406 tv->vval.v_number = 0;
407 }
408 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100410 if (pt != NULL || ufunc->uf_partial != NULL
411 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100412 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200413 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100414
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200415 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100416 return FAIL;
417 if (pt != NULL)
418 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200419 ref->or_outer = &pt->pt_outer;
420 ++pt->pt_refcount;
421 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100422 }
423 else if (ufunc->uf_partial != NULL)
424 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200425 ref->or_outer = &ufunc->uf_partial->pt_outer;
426 ++ufunc->uf_partial->pt_refcount;
427 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100428 }
429 else
430 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200431 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200432 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200433 {
434 vim_free(ref);
435 return FAIL;
436 }
437 ref->or_outer_allocated = TRUE;
438 ref->or_outer->out_stack = &ectx->ec_stack;
439 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
440 if (ectx->ec_outer_ref != NULL)
441 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100442 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200443 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100444 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100445 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200446 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100447
Bram Moolenaarc970e422021-03-17 15:03:04 +0100448 ++ufunc->uf_calls;
449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 // Set execution state to the start of the called function.
451 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100452 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100453 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200454 if (entry != NULL)
455 {
456 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200457 // Save the current context so it can be restored on return.
458 entry->es_save_sctx = current_sctx;
459 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200460 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100461
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200462 // Start execution at the first instruction.
463 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464
465 return OK;
466}
467
468// Get pointer to item in the stack.
469#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
470
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000471// Double linked list of funcstack_T in use.
472static funcstack_T *first_funcstack = NULL;
473
474 static void
475add_funcstack_to_list(funcstack_T *funcstack)
476{
477 // Link in list of funcstacks.
478 if (first_funcstack != NULL)
479 first_funcstack->fs_prev = funcstack;
480 funcstack->fs_next = first_funcstack;
481 funcstack->fs_prev = NULL;
482 first_funcstack = funcstack;
483}
484
485 static void
486remove_funcstack_from_list(funcstack_T *funcstack)
487{
488 if (funcstack->fs_prev == NULL)
489 first_funcstack = funcstack->fs_next;
490 else
491 funcstack->fs_prev->fs_next = funcstack->fs_next;
492 if (funcstack->fs_next != NULL)
493 funcstack->fs_next->fs_prev = funcstack->fs_prev;
494}
495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200497 * Used when returning from a function: Check if any closure is still
498 * referenced. If so then move the arguments and variables to a separate piece
499 * of stack to be used when the closure is called.
500 * When "free_arguments" is TRUE the arguments are to be freed.
501 * Returns FAIL when out of memory.
502 */
503 static int
504handle_closure_in_use(ectx_T *ectx, int free_arguments)
505{
506 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
507 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200508 int argcount;
509 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200510 int idx;
511 typval_T *tv;
512 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200513 garray_T *gap = &ectx->ec_funcrefs;
514 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200516 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200517 return OK; // function was freed
518 if (dfunc->df_has_closure == 0)
519 return OK; // no closures
520 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
521 closure_count = tv->vval.v_number;
522 if (closure_count == 0)
523 return OK; // no funcrefs created
524
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200525 argcount = ufunc_argcount(dfunc->df_ufunc);
526 top = ectx->ec_frame_idx - argcount;
527
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200528 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200529 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200531 partial_T *pt;
532 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200533
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200534 if (off < 0)
535 continue; // count is off or already done
536 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200537 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200538 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200540 int i;
541
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200542 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200543 // unreferenced on return.
544 for (i = 0; i < dfunc->df_varcount; ++i)
545 {
546 typval_T *stv = STACK_TV(ectx->ec_frame_idx
547 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200548 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200549 --refcount;
550 }
551 if (refcount > 1)
552 {
553 closure_in_use = TRUE;
554 break;
555 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200556 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200557 }
558
559 if (closure_in_use)
560 {
561 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
562 typval_T *stack;
563
564 // A closure is using the arguments and/or local variables.
565 // Move them to the called function.
566 if (funcstack == NULL)
567 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000568
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200569 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
570 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200571 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
572 funcstack->fs_ga.ga_data = stack;
573 if (stack == NULL)
574 {
575 vim_free(funcstack);
576 return FAIL;
577 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000578 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200579
580 // Move or copy the arguments.
581 for (idx = 0; idx < argcount; ++idx)
582 {
583 tv = STACK_TV(top + idx);
584 if (free_arguments)
585 {
586 *(stack + idx) = *tv;
587 tv->v_type = VAR_UNKNOWN;
588 }
589 else
590 copy_tv(tv, stack + idx);
591 }
592 // Move the local variables.
593 for (idx = 0; idx < dfunc->df_varcount; ++idx)
594 {
595 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200596
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200597 // A partial created for a local function, that is also used as a
598 // local variable, has a reference count for the variable, thus
599 // will never go down to zero. When all these refcounts are one
600 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000601 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200602 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
603 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200604 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200605
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200606 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200607 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
608 gap->ga_len - closure_count + i])
609 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200610 }
611
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200612 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200613 tv->v_type = VAR_UNKNOWN;
614 }
615
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200616 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200617 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200618 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
619 - closure_count + idx];
620 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200621 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200622 ++funcstack->fs_refcount;
623 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100624 pt->pt_outer.out_stack = &funcstack->fs_ga;
625 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 }
627 }
628 }
629
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200630 for (idx = 0; idx < closure_count; ++idx)
631 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
632 - closure_count + idx]);
633 gap->ga_len -= closure_count;
634 if (gap->ga_len == 0)
635 ga_clear(gap);
636
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 return OK;
638}
639
640/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200641 * Called when a partial is freed or its reference count goes down to one. The
642 * funcstack may be the only reference to the partials in the local variables.
643 * Go over all of them, the funcref and can be freed if all partials
644 * referencing the funcstack have a reference count of one.
645 */
646 void
647funcstack_check_refcount(funcstack_T *funcstack)
648{
649 int i;
650 garray_T *gap = &funcstack->fs_ga;
651 int done = 0;
652
653 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
654 return;
655 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
656 {
657 typval_T *tv = ((typval_T *)gap->ga_data) + i;
658
659 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
660 && tv->vval.v_partial->pt_funcstack == funcstack
661 && tv->vval.v_partial->pt_refcount == 1)
662 ++done;
663 }
664 if (done == funcstack->fs_min_refcount)
665 {
666 typval_T *stack = gap->ga_data;
667
668 // All partials referencing the funcstack have a reference count of
669 // one, thus the funcstack is no longer of use.
670 for (i = 0; i < gap->ga_len; ++i)
671 clear_tv(stack + i);
672 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000673 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200674 vim_free(funcstack);
675 }
676}
677
678/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000679 * For garbage collecting: set references in all variables referenced by
680 * all funcstacks.
681 */
682 int
683set_ref_in_funcstacks(int copyID)
684{
685 funcstack_T *funcstack;
686
687 for (funcstack = first_funcstack; funcstack != NULL;
688 funcstack = funcstack->fs_next)
689 {
690 typval_T *stack = funcstack->fs_ga.ga_data;
691 int i;
692
693 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
694 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
695 return TRUE; // abort
696 }
697 return FALSE;
698}
699
700/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 * Return from the current function.
702 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200703 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200704func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100707 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
709 + ectx->ec_dfunc_idx;
710 int argcount = ufunc_argcount(dfunc->df_ufunc);
711 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200712 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100713 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
714 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200715 funclocal_T *floc;
716#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100717 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
718 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719
Bram Moolenaar12d26532021-02-19 19:13:21 +0100720 if (do_profiling == PROF_YES)
721 {
722 ufunc_T *caller = prev_dfunc->df_ufunc;
723
724 if (dfunc->df_ufunc->uf_profiling
725 || (caller != NULL && caller->uf_profiling))
726 {
727 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
728 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
729 --profile_info_ga.ga_len;
730 }
731 }
732#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000733
734 // No check for uf_refcount being zero, cannot think of a way that would
735 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100736 --dfunc->df_ufunc->uf_calls;
737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200739 entry = estack_pop();
740 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200741 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 if (handle_closure_in_use(ectx, TRUE) == FAIL)
744 return FAIL;
745
746 // Clear the arguments.
747 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
748 clear_tv(STACK_TV(idx));
749
750 // Clear local variables and temp values, but not the return value.
751 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100752 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100754
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100755 // The return value should be on top of the stack. However, when aborting
756 // it may not be there and ec_frame_idx is the top of the stack.
757 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100758 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100759 ret_idx = 0;
760
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200761 if (ectx->ec_outer_ref != NULL)
762 {
763 if (ectx->ec_outer_ref->or_outer_allocated)
764 vim_free(ectx->ec_outer_ref->or_outer);
765 partial_unref(ectx->ec_outer_ref->or_partial);
766 vim_free(ectx->ec_outer_ref);
767 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100768
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100769 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100770 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100771 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
772 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200773 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
774 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200775 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100776 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100777 floc = (void *)STACK_TV(ectx->ec_frame_idx
778 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200779 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100780 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
781 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200782
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100783 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200784 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100785 else
786 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200787 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100788 vim_free(floc);
789 }
790
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100791 if (ret_idx > 0)
792 {
793 // Reset the stack to the position before the call, with a spot for the
794 // return value, moved there from above the frame.
795 ectx->ec_stack.ga_len = top + 1;
796 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
797 }
798 else
799 // Reset the stack to the position before the call.
800 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200801
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100802 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200803 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200804 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805}
806
807#undef STACK_TV
808
809/*
810 * Prepare arguments and rettv for calling a builtin or user function.
811 */
812 static int
813call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
814{
815 int idx;
816 typval_T *tv;
817
818 // Move arguments from bottom of the stack to argvars[] and add terminator.
819 for (idx = 0; idx < argcount; ++idx)
820 argvars[idx] = *STACK_TV_BOT(idx - argcount);
821 argvars[argcount].v_type = VAR_UNKNOWN;
822
823 // Result replaces the arguments on the stack.
824 if (argcount > 0)
825 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200826 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 return FAIL;
828 else
829 ++ectx->ec_stack.ga_len;
830
831 // Default return value is zero.
832 tv = STACK_TV_BOT(-1);
833 tv->v_type = VAR_NUMBER;
834 tv->vval.v_number = 0;
835
836 return OK;
837}
838
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200839// Ugly global to avoid passing the execution context around through many
840// layers.
841static ectx_T *current_ectx = NULL;
842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843/*
844 * Call a builtin function by index.
845 */
846 static int
847call_bfunc(int func_idx, int argcount, ectx_T *ectx)
848{
849 typval_T argvars[MAX_FUNC_ARGS];
850 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100851 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200852 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200853 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854
855 if (call_prepare(argcount, argvars, ectx) == FAIL)
856 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200857 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200859 // Call the builtin function. Set "current_ectx" so that when it
860 // recursively invokes call_def_function() a closure context can be set.
861 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200863 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200864 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865
866 // Clear the arguments.
867 for (idx = 0; idx < argcount; ++idx)
868 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200869
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100870 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200871 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 return OK;
873}
874
875/*
876 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100877 * If the function is compiled this will add a stack frame and set the
878 * instruction pointer at the start of the function.
879 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200880 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100881 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 */
883 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100884call_ufunc(
885 ufunc_T *ufunc,
886 partial_T *pt,
887 int argcount,
888 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200889 isn_T *iptr,
890 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891{
892 typval_T argvars[MAX_FUNC_ARGS];
893 funcexe_T funcexe;
894 int error;
895 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100896 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200897 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898
Bram Moolenaare99d4222021-06-13 14:01:26 +0200899 if (func_needs_compiling(ufunc, compile_type)
900 && compile_def_function(ufunc, FALSE, compile_type, NULL)
901 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200902 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200903 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100904 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100905 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100906 if (error != FCERR_UNKNOWN)
907 {
908 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000909 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100910 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000911 semsg(_(e_not_enough_arguments_for_function_str),
912 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100913 return FAIL;
914 }
915
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100916 // The function has been compiled, can call it quickly. For a function
917 // that was defined later: we can call it directly next time.
918 if (iptr != NULL)
919 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100920 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100921 iptr->isn_type = ISN_DCALL;
922 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
923 iptr->isn_arg.dfunc.cdf_argcount = argcount;
924 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200925 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
928 if (call_prepare(argcount, argvars, ectx) == FAIL)
929 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200930 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000931 funcexe.fe_evaluate = TRUE;
932 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
934 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000936 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937
938 // Clear the arguments.
939 for (idx = 0; idx < argcount; ++idx)
940 clear_tv(&argvars[idx]);
941
942 if (error != FCERR_NONE)
943 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000944 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 return FAIL;
946 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100947 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200948 // Error other than from calling the function itself.
949 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 return OK;
951}
952
953/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100954 * If command modifiers were applied restore them.
955 */
956 static void
957may_restore_cmdmod(funclocal_T *funclocal)
958{
959 if (funclocal->floc_restore_cmdmod)
960 {
961 cmdmod.cmod_filter_regmatch.regprog = NULL;
962 undo_cmdmod(&cmdmod);
963 cmdmod = funclocal->floc_save_cmdmod;
964 funclocal->floc_restore_cmdmod = FALSE;
965 }
966}
967
968/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200969 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
970 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200971 */
972 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200973vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200974{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200975 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200976}
977
978/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 * Execute a function by "name".
980 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100981 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 * Returns FAIL if not found without an error message.
983 */
984 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100985call_by_name(
986 char_u *name,
987 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100988 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200989 isn_T *iptr,
990 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991{
992 ufunc_T *ufunc;
993
994 if (builtin_function(name, -1))
995 {
996 int func_idx = find_internal_func(name);
997
998 if (func_idx < 0)
999 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001000 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 return FAIL;
1002 return call_bfunc(func_idx, argcount, ectx);
1003 }
1004
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001005 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +02001006
1007 if (ufunc == NULL)
1008 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001009 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001010
1011 if (script_autoload(name, TRUE))
1012 // loaded a package, search for the function again
1013 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001014
1015 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001016 return FAIL; // bail out if loading the script caused an error
1017 }
1018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001020 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001021 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001022 {
1023 int i;
1024 typval_T *argv = STACK_TV_BOT(0) - argcount;
1025
1026 // The function can change at runtime, check that the argument
1027 // types are correct.
1028 for (i = 0; i < argcount; ++i)
1029 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001030 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001031
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001032 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001033 type = ufunc->uf_arg_types[i];
1034 else if (ufunc->uf_va_type != NULL)
1035 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001036 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001037 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001038 return FAIL;
1039 }
1040 }
1041
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001042 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044
1045 return FAIL;
1046}
1047
1048 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001049call_partial(
1050 typval_T *tv,
1051 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001052 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001054 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001055 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001057 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001058 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059
1060 if (tv->v_type == VAR_PARTIAL)
1061 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001062 partial_T *pt = tv->vval.v_partial;
1063 int i;
1064
1065 if (pt->pt_argc > 0)
1066 {
1067 // Make space for arguments from the partial, shift the "argcount"
1068 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001069 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001070 return FAIL;
1071 for (i = 1; i <= argcount; ++i)
1072 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1073 ectx->ec_stack.ga_len += pt->pt_argc;
1074 argcount += pt->pt_argc;
1075
1076 // copy the arguments from the partial onto the stack
1077 for (i = 0; i < pt->pt_argc; ++i)
1078 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1079 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001080 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081
1082 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001083 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 name = pt->pt_name;
1086 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001087 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001089 if (name != NULL)
1090 {
1091 char_u fname_buf[FLEN_FIXED + 1];
1092 char_u *tofree = NULL;
1093 int error = FCERR_NONE;
1094 char_u *fname;
1095
1096 // May need to translate <SNR>123_ to K_SNR.
1097 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1098 if (error != FCERR_NONE)
1099 res = FAIL;
1100 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001101 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001102 vim_free(tofree);
1103 }
1104
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001105 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 {
1107 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001108 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001109 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 return FAIL;
1111 }
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001116 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1117 * TRUE.
1118 */
1119 static int
1120error_if_locked(int lock, char *error)
1121{
1122 if (lock & (VAR_LOCKED | VAR_FIXED))
1123 {
1124 emsg(_(error));
1125 return TRUE;
1126 }
1127 return FALSE;
1128}
1129
1130/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001131 * Give an error if "tv" is not a number and return FAIL.
1132 */
1133 static int
1134check_for_number(typval_T *tv)
1135{
1136 if (tv->v_type != VAR_NUMBER)
1137 {
1138 semsg(_(e_expected_str_but_got_str),
1139 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1140 return FAIL;
1141 }
1142 return OK;
1143}
1144
1145/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001146 * Store "tv" in variable "name".
1147 * This is for s: and g: variables.
1148 */
1149 static void
1150store_var(char_u *name, typval_T *tv)
1151{
1152 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001153 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001154
Bram Moolenaard877a572021-04-01 19:42:48 +02001155 if (tv->v_lock)
1156 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001157 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001158 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001159 restore_funccal();
1160}
1161
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001162/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001163 * Convert "tv" to a string.
1164 * Return FAIL if not allowed.
1165 */
1166 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001167do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001168{
1169 if (tv->v_type != VAR_STRING)
1170 {
1171 char_u *str;
1172
1173 if (is_2string_any)
1174 {
1175 switch (tv->v_type)
1176 {
1177 case VAR_SPECIAL:
1178 case VAR_BOOL:
1179 case VAR_NUMBER:
1180 case VAR_FLOAT:
1181 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001182
1183 case VAR_LIST:
1184 if (tolerant)
1185 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001186 char_u *s, *e, *p;
1187 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001188
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001189 ga_init2(&ga, sizeof(char_u *), 1);
1190
1191 // Convert to NL separated items, then
1192 // escape the items and replace the NL with
1193 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001194 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001195 if (str == NULL)
1196 return FAIL;
1197 s = str;
1198 while ((e = vim_strchr(s, '\n')) != NULL)
1199 {
1200 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001201 p = vim_strsave_fnameescape(s,
1202 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001203 if (p != NULL)
1204 {
1205 ga_concat(&ga, p);
1206 ga_concat(&ga, (char_u *)" ");
1207 vim_free(p);
1208 }
1209 s = e + 1;
1210 }
1211 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001212 clear_tv(tv);
1213 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001214 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001215 return OK;
1216 }
1217 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001218 default: to_string_error(tv->v_type);
1219 return FAIL;
1220 }
1221 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001222 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001223 clear_tv(tv);
1224 tv->v_type = VAR_STRING;
1225 tv->vval.v_string = str;
1226 }
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001231 * When the value of "sv" is a null list of dict, allocate it.
1232 */
1233 static void
1234allocate_if_null(typval_T *tv)
1235{
1236 switch (tv->v_type)
1237 {
1238 case VAR_LIST:
1239 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001240 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001241 break;
1242 case VAR_DICT:
1243 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001244 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001245 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001246 case VAR_BLOB:
1247 if (tv->vval.v_blob == NULL)
1248 (void)rettv_blob_alloc(tv);
1249 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001250 default:
1251 break;
1252 }
1253}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001254
Bram Moolenaare7525c52021-01-09 13:20:37 +01001255/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001256 * Return the character "str[index]" where "index" is the character index,
1257 * including composing characters.
1258 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001259 */
1260 char_u *
1261char_from_string(char_u *str, varnumber_T index)
1262{
1263 size_t nbyte = 0;
1264 varnumber_T nchar = index;
1265 size_t slen;
1266
1267 if (str == NULL)
1268 return NULL;
1269 slen = STRLEN(str);
1270
Bram Moolenaarff871402021-03-26 13:34:05 +01001271 // Do the same as for a list: a negative index counts from the end.
1272 // Optimization to check the first byte to be below 0x80 (and no composing
1273 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001274 if (index < 0)
1275 {
1276 int clen = 0;
1277
1278 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001279 {
1280 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1281 ++nbyte;
1282 else if (enc_utf8)
1283 nbyte += utfc_ptr2len(str + nbyte);
1284 else
1285 nbyte += mb_ptr2len(str + nbyte);
1286 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001287 nchar = clen + index;
1288 if (nchar < 0)
1289 // unlike list: index out of range results in empty string
1290 return NULL;
1291 }
1292
1293 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001294 {
1295 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1296 ++nbyte;
1297 else if (enc_utf8)
1298 nbyte += utfc_ptr2len(str + nbyte);
1299 else
1300 nbyte += mb_ptr2len(str + nbyte);
1301 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001302 if (nbyte >= slen)
1303 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001304 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001305}
1306
1307/*
1308 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001309 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001310 * If going over the end return "str_len".
1311 * If "idx" is negative count from the end, -1 is the last character.
1312 * When going over the start return -1.
1313 */
1314 static long
1315char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1316{
1317 varnumber_T nchar = idx;
1318 size_t nbyte = 0;
1319
1320 if (nchar >= 0)
1321 {
1322 while (nchar > 0 && nbyte < str_len)
1323 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001324 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001325 --nchar;
1326 }
1327 }
1328 else
1329 {
1330 nbyte = str_len;
1331 while (nchar < 0 && nbyte > 0)
1332 {
1333 --nbyte;
1334 nbyte -= mb_head_off(str, str + nbyte);
1335 ++nchar;
1336 }
1337 if (nchar < 0)
1338 return -1;
1339 }
1340 return (long)nbyte;
1341}
1342
1343/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001344 * Return the slice "str[first : last]" using character indexes. Composing
1345 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001346 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001347 * Return NULL when the result is empty.
1348 */
1349 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001350string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001351{
1352 long start_byte, end_byte;
1353 size_t slen;
1354
1355 if (str == NULL)
1356 return NULL;
1357 slen = STRLEN(str);
1358 start_byte = char_idx2byte(str, slen, first);
1359 if (start_byte < 0)
1360 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001361 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001362 end_byte = (long)slen;
1363 else
1364 {
1365 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001366 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001367 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001368 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001369 }
1370
1371 if (start_byte >= (long)slen || end_byte <= start_byte)
1372 return NULL;
1373 return vim_strnsave(str + start_byte, end_byte - start_byte);
1374}
1375
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001376/*
1377 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1378 * When "dfunc_idx" is negative don't give an error.
1379 * Returns NULL for an error.
1380 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001381 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001382get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001383{
1384 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001385 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1386 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001387 svar_T *sv;
1388
1389 if (sref->sref_seq != si->sn_script_seq)
1390 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001391 // The script was reloaded after the function was compiled, the
1392 // script_idx may not be valid.
1393 if (dfunc != NULL)
1394 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1395 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001396 return NULL;
1397 }
1398 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001399 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001400 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001401 if (dfunc != NULL)
1402 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001403 return NULL;
1404 }
1405 return sv;
1406}
1407
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001408/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001409 * Function passed to do_cmdline() for splitting a script joined by NL
1410 * characters.
1411 */
1412 static char_u *
1413get_split_sourceline(
1414 int c UNUSED,
1415 void *cookie,
1416 int indent UNUSED,
1417 getline_opt_T options UNUSED)
1418{
1419 source_cookie_T *sp = (source_cookie_T *)cookie;
1420 char_u *p;
1421 char_u *line;
1422
1423 if (*sp->nextline == NUL)
1424 return NULL;
1425 p = vim_strchr(sp->nextline, '\n');
1426 if (p == NULL)
1427 {
1428 line = vim_strsave(sp->nextline);
1429 sp->nextline += STRLEN(sp->nextline);
1430 }
1431 else
1432 {
1433 line = vim_strnsave(sp->nextline, p - sp->nextline);
1434 sp->nextline = p + 1;
1435 }
1436 return line;
1437}
1438
1439/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 * Execute a function by "name".
1441 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001442 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 */
1444 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001445call_eval_func(
1446 char_u *name,
1447 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001448 ectx_T *ectx,
1449 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450{
Bram Moolenaared677f52020-08-12 16:38:10 +02001451 int called_emsg_before = called_emsg;
1452 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001454 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001455 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001457 dictitem_T *v;
1458
1459 v = find_var(name, NULL, FALSE);
1460 if (v == NULL)
1461 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001462 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001463 return FAIL;
1464 }
1465 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
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 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001470 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001472 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473}
1474
1475/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001476 * When a function reference is used, fill a partial with the information
1477 * needed, especially when it is used as a closure.
1478 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001479 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001480fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1481{
1482 pt->pt_func = ufunc;
1483 pt->pt_refcount = 1;
1484
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001485 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001486 {
1487 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1488 + ectx->ec_dfunc_idx;
1489
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001490 // The closure may need to find arguments and local variables in the
1491 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001492 pt->pt_outer.out_stack = &ectx->ec_stack;
1493 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001494 if (ectx->ec_outer_ref != NULL)
1495 {
1496 // The current context already has a context, link to that one.
1497 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1498 if (ectx->ec_outer_ref->or_partial != NULL)
1499 {
1500 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1501 ++pt->pt_outer.out_up_partial->pt_refcount;
1502 }
1503 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001504
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001505 // If this function returns and the closure is still being used, we
1506 // need to make a copy of the context (arguments and local variables).
1507 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001508 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001509 {
1510 vim_free(pt);
1511 return FAIL;
1512 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001513 // Extra variable keeps the count of closures created in the current
1514 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001515 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1516 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1517
1518 ((partial_T **)ectx->ec_funcrefs.ga_data)
1519 [ectx->ec_funcrefs.ga_len] = pt;
1520 ++pt->pt_refcount;
1521 ++ectx->ec_funcrefs.ga_len;
1522 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001523 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001524 return OK;
1525}
1526
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001527/*
1528 * Execute iptr->isn_arg.string as an Ex command.
1529 */
1530 static int
1531exec_command(isn_T *iptr)
1532{
1533 source_cookie_T cookie;
1534
1535 SOURCING_LNUM = iptr->isn_lnum;
1536 // Pass getsourceline to get an error for a missing ":end"
1537 // command.
1538 CLEAR_FIELD(cookie);
1539 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1540 if (do_cmdline(iptr->isn_arg.string,
1541 getsourceline, &cookie,
1542 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1543 || did_emsg)
1544 return FAIL;
1545 return OK;
1546}
1547
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001548// used for v_instr of typval of VAR_INSTR
1549struct instr_S {
1550 ectx_T *instr_ectx;
1551 isn_T *instr_instr;
1552};
1553
Bram Moolenaar4c137212021-04-19 16:48:48 +02001554// used for substitute_instr
1555typedef struct subs_expr_S {
1556 ectx_T *subs_ectx;
1557 isn_T *subs_instr;
1558 int subs_status;
1559} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560
1561// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001562#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563
1564// Get pointer to item at the bottom of the stack, -1 is the bottom.
1565#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001566#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 +01001567
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001568// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001569#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 +01001570
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001571// Set when calling do_debug().
1572static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001573static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001574
1575/*
1576 * When debugging lookup "name" and return the typeval.
1577 * When not found return NULL.
1578 */
1579 typval_T *
1580lookup_debug_var(char_u *name)
1581{
1582 int idx;
1583 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001584 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001585 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001586 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001587
1588 if (ectx == NULL)
1589 return NULL;
1590 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1591
1592 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001593 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001594 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001595 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001596 return STACK_TV_VAR(idx);
1597 }
1598
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001599 // Go through argument names.
1600 ufunc = dfunc->df_ufunc;
1601 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1602 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1603 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1604 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1605 - varargs_off + idx);
1606 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1607 return STACK_TV(ectx->ec_frame_idx - 1);
1608
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001609 return NULL;
1610}
1611
Bram Moolenaar26a44842021-09-02 18:49:06 +02001612/*
1613 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1614 * breakpoint was set in that function or when there is any expression.
1615 */
1616 int
1617may_break_in_function(ufunc_T *ufunc)
1618{
1619 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1620}
1621
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001622 static void
1623handle_debug(isn_T *iptr, ectx_T *ectx)
1624{
1625 char_u *line;
1626 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1627 + ectx->ec_dfunc_idx)->df_ufunc;
1628 isn_T *ni;
1629 int end_lnum = iptr->isn_lnum;
1630 garray_T ga;
1631 int lnum;
1632
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001633 if (ex_nesting_level > debug_break_level)
1634 {
1635 linenr_T breakpoint;
1636
Bram Moolenaar26a44842021-09-02 18:49:06 +02001637 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001638 return;
1639
1640 // check for the next breakpoint if needed
1641 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001642 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001643 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1644 return;
1645 }
1646
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001647 SOURCING_LNUM = iptr->isn_lnum;
1648 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001649 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001650
1651 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1652 if (ni->isn_type == ISN_DEBUG
1653 || ni->isn_type == ISN_RETURN
1654 || ni->isn_type == ISN_RETURN_VOID)
1655 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001656 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001657 break;
1658 }
1659
1660 if (end_lnum > iptr->isn_lnum)
1661 {
1662 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001663 for (lnum = iptr->isn_lnum; lnum < end_lnum
1664 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001665 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001666 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001667
Bram Moolenaar303215d2021-07-07 20:10:43 +02001668 if (p == NULL)
1669 continue; // left over from continuation line
1670 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001671 if (*p == '#')
1672 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001673 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001674 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1675 if (STRNCMP(p, "def ", 4) == 0)
1676 break;
1677 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001678 line = ga_concat_strings(&ga, " ");
1679 vim_free(ga.ga_data);
1680 }
1681 else
1682 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001683
Dominique Pelle6e969552021-06-17 13:53:41 +02001684 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001685 debug_context = NULL;
1686
1687 if (end_lnum > iptr->isn_lnum)
1688 vim_free(line);
1689}
1690
Bram Moolenaar4c137212021-04-19 16:48:48 +02001691/*
1692 * Execute instructions in execution context "ectx".
1693 * Return OK or FAIL;
1694 */
1695 static int
1696exec_instructions(ectx_T *ectx)
1697{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001698 int ret = FAIL;
1699 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001700 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001701
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001702 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001703 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001704
Bram Moolenaarff652882021-05-16 15:24:49 +02001705 // Only catch exceptions in this instruction list.
1706 ectx->ec_trylevel_at_start = trylevel;
1707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 for (;;)
1709 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001710 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001712 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001713
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001714 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001715 {
1716 line_breakcheck();
1717 breakcheck_count = 0;
1718 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001719 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001720 {
1721 // Turn CTRL-C into an exception.
1722 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001723 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001724 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001725 did_throw = TRUE;
1726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001728 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001729 {
1730 // Turn an error message into an exception.
1731 did_emsg = FALSE;
1732 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001733 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001734 did_throw = TRUE;
1735 *msg_list = NULL;
1736 }
1737
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001738 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001740 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001741 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001742 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
1744 // An exception jumps to the first catch, finally, or returns from
1745 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001746 while (index > 0)
1747 {
1748 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001749 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001750 break;
1751 // In the catch and finally block of this try we have to go up
1752 // one level.
1753 --index;
1754 trycmd = NULL;
1755 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001756 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001758 if (trycmd->tcd_in_catch)
1759 {
1760 // exception inside ":catch", jump to ":finally" once
1761 ectx->ec_iidx = trycmd->tcd_finally_idx;
1762 trycmd->tcd_finally_idx = 0;
1763 }
1764 else
1765 // jump to first ":catch"
1766 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001767 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001768 did_throw = FALSE; // don't come back here until :endtry
1769 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 }
1771 else
1772 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001773 // Not inside try or need to return from current functions.
1774 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001775 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001776 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001777 tv = STACK_TV_BOT(0);
1778 tv->v_type = VAR_NUMBER;
1779 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001780 ++ectx->ec_stack.ga_len;
1781 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001783 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001784 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001785 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001786 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 goto done;
1788 }
1789
Bram Moolenaar4c137212021-04-19 16:48:48 +02001790 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001791 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 }
1793 continue;
1794 }
1795
Bram Moolenaar4c137212021-04-19 16:48:48 +02001796 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 switch (iptr->isn_type)
1798 {
1799 // execute Ex command line
1800 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001801 if (exec_command(iptr) == FAIL)
1802 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 break;
1804
Bram Moolenaar20677332021-06-06 17:02:53 +02001805 // execute Ex command line split at NL characters.
1806 case ISN_EXEC_SPLIT:
1807 {
1808 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001809 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001810
1811 SOURCING_LNUM = iptr->isn_lnum;
1812 CLEAR_FIELD(cookie);
1813 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1814 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001815 line = get_split_sourceline(0, &cookie, 0, 0);
1816 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001817 get_split_sourceline, &cookie,
1818 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1819 == FAIL
1820 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001821 {
1822 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001823 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001824 }
1825 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001826 }
1827 break;
1828
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001829 // execute Ex command line that is only a range
1830 case ISN_EXECRANGE:
1831 {
1832 exarg_T ea;
1833 char *error = NULL;
1834
1835 CLEAR_FIELD(ea);
1836 ea.cmdidx = CMD_SIZE;
1837 ea.addr_type = ADDR_LINES;
1838 ea.cmd = iptr->isn_arg.string;
1839 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00001840 if (ea.cmd == NULL)
1841 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001842 if (error == NULL)
1843 error = ex_range_without_command(&ea);
1844 if (error != NULL)
1845 {
1846 SOURCING_LNUM = iptr->isn_lnum;
1847 emsg(error);
1848 goto on_error;
1849 }
1850 }
1851 break;
1852
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001853 // Evaluate an expression with legacy syntax, push it onto the
1854 // stack.
1855 case ISN_LEGACY_EVAL:
1856 {
1857 char_u *arg = iptr->isn_arg.string;
1858 int res;
1859 int save_flags = cmdmod.cmod_flags;
1860
Bram Moolenaar35578162021-08-02 19:10:38 +02001861 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001862 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001863 tv = STACK_TV_BOT(0);
1864 init_tv(tv);
1865 cmdmod.cmod_flags |= CMOD_LEGACY;
1866 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1867 cmdmod.cmod_flags = save_flags;
1868 if (res == FAIL)
1869 goto on_error;
1870 ++ectx->ec_stack.ga_len;
1871 }
1872 break;
1873
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001874 // push typeval VAR_INSTR with instructions to be executed
1875 case ISN_INSTR:
1876 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001877 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001878 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001879 tv = STACK_TV_BOT(0);
1880 tv->vval.v_instr = ALLOC_ONE(instr_T);
1881 if (tv->vval.v_instr == NULL)
1882 goto on_error;
1883 ++ectx->ec_stack.ga_len;
1884
1885 tv->v_type = VAR_INSTR;
1886 tv->vval.v_instr->instr_ectx = ectx;
1887 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1888 }
1889 break;
1890
Bram Moolenaar4c137212021-04-19 16:48:48 +02001891 // execute :substitute with an expression
1892 case ISN_SUBSTITUTE:
1893 {
1894 subs_T *subs = &iptr->isn_arg.subs;
1895 source_cookie_T cookie;
1896 struct subs_expr_S *save_instr = substitute_instr;
1897 struct subs_expr_S subs_instr;
1898 int res;
1899
1900 subs_instr.subs_ectx = ectx;
1901 subs_instr.subs_instr = subs->subs_instr;
1902 subs_instr.subs_status = OK;
1903 substitute_instr = &subs_instr;
1904
1905 SOURCING_LNUM = iptr->isn_lnum;
1906 // This is very much like ISN_EXEC
1907 CLEAR_FIELD(cookie);
1908 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1909 res = do_cmdline(subs->subs_cmd,
1910 getsourceline, &cookie,
1911 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1912 substitute_instr = save_instr;
1913
1914 if (res == FAIL || did_emsg
1915 || subs_instr.subs_status == FAIL)
1916 goto on_error;
1917 }
1918 break;
1919
1920 case ISN_FINISH:
1921 goto done;
1922
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001923 case ISN_REDIRSTART:
1924 // create a dummy entry for var_redir_str()
1925 if (alloc_redir_lval() == FAIL)
1926 goto on_error;
1927
1928 // The output is stored in growarray "redir_ga" until
1929 // redirection ends.
1930 init_redir_ga();
1931 redir_vname = 1;
1932 break;
1933
1934 case ISN_REDIREND:
1935 {
1936 char_u *res = get_clear_redir_ga();
1937
1938 // End redirection, put redirected text on the stack.
1939 clear_redir_lval();
1940 redir_vname = 0;
1941
Bram Moolenaar35578162021-08-02 19:10:38 +02001942 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001943 {
1944 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001945 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001946 }
1947 tv = STACK_TV_BOT(0);
1948 tv->v_type = VAR_STRING;
1949 tv->vval.v_string = res;
1950 ++ectx->ec_stack.ga_len;
1951 }
1952 break;
1953
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001954 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001955#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001956 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1957 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001958#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001959 break;
1960
1961 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001962#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001963 {
1964 exarg_T ea;
1965 int res;
1966
1967 CLEAR_FIELD(ea);
1968 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1969 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1970 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1971 --ectx->ec_stack.ga_len;
1972 tv = STACK_TV_BOT(0);
1973 res = cexpr_core(&ea, tv);
1974 clear_tv(tv);
1975 if (res == FAIL)
1976 goto on_error;
1977 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001978#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001979 break;
1980
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001981 // execute Ex command from pieces on the stack
1982 case ISN_EXECCONCAT:
1983 {
1984 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001985 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001986 int pass;
1987 int i;
1988 char_u *cmd = NULL;
1989 char_u *str;
1990
1991 for (pass = 1; pass <= 2; ++pass)
1992 {
1993 for (i = 0; i < count; ++i)
1994 {
1995 tv = STACK_TV_BOT(i - count);
1996 str = tv->vval.v_string;
1997 if (str != NULL && *str != NUL)
1998 {
1999 if (pass == 2)
2000 STRCPY(cmd + len, str);
2001 len += STRLEN(str);
2002 }
2003 if (pass == 2)
2004 clear_tv(tv);
2005 }
2006 if (pass == 1)
2007 {
2008 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002009 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002010 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002011 len = 0;
2012 }
2013 }
2014
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002015 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002016 do_cmdline_cmd(cmd);
2017 vim_free(cmd);
2018 }
2019 break;
2020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 // execute :echo {string} ...
2022 case ISN_ECHO:
2023 {
2024 int count = iptr->isn_arg.echo.echo_count;
2025 int atstart = TRUE;
2026 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002027 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028
2029 for (idx = 0; idx < count; ++idx)
2030 {
2031 tv = STACK_TV_BOT(idx - count);
2032 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2033 &atstart, &needclr);
2034 clear_tv(tv);
2035 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002036 if (needclr)
2037 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002038 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 }
2040 break;
2041
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002042 // :execute {string} ...
2043 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002044 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002045 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002046 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002047 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002048 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002049 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002050 {
2051 int count = iptr->isn_arg.number;
2052 garray_T ga;
2053 char_u buf[NUMBUFLEN];
2054 char_u *p;
2055 int len;
2056 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002057 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002058
2059 ga_init2(&ga, 1, 80);
2060 for (idx = 0; idx < count; ++idx)
2061 {
2062 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002063 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002064 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002065 if (tv->v_type == VAR_CHANNEL
2066 || tv->v_type == VAR_JOB)
2067 {
2068 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002069 semsg(_(e_using_invalid_value_as_string_str),
2070 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002071 break;
2072 }
2073 else
2074 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002075 }
2076 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002077 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002078
2079 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002080 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002081 failed = TRUE;
2082 else
2083 {
2084 if (ga.ga_len > 0)
2085 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2086 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2087 ga.ga_len += len;
2088 }
2089 clear_tv(tv);
2090 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002091 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002092 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002093 {
2094 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002095 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002096 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002097
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002098 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002099 {
2100 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002101 {
2102 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002103 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002104 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002105 {
2106 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002107 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002108 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002109 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002110 else
2111 {
2112 msg_sb_eol();
2113 if (iptr->isn_type == ISN_ECHOMSG)
2114 {
2115 msg_attr(ga.ga_data, echo_attr);
2116 out_flush();
2117 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002118 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2119 {
2120 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2121 TRUE);
2122 ui_write((char_u *)"\r\n", 2, TRUE);
2123 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002124 else
2125 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002126 SOURCING_LNUM = iptr->isn_lnum;
2127 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002128 }
2129 }
2130 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002131 ga_clear(&ga);
2132 }
2133 break;
2134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 // load local variable or argument
2136 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002137 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002138 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002140 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 break;
2142
2143 // load v: variable
2144 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002145 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002146 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002148 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 break;
2150
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002151 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152 case ISN_LOADSCRIPT:
2153 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002154 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 svar_T *sv;
2156
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002157 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002158 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002159 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002160 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002161 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002162 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002164 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 }
2166 break;
2167
2168 // load s: variable in old script
2169 case ISN_LOADS:
2170 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002171 hashtab_T *ht = &SCRIPT_VARS(
2172 iptr->isn_arg.loadstore.ls_sid);
2173 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 if (di == NULL)
2177 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002178 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002179 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002180 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 }
2182 else
2183 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002184 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002185 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002187 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 }
2189 }
2190 break;
2191
Bram Moolenaard3aac292020-04-19 14:32:17 +02002192 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002194 case ISN_LOADB:
2195 case ISN_LOADW:
2196 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002198 dictitem_T *di = NULL;
2199 hashtab_T *ht = NULL;
2200 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002201
Bram Moolenaard3aac292020-04-19 14:32:17 +02002202 switch (iptr->isn_type)
2203 {
2204 case ISN_LOADG:
2205 ht = get_globvar_ht();
2206 namespace = 'g';
2207 break;
2208 case ISN_LOADB:
2209 ht = &curbuf->b_vars->dv_hashtab;
2210 namespace = 'b';
2211 break;
2212 case ISN_LOADW:
2213 ht = &curwin->w_vars->dv_hashtab;
2214 namespace = 'w';
2215 break;
2216 case ISN_LOADT:
2217 ht = &curtab->tp_vars->dv_hashtab;
2218 namespace = 't';
2219 break;
2220 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002221 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002222 }
2223 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 if (di == NULL)
2226 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002227 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002228 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002229 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002230 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 }
2232 else
2233 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002234 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002235 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002237 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 }
2239 }
2240 break;
2241
Bram Moolenaar03290b82020-12-19 16:30:44 +01002242 // load autoload variable
2243 case ISN_LOADAUTO:
2244 {
2245 char_u *name = iptr->isn_arg.string;
2246
Bram Moolenaar35578162021-08-02 19:10:38 +02002247 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002248 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002249 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002250 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002251 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002252 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002253 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002254 }
2255 break;
2256
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002257 // load g:/b:/w:/t: namespace
2258 case ISN_LOADGDICT:
2259 case ISN_LOADBDICT:
2260 case ISN_LOADWDICT:
2261 case ISN_LOADTDICT:
2262 {
2263 dict_T *d = NULL;
2264
2265 switch (iptr->isn_type)
2266 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002267 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2268 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2269 case ISN_LOADWDICT: d = curwin->w_vars; break;
2270 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002271 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002272 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002273 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002274 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002275 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002276 tv = STACK_TV_BOT(0);
2277 tv->v_type = VAR_DICT;
2278 tv->v_lock = 0;
2279 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002280 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002281 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002282 }
2283 break;
2284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 // load &option
2286 case ISN_LOADOPT:
2287 {
2288 typval_T optval;
2289 char_u *name = iptr->isn_arg.string;
2290
Bram Moolenaara8c17702020-04-01 21:17:24 +02002291 // This is not expected to fail, name is checked during
2292 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002293 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002294 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002295 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002296 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002298 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 }
2300 break;
2301
2302 // load $ENV
2303 case ISN_LOADENV:
2304 {
2305 typval_T optval;
2306 char_u *name = iptr->isn_arg.string;
2307
Bram Moolenaar35578162021-08-02 19:10:38 +02002308 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002309 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002310 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002311 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002313 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 }
2315 break;
2316
2317 // load @register
2318 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002319 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002320 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 tv = STACK_TV_BOT(0);
2322 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002323 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002324 // This may result in NULL, which should be equivalent to an
2325 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 tv->vval.v_string = get_reg_contents(
2327 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002328 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 break;
2330
2331 // store local variable
2332 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002333 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 tv = STACK_TV_VAR(iptr->isn_arg.number);
2335 clear_tv(tv);
2336 *tv = *STACK_TV_BOT(0);
2337 break;
2338
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002339 // store s: variable in old script
2340 case ISN_STORES:
2341 {
2342 hashtab_T *ht = &SCRIPT_VARS(
2343 iptr->isn_arg.loadstore.ls_sid);
2344 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002345 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002346
Bram Moolenaar4c137212021-04-19 16:48:48 +02002347 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002348 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002349 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002350 else
2351 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002352 SOURCING_LNUM = iptr->isn_lnum;
2353 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002354 {
2355 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002356 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002357 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002358 clear_tv(&di->di_tv);
2359 di->di_tv = *STACK_TV_BOT(0);
2360 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002361 }
2362 break;
2363
2364 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 case ISN_STORESCRIPT:
2366 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002367 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2368 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002370 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002371 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002372 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002373 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002374
2375 // "const" and "final" are checked at compile time, locking
2376 // the value needs to be checked here.
2377 SOURCING_LNUM = iptr->isn_lnum;
2378 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002379 {
2380 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002381 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002382 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 clear_tv(sv->sv_tv);
2385 *sv->sv_tv = *STACK_TV_BOT(0);
2386 }
2387 break;
2388
2389 // store option
2390 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002391 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002393 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
2394 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 long n = 0;
2396 char_u *s = NULL;
2397 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002398 char_u numbuf[NUMBUFLEN];
2399 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400
Bram Moolenaar4c137212021-04-19 16:48:48 +02002401 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 tv = STACK_TV_BOT(0);
2403 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002404 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002406 if (s == NULL)
2407 s = (char_u *)"";
2408 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002409 else if (iptr->isn_type == ISN_STOREFUNCOPT)
2410 {
2411 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002412 // If the option can be set to a function reference or
2413 // a lambda and the passed value is a function
2414 // reference, then convert it to the name (string) of
2415 // the function reference.
2416 s = tv2string(tv, &tofree, numbuf, 0);
2417 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002418 {
2419 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002420 goto on_error;
2421 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002424 // must be VAR_NUMBER, CHECKTYPE makes sure
2425 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002426 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002427 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002428 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 if (msg != NULL)
2430 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002431 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002433 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 }
2436 break;
2437
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002438 // store $ENV
2439 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002440 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002441 tv = STACK_TV_BOT(0);
2442 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2443 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002444 break;
2445
2446 // store @r
2447 case ISN_STOREREG:
2448 {
2449 int reg = iptr->isn_arg.number;
2450
Bram Moolenaar4c137212021-04-19 16:48:48 +02002451 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002452 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002453 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002454 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002455 }
2456 break;
2457
2458 // store v: variable
2459 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002460 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002461 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2462 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002463 // should not happen, type is checked when compiling
2464 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002465 break;
2466
Bram Moolenaard3aac292020-04-19 14:32:17 +02002467 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002469 case ISN_STOREB:
2470 case ISN_STOREW:
2471 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002473 dictitem_T *di;
2474 hashtab_T *ht;
2475 char_u *name = iptr->isn_arg.string + 2;
2476
Bram Moolenaard3aac292020-04-19 14:32:17 +02002477 switch (iptr->isn_type)
2478 {
2479 case ISN_STOREG:
2480 ht = get_globvar_ht();
2481 break;
2482 case ISN_STOREB:
2483 ht = &curbuf->b_vars->dv_hashtab;
2484 break;
2485 case ISN_STOREW:
2486 ht = &curwin->w_vars->dv_hashtab;
2487 break;
2488 case ISN_STORET:
2489 ht = &curtab->tp_vars->dv_hashtab;
2490 break;
2491 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002492 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494
Bram Moolenaar4c137212021-04-19 16:48:48 +02002495 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002496 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002498 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 else
2500 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002501 SOURCING_LNUM = iptr->isn_lnum;
2502 if (var_check_permission(di, name) == FAIL)
2503 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 clear_tv(&di->di_tv);
2505 di->di_tv = *STACK_TV_BOT(0);
2506 }
2507 }
2508 break;
2509
Bram Moolenaar03290b82020-12-19 16:30:44 +01002510 // store an autoload variable
2511 case ISN_STOREAUTO:
2512 SOURCING_LNUM = iptr->isn_lnum;
2513 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2514 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002515 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002516 break;
2517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 // store number in local variable
2519 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002520 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 clear_tv(tv);
2522 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002523 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 break;
2525
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002526 // store value in list or dict variable
2527 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002528 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002529 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002530 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002531 typval_T *tv_dest = STACK_TV_BOT(-1);
2532 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002533
Bram Moolenaar752fc692021-01-04 21:57:11 +01002534 // Stack contains:
2535 // -3 value to be stored
2536 // -2 index
2537 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002538 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002539 SOURCING_LNUM = iptr->isn_lnum;
2540 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002541 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002542 dest_type = tv_dest->v_type;
2543 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002544 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002545 else if (dest_type == VAR_LIST
2546 && tv_idx->v_type != VAR_NUMBER)
2547 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002548 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002549 status = FAIL;
2550 }
2551 }
2552 else if (dest_type != tv_dest->v_type)
2553 {
2554 // just in case, should be OK
2555 semsg(_(e_expected_str_but_got_str),
2556 vartype_name(dest_type),
2557 vartype_name(tv_dest->v_type));
2558 status = FAIL;
2559 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002560
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002561 if (status == OK && dest_type == VAR_LIST)
2562 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002563 long lidx = (long)tv_idx->vval.v_number;
2564 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002565
2566 if (list == NULL)
2567 {
2568 emsg(_(e_list_not_set));
2569 goto on_error;
2570 }
2571 if (lidx < 0 && list->lv_len + lidx >= 0)
2572 // negative index is relative to the end
2573 lidx = list->lv_len + lidx;
2574 if (lidx < 0 || lidx > list->lv_len)
2575 {
2576 semsg(_(e_listidx), lidx);
2577 goto on_error;
2578 }
2579 if (lidx < list->lv_len)
2580 {
2581 listitem_T *li = list_find(list, lidx);
2582
2583 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002584 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002585 goto on_error;
2586 // overwrite existing list item
2587 clear_tv(&li->li_tv);
2588 li->li_tv = *tv;
2589 }
2590 else
2591 {
2592 if (error_if_locked(list->lv_lock,
2593 e_cannot_change_list))
2594 goto on_error;
2595 // append to list, only fails when out of memory
2596 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002597 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002598 clear_tv(tv);
2599 }
2600 }
2601 else if (status == OK && dest_type == VAR_DICT)
2602 {
2603 char_u *key = tv_idx->vval.v_string;
2604 dict_T *dict = tv_dest->vval.v_dict;
2605 dictitem_T *di;
2606
2607 SOURCING_LNUM = iptr->isn_lnum;
2608 if (dict == NULL)
2609 {
2610 emsg(_(e_dictionary_not_set));
2611 goto on_error;
2612 }
2613 if (key == NULL)
2614 key = (char_u *)"";
2615 di = dict_find(dict, key, -1);
2616 if (di != NULL)
2617 {
2618 if (error_if_locked(di->di_tv.v_lock,
2619 e_cannot_change_dict_item))
2620 goto on_error;
2621 // overwrite existing value
2622 clear_tv(&di->di_tv);
2623 di->di_tv = *tv;
2624 }
2625 else
2626 {
2627 if (error_if_locked(dict->dv_lock,
2628 e_cannot_change_dict))
2629 goto on_error;
2630 // add to dict, only fails when out of memory
2631 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002632 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002633 clear_tv(tv);
2634 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002635 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002636 else if (status == OK && dest_type == VAR_BLOB)
2637 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002638 long lidx = (long)tv_idx->vval.v_number;
2639 blob_T *blob = tv_dest->vval.v_blob;
2640 varnumber_T nr;
2641 int error = FALSE;
2642 int len;
2643
2644 if (blob == NULL)
2645 {
2646 emsg(_(e_blob_not_set));
2647 goto on_error;
2648 }
2649 len = blob_len(blob);
2650 if (lidx < 0 && len + lidx >= 0)
2651 // negative index is relative to the end
2652 lidx = len + lidx;
2653
2654 // Can add one byte at the end.
2655 if (lidx < 0 || lidx > len)
2656 {
2657 semsg(_(e_blobidx), lidx);
2658 goto on_error;
2659 }
2660 if (value_check_lock(blob->bv_lock,
2661 (char_u *)"blob", FALSE))
2662 goto on_error;
2663 nr = tv_get_number_chk(tv, &error);
2664 if (error)
2665 goto on_error;
2666 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002667 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002668 else
2669 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002670 status = FAIL;
2671 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002672 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002673
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002674 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002675 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002676 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002677 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002678 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002679 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002680 goto on_error;
2681 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002682 }
2683 break;
2684
Bram Moolenaar68452172021-04-12 21:21:02 +02002685 // store value in blob range
2686 case ISN_STORERANGE:
2687 {
2688 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2689 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2690 typval_T *tv_dest = STACK_TV_BOT(-1);
2691 int status = OK;
2692
2693 // Stack contains:
2694 // -4 value to be stored
2695 // -3 first index or "none"
2696 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002697 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002698 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002699 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002700 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002701 long n1;
2702 long n2;
2703 int error = FALSE;
2704
2705 SOURCING_LNUM = iptr->isn_lnum;
2706 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2707 if (error)
2708 status = FAIL;
2709 else
2710 {
2711 if (tv_idx2->v_type == VAR_SPECIAL
2712 && tv_idx2->vval.v_number == VVAL_NONE)
2713 n2 = list_len(tv_dest->vval.v_list) - 1;
2714 else
2715 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2716 if (error)
2717 status = FAIL;
2718 else
2719 {
2720 listitem_T *li1 = check_range_index_one(
2721 tv_dest->vval.v_list, &n1, FALSE);
2722
2723 if (li1 == NULL)
2724 status = FAIL;
2725 else
2726 {
2727 status = check_range_index_two(
2728 tv_dest->vval.v_list,
2729 &n1, li1, &n2, FALSE);
2730 if (status != FAIL)
2731 status = list_assign_range(
2732 tv_dest->vval.v_list,
2733 tv->vval.v_list,
2734 n1,
2735 n2,
2736 tv_idx2->v_type == VAR_SPECIAL,
2737 (char_u *)"=",
2738 (char_u *)"[unknown]");
2739 }
2740 }
2741 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002742 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002743 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002744 {
2745 varnumber_T n1;
2746 varnumber_T n2;
2747 int error = FALSE;
2748
2749 n1 = tv_get_number_chk(tv_idx1, &error);
2750 if (error)
2751 status = FAIL;
2752 else
2753 {
2754 if (tv_idx2->v_type == VAR_SPECIAL
2755 && tv_idx2->vval.v_number == VVAL_NONE)
2756 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2757 else
2758 n2 = tv_get_number_chk(tv_idx2, &error);
2759 if (error)
2760 status = FAIL;
2761 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002762 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002763 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002764
2765 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002766 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002767 || check_blob_range(bloblen,
2768 n1, n2, FALSE) == FAIL)
2769 status = FAIL;
2770 else
2771 status = blob_set_range(
2772 tv_dest->vval.v_blob, n1, n2, tv);
2773 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002774 }
2775 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002776 else
2777 {
2778 status = FAIL;
2779 emsg(_(e_blob_required));
2780 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002781
2782 clear_tv(tv_idx1);
2783 clear_tv(tv_idx2);
2784 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002785 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002786 clear_tv(tv);
2787
2788 if (status == FAIL)
2789 goto on_error;
2790 }
2791 break;
2792
Bram Moolenaar0186e582021-01-10 18:33:11 +01002793 // load or store variable or argument from outer scope
2794 case ISN_LOADOUTER:
2795 case ISN_STOREOUTER:
2796 {
2797 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002798 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2799 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002800
2801 while (depth > 1 && outer != NULL)
2802 {
2803 outer = outer->out_up;
2804 --depth;
2805 }
2806 if (outer == NULL)
2807 {
2808 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00002809 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
2810 || ectx->ec_outer_ref == NULL)
2811 // Possibly :def function called from legacy
2812 // context.
2813 emsg(_(e_closure_called_from_invalid_context));
2814 else
2815 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002816 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002817 }
2818 tv = ((typval_T *)outer->out_stack->ga_data)
2819 + outer->out_frame_idx + STACK_FRAME_SIZE
2820 + iptr->isn_arg.outer.outer_idx;
2821 if (iptr->isn_type == ISN_LOADOUTER)
2822 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002823 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002824 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002825 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002826 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002827 }
2828 else
2829 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002830 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002831 clear_tv(tv);
2832 *tv = *STACK_TV_BOT(0);
2833 }
2834 }
2835 break;
2836
Bram Moolenaar752fc692021-01-04 21:57:11 +01002837 // unlet item in list or dict variable
2838 case ISN_UNLETINDEX:
2839 {
2840 typval_T *tv_idx = STACK_TV_BOT(-2);
2841 typval_T *tv_dest = STACK_TV_BOT(-1);
2842 int status = OK;
2843
2844 // Stack contains:
2845 // -2 index
2846 // -1 dict or list
2847 if (tv_dest->v_type == VAR_DICT)
2848 {
2849 // unlet a dict item, index must be a string
2850 if (tv_idx->v_type != VAR_STRING)
2851 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002852 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002853 semsg(_(e_expected_str_but_got_str),
2854 vartype_name(VAR_STRING),
2855 vartype_name(tv_idx->v_type));
2856 status = FAIL;
2857 }
2858 else
2859 {
2860 dict_T *d = tv_dest->vval.v_dict;
2861 char_u *key = tv_idx->vval.v_string;
2862 dictitem_T *di = NULL;
2863
Bram Moolenaar71b76852021-12-17 20:15:38 +00002864 if (d != NULL && value_check_lock(
2865 d->dv_lock, NULL, FALSE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002866 status = FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002867 else
2868 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002869 SOURCING_LNUM = iptr->isn_lnum;
2870 if (key == NULL)
2871 key = (char_u *)"";
2872 if (d != NULL)
2873 di = dict_find(d, key, (int)STRLEN(key));
2874 if (di == NULL)
2875 {
2876 // NULL dict is equivalent to empty dict
2877 semsg(_(e_dictkey), key);
2878 status = FAIL;
2879 }
2880 else if (var_check_fixed(di->di_flags,
2881 NULL, FALSE)
2882 || var_check_ro(di->di_flags,
2883 NULL, FALSE))
2884 status = FAIL;
2885 else
2886 dictitem_remove(d, di);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002887 }
2888 }
2889 }
2890 else if (tv_dest->v_type == VAR_LIST)
2891 {
2892 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002893 SOURCING_LNUM = iptr->isn_lnum;
2894 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002895 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002896 status = FAIL;
2897 }
2898 else
2899 {
2900 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002901 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002902
Bram Moolenaar422085f2021-12-17 20:36:15 +00002903 if (l != NULL && value_check_lock(
2904 l->lv_lock, NULL, FALSE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002905 status = FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002906 else
Bram Moolenaar422085f2021-12-17 20:36:15 +00002907 {
2908 listitem_T *li = list_find(l, n);
2909
2910 if (li == NULL)
2911 {
2912 SOURCING_LNUM = iptr->isn_lnum;
2913 semsg(_(e_listidx), n);
2914 status = FAIL;
2915 }
2916 else if (value_check_lock(li->li_tv.v_lock,
2917 NULL, FALSE))
2918 status = FAIL;
2919 else
2920 listitem_remove(l, li);
2921 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002922 }
2923 }
2924 else
2925 {
2926 status = FAIL;
2927 semsg(_(e_cannot_index_str),
2928 vartype_name(tv_dest->v_type));
2929 }
2930
2931 clear_tv(tv_idx);
2932 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002933 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002934 if (status == FAIL)
2935 goto on_error;
2936 }
2937 break;
2938
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002939 // unlet range of items in list variable
2940 case ISN_UNLETRANGE:
2941 {
2942 // Stack contains:
2943 // -3 index1
2944 // -2 index2
2945 // -1 dict or list
2946 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2947 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2948 typval_T *tv_dest = STACK_TV_BOT(-1);
2949 int status = OK;
2950
2951 if (tv_dest->v_type == VAR_LIST)
2952 {
2953 // indexes must be a number
2954 SOURCING_LNUM = iptr->isn_lnum;
2955 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002956 || (tv_idx2->v_type != VAR_SPECIAL
2957 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002958 {
2959 status = FAIL;
2960 }
2961 else
2962 {
2963 list_T *l = tv_dest->vval.v_list;
2964 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002965 long n2 = tv_idx2->v_type == VAR_SPECIAL
2966 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002967 listitem_T *li;
2968
2969 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002970 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002971 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002972 else
2973 {
2974 if (n1 < 0)
2975 n1 = list_idx_of_item(l, li);
2976 if (n2 < 0)
2977 {
2978 listitem_T *li2 = list_find(l, n2);
2979
2980 if (li2 == NULL)
2981 status = FAIL;
2982 else
2983 n2 = list_idx_of_item(l, li2);
2984 }
2985 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002986 && tv_idx2->v_type != VAR_SPECIAL
2987 && n2 < n1)
2988 {
2989 semsg(_(e_listidx), n2);
2990 status = FAIL;
2991 }
2992 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002993 && list_unlet_range(l, li, NULL, n1,
2994 tv_idx2->v_type != VAR_SPECIAL, n2)
2995 == FAIL)
2996 status = FAIL;
2997 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002998 }
2999 }
3000 else
3001 {
3002 status = FAIL;
3003 SOURCING_LNUM = iptr->isn_lnum;
3004 semsg(_(e_cannot_index_str),
3005 vartype_name(tv_dest->v_type));
3006 }
3007
3008 clear_tv(tv_idx1);
3009 clear_tv(tv_idx2);
3010 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003011 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003012 if (status == FAIL)
3013 goto on_error;
3014 }
3015 break;
3016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 // push constant
3018 case ISN_PUSHNR:
3019 case ISN_PUSHBOOL:
3020 case ISN_PUSHSPEC:
3021 case ISN_PUSHF:
3022 case ISN_PUSHS:
3023 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003024 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003025 case ISN_PUSHCHANNEL:
3026 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003027 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003028 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003030 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003031 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 switch (iptr->isn_type)
3033 {
3034 case ISN_PUSHNR:
3035 tv->v_type = VAR_NUMBER;
3036 tv->vval.v_number = iptr->isn_arg.number;
3037 break;
3038 case ISN_PUSHBOOL:
3039 tv->v_type = VAR_BOOL;
3040 tv->vval.v_number = iptr->isn_arg.number;
3041 break;
3042 case ISN_PUSHSPEC:
3043 tv->v_type = VAR_SPECIAL;
3044 tv->vval.v_number = iptr->isn_arg.number;
3045 break;
3046#ifdef FEAT_FLOAT
3047 case ISN_PUSHF:
3048 tv->v_type = VAR_FLOAT;
3049 tv->vval.v_float = iptr->isn_arg.fnumber;
3050 break;
3051#endif
3052 case ISN_PUSHBLOB:
3053 blob_copy(iptr->isn_arg.blob, tv);
3054 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003055 case ISN_PUSHFUNC:
3056 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003057 if (iptr->isn_arg.string == NULL)
3058 tv->vval.v_string = NULL;
3059 else
3060 tv->vval.v_string =
3061 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003062 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003063 case ISN_PUSHCHANNEL:
3064#ifdef FEAT_JOB_CHANNEL
3065 tv->v_type = VAR_CHANNEL;
3066 tv->vval.v_channel = iptr->isn_arg.channel;
3067 if (tv->vval.v_channel != NULL)
3068 ++tv->vval.v_channel->ch_refcount;
3069#endif
3070 break;
3071 case ISN_PUSHJOB:
3072#ifdef FEAT_JOB_CHANNEL
3073 tv->v_type = VAR_JOB;
3074 tv->vval.v_job = iptr->isn_arg.job;
3075 if (tv->vval.v_job != NULL)
3076 ++tv->vval.v_job->jv_refcount;
3077#endif
3078 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 default:
3080 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003081 tv->vval.v_string = vim_strsave(
3082 iptr->isn_arg.string == NULL
3083 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 }
3085 break;
3086
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003087 case ISN_UNLET:
3088 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3089 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003090 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003091 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003092 case ISN_UNLETENV:
3093 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3094 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003095
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003096 case ISN_LOCKUNLOCK:
3097 {
3098 typval_T *lval_root_save = lval_root;
3099 int res;
3100
3101 // Stack has the local variable, argument the whole :lock
3102 // or :unlock command, like ISN_EXEC.
3103 --ectx->ec_stack.ga_len;
3104 lval_root = STACK_TV_BOT(0);
3105 res = exec_command(iptr);
3106 clear_tv(lval_root);
3107 lval_root = lval_root_save;
3108 if (res == FAIL)
3109 goto on_error;
3110 }
3111 break;
3112
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003113 case ISN_LOCKCONST:
3114 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3115 break;
3116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 // create a list from items on the stack; uses a single allocation
3118 // for the list header and the items
3119 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003120 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003121 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 break;
3123
3124 // create a dict from items on the stack
3125 case ISN_NEWDICT:
3126 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003127 int count = iptr->isn_arg.number;
3128 dict_T *dict = dict_alloc();
3129 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003130 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003131 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003133 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003134 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 for (idx = 0; idx < count; ++idx)
3136 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003137 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003139 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003140 key = tv->vval.v_string == NULL
3141 ? (char_u *)"" : tv->vval.v_string;
3142 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003143 if (item != NULL)
3144 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003145 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003146 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003147 dict_unref(dict);
3148 goto on_error;
3149 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003150 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003152 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003153 {
3154 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003155 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3158 item->di_tv.v_lock = 0;
3159 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003160 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003161 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003162 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003163 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 }
3166
3167 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003168 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003169 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003170 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003172 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 tv = STACK_TV_BOT(-1);
3174 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003175 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 tv->vval.v_dict = dict;
3177 ++dict->dv_refcount;
3178 }
3179 break;
3180
3181 // call a :def function
3182 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003183 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003184 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3185 NULL,
3186 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003187 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003188 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 break;
3190
3191 // call a builtin function
3192 case ISN_BCALL:
3193 SOURCING_LNUM = iptr->isn_lnum;
3194 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3195 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003196 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003197 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 break;
3199
3200 // call a funcref or partial
3201 case ISN_PCALL:
3202 {
3203 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3204 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003205 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206
3207 SOURCING_LNUM = iptr->isn_lnum;
3208 if (pfunc->cpf_top)
3209 {
3210 // funcref is above the arguments
3211 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3212 }
3213 else
3214 {
3215 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003216 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003217 partial_tv = *STACK_TV_BOT(0);
3218 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003220 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003221 if (tv == &partial_tv)
3222 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003224 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 }
3226 break;
3227
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003228 case ISN_PCALL_END:
3229 // PCALL finished, arguments have been consumed and replaced by
3230 // the return value. Now clear the funcref from the stack,
3231 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003232 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003233 clear_tv(STACK_TV_BOT(-1));
3234 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3235 break;
3236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 // call a user defined function or funcref/partial
3238 case ISN_UCALL:
3239 {
3240 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3241
3242 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003243 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003244 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003245 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 }
3247 break;
3248
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003249 // return from a :def function call without a value
3250 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003251 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003252 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003253 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003254 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003255 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003256 tv->vval.v_number = 0;
3257 tv->v_lock = 0;
3258 // FALLTHROUGH
3259
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003260 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 case ISN_RETURN:
3262 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003263 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003264 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003265
3266 if (trystack->ga_len > 0)
3267 trycmd = ((trycmd_T *)trystack->ga_data)
3268 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003269 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003270 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003272 // jump to ":finally" or ":endtry"
3273 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003274 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003275 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003276 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 trycmd->tcd_return = TRUE;
3278 }
3279 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003280 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 }
3282 break;
3283
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003284 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 case ISN_FUNCREF:
3286 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003287 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003288 ufunc_T *ufunc;
3289 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003292 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003293 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003294 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003295 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003296 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003297 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003298 if (funcref->fr_func_name == NULL)
3299 {
3300 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3301 + funcref->fr_dfunc_idx;
3302
3303 ufunc = pt_dfunc->df_ufunc;
3304 }
3305 else
3306 {
3307 ufunc = find_func(funcref->fr_func_name, FALSE, NULL);
3308 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003309 if (ufunc == NULL)
3310 {
3311 SOURCING_LNUM = iptr->isn_lnum;
3312 emsg(_(e_function_reference_invalid));
3313 goto theend;
3314 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003315 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003316 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003318 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 tv->vval.v_partial = pt;
3320 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003321 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 }
3323 break;
3324
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003325 // Create a global function from a lambda.
3326 case ISN_NEWFUNC:
3327 {
3328 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3329
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003330 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003331 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003332 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003333 }
3334 break;
3335
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003336 // List functions
3337 case ISN_DEF:
3338 if (iptr->isn_arg.string == NULL)
3339 list_functions(NULL);
3340 else
3341 {
3342 exarg_T ea;
3343
3344 CLEAR_FIELD(ea);
3345 ea.cmd = ea.arg = iptr->isn_arg.string;
3346 define_function(&ea, NULL);
3347 }
3348 break;
3349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 // jump if a condition is met
3351 case ISN_JUMP:
3352 {
3353 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003354 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 int jump = TRUE;
3356
3357 if (when != JUMP_ALWAYS)
3358 {
3359 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003360 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003361 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003362 || when == JUMP_IF_COND_TRUE)
3363 {
3364 SOURCING_LNUM = iptr->isn_lnum;
3365 jump = tv_get_bool_chk(tv, &error);
3366 if (error)
3367 goto on_error;
3368 }
3369 else
3370 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003372 || when == JUMP_AND_KEEP_IF_FALSE
3373 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003375 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 {
3377 // drop the value from the stack
3378 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003379 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 }
3381 }
3382 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003383 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 }
3385 break;
3386
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003387 // Jump if an argument with a default value was already set and not
3388 // v:none.
3389 case ISN_JUMP_IF_ARG_SET:
3390 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3391 if (tv->v_type != VAR_UNKNOWN
3392 && !(tv->v_type == VAR_SPECIAL
3393 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003394 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003395 break;
3396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 // top of a for loop
3398 case ISN_FOR:
3399 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003400 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 typval_T *idxtv =
3402 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3403
Bram Moolenaar35578162021-08-02 19:10:38 +02003404 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003405 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003406 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003407 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003408 list_T *list = ltv->vval.v_list;
3409
3410 // push the next item from the list
3411 ++idxtv->vval.v_number;
3412 if (list == NULL
3413 || idxtv->vval.v_number >= list->lv_len)
3414 {
3415 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3417 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003418 }
3419 else if (list->lv_first == &range_list_item)
3420 {
3421 // non-materialized range() list
3422 tv = STACK_TV_BOT(0);
3423 tv->v_type = VAR_NUMBER;
3424 tv->v_lock = 0;
3425 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003427 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003428 }
3429 else
3430 {
3431 listitem_T *li = list_find(list,
3432 idxtv->vval.v_number);
3433
3434 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003435 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003436 }
3437 }
3438 else if (ltv->v_type == VAR_STRING)
3439 {
3440 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003441
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003442 // The index is for the last byte of the previous
3443 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003444 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003445 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003446 {
3447 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003448 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3449 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003450 }
3451 else
3452 {
3453 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3454
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003455 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003456 tv = STACK_TV_BOT(0);
3457 tv->v_type = VAR_STRING;
3458 tv->vval.v_string = vim_strnsave(
3459 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003460 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003461 idxtv->vval.v_number += clen - 1;
3462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003464 else if (ltv->v_type == VAR_BLOB)
3465 {
3466 blob_T *blob = ltv->vval.v_blob;
3467
3468 // When we get here the first time make a copy of the
3469 // blob, so that the iteration still works when it is
3470 // changed.
3471 if (idxtv->vval.v_number == -1 && blob != NULL)
3472 {
3473 blob_copy(blob, ltv);
3474 blob_unref(blob);
3475 blob = ltv->vval.v_blob;
3476 }
3477
3478 // The index is for the previous byte.
3479 ++idxtv->vval.v_number;
3480 if (blob == NULL
3481 || idxtv->vval.v_number >= blob_len(blob))
3482 {
3483 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003484 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3485 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003486 }
3487 else
3488 {
3489 // Push the next byte from the blob.
3490 tv = STACK_TV_BOT(0);
3491 tv->v_type = VAR_NUMBER;
3492 tv->vval.v_number = blob_get(blob,
3493 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003494 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003495 }
3496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 else
3498 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003499 semsg(_(e_for_loop_on_str_not_supported),
3500 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003501 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 }
3503 }
3504 break;
3505
3506 // start of ":try" block
3507 case ISN_TRY:
3508 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003509 trycmd_T *trycmd = NULL;
3510
Bram Moolenaar35578162021-08-02 19:10:38 +02003511 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003512 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003513 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3514 + ectx->ec_trystack.ga_len;
3515 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003517 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003518 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3519 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003520 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003521 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003522 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003523 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003524 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003525 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 }
3527 break;
3528
3529 case ISN_PUSHEXC:
3530 if (current_exception == NULL)
3531 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003532 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003534 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003536 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003537 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003539 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003541 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 tv->vval.v_string = vim_strsave(
3543 (char_u *)current_exception->value);
3544 break;
3545
3546 case ISN_CATCH:
3547 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003548 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549
Bram Moolenaar4c137212021-04-19 16:48:48 +02003550 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 if (trystack->ga_len > 0)
3552 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003553 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 + trystack->ga_len - 1;
3555 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003556 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 }
3558 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003559 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 catch_exception(current_exception);
3561 }
3562 break;
3563
Bram Moolenaarc150c092021-02-13 15:02:46 +01003564 case ISN_TRYCONT:
3565 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003566 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003567 trycont_T *trycont = &iptr->isn_arg.trycont;
3568 int i;
3569 trycmd_T *trycmd;
3570 int iidx = trycont->tct_where;
3571
3572 if (trystack->ga_len < trycont->tct_levels)
3573 {
3574 siemsg("TRYCONT: expected %d levels, found %d",
3575 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003576 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003577 }
3578 // Make :endtry jump to any outer try block and the last
3579 // :endtry inside the loop to the loop start.
3580 for (i = trycont->tct_levels; i > 0; --i)
3581 {
3582 trycmd = ((trycmd_T *)trystack->ga_data)
3583 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003584 // Add one to tcd_cont to be able to jump to
3585 // instruction with index zero.
3586 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003587 iidx = trycmd->tcd_finally_idx == 0
3588 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003589 }
3590 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003591 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003592 }
3593 break;
3594
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003595 case ISN_FINALLY:
3596 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003597 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003598 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3599 + trystack->ga_len - 1;
3600
3601 // Reset the index to avoid a return statement jumps here
3602 // again.
3603 trycmd->tcd_finally_idx = 0;
3604 break;
3605 }
3606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 // end of ":try" block
3608 case ISN_ENDTRY:
3609 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003610 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611
3612 if (trystack->ga_len > 0)
3613 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003614 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 --trystack->ga_len;
3617 --trylevel;
3618 trycmd = ((trycmd_T *)trystack->ga_data)
3619 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003620 if (trycmd->tcd_did_throw)
3621 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003622 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 {
3624 // discard the exception
3625 if (caught_stack == current_exception)
3626 caught_stack = caught_stack->caught;
3627 discard_current_exception();
3628 }
3629
3630 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003631 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003632
Bram Moolenaar4c137212021-04-19 16:48:48 +02003633 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003634 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003635 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003636 clear_tv(STACK_TV_BOT(0));
3637 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003638 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003639 // handling :continue: jump to outer try block or
3640 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003641 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 }
3643 }
3644 break;
3645
3646 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003647 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003648 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003649
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003650 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3651 {
3652 // throwing an exception while using "silent!" causes
3653 // the function to abort but not display an error.
3654 tv = STACK_TV_BOT(-1);
3655 clear_tv(tv);
3656 tv->v_type = VAR_NUMBER;
3657 tv->vval.v_number = 0;
3658 goto done;
3659 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003660 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003661 tv = STACK_TV_BOT(0);
3662 if (tv->vval.v_string == NULL
3663 || *skipwhite(tv->vval.v_string) == NUL)
3664 {
3665 vim_free(tv->vval.v_string);
3666 SOURCING_LNUM = iptr->isn_lnum;
3667 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003668 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003669 }
3670
3671 // Inside a "catch" we need to first discard the caught
3672 // exception.
3673 if (trystack->ga_len > 0)
3674 {
3675 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3676 + trystack->ga_len - 1;
3677 if (trycmd->tcd_caught && current_exception != NULL)
3678 {
3679 // discard the exception
3680 if (caught_stack == current_exception)
3681 caught_stack = caught_stack->caught;
3682 discard_current_exception();
3683 trycmd->tcd_caught = FALSE;
3684 }
3685 }
3686
3687 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3688 == FAIL)
3689 {
3690 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003691 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003692 }
3693 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 break;
3696
3697 // compare with special values
3698 case ISN_COMPAREBOOL:
3699 case ISN_COMPARESPECIAL:
3700 {
3701 typval_T *tv1 = STACK_TV_BOT(-2);
3702 typval_T *tv2 = STACK_TV_BOT(-1);
3703 varnumber_T arg1 = tv1->vval.v_number;
3704 varnumber_T arg2 = tv2->vval.v_number;
3705 int res;
3706
3707 switch (iptr->isn_arg.op.op_type)
3708 {
3709 case EXPR_EQUAL: res = arg1 == arg2; break;
3710 case EXPR_NEQUAL: res = arg1 != arg2; break;
3711 default: res = 0; break;
3712 }
3713
Bram Moolenaar4c137212021-04-19 16:48:48 +02003714 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 tv1->v_type = VAR_BOOL;
3716 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3717 }
3718 break;
3719
3720 // Operation with two number arguments
3721 case ISN_OPNR:
3722 case ISN_COMPARENR:
3723 {
3724 typval_T *tv1 = STACK_TV_BOT(-2);
3725 typval_T *tv2 = STACK_TV_BOT(-1);
3726 varnumber_T arg1 = tv1->vval.v_number;
3727 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003728 varnumber_T res = 0;
3729 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730
3731 switch (iptr->isn_arg.op.op_type)
3732 {
3733 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003734 case EXPR_DIV: if (arg2 == 0)
3735 div_zero = TRUE;
3736 else
3737 res = arg1 / arg2;
3738 break;
3739 case EXPR_REM: if (arg2 == 0)
3740 div_zero = TRUE;
3741 else
3742 res = arg1 % arg2;
3743 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 case EXPR_SUB: res = arg1 - arg2; break;
3745 case EXPR_ADD: res = arg1 + arg2; break;
3746
3747 case EXPR_EQUAL: res = arg1 == arg2; break;
3748 case EXPR_NEQUAL: res = arg1 != arg2; break;
3749 case EXPR_GREATER: res = arg1 > arg2; break;
3750 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3751 case EXPR_SMALLER: res = arg1 < arg2; break;
3752 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003753 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 }
3755
Bram Moolenaar4c137212021-04-19 16:48:48 +02003756 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 if (iptr->isn_type == ISN_COMPARENR)
3758 {
3759 tv1->v_type = VAR_BOOL;
3760 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3761 }
3762 else
3763 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003764 if (div_zero)
3765 {
3766 SOURCING_LNUM = iptr->isn_lnum;
3767 emsg(_(e_divide_by_zero));
3768 goto on_error;
3769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 }
3771 break;
3772
3773 // Computation with two float arguments
3774 case ISN_OPFLOAT:
3775 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003776#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 {
3778 typval_T *tv1 = STACK_TV_BOT(-2);
3779 typval_T *tv2 = STACK_TV_BOT(-1);
3780 float_T arg1 = tv1->vval.v_float;
3781 float_T arg2 = tv2->vval.v_float;
3782 float_T res = 0;
3783 int cmp = FALSE;
3784
3785 switch (iptr->isn_arg.op.op_type)
3786 {
3787 case EXPR_MULT: res = arg1 * arg2; break;
3788 case EXPR_DIV: res = arg1 / arg2; break;
3789 case EXPR_SUB: res = arg1 - arg2; break;
3790 case EXPR_ADD: res = arg1 + arg2; break;
3791
3792 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3793 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3794 case EXPR_GREATER: cmp = arg1 > arg2; break;
3795 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3796 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3797 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3798 default: cmp = 0; break;
3799 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003800 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 if (iptr->isn_type == ISN_COMPAREFLOAT)
3802 {
3803 tv1->v_type = VAR_BOOL;
3804 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3805 }
3806 else
3807 tv1->vval.v_float = res;
3808 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003809#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 break;
3811
3812 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00003813 case ISN_COMPAREDICT:
3814 case ISN_COMPAREFUNC:
3815 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 case ISN_COMPAREBLOB:
3817 {
3818 typval_T *tv1 = STACK_TV_BOT(-2);
3819 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00003820 exprtype_T exprtype = iptr->isn_arg.op.op_type;
3821 int ic = iptr->isn_arg.op.op_ic;
3822 int res = FALSE;
3823 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824
Bram Moolenaar265f8112021-12-19 12:33:05 +00003825 SOURCING_LNUM = iptr->isn_lnum;
3826 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00003828 status = typval_compare_list(tv1, tv2,
3829 exprtype, ic, &res);
3830 }
3831 else if (iptr->isn_type == ISN_COMPAREDICT)
3832 {
3833 status = typval_compare_dict(tv1, tv2,
3834 exprtype, ic, &res);
3835 }
3836 else if (iptr->isn_type == ISN_COMPAREFUNC)
3837 {
3838 status = typval_compare_func(tv1, tv2,
3839 exprtype, ic, &res);
3840 }
3841 else if (iptr->isn_type == ISN_COMPARESTRING)
3842 {
3843 status = typval_compare_string(tv1, tv2,
3844 exprtype, ic, &res);
3845 }
3846 else
3847 {
3848 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003850 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 clear_tv(tv1);
3852 clear_tv(tv2);
3853 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003854 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3855 if (status == FAIL)
3856 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 }
3858 break;
3859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 case ISN_COMPAREANY:
3861 {
3862 typval_T *tv1 = STACK_TV_BOT(-2);
3863 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003864 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003866 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867
Bram Moolenaareb26f432020-09-14 16:50:05 +02003868 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003869 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003871 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003872 if (status == FAIL)
3873 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 }
3875 break;
3876
3877 case ISN_ADDLIST:
3878 case ISN_ADDBLOB:
3879 {
3880 typval_T *tv1 = STACK_TV_BOT(-2);
3881 typval_T *tv2 = STACK_TV_BOT(-1);
3882
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003883 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003885 {
3886 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3887 && tv1->vval.v_list != NULL)
3888 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3889 NULL);
3890 else
3891 eval_addlist(tv1, tv2);
3892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 else
3894 eval_addblob(tv1, tv2);
3895 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003896 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 }
3898 break;
3899
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003900 case ISN_LISTAPPEND:
3901 {
3902 typval_T *tv1 = STACK_TV_BOT(-2);
3903 typval_T *tv2 = STACK_TV_BOT(-1);
3904 list_T *l = tv1->vval.v_list;
3905
3906 // add an item to a list
3907 if (l == NULL)
3908 {
3909 SOURCING_LNUM = iptr->isn_lnum;
3910 emsg(_(e_cannot_add_to_null_list));
3911 goto on_error;
3912 }
3913 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003914 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003915 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003916 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003917 }
3918 break;
3919
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003920 case ISN_BLOBAPPEND:
3921 {
3922 typval_T *tv1 = STACK_TV_BOT(-2);
3923 typval_T *tv2 = STACK_TV_BOT(-1);
3924 blob_T *b = tv1->vval.v_blob;
3925 int error = FALSE;
3926 varnumber_T n;
3927
3928 // add a number to a blob
3929 if (b == NULL)
3930 {
3931 SOURCING_LNUM = iptr->isn_lnum;
3932 emsg(_(e_cannot_add_to_null_blob));
3933 goto on_error;
3934 }
3935 n = tv_get_number_chk(tv2, &error);
3936 if (error)
3937 goto on_error;
3938 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003939 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003940 }
3941 break;
3942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 // Computation with two arguments of unknown type
3944 case ISN_OPANY:
3945 {
3946 typval_T *tv1 = STACK_TV_BOT(-2);
3947 typval_T *tv2 = STACK_TV_BOT(-1);
3948 varnumber_T n1, n2;
3949#ifdef FEAT_FLOAT
3950 float_T f1 = 0, f2 = 0;
3951#endif
3952 int error = FALSE;
3953
3954 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3955 {
3956 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3957 {
3958 eval_addlist(tv1, tv2);
3959 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003960 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 break;
3962 }
3963 else if (tv1->v_type == VAR_BLOB
3964 && tv2->v_type == VAR_BLOB)
3965 {
3966 eval_addblob(tv1, tv2);
3967 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003968 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 break;
3970 }
3971 }
3972#ifdef FEAT_FLOAT
3973 if (tv1->v_type == VAR_FLOAT)
3974 {
3975 f1 = tv1->vval.v_float;
3976 n1 = 0;
3977 }
3978 else
3979#endif
3980 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003981 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 n1 = tv_get_number_chk(tv1, &error);
3983 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003984 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985#ifdef FEAT_FLOAT
3986 if (tv2->v_type == VAR_FLOAT)
3987 f1 = n1;
3988#endif
3989 }
3990#ifdef FEAT_FLOAT
3991 if (tv2->v_type == VAR_FLOAT)
3992 {
3993 f2 = tv2->vval.v_float;
3994 n2 = 0;
3995 }
3996 else
3997#endif
3998 {
3999 n2 = tv_get_number_chk(tv2, &error);
4000 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002#ifdef FEAT_FLOAT
4003 if (tv1->v_type == VAR_FLOAT)
4004 f2 = n2;
4005#endif
4006 }
4007#ifdef FEAT_FLOAT
4008 // if there is a float on either side the result is a float
4009 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4010 {
4011 switch (iptr->isn_arg.op.op_type)
4012 {
4013 case EXPR_MULT: f1 = f1 * f2; break;
4014 case EXPR_DIV: f1 = f1 / f2; break;
4015 case EXPR_SUB: f1 = f1 - f2; break;
4016 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004017 default: SOURCING_LNUM = iptr->isn_lnum;
4018 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004019 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 }
4021 clear_tv(tv1);
4022 clear_tv(tv2);
4023 tv1->v_type = VAR_FLOAT;
4024 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004025 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 }
4027 else
4028#endif
4029 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004030 int failed = FALSE;
4031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 switch (iptr->isn_arg.op.op_type)
4033 {
4034 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004035 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4036 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004037 goto on_error;
4038 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 case EXPR_SUB: n1 = n1 - n2; break;
4040 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004041 default: n1 = num_modulus(n1, n2, &failed);
4042 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004043 goto on_error;
4044 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 }
4046 clear_tv(tv1);
4047 clear_tv(tv2);
4048 tv1->v_type = VAR_NUMBER;
4049 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004050 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 }
4052 }
4053 break;
4054
4055 case ISN_CONCAT:
4056 {
4057 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4058 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4059 char_u *res;
4060
4061 res = concat_str(str1, str2);
4062 clear_tv(STACK_TV_BOT(-2));
4063 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004064 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 STACK_TV_BOT(-1)->vval.v_string = res;
4066 }
4067 break;
4068
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004069 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004070 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004071 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004072 int is_slice = iptr->isn_type == ISN_STRSLICE;
4073 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004074 char_u *res;
4075
4076 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004077 // string slice: string is at stack-3, first index at
4078 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004079 if (is_slice)
4080 {
4081 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004082 n1 = tv->vval.v_number;
4083 }
4084
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004085 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004086 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004087
Bram Moolenaar4c137212021-04-19 16:48:48 +02004088 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004089 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004090 if (is_slice)
4091 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004092 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004093 else
4094 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004095 // single character (including composing characters).
4096 // If the index is too big or negative the result is
4097 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004098 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004099 vim_free(tv->vval.v_string);
4100 tv->vval.v_string = res;
4101 }
4102 break;
4103
4104 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004105 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004106 case ISN_BLOBINDEX:
4107 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004109 int is_slice = iptr->isn_type == ISN_LISTSLICE
4110 || iptr->isn_type == ISN_BLOBSLICE;
4111 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4112 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004113 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004114 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115
4116 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004117 // list slice: list is at stack-3, indexes at stack-2 and
4118 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004119 // Same for blob.
4120 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121
4122 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004123 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004125
4126 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 {
Bram Moolenaared591872020-08-15 22:14:53 +02004128 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004129 n1 = tv->vval.v_number;
4130 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 }
Bram Moolenaared591872020-08-15 22:14:53 +02004132
Bram Moolenaar4c137212021-04-19 16:48:48 +02004133 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004134 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004135 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004136 if (is_blob)
4137 {
4138 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4139 n1, n2, FALSE, tv) == FAIL)
4140 goto on_error;
4141 }
4142 else
4143 {
4144 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4145 n1, n2, FALSE, tv, TRUE) == FAIL)
4146 goto on_error;
4147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 }
4149 break;
4150
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004151 case ISN_ANYINDEX:
4152 case ISN_ANYSLICE:
4153 {
4154 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4155 typval_T *var1, *var2;
4156 int res;
4157
4158 // index: composite is at stack-2, index at stack-1
4159 // slice: composite is at stack-3, indexes at stack-2 and
4160 // stack-1
4161 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004162 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004163 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4164 goto on_error;
4165 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4166 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004167 res = eval_index_inner(tv, is_slice, var1, var2,
4168 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004169 clear_tv(var1);
4170 if (is_slice)
4171 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004172 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173 if (res == FAIL)
4174 goto on_error;
4175 }
4176 break;
4177
Bram Moolenaar9af78762020-06-16 11:34:42 +02004178 case ISN_SLICE:
4179 {
4180 list_T *list;
4181 int count = iptr->isn_arg.number;
4182
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004183 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004184 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004185 list = tv->vval.v_list;
4186
4187 // no error for short list, expect it to be checked earlier
4188 if (list != NULL && list->lv_len >= count)
4189 {
4190 list_T *newlist = list_slice(list,
4191 count, list->lv_len - 1);
4192
4193 if (newlist != NULL)
4194 {
4195 list_unref(list);
4196 tv->vval.v_list = newlist;
4197 ++newlist->lv_refcount;
4198 }
4199 }
4200 }
4201 break;
4202
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004203 case ISN_GETITEM:
4204 {
4205 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004206 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004207
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004208 // Get list item: list is at stack-1, push item.
4209 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004210 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4211 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004212
Bram Moolenaar35578162021-08-02 19:10:38 +02004213 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004214 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004215 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004216 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004217
4218 // Useful when used in unpack assignment. Reset at
4219 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004220 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004221 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004222 }
4223 break;
4224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 case ISN_MEMBER:
4226 {
4227 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004228 char_u *key;
4229 dictitem_T *di;
4230
4231 // dict member: dict is at stack-2, key at stack-1
4232 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004233 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004234 dict = tv->vval.v_dict;
4235
4236 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004237 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004238 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004239 if (key == NULL)
4240 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004241
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004242 if ((di = dict_find(dict, key, -1)) == NULL)
4243 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004244 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004245 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004246
4247 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004248 // stack contents makes sense and the dict stack is
4249 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004250 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004251 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004252 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004253 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004254 tv->v_type = VAR_NUMBER;
4255 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004256 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004257 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004258 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004259 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004260 // Put the dict used on the dict stack, it might be used by
4261 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004262 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004263 if (dict_stack_save(tv) == FAIL)
4264 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004265 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004266 }
4267 break;
4268
4269 // dict member with string key
4270 case ISN_STRINGMEMBER:
4271 {
4272 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 dictitem_T *di;
4274
4275 tv = STACK_TV_BOT(-1);
4276 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4277 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004278 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004280 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 }
4282 dict = tv->vval.v_dict;
4283
4284 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4285 == NULL)
4286 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004287 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004289 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004291 // Put the dict used on the dict stack, it might be used by
4292 // a dict function later.
4293 if (dict_stack_save(tv) == FAIL)
4294 goto on_fatal_error;
4295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004297 }
4298 break;
4299
4300 case ISN_CLEARDICT:
4301 dict_stack_drop();
4302 break;
4303
4304 case ISN_USEDICT:
4305 {
4306 typval_T *dict_tv = dict_stack_get_tv();
4307
4308 // Turn "dict.Func" into a partial for "Func" bound to
4309 // "dict". Don't do this when "Func" is already a partial
4310 // that was bound explicitly (pt_auto is FALSE).
4311 tv = STACK_TV_BOT(-1);
4312 if (dict_tv != NULL
4313 && dict_tv->v_type == VAR_DICT
4314 && dict_tv->vval.v_dict != NULL
4315 && (tv->v_type == VAR_FUNC
4316 || (tv->v_type == VAR_PARTIAL
4317 && (tv->vval.v_partial->pt_auto
4318 || tv->vval.v_partial->pt_dict == NULL))))
4319 dict_tv->vval.v_dict =
4320 make_partial(dict_tv->vval.v_dict, tv);
4321 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 }
4323 break;
4324
4325 case ISN_NEGATENR:
4326 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004327 if (tv->v_type != VAR_NUMBER
4328#ifdef FEAT_FLOAT
4329 && tv->v_type != VAR_FLOAT
4330#endif
4331 )
4332 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004333 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004334 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004335 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004336 }
4337#ifdef FEAT_FLOAT
4338 if (tv->v_type == VAR_FLOAT)
4339 tv->vval.v_float = -tv->vval.v_float;
4340 else
4341#endif
4342 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 break;
4344
4345 case ISN_CHECKNR:
4346 {
4347 int error = FALSE;
4348
4349 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004350 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004352 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 (void)tv_get_number_chk(tv, &error);
4354 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004355 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 }
4357 break;
4358
4359 case ISN_CHECKTYPE:
4360 {
4361 checktype_T *ct = &iptr->isn_arg.type;
4362
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004363 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004364 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004365 if (!ectx->ec_where.wt_variable)
4366 ectx->ec_where.wt_index = ct->ct_arg_idx;
4367 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4368 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004369 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004370 if (!ectx->ec_where.wt_variable)
4371 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004372
4373 // number 0 is FALSE, number 1 is TRUE
4374 if (tv->v_type == VAR_NUMBER
4375 && ct->ct_type->tt_type == VAR_BOOL
4376 && (tv->vval.v_number == 0
4377 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004379 tv->v_type = VAR_BOOL;
4380 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004381 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 }
4383 }
4384 break;
4385
Bram Moolenaar9af78762020-06-16 11:34:42 +02004386 case ISN_CHECKLEN:
4387 {
4388 int min_len = iptr->isn_arg.checklen.cl_min_len;
4389 list_T *list = NULL;
4390
4391 tv = STACK_TV_BOT(-1);
4392 if (tv->v_type == VAR_LIST)
4393 list = tv->vval.v_list;
4394 if (list == NULL || list->lv_len < min_len
4395 || (list->lv_len > min_len
4396 && !iptr->isn_arg.checklen.cl_more_OK))
4397 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004398 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004399 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004400 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004401 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004402 }
4403 }
4404 break;
4405
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004406 case ISN_SETTYPE:
4407 {
4408 checktype_T *ct = &iptr->isn_arg.type;
4409
4410 tv = STACK_TV_BOT(-1);
4411 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4412 {
4413 free_type(tv->vval.v_dict->dv_type);
4414 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4415 }
4416 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4417 {
4418 free_type(tv->vval.v_list->lv_type);
4419 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4420 }
4421 }
4422 break;
4423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004425 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 {
4427 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004428 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004430 if (iptr->isn_type == ISN_2BOOL)
4431 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004432 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004433 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004434 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004435 n = !n;
4436 }
4437 else
4438 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004439 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004440 SOURCING_LNUM = iptr->isn_lnum;
4441 n = tv_get_bool_chk(tv, &error);
4442 if (error)
4443 goto on_error;
4444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 clear_tv(tv);
4446 tv->v_type = VAR_BOOL;
4447 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4448 }
4449 break;
4450
4451 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004452 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004454 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4455 iptr->isn_type == ISN_2STRING_ANY,
4456 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004457 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 break;
4459
Bram Moolenaar08597872020-12-10 19:43:40 +01004460 case ISN_RANGE:
4461 {
4462 exarg_T ea;
4463 char *errormsg;
4464
Bram Moolenaarece0b872021-01-08 20:40:45 +01004465 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004466 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004467 ea.addr_type = ADDR_LINES;
4468 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004469 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004470 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004471 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004472
Bram Moolenaar35578162021-08-02 19:10:38 +02004473 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004474 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004475 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004476 tv = STACK_TV_BOT(-1);
4477 tv->v_type = VAR_NUMBER;
4478 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004479 if (ea.addr_count == 0)
4480 tv->vval.v_number = curwin->w_cursor.lnum;
4481 else
4482 tv->vval.v_number = ea.line2;
4483 }
4484 break;
4485
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004486 case ISN_PUT:
4487 {
4488 int regname = iptr->isn_arg.put.put_regname;
4489 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4490 char_u *expr = NULL;
4491 int dir = FORWARD;
4492
Bram Moolenaar08597872020-12-10 19:43:40 +01004493 if (lnum < -2)
4494 {
4495 // line number was put on the stack by ISN_RANGE
4496 tv = STACK_TV_BOT(-1);
4497 curwin->w_cursor.lnum = tv->vval.v_number;
4498 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4499 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004500 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004501 }
4502 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004503 // :put! above cursor
4504 dir = BACKWARD;
4505 else if (lnum >= 0)
4506 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004507
4508 if (regname == '=')
4509 {
4510 tv = STACK_TV_BOT(-1);
4511 if (tv->v_type == VAR_STRING)
4512 expr = tv->vval.v_string;
4513 else
4514 {
4515 expr = typval2string(tv, TRUE); // allocates value
4516 clear_tv(tv);
4517 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004518 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004519 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004520 check_cursor();
4521 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4522 vim_free(expr);
4523 }
4524 break;
4525
Bram Moolenaar02194d22020-10-24 23:08:38 +02004526 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004527 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4528 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4529 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4530 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004531 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4532 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004533 break;
4534
Bram Moolenaar02194d22020-10-24 23:08:38 +02004535 case ISN_CMDMOD_REV:
4536 // filter regprog is owned by the instruction, don't free it
4537 cmdmod.cmod_filter_regmatch.regprog = NULL;
4538 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004539 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4540 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004541 break;
4542
Bram Moolenaar792f7862020-11-23 08:31:18 +01004543 case ISN_UNPACK:
4544 {
4545 int count = iptr->isn_arg.unpack.unp_count;
4546 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4547 list_T *l;
4548 listitem_T *li;
4549 int i;
4550
4551 // Check there is a valid list to unpack.
4552 tv = STACK_TV_BOT(-1);
4553 if (tv->v_type != VAR_LIST)
4554 {
4555 SOURCING_LNUM = iptr->isn_lnum;
4556 emsg(_(e_for_argument_must_be_sequence_of_lists));
4557 goto on_error;
4558 }
4559 l = tv->vval.v_list;
4560 if (l == NULL
4561 || l->lv_len < (semicolon ? count - 1 : count))
4562 {
4563 SOURCING_LNUM = iptr->isn_lnum;
4564 emsg(_(e_list_value_does_not_have_enough_items));
4565 goto on_error;
4566 }
4567 else if (!semicolon && l->lv_len > count)
4568 {
4569 SOURCING_LNUM = iptr->isn_lnum;
4570 emsg(_(e_list_value_has_more_items_than_targets));
4571 goto on_error;
4572 }
4573
4574 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004575 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004576 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004577 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004578
4579 // Variable after semicolon gets a list with the remaining
4580 // items.
4581 if (semicolon)
4582 {
4583 list_T *rem_list =
4584 list_alloc_with_items(l->lv_len - count + 1);
4585
4586 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004587 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004588 tv = STACK_TV_BOT(-count);
4589 tv->vval.v_list = rem_list;
4590 ++rem_list->lv_refcount;
4591 tv->v_lock = 0;
4592 li = l->lv_first;
4593 for (i = 0; i < count - 1; ++i)
4594 li = li->li_next;
4595 for (i = 0; li != NULL; ++i)
4596 {
4597 list_set_item(rem_list, i, &li->li_tv);
4598 li = li->li_next;
4599 }
4600 --count;
4601 }
4602
4603 // Produce the values in reverse order, first item last.
4604 li = l->lv_first;
4605 for (i = 0; i < count; ++i)
4606 {
4607 tv = STACK_TV_BOT(-i - 1);
4608 copy_tv(&li->li_tv, tv);
4609 li = li->li_next;
4610 }
4611
4612 list_unref(l);
4613 }
4614 break;
4615
Bram Moolenaarb2049902021-01-24 12:53:53 +01004616 case ISN_PROF_START:
4617 case ISN_PROF_END:
4618 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004619#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004620 funccall_T cookie;
4621 ufunc_T *cur_ufunc =
4622 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004623 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004624
4625 cookie.func = cur_ufunc;
4626 if (iptr->isn_type == ISN_PROF_START)
4627 {
4628 func_line_start(&cookie, iptr->isn_lnum);
4629 // if we get here the instruction is executed
4630 func_line_exec(&cookie);
4631 }
4632 else
4633 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004634#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004635 }
4636 break;
4637
Bram Moolenaare99d4222021-06-13 14:01:26 +02004638 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004639 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004640 break;
4641
Bram Moolenaar389df252020-07-09 21:20:47 +02004642 case ISN_SHUFFLE:
4643 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004644 typval_T tmp_tv;
4645 int item = iptr->isn_arg.shuffle.shfl_item;
4646 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004647
4648 tmp_tv = *STACK_TV_BOT(-item);
4649 for ( ; up > 0 && item > 1; --up)
4650 {
4651 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4652 --item;
4653 }
4654 *STACK_TV_BOT(-item) = tmp_tv;
4655 }
4656 break;
4657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004659 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004661 ectx->ec_where.wt_index = 0;
4662 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 break;
4664 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004665 continue;
4666
Bram Moolenaard032f342020-07-18 18:13:02 +02004667func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004668 // Restore previous function. If the frame pointer is where we started
4669 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004670 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004671 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004672
Bram Moolenaar4c137212021-04-19 16:48:48 +02004673 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004674 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004675 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004676 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004677
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004678on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004679 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004680 // If "emsg_silent" is set then ignore the error, unless it was set
4681 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004682 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004683 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004684 {
4685 // If a sequence of instructions causes an error while ":silent!"
4686 // was used, restore the stack length and jump ahead to restoring
4687 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004688 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004689 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004690 while (ectx->ec_stack.ga_len
4691 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004692 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004693 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004694 clear_tv(STACK_TV_BOT(0));
4695 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004696 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4697 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004698 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004699 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004700 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004701on_fatal_error:
4702 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004703 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004704 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004705 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 }
4707
4708done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004709 ret = OK;
4710theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004711 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004712 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4713 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004714}
4715
4716/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004717 * Execute the instructions from a VAR_INSTR typeval and put the result in
4718 * "rettv".
4719 * Return OK or FAIL.
4720 */
4721 int
4722exe_typval_instr(typval_T *tv, typval_T *rettv)
4723{
4724 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4725 isn_T *save_instr = ectx->ec_instr;
4726 int save_iidx = ectx->ec_iidx;
4727 int res;
4728
4729 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4730 res = exec_instructions(ectx);
4731 if (res == OK)
4732 {
4733 *rettv = *STACK_TV_BOT(-1);
4734 --ectx->ec_stack.ga_len;
4735 }
4736
4737 ectx->ec_instr = save_instr;
4738 ectx->ec_iidx = save_iidx;
4739
4740 return res;
4741}
4742
4743/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004744 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4745 * "substitute_instr".
4746 */
4747 char_u *
4748exe_substitute_instr(void)
4749{
4750 ectx_T *ectx = substitute_instr->subs_ectx;
4751 isn_T *save_instr = ectx->ec_instr;
4752 int save_iidx = ectx->ec_iidx;
4753 char_u *res;
4754
4755 ectx->ec_instr = substitute_instr->subs_instr;
4756 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004757 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004758 typval_T *tv = STACK_TV_BOT(-1);
4759
Bram Moolenaar27523602021-06-05 21:36:19 +02004760 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004761 --ectx->ec_stack.ga_len;
4762 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004763 }
4764 else
4765 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004766 substitute_instr->subs_status = FAIL;
4767 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769
Bram Moolenaar4c137212021-04-19 16:48:48 +02004770 ectx->ec_instr = save_instr;
4771 ectx->ec_iidx = save_iidx;
4772
4773 return res;
4774}
4775
4776/*
4777 * Call a "def" function from old Vim script.
4778 * Return OK or FAIL.
4779 */
4780 int
4781call_def_function(
4782 ufunc_T *ufunc,
4783 int argc_arg, // nr of arguments
4784 typval_T *argv, // arguments
4785 partial_T *partial, // optional partial for context
4786 typval_T *rettv) // return value
4787{
4788 ectx_T ectx; // execution context
4789 int argc = argc_arg;
4790 typval_T *tv;
4791 int idx;
4792 int ret = FAIL;
4793 int defcount = ufunc->uf_args.ga_len - argc;
4794 sctx_T save_current_sctx = current_sctx;
4795 int did_emsg_before = did_emsg_cumul + did_emsg;
4796 int save_suppress_errthrow = suppress_errthrow;
4797 msglist_T **saved_msg_list = NULL;
4798 msglist_T *private_msg_list = NULL;
4799 int save_emsg_silent_def = emsg_silent_def;
4800 int save_did_emsg_def = did_emsg_def;
4801 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004802 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004803
4804// Get pointer to item in the stack.
4805#undef STACK_TV
4806#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4807
4808// Get pointer to item at the bottom of the stack, -1 is the bottom.
4809#undef STACK_TV_BOT
4810#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4811
4812// Get pointer to a local variable on the stack. Negative for arguments.
4813#undef STACK_TV_VAR
4814#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4815
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004816 // Update uf_has_breakpoint if needed.
4817 update_has_breakpoint(ufunc);
4818
Bram Moolenaar4c137212021-04-19 16:48:48 +02004819 if (ufunc->uf_def_status == UF_NOT_COMPILED
4820 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004821 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4822 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004823 == FAIL))
4824 {
4825 if (did_emsg_cumul + did_emsg == did_emsg_before)
4826 semsg(_(e_function_is_not_compiled_str),
4827 printable_func_name(ufunc));
4828 return FAIL;
4829 }
4830
4831 {
4832 // Check the function was really compiled.
4833 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4834 + ufunc->uf_dfunc_idx;
4835 if (INSTRUCTIONS(dfunc) == NULL)
4836 {
4837 iemsg("using call_def_function() on not compiled function");
4838 return FAIL;
4839 }
4840 }
4841
4842 // If depth of calling is getting too high, don't execute the function.
4843 orig_funcdepth = funcdepth_get();
4844 if (funcdepth_increment() == FAIL)
4845 return FAIL;
4846
4847 CLEAR_FIELD(ectx);
4848 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4849 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004850 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004851 {
4852 funcdepth_decrement();
4853 return FAIL;
4854 }
4855 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4856 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4857 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004858 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004859
4860 idx = argc - ufunc->uf_args.ga_len;
4861 if (idx > 0 && ufunc->uf_va_name == NULL)
4862 {
4863 if (idx == 1)
4864 emsg(_(e_one_argument_too_many));
4865 else
4866 semsg(_(e_nr_arguments_too_many), idx);
4867 goto failed_early;
4868 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004869 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4870 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004871 {
4872 if (idx == -1)
4873 emsg(_(e_one_argument_too_few));
4874 else
4875 semsg(_(e_nr_arguments_too_few), -idx);
4876 goto failed_early;
4877 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004878
4879 // Put arguments on the stack, but no more than what the function expects.
4880 // A lambda can be called with more arguments than it uses.
4881 for (idx = 0; idx < argc
4882 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4883 ++idx)
4884 {
4885 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4886 && argv[idx].v_type == VAR_SPECIAL
4887 && argv[idx].vval.v_number == VVAL_NONE)
4888 {
4889 // Use the default value.
4890 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4891 }
4892 else
4893 {
4894 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4895 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004896 ufunc->uf_arg_types[idx], &argv[idx],
4897 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004898 goto failed_early;
4899 copy_tv(&argv[idx], STACK_TV_BOT(0));
4900 }
4901 ++ectx.ec_stack.ga_len;
4902 }
4903
4904 // Turn varargs into a list. Empty list if no args.
4905 if (ufunc->uf_va_name != NULL)
4906 {
4907 int vararg_count = argc - ufunc->uf_args.ga_len;
4908
4909 if (vararg_count < 0)
4910 vararg_count = 0;
4911 else
4912 argc -= vararg_count;
4913 if (exe_newlist(vararg_count, &ectx) == FAIL)
4914 goto failed_early;
4915
4916 // Check the type of the list items.
4917 tv = STACK_TV_BOT(-1);
4918 if (ufunc->uf_va_type != NULL
4919 && ufunc->uf_va_type != &t_list_any
4920 && ufunc->uf_va_type->tt_member != &t_any
4921 && tv->vval.v_list != NULL)
4922 {
4923 type_T *expected = ufunc->uf_va_type->tt_member;
4924 listitem_T *li = tv->vval.v_list->lv_first;
4925
4926 for (idx = 0; idx < vararg_count; ++idx)
4927 {
4928 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004929 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004930 goto failed_early;
4931 li = li->li_next;
4932 }
4933 }
4934
4935 if (defcount > 0)
4936 // Move varargs list to below missing default arguments.
4937 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4938 --ectx.ec_stack.ga_len;
4939 }
4940
4941 // Make space for omitted arguments, will store default value below.
4942 // Any varargs list goes after them.
4943 if (defcount > 0)
4944 for (idx = 0; idx < defcount; ++idx)
4945 {
4946 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4947 ++ectx.ec_stack.ga_len;
4948 }
4949 if (ufunc->uf_va_name != NULL)
4950 ++ectx.ec_stack.ga_len;
4951
4952 // Frame pointer points to just after arguments.
4953 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4954 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4955
4956 {
4957 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4958 + ufunc->uf_dfunc_idx;
4959 ufunc_T *base_ufunc = dfunc->df_ufunc;
4960
4961 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4962 // by copy_func().
4963 if (partial != NULL || base_ufunc->uf_partial != NULL)
4964 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004965 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4966 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004967 goto failed_early;
4968 if (partial != NULL)
4969 {
4970 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4971 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004972 if (current_ectx->ec_outer_ref != NULL
4973 && current_ectx->ec_outer_ref->or_outer != NULL)
4974 ectx.ec_outer_ref->or_outer =
4975 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004976 }
4977 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004978 {
4979 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4980 ++partial->pt_refcount;
4981 ectx.ec_outer_ref->or_partial = partial;
4982 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004983 }
4984 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004985 {
4986 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4987 ++base_ufunc->uf_partial->pt_refcount;
4988 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4989 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004990 }
4991 }
4992
4993 // dummy frame entries
4994 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4995 {
4996 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4997 ++ectx.ec_stack.ga_len;
4998 }
4999
5000 {
5001 // Reserve space for local variables and any closure reference count.
5002 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5003 + ufunc->uf_dfunc_idx;
5004
5005 for (idx = 0; idx < dfunc->df_varcount; ++idx)
5006 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
5007 ectx.ec_stack.ga_len += dfunc->df_varcount;
5008 if (dfunc->df_has_closure)
5009 {
5010 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5011 STACK_TV_VAR(idx)->vval.v_number = 0;
5012 ++ectx.ec_stack.ga_len;
5013 }
5014
5015 ectx.ec_instr = INSTRUCTIONS(dfunc);
5016 }
5017
5018 // Following errors are in the function, not the caller.
5019 // Commands behave like vim9script.
5020 estack_push_ufunc(ufunc, 1);
5021 current_sctx = ufunc->uf_script_ctx;
5022 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5023
5024 // Use a specific location for storing error messages to be converted to an
5025 // exception.
5026 saved_msg_list = msg_list;
5027 msg_list = &private_msg_list;
5028
5029 // Do turn errors into exceptions.
5030 suppress_errthrow = FALSE;
5031
5032 // Do not delete the function while executing it.
5033 ++ufunc->uf_calls;
5034
5035 // When ":silent!" was used before calling then we still abort the
5036 // function. If ":silent!" is used in the function then we don't.
5037 emsg_silent_def = emsg_silent;
5038 did_emsg_def = 0;
5039
5040 ectx.ec_where.wt_index = 0;
5041 ectx.ec_where.wt_variable = FALSE;
5042
5043 // Execute the instructions until done.
5044 ret = exec_instructions(&ectx);
5045 if (ret == OK)
5046 {
5047 // function finished, get result from the stack.
5048 if (ufunc->uf_ret_type == &t_void)
5049 {
5050 rettv->v_type = VAR_VOID;
5051 }
5052 else
5053 {
5054 tv = STACK_TV_BOT(-1);
5055 *rettv = *tv;
5056 tv->v_type = VAR_UNKNOWN;
5057 }
5058 }
5059
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005060 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005061 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5062 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005063
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005064 // Deal with any remaining closures, they may be in use somewhere.
5065 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005066 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005067 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005068 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005069 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005070
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005071 estack_pop();
5072 current_sctx = save_current_sctx;
5073
Bram Moolenaar2336c372021-12-06 15:06:54 +00005074 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5075 // Function was unreferenced while being used, free it now.
5076 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005077
Bram Moolenaar352134b2020-10-17 22:04:08 +02005078 if (*msg_list != NULL && saved_msg_list != NULL)
5079 {
5080 msglist_T **plist = saved_msg_list;
5081
5082 // Append entries from the current msg_list (uncaught exceptions) to
5083 // the saved msg_list.
5084 while (*plist != NULL)
5085 plist = &(*plist)->next;
5086
5087 *plist = *msg_list;
5088 }
5089 msg_list = saved_msg_list;
5090
Bram Moolenaar4c137212021-04-19 16:48:48 +02005091 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005092 {
5093 cmdmod.cmod_filter_regmatch.regprog = NULL;
5094 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005095 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005096 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005097 emsg_silent_def = save_emsg_silent_def;
5098 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005099
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005100failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005101 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5103 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005104 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005107 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005108 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005109 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005110 if (ectx.ec_outer_ref->or_outer_allocated)
5111 vim_free(ectx.ec_outer_ref->or_outer);
5112 partial_unref(ectx.ec_outer_ref->or_partial);
5113 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005114 }
5115
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005116 // Not sure if this is necessary.
5117 suppress_errthrow = save_suppress_errthrow;
5118
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005119 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5120 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005121 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005122 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005123 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 return ret;
5125}
5126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005128 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5129 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5130 * "pfx" is prefixed to every line.
5131 */
5132 static void
5133list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5134{
5135 int line_idx = 0;
5136 int prev_current = 0;
5137 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005138 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005139
5140 for (current = 0; current < instr_count; ++current)
5141 {
5142 isn_T *iptr = &instr[current];
5143 char *line;
5144
5145 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005146 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005147 while (line_idx < iptr->isn_lnum
5148 && line_idx < ufunc->uf_lines.ga_len)
5149 {
5150 if (current > prev_current)
5151 {
5152 msg_puts("\n\n");
5153 prev_current = current;
5154 }
5155 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5156 if (line != NULL)
5157 msg(line);
5158 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005159 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5160 {
5161 int first_def_arg = ufunc->uf_args.ga_len
5162 - ufunc->uf_def_args.ga_len;
5163
5164 if (def_arg_idx > 0)
5165 msg_puts("\n\n");
5166 msg_start();
5167 msg_puts(" ");
5168 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5169 first_def_arg + def_arg_idx]);
5170 msg_puts(" = ");
5171 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5172 msg_clr_eos();
5173 msg_end();
5174 }
5175 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005176
5177 switch (iptr->isn_type)
5178 {
5179 case ISN_EXEC:
5180 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5181 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005182 case ISN_EXEC_SPLIT:
5183 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5184 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005185 case ISN_EXECRANGE:
5186 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5187 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005188 case ISN_LEGACY_EVAL:
5189 smsg("%s%4d EVAL legacy %s", pfx, current,
5190 iptr->isn_arg.string);
5191 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005192 case ISN_REDIRSTART:
5193 smsg("%s%4d REDIR", pfx, current);
5194 break;
5195 case ISN_REDIREND:
5196 smsg("%s%4d REDIR END%s", pfx, current,
5197 iptr->isn_arg.number ? " append" : "");
5198 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005199 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005200#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005201 smsg("%s%4d CEXPR pre %s", pfx, current,
5202 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005203#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005204 break;
5205 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005206#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005207 {
5208 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5209
5210 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5211 cexpr_get_auname(cer->cer_cmdidx),
5212 cer->cer_forceit ? "!" : "",
5213 cer->cer_cmdline);
5214 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005215#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005216 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005217 case ISN_INSTR:
5218 {
5219 smsg("%s%4d INSTR", pfx, current);
5220 list_instructions(" ", iptr->isn_arg.instr,
5221 INT_MAX, NULL);
5222 msg(" -------------");
5223 }
5224 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005225 case ISN_SUBSTITUTE:
5226 {
5227 subs_T *subs = &iptr->isn_arg.subs;
5228
5229 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5230 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5231 msg(" -------------");
5232 }
5233 break;
5234 case ISN_EXECCONCAT:
5235 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5236 (varnumber_T)iptr->isn_arg.number);
5237 break;
5238 case ISN_ECHO:
5239 {
5240 echo_T *echo = &iptr->isn_arg.echo;
5241
5242 smsg("%s%4d %s %d", pfx, current,
5243 echo->echo_with_white ? "ECHO" : "ECHON",
5244 echo->echo_count);
5245 }
5246 break;
5247 case ISN_EXECUTE:
5248 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005249 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005250 break;
5251 case ISN_ECHOMSG:
5252 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005253 (varnumber_T)(iptr->isn_arg.number));
5254 break;
5255 case ISN_ECHOCONSOLE:
5256 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5257 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005258 break;
5259 case ISN_ECHOERR:
5260 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005261 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005262 break;
5263 case ISN_LOAD:
5264 {
5265 if (iptr->isn_arg.number < 0)
5266 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5267 (varnumber_T)(iptr->isn_arg.number
5268 + STACK_FRAME_SIZE));
5269 else
5270 smsg("%s%4d LOAD $%lld", pfx, current,
5271 (varnumber_T)(iptr->isn_arg.number));
5272 }
5273 break;
5274 case ISN_LOADOUTER:
5275 {
5276 if (iptr->isn_arg.number < 0)
5277 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5278 iptr->isn_arg.outer.outer_depth,
5279 iptr->isn_arg.outer.outer_idx
5280 + STACK_FRAME_SIZE);
5281 else
5282 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5283 iptr->isn_arg.outer.outer_depth,
5284 iptr->isn_arg.outer.outer_idx);
5285 }
5286 break;
5287 case ISN_LOADV:
5288 smsg("%s%4d LOADV v:%s", pfx, current,
5289 get_vim_var_name(iptr->isn_arg.number));
5290 break;
5291 case ISN_LOADSCRIPT:
5292 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005293 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5294 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5295 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005296
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005297 sv = get_script_svar(sref, -1);
5298 if (sv == NULL)
5299 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5300 pfx, current, si->sn_name);
5301 else
5302 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005303 sv->sv_name,
5304 sref->sref_idx,
5305 si->sn_name);
5306 }
5307 break;
5308 case ISN_LOADS:
5309 {
5310 scriptitem_T *si = SCRIPT_ITEM(
5311 iptr->isn_arg.loadstore.ls_sid);
5312
5313 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5314 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5315 }
5316 break;
5317 case ISN_LOADAUTO:
5318 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5319 break;
5320 case ISN_LOADG:
5321 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5322 break;
5323 case ISN_LOADB:
5324 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5325 break;
5326 case ISN_LOADW:
5327 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5328 break;
5329 case ISN_LOADT:
5330 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5331 break;
5332 case ISN_LOADGDICT:
5333 smsg("%s%4d LOAD g:", pfx, current);
5334 break;
5335 case ISN_LOADBDICT:
5336 smsg("%s%4d LOAD b:", pfx, current);
5337 break;
5338 case ISN_LOADWDICT:
5339 smsg("%s%4d LOAD w:", pfx, current);
5340 break;
5341 case ISN_LOADTDICT:
5342 smsg("%s%4d LOAD t:", pfx, current);
5343 break;
5344 case ISN_LOADOPT:
5345 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5346 break;
5347 case ISN_LOADENV:
5348 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5349 break;
5350 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005351 smsg("%s%4d LOADREG @%c", pfx, current,
5352 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005353 break;
5354
5355 case ISN_STORE:
5356 if (iptr->isn_arg.number < 0)
5357 smsg("%s%4d STORE arg[%lld]", pfx, current,
5358 iptr->isn_arg.number + STACK_FRAME_SIZE);
5359 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005360 smsg("%s%4d STORE $%lld", pfx, current,
5361 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005362 break;
5363 case ISN_STOREOUTER:
5364 {
5365 if (iptr->isn_arg.number < 0)
5366 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5367 iptr->isn_arg.outer.outer_depth,
5368 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5369 else
5370 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5371 iptr->isn_arg.outer.outer_depth,
5372 iptr->isn_arg.outer.outer_idx);
5373 }
5374 break;
5375 case ISN_STOREV:
5376 smsg("%s%4d STOREV v:%s", pfx, current,
5377 get_vim_var_name(iptr->isn_arg.number));
5378 break;
5379 case ISN_STOREAUTO:
5380 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5381 break;
5382 case ISN_STOREG:
5383 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5384 break;
5385 case ISN_STOREB:
5386 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5387 break;
5388 case ISN_STOREW:
5389 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5390 break;
5391 case ISN_STORET:
5392 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5393 break;
5394 case ISN_STORES:
5395 {
5396 scriptitem_T *si = SCRIPT_ITEM(
5397 iptr->isn_arg.loadstore.ls_sid);
5398
5399 smsg("%s%4d STORES %s in %s", pfx, current,
5400 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5401 }
5402 break;
5403 case ISN_STORESCRIPT:
5404 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005405 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5406 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5407 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005408
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005409 sv = get_script_svar(sref, -1);
5410 if (sv == NULL)
5411 smsg("%s%4d STORESCRIPT [deleted] in %s",
5412 pfx, current, si->sn_name);
5413 else
5414 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005415 sv->sv_name,
5416 sref->sref_idx,
5417 si->sn_name);
5418 }
5419 break;
5420 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005421 case ISN_STOREFUNCOPT:
5422 smsg("%s%4d %s &%s", pfx, current,
5423 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005424 iptr->isn_arg.storeopt.so_name);
5425 break;
5426 case ISN_STOREENV:
5427 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5428 break;
5429 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005430 smsg("%s%4d STOREREG @%c", pfx, current,
5431 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005432 break;
5433 case ISN_STORENR:
5434 smsg("%s%4d STORE %lld in $%d", pfx, current,
5435 iptr->isn_arg.storenr.stnr_val,
5436 iptr->isn_arg.storenr.stnr_idx);
5437 break;
5438
5439 case ISN_STOREINDEX:
5440 smsg("%s%4d STOREINDEX %s", pfx, current,
5441 vartype_name(iptr->isn_arg.vartype));
5442 break;
5443
5444 case ISN_STORERANGE:
5445 smsg("%s%4d STORERANGE", pfx, current);
5446 break;
5447
5448 // constants
5449 case ISN_PUSHNR:
5450 smsg("%s%4d PUSHNR %lld", pfx, current,
5451 (varnumber_T)(iptr->isn_arg.number));
5452 break;
5453 case ISN_PUSHBOOL:
5454 case ISN_PUSHSPEC:
5455 smsg("%s%4d PUSH %s", pfx, current,
5456 get_var_special_name(iptr->isn_arg.number));
5457 break;
5458 case ISN_PUSHF:
5459#ifdef FEAT_FLOAT
5460 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5461#endif
5462 break;
5463 case ISN_PUSHS:
5464 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5465 break;
5466 case ISN_PUSHBLOB:
5467 {
5468 char_u *r;
5469 char_u numbuf[NUMBUFLEN];
5470 char_u *tofree;
5471
5472 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5473 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5474 vim_free(tofree);
5475 }
5476 break;
5477 case ISN_PUSHFUNC:
5478 {
5479 char *name = (char *)iptr->isn_arg.string;
5480
5481 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5482 name == NULL ? "[none]" : name);
5483 }
5484 break;
5485 case ISN_PUSHCHANNEL:
5486#ifdef FEAT_JOB_CHANNEL
5487 {
5488 channel_T *channel = iptr->isn_arg.channel;
5489
5490 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5491 channel == NULL ? 0 : channel->ch_id);
5492 }
5493#endif
5494 break;
5495 case ISN_PUSHJOB:
5496#ifdef FEAT_JOB_CHANNEL
5497 {
5498 typval_T tv;
5499 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005500 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005501
5502 tv.v_type = VAR_JOB;
5503 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005504 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005505 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5506 }
5507#endif
5508 break;
5509 case ISN_PUSHEXC:
5510 smsg("%s%4d PUSH v:exception", pfx, current);
5511 break;
5512 case ISN_UNLET:
5513 smsg("%s%4d UNLET%s %s", pfx, current,
5514 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5515 iptr->isn_arg.unlet.ul_name);
5516 break;
5517 case ISN_UNLETENV:
5518 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5519 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5520 iptr->isn_arg.unlet.ul_name);
5521 break;
5522 case ISN_UNLETINDEX:
5523 smsg("%s%4d UNLETINDEX", pfx, current);
5524 break;
5525 case ISN_UNLETRANGE:
5526 smsg("%s%4d UNLETRANGE", pfx, current);
5527 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005528 case ISN_LOCKUNLOCK:
5529 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5530 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005531 case ISN_LOCKCONST:
5532 smsg("%s%4d LOCKCONST", pfx, current);
5533 break;
5534 case ISN_NEWLIST:
5535 smsg("%s%4d NEWLIST size %lld", pfx, current,
5536 (varnumber_T)(iptr->isn_arg.number));
5537 break;
5538 case ISN_NEWDICT:
5539 smsg("%s%4d NEWDICT size %lld", pfx, current,
5540 (varnumber_T)(iptr->isn_arg.number));
5541 break;
5542
5543 // function call
5544 case ISN_BCALL:
5545 {
5546 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5547
5548 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5549 internal_func_name(cbfunc->cbf_idx),
5550 cbfunc->cbf_argcount);
5551 }
5552 break;
5553 case ISN_DCALL:
5554 {
5555 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5556 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5557 + cdfunc->cdf_idx;
5558
5559 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005560 printable_func_name(df->df_ufunc),
5561 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005562 }
5563 break;
5564 case ISN_UCALL:
5565 {
5566 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5567
5568 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5569 cufunc->cuf_name, cufunc->cuf_argcount);
5570 }
5571 break;
5572 case ISN_PCALL:
5573 {
5574 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5575
5576 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5577 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5578 }
5579 break;
5580 case ISN_PCALL_END:
5581 smsg("%s%4d PCALL end", pfx, current);
5582 break;
5583 case ISN_RETURN:
5584 smsg("%s%4d RETURN", pfx, current);
5585 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005586 case ISN_RETURN_VOID:
5587 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005588 break;
5589 case ISN_FUNCREF:
5590 {
5591 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005592 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005593
Bram Moolenaar38453522021-11-28 22:00:12 +00005594 if (funcref->fr_func_name == NULL)
5595 {
5596 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5597 + funcref->fr_dfunc_idx;
5598 name = df->df_ufunc->uf_name;
5599 }
5600 else
5601 name = funcref->fr_func_name;
5602 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005603 }
5604 break;
5605
5606 case ISN_NEWFUNC:
5607 {
5608 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5609
5610 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5611 newfunc->nf_lambda, newfunc->nf_global);
5612 }
5613 break;
5614
5615 case ISN_DEF:
5616 {
5617 char_u *name = iptr->isn_arg.string;
5618
5619 smsg("%s%4d DEF %s", pfx, current,
5620 name == NULL ? (char_u *)"" : name);
5621 }
5622 break;
5623
5624 case ISN_JUMP:
5625 {
5626 char *when = "?";
5627
5628 switch (iptr->isn_arg.jump.jump_when)
5629 {
5630 case JUMP_ALWAYS:
5631 when = "JUMP";
5632 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005633 case JUMP_NEVER:
5634 iemsg("JUMP_NEVER should not be used");
5635 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005636 case JUMP_AND_KEEP_IF_TRUE:
5637 when = "JUMP_AND_KEEP_IF_TRUE";
5638 break;
5639 case JUMP_IF_FALSE:
5640 when = "JUMP_IF_FALSE";
5641 break;
5642 case JUMP_AND_KEEP_IF_FALSE:
5643 when = "JUMP_AND_KEEP_IF_FALSE";
5644 break;
5645 case JUMP_IF_COND_FALSE:
5646 when = "JUMP_IF_COND_FALSE";
5647 break;
5648 case JUMP_IF_COND_TRUE:
5649 when = "JUMP_IF_COND_TRUE";
5650 break;
5651 }
5652 smsg("%s%4d %s -> %d", pfx, current, when,
5653 iptr->isn_arg.jump.jump_where);
5654 }
5655 break;
5656
5657 case ISN_JUMP_IF_ARG_SET:
5658 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5659 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5660 iptr->isn_arg.jump.jump_where);
5661 break;
5662
5663 case ISN_FOR:
5664 {
5665 forloop_T *forloop = &iptr->isn_arg.forloop;
5666
5667 smsg("%s%4d FOR $%d -> %d", pfx, current,
5668 forloop->for_idx, forloop->for_end);
5669 }
5670 break;
5671
5672 case ISN_TRY:
5673 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005674 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005675
5676 if (try->try_ref->try_finally == 0)
5677 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5678 pfx, current,
5679 try->try_ref->try_catch,
5680 try->try_ref->try_endtry);
5681 else
5682 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5683 pfx, current,
5684 try->try_ref->try_catch,
5685 try->try_ref->try_finally,
5686 try->try_ref->try_endtry);
5687 }
5688 break;
5689 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005690 smsg("%s%4d CATCH", pfx, current);
5691 break;
5692 case ISN_TRYCONT:
5693 {
5694 trycont_T *trycont = &iptr->isn_arg.trycont;
5695
5696 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5697 trycont->tct_levels,
5698 trycont->tct_levels == 1 ? "" : "s",
5699 trycont->tct_where);
5700 }
5701 break;
5702 case ISN_FINALLY:
5703 smsg("%s%4d FINALLY", pfx, current);
5704 break;
5705 case ISN_ENDTRY:
5706 smsg("%s%4d ENDTRY", pfx, current);
5707 break;
5708 case ISN_THROW:
5709 smsg("%s%4d THROW", pfx, current);
5710 break;
5711
5712 // expression operations on number
5713 case ISN_OPNR:
5714 case ISN_OPFLOAT:
5715 case ISN_OPANY:
5716 {
5717 char *what;
5718 char *ins;
5719
5720 switch (iptr->isn_arg.op.op_type)
5721 {
5722 case EXPR_MULT: what = "*"; break;
5723 case EXPR_DIV: what = "/"; break;
5724 case EXPR_REM: what = "%"; break;
5725 case EXPR_SUB: what = "-"; break;
5726 case EXPR_ADD: what = "+"; break;
5727 default: what = "???"; break;
5728 }
5729 switch (iptr->isn_type)
5730 {
5731 case ISN_OPNR: ins = "OPNR"; break;
5732 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5733 case ISN_OPANY: ins = "OPANY"; break;
5734 default: ins = "???"; break;
5735 }
5736 smsg("%s%4d %s %s", pfx, current, ins, what);
5737 }
5738 break;
5739
5740 case ISN_COMPAREBOOL:
5741 case ISN_COMPARESPECIAL:
5742 case ISN_COMPARENR:
5743 case ISN_COMPAREFLOAT:
5744 case ISN_COMPARESTRING:
5745 case ISN_COMPAREBLOB:
5746 case ISN_COMPARELIST:
5747 case ISN_COMPAREDICT:
5748 case ISN_COMPAREFUNC:
5749 case ISN_COMPAREANY:
5750 {
5751 char *p;
5752 char buf[10];
5753 char *type;
5754
5755 switch (iptr->isn_arg.op.op_type)
5756 {
5757 case EXPR_EQUAL: p = "=="; break;
5758 case EXPR_NEQUAL: p = "!="; break;
5759 case EXPR_GREATER: p = ">"; break;
5760 case EXPR_GEQUAL: p = ">="; break;
5761 case EXPR_SMALLER: p = "<"; break;
5762 case EXPR_SEQUAL: p = "<="; break;
5763 case EXPR_MATCH: p = "=~"; break;
5764 case EXPR_IS: p = "is"; break;
5765 case EXPR_ISNOT: p = "isnot"; break;
5766 case EXPR_NOMATCH: p = "!~"; break;
5767 default: p = "???"; break;
5768 }
5769 STRCPY(buf, p);
5770 if (iptr->isn_arg.op.op_ic == TRUE)
5771 strcat(buf, "?");
5772 switch(iptr->isn_type)
5773 {
5774 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5775 case ISN_COMPARESPECIAL:
5776 type = "COMPARESPECIAL"; break;
5777 case ISN_COMPARENR: type = "COMPARENR"; break;
5778 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5779 case ISN_COMPARESTRING:
5780 type = "COMPARESTRING"; break;
5781 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5782 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5783 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5784 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5785 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5786 default: type = "???"; break;
5787 }
5788
5789 smsg("%s%4d %s %s", pfx, current, type, buf);
5790 }
5791 break;
5792
5793 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5794 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5795
5796 // expression operations
5797 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5798 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5799 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5800 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5801 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5802 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5803 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5804 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5805 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5806 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5807 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5808 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005809 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005810 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5811 iptr->isn_arg.getitem.gi_index,
5812 iptr->isn_arg.getitem.gi_with_op ?
5813 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005814 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5815 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5816 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005817 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5818 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5819
Bram Moolenaar4c137212021-04-19 16:48:48 +02005820 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5821
5822 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5823 case ISN_CHECKTYPE:
5824 {
5825 checktype_T *ct = &iptr->isn_arg.type;
5826 char *tofree;
5827
5828 if (ct->ct_arg_idx == 0)
5829 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5830 type_name(ct->ct_type, &tofree),
5831 (int)ct->ct_off);
5832 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005833 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5834 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005835 type_name(ct->ct_type, &tofree),
5836 (int)ct->ct_off,
5837 (int)ct->ct_arg_idx);
5838 vim_free(tofree);
5839 break;
5840 }
5841 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5842 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5843 iptr->isn_arg.checklen.cl_min_len);
5844 break;
5845 case ISN_SETTYPE:
5846 {
5847 char *tofree;
5848
5849 smsg("%s%4d SETTYPE %s", pfx, current,
5850 type_name(iptr->isn_arg.type.ct_type, &tofree));
5851 vim_free(tofree);
5852 break;
5853 }
5854 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005855 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5856 smsg("%s%4d INVERT %d (!val)", pfx, current,
5857 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005858 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005859 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5860 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861 break;
5862 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005863 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005864 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005865 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5866 pfx, current,
5867 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005868 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005869 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5870 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005871 break;
5872 case ISN_PUT:
5873 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5874 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005875 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005876 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5877 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005878 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005879 else
5880 smsg("%s%4d PUT %c %ld", pfx, current,
5881 iptr->isn_arg.put.put_regname,
5882 (long)iptr->isn_arg.put.put_lnum);
5883 break;
5884
Bram Moolenaar4c137212021-04-19 16:48:48 +02005885 case ISN_CMDMOD:
5886 {
5887 char_u *buf;
5888 size_t len = produce_cmdmods(
5889 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5890
5891 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005892 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005893 {
5894 (void)produce_cmdmods(
5895 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5896 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5897 vim_free(buf);
5898 }
5899 break;
5900 }
5901 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5902
5903 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005904 smsg("%s%4d PROFILE START line %d", pfx, current,
5905 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005906 break;
5907
5908 case ISN_PROF_END:
5909 smsg("%s%4d PROFILE END", pfx, current);
5910 break;
5911
Bram Moolenaare99d4222021-06-13 14:01:26 +02005912 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005913 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5914 iptr->isn_arg.debug.dbg_break_lnum + 1,
5915 iptr->isn_lnum,
5916 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005917 break;
5918
Bram Moolenaar4c137212021-04-19 16:48:48 +02005919 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5920 iptr->isn_arg.unpack.unp_count,
5921 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5922 break;
5923 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5924 iptr->isn_arg.shuffle.shfl_item,
5925 iptr->isn_arg.shuffle.shfl_up);
5926 break;
5927 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5928
5929 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5930 return;
5931 }
5932
5933 out_flush(); // output one line at a time
5934 ui_breakcheck();
5935 if (got_int)
5936 break;
5937 }
5938}
5939
5940/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005941 * Handle command line completion for the :disassemble command.
5942 */
5943 void
5944set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5945{
5946 char_u *p;
5947
5948 // Default: expand user functions, "debug" and "profile"
5949 xp->xp_context = EXPAND_DISASSEMBLE;
5950 xp->xp_pattern = arg;
5951
5952 // first argument already typed: only user function names
5953 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5954 {
5955 xp->xp_context = EXPAND_USER_FUNC;
5956 xp->xp_pattern = skipwhite(p);
5957 }
5958}
5959
5960/*
5961 * Function given to ExpandGeneric() to obtain the list of :disassemble
5962 * arguments.
5963 */
5964 char_u *
5965get_disassemble_argument(expand_T *xp, int idx)
5966{
5967 if (idx == 0)
5968 return (char_u *)"debug";
5969 if (idx == 1)
5970 return (char_u *)"profile";
5971 return get_user_func_name(xp, idx - 2);
5972}
5973
5974/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005975 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005976 * We don't really need this at runtime, but we do have tests that require it,
5977 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005978 */
5979 void
5980ex_disassemble(exarg_T *eap)
5981{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005982 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005983 char_u *fname;
5984 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985 dfunc_T *dfunc;
5986 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005987 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005988 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005989 compiletype_T compile_type = CT_NONE;
5990
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005991 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005992 {
5993 compile_type = CT_PROFILE;
5994 arg = skipwhite(arg + 7);
5995 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005996 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005997 {
5998 compile_type = CT_DEBUG;
5999 arg = skipwhite(arg + 5);
6000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006002 if (STRNCMP(arg, "<lambda>", 8) == 0)
6003 {
6004 arg += 8;
6005 (void)getdigits(&arg);
6006 fname = vim_strnsave(eap->arg, arg - eap->arg);
6007 }
6008 else
6009 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006010 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006011 if (fname == NULL)
6012 {
6013 semsg(_(e_invarg2), eap->arg);
6014 return;
6015 }
6016
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006017 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006018 if (ufunc == NULL)
6019 {
6020 char_u *p = untrans_function_name(fname);
6021
6022 if (p != NULL)
6023 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006024 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006025 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006026 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006027 if (ufunc == NULL)
6028 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006029 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006030 return;
6031 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006032 if (func_needs_compiling(ufunc, compile_type)
6033 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006034 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006035 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006037 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038 return;
6039 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006040 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041
6042 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006043 switch (compile_type)
6044 {
6045 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006046#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006047 instr = dfunc->df_instr_prof;
6048 instr_count = dfunc->df_instr_prof_count;
6049 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006050#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006051 // FALLTHROUGH
6052 case CT_NONE:
6053 instr = dfunc->df_instr;
6054 instr_count = dfunc->df_instr_count;
6055 break;
6056 case CT_DEBUG:
6057 instr = dfunc->df_instr_debug;
6058 instr_count = dfunc->df_instr_debug_count;
6059 break;
6060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061
Bram Moolenaar4c137212021-04-19 16:48:48 +02006062 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063}
6064
6065/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006066 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006067 * list, etc. Mostly like what JavaScript does, except that empty list and
6068 * empty dictionary are FALSE.
6069 */
6070 int
6071tv2bool(typval_T *tv)
6072{
6073 switch (tv->v_type)
6074 {
6075 case VAR_NUMBER:
6076 return tv->vval.v_number != 0;
6077 case VAR_FLOAT:
6078#ifdef FEAT_FLOAT
6079 return tv->vval.v_float != 0.0;
6080#else
6081 break;
6082#endif
6083 case VAR_PARTIAL:
6084 return tv->vval.v_partial != NULL;
6085 case VAR_FUNC:
6086 case VAR_STRING:
6087 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6088 case VAR_LIST:
6089 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6090 case VAR_DICT:
6091 return tv->vval.v_dict != NULL
6092 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6093 case VAR_BOOL:
6094 case VAR_SPECIAL:
6095 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6096 case VAR_JOB:
6097#ifdef FEAT_JOB_CHANNEL
6098 return tv->vval.v_job != NULL;
6099#else
6100 break;
6101#endif
6102 case VAR_CHANNEL:
6103#ifdef FEAT_JOB_CHANNEL
6104 return tv->vval.v_channel != NULL;
6105#else
6106 break;
6107#endif
6108 case VAR_BLOB:
6109 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6110 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006111 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006113 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114 break;
6115 }
6116 return FALSE;
6117}
6118
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006119 void
6120emsg_using_string_as(typval_T *tv, int as_number)
6121{
6122 semsg(_(as_number ? e_using_string_as_number_str
6123 : e_using_string_as_bool_str),
6124 tv->vval.v_string == NULL
6125 ? (char_u *)"" : tv->vval.v_string);
6126}
6127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006128/*
6129 * If "tv" is a string give an error and return FAIL.
6130 */
6131 int
6132check_not_string(typval_T *tv)
6133{
6134 if (tv->v_type == VAR_STRING)
6135 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006136 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137 clear_tv(tv);
6138 return FAIL;
6139 }
6140 return OK;
6141}
6142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143
6144#endif // FEAT_EVAL