blob: 3160f0df2e1d325b41a494d77a37d1bbb09a1834 [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 Moolenaar919c12c2021-12-14 14:29:16 +0000683
684 // No check for uf_refcount being zero, cannot think of a way that would
685 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100686 --dfunc->df_ufunc->uf_calls;
687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200689 entry = estack_pop();
690 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200691 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200693 if (handle_closure_in_use(ectx, TRUE) == FAIL)
694 return FAIL;
695
696 // Clear the arguments.
697 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
698 clear_tv(STACK_TV(idx));
699
700 // Clear local variables and temp values, but not the return value.
701 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100702 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100704
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100705 // The return value should be on top of the stack. However, when aborting
706 // it may not be there and ec_frame_idx is the top of the stack.
707 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100708 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100709 ret_idx = 0;
710
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200711 if (ectx->ec_outer_ref != NULL)
712 {
713 if (ectx->ec_outer_ref->or_outer_allocated)
714 vim_free(ectx->ec_outer_ref->or_outer);
715 partial_unref(ectx->ec_outer_ref->or_partial);
716 vim_free(ectx->ec_outer_ref);
717 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100718
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100719 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100720 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100721 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
722 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200723 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
724 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200725 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100726 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100727 floc = (void *)STACK_TV(ectx->ec_frame_idx
728 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200729 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100730 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
731 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200732
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100733 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200734 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100735 else
736 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200737 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100738 vim_free(floc);
739 }
740
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100741 if (ret_idx > 0)
742 {
743 // Reset the stack to the position before the call, with a spot for the
744 // return value, moved there from above the frame.
745 ectx->ec_stack.ga_len = top + 1;
746 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
747 }
748 else
749 // Reset the stack to the position before the call.
750 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200751
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100752 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200753 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200754 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755}
756
757#undef STACK_TV
758
759/*
760 * Prepare arguments and rettv for calling a builtin or user function.
761 */
762 static int
763call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
764{
765 int idx;
766 typval_T *tv;
767
768 // Move arguments from bottom of the stack to argvars[] and add terminator.
769 for (idx = 0; idx < argcount; ++idx)
770 argvars[idx] = *STACK_TV_BOT(idx - argcount);
771 argvars[argcount].v_type = VAR_UNKNOWN;
772
773 // Result replaces the arguments on the stack.
774 if (argcount > 0)
775 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200776 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 return FAIL;
778 else
779 ++ectx->ec_stack.ga_len;
780
781 // Default return value is zero.
782 tv = STACK_TV_BOT(-1);
783 tv->v_type = VAR_NUMBER;
784 tv->vval.v_number = 0;
785
786 return OK;
787}
788
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200789// Ugly global to avoid passing the execution context around through many
790// layers.
791static ectx_T *current_ectx = NULL;
792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793/*
794 * Call a builtin function by index.
795 */
796 static int
797call_bfunc(int func_idx, int argcount, ectx_T *ectx)
798{
799 typval_T argvars[MAX_FUNC_ARGS];
800 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100801 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200802 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200803 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804
805 if (call_prepare(argcount, argvars, ectx) == FAIL)
806 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200807 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200809 // Call the builtin function. Set "current_ectx" so that when it
810 // recursively invokes call_def_function() a closure context can be set.
811 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200813 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200814 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815
816 // Clear the arguments.
817 for (idx = 0; idx < argcount; ++idx)
818 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200819
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100820 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200821 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822 return OK;
823}
824
825/*
826 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100827 * If the function is compiled this will add a stack frame and set the
828 * instruction pointer at the start of the function.
829 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200830 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100831 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 */
833 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100834call_ufunc(
835 ufunc_T *ufunc,
836 partial_T *pt,
837 int argcount,
838 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200839 isn_T *iptr,
840 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841{
842 typval_T argvars[MAX_FUNC_ARGS];
843 funcexe_T funcexe;
844 int error;
845 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100846 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200847 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848
Bram Moolenaare99d4222021-06-13 14:01:26 +0200849 if (func_needs_compiling(ufunc, compile_type)
850 && compile_def_function(ufunc, FALSE, compile_type, NULL)
851 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200852 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200853 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100854 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100855 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100856 if (error != FCERR_UNKNOWN)
857 {
858 if (error == FCERR_TOOMANY)
859 semsg(_(e_toomanyarg), ufunc->uf_name);
860 else
861 semsg(_(e_toofewarg), ufunc->uf_name);
862 return FAIL;
863 }
864
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100865 // The function has been compiled, can call it quickly. For a function
866 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100867 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100868 if (iptr != NULL)
869 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100870 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100871 iptr->isn_type = ISN_DCALL;
872 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
873 iptr->isn_arg.dfunc.cdf_argcount = argcount;
874 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200875 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877
878 if (call_prepare(argcount, argvars, ectx) == FAIL)
879 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200880 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000881 funcexe.fe_evaluate = TRUE;
882 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883
884 // Call the user function. Result goes in last position on the stack.
885 // TODO: add selfdict if there is one
886 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000887 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
889 // Clear the arguments.
890 for (idx = 0; idx < argcount; ++idx)
891 clear_tv(&argvars[idx]);
892
893 if (error != FCERR_NONE)
894 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000895 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 return FAIL;
897 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100898 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200899 // Error other than from calling the function itself.
900 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 return OK;
902}
903
904/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100905 * If command modifiers were applied restore them.
906 */
907 static void
908may_restore_cmdmod(funclocal_T *funclocal)
909{
910 if (funclocal->floc_restore_cmdmod)
911 {
912 cmdmod.cmod_filter_regmatch.regprog = NULL;
913 undo_cmdmod(&cmdmod);
914 cmdmod = funclocal->floc_save_cmdmod;
915 funclocal->floc_restore_cmdmod = FALSE;
916 }
917}
918
919/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200920 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
921 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200922 */
923 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200924vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200925{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200926 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200927}
928
929/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 * Execute a function by "name".
931 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100932 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 * Returns FAIL if not found without an error message.
934 */
935 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100936call_by_name(
937 char_u *name,
938 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100939 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200940 isn_T *iptr,
941 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942{
943 ufunc_T *ufunc;
944
945 if (builtin_function(name, -1))
946 {
947 int func_idx = find_internal_func(name);
948
949 if (func_idx < 0)
950 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200951 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952 return FAIL;
953 return call_bfunc(func_idx, argcount, ectx);
954 }
955
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200956 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200957
958 if (ufunc == NULL)
959 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200960 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +0200961
962 if (script_autoload(name, TRUE))
963 // loaded a package, search for the function again
964 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200965
966 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +0200967 return FAIL; // bail out if loading the script caused an error
968 }
969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100971 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200972 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100973 {
974 int i;
975 typval_T *argv = STACK_TV_BOT(0) - argcount;
976
977 // The function can change at runtime, check that the argument
978 // types are correct.
979 for (i = 0; i < argcount; ++i)
980 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100981 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100982
Bram Moolenaar6ce46b92021-08-07 15:35:36 +0200983 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100984 type = ufunc->uf_arg_types[i];
985 else if (ufunc->uf_va_type != NULL)
986 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100987 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200988 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100989 return FAIL;
990 }
991 }
992
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200993 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995
996 return FAIL;
997}
998
999 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001000call_partial(
1001 typval_T *tv,
1002 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001003 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001005 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001006 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001008 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001009 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010
1011 if (tv->v_type == VAR_PARTIAL)
1012 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001013 partial_T *pt = tv->vval.v_partial;
1014 int i;
1015
1016 if (pt->pt_argc > 0)
1017 {
1018 // Make space for arguments from the partial, shift the "argcount"
1019 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001020 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001021 return FAIL;
1022 for (i = 1; i <= argcount; ++i)
1023 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1024 ectx->ec_stack.ga_len += pt->pt_argc;
1025 argcount += pt->pt_argc;
1026
1027 // copy the arguments from the partial onto the stack
1028 for (i = 0; i < pt->pt_argc; ++i)
1029 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1030 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001031 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032
1033 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001034 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 name = pt->pt_name;
1037 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001038 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001040 if (name != NULL)
1041 {
1042 char_u fname_buf[FLEN_FIXED + 1];
1043 char_u *tofree = NULL;
1044 int error = FCERR_NONE;
1045 char_u *fname;
1046
1047 // May need to translate <SNR>123_ to K_SNR.
1048 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1049 if (error != FCERR_NONE)
1050 res = FAIL;
1051 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001052 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001053 vim_free(tofree);
1054 }
1055
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001056 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 {
1058 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001059 semsg(_(e_unknownfunc),
1060 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 return FAIL;
1062 }
1063 return OK;
1064}
1065
1066/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001067 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1068 * TRUE.
1069 */
1070 static int
1071error_if_locked(int lock, char *error)
1072{
1073 if (lock & (VAR_LOCKED | VAR_FIXED))
1074 {
1075 emsg(_(error));
1076 return TRUE;
1077 }
1078 return FALSE;
1079}
1080
1081/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001082 * Give an error if "tv" is not a number and return FAIL.
1083 */
1084 static int
1085check_for_number(typval_T *tv)
1086{
1087 if (tv->v_type != VAR_NUMBER)
1088 {
1089 semsg(_(e_expected_str_but_got_str),
1090 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1091 return FAIL;
1092 }
1093 return OK;
1094}
1095
1096/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001097 * Store "tv" in variable "name".
1098 * This is for s: and g: variables.
1099 */
1100 static void
1101store_var(char_u *name, typval_T *tv)
1102{
1103 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001104 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001105
Bram Moolenaard877a572021-04-01 19:42:48 +02001106 if (tv->v_lock)
1107 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001108 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001109 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001110 restore_funccal();
1111}
1112
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001113/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001114 * Convert "tv" to a string.
1115 * Return FAIL if not allowed.
1116 */
1117 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001118do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001119{
1120 if (tv->v_type != VAR_STRING)
1121 {
1122 char_u *str;
1123
1124 if (is_2string_any)
1125 {
1126 switch (tv->v_type)
1127 {
1128 case VAR_SPECIAL:
1129 case VAR_BOOL:
1130 case VAR_NUMBER:
1131 case VAR_FLOAT:
1132 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001133
1134 case VAR_LIST:
1135 if (tolerant)
1136 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001137 char_u *s, *e, *p;
1138 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001139
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001140 ga_init2(&ga, sizeof(char_u *), 1);
1141
1142 // Convert to NL separated items, then
1143 // escape the items and replace the NL with
1144 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001145 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001146 if (str == NULL)
1147 return FAIL;
1148 s = str;
1149 while ((e = vim_strchr(s, '\n')) != NULL)
1150 {
1151 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001152 p = vim_strsave_fnameescape(s,
1153 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001154 if (p != NULL)
1155 {
1156 ga_concat(&ga, p);
1157 ga_concat(&ga, (char_u *)" ");
1158 vim_free(p);
1159 }
1160 s = e + 1;
1161 }
1162 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001163 clear_tv(tv);
1164 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001165 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001166 return OK;
1167 }
1168 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001169 default: to_string_error(tv->v_type);
1170 return FAIL;
1171 }
1172 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001173 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001174 clear_tv(tv);
1175 tv->v_type = VAR_STRING;
1176 tv->vval.v_string = str;
1177 }
1178 return OK;
1179}
1180
1181/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001182 * When the value of "sv" is a null list of dict, allocate it.
1183 */
1184 static void
1185allocate_if_null(typval_T *tv)
1186{
1187 switch (tv->v_type)
1188 {
1189 case VAR_LIST:
1190 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001191 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001192 break;
1193 case VAR_DICT:
1194 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001195 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001196 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001197 case VAR_BLOB:
1198 if (tv->vval.v_blob == NULL)
1199 (void)rettv_blob_alloc(tv);
1200 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001201 default:
1202 break;
1203 }
1204}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001205
Bram Moolenaare7525c52021-01-09 13:20:37 +01001206/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001207 * Return the character "str[index]" where "index" is the character index,
1208 * including composing characters.
1209 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001210 */
1211 char_u *
1212char_from_string(char_u *str, varnumber_T index)
1213{
1214 size_t nbyte = 0;
1215 varnumber_T nchar = index;
1216 size_t slen;
1217
1218 if (str == NULL)
1219 return NULL;
1220 slen = STRLEN(str);
1221
Bram Moolenaarff871402021-03-26 13:34:05 +01001222 // Do the same as for a list: a negative index counts from the end.
1223 // Optimization to check the first byte to be below 0x80 (and no composing
1224 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001225 if (index < 0)
1226 {
1227 int clen = 0;
1228
1229 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001230 {
1231 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1232 ++nbyte;
1233 else if (enc_utf8)
1234 nbyte += utfc_ptr2len(str + nbyte);
1235 else
1236 nbyte += mb_ptr2len(str + nbyte);
1237 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001238 nchar = clen + index;
1239 if (nchar < 0)
1240 // unlike list: index out of range results in empty string
1241 return NULL;
1242 }
1243
1244 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001245 {
1246 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1247 ++nbyte;
1248 else if (enc_utf8)
1249 nbyte += utfc_ptr2len(str + nbyte);
1250 else
1251 nbyte += mb_ptr2len(str + nbyte);
1252 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001253 if (nbyte >= slen)
1254 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001255 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001256}
1257
1258/*
1259 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001260 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001261 * If going over the end return "str_len".
1262 * If "idx" is negative count from the end, -1 is the last character.
1263 * When going over the start return -1.
1264 */
1265 static long
1266char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1267{
1268 varnumber_T nchar = idx;
1269 size_t nbyte = 0;
1270
1271 if (nchar >= 0)
1272 {
1273 while (nchar > 0 && nbyte < str_len)
1274 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001275 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001276 --nchar;
1277 }
1278 }
1279 else
1280 {
1281 nbyte = str_len;
1282 while (nchar < 0 && nbyte > 0)
1283 {
1284 --nbyte;
1285 nbyte -= mb_head_off(str, str + nbyte);
1286 ++nchar;
1287 }
1288 if (nchar < 0)
1289 return -1;
1290 }
1291 return (long)nbyte;
1292}
1293
1294/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001295 * Return the slice "str[first : last]" using character indexes. Composing
1296 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001297 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001298 * Return NULL when the result is empty.
1299 */
1300 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001301string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001302{
1303 long start_byte, end_byte;
1304 size_t slen;
1305
1306 if (str == NULL)
1307 return NULL;
1308 slen = STRLEN(str);
1309 start_byte = char_idx2byte(str, slen, first);
1310 if (start_byte < 0)
1311 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001312 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001313 end_byte = (long)slen;
1314 else
1315 {
1316 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001317 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001318 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001319 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001320 }
1321
1322 if (start_byte >= (long)slen || end_byte <= start_byte)
1323 return NULL;
1324 return vim_strnsave(str + start_byte, end_byte - start_byte);
1325}
1326
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001327/*
1328 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1329 * When "dfunc_idx" is negative don't give an error.
1330 * Returns NULL for an error.
1331 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001332 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001333get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001334{
1335 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001336 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1337 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001338 svar_T *sv;
1339
1340 if (sref->sref_seq != si->sn_script_seq)
1341 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001342 // The script was reloaded after the function was compiled, the
1343 // script_idx may not be valid.
1344 if (dfunc != NULL)
1345 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1346 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001347 return NULL;
1348 }
1349 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001350 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001351 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001352 if (dfunc != NULL)
1353 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001354 return NULL;
1355 }
1356 return sv;
1357}
1358
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001359/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001360 * Function passed to do_cmdline() for splitting a script joined by NL
1361 * characters.
1362 */
1363 static char_u *
1364get_split_sourceline(
1365 int c UNUSED,
1366 void *cookie,
1367 int indent UNUSED,
1368 getline_opt_T options UNUSED)
1369{
1370 source_cookie_T *sp = (source_cookie_T *)cookie;
1371 char_u *p;
1372 char_u *line;
1373
1374 if (*sp->nextline == NUL)
1375 return NULL;
1376 p = vim_strchr(sp->nextline, '\n');
1377 if (p == NULL)
1378 {
1379 line = vim_strsave(sp->nextline);
1380 sp->nextline += STRLEN(sp->nextline);
1381 }
1382 else
1383 {
1384 line = vim_strnsave(sp->nextline, p - sp->nextline);
1385 sp->nextline = p + 1;
1386 }
1387 return line;
1388}
1389
1390/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 * Execute a function by "name".
1392 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001393 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 */
1395 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001396call_eval_func(
1397 char_u *name,
1398 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001399 ectx_T *ectx,
1400 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401{
Bram Moolenaared677f52020-08-12 16:38:10 +02001402 int called_emsg_before = called_emsg;
1403 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001405 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001406 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001408 dictitem_T *v;
1409
1410 v = find_var(name, NULL, FALSE);
1411 if (v == NULL)
1412 {
1413 semsg(_(e_unknownfunc), name);
1414 return FAIL;
1415 }
1416 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1417 {
1418 semsg(_(e_unknownfunc), name);
1419 return FAIL;
1420 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001421 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001423 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424}
1425
1426/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001427 * When a function reference is used, fill a partial with the information
1428 * needed, especially when it is used as a closure.
1429 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001430 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001431fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1432{
1433 pt->pt_func = ufunc;
1434 pt->pt_refcount = 1;
1435
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001436 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001437 {
1438 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1439 + ectx->ec_dfunc_idx;
1440
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001441 // The closure may need to find arguments and local variables in the
1442 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001443 pt->pt_outer.out_stack = &ectx->ec_stack;
1444 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001445 if (ectx->ec_outer_ref != NULL)
1446 {
1447 // The current context already has a context, link to that one.
1448 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1449 if (ectx->ec_outer_ref->or_partial != NULL)
1450 {
1451 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1452 ++pt->pt_outer.out_up_partial->pt_refcount;
1453 }
1454 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001455
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001456 // If this function returns and the closure is still being used, we
1457 // need to make a copy of the context (arguments and local variables).
1458 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001459 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001460 {
1461 vim_free(pt);
1462 return FAIL;
1463 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001464 // Extra variable keeps the count of closures created in the current
1465 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001466 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1467 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1468
1469 ((partial_T **)ectx->ec_funcrefs.ga_data)
1470 [ectx->ec_funcrefs.ga_len] = pt;
1471 ++pt->pt_refcount;
1472 ++ectx->ec_funcrefs.ga_len;
1473 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001474 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001475 return OK;
1476}
1477
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001478/*
1479 * Execute iptr->isn_arg.string as an Ex command.
1480 */
1481 static int
1482exec_command(isn_T *iptr)
1483{
1484 source_cookie_T cookie;
1485
1486 SOURCING_LNUM = iptr->isn_lnum;
1487 // Pass getsourceline to get an error for a missing ":end"
1488 // command.
1489 CLEAR_FIELD(cookie);
1490 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1491 if (do_cmdline(iptr->isn_arg.string,
1492 getsourceline, &cookie,
1493 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1494 || did_emsg)
1495 return FAIL;
1496 return OK;
1497}
1498
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001499// used for v_instr of typval of VAR_INSTR
1500struct instr_S {
1501 ectx_T *instr_ectx;
1502 isn_T *instr_instr;
1503};
1504
Bram Moolenaar4c137212021-04-19 16:48:48 +02001505// used for substitute_instr
1506typedef struct subs_expr_S {
1507 ectx_T *subs_ectx;
1508 isn_T *subs_instr;
1509 int subs_status;
1510} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511
1512// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001513#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514
1515// Get pointer to item at the bottom of the stack, -1 is the bottom.
1516#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001517#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 +01001518
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001519// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001520#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 +01001521
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001522// Set when calling do_debug().
1523static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001524static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001525
1526/*
1527 * When debugging lookup "name" and return the typeval.
1528 * When not found return NULL.
1529 */
1530 typval_T *
1531lookup_debug_var(char_u *name)
1532{
1533 int idx;
1534 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001535 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001536 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001537 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001538
1539 if (ectx == NULL)
1540 return NULL;
1541 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1542
1543 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001544 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001545 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001546 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001547 return STACK_TV_VAR(idx);
1548 }
1549
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001550 // Go through argument names.
1551 ufunc = dfunc->df_ufunc;
1552 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1553 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1554 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1555 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1556 - varargs_off + idx);
1557 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1558 return STACK_TV(ectx->ec_frame_idx - 1);
1559
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001560 return NULL;
1561}
1562
Bram Moolenaar26a44842021-09-02 18:49:06 +02001563/*
1564 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1565 * breakpoint was set in that function or when there is any expression.
1566 */
1567 int
1568may_break_in_function(ufunc_T *ufunc)
1569{
1570 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1571}
1572
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001573 static void
1574handle_debug(isn_T *iptr, ectx_T *ectx)
1575{
1576 char_u *line;
1577 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1578 + ectx->ec_dfunc_idx)->df_ufunc;
1579 isn_T *ni;
1580 int end_lnum = iptr->isn_lnum;
1581 garray_T ga;
1582 int lnum;
1583
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001584 if (ex_nesting_level > debug_break_level)
1585 {
1586 linenr_T breakpoint;
1587
Bram Moolenaar26a44842021-09-02 18:49:06 +02001588 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001589 return;
1590
1591 // check for the next breakpoint if needed
1592 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001593 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001594 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1595 return;
1596 }
1597
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001598 SOURCING_LNUM = iptr->isn_lnum;
1599 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001600 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001601
1602 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1603 if (ni->isn_type == ISN_DEBUG
1604 || ni->isn_type == ISN_RETURN
1605 || ni->isn_type == ISN_RETURN_VOID)
1606 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001607 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001608 break;
1609 }
1610
1611 if (end_lnum > iptr->isn_lnum)
1612 {
1613 ga_init2(&ga, sizeof(char_u *), 10);
1614 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001615 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001616 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001617
Bram Moolenaar303215d2021-07-07 20:10:43 +02001618 if (p == NULL)
1619 continue; // left over from continuation line
1620 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001621 if (*p == '#')
1622 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001623 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001624 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1625 if (STRNCMP(p, "def ", 4) == 0)
1626 break;
1627 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001628 line = ga_concat_strings(&ga, " ");
1629 vim_free(ga.ga_data);
1630 }
1631 else
1632 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001633
Dominique Pelle6e969552021-06-17 13:53:41 +02001634 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001635 debug_context = NULL;
1636
1637 if (end_lnum > iptr->isn_lnum)
1638 vim_free(line);
1639}
1640
Bram Moolenaar4c137212021-04-19 16:48:48 +02001641/*
1642 * Execute instructions in execution context "ectx".
1643 * Return OK or FAIL;
1644 */
1645 static int
1646exec_instructions(ectx_T *ectx)
1647{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001648 int ret = FAIL;
1649 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001650 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001651
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001652 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001653 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001654
Bram Moolenaarff652882021-05-16 15:24:49 +02001655 // Only catch exceptions in this instruction list.
1656 ectx->ec_trylevel_at_start = trylevel;
1657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 for (;;)
1659 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001660 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001662 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001663
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001664 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001665 {
1666 line_breakcheck();
1667 breakcheck_count = 0;
1668 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001669 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001670 {
1671 // Turn CTRL-C into an exception.
1672 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001673 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001674 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001675 did_throw = TRUE;
1676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001678 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001679 {
1680 // Turn an error message into an exception.
1681 did_emsg = FALSE;
1682 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001683 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001684 did_throw = TRUE;
1685 *msg_list = NULL;
1686 }
1687
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001688 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001690 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001691 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001692 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693
1694 // An exception jumps to the first catch, finally, or returns from
1695 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001696 while (index > 0)
1697 {
1698 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001699 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001700 break;
1701 // In the catch and finally block of this try we have to go up
1702 // one level.
1703 --index;
1704 trycmd = NULL;
1705 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001706 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001708 if (trycmd->tcd_in_catch)
1709 {
1710 // exception inside ":catch", jump to ":finally" once
1711 ectx->ec_iidx = trycmd->tcd_finally_idx;
1712 trycmd->tcd_finally_idx = 0;
1713 }
1714 else
1715 // jump to first ":catch"
1716 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001717 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001718 did_throw = FALSE; // don't come back here until :endtry
1719 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 }
1721 else
1722 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001723 // Not inside try or need to return from current functions.
1724 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001725 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001726 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001727 tv = STACK_TV_BOT(0);
1728 tv->v_type = VAR_NUMBER;
1729 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001730 ++ectx->ec_stack.ga_len;
1731 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001733 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001734 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001735 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001736 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 goto done;
1738 }
1739
Bram Moolenaar4c137212021-04-19 16:48:48 +02001740 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001741 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 }
1743 continue;
1744 }
1745
Bram Moolenaar4c137212021-04-19 16:48:48 +02001746 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 switch (iptr->isn_type)
1748 {
1749 // execute Ex command line
1750 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001751 if (exec_command(iptr) == FAIL)
1752 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 break;
1754
Bram Moolenaar20677332021-06-06 17:02:53 +02001755 // execute Ex command line split at NL characters.
1756 case ISN_EXEC_SPLIT:
1757 {
1758 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001759 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001760
1761 SOURCING_LNUM = iptr->isn_lnum;
1762 CLEAR_FIELD(cookie);
1763 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1764 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001765 line = get_split_sourceline(0, &cookie, 0, 0);
1766 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001767 get_split_sourceline, &cookie,
1768 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1769 == FAIL
1770 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001771 {
1772 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001773 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001774 }
1775 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001776 }
1777 break;
1778
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001779 // execute Ex command line that is only a range
1780 case ISN_EXECRANGE:
1781 {
1782 exarg_T ea;
1783 char *error = NULL;
1784
1785 CLEAR_FIELD(ea);
1786 ea.cmdidx = CMD_SIZE;
1787 ea.addr_type = ADDR_LINES;
1788 ea.cmd = iptr->isn_arg.string;
1789 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00001790 if (ea.cmd == NULL)
1791 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001792 if (error == NULL)
1793 error = ex_range_without_command(&ea);
1794 if (error != NULL)
1795 {
1796 SOURCING_LNUM = iptr->isn_lnum;
1797 emsg(error);
1798 goto on_error;
1799 }
1800 }
1801 break;
1802
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001803 // Evaluate an expression with legacy syntax, push it onto the
1804 // stack.
1805 case ISN_LEGACY_EVAL:
1806 {
1807 char_u *arg = iptr->isn_arg.string;
1808 int res;
1809 int save_flags = cmdmod.cmod_flags;
1810
Bram Moolenaar35578162021-08-02 19:10:38 +02001811 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001812 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001813 tv = STACK_TV_BOT(0);
1814 init_tv(tv);
1815 cmdmod.cmod_flags |= CMOD_LEGACY;
1816 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1817 cmdmod.cmod_flags = save_flags;
1818 if (res == FAIL)
1819 goto on_error;
1820 ++ectx->ec_stack.ga_len;
1821 }
1822 break;
1823
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001824 // push typeval VAR_INSTR with instructions to be executed
1825 case ISN_INSTR:
1826 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001827 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001828 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001829 tv = STACK_TV_BOT(0);
1830 tv->vval.v_instr = ALLOC_ONE(instr_T);
1831 if (tv->vval.v_instr == NULL)
1832 goto on_error;
1833 ++ectx->ec_stack.ga_len;
1834
1835 tv->v_type = VAR_INSTR;
1836 tv->vval.v_instr->instr_ectx = ectx;
1837 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1838 }
1839 break;
1840
Bram Moolenaar4c137212021-04-19 16:48:48 +02001841 // execute :substitute with an expression
1842 case ISN_SUBSTITUTE:
1843 {
1844 subs_T *subs = &iptr->isn_arg.subs;
1845 source_cookie_T cookie;
1846 struct subs_expr_S *save_instr = substitute_instr;
1847 struct subs_expr_S subs_instr;
1848 int res;
1849
1850 subs_instr.subs_ectx = ectx;
1851 subs_instr.subs_instr = subs->subs_instr;
1852 subs_instr.subs_status = OK;
1853 substitute_instr = &subs_instr;
1854
1855 SOURCING_LNUM = iptr->isn_lnum;
1856 // This is very much like ISN_EXEC
1857 CLEAR_FIELD(cookie);
1858 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1859 res = do_cmdline(subs->subs_cmd,
1860 getsourceline, &cookie,
1861 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1862 substitute_instr = save_instr;
1863
1864 if (res == FAIL || did_emsg
1865 || subs_instr.subs_status == FAIL)
1866 goto on_error;
1867 }
1868 break;
1869
1870 case ISN_FINISH:
1871 goto done;
1872
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001873 case ISN_REDIRSTART:
1874 // create a dummy entry for var_redir_str()
1875 if (alloc_redir_lval() == FAIL)
1876 goto on_error;
1877
1878 // The output is stored in growarray "redir_ga" until
1879 // redirection ends.
1880 init_redir_ga();
1881 redir_vname = 1;
1882 break;
1883
1884 case ISN_REDIREND:
1885 {
1886 char_u *res = get_clear_redir_ga();
1887
1888 // End redirection, put redirected text on the stack.
1889 clear_redir_lval();
1890 redir_vname = 0;
1891
Bram Moolenaar35578162021-08-02 19:10:38 +02001892 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001893 {
1894 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001895 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001896 }
1897 tv = STACK_TV_BOT(0);
1898 tv->v_type = VAR_STRING;
1899 tv->vval.v_string = res;
1900 ++ectx->ec_stack.ga_len;
1901 }
1902 break;
1903
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001904 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001905#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001906 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1907 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001908#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001909 break;
1910
1911 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001912#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001913 {
1914 exarg_T ea;
1915 int res;
1916
1917 CLEAR_FIELD(ea);
1918 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1919 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1920 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1921 --ectx->ec_stack.ga_len;
1922 tv = STACK_TV_BOT(0);
1923 res = cexpr_core(&ea, tv);
1924 clear_tv(tv);
1925 if (res == FAIL)
1926 goto on_error;
1927 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001928#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001929 break;
1930
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001931 // execute Ex command from pieces on the stack
1932 case ISN_EXECCONCAT:
1933 {
1934 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001935 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001936 int pass;
1937 int i;
1938 char_u *cmd = NULL;
1939 char_u *str;
1940
1941 for (pass = 1; pass <= 2; ++pass)
1942 {
1943 for (i = 0; i < count; ++i)
1944 {
1945 tv = STACK_TV_BOT(i - count);
1946 str = tv->vval.v_string;
1947 if (str != NULL && *str != NUL)
1948 {
1949 if (pass == 2)
1950 STRCPY(cmd + len, str);
1951 len += STRLEN(str);
1952 }
1953 if (pass == 2)
1954 clear_tv(tv);
1955 }
1956 if (pass == 1)
1957 {
1958 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001959 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001960 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001961 len = 0;
1962 }
1963 }
1964
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001965 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001966 do_cmdline_cmd(cmd);
1967 vim_free(cmd);
1968 }
1969 break;
1970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 // execute :echo {string} ...
1972 case ISN_ECHO:
1973 {
1974 int count = iptr->isn_arg.echo.echo_count;
1975 int atstart = TRUE;
1976 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001977 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978
1979 for (idx = 0; idx < count; ++idx)
1980 {
1981 tv = STACK_TV_BOT(idx - count);
1982 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1983 &atstart, &needclr);
1984 clear_tv(tv);
1985 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001986 if (needclr)
1987 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001988 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 }
1990 break;
1991
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001992 // :execute {string} ...
1993 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02001994 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001995 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001996 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001997 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02001998 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001999 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002000 {
2001 int count = iptr->isn_arg.number;
2002 garray_T ga;
2003 char_u buf[NUMBUFLEN];
2004 char_u *p;
2005 int len;
2006 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002007 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002008
2009 ga_init2(&ga, 1, 80);
2010 for (idx = 0; idx < count; ++idx)
2011 {
2012 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002013 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002014 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002015 if (tv->v_type == VAR_CHANNEL
2016 || tv->v_type == VAR_JOB)
2017 {
2018 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002019 semsg(_(e_using_invalid_value_as_string_str),
2020 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002021 break;
2022 }
2023 else
2024 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002025 }
2026 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002027 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002028
2029 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002030 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002031 failed = TRUE;
2032 else
2033 {
2034 if (ga.ga_len > 0)
2035 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2036 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2037 ga.ga_len += len;
2038 }
2039 clear_tv(tv);
2040 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002041 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002042 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002043 {
2044 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002045 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002046 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002047
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002048 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002049 {
2050 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002051 {
2052 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002053 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002054 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002055 {
2056 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002057 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002058 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002059 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002060 else
2061 {
2062 msg_sb_eol();
2063 if (iptr->isn_type == ISN_ECHOMSG)
2064 {
2065 msg_attr(ga.ga_data, echo_attr);
2066 out_flush();
2067 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002068 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2069 {
2070 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2071 TRUE);
2072 ui_write((char_u *)"\r\n", 2, TRUE);
2073 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002074 else
2075 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002076 SOURCING_LNUM = iptr->isn_lnum;
2077 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002078 }
2079 }
2080 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002081 ga_clear(&ga);
2082 }
2083 break;
2084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 // load local variable or argument
2086 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002087 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002088 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002090 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 break;
2092
2093 // load v: variable
2094 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002095 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002096 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002098 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 break;
2100
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002101 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002102 case ISN_LOADSCRIPT:
2103 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002104 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 svar_T *sv;
2106
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002107 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002108 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002109 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002110 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002111 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002112 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002114 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 }
2116 break;
2117
2118 // load s: variable in old script
2119 case ISN_LOADS:
2120 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002121 hashtab_T *ht = &SCRIPT_VARS(
2122 iptr->isn_arg.loadstore.ls_sid);
2123 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 if (di == NULL)
2127 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002128 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002129 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002130 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 }
2132 else
2133 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002134 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002135 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002137 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 }
2139 }
2140 break;
2141
Bram Moolenaard3aac292020-04-19 14:32:17 +02002142 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002144 case ISN_LOADB:
2145 case ISN_LOADW:
2146 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002148 dictitem_T *di = NULL;
2149 hashtab_T *ht = NULL;
2150 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002151
Bram Moolenaard3aac292020-04-19 14:32:17 +02002152 switch (iptr->isn_type)
2153 {
2154 case ISN_LOADG:
2155 ht = get_globvar_ht();
2156 namespace = 'g';
2157 break;
2158 case ISN_LOADB:
2159 ht = &curbuf->b_vars->dv_hashtab;
2160 namespace = 'b';
2161 break;
2162 case ISN_LOADW:
2163 ht = &curwin->w_vars->dv_hashtab;
2164 namespace = 'w';
2165 break;
2166 case ISN_LOADT:
2167 ht = &curtab->tp_vars->dv_hashtab;
2168 namespace = 't';
2169 break;
2170 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002171 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002172 }
2173 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 if (di == NULL)
2176 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002177 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002178 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002179 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002180 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 }
2182 else
2183 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002184 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002185 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002187 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 }
2189 }
2190 break;
2191
Bram Moolenaar03290b82020-12-19 16:30:44 +01002192 // load autoload variable
2193 case ISN_LOADAUTO:
2194 {
2195 char_u *name = iptr->isn_arg.string;
2196
Bram Moolenaar35578162021-08-02 19:10:38 +02002197 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002198 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002199 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002200 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002201 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002202 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002203 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002204 }
2205 break;
2206
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002207 // load g:/b:/w:/t: namespace
2208 case ISN_LOADGDICT:
2209 case ISN_LOADBDICT:
2210 case ISN_LOADWDICT:
2211 case ISN_LOADTDICT:
2212 {
2213 dict_T *d = NULL;
2214
2215 switch (iptr->isn_type)
2216 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002217 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2218 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2219 case ISN_LOADWDICT: d = curwin->w_vars; break;
2220 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002221 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002222 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002223 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002224 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002225 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002226 tv = STACK_TV_BOT(0);
2227 tv->v_type = VAR_DICT;
2228 tv->v_lock = 0;
2229 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002230 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002231 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002232 }
2233 break;
2234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 // load &option
2236 case ISN_LOADOPT:
2237 {
2238 typval_T optval;
2239 char_u *name = iptr->isn_arg.string;
2240
Bram Moolenaara8c17702020-04-01 21:17:24 +02002241 // This is not expected to fail, name is checked during
2242 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002243 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002244 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002245 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002246 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002248 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 }
2250 break;
2251
2252 // load $ENV
2253 case ISN_LOADENV:
2254 {
2255 typval_T optval;
2256 char_u *name = iptr->isn_arg.string;
2257
Bram Moolenaar35578162021-08-02 19:10:38 +02002258 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002259 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002260 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002261 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002263 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 }
2265 break;
2266
2267 // load @register
2268 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002269 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002270 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 tv = STACK_TV_BOT(0);
2272 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002273 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002274 // This may result in NULL, which should be equivalent to an
2275 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 tv->vval.v_string = get_reg_contents(
2277 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002278 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 break;
2280
2281 // store local variable
2282 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002283 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 tv = STACK_TV_VAR(iptr->isn_arg.number);
2285 clear_tv(tv);
2286 *tv = *STACK_TV_BOT(0);
2287 break;
2288
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002289 // store s: variable in old script
2290 case ISN_STORES:
2291 {
2292 hashtab_T *ht = &SCRIPT_VARS(
2293 iptr->isn_arg.loadstore.ls_sid);
2294 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002295 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002296
Bram Moolenaar4c137212021-04-19 16:48:48 +02002297 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002298 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002299 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002300 else
2301 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002302 SOURCING_LNUM = iptr->isn_lnum;
2303 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002304 {
2305 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002306 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002307 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002308 clear_tv(&di->di_tv);
2309 di->di_tv = *STACK_TV_BOT(0);
2310 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002311 }
2312 break;
2313
2314 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 case ISN_STORESCRIPT:
2316 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002317 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2318 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002320 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002321 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002322 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002323 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002324
2325 // "const" and "final" are checked at compile time, locking
2326 // the value needs to be checked here.
2327 SOURCING_LNUM = iptr->isn_lnum;
2328 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002329 {
2330 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002331 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002332 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 clear_tv(sv->sv_tv);
2335 *sv->sv_tv = *STACK_TV_BOT(0);
2336 }
2337 break;
2338
2339 // store option
2340 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002341 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002343 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
2344 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 long n = 0;
2346 char_u *s = NULL;
2347 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002348 char_u numbuf[NUMBUFLEN];
2349 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350
Bram Moolenaar4c137212021-04-19 16:48:48 +02002351 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 tv = STACK_TV_BOT(0);
2353 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002354 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002356 if (s == NULL)
2357 s = (char_u *)"";
2358 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002359 else if (iptr->isn_type == ISN_STOREFUNCOPT)
2360 {
2361 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002362 // If the option can be set to a function reference or
2363 // a lambda and the passed value is a function
2364 // reference, then convert it to the name (string) of
2365 // the function reference.
2366 s = tv2string(tv, &tofree, numbuf, 0);
2367 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002368 {
2369 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002370 goto on_error;
2371 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002374 // must be VAR_NUMBER, CHECKTYPE makes sure
2375 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002376 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002377 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002378 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 if (msg != NULL)
2380 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002381 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002383 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 }
2386 break;
2387
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002388 // store $ENV
2389 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002390 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002391 tv = STACK_TV_BOT(0);
2392 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2393 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002394 break;
2395
2396 // store @r
2397 case ISN_STOREREG:
2398 {
2399 int reg = iptr->isn_arg.number;
2400
Bram Moolenaar4c137212021-04-19 16:48:48 +02002401 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002402 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002403 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002404 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002405 }
2406 break;
2407
2408 // store v: variable
2409 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002410 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002411 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2412 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002413 // should not happen, type is checked when compiling
2414 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002415 break;
2416
Bram Moolenaard3aac292020-04-19 14:32:17 +02002417 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002419 case ISN_STOREB:
2420 case ISN_STOREW:
2421 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002423 dictitem_T *di;
2424 hashtab_T *ht;
2425 char_u *name = iptr->isn_arg.string + 2;
2426
Bram Moolenaard3aac292020-04-19 14:32:17 +02002427 switch (iptr->isn_type)
2428 {
2429 case ISN_STOREG:
2430 ht = get_globvar_ht();
2431 break;
2432 case ISN_STOREB:
2433 ht = &curbuf->b_vars->dv_hashtab;
2434 break;
2435 case ISN_STOREW:
2436 ht = &curwin->w_vars->dv_hashtab;
2437 break;
2438 case ISN_STORET:
2439 ht = &curtab->tp_vars->dv_hashtab;
2440 break;
2441 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002442 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444
Bram Moolenaar4c137212021-04-19 16:48:48 +02002445 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002446 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002448 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 else
2450 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002451 SOURCING_LNUM = iptr->isn_lnum;
2452 if (var_check_permission(di, name) == FAIL)
2453 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 clear_tv(&di->di_tv);
2455 di->di_tv = *STACK_TV_BOT(0);
2456 }
2457 }
2458 break;
2459
Bram Moolenaar03290b82020-12-19 16:30:44 +01002460 // store an autoload variable
2461 case ISN_STOREAUTO:
2462 SOURCING_LNUM = iptr->isn_lnum;
2463 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2464 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002465 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002466 break;
2467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 // store number in local variable
2469 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002470 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 clear_tv(tv);
2472 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002473 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 break;
2475
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002476 // store value in list or dict variable
2477 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002478 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002479 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002480 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002481 typval_T *tv_dest = STACK_TV_BOT(-1);
2482 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002483
Bram Moolenaar752fc692021-01-04 21:57:11 +01002484 // Stack contains:
2485 // -3 value to be stored
2486 // -2 index
2487 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002488 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002489 SOURCING_LNUM = iptr->isn_lnum;
2490 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002491 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002492 dest_type = tv_dest->v_type;
2493 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002494 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002495 else if (dest_type == VAR_LIST
2496 && tv_idx->v_type != VAR_NUMBER)
2497 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002498 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002499 status = FAIL;
2500 }
2501 }
2502 else if (dest_type != tv_dest->v_type)
2503 {
2504 // just in case, should be OK
2505 semsg(_(e_expected_str_but_got_str),
2506 vartype_name(dest_type),
2507 vartype_name(tv_dest->v_type));
2508 status = FAIL;
2509 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002510
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002511 if (status == OK && dest_type == VAR_LIST)
2512 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002513 long lidx = (long)tv_idx->vval.v_number;
2514 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002515
2516 if (list == NULL)
2517 {
2518 emsg(_(e_list_not_set));
2519 goto on_error;
2520 }
2521 if (lidx < 0 && list->lv_len + lidx >= 0)
2522 // negative index is relative to the end
2523 lidx = list->lv_len + lidx;
2524 if (lidx < 0 || lidx > list->lv_len)
2525 {
2526 semsg(_(e_listidx), lidx);
2527 goto on_error;
2528 }
2529 if (lidx < list->lv_len)
2530 {
2531 listitem_T *li = list_find(list, lidx);
2532
2533 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002534 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002535 goto on_error;
2536 // overwrite existing list item
2537 clear_tv(&li->li_tv);
2538 li->li_tv = *tv;
2539 }
2540 else
2541 {
2542 if (error_if_locked(list->lv_lock,
2543 e_cannot_change_list))
2544 goto on_error;
2545 // append to list, only fails when out of memory
2546 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002547 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002548 clear_tv(tv);
2549 }
2550 }
2551 else if (status == OK && dest_type == VAR_DICT)
2552 {
2553 char_u *key = tv_idx->vval.v_string;
2554 dict_T *dict = tv_dest->vval.v_dict;
2555 dictitem_T *di;
2556
2557 SOURCING_LNUM = iptr->isn_lnum;
2558 if (dict == NULL)
2559 {
2560 emsg(_(e_dictionary_not_set));
2561 goto on_error;
2562 }
2563 if (key == NULL)
2564 key = (char_u *)"";
2565 di = dict_find(dict, key, -1);
2566 if (di != NULL)
2567 {
2568 if (error_if_locked(di->di_tv.v_lock,
2569 e_cannot_change_dict_item))
2570 goto on_error;
2571 // overwrite existing value
2572 clear_tv(&di->di_tv);
2573 di->di_tv = *tv;
2574 }
2575 else
2576 {
2577 if (error_if_locked(dict->dv_lock,
2578 e_cannot_change_dict))
2579 goto on_error;
2580 // add to dict, only fails when out of memory
2581 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002582 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002583 clear_tv(tv);
2584 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002585 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002586 else if (status == OK && dest_type == VAR_BLOB)
2587 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002588 long lidx = (long)tv_idx->vval.v_number;
2589 blob_T *blob = tv_dest->vval.v_blob;
2590 varnumber_T nr;
2591 int error = FALSE;
2592 int len;
2593
2594 if (blob == NULL)
2595 {
2596 emsg(_(e_blob_not_set));
2597 goto on_error;
2598 }
2599 len = blob_len(blob);
2600 if (lidx < 0 && len + lidx >= 0)
2601 // negative index is relative to the end
2602 lidx = len + lidx;
2603
2604 // Can add one byte at the end.
2605 if (lidx < 0 || lidx > len)
2606 {
2607 semsg(_(e_blobidx), lidx);
2608 goto on_error;
2609 }
2610 if (value_check_lock(blob->bv_lock,
2611 (char_u *)"blob", FALSE))
2612 goto on_error;
2613 nr = tv_get_number_chk(tv, &error);
2614 if (error)
2615 goto on_error;
2616 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002617 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002618 else
2619 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002620 status = FAIL;
2621 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002622 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002623
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002624 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002625 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002626 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002627 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002628 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002629 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002630 goto on_error;
2631 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002632 }
2633 break;
2634
Bram Moolenaar68452172021-04-12 21:21:02 +02002635 // store value in blob range
2636 case ISN_STORERANGE:
2637 {
2638 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2639 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2640 typval_T *tv_dest = STACK_TV_BOT(-1);
2641 int status = OK;
2642
2643 // Stack contains:
2644 // -4 value to be stored
2645 // -3 first index or "none"
2646 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002647 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002648 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002649 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002650 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002651 long n1;
2652 long n2;
2653 int error = FALSE;
2654
2655 SOURCING_LNUM = iptr->isn_lnum;
2656 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2657 if (error)
2658 status = FAIL;
2659 else
2660 {
2661 if (tv_idx2->v_type == VAR_SPECIAL
2662 && tv_idx2->vval.v_number == VVAL_NONE)
2663 n2 = list_len(tv_dest->vval.v_list) - 1;
2664 else
2665 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2666 if (error)
2667 status = FAIL;
2668 else
2669 {
2670 listitem_T *li1 = check_range_index_one(
2671 tv_dest->vval.v_list, &n1, FALSE);
2672
2673 if (li1 == NULL)
2674 status = FAIL;
2675 else
2676 {
2677 status = check_range_index_two(
2678 tv_dest->vval.v_list,
2679 &n1, li1, &n2, FALSE);
2680 if (status != FAIL)
2681 status = list_assign_range(
2682 tv_dest->vval.v_list,
2683 tv->vval.v_list,
2684 n1,
2685 n2,
2686 tv_idx2->v_type == VAR_SPECIAL,
2687 (char_u *)"=",
2688 (char_u *)"[unknown]");
2689 }
2690 }
2691 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002692 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002693 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002694 {
2695 varnumber_T n1;
2696 varnumber_T n2;
2697 int error = FALSE;
2698
2699 n1 = tv_get_number_chk(tv_idx1, &error);
2700 if (error)
2701 status = FAIL;
2702 else
2703 {
2704 if (tv_idx2->v_type == VAR_SPECIAL
2705 && tv_idx2->vval.v_number == VVAL_NONE)
2706 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2707 else
2708 n2 = tv_get_number_chk(tv_idx2, &error);
2709 if (error)
2710 status = FAIL;
2711 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002712 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002713 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002714
2715 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002716 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002717 || check_blob_range(bloblen,
2718 n1, n2, FALSE) == FAIL)
2719 status = FAIL;
2720 else
2721 status = blob_set_range(
2722 tv_dest->vval.v_blob, n1, n2, tv);
2723 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002724 }
2725 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002726 else
2727 {
2728 status = FAIL;
2729 emsg(_(e_blob_required));
2730 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002731
2732 clear_tv(tv_idx1);
2733 clear_tv(tv_idx2);
2734 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002735 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002736 clear_tv(tv);
2737
2738 if (status == FAIL)
2739 goto on_error;
2740 }
2741 break;
2742
Bram Moolenaar0186e582021-01-10 18:33:11 +01002743 // load or store variable or argument from outer scope
2744 case ISN_LOADOUTER:
2745 case ISN_STOREOUTER:
2746 {
2747 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002748 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2749 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002750
2751 while (depth > 1 && outer != NULL)
2752 {
2753 outer = outer->out_up;
2754 --depth;
2755 }
2756 if (outer == NULL)
2757 {
2758 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00002759 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
2760 || ectx->ec_outer_ref == NULL)
2761 // Possibly :def function called from legacy
2762 // context.
2763 emsg(_(e_closure_called_from_invalid_context));
2764 else
2765 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002766 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002767 }
2768 tv = ((typval_T *)outer->out_stack->ga_data)
2769 + outer->out_frame_idx + STACK_FRAME_SIZE
2770 + iptr->isn_arg.outer.outer_idx;
2771 if (iptr->isn_type == ISN_LOADOUTER)
2772 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002773 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002774 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002775 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002776 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002777 }
2778 else
2779 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002780 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002781 clear_tv(tv);
2782 *tv = *STACK_TV_BOT(0);
2783 }
2784 }
2785 break;
2786
Bram Moolenaar752fc692021-01-04 21:57:11 +01002787 // unlet item in list or dict variable
2788 case ISN_UNLETINDEX:
2789 {
2790 typval_T *tv_idx = STACK_TV_BOT(-2);
2791 typval_T *tv_dest = STACK_TV_BOT(-1);
2792 int status = OK;
2793
2794 // Stack contains:
2795 // -2 index
2796 // -1 dict or list
2797 if (tv_dest->v_type == VAR_DICT)
2798 {
2799 // unlet a dict item, index must be a string
2800 if (tv_idx->v_type != VAR_STRING)
2801 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002802 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002803 semsg(_(e_expected_str_but_got_str),
2804 vartype_name(VAR_STRING),
2805 vartype_name(tv_idx->v_type));
2806 status = FAIL;
2807 }
2808 else
2809 {
2810 dict_T *d = tv_dest->vval.v_dict;
2811 char_u *key = tv_idx->vval.v_string;
2812 dictitem_T *di = NULL;
2813
2814 if (key == NULL)
2815 key = (char_u *)"";
2816 if (d != NULL)
2817 di = dict_find(d, key, (int)STRLEN(key));
2818 if (di == NULL)
2819 {
2820 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002821 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002822 semsg(_(e_dictkey), key);
2823 status = FAIL;
2824 }
2825 else
2826 {
2827 // TODO: check for dict or item locked
2828 dictitem_remove(d, di);
2829 }
2830 }
2831 }
2832 else if (tv_dest->v_type == VAR_LIST)
2833 {
2834 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002835 SOURCING_LNUM = iptr->isn_lnum;
2836 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002837 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002838 status = FAIL;
2839 }
2840 else
2841 {
2842 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002843 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002844 listitem_T *li = NULL;
2845
2846 li = list_find(l, n);
2847 if (li == NULL)
2848 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002849 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002850 semsg(_(e_listidx), n);
2851 status = FAIL;
2852 }
2853 else
2854 // TODO: check for list or item locked
2855 listitem_remove(l, li);
2856 }
2857 }
2858 else
2859 {
2860 status = FAIL;
2861 semsg(_(e_cannot_index_str),
2862 vartype_name(tv_dest->v_type));
2863 }
2864
2865 clear_tv(tv_idx);
2866 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002867 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002868 if (status == FAIL)
2869 goto on_error;
2870 }
2871 break;
2872
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002873 // unlet range of items in list variable
2874 case ISN_UNLETRANGE:
2875 {
2876 // Stack contains:
2877 // -3 index1
2878 // -2 index2
2879 // -1 dict or list
2880 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2881 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2882 typval_T *tv_dest = STACK_TV_BOT(-1);
2883 int status = OK;
2884
2885 if (tv_dest->v_type == VAR_LIST)
2886 {
2887 // indexes must be a number
2888 SOURCING_LNUM = iptr->isn_lnum;
2889 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002890 || (tv_idx2->v_type != VAR_SPECIAL
2891 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002892 {
2893 status = FAIL;
2894 }
2895 else
2896 {
2897 list_T *l = tv_dest->vval.v_list;
2898 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002899 long n2 = tv_idx2->v_type == VAR_SPECIAL
2900 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002901 listitem_T *li;
2902
2903 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002904 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002905 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002906 else
2907 {
2908 if (n1 < 0)
2909 n1 = list_idx_of_item(l, li);
2910 if (n2 < 0)
2911 {
2912 listitem_T *li2 = list_find(l, n2);
2913
2914 if (li2 == NULL)
2915 status = FAIL;
2916 else
2917 n2 = list_idx_of_item(l, li2);
2918 }
2919 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002920 && tv_idx2->v_type != VAR_SPECIAL
2921 && n2 < n1)
2922 {
2923 semsg(_(e_listidx), n2);
2924 status = FAIL;
2925 }
2926 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002927 && list_unlet_range(l, li, NULL, n1,
2928 tv_idx2->v_type != VAR_SPECIAL, n2)
2929 == FAIL)
2930 status = FAIL;
2931 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002932 }
2933 }
2934 else
2935 {
2936 status = FAIL;
2937 SOURCING_LNUM = iptr->isn_lnum;
2938 semsg(_(e_cannot_index_str),
2939 vartype_name(tv_dest->v_type));
2940 }
2941
2942 clear_tv(tv_idx1);
2943 clear_tv(tv_idx2);
2944 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002945 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002946 if (status == FAIL)
2947 goto on_error;
2948 }
2949 break;
2950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 // push constant
2952 case ISN_PUSHNR:
2953 case ISN_PUSHBOOL:
2954 case ISN_PUSHSPEC:
2955 case ISN_PUSHF:
2956 case ISN_PUSHS:
2957 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002958 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002959 case ISN_PUSHCHANNEL:
2960 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02002961 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002962 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002964 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 switch (iptr->isn_type)
2967 {
2968 case ISN_PUSHNR:
2969 tv->v_type = VAR_NUMBER;
2970 tv->vval.v_number = iptr->isn_arg.number;
2971 break;
2972 case ISN_PUSHBOOL:
2973 tv->v_type = VAR_BOOL;
2974 tv->vval.v_number = iptr->isn_arg.number;
2975 break;
2976 case ISN_PUSHSPEC:
2977 tv->v_type = VAR_SPECIAL;
2978 tv->vval.v_number = iptr->isn_arg.number;
2979 break;
2980#ifdef FEAT_FLOAT
2981 case ISN_PUSHF:
2982 tv->v_type = VAR_FLOAT;
2983 tv->vval.v_float = iptr->isn_arg.fnumber;
2984 break;
2985#endif
2986 case ISN_PUSHBLOB:
2987 blob_copy(iptr->isn_arg.blob, tv);
2988 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002989 case ISN_PUSHFUNC:
2990 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002991 if (iptr->isn_arg.string == NULL)
2992 tv->vval.v_string = NULL;
2993 else
2994 tv->vval.v_string =
2995 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002996 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002997 case ISN_PUSHCHANNEL:
2998#ifdef FEAT_JOB_CHANNEL
2999 tv->v_type = VAR_CHANNEL;
3000 tv->vval.v_channel = iptr->isn_arg.channel;
3001 if (tv->vval.v_channel != NULL)
3002 ++tv->vval.v_channel->ch_refcount;
3003#endif
3004 break;
3005 case ISN_PUSHJOB:
3006#ifdef FEAT_JOB_CHANNEL
3007 tv->v_type = VAR_JOB;
3008 tv->vval.v_job = iptr->isn_arg.job;
3009 if (tv->vval.v_job != NULL)
3010 ++tv->vval.v_job->jv_refcount;
3011#endif
3012 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 default:
3014 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003015 tv->vval.v_string = vim_strsave(
3016 iptr->isn_arg.string == NULL
3017 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 }
3019 break;
3020
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003021 case ISN_UNLET:
3022 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3023 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003024 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003025 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003026 case ISN_UNLETENV:
3027 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3028 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003029
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003030 case ISN_LOCKUNLOCK:
3031 {
3032 typval_T *lval_root_save = lval_root;
3033 int res;
3034
3035 // Stack has the local variable, argument the whole :lock
3036 // or :unlock command, like ISN_EXEC.
3037 --ectx->ec_stack.ga_len;
3038 lval_root = STACK_TV_BOT(0);
3039 res = exec_command(iptr);
3040 clear_tv(lval_root);
3041 lval_root = lval_root_save;
3042 if (res == FAIL)
3043 goto on_error;
3044 }
3045 break;
3046
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003047 case ISN_LOCKCONST:
3048 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3049 break;
3050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 // create a list from items on the stack; uses a single allocation
3052 // for the list header and the items
3053 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003054 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003055 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 break;
3057
3058 // create a dict from items on the stack
3059 case ISN_NEWDICT:
3060 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003061 int count = iptr->isn_arg.number;
3062 dict_T *dict = dict_alloc();
3063 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003064 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003065 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003067 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003068 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 for (idx = 0; idx < count; ++idx)
3070 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003071 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003073 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003074 key = tv->vval.v_string == NULL
3075 ? (char_u *)"" : tv->vval.v_string;
3076 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003077 if (item != NULL)
3078 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003079 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003080 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003081 dict_unref(dict);
3082 goto on_error;
3083 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003084 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003086 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003087 {
3088 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003089 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003090 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3092 item->di_tv.v_lock = 0;
3093 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003094 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003095 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003096 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003097 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 }
3100
3101 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003102 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003103 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003104 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003106 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 tv = STACK_TV_BOT(-1);
3108 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003109 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 tv->vval.v_dict = dict;
3111 ++dict->dv_refcount;
3112 }
3113 break;
3114
3115 // call a :def function
3116 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003117 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003118 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3119 NULL,
3120 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003121 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003122 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 break;
3124
3125 // call a builtin function
3126 case ISN_BCALL:
3127 SOURCING_LNUM = iptr->isn_lnum;
3128 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3129 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003131 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 break;
3133
3134 // call a funcref or partial
3135 case ISN_PCALL:
3136 {
3137 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3138 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003139 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140
3141 SOURCING_LNUM = iptr->isn_lnum;
3142 if (pfunc->cpf_top)
3143 {
3144 // funcref is above the arguments
3145 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3146 }
3147 else
3148 {
3149 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003150 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003151 partial_tv = *STACK_TV_BOT(0);
3152 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003154 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003155 if (tv == &partial_tv)
3156 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003158 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 }
3160 break;
3161
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003162 case ISN_PCALL_END:
3163 // PCALL finished, arguments have been consumed and replaced by
3164 // the return value. Now clear the funcref from the stack,
3165 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003166 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003167 clear_tv(STACK_TV_BOT(-1));
3168 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3169 break;
3170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 // call a user defined function or funcref/partial
3172 case ISN_UCALL:
3173 {
3174 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3175
3176 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003177 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003178 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003179 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 }
3181 break;
3182
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003183 // return from a :def function call without a value
3184 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003185 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003186 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003187 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003188 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003189 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003190 tv->vval.v_number = 0;
3191 tv->v_lock = 0;
3192 // FALLTHROUGH
3193
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003194 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 case ISN_RETURN:
3196 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003197 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003198 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003199
3200 if (trystack->ga_len > 0)
3201 trycmd = ((trycmd_T *)trystack->ga_data)
3202 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003203 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003204 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003206 // jump to ":finally" or ":endtry"
3207 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003208 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003209 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003210 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 trycmd->tcd_return = TRUE;
3212 }
3213 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003214 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 }
3216 break;
3217
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003218 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 case ISN_FUNCREF:
3220 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003221 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003222 ufunc_T *ufunc;
3223 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003226 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003227 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003228 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003229 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003230 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003231 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003232 if (funcref->fr_func_name == NULL)
3233 {
3234 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3235 + funcref->fr_dfunc_idx;
3236
3237 ufunc = pt_dfunc->df_ufunc;
3238 }
3239 else
3240 {
3241 ufunc = find_func(funcref->fr_func_name, FALSE, NULL);
3242 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003243 if (ufunc == NULL)
3244 {
3245 SOURCING_LNUM = iptr->isn_lnum;
3246 emsg(_(e_function_reference_invalid));
3247 goto theend;
3248 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003249 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003250 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003252 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 tv->vval.v_partial = pt;
3254 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003255 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 }
3257 break;
3258
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003259 // Create a global function from a lambda.
3260 case ISN_NEWFUNC:
3261 {
3262 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3263
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003264 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003265 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003266 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003267 }
3268 break;
3269
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003270 // List functions
3271 case ISN_DEF:
3272 if (iptr->isn_arg.string == NULL)
3273 list_functions(NULL);
3274 else
3275 {
3276 exarg_T ea;
3277
3278 CLEAR_FIELD(ea);
3279 ea.cmd = ea.arg = iptr->isn_arg.string;
3280 define_function(&ea, NULL);
3281 }
3282 break;
3283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 // jump if a condition is met
3285 case ISN_JUMP:
3286 {
3287 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003288 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 int jump = TRUE;
3290
3291 if (when != JUMP_ALWAYS)
3292 {
3293 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003294 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003295 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003296 || when == JUMP_IF_COND_TRUE)
3297 {
3298 SOURCING_LNUM = iptr->isn_lnum;
3299 jump = tv_get_bool_chk(tv, &error);
3300 if (error)
3301 goto on_error;
3302 }
3303 else
3304 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003306 || when == JUMP_AND_KEEP_IF_FALSE
3307 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003309 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 {
3311 // drop the value from the stack
3312 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003313 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 }
3315 }
3316 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003317 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 }
3319 break;
3320
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003321 // Jump if an argument with a default value was already set and not
3322 // v:none.
3323 case ISN_JUMP_IF_ARG_SET:
3324 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3325 if (tv->v_type != VAR_UNKNOWN
3326 && !(tv->v_type == VAR_SPECIAL
3327 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003328 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003329 break;
3330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 // top of a for loop
3332 case ISN_FOR:
3333 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003334 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 typval_T *idxtv =
3336 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3337
Bram Moolenaar35578162021-08-02 19:10:38 +02003338 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003339 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003340 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003341 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003342 list_T *list = ltv->vval.v_list;
3343
3344 // push the next item from the list
3345 ++idxtv->vval.v_number;
3346 if (list == NULL
3347 || idxtv->vval.v_number >= list->lv_len)
3348 {
3349 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003350 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3351 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003352 }
3353 else if (list->lv_first == &range_list_item)
3354 {
3355 // non-materialized range() list
3356 tv = STACK_TV_BOT(0);
3357 tv->v_type = VAR_NUMBER;
3358 tv->v_lock = 0;
3359 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003361 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003362 }
3363 else
3364 {
3365 listitem_T *li = list_find(list,
3366 idxtv->vval.v_number);
3367
3368 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003369 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003370 }
3371 }
3372 else if (ltv->v_type == VAR_STRING)
3373 {
3374 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003375
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003376 // The index is for the last byte of the previous
3377 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003378 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003379 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003380 {
3381 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003382 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3383 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003384 }
3385 else
3386 {
3387 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3388
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003389 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003390 tv = STACK_TV_BOT(0);
3391 tv->v_type = VAR_STRING;
3392 tv->vval.v_string = vim_strnsave(
3393 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003394 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003395 idxtv->vval.v_number += clen - 1;
3396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003398 else if (ltv->v_type == VAR_BLOB)
3399 {
3400 blob_T *blob = ltv->vval.v_blob;
3401
3402 // When we get here the first time make a copy of the
3403 // blob, so that the iteration still works when it is
3404 // changed.
3405 if (idxtv->vval.v_number == -1 && blob != NULL)
3406 {
3407 blob_copy(blob, ltv);
3408 blob_unref(blob);
3409 blob = ltv->vval.v_blob;
3410 }
3411
3412 // The index is for the previous byte.
3413 ++idxtv->vval.v_number;
3414 if (blob == NULL
3415 || idxtv->vval.v_number >= blob_len(blob))
3416 {
3417 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003418 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3419 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003420 }
3421 else
3422 {
3423 // Push the next byte from the blob.
3424 tv = STACK_TV_BOT(0);
3425 tv->v_type = VAR_NUMBER;
3426 tv->vval.v_number = blob_get(blob,
3427 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003428 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003429 }
3430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 else
3432 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003433 semsg(_(e_for_loop_on_str_not_supported),
3434 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003435 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 }
3437 }
3438 break;
3439
3440 // start of ":try" block
3441 case ISN_TRY:
3442 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003443 trycmd_T *trycmd = NULL;
3444
Bram Moolenaar35578162021-08-02 19:10:38 +02003445 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003446 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003447 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3448 + ectx->ec_trystack.ga_len;
3449 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003451 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003452 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3453 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003454 trycmd->tcd_catch_idx =
3455 iptr->isn_arg.try.try_ref->try_catch;
3456 trycmd->tcd_finally_idx =
3457 iptr->isn_arg.try.try_ref->try_finally;
3458 trycmd->tcd_endtry_idx =
3459 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 }
3461 break;
3462
3463 case ISN_PUSHEXC:
3464 if (current_exception == NULL)
3465 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003466 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003468 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003470 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003471 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003473 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003475 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 tv->vval.v_string = vim_strsave(
3477 (char_u *)current_exception->value);
3478 break;
3479
3480 case ISN_CATCH:
3481 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003482 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483
Bram Moolenaar4c137212021-04-19 16:48:48 +02003484 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 if (trystack->ga_len > 0)
3486 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003487 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 + trystack->ga_len - 1;
3489 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003490 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 }
3492 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003493 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 catch_exception(current_exception);
3495 }
3496 break;
3497
Bram Moolenaarc150c092021-02-13 15:02:46 +01003498 case ISN_TRYCONT:
3499 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003500 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003501 trycont_T *trycont = &iptr->isn_arg.trycont;
3502 int i;
3503 trycmd_T *trycmd;
3504 int iidx = trycont->tct_where;
3505
3506 if (trystack->ga_len < trycont->tct_levels)
3507 {
3508 siemsg("TRYCONT: expected %d levels, found %d",
3509 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003510 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003511 }
3512 // Make :endtry jump to any outer try block and the last
3513 // :endtry inside the loop to the loop start.
3514 for (i = trycont->tct_levels; i > 0; --i)
3515 {
3516 trycmd = ((trycmd_T *)trystack->ga_data)
3517 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003518 // Add one to tcd_cont to be able to jump to
3519 // instruction with index zero.
3520 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003521 iidx = trycmd->tcd_finally_idx == 0
3522 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003523 }
3524 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003525 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003526 }
3527 break;
3528
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003529 case ISN_FINALLY:
3530 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003531 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003532 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3533 + trystack->ga_len - 1;
3534
3535 // Reset the index to avoid a return statement jumps here
3536 // again.
3537 trycmd->tcd_finally_idx = 0;
3538 break;
3539 }
3540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 // end of ":try" block
3542 case ISN_ENDTRY:
3543 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003544 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545
3546 if (trystack->ga_len > 0)
3547 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003548 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 --trystack->ga_len;
3551 --trylevel;
3552 trycmd = ((trycmd_T *)trystack->ga_data)
3553 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003554 if (trycmd->tcd_did_throw)
3555 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003556 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 {
3558 // discard the exception
3559 if (caught_stack == current_exception)
3560 caught_stack = caught_stack->caught;
3561 discard_current_exception();
3562 }
3563
3564 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003565 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003566
Bram Moolenaar4c137212021-04-19 16:48:48 +02003567 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003568 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003569 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003570 clear_tv(STACK_TV_BOT(0));
3571 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003572 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003573 // handling :continue: jump to outer try block or
3574 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003575 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 }
3577 }
3578 break;
3579
3580 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003581 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003582 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003583
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003584 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3585 {
3586 // throwing an exception while using "silent!" causes
3587 // the function to abort but not display an error.
3588 tv = STACK_TV_BOT(-1);
3589 clear_tv(tv);
3590 tv->v_type = VAR_NUMBER;
3591 tv->vval.v_number = 0;
3592 goto done;
3593 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003594 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003595 tv = STACK_TV_BOT(0);
3596 if (tv->vval.v_string == NULL
3597 || *skipwhite(tv->vval.v_string) == NUL)
3598 {
3599 vim_free(tv->vval.v_string);
3600 SOURCING_LNUM = iptr->isn_lnum;
3601 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003602 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003603 }
3604
3605 // Inside a "catch" we need to first discard the caught
3606 // exception.
3607 if (trystack->ga_len > 0)
3608 {
3609 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3610 + trystack->ga_len - 1;
3611 if (trycmd->tcd_caught && current_exception != NULL)
3612 {
3613 // discard the exception
3614 if (caught_stack == current_exception)
3615 caught_stack = caught_stack->caught;
3616 discard_current_exception();
3617 trycmd->tcd_caught = FALSE;
3618 }
3619 }
3620
3621 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3622 == FAIL)
3623 {
3624 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003625 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003626 }
3627 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 break;
3630
3631 // compare with special values
3632 case ISN_COMPAREBOOL:
3633 case ISN_COMPARESPECIAL:
3634 {
3635 typval_T *tv1 = STACK_TV_BOT(-2);
3636 typval_T *tv2 = STACK_TV_BOT(-1);
3637 varnumber_T arg1 = tv1->vval.v_number;
3638 varnumber_T arg2 = tv2->vval.v_number;
3639 int res;
3640
3641 switch (iptr->isn_arg.op.op_type)
3642 {
3643 case EXPR_EQUAL: res = arg1 == arg2; break;
3644 case EXPR_NEQUAL: res = arg1 != arg2; break;
3645 default: res = 0; break;
3646 }
3647
Bram Moolenaar4c137212021-04-19 16:48:48 +02003648 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 tv1->v_type = VAR_BOOL;
3650 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3651 }
3652 break;
3653
3654 // Operation with two number arguments
3655 case ISN_OPNR:
3656 case ISN_COMPARENR:
3657 {
3658 typval_T *tv1 = STACK_TV_BOT(-2);
3659 typval_T *tv2 = STACK_TV_BOT(-1);
3660 varnumber_T arg1 = tv1->vval.v_number;
3661 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003662 varnumber_T res = 0;
3663 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664
3665 switch (iptr->isn_arg.op.op_type)
3666 {
3667 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003668 case EXPR_DIV: if (arg2 == 0)
3669 div_zero = TRUE;
3670 else
3671 res = arg1 / arg2;
3672 break;
3673 case EXPR_REM: if (arg2 == 0)
3674 div_zero = TRUE;
3675 else
3676 res = arg1 % arg2;
3677 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 case EXPR_SUB: res = arg1 - arg2; break;
3679 case EXPR_ADD: res = arg1 + arg2; break;
3680
3681 case EXPR_EQUAL: res = arg1 == arg2; break;
3682 case EXPR_NEQUAL: res = arg1 != arg2; break;
3683 case EXPR_GREATER: res = arg1 > arg2; break;
3684 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3685 case EXPR_SMALLER: res = arg1 < arg2; break;
3686 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003687 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 }
3689
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 if (iptr->isn_type == ISN_COMPARENR)
3692 {
3693 tv1->v_type = VAR_BOOL;
3694 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3695 }
3696 else
3697 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003698 if (div_zero)
3699 {
3700 SOURCING_LNUM = iptr->isn_lnum;
3701 emsg(_(e_divide_by_zero));
3702 goto on_error;
3703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 }
3705 break;
3706
3707 // Computation with two float arguments
3708 case ISN_OPFLOAT:
3709 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003710#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 {
3712 typval_T *tv1 = STACK_TV_BOT(-2);
3713 typval_T *tv2 = STACK_TV_BOT(-1);
3714 float_T arg1 = tv1->vval.v_float;
3715 float_T arg2 = tv2->vval.v_float;
3716 float_T res = 0;
3717 int cmp = FALSE;
3718
3719 switch (iptr->isn_arg.op.op_type)
3720 {
3721 case EXPR_MULT: res = arg1 * arg2; break;
3722 case EXPR_DIV: res = arg1 / arg2; break;
3723 case EXPR_SUB: res = arg1 - arg2; break;
3724 case EXPR_ADD: res = arg1 + arg2; break;
3725
3726 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3727 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3728 case EXPR_GREATER: cmp = arg1 > arg2; break;
3729 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3730 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3731 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3732 default: cmp = 0; break;
3733 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003734 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 if (iptr->isn_type == ISN_COMPAREFLOAT)
3736 {
3737 tv1->v_type = VAR_BOOL;
3738 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3739 }
3740 else
3741 tv1->vval.v_float = res;
3742 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003743#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 break;
3745
3746 case ISN_COMPARELIST:
3747 {
3748 typval_T *tv1 = STACK_TV_BOT(-2);
3749 typval_T *tv2 = STACK_TV_BOT(-1);
3750 list_T *arg1 = tv1->vval.v_list;
3751 list_T *arg2 = tv2->vval.v_list;
3752 int cmp = FALSE;
3753 int ic = iptr->isn_arg.op.op_ic;
3754
3755 switch (iptr->isn_arg.op.op_type)
3756 {
3757 case EXPR_EQUAL: cmp =
3758 list_equal(arg1, arg2, ic, FALSE); break;
3759 case EXPR_NEQUAL: cmp =
3760 !list_equal(arg1, arg2, ic, FALSE); break;
3761 case EXPR_IS: cmp = arg1 == arg2; break;
3762 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3763 default: cmp = 0; break;
3764 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003765 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 clear_tv(tv1);
3767 clear_tv(tv2);
3768 tv1->v_type = VAR_BOOL;
3769 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3770 }
3771 break;
3772
3773 case ISN_COMPAREBLOB:
3774 {
3775 typval_T *tv1 = STACK_TV_BOT(-2);
3776 typval_T *tv2 = STACK_TV_BOT(-1);
3777 blob_T *arg1 = tv1->vval.v_blob;
3778 blob_T *arg2 = tv2->vval.v_blob;
3779 int cmp = FALSE;
3780
3781 switch (iptr->isn_arg.op.op_type)
3782 {
3783 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3784 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3785 case EXPR_IS: cmp = arg1 == arg2; break;
3786 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3787 default: cmp = 0; break;
3788 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003789 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 clear_tv(tv1);
3791 clear_tv(tv2);
3792 tv1->v_type = VAR_BOOL;
3793 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3794 }
3795 break;
3796
3797 // TODO: handle separately
3798 case ISN_COMPARESTRING:
3799 case ISN_COMPAREDICT:
3800 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 case ISN_COMPAREANY:
3802 {
3803 typval_T *tv1 = STACK_TV_BOT(-2);
3804 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003805 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 int ic = iptr->isn_arg.op.op_ic;
3807
Bram Moolenaareb26f432020-09-14 16:50:05 +02003808 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003809 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003811 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 }
3813 break;
3814
3815 case ISN_ADDLIST:
3816 case ISN_ADDBLOB:
3817 {
3818 typval_T *tv1 = STACK_TV_BOT(-2);
3819 typval_T *tv2 = STACK_TV_BOT(-1);
3820
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003821 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003823 {
3824 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3825 && tv1->vval.v_list != NULL)
3826 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3827 NULL);
3828 else
3829 eval_addlist(tv1, tv2);
3830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 else
3832 eval_addblob(tv1, tv2);
3833 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003834 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 }
3836 break;
3837
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003838 case ISN_LISTAPPEND:
3839 {
3840 typval_T *tv1 = STACK_TV_BOT(-2);
3841 typval_T *tv2 = STACK_TV_BOT(-1);
3842 list_T *l = tv1->vval.v_list;
3843
3844 // add an item to a list
3845 if (l == NULL)
3846 {
3847 SOURCING_LNUM = iptr->isn_lnum;
3848 emsg(_(e_cannot_add_to_null_list));
3849 goto on_error;
3850 }
3851 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003852 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003853 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003854 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003855 }
3856 break;
3857
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003858 case ISN_BLOBAPPEND:
3859 {
3860 typval_T *tv1 = STACK_TV_BOT(-2);
3861 typval_T *tv2 = STACK_TV_BOT(-1);
3862 blob_T *b = tv1->vval.v_blob;
3863 int error = FALSE;
3864 varnumber_T n;
3865
3866 // add a number to a blob
3867 if (b == NULL)
3868 {
3869 SOURCING_LNUM = iptr->isn_lnum;
3870 emsg(_(e_cannot_add_to_null_blob));
3871 goto on_error;
3872 }
3873 n = tv_get_number_chk(tv2, &error);
3874 if (error)
3875 goto on_error;
3876 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003877 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003878 }
3879 break;
3880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 // Computation with two arguments of unknown type
3882 case ISN_OPANY:
3883 {
3884 typval_T *tv1 = STACK_TV_BOT(-2);
3885 typval_T *tv2 = STACK_TV_BOT(-1);
3886 varnumber_T n1, n2;
3887#ifdef FEAT_FLOAT
3888 float_T f1 = 0, f2 = 0;
3889#endif
3890 int error = FALSE;
3891
3892 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3893 {
3894 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3895 {
3896 eval_addlist(tv1, tv2);
3897 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003898 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 break;
3900 }
3901 else if (tv1->v_type == VAR_BLOB
3902 && tv2->v_type == VAR_BLOB)
3903 {
3904 eval_addblob(tv1, tv2);
3905 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003906 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 break;
3908 }
3909 }
3910#ifdef FEAT_FLOAT
3911 if (tv1->v_type == VAR_FLOAT)
3912 {
3913 f1 = tv1->vval.v_float;
3914 n1 = 0;
3915 }
3916 else
3917#endif
3918 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003919 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 n1 = tv_get_number_chk(tv1, &error);
3921 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003922 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923#ifdef FEAT_FLOAT
3924 if (tv2->v_type == VAR_FLOAT)
3925 f1 = n1;
3926#endif
3927 }
3928#ifdef FEAT_FLOAT
3929 if (tv2->v_type == VAR_FLOAT)
3930 {
3931 f2 = tv2->vval.v_float;
3932 n2 = 0;
3933 }
3934 else
3935#endif
3936 {
3937 n2 = tv_get_number_chk(tv2, &error);
3938 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003939 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940#ifdef FEAT_FLOAT
3941 if (tv1->v_type == VAR_FLOAT)
3942 f2 = n2;
3943#endif
3944 }
3945#ifdef FEAT_FLOAT
3946 // if there is a float on either side the result is a float
3947 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3948 {
3949 switch (iptr->isn_arg.op.op_type)
3950 {
3951 case EXPR_MULT: f1 = f1 * f2; break;
3952 case EXPR_DIV: f1 = f1 / f2; break;
3953 case EXPR_SUB: f1 = f1 - f2; break;
3954 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003955 default: SOURCING_LNUM = iptr->isn_lnum;
3956 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003957 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 }
3959 clear_tv(tv1);
3960 clear_tv(tv2);
3961 tv1->v_type = VAR_FLOAT;
3962 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003963 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 }
3965 else
3966#endif
3967 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003968 int failed = FALSE;
3969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 switch (iptr->isn_arg.op.op_type)
3971 {
3972 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003973 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3974 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003975 goto on_error;
3976 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 case EXPR_SUB: n1 = n1 - n2; break;
3978 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003979 default: n1 = num_modulus(n1, n2, &failed);
3980 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003981 goto on_error;
3982 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 }
3984 clear_tv(tv1);
3985 clear_tv(tv2);
3986 tv1->v_type = VAR_NUMBER;
3987 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003988 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 }
3990 }
3991 break;
3992
3993 case ISN_CONCAT:
3994 {
3995 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3996 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3997 char_u *res;
3998
3999 res = concat_str(str1, str2);
4000 clear_tv(STACK_TV_BOT(-2));
4001 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004002 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 STACK_TV_BOT(-1)->vval.v_string = res;
4004 }
4005 break;
4006
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004007 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004008 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004009 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004010 int is_slice = iptr->isn_type == ISN_STRSLICE;
4011 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004012 char_u *res;
4013
4014 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004015 // string slice: string is at stack-3, first index at
4016 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004017 if (is_slice)
4018 {
4019 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004020 n1 = tv->vval.v_number;
4021 }
4022
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004023 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004024 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004025
Bram Moolenaar4c137212021-04-19 16:48:48 +02004026 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004027 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004028 if (is_slice)
4029 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004030 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004031 else
4032 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004033 // single character (including composing characters).
4034 // If the index is too big or negative the result is
4035 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004036 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004037 vim_free(tv->vval.v_string);
4038 tv->vval.v_string = res;
4039 }
4040 break;
4041
4042 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004043 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004044 case ISN_BLOBINDEX:
4045 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004047 int is_slice = iptr->isn_type == ISN_LISTSLICE
4048 || iptr->isn_type == ISN_BLOBSLICE;
4049 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4050 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004051 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004052 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
4054 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004055 // list slice: list is at stack-3, indexes at stack-2 and
4056 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004057 // Same for blob.
4058 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059
4060 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004061 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004063
4064 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 {
Bram Moolenaared591872020-08-15 22:14:53 +02004066 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004067 n1 = tv->vval.v_number;
4068 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 }
Bram Moolenaared591872020-08-15 22:14:53 +02004070
Bram Moolenaar4c137212021-04-19 16:48:48 +02004071 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004072 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004073 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004074 if (is_blob)
4075 {
4076 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4077 n1, n2, FALSE, tv) == FAIL)
4078 goto on_error;
4079 }
4080 else
4081 {
4082 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4083 n1, n2, FALSE, tv, TRUE) == FAIL)
4084 goto on_error;
4085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 }
4087 break;
4088
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004089 case ISN_ANYINDEX:
4090 case ISN_ANYSLICE:
4091 {
4092 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4093 typval_T *var1, *var2;
4094 int res;
4095
4096 // index: composite is at stack-2, index at stack-1
4097 // slice: composite is at stack-3, indexes at stack-2 and
4098 // stack-1
4099 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004100 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004101 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4102 goto on_error;
4103 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4104 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004105 res = eval_index_inner(tv, is_slice, var1, var2,
4106 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004107 clear_tv(var1);
4108 if (is_slice)
4109 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004110 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004111 if (res == FAIL)
4112 goto on_error;
4113 }
4114 break;
4115
Bram Moolenaar9af78762020-06-16 11:34:42 +02004116 case ISN_SLICE:
4117 {
4118 list_T *list;
4119 int count = iptr->isn_arg.number;
4120
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004121 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004122 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004123 list = tv->vval.v_list;
4124
4125 // no error for short list, expect it to be checked earlier
4126 if (list != NULL && list->lv_len >= count)
4127 {
4128 list_T *newlist = list_slice(list,
4129 count, list->lv_len - 1);
4130
4131 if (newlist != NULL)
4132 {
4133 list_unref(list);
4134 tv->vval.v_list = newlist;
4135 ++newlist->lv_refcount;
4136 }
4137 }
4138 }
4139 break;
4140
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004141 case ISN_GETITEM:
4142 {
4143 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004144 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004145
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004146 // Get list item: list is at stack-1, push item.
4147 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004148 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4149 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004150
Bram Moolenaar35578162021-08-02 19:10:38 +02004151 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004152 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004153 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004154 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004155
4156 // Useful when used in unpack assignment. Reset at
4157 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004158 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004159 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004160 }
4161 break;
4162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 case ISN_MEMBER:
4164 {
4165 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004166 char_u *key;
4167 dictitem_T *di;
4168
4169 // dict member: dict is at stack-2, key at stack-1
4170 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004171 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004172 dict = tv->vval.v_dict;
4173
4174 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004175 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004176 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004177 if (key == NULL)
4178 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004179
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004180 if ((di = dict_find(dict, key, -1)) == NULL)
4181 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004182 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004183 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004184
4185 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004186 // stack contents makes sense and the dict stack is
4187 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004188 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004189 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004190 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004191 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004192 tv->v_type = VAR_NUMBER;
4193 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004194 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004195 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004196 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004197 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004198 // Put the dict used on the dict stack, it might be used by
4199 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004200 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004201 if (dict_stack_save(tv) == FAIL)
4202 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004203 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004204 }
4205 break;
4206
4207 // dict member with string key
4208 case ISN_STRINGMEMBER:
4209 {
4210 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 dictitem_T *di;
4212
4213 tv = STACK_TV_BOT(-1);
4214 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4215 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004216 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004218 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 }
4220 dict = tv->vval.v_dict;
4221
4222 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4223 == NULL)
4224 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004225 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004227 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004229 // Put the dict used on the dict stack, it might be used by
4230 // a dict function later.
4231 if (dict_stack_save(tv) == FAIL)
4232 goto on_fatal_error;
4233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004235 }
4236 break;
4237
4238 case ISN_CLEARDICT:
4239 dict_stack_drop();
4240 break;
4241
4242 case ISN_USEDICT:
4243 {
4244 typval_T *dict_tv = dict_stack_get_tv();
4245
4246 // Turn "dict.Func" into a partial for "Func" bound to
4247 // "dict". Don't do this when "Func" is already a partial
4248 // that was bound explicitly (pt_auto is FALSE).
4249 tv = STACK_TV_BOT(-1);
4250 if (dict_tv != NULL
4251 && dict_tv->v_type == VAR_DICT
4252 && dict_tv->vval.v_dict != NULL
4253 && (tv->v_type == VAR_FUNC
4254 || (tv->v_type == VAR_PARTIAL
4255 && (tv->vval.v_partial->pt_auto
4256 || tv->vval.v_partial->pt_dict == NULL))))
4257 dict_tv->vval.v_dict =
4258 make_partial(dict_tv->vval.v_dict, tv);
4259 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 }
4261 break;
4262
4263 case ISN_NEGATENR:
4264 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004265 if (tv->v_type != VAR_NUMBER
4266#ifdef FEAT_FLOAT
4267 && tv->v_type != VAR_FLOAT
4268#endif
4269 )
4270 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004271 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004272 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004273 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004274 }
4275#ifdef FEAT_FLOAT
4276 if (tv->v_type == VAR_FLOAT)
4277 tv->vval.v_float = -tv->vval.v_float;
4278 else
4279#endif
4280 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 break;
4282
4283 case ISN_CHECKNR:
4284 {
4285 int error = FALSE;
4286
4287 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004288 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004290 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 (void)tv_get_number_chk(tv, &error);
4292 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004293 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 }
4295 break;
4296
4297 case ISN_CHECKTYPE:
4298 {
4299 checktype_T *ct = &iptr->isn_arg.type;
4300
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004301 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004302 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004303 if (!ectx->ec_where.wt_variable)
4304 ectx->ec_where.wt_index = ct->ct_arg_idx;
4305 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4306 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004307 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004308 if (!ectx->ec_where.wt_variable)
4309 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004310
4311 // number 0 is FALSE, number 1 is TRUE
4312 if (tv->v_type == VAR_NUMBER
4313 && ct->ct_type->tt_type == VAR_BOOL
4314 && (tv->vval.v_number == 0
4315 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004317 tv->v_type = VAR_BOOL;
4318 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004319 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 }
4321 }
4322 break;
4323
Bram Moolenaar9af78762020-06-16 11:34:42 +02004324 case ISN_CHECKLEN:
4325 {
4326 int min_len = iptr->isn_arg.checklen.cl_min_len;
4327 list_T *list = NULL;
4328
4329 tv = STACK_TV_BOT(-1);
4330 if (tv->v_type == VAR_LIST)
4331 list = tv->vval.v_list;
4332 if (list == NULL || list->lv_len < min_len
4333 || (list->lv_len > min_len
4334 && !iptr->isn_arg.checklen.cl_more_OK))
4335 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004336 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004337 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004338 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004339 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004340 }
4341 }
4342 break;
4343
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004344 case ISN_SETTYPE:
4345 {
4346 checktype_T *ct = &iptr->isn_arg.type;
4347
4348 tv = STACK_TV_BOT(-1);
4349 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4350 {
4351 free_type(tv->vval.v_dict->dv_type);
4352 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4353 }
4354 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4355 {
4356 free_type(tv->vval.v_list->lv_type);
4357 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4358 }
4359 }
4360 break;
4361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004363 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 {
4365 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004366 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004368 if (iptr->isn_type == ISN_2BOOL)
4369 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004370 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004371 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004372 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004373 n = !n;
4374 }
4375 else
4376 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004377 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004378 SOURCING_LNUM = iptr->isn_lnum;
4379 n = tv_get_bool_chk(tv, &error);
4380 if (error)
4381 goto on_error;
4382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 clear_tv(tv);
4384 tv->v_type = VAR_BOOL;
4385 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4386 }
4387 break;
4388
4389 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004390 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004391 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004392 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4393 iptr->isn_type == ISN_2STRING_ANY,
4394 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004395 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 break;
4397
Bram Moolenaar08597872020-12-10 19:43:40 +01004398 case ISN_RANGE:
4399 {
4400 exarg_T ea;
4401 char *errormsg;
4402
Bram Moolenaarece0b872021-01-08 20:40:45 +01004403 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004404 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004405 ea.addr_type = ADDR_LINES;
4406 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004407 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004408 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004409 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004410
Bram Moolenaar35578162021-08-02 19:10:38 +02004411 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004412 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004413 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004414 tv = STACK_TV_BOT(-1);
4415 tv->v_type = VAR_NUMBER;
4416 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004417 if (ea.addr_count == 0)
4418 tv->vval.v_number = curwin->w_cursor.lnum;
4419 else
4420 tv->vval.v_number = ea.line2;
4421 }
4422 break;
4423
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004424 case ISN_PUT:
4425 {
4426 int regname = iptr->isn_arg.put.put_regname;
4427 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4428 char_u *expr = NULL;
4429 int dir = FORWARD;
4430
Bram Moolenaar08597872020-12-10 19:43:40 +01004431 if (lnum < -2)
4432 {
4433 // line number was put on the stack by ISN_RANGE
4434 tv = STACK_TV_BOT(-1);
4435 curwin->w_cursor.lnum = tv->vval.v_number;
4436 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4437 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004438 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004439 }
4440 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004441 // :put! above cursor
4442 dir = BACKWARD;
4443 else if (lnum >= 0)
4444 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004445
4446 if (regname == '=')
4447 {
4448 tv = STACK_TV_BOT(-1);
4449 if (tv->v_type == VAR_STRING)
4450 expr = tv->vval.v_string;
4451 else
4452 {
4453 expr = typval2string(tv, TRUE); // allocates value
4454 clear_tv(tv);
4455 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004456 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004457 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004458 check_cursor();
4459 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4460 vim_free(expr);
4461 }
4462 break;
4463
Bram Moolenaar02194d22020-10-24 23:08:38 +02004464 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004465 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4466 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4467 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4468 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004469 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4470 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004471 break;
4472
Bram Moolenaar02194d22020-10-24 23:08:38 +02004473 case ISN_CMDMOD_REV:
4474 // filter regprog is owned by the instruction, don't free it
4475 cmdmod.cmod_filter_regmatch.regprog = NULL;
4476 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004477 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4478 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004479 break;
4480
Bram Moolenaar792f7862020-11-23 08:31:18 +01004481 case ISN_UNPACK:
4482 {
4483 int count = iptr->isn_arg.unpack.unp_count;
4484 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4485 list_T *l;
4486 listitem_T *li;
4487 int i;
4488
4489 // Check there is a valid list to unpack.
4490 tv = STACK_TV_BOT(-1);
4491 if (tv->v_type != VAR_LIST)
4492 {
4493 SOURCING_LNUM = iptr->isn_lnum;
4494 emsg(_(e_for_argument_must_be_sequence_of_lists));
4495 goto on_error;
4496 }
4497 l = tv->vval.v_list;
4498 if (l == NULL
4499 || l->lv_len < (semicolon ? count - 1 : count))
4500 {
4501 SOURCING_LNUM = iptr->isn_lnum;
4502 emsg(_(e_list_value_does_not_have_enough_items));
4503 goto on_error;
4504 }
4505 else if (!semicolon && l->lv_len > count)
4506 {
4507 SOURCING_LNUM = iptr->isn_lnum;
4508 emsg(_(e_list_value_has_more_items_than_targets));
4509 goto on_error;
4510 }
4511
4512 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004513 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004514 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004515 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004516
4517 // Variable after semicolon gets a list with the remaining
4518 // items.
4519 if (semicolon)
4520 {
4521 list_T *rem_list =
4522 list_alloc_with_items(l->lv_len - count + 1);
4523
4524 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004525 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004526 tv = STACK_TV_BOT(-count);
4527 tv->vval.v_list = rem_list;
4528 ++rem_list->lv_refcount;
4529 tv->v_lock = 0;
4530 li = l->lv_first;
4531 for (i = 0; i < count - 1; ++i)
4532 li = li->li_next;
4533 for (i = 0; li != NULL; ++i)
4534 {
4535 list_set_item(rem_list, i, &li->li_tv);
4536 li = li->li_next;
4537 }
4538 --count;
4539 }
4540
4541 // Produce the values in reverse order, first item last.
4542 li = l->lv_first;
4543 for (i = 0; i < count; ++i)
4544 {
4545 tv = STACK_TV_BOT(-i - 1);
4546 copy_tv(&li->li_tv, tv);
4547 li = li->li_next;
4548 }
4549
4550 list_unref(l);
4551 }
4552 break;
4553
Bram Moolenaarb2049902021-01-24 12:53:53 +01004554 case ISN_PROF_START:
4555 case ISN_PROF_END:
4556 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004557#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004558 funccall_T cookie;
4559 ufunc_T *cur_ufunc =
4560 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004561 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004562
4563 cookie.func = cur_ufunc;
4564 if (iptr->isn_type == ISN_PROF_START)
4565 {
4566 func_line_start(&cookie, iptr->isn_lnum);
4567 // if we get here the instruction is executed
4568 func_line_exec(&cookie);
4569 }
4570 else
4571 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004572#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004573 }
4574 break;
4575
Bram Moolenaare99d4222021-06-13 14:01:26 +02004576 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004577 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004578 break;
4579
Bram Moolenaar389df252020-07-09 21:20:47 +02004580 case ISN_SHUFFLE:
4581 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004582 typval_T tmp_tv;
4583 int item = iptr->isn_arg.shuffle.shfl_item;
4584 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004585
4586 tmp_tv = *STACK_TV_BOT(-item);
4587 for ( ; up > 0 && item > 1; --up)
4588 {
4589 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4590 --item;
4591 }
4592 *STACK_TV_BOT(-item) = tmp_tv;
4593 }
4594 break;
4595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004597 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004599 ectx->ec_where.wt_index = 0;
4600 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 break;
4602 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004603 continue;
4604
Bram Moolenaard032f342020-07-18 18:13:02 +02004605func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004606 // Restore previous function. If the frame pointer is where we started
4607 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004608 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004609 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004610
Bram Moolenaar4c137212021-04-19 16:48:48 +02004611 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004612 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004613 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004614 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004615
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004616on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004617 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004618 // If "emsg_silent" is set then ignore the error, unless it was set
4619 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004620 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004621 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004622 {
4623 // If a sequence of instructions causes an error while ":silent!"
4624 // was used, restore the stack length and jump ahead to restoring
4625 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004626 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004627 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004628 while (ectx->ec_stack.ga_len
4629 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004630 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004631 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004632 clear_tv(STACK_TV_BOT(0));
4633 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004634 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4635 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004636 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004637 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004638 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004639on_fatal_error:
4640 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004641 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004642 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004643 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 }
4645
4646done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004647 ret = OK;
4648theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004649 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004650 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4651 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004652}
4653
4654/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004655 * Execute the instructions from a VAR_INSTR typeval and put the result in
4656 * "rettv".
4657 * Return OK or FAIL.
4658 */
4659 int
4660exe_typval_instr(typval_T *tv, typval_T *rettv)
4661{
4662 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4663 isn_T *save_instr = ectx->ec_instr;
4664 int save_iidx = ectx->ec_iidx;
4665 int res;
4666
4667 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4668 res = exec_instructions(ectx);
4669 if (res == OK)
4670 {
4671 *rettv = *STACK_TV_BOT(-1);
4672 --ectx->ec_stack.ga_len;
4673 }
4674
4675 ectx->ec_instr = save_instr;
4676 ectx->ec_iidx = save_iidx;
4677
4678 return res;
4679}
4680
4681/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004682 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4683 * "substitute_instr".
4684 */
4685 char_u *
4686exe_substitute_instr(void)
4687{
4688 ectx_T *ectx = substitute_instr->subs_ectx;
4689 isn_T *save_instr = ectx->ec_instr;
4690 int save_iidx = ectx->ec_iidx;
4691 char_u *res;
4692
4693 ectx->ec_instr = substitute_instr->subs_instr;
4694 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004695 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004696 typval_T *tv = STACK_TV_BOT(-1);
4697
Bram Moolenaar27523602021-06-05 21:36:19 +02004698 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004699 --ectx->ec_stack.ga_len;
4700 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004701 }
4702 else
4703 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004704 substitute_instr->subs_status = FAIL;
4705 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707
Bram Moolenaar4c137212021-04-19 16:48:48 +02004708 ectx->ec_instr = save_instr;
4709 ectx->ec_iidx = save_iidx;
4710
4711 return res;
4712}
4713
4714/*
4715 * Call a "def" function from old Vim script.
4716 * Return OK or FAIL.
4717 */
4718 int
4719call_def_function(
4720 ufunc_T *ufunc,
4721 int argc_arg, // nr of arguments
4722 typval_T *argv, // arguments
4723 partial_T *partial, // optional partial for context
4724 typval_T *rettv) // return value
4725{
4726 ectx_T ectx; // execution context
4727 int argc = argc_arg;
4728 typval_T *tv;
4729 int idx;
4730 int ret = FAIL;
4731 int defcount = ufunc->uf_args.ga_len - argc;
4732 sctx_T save_current_sctx = current_sctx;
4733 int did_emsg_before = did_emsg_cumul + did_emsg;
4734 int save_suppress_errthrow = suppress_errthrow;
4735 msglist_T **saved_msg_list = NULL;
4736 msglist_T *private_msg_list = NULL;
4737 int save_emsg_silent_def = emsg_silent_def;
4738 int save_did_emsg_def = did_emsg_def;
4739 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004740 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004741
4742// Get pointer to item in the stack.
4743#undef STACK_TV
4744#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4745
4746// Get pointer to item at the bottom of the stack, -1 is the bottom.
4747#undef STACK_TV_BOT
4748#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4749
4750// Get pointer to a local variable on the stack. Negative for arguments.
4751#undef STACK_TV_VAR
4752#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4753
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004754 // Update uf_has_breakpoint if needed.
4755 update_has_breakpoint(ufunc);
4756
Bram Moolenaar4c137212021-04-19 16:48:48 +02004757 if (ufunc->uf_def_status == UF_NOT_COMPILED
4758 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004759 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4760 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004761 == FAIL))
4762 {
4763 if (did_emsg_cumul + did_emsg == did_emsg_before)
4764 semsg(_(e_function_is_not_compiled_str),
4765 printable_func_name(ufunc));
4766 return FAIL;
4767 }
4768
4769 {
4770 // Check the function was really compiled.
4771 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4772 + ufunc->uf_dfunc_idx;
4773 if (INSTRUCTIONS(dfunc) == NULL)
4774 {
4775 iemsg("using call_def_function() on not compiled function");
4776 return FAIL;
4777 }
4778 }
4779
4780 // If depth of calling is getting too high, don't execute the function.
4781 orig_funcdepth = funcdepth_get();
4782 if (funcdepth_increment() == FAIL)
4783 return FAIL;
4784
4785 CLEAR_FIELD(ectx);
4786 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4787 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004788 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004789 {
4790 funcdepth_decrement();
4791 return FAIL;
4792 }
4793 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4794 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4795 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004796 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004797
4798 idx = argc - ufunc->uf_args.ga_len;
4799 if (idx > 0 && ufunc->uf_va_name == NULL)
4800 {
4801 if (idx == 1)
4802 emsg(_(e_one_argument_too_many));
4803 else
4804 semsg(_(e_nr_arguments_too_many), idx);
4805 goto failed_early;
4806 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004807 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4808 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004809 {
4810 if (idx == -1)
4811 emsg(_(e_one_argument_too_few));
4812 else
4813 semsg(_(e_nr_arguments_too_few), -idx);
4814 goto failed_early;
4815 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004816
4817 // Put arguments on the stack, but no more than what the function expects.
4818 // A lambda can be called with more arguments than it uses.
4819 for (idx = 0; idx < argc
4820 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4821 ++idx)
4822 {
4823 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4824 && argv[idx].v_type == VAR_SPECIAL
4825 && argv[idx].vval.v_number == VVAL_NONE)
4826 {
4827 // Use the default value.
4828 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4829 }
4830 else
4831 {
4832 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4833 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004834 ufunc->uf_arg_types[idx], &argv[idx],
4835 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004836 goto failed_early;
4837 copy_tv(&argv[idx], STACK_TV_BOT(0));
4838 }
4839 ++ectx.ec_stack.ga_len;
4840 }
4841
4842 // Turn varargs into a list. Empty list if no args.
4843 if (ufunc->uf_va_name != NULL)
4844 {
4845 int vararg_count = argc - ufunc->uf_args.ga_len;
4846
4847 if (vararg_count < 0)
4848 vararg_count = 0;
4849 else
4850 argc -= vararg_count;
4851 if (exe_newlist(vararg_count, &ectx) == FAIL)
4852 goto failed_early;
4853
4854 // Check the type of the list items.
4855 tv = STACK_TV_BOT(-1);
4856 if (ufunc->uf_va_type != NULL
4857 && ufunc->uf_va_type != &t_list_any
4858 && ufunc->uf_va_type->tt_member != &t_any
4859 && tv->vval.v_list != NULL)
4860 {
4861 type_T *expected = ufunc->uf_va_type->tt_member;
4862 listitem_T *li = tv->vval.v_list->lv_first;
4863
4864 for (idx = 0; idx < vararg_count; ++idx)
4865 {
4866 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004867 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004868 goto failed_early;
4869 li = li->li_next;
4870 }
4871 }
4872
4873 if (defcount > 0)
4874 // Move varargs list to below missing default arguments.
4875 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4876 --ectx.ec_stack.ga_len;
4877 }
4878
4879 // Make space for omitted arguments, will store default value below.
4880 // Any varargs list goes after them.
4881 if (defcount > 0)
4882 for (idx = 0; idx < defcount; ++idx)
4883 {
4884 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4885 ++ectx.ec_stack.ga_len;
4886 }
4887 if (ufunc->uf_va_name != NULL)
4888 ++ectx.ec_stack.ga_len;
4889
4890 // Frame pointer points to just after arguments.
4891 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4892 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4893
4894 {
4895 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4896 + ufunc->uf_dfunc_idx;
4897 ufunc_T *base_ufunc = dfunc->df_ufunc;
4898
4899 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4900 // by copy_func().
4901 if (partial != NULL || base_ufunc->uf_partial != NULL)
4902 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004903 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4904 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004905 goto failed_early;
4906 if (partial != NULL)
4907 {
4908 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4909 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004910 if (current_ectx->ec_outer_ref != NULL
4911 && current_ectx->ec_outer_ref->or_outer != NULL)
4912 ectx.ec_outer_ref->or_outer =
4913 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004914 }
4915 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004916 {
4917 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4918 ++partial->pt_refcount;
4919 ectx.ec_outer_ref->or_partial = partial;
4920 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004921 }
4922 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004923 {
4924 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4925 ++base_ufunc->uf_partial->pt_refcount;
4926 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4927 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004928 }
4929 }
4930
4931 // dummy frame entries
4932 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4933 {
4934 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4935 ++ectx.ec_stack.ga_len;
4936 }
4937
4938 {
4939 // Reserve space for local variables and any closure reference count.
4940 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4941 + ufunc->uf_dfunc_idx;
4942
4943 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4944 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4945 ectx.ec_stack.ga_len += dfunc->df_varcount;
4946 if (dfunc->df_has_closure)
4947 {
4948 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4949 STACK_TV_VAR(idx)->vval.v_number = 0;
4950 ++ectx.ec_stack.ga_len;
4951 }
4952
4953 ectx.ec_instr = INSTRUCTIONS(dfunc);
4954 }
4955
4956 // Following errors are in the function, not the caller.
4957 // Commands behave like vim9script.
4958 estack_push_ufunc(ufunc, 1);
4959 current_sctx = ufunc->uf_script_ctx;
4960 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4961
4962 // Use a specific location for storing error messages to be converted to an
4963 // exception.
4964 saved_msg_list = msg_list;
4965 msg_list = &private_msg_list;
4966
4967 // Do turn errors into exceptions.
4968 suppress_errthrow = FALSE;
4969
4970 // Do not delete the function while executing it.
4971 ++ufunc->uf_calls;
4972
4973 // When ":silent!" was used before calling then we still abort the
4974 // function. If ":silent!" is used in the function then we don't.
4975 emsg_silent_def = emsg_silent;
4976 did_emsg_def = 0;
4977
4978 ectx.ec_where.wt_index = 0;
4979 ectx.ec_where.wt_variable = FALSE;
4980
4981 // Execute the instructions until done.
4982 ret = exec_instructions(&ectx);
4983 if (ret == OK)
4984 {
4985 // function finished, get result from the stack.
4986 if (ufunc->uf_ret_type == &t_void)
4987 {
4988 rettv->v_type = VAR_VOID;
4989 }
4990 else
4991 {
4992 tv = STACK_TV_BOT(-1);
4993 *rettv = *tv;
4994 tv->v_type = VAR_UNKNOWN;
4995 }
4996 }
4997
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004998 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004999 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5000 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005001
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005002 // Deal with any remaining closures, they may be in use somewhere.
5003 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005004 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005005 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005006 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
5007 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005008
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005009 estack_pop();
5010 current_sctx = save_current_sctx;
5011
Bram Moolenaar2336c372021-12-06 15:06:54 +00005012 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5013 // Function was unreferenced while being used, free it now.
5014 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005015
Bram Moolenaar352134b2020-10-17 22:04:08 +02005016 if (*msg_list != NULL && saved_msg_list != NULL)
5017 {
5018 msglist_T **plist = saved_msg_list;
5019
5020 // Append entries from the current msg_list (uncaught exceptions) to
5021 // the saved msg_list.
5022 while (*plist != NULL)
5023 plist = &(*plist)->next;
5024
5025 *plist = *msg_list;
5026 }
5027 msg_list = saved_msg_list;
5028
Bram Moolenaar4c137212021-04-19 16:48:48 +02005029 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005030 {
5031 cmdmod.cmod_filter_regmatch.regprog = NULL;
5032 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005033 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005034 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005035 emsg_silent_def = save_emsg_silent_def;
5036 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005037
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005038failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005039 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5041 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005042 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005045 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005046 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005047 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005048 if (ectx.ec_outer_ref->or_outer_allocated)
5049 vim_free(ectx.ec_outer_ref->or_outer);
5050 partial_unref(ectx.ec_outer_ref->or_partial);
5051 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005052 }
5053
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005054 // Not sure if this is necessary.
5055 suppress_errthrow = save_suppress_errthrow;
5056
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005057 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5058 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005059 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005060 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005061 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 return ret;
5063}
5064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005066 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5067 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5068 * "pfx" is prefixed to every line.
5069 */
5070 static void
5071list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5072{
5073 int line_idx = 0;
5074 int prev_current = 0;
5075 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005076 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005077
5078 for (current = 0; current < instr_count; ++current)
5079 {
5080 isn_T *iptr = &instr[current];
5081 char *line;
5082
5083 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005084 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005085 while (line_idx < iptr->isn_lnum
5086 && line_idx < ufunc->uf_lines.ga_len)
5087 {
5088 if (current > prev_current)
5089 {
5090 msg_puts("\n\n");
5091 prev_current = current;
5092 }
5093 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5094 if (line != NULL)
5095 msg(line);
5096 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005097 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5098 {
5099 int first_def_arg = ufunc->uf_args.ga_len
5100 - ufunc->uf_def_args.ga_len;
5101
5102 if (def_arg_idx > 0)
5103 msg_puts("\n\n");
5104 msg_start();
5105 msg_puts(" ");
5106 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5107 first_def_arg + def_arg_idx]);
5108 msg_puts(" = ");
5109 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5110 msg_clr_eos();
5111 msg_end();
5112 }
5113 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005114
5115 switch (iptr->isn_type)
5116 {
5117 case ISN_EXEC:
5118 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5119 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005120 case ISN_EXEC_SPLIT:
5121 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5122 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005123 case ISN_EXECRANGE:
5124 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5125 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005126 case ISN_LEGACY_EVAL:
5127 smsg("%s%4d EVAL legacy %s", pfx, current,
5128 iptr->isn_arg.string);
5129 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005130 case ISN_REDIRSTART:
5131 smsg("%s%4d REDIR", pfx, current);
5132 break;
5133 case ISN_REDIREND:
5134 smsg("%s%4d REDIR END%s", pfx, current,
5135 iptr->isn_arg.number ? " append" : "");
5136 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005137 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005138#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005139 smsg("%s%4d CEXPR pre %s", pfx, current,
5140 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005141#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005142 break;
5143 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005144#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005145 {
5146 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5147
5148 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5149 cexpr_get_auname(cer->cer_cmdidx),
5150 cer->cer_forceit ? "!" : "",
5151 cer->cer_cmdline);
5152 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005153#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005154 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005155 case ISN_INSTR:
5156 {
5157 smsg("%s%4d INSTR", pfx, current);
5158 list_instructions(" ", iptr->isn_arg.instr,
5159 INT_MAX, NULL);
5160 msg(" -------------");
5161 }
5162 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005163 case ISN_SUBSTITUTE:
5164 {
5165 subs_T *subs = &iptr->isn_arg.subs;
5166
5167 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5168 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5169 msg(" -------------");
5170 }
5171 break;
5172 case ISN_EXECCONCAT:
5173 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5174 (varnumber_T)iptr->isn_arg.number);
5175 break;
5176 case ISN_ECHO:
5177 {
5178 echo_T *echo = &iptr->isn_arg.echo;
5179
5180 smsg("%s%4d %s %d", pfx, current,
5181 echo->echo_with_white ? "ECHO" : "ECHON",
5182 echo->echo_count);
5183 }
5184 break;
5185 case ISN_EXECUTE:
5186 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005187 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005188 break;
5189 case ISN_ECHOMSG:
5190 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005191 (varnumber_T)(iptr->isn_arg.number));
5192 break;
5193 case ISN_ECHOCONSOLE:
5194 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5195 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005196 break;
5197 case ISN_ECHOERR:
5198 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005199 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005200 break;
5201 case ISN_LOAD:
5202 {
5203 if (iptr->isn_arg.number < 0)
5204 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5205 (varnumber_T)(iptr->isn_arg.number
5206 + STACK_FRAME_SIZE));
5207 else
5208 smsg("%s%4d LOAD $%lld", pfx, current,
5209 (varnumber_T)(iptr->isn_arg.number));
5210 }
5211 break;
5212 case ISN_LOADOUTER:
5213 {
5214 if (iptr->isn_arg.number < 0)
5215 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5216 iptr->isn_arg.outer.outer_depth,
5217 iptr->isn_arg.outer.outer_idx
5218 + STACK_FRAME_SIZE);
5219 else
5220 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5221 iptr->isn_arg.outer.outer_depth,
5222 iptr->isn_arg.outer.outer_idx);
5223 }
5224 break;
5225 case ISN_LOADV:
5226 smsg("%s%4d LOADV v:%s", pfx, current,
5227 get_vim_var_name(iptr->isn_arg.number));
5228 break;
5229 case ISN_LOADSCRIPT:
5230 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005231 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5232 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5233 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005234
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005235 sv = get_script_svar(sref, -1);
5236 if (sv == NULL)
5237 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5238 pfx, current, si->sn_name);
5239 else
5240 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005241 sv->sv_name,
5242 sref->sref_idx,
5243 si->sn_name);
5244 }
5245 break;
5246 case ISN_LOADS:
5247 {
5248 scriptitem_T *si = SCRIPT_ITEM(
5249 iptr->isn_arg.loadstore.ls_sid);
5250
5251 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5252 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5253 }
5254 break;
5255 case ISN_LOADAUTO:
5256 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5257 break;
5258 case ISN_LOADG:
5259 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5260 break;
5261 case ISN_LOADB:
5262 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5263 break;
5264 case ISN_LOADW:
5265 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5266 break;
5267 case ISN_LOADT:
5268 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5269 break;
5270 case ISN_LOADGDICT:
5271 smsg("%s%4d LOAD g:", pfx, current);
5272 break;
5273 case ISN_LOADBDICT:
5274 smsg("%s%4d LOAD b:", pfx, current);
5275 break;
5276 case ISN_LOADWDICT:
5277 smsg("%s%4d LOAD w:", pfx, current);
5278 break;
5279 case ISN_LOADTDICT:
5280 smsg("%s%4d LOAD t:", pfx, current);
5281 break;
5282 case ISN_LOADOPT:
5283 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5284 break;
5285 case ISN_LOADENV:
5286 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5287 break;
5288 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005289 smsg("%s%4d LOADREG @%c", pfx, current,
5290 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005291 break;
5292
5293 case ISN_STORE:
5294 if (iptr->isn_arg.number < 0)
5295 smsg("%s%4d STORE arg[%lld]", pfx, current,
5296 iptr->isn_arg.number + STACK_FRAME_SIZE);
5297 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005298 smsg("%s%4d STORE $%lld", pfx, current,
5299 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005300 break;
5301 case ISN_STOREOUTER:
5302 {
5303 if (iptr->isn_arg.number < 0)
5304 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5305 iptr->isn_arg.outer.outer_depth,
5306 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5307 else
5308 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5309 iptr->isn_arg.outer.outer_depth,
5310 iptr->isn_arg.outer.outer_idx);
5311 }
5312 break;
5313 case ISN_STOREV:
5314 smsg("%s%4d STOREV v:%s", pfx, current,
5315 get_vim_var_name(iptr->isn_arg.number));
5316 break;
5317 case ISN_STOREAUTO:
5318 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5319 break;
5320 case ISN_STOREG:
5321 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5322 break;
5323 case ISN_STOREB:
5324 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5325 break;
5326 case ISN_STOREW:
5327 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5328 break;
5329 case ISN_STORET:
5330 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5331 break;
5332 case ISN_STORES:
5333 {
5334 scriptitem_T *si = SCRIPT_ITEM(
5335 iptr->isn_arg.loadstore.ls_sid);
5336
5337 smsg("%s%4d STORES %s in %s", pfx, current,
5338 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5339 }
5340 break;
5341 case ISN_STORESCRIPT:
5342 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005343 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5344 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5345 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005346
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005347 sv = get_script_svar(sref, -1);
5348 if (sv == NULL)
5349 smsg("%s%4d STORESCRIPT [deleted] in %s",
5350 pfx, current, si->sn_name);
5351 else
5352 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005353 sv->sv_name,
5354 sref->sref_idx,
5355 si->sn_name);
5356 }
5357 break;
5358 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005359 case ISN_STOREFUNCOPT:
5360 smsg("%s%4d %s &%s", pfx, current,
5361 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005362 iptr->isn_arg.storeopt.so_name);
5363 break;
5364 case ISN_STOREENV:
5365 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5366 break;
5367 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005368 smsg("%s%4d STOREREG @%c", pfx, current,
5369 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005370 break;
5371 case ISN_STORENR:
5372 smsg("%s%4d STORE %lld in $%d", pfx, current,
5373 iptr->isn_arg.storenr.stnr_val,
5374 iptr->isn_arg.storenr.stnr_idx);
5375 break;
5376
5377 case ISN_STOREINDEX:
5378 smsg("%s%4d STOREINDEX %s", pfx, current,
5379 vartype_name(iptr->isn_arg.vartype));
5380 break;
5381
5382 case ISN_STORERANGE:
5383 smsg("%s%4d STORERANGE", pfx, current);
5384 break;
5385
5386 // constants
5387 case ISN_PUSHNR:
5388 smsg("%s%4d PUSHNR %lld", pfx, current,
5389 (varnumber_T)(iptr->isn_arg.number));
5390 break;
5391 case ISN_PUSHBOOL:
5392 case ISN_PUSHSPEC:
5393 smsg("%s%4d PUSH %s", pfx, current,
5394 get_var_special_name(iptr->isn_arg.number));
5395 break;
5396 case ISN_PUSHF:
5397#ifdef FEAT_FLOAT
5398 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5399#endif
5400 break;
5401 case ISN_PUSHS:
5402 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5403 break;
5404 case ISN_PUSHBLOB:
5405 {
5406 char_u *r;
5407 char_u numbuf[NUMBUFLEN];
5408 char_u *tofree;
5409
5410 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5411 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5412 vim_free(tofree);
5413 }
5414 break;
5415 case ISN_PUSHFUNC:
5416 {
5417 char *name = (char *)iptr->isn_arg.string;
5418
5419 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5420 name == NULL ? "[none]" : name);
5421 }
5422 break;
5423 case ISN_PUSHCHANNEL:
5424#ifdef FEAT_JOB_CHANNEL
5425 {
5426 channel_T *channel = iptr->isn_arg.channel;
5427
5428 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5429 channel == NULL ? 0 : channel->ch_id);
5430 }
5431#endif
5432 break;
5433 case ISN_PUSHJOB:
5434#ifdef FEAT_JOB_CHANNEL
5435 {
5436 typval_T tv;
5437 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005438 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005439
5440 tv.v_type = VAR_JOB;
5441 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005442 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005443 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5444 }
5445#endif
5446 break;
5447 case ISN_PUSHEXC:
5448 smsg("%s%4d PUSH v:exception", pfx, current);
5449 break;
5450 case ISN_UNLET:
5451 smsg("%s%4d UNLET%s %s", pfx, current,
5452 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5453 iptr->isn_arg.unlet.ul_name);
5454 break;
5455 case ISN_UNLETENV:
5456 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5457 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5458 iptr->isn_arg.unlet.ul_name);
5459 break;
5460 case ISN_UNLETINDEX:
5461 smsg("%s%4d UNLETINDEX", pfx, current);
5462 break;
5463 case ISN_UNLETRANGE:
5464 smsg("%s%4d UNLETRANGE", pfx, current);
5465 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005466 case ISN_LOCKUNLOCK:
5467 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5468 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005469 case ISN_LOCKCONST:
5470 smsg("%s%4d LOCKCONST", pfx, current);
5471 break;
5472 case ISN_NEWLIST:
5473 smsg("%s%4d NEWLIST size %lld", pfx, current,
5474 (varnumber_T)(iptr->isn_arg.number));
5475 break;
5476 case ISN_NEWDICT:
5477 smsg("%s%4d NEWDICT size %lld", pfx, current,
5478 (varnumber_T)(iptr->isn_arg.number));
5479 break;
5480
5481 // function call
5482 case ISN_BCALL:
5483 {
5484 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5485
5486 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5487 internal_func_name(cbfunc->cbf_idx),
5488 cbfunc->cbf_argcount);
5489 }
5490 break;
5491 case ISN_DCALL:
5492 {
5493 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5494 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5495 + cdfunc->cdf_idx;
5496
5497 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005498 printable_func_name(df->df_ufunc),
5499 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005500 }
5501 break;
5502 case ISN_UCALL:
5503 {
5504 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5505
5506 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5507 cufunc->cuf_name, cufunc->cuf_argcount);
5508 }
5509 break;
5510 case ISN_PCALL:
5511 {
5512 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5513
5514 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5515 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5516 }
5517 break;
5518 case ISN_PCALL_END:
5519 smsg("%s%4d PCALL end", pfx, current);
5520 break;
5521 case ISN_RETURN:
5522 smsg("%s%4d RETURN", pfx, current);
5523 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005524 case ISN_RETURN_VOID:
5525 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005526 break;
5527 case ISN_FUNCREF:
5528 {
5529 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005530 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005531
Bram Moolenaar38453522021-11-28 22:00:12 +00005532 if (funcref->fr_func_name == NULL)
5533 {
5534 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5535 + funcref->fr_dfunc_idx;
5536 name = df->df_ufunc->uf_name;
5537 }
5538 else
5539 name = funcref->fr_func_name;
5540 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005541 }
5542 break;
5543
5544 case ISN_NEWFUNC:
5545 {
5546 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5547
5548 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5549 newfunc->nf_lambda, newfunc->nf_global);
5550 }
5551 break;
5552
5553 case ISN_DEF:
5554 {
5555 char_u *name = iptr->isn_arg.string;
5556
5557 smsg("%s%4d DEF %s", pfx, current,
5558 name == NULL ? (char_u *)"" : name);
5559 }
5560 break;
5561
5562 case ISN_JUMP:
5563 {
5564 char *when = "?";
5565
5566 switch (iptr->isn_arg.jump.jump_when)
5567 {
5568 case JUMP_ALWAYS:
5569 when = "JUMP";
5570 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005571 case JUMP_NEVER:
5572 iemsg("JUMP_NEVER should not be used");
5573 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005574 case JUMP_AND_KEEP_IF_TRUE:
5575 when = "JUMP_AND_KEEP_IF_TRUE";
5576 break;
5577 case JUMP_IF_FALSE:
5578 when = "JUMP_IF_FALSE";
5579 break;
5580 case JUMP_AND_KEEP_IF_FALSE:
5581 when = "JUMP_AND_KEEP_IF_FALSE";
5582 break;
5583 case JUMP_IF_COND_FALSE:
5584 when = "JUMP_IF_COND_FALSE";
5585 break;
5586 case JUMP_IF_COND_TRUE:
5587 when = "JUMP_IF_COND_TRUE";
5588 break;
5589 }
5590 smsg("%s%4d %s -> %d", pfx, current, when,
5591 iptr->isn_arg.jump.jump_where);
5592 }
5593 break;
5594
5595 case ISN_JUMP_IF_ARG_SET:
5596 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5597 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5598 iptr->isn_arg.jump.jump_where);
5599 break;
5600
5601 case ISN_FOR:
5602 {
5603 forloop_T *forloop = &iptr->isn_arg.forloop;
5604
5605 smsg("%s%4d FOR $%d -> %d", pfx, current,
5606 forloop->for_idx, forloop->for_end);
5607 }
5608 break;
5609
5610 case ISN_TRY:
5611 {
5612 try_T *try = &iptr->isn_arg.try;
5613
5614 if (try->try_ref->try_finally == 0)
5615 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5616 pfx, current,
5617 try->try_ref->try_catch,
5618 try->try_ref->try_endtry);
5619 else
5620 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5621 pfx, current,
5622 try->try_ref->try_catch,
5623 try->try_ref->try_finally,
5624 try->try_ref->try_endtry);
5625 }
5626 break;
5627 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 smsg("%s%4d CATCH", pfx, current);
5629 break;
5630 case ISN_TRYCONT:
5631 {
5632 trycont_T *trycont = &iptr->isn_arg.trycont;
5633
5634 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5635 trycont->tct_levels,
5636 trycont->tct_levels == 1 ? "" : "s",
5637 trycont->tct_where);
5638 }
5639 break;
5640 case ISN_FINALLY:
5641 smsg("%s%4d FINALLY", pfx, current);
5642 break;
5643 case ISN_ENDTRY:
5644 smsg("%s%4d ENDTRY", pfx, current);
5645 break;
5646 case ISN_THROW:
5647 smsg("%s%4d THROW", pfx, current);
5648 break;
5649
5650 // expression operations on number
5651 case ISN_OPNR:
5652 case ISN_OPFLOAT:
5653 case ISN_OPANY:
5654 {
5655 char *what;
5656 char *ins;
5657
5658 switch (iptr->isn_arg.op.op_type)
5659 {
5660 case EXPR_MULT: what = "*"; break;
5661 case EXPR_DIV: what = "/"; break;
5662 case EXPR_REM: what = "%"; break;
5663 case EXPR_SUB: what = "-"; break;
5664 case EXPR_ADD: what = "+"; break;
5665 default: what = "???"; break;
5666 }
5667 switch (iptr->isn_type)
5668 {
5669 case ISN_OPNR: ins = "OPNR"; break;
5670 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5671 case ISN_OPANY: ins = "OPANY"; break;
5672 default: ins = "???"; break;
5673 }
5674 smsg("%s%4d %s %s", pfx, current, ins, what);
5675 }
5676 break;
5677
5678 case ISN_COMPAREBOOL:
5679 case ISN_COMPARESPECIAL:
5680 case ISN_COMPARENR:
5681 case ISN_COMPAREFLOAT:
5682 case ISN_COMPARESTRING:
5683 case ISN_COMPAREBLOB:
5684 case ISN_COMPARELIST:
5685 case ISN_COMPAREDICT:
5686 case ISN_COMPAREFUNC:
5687 case ISN_COMPAREANY:
5688 {
5689 char *p;
5690 char buf[10];
5691 char *type;
5692
5693 switch (iptr->isn_arg.op.op_type)
5694 {
5695 case EXPR_EQUAL: p = "=="; break;
5696 case EXPR_NEQUAL: p = "!="; break;
5697 case EXPR_GREATER: p = ">"; break;
5698 case EXPR_GEQUAL: p = ">="; break;
5699 case EXPR_SMALLER: p = "<"; break;
5700 case EXPR_SEQUAL: p = "<="; break;
5701 case EXPR_MATCH: p = "=~"; break;
5702 case EXPR_IS: p = "is"; break;
5703 case EXPR_ISNOT: p = "isnot"; break;
5704 case EXPR_NOMATCH: p = "!~"; break;
5705 default: p = "???"; break;
5706 }
5707 STRCPY(buf, p);
5708 if (iptr->isn_arg.op.op_ic == TRUE)
5709 strcat(buf, "?");
5710 switch(iptr->isn_type)
5711 {
5712 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5713 case ISN_COMPARESPECIAL:
5714 type = "COMPARESPECIAL"; break;
5715 case ISN_COMPARENR: type = "COMPARENR"; break;
5716 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5717 case ISN_COMPARESTRING:
5718 type = "COMPARESTRING"; break;
5719 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5720 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5721 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5722 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5723 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5724 default: type = "???"; break;
5725 }
5726
5727 smsg("%s%4d %s %s", pfx, current, type, buf);
5728 }
5729 break;
5730
5731 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5732 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5733
5734 // expression operations
5735 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5736 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5737 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5738 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5739 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5740 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5741 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5742 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5743 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5744 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5745 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5746 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005747 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005748 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5749 iptr->isn_arg.getitem.gi_index,
5750 iptr->isn_arg.getitem.gi_with_op ?
5751 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005752 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5753 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5754 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005755 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5756 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5757
Bram Moolenaar4c137212021-04-19 16:48:48 +02005758 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5759
5760 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5761 case ISN_CHECKTYPE:
5762 {
5763 checktype_T *ct = &iptr->isn_arg.type;
5764 char *tofree;
5765
5766 if (ct->ct_arg_idx == 0)
5767 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5768 type_name(ct->ct_type, &tofree),
5769 (int)ct->ct_off);
5770 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005771 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5772 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005773 type_name(ct->ct_type, &tofree),
5774 (int)ct->ct_off,
5775 (int)ct->ct_arg_idx);
5776 vim_free(tofree);
5777 break;
5778 }
5779 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5780 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5781 iptr->isn_arg.checklen.cl_min_len);
5782 break;
5783 case ISN_SETTYPE:
5784 {
5785 char *tofree;
5786
5787 smsg("%s%4d SETTYPE %s", pfx, current,
5788 type_name(iptr->isn_arg.type.ct_type, &tofree));
5789 vim_free(tofree);
5790 break;
5791 }
5792 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005793 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5794 smsg("%s%4d INVERT %d (!val)", pfx, current,
5795 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005796 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005797 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5798 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005799 break;
5800 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005801 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005802 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005803 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5804 pfx, current,
5805 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005806 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005807 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5808 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005809 break;
5810 case ISN_PUT:
5811 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5812 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005813 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005814 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5815 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005816 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005817 else
5818 smsg("%s%4d PUT %c %ld", pfx, current,
5819 iptr->isn_arg.put.put_regname,
5820 (long)iptr->isn_arg.put.put_lnum);
5821 break;
5822
Bram Moolenaar4c137212021-04-19 16:48:48 +02005823 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