blob: c5d39535038505c3a58defe7fd2c45991ea9ac88 [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
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
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
471/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200472 * Used when returning from a function: Check if any closure is still
473 * referenced. If so then move the arguments and variables to a separate piece
474 * of stack to be used when the closure is called.
475 * When "free_arguments" is TRUE the arguments are to be freed.
476 * Returns FAIL when out of memory.
477 */
478 static int
479handle_closure_in_use(ectx_T *ectx, int free_arguments)
480{
481 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
482 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200483 int argcount;
484 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200485 int idx;
486 typval_T *tv;
487 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200488 garray_T *gap = &ectx->ec_funcrefs;
489 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200491 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200492 return OK; // function was freed
493 if (dfunc->df_has_closure == 0)
494 return OK; // no closures
495 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
496 closure_count = tv->vval.v_number;
497 if (closure_count == 0)
498 return OK; // no funcrefs created
499
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200500 argcount = ufunc_argcount(dfunc->df_ufunc);
501 top = ectx->ec_frame_idx - argcount;
502
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200503 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200504 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200505 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200506 partial_T *pt;
507 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200508
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200509 if (off < 0)
510 continue; // count is off or already done
511 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200512 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200513 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200514 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200515 int i;
516
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200517 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200518 // unreferenced on return.
519 for (i = 0; i < dfunc->df_varcount; ++i)
520 {
521 typval_T *stv = STACK_TV(ectx->ec_frame_idx
522 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200523 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200524 --refcount;
525 }
526 if (refcount > 1)
527 {
528 closure_in_use = TRUE;
529 break;
530 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200531 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200532 }
533
534 if (closure_in_use)
535 {
536 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
537 typval_T *stack;
538
539 // A closure is using the arguments and/or local variables.
540 // Move them to the called function.
541 if (funcstack == NULL)
542 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200543 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
544 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200545 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
546 funcstack->fs_ga.ga_data = stack;
547 if (stack == NULL)
548 {
549 vim_free(funcstack);
550 return FAIL;
551 }
552
553 // Move or copy the arguments.
554 for (idx = 0; idx < argcount; ++idx)
555 {
556 tv = STACK_TV(top + idx);
557 if (free_arguments)
558 {
559 *(stack + idx) = *tv;
560 tv->v_type = VAR_UNKNOWN;
561 }
562 else
563 copy_tv(tv, stack + idx);
564 }
565 // Move the local variables.
566 for (idx = 0; idx < dfunc->df_varcount; ++idx)
567 {
568 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200569
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200570 // A partial created for a local function, that is also used as a
571 // local variable, has a reference count for the variable, thus
572 // will never go down to zero. When all these refcounts are one
573 // then the funcstack is unused. We need to count how many we have
574 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200575 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
576 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200577 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200578
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200579 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200580 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
581 gap->ga_len - closure_count + i])
582 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200583 }
584
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200585 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200586 tv->v_type = VAR_UNKNOWN;
587 }
588
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200589 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200590 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200591 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
592 - closure_count + idx];
593 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200594 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200595 ++funcstack->fs_refcount;
596 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100597 pt->pt_outer.out_stack = &funcstack->fs_ga;
598 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200599 }
600 }
601 }
602
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200603 for (idx = 0; idx < closure_count; ++idx)
604 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
605 - closure_count + idx]);
606 gap->ga_len -= closure_count;
607 if (gap->ga_len == 0)
608 ga_clear(gap);
609
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200610 return OK;
611}
612
613/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200614 * Called when a partial is freed or its reference count goes down to one. The
615 * funcstack may be the only reference to the partials in the local variables.
616 * Go over all of them, the funcref and can be freed if all partials
617 * referencing the funcstack have a reference count of one.
618 */
619 void
620funcstack_check_refcount(funcstack_T *funcstack)
621{
622 int i;
623 garray_T *gap = &funcstack->fs_ga;
624 int done = 0;
625
626 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
627 return;
628 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
629 {
630 typval_T *tv = ((typval_T *)gap->ga_data) + i;
631
632 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
633 && tv->vval.v_partial->pt_funcstack == funcstack
634 && tv->vval.v_partial->pt_refcount == 1)
635 ++done;
636 }
637 if (done == funcstack->fs_min_refcount)
638 {
639 typval_T *stack = gap->ga_data;
640
641 // All partials referencing the funcstack have a reference count of
642 // one, thus the funcstack is no longer of use.
643 for (i = 0; i < gap->ga_len; ++i)
644 clear_tv(stack + i);
645 vim_free(stack);
646 vim_free(funcstack);
647 }
648}
649
650/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 * Return from the current function.
652 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200653 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200654func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100657 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200658 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
659 + ectx->ec_dfunc_idx;
660 int argcount = ufunc_argcount(dfunc->df_ufunc);
661 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200662 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100663 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
664 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200665 funclocal_T *floc;
666#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100667 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
668 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669
Bram Moolenaar12d26532021-02-19 19:13:21 +0100670 if (do_profiling == PROF_YES)
671 {
672 ufunc_T *caller = prev_dfunc->df_ufunc;
673
674 if (dfunc->df_ufunc->uf_profiling
675 || (caller != NULL && caller->uf_profiling))
676 {
677 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
678 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
679 --profile_info_ga.ga_len;
680 }
681 }
682#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100683 // TODO: when is it safe to delete the function when it is no longer used?
684 --dfunc->df_ufunc->uf_calls;
685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200687 entry = estack_pop();
688 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200689 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200691 if (handle_closure_in_use(ectx, TRUE) == FAIL)
692 return FAIL;
693
694 // Clear the arguments.
695 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
696 clear_tv(STACK_TV(idx));
697
698 // Clear local variables and temp values, but not the return value.
699 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100700 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100702
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100703 // The return value should be on top of the stack. However, when aborting
704 // it may not be there and ec_frame_idx is the top of the stack.
705 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100706 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100707 ret_idx = 0;
708
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200709 if (ectx->ec_outer_ref != NULL)
710 {
711 if (ectx->ec_outer_ref->or_outer_allocated)
712 vim_free(ectx->ec_outer_ref->or_outer);
713 partial_unref(ectx->ec_outer_ref->or_partial);
714 vim_free(ectx->ec_outer_ref);
715 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100716
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100717 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100718 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100719 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
720 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200721 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
722 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200723 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100724 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100725 floc = (void *)STACK_TV(ectx->ec_frame_idx
726 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200727 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100728 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
729 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200730
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100731 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200732 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100733 else
734 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200735 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100736 vim_free(floc);
737 }
738
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100739 if (ret_idx > 0)
740 {
741 // Reset the stack to the position before the call, with a spot for the
742 // return value, moved there from above the frame.
743 ectx->ec_stack.ga_len = top + 1;
744 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
745 }
746 else
747 // Reset the stack to the position before the call.
748 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200749
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100750 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200751 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200752 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753}
754
755#undef STACK_TV
756
757/*
758 * Prepare arguments and rettv for calling a builtin or user function.
759 */
760 static int
761call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
762{
763 int idx;
764 typval_T *tv;
765
766 // Move arguments from bottom of the stack to argvars[] and add terminator.
767 for (idx = 0; idx < argcount; ++idx)
768 argvars[idx] = *STACK_TV_BOT(idx - argcount);
769 argvars[argcount].v_type = VAR_UNKNOWN;
770
771 // Result replaces the arguments on the stack.
772 if (argcount > 0)
773 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200774 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 return FAIL;
776 else
777 ++ectx->ec_stack.ga_len;
778
779 // Default return value is zero.
780 tv = STACK_TV_BOT(-1);
781 tv->v_type = VAR_NUMBER;
782 tv->vval.v_number = 0;
783
784 return OK;
785}
786
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200787// Ugly global to avoid passing the execution context around through many
788// layers.
789static ectx_T *current_ectx = NULL;
790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100791/*
792 * Call a builtin function by index.
793 */
794 static int
795call_bfunc(int func_idx, int argcount, ectx_T *ectx)
796{
797 typval_T argvars[MAX_FUNC_ARGS];
798 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100799 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200800 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200801 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802
803 if (call_prepare(argcount, argvars, ectx) == FAIL)
804 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200805 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200807 // Call the builtin function. Set "current_ectx" so that when it
808 // recursively invokes call_def_function() a closure context can be set.
809 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200811 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200812 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
814 // Clear the arguments.
815 for (idx = 0; idx < argcount; ++idx)
816 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200817
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100818 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200819 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 return OK;
821}
822
823/*
824 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100825 * If the function is compiled this will add a stack frame and set the
826 * instruction pointer at the start of the function.
827 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200828 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100829 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 */
831 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100832call_ufunc(
833 ufunc_T *ufunc,
834 partial_T *pt,
835 int argcount,
836 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200837 isn_T *iptr,
838 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839{
840 typval_T argvars[MAX_FUNC_ARGS];
841 funcexe_T funcexe;
842 int error;
843 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100844 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200845 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846
Bram Moolenaare99d4222021-06-13 14:01:26 +0200847 if (func_needs_compiling(ufunc, compile_type)
848 && compile_def_function(ufunc, FALSE, compile_type, NULL)
849 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200850 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200851 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100852 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100853 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100854 if (error != FCERR_UNKNOWN)
855 {
856 if (error == FCERR_TOOMANY)
857 semsg(_(e_toomanyarg), ufunc->uf_name);
858 else
859 semsg(_(e_toofewarg), ufunc->uf_name);
860 return FAIL;
861 }
862
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100863 // The function has been compiled, can call it quickly. For a function
864 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100865 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100866 if (iptr != NULL)
867 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100868 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100869 iptr->isn_type = ISN_DCALL;
870 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
871 iptr->isn_arg.dfunc.cdf_argcount = argcount;
872 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200873 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875
876 if (call_prepare(argcount, argvars, ectx) == FAIL)
877 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200878 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 funcexe.evaluate = TRUE;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200880 funcexe.selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881
882 // Call the user function. Result goes in last position on the stack.
883 // TODO: add selfdict if there is one
884 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200885 STACK_TV_BOT(-1), &funcexe, funcexe.selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886
887 // Clear the arguments.
888 for (idx = 0; idx < argcount; ++idx)
889 clear_tv(&argvars[idx]);
890
891 if (error != FCERR_NONE)
892 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000893 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 return FAIL;
895 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100896 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200897 // Error other than from calling the function itself.
898 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 return OK;
900}
901
902/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100903 * If command modifiers were applied restore them.
904 */
905 static void
906may_restore_cmdmod(funclocal_T *funclocal)
907{
908 if (funclocal->floc_restore_cmdmod)
909 {
910 cmdmod.cmod_filter_regmatch.regprog = NULL;
911 undo_cmdmod(&cmdmod);
912 cmdmod = funclocal->floc_save_cmdmod;
913 funclocal->floc_restore_cmdmod = FALSE;
914 }
915}
916
917/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200918 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
919 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200920 */
921 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200922vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200923{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200924 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200925}
926
927/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 * Execute a function by "name".
929 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100930 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 * Returns FAIL if not found without an error message.
932 */
933 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100934call_by_name(
935 char_u *name,
936 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100937 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200938 isn_T *iptr,
939 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940{
941 ufunc_T *ufunc;
942
943 if (builtin_function(name, -1))
944 {
945 int func_idx = find_internal_func(name);
946
947 if (func_idx < 0)
948 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200949 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 return FAIL;
951 return call_bfunc(func_idx, argcount, ectx);
952 }
953
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200954 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200955
956 if (ufunc == NULL)
957 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200958 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +0200959
960 if (script_autoload(name, TRUE))
961 // loaded a package, search for the function again
962 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200963
964 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +0200965 return FAIL; // bail out if loading the script caused an error
966 }
967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100969 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200970 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100971 {
972 int i;
973 typval_T *argv = STACK_TV_BOT(0) - argcount;
974
975 // The function can change at runtime, check that the argument
976 // types are correct.
977 for (i = 0; i < argcount; ++i)
978 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100979 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100980
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200981 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100982 type = ufunc->uf_arg_types[i];
983 else if (ufunc->uf_va_type != NULL)
984 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100985 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200986 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100987 return FAIL;
988 }
989 }
990
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200991 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993
994 return FAIL;
995}
996
997 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100998call_partial(
999 typval_T *tv,
1000 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001001 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001003 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001004 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001006 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001007 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008
1009 if (tv->v_type == VAR_PARTIAL)
1010 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001011 partial_T *pt = tv->vval.v_partial;
1012 int i;
1013
1014 if (pt->pt_argc > 0)
1015 {
1016 // Make space for arguments from the partial, shift the "argcount"
1017 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001018 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001019 return FAIL;
1020 for (i = 1; i <= argcount; ++i)
1021 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1022 ectx->ec_stack.ga_len += pt->pt_argc;
1023 argcount += pt->pt_argc;
1024
1025 // copy the arguments from the partial onto the stack
1026 for (i = 0; i < pt->pt_argc; ++i)
1027 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1028 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001029 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030
1031 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001032 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 name = pt->pt_name;
1035 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001036 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001038 if (name != NULL)
1039 {
1040 char_u fname_buf[FLEN_FIXED + 1];
1041 char_u *tofree = NULL;
1042 int error = FCERR_NONE;
1043 char_u *fname;
1044
1045 // May need to translate <SNR>123_ to K_SNR.
1046 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1047 if (error != FCERR_NONE)
1048 res = FAIL;
1049 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001050 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001051 vim_free(tofree);
1052 }
1053
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001054 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 {
1056 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001057 semsg(_(e_unknownfunc),
1058 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 return FAIL;
1060 }
1061 return OK;
1062}
1063
1064/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001065 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1066 * TRUE.
1067 */
1068 static int
1069error_if_locked(int lock, char *error)
1070{
1071 if (lock & (VAR_LOCKED | VAR_FIXED))
1072 {
1073 emsg(_(error));
1074 return TRUE;
1075 }
1076 return FALSE;
1077}
1078
1079/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001080 * Give an error if "tv" is not a number and return FAIL.
1081 */
1082 static int
1083check_for_number(typval_T *tv)
1084{
1085 if (tv->v_type != VAR_NUMBER)
1086 {
1087 semsg(_(e_expected_str_but_got_str),
1088 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1089 return FAIL;
1090 }
1091 return OK;
1092}
1093
1094/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001095 * Store "tv" in variable "name".
1096 * This is for s: and g: variables.
1097 */
1098 static void
1099store_var(char_u *name, typval_T *tv)
1100{
1101 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001102 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001103
Bram Moolenaard877a572021-04-01 19:42:48 +02001104 if (tv->v_lock)
1105 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001106 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001107 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001108 restore_funccal();
1109}
1110
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001111/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001112 * Convert "tv" to a string.
1113 * Return FAIL if not allowed.
1114 */
1115 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001116do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001117{
1118 if (tv->v_type != VAR_STRING)
1119 {
1120 char_u *str;
1121
1122 if (is_2string_any)
1123 {
1124 switch (tv->v_type)
1125 {
1126 case VAR_SPECIAL:
1127 case VAR_BOOL:
1128 case VAR_NUMBER:
1129 case VAR_FLOAT:
1130 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001131
1132 case VAR_LIST:
1133 if (tolerant)
1134 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001135 char_u *s, *e, *p;
1136 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001137
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001138 ga_init2(&ga, sizeof(char_u *), 1);
1139
1140 // Convert to NL separated items, then
1141 // escape the items and replace the NL with
1142 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001143 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001144 if (str == NULL)
1145 return FAIL;
1146 s = str;
1147 while ((e = vim_strchr(s, '\n')) != NULL)
1148 {
1149 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001150 p = vim_strsave_fnameescape(s,
1151 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001152 if (p != NULL)
1153 {
1154 ga_concat(&ga, p);
1155 ga_concat(&ga, (char_u *)" ");
1156 vim_free(p);
1157 }
1158 s = e + 1;
1159 }
1160 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001161 clear_tv(tv);
1162 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001163 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001164 return OK;
1165 }
1166 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001167 default: to_string_error(tv->v_type);
1168 return FAIL;
1169 }
1170 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001171 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001172 clear_tv(tv);
1173 tv->v_type = VAR_STRING;
1174 tv->vval.v_string = str;
1175 }
1176 return OK;
1177}
1178
1179/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001180 * When the value of "sv" is a null list of dict, allocate it.
1181 */
1182 static void
1183allocate_if_null(typval_T *tv)
1184{
1185 switch (tv->v_type)
1186 {
1187 case VAR_LIST:
1188 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001189 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001190 break;
1191 case VAR_DICT:
1192 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001193 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001194 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001195 case VAR_BLOB:
1196 if (tv->vval.v_blob == NULL)
1197 (void)rettv_blob_alloc(tv);
1198 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001199 default:
1200 break;
1201 }
1202}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001203
Bram Moolenaare7525c52021-01-09 13:20:37 +01001204/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001205 * Return the character "str[index]" where "index" is the character index,
1206 * including composing characters.
1207 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001208 */
1209 char_u *
1210char_from_string(char_u *str, varnumber_T index)
1211{
1212 size_t nbyte = 0;
1213 varnumber_T nchar = index;
1214 size_t slen;
1215
1216 if (str == NULL)
1217 return NULL;
1218 slen = STRLEN(str);
1219
Bram Moolenaarff871402021-03-26 13:34:05 +01001220 // Do the same as for a list: a negative index counts from the end.
1221 // Optimization to check the first byte to be below 0x80 (and no composing
1222 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001223 if (index < 0)
1224 {
1225 int clen = 0;
1226
1227 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001228 {
1229 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1230 ++nbyte;
1231 else if (enc_utf8)
1232 nbyte += utfc_ptr2len(str + nbyte);
1233 else
1234 nbyte += mb_ptr2len(str + nbyte);
1235 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001236 nchar = clen + index;
1237 if (nchar < 0)
1238 // unlike list: index out of range results in empty string
1239 return NULL;
1240 }
1241
1242 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001243 {
1244 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1245 ++nbyte;
1246 else if (enc_utf8)
1247 nbyte += utfc_ptr2len(str + nbyte);
1248 else
1249 nbyte += mb_ptr2len(str + nbyte);
1250 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001251 if (nbyte >= slen)
1252 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001253 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001254}
1255
1256/*
1257 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001258 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001259 * If going over the end return "str_len".
1260 * If "idx" is negative count from the end, -1 is the last character.
1261 * When going over the start return -1.
1262 */
1263 static long
1264char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1265{
1266 varnumber_T nchar = idx;
1267 size_t nbyte = 0;
1268
1269 if (nchar >= 0)
1270 {
1271 while (nchar > 0 && nbyte < str_len)
1272 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001273 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001274 --nchar;
1275 }
1276 }
1277 else
1278 {
1279 nbyte = str_len;
1280 while (nchar < 0 && nbyte > 0)
1281 {
1282 --nbyte;
1283 nbyte -= mb_head_off(str, str + nbyte);
1284 ++nchar;
1285 }
1286 if (nchar < 0)
1287 return -1;
1288 }
1289 return (long)nbyte;
1290}
1291
1292/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001293 * Return the slice "str[first : last]" using character indexes. Composing
1294 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001295 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001296 * Return NULL when the result is empty.
1297 */
1298 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001299string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001300{
1301 long start_byte, end_byte;
1302 size_t slen;
1303
1304 if (str == NULL)
1305 return NULL;
1306 slen = STRLEN(str);
1307 start_byte = char_idx2byte(str, slen, first);
1308 if (start_byte < 0)
1309 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001310 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001311 end_byte = (long)slen;
1312 else
1313 {
1314 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001315 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001316 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001317 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001318 }
1319
1320 if (start_byte >= (long)slen || end_byte <= start_byte)
1321 return NULL;
1322 return vim_strnsave(str + start_byte, end_byte - start_byte);
1323}
1324
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001325/*
1326 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1327 * When "dfunc_idx" is negative don't give an error.
1328 * Returns NULL for an error.
1329 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001330 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001331get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001332{
1333 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001334 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1335 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001336 svar_T *sv;
1337
1338 if (sref->sref_seq != si->sn_script_seq)
1339 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001340 // The script was reloaded after the function was compiled, the
1341 // script_idx may not be valid.
1342 if (dfunc != NULL)
1343 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1344 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001345 return NULL;
1346 }
1347 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001348 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001349 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001350 if (dfunc != NULL)
1351 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001352 return NULL;
1353 }
1354 return sv;
1355}
1356
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001357/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001358 * Function passed to do_cmdline() for splitting a script joined by NL
1359 * characters.
1360 */
1361 static char_u *
1362get_split_sourceline(
1363 int c UNUSED,
1364 void *cookie,
1365 int indent UNUSED,
1366 getline_opt_T options UNUSED)
1367{
1368 source_cookie_T *sp = (source_cookie_T *)cookie;
1369 char_u *p;
1370 char_u *line;
1371
1372 if (*sp->nextline == NUL)
1373 return NULL;
1374 p = vim_strchr(sp->nextline, '\n');
1375 if (p == NULL)
1376 {
1377 line = vim_strsave(sp->nextline);
1378 sp->nextline += STRLEN(sp->nextline);
1379 }
1380 else
1381 {
1382 line = vim_strnsave(sp->nextline, p - sp->nextline);
1383 sp->nextline = p + 1;
1384 }
1385 return line;
1386}
1387
1388/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 * Execute a function by "name".
1390 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001391 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 */
1393 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001394call_eval_func(
1395 char_u *name,
1396 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001397 ectx_T *ectx,
1398 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399{
Bram Moolenaared677f52020-08-12 16:38:10 +02001400 int called_emsg_before = called_emsg;
1401 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001403 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001404 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001406 dictitem_T *v;
1407
1408 v = find_var(name, NULL, FALSE);
1409 if (v == NULL)
1410 {
1411 semsg(_(e_unknownfunc), name);
1412 return FAIL;
1413 }
1414 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1415 {
1416 semsg(_(e_unknownfunc), name);
1417 return FAIL;
1418 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001419 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001421 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422}
1423
1424/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001425 * When a function reference is used, fill a partial with the information
1426 * needed, especially when it is used as a closure.
1427 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001428 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001429fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1430{
1431 pt->pt_func = ufunc;
1432 pt->pt_refcount = 1;
1433
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001434 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001435 {
1436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1437 + ectx->ec_dfunc_idx;
1438
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001439 // The closure may need to find arguments and local variables in the
1440 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001441 pt->pt_outer.out_stack = &ectx->ec_stack;
1442 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001443 if (ectx->ec_outer_ref != NULL)
1444 {
1445 // The current context already has a context, link to that one.
1446 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1447 if (ectx->ec_outer_ref->or_partial != NULL)
1448 {
1449 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1450 ++pt->pt_outer.out_up_partial->pt_refcount;
1451 }
1452 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001453
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001454 // If this function returns and the closure is still being used, we
1455 // need to make a copy of the context (arguments and local variables).
1456 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001457 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001458 {
1459 vim_free(pt);
1460 return FAIL;
1461 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001462 // Extra variable keeps the count of closures created in the current
1463 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001464 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1465 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1466
1467 ((partial_T **)ectx->ec_funcrefs.ga_data)
1468 [ectx->ec_funcrefs.ga_len] = pt;
1469 ++pt->pt_refcount;
1470 ++ectx->ec_funcrefs.ga_len;
1471 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001472 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001473 return OK;
1474}
1475
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001476/*
1477 * Execute iptr->isn_arg.string as an Ex command.
1478 */
1479 static int
1480exec_command(isn_T *iptr)
1481{
1482 source_cookie_T cookie;
1483
1484 SOURCING_LNUM = iptr->isn_lnum;
1485 // Pass getsourceline to get an error for a missing ":end"
1486 // command.
1487 CLEAR_FIELD(cookie);
1488 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1489 if (do_cmdline(iptr->isn_arg.string,
1490 getsourceline, &cookie,
1491 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1492 || did_emsg)
1493 return FAIL;
1494 return OK;
1495}
1496
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001497// used for v_instr of typval of VAR_INSTR
1498struct instr_S {
1499 ectx_T *instr_ectx;
1500 isn_T *instr_instr;
1501};
1502
Bram Moolenaar4c137212021-04-19 16:48:48 +02001503// used for substitute_instr
1504typedef struct subs_expr_S {
1505 ectx_T *subs_ectx;
1506 isn_T *subs_instr;
1507 int subs_status;
1508} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509
1510// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001511#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512
1513// Get pointer to item at the bottom of the stack, -1 is the bottom.
1514#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001515#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 +01001516
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001517// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001518#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 +01001519
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001520// Set when calling do_debug().
1521static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001522static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001523
1524/*
1525 * When debugging lookup "name" and return the typeval.
1526 * When not found return NULL.
1527 */
1528 typval_T *
1529lookup_debug_var(char_u *name)
1530{
1531 int idx;
1532 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001533 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001534 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001535 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001536
1537 if (ectx == NULL)
1538 return NULL;
1539 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1540
1541 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001542 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001543 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001544 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001545 return STACK_TV_VAR(idx);
1546 }
1547
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001548 // Go through argument names.
1549 ufunc = dfunc->df_ufunc;
1550 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1551 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1552 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1553 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1554 - varargs_off + idx);
1555 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1556 return STACK_TV(ectx->ec_frame_idx - 1);
1557
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001558 return NULL;
1559}
1560
Bram Moolenaar26a44842021-09-02 18:49:06 +02001561/*
1562 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1563 * breakpoint was set in that function or when there is any expression.
1564 */
1565 int
1566may_break_in_function(ufunc_T *ufunc)
1567{
1568 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1569}
1570
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001571 static void
1572handle_debug(isn_T *iptr, ectx_T *ectx)
1573{
1574 char_u *line;
1575 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1576 + ectx->ec_dfunc_idx)->df_ufunc;
1577 isn_T *ni;
1578 int end_lnum = iptr->isn_lnum;
1579 garray_T ga;
1580 int lnum;
1581
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001582 if (ex_nesting_level > debug_break_level)
1583 {
1584 linenr_T breakpoint;
1585
Bram Moolenaar26a44842021-09-02 18:49:06 +02001586 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001587 return;
1588
1589 // check for the next breakpoint if needed
1590 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001591 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001592 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1593 return;
1594 }
1595
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001596 SOURCING_LNUM = iptr->isn_lnum;
1597 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001598 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001599
1600 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1601 if (ni->isn_type == ISN_DEBUG
1602 || ni->isn_type == ISN_RETURN
1603 || ni->isn_type == ISN_RETURN_VOID)
1604 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001605 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001606 break;
1607 }
1608
1609 if (end_lnum > iptr->isn_lnum)
1610 {
1611 ga_init2(&ga, sizeof(char_u *), 10);
1612 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001613 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001614 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001615
Bram Moolenaar303215d2021-07-07 20:10:43 +02001616 if (p == NULL)
1617 continue; // left over from continuation line
1618 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001619 if (*p == '#')
1620 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001621 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001622 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1623 if (STRNCMP(p, "def ", 4) == 0)
1624 break;
1625 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001626 line = ga_concat_strings(&ga, " ");
1627 vim_free(ga.ga_data);
1628 }
1629 else
1630 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001631
Dominique Pelle6e969552021-06-17 13:53:41 +02001632 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001633 debug_context = NULL;
1634
1635 if (end_lnum > iptr->isn_lnum)
1636 vim_free(line);
1637}
1638
Bram Moolenaar4c137212021-04-19 16:48:48 +02001639/*
1640 * Execute instructions in execution context "ectx".
1641 * Return OK or FAIL;
1642 */
1643 static int
1644exec_instructions(ectx_T *ectx)
1645{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001646 int ret = FAIL;
1647 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001648 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001649
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001650 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001651 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001652
Bram Moolenaarff652882021-05-16 15:24:49 +02001653 // Only catch exceptions in this instruction list.
1654 ectx->ec_trylevel_at_start = trylevel;
1655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 for (;;)
1657 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001658 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001660 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001661
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001662 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001663 {
1664 line_breakcheck();
1665 breakcheck_count = 0;
1666 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001667 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001668 {
1669 // Turn CTRL-C into an exception.
1670 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001671 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001672 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001673 did_throw = TRUE;
1674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001676 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001677 {
1678 // Turn an error message into an exception.
1679 did_emsg = FALSE;
1680 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001681 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001682 did_throw = TRUE;
1683 *msg_list = NULL;
1684 }
1685
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001686 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001688 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001689 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001690 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
1692 // An exception jumps to the first catch, finally, or returns from
1693 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001694 while (index > 0)
1695 {
1696 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001697 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001698 break;
1699 // In the catch and finally block of this try we have to go up
1700 // one level.
1701 --index;
1702 trycmd = NULL;
1703 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001704 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001706 if (trycmd->tcd_in_catch)
1707 {
1708 // exception inside ":catch", jump to ":finally" once
1709 ectx->ec_iidx = trycmd->tcd_finally_idx;
1710 trycmd->tcd_finally_idx = 0;
1711 }
1712 else
1713 // jump to first ":catch"
1714 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001715 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001716 did_throw = FALSE; // don't come back here until :endtry
1717 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 }
1719 else
1720 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001721 // Not inside try or need to return from current functions.
1722 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001723 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001724 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001725 tv = STACK_TV_BOT(0);
1726 tv->v_type = VAR_NUMBER;
1727 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001728 ++ectx->ec_stack.ga_len;
1729 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001731 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001732 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001733 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001734 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 goto done;
1736 }
1737
Bram Moolenaar4c137212021-04-19 16:48:48 +02001738 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001739 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 }
1741 continue;
1742 }
1743
Bram Moolenaar4c137212021-04-19 16:48:48 +02001744 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 switch (iptr->isn_type)
1746 {
1747 // execute Ex command line
1748 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001749 if (exec_command(iptr) == FAIL)
1750 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 break;
1752
Bram Moolenaar20677332021-06-06 17:02:53 +02001753 // execute Ex command line split at NL characters.
1754 case ISN_EXEC_SPLIT:
1755 {
1756 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001757 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001758
1759 SOURCING_LNUM = iptr->isn_lnum;
1760 CLEAR_FIELD(cookie);
1761 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1762 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001763 line = get_split_sourceline(0, &cookie, 0, 0);
1764 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001765 get_split_sourceline, &cookie,
1766 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1767 == FAIL
1768 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001769 {
1770 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001771 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001772 }
1773 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001774 }
1775 break;
1776
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001777 // execute Ex command line that is only a range
1778 case ISN_EXECRANGE:
1779 {
1780 exarg_T ea;
1781 char *error = NULL;
1782
1783 CLEAR_FIELD(ea);
1784 ea.cmdidx = CMD_SIZE;
1785 ea.addr_type = ADDR_LINES;
1786 ea.cmd = iptr->isn_arg.string;
1787 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00001788 if (ea.cmd == NULL)
1789 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001790 if (error == NULL)
1791 error = ex_range_without_command(&ea);
1792 if (error != NULL)
1793 {
1794 SOURCING_LNUM = iptr->isn_lnum;
1795 emsg(error);
1796 goto on_error;
1797 }
1798 }
1799 break;
1800
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001801 // Evaluate an expression with legacy syntax, push it onto the
1802 // stack.
1803 case ISN_LEGACY_EVAL:
1804 {
1805 char_u *arg = iptr->isn_arg.string;
1806 int res;
1807 int save_flags = cmdmod.cmod_flags;
1808
Bram Moolenaar35578162021-08-02 19:10:38 +02001809 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001810 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001811 tv = STACK_TV_BOT(0);
1812 init_tv(tv);
1813 cmdmod.cmod_flags |= CMOD_LEGACY;
1814 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1815 cmdmod.cmod_flags = save_flags;
1816 if (res == FAIL)
1817 goto on_error;
1818 ++ectx->ec_stack.ga_len;
1819 }
1820 break;
1821
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001822 // push typeval VAR_INSTR with instructions to be executed
1823 case ISN_INSTR:
1824 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001825 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001826 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001827 tv = STACK_TV_BOT(0);
1828 tv->vval.v_instr = ALLOC_ONE(instr_T);
1829 if (tv->vval.v_instr == NULL)
1830 goto on_error;
1831 ++ectx->ec_stack.ga_len;
1832
1833 tv->v_type = VAR_INSTR;
1834 tv->vval.v_instr->instr_ectx = ectx;
1835 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1836 }
1837 break;
1838
Bram Moolenaar4c137212021-04-19 16:48:48 +02001839 // execute :substitute with an expression
1840 case ISN_SUBSTITUTE:
1841 {
1842 subs_T *subs = &iptr->isn_arg.subs;
1843 source_cookie_T cookie;
1844 struct subs_expr_S *save_instr = substitute_instr;
1845 struct subs_expr_S subs_instr;
1846 int res;
1847
1848 subs_instr.subs_ectx = ectx;
1849 subs_instr.subs_instr = subs->subs_instr;
1850 subs_instr.subs_status = OK;
1851 substitute_instr = &subs_instr;
1852
1853 SOURCING_LNUM = iptr->isn_lnum;
1854 // This is very much like ISN_EXEC
1855 CLEAR_FIELD(cookie);
1856 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1857 res = do_cmdline(subs->subs_cmd,
1858 getsourceline, &cookie,
1859 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1860 substitute_instr = save_instr;
1861
1862 if (res == FAIL || did_emsg
1863 || subs_instr.subs_status == FAIL)
1864 goto on_error;
1865 }
1866 break;
1867
1868 case ISN_FINISH:
1869 goto done;
1870
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001871 case ISN_REDIRSTART:
1872 // create a dummy entry for var_redir_str()
1873 if (alloc_redir_lval() == FAIL)
1874 goto on_error;
1875
1876 // The output is stored in growarray "redir_ga" until
1877 // redirection ends.
1878 init_redir_ga();
1879 redir_vname = 1;
1880 break;
1881
1882 case ISN_REDIREND:
1883 {
1884 char_u *res = get_clear_redir_ga();
1885
1886 // End redirection, put redirected text on the stack.
1887 clear_redir_lval();
1888 redir_vname = 0;
1889
Bram Moolenaar35578162021-08-02 19:10:38 +02001890 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001891 {
1892 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001893 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001894 }
1895 tv = STACK_TV_BOT(0);
1896 tv->v_type = VAR_STRING;
1897 tv->vval.v_string = res;
1898 ++ectx->ec_stack.ga_len;
1899 }
1900 break;
1901
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001902 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001903#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001904 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1905 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001906#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001907 break;
1908
1909 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001910#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001911 {
1912 exarg_T ea;
1913 int res;
1914
1915 CLEAR_FIELD(ea);
1916 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1917 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1918 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1919 --ectx->ec_stack.ga_len;
1920 tv = STACK_TV_BOT(0);
1921 res = cexpr_core(&ea, tv);
1922 clear_tv(tv);
1923 if (res == FAIL)
1924 goto on_error;
1925 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001926#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001927 break;
1928
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001929 // execute Ex command from pieces on the stack
1930 case ISN_EXECCONCAT:
1931 {
1932 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001933 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001934 int pass;
1935 int i;
1936 char_u *cmd = NULL;
1937 char_u *str;
1938
1939 for (pass = 1; pass <= 2; ++pass)
1940 {
1941 for (i = 0; i < count; ++i)
1942 {
1943 tv = STACK_TV_BOT(i - count);
1944 str = tv->vval.v_string;
1945 if (str != NULL && *str != NUL)
1946 {
1947 if (pass == 2)
1948 STRCPY(cmd + len, str);
1949 len += STRLEN(str);
1950 }
1951 if (pass == 2)
1952 clear_tv(tv);
1953 }
1954 if (pass == 1)
1955 {
1956 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001957 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001958 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001959 len = 0;
1960 }
1961 }
1962
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001963 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001964 do_cmdline_cmd(cmd);
1965 vim_free(cmd);
1966 }
1967 break;
1968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 // execute :echo {string} ...
1970 case ISN_ECHO:
1971 {
1972 int count = iptr->isn_arg.echo.echo_count;
1973 int atstart = TRUE;
1974 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001975 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976
1977 for (idx = 0; idx < count; ++idx)
1978 {
1979 tv = STACK_TV_BOT(idx - count);
1980 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1981 &atstart, &needclr);
1982 clear_tv(tv);
1983 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001984 if (needclr)
1985 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001986 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 }
1988 break;
1989
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001990 // :execute {string} ...
1991 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02001992 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001993 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001994 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001995 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02001996 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001997 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001998 {
1999 int count = iptr->isn_arg.number;
2000 garray_T ga;
2001 char_u buf[NUMBUFLEN];
2002 char_u *p;
2003 int len;
2004 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002005 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002006
2007 ga_init2(&ga, 1, 80);
2008 for (idx = 0; idx < count; ++idx)
2009 {
2010 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002011 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002012 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002013 if (tv->v_type == VAR_CHANNEL
2014 || tv->v_type == VAR_JOB)
2015 {
2016 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002017 semsg(_(e_using_invalid_value_as_string_str),
2018 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002019 break;
2020 }
2021 else
2022 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002023 }
2024 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002025 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002026
2027 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002028 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002029 failed = TRUE;
2030 else
2031 {
2032 if (ga.ga_len > 0)
2033 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2034 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2035 ga.ga_len += len;
2036 }
2037 clear_tv(tv);
2038 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002039 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002040 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002041 {
2042 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002043 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002044 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002045
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002046 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002047 {
2048 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002049 {
2050 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002051 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002052 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002053 {
2054 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002055 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002056 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002057 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002058 else
2059 {
2060 msg_sb_eol();
2061 if (iptr->isn_type == ISN_ECHOMSG)
2062 {
2063 msg_attr(ga.ga_data, echo_attr);
2064 out_flush();
2065 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002066 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2067 {
2068 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2069 TRUE);
2070 ui_write((char_u *)"\r\n", 2, TRUE);
2071 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002072 else
2073 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002074 SOURCING_LNUM = iptr->isn_lnum;
2075 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002076 }
2077 }
2078 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002079 ga_clear(&ga);
2080 }
2081 break;
2082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 // load local variable or argument
2084 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002085 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002086 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002088 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 break;
2090
2091 // load v: variable
2092 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002093 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002094 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002095 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002096 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 break;
2098
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002099 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 case ISN_LOADSCRIPT:
2101 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002102 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 svar_T *sv;
2104
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002105 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002106 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002107 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002108 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002109 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002110 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002112 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 }
2114 break;
2115
2116 // load s: variable in old script
2117 case ISN_LOADS:
2118 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002119 hashtab_T *ht = &SCRIPT_VARS(
2120 iptr->isn_arg.loadstore.ls_sid);
2121 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 if (di == NULL)
2125 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002126 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002127 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002128 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 }
2130 else
2131 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002132 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002133 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002135 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 }
2137 }
2138 break;
2139
Bram Moolenaard3aac292020-04-19 14:32:17 +02002140 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002142 case ISN_LOADB:
2143 case ISN_LOADW:
2144 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002146 dictitem_T *di = NULL;
2147 hashtab_T *ht = NULL;
2148 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002149
Bram Moolenaard3aac292020-04-19 14:32:17 +02002150 switch (iptr->isn_type)
2151 {
2152 case ISN_LOADG:
2153 ht = get_globvar_ht();
2154 namespace = 'g';
2155 break;
2156 case ISN_LOADB:
2157 ht = &curbuf->b_vars->dv_hashtab;
2158 namespace = 'b';
2159 break;
2160 case ISN_LOADW:
2161 ht = &curwin->w_vars->dv_hashtab;
2162 namespace = 'w';
2163 break;
2164 case ISN_LOADT:
2165 ht = &curtab->tp_vars->dv_hashtab;
2166 namespace = 't';
2167 break;
2168 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002169 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002170 }
2171 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 if (di == NULL)
2174 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002175 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002176 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002177 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002178 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 }
2180 else
2181 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002182 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002183 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002185 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 }
2187 }
2188 break;
2189
Bram Moolenaar03290b82020-12-19 16:30:44 +01002190 // load autoload variable
2191 case ISN_LOADAUTO:
2192 {
2193 char_u *name = iptr->isn_arg.string;
2194
Bram Moolenaar35578162021-08-02 19:10:38 +02002195 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002196 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002197 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002198 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002199 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002200 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002201 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002202 }
2203 break;
2204
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002205 // load g:/b:/w:/t: namespace
2206 case ISN_LOADGDICT:
2207 case ISN_LOADBDICT:
2208 case ISN_LOADWDICT:
2209 case ISN_LOADTDICT:
2210 {
2211 dict_T *d = NULL;
2212
2213 switch (iptr->isn_type)
2214 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002215 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2216 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2217 case ISN_LOADWDICT: d = curwin->w_vars; break;
2218 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002219 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002220 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002221 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002222 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002223 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002224 tv = STACK_TV_BOT(0);
2225 tv->v_type = VAR_DICT;
2226 tv->v_lock = 0;
2227 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002228 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002229 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002230 }
2231 break;
2232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 // load &option
2234 case ISN_LOADOPT:
2235 {
2236 typval_T optval;
2237 char_u *name = iptr->isn_arg.string;
2238
Bram Moolenaara8c17702020-04-01 21:17:24 +02002239 // This is not expected to fail, name is checked during
2240 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002241 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002242 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002243 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002244 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002246 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 }
2248 break;
2249
2250 // load $ENV
2251 case ISN_LOADENV:
2252 {
2253 typval_T optval;
2254 char_u *name = iptr->isn_arg.string;
2255
Bram Moolenaar35578162021-08-02 19:10:38 +02002256 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002257 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002258 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002259 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002261 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 }
2263 break;
2264
2265 // load @register
2266 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002267 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002268 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 tv = STACK_TV_BOT(0);
2270 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002271 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002272 // This may result in NULL, which should be equivalent to an
2273 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 tv->vval.v_string = get_reg_contents(
2275 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002276 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 break;
2278
2279 // store local variable
2280 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002281 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 tv = STACK_TV_VAR(iptr->isn_arg.number);
2283 clear_tv(tv);
2284 *tv = *STACK_TV_BOT(0);
2285 break;
2286
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002287 // store s: variable in old script
2288 case ISN_STORES:
2289 {
2290 hashtab_T *ht = &SCRIPT_VARS(
2291 iptr->isn_arg.loadstore.ls_sid);
2292 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002293 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002294
Bram Moolenaar4c137212021-04-19 16:48:48 +02002295 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002296 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002297 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002298 else
2299 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002300 SOURCING_LNUM = iptr->isn_lnum;
2301 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002302 {
2303 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002304 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002305 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002306 clear_tv(&di->di_tv);
2307 di->di_tv = *STACK_TV_BOT(0);
2308 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002309 }
2310 break;
2311
2312 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 case ISN_STORESCRIPT:
2314 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002315 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2316 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002318 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002319 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002320 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002321 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002322
2323 // "const" and "final" are checked at compile time, locking
2324 // the value needs to be checked here.
2325 SOURCING_LNUM = iptr->isn_lnum;
2326 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002327 {
2328 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002329 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002330 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 clear_tv(sv->sv_tv);
2333 *sv->sv_tv = *STACK_TV_BOT(0);
2334 }
2335 break;
2336
2337 // store option
2338 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002339 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002341 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
2342 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 long n = 0;
2344 char_u *s = NULL;
2345 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002346 char_u numbuf[NUMBUFLEN];
2347 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348
Bram Moolenaar4c137212021-04-19 16:48:48 +02002349 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 tv = STACK_TV_BOT(0);
2351 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002352 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002354 if (s == NULL)
2355 s = (char_u *)"";
2356 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002357 else if (iptr->isn_type == ISN_STOREFUNCOPT)
2358 {
2359 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002360 // If the option can be set to a function reference or
2361 // a lambda and the passed value is a function
2362 // reference, then convert it to the name (string) of
2363 // the function reference.
2364 s = tv2string(tv, &tofree, numbuf, 0);
2365 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002366 {
2367 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002368 goto on_error;
2369 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002372 // must be VAR_NUMBER, CHECKTYPE makes sure
2373 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002374 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002375 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002376 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 if (msg != NULL)
2378 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002379 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002381 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 }
2384 break;
2385
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002386 // store $ENV
2387 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002388 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002389 tv = STACK_TV_BOT(0);
2390 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2391 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002392 break;
2393
2394 // store @r
2395 case ISN_STOREREG:
2396 {
2397 int reg = iptr->isn_arg.number;
2398
Bram Moolenaar4c137212021-04-19 16:48:48 +02002399 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002400 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002401 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002402 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002403 }
2404 break;
2405
2406 // store v: variable
2407 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002408 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002409 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2410 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002411 // should not happen, type is checked when compiling
2412 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002413 break;
2414
Bram Moolenaard3aac292020-04-19 14:32:17 +02002415 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002417 case ISN_STOREB:
2418 case ISN_STOREW:
2419 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002421 dictitem_T *di;
2422 hashtab_T *ht;
2423 char_u *name = iptr->isn_arg.string + 2;
2424
Bram Moolenaard3aac292020-04-19 14:32:17 +02002425 switch (iptr->isn_type)
2426 {
2427 case ISN_STOREG:
2428 ht = get_globvar_ht();
2429 break;
2430 case ISN_STOREB:
2431 ht = &curbuf->b_vars->dv_hashtab;
2432 break;
2433 case ISN_STOREW:
2434 ht = &curwin->w_vars->dv_hashtab;
2435 break;
2436 case ISN_STORET:
2437 ht = &curtab->tp_vars->dv_hashtab;
2438 break;
2439 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002440 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442
Bram Moolenaar4c137212021-04-19 16:48:48 +02002443 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002444 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002446 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 else
2448 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002449 SOURCING_LNUM = iptr->isn_lnum;
2450 if (var_check_permission(di, name) == FAIL)
2451 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 clear_tv(&di->di_tv);
2453 di->di_tv = *STACK_TV_BOT(0);
2454 }
2455 }
2456 break;
2457
Bram Moolenaar03290b82020-12-19 16:30:44 +01002458 // store an autoload variable
2459 case ISN_STOREAUTO:
2460 SOURCING_LNUM = iptr->isn_lnum;
2461 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2462 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002463 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002464 break;
2465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 // store number in local variable
2467 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002468 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 clear_tv(tv);
2470 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002471 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 break;
2473
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002474 // store value in list or dict variable
2475 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002476 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002477 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002478 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002479 typval_T *tv_dest = STACK_TV_BOT(-1);
2480 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002481
Bram Moolenaar752fc692021-01-04 21:57:11 +01002482 // Stack contains:
2483 // -3 value to be stored
2484 // -2 index
2485 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002486 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002487 SOURCING_LNUM = iptr->isn_lnum;
2488 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002489 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002490 dest_type = tv_dest->v_type;
2491 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002492 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002493 else if (dest_type == VAR_LIST
2494 && tv_idx->v_type != VAR_NUMBER)
2495 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002496 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002497 status = FAIL;
2498 }
2499 }
2500 else if (dest_type != tv_dest->v_type)
2501 {
2502 // just in case, should be OK
2503 semsg(_(e_expected_str_but_got_str),
2504 vartype_name(dest_type),
2505 vartype_name(tv_dest->v_type));
2506 status = FAIL;
2507 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002508
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002509 if (status == OK && dest_type == VAR_LIST)
2510 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002511 long lidx = (long)tv_idx->vval.v_number;
2512 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002513
2514 if (list == NULL)
2515 {
2516 emsg(_(e_list_not_set));
2517 goto on_error;
2518 }
2519 if (lidx < 0 && list->lv_len + lidx >= 0)
2520 // negative index is relative to the end
2521 lidx = list->lv_len + lidx;
2522 if (lidx < 0 || lidx > list->lv_len)
2523 {
2524 semsg(_(e_listidx), lidx);
2525 goto on_error;
2526 }
2527 if (lidx < list->lv_len)
2528 {
2529 listitem_T *li = list_find(list, lidx);
2530
2531 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002532 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002533 goto on_error;
2534 // overwrite existing list item
2535 clear_tv(&li->li_tv);
2536 li->li_tv = *tv;
2537 }
2538 else
2539 {
2540 if (error_if_locked(list->lv_lock,
2541 e_cannot_change_list))
2542 goto on_error;
2543 // append to list, only fails when out of memory
2544 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002545 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002546 clear_tv(tv);
2547 }
2548 }
2549 else if (status == OK && dest_type == VAR_DICT)
2550 {
2551 char_u *key = tv_idx->vval.v_string;
2552 dict_T *dict = tv_dest->vval.v_dict;
2553 dictitem_T *di;
2554
2555 SOURCING_LNUM = iptr->isn_lnum;
2556 if (dict == NULL)
2557 {
2558 emsg(_(e_dictionary_not_set));
2559 goto on_error;
2560 }
2561 if (key == NULL)
2562 key = (char_u *)"";
2563 di = dict_find(dict, key, -1);
2564 if (di != NULL)
2565 {
2566 if (error_if_locked(di->di_tv.v_lock,
2567 e_cannot_change_dict_item))
2568 goto on_error;
2569 // overwrite existing value
2570 clear_tv(&di->di_tv);
2571 di->di_tv = *tv;
2572 }
2573 else
2574 {
2575 if (error_if_locked(dict->dv_lock,
2576 e_cannot_change_dict))
2577 goto on_error;
2578 // add to dict, only fails when out of memory
2579 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002580 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002581 clear_tv(tv);
2582 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002583 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002584 else if (status == OK && dest_type == VAR_BLOB)
2585 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002586 long lidx = (long)tv_idx->vval.v_number;
2587 blob_T *blob = tv_dest->vval.v_blob;
2588 varnumber_T nr;
2589 int error = FALSE;
2590 int len;
2591
2592 if (blob == NULL)
2593 {
2594 emsg(_(e_blob_not_set));
2595 goto on_error;
2596 }
2597 len = blob_len(blob);
2598 if (lidx < 0 && len + lidx >= 0)
2599 // negative index is relative to the end
2600 lidx = len + lidx;
2601
2602 // Can add one byte at the end.
2603 if (lidx < 0 || lidx > len)
2604 {
2605 semsg(_(e_blobidx), lidx);
2606 goto on_error;
2607 }
2608 if (value_check_lock(blob->bv_lock,
2609 (char_u *)"blob", FALSE))
2610 goto on_error;
2611 nr = tv_get_number_chk(tv, &error);
2612 if (error)
2613 goto on_error;
2614 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002615 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002616 else
2617 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002618 status = FAIL;
2619 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002620 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002621
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002622 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002623 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002624 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002625 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002626 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002627 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002628 goto on_error;
2629 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002630 }
2631 break;
2632
Bram Moolenaar68452172021-04-12 21:21:02 +02002633 // store value in blob range
2634 case ISN_STORERANGE:
2635 {
2636 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2637 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2638 typval_T *tv_dest = STACK_TV_BOT(-1);
2639 int status = OK;
2640
2641 // Stack contains:
2642 // -4 value to be stored
2643 // -3 first index or "none"
2644 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002645 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002646 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002647 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002648 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002649 long n1;
2650 long n2;
2651 int error = FALSE;
2652
2653 SOURCING_LNUM = iptr->isn_lnum;
2654 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2655 if (error)
2656 status = FAIL;
2657 else
2658 {
2659 if (tv_idx2->v_type == VAR_SPECIAL
2660 && tv_idx2->vval.v_number == VVAL_NONE)
2661 n2 = list_len(tv_dest->vval.v_list) - 1;
2662 else
2663 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2664 if (error)
2665 status = FAIL;
2666 else
2667 {
2668 listitem_T *li1 = check_range_index_one(
2669 tv_dest->vval.v_list, &n1, FALSE);
2670
2671 if (li1 == NULL)
2672 status = FAIL;
2673 else
2674 {
2675 status = check_range_index_two(
2676 tv_dest->vval.v_list,
2677 &n1, li1, &n2, FALSE);
2678 if (status != FAIL)
2679 status = list_assign_range(
2680 tv_dest->vval.v_list,
2681 tv->vval.v_list,
2682 n1,
2683 n2,
2684 tv_idx2->v_type == VAR_SPECIAL,
2685 (char_u *)"=",
2686 (char_u *)"[unknown]");
2687 }
2688 }
2689 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002690 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002691 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002692 {
2693 varnumber_T n1;
2694 varnumber_T n2;
2695 int error = FALSE;
2696
2697 n1 = tv_get_number_chk(tv_idx1, &error);
2698 if (error)
2699 status = FAIL;
2700 else
2701 {
2702 if (tv_idx2->v_type == VAR_SPECIAL
2703 && tv_idx2->vval.v_number == VVAL_NONE)
2704 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2705 else
2706 n2 = tv_get_number_chk(tv_idx2, &error);
2707 if (error)
2708 status = FAIL;
2709 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002710 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002711 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002712
2713 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002714 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002715 || check_blob_range(bloblen,
2716 n1, n2, FALSE) == FAIL)
2717 status = FAIL;
2718 else
2719 status = blob_set_range(
2720 tv_dest->vval.v_blob, n1, n2, tv);
2721 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002722 }
2723 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002724 else
2725 {
2726 status = FAIL;
2727 emsg(_(e_blob_required));
2728 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002729
2730 clear_tv(tv_idx1);
2731 clear_tv(tv_idx2);
2732 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002733 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002734 clear_tv(tv);
2735
2736 if (status == FAIL)
2737 goto on_error;
2738 }
2739 break;
2740
Bram Moolenaar0186e582021-01-10 18:33:11 +01002741 // load or store variable or argument from outer scope
2742 case ISN_LOADOUTER:
2743 case ISN_STOREOUTER:
2744 {
2745 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002746 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2747 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002748
2749 while (depth > 1 && outer != NULL)
2750 {
2751 outer = outer->out_up;
2752 --depth;
2753 }
2754 if (outer == NULL)
2755 {
2756 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00002757 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
2758 || ectx->ec_outer_ref == NULL)
2759 // Possibly :def function called from legacy
2760 // context.
2761 emsg(_(e_closure_called_from_invalid_context));
2762 else
2763 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002764 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002765 }
2766 tv = ((typval_T *)outer->out_stack->ga_data)
2767 + outer->out_frame_idx + STACK_FRAME_SIZE
2768 + iptr->isn_arg.outer.outer_idx;
2769 if (iptr->isn_type == ISN_LOADOUTER)
2770 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002771 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002772 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002773 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002774 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002775 }
2776 else
2777 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002778 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002779 clear_tv(tv);
2780 *tv = *STACK_TV_BOT(0);
2781 }
2782 }
2783 break;
2784
Bram Moolenaar752fc692021-01-04 21:57:11 +01002785 // unlet item in list or dict variable
2786 case ISN_UNLETINDEX:
2787 {
2788 typval_T *tv_idx = STACK_TV_BOT(-2);
2789 typval_T *tv_dest = STACK_TV_BOT(-1);
2790 int status = OK;
2791
2792 // Stack contains:
2793 // -2 index
2794 // -1 dict or list
2795 if (tv_dest->v_type == VAR_DICT)
2796 {
2797 // unlet a dict item, index must be a string
2798 if (tv_idx->v_type != VAR_STRING)
2799 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002800 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002801 semsg(_(e_expected_str_but_got_str),
2802 vartype_name(VAR_STRING),
2803 vartype_name(tv_idx->v_type));
2804 status = FAIL;
2805 }
2806 else
2807 {
2808 dict_T *d = tv_dest->vval.v_dict;
2809 char_u *key = tv_idx->vval.v_string;
2810 dictitem_T *di = NULL;
2811
2812 if (key == NULL)
2813 key = (char_u *)"";
2814 if (d != NULL)
2815 di = dict_find(d, key, (int)STRLEN(key));
2816 if (di == NULL)
2817 {
2818 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002819 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002820 semsg(_(e_dictkey), key);
2821 status = FAIL;
2822 }
2823 else
2824 {
2825 // TODO: check for dict or item locked
2826 dictitem_remove(d, di);
2827 }
2828 }
2829 }
2830 else if (tv_dest->v_type == VAR_LIST)
2831 {
2832 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002833 SOURCING_LNUM = iptr->isn_lnum;
2834 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002835 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002836 status = FAIL;
2837 }
2838 else
2839 {
2840 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002841 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002842 listitem_T *li = NULL;
2843
2844 li = list_find(l, n);
2845 if (li == NULL)
2846 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002847 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002848 semsg(_(e_listidx), n);
2849 status = FAIL;
2850 }
2851 else
2852 // TODO: check for list or item locked
2853 listitem_remove(l, li);
2854 }
2855 }
2856 else
2857 {
2858 status = FAIL;
2859 semsg(_(e_cannot_index_str),
2860 vartype_name(tv_dest->v_type));
2861 }
2862
2863 clear_tv(tv_idx);
2864 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002865 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002866 if (status == FAIL)
2867 goto on_error;
2868 }
2869 break;
2870
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002871 // unlet range of items in list variable
2872 case ISN_UNLETRANGE:
2873 {
2874 // Stack contains:
2875 // -3 index1
2876 // -2 index2
2877 // -1 dict or list
2878 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2879 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2880 typval_T *tv_dest = STACK_TV_BOT(-1);
2881 int status = OK;
2882
2883 if (tv_dest->v_type == VAR_LIST)
2884 {
2885 // indexes must be a number
2886 SOURCING_LNUM = iptr->isn_lnum;
2887 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002888 || (tv_idx2->v_type != VAR_SPECIAL
2889 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002890 {
2891 status = FAIL;
2892 }
2893 else
2894 {
2895 list_T *l = tv_dest->vval.v_list;
2896 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002897 long n2 = tv_idx2->v_type == VAR_SPECIAL
2898 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002899 listitem_T *li;
2900
2901 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002902 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002903 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002904 else
2905 {
2906 if (n1 < 0)
2907 n1 = list_idx_of_item(l, li);
2908 if (n2 < 0)
2909 {
2910 listitem_T *li2 = list_find(l, n2);
2911
2912 if (li2 == NULL)
2913 status = FAIL;
2914 else
2915 n2 = list_idx_of_item(l, li2);
2916 }
2917 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002918 && tv_idx2->v_type != VAR_SPECIAL
2919 && n2 < n1)
2920 {
2921 semsg(_(e_listidx), n2);
2922 status = FAIL;
2923 }
2924 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002925 && list_unlet_range(l, li, NULL, n1,
2926 tv_idx2->v_type != VAR_SPECIAL, n2)
2927 == FAIL)
2928 status = FAIL;
2929 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002930 }
2931 }
2932 else
2933 {
2934 status = FAIL;
2935 SOURCING_LNUM = iptr->isn_lnum;
2936 semsg(_(e_cannot_index_str),
2937 vartype_name(tv_dest->v_type));
2938 }
2939
2940 clear_tv(tv_idx1);
2941 clear_tv(tv_idx2);
2942 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002943 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002944 if (status == FAIL)
2945 goto on_error;
2946 }
2947 break;
2948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 // push constant
2950 case ISN_PUSHNR:
2951 case ISN_PUSHBOOL:
2952 case ISN_PUSHSPEC:
2953 case ISN_PUSHF:
2954 case ISN_PUSHS:
2955 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002956 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002957 case ISN_PUSHCHANNEL:
2958 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02002959 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002960 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002962 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002963 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 switch (iptr->isn_type)
2965 {
2966 case ISN_PUSHNR:
2967 tv->v_type = VAR_NUMBER;
2968 tv->vval.v_number = iptr->isn_arg.number;
2969 break;
2970 case ISN_PUSHBOOL:
2971 tv->v_type = VAR_BOOL;
2972 tv->vval.v_number = iptr->isn_arg.number;
2973 break;
2974 case ISN_PUSHSPEC:
2975 tv->v_type = VAR_SPECIAL;
2976 tv->vval.v_number = iptr->isn_arg.number;
2977 break;
2978#ifdef FEAT_FLOAT
2979 case ISN_PUSHF:
2980 tv->v_type = VAR_FLOAT;
2981 tv->vval.v_float = iptr->isn_arg.fnumber;
2982 break;
2983#endif
2984 case ISN_PUSHBLOB:
2985 blob_copy(iptr->isn_arg.blob, tv);
2986 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002987 case ISN_PUSHFUNC:
2988 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002989 if (iptr->isn_arg.string == NULL)
2990 tv->vval.v_string = NULL;
2991 else
2992 tv->vval.v_string =
2993 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002994 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002995 case ISN_PUSHCHANNEL:
2996#ifdef FEAT_JOB_CHANNEL
2997 tv->v_type = VAR_CHANNEL;
2998 tv->vval.v_channel = iptr->isn_arg.channel;
2999 if (tv->vval.v_channel != NULL)
3000 ++tv->vval.v_channel->ch_refcount;
3001#endif
3002 break;
3003 case ISN_PUSHJOB:
3004#ifdef FEAT_JOB_CHANNEL
3005 tv->v_type = VAR_JOB;
3006 tv->vval.v_job = iptr->isn_arg.job;
3007 if (tv->vval.v_job != NULL)
3008 ++tv->vval.v_job->jv_refcount;
3009#endif
3010 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 default:
3012 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003013 tv->vval.v_string = vim_strsave(
3014 iptr->isn_arg.string == NULL
3015 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 }
3017 break;
3018
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003019 case ISN_UNLET:
3020 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3021 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003022 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003023 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003024 case ISN_UNLETENV:
3025 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3026 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003027
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003028 case ISN_LOCKUNLOCK:
3029 {
3030 typval_T *lval_root_save = lval_root;
3031 int res;
3032
3033 // Stack has the local variable, argument the whole :lock
3034 // or :unlock command, like ISN_EXEC.
3035 --ectx->ec_stack.ga_len;
3036 lval_root = STACK_TV_BOT(0);
3037 res = exec_command(iptr);
3038 clear_tv(lval_root);
3039 lval_root = lval_root_save;
3040 if (res == FAIL)
3041 goto on_error;
3042 }
3043 break;
3044
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003045 case ISN_LOCKCONST:
3046 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3047 break;
3048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 // create a list from items on the stack; uses a single allocation
3050 // for the list header and the items
3051 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003052 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003053 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 break;
3055
3056 // create a dict from items on the stack
3057 case ISN_NEWDICT:
3058 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003059 int count = iptr->isn_arg.number;
3060 dict_T *dict = dict_alloc();
3061 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003062 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003063 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003065 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003066 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 for (idx = 0; idx < count; ++idx)
3068 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003069 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003071 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003072 key = tv->vval.v_string == NULL
3073 ? (char_u *)"" : tv->vval.v_string;
3074 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003075 if (item != NULL)
3076 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003077 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003078 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003079 dict_unref(dict);
3080 goto on_error;
3081 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003082 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003084 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003085 {
3086 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003087 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3090 item->di_tv.v_lock = 0;
3091 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003092 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003093 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003094 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003095 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 }
3098
3099 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003100 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003101 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003102 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003104 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 tv = STACK_TV_BOT(-1);
3106 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003107 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 tv->vval.v_dict = dict;
3109 ++dict->dv_refcount;
3110 }
3111 break;
3112
3113 // call a :def function
3114 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003115 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003116 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3117 NULL,
3118 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003119 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003120 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 break;
3122
3123 // call a builtin function
3124 case ISN_BCALL:
3125 SOURCING_LNUM = iptr->isn_lnum;
3126 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3127 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003128 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003129 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 break;
3131
3132 // call a funcref or partial
3133 case ISN_PCALL:
3134 {
3135 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3136 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003137 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138
3139 SOURCING_LNUM = iptr->isn_lnum;
3140 if (pfunc->cpf_top)
3141 {
3142 // funcref is above the arguments
3143 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3144 }
3145 else
3146 {
3147 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003148 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003149 partial_tv = *STACK_TV_BOT(0);
3150 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003152 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003153 if (tv == &partial_tv)
3154 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003156 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 }
3158 break;
3159
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003160 case ISN_PCALL_END:
3161 // PCALL finished, arguments have been consumed and replaced by
3162 // the return value. Now clear the funcref from the stack,
3163 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003164 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003165 clear_tv(STACK_TV_BOT(-1));
3166 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3167 break;
3168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 // call a user defined function or funcref/partial
3170 case ISN_UCALL:
3171 {
3172 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3173
3174 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003175 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003176 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003177 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 }
3179 break;
3180
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003181 // return from a :def function call without a value
3182 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003183 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003184 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003185 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003186 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003187 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003188 tv->vval.v_number = 0;
3189 tv->v_lock = 0;
3190 // FALLTHROUGH
3191
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003192 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 case ISN_RETURN:
3194 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003196 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003197
3198 if (trystack->ga_len > 0)
3199 trycmd = ((trycmd_T *)trystack->ga_data)
3200 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003201 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003202 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003204 // jump to ":finally" or ":endtry"
3205 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003206 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003207 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003208 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 trycmd->tcd_return = TRUE;
3210 }
3211 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003212 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 }
3214 break;
3215
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003216 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 case ISN_FUNCREF:
3218 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003219 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003220 ufunc_T *ufunc;
3221 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003224 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003225 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003226 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003227 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003228 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003229 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003230 if (funcref->fr_func_name == NULL)
3231 {
3232 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3233 + funcref->fr_dfunc_idx;
3234
3235 ufunc = pt_dfunc->df_ufunc;
3236 }
3237 else
3238 {
3239 ufunc = find_func(funcref->fr_func_name, FALSE, NULL);
3240 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003241 if (ufunc == NULL)
3242 {
3243 SOURCING_LNUM = iptr->isn_lnum;
3244 emsg(_(e_function_reference_invalid));
3245 goto theend;
3246 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003247 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003248 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003250 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 tv->vval.v_partial = pt;
3252 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003253 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 }
3255 break;
3256
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003257 // Create a global function from a lambda.
3258 case ISN_NEWFUNC:
3259 {
3260 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3261
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003262 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003263 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003264 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003265 }
3266 break;
3267
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003268 // List functions
3269 case ISN_DEF:
3270 if (iptr->isn_arg.string == NULL)
3271 list_functions(NULL);
3272 else
3273 {
3274 exarg_T ea;
3275
3276 CLEAR_FIELD(ea);
3277 ea.cmd = ea.arg = iptr->isn_arg.string;
3278 define_function(&ea, NULL);
3279 }
3280 break;
3281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 // jump if a condition is met
3283 case ISN_JUMP:
3284 {
3285 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003286 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 int jump = TRUE;
3288
3289 if (when != JUMP_ALWAYS)
3290 {
3291 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003292 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003293 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003294 || when == JUMP_IF_COND_TRUE)
3295 {
3296 SOURCING_LNUM = iptr->isn_lnum;
3297 jump = tv_get_bool_chk(tv, &error);
3298 if (error)
3299 goto on_error;
3300 }
3301 else
3302 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003304 || when == JUMP_AND_KEEP_IF_FALSE
3305 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003307 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 {
3309 // drop the value from the stack
3310 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003311 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 }
3313 }
3314 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003315 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 }
3317 break;
3318
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003319 // Jump if an argument with a default value was already set and not
3320 // v:none.
3321 case ISN_JUMP_IF_ARG_SET:
3322 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3323 if (tv->v_type != VAR_UNKNOWN
3324 && !(tv->v_type == VAR_SPECIAL
3325 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003326 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003327 break;
3328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 // top of a for loop
3330 case ISN_FOR:
3331 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003332 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 typval_T *idxtv =
3334 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3335
Bram Moolenaar35578162021-08-02 19:10:38 +02003336 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003337 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003338 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003339 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003340 list_T *list = ltv->vval.v_list;
3341
3342 // push the next item from the list
3343 ++idxtv->vval.v_number;
3344 if (list == NULL
3345 || idxtv->vval.v_number >= list->lv_len)
3346 {
3347 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003348 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3349 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003350 }
3351 else if (list->lv_first == &range_list_item)
3352 {
3353 // non-materialized range() list
3354 tv = STACK_TV_BOT(0);
3355 tv->v_type = VAR_NUMBER;
3356 tv->v_lock = 0;
3357 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003359 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003360 }
3361 else
3362 {
3363 listitem_T *li = list_find(list,
3364 idxtv->vval.v_number);
3365
3366 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003367 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003368 }
3369 }
3370 else if (ltv->v_type == VAR_STRING)
3371 {
3372 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003373
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003374 // The index is for the last byte of the previous
3375 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003376 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003377 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003378 {
3379 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003380 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3381 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003382 }
3383 else
3384 {
3385 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3386
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003387 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003388 tv = STACK_TV_BOT(0);
3389 tv->v_type = VAR_STRING;
3390 tv->vval.v_string = vim_strnsave(
3391 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003392 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003393 idxtv->vval.v_number += clen - 1;
3394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003396 else if (ltv->v_type == VAR_BLOB)
3397 {
3398 blob_T *blob = ltv->vval.v_blob;
3399
3400 // When we get here the first time make a copy of the
3401 // blob, so that the iteration still works when it is
3402 // changed.
3403 if (idxtv->vval.v_number == -1 && blob != NULL)
3404 {
3405 blob_copy(blob, ltv);
3406 blob_unref(blob);
3407 blob = ltv->vval.v_blob;
3408 }
3409
3410 // The index is for the previous byte.
3411 ++idxtv->vval.v_number;
3412 if (blob == NULL
3413 || idxtv->vval.v_number >= blob_len(blob))
3414 {
3415 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3417 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003418 }
3419 else
3420 {
3421 // Push the next byte from the blob.
3422 tv = STACK_TV_BOT(0);
3423 tv->v_type = VAR_NUMBER;
3424 tv->vval.v_number = blob_get(blob,
3425 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003426 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003427 }
3428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 else
3430 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003431 semsg(_(e_for_loop_on_str_not_supported),
3432 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003433 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 }
3435 }
3436 break;
3437
3438 // start of ":try" block
3439 case ISN_TRY:
3440 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003441 trycmd_T *trycmd = NULL;
3442
Bram Moolenaar35578162021-08-02 19:10:38 +02003443 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003444 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003445 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3446 + ectx->ec_trystack.ga_len;
3447 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003449 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003450 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3451 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003452 trycmd->tcd_catch_idx =
3453 iptr->isn_arg.try.try_ref->try_catch;
3454 trycmd->tcd_finally_idx =
3455 iptr->isn_arg.try.try_ref->try_finally;
3456 trycmd->tcd_endtry_idx =
3457 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 }
3459 break;
3460
3461 case ISN_PUSHEXC:
3462 if (current_exception == NULL)
3463 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003464 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003466 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003468 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003469 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003471 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003473 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 tv->vval.v_string = vim_strsave(
3475 (char_u *)current_exception->value);
3476 break;
3477
3478 case ISN_CATCH:
3479 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003480 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481
Bram Moolenaar4c137212021-04-19 16:48:48 +02003482 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 if (trystack->ga_len > 0)
3484 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003485 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 + trystack->ga_len - 1;
3487 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003488 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 }
3490 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003491 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 catch_exception(current_exception);
3493 }
3494 break;
3495
Bram Moolenaarc150c092021-02-13 15:02:46 +01003496 case ISN_TRYCONT:
3497 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003498 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003499 trycont_T *trycont = &iptr->isn_arg.trycont;
3500 int i;
3501 trycmd_T *trycmd;
3502 int iidx = trycont->tct_where;
3503
3504 if (trystack->ga_len < trycont->tct_levels)
3505 {
3506 siemsg("TRYCONT: expected %d levels, found %d",
3507 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003508 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003509 }
3510 // Make :endtry jump to any outer try block and the last
3511 // :endtry inside the loop to the loop start.
3512 for (i = trycont->tct_levels; i > 0; --i)
3513 {
3514 trycmd = ((trycmd_T *)trystack->ga_data)
3515 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003516 // Add one to tcd_cont to be able to jump to
3517 // instruction with index zero.
3518 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003519 iidx = trycmd->tcd_finally_idx == 0
3520 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003521 }
3522 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003523 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003524 }
3525 break;
3526
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003527 case ISN_FINALLY:
3528 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003529 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003530 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3531 + trystack->ga_len - 1;
3532
3533 // Reset the index to avoid a return statement jumps here
3534 // again.
3535 trycmd->tcd_finally_idx = 0;
3536 break;
3537 }
3538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 // end of ":try" block
3540 case ISN_ENDTRY:
3541 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003542 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543
3544 if (trystack->ga_len > 0)
3545 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003546 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 --trystack->ga_len;
3549 --trylevel;
3550 trycmd = ((trycmd_T *)trystack->ga_data)
3551 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003552 if (trycmd->tcd_did_throw)
3553 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003554 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 {
3556 // discard the exception
3557 if (caught_stack == current_exception)
3558 caught_stack = caught_stack->caught;
3559 discard_current_exception();
3560 }
3561
3562 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003563 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003564
Bram Moolenaar4c137212021-04-19 16:48:48 +02003565 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003566 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003567 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003568 clear_tv(STACK_TV_BOT(0));
3569 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003570 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003571 // handling :continue: jump to outer try block or
3572 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003573 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 }
3575 }
3576 break;
3577
3578 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003579 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003580 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003581
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003582 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3583 {
3584 // throwing an exception while using "silent!" causes
3585 // the function to abort but not display an error.
3586 tv = STACK_TV_BOT(-1);
3587 clear_tv(tv);
3588 tv->v_type = VAR_NUMBER;
3589 tv->vval.v_number = 0;
3590 goto done;
3591 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003592 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003593 tv = STACK_TV_BOT(0);
3594 if (tv->vval.v_string == NULL
3595 || *skipwhite(tv->vval.v_string) == NUL)
3596 {
3597 vim_free(tv->vval.v_string);
3598 SOURCING_LNUM = iptr->isn_lnum;
3599 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003600 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003601 }
3602
3603 // Inside a "catch" we need to first discard the caught
3604 // exception.
3605 if (trystack->ga_len > 0)
3606 {
3607 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3608 + trystack->ga_len - 1;
3609 if (trycmd->tcd_caught && current_exception != NULL)
3610 {
3611 // discard the exception
3612 if (caught_stack == current_exception)
3613 caught_stack = caught_stack->caught;
3614 discard_current_exception();
3615 trycmd->tcd_caught = FALSE;
3616 }
3617 }
3618
3619 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3620 == FAIL)
3621 {
3622 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003623 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003624 }
3625 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 break;
3628
3629 // compare with special values
3630 case ISN_COMPAREBOOL:
3631 case ISN_COMPARESPECIAL:
3632 {
3633 typval_T *tv1 = STACK_TV_BOT(-2);
3634 typval_T *tv2 = STACK_TV_BOT(-1);
3635 varnumber_T arg1 = tv1->vval.v_number;
3636 varnumber_T arg2 = tv2->vval.v_number;
3637 int res;
3638
3639 switch (iptr->isn_arg.op.op_type)
3640 {
3641 case EXPR_EQUAL: res = arg1 == arg2; break;
3642 case EXPR_NEQUAL: res = arg1 != arg2; break;
3643 default: res = 0; break;
3644 }
3645
Bram Moolenaar4c137212021-04-19 16:48:48 +02003646 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 tv1->v_type = VAR_BOOL;
3648 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3649 }
3650 break;
3651
3652 // Operation with two number arguments
3653 case ISN_OPNR:
3654 case ISN_COMPARENR:
3655 {
3656 typval_T *tv1 = STACK_TV_BOT(-2);
3657 typval_T *tv2 = STACK_TV_BOT(-1);
3658 varnumber_T arg1 = tv1->vval.v_number;
3659 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003660 varnumber_T res = 0;
3661 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662
3663 switch (iptr->isn_arg.op.op_type)
3664 {
3665 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003666 case EXPR_DIV: if (arg2 == 0)
3667 div_zero = TRUE;
3668 else
3669 res = arg1 / arg2;
3670 break;
3671 case EXPR_REM: if (arg2 == 0)
3672 div_zero = TRUE;
3673 else
3674 res = arg1 % arg2;
3675 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 case EXPR_SUB: res = arg1 - arg2; break;
3677 case EXPR_ADD: res = arg1 + arg2; break;
3678
3679 case EXPR_EQUAL: res = arg1 == arg2; break;
3680 case EXPR_NEQUAL: res = arg1 != arg2; break;
3681 case EXPR_GREATER: res = arg1 > arg2; break;
3682 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3683 case EXPR_SMALLER: res = arg1 < arg2; break;
3684 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003685 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 }
3687
Bram Moolenaar4c137212021-04-19 16:48:48 +02003688 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 if (iptr->isn_type == ISN_COMPARENR)
3690 {
3691 tv1->v_type = VAR_BOOL;
3692 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3693 }
3694 else
3695 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003696 if (div_zero)
3697 {
3698 SOURCING_LNUM = iptr->isn_lnum;
3699 emsg(_(e_divide_by_zero));
3700 goto on_error;
3701 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 }
3703 break;
3704
3705 // Computation with two float arguments
3706 case ISN_OPFLOAT:
3707 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003708#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 {
3710 typval_T *tv1 = STACK_TV_BOT(-2);
3711 typval_T *tv2 = STACK_TV_BOT(-1);
3712 float_T arg1 = tv1->vval.v_float;
3713 float_T arg2 = tv2->vval.v_float;
3714 float_T res = 0;
3715 int cmp = FALSE;
3716
3717 switch (iptr->isn_arg.op.op_type)
3718 {
3719 case EXPR_MULT: res = arg1 * arg2; break;
3720 case EXPR_DIV: res = arg1 / arg2; break;
3721 case EXPR_SUB: res = arg1 - arg2; break;
3722 case EXPR_ADD: res = arg1 + arg2; break;
3723
3724 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3725 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3726 case EXPR_GREATER: cmp = arg1 > arg2; break;
3727 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3728 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3729 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3730 default: cmp = 0; break;
3731 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003732 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 if (iptr->isn_type == ISN_COMPAREFLOAT)
3734 {
3735 tv1->v_type = VAR_BOOL;
3736 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3737 }
3738 else
3739 tv1->vval.v_float = res;
3740 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003741#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 break;
3743
3744 case ISN_COMPARELIST:
3745 {
3746 typval_T *tv1 = STACK_TV_BOT(-2);
3747 typval_T *tv2 = STACK_TV_BOT(-1);
3748 list_T *arg1 = tv1->vval.v_list;
3749 list_T *arg2 = tv2->vval.v_list;
3750 int cmp = FALSE;
3751 int ic = iptr->isn_arg.op.op_ic;
3752
3753 switch (iptr->isn_arg.op.op_type)
3754 {
3755 case EXPR_EQUAL: cmp =
3756 list_equal(arg1, arg2, ic, FALSE); break;
3757 case EXPR_NEQUAL: cmp =
3758 !list_equal(arg1, arg2, ic, FALSE); break;
3759 case EXPR_IS: cmp = arg1 == arg2; break;
3760 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3761 default: cmp = 0; break;
3762 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003763 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 clear_tv(tv1);
3765 clear_tv(tv2);
3766 tv1->v_type = VAR_BOOL;
3767 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3768 }
3769 break;
3770
3771 case ISN_COMPAREBLOB:
3772 {
3773 typval_T *tv1 = STACK_TV_BOT(-2);
3774 typval_T *tv2 = STACK_TV_BOT(-1);
3775 blob_T *arg1 = tv1->vval.v_blob;
3776 blob_T *arg2 = tv2->vval.v_blob;
3777 int cmp = FALSE;
3778
3779 switch (iptr->isn_arg.op.op_type)
3780 {
3781 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3782 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3783 case EXPR_IS: cmp = arg1 == arg2; break;
3784 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3785 default: cmp = 0; break;
3786 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003787 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 clear_tv(tv1);
3789 clear_tv(tv2);
3790 tv1->v_type = VAR_BOOL;
3791 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3792 }
3793 break;
3794
3795 // TODO: handle separately
3796 case ISN_COMPARESTRING:
3797 case ISN_COMPAREDICT:
3798 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 case ISN_COMPAREANY:
3800 {
3801 typval_T *tv1 = STACK_TV_BOT(-2);
3802 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003803 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 int ic = iptr->isn_arg.op.op_ic;
3805
Bram Moolenaareb26f432020-09-14 16:50:05 +02003806 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003807 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003809 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 }
3811 break;
3812
3813 case ISN_ADDLIST:
3814 case ISN_ADDBLOB:
3815 {
3816 typval_T *tv1 = STACK_TV_BOT(-2);
3817 typval_T *tv2 = STACK_TV_BOT(-1);
3818
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003819 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003821 {
3822 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3823 && tv1->vval.v_list != NULL)
3824 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3825 NULL);
3826 else
3827 eval_addlist(tv1, tv2);
3828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 else
3830 eval_addblob(tv1, tv2);
3831 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003832 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 }
3834 break;
3835
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003836 case ISN_LISTAPPEND:
3837 {
3838 typval_T *tv1 = STACK_TV_BOT(-2);
3839 typval_T *tv2 = STACK_TV_BOT(-1);
3840 list_T *l = tv1->vval.v_list;
3841
3842 // add an item to a list
3843 if (l == NULL)
3844 {
3845 SOURCING_LNUM = iptr->isn_lnum;
3846 emsg(_(e_cannot_add_to_null_list));
3847 goto on_error;
3848 }
3849 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003850 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003851 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003852 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003853 }
3854 break;
3855
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003856 case ISN_BLOBAPPEND:
3857 {
3858 typval_T *tv1 = STACK_TV_BOT(-2);
3859 typval_T *tv2 = STACK_TV_BOT(-1);
3860 blob_T *b = tv1->vval.v_blob;
3861 int error = FALSE;
3862 varnumber_T n;
3863
3864 // add a number to a blob
3865 if (b == NULL)
3866 {
3867 SOURCING_LNUM = iptr->isn_lnum;
3868 emsg(_(e_cannot_add_to_null_blob));
3869 goto on_error;
3870 }
3871 n = tv_get_number_chk(tv2, &error);
3872 if (error)
3873 goto on_error;
3874 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003875 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003876 }
3877 break;
3878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 // Computation with two arguments of unknown type
3880 case ISN_OPANY:
3881 {
3882 typval_T *tv1 = STACK_TV_BOT(-2);
3883 typval_T *tv2 = STACK_TV_BOT(-1);
3884 varnumber_T n1, n2;
3885#ifdef FEAT_FLOAT
3886 float_T f1 = 0, f2 = 0;
3887#endif
3888 int error = FALSE;
3889
3890 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3891 {
3892 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3893 {
3894 eval_addlist(tv1, tv2);
3895 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003896 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 break;
3898 }
3899 else if (tv1->v_type == VAR_BLOB
3900 && tv2->v_type == VAR_BLOB)
3901 {
3902 eval_addblob(tv1, tv2);
3903 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003904 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 break;
3906 }
3907 }
3908#ifdef FEAT_FLOAT
3909 if (tv1->v_type == VAR_FLOAT)
3910 {
3911 f1 = tv1->vval.v_float;
3912 n1 = 0;
3913 }
3914 else
3915#endif
3916 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003917 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 n1 = tv_get_number_chk(tv1, &error);
3919 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003920 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921#ifdef FEAT_FLOAT
3922 if (tv2->v_type == VAR_FLOAT)
3923 f1 = n1;
3924#endif
3925 }
3926#ifdef FEAT_FLOAT
3927 if (tv2->v_type == VAR_FLOAT)
3928 {
3929 f2 = tv2->vval.v_float;
3930 n2 = 0;
3931 }
3932 else
3933#endif
3934 {
3935 n2 = tv_get_number_chk(tv2, &error);
3936 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003937 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938#ifdef FEAT_FLOAT
3939 if (tv1->v_type == VAR_FLOAT)
3940 f2 = n2;
3941#endif
3942 }
3943#ifdef FEAT_FLOAT
3944 // if there is a float on either side the result is a float
3945 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3946 {
3947 switch (iptr->isn_arg.op.op_type)
3948 {
3949 case EXPR_MULT: f1 = f1 * f2; break;
3950 case EXPR_DIV: f1 = f1 / f2; break;
3951 case EXPR_SUB: f1 = f1 - f2; break;
3952 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003953 default: SOURCING_LNUM = iptr->isn_lnum;
3954 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003955 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 }
3957 clear_tv(tv1);
3958 clear_tv(tv2);
3959 tv1->v_type = VAR_FLOAT;
3960 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003961 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 }
3963 else
3964#endif
3965 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003966 int failed = FALSE;
3967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 switch (iptr->isn_arg.op.op_type)
3969 {
3970 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003971 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3972 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003973 goto on_error;
3974 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 case EXPR_SUB: n1 = n1 - n2; break;
3976 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003977 default: n1 = num_modulus(n1, n2, &failed);
3978 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003979 goto on_error;
3980 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 }
3982 clear_tv(tv1);
3983 clear_tv(tv2);
3984 tv1->v_type = VAR_NUMBER;
3985 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003986 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 }
3988 }
3989 break;
3990
3991 case ISN_CONCAT:
3992 {
3993 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3994 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3995 char_u *res;
3996
3997 res = concat_str(str1, str2);
3998 clear_tv(STACK_TV_BOT(-2));
3999 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004000 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 STACK_TV_BOT(-1)->vval.v_string = res;
4002 }
4003 break;
4004
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004005 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004006 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004007 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004008 int is_slice = iptr->isn_type == ISN_STRSLICE;
4009 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004010 char_u *res;
4011
4012 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004013 // string slice: string is at stack-3, first index at
4014 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004015 if (is_slice)
4016 {
4017 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004018 n1 = tv->vval.v_number;
4019 }
4020
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004021 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004022 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004023
Bram Moolenaar4c137212021-04-19 16:48:48 +02004024 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004025 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004026 if (is_slice)
4027 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004028 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004029 else
4030 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004031 // single character (including composing characters).
4032 // If the index is too big or negative the result is
4033 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004034 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004035 vim_free(tv->vval.v_string);
4036 tv->vval.v_string = res;
4037 }
4038 break;
4039
4040 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004041 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004042 case ISN_BLOBINDEX:
4043 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004045 int is_slice = iptr->isn_type == ISN_LISTSLICE
4046 || iptr->isn_type == ISN_BLOBSLICE;
4047 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4048 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004049 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004050 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051
4052 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004053 // list slice: list is at stack-3, indexes at stack-2 and
4054 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004055 // Same for blob.
4056 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057
4058 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004059 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004061
4062 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 {
Bram Moolenaared591872020-08-15 22:14:53 +02004064 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004065 n1 = tv->vval.v_number;
4066 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 }
Bram Moolenaared591872020-08-15 22:14:53 +02004068
Bram Moolenaar4c137212021-04-19 16:48:48 +02004069 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004070 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004071 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004072 if (is_blob)
4073 {
4074 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4075 n1, n2, FALSE, tv) == FAIL)
4076 goto on_error;
4077 }
4078 else
4079 {
4080 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4081 n1, n2, FALSE, tv, TRUE) == FAIL)
4082 goto on_error;
4083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 }
4085 break;
4086
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004087 case ISN_ANYINDEX:
4088 case ISN_ANYSLICE:
4089 {
4090 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4091 typval_T *var1, *var2;
4092 int res;
4093
4094 // index: composite is at stack-2, index at stack-1
4095 // slice: composite is at stack-3, indexes at stack-2 and
4096 // stack-1
4097 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004098 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004099 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4100 goto on_error;
4101 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4102 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004103 res = eval_index_inner(tv, is_slice, var1, var2,
4104 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004105 clear_tv(var1);
4106 if (is_slice)
4107 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004108 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004109 if (res == FAIL)
4110 goto on_error;
4111 }
4112 break;
4113
Bram Moolenaar9af78762020-06-16 11:34:42 +02004114 case ISN_SLICE:
4115 {
4116 list_T *list;
4117 int count = iptr->isn_arg.number;
4118
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004119 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004120 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004121 list = tv->vval.v_list;
4122
4123 // no error for short list, expect it to be checked earlier
4124 if (list != NULL && list->lv_len >= count)
4125 {
4126 list_T *newlist = list_slice(list,
4127 count, list->lv_len - 1);
4128
4129 if (newlist != NULL)
4130 {
4131 list_unref(list);
4132 tv->vval.v_list = newlist;
4133 ++newlist->lv_refcount;
4134 }
4135 }
4136 }
4137 break;
4138
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004139 case ISN_GETITEM:
4140 {
4141 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004142 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004143
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004144 // Get list item: list is at stack-1, push item.
4145 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004146 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4147 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004148
Bram Moolenaar35578162021-08-02 19:10:38 +02004149 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004150 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004151 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004152 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004153
4154 // Useful when used in unpack assignment. Reset at
4155 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004156 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004157 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004158 }
4159 break;
4160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 case ISN_MEMBER:
4162 {
4163 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004164 char_u *key;
4165 dictitem_T *di;
4166
4167 // dict member: dict is at stack-2, key at stack-1
4168 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004169 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004170 dict = tv->vval.v_dict;
4171
4172 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004173 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004174 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004175 if (key == NULL)
4176 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004177
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004178 if ((di = dict_find(dict, key, -1)) == NULL)
4179 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004180 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004181 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004182
4183 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004184 // stack contents makes sense and the dict stack is
4185 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004186 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004187 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004188 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004189 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004190 tv->v_type = VAR_NUMBER;
4191 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004192 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004193 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004194 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004195 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004196 // Put the dict used on the dict stack, it might be used by
4197 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004198 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004199 if (dict_stack_save(tv) == FAIL)
4200 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004201 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004202 }
4203 break;
4204
4205 // dict member with string key
4206 case ISN_STRINGMEMBER:
4207 {
4208 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 dictitem_T *di;
4210
4211 tv = STACK_TV_BOT(-1);
4212 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4213 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004214 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004216 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 }
4218 dict = tv->vval.v_dict;
4219
4220 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4221 == NULL)
4222 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004225 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004227 // Put the dict used on the dict stack, it might be used by
4228 // a dict function later.
4229 if (dict_stack_save(tv) == FAIL)
4230 goto on_fatal_error;
4231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004233 }
4234 break;
4235
4236 case ISN_CLEARDICT:
4237 dict_stack_drop();
4238 break;
4239
4240 case ISN_USEDICT:
4241 {
4242 typval_T *dict_tv = dict_stack_get_tv();
4243
4244 // Turn "dict.Func" into a partial for "Func" bound to
4245 // "dict". Don't do this when "Func" is already a partial
4246 // that was bound explicitly (pt_auto is FALSE).
4247 tv = STACK_TV_BOT(-1);
4248 if (dict_tv != NULL
4249 && dict_tv->v_type == VAR_DICT
4250 && dict_tv->vval.v_dict != NULL
4251 && (tv->v_type == VAR_FUNC
4252 || (tv->v_type == VAR_PARTIAL
4253 && (tv->vval.v_partial->pt_auto
4254 || tv->vval.v_partial->pt_dict == NULL))))
4255 dict_tv->vval.v_dict =
4256 make_partial(dict_tv->vval.v_dict, tv);
4257 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 }
4259 break;
4260
4261 case ISN_NEGATENR:
4262 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004263 if (tv->v_type != VAR_NUMBER
4264#ifdef FEAT_FLOAT
4265 && tv->v_type != VAR_FLOAT
4266#endif
4267 )
4268 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004269 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004270 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004271 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004272 }
4273#ifdef FEAT_FLOAT
4274 if (tv->v_type == VAR_FLOAT)
4275 tv->vval.v_float = -tv->vval.v_float;
4276 else
4277#endif
4278 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 break;
4280
4281 case ISN_CHECKNR:
4282 {
4283 int error = FALSE;
4284
4285 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004286 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004288 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 (void)tv_get_number_chk(tv, &error);
4290 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004291 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 }
4293 break;
4294
4295 case ISN_CHECKTYPE:
4296 {
4297 checktype_T *ct = &iptr->isn_arg.type;
4298
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004299 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004300 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004301 if (!ectx->ec_where.wt_variable)
4302 ectx->ec_where.wt_index = ct->ct_arg_idx;
4303 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4304 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004305 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004306 if (!ectx->ec_where.wt_variable)
4307 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004308
4309 // number 0 is FALSE, number 1 is TRUE
4310 if (tv->v_type == VAR_NUMBER
4311 && ct->ct_type->tt_type == VAR_BOOL
4312 && (tv->vval.v_number == 0
4313 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004315 tv->v_type = VAR_BOOL;
4316 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004317 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 }
4319 }
4320 break;
4321
Bram Moolenaar9af78762020-06-16 11:34:42 +02004322 case ISN_CHECKLEN:
4323 {
4324 int min_len = iptr->isn_arg.checklen.cl_min_len;
4325 list_T *list = NULL;
4326
4327 tv = STACK_TV_BOT(-1);
4328 if (tv->v_type == VAR_LIST)
4329 list = tv->vval.v_list;
4330 if (list == NULL || list->lv_len < min_len
4331 || (list->lv_len > min_len
4332 && !iptr->isn_arg.checklen.cl_more_OK))
4333 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004334 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004335 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004336 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004337 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004338 }
4339 }
4340 break;
4341
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004342 case ISN_SETTYPE:
4343 {
4344 checktype_T *ct = &iptr->isn_arg.type;
4345
4346 tv = STACK_TV_BOT(-1);
4347 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4348 {
4349 free_type(tv->vval.v_dict->dv_type);
4350 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4351 }
4352 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4353 {
4354 free_type(tv->vval.v_list->lv_type);
4355 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4356 }
4357 }
4358 break;
4359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004361 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 {
4363 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004364 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004366 if (iptr->isn_type == ISN_2BOOL)
4367 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004368 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004369 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004370 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004371 n = !n;
4372 }
4373 else
4374 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004375 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004376 SOURCING_LNUM = iptr->isn_lnum;
4377 n = tv_get_bool_chk(tv, &error);
4378 if (error)
4379 goto on_error;
4380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 clear_tv(tv);
4382 tv->v_type = VAR_BOOL;
4383 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4384 }
4385 break;
4386
4387 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004388 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004389 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004390 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4391 iptr->isn_type == ISN_2STRING_ANY,
4392 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004393 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 break;
4395
Bram Moolenaar08597872020-12-10 19:43:40 +01004396 case ISN_RANGE:
4397 {
4398 exarg_T ea;
4399 char *errormsg;
4400
Bram Moolenaarece0b872021-01-08 20:40:45 +01004401 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004402 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004403 ea.addr_type = ADDR_LINES;
4404 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004405 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004406 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004407 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004408
Bram Moolenaar35578162021-08-02 19:10:38 +02004409 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004410 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004411 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004412 tv = STACK_TV_BOT(-1);
4413 tv->v_type = VAR_NUMBER;
4414 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004415 if (ea.addr_count == 0)
4416 tv->vval.v_number = curwin->w_cursor.lnum;
4417 else
4418 tv->vval.v_number = ea.line2;
4419 }
4420 break;
4421
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004422 case ISN_PUT:
4423 {
4424 int regname = iptr->isn_arg.put.put_regname;
4425 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4426 char_u *expr = NULL;
4427 int dir = FORWARD;
4428
Bram Moolenaar08597872020-12-10 19:43:40 +01004429 if (lnum < -2)
4430 {
4431 // line number was put on the stack by ISN_RANGE
4432 tv = STACK_TV_BOT(-1);
4433 curwin->w_cursor.lnum = tv->vval.v_number;
4434 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4435 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004436 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004437 }
4438 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004439 // :put! above cursor
4440 dir = BACKWARD;
4441 else if (lnum >= 0)
4442 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004443
4444 if (regname == '=')
4445 {
4446 tv = STACK_TV_BOT(-1);
4447 if (tv->v_type == VAR_STRING)
4448 expr = tv->vval.v_string;
4449 else
4450 {
4451 expr = typval2string(tv, TRUE); // allocates value
4452 clear_tv(tv);
4453 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004454 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004455 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004456 check_cursor();
4457 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4458 vim_free(expr);
4459 }
4460 break;
4461
Bram Moolenaar02194d22020-10-24 23:08:38 +02004462 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004463 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4464 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4465 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4466 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004467 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4468 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004469 break;
4470
Bram Moolenaar02194d22020-10-24 23:08:38 +02004471 case ISN_CMDMOD_REV:
4472 // filter regprog is owned by the instruction, don't free it
4473 cmdmod.cmod_filter_regmatch.regprog = NULL;
4474 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004475 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4476 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004477 break;
4478
Bram Moolenaar792f7862020-11-23 08:31:18 +01004479 case ISN_UNPACK:
4480 {
4481 int count = iptr->isn_arg.unpack.unp_count;
4482 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4483 list_T *l;
4484 listitem_T *li;
4485 int i;
4486
4487 // Check there is a valid list to unpack.
4488 tv = STACK_TV_BOT(-1);
4489 if (tv->v_type != VAR_LIST)
4490 {
4491 SOURCING_LNUM = iptr->isn_lnum;
4492 emsg(_(e_for_argument_must_be_sequence_of_lists));
4493 goto on_error;
4494 }
4495 l = tv->vval.v_list;
4496 if (l == NULL
4497 || l->lv_len < (semicolon ? count - 1 : count))
4498 {
4499 SOURCING_LNUM = iptr->isn_lnum;
4500 emsg(_(e_list_value_does_not_have_enough_items));
4501 goto on_error;
4502 }
4503 else if (!semicolon && l->lv_len > count)
4504 {
4505 SOURCING_LNUM = iptr->isn_lnum;
4506 emsg(_(e_list_value_has_more_items_than_targets));
4507 goto on_error;
4508 }
4509
4510 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004511 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004512 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004513 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004514
4515 // Variable after semicolon gets a list with the remaining
4516 // items.
4517 if (semicolon)
4518 {
4519 list_T *rem_list =
4520 list_alloc_with_items(l->lv_len - count + 1);
4521
4522 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004523 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004524 tv = STACK_TV_BOT(-count);
4525 tv->vval.v_list = rem_list;
4526 ++rem_list->lv_refcount;
4527 tv->v_lock = 0;
4528 li = l->lv_first;
4529 for (i = 0; i < count - 1; ++i)
4530 li = li->li_next;
4531 for (i = 0; li != NULL; ++i)
4532 {
4533 list_set_item(rem_list, i, &li->li_tv);
4534 li = li->li_next;
4535 }
4536 --count;
4537 }
4538
4539 // Produce the values in reverse order, first item last.
4540 li = l->lv_first;
4541 for (i = 0; i < count; ++i)
4542 {
4543 tv = STACK_TV_BOT(-i - 1);
4544 copy_tv(&li->li_tv, tv);
4545 li = li->li_next;
4546 }
4547
4548 list_unref(l);
4549 }
4550 break;
4551
Bram Moolenaarb2049902021-01-24 12:53:53 +01004552 case ISN_PROF_START:
4553 case ISN_PROF_END:
4554 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004555#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004556 funccall_T cookie;
4557 ufunc_T *cur_ufunc =
4558 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004559 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004560
4561 cookie.func = cur_ufunc;
4562 if (iptr->isn_type == ISN_PROF_START)
4563 {
4564 func_line_start(&cookie, iptr->isn_lnum);
4565 // if we get here the instruction is executed
4566 func_line_exec(&cookie);
4567 }
4568 else
4569 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004570#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004571 }
4572 break;
4573
Bram Moolenaare99d4222021-06-13 14:01:26 +02004574 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004575 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004576 break;
4577
Bram Moolenaar389df252020-07-09 21:20:47 +02004578 case ISN_SHUFFLE:
4579 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004580 typval_T tmp_tv;
4581 int item = iptr->isn_arg.shuffle.shfl_item;
4582 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004583
4584 tmp_tv = *STACK_TV_BOT(-item);
4585 for ( ; up > 0 && item > 1; --up)
4586 {
4587 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4588 --item;
4589 }
4590 *STACK_TV_BOT(-item) = tmp_tv;
4591 }
4592 break;
4593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004595 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004597 ectx->ec_where.wt_index = 0;
4598 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 break;
4600 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004601 continue;
4602
Bram Moolenaard032f342020-07-18 18:13:02 +02004603func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004604 // Restore previous function. If the frame pointer is where we started
4605 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004606 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004607 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004608
Bram Moolenaar4c137212021-04-19 16:48:48 +02004609 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004610 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004611 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004612 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004613
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004614on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004615 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004616 // If "emsg_silent" is set then ignore the error, unless it was set
4617 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004618 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004619 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004620 {
4621 // If a sequence of instructions causes an error while ":silent!"
4622 // was used, restore the stack length and jump ahead to restoring
4623 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004624 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004625 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004626 while (ectx->ec_stack.ga_len
4627 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004628 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004629 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004630 clear_tv(STACK_TV_BOT(0));
4631 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004632 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4633 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004634 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004635 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004636 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004637on_fatal_error:
4638 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004639 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004640 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004641 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 }
4643
4644done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004645 ret = OK;
4646theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004647 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004648 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4649 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004650}
4651
4652/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004653 * Execute the instructions from a VAR_INSTR typeval and put the result in
4654 * "rettv".
4655 * Return OK or FAIL.
4656 */
4657 int
4658exe_typval_instr(typval_T *tv, typval_T *rettv)
4659{
4660 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4661 isn_T *save_instr = ectx->ec_instr;
4662 int save_iidx = ectx->ec_iidx;
4663 int res;
4664
4665 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4666 res = exec_instructions(ectx);
4667 if (res == OK)
4668 {
4669 *rettv = *STACK_TV_BOT(-1);
4670 --ectx->ec_stack.ga_len;
4671 }
4672
4673 ectx->ec_instr = save_instr;
4674 ectx->ec_iidx = save_iidx;
4675
4676 return res;
4677}
4678
4679/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004680 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4681 * "substitute_instr".
4682 */
4683 char_u *
4684exe_substitute_instr(void)
4685{
4686 ectx_T *ectx = substitute_instr->subs_ectx;
4687 isn_T *save_instr = ectx->ec_instr;
4688 int save_iidx = ectx->ec_iidx;
4689 char_u *res;
4690
4691 ectx->ec_instr = substitute_instr->subs_instr;
4692 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004693 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004694 typval_T *tv = STACK_TV_BOT(-1);
4695
Bram Moolenaar27523602021-06-05 21:36:19 +02004696 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004697 --ectx->ec_stack.ga_len;
4698 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004699 }
4700 else
4701 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004702 substitute_instr->subs_status = FAIL;
4703 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705
Bram Moolenaar4c137212021-04-19 16:48:48 +02004706 ectx->ec_instr = save_instr;
4707 ectx->ec_iidx = save_iidx;
4708
4709 return res;
4710}
4711
4712/*
4713 * Call a "def" function from old Vim script.
4714 * Return OK or FAIL.
4715 */
4716 int
4717call_def_function(
4718 ufunc_T *ufunc,
4719 int argc_arg, // nr of arguments
4720 typval_T *argv, // arguments
4721 partial_T *partial, // optional partial for context
4722 typval_T *rettv) // return value
4723{
4724 ectx_T ectx; // execution context
4725 int argc = argc_arg;
4726 typval_T *tv;
4727 int idx;
4728 int ret = FAIL;
4729 int defcount = ufunc->uf_args.ga_len - argc;
4730 sctx_T save_current_sctx = current_sctx;
4731 int did_emsg_before = did_emsg_cumul + did_emsg;
4732 int save_suppress_errthrow = suppress_errthrow;
4733 msglist_T **saved_msg_list = NULL;
4734 msglist_T *private_msg_list = NULL;
4735 int save_emsg_silent_def = emsg_silent_def;
4736 int save_did_emsg_def = did_emsg_def;
4737 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004738 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004739
4740// Get pointer to item in the stack.
4741#undef STACK_TV
4742#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4743
4744// Get pointer to item at the bottom of the stack, -1 is the bottom.
4745#undef STACK_TV_BOT
4746#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4747
4748// Get pointer to a local variable on the stack. Negative for arguments.
4749#undef STACK_TV_VAR
4750#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4751
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004752 // Update uf_has_breakpoint if needed.
4753 update_has_breakpoint(ufunc);
4754
Bram Moolenaar4c137212021-04-19 16:48:48 +02004755 if (ufunc->uf_def_status == UF_NOT_COMPILED
4756 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004757 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4758 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004759 == FAIL))
4760 {
4761 if (did_emsg_cumul + did_emsg == did_emsg_before)
4762 semsg(_(e_function_is_not_compiled_str),
4763 printable_func_name(ufunc));
4764 return FAIL;
4765 }
4766
4767 {
4768 // Check the function was really compiled.
4769 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4770 + ufunc->uf_dfunc_idx;
4771 if (INSTRUCTIONS(dfunc) == NULL)
4772 {
4773 iemsg("using call_def_function() on not compiled function");
4774 return FAIL;
4775 }
4776 }
4777
4778 // If depth of calling is getting too high, don't execute the function.
4779 orig_funcdepth = funcdepth_get();
4780 if (funcdepth_increment() == FAIL)
4781 return FAIL;
4782
4783 CLEAR_FIELD(ectx);
4784 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4785 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004786 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004787 {
4788 funcdepth_decrement();
4789 return FAIL;
4790 }
4791 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4792 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4793 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004794 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004795
4796 idx = argc - ufunc->uf_args.ga_len;
4797 if (idx > 0 && ufunc->uf_va_name == NULL)
4798 {
4799 if (idx == 1)
4800 emsg(_(e_one_argument_too_many));
4801 else
4802 semsg(_(e_nr_arguments_too_many), idx);
4803 goto failed_early;
4804 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004805 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4806 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004807 {
4808 if (idx == -1)
4809 emsg(_(e_one_argument_too_few));
4810 else
4811 semsg(_(e_nr_arguments_too_few), -idx);
4812 goto failed_early;
4813 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004814
4815 // Put arguments on the stack, but no more than what the function expects.
4816 // A lambda can be called with more arguments than it uses.
4817 for (idx = 0; idx < argc
4818 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4819 ++idx)
4820 {
4821 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4822 && argv[idx].v_type == VAR_SPECIAL
4823 && argv[idx].vval.v_number == VVAL_NONE)
4824 {
4825 // Use the default value.
4826 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4827 }
4828 else
4829 {
4830 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4831 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004832 ufunc->uf_arg_types[idx], &argv[idx],
4833 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004834 goto failed_early;
4835 copy_tv(&argv[idx], STACK_TV_BOT(0));
4836 }
4837 ++ectx.ec_stack.ga_len;
4838 }
4839
4840 // Turn varargs into a list. Empty list if no args.
4841 if (ufunc->uf_va_name != NULL)
4842 {
4843 int vararg_count = argc - ufunc->uf_args.ga_len;
4844
4845 if (vararg_count < 0)
4846 vararg_count = 0;
4847 else
4848 argc -= vararg_count;
4849 if (exe_newlist(vararg_count, &ectx) == FAIL)
4850 goto failed_early;
4851
4852 // Check the type of the list items.
4853 tv = STACK_TV_BOT(-1);
4854 if (ufunc->uf_va_type != NULL
4855 && ufunc->uf_va_type != &t_list_any
4856 && ufunc->uf_va_type->tt_member != &t_any
4857 && tv->vval.v_list != NULL)
4858 {
4859 type_T *expected = ufunc->uf_va_type->tt_member;
4860 listitem_T *li = tv->vval.v_list->lv_first;
4861
4862 for (idx = 0; idx < vararg_count; ++idx)
4863 {
4864 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004865 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004866 goto failed_early;
4867 li = li->li_next;
4868 }
4869 }
4870
4871 if (defcount > 0)
4872 // Move varargs list to below missing default arguments.
4873 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4874 --ectx.ec_stack.ga_len;
4875 }
4876
4877 // Make space for omitted arguments, will store default value below.
4878 // Any varargs list goes after them.
4879 if (defcount > 0)
4880 for (idx = 0; idx < defcount; ++idx)
4881 {
4882 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4883 ++ectx.ec_stack.ga_len;
4884 }
4885 if (ufunc->uf_va_name != NULL)
4886 ++ectx.ec_stack.ga_len;
4887
4888 // Frame pointer points to just after arguments.
4889 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4890 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4891
4892 {
4893 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4894 + ufunc->uf_dfunc_idx;
4895 ufunc_T *base_ufunc = dfunc->df_ufunc;
4896
4897 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4898 // by copy_func().
4899 if (partial != NULL || base_ufunc->uf_partial != NULL)
4900 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004901 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4902 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004903 goto failed_early;
4904 if (partial != NULL)
4905 {
4906 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4907 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004908 if (current_ectx->ec_outer_ref != NULL
4909 && current_ectx->ec_outer_ref->or_outer != NULL)
4910 ectx.ec_outer_ref->or_outer =
4911 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004912 }
4913 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004914 {
4915 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4916 ++partial->pt_refcount;
4917 ectx.ec_outer_ref->or_partial = partial;
4918 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004919 }
4920 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004921 {
4922 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4923 ++base_ufunc->uf_partial->pt_refcount;
4924 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4925 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004926 }
4927 }
4928
4929 // dummy frame entries
4930 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4931 {
4932 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4933 ++ectx.ec_stack.ga_len;
4934 }
4935
4936 {
4937 // Reserve space for local variables and any closure reference count.
4938 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4939 + ufunc->uf_dfunc_idx;
4940
4941 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4942 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4943 ectx.ec_stack.ga_len += dfunc->df_varcount;
4944 if (dfunc->df_has_closure)
4945 {
4946 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4947 STACK_TV_VAR(idx)->vval.v_number = 0;
4948 ++ectx.ec_stack.ga_len;
4949 }
4950
4951 ectx.ec_instr = INSTRUCTIONS(dfunc);
4952 }
4953
4954 // Following errors are in the function, not the caller.
4955 // Commands behave like vim9script.
4956 estack_push_ufunc(ufunc, 1);
4957 current_sctx = ufunc->uf_script_ctx;
4958 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4959
4960 // Use a specific location for storing error messages to be converted to an
4961 // exception.
4962 saved_msg_list = msg_list;
4963 msg_list = &private_msg_list;
4964
4965 // Do turn errors into exceptions.
4966 suppress_errthrow = FALSE;
4967
4968 // Do not delete the function while executing it.
4969 ++ufunc->uf_calls;
4970
4971 // When ":silent!" was used before calling then we still abort the
4972 // function. If ":silent!" is used in the function then we don't.
4973 emsg_silent_def = emsg_silent;
4974 did_emsg_def = 0;
4975
4976 ectx.ec_where.wt_index = 0;
4977 ectx.ec_where.wt_variable = FALSE;
4978
4979 // Execute the instructions until done.
4980 ret = exec_instructions(&ectx);
4981 if (ret == OK)
4982 {
4983 // function finished, get result from the stack.
4984 if (ufunc->uf_ret_type == &t_void)
4985 {
4986 rettv->v_type = VAR_VOID;
4987 }
4988 else
4989 {
4990 tv = STACK_TV_BOT(-1);
4991 *rettv = *tv;
4992 tv->v_type = VAR_UNKNOWN;
4993 }
4994 }
4995
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004996 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004997 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4998 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004999
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005000 // Deal with any remaining closures, they may be in use somewhere.
5001 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005002 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005003 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005004 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
5005 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005006
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005007 estack_pop();
5008 current_sctx = save_current_sctx;
5009
Bram Moolenaar2336c372021-12-06 15:06:54 +00005010 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5011 // Function was unreferenced while being used, free it now.
5012 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005013
Bram Moolenaar352134b2020-10-17 22:04:08 +02005014 if (*msg_list != NULL && saved_msg_list != NULL)
5015 {
5016 msglist_T **plist = saved_msg_list;
5017
5018 // Append entries from the current msg_list (uncaught exceptions) to
5019 // the saved msg_list.
5020 while (*plist != NULL)
5021 plist = &(*plist)->next;
5022
5023 *plist = *msg_list;
5024 }
5025 msg_list = saved_msg_list;
5026
Bram Moolenaar4c137212021-04-19 16:48:48 +02005027 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005028 {
5029 cmdmod.cmod_filter_regmatch.regprog = NULL;
5030 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005031 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005032 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005033 emsg_silent_def = save_emsg_silent_def;
5034 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005035
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005036failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005037 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5039 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005040 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005041
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005043 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005044 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005045 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005046 if (ectx.ec_outer_ref->or_outer_allocated)
5047 vim_free(ectx.ec_outer_ref->or_outer);
5048 partial_unref(ectx.ec_outer_ref->or_partial);
5049 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005050 }
5051
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005052 // Not sure if this is necessary.
5053 suppress_errthrow = save_suppress_errthrow;
5054
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005055 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5056 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005057 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005058 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005059 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 return ret;
5061}
5062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005064 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5065 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5066 * "pfx" is prefixed to every line.
5067 */
5068 static void
5069list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5070{
5071 int line_idx = 0;
5072 int prev_current = 0;
5073 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005074 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005075
5076 for (current = 0; current < instr_count; ++current)
5077 {
5078 isn_T *iptr = &instr[current];
5079 char *line;
5080
5081 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005082 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005083 while (line_idx < iptr->isn_lnum
5084 && line_idx < ufunc->uf_lines.ga_len)
5085 {
5086 if (current > prev_current)
5087 {
5088 msg_puts("\n\n");
5089 prev_current = current;
5090 }
5091 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5092 if (line != NULL)
5093 msg(line);
5094 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005095 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5096 {
5097 int first_def_arg = ufunc->uf_args.ga_len
5098 - ufunc->uf_def_args.ga_len;
5099
5100 if (def_arg_idx > 0)
5101 msg_puts("\n\n");
5102 msg_start();
5103 msg_puts(" ");
5104 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5105 first_def_arg + def_arg_idx]);
5106 msg_puts(" = ");
5107 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5108 msg_clr_eos();
5109 msg_end();
5110 }
5111 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005112
5113 switch (iptr->isn_type)
5114 {
5115 case ISN_EXEC:
5116 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5117 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005118 case ISN_EXEC_SPLIT:
5119 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5120 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005121 case ISN_EXECRANGE:
5122 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5123 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005124 case ISN_LEGACY_EVAL:
5125 smsg("%s%4d EVAL legacy %s", pfx, current,
5126 iptr->isn_arg.string);
5127 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005128 case ISN_REDIRSTART:
5129 smsg("%s%4d REDIR", pfx, current);
5130 break;
5131 case ISN_REDIREND:
5132 smsg("%s%4d REDIR END%s", pfx, current,
5133 iptr->isn_arg.number ? " append" : "");
5134 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005135 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005136#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005137 smsg("%s%4d CEXPR pre %s", pfx, current,
5138 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005139#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005140 break;
5141 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005142#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005143 {
5144 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5145
5146 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5147 cexpr_get_auname(cer->cer_cmdidx),
5148 cer->cer_forceit ? "!" : "",
5149 cer->cer_cmdline);
5150 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005151#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005152 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005153 case ISN_INSTR:
5154 {
5155 smsg("%s%4d INSTR", pfx, current);
5156 list_instructions(" ", iptr->isn_arg.instr,
5157 INT_MAX, NULL);
5158 msg(" -------------");
5159 }
5160 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005161 case ISN_SUBSTITUTE:
5162 {
5163 subs_T *subs = &iptr->isn_arg.subs;
5164
5165 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5166 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5167 msg(" -------------");
5168 }
5169 break;
5170 case ISN_EXECCONCAT:
5171 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5172 (varnumber_T)iptr->isn_arg.number);
5173 break;
5174 case ISN_ECHO:
5175 {
5176 echo_T *echo = &iptr->isn_arg.echo;
5177
5178 smsg("%s%4d %s %d", pfx, current,
5179 echo->echo_with_white ? "ECHO" : "ECHON",
5180 echo->echo_count);
5181 }
5182 break;
5183 case ISN_EXECUTE:
5184 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005185 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005186 break;
5187 case ISN_ECHOMSG:
5188 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005189 (varnumber_T)(iptr->isn_arg.number));
5190 break;
5191 case ISN_ECHOCONSOLE:
5192 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5193 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005194 break;
5195 case ISN_ECHOERR:
5196 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005197 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005198 break;
5199 case ISN_LOAD:
5200 {
5201 if (iptr->isn_arg.number < 0)
5202 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5203 (varnumber_T)(iptr->isn_arg.number
5204 + STACK_FRAME_SIZE));
5205 else
5206 smsg("%s%4d LOAD $%lld", pfx, current,
5207 (varnumber_T)(iptr->isn_arg.number));
5208 }
5209 break;
5210 case ISN_LOADOUTER:
5211 {
5212 if (iptr->isn_arg.number < 0)
5213 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5214 iptr->isn_arg.outer.outer_depth,
5215 iptr->isn_arg.outer.outer_idx
5216 + STACK_FRAME_SIZE);
5217 else
5218 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5219 iptr->isn_arg.outer.outer_depth,
5220 iptr->isn_arg.outer.outer_idx);
5221 }
5222 break;
5223 case ISN_LOADV:
5224 smsg("%s%4d LOADV v:%s", pfx, current,
5225 get_vim_var_name(iptr->isn_arg.number));
5226 break;
5227 case ISN_LOADSCRIPT:
5228 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005229 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5230 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5231 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005232
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005233 sv = get_script_svar(sref, -1);
5234 if (sv == NULL)
5235 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5236 pfx, current, si->sn_name);
5237 else
5238 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005239 sv->sv_name,
5240 sref->sref_idx,
5241 si->sn_name);
5242 }
5243 break;
5244 case ISN_LOADS:
5245 {
5246 scriptitem_T *si = SCRIPT_ITEM(
5247 iptr->isn_arg.loadstore.ls_sid);
5248
5249 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5250 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5251 }
5252 break;
5253 case ISN_LOADAUTO:
5254 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5255 break;
5256 case ISN_LOADG:
5257 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5258 break;
5259 case ISN_LOADB:
5260 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5261 break;
5262 case ISN_LOADW:
5263 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5264 break;
5265 case ISN_LOADT:
5266 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5267 break;
5268 case ISN_LOADGDICT:
5269 smsg("%s%4d LOAD g:", pfx, current);
5270 break;
5271 case ISN_LOADBDICT:
5272 smsg("%s%4d LOAD b:", pfx, current);
5273 break;
5274 case ISN_LOADWDICT:
5275 smsg("%s%4d LOAD w:", pfx, current);
5276 break;
5277 case ISN_LOADTDICT:
5278 smsg("%s%4d LOAD t:", pfx, current);
5279 break;
5280 case ISN_LOADOPT:
5281 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5282 break;
5283 case ISN_LOADENV:
5284 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5285 break;
5286 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005287 smsg("%s%4d LOADREG @%c", pfx, current,
5288 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005289 break;
5290
5291 case ISN_STORE:
5292 if (iptr->isn_arg.number < 0)
5293 smsg("%s%4d STORE arg[%lld]", pfx, current,
5294 iptr->isn_arg.number + STACK_FRAME_SIZE);
5295 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005296 smsg("%s%4d STORE $%lld", pfx, current,
5297 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005298 break;
5299 case ISN_STOREOUTER:
5300 {
5301 if (iptr->isn_arg.number < 0)
5302 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5303 iptr->isn_arg.outer.outer_depth,
5304 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5305 else
5306 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5307 iptr->isn_arg.outer.outer_depth,
5308 iptr->isn_arg.outer.outer_idx);
5309 }
5310 break;
5311 case ISN_STOREV:
5312 smsg("%s%4d STOREV v:%s", pfx, current,
5313 get_vim_var_name(iptr->isn_arg.number));
5314 break;
5315 case ISN_STOREAUTO:
5316 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5317 break;
5318 case ISN_STOREG:
5319 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5320 break;
5321 case ISN_STOREB:
5322 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5323 break;
5324 case ISN_STOREW:
5325 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5326 break;
5327 case ISN_STORET:
5328 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5329 break;
5330 case ISN_STORES:
5331 {
5332 scriptitem_T *si = SCRIPT_ITEM(
5333 iptr->isn_arg.loadstore.ls_sid);
5334
5335 smsg("%s%4d STORES %s in %s", pfx, current,
5336 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5337 }
5338 break;
5339 case ISN_STORESCRIPT:
5340 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005341 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5342 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5343 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005344
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005345 sv = get_script_svar(sref, -1);
5346 if (sv == NULL)
5347 smsg("%s%4d STORESCRIPT [deleted] in %s",
5348 pfx, current, si->sn_name);
5349 else
5350 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005351 sv->sv_name,
5352 sref->sref_idx,
5353 si->sn_name);
5354 }
5355 break;
5356 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005357 case ISN_STOREFUNCOPT:
5358 smsg("%s%4d %s &%s", pfx, current,
5359 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005360 iptr->isn_arg.storeopt.so_name);
5361 break;
5362 case ISN_STOREENV:
5363 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5364 break;
5365 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005366 smsg("%s%4d STOREREG @%c", pfx, current,
5367 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005368 break;
5369 case ISN_STORENR:
5370 smsg("%s%4d STORE %lld in $%d", pfx, current,
5371 iptr->isn_arg.storenr.stnr_val,
5372 iptr->isn_arg.storenr.stnr_idx);
5373 break;
5374
5375 case ISN_STOREINDEX:
5376 smsg("%s%4d STOREINDEX %s", pfx, current,
5377 vartype_name(iptr->isn_arg.vartype));
5378 break;
5379
5380 case ISN_STORERANGE:
5381 smsg("%s%4d STORERANGE", pfx, current);
5382 break;
5383
5384 // constants
5385 case ISN_PUSHNR:
5386 smsg("%s%4d PUSHNR %lld", pfx, current,
5387 (varnumber_T)(iptr->isn_arg.number));
5388 break;
5389 case ISN_PUSHBOOL:
5390 case ISN_PUSHSPEC:
5391 smsg("%s%4d PUSH %s", pfx, current,
5392 get_var_special_name(iptr->isn_arg.number));
5393 break;
5394 case ISN_PUSHF:
5395#ifdef FEAT_FLOAT
5396 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5397#endif
5398 break;
5399 case ISN_PUSHS:
5400 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5401 break;
5402 case ISN_PUSHBLOB:
5403 {
5404 char_u *r;
5405 char_u numbuf[NUMBUFLEN];
5406 char_u *tofree;
5407
5408 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5409 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5410 vim_free(tofree);
5411 }
5412 break;
5413 case ISN_PUSHFUNC:
5414 {
5415 char *name = (char *)iptr->isn_arg.string;
5416
5417 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5418 name == NULL ? "[none]" : name);
5419 }
5420 break;
5421 case ISN_PUSHCHANNEL:
5422#ifdef FEAT_JOB_CHANNEL
5423 {
5424 channel_T *channel = iptr->isn_arg.channel;
5425
5426 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5427 channel == NULL ? 0 : channel->ch_id);
5428 }
5429#endif
5430 break;
5431 case ISN_PUSHJOB:
5432#ifdef FEAT_JOB_CHANNEL
5433 {
5434 typval_T tv;
5435 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005436 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005437
5438 tv.v_type = VAR_JOB;
5439 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005440 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005441 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5442 }
5443#endif
5444 break;
5445 case ISN_PUSHEXC:
5446 smsg("%s%4d PUSH v:exception", pfx, current);
5447 break;
5448 case ISN_UNLET:
5449 smsg("%s%4d UNLET%s %s", pfx, current,
5450 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5451 iptr->isn_arg.unlet.ul_name);
5452 break;
5453 case ISN_UNLETENV:
5454 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5455 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5456 iptr->isn_arg.unlet.ul_name);
5457 break;
5458 case ISN_UNLETINDEX:
5459 smsg("%s%4d UNLETINDEX", pfx, current);
5460 break;
5461 case ISN_UNLETRANGE:
5462 smsg("%s%4d UNLETRANGE", pfx, current);
5463 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005464 case ISN_LOCKUNLOCK:
5465 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5466 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005467 case ISN_LOCKCONST:
5468 smsg("%s%4d LOCKCONST", pfx, current);
5469 break;
5470 case ISN_NEWLIST:
5471 smsg("%s%4d NEWLIST size %lld", pfx, current,
5472 (varnumber_T)(iptr->isn_arg.number));
5473 break;
5474 case ISN_NEWDICT:
5475 smsg("%s%4d NEWDICT size %lld", pfx, current,
5476 (varnumber_T)(iptr->isn_arg.number));
5477 break;
5478
5479 // function call
5480 case ISN_BCALL:
5481 {
5482 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5483
5484 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5485 internal_func_name(cbfunc->cbf_idx),
5486 cbfunc->cbf_argcount);
5487 }
5488 break;
5489 case ISN_DCALL:
5490 {
5491 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5492 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5493 + cdfunc->cdf_idx;
5494
5495 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005496 printable_func_name(df->df_ufunc),
5497 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005498 }
5499 break;
5500 case ISN_UCALL:
5501 {
5502 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5503
5504 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5505 cufunc->cuf_name, cufunc->cuf_argcount);
5506 }
5507 break;
5508 case ISN_PCALL:
5509 {
5510 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5511
5512 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5513 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5514 }
5515 break;
5516 case ISN_PCALL_END:
5517 smsg("%s%4d PCALL end", pfx, current);
5518 break;
5519 case ISN_RETURN:
5520 smsg("%s%4d RETURN", pfx, current);
5521 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005522 case ISN_RETURN_VOID:
5523 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005524 break;
5525 case ISN_FUNCREF:
5526 {
5527 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005528 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005529
Bram Moolenaar38453522021-11-28 22:00:12 +00005530 if (funcref->fr_func_name == NULL)
5531 {
5532 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5533 + funcref->fr_dfunc_idx;
5534 name = df->df_ufunc->uf_name;
5535 }
5536 else
5537 name = funcref->fr_func_name;
5538 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005539 }
5540 break;
5541
5542 case ISN_NEWFUNC:
5543 {
5544 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5545
5546 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5547 newfunc->nf_lambda, newfunc->nf_global);
5548 }
5549 break;
5550
5551 case ISN_DEF:
5552 {
5553 char_u *name = iptr->isn_arg.string;
5554
5555 smsg("%s%4d DEF %s", pfx, current,
5556 name == NULL ? (char_u *)"" : name);
5557 }
5558 break;
5559
5560 case ISN_JUMP:
5561 {
5562 char *when = "?";
5563
5564 switch (iptr->isn_arg.jump.jump_when)
5565 {
5566 case JUMP_ALWAYS:
5567 when = "JUMP";
5568 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005569 case JUMP_NEVER:
5570 iemsg("JUMP_NEVER should not be used");
5571 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005572 case JUMP_AND_KEEP_IF_TRUE:
5573 when = "JUMP_AND_KEEP_IF_TRUE";
5574 break;
5575 case JUMP_IF_FALSE:
5576 when = "JUMP_IF_FALSE";
5577 break;
5578 case JUMP_AND_KEEP_IF_FALSE:
5579 when = "JUMP_AND_KEEP_IF_FALSE";
5580 break;
5581 case JUMP_IF_COND_FALSE:
5582 when = "JUMP_IF_COND_FALSE";
5583 break;
5584 case JUMP_IF_COND_TRUE:
5585 when = "JUMP_IF_COND_TRUE";
5586 break;
5587 }
5588 smsg("%s%4d %s -> %d", pfx, current, when,
5589 iptr->isn_arg.jump.jump_where);
5590 }
5591 break;
5592
5593 case ISN_JUMP_IF_ARG_SET:
5594 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5595 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5596 iptr->isn_arg.jump.jump_where);
5597 break;
5598
5599 case ISN_FOR:
5600 {
5601 forloop_T *forloop = &iptr->isn_arg.forloop;
5602
5603 smsg("%s%4d FOR $%d -> %d", pfx, current,
5604 forloop->for_idx, forloop->for_end);
5605 }
5606 break;
5607
5608 case ISN_TRY:
5609 {
5610 try_T *try = &iptr->isn_arg.try;
5611
5612 if (try->try_ref->try_finally == 0)
5613 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5614 pfx, current,
5615 try->try_ref->try_catch,
5616 try->try_ref->try_endtry);
5617 else
5618 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5619 pfx, current,
5620 try->try_ref->try_catch,
5621 try->try_ref->try_finally,
5622 try->try_ref->try_endtry);
5623 }
5624 break;
5625 case ISN_CATCH:
5626 // TODO
5627 smsg("%s%4d CATCH", pfx, current);
5628 break;
5629 case ISN_TRYCONT:
5630 {
5631 trycont_T *trycont = &iptr->isn_arg.trycont;
5632
5633 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5634 trycont->tct_levels,
5635 trycont->tct_levels == 1 ? "" : "s",
5636 trycont->tct_where);
5637 }
5638 break;
5639 case ISN_FINALLY:
5640 smsg("%s%4d FINALLY", pfx, current);
5641 break;
5642 case ISN_ENDTRY:
5643 smsg("%s%4d ENDTRY", pfx, current);
5644 break;
5645 case ISN_THROW:
5646 smsg("%s%4d THROW", pfx, current);
5647 break;
5648
5649 // expression operations on number
5650 case ISN_OPNR:
5651 case ISN_OPFLOAT:
5652 case ISN_OPANY:
5653 {
5654 char *what;
5655 char *ins;
5656
5657 switch (iptr->isn_arg.op.op_type)
5658 {
5659 case EXPR_MULT: what = "*"; break;
5660 case EXPR_DIV: what = "/"; break;
5661 case EXPR_REM: what = "%"; break;
5662 case EXPR_SUB: what = "-"; break;
5663 case EXPR_ADD: what = "+"; break;
5664 default: what = "???"; break;
5665 }
5666 switch (iptr->isn_type)
5667 {
5668 case ISN_OPNR: ins = "OPNR"; break;
5669 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5670 case ISN_OPANY: ins = "OPANY"; break;
5671 default: ins = "???"; break;
5672 }
5673 smsg("%s%4d %s %s", pfx, current, ins, what);
5674 }
5675 break;
5676
5677 case ISN_COMPAREBOOL:
5678 case ISN_COMPARESPECIAL:
5679 case ISN_COMPARENR:
5680 case ISN_COMPAREFLOAT:
5681 case ISN_COMPARESTRING:
5682 case ISN_COMPAREBLOB:
5683 case ISN_COMPARELIST:
5684 case ISN_COMPAREDICT:
5685 case ISN_COMPAREFUNC:
5686 case ISN_COMPAREANY:
5687 {
5688 char *p;
5689 char buf[10];
5690 char *type;
5691
5692 switch (iptr->isn_arg.op.op_type)
5693 {
5694 case EXPR_EQUAL: p = "=="; break;
5695 case EXPR_NEQUAL: p = "!="; break;
5696 case EXPR_GREATER: p = ">"; break;
5697 case EXPR_GEQUAL: p = ">="; break;
5698 case EXPR_SMALLER: p = "<"; break;
5699 case EXPR_SEQUAL: p = "<="; break;
5700 case EXPR_MATCH: p = "=~"; break;
5701 case EXPR_IS: p = "is"; break;
5702 case EXPR_ISNOT: p = "isnot"; break;
5703 case EXPR_NOMATCH: p = "!~"; break;
5704 default: p = "???"; break;
5705 }
5706 STRCPY(buf, p);
5707 if (iptr->isn_arg.op.op_ic == TRUE)
5708 strcat(buf, "?");
5709 switch(iptr->isn_type)
5710 {
5711 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5712 case ISN_COMPARESPECIAL:
5713 type = "COMPARESPECIAL"; break;
5714 case ISN_COMPARENR: type = "COMPARENR"; break;
5715 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5716 case ISN_COMPARESTRING:
5717 type = "COMPARESTRING"; break;
5718 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5719 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5720 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5721 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5722 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5723 default: type = "???"; break;
5724 }
5725
5726 smsg("%s%4d %s %s", pfx, current, type, buf);
5727 }
5728 break;
5729
5730 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5731 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5732
5733 // expression operations
5734 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5735 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5736 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5737 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5738 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5739 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5740 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5741 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5742 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5743 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5744 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5745 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005746 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005747 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5748 iptr->isn_arg.getitem.gi_index,
5749 iptr->isn_arg.getitem.gi_with_op ?
5750 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005751 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5752 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5753 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005754 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5755 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5756
Bram Moolenaar4c137212021-04-19 16:48:48 +02005757 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5758
5759 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5760 case ISN_CHECKTYPE:
5761 {
5762 checktype_T *ct = &iptr->isn_arg.type;
5763 char *tofree;
5764
5765 if (ct->ct_arg_idx == 0)
5766 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5767 type_name(ct->ct_type, &tofree),
5768 (int)ct->ct_off);
5769 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005770 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5771 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005772 type_name(ct->ct_type, &tofree),
5773 (int)ct->ct_off,
5774 (int)ct->ct_arg_idx);
5775 vim_free(tofree);
5776 break;
5777 }
5778 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5779 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5780 iptr->isn_arg.checklen.cl_min_len);
5781 break;
5782 case ISN_SETTYPE:
5783 {
5784 char *tofree;
5785
5786 smsg("%s%4d SETTYPE %s", pfx, current,
5787 type_name(iptr->isn_arg.type.ct_type, &tofree));
5788 vim_free(tofree);
5789 break;
5790 }
5791 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005792 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5793 smsg("%s%4d INVERT %d (!val)", pfx, current,
5794 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005795 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005796 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5797 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005798 break;
5799 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005800 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005801 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005802 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5803 pfx, current,
5804 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005805 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005806 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5807 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005808 break;
5809 case ISN_PUT:
5810 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5811 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005812 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005813 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5814 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005815 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005816 else
5817 smsg("%s%4d PUT %c %ld", pfx, current,
5818 iptr->isn_arg.put.put_regname,
5819 (long)iptr->isn_arg.put.put_lnum);
5820 break;
5821
5822 // TODO: summarize modifiers
5823 case ISN_CMDMOD:
5824 {
5825 char_u *buf;
5826 size_t len = produce_cmdmods(
5827 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5828
5829 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005830 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005831 {
5832 (void)produce_cmdmods(
5833 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5834 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5835 vim_free(buf);
5836 }
5837 break;
5838 }
5839 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5840
5841 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005842 smsg("%s%4d PROFILE START line %d", pfx, current,
5843 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005844 break;
5845
5846 case ISN_PROF_END:
5847 smsg("%s%4d PROFILE END", pfx, current);
5848 break;
5849
Bram Moolenaare99d4222021-06-13 14:01:26 +02005850 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005851 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5852 iptr->isn_arg.debug.dbg_break_lnum + 1,
5853 iptr->isn_lnum,
5854 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005855 break;
5856
Bram Moolenaar4c137212021-04-19 16:48:48 +02005857 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5858 iptr->isn_arg.unpack.unp_count,
5859 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5860 break;
5861 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5862 iptr->isn_arg.shuffle.shfl_item,
5863 iptr->isn_arg.shuffle.shfl_up);
5864 break;
5865 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5866
5867 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5868 return;
5869 }
5870
5871 out_flush(); // output one line at a time
5872 ui_breakcheck();
5873 if (got_int)
5874 break;
5875 }
5876}
5877
5878/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005879 * Handle command line completion for the :disassemble command.
5880 */
5881 void
5882set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5883{
5884 char_u *p;
5885
5886 // Default: expand user functions, "debug" and "profile"
5887 xp->xp_context = EXPAND_DISASSEMBLE;
5888 xp->xp_pattern = arg;
5889
5890 // first argument already typed: only user function names
5891 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5892 {
5893 xp->xp_context = EXPAND_USER_FUNC;
5894 xp->xp_pattern = skipwhite(p);
5895 }
5896}
5897
5898/*
5899 * Function given to ExpandGeneric() to obtain the list of :disassemble
5900 * arguments.
5901 */
5902 char_u *
5903get_disassemble_argument(expand_T *xp, int idx)
5904{
5905 if (idx == 0)
5906 return (char_u *)"debug";
5907 if (idx == 1)
5908 return (char_u *)"profile";
5909 return get_user_func_name(xp, idx - 2);
5910}
5911
5912/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005913 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005914 * We don't really need this at runtime, but we do have tests that require it,
5915 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 */
5917 void
5918ex_disassemble(exarg_T *eap)
5919{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005920 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005921 char_u *fname;
5922 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005923 dfunc_T *dfunc;
5924 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005925 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005926 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005927 compiletype_T compile_type = CT_NONE;
5928
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005929 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005930 {
5931 compile_type = CT_PROFILE;
5932 arg = skipwhite(arg + 7);
5933 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005934 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005935 {
5936 compile_type = CT_DEBUG;
5937 arg = skipwhite(arg + 5);
5938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005940 if (STRNCMP(arg, "<lambda>", 8) == 0)
5941 {
5942 arg += 8;
5943 (void)getdigits(&arg);
5944 fname = vim_strnsave(eap->arg, arg - eap->arg);
5945 }
5946 else
5947 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005948 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005949 if (fname == NULL)
5950 {
5951 semsg(_(e_invarg2), eap->arg);
5952 return;
5953 }
5954
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005955 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005956 if (ufunc == NULL)
5957 {
5958 char_u *p = untrans_function_name(fname);
5959
5960 if (p != NULL)
5961 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005962 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005963 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005964 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965 if (ufunc == NULL)
5966 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005967 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968 return;
5969 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005970 if (func_needs_compiling(ufunc, compile_type)
5971 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005972 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005973 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005975 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005976 return;
5977 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005978 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979
5980 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005981 switch (compile_type)
5982 {
5983 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005984#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005985 instr = dfunc->df_instr_prof;
5986 instr_count = dfunc->df_instr_prof_count;
5987 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005988#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005989 // FALLTHROUGH
5990 case CT_NONE:
5991 instr = dfunc->df_instr;
5992 instr_count = dfunc->df_instr_count;
5993 break;
5994 case CT_DEBUG:
5995 instr = dfunc->df_instr_debug;
5996 instr_count = dfunc->df_instr_debug_count;
5997 break;
5998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999
Bram Moolenaar4c137212021-04-19 16:48:48 +02006000 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001}
6002
6003/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006004 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005 * list, etc. Mostly like what JavaScript does, except that empty list and
6006 * empty dictionary are FALSE.
6007 */
6008 int
6009tv2bool(typval_T *tv)
6010{
6011 switch (tv->v_type)
6012 {
6013 case VAR_NUMBER:
6014 return tv->vval.v_number != 0;
6015 case VAR_FLOAT:
6016#ifdef FEAT_FLOAT
6017 return tv->vval.v_float != 0.0;
6018#else
6019 break;
6020#endif
6021 case VAR_PARTIAL:
6022 return tv->vval.v_partial != NULL;
6023 case VAR_FUNC:
6024 case VAR_STRING:
6025 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6026 case VAR_LIST:
6027 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6028 case VAR_DICT:
6029 return tv->vval.v_dict != NULL
6030 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6031 case VAR_BOOL:
6032 case VAR_SPECIAL:
6033 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6034 case VAR_JOB:
6035#ifdef FEAT_JOB_CHANNEL
6036 return tv->vval.v_job != NULL;
6037#else
6038 break;
6039#endif
6040 case VAR_CHANNEL:
6041#ifdef FEAT_JOB_CHANNEL
6042 return tv->vval.v_channel != NULL;
6043#else
6044 break;
6045#endif
6046 case VAR_BLOB:
6047 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6048 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006049 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006051 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 break;
6053 }
6054 return FALSE;
6055}
6056
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006057 void
6058emsg_using_string_as(typval_T *tv, int as_number)
6059{
6060 semsg(_(as_number ? e_using_string_as_number_str
6061 : e_using_string_as_bool_str),
6062 tv->vval.v_string == NULL
6063 ? (char_u *)"" : tv->vval.v_string);
6064}
6065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066/*
6067 * If "tv" is a string give an error and return FAIL.
6068 */
6069 int
6070check_not_string(typval_T *tv)
6071{
6072 if (tv->v_type == VAR_STRING)
6073 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006074 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075 clear_tv(tv);
6076 return FAIL;
6077 }
6078 return OK;
6079}
6080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081
6082#endif // FEAT_EVAL