blob: 74851121b22dd99f76d79474af2902b563a0348c [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);
1663 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001664 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001665 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001666
Bram Moolenaar303215d2021-07-07 20:10:43 +02001667 if (p == NULL)
1668 continue; // left over from continuation line
1669 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001670 if (*p == '#')
1671 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001672 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001673 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1674 if (STRNCMP(p, "def ", 4) == 0)
1675 break;
1676 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001677 line = ga_concat_strings(&ga, " ");
1678 vim_free(ga.ga_data);
1679 }
1680 else
1681 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001682
Dominique Pelle6e969552021-06-17 13:53:41 +02001683 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001684 debug_context = NULL;
1685
1686 if (end_lnum > iptr->isn_lnum)
1687 vim_free(line);
1688}
1689
Bram Moolenaar4c137212021-04-19 16:48:48 +02001690/*
1691 * Execute instructions in execution context "ectx".
1692 * Return OK or FAIL;
1693 */
1694 static int
1695exec_instructions(ectx_T *ectx)
1696{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001697 int ret = FAIL;
1698 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001699 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001700
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001701 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001702 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001703
Bram Moolenaarff652882021-05-16 15:24:49 +02001704 // Only catch exceptions in this instruction list.
1705 ectx->ec_trylevel_at_start = trylevel;
1706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 for (;;)
1708 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001709 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001711 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001712
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001713 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001714 {
1715 line_breakcheck();
1716 breakcheck_count = 0;
1717 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001718 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001719 {
1720 // Turn CTRL-C into an exception.
1721 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001722 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001723 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001724 did_throw = TRUE;
1725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001727 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001728 {
1729 // Turn an error message into an exception.
1730 did_emsg = FALSE;
1731 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001732 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001733 did_throw = TRUE;
1734 *msg_list = NULL;
1735 }
1736
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001737 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001739 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001740 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001741 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
1743 // An exception jumps to the first catch, finally, or returns from
1744 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001745 while (index > 0)
1746 {
1747 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001748 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001749 break;
1750 // In the catch and finally block of this try we have to go up
1751 // one level.
1752 --index;
1753 trycmd = NULL;
1754 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001755 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001757 if (trycmd->tcd_in_catch)
1758 {
1759 // exception inside ":catch", jump to ":finally" once
1760 ectx->ec_iidx = trycmd->tcd_finally_idx;
1761 trycmd->tcd_finally_idx = 0;
1762 }
1763 else
1764 // jump to first ":catch"
1765 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001766 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001767 did_throw = FALSE; // don't come back here until :endtry
1768 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 }
1770 else
1771 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001772 // Not inside try or need to return from current functions.
1773 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001774 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001775 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001776 tv = STACK_TV_BOT(0);
1777 tv->v_type = VAR_NUMBER;
1778 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001779 ++ectx->ec_stack.ga_len;
1780 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001782 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001783 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001784 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001785 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 goto done;
1787 }
1788
Bram Moolenaar4c137212021-04-19 16:48:48 +02001789 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001790 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 }
1792 continue;
1793 }
1794
Bram Moolenaar4c137212021-04-19 16:48:48 +02001795 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 switch (iptr->isn_type)
1797 {
1798 // execute Ex command line
1799 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001800 if (exec_command(iptr) == FAIL)
1801 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 break;
1803
Bram Moolenaar20677332021-06-06 17:02:53 +02001804 // execute Ex command line split at NL characters.
1805 case ISN_EXEC_SPLIT:
1806 {
1807 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001808 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001809
1810 SOURCING_LNUM = iptr->isn_lnum;
1811 CLEAR_FIELD(cookie);
1812 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1813 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001814 line = get_split_sourceline(0, &cookie, 0, 0);
1815 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001816 get_split_sourceline, &cookie,
1817 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1818 == FAIL
1819 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001820 {
1821 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001822 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001823 }
1824 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001825 }
1826 break;
1827
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001828 // execute Ex command line that is only a range
1829 case ISN_EXECRANGE:
1830 {
1831 exarg_T ea;
1832 char *error = NULL;
1833
1834 CLEAR_FIELD(ea);
1835 ea.cmdidx = CMD_SIZE;
1836 ea.addr_type = ADDR_LINES;
1837 ea.cmd = iptr->isn_arg.string;
1838 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00001839 if (ea.cmd == NULL)
1840 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001841 if (error == NULL)
1842 error = ex_range_without_command(&ea);
1843 if (error != NULL)
1844 {
1845 SOURCING_LNUM = iptr->isn_lnum;
1846 emsg(error);
1847 goto on_error;
1848 }
1849 }
1850 break;
1851
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001852 // Evaluate an expression with legacy syntax, push it onto the
1853 // stack.
1854 case ISN_LEGACY_EVAL:
1855 {
1856 char_u *arg = iptr->isn_arg.string;
1857 int res;
1858 int save_flags = cmdmod.cmod_flags;
1859
Bram Moolenaar35578162021-08-02 19:10:38 +02001860 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001861 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001862 tv = STACK_TV_BOT(0);
1863 init_tv(tv);
1864 cmdmod.cmod_flags |= CMOD_LEGACY;
1865 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1866 cmdmod.cmod_flags = save_flags;
1867 if (res == FAIL)
1868 goto on_error;
1869 ++ectx->ec_stack.ga_len;
1870 }
1871 break;
1872
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001873 // push typeval VAR_INSTR with instructions to be executed
1874 case ISN_INSTR:
1875 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001876 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001877 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001878 tv = STACK_TV_BOT(0);
1879 tv->vval.v_instr = ALLOC_ONE(instr_T);
1880 if (tv->vval.v_instr == NULL)
1881 goto on_error;
1882 ++ectx->ec_stack.ga_len;
1883
1884 tv->v_type = VAR_INSTR;
1885 tv->vval.v_instr->instr_ectx = ectx;
1886 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1887 }
1888 break;
1889
Bram Moolenaar4c137212021-04-19 16:48:48 +02001890 // execute :substitute with an expression
1891 case ISN_SUBSTITUTE:
1892 {
1893 subs_T *subs = &iptr->isn_arg.subs;
1894 source_cookie_T cookie;
1895 struct subs_expr_S *save_instr = substitute_instr;
1896 struct subs_expr_S subs_instr;
1897 int res;
1898
1899 subs_instr.subs_ectx = ectx;
1900 subs_instr.subs_instr = subs->subs_instr;
1901 subs_instr.subs_status = OK;
1902 substitute_instr = &subs_instr;
1903
1904 SOURCING_LNUM = iptr->isn_lnum;
1905 // This is very much like ISN_EXEC
1906 CLEAR_FIELD(cookie);
1907 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1908 res = do_cmdline(subs->subs_cmd,
1909 getsourceline, &cookie,
1910 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1911 substitute_instr = save_instr;
1912
1913 if (res == FAIL || did_emsg
1914 || subs_instr.subs_status == FAIL)
1915 goto on_error;
1916 }
1917 break;
1918
1919 case ISN_FINISH:
1920 goto done;
1921
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001922 case ISN_REDIRSTART:
1923 // create a dummy entry for var_redir_str()
1924 if (alloc_redir_lval() == FAIL)
1925 goto on_error;
1926
1927 // The output is stored in growarray "redir_ga" until
1928 // redirection ends.
1929 init_redir_ga();
1930 redir_vname = 1;
1931 break;
1932
1933 case ISN_REDIREND:
1934 {
1935 char_u *res = get_clear_redir_ga();
1936
1937 // End redirection, put redirected text on the stack.
1938 clear_redir_lval();
1939 redir_vname = 0;
1940
Bram Moolenaar35578162021-08-02 19:10:38 +02001941 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001942 {
1943 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001944 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001945 }
1946 tv = STACK_TV_BOT(0);
1947 tv->v_type = VAR_STRING;
1948 tv->vval.v_string = res;
1949 ++ectx->ec_stack.ga_len;
1950 }
1951 break;
1952
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001953 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001954#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001955 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1956 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001957#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001958 break;
1959
1960 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001961#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001962 {
1963 exarg_T ea;
1964 int res;
1965
1966 CLEAR_FIELD(ea);
1967 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1968 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1969 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1970 --ectx->ec_stack.ga_len;
1971 tv = STACK_TV_BOT(0);
1972 res = cexpr_core(&ea, tv);
1973 clear_tv(tv);
1974 if (res == FAIL)
1975 goto on_error;
1976 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001977#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001978 break;
1979
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001980 // execute Ex command from pieces on the stack
1981 case ISN_EXECCONCAT:
1982 {
1983 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001984 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001985 int pass;
1986 int i;
1987 char_u *cmd = NULL;
1988 char_u *str;
1989
1990 for (pass = 1; pass <= 2; ++pass)
1991 {
1992 for (i = 0; i < count; ++i)
1993 {
1994 tv = STACK_TV_BOT(i - count);
1995 str = tv->vval.v_string;
1996 if (str != NULL && *str != NUL)
1997 {
1998 if (pass == 2)
1999 STRCPY(cmd + len, str);
2000 len += STRLEN(str);
2001 }
2002 if (pass == 2)
2003 clear_tv(tv);
2004 }
2005 if (pass == 1)
2006 {
2007 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002008 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002009 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002010 len = 0;
2011 }
2012 }
2013
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002014 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002015 do_cmdline_cmd(cmd);
2016 vim_free(cmd);
2017 }
2018 break;
2019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 // execute :echo {string} ...
2021 case ISN_ECHO:
2022 {
2023 int count = iptr->isn_arg.echo.echo_count;
2024 int atstart = TRUE;
2025 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002026 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027
2028 for (idx = 0; idx < count; ++idx)
2029 {
2030 tv = STACK_TV_BOT(idx - count);
2031 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2032 &atstart, &needclr);
2033 clear_tv(tv);
2034 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002035 if (needclr)
2036 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002037 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 }
2039 break;
2040
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002041 // :execute {string} ...
2042 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002043 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002044 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002045 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002046 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002047 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002048 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002049 {
2050 int count = iptr->isn_arg.number;
2051 garray_T ga;
2052 char_u buf[NUMBUFLEN];
2053 char_u *p;
2054 int len;
2055 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002056 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002057
2058 ga_init2(&ga, 1, 80);
2059 for (idx = 0; idx < count; ++idx)
2060 {
2061 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002062 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002063 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002064 if (tv->v_type == VAR_CHANNEL
2065 || tv->v_type == VAR_JOB)
2066 {
2067 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002068 semsg(_(e_using_invalid_value_as_string_str),
2069 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002070 break;
2071 }
2072 else
2073 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002074 }
2075 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002076 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002077
2078 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002079 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002080 failed = TRUE;
2081 else
2082 {
2083 if (ga.ga_len > 0)
2084 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2085 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2086 ga.ga_len += len;
2087 }
2088 clear_tv(tv);
2089 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002090 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002091 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002092 {
2093 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002094 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002095 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002096
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002097 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002098 {
2099 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002100 {
2101 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002102 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002103 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002104 {
2105 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002106 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002107 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002108 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002109 else
2110 {
2111 msg_sb_eol();
2112 if (iptr->isn_type == ISN_ECHOMSG)
2113 {
2114 msg_attr(ga.ga_data, echo_attr);
2115 out_flush();
2116 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002117 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2118 {
2119 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2120 TRUE);
2121 ui_write((char_u *)"\r\n", 2, TRUE);
2122 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002123 else
2124 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002125 SOURCING_LNUM = iptr->isn_lnum;
2126 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002127 }
2128 }
2129 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002130 ga_clear(&ga);
2131 }
2132 break;
2133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 // load local variable or argument
2135 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002136 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002137 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002139 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 break;
2141
2142 // load v: variable
2143 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002144 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002145 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002147 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 break;
2149
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002150 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 case ISN_LOADSCRIPT:
2152 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002153 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 svar_T *sv;
2155
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002156 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002157 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002158 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002159 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002160 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002161 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002163 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 }
2165 break;
2166
2167 // load s: variable in old script
2168 case ISN_LOADS:
2169 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002170 hashtab_T *ht = &SCRIPT_VARS(
2171 iptr->isn_arg.loadstore.ls_sid);
2172 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 if (di == NULL)
2176 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002177 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002178 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002179 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 }
2181 else
2182 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002183 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002184 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002186 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 }
2188 }
2189 break;
2190
Bram Moolenaard3aac292020-04-19 14:32:17 +02002191 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002193 case ISN_LOADB:
2194 case ISN_LOADW:
2195 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002197 dictitem_T *di = NULL;
2198 hashtab_T *ht = NULL;
2199 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002200
Bram Moolenaard3aac292020-04-19 14:32:17 +02002201 switch (iptr->isn_type)
2202 {
2203 case ISN_LOADG:
2204 ht = get_globvar_ht();
2205 namespace = 'g';
2206 break;
2207 case ISN_LOADB:
2208 ht = &curbuf->b_vars->dv_hashtab;
2209 namespace = 'b';
2210 break;
2211 case ISN_LOADW:
2212 ht = &curwin->w_vars->dv_hashtab;
2213 namespace = 'w';
2214 break;
2215 case ISN_LOADT:
2216 ht = &curtab->tp_vars->dv_hashtab;
2217 namespace = 't';
2218 break;
2219 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002220 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002221 }
2222 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 if (di == NULL)
2225 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002226 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002227 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002228 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002229 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 }
2231 else
2232 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002233 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002234 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002236 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 }
2238 }
2239 break;
2240
Bram Moolenaar03290b82020-12-19 16:30:44 +01002241 // load autoload variable
2242 case ISN_LOADAUTO:
2243 {
2244 char_u *name = iptr->isn_arg.string;
2245
Bram Moolenaar35578162021-08-02 19:10:38 +02002246 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002247 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002249 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002250 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002251 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002252 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002253 }
2254 break;
2255
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002256 // load g:/b:/w:/t: namespace
2257 case ISN_LOADGDICT:
2258 case ISN_LOADBDICT:
2259 case ISN_LOADWDICT:
2260 case ISN_LOADTDICT:
2261 {
2262 dict_T *d = NULL;
2263
2264 switch (iptr->isn_type)
2265 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002266 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2267 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2268 case ISN_LOADWDICT: d = curwin->w_vars; break;
2269 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002270 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002271 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002272 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002273 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002274 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002275 tv = STACK_TV_BOT(0);
2276 tv->v_type = VAR_DICT;
2277 tv->v_lock = 0;
2278 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002279 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002280 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002281 }
2282 break;
2283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 // load &option
2285 case ISN_LOADOPT:
2286 {
2287 typval_T optval;
2288 char_u *name = iptr->isn_arg.string;
2289
Bram Moolenaara8c17702020-04-01 21:17:24 +02002290 // This is not expected to fail, name is checked during
2291 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002292 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002293 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002294 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002297 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 }
2299 break;
2300
2301 // load $ENV
2302 case ISN_LOADENV:
2303 {
2304 typval_T optval;
2305 char_u *name = iptr->isn_arg.string;
2306
Bram Moolenaar35578162021-08-02 19:10:38 +02002307 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002308 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002309 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002310 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002312 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 }
2314 break;
2315
2316 // load @register
2317 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002318 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002319 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 tv = STACK_TV_BOT(0);
2321 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002322 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002323 // This may result in NULL, which should be equivalent to an
2324 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 tv->vval.v_string = get_reg_contents(
2326 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002327 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 break;
2329
2330 // store local variable
2331 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002332 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 tv = STACK_TV_VAR(iptr->isn_arg.number);
2334 clear_tv(tv);
2335 *tv = *STACK_TV_BOT(0);
2336 break;
2337
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002338 // store s: variable in old script
2339 case ISN_STORES:
2340 {
2341 hashtab_T *ht = &SCRIPT_VARS(
2342 iptr->isn_arg.loadstore.ls_sid);
2343 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002344 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002345
Bram Moolenaar4c137212021-04-19 16:48:48 +02002346 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002347 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002348 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002349 else
2350 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002351 SOURCING_LNUM = iptr->isn_lnum;
2352 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002353 {
2354 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002355 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002356 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002357 clear_tv(&di->di_tv);
2358 di->di_tv = *STACK_TV_BOT(0);
2359 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002360 }
2361 break;
2362
2363 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 case ISN_STORESCRIPT:
2365 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002366 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2367 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002369 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002370 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002371 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002372 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002373
2374 // "const" and "final" are checked at compile time, locking
2375 // the value needs to be checked here.
2376 SOURCING_LNUM = iptr->isn_lnum;
2377 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002378 {
2379 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002380 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002381 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 clear_tv(sv->sv_tv);
2384 *sv->sv_tv = *STACK_TV_BOT(0);
2385 }
2386 break;
2387
2388 // store option
2389 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002390 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002392 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
2393 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 long n = 0;
2395 char_u *s = NULL;
2396 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002397 char_u numbuf[NUMBUFLEN];
2398 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399
Bram Moolenaar4c137212021-04-19 16:48:48 +02002400 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 tv = STACK_TV_BOT(0);
2402 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002403 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002405 if (s == NULL)
2406 s = (char_u *)"";
2407 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002408 else if (iptr->isn_type == ISN_STOREFUNCOPT)
2409 {
2410 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002411 // If the option can be set to a function reference or
2412 // a lambda and the passed value is a function
2413 // reference, then convert it to the name (string) of
2414 // the function reference.
2415 s = tv2string(tv, &tofree, numbuf, 0);
2416 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002417 {
2418 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002419 goto on_error;
2420 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002423 // must be VAR_NUMBER, CHECKTYPE makes sure
2424 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002425 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002426 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002427 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 if (msg != NULL)
2429 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002430 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002432 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 }
2435 break;
2436
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002437 // store $ENV
2438 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002439 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002440 tv = STACK_TV_BOT(0);
2441 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2442 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002443 break;
2444
2445 // store @r
2446 case ISN_STOREREG:
2447 {
2448 int reg = iptr->isn_arg.number;
2449
Bram Moolenaar4c137212021-04-19 16:48:48 +02002450 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002451 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002452 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002453 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002454 }
2455 break;
2456
2457 // store v: variable
2458 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002459 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002460 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2461 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002462 // should not happen, type is checked when compiling
2463 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002464 break;
2465
Bram Moolenaard3aac292020-04-19 14:32:17 +02002466 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002468 case ISN_STOREB:
2469 case ISN_STOREW:
2470 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002472 dictitem_T *di;
2473 hashtab_T *ht;
2474 char_u *name = iptr->isn_arg.string + 2;
2475
Bram Moolenaard3aac292020-04-19 14:32:17 +02002476 switch (iptr->isn_type)
2477 {
2478 case ISN_STOREG:
2479 ht = get_globvar_ht();
2480 break;
2481 case ISN_STOREB:
2482 ht = &curbuf->b_vars->dv_hashtab;
2483 break;
2484 case ISN_STOREW:
2485 ht = &curwin->w_vars->dv_hashtab;
2486 break;
2487 case ISN_STORET:
2488 ht = &curtab->tp_vars->dv_hashtab;
2489 break;
2490 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002491 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493
Bram Moolenaar4c137212021-04-19 16:48:48 +02002494 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002495 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002497 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 else
2499 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002500 SOURCING_LNUM = iptr->isn_lnum;
2501 if (var_check_permission(di, name) == FAIL)
2502 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 clear_tv(&di->di_tv);
2504 di->di_tv = *STACK_TV_BOT(0);
2505 }
2506 }
2507 break;
2508
Bram Moolenaar03290b82020-12-19 16:30:44 +01002509 // store an autoload variable
2510 case ISN_STOREAUTO:
2511 SOURCING_LNUM = iptr->isn_lnum;
2512 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2513 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002514 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002515 break;
2516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 // store number in local variable
2518 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002519 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 clear_tv(tv);
2521 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002522 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 break;
2524
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002525 // store value in list or dict variable
2526 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002527 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002528 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002529 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002530 typval_T *tv_dest = STACK_TV_BOT(-1);
2531 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002532
Bram Moolenaar752fc692021-01-04 21:57:11 +01002533 // Stack contains:
2534 // -3 value to be stored
2535 // -2 index
2536 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002537 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002538 SOURCING_LNUM = iptr->isn_lnum;
2539 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002540 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002541 dest_type = tv_dest->v_type;
2542 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002543 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002544 else if (dest_type == VAR_LIST
2545 && tv_idx->v_type != VAR_NUMBER)
2546 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002547 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002548 status = FAIL;
2549 }
2550 }
2551 else if (dest_type != tv_dest->v_type)
2552 {
2553 // just in case, should be OK
2554 semsg(_(e_expected_str_but_got_str),
2555 vartype_name(dest_type),
2556 vartype_name(tv_dest->v_type));
2557 status = FAIL;
2558 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002559
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002560 if (status == OK && dest_type == VAR_LIST)
2561 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002562 long lidx = (long)tv_idx->vval.v_number;
2563 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002564
2565 if (list == NULL)
2566 {
2567 emsg(_(e_list_not_set));
2568 goto on_error;
2569 }
2570 if (lidx < 0 && list->lv_len + lidx >= 0)
2571 // negative index is relative to the end
2572 lidx = list->lv_len + lidx;
2573 if (lidx < 0 || lidx > list->lv_len)
2574 {
2575 semsg(_(e_listidx), lidx);
2576 goto on_error;
2577 }
2578 if (lidx < list->lv_len)
2579 {
2580 listitem_T *li = list_find(list, lidx);
2581
2582 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002583 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002584 goto on_error;
2585 // overwrite existing list item
2586 clear_tv(&li->li_tv);
2587 li->li_tv = *tv;
2588 }
2589 else
2590 {
2591 if (error_if_locked(list->lv_lock,
2592 e_cannot_change_list))
2593 goto on_error;
2594 // append to list, only fails when out of memory
2595 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002596 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002597 clear_tv(tv);
2598 }
2599 }
2600 else if (status == OK && dest_type == VAR_DICT)
2601 {
2602 char_u *key = tv_idx->vval.v_string;
2603 dict_T *dict = tv_dest->vval.v_dict;
2604 dictitem_T *di;
2605
2606 SOURCING_LNUM = iptr->isn_lnum;
2607 if (dict == NULL)
2608 {
2609 emsg(_(e_dictionary_not_set));
2610 goto on_error;
2611 }
2612 if (key == NULL)
2613 key = (char_u *)"";
2614 di = dict_find(dict, key, -1);
2615 if (di != NULL)
2616 {
2617 if (error_if_locked(di->di_tv.v_lock,
2618 e_cannot_change_dict_item))
2619 goto on_error;
2620 // overwrite existing value
2621 clear_tv(&di->di_tv);
2622 di->di_tv = *tv;
2623 }
2624 else
2625 {
2626 if (error_if_locked(dict->dv_lock,
2627 e_cannot_change_dict))
2628 goto on_error;
2629 // add to dict, only fails when out of memory
2630 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002631 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002632 clear_tv(tv);
2633 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002634 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002635 else if (status == OK && dest_type == VAR_BLOB)
2636 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002637 long lidx = (long)tv_idx->vval.v_number;
2638 blob_T *blob = tv_dest->vval.v_blob;
2639 varnumber_T nr;
2640 int error = FALSE;
2641 int len;
2642
2643 if (blob == NULL)
2644 {
2645 emsg(_(e_blob_not_set));
2646 goto on_error;
2647 }
2648 len = blob_len(blob);
2649 if (lidx < 0 && len + lidx >= 0)
2650 // negative index is relative to the end
2651 lidx = len + lidx;
2652
2653 // Can add one byte at the end.
2654 if (lidx < 0 || lidx > len)
2655 {
2656 semsg(_(e_blobidx), lidx);
2657 goto on_error;
2658 }
2659 if (value_check_lock(blob->bv_lock,
2660 (char_u *)"blob", FALSE))
2661 goto on_error;
2662 nr = tv_get_number_chk(tv, &error);
2663 if (error)
2664 goto on_error;
2665 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002666 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002667 else
2668 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002669 status = FAIL;
2670 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002671 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002672
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002673 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002674 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002675 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002676 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002677 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002678 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002679 goto on_error;
2680 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002681 }
2682 break;
2683
Bram Moolenaar68452172021-04-12 21:21:02 +02002684 // store value in blob range
2685 case ISN_STORERANGE:
2686 {
2687 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2688 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2689 typval_T *tv_dest = STACK_TV_BOT(-1);
2690 int status = OK;
2691
2692 // Stack contains:
2693 // -4 value to be stored
2694 // -3 first index or "none"
2695 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002696 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002697 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002698 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002699 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002700 long n1;
2701 long n2;
2702 int error = FALSE;
2703
2704 SOURCING_LNUM = iptr->isn_lnum;
2705 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2706 if (error)
2707 status = FAIL;
2708 else
2709 {
2710 if (tv_idx2->v_type == VAR_SPECIAL
2711 && tv_idx2->vval.v_number == VVAL_NONE)
2712 n2 = list_len(tv_dest->vval.v_list) - 1;
2713 else
2714 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2715 if (error)
2716 status = FAIL;
2717 else
2718 {
2719 listitem_T *li1 = check_range_index_one(
2720 tv_dest->vval.v_list, &n1, FALSE);
2721
2722 if (li1 == NULL)
2723 status = FAIL;
2724 else
2725 {
2726 status = check_range_index_two(
2727 tv_dest->vval.v_list,
2728 &n1, li1, &n2, FALSE);
2729 if (status != FAIL)
2730 status = list_assign_range(
2731 tv_dest->vval.v_list,
2732 tv->vval.v_list,
2733 n1,
2734 n2,
2735 tv_idx2->v_type == VAR_SPECIAL,
2736 (char_u *)"=",
2737 (char_u *)"[unknown]");
2738 }
2739 }
2740 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002741 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002742 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002743 {
2744 varnumber_T n1;
2745 varnumber_T n2;
2746 int error = FALSE;
2747
2748 n1 = tv_get_number_chk(tv_idx1, &error);
2749 if (error)
2750 status = FAIL;
2751 else
2752 {
2753 if (tv_idx2->v_type == VAR_SPECIAL
2754 && tv_idx2->vval.v_number == VVAL_NONE)
2755 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2756 else
2757 n2 = tv_get_number_chk(tv_idx2, &error);
2758 if (error)
2759 status = FAIL;
2760 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002761 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002762 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002763
2764 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002765 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002766 || check_blob_range(bloblen,
2767 n1, n2, FALSE) == FAIL)
2768 status = FAIL;
2769 else
2770 status = blob_set_range(
2771 tv_dest->vval.v_blob, n1, n2, tv);
2772 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002773 }
2774 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002775 else
2776 {
2777 status = FAIL;
2778 emsg(_(e_blob_required));
2779 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002780
2781 clear_tv(tv_idx1);
2782 clear_tv(tv_idx2);
2783 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002784 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002785 clear_tv(tv);
2786
2787 if (status == FAIL)
2788 goto on_error;
2789 }
2790 break;
2791
Bram Moolenaar0186e582021-01-10 18:33:11 +01002792 // load or store variable or argument from outer scope
2793 case ISN_LOADOUTER:
2794 case ISN_STOREOUTER:
2795 {
2796 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002797 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2798 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002799
2800 while (depth > 1 && outer != NULL)
2801 {
2802 outer = outer->out_up;
2803 --depth;
2804 }
2805 if (outer == NULL)
2806 {
2807 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00002808 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
2809 || ectx->ec_outer_ref == NULL)
2810 // Possibly :def function called from legacy
2811 // context.
2812 emsg(_(e_closure_called_from_invalid_context));
2813 else
2814 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002815 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002816 }
2817 tv = ((typval_T *)outer->out_stack->ga_data)
2818 + outer->out_frame_idx + STACK_FRAME_SIZE
2819 + iptr->isn_arg.outer.outer_idx;
2820 if (iptr->isn_type == ISN_LOADOUTER)
2821 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002822 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002823 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002824 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002825 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002826 }
2827 else
2828 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002829 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002830 clear_tv(tv);
2831 *tv = *STACK_TV_BOT(0);
2832 }
2833 }
2834 break;
2835
Bram Moolenaar752fc692021-01-04 21:57:11 +01002836 // unlet item in list or dict variable
2837 case ISN_UNLETINDEX:
2838 {
2839 typval_T *tv_idx = STACK_TV_BOT(-2);
2840 typval_T *tv_dest = STACK_TV_BOT(-1);
2841 int status = OK;
2842
2843 // Stack contains:
2844 // -2 index
2845 // -1 dict or list
2846 if (tv_dest->v_type == VAR_DICT)
2847 {
2848 // unlet a dict item, index must be a string
2849 if (tv_idx->v_type != VAR_STRING)
2850 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002851 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002852 semsg(_(e_expected_str_but_got_str),
2853 vartype_name(VAR_STRING),
2854 vartype_name(tv_idx->v_type));
2855 status = FAIL;
2856 }
2857 else
2858 {
2859 dict_T *d = tv_dest->vval.v_dict;
2860 char_u *key = tv_idx->vval.v_string;
2861 dictitem_T *di = NULL;
2862
Bram Moolenaar71b76852021-12-17 20:15:38 +00002863 if (d != NULL && value_check_lock(
2864 d->dv_lock, NULL, FALSE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002865 status = FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002866 else
2867 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002868 SOURCING_LNUM = iptr->isn_lnum;
2869 if (key == NULL)
2870 key = (char_u *)"";
2871 if (d != NULL)
2872 di = dict_find(d, key, (int)STRLEN(key));
2873 if (di == NULL)
2874 {
2875 // NULL dict is equivalent to empty dict
2876 semsg(_(e_dictkey), key);
2877 status = FAIL;
2878 }
2879 else if (var_check_fixed(di->di_flags,
2880 NULL, FALSE)
2881 || var_check_ro(di->di_flags,
2882 NULL, FALSE))
2883 status = FAIL;
2884 else
2885 dictitem_remove(d, di);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002886 }
2887 }
2888 }
2889 else if (tv_dest->v_type == VAR_LIST)
2890 {
2891 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002892 SOURCING_LNUM = iptr->isn_lnum;
2893 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002894 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002895 status = FAIL;
2896 }
2897 else
2898 {
2899 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002900 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002901
Bram Moolenaar422085f2021-12-17 20:36:15 +00002902 if (l != NULL && value_check_lock(
2903 l->lv_lock, NULL, FALSE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002904 status = FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002905 else
Bram Moolenaar422085f2021-12-17 20:36:15 +00002906 {
2907 listitem_T *li = list_find(l, n);
2908
2909 if (li == NULL)
2910 {
2911 SOURCING_LNUM = iptr->isn_lnum;
2912 semsg(_(e_listidx), n);
2913 status = FAIL;
2914 }
2915 else if (value_check_lock(li->li_tv.v_lock,
2916 NULL, FALSE))
2917 status = FAIL;
2918 else
2919 listitem_remove(l, li);
2920 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002921 }
2922 }
2923 else
2924 {
2925 status = FAIL;
2926 semsg(_(e_cannot_index_str),
2927 vartype_name(tv_dest->v_type));
2928 }
2929
2930 clear_tv(tv_idx);
2931 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002932 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002933 if (status == FAIL)
2934 goto on_error;
2935 }
2936 break;
2937
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002938 // unlet range of items in list variable
2939 case ISN_UNLETRANGE:
2940 {
2941 // Stack contains:
2942 // -3 index1
2943 // -2 index2
2944 // -1 dict or list
2945 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2946 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2947 typval_T *tv_dest = STACK_TV_BOT(-1);
2948 int status = OK;
2949
2950 if (tv_dest->v_type == VAR_LIST)
2951 {
2952 // indexes must be a number
2953 SOURCING_LNUM = iptr->isn_lnum;
2954 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002955 || (tv_idx2->v_type != VAR_SPECIAL
2956 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002957 {
2958 status = FAIL;
2959 }
2960 else
2961 {
2962 list_T *l = tv_dest->vval.v_list;
2963 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002964 long n2 = tv_idx2->v_type == VAR_SPECIAL
2965 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002966 listitem_T *li;
2967
2968 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002969 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002970 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002971 else
2972 {
2973 if (n1 < 0)
2974 n1 = list_idx_of_item(l, li);
2975 if (n2 < 0)
2976 {
2977 listitem_T *li2 = list_find(l, n2);
2978
2979 if (li2 == NULL)
2980 status = FAIL;
2981 else
2982 n2 = list_idx_of_item(l, li2);
2983 }
2984 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002985 && tv_idx2->v_type != VAR_SPECIAL
2986 && n2 < n1)
2987 {
2988 semsg(_(e_listidx), n2);
2989 status = FAIL;
2990 }
2991 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002992 && list_unlet_range(l, li, NULL, n1,
2993 tv_idx2->v_type != VAR_SPECIAL, n2)
2994 == FAIL)
2995 status = FAIL;
2996 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002997 }
2998 }
2999 else
3000 {
3001 status = FAIL;
3002 SOURCING_LNUM = iptr->isn_lnum;
3003 semsg(_(e_cannot_index_str),
3004 vartype_name(tv_dest->v_type));
3005 }
3006
3007 clear_tv(tv_idx1);
3008 clear_tv(tv_idx2);
3009 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003010 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003011 if (status == FAIL)
3012 goto on_error;
3013 }
3014 break;
3015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 // push constant
3017 case ISN_PUSHNR:
3018 case ISN_PUSHBOOL:
3019 case ISN_PUSHSPEC:
3020 case ISN_PUSHF:
3021 case ISN_PUSHS:
3022 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003023 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003024 case ISN_PUSHCHANNEL:
3025 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003026 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003027 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003029 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003030 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 switch (iptr->isn_type)
3032 {
3033 case ISN_PUSHNR:
3034 tv->v_type = VAR_NUMBER;
3035 tv->vval.v_number = iptr->isn_arg.number;
3036 break;
3037 case ISN_PUSHBOOL:
3038 tv->v_type = VAR_BOOL;
3039 tv->vval.v_number = iptr->isn_arg.number;
3040 break;
3041 case ISN_PUSHSPEC:
3042 tv->v_type = VAR_SPECIAL;
3043 tv->vval.v_number = iptr->isn_arg.number;
3044 break;
3045#ifdef FEAT_FLOAT
3046 case ISN_PUSHF:
3047 tv->v_type = VAR_FLOAT;
3048 tv->vval.v_float = iptr->isn_arg.fnumber;
3049 break;
3050#endif
3051 case ISN_PUSHBLOB:
3052 blob_copy(iptr->isn_arg.blob, tv);
3053 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003054 case ISN_PUSHFUNC:
3055 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003056 if (iptr->isn_arg.string == NULL)
3057 tv->vval.v_string = NULL;
3058 else
3059 tv->vval.v_string =
3060 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003061 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003062 case ISN_PUSHCHANNEL:
3063#ifdef FEAT_JOB_CHANNEL
3064 tv->v_type = VAR_CHANNEL;
3065 tv->vval.v_channel = iptr->isn_arg.channel;
3066 if (tv->vval.v_channel != NULL)
3067 ++tv->vval.v_channel->ch_refcount;
3068#endif
3069 break;
3070 case ISN_PUSHJOB:
3071#ifdef FEAT_JOB_CHANNEL
3072 tv->v_type = VAR_JOB;
3073 tv->vval.v_job = iptr->isn_arg.job;
3074 if (tv->vval.v_job != NULL)
3075 ++tv->vval.v_job->jv_refcount;
3076#endif
3077 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 default:
3079 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003080 tv->vval.v_string = vim_strsave(
3081 iptr->isn_arg.string == NULL
3082 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 }
3084 break;
3085
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003086 case ISN_UNLET:
3087 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3088 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003089 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003090 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003091 case ISN_UNLETENV:
3092 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3093 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003094
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003095 case ISN_LOCKUNLOCK:
3096 {
3097 typval_T *lval_root_save = lval_root;
3098 int res;
3099
3100 // Stack has the local variable, argument the whole :lock
3101 // or :unlock command, like ISN_EXEC.
3102 --ectx->ec_stack.ga_len;
3103 lval_root = STACK_TV_BOT(0);
3104 res = exec_command(iptr);
3105 clear_tv(lval_root);
3106 lval_root = lval_root_save;
3107 if (res == FAIL)
3108 goto on_error;
3109 }
3110 break;
3111
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003112 case ISN_LOCKCONST:
3113 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3114 break;
3115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 // create a list from items on the stack; uses a single allocation
3117 // for the list header and the items
3118 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003119 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003120 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 break;
3122
3123 // create a dict from items on the stack
3124 case ISN_NEWDICT:
3125 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003126 int count = iptr->isn_arg.number;
3127 dict_T *dict = dict_alloc();
3128 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003129 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003132 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003133 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 for (idx = 0; idx < count; ++idx)
3135 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003136 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003138 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003139 key = tv->vval.v_string == NULL
3140 ? (char_u *)"" : tv->vval.v_string;
3141 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003142 if (item != NULL)
3143 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003144 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003145 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003146 dict_unref(dict);
3147 goto on_error;
3148 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003149 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003151 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003152 {
3153 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003154 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3157 item->di_tv.v_lock = 0;
3158 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003159 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003160 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003161 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003162 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 }
3165
3166 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003167 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003168 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003169 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003171 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 tv = STACK_TV_BOT(-1);
3173 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003174 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 tv->vval.v_dict = dict;
3176 ++dict->dv_refcount;
3177 }
3178 break;
3179
3180 // call a :def function
3181 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003182 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003183 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3184 NULL,
3185 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003186 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003187 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 break;
3189
3190 // call a builtin function
3191 case ISN_BCALL:
3192 SOURCING_LNUM = iptr->isn_lnum;
3193 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3194 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003196 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 break;
3198
3199 // call a funcref or partial
3200 case ISN_PCALL:
3201 {
3202 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3203 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003204 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205
3206 SOURCING_LNUM = iptr->isn_lnum;
3207 if (pfunc->cpf_top)
3208 {
3209 // funcref is above the arguments
3210 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3211 }
3212 else
3213 {
3214 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003215 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003216 partial_tv = *STACK_TV_BOT(0);
3217 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003219 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003220 if (tv == &partial_tv)
3221 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003223 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
3225 break;
3226
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003227 case ISN_PCALL_END:
3228 // PCALL finished, arguments have been consumed and replaced by
3229 // the return value. Now clear the funcref from the stack,
3230 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003231 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003232 clear_tv(STACK_TV_BOT(-1));
3233 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3234 break;
3235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 // call a user defined function or funcref/partial
3237 case ISN_UCALL:
3238 {
3239 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3240
3241 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003242 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003243 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003244 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 }
3246 break;
3247
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003248 // return from a :def function call without a value
3249 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003250 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003251 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003252 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003253 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003254 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003255 tv->vval.v_number = 0;
3256 tv->v_lock = 0;
3257 // FALLTHROUGH
3258
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003259 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 case ISN_RETURN:
3261 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003262 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003263 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003264
3265 if (trystack->ga_len > 0)
3266 trycmd = ((trycmd_T *)trystack->ga_data)
3267 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003268 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003269 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003271 // jump to ":finally" or ":endtry"
3272 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003273 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003274 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003275 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 trycmd->tcd_return = TRUE;
3277 }
3278 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003279 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 }
3281 break;
3282
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003283 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 case ISN_FUNCREF:
3285 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003286 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003287 ufunc_T *ufunc;
3288 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003291 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003292 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003293 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003294 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003295 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003296 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003297 if (funcref->fr_func_name == NULL)
3298 {
3299 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3300 + funcref->fr_dfunc_idx;
3301
3302 ufunc = pt_dfunc->df_ufunc;
3303 }
3304 else
3305 {
3306 ufunc = find_func(funcref->fr_func_name, FALSE, NULL);
3307 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003308 if (ufunc == NULL)
3309 {
3310 SOURCING_LNUM = iptr->isn_lnum;
3311 emsg(_(e_function_reference_invalid));
3312 goto theend;
3313 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003314 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003315 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003317 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 tv->vval.v_partial = pt;
3319 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003320 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 }
3322 break;
3323
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003324 // Create a global function from a lambda.
3325 case ISN_NEWFUNC:
3326 {
3327 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3328
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003329 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003330 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003331 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003332 }
3333 break;
3334
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003335 // List functions
3336 case ISN_DEF:
3337 if (iptr->isn_arg.string == NULL)
3338 list_functions(NULL);
3339 else
3340 {
3341 exarg_T ea;
3342
3343 CLEAR_FIELD(ea);
3344 ea.cmd = ea.arg = iptr->isn_arg.string;
3345 define_function(&ea, NULL);
3346 }
3347 break;
3348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 // jump if a condition is met
3350 case ISN_JUMP:
3351 {
3352 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003353 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 int jump = TRUE;
3355
3356 if (when != JUMP_ALWAYS)
3357 {
3358 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003359 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003360 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003361 || when == JUMP_IF_COND_TRUE)
3362 {
3363 SOURCING_LNUM = iptr->isn_lnum;
3364 jump = tv_get_bool_chk(tv, &error);
3365 if (error)
3366 goto on_error;
3367 }
3368 else
3369 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003371 || when == JUMP_AND_KEEP_IF_FALSE
3372 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003374 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 {
3376 // drop the value from the stack
3377 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003378 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 }
3380 }
3381 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003382 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 }
3384 break;
3385
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003386 // Jump if an argument with a default value was already set and not
3387 // v:none.
3388 case ISN_JUMP_IF_ARG_SET:
3389 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3390 if (tv->v_type != VAR_UNKNOWN
3391 && !(tv->v_type == VAR_SPECIAL
3392 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003393 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003394 break;
3395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 // top of a for loop
3397 case ISN_FOR:
3398 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003399 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 typval_T *idxtv =
3401 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3402
Bram Moolenaar35578162021-08-02 19:10:38 +02003403 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003404 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003405 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003406 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003407 list_T *list = ltv->vval.v_list;
3408
3409 // push the next item from the list
3410 ++idxtv->vval.v_number;
3411 if (list == NULL
3412 || idxtv->vval.v_number >= list->lv_len)
3413 {
3414 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003415 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3416 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003417 }
3418 else if (list->lv_first == &range_list_item)
3419 {
3420 // non-materialized range() list
3421 tv = STACK_TV_BOT(0);
3422 tv->v_type = VAR_NUMBER;
3423 tv->v_lock = 0;
3424 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003426 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003427 }
3428 else
3429 {
3430 listitem_T *li = list_find(list,
3431 idxtv->vval.v_number);
3432
3433 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003434 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003435 }
3436 }
3437 else if (ltv->v_type == VAR_STRING)
3438 {
3439 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003440
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003441 // The index is for the last byte of the previous
3442 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003443 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003444 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003445 {
3446 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003447 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3448 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003449 }
3450 else
3451 {
3452 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3453
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003454 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003455 tv = STACK_TV_BOT(0);
3456 tv->v_type = VAR_STRING;
3457 tv->vval.v_string = vim_strnsave(
3458 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003459 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003460 idxtv->vval.v_number += clen - 1;
3461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003463 else if (ltv->v_type == VAR_BLOB)
3464 {
3465 blob_T *blob = ltv->vval.v_blob;
3466
3467 // When we get here the first time make a copy of the
3468 // blob, so that the iteration still works when it is
3469 // changed.
3470 if (idxtv->vval.v_number == -1 && blob != NULL)
3471 {
3472 blob_copy(blob, ltv);
3473 blob_unref(blob);
3474 blob = ltv->vval.v_blob;
3475 }
3476
3477 // The index is for the previous byte.
3478 ++idxtv->vval.v_number;
3479 if (blob == NULL
3480 || idxtv->vval.v_number >= blob_len(blob))
3481 {
3482 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003483 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3484 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003485 }
3486 else
3487 {
3488 // Push the next byte from the blob.
3489 tv = STACK_TV_BOT(0);
3490 tv->v_type = VAR_NUMBER;
3491 tv->vval.v_number = blob_get(blob,
3492 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003493 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003494 }
3495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 else
3497 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003498 semsg(_(e_for_loop_on_str_not_supported),
3499 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003500 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 }
3502 }
3503 break;
3504
3505 // start of ":try" block
3506 case ISN_TRY:
3507 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003508 trycmd_T *trycmd = NULL;
3509
Bram Moolenaar35578162021-08-02 19:10:38 +02003510 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003511 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3513 + ectx->ec_trystack.ga_len;
3514 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003516 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003517 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3518 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003519 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003520 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003521 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003522 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003523 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003524 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 }
3526 break;
3527
3528 case ISN_PUSHEXC:
3529 if (current_exception == NULL)
3530 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003531 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003533 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003535 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003536 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003538 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003540 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 tv->vval.v_string = vim_strsave(
3542 (char_u *)current_exception->value);
3543 break;
3544
3545 case ISN_CATCH:
3546 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003547 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548
Bram Moolenaar4c137212021-04-19 16:48:48 +02003549 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 if (trystack->ga_len > 0)
3551 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003552 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 + trystack->ga_len - 1;
3554 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003555 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 }
3557 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003558 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 catch_exception(current_exception);
3560 }
3561 break;
3562
Bram Moolenaarc150c092021-02-13 15:02:46 +01003563 case ISN_TRYCONT:
3564 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003565 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003566 trycont_T *trycont = &iptr->isn_arg.trycont;
3567 int i;
3568 trycmd_T *trycmd;
3569 int iidx = trycont->tct_where;
3570
3571 if (trystack->ga_len < trycont->tct_levels)
3572 {
3573 siemsg("TRYCONT: expected %d levels, found %d",
3574 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003575 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003576 }
3577 // Make :endtry jump to any outer try block and the last
3578 // :endtry inside the loop to the loop start.
3579 for (i = trycont->tct_levels; i > 0; --i)
3580 {
3581 trycmd = ((trycmd_T *)trystack->ga_data)
3582 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003583 // Add one to tcd_cont to be able to jump to
3584 // instruction with index zero.
3585 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003586 iidx = trycmd->tcd_finally_idx == 0
3587 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003588 }
3589 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003590 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003591 }
3592 break;
3593
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003594 case ISN_FINALLY:
3595 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003596 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003597 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3598 + trystack->ga_len - 1;
3599
3600 // Reset the index to avoid a return statement jumps here
3601 // again.
3602 trycmd->tcd_finally_idx = 0;
3603 break;
3604 }
3605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 // end of ":try" block
3607 case ISN_ENDTRY:
3608 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
3611 if (trystack->ga_len > 0)
3612 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003613 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 --trystack->ga_len;
3616 --trylevel;
3617 trycmd = ((trycmd_T *)trystack->ga_data)
3618 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003619 if (trycmd->tcd_did_throw)
3620 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003621 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 {
3623 // discard the exception
3624 if (caught_stack == current_exception)
3625 caught_stack = caught_stack->caught;
3626 discard_current_exception();
3627 }
3628
3629 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003630 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003631
Bram Moolenaar4c137212021-04-19 16:48:48 +02003632 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003633 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003634 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003635 clear_tv(STACK_TV_BOT(0));
3636 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003637 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003638 // handling :continue: jump to outer try block or
3639 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003640 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 }
3642 }
3643 break;
3644
3645 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003646 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003647 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003648
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003649 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3650 {
3651 // throwing an exception while using "silent!" causes
3652 // the function to abort but not display an error.
3653 tv = STACK_TV_BOT(-1);
3654 clear_tv(tv);
3655 tv->v_type = VAR_NUMBER;
3656 tv->vval.v_number = 0;
3657 goto done;
3658 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003659 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003660 tv = STACK_TV_BOT(0);
3661 if (tv->vval.v_string == NULL
3662 || *skipwhite(tv->vval.v_string) == NUL)
3663 {
3664 vim_free(tv->vval.v_string);
3665 SOURCING_LNUM = iptr->isn_lnum;
3666 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003667 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003668 }
3669
3670 // Inside a "catch" we need to first discard the caught
3671 // exception.
3672 if (trystack->ga_len > 0)
3673 {
3674 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3675 + trystack->ga_len - 1;
3676 if (trycmd->tcd_caught && current_exception != NULL)
3677 {
3678 // discard the exception
3679 if (caught_stack == current_exception)
3680 caught_stack = caught_stack->caught;
3681 discard_current_exception();
3682 trycmd->tcd_caught = FALSE;
3683 }
3684 }
3685
3686 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3687 == FAIL)
3688 {
3689 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003690 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003691 }
3692 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 break;
3695
3696 // compare with special values
3697 case ISN_COMPAREBOOL:
3698 case ISN_COMPARESPECIAL:
3699 {
3700 typval_T *tv1 = STACK_TV_BOT(-2);
3701 typval_T *tv2 = STACK_TV_BOT(-1);
3702 varnumber_T arg1 = tv1->vval.v_number;
3703 varnumber_T arg2 = tv2->vval.v_number;
3704 int res;
3705
3706 switch (iptr->isn_arg.op.op_type)
3707 {
3708 case EXPR_EQUAL: res = arg1 == arg2; break;
3709 case EXPR_NEQUAL: res = arg1 != arg2; break;
3710 default: res = 0; break;
3711 }
3712
Bram Moolenaar4c137212021-04-19 16:48:48 +02003713 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 tv1->v_type = VAR_BOOL;
3715 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3716 }
3717 break;
3718
3719 // Operation with two number arguments
3720 case ISN_OPNR:
3721 case ISN_COMPARENR:
3722 {
3723 typval_T *tv1 = STACK_TV_BOT(-2);
3724 typval_T *tv2 = STACK_TV_BOT(-1);
3725 varnumber_T arg1 = tv1->vval.v_number;
3726 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003727 varnumber_T res = 0;
3728 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729
3730 switch (iptr->isn_arg.op.op_type)
3731 {
3732 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003733 case EXPR_DIV: if (arg2 == 0)
3734 div_zero = TRUE;
3735 else
3736 res = arg1 / arg2;
3737 break;
3738 case EXPR_REM: if (arg2 == 0)
3739 div_zero = TRUE;
3740 else
3741 res = arg1 % arg2;
3742 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 case EXPR_SUB: res = arg1 - arg2; break;
3744 case EXPR_ADD: res = arg1 + arg2; break;
3745
3746 case EXPR_EQUAL: res = arg1 == arg2; break;
3747 case EXPR_NEQUAL: res = arg1 != arg2; break;
3748 case EXPR_GREATER: res = arg1 > arg2; break;
3749 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3750 case EXPR_SMALLER: res = arg1 < arg2; break;
3751 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003752 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 }
3754
Bram Moolenaar4c137212021-04-19 16:48:48 +02003755 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 if (iptr->isn_type == ISN_COMPARENR)
3757 {
3758 tv1->v_type = VAR_BOOL;
3759 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3760 }
3761 else
3762 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003763 if (div_zero)
3764 {
3765 SOURCING_LNUM = iptr->isn_lnum;
3766 emsg(_(e_divide_by_zero));
3767 goto on_error;
3768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 }
3770 break;
3771
3772 // Computation with two float arguments
3773 case ISN_OPFLOAT:
3774 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003775#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 {
3777 typval_T *tv1 = STACK_TV_BOT(-2);
3778 typval_T *tv2 = STACK_TV_BOT(-1);
3779 float_T arg1 = tv1->vval.v_float;
3780 float_T arg2 = tv2->vval.v_float;
3781 float_T res = 0;
3782 int cmp = FALSE;
3783
3784 switch (iptr->isn_arg.op.op_type)
3785 {
3786 case EXPR_MULT: res = arg1 * arg2; break;
3787 case EXPR_DIV: res = arg1 / arg2; break;
3788 case EXPR_SUB: res = arg1 - arg2; break;
3789 case EXPR_ADD: res = arg1 + arg2; break;
3790
3791 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3792 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3793 case EXPR_GREATER: cmp = arg1 > arg2; break;
3794 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3795 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3796 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3797 default: cmp = 0; break;
3798 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003799 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 if (iptr->isn_type == ISN_COMPAREFLOAT)
3801 {
3802 tv1->v_type = VAR_BOOL;
3803 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3804 }
3805 else
3806 tv1->vval.v_float = res;
3807 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003808#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 break;
3810
3811 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00003812 case ISN_COMPAREDICT:
3813 case ISN_COMPAREFUNC:
3814 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 case ISN_COMPAREBLOB:
3816 {
3817 typval_T *tv1 = STACK_TV_BOT(-2);
3818 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00003819 exprtype_T exprtype = iptr->isn_arg.op.op_type;
3820 int ic = iptr->isn_arg.op.op_ic;
3821 int res = FALSE;
3822 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823
Bram Moolenaar265f8112021-12-19 12:33:05 +00003824 SOURCING_LNUM = iptr->isn_lnum;
3825 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00003827 status = typval_compare_list(tv1, tv2,
3828 exprtype, ic, &res);
3829 }
3830 else if (iptr->isn_type == ISN_COMPAREDICT)
3831 {
3832 status = typval_compare_dict(tv1, tv2,
3833 exprtype, ic, &res);
3834 }
3835 else if (iptr->isn_type == ISN_COMPAREFUNC)
3836 {
3837 status = typval_compare_func(tv1, tv2,
3838 exprtype, ic, &res);
3839 }
3840 else if (iptr->isn_type == ISN_COMPARESTRING)
3841 {
3842 status = typval_compare_string(tv1, tv2,
3843 exprtype, ic, &res);
3844 }
3845 else
3846 {
3847 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003849 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 clear_tv(tv1);
3851 clear_tv(tv2);
3852 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003853 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3854 if (status == FAIL)
3855 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 }
3857 break;
3858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 case ISN_COMPAREANY:
3860 {
3861 typval_T *tv1 = STACK_TV_BOT(-2);
3862 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003863 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003865 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866
Bram Moolenaareb26f432020-09-14 16:50:05 +02003867 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003868 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003870 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003871 if (status == FAIL)
3872 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 }
3874 break;
3875
3876 case ISN_ADDLIST:
3877 case ISN_ADDBLOB:
3878 {
3879 typval_T *tv1 = STACK_TV_BOT(-2);
3880 typval_T *tv2 = STACK_TV_BOT(-1);
3881
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003882 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003884 {
3885 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3886 && tv1->vval.v_list != NULL)
3887 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3888 NULL);
3889 else
3890 eval_addlist(tv1, tv2);
3891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 else
3893 eval_addblob(tv1, tv2);
3894 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003895 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 }
3897 break;
3898
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003899 case ISN_LISTAPPEND:
3900 {
3901 typval_T *tv1 = STACK_TV_BOT(-2);
3902 typval_T *tv2 = STACK_TV_BOT(-1);
3903 list_T *l = tv1->vval.v_list;
3904
3905 // add an item to a list
3906 if (l == NULL)
3907 {
3908 SOURCING_LNUM = iptr->isn_lnum;
3909 emsg(_(e_cannot_add_to_null_list));
3910 goto on_error;
3911 }
3912 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003913 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003914 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003915 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003916 }
3917 break;
3918
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003919 case ISN_BLOBAPPEND:
3920 {
3921 typval_T *tv1 = STACK_TV_BOT(-2);
3922 typval_T *tv2 = STACK_TV_BOT(-1);
3923 blob_T *b = tv1->vval.v_blob;
3924 int error = FALSE;
3925 varnumber_T n;
3926
3927 // add a number to a blob
3928 if (b == NULL)
3929 {
3930 SOURCING_LNUM = iptr->isn_lnum;
3931 emsg(_(e_cannot_add_to_null_blob));
3932 goto on_error;
3933 }
3934 n = tv_get_number_chk(tv2, &error);
3935 if (error)
3936 goto on_error;
3937 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003938 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003939 }
3940 break;
3941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 // Computation with two arguments of unknown type
3943 case ISN_OPANY:
3944 {
3945 typval_T *tv1 = STACK_TV_BOT(-2);
3946 typval_T *tv2 = STACK_TV_BOT(-1);
3947 varnumber_T n1, n2;
3948#ifdef FEAT_FLOAT
3949 float_T f1 = 0, f2 = 0;
3950#endif
3951 int error = FALSE;
3952
3953 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3954 {
3955 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3956 {
3957 eval_addlist(tv1, tv2);
3958 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003959 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 break;
3961 }
3962 else if (tv1->v_type == VAR_BLOB
3963 && tv2->v_type == VAR_BLOB)
3964 {
3965 eval_addblob(tv1, tv2);
3966 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003967 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 break;
3969 }
3970 }
3971#ifdef FEAT_FLOAT
3972 if (tv1->v_type == VAR_FLOAT)
3973 {
3974 f1 = tv1->vval.v_float;
3975 n1 = 0;
3976 }
3977 else
3978#endif
3979 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003980 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 n1 = tv_get_number_chk(tv1, &error);
3982 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003983 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984#ifdef FEAT_FLOAT
3985 if (tv2->v_type == VAR_FLOAT)
3986 f1 = n1;
3987#endif
3988 }
3989#ifdef FEAT_FLOAT
3990 if (tv2->v_type == VAR_FLOAT)
3991 {
3992 f2 = tv2->vval.v_float;
3993 n2 = 0;
3994 }
3995 else
3996#endif
3997 {
3998 n2 = tv_get_number_chk(tv2, &error);
3999 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004000 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001#ifdef FEAT_FLOAT
4002 if (tv1->v_type == VAR_FLOAT)
4003 f2 = n2;
4004#endif
4005 }
4006#ifdef FEAT_FLOAT
4007 // if there is a float on either side the result is a float
4008 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4009 {
4010 switch (iptr->isn_arg.op.op_type)
4011 {
4012 case EXPR_MULT: f1 = f1 * f2; break;
4013 case EXPR_DIV: f1 = f1 / f2; break;
4014 case EXPR_SUB: f1 = f1 - f2; break;
4015 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004016 default: SOURCING_LNUM = iptr->isn_lnum;
4017 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004018 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 }
4020 clear_tv(tv1);
4021 clear_tv(tv2);
4022 tv1->v_type = VAR_FLOAT;
4023 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004024 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 }
4026 else
4027#endif
4028 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004029 int failed = FALSE;
4030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 switch (iptr->isn_arg.op.op_type)
4032 {
4033 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004034 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4035 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004036 goto on_error;
4037 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 case EXPR_SUB: n1 = n1 - n2; break;
4039 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004040 default: n1 = num_modulus(n1, n2, &failed);
4041 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004042 goto on_error;
4043 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 }
4045 clear_tv(tv1);
4046 clear_tv(tv2);
4047 tv1->v_type = VAR_NUMBER;
4048 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004049 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 }
4051 }
4052 break;
4053
4054 case ISN_CONCAT:
4055 {
4056 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4057 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4058 char_u *res;
4059
4060 res = concat_str(str1, str2);
4061 clear_tv(STACK_TV_BOT(-2));
4062 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004063 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 STACK_TV_BOT(-1)->vval.v_string = res;
4065 }
4066 break;
4067
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004068 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004069 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004070 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004071 int is_slice = iptr->isn_type == ISN_STRSLICE;
4072 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004073 char_u *res;
4074
4075 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004076 // string slice: string is at stack-3, first index at
4077 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004078 if (is_slice)
4079 {
4080 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004081 n1 = tv->vval.v_number;
4082 }
4083
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004084 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004085 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004086
Bram Moolenaar4c137212021-04-19 16:48:48 +02004087 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004088 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004089 if (is_slice)
4090 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004091 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004092 else
4093 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004094 // single character (including composing characters).
4095 // If the index is too big or negative the result is
4096 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004097 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004098 vim_free(tv->vval.v_string);
4099 tv->vval.v_string = res;
4100 }
4101 break;
4102
4103 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004104 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004105 case ISN_BLOBINDEX:
4106 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004108 int is_slice = iptr->isn_type == ISN_LISTSLICE
4109 || iptr->isn_type == ISN_BLOBSLICE;
4110 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4111 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004112 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004113 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114
4115 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004116 // list slice: list is at stack-3, indexes at stack-2 and
4117 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004118 // Same for blob.
4119 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120
4121 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004122 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004124
4125 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 {
Bram Moolenaared591872020-08-15 22:14:53 +02004127 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004128 n1 = tv->vval.v_number;
4129 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 }
Bram Moolenaared591872020-08-15 22:14:53 +02004131
Bram Moolenaar4c137212021-04-19 16:48:48 +02004132 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004133 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004134 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004135 if (is_blob)
4136 {
4137 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4138 n1, n2, FALSE, tv) == FAIL)
4139 goto on_error;
4140 }
4141 else
4142 {
4143 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4144 n1, n2, FALSE, tv, TRUE) == FAIL)
4145 goto on_error;
4146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 }
4148 break;
4149
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004150 case ISN_ANYINDEX:
4151 case ISN_ANYSLICE:
4152 {
4153 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4154 typval_T *var1, *var2;
4155 int res;
4156
4157 // index: composite is at stack-2, index at stack-1
4158 // slice: composite is at stack-3, indexes at stack-2 and
4159 // stack-1
4160 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004161 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4163 goto on_error;
4164 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4165 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004166 res = eval_index_inner(tv, is_slice, var1, var2,
4167 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004168 clear_tv(var1);
4169 if (is_slice)
4170 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004171 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004172 if (res == FAIL)
4173 goto on_error;
4174 }
4175 break;
4176
Bram Moolenaar9af78762020-06-16 11:34:42 +02004177 case ISN_SLICE:
4178 {
4179 list_T *list;
4180 int count = iptr->isn_arg.number;
4181
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004182 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004183 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004184 list = tv->vval.v_list;
4185
4186 // no error for short list, expect it to be checked earlier
4187 if (list != NULL && list->lv_len >= count)
4188 {
4189 list_T *newlist = list_slice(list,
4190 count, list->lv_len - 1);
4191
4192 if (newlist != NULL)
4193 {
4194 list_unref(list);
4195 tv->vval.v_list = newlist;
4196 ++newlist->lv_refcount;
4197 }
4198 }
4199 }
4200 break;
4201
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004202 case ISN_GETITEM:
4203 {
4204 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004205 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004206
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004207 // Get list item: list is at stack-1, push item.
4208 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004209 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4210 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004211
Bram Moolenaar35578162021-08-02 19:10:38 +02004212 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004213 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004214 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004215 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004216
4217 // Useful when used in unpack assignment. Reset at
4218 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004219 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004220 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004221 }
4222 break;
4223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 case ISN_MEMBER:
4225 {
4226 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004227 char_u *key;
4228 dictitem_T *di;
4229
4230 // dict member: dict is at stack-2, key at stack-1
4231 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004232 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004233 dict = tv->vval.v_dict;
4234
4235 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004236 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004237 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004238 if (key == NULL)
4239 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004240
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004241 if ((di = dict_find(dict, key, -1)) == NULL)
4242 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004243 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004244 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004245
4246 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004247 // stack contents makes sense and the dict stack is
4248 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004249 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004250 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004251 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004252 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004253 tv->v_type = VAR_NUMBER;
4254 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004255 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004256 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004257 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004258 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004259 // Put the dict used on the dict stack, it might be used by
4260 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004261 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004262 if (dict_stack_save(tv) == FAIL)
4263 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004264 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004265 }
4266 break;
4267
4268 // dict member with string key
4269 case ISN_STRINGMEMBER:
4270 {
4271 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 dictitem_T *di;
4273
4274 tv = STACK_TV_BOT(-1);
4275 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4276 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004277 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004279 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 }
4281 dict = tv->vval.v_dict;
4282
4283 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4284 == NULL)
4285 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004286 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004288 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004290 // Put the dict used on the dict stack, it might be used by
4291 // a dict function later.
4292 if (dict_stack_save(tv) == FAIL)
4293 goto on_fatal_error;
4294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004296 }
4297 break;
4298
4299 case ISN_CLEARDICT:
4300 dict_stack_drop();
4301 break;
4302
4303 case ISN_USEDICT:
4304 {
4305 typval_T *dict_tv = dict_stack_get_tv();
4306
4307 // Turn "dict.Func" into a partial for "Func" bound to
4308 // "dict". Don't do this when "Func" is already a partial
4309 // that was bound explicitly (pt_auto is FALSE).
4310 tv = STACK_TV_BOT(-1);
4311 if (dict_tv != NULL
4312 && dict_tv->v_type == VAR_DICT
4313 && dict_tv->vval.v_dict != NULL
4314 && (tv->v_type == VAR_FUNC
4315 || (tv->v_type == VAR_PARTIAL
4316 && (tv->vval.v_partial->pt_auto
4317 || tv->vval.v_partial->pt_dict == NULL))))
4318 dict_tv->vval.v_dict =
4319 make_partial(dict_tv->vval.v_dict, tv);
4320 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 }
4322 break;
4323
4324 case ISN_NEGATENR:
4325 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004326 if (tv->v_type != VAR_NUMBER
4327#ifdef FEAT_FLOAT
4328 && tv->v_type != VAR_FLOAT
4329#endif
4330 )
4331 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004333 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004334 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004335 }
4336#ifdef FEAT_FLOAT
4337 if (tv->v_type == VAR_FLOAT)
4338 tv->vval.v_float = -tv->vval.v_float;
4339 else
4340#endif
4341 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 break;
4343
4344 case ISN_CHECKNR:
4345 {
4346 int error = FALSE;
4347
4348 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004349 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004351 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 (void)tv_get_number_chk(tv, &error);
4353 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004354 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 }
4356 break;
4357
4358 case ISN_CHECKTYPE:
4359 {
4360 checktype_T *ct = &iptr->isn_arg.type;
4361
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004362 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004363 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004364 if (!ectx->ec_where.wt_variable)
4365 ectx->ec_where.wt_index = ct->ct_arg_idx;
4366 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4367 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004368 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004369 if (!ectx->ec_where.wt_variable)
4370 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004371
4372 // number 0 is FALSE, number 1 is TRUE
4373 if (tv->v_type == VAR_NUMBER
4374 && ct->ct_type->tt_type == VAR_BOOL
4375 && (tv->vval.v_number == 0
4376 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004378 tv->v_type = VAR_BOOL;
4379 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004380 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 }
4382 }
4383 break;
4384
Bram Moolenaar9af78762020-06-16 11:34:42 +02004385 case ISN_CHECKLEN:
4386 {
4387 int min_len = iptr->isn_arg.checklen.cl_min_len;
4388 list_T *list = NULL;
4389
4390 tv = STACK_TV_BOT(-1);
4391 if (tv->v_type == VAR_LIST)
4392 list = tv->vval.v_list;
4393 if (list == NULL || list->lv_len < min_len
4394 || (list->lv_len > min_len
4395 && !iptr->isn_arg.checklen.cl_more_OK))
4396 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004397 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004398 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004399 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004400 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004401 }
4402 }
4403 break;
4404
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004405 case ISN_SETTYPE:
4406 {
4407 checktype_T *ct = &iptr->isn_arg.type;
4408
4409 tv = STACK_TV_BOT(-1);
4410 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4411 {
4412 free_type(tv->vval.v_dict->dv_type);
4413 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4414 }
4415 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4416 {
4417 free_type(tv->vval.v_list->lv_type);
4418 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4419 }
4420 }
4421 break;
4422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004424 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 {
4426 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004427 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004429 if (iptr->isn_type == ISN_2BOOL)
4430 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004431 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004432 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004433 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004434 n = !n;
4435 }
4436 else
4437 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004438 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004439 SOURCING_LNUM = iptr->isn_lnum;
4440 n = tv_get_bool_chk(tv, &error);
4441 if (error)
4442 goto on_error;
4443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 clear_tv(tv);
4445 tv->v_type = VAR_BOOL;
4446 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4447 }
4448 break;
4449
4450 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004451 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004452 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004453 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4454 iptr->isn_type == ISN_2STRING_ANY,
4455 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004456 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 break;
4458
Bram Moolenaar08597872020-12-10 19:43:40 +01004459 case ISN_RANGE:
4460 {
4461 exarg_T ea;
4462 char *errormsg;
4463
Bram Moolenaarece0b872021-01-08 20:40:45 +01004464 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004465 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004466 ea.addr_type = ADDR_LINES;
4467 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004468 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004469 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004470 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004471
Bram Moolenaar35578162021-08-02 19:10:38 +02004472 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004473 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004474 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004475 tv = STACK_TV_BOT(-1);
4476 tv->v_type = VAR_NUMBER;
4477 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004478 if (ea.addr_count == 0)
4479 tv->vval.v_number = curwin->w_cursor.lnum;
4480 else
4481 tv->vval.v_number = ea.line2;
4482 }
4483 break;
4484
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004485 case ISN_PUT:
4486 {
4487 int regname = iptr->isn_arg.put.put_regname;
4488 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4489 char_u *expr = NULL;
4490 int dir = FORWARD;
4491
Bram Moolenaar08597872020-12-10 19:43:40 +01004492 if (lnum < -2)
4493 {
4494 // line number was put on the stack by ISN_RANGE
4495 tv = STACK_TV_BOT(-1);
4496 curwin->w_cursor.lnum = tv->vval.v_number;
4497 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4498 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004499 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004500 }
4501 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004502 // :put! above cursor
4503 dir = BACKWARD;
4504 else if (lnum >= 0)
4505 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004506
4507 if (regname == '=')
4508 {
4509 tv = STACK_TV_BOT(-1);
4510 if (tv->v_type == VAR_STRING)
4511 expr = tv->vval.v_string;
4512 else
4513 {
4514 expr = typval2string(tv, TRUE); // allocates value
4515 clear_tv(tv);
4516 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004517 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004518 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004519 check_cursor();
4520 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4521 vim_free(expr);
4522 }
4523 break;
4524
Bram Moolenaar02194d22020-10-24 23:08:38 +02004525 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004526 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4527 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4528 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4529 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004530 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4531 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004532 break;
4533
Bram Moolenaar02194d22020-10-24 23:08:38 +02004534 case ISN_CMDMOD_REV:
4535 // filter regprog is owned by the instruction, don't free it
4536 cmdmod.cmod_filter_regmatch.regprog = NULL;
4537 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004538 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4539 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004540 break;
4541
Bram Moolenaar792f7862020-11-23 08:31:18 +01004542 case ISN_UNPACK:
4543 {
4544 int count = iptr->isn_arg.unpack.unp_count;
4545 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4546 list_T *l;
4547 listitem_T *li;
4548 int i;
4549
4550 // Check there is a valid list to unpack.
4551 tv = STACK_TV_BOT(-1);
4552 if (tv->v_type != VAR_LIST)
4553 {
4554 SOURCING_LNUM = iptr->isn_lnum;
4555 emsg(_(e_for_argument_must_be_sequence_of_lists));
4556 goto on_error;
4557 }
4558 l = tv->vval.v_list;
4559 if (l == NULL
4560 || l->lv_len < (semicolon ? count - 1 : count))
4561 {
4562 SOURCING_LNUM = iptr->isn_lnum;
4563 emsg(_(e_list_value_does_not_have_enough_items));
4564 goto on_error;
4565 }
4566 else if (!semicolon && l->lv_len > count)
4567 {
4568 SOURCING_LNUM = iptr->isn_lnum;
4569 emsg(_(e_list_value_has_more_items_than_targets));
4570 goto on_error;
4571 }
4572
4573 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004574 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004575 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004576 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004577
4578 // Variable after semicolon gets a list with the remaining
4579 // items.
4580 if (semicolon)
4581 {
4582 list_T *rem_list =
4583 list_alloc_with_items(l->lv_len - count + 1);
4584
4585 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004586 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004587 tv = STACK_TV_BOT(-count);
4588 tv->vval.v_list = rem_list;
4589 ++rem_list->lv_refcount;
4590 tv->v_lock = 0;
4591 li = l->lv_first;
4592 for (i = 0; i < count - 1; ++i)
4593 li = li->li_next;
4594 for (i = 0; li != NULL; ++i)
4595 {
4596 list_set_item(rem_list, i, &li->li_tv);
4597 li = li->li_next;
4598 }
4599 --count;
4600 }
4601
4602 // Produce the values in reverse order, first item last.
4603 li = l->lv_first;
4604 for (i = 0; i < count; ++i)
4605 {
4606 tv = STACK_TV_BOT(-i - 1);
4607 copy_tv(&li->li_tv, tv);
4608 li = li->li_next;
4609 }
4610
4611 list_unref(l);
4612 }
4613 break;
4614
Bram Moolenaarb2049902021-01-24 12:53:53 +01004615 case ISN_PROF_START:
4616 case ISN_PROF_END:
4617 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004618#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004619 funccall_T cookie;
4620 ufunc_T *cur_ufunc =
4621 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004622 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004623
4624 cookie.func = cur_ufunc;
4625 if (iptr->isn_type == ISN_PROF_START)
4626 {
4627 func_line_start(&cookie, iptr->isn_lnum);
4628 // if we get here the instruction is executed
4629 func_line_exec(&cookie);
4630 }
4631 else
4632 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004633#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004634 }
4635 break;
4636
Bram Moolenaare99d4222021-06-13 14:01:26 +02004637 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004638 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004639 break;
4640
Bram Moolenaar389df252020-07-09 21:20:47 +02004641 case ISN_SHUFFLE:
4642 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004643 typval_T tmp_tv;
4644 int item = iptr->isn_arg.shuffle.shfl_item;
4645 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004646
4647 tmp_tv = *STACK_TV_BOT(-item);
4648 for ( ; up > 0 && item > 1; --up)
4649 {
4650 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4651 --item;
4652 }
4653 *STACK_TV_BOT(-item) = tmp_tv;
4654 }
4655 break;
4656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004658 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004660 ectx->ec_where.wt_index = 0;
4661 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 break;
4663 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004664 continue;
4665
Bram Moolenaard032f342020-07-18 18:13:02 +02004666func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004667 // Restore previous function. If the frame pointer is where we started
4668 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004669 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004670 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004671
Bram Moolenaar4c137212021-04-19 16:48:48 +02004672 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004673 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004674 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004675 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004676
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004677on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004678 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004679 // If "emsg_silent" is set then ignore the error, unless it was set
4680 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004681 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004682 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004683 {
4684 // If a sequence of instructions causes an error while ":silent!"
4685 // was used, restore the stack length and jump ahead to restoring
4686 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004687 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004688 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004689 while (ectx->ec_stack.ga_len
4690 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004691 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004692 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004693 clear_tv(STACK_TV_BOT(0));
4694 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004695 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4696 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004697 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004698 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004699 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004700on_fatal_error:
4701 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004702 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004703 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004704 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 }
4706
4707done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004708 ret = OK;
4709theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004710 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004711 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4712 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004713}
4714
4715/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004716 * Execute the instructions from a VAR_INSTR typeval and put the result in
4717 * "rettv".
4718 * Return OK or FAIL.
4719 */
4720 int
4721exe_typval_instr(typval_T *tv, typval_T *rettv)
4722{
4723 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4724 isn_T *save_instr = ectx->ec_instr;
4725 int save_iidx = ectx->ec_iidx;
4726 int res;
4727
4728 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4729 res = exec_instructions(ectx);
4730 if (res == OK)
4731 {
4732 *rettv = *STACK_TV_BOT(-1);
4733 --ectx->ec_stack.ga_len;
4734 }
4735
4736 ectx->ec_instr = save_instr;
4737 ectx->ec_iidx = save_iidx;
4738
4739 return res;
4740}
4741
4742/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004743 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4744 * "substitute_instr".
4745 */
4746 char_u *
4747exe_substitute_instr(void)
4748{
4749 ectx_T *ectx = substitute_instr->subs_ectx;
4750 isn_T *save_instr = ectx->ec_instr;
4751 int save_iidx = ectx->ec_iidx;
4752 char_u *res;
4753
4754 ectx->ec_instr = substitute_instr->subs_instr;
4755 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004756 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004757 typval_T *tv = STACK_TV_BOT(-1);
4758
Bram Moolenaar27523602021-06-05 21:36:19 +02004759 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004760 --ectx->ec_stack.ga_len;
4761 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004762 }
4763 else
4764 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004765 substitute_instr->subs_status = FAIL;
4766 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768
Bram Moolenaar4c137212021-04-19 16:48:48 +02004769 ectx->ec_instr = save_instr;
4770 ectx->ec_iidx = save_iidx;
4771
4772 return res;
4773}
4774
4775/*
4776 * Call a "def" function from old Vim script.
4777 * Return OK or FAIL.
4778 */
4779 int
4780call_def_function(
4781 ufunc_T *ufunc,
4782 int argc_arg, // nr of arguments
4783 typval_T *argv, // arguments
4784 partial_T *partial, // optional partial for context
4785 typval_T *rettv) // return value
4786{
4787 ectx_T ectx; // execution context
4788 int argc = argc_arg;
4789 typval_T *tv;
4790 int idx;
4791 int ret = FAIL;
4792 int defcount = ufunc->uf_args.ga_len - argc;
4793 sctx_T save_current_sctx = current_sctx;
4794 int did_emsg_before = did_emsg_cumul + did_emsg;
4795 int save_suppress_errthrow = suppress_errthrow;
4796 msglist_T **saved_msg_list = NULL;
4797 msglist_T *private_msg_list = NULL;
4798 int save_emsg_silent_def = emsg_silent_def;
4799 int save_did_emsg_def = did_emsg_def;
4800 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004801 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004802
4803// Get pointer to item in the stack.
4804#undef STACK_TV
4805#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4806
4807// Get pointer to item at the bottom of the stack, -1 is the bottom.
4808#undef STACK_TV_BOT
4809#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4810
4811// Get pointer to a local variable on the stack. Negative for arguments.
4812#undef STACK_TV_VAR
4813#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4814
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004815 // Update uf_has_breakpoint if needed.
4816 update_has_breakpoint(ufunc);
4817
Bram Moolenaar4c137212021-04-19 16:48:48 +02004818 if (ufunc->uf_def_status == UF_NOT_COMPILED
4819 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004820 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4821 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004822 == FAIL))
4823 {
4824 if (did_emsg_cumul + did_emsg == did_emsg_before)
4825 semsg(_(e_function_is_not_compiled_str),
4826 printable_func_name(ufunc));
4827 return FAIL;
4828 }
4829
4830 {
4831 // Check the function was really compiled.
4832 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4833 + ufunc->uf_dfunc_idx;
4834 if (INSTRUCTIONS(dfunc) == NULL)
4835 {
4836 iemsg("using call_def_function() on not compiled function");
4837 return FAIL;
4838 }
4839 }
4840
4841 // If depth of calling is getting too high, don't execute the function.
4842 orig_funcdepth = funcdepth_get();
4843 if (funcdepth_increment() == FAIL)
4844 return FAIL;
4845
4846 CLEAR_FIELD(ectx);
4847 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4848 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004849 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 {
4851 funcdepth_decrement();
4852 return FAIL;
4853 }
4854 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4855 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4856 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004857 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004858
4859 idx = argc - ufunc->uf_args.ga_len;
4860 if (idx > 0 && ufunc->uf_va_name == NULL)
4861 {
4862 if (idx == 1)
4863 emsg(_(e_one_argument_too_many));
4864 else
4865 semsg(_(e_nr_arguments_too_many), idx);
4866 goto failed_early;
4867 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004868 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4869 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004870 {
4871 if (idx == -1)
4872 emsg(_(e_one_argument_too_few));
4873 else
4874 semsg(_(e_nr_arguments_too_few), -idx);
4875 goto failed_early;
4876 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004877
4878 // Put arguments on the stack, but no more than what the function expects.
4879 // A lambda can be called with more arguments than it uses.
4880 for (idx = 0; idx < argc
4881 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4882 ++idx)
4883 {
4884 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4885 && argv[idx].v_type == VAR_SPECIAL
4886 && argv[idx].vval.v_number == VVAL_NONE)
4887 {
4888 // Use the default value.
4889 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4890 }
4891 else
4892 {
4893 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4894 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004895 ufunc->uf_arg_types[idx], &argv[idx],
4896 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004897 goto failed_early;
4898 copy_tv(&argv[idx], STACK_TV_BOT(0));
4899 }
4900 ++ectx.ec_stack.ga_len;
4901 }
4902
4903 // Turn varargs into a list. Empty list if no args.
4904 if (ufunc->uf_va_name != NULL)
4905 {
4906 int vararg_count = argc - ufunc->uf_args.ga_len;
4907
4908 if (vararg_count < 0)
4909 vararg_count = 0;
4910 else
4911 argc -= vararg_count;
4912 if (exe_newlist(vararg_count, &ectx) == FAIL)
4913 goto failed_early;
4914
4915 // Check the type of the list items.
4916 tv = STACK_TV_BOT(-1);
4917 if (ufunc->uf_va_type != NULL
4918 && ufunc->uf_va_type != &t_list_any
4919 && ufunc->uf_va_type->tt_member != &t_any
4920 && tv->vval.v_list != NULL)
4921 {
4922 type_T *expected = ufunc->uf_va_type->tt_member;
4923 listitem_T *li = tv->vval.v_list->lv_first;
4924
4925 for (idx = 0; idx < vararg_count; ++idx)
4926 {
4927 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004928 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004929 goto failed_early;
4930 li = li->li_next;
4931 }
4932 }
4933
4934 if (defcount > 0)
4935 // Move varargs list to below missing default arguments.
4936 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4937 --ectx.ec_stack.ga_len;
4938 }
4939
4940 // Make space for omitted arguments, will store default value below.
4941 // Any varargs list goes after them.
4942 if (defcount > 0)
4943 for (idx = 0; idx < defcount; ++idx)
4944 {
4945 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4946 ++ectx.ec_stack.ga_len;
4947 }
4948 if (ufunc->uf_va_name != NULL)
4949 ++ectx.ec_stack.ga_len;
4950
4951 // Frame pointer points to just after arguments.
4952 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4953 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4954
4955 {
4956 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4957 + ufunc->uf_dfunc_idx;
4958 ufunc_T *base_ufunc = dfunc->df_ufunc;
4959
4960 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4961 // by copy_func().
4962 if (partial != NULL || base_ufunc->uf_partial != NULL)
4963 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004964 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4965 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004966 goto failed_early;
4967 if (partial != NULL)
4968 {
4969 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4970 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004971 if (current_ectx->ec_outer_ref != NULL
4972 && current_ectx->ec_outer_ref->or_outer != NULL)
4973 ectx.ec_outer_ref->or_outer =
4974 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004975 }
4976 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004977 {
4978 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4979 ++partial->pt_refcount;
4980 ectx.ec_outer_ref->or_partial = partial;
4981 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004982 }
4983 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004984 {
4985 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4986 ++base_ufunc->uf_partial->pt_refcount;
4987 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4988 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004989 }
4990 }
4991
4992 // dummy frame entries
4993 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4994 {
4995 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4996 ++ectx.ec_stack.ga_len;
4997 }
4998
4999 {
5000 // Reserve space for local variables and any closure reference count.
5001 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5002 + ufunc->uf_dfunc_idx;
5003
5004 for (idx = 0; idx < dfunc->df_varcount; ++idx)
5005 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
5006 ectx.ec_stack.ga_len += dfunc->df_varcount;
5007 if (dfunc->df_has_closure)
5008 {
5009 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5010 STACK_TV_VAR(idx)->vval.v_number = 0;
5011 ++ectx.ec_stack.ga_len;
5012 }
5013
5014 ectx.ec_instr = INSTRUCTIONS(dfunc);
5015 }
5016
5017 // Following errors are in the function, not the caller.
5018 // Commands behave like vim9script.
5019 estack_push_ufunc(ufunc, 1);
5020 current_sctx = ufunc->uf_script_ctx;
5021 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5022
5023 // Use a specific location for storing error messages to be converted to an
5024 // exception.
5025 saved_msg_list = msg_list;
5026 msg_list = &private_msg_list;
5027
5028 // Do turn errors into exceptions.
5029 suppress_errthrow = FALSE;
5030
5031 // Do not delete the function while executing it.
5032 ++ufunc->uf_calls;
5033
5034 // When ":silent!" was used before calling then we still abort the
5035 // function. If ":silent!" is used in the function then we don't.
5036 emsg_silent_def = emsg_silent;
5037 did_emsg_def = 0;
5038
5039 ectx.ec_where.wt_index = 0;
5040 ectx.ec_where.wt_variable = FALSE;
5041
5042 // Execute the instructions until done.
5043 ret = exec_instructions(&ectx);
5044 if (ret == OK)
5045 {
5046 // function finished, get result from the stack.
5047 if (ufunc->uf_ret_type == &t_void)
5048 {
5049 rettv->v_type = VAR_VOID;
5050 }
5051 else
5052 {
5053 tv = STACK_TV_BOT(-1);
5054 *rettv = *tv;
5055 tv->v_type = VAR_UNKNOWN;
5056 }
5057 }
5058
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005059 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005060 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5061 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005062
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005063 // Deal with any remaining closures, they may be in use somewhere.
5064 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005065 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005066 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005067 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005068 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005069
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005070 estack_pop();
5071 current_sctx = save_current_sctx;
5072
Bram Moolenaar2336c372021-12-06 15:06:54 +00005073 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5074 // Function was unreferenced while being used, free it now.
5075 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005076
Bram Moolenaar352134b2020-10-17 22:04:08 +02005077 if (*msg_list != NULL && saved_msg_list != NULL)
5078 {
5079 msglist_T **plist = saved_msg_list;
5080
5081 // Append entries from the current msg_list (uncaught exceptions) to
5082 // the saved msg_list.
5083 while (*plist != NULL)
5084 plist = &(*plist)->next;
5085
5086 *plist = *msg_list;
5087 }
5088 msg_list = saved_msg_list;
5089
Bram Moolenaar4c137212021-04-19 16:48:48 +02005090 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005091 {
5092 cmdmod.cmod_filter_regmatch.regprog = NULL;
5093 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005094 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005095 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005096 emsg_silent_def = save_emsg_silent_def;
5097 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005098
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005099failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005100 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5102 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005103 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005106 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005107 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005108 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005109 if (ectx.ec_outer_ref->or_outer_allocated)
5110 vim_free(ectx.ec_outer_ref->or_outer);
5111 partial_unref(ectx.ec_outer_ref->or_partial);
5112 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005113 }
5114
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005115 // Not sure if this is necessary.
5116 suppress_errthrow = save_suppress_errthrow;
5117
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005118 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5119 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005120 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005121 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005122 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 return ret;
5124}
5125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005127 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5128 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5129 * "pfx" is prefixed to every line.
5130 */
5131 static void
5132list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5133{
5134 int line_idx = 0;
5135 int prev_current = 0;
5136 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005137 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005138
5139 for (current = 0; current < instr_count; ++current)
5140 {
5141 isn_T *iptr = &instr[current];
5142 char *line;
5143
5144 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005145 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005146 while (line_idx < iptr->isn_lnum
5147 && line_idx < ufunc->uf_lines.ga_len)
5148 {
5149 if (current > prev_current)
5150 {
5151 msg_puts("\n\n");
5152 prev_current = current;
5153 }
5154 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5155 if (line != NULL)
5156 msg(line);
5157 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005158 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5159 {
5160 int first_def_arg = ufunc->uf_args.ga_len
5161 - ufunc->uf_def_args.ga_len;
5162
5163 if (def_arg_idx > 0)
5164 msg_puts("\n\n");
5165 msg_start();
5166 msg_puts(" ");
5167 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5168 first_def_arg + def_arg_idx]);
5169 msg_puts(" = ");
5170 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5171 msg_clr_eos();
5172 msg_end();
5173 }
5174 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005175
5176 switch (iptr->isn_type)
5177 {
5178 case ISN_EXEC:
5179 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5180 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005181 case ISN_EXEC_SPLIT:
5182 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5183 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005184 case ISN_EXECRANGE:
5185 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5186 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005187 case ISN_LEGACY_EVAL:
5188 smsg("%s%4d EVAL legacy %s", pfx, current,
5189 iptr->isn_arg.string);
5190 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005191 case ISN_REDIRSTART:
5192 smsg("%s%4d REDIR", pfx, current);
5193 break;
5194 case ISN_REDIREND:
5195 smsg("%s%4d REDIR END%s", pfx, current,
5196 iptr->isn_arg.number ? " append" : "");
5197 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005198 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005199#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005200 smsg("%s%4d CEXPR pre %s", pfx, current,
5201 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005202#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005203 break;
5204 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005205#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005206 {
5207 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5208
5209 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5210 cexpr_get_auname(cer->cer_cmdidx),
5211 cer->cer_forceit ? "!" : "",
5212 cer->cer_cmdline);
5213 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005214#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005215 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005216 case ISN_INSTR:
5217 {
5218 smsg("%s%4d INSTR", pfx, current);
5219 list_instructions(" ", iptr->isn_arg.instr,
5220 INT_MAX, NULL);
5221 msg(" -------------");
5222 }
5223 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005224 case ISN_SUBSTITUTE:
5225 {
5226 subs_T *subs = &iptr->isn_arg.subs;
5227
5228 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5229 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5230 msg(" -------------");
5231 }
5232 break;
5233 case ISN_EXECCONCAT:
5234 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5235 (varnumber_T)iptr->isn_arg.number);
5236 break;
5237 case ISN_ECHO:
5238 {
5239 echo_T *echo = &iptr->isn_arg.echo;
5240
5241 smsg("%s%4d %s %d", pfx, current,
5242 echo->echo_with_white ? "ECHO" : "ECHON",
5243 echo->echo_count);
5244 }
5245 break;
5246 case ISN_EXECUTE:
5247 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005248 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005249 break;
5250 case ISN_ECHOMSG:
5251 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005252 (varnumber_T)(iptr->isn_arg.number));
5253 break;
5254 case ISN_ECHOCONSOLE:
5255 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5256 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005257 break;
5258 case ISN_ECHOERR:
5259 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005260 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005261 break;
5262 case ISN_LOAD:
5263 {
5264 if (iptr->isn_arg.number < 0)
5265 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5266 (varnumber_T)(iptr->isn_arg.number
5267 + STACK_FRAME_SIZE));
5268 else
5269 smsg("%s%4d LOAD $%lld", pfx, current,
5270 (varnumber_T)(iptr->isn_arg.number));
5271 }
5272 break;
5273 case ISN_LOADOUTER:
5274 {
5275 if (iptr->isn_arg.number < 0)
5276 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5277 iptr->isn_arg.outer.outer_depth,
5278 iptr->isn_arg.outer.outer_idx
5279 + STACK_FRAME_SIZE);
5280 else
5281 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5282 iptr->isn_arg.outer.outer_depth,
5283 iptr->isn_arg.outer.outer_idx);
5284 }
5285 break;
5286 case ISN_LOADV:
5287 smsg("%s%4d LOADV v:%s", pfx, current,
5288 get_vim_var_name(iptr->isn_arg.number));
5289 break;
5290 case ISN_LOADSCRIPT:
5291 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005292 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5293 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5294 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005295
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005296 sv = get_script_svar(sref, -1);
5297 if (sv == NULL)
5298 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5299 pfx, current, si->sn_name);
5300 else
5301 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005302 sv->sv_name,
5303 sref->sref_idx,
5304 si->sn_name);
5305 }
5306 break;
5307 case ISN_LOADS:
5308 {
5309 scriptitem_T *si = SCRIPT_ITEM(
5310 iptr->isn_arg.loadstore.ls_sid);
5311
5312 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5313 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5314 }
5315 break;
5316 case ISN_LOADAUTO:
5317 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5318 break;
5319 case ISN_LOADG:
5320 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5321 break;
5322 case ISN_LOADB:
5323 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5324 break;
5325 case ISN_LOADW:
5326 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5327 break;
5328 case ISN_LOADT:
5329 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5330 break;
5331 case ISN_LOADGDICT:
5332 smsg("%s%4d LOAD g:", pfx, current);
5333 break;
5334 case ISN_LOADBDICT:
5335 smsg("%s%4d LOAD b:", pfx, current);
5336 break;
5337 case ISN_LOADWDICT:
5338 smsg("%s%4d LOAD w:", pfx, current);
5339 break;
5340 case ISN_LOADTDICT:
5341 smsg("%s%4d LOAD t:", pfx, current);
5342 break;
5343 case ISN_LOADOPT:
5344 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5345 break;
5346 case ISN_LOADENV:
5347 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5348 break;
5349 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005350 smsg("%s%4d LOADREG @%c", pfx, current,
5351 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005352 break;
5353
5354 case ISN_STORE:
5355 if (iptr->isn_arg.number < 0)
5356 smsg("%s%4d STORE arg[%lld]", pfx, current,
5357 iptr->isn_arg.number + STACK_FRAME_SIZE);
5358 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005359 smsg("%s%4d STORE $%lld", pfx, current,
5360 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005361 break;
5362 case ISN_STOREOUTER:
5363 {
5364 if (iptr->isn_arg.number < 0)
5365 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5366 iptr->isn_arg.outer.outer_depth,
5367 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5368 else
5369 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5370 iptr->isn_arg.outer.outer_depth,
5371 iptr->isn_arg.outer.outer_idx);
5372 }
5373 break;
5374 case ISN_STOREV:
5375 smsg("%s%4d STOREV v:%s", pfx, current,
5376 get_vim_var_name(iptr->isn_arg.number));
5377 break;
5378 case ISN_STOREAUTO:
5379 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5380 break;
5381 case ISN_STOREG:
5382 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5383 break;
5384 case ISN_STOREB:
5385 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5386 break;
5387 case ISN_STOREW:
5388 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5389 break;
5390 case ISN_STORET:
5391 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5392 break;
5393 case ISN_STORES:
5394 {
5395 scriptitem_T *si = SCRIPT_ITEM(
5396 iptr->isn_arg.loadstore.ls_sid);
5397
5398 smsg("%s%4d STORES %s in %s", pfx, current,
5399 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5400 }
5401 break;
5402 case ISN_STORESCRIPT:
5403 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005404 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5405 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5406 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005407
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005408 sv = get_script_svar(sref, -1);
5409 if (sv == NULL)
5410 smsg("%s%4d STORESCRIPT [deleted] in %s",
5411 pfx, current, si->sn_name);
5412 else
5413 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005414 sv->sv_name,
5415 sref->sref_idx,
5416 si->sn_name);
5417 }
5418 break;
5419 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005420 case ISN_STOREFUNCOPT:
5421 smsg("%s%4d %s &%s", pfx, current,
5422 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005423 iptr->isn_arg.storeopt.so_name);
5424 break;
5425 case ISN_STOREENV:
5426 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5427 break;
5428 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005429 smsg("%s%4d STOREREG @%c", pfx, current,
5430 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005431 break;
5432 case ISN_STORENR:
5433 smsg("%s%4d STORE %lld in $%d", pfx, current,
5434 iptr->isn_arg.storenr.stnr_val,
5435 iptr->isn_arg.storenr.stnr_idx);
5436 break;
5437
5438 case ISN_STOREINDEX:
5439 smsg("%s%4d STOREINDEX %s", pfx, current,
5440 vartype_name(iptr->isn_arg.vartype));
5441 break;
5442
5443 case ISN_STORERANGE:
5444 smsg("%s%4d STORERANGE", pfx, current);
5445 break;
5446
5447 // constants
5448 case ISN_PUSHNR:
5449 smsg("%s%4d PUSHNR %lld", pfx, current,
5450 (varnumber_T)(iptr->isn_arg.number));
5451 break;
5452 case ISN_PUSHBOOL:
5453 case ISN_PUSHSPEC:
5454 smsg("%s%4d PUSH %s", pfx, current,
5455 get_var_special_name(iptr->isn_arg.number));
5456 break;
5457 case ISN_PUSHF:
5458#ifdef FEAT_FLOAT
5459 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5460#endif
5461 break;
5462 case ISN_PUSHS:
5463 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5464 break;
5465 case ISN_PUSHBLOB:
5466 {
5467 char_u *r;
5468 char_u numbuf[NUMBUFLEN];
5469 char_u *tofree;
5470
5471 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5472 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5473 vim_free(tofree);
5474 }
5475 break;
5476 case ISN_PUSHFUNC:
5477 {
5478 char *name = (char *)iptr->isn_arg.string;
5479
5480 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5481 name == NULL ? "[none]" : name);
5482 }
5483 break;
5484 case ISN_PUSHCHANNEL:
5485#ifdef FEAT_JOB_CHANNEL
5486 {
5487 channel_T *channel = iptr->isn_arg.channel;
5488
5489 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5490 channel == NULL ? 0 : channel->ch_id);
5491 }
5492#endif
5493 break;
5494 case ISN_PUSHJOB:
5495#ifdef FEAT_JOB_CHANNEL
5496 {
5497 typval_T tv;
5498 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005499 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005500
5501 tv.v_type = VAR_JOB;
5502 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005503 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005504 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5505 }
5506#endif
5507 break;
5508 case ISN_PUSHEXC:
5509 smsg("%s%4d PUSH v:exception", pfx, current);
5510 break;
5511 case ISN_UNLET:
5512 smsg("%s%4d UNLET%s %s", pfx, current,
5513 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5514 iptr->isn_arg.unlet.ul_name);
5515 break;
5516 case ISN_UNLETENV:
5517 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5518 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5519 iptr->isn_arg.unlet.ul_name);
5520 break;
5521 case ISN_UNLETINDEX:
5522 smsg("%s%4d UNLETINDEX", pfx, current);
5523 break;
5524 case ISN_UNLETRANGE:
5525 smsg("%s%4d UNLETRANGE", pfx, current);
5526 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005527 case ISN_LOCKUNLOCK:
5528 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5529 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005530 case ISN_LOCKCONST:
5531 smsg("%s%4d LOCKCONST", pfx, current);
5532 break;
5533 case ISN_NEWLIST:
5534 smsg("%s%4d NEWLIST size %lld", pfx, current,
5535 (varnumber_T)(iptr->isn_arg.number));
5536 break;
5537 case ISN_NEWDICT:
5538 smsg("%s%4d NEWDICT size %lld", pfx, current,
5539 (varnumber_T)(iptr->isn_arg.number));
5540 break;
5541
5542 // function call
5543 case ISN_BCALL:
5544 {
5545 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5546
5547 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5548 internal_func_name(cbfunc->cbf_idx),
5549 cbfunc->cbf_argcount);
5550 }
5551 break;
5552 case ISN_DCALL:
5553 {
5554 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5555 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5556 + cdfunc->cdf_idx;
5557
5558 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005559 printable_func_name(df->df_ufunc),
5560 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005561 }
5562 break;
5563 case ISN_UCALL:
5564 {
5565 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5566
5567 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5568 cufunc->cuf_name, cufunc->cuf_argcount);
5569 }
5570 break;
5571 case ISN_PCALL:
5572 {
5573 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5574
5575 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5576 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5577 }
5578 break;
5579 case ISN_PCALL_END:
5580 smsg("%s%4d PCALL end", pfx, current);
5581 break;
5582 case ISN_RETURN:
5583 smsg("%s%4d RETURN", pfx, current);
5584 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005585 case ISN_RETURN_VOID:
5586 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005587 break;
5588 case ISN_FUNCREF:
5589 {
5590 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005591 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005592
Bram Moolenaar38453522021-11-28 22:00:12 +00005593 if (funcref->fr_func_name == NULL)
5594 {
5595 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5596 + funcref->fr_dfunc_idx;
5597 name = df->df_ufunc->uf_name;
5598 }
5599 else
5600 name = funcref->fr_func_name;
5601 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005602 }
5603 break;
5604
5605 case ISN_NEWFUNC:
5606 {
5607 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5608
5609 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5610 newfunc->nf_lambda, newfunc->nf_global);
5611 }
5612 break;
5613
5614 case ISN_DEF:
5615 {
5616 char_u *name = iptr->isn_arg.string;
5617
5618 smsg("%s%4d DEF %s", pfx, current,
5619 name == NULL ? (char_u *)"" : name);
5620 }
5621 break;
5622
5623 case ISN_JUMP:
5624 {
5625 char *when = "?";
5626
5627 switch (iptr->isn_arg.jump.jump_when)
5628 {
5629 case JUMP_ALWAYS:
5630 when = "JUMP";
5631 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005632 case JUMP_NEVER:
5633 iemsg("JUMP_NEVER should not be used");
5634 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005635 case JUMP_AND_KEEP_IF_TRUE:
5636 when = "JUMP_AND_KEEP_IF_TRUE";
5637 break;
5638 case JUMP_IF_FALSE:
5639 when = "JUMP_IF_FALSE";
5640 break;
5641 case JUMP_AND_KEEP_IF_FALSE:
5642 when = "JUMP_AND_KEEP_IF_FALSE";
5643 break;
5644 case JUMP_IF_COND_FALSE:
5645 when = "JUMP_IF_COND_FALSE";
5646 break;
5647 case JUMP_IF_COND_TRUE:
5648 when = "JUMP_IF_COND_TRUE";
5649 break;
5650 }
5651 smsg("%s%4d %s -> %d", pfx, current, when,
5652 iptr->isn_arg.jump.jump_where);
5653 }
5654 break;
5655
5656 case ISN_JUMP_IF_ARG_SET:
5657 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5658 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5659 iptr->isn_arg.jump.jump_where);
5660 break;
5661
5662 case ISN_FOR:
5663 {
5664 forloop_T *forloop = &iptr->isn_arg.forloop;
5665
5666 smsg("%s%4d FOR $%d -> %d", pfx, current,
5667 forloop->for_idx, forloop->for_end);
5668 }
5669 break;
5670
5671 case ISN_TRY:
5672 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005673 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005674
5675 if (try->try_ref->try_finally == 0)
5676 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5677 pfx, current,
5678 try->try_ref->try_catch,
5679 try->try_ref->try_endtry);
5680 else
5681 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5682 pfx, current,
5683 try->try_ref->try_catch,
5684 try->try_ref->try_finally,
5685 try->try_ref->try_endtry);
5686 }
5687 break;
5688 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005689 smsg("%s%4d CATCH", pfx, current);
5690 break;
5691 case ISN_TRYCONT:
5692 {
5693 trycont_T *trycont = &iptr->isn_arg.trycont;
5694
5695 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5696 trycont->tct_levels,
5697 trycont->tct_levels == 1 ? "" : "s",
5698 trycont->tct_where);
5699 }
5700 break;
5701 case ISN_FINALLY:
5702 smsg("%s%4d FINALLY", pfx, current);
5703 break;
5704 case ISN_ENDTRY:
5705 smsg("%s%4d ENDTRY", pfx, current);
5706 break;
5707 case ISN_THROW:
5708 smsg("%s%4d THROW", pfx, current);
5709 break;
5710
5711 // expression operations on number
5712 case ISN_OPNR:
5713 case ISN_OPFLOAT:
5714 case ISN_OPANY:
5715 {
5716 char *what;
5717 char *ins;
5718
5719 switch (iptr->isn_arg.op.op_type)
5720 {
5721 case EXPR_MULT: what = "*"; break;
5722 case EXPR_DIV: what = "/"; break;
5723 case EXPR_REM: what = "%"; break;
5724 case EXPR_SUB: what = "-"; break;
5725 case EXPR_ADD: what = "+"; break;
5726 default: what = "???"; break;
5727 }
5728 switch (iptr->isn_type)
5729 {
5730 case ISN_OPNR: ins = "OPNR"; break;
5731 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5732 case ISN_OPANY: ins = "OPANY"; break;
5733 default: ins = "???"; break;
5734 }
5735 smsg("%s%4d %s %s", pfx, current, ins, what);
5736 }
5737 break;
5738
5739 case ISN_COMPAREBOOL:
5740 case ISN_COMPARESPECIAL:
5741 case ISN_COMPARENR:
5742 case ISN_COMPAREFLOAT:
5743 case ISN_COMPARESTRING:
5744 case ISN_COMPAREBLOB:
5745 case ISN_COMPARELIST:
5746 case ISN_COMPAREDICT:
5747 case ISN_COMPAREFUNC:
5748 case ISN_COMPAREANY:
5749 {
5750 char *p;
5751 char buf[10];
5752 char *type;
5753
5754 switch (iptr->isn_arg.op.op_type)
5755 {
5756 case EXPR_EQUAL: p = "=="; break;
5757 case EXPR_NEQUAL: p = "!="; break;
5758 case EXPR_GREATER: p = ">"; break;
5759 case EXPR_GEQUAL: p = ">="; break;
5760 case EXPR_SMALLER: p = "<"; break;
5761 case EXPR_SEQUAL: p = "<="; break;
5762 case EXPR_MATCH: p = "=~"; break;
5763 case EXPR_IS: p = "is"; break;
5764 case EXPR_ISNOT: p = "isnot"; break;
5765 case EXPR_NOMATCH: p = "!~"; break;
5766 default: p = "???"; break;
5767 }
5768 STRCPY(buf, p);
5769 if (iptr->isn_arg.op.op_ic == TRUE)
5770 strcat(buf, "?");
5771 switch(iptr->isn_type)
5772 {
5773 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5774 case ISN_COMPARESPECIAL:
5775 type = "COMPARESPECIAL"; break;
5776 case ISN_COMPARENR: type = "COMPARENR"; break;
5777 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5778 case ISN_COMPARESTRING:
5779 type = "COMPARESTRING"; break;
5780 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5781 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5782 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5783 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5784 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5785 default: type = "???"; break;
5786 }
5787
5788 smsg("%s%4d %s %s", pfx, current, type, buf);
5789 }
5790 break;
5791
5792 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5793 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5794
5795 // expression operations
5796 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5797 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5798 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5799 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5800 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5801 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5802 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5803 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5804 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5805 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5806 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5807 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005808 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005809 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5810 iptr->isn_arg.getitem.gi_index,
5811 iptr->isn_arg.getitem.gi_with_op ?
5812 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005813 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5814 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5815 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005816 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5817 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5818
Bram Moolenaar4c137212021-04-19 16:48:48 +02005819 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5820
5821 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5822 case ISN_CHECKTYPE:
5823 {
5824 checktype_T *ct = &iptr->isn_arg.type;
5825 char *tofree;
5826
5827 if (ct->ct_arg_idx == 0)
5828 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5829 type_name(ct->ct_type, &tofree),
5830 (int)ct->ct_off);
5831 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005832 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5833 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005834 type_name(ct->ct_type, &tofree),
5835 (int)ct->ct_off,
5836 (int)ct->ct_arg_idx);
5837 vim_free(tofree);
5838 break;
5839 }
5840 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5841 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5842 iptr->isn_arg.checklen.cl_min_len);
5843 break;
5844 case ISN_SETTYPE:
5845 {
5846 char *tofree;
5847
5848 smsg("%s%4d SETTYPE %s", pfx, current,
5849 type_name(iptr->isn_arg.type.ct_type, &tofree));
5850 vim_free(tofree);
5851 break;
5852 }
5853 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005854 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5855 smsg("%s%4d INVERT %d (!val)", pfx, current,
5856 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005857 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005858 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5859 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860 break;
5861 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005862 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005863 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005864 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5865 pfx, current,
5866 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005867 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005868 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5869 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005870 break;
5871 case ISN_PUT:
5872 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5873 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005874 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005875 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5876 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005877 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005878 else
5879 smsg("%s%4d PUT %c %ld", pfx, current,
5880 iptr->isn_arg.put.put_regname,
5881 (long)iptr->isn_arg.put.put_lnum);
5882 break;
5883
Bram Moolenaar4c137212021-04-19 16:48:48 +02005884 case ISN_CMDMOD:
5885 {
5886 char_u *buf;
5887 size_t len = produce_cmdmods(
5888 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5889
5890 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005891 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005892 {
5893 (void)produce_cmdmods(
5894 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5895 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5896 vim_free(buf);
5897 }
5898 break;
5899 }
5900 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5901
5902 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005903 smsg("%s%4d PROFILE START line %d", pfx, current,
5904 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005905 break;
5906
5907 case ISN_PROF_END:
5908 smsg("%s%4d PROFILE END", pfx, current);
5909 break;
5910
Bram Moolenaare99d4222021-06-13 14:01:26 +02005911 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005912 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5913 iptr->isn_arg.debug.dbg_break_lnum + 1,
5914 iptr->isn_lnum,
5915 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005916 break;
5917
Bram Moolenaar4c137212021-04-19 16:48:48 +02005918 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5919 iptr->isn_arg.unpack.unp_count,
5920 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5921 break;
5922 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5923 iptr->isn_arg.shuffle.shfl_item,
5924 iptr->isn_arg.shuffle.shfl_up);
5925 break;
5926 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5927
5928 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5929 return;
5930 }
5931
5932 out_flush(); // output one line at a time
5933 ui_breakcheck();
5934 if (got_int)
5935 break;
5936 }
5937}
5938
5939/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005940 * Handle command line completion for the :disassemble command.
5941 */
5942 void
5943set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5944{
5945 char_u *p;
5946
5947 // Default: expand user functions, "debug" and "profile"
5948 xp->xp_context = EXPAND_DISASSEMBLE;
5949 xp->xp_pattern = arg;
5950
5951 // first argument already typed: only user function names
5952 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5953 {
5954 xp->xp_context = EXPAND_USER_FUNC;
5955 xp->xp_pattern = skipwhite(p);
5956 }
5957}
5958
5959/*
5960 * Function given to ExpandGeneric() to obtain the list of :disassemble
5961 * arguments.
5962 */
5963 char_u *
5964get_disassemble_argument(expand_T *xp, int idx)
5965{
5966 if (idx == 0)
5967 return (char_u *)"debug";
5968 if (idx == 1)
5969 return (char_u *)"profile";
5970 return get_user_func_name(xp, idx - 2);
5971}
5972
5973/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005974 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005975 * We don't really need this at runtime, but we do have tests that require it,
5976 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 */
5978 void
5979ex_disassemble(exarg_T *eap)
5980{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005981 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005982 char_u *fname;
5983 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984 dfunc_T *dfunc;
5985 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005986 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005987 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005988 compiletype_T compile_type = CT_NONE;
5989
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005990 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005991 {
5992 compile_type = CT_PROFILE;
5993 arg = skipwhite(arg + 7);
5994 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005995 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005996 {
5997 compile_type = CT_DEBUG;
5998 arg = skipwhite(arg + 5);
5999 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006001 if (STRNCMP(arg, "<lambda>", 8) == 0)
6002 {
6003 arg += 8;
6004 (void)getdigits(&arg);
6005 fname = vim_strnsave(eap->arg, arg - eap->arg);
6006 }
6007 else
6008 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006009 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006010 if (fname == NULL)
6011 {
6012 semsg(_(e_invarg2), eap->arg);
6013 return;
6014 }
6015
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006016 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006017 if (ufunc == NULL)
6018 {
6019 char_u *p = untrans_function_name(fname);
6020
6021 if (p != NULL)
6022 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006023 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006024 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006025 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 if (ufunc == NULL)
6027 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006028 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029 return;
6030 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006031 if (func_needs_compiling(ufunc, compile_type)
6032 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006033 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006034 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006036 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 return;
6038 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006039 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040
6041 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006042 switch (compile_type)
6043 {
6044 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006045#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006046 instr = dfunc->df_instr_prof;
6047 instr_count = dfunc->df_instr_prof_count;
6048 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006049#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006050 // FALLTHROUGH
6051 case CT_NONE:
6052 instr = dfunc->df_instr;
6053 instr_count = dfunc->df_instr_count;
6054 break;
6055 case CT_DEBUG:
6056 instr = dfunc->df_instr_debug;
6057 instr_count = dfunc->df_instr_debug_count;
6058 break;
6059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060
Bram Moolenaar4c137212021-04-19 16:48:48 +02006061 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062}
6063
6064/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006065 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 * list, etc. Mostly like what JavaScript does, except that empty list and
6067 * empty dictionary are FALSE.
6068 */
6069 int
6070tv2bool(typval_T *tv)
6071{
6072 switch (tv->v_type)
6073 {
6074 case VAR_NUMBER:
6075 return tv->vval.v_number != 0;
6076 case VAR_FLOAT:
6077#ifdef FEAT_FLOAT
6078 return tv->vval.v_float != 0.0;
6079#else
6080 break;
6081#endif
6082 case VAR_PARTIAL:
6083 return tv->vval.v_partial != NULL;
6084 case VAR_FUNC:
6085 case VAR_STRING:
6086 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6087 case VAR_LIST:
6088 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6089 case VAR_DICT:
6090 return tv->vval.v_dict != NULL
6091 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6092 case VAR_BOOL:
6093 case VAR_SPECIAL:
6094 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6095 case VAR_JOB:
6096#ifdef FEAT_JOB_CHANNEL
6097 return tv->vval.v_job != NULL;
6098#else
6099 break;
6100#endif
6101 case VAR_CHANNEL:
6102#ifdef FEAT_JOB_CHANNEL
6103 return tv->vval.v_channel != NULL;
6104#else
6105 break;
6106#endif
6107 case VAR_BLOB:
6108 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6109 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006110 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006112 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 break;
6114 }
6115 return FALSE;
6116}
6117
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006118 void
6119emsg_using_string_as(typval_T *tv, int as_number)
6120{
6121 semsg(_(as_number ? e_using_string_as_number_str
6122 : e_using_string_as_bool_str),
6123 tv->vval.v_string == NULL
6124 ? (char_u *)"" : tv->vval.v_string);
6125}
6126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127/*
6128 * If "tv" is a string give an error and return FAIL.
6129 */
6130 int
6131check_not_string(typval_T *tv)
6132{
6133 if (tv->v_type == VAR_STRING)
6134 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006135 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136 clear_tv(tv);
6137 return FAIL;
6138 }
6139 return OK;
6140}
6141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142
6143#endif // FEAT_EVAL