blob: afd0a613ddcba8f5e45f6b772de8eb16e89c7c03 [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
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000471// Double linked list of funcstack_T in use.
472static funcstack_T *first_funcstack = NULL;
473
474 static void
475add_funcstack_to_list(funcstack_T *funcstack)
476{
477 // Link in list of funcstacks.
478 if (first_funcstack != NULL)
479 first_funcstack->fs_prev = funcstack;
480 funcstack->fs_next = first_funcstack;
481 funcstack->fs_prev = NULL;
482 first_funcstack = funcstack;
483}
484
485 static void
486remove_funcstack_from_list(funcstack_T *funcstack)
487{
488 if (funcstack->fs_prev == NULL)
489 first_funcstack = funcstack->fs_next;
490 else
491 funcstack->fs_prev->fs_next = funcstack->fs_next;
492 if (funcstack->fs_next != NULL)
493 funcstack->fs_next->fs_prev = funcstack->fs_prev;
494}
495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200497 * Used when returning from a function: Check if any closure is still
498 * referenced. If so then move the arguments and variables to a separate piece
499 * of stack to be used when the closure is called.
500 * When "free_arguments" is TRUE the arguments are to be freed.
501 * Returns FAIL when out of memory.
502 */
503 static int
504handle_closure_in_use(ectx_T *ectx, int free_arguments)
505{
506 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
507 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200508 int argcount;
509 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200510 int idx;
511 typval_T *tv;
512 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200513 garray_T *gap = &ectx->ec_funcrefs;
514 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200516 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200517 return OK; // function was freed
518 if (dfunc->df_has_closure == 0)
519 return OK; // no closures
520 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
521 closure_count = tv->vval.v_number;
522 if (closure_count == 0)
523 return OK; // no funcrefs created
524
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200525 argcount = ufunc_argcount(dfunc->df_ufunc);
526 top = ectx->ec_frame_idx - argcount;
527
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200528 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200529 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200531 partial_T *pt;
532 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200533
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200534 if (off < 0)
535 continue; // count is off or already done
536 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200537 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200538 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200540 int i;
541
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200542 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200543 // unreferenced on return.
544 for (i = 0; i < dfunc->df_varcount; ++i)
545 {
546 typval_T *stv = STACK_TV(ectx->ec_frame_idx
547 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200548 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200549 --refcount;
550 }
551 if (refcount > 1)
552 {
553 closure_in_use = TRUE;
554 break;
555 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200556 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200557 }
558
559 if (closure_in_use)
560 {
561 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
562 typval_T *stack;
563
564 // A closure is using the arguments and/or local variables.
565 // Move them to the called function.
566 if (funcstack == NULL)
567 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000568
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200569 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
570 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200571 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
572 funcstack->fs_ga.ga_data = stack;
573 if (stack == NULL)
574 {
575 vim_free(funcstack);
576 return FAIL;
577 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000578 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200579
580 // Move or copy the arguments.
581 for (idx = 0; idx < argcount; ++idx)
582 {
583 tv = STACK_TV(top + idx);
584 if (free_arguments)
585 {
586 *(stack + idx) = *tv;
587 tv->v_type = VAR_UNKNOWN;
588 }
589 else
590 copy_tv(tv, stack + idx);
591 }
592 // Move the local variables.
593 for (idx = 0; idx < dfunc->df_varcount; ++idx)
594 {
595 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200596
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200597 // A partial created for a local function, that is also used as a
598 // local variable, has a reference count for the variable, thus
599 // will never go down to zero. When all these refcounts are one
600 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000601 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200602 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
603 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200604 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200605
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200606 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200607 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
608 gap->ga_len - closure_count + i])
609 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200610 }
611
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200612 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200613 tv->v_type = VAR_UNKNOWN;
614 }
615
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200616 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200617 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200618 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
619 - closure_count + idx];
620 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200621 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200622 ++funcstack->fs_refcount;
623 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100624 pt->pt_outer.out_stack = &funcstack->fs_ga;
625 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 }
627 }
628 }
629
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200630 for (idx = 0; idx < closure_count; ++idx)
631 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
632 - closure_count + idx]);
633 gap->ga_len -= closure_count;
634 if (gap->ga_len == 0)
635 ga_clear(gap);
636
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 return OK;
638}
639
640/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200641 * Called when a partial is freed or its reference count goes down to one. The
642 * funcstack may be the only reference to the partials in the local variables.
643 * Go over all of them, the funcref and can be freed if all partials
644 * referencing the funcstack have a reference count of one.
645 */
646 void
647funcstack_check_refcount(funcstack_T *funcstack)
648{
649 int i;
650 garray_T *gap = &funcstack->fs_ga;
651 int done = 0;
652
653 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
654 return;
655 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
656 {
657 typval_T *tv = ((typval_T *)gap->ga_data) + i;
658
659 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
660 && tv->vval.v_partial->pt_funcstack == funcstack
661 && tv->vval.v_partial->pt_refcount == 1)
662 ++done;
663 }
664 if (done == funcstack->fs_min_refcount)
665 {
666 typval_T *stack = gap->ga_data;
667
668 // All partials referencing the funcstack have a reference count of
669 // one, thus the funcstack is no longer of use.
670 for (i = 0; i < gap->ga_len; ++i)
671 clear_tv(stack + i);
672 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000673 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200674 vim_free(funcstack);
675 }
676}
677
678/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000679 * For garbage collecting: set references in all variables referenced by
680 * all funcstacks.
681 */
682 int
683set_ref_in_funcstacks(int copyID)
684{
685 funcstack_T *funcstack;
686
687 for (funcstack = first_funcstack; funcstack != NULL;
688 funcstack = funcstack->fs_next)
689 {
690 typval_T *stack = funcstack->fs_ga.ga_data;
691 int i;
692
693 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
694 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
695 return TRUE; // abort
696 }
697 return FALSE;
698}
699
700/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 * Return from the current function.
702 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200703 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200704func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100707 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
709 + ectx->ec_dfunc_idx;
710 int argcount = ufunc_argcount(dfunc->df_ufunc);
711 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200712 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100713 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
714 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200715 funclocal_T *floc;
716#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100717 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
718 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719
Bram Moolenaar12d26532021-02-19 19:13:21 +0100720 if (do_profiling == PROF_YES)
721 {
722 ufunc_T *caller = prev_dfunc->df_ufunc;
723
724 if (dfunc->df_ufunc->uf_profiling
725 || (caller != NULL && caller->uf_profiling))
726 {
727 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
728 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
729 --profile_info_ga.ga_len;
730 }
731 }
732#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000733
734 // No check for uf_refcount being zero, cannot think of a way that would
735 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100736 --dfunc->df_ufunc->uf_calls;
737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200739 entry = estack_pop();
740 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200741 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 if (handle_closure_in_use(ectx, TRUE) == FAIL)
744 return FAIL;
745
746 // Clear the arguments.
747 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
748 clear_tv(STACK_TV(idx));
749
750 // Clear local variables and temp values, but not the return value.
751 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100752 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100754
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100755 // The return value should be on top of the stack. However, when aborting
756 // it may not be there and ec_frame_idx is the top of the stack.
757 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100758 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100759 ret_idx = 0;
760
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200761 if (ectx->ec_outer_ref != NULL)
762 {
763 if (ectx->ec_outer_ref->or_outer_allocated)
764 vim_free(ectx->ec_outer_ref->or_outer);
765 partial_unref(ectx->ec_outer_ref->or_partial);
766 vim_free(ectx->ec_outer_ref);
767 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100768
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100769 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100770 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100771 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
772 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200773 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
774 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200775 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100776 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100777 floc = (void *)STACK_TV(ectx->ec_frame_idx
778 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200779 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100780 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
781 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200782
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100783 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200784 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100785 else
786 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200787 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100788 vim_free(floc);
789 }
790
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100791 if (ret_idx > 0)
792 {
793 // Reset the stack to the position before the call, with a spot for the
794 // return value, moved there from above the frame.
795 ectx->ec_stack.ga_len = top + 1;
796 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
797 }
798 else
799 // Reset the stack to the position before the call.
800 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200801
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100802 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200803 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200804 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805}
806
807#undef STACK_TV
808
809/*
810 * Prepare arguments and rettv for calling a builtin or user function.
811 */
812 static int
813call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
814{
815 int idx;
816 typval_T *tv;
817
818 // Move arguments from bottom of the stack to argvars[] and add terminator.
819 for (idx = 0; idx < argcount; ++idx)
820 argvars[idx] = *STACK_TV_BOT(idx - argcount);
821 argvars[argcount].v_type = VAR_UNKNOWN;
822
823 // Result replaces the arguments on the stack.
824 if (argcount > 0)
825 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200826 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 return FAIL;
828 else
829 ++ectx->ec_stack.ga_len;
830
831 // Default return value is zero.
832 tv = STACK_TV_BOT(-1);
833 tv->v_type = VAR_NUMBER;
834 tv->vval.v_number = 0;
835
836 return OK;
837}
838
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200839// Ugly global to avoid passing the execution context around through many
840// layers.
841static ectx_T *current_ectx = NULL;
842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843/*
844 * Call a builtin function by index.
845 */
846 static int
847call_bfunc(int func_idx, int argcount, ectx_T *ectx)
848{
849 typval_T argvars[MAX_FUNC_ARGS];
850 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100851 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200852 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200853 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854
855 if (call_prepare(argcount, argvars, ectx) == FAIL)
856 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200857 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200859 // Call the builtin function. Set "current_ectx" so that when it
860 // recursively invokes call_def_function() a closure context can be set.
861 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200863 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200864 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865
866 // Clear the arguments.
867 for (idx = 0; idx < argcount; ++idx)
868 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200869
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100870 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200871 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 return OK;
873}
874
875/*
876 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100877 * If the function is compiled this will add a stack frame and set the
878 * instruction pointer at the start of the function.
879 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200880 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100881 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 */
883 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100884call_ufunc(
885 ufunc_T *ufunc,
886 partial_T *pt,
887 int argcount,
888 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200889 isn_T *iptr,
890 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891{
892 typval_T argvars[MAX_FUNC_ARGS];
893 funcexe_T funcexe;
894 int error;
895 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100896 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200897 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898
Bram Moolenaare99d4222021-06-13 14:01:26 +0200899 if (func_needs_compiling(ufunc, compile_type)
900 && compile_def_function(ufunc, FALSE, compile_type, NULL)
901 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200902 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200903 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100904 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100905 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100906 if (error != FCERR_UNKNOWN)
907 {
908 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000909 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100910 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000911 semsg(_(e_not_enough_arguments_for_function_str),
912 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100913 return FAIL;
914 }
915
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100916 // The function has been compiled, can call it quickly. For a function
917 // that was defined later: we can call it directly next time.
918 if (iptr != NULL)
919 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100920 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100921 iptr->isn_type = ISN_DCALL;
922 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
923 iptr->isn_arg.dfunc.cdf_argcount = argcount;
924 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200925 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
928 if (call_prepare(argcount, argvars, ectx) == FAIL)
929 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200930 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000931 funcexe.fe_evaluate = TRUE;
932 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
934 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000936 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937
938 // Clear the arguments.
939 for (idx = 0; idx < argcount; ++idx)
940 clear_tv(&argvars[idx]);
941
942 if (error != FCERR_NONE)
943 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000944 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 return FAIL;
946 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100947 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200948 // Error other than from calling the function itself.
949 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 return OK;
951}
952
953/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100954 * If command modifiers were applied restore them.
955 */
956 static void
957may_restore_cmdmod(funclocal_T *funclocal)
958{
959 if (funclocal->floc_restore_cmdmod)
960 {
961 cmdmod.cmod_filter_regmatch.regprog = NULL;
962 undo_cmdmod(&cmdmod);
963 cmdmod = funclocal->floc_save_cmdmod;
964 funclocal->floc_restore_cmdmod = FALSE;
965 }
966}
967
968/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200969 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
970 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200971 */
972 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200973vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200974{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200975 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200976}
977
978/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 * Execute a function by "name".
980 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100981 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 * Returns FAIL if not found without an error message.
983 */
984 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100985call_by_name(
986 char_u *name,
987 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100988 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200989 isn_T *iptr,
990 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991{
992 ufunc_T *ufunc;
993
994 if (builtin_function(name, -1))
995 {
996 int func_idx = find_internal_func(name);
997
998 if (func_idx < 0)
999 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001000 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 return FAIL;
1002 return call_bfunc(func_idx, argcount, ectx);
1003 }
1004
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001005 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +02001006
1007 if (ufunc == NULL)
1008 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001009 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001010
1011 if (script_autoload(name, TRUE))
1012 // loaded a package, search for the function again
1013 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001014
1015 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001016 return FAIL; // bail out if loading the script caused an error
1017 }
1018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001020 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001021 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001022 {
1023 int i;
1024 typval_T *argv = STACK_TV_BOT(0) - argcount;
1025
1026 // The function can change at runtime, check that the argument
1027 // types are correct.
1028 for (i = 0; i < argcount; ++i)
1029 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001030 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001031
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001032 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001033 type = ufunc->uf_arg_types[i];
1034 else if (ufunc->uf_va_type != NULL)
1035 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001036 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001037 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001038 return FAIL;
1039 }
1040 }
1041
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001042 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044
1045 return FAIL;
1046}
1047
1048 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001049call_partial(
1050 typval_T *tv,
1051 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001052 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001054 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001055 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001057 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001058 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059
1060 if (tv->v_type == VAR_PARTIAL)
1061 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001062 partial_T *pt = tv->vval.v_partial;
1063 int i;
1064
1065 if (pt->pt_argc > 0)
1066 {
1067 // Make space for arguments from the partial, shift the "argcount"
1068 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001069 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001070 return FAIL;
1071 for (i = 1; i <= argcount; ++i)
1072 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1073 ectx->ec_stack.ga_len += pt->pt_argc;
1074 argcount += pt->pt_argc;
1075
1076 // copy the arguments from the partial onto the stack
1077 for (i = 0; i < pt->pt_argc; ++i)
1078 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1079 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001080 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081
1082 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001083 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 name = pt->pt_name;
1086 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001087 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001089 if (name != NULL)
1090 {
1091 char_u fname_buf[FLEN_FIXED + 1];
1092 char_u *tofree = NULL;
1093 int error = FCERR_NONE;
1094 char_u *fname;
1095
1096 // May need to translate <SNR>123_ to K_SNR.
1097 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1098 if (error != FCERR_NONE)
1099 res = FAIL;
1100 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001101 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001102 vim_free(tofree);
1103 }
1104
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001105 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 {
1107 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001108 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001109 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 return FAIL;
1111 }
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001116 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1117 * TRUE.
1118 */
1119 static int
1120error_if_locked(int lock, char *error)
1121{
1122 if (lock & (VAR_LOCKED | VAR_FIXED))
1123 {
1124 emsg(_(error));
1125 return TRUE;
1126 }
1127 return FALSE;
1128}
1129
1130/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001131 * Give an error if "tv" is not a number and return FAIL.
1132 */
1133 static int
1134check_for_number(typval_T *tv)
1135{
1136 if (tv->v_type != VAR_NUMBER)
1137 {
1138 semsg(_(e_expected_str_but_got_str),
1139 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1140 return FAIL;
1141 }
1142 return OK;
1143}
1144
1145/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001146 * Store "tv" in variable "name".
1147 * This is for s: and g: variables.
1148 */
1149 static void
1150store_var(char_u *name, typval_T *tv)
1151{
1152 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001153 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001154
Bram Moolenaard877a572021-04-01 19:42:48 +02001155 if (tv->v_lock)
1156 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001157 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001158 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001159 restore_funccal();
1160}
1161
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001162/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001163 * Convert "tv" to a string.
1164 * Return FAIL if not allowed.
1165 */
1166 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001167do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001168{
1169 if (tv->v_type != VAR_STRING)
1170 {
1171 char_u *str;
1172
1173 if (is_2string_any)
1174 {
1175 switch (tv->v_type)
1176 {
1177 case VAR_SPECIAL:
1178 case VAR_BOOL:
1179 case VAR_NUMBER:
1180 case VAR_FLOAT:
1181 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001182
1183 case VAR_LIST:
1184 if (tolerant)
1185 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001186 char_u *s, *e, *p;
1187 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001188
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001189 ga_init2(&ga, sizeof(char_u *), 1);
1190
1191 // Convert to NL separated items, then
1192 // escape the items and replace the NL with
1193 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001194 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001195 if (str == NULL)
1196 return FAIL;
1197 s = str;
1198 while ((e = vim_strchr(s, '\n')) != NULL)
1199 {
1200 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001201 p = vim_strsave_fnameescape(s,
1202 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001203 if (p != NULL)
1204 {
1205 ga_concat(&ga, p);
1206 ga_concat(&ga, (char_u *)" ");
1207 vim_free(p);
1208 }
1209 s = e + 1;
1210 }
1211 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001212 clear_tv(tv);
1213 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001214 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001215 return OK;
1216 }
1217 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001218 default: to_string_error(tv->v_type);
1219 return FAIL;
1220 }
1221 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001222 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001223 clear_tv(tv);
1224 tv->v_type = VAR_STRING;
1225 tv->vval.v_string = str;
1226 }
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001231 * When the value of "sv" is a null list of dict, allocate it.
1232 */
1233 static void
1234allocate_if_null(typval_T *tv)
1235{
1236 switch (tv->v_type)
1237 {
1238 case VAR_LIST:
1239 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001240 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001241 break;
1242 case VAR_DICT:
1243 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001244 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001245 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001246 case VAR_BLOB:
1247 if (tv->vval.v_blob == NULL)
1248 (void)rettv_blob_alloc(tv);
1249 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001250 default:
1251 break;
1252 }
1253}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001254
Bram Moolenaare7525c52021-01-09 13:20:37 +01001255/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001256 * Return the character "str[index]" where "index" is the character index,
1257 * including composing characters.
1258 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001259 */
1260 char_u *
1261char_from_string(char_u *str, varnumber_T index)
1262{
1263 size_t nbyte = 0;
1264 varnumber_T nchar = index;
1265 size_t slen;
1266
1267 if (str == NULL)
1268 return NULL;
1269 slen = STRLEN(str);
1270
Bram Moolenaarff871402021-03-26 13:34:05 +01001271 // Do the same as for a list: a negative index counts from the end.
1272 // Optimization to check the first byte to be below 0x80 (and no composing
1273 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001274 if (index < 0)
1275 {
1276 int clen = 0;
1277
1278 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001279 {
1280 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1281 ++nbyte;
1282 else if (enc_utf8)
1283 nbyte += utfc_ptr2len(str + nbyte);
1284 else
1285 nbyte += mb_ptr2len(str + nbyte);
1286 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001287 nchar = clen + index;
1288 if (nchar < 0)
1289 // unlike list: index out of range results in empty string
1290 return NULL;
1291 }
1292
1293 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001294 {
1295 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1296 ++nbyte;
1297 else if (enc_utf8)
1298 nbyte += utfc_ptr2len(str + nbyte);
1299 else
1300 nbyte += mb_ptr2len(str + nbyte);
1301 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001302 if (nbyte >= slen)
1303 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001304 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001305}
1306
1307/*
1308 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001309 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001310 * If going over the end return "str_len".
1311 * If "idx" is negative count from the end, -1 is the last character.
1312 * When going over the start return -1.
1313 */
1314 static long
1315char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1316{
1317 varnumber_T nchar = idx;
1318 size_t nbyte = 0;
1319
1320 if (nchar >= 0)
1321 {
1322 while (nchar > 0 && nbyte < str_len)
1323 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001324 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001325 --nchar;
1326 }
1327 }
1328 else
1329 {
1330 nbyte = str_len;
1331 while (nchar < 0 && nbyte > 0)
1332 {
1333 --nbyte;
1334 nbyte -= mb_head_off(str, str + nbyte);
1335 ++nchar;
1336 }
1337 if (nchar < 0)
1338 return -1;
1339 }
1340 return (long)nbyte;
1341}
1342
1343/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001344 * Return the slice "str[first : last]" using character indexes. Composing
1345 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001346 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001347 * Return NULL when the result is empty.
1348 */
1349 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001350string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001351{
1352 long start_byte, end_byte;
1353 size_t slen;
1354
1355 if (str == NULL)
1356 return NULL;
1357 slen = STRLEN(str);
1358 start_byte = char_idx2byte(str, slen, first);
1359 if (start_byte < 0)
1360 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001361 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001362 end_byte = (long)slen;
1363 else
1364 {
1365 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001366 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001367 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001368 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001369 }
1370
1371 if (start_byte >= (long)slen || end_byte <= start_byte)
1372 return NULL;
1373 return vim_strnsave(str + start_byte, end_byte - start_byte);
1374}
1375
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001376/*
1377 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1378 * When "dfunc_idx" is negative don't give an error.
1379 * Returns NULL for an error.
1380 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001381 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001382get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001383{
1384 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001385 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1386 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001387 svar_T *sv;
1388
1389 if (sref->sref_seq != si->sn_script_seq)
1390 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001391 // The script was reloaded after the function was compiled, the
1392 // script_idx may not be valid.
1393 if (dfunc != NULL)
1394 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1395 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001396 return NULL;
1397 }
1398 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001399 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001400 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001401 if (dfunc != NULL)
1402 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001403 return NULL;
1404 }
1405 return sv;
1406}
1407
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001408/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001409 * Function passed to do_cmdline() for splitting a script joined by NL
1410 * characters.
1411 */
1412 static char_u *
1413get_split_sourceline(
1414 int c UNUSED,
1415 void *cookie,
1416 int indent UNUSED,
1417 getline_opt_T options UNUSED)
1418{
1419 source_cookie_T *sp = (source_cookie_T *)cookie;
1420 char_u *p;
1421 char_u *line;
1422
1423 if (*sp->nextline == NUL)
1424 return NULL;
1425 p = vim_strchr(sp->nextline, '\n');
1426 if (p == NULL)
1427 {
1428 line = vim_strsave(sp->nextline);
1429 sp->nextline += STRLEN(sp->nextline);
1430 }
1431 else
1432 {
1433 line = vim_strnsave(sp->nextline, p - sp->nextline);
1434 sp->nextline = p + 1;
1435 }
1436 return line;
1437}
1438
1439/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 * Execute a function by "name".
1441 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001442 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 */
1444 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001445call_eval_func(
1446 char_u *name,
1447 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001448 ectx_T *ectx,
1449 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450{
Bram Moolenaared677f52020-08-12 16:38:10 +02001451 int called_emsg_before = called_emsg;
1452 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001454 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001455 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001457 dictitem_T *v;
1458
1459 v = find_var(name, NULL, FALSE);
1460 if (v == NULL)
1461 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001462 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001463 return FAIL;
1464 }
1465 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1466 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001467 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001468 return FAIL;
1469 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001470 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001472 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473}
1474
1475/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001476 * When a function reference is used, fill a partial with the information
1477 * needed, especially when it is used as a closure.
1478 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001479 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001480fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1481{
1482 pt->pt_func = ufunc;
1483 pt->pt_refcount = 1;
1484
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001485 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001486 {
1487 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1488 + ectx->ec_dfunc_idx;
1489
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001490 // The closure may need to find arguments and local variables in the
1491 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001492 pt->pt_outer.out_stack = &ectx->ec_stack;
1493 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001494 if (ectx->ec_outer_ref != NULL)
1495 {
1496 // The current context already has a context, link to that one.
1497 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1498 if (ectx->ec_outer_ref->or_partial != NULL)
1499 {
1500 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1501 ++pt->pt_outer.out_up_partial->pt_refcount;
1502 }
1503 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001504
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001505 // If this function returns and the closure is still being used, we
1506 // need to make a copy of the context (arguments and local variables).
1507 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001508 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001509 {
1510 vim_free(pt);
1511 return FAIL;
1512 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001513 // Extra variable keeps the count of closures created in the current
1514 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001515 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1516 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1517
1518 ((partial_T **)ectx->ec_funcrefs.ga_data)
1519 [ectx->ec_funcrefs.ga_len] = pt;
1520 ++pt->pt_refcount;
1521 ++ectx->ec_funcrefs.ga_len;
1522 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001523 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001524 return OK;
1525}
1526
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001527/*
1528 * Execute iptr->isn_arg.string as an Ex command.
1529 */
1530 static int
1531exec_command(isn_T *iptr)
1532{
1533 source_cookie_T cookie;
1534
1535 SOURCING_LNUM = iptr->isn_lnum;
1536 // Pass getsourceline to get an error for a missing ":end"
1537 // command.
1538 CLEAR_FIELD(cookie);
1539 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1540 if (do_cmdline(iptr->isn_arg.string,
1541 getsourceline, &cookie,
1542 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1543 || did_emsg)
1544 return FAIL;
1545 return OK;
1546}
1547
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001548// used for v_instr of typval of VAR_INSTR
1549struct instr_S {
1550 ectx_T *instr_ectx;
1551 isn_T *instr_instr;
1552};
1553
Bram Moolenaar4c137212021-04-19 16:48:48 +02001554// used for substitute_instr
1555typedef struct subs_expr_S {
1556 ectx_T *subs_ectx;
1557 isn_T *subs_instr;
1558 int subs_status;
1559} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560
1561// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001562#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563
1564// Get pointer to item at the bottom of the stack, -1 is the bottom.
1565#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001566#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001568// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001569#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001571// Set when calling do_debug().
1572static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001573static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001574
1575/*
1576 * When debugging lookup "name" and return the typeval.
1577 * When not found return NULL.
1578 */
1579 typval_T *
1580lookup_debug_var(char_u *name)
1581{
1582 int idx;
1583 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001584 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001585 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001586 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001587
1588 if (ectx == NULL)
1589 return NULL;
1590 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1591
1592 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001593 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001594 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001595 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001596 return STACK_TV_VAR(idx);
1597 }
1598
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001599 // Go through argument names.
1600 ufunc = dfunc->df_ufunc;
1601 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1602 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1603 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1604 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1605 - varargs_off + idx);
1606 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1607 return STACK_TV(ectx->ec_frame_idx - 1);
1608
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001609 return NULL;
1610}
1611
Bram Moolenaar26a44842021-09-02 18:49:06 +02001612/*
1613 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1614 * breakpoint was set in that function or when there is any expression.
1615 */
1616 int
1617may_break_in_function(ufunc_T *ufunc)
1618{
1619 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1620}
1621
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001622 static void
1623handle_debug(isn_T *iptr, ectx_T *ectx)
1624{
1625 char_u *line;
1626 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1627 + ectx->ec_dfunc_idx)->df_ufunc;
1628 isn_T *ni;
1629 int end_lnum = iptr->isn_lnum;
1630 garray_T ga;
1631 int lnum;
1632
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001633 if (ex_nesting_level > debug_break_level)
1634 {
1635 linenr_T breakpoint;
1636
Bram Moolenaar26a44842021-09-02 18:49:06 +02001637 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001638 return;
1639
1640 // check for the next breakpoint if needed
1641 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001642 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001643 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1644 return;
1645 }
1646
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001647 SOURCING_LNUM = iptr->isn_lnum;
1648 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001649 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001650
1651 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1652 if (ni->isn_type == ISN_DEBUG
1653 || ni->isn_type == ISN_RETURN
1654 || ni->isn_type == ISN_RETURN_VOID)
1655 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001656 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001657 break;
1658 }
1659
1660 if (end_lnum > iptr->isn_lnum)
1661 {
1662 ga_init2(&ga, sizeof(char_u *), 10);
1663 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001664 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001665 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001666
Bram Moolenaar303215d2021-07-07 20:10:43 +02001667 if (p == NULL)
1668 continue; // left over from continuation line
1669 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001670 if (*p == '#')
1671 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001672 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001673 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1674 if (STRNCMP(p, "def ", 4) == 0)
1675 break;
1676 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001677 line = ga_concat_strings(&ga, " ");
1678 vim_free(ga.ga_data);
1679 }
1680 else
1681 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001682
Dominique Pelle6e969552021-06-17 13:53:41 +02001683 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001684 debug_context = NULL;
1685
1686 if (end_lnum > iptr->isn_lnum)
1687 vim_free(line);
1688}
1689
Bram Moolenaar4c137212021-04-19 16:48:48 +02001690/*
1691 * Execute instructions in execution context "ectx".
1692 * Return OK or FAIL;
1693 */
1694 static int
1695exec_instructions(ectx_T *ectx)
1696{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001697 int ret = FAIL;
1698 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001699 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001700
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001701 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001702 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001703
Bram Moolenaarff652882021-05-16 15:24:49 +02001704 // Only catch exceptions in this instruction list.
1705 ectx->ec_trylevel_at_start = trylevel;
1706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 for (;;)
1708 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001709 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001711 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001712
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001713 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001714 {
1715 line_breakcheck();
1716 breakcheck_count = 0;
1717 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001718 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001719 {
1720 // Turn CTRL-C into an exception.
1721 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001722 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001723 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001724 did_throw = TRUE;
1725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001727 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001728 {
1729 // Turn an error message into an exception.
1730 did_emsg = FALSE;
1731 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001732 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001733 did_throw = TRUE;
1734 *msg_list = NULL;
1735 }
1736
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001737 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001739 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001740 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001741 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
1743 // An exception jumps to the first catch, finally, or returns from
1744 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001745 while (index > 0)
1746 {
1747 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001748 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001749 break;
1750 // In the catch and finally block of this try we have to go up
1751 // one level.
1752 --index;
1753 trycmd = NULL;
1754 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001755 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001757 if (trycmd->tcd_in_catch)
1758 {
1759 // exception inside ":catch", jump to ":finally" once
1760 ectx->ec_iidx = trycmd->tcd_finally_idx;
1761 trycmd->tcd_finally_idx = 0;
1762 }
1763 else
1764 // jump to first ":catch"
1765 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001766 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001767 did_throw = FALSE; // don't come back here until :endtry
1768 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 }
1770 else
1771 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001772 // Not inside try or need to return from current functions.
1773 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001774 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001775 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001776 tv = STACK_TV_BOT(0);
1777 tv->v_type = VAR_NUMBER;
1778 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001779 ++ectx->ec_stack.ga_len;
1780 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001782 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001783 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001784 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001785 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 goto done;
1787 }
1788
Bram Moolenaar4c137212021-04-19 16:48:48 +02001789 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001790 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 }
1792 continue;
1793 }
1794
Bram Moolenaar4c137212021-04-19 16:48:48 +02001795 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 switch (iptr->isn_type)
1797 {
1798 // execute Ex command line
1799 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001800 if (exec_command(iptr) == FAIL)
1801 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 break;
1803
Bram Moolenaar20677332021-06-06 17:02:53 +02001804 // execute Ex command line split at NL characters.
1805 case ISN_EXEC_SPLIT:
1806 {
1807 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001808 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001809
1810 SOURCING_LNUM = iptr->isn_lnum;
1811 CLEAR_FIELD(cookie);
1812 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1813 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001814 line = get_split_sourceline(0, &cookie, 0, 0);
1815 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001816 get_split_sourceline, &cookie,
1817 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1818 == FAIL
1819 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001820 {
1821 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001822 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001823 }
1824 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001825 }
1826 break;
1827
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001828 // execute Ex command line that is only a range
1829 case ISN_EXECRANGE:
1830 {
1831 exarg_T ea;
1832 char *error = NULL;
1833
1834 CLEAR_FIELD(ea);
1835 ea.cmdidx = CMD_SIZE;
1836 ea.addr_type = ADDR_LINES;
1837 ea.cmd = iptr->isn_arg.string;
1838 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00001839 if (ea.cmd == NULL)
1840 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001841 if (error == NULL)
1842 error = ex_range_without_command(&ea);
1843 if (error != NULL)
1844 {
1845 SOURCING_LNUM = iptr->isn_lnum;
1846 emsg(error);
1847 goto on_error;
1848 }
1849 }
1850 break;
1851
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001852 // Evaluate an expression with legacy syntax, push it onto the
1853 // stack.
1854 case ISN_LEGACY_EVAL:
1855 {
1856 char_u *arg = iptr->isn_arg.string;
1857 int res;
1858 int save_flags = cmdmod.cmod_flags;
1859
Bram Moolenaar35578162021-08-02 19:10:38 +02001860 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001861 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001862 tv = STACK_TV_BOT(0);
1863 init_tv(tv);
1864 cmdmod.cmod_flags |= CMOD_LEGACY;
1865 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1866 cmdmod.cmod_flags = save_flags;
1867 if (res == FAIL)
1868 goto on_error;
1869 ++ectx->ec_stack.ga_len;
1870 }
1871 break;
1872
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001873 // push typeval VAR_INSTR with instructions to be executed
1874 case ISN_INSTR:
1875 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001876 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001877 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001878 tv = STACK_TV_BOT(0);
1879 tv->vval.v_instr = ALLOC_ONE(instr_T);
1880 if (tv->vval.v_instr == NULL)
1881 goto on_error;
1882 ++ectx->ec_stack.ga_len;
1883
1884 tv->v_type = VAR_INSTR;
1885 tv->vval.v_instr->instr_ectx = ectx;
1886 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1887 }
1888 break;
1889
Bram Moolenaar4c137212021-04-19 16:48:48 +02001890 // execute :substitute with an expression
1891 case ISN_SUBSTITUTE:
1892 {
1893 subs_T *subs = &iptr->isn_arg.subs;
1894 source_cookie_T cookie;
1895 struct subs_expr_S *save_instr = substitute_instr;
1896 struct subs_expr_S subs_instr;
1897 int res;
1898
1899 subs_instr.subs_ectx = ectx;
1900 subs_instr.subs_instr = subs->subs_instr;
1901 subs_instr.subs_status = OK;
1902 substitute_instr = &subs_instr;
1903
1904 SOURCING_LNUM = iptr->isn_lnum;
1905 // This is very much like ISN_EXEC
1906 CLEAR_FIELD(cookie);
1907 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1908 res = do_cmdline(subs->subs_cmd,
1909 getsourceline, &cookie,
1910 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1911 substitute_instr = save_instr;
1912
1913 if (res == FAIL || did_emsg
1914 || subs_instr.subs_status == FAIL)
1915 goto on_error;
1916 }
1917 break;
1918
1919 case ISN_FINISH:
1920 goto done;
1921
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001922 case ISN_REDIRSTART:
1923 // create a dummy entry for var_redir_str()
1924 if (alloc_redir_lval() == FAIL)
1925 goto on_error;
1926
1927 // The output is stored in growarray "redir_ga" until
1928 // redirection ends.
1929 init_redir_ga();
1930 redir_vname = 1;
1931 break;
1932
1933 case ISN_REDIREND:
1934 {
1935 char_u *res = get_clear_redir_ga();
1936
1937 // End redirection, put redirected text on the stack.
1938 clear_redir_lval();
1939 redir_vname = 0;
1940
Bram Moolenaar35578162021-08-02 19:10:38 +02001941 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001942 {
1943 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001944 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001945 }
1946 tv = STACK_TV_BOT(0);
1947 tv->v_type = VAR_STRING;
1948 tv->vval.v_string = res;
1949 ++ectx->ec_stack.ga_len;
1950 }
1951 break;
1952
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001953 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001954#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001955 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1956 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001957#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001958 break;
1959
1960 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001961#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001962 {
1963 exarg_T ea;
1964 int res;
1965
1966 CLEAR_FIELD(ea);
1967 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1968 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1969 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1970 --ectx->ec_stack.ga_len;
1971 tv = STACK_TV_BOT(0);
1972 res = cexpr_core(&ea, tv);
1973 clear_tv(tv);
1974 if (res == FAIL)
1975 goto on_error;
1976 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001977#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001978 break;
1979
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001980 // execute Ex command from pieces on the stack
1981 case ISN_EXECCONCAT:
1982 {
1983 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001984 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001985 int pass;
1986 int i;
1987 char_u *cmd = NULL;
1988 char_u *str;
1989
1990 for (pass = 1; pass <= 2; ++pass)
1991 {
1992 for (i = 0; i < count; ++i)
1993 {
1994 tv = STACK_TV_BOT(i - count);
1995 str = tv->vval.v_string;
1996 if (str != NULL && *str != NUL)
1997 {
1998 if (pass == 2)
1999 STRCPY(cmd + len, str);
2000 len += STRLEN(str);
2001 }
2002 if (pass == 2)
2003 clear_tv(tv);
2004 }
2005 if (pass == 1)
2006 {
2007 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002008 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002009 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002010 len = 0;
2011 }
2012 }
2013
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002014 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002015 do_cmdline_cmd(cmd);
2016 vim_free(cmd);
2017 }
2018 break;
2019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 // execute :echo {string} ...
2021 case ISN_ECHO:
2022 {
2023 int count = iptr->isn_arg.echo.echo_count;
2024 int atstart = TRUE;
2025 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002026 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027
2028 for (idx = 0; idx < count; ++idx)
2029 {
2030 tv = STACK_TV_BOT(idx - count);
2031 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2032 &atstart, &needclr);
2033 clear_tv(tv);
2034 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002035 if (needclr)
2036 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002037 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 }
2039 break;
2040
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002041 // :execute {string} ...
2042 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002043 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002044 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002045 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002046 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002047 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002048 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002049 {
2050 int count = iptr->isn_arg.number;
2051 garray_T ga;
2052 char_u buf[NUMBUFLEN];
2053 char_u *p;
2054 int len;
2055 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002056 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002057
2058 ga_init2(&ga, 1, 80);
2059 for (idx = 0; idx < count; ++idx)
2060 {
2061 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002062 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002063 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002064 if (tv->v_type == VAR_CHANNEL
2065 || tv->v_type == VAR_JOB)
2066 {
2067 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002068 semsg(_(e_using_invalid_value_as_string_str),
2069 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002070 break;
2071 }
2072 else
2073 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002074 }
2075 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002076 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002077
2078 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002079 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002080 failed = TRUE;
2081 else
2082 {
2083 if (ga.ga_len > 0)
2084 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2085 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2086 ga.ga_len += len;
2087 }
2088 clear_tv(tv);
2089 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002090 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002091 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002092 {
2093 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002094 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002095 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002096
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002097 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002098 {
2099 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002100 {
2101 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002102 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002103 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002104 {
2105 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002106 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002107 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002108 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002109 else
2110 {
2111 msg_sb_eol();
2112 if (iptr->isn_type == ISN_ECHOMSG)
2113 {
2114 msg_attr(ga.ga_data, echo_attr);
2115 out_flush();
2116 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002117 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2118 {
2119 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2120 TRUE);
2121 ui_write((char_u *)"\r\n", 2, TRUE);
2122 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002123 else
2124 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002125 SOURCING_LNUM = iptr->isn_lnum;
2126 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002127 }
2128 }
2129 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002130 ga_clear(&ga);
2131 }
2132 break;
2133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 // load local variable or argument
2135 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002136 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002137 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002139 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 break;
2141
2142 // load v: variable
2143 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002144 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002145 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002147 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 break;
2149
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002150 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 case ISN_LOADSCRIPT:
2152 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002153 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 svar_T *sv;
2155
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002156 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002157 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002158 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002159 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002160 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002161 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002163 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 }
2165 break;
2166
2167 // load s: variable in old script
2168 case ISN_LOADS:
2169 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002170 hashtab_T *ht = &SCRIPT_VARS(
2171 iptr->isn_arg.loadstore.ls_sid);
2172 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 if (di == NULL)
2176 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002177 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002178 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002179 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 }
2181 else
2182 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002183 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002184 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002186 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 }
2188 }
2189 break;
2190
Bram Moolenaard3aac292020-04-19 14:32:17 +02002191 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002193 case ISN_LOADB:
2194 case ISN_LOADW:
2195 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002197 dictitem_T *di = NULL;
2198 hashtab_T *ht = NULL;
2199 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002200
Bram Moolenaard3aac292020-04-19 14:32:17 +02002201 switch (iptr->isn_type)
2202 {
2203 case ISN_LOADG:
2204 ht = get_globvar_ht();
2205 namespace = 'g';
2206 break;
2207 case ISN_LOADB:
2208 ht = &curbuf->b_vars->dv_hashtab;
2209 namespace = 'b';
2210 break;
2211 case ISN_LOADW:
2212 ht = &curwin->w_vars->dv_hashtab;
2213 namespace = 'w';
2214 break;
2215 case ISN_LOADT:
2216 ht = &curtab->tp_vars->dv_hashtab;
2217 namespace = 't';
2218 break;
2219 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002220 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002221 }
2222 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 if (di == NULL)
2225 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002226 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002227 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002228 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002229 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 }
2231 else
2232 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002233 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002234 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002236 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 }
2238 }
2239 break;
2240
Bram Moolenaar03290b82020-12-19 16:30:44 +01002241 // load autoload variable
2242 case ISN_LOADAUTO:
2243 {
2244 char_u *name = iptr->isn_arg.string;
2245
Bram Moolenaar35578162021-08-02 19:10:38 +02002246 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002247 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002249 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002250 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002251 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002252 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002253 }
2254 break;
2255
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002256 // load g:/b:/w:/t: namespace
2257 case ISN_LOADGDICT:
2258 case ISN_LOADBDICT:
2259 case ISN_LOADWDICT:
2260 case ISN_LOADTDICT:
2261 {
2262 dict_T *d = NULL;
2263
2264 switch (iptr->isn_type)
2265 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002266 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2267 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2268 case ISN_LOADWDICT: d = curwin->w_vars; break;
2269 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002270 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002271 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002272 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002273 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002274 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002275 tv = STACK_TV_BOT(0);
2276 tv->v_type = VAR_DICT;
2277 tv->v_lock = 0;
2278 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002279 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002280 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002281 }
2282 break;
2283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 // load &option
2285 case ISN_LOADOPT:
2286 {
2287 typval_T optval;
2288 char_u *name = iptr->isn_arg.string;
2289
Bram Moolenaara8c17702020-04-01 21:17:24 +02002290 // This is not expected to fail, name is checked during
2291 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002292 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002293 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002294 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002297 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 }
2299 break;
2300
2301 // load $ENV
2302 case ISN_LOADENV:
2303 {
2304 typval_T optval;
2305 char_u *name = iptr->isn_arg.string;
2306
Bram Moolenaar35578162021-08-02 19:10:38 +02002307 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002308 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002309 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002310 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002312 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 }
2314 break;
2315
2316 // load @register
2317 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002318 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002319 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 tv = STACK_TV_BOT(0);
2321 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002322 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002323 // This may result in NULL, which should be equivalent to an
2324 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 tv->vval.v_string = get_reg_contents(
2326 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002327 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 break;
2329
2330 // store local variable
2331 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002332 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 tv = STACK_TV_VAR(iptr->isn_arg.number);
2334 clear_tv(tv);
2335 *tv = *STACK_TV_BOT(0);
2336 break;
2337
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002338 // store s: variable in old script
2339 case ISN_STORES:
2340 {
2341 hashtab_T *ht = &SCRIPT_VARS(
2342 iptr->isn_arg.loadstore.ls_sid);
2343 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002344 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002345
Bram Moolenaar4c137212021-04-19 16:48:48 +02002346 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002347 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002348 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002349 else
2350 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002351 SOURCING_LNUM = iptr->isn_lnum;
2352 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002353 {
2354 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002355 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002356 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002357 clear_tv(&di->di_tv);
2358 di->di_tv = *STACK_TV_BOT(0);
2359 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002360 }
2361 break;
2362
2363 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 case ISN_STORESCRIPT:
2365 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002366 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2367 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002369 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002370 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002371 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002372 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002373
2374 // "const" and "final" are checked at compile time, locking
2375 // the value needs to be checked here.
2376 SOURCING_LNUM = iptr->isn_lnum;
2377 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002378 {
2379 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002380 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002381 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 clear_tv(sv->sv_tv);
2384 *sv->sv_tv = *STACK_TV_BOT(0);
2385 }
2386 break;
2387
2388 // store option
2389 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002390 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002392 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
2393 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 long n = 0;
2395 char_u *s = NULL;
2396 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002397 char_u numbuf[NUMBUFLEN];
2398 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399
Bram Moolenaar4c137212021-04-19 16:48:48 +02002400 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 tv = STACK_TV_BOT(0);
2402 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002403 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002405 if (s == NULL)
2406 s = (char_u *)"";
2407 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002408 else if (iptr->isn_type == ISN_STOREFUNCOPT)
2409 {
2410 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002411 // If the option can be set to a function reference or
2412 // a lambda and the passed value is a function
2413 // reference, then convert it to the name (string) of
2414 // the function reference.
2415 s = tv2string(tv, &tofree, numbuf, 0);
2416 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002417 {
2418 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002419 goto on_error;
2420 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002423 // must be VAR_NUMBER, CHECKTYPE makes sure
2424 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002425 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002426 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002427 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 if (msg != NULL)
2429 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002430 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002432 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 }
2435 break;
2436
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002437 // store $ENV
2438 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002439 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002440 tv = STACK_TV_BOT(0);
2441 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2442 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002443 break;
2444
2445 // store @r
2446 case ISN_STOREREG:
2447 {
2448 int reg = iptr->isn_arg.number;
2449
Bram Moolenaar4c137212021-04-19 16:48:48 +02002450 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002451 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002452 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002453 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002454 }
2455 break;
2456
2457 // store v: variable
2458 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002459 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002460 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2461 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002462 // should not happen, type is checked when compiling
2463 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002464 break;
2465
Bram Moolenaard3aac292020-04-19 14:32:17 +02002466 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002468 case ISN_STOREB:
2469 case ISN_STOREW:
2470 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002472 dictitem_T *di;
2473 hashtab_T *ht;
2474 char_u *name = iptr->isn_arg.string + 2;
2475
Bram Moolenaard3aac292020-04-19 14:32:17 +02002476 switch (iptr->isn_type)
2477 {
2478 case ISN_STOREG:
2479 ht = get_globvar_ht();
2480 break;
2481 case ISN_STOREB:
2482 ht = &curbuf->b_vars->dv_hashtab;
2483 break;
2484 case ISN_STOREW:
2485 ht = &curwin->w_vars->dv_hashtab;
2486 break;
2487 case ISN_STORET:
2488 ht = &curtab->tp_vars->dv_hashtab;
2489 break;
2490 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002491 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493
Bram Moolenaar4c137212021-04-19 16:48:48 +02002494 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002495 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002497 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 else
2499 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002500 SOURCING_LNUM = iptr->isn_lnum;
2501 if (var_check_permission(di, name) == FAIL)
2502 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 clear_tv(&di->di_tv);
2504 di->di_tv = *STACK_TV_BOT(0);
2505 }
2506 }
2507 break;
2508
Bram Moolenaar03290b82020-12-19 16:30:44 +01002509 // store an autoload variable
2510 case ISN_STOREAUTO:
2511 SOURCING_LNUM = iptr->isn_lnum;
2512 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2513 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002514 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002515 break;
2516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 // store number in local variable
2518 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002519 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 clear_tv(tv);
2521 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002522 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 break;
2524
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002525 // store value in list or dict variable
2526 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002527 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002528 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002529 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002530 typval_T *tv_dest = STACK_TV_BOT(-1);
2531 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002532
Bram Moolenaar752fc692021-01-04 21:57:11 +01002533 // Stack contains:
2534 // -3 value to be stored
2535 // -2 index
2536 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002537 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002538 SOURCING_LNUM = iptr->isn_lnum;
2539 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002540 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002541 dest_type = tv_dest->v_type;
2542 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002543 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002544 else if (dest_type == VAR_LIST
2545 && tv_idx->v_type != VAR_NUMBER)
2546 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002547 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002548 status = FAIL;
2549 }
2550 }
2551 else if (dest_type != tv_dest->v_type)
2552 {
2553 // just in case, should be OK
2554 semsg(_(e_expected_str_but_got_str),
2555 vartype_name(dest_type),
2556 vartype_name(tv_dest->v_type));
2557 status = FAIL;
2558 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002559
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002560 if (status == OK && dest_type == VAR_LIST)
2561 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002562 long lidx = (long)tv_idx->vval.v_number;
2563 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002564
2565 if (list == NULL)
2566 {
2567 emsg(_(e_list_not_set));
2568 goto on_error;
2569 }
2570 if (lidx < 0 && list->lv_len + lidx >= 0)
2571 // negative index is relative to the end
2572 lidx = list->lv_len + lidx;
2573 if (lidx < 0 || lidx > list->lv_len)
2574 {
2575 semsg(_(e_listidx), lidx);
2576 goto on_error;
2577 }
2578 if (lidx < list->lv_len)
2579 {
2580 listitem_T *li = list_find(list, lidx);
2581
2582 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002583 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002584 goto on_error;
2585 // overwrite existing list item
2586 clear_tv(&li->li_tv);
2587 li->li_tv = *tv;
2588 }
2589 else
2590 {
2591 if (error_if_locked(list->lv_lock,
2592 e_cannot_change_list))
2593 goto on_error;
2594 // append to list, only fails when out of memory
2595 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002596 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002597 clear_tv(tv);
2598 }
2599 }
2600 else if (status == OK && dest_type == VAR_DICT)
2601 {
2602 char_u *key = tv_idx->vval.v_string;
2603 dict_T *dict = tv_dest->vval.v_dict;
2604 dictitem_T *di;
2605
2606 SOURCING_LNUM = iptr->isn_lnum;
2607 if (dict == NULL)
2608 {
2609 emsg(_(e_dictionary_not_set));
2610 goto on_error;
2611 }
2612 if (key == NULL)
2613 key = (char_u *)"";
2614 di = dict_find(dict, key, -1);
2615 if (di != NULL)
2616 {
2617 if (error_if_locked(di->di_tv.v_lock,
2618 e_cannot_change_dict_item))
2619 goto on_error;
2620 // overwrite existing value
2621 clear_tv(&di->di_tv);
2622 di->di_tv = *tv;
2623 }
2624 else
2625 {
2626 if (error_if_locked(dict->dv_lock,
2627 e_cannot_change_dict))
2628 goto on_error;
2629 // add to dict, only fails when out of memory
2630 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002631 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002632 clear_tv(tv);
2633 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002634 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002635 else if (status == OK && dest_type == VAR_BLOB)
2636 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002637 long lidx = (long)tv_idx->vval.v_number;
2638 blob_T *blob = tv_dest->vval.v_blob;
2639 varnumber_T nr;
2640 int error = FALSE;
2641 int len;
2642
2643 if (blob == NULL)
2644 {
2645 emsg(_(e_blob_not_set));
2646 goto on_error;
2647 }
2648 len = blob_len(blob);
2649 if (lidx < 0 && len + lidx >= 0)
2650 // negative index is relative to the end
2651 lidx = len + lidx;
2652
2653 // Can add one byte at the end.
2654 if (lidx < 0 || lidx > len)
2655 {
2656 semsg(_(e_blobidx), lidx);
2657 goto on_error;
2658 }
2659 if (value_check_lock(blob->bv_lock,
2660 (char_u *)"blob", FALSE))
2661 goto on_error;
2662 nr = tv_get_number_chk(tv, &error);
2663 if (error)
2664 goto on_error;
2665 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002666 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002667 else
2668 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002669 status = FAIL;
2670 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002671 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002672
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002673 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002674 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002675 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002676 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002677 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002678 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002679 goto on_error;
2680 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002681 }
2682 break;
2683
Bram Moolenaar68452172021-04-12 21:21:02 +02002684 // store value in blob range
2685 case ISN_STORERANGE:
2686 {
2687 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2688 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2689 typval_T *tv_dest = STACK_TV_BOT(-1);
2690 int status = OK;
2691
2692 // Stack contains:
2693 // -4 value to be stored
2694 // -3 first index or "none"
2695 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002696 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002697 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002698 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002699 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002700 long n1;
2701 long n2;
2702 int error = FALSE;
2703
2704 SOURCING_LNUM = iptr->isn_lnum;
2705 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2706 if (error)
2707 status = FAIL;
2708 else
2709 {
2710 if (tv_idx2->v_type == VAR_SPECIAL
2711 && tv_idx2->vval.v_number == VVAL_NONE)
2712 n2 = list_len(tv_dest->vval.v_list) - 1;
2713 else
2714 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2715 if (error)
2716 status = FAIL;
2717 else
2718 {
2719 listitem_T *li1 = check_range_index_one(
2720 tv_dest->vval.v_list, &n1, FALSE);
2721
2722 if (li1 == NULL)
2723 status = FAIL;
2724 else
2725 {
2726 status = check_range_index_two(
2727 tv_dest->vval.v_list,
2728 &n1, li1, &n2, FALSE);
2729 if (status != FAIL)
2730 status = list_assign_range(
2731 tv_dest->vval.v_list,
2732 tv->vval.v_list,
2733 n1,
2734 n2,
2735 tv_idx2->v_type == VAR_SPECIAL,
2736 (char_u *)"=",
2737 (char_u *)"[unknown]");
2738 }
2739 }
2740 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002741 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002742 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002743 {
2744 varnumber_T n1;
2745 varnumber_T n2;
2746 int error = FALSE;
2747
2748 n1 = tv_get_number_chk(tv_idx1, &error);
2749 if (error)
2750 status = FAIL;
2751 else
2752 {
2753 if (tv_idx2->v_type == VAR_SPECIAL
2754 && tv_idx2->vval.v_number == VVAL_NONE)
2755 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2756 else
2757 n2 = tv_get_number_chk(tv_idx2, &error);
2758 if (error)
2759 status = FAIL;
2760 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002761 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002762 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002763
2764 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002765 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002766 || check_blob_range(bloblen,
2767 n1, n2, FALSE) == FAIL)
2768 status = FAIL;
2769 else
2770 status = blob_set_range(
2771 tv_dest->vval.v_blob, n1, n2, tv);
2772 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002773 }
2774 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002775 else
2776 {
2777 status = FAIL;
2778 emsg(_(e_blob_required));
2779 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002780
2781 clear_tv(tv_idx1);
2782 clear_tv(tv_idx2);
2783 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002784 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002785 clear_tv(tv);
2786
2787 if (status == FAIL)
2788 goto on_error;
2789 }
2790 break;
2791
Bram Moolenaar0186e582021-01-10 18:33:11 +01002792 // load or store variable or argument from outer scope
2793 case ISN_LOADOUTER:
2794 case ISN_STOREOUTER:
2795 {
2796 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002797 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2798 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002799
2800 while (depth > 1 && outer != NULL)
2801 {
2802 outer = outer->out_up;
2803 --depth;
2804 }
2805 if (outer == NULL)
2806 {
2807 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00002808 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
2809 || ectx->ec_outer_ref == NULL)
2810 // Possibly :def function called from legacy
2811 // context.
2812 emsg(_(e_closure_called_from_invalid_context));
2813 else
2814 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002815 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002816 }
2817 tv = ((typval_T *)outer->out_stack->ga_data)
2818 + outer->out_frame_idx + STACK_FRAME_SIZE
2819 + iptr->isn_arg.outer.outer_idx;
2820 if (iptr->isn_type == ISN_LOADOUTER)
2821 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002822 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002823 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002824 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002825 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002826 }
2827 else
2828 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002829 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002830 clear_tv(tv);
2831 *tv = *STACK_TV_BOT(0);
2832 }
2833 }
2834 break;
2835
Bram Moolenaar752fc692021-01-04 21:57:11 +01002836 // unlet item in list or dict variable
2837 case ISN_UNLETINDEX:
2838 {
2839 typval_T *tv_idx = STACK_TV_BOT(-2);
2840 typval_T *tv_dest = STACK_TV_BOT(-1);
2841 int status = OK;
2842
2843 // Stack contains:
2844 // -2 index
2845 // -1 dict or list
2846 if (tv_dest->v_type == VAR_DICT)
2847 {
2848 // unlet a dict item, index must be a string
2849 if (tv_idx->v_type != VAR_STRING)
2850 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002851 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002852 semsg(_(e_expected_str_but_got_str),
2853 vartype_name(VAR_STRING),
2854 vartype_name(tv_idx->v_type));
2855 status = FAIL;
2856 }
2857 else
2858 {
2859 dict_T *d = tv_dest->vval.v_dict;
2860 char_u *key = tv_idx->vval.v_string;
2861 dictitem_T *di = NULL;
2862
Bram Moolenaar71b76852021-12-17 20:15:38 +00002863 if (d != NULL && value_check_lock(
2864 d->dv_lock, NULL, FALSE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002865 status = FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002866 else
2867 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002868 SOURCING_LNUM = iptr->isn_lnum;
2869 if (key == NULL)
2870 key = (char_u *)"";
2871 if (d != NULL)
2872 di = dict_find(d, key, (int)STRLEN(key));
2873 if (di == NULL)
2874 {
2875 // NULL dict is equivalent to empty dict
2876 semsg(_(e_dictkey), key);
2877 status = FAIL;
2878 }
2879 else if (var_check_fixed(di->di_flags,
2880 NULL, FALSE)
2881 || var_check_ro(di->di_flags,
2882 NULL, FALSE))
2883 status = FAIL;
2884 else
2885 dictitem_remove(d, di);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002886 }
2887 }
2888 }
2889 else if (tv_dest->v_type == VAR_LIST)
2890 {
2891 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002892 SOURCING_LNUM = iptr->isn_lnum;
2893 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002894 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002895 status = FAIL;
2896 }
2897 else
2898 {
2899 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002900 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002901
Bram Moolenaar422085f2021-12-17 20:36:15 +00002902 if (l != NULL && value_check_lock(
2903 l->lv_lock, NULL, FALSE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002904 status = FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002905 else
Bram Moolenaar422085f2021-12-17 20:36:15 +00002906 {
2907 listitem_T *li = list_find(l, n);
2908
2909 if (li == NULL)
2910 {
2911 SOURCING_LNUM = iptr->isn_lnum;
2912 semsg(_(e_listidx), n);
2913 status = FAIL;
2914 }
2915 else if (value_check_lock(li->li_tv.v_lock,
2916 NULL, FALSE))
2917 status = FAIL;
2918 else
2919 listitem_remove(l, li);
2920 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002921 }
2922 }
2923 else
2924 {
2925 status = FAIL;
2926 semsg(_(e_cannot_index_str),
2927 vartype_name(tv_dest->v_type));
2928 }
2929
2930 clear_tv(tv_idx);
2931 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002932 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002933 if (status == FAIL)
2934 goto on_error;
2935 }
2936 break;
2937
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002938 // unlet range of items in list variable
2939 case ISN_UNLETRANGE:
2940 {
2941 // Stack contains:
2942 // -3 index1
2943 // -2 index2
2944 // -1 dict or list
2945 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2946 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2947 typval_T *tv_dest = STACK_TV_BOT(-1);
2948 int status = OK;
2949
2950 if (tv_dest->v_type == VAR_LIST)
2951 {
2952 // indexes must be a number
2953 SOURCING_LNUM = iptr->isn_lnum;
2954 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002955 || (tv_idx2->v_type != VAR_SPECIAL
2956 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002957 {
2958 status = FAIL;
2959 }
2960 else
2961 {
2962 list_T *l = tv_dest->vval.v_list;
2963 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002964 long n2 = tv_idx2->v_type == VAR_SPECIAL
2965 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002966 listitem_T *li;
2967
2968 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002969 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002970 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002971 else
2972 {
2973 if (n1 < 0)
2974 n1 = list_idx_of_item(l, li);
2975 if (n2 < 0)
2976 {
2977 listitem_T *li2 = list_find(l, n2);
2978
2979 if (li2 == NULL)
2980 status = FAIL;
2981 else
2982 n2 = list_idx_of_item(l, li2);
2983 }
2984 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002985 && tv_idx2->v_type != VAR_SPECIAL
2986 && n2 < n1)
2987 {
2988 semsg(_(e_listidx), n2);
2989 status = FAIL;
2990 }
2991 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002992 && list_unlet_range(l, li, NULL, n1,
2993 tv_idx2->v_type != VAR_SPECIAL, n2)
2994 == FAIL)
2995 status = FAIL;
2996 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002997 }
2998 }
2999 else
3000 {
3001 status = FAIL;
3002 SOURCING_LNUM = iptr->isn_lnum;
3003 semsg(_(e_cannot_index_str),
3004 vartype_name(tv_dest->v_type));
3005 }
3006
3007 clear_tv(tv_idx1);
3008 clear_tv(tv_idx2);
3009 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003010 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003011 if (status == FAIL)
3012 goto on_error;
3013 }
3014 break;
3015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 // push constant
3017 case ISN_PUSHNR:
3018 case ISN_PUSHBOOL:
3019 case ISN_PUSHSPEC:
3020 case ISN_PUSHF:
3021 case ISN_PUSHS:
3022 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003023 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003024 case ISN_PUSHCHANNEL:
3025 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003026 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003027 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003029 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003030 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 switch (iptr->isn_type)
3032 {
3033 case ISN_PUSHNR:
3034 tv->v_type = VAR_NUMBER;
3035 tv->vval.v_number = iptr->isn_arg.number;
3036 break;
3037 case ISN_PUSHBOOL:
3038 tv->v_type = VAR_BOOL;
3039 tv->vval.v_number = iptr->isn_arg.number;
3040 break;
3041 case ISN_PUSHSPEC:
3042 tv->v_type = VAR_SPECIAL;
3043 tv->vval.v_number = iptr->isn_arg.number;
3044 break;
3045#ifdef FEAT_FLOAT
3046 case ISN_PUSHF:
3047 tv->v_type = VAR_FLOAT;
3048 tv->vval.v_float = iptr->isn_arg.fnumber;
3049 break;
3050#endif
3051 case ISN_PUSHBLOB:
3052 blob_copy(iptr->isn_arg.blob, tv);
3053 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003054 case ISN_PUSHFUNC:
3055 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003056 if (iptr->isn_arg.string == NULL)
3057 tv->vval.v_string = NULL;
3058 else
3059 tv->vval.v_string =
3060 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003061 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003062 case ISN_PUSHCHANNEL:
3063#ifdef FEAT_JOB_CHANNEL
3064 tv->v_type = VAR_CHANNEL;
3065 tv->vval.v_channel = iptr->isn_arg.channel;
3066 if (tv->vval.v_channel != NULL)
3067 ++tv->vval.v_channel->ch_refcount;
3068#endif
3069 break;
3070 case ISN_PUSHJOB:
3071#ifdef FEAT_JOB_CHANNEL
3072 tv->v_type = VAR_JOB;
3073 tv->vval.v_job = iptr->isn_arg.job;
3074 if (tv->vval.v_job != NULL)
3075 ++tv->vval.v_job->jv_refcount;
3076#endif
3077 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 default:
3079 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003080 tv->vval.v_string = vim_strsave(
3081 iptr->isn_arg.string == NULL
3082 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 }
3084 break;
3085
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003086 case ISN_UNLET:
3087 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3088 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003089 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003090 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003091 case ISN_UNLETENV:
3092 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3093 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003094
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003095 case ISN_LOCKUNLOCK:
3096 {
3097 typval_T *lval_root_save = lval_root;
3098 int res;
3099
3100 // Stack has the local variable, argument the whole :lock
3101 // or :unlock command, like ISN_EXEC.
3102 --ectx->ec_stack.ga_len;
3103 lval_root = STACK_TV_BOT(0);
3104 res = exec_command(iptr);
3105 clear_tv(lval_root);
3106 lval_root = lval_root_save;
3107 if (res == FAIL)
3108 goto on_error;
3109 }
3110 break;
3111
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003112 case ISN_LOCKCONST:
3113 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3114 break;
3115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 // create a list from items on the stack; uses a single allocation
3117 // for the list header and the items
3118 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003119 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003120 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 break;
3122
3123 // create a dict from items on the stack
3124 case ISN_NEWDICT:
3125 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003126 int count = iptr->isn_arg.number;
3127 dict_T *dict = dict_alloc();
3128 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003129 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003132 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003133 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 for (idx = 0; idx < count; ++idx)
3135 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003136 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003138 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003139 key = tv->vval.v_string == NULL
3140 ? (char_u *)"" : tv->vval.v_string;
3141 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003142 if (item != NULL)
3143 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003144 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003145 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003146 dict_unref(dict);
3147 goto on_error;
3148 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003149 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003151 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003152 {
3153 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003154 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3157 item->di_tv.v_lock = 0;
3158 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003159 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003160 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003161 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003162 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 }
3165
3166 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003167 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003168 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003169 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003171 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 tv = STACK_TV_BOT(-1);
3173 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003174 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 tv->vval.v_dict = dict;
3176 ++dict->dv_refcount;
3177 }
3178 break;
3179
3180 // call a :def function
3181 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003182 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003183 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3184 NULL,
3185 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003186 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003187 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 break;
3189
3190 // call a builtin function
3191 case ISN_BCALL:
3192 SOURCING_LNUM = iptr->isn_lnum;
3193 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3194 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003196 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 break;
3198
3199 // call a funcref or partial
3200 case ISN_PCALL:
3201 {
3202 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3203 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003204 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205
3206 SOURCING_LNUM = iptr->isn_lnum;
3207 if (pfunc->cpf_top)
3208 {
3209 // funcref is above the arguments
3210 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3211 }
3212 else
3213 {
3214 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003215 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003216 partial_tv = *STACK_TV_BOT(0);
3217 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003219 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003220 if (tv == &partial_tv)
3221 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003223 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
3225 break;
3226
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003227 case ISN_PCALL_END:
3228 // PCALL finished, arguments have been consumed and replaced by
3229 // the return value. Now clear the funcref from the stack,
3230 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003231 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003232 clear_tv(STACK_TV_BOT(-1));
3233 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3234 break;
3235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 // call a user defined function or funcref/partial
3237 case ISN_UCALL:
3238 {
3239 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3240
3241 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003242 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003243 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003244 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 }
3246 break;
3247
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003248 // return from a :def function call without a value
3249 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003250 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003251 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003252 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003253 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003254 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003255 tv->vval.v_number = 0;
3256 tv->v_lock = 0;
3257 // FALLTHROUGH
3258
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003259 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 case ISN_RETURN:
3261 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003262 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003263 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003264
3265 if (trystack->ga_len > 0)
3266 trycmd = ((trycmd_T *)trystack->ga_data)
3267 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003268 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003269 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003271 // jump to ":finally" or ":endtry"
3272 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003273 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003274 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003275 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 trycmd->tcd_return = TRUE;
3277 }
3278 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003279 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 }
3281 break;
3282
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003283 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 case ISN_FUNCREF:
3285 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003286 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003287 ufunc_T *ufunc;
3288 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003291 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003292 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003293 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003294 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003295 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003296 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003297 if (funcref->fr_func_name == NULL)
3298 {
3299 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3300 + funcref->fr_dfunc_idx;
3301
3302 ufunc = pt_dfunc->df_ufunc;
3303 }
3304 else
3305 {
3306 ufunc = find_func(funcref->fr_func_name, FALSE, NULL);
3307 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003308 if (ufunc == NULL)
3309 {
3310 SOURCING_LNUM = iptr->isn_lnum;
3311 emsg(_(e_function_reference_invalid));
3312 goto theend;
3313 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003314 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003315 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003317 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 tv->vval.v_partial = pt;
3319 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003320 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 }
3322 break;
3323
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003324 // Create a global function from a lambda.
3325 case ISN_NEWFUNC:
3326 {
3327 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3328
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003329 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003330 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003331 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003332 }
3333 break;
3334
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003335 // List functions
3336 case ISN_DEF:
3337 if (iptr->isn_arg.string == NULL)
3338 list_functions(NULL);
3339 else
3340 {
3341 exarg_T ea;
3342
3343 CLEAR_FIELD(ea);
3344 ea.cmd = ea.arg = iptr->isn_arg.string;
3345 define_function(&ea, NULL);
3346 }
3347 break;
3348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 // jump if a condition is met
3350 case ISN_JUMP:
3351 {
3352 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003353 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 int jump = TRUE;
3355
3356 if (when != JUMP_ALWAYS)
3357 {
3358 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003359 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003360 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003361 || when == JUMP_IF_COND_TRUE)
3362 {
3363 SOURCING_LNUM = iptr->isn_lnum;
3364 jump = tv_get_bool_chk(tv, &error);
3365 if (error)
3366 goto on_error;
3367 }
3368 else
3369 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003371 || when == JUMP_AND_KEEP_IF_FALSE
3372 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003374 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 {
3376 // drop the value from the stack
3377 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003378 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 }
3380 }
3381 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003382 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 }
3384 break;
3385
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003386 // Jump if an argument with a default value was already set and not
3387 // v:none.
3388 case ISN_JUMP_IF_ARG_SET:
3389 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3390 if (tv->v_type != VAR_UNKNOWN
3391 && !(tv->v_type == VAR_SPECIAL
3392 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003393 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003394 break;
3395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 // top of a for loop
3397 case ISN_FOR:
3398 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003399 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 typval_T *idxtv =
3401 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3402
Bram Moolenaar35578162021-08-02 19:10:38 +02003403 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003404 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003405 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003406 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003407 list_T *list = ltv->vval.v_list;
3408
3409 // push the next item from the list
3410 ++idxtv->vval.v_number;
3411 if (list == NULL
3412 || idxtv->vval.v_number >= list->lv_len)
3413 {
3414 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003415 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3416 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003417 }
3418 else if (list->lv_first == &range_list_item)
3419 {
3420 // non-materialized range() list
3421 tv = STACK_TV_BOT(0);
3422 tv->v_type = VAR_NUMBER;
3423 tv->v_lock = 0;
3424 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003426 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003427 }
3428 else
3429 {
3430 listitem_T *li = list_find(list,
3431 idxtv->vval.v_number);
3432
3433 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003434 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003435 }
3436 }
3437 else if (ltv->v_type == VAR_STRING)
3438 {
3439 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003440
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003441 // The index is for the last byte of the previous
3442 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003443 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003444 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003445 {
3446 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003447 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3448 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003449 }
3450 else
3451 {
3452 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3453
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003454 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003455 tv = STACK_TV_BOT(0);
3456 tv->v_type = VAR_STRING;
3457 tv->vval.v_string = vim_strnsave(
3458 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003459 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003460 idxtv->vval.v_number += clen - 1;
3461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003463 else if (ltv->v_type == VAR_BLOB)
3464 {
3465 blob_T *blob = ltv->vval.v_blob;
3466
3467 // When we get here the first time make a copy of the
3468 // blob, so that the iteration still works when it is
3469 // changed.
3470 if (idxtv->vval.v_number == -1 && blob != NULL)
3471 {
3472 blob_copy(blob, ltv);
3473 blob_unref(blob);
3474 blob = ltv->vval.v_blob;
3475 }
3476
3477 // The index is for the previous byte.
3478 ++idxtv->vval.v_number;
3479 if (blob == NULL
3480 || idxtv->vval.v_number >= blob_len(blob))
3481 {
3482 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003483 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3484 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003485 }
3486 else
3487 {
3488 // Push the next byte from the blob.
3489 tv = STACK_TV_BOT(0);
3490 tv->v_type = VAR_NUMBER;
3491 tv->vval.v_number = blob_get(blob,
3492 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003493 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003494 }
3495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 else
3497 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003498 semsg(_(e_for_loop_on_str_not_supported),
3499 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003500 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 }
3502 }
3503 break;
3504
3505 // start of ":try" block
3506 case ISN_TRY:
3507 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003508 trycmd_T *trycmd = NULL;
3509
Bram Moolenaar35578162021-08-02 19:10:38 +02003510 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003511 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3513 + ectx->ec_trystack.ga_len;
3514 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003516 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003517 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3518 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003519 trycmd->tcd_catch_idx =
3520 iptr->isn_arg.try.try_ref->try_catch;
3521 trycmd->tcd_finally_idx =
3522 iptr->isn_arg.try.try_ref->try_finally;
3523 trycmd->tcd_endtry_idx =
3524 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 }
3526 break;
3527
3528 case ISN_PUSHEXC:
3529 if (current_exception == NULL)
3530 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003531 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003533 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003535 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003536 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003538 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003540 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 tv->vval.v_string = vim_strsave(
3542 (char_u *)current_exception->value);
3543 break;
3544
3545 case ISN_CATCH:
3546 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003547 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548
Bram Moolenaar4c137212021-04-19 16:48:48 +02003549 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 if (trystack->ga_len > 0)
3551 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003552 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 + trystack->ga_len - 1;
3554 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003555 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 }
3557 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003558 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 catch_exception(current_exception);
3560 }
3561 break;
3562
Bram Moolenaarc150c092021-02-13 15:02:46 +01003563 case ISN_TRYCONT:
3564 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003565 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003566 trycont_T *trycont = &iptr->isn_arg.trycont;
3567 int i;
3568 trycmd_T *trycmd;
3569 int iidx = trycont->tct_where;
3570
3571 if (trystack->ga_len < trycont->tct_levels)
3572 {
3573 siemsg("TRYCONT: expected %d levels, found %d",
3574 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003575 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003576 }
3577 // Make :endtry jump to any outer try block and the last
3578 // :endtry inside the loop to the loop start.
3579 for (i = trycont->tct_levels; i > 0; --i)
3580 {
3581 trycmd = ((trycmd_T *)trystack->ga_data)
3582 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003583 // Add one to tcd_cont to be able to jump to
3584 // instruction with index zero.
3585 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003586 iidx = trycmd->tcd_finally_idx == 0
3587 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003588 }
3589 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003590 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003591 }
3592 break;
3593
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003594 case ISN_FINALLY:
3595 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003596 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003597 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3598 + trystack->ga_len - 1;
3599
3600 // Reset the index to avoid a return statement jumps here
3601 // again.
3602 trycmd->tcd_finally_idx = 0;
3603 break;
3604 }
3605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 // end of ":try" block
3607 case ISN_ENDTRY:
3608 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
3611 if (trystack->ga_len > 0)
3612 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003613 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 --trystack->ga_len;
3616 --trylevel;
3617 trycmd = ((trycmd_T *)trystack->ga_data)
3618 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003619 if (trycmd->tcd_did_throw)
3620 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003621 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 {
3623 // discard the exception
3624 if (caught_stack == current_exception)
3625 caught_stack = caught_stack->caught;
3626 discard_current_exception();
3627 }
3628
3629 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003630 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003631
Bram Moolenaar4c137212021-04-19 16:48:48 +02003632 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003633 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003634 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003635 clear_tv(STACK_TV_BOT(0));
3636 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003637 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003638 // handling :continue: jump to outer try block or
3639 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003640 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 }
3642 }
3643 break;
3644
3645 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003646 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003647 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003648
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003649 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3650 {
3651 // throwing an exception while using "silent!" causes
3652 // the function to abort but not display an error.
3653 tv = STACK_TV_BOT(-1);
3654 clear_tv(tv);
3655 tv->v_type = VAR_NUMBER;
3656 tv->vval.v_number = 0;
3657 goto done;
3658 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003659 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003660 tv = STACK_TV_BOT(0);
3661 if (tv->vval.v_string == NULL
3662 || *skipwhite(tv->vval.v_string) == NUL)
3663 {
3664 vim_free(tv->vval.v_string);
3665 SOURCING_LNUM = iptr->isn_lnum;
3666 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003667 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003668 }
3669
3670 // Inside a "catch" we need to first discard the caught
3671 // exception.
3672 if (trystack->ga_len > 0)
3673 {
3674 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3675 + trystack->ga_len - 1;
3676 if (trycmd->tcd_caught && current_exception != NULL)
3677 {
3678 // discard the exception
3679 if (caught_stack == current_exception)
3680 caught_stack = caught_stack->caught;
3681 discard_current_exception();
3682 trycmd->tcd_caught = FALSE;
3683 }
3684 }
3685
3686 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3687 == FAIL)
3688 {
3689 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003690 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003691 }
3692 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 break;
3695
3696 // compare with special values
3697 case ISN_COMPAREBOOL:
3698 case ISN_COMPARESPECIAL:
3699 {
3700 typval_T *tv1 = STACK_TV_BOT(-2);
3701 typval_T *tv2 = STACK_TV_BOT(-1);
3702 varnumber_T arg1 = tv1->vval.v_number;
3703 varnumber_T arg2 = tv2->vval.v_number;
3704 int res;
3705
3706 switch (iptr->isn_arg.op.op_type)
3707 {
3708 case EXPR_EQUAL: res = arg1 == arg2; break;
3709 case EXPR_NEQUAL: res = arg1 != arg2; break;
3710 default: res = 0; break;
3711 }
3712
Bram Moolenaar4c137212021-04-19 16:48:48 +02003713 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 tv1->v_type = VAR_BOOL;
3715 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3716 }
3717 break;
3718
3719 // Operation with two number arguments
3720 case ISN_OPNR:
3721 case ISN_COMPARENR:
3722 {
3723 typval_T *tv1 = STACK_TV_BOT(-2);
3724 typval_T *tv2 = STACK_TV_BOT(-1);
3725 varnumber_T arg1 = tv1->vval.v_number;
3726 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003727 varnumber_T res = 0;
3728 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729
3730 switch (iptr->isn_arg.op.op_type)
3731 {
3732 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003733 case EXPR_DIV: if (arg2 == 0)
3734 div_zero = TRUE;
3735 else
3736 res = arg1 / arg2;
3737 break;
3738 case EXPR_REM: if (arg2 == 0)
3739 div_zero = TRUE;
3740 else
3741 res = arg1 % arg2;
3742 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 case EXPR_SUB: res = arg1 - arg2; break;
3744 case EXPR_ADD: res = arg1 + arg2; break;
3745
3746 case EXPR_EQUAL: res = arg1 == arg2; break;
3747 case EXPR_NEQUAL: res = arg1 != arg2; break;
3748 case EXPR_GREATER: res = arg1 > arg2; break;
3749 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3750 case EXPR_SMALLER: res = arg1 < arg2; break;
3751 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003752 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 }
3754
Bram Moolenaar4c137212021-04-19 16:48:48 +02003755 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 if (iptr->isn_type == ISN_COMPARENR)
3757 {
3758 tv1->v_type = VAR_BOOL;
3759 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3760 }
3761 else
3762 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003763 if (div_zero)
3764 {
3765 SOURCING_LNUM = iptr->isn_lnum;
3766 emsg(_(e_divide_by_zero));
3767 goto on_error;
3768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 }
3770 break;
3771
3772 // Computation with two float arguments
3773 case ISN_OPFLOAT:
3774 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003775#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 {
3777 typval_T *tv1 = STACK_TV_BOT(-2);
3778 typval_T *tv2 = STACK_TV_BOT(-1);
3779 float_T arg1 = tv1->vval.v_float;
3780 float_T arg2 = tv2->vval.v_float;
3781 float_T res = 0;
3782 int cmp = FALSE;
3783
3784 switch (iptr->isn_arg.op.op_type)
3785 {
3786 case EXPR_MULT: res = arg1 * arg2; break;
3787 case EXPR_DIV: res = arg1 / arg2; break;
3788 case EXPR_SUB: res = arg1 - arg2; break;
3789 case EXPR_ADD: res = arg1 + arg2; break;
3790
3791 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3792 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3793 case EXPR_GREATER: cmp = arg1 > arg2; break;
3794 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3795 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3796 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3797 default: cmp = 0; break;
3798 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003799 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 if (iptr->isn_type == ISN_COMPAREFLOAT)
3801 {
3802 tv1->v_type = VAR_BOOL;
3803 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3804 }
3805 else
3806 tv1->vval.v_float = res;
3807 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003808#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 break;
3810
3811 case ISN_COMPARELIST:
3812 {
3813 typval_T *tv1 = STACK_TV_BOT(-2);
3814 typval_T *tv2 = STACK_TV_BOT(-1);
3815 list_T *arg1 = tv1->vval.v_list;
3816 list_T *arg2 = tv2->vval.v_list;
3817 int cmp = FALSE;
3818 int ic = iptr->isn_arg.op.op_ic;
3819
3820 switch (iptr->isn_arg.op.op_type)
3821 {
3822 case EXPR_EQUAL: cmp =
3823 list_equal(arg1, arg2, ic, FALSE); break;
3824 case EXPR_NEQUAL: cmp =
3825 !list_equal(arg1, arg2, ic, FALSE); break;
3826 case EXPR_IS: cmp = arg1 == arg2; break;
3827 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3828 default: cmp = 0; break;
3829 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003830 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 clear_tv(tv1);
3832 clear_tv(tv2);
3833 tv1->v_type = VAR_BOOL;
3834 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3835 }
3836 break;
3837
3838 case ISN_COMPAREBLOB:
3839 {
3840 typval_T *tv1 = STACK_TV_BOT(-2);
3841 typval_T *tv2 = STACK_TV_BOT(-1);
3842 blob_T *arg1 = tv1->vval.v_blob;
3843 blob_T *arg2 = tv2->vval.v_blob;
3844 int cmp = FALSE;
3845
3846 switch (iptr->isn_arg.op.op_type)
3847 {
3848 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3849 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3850 case EXPR_IS: cmp = arg1 == arg2; break;
3851 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3852 default: cmp = 0; break;
3853 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003854 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 clear_tv(tv1);
3856 clear_tv(tv2);
3857 tv1->v_type = VAR_BOOL;
3858 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3859 }
3860 break;
3861
3862 // TODO: handle separately
3863 case ISN_COMPARESTRING:
3864 case ISN_COMPAREDICT:
3865 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 case ISN_COMPAREANY:
3867 {
3868 typval_T *tv1 = STACK_TV_BOT(-2);
3869 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003870 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 int ic = iptr->isn_arg.op.op_ic;
3872
Bram Moolenaareb26f432020-09-14 16:50:05 +02003873 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003874 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003876 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 }
3878 break;
3879
3880 case ISN_ADDLIST:
3881 case ISN_ADDBLOB:
3882 {
3883 typval_T *tv1 = STACK_TV_BOT(-2);
3884 typval_T *tv2 = STACK_TV_BOT(-1);
3885
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003886 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003888 {
3889 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3890 && tv1->vval.v_list != NULL)
3891 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3892 NULL);
3893 else
3894 eval_addlist(tv1, tv2);
3895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 else
3897 eval_addblob(tv1, tv2);
3898 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003899 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 }
3901 break;
3902
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003903 case ISN_LISTAPPEND:
3904 {
3905 typval_T *tv1 = STACK_TV_BOT(-2);
3906 typval_T *tv2 = STACK_TV_BOT(-1);
3907 list_T *l = tv1->vval.v_list;
3908
3909 // add an item to a list
3910 if (l == NULL)
3911 {
3912 SOURCING_LNUM = iptr->isn_lnum;
3913 emsg(_(e_cannot_add_to_null_list));
3914 goto on_error;
3915 }
3916 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003917 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003918 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003919 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003920 }
3921 break;
3922
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003923 case ISN_BLOBAPPEND:
3924 {
3925 typval_T *tv1 = STACK_TV_BOT(-2);
3926 typval_T *tv2 = STACK_TV_BOT(-1);
3927 blob_T *b = tv1->vval.v_blob;
3928 int error = FALSE;
3929 varnumber_T n;
3930
3931 // add a number to a blob
3932 if (b == NULL)
3933 {
3934 SOURCING_LNUM = iptr->isn_lnum;
3935 emsg(_(e_cannot_add_to_null_blob));
3936 goto on_error;
3937 }
3938 n = tv_get_number_chk(tv2, &error);
3939 if (error)
3940 goto on_error;
3941 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003942 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003943 }
3944 break;
3945
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 // Computation with two arguments of unknown type
3947 case ISN_OPANY:
3948 {
3949 typval_T *tv1 = STACK_TV_BOT(-2);
3950 typval_T *tv2 = STACK_TV_BOT(-1);
3951 varnumber_T n1, n2;
3952#ifdef FEAT_FLOAT
3953 float_T f1 = 0, f2 = 0;
3954#endif
3955 int error = FALSE;
3956
3957 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3958 {
3959 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3960 {
3961 eval_addlist(tv1, tv2);
3962 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003963 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 break;
3965 }
3966 else if (tv1->v_type == VAR_BLOB
3967 && tv2->v_type == VAR_BLOB)
3968 {
3969 eval_addblob(tv1, tv2);
3970 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003971 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 break;
3973 }
3974 }
3975#ifdef FEAT_FLOAT
3976 if (tv1->v_type == VAR_FLOAT)
3977 {
3978 f1 = tv1->vval.v_float;
3979 n1 = 0;
3980 }
3981 else
3982#endif
3983 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003984 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 n1 = tv_get_number_chk(tv1, &error);
3986 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003987 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988#ifdef FEAT_FLOAT
3989 if (tv2->v_type == VAR_FLOAT)
3990 f1 = n1;
3991#endif
3992 }
3993#ifdef FEAT_FLOAT
3994 if (tv2->v_type == VAR_FLOAT)
3995 {
3996 f2 = tv2->vval.v_float;
3997 n2 = 0;
3998 }
3999 else
4000#endif
4001 {
4002 n2 = tv_get_number_chk(tv2, &error);
4003 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004004 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005#ifdef FEAT_FLOAT
4006 if (tv1->v_type == VAR_FLOAT)
4007 f2 = n2;
4008#endif
4009 }
4010#ifdef FEAT_FLOAT
4011 // if there is a float on either side the result is a float
4012 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4013 {
4014 switch (iptr->isn_arg.op.op_type)
4015 {
4016 case EXPR_MULT: f1 = f1 * f2; break;
4017 case EXPR_DIV: f1 = f1 / f2; break;
4018 case EXPR_SUB: f1 = f1 - f2; break;
4019 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004020 default: SOURCING_LNUM = iptr->isn_lnum;
4021 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004022 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 }
4024 clear_tv(tv1);
4025 clear_tv(tv2);
4026 tv1->v_type = VAR_FLOAT;
4027 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004028 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 }
4030 else
4031#endif
4032 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004033 int failed = FALSE;
4034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 switch (iptr->isn_arg.op.op_type)
4036 {
4037 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004038 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4039 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004040 goto on_error;
4041 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 case EXPR_SUB: n1 = n1 - n2; break;
4043 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004044 default: n1 = num_modulus(n1, n2, &failed);
4045 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004046 goto on_error;
4047 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 }
4049 clear_tv(tv1);
4050 clear_tv(tv2);
4051 tv1->v_type = VAR_NUMBER;
4052 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004053 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 }
4055 }
4056 break;
4057
4058 case ISN_CONCAT:
4059 {
4060 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4061 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4062 char_u *res;
4063
4064 res = concat_str(str1, str2);
4065 clear_tv(STACK_TV_BOT(-2));
4066 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004067 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 STACK_TV_BOT(-1)->vval.v_string = res;
4069 }
4070 break;
4071
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004072 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004073 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004074 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004075 int is_slice = iptr->isn_type == ISN_STRSLICE;
4076 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004077 char_u *res;
4078
4079 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004080 // string slice: string is at stack-3, first index at
4081 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004082 if (is_slice)
4083 {
4084 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004085 n1 = tv->vval.v_number;
4086 }
4087
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004088 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004089 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004090
Bram Moolenaar4c137212021-04-19 16:48:48 +02004091 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004092 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004093 if (is_slice)
4094 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004095 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004096 else
4097 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004098 // single character (including composing characters).
4099 // If the index is too big or negative the result is
4100 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004101 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004102 vim_free(tv->vval.v_string);
4103 tv->vval.v_string = res;
4104 }
4105 break;
4106
4107 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004108 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004109 case ISN_BLOBINDEX:
4110 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004112 int is_slice = iptr->isn_type == ISN_LISTSLICE
4113 || iptr->isn_type == ISN_BLOBSLICE;
4114 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4115 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004116 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004117 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118
4119 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004120 // list slice: list is at stack-3, indexes at stack-2 and
4121 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004122 // Same for blob.
4123 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124
4125 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004126 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004128
4129 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 {
Bram Moolenaared591872020-08-15 22:14:53 +02004131 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004132 n1 = tv->vval.v_number;
4133 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 }
Bram Moolenaared591872020-08-15 22:14:53 +02004135
Bram Moolenaar4c137212021-04-19 16:48:48 +02004136 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004137 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004138 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004139 if (is_blob)
4140 {
4141 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4142 n1, n2, FALSE, tv) == FAIL)
4143 goto on_error;
4144 }
4145 else
4146 {
4147 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4148 n1, n2, FALSE, tv, TRUE) == FAIL)
4149 goto on_error;
4150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 }
4152 break;
4153
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004154 case ISN_ANYINDEX:
4155 case ISN_ANYSLICE:
4156 {
4157 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4158 typval_T *var1, *var2;
4159 int res;
4160
4161 // index: composite is at stack-2, index at stack-1
4162 // slice: composite is at stack-3, indexes at stack-2 and
4163 // stack-1
4164 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004165 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004166 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4167 goto on_error;
4168 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4169 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004170 res = eval_index_inner(tv, is_slice, var1, var2,
4171 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004172 clear_tv(var1);
4173 if (is_slice)
4174 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004175 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004176 if (res == FAIL)
4177 goto on_error;
4178 }
4179 break;
4180
Bram Moolenaar9af78762020-06-16 11:34:42 +02004181 case ISN_SLICE:
4182 {
4183 list_T *list;
4184 int count = iptr->isn_arg.number;
4185
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004186 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004187 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004188 list = tv->vval.v_list;
4189
4190 // no error for short list, expect it to be checked earlier
4191 if (list != NULL && list->lv_len >= count)
4192 {
4193 list_T *newlist = list_slice(list,
4194 count, list->lv_len - 1);
4195
4196 if (newlist != NULL)
4197 {
4198 list_unref(list);
4199 tv->vval.v_list = newlist;
4200 ++newlist->lv_refcount;
4201 }
4202 }
4203 }
4204 break;
4205
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004206 case ISN_GETITEM:
4207 {
4208 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004209 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004210
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004211 // Get list item: list is at stack-1, push item.
4212 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004213 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4214 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004215
Bram Moolenaar35578162021-08-02 19:10:38 +02004216 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004217 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004218 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004219 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004220
4221 // Useful when used in unpack assignment. Reset at
4222 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004223 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004224 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004225 }
4226 break;
4227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 case ISN_MEMBER:
4229 {
4230 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004231 char_u *key;
4232 dictitem_T *di;
4233
4234 // dict member: dict is at stack-2, key at stack-1
4235 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004236 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004237 dict = tv->vval.v_dict;
4238
4239 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004240 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004241 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004242 if (key == NULL)
4243 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004244
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004245 if ((di = dict_find(dict, key, -1)) == NULL)
4246 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004247 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004248 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004249
4250 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004251 // stack contents makes sense and the dict stack is
4252 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004253 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004254 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004255 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004256 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004257 tv->v_type = VAR_NUMBER;
4258 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004259 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004260 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004261 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004262 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004263 // Put the dict used on the dict stack, it might be used by
4264 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004265 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004266 if (dict_stack_save(tv) == FAIL)
4267 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004268 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004269 }
4270 break;
4271
4272 // dict member with string key
4273 case ISN_STRINGMEMBER:
4274 {
4275 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 dictitem_T *di;
4277
4278 tv = STACK_TV_BOT(-1);
4279 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4280 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004281 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004283 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 }
4285 dict = tv->vval.v_dict;
4286
4287 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4288 == NULL)
4289 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004290 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004292 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004294 // Put the dict used on the dict stack, it might be used by
4295 // a dict function later.
4296 if (dict_stack_save(tv) == FAIL)
4297 goto on_fatal_error;
4298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004300 }
4301 break;
4302
4303 case ISN_CLEARDICT:
4304 dict_stack_drop();
4305 break;
4306
4307 case ISN_USEDICT:
4308 {
4309 typval_T *dict_tv = dict_stack_get_tv();
4310
4311 // Turn "dict.Func" into a partial for "Func" bound to
4312 // "dict". Don't do this when "Func" is already a partial
4313 // that was bound explicitly (pt_auto is FALSE).
4314 tv = STACK_TV_BOT(-1);
4315 if (dict_tv != NULL
4316 && dict_tv->v_type == VAR_DICT
4317 && dict_tv->vval.v_dict != NULL
4318 && (tv->v_type == VAR_FUNC
4319 || (tv->v_type == VAR_PARTIAL
4320 && (tv->vval.v_partial->pt_auto
4321 || tv->vval.v_partial->pt_dict == NULL))))
4322 dict_tv->vval.v_dict =
4323 make_partial(dict_tv->vval.v_dict, tv);
4324 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 }
4326 break;
4327
4328 case ISN_NEGATENR:
4329 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004330 if (tv->v_type != VAR_NUMBER
4331#ifdef FEAT_FLOAT
4332 && tv->v_type != VAR_FLOAT
4333#endif
4334 )
4335 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004336 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004337 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004338 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004339 }
4340#ifdef FEAT_FLOAT
4341 if (tv->v_type == VAR_FLOAT)
4342 tv->vval.v_float = -tv->vval.v_float;
4343 else
4344#endif
4345 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 break;
4347
4348 case ISN_CHECKNR:
4349 {
4350 int error = FALSE;
4351
4352 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004353 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004355 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 (void)tv_get_number_chk(tv, &error);
4357 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004358 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 }
4360 break;
4361
4362 case ISN_CHECKTYPE:
4363 {
4364 checktype_T *ct = &iptr->isn_arg.type;
4365
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004366 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004367 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004368 if (!ectx->ec_where.wt_variable)
4369 ectx->ec_where.wt_index = ct->ct_arg_idx;
4370 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4371 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004372 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004373 if (!ectx->ec_where.wt_variable)
4374 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004375
4376 // number 0 is FALSE, number 1 is TRUE
4377 if (tv->v_type == VAR_NUMBER
4378 && ct->ct_type->tt_type == VAR_BOOL
4379 && (tv->vval.v_number == 0
4380 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004382 tv->v_type = VAR_BOOL;
4383 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004384 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 }
4386 }
4387 break;
4388
Bram Moolenaar9af78762020-06-16 11:34:42 +02004389 case ISN_CHECKLEN:
4390 {
4391 int min_len = iptr->isn_arg.checklen.cl_min_len;
4392 list_T *list = NULL;
4393
4394 tv = STACK_TV_BOT(-1);
4395 if (tv->v_type == VAR_LIST)
4396 list = tv->vval.v_list;
4397 if (list == NULL || list->lv_len < min_len
4398 || (list->lv_len > min_len
4399 && !iptr->isn_arg.checklen.cl_more_OK))
4400 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004401 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004402 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004403 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004404 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004405 }
4406 }
4407 break;
4408
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004409 case ISN_SETTYPE:
4410 {
4411 checktype_T *ct = &iptr->isn_arg.type;
4412
4413 tv = STACK_TV_BOT(-1);
4414 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4415 {
4416 free_type(tv->vval.v_dict->dv_type);
4417 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4418 }
4419 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4420 {
4421 free_type(tv->vval.v_list->lv_type);
4422 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4423 }
4424 }
4425 break;
4426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004428 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 {
4430 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004431 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004433 if (iptr->isn_type == ISN_2BOOL)
4434 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004435 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004436 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004437 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004438 n = !n;
4439 }
4440 else
4441 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004442 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004443 SOURCING_LNUM = iptr->isn_lnum;
4444 n = tv_get_bool_chk(tv, &error);
4445 if (error)
4446 goto on_error;
4447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 clear_tv(tv);
4449 tv->v_type = VAR_BOOL;
4450 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4451 }
4452 break;
4453
4454 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004455 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004456 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004457 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4458 iptr->isn_type == ISN_2STRING_ANY,
4459 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004460 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 break;
4462
Bram Moolenaar08597872020-12-10 19:43:40 +01004463 case ISN_RANGE:
4464 {
4465 exarg_T ea;
4466 char *errormsg;
4467
Bram Moolenaarece0b872021-01-08 20:40:45 +01004468 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004469 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004470 ea.addr_type = ADDR_LINES;
4471 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004472 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004473 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004474 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004475
Bram Moolenaar35578162021-08-02 19:10:38 +02004476 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004477 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004478 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004479 tv = STACK_TV_BOT(-1);
4480 tv->v_type = VAR_NUMBER;
4481 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004482 if (ea.addr_count == 0)
4483 tv->vval.v_number = curwin->w_cursor.lnum;
4484 else
4485 tv->vval.v_number = ea.line2;
4486 }
4487 break;
4488
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004489 case ISN_PUT:
4490 {
4491 int regname = iptr->isn_arg.put.put_regname;
4492 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4493 char_u *expr = NULL;
4494 int dir = FORWARD;
4495
Bram Moolenaar08597872020-12-10 19:43:40 +01004496 if (lnum < -2)
4497 {
4498 // line number was put on the stack by ISN_RANGE
4499 tv = STACK_TV_BOT(-1);
4500 curwin->w_cursor.lnum = tv->vval.v_number;
4501 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4502 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004503 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004504 }
4505 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004506 // :put! above cursor
4507 dir = BACKWARD;
4508 else if (lnum >= 0)
4509 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004510
4511 if (regname == '=')
4512 {
4513 tv = STACK_TV_BOT(-1);
4514 if (tv->v_type == VAR_STRING)
4515 expr = tv->vval.v_string;
4516 else
4517 {
4518 expr = typval2string(tv, TRUE); // allocates value
4519 clear_tv(tv);
4520 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004521 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004522 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004523 check_cursor();
4524 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4525 vim_free(expr);
4526 }
4527 break;
4528
Bram Moolenaar02194d22020-10-24 23:08:38 +02004529 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004530 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4531 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4532 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4533 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004534 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4535 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004536 break;
4537
Bram Moolenaar02194d22020-10-24 23:08:38 +02004538 case ISN_CMDMOD_REV:
4539 // filter regprog is owned by the instruction, don't free it
4540 cmdmod.cmod_filter_regmatch.regprog = NULL;
4541 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004542 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4543 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004544 break;
4545
Bram Moolenaar792f7862020-11-23 08:31:18 +01004546 case ISN_UNPACK:
4547 {
4548 int count = iptr->isn_arg.unpack.unp_count;
4549 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4550 list_T *l;
4551 listitem_T *li;
4552 int i;
4553
4554 // Check there is a valid list to unpack.
4555 tv = STACK_TV_BOT(-1);
4556 if (tv->v_type != VAR_LIST)
4557 {
4558 SOURCING_LNUM = iptr->isn_lnum;
4559 emsg(_(e_for_argument_must_be_sequence_of_lists));
4560 goto on_error;
4561 }
4562 l = tv->vval.v_list;
4563 if (l == NULL
4564 || l->lv_len < (semicolon ? count - 1 : count))
4565 {
4566 SOURCING_LNUM = iptr->isn_lnum;
4567 emsg(_(e_list_value_does_not_have_enough_items));
4568 goto on_error;
4569 }
4570 else if (!semicolon && l->lv_len > count)
4571 {
4572 SOURCING_LNUM = iptr->isn_lnum;
4573 emsg(_(e_list_value_has_more_items_than_targets));
4574 goto on_error;
4575 }
4576
4577 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004578 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004579 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004580 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004581
4582 // Variable after semicolon gets a list with the remaining
4583 // items.
4584 if (semicolon)
4585 {
4586 list_T *rem_list =
4587 list_alloc_with_items(l->lv_len - count + 1);
4588
4589 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004590 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004591 tv = STACK_TV_BOT(-count);
4592 tv->vval.v_list = rem_list;
4593 ++rem_list->lv_refcount;
4594 tv->v_lock = 0;
4595 li = l->lv_first;
4596 for (i = 0; i < count - 1; ++i)
4597 li = li->li_next;
4598 for (i = 0; li != NULL; ++i)
4599 {
4600 list_set_item(rem_list, i, &li->li_tv);
4601 li = li->li_next;
4602 }
4603 --count;
4604 }
4605
4606 // Produce the values in reverse order, first item last.
4607 li = l->lv_first;
4608 for (i = 0; i < count; ++i)
4609 {
4610 tv = STACK_TV_BOT(-i - 1);
4611 copy_tv(&li->li_tv, tv);
4612 li = li->li_next;
4613 }
4614
4615 list_unref(l);
4616 }
4617 break;
4618
Bram Moolenaarb2049902021-01-24 12:53:53 +01004619 case ISN_PROF_START:
4620 case ISN_PROF_END:
4621 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004622#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004623 funccall_T cookie;
4624 ufunc_T *cur_ufunc =
4625 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004626 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004627
4628 cookie.func = cur_ufunc;
4629 if (iptr->isn_type == ISN_PROF_START)
4630 {
4631 func_line_start(&cookie, iptr->isn_lnum);
4632 // if we get here the instruction is executed
4633 func_line_exec(&cookie);
4634 }
4635 else
4636 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004637#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004638 }
4639 break;
4640
Bram Moolenaare99d4222021-06-13 14:01:26 +02004641 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004642 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004643 break;
4644
Bram Moolenaar389df252020-07-09 21:20:47 +02004645 case ISN_SHUFFLE:
4646 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004647 typval_T tmp_tv;
4648 int item = iptr->isn_arg.shuffle.shfl_item;
4649 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004650
4651 tmp_tv = *STACK_TV_BOT(-item);
4652 for ( ; up > 0 && item > 1; --up)
4653 {
4654 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4655 --item;
4656 }
4657 *STACK_TV_BOT(-item) = tmp_tv;
4658 }
4659 break;
4660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004662 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004664 ectx->ec_where.wt_index = 0;
4665 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 break;
4667 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004668 continue;
4669
Bram Moolenaard032f342020-07-18 18:13:02 +02004670func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004671 // Restore previous function. If the frame pointer is where we started
4672 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004673 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004674 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004675
Bram Moolenaar4c137212021-04-19 16:48:48 +02004676 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004677 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004678 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004679 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004680
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004681on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004682 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004683 // If "emsg_silent" is set then ignore the error, unless it was set
4684 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004685 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004686 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004687 {
4688 // If a sequence of instructions causes an error while ":silent!"
4689 // was used, restore the stack length and jump ahead to restoring
4690 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004691 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004692 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004693 while (ectx->ec_stack.ga_len
4694 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004695 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004696 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004697 clear_tv(STACK_TV_BOT(0));
4698 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004699 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4700 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004701 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004702 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004703 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004704on_fatal_error:
4705 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004706 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004707 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004708 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 }
4710
4711done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004712 ret = OK;
4713theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004714 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004715 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4716 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004717}
4718
4719/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004720 * Execute the instructions from a VAR_INSTR typeval and put the result in
4721 * "rettv".
4722 * Return OK or FAIL.
4723 */
4724 int
4725exe_typval_instr(typval_T *tv, typval_T *rettv)
4726{
4727 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4728 isn_T *save_instr = ectx->ec_instr;
4729 int save_iidx = ectx->ec_iidx;
4730 int res;
4731
4732 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4733 res = exec_instructions(ectx);
4734 if (res == OK)
4735 {
4736 *rettv = *STACK_TV_BOT(-1);
4737 --ectx->ec_stack.ga_len;
4738 }
4739
4740 ectx->ec_instr = save_instr;
4741 ectx->ec_iidx = save_iidx;
4742
4743 return res;
4744}
4745
4746/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004747 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4748 * "substitute_instr".
4749 */
4750 char_u *
4751exe_substitute_instr(void)
4752{
4753 ectx_T *ectx = substitute_instr->subs_ectx;
4754 isn_T *save_instr = ectx->ec_instr;
4755 int save_iidx = ectx->ec_iidx;
4756 char_u *res;
4757
4758 ectx->ec_instr = substitute_instr->subs_instr;
4759 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004760 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004761 typval_T *tv = STACK_TV_BOT(-1);
4762
Bram Moolenaar27523602021-06-05 21:36:19 +02004763 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004764 --ectx->ec_stack.ga_len;
4765 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004766 }
4767 else
4768 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004769 substitute_instr->subs_status = FAIL;
4770 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772
Bram Moolenaar4c137212021-04-19 16:48:48 +02004773 ectx->ec_instr = save_instr;
4774 ectx->ec_iidx = save_iidx;
4775
4776 return res;
4777}
4778
4779/*
4780 * Call a "def" function from old Vim script.
4781 * Return OK or FAIL.
4782 */
4783 int
4784call_def_function(
4785 ufunc_T *ufunc,
4786 int argc_arg, // nr of arguments
4787 typval_T *argv, // arguments
4788 partial_T *partial, // optional partial for context
4789 typval_T *rettv) // return value
4790{
4791 ectx_T ectx; // execution context
4792 int argc = argc_arg;
4793 typval_T *tv;
4794 int idx;
4795 int ret = FAIL;
4796 int defcount = ufunc->uf_args.ga_len - argc;
4797 sctx_T save_current_sctx = current_sctx;
4798 int did_emsg_before = did_emsg_cumul + did_emsg;
4799 int save_suppress_errthrow = suppress_errthrow;
4800 msglist_T **saved_msg_list = NULL;
4801 msglist_T *private_msg_list = NULL;
4802 int save_emsg_silent_def = emsg_silent_def;
4803 int save_did_emsg_def = did_emsg_def;
4804 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004805 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004806
4807// Get pointer to item in the stack.
4808#undef STACK_TV
4809#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4810
4811// Get pointer to item at the bottom of the stack, -1 is the bottom.
4812#undef STACK_TV_BOT
4813#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4814
4815// Get pointer to a local variable on the stack. Negative for arguments.
4816#undef STACK_TV_VAR
4817#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4818
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004819 // Update uf_has_breakpoint if needed.
4820 update_has_breakpoint(ufunc);
4821
Bram Moolenaar4c137212021-04-19 16:48:48 +02004822 if (ufunc->uf_def_status == UF_NOT_COMPILED
4823 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004824 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4825 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004826 == FAIL))
4827 {
4828 if (did_emsg_cumul + did_emsg == did_emsg_before)
4829 semsg(_(e_function_is_not_compiled_str),
4830 printable_func_name(ufunc));
4831 return FAIL;
4832 }
4833
4834 {
4835 // Check the function was really compiled.
4836 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4837 + ufunc->uf_dfunc_idx;
4838 if (INSTRUCTIONS(dfunc) == NULL)
4839 {
4840 iemsg("using call_def_function() on not compiled function");
4841 return FAIL;
4842 }
4843 }
4844
4845 // If depth of calling is getting too high, don't execute the function.
4846 orig_funcdepth = funcdepth_get();
4847 if (funcdepth_increment() == FAIL)
4848 return FAIL;
4849
4850 CLEAR_FIELD(ectx);
4851 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4852 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004853 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004854 {
4855 funcdepth_decrement();
4856 return FAIL;
4857 }
4858 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4859 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4860 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004861 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004862
4863 idx = argc - ufunc->uf_args.ga_len;
4864 if (idx > 0 && ufunc->uf_va_name == NULL)
4865 {
4866 if (idx == 1)
4867 emsg(_(e_one_argument_too_many));
4868 else
4869 semsg(_(e_nr_arguments_too_many), idx);
4870 goto failed_early;
4871 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004872 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4873 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004874 {
4875 if (idx == -1)
4876 emsg(_(e_one_argument_too_few));
4877 else
4878 semsg(_(e_nr_arguments_too_few), -idx);
4879 goto failed_early;
4880 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004881
4882 // Put arguments on the stack, but no more than what the function expects.
4883 // A lambda can be called with more arguments than it uses.
4884 for (idx = 0; idx < argc
4885 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4886 ++idx)
4887 {
4888 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4889 && argv[idx].v_type == VAR_SPECIAL
4890 && argv[idx].vval.v_number == VVAL_NONE)
4891 {
4892 // Use the default value.
4893 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4894 }
4895 else
4896 {
4897 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4898 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004899 ufunc->uf_arg_types[idx], &argv[idx],
4900 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004901 goto failed_early;
4902 copy_tv(&argv[idx], STACK_TV_BOT(0));
4903 }
4904 ++ectx.ec_stack.ga_len;
4905 }
4906
4907 // Turn varargs into a list. Empty list if no args.
4908 if (ufunc->uf_va_name != NULL)
4909 {
4910 int vararg_count = argc - ufunc->uf_args.ga_len;
4911
4912 if (vararg_count < 0)
4913 vararg_count = 0;
4914 else
4915 argc -= vararg_count;
4916 if (exe_newlist(vararg_count, &ectx) == FAIL)
4917 goto failed_early;
4918
4919 // Check the type of the list items.
4920 tv = STACK_TV_BOT(-1);
4921 if (ufunc->uf_va_type != NULL
4922 && ufunc->uf_va_type != &t_list_any
4923 && ufunc->uf_va_type->tt_member != &t_any
4924 && tv->vval.v_list != NULL)
4925 {
4926 type_T *expected = ufunc->uf_va_type->tt_member;
4927 listitem_T *li = tv->vval.v_list->lv_first;
4928
4929 for (idx = 0; idx < vararg_count; ++idx)
4930 {
4931 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004932 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004933 goto failed_early;
4934 li = li->li_next;
4935 }
4936 }
4937
4938 if (defcount > 0)
4939 // Move varargs list to below missing default arguments.
4940 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4941 --ectx.ec_stack.ga_len;
4942 }
4943
4944 // Make space for omitted arguments, will store default value below.
4945 // Any varargs list goes after them.
4946 if (defcount > 0)
4947 for (idx = 0; idx < defcount; ++idx)
4948 {
4949 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4950 ++ectx.ec_stack.ga_len;
4951 }
4952 if (ufunc->uf_va_name != NULL)
4953 ++ectx.ec_stack.ga_len;
4954
4955 // Frame pointer points to just after arguments.
4956 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4957 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4958
4959 {
4960 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4961 + ufunc->uf_dfunc_idx;
4962 ufunc_T *base_ufunc = dfunc->df_ufunc;
4963
4964 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4965 // by copy_func().
4966 if (partial != NULL || base_ufunc->uf_partial != NULL)
4967 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004968 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4969 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004970 goto failed_early;
4971 if (partial != NULL)
4972 {
4973 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4974 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004975 if (current_ectx->ec_outer_ref != NULL
4976 && current_ectx->ec_outer_ref->or_outer != NULL)
4977 ectx.ec_outer_ref->or_outer =
4978 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004979 }
4980 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004981 {
4982 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4983 ++partial->pt_refcount;
4984 ectx.ec_outer_ref->or_partial = partial;
4985 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004986 }
4987 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004988 {
4989 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4990 ++base_ufunc->uf_partial->pt_refcount;
4991 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4992 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004993 }
4994 }
4995
4996 // dummy frame entries
4997 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4998 {
4999 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5000 ++ectx.ec_stack.ga_len;
5001 }
5002
5003 {
5004 // Reserve space for local variables and any closure reference count.
5005 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5006 + ufunc->uf_dfunc_idx;
5007
5008 for (idx = 0; idx < dfunc->df_varcount; ++idx)
5009 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
5010 ectx.ec_stack.ga_len += dfunc->df_varcount;
5011 if (dfunc->df_has_closure)
5012 {
5013 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5014 STACK_TV_VAR(idx)->vval.v_number = 0;
5015 ++ectx.ec_stack.ga_len;
5016 }
5017
5018 ectx.ec_instr = INSTRUCTIONS(dfunc);
5019 }
5020
5021 // Following errors are in the function, not the caller.
5022 // Commands behave like vim9script.
5023 estack_push_ufunc(ufunc, 1);
5024 current_sctx = ufunc->uf_script_ctx;
5025 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5026
5027 // Use a specific location for storing error messages to be converted to an
5028 // exception.
5029 saved_msg_list = msg_list;
5030 msg_list = &private_msg_list;
5031
5032 // Do turn errors into exceptions.
5033 suppress_errthrow = FALSE;
5034
5035 // Do not delete the function while executing it.
5036 ++ufunc->uf_calls;
5037
5038 // When ":silent!" was used before calling then we still abort the
5039 // function. If ":silent!" is used in the function then we don't.
5040 emsg_silent_def = emsg_silent;
5041 did_emsg_def = 0;
5042
5043 ectx.ec_where.wt_index = 0;
5044 ectx.ec_where.wt_variable = FALSE;
5045
5046 // Execute the instructions until done.
5047 ret = exec_instructions(&ectx);
5048 if (ret == OK)
5049 {
5050 // function finished, get result from the stack.
5051 if (ufunc->uf_ret_type == &t_void)
5052 {
5053 rettv->v_type = VAR_VOID;
5054 }
5055 else
5056 {
5057 tv = STACK_TV_BOT(-1);
5058 *rettv = *tv;
5059 tv->v_type = VAR_UNKNOWN;
5060 }
5061 }
5062
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005063 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005064 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5065 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005066
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005067 // Deal with any remaining closures, they may be in use somewhere.
5068 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005069 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005070 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005071 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
5072 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005073
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005074 estack_pop();
5075 current_sctx = save_current_sctx;
5076
Bram Moolenaar2336c372021-12-06 15:06:54 +00005077 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5078 // Function was unreferenced while being used, free it now.
5079 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005080
Bram Moolenaar352134b2020-10-17 22:04:08 +02005081 if (*msg_list != NULL && saved_msg_list != NULL)
5082 {
5083 msglist_T **plist = saved_msg_list;
5084
5085 // Append entries from the current msg_list (uncaught exceptions) to
5086 // the saved msg_list.
5087 while (*plist != NULL)
5088 plist = &(*plist)->next;
5089
5090 *plist = *msg_list;
5091 }
5092 msg_list = saved_msg_list;
5093
Bram Moolenaar4c137212021-04-19 16:48:48 +02005094 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005095 {
5096 cmdmod.cmod_filter_regmatch.regprog = NULL;
5097 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005098 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005099 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005100 emsg_silent_def = save_emsg_silent_def;
5101 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005102
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005103failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005104 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5106 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005107 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005110 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005111 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005112 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005113 if (ectx.ec_outer_ref->or_outer_allocated)
5114 vim_free(ectx.ec_outer_ref->or_outer);
5115 partial_unref(ectx.ec_outer_ref->or_partial);
5116 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005117 }
5118
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005119 // Not sure if this is necessary.
5120 suppress_errthrow = save_suppress_errthrow;
5121
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005122 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5123 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005124 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005125 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005126 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 return ret;
5128}
5129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005131 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5132 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5133 * "pfx" is prefixed to every line.
5134 */
5135 static void
5136list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5137{
5138 int line_idx = 0;
5139 int prev_current = 0;
5140 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005141 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005142
5143 for (current = 0; current < instr_count; ++current)
5144 {
5145 isn_T *iptr = &instr[current];
5146 char *line;
5147
5148 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005149 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005150 while (line_idx < iptr->isn_lnum
5151 && line_idx < ufunc->uf_lines.ga_len)
5152 {
5153 if (current > prev_current)
5154 {
5155 msg_puts("\n\n");
5156 prev_current = current;
5157 }
5158 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5159 if (line != NULL)
5160 msg(line);
5161 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005162 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5163 {
5164 int first_def_arg = ufunc->uf_args.ga_len
5165 - ufunc->uf_def_args.ga_len;
5166
5167 if (def_arg_idx > 0)
5168 msg_puts("\n\n");
5169 msg_start();
5170 msg_puts(" ");
5171 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5172 first_def_arg + def_arg_idx]);
5173 msg_puts(" = ");
5174 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5175 msg_clr_eos();
5176 msg_end();
5177 }
5178 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005179
5180 switch (iptr->isn_type)
5181 {
5182 case ISN_EXEC:
5183 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5184 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005185 case ISN_EXEC_SPLIT:
5186 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5187 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005188 case ISN_EXECRANGE:
5189 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5190 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005191 case ISN_LEGACY_EVAL:
5192 smsg("%s%4d EVAL legacy %s", pfx, current,
5193 iptr->isn_arg.string);
5194 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005195 case ISN_REDIRSTART:
5196 smsg("%s%4d REDIR", pfx, current);
5197 break;
5198 case ISN_REDIREND:
5199 smsg("%s%4d REDIR END%s", pfx, current,
5200 iptr->isn_arg.number ? " append" : "");
5201 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005202 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005203#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005204 smsg("%s%4d CEXPR pre %s", pfx, current,
5205 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005206#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005207 break;
5208 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005209#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005210 {
5211 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5212
5213 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5214 cexpr_get_auname(cer->cer_cmdidx),
5215 cer->cer_forceit ? "!" : "",
5216 cer->cer_cmdline);
5217 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005218#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005219 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005220 case ISN_INSTR:
5221 {
5222 smsg("%s%4d INSTR", pfx, current);
5223 list_instructions(" ", iptr->isn_arg.instr,
5224 INT_MAX, NULL);
5225 msg(" -------------");
5226 }
5227 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005228 case ISN_SUBSTITUTE:
5229 {
5230 subs_T *subs = &iptr->isn_arg.subs;
5231
5232 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5233 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5234 msg(" -------------");
5235 }
5236 break;
5237 case ISN_EXECCONCAT:
5238 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5239 (varnumber_T)iptr->isn_arg.number);
5240 break;
5241 case ISN_ECHO:
5242 {
5243 echo_T *echo = &iptr->isn_arg.echo;
5244
5245 smsg("%s%4d %s %d", pfx, current,
5246 echo->echo_with_white ? "ECHO" : "ECHON",
5247 echo->echo_count);
5248 }
5249 break;
5250 case ISN_EXECUTE:
5251 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005252 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005253 break;
5254 case ISN_ECHOMSG:
5255 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005256 (varnumber_T)(iptr->isn_arg.number));
5257 break;
5258 case ISN_ECHOCONSOLE:
5259 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5260 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005261 break;
5262 case ISN_ECHOERR:
5263 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005264 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005265 break;
5266 case ISN_LOAD:
5267 {
5268 if (iptr->isn_arg.number < 0)
5269 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5270 (varnumber_T)(iptr->isn_arg.number
5271 + STACK_FRAME_SIZE));
5272 else
5273 smsg("%s%4d LOAD $%lld", pfx, current,
5274 (varnumber_T)(iptr->isn_arg.number));
5275 }
5276 break;
5277 case ISN_LOADOUTER:
5278 {
5279 if (iptr->isn_arg.number < 0)
5280 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5281 iptr->isn_arg.outer.outer_depth,
5282 iptr->isn_arg.outer.outer_idx
5283 + STACK_FRAME_SIZE);
5284 else
5285 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5286 iptr->isn_arg.outer.outer_depth,
5287 iptr->isn_arg.outer.outer_idx);
5288 }
5289 break;
5290 case ISN_LOADV:
5291 smsg("%s%4d LOADV v:%s", pfx, current,
5292 get_vim_var_name(iptr->isn_arg.number));
5293 break;
5294 case ISN_LOADSCRIPT:
5295 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005296 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5297 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5298 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005299
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005300 sv = get_script_svar(sref, -1);
5301 if (sv == NULL)
5302 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5303 pfx, current, si->sn_name);
5304 else
5305 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005306 sv->sv_name,
5307 sref->sref_idx,
5308 si->sn_name);
5309 }
5310 break;
5311 case ISN_LOADS:
5312 {
5313 scriptitem_T *si = SCRIPT_ITEM(
5314 iptr->isn_arg.loadstore.ls_sid);
5315
5316 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5317 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5318 }
5319 break;
5320 case ISN_LOADAUTO:
5321 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5322 break;
5323 case ISN_LOADG:
5324 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5325 break;
5326 case ISN_LOADB:
5327 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5328 break;
5329 case ISN_LOADW:
5330 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5331 break;
5332 case ISN_LOADT:
5333 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5334 break;
5335 case ISN_LOADGDICT:
5336 smsg("%s%4d LOAD g:", pfx, current);
5337 break;
5338 case ISN_LOADBDICT:
5339 smsg("%s%4d LOAD b:", pfx, current);
5340 break;
5341 case ISN_LOADWDICT:
5342 smsg("%s%4d LOAD w:", pfx, current);
5343 break;
5344 case ISN_LOADTDICT:
5345 smsg("%s%4d LOAD t:", pfx, current);
5346 break;
5347 case ISN_LOADOPT:
5348 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5349 break;
5350 case ISN_LOADENV:
5351 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5352 break;
5353 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005354 smsg("%s%4d LOADREG @%c", pfx, current,
5355 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005356 break;
5357
5358 case ISN_STORE:
5359 if (iptr->isn_arg.number < 0)
5360 smsg("%s%4d STORE arg[%lld]", pfx, current,
5361 iptr->isn_arg.number + STACK_FRAME_SIZE);
5362 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005363 smsg("%s%4d STORE $%lld", pfx, current,
5364 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005365 break;
5366 case ISN_STOREOUTER:
5367 {
5368 if (iptr->isn_arg.number < 0)
5369 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5370 iptr->isn_arg.outer.outer_depth,
5371 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5372 else
5373 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5374 iptr->isn_arg.outer.outer_depth,
5375 iptr->isn_arg.outer.outer_idx);
5376 }
5377 break;
5378 case ISN_STOREV:
5379 smsg("%s%4d STOREV v:%s", pfx, current,
5380 get_vim_var_name(iptr->isn_arg.number));
5381 break;
5382 case ISN_STOREAUTO:
5383 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5384 break;
5385 case ISN_STOREG:
5386 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5387 break;
5388 case ISN_STOREB:
5389 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5390 break;
5391 case ISN_STOREW:
5392 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5393 break;
5394 case ISN_STORET:
5395 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5396 break;
5397 case ISN_STORES:
5398 {
5399 scriptitem_T *si = SCRIPT_ITEM(
5400 iptr->isn_arg.loadstore.ls_sid);
5401
5402 smsg("%s%4d STORES %s in %s", pfx, current,
5403 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5404 }
5405 break;
5406 case ISN_STORESCRIPT:
5407 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005408 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5409 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5410 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005411
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005412 sv = get_script_svar(sref, -1);
5413 if (sv == NULL)
5414 smsg("%s%4d STORESCRIPT [deleted] in %s",
5415 pfx, current, si->sn_name);
5416 else
5417 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005418 sv->sv_name,
5419 sref->sref_idx,
5420 si->sn_name);
5421 }
5422 break;
5423 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005424 case ISN_STOREFUNCOPT:
5425 smsg("%s%4d %s &%s", pfx, current,
5426 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005427 iptr->isn_arg.storeopt.so_name);
5428 break;
5429 case ISN_STOREENV:
5430 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5431 break;
5432 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005433 smsg("%s%4d STOREREG @%c", pfx, current,
5434 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005435 break;
5436 case ISN_STORENR:
5437 smsg("%s%4d STORE %lld in $%d", pfx, current,
5438 iptr->isn_arg.storenr.stnr_val,
5439 iptr->isn_arg.storenr.stnr_idx);
5440 break;
5441
5442 case ISN_STOREINDEX:
5443 smsg("%s%4d STOREINDEX %s", pfx, current,
5444 vartype_name(iptr->isn_arg.vartype));
5445 break;
5446
5447 case ISN_STORERANGE:
5448 smsg("%s%4d STORERANGE", pfx, current);
5449 break;
5450
5451 // constants
5452 case ISN_PUSHNR:
5453 smsg("%s%4d PUSHNR %lld", pfx, current,
5454 (varnumber_T)(iptr->isn_arg.number));
5455 break;
5456 case ISN_PUSHBOOL:
5457 case ISN_PUSHSPEC:
5458 smsg("%s%4d PUSH %s", pfx, current,
5459 get_var_special_name(iptr->isn_arg.number));
5460 break;
5461 case ISN_PUSHF:
5462#ifdef FEAT_FLOAT
5463 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5464#endif
5465 break;
5466 case ISN_PUSHS:
5467 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5468 break;
5469 case ISN_PUSHBLOB:
5470 {
5471 char_u *r;
5472 char_u numbuf[NUMBUFLEN];
5473 char_u *tofree;
5474
5475 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5476 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5477 vim_free(tofree);
5478 }
5479 break;
5480 case ISN_PUSHFUNC:
5481 {
5482 char *name = (char *)iptr->isn_arg.string;
5483
5484 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5485 name == NULL ? "[none]" : name);
5486 }
5487 break;
5488 case ISN_PUSHCHANNEL:
5489#ifdef FEAT_JOB_CHANNEL
5490 {
5491 channel_T *channel = iptr->isn_arg.channel;
5492
5493 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5494 channel == NULL ? 0 : channel->ch_id);
5495 }
5496#endif
5497 break;
5498 case ISN_PUSHJOB:
5499#ifdef FEAT_JOB_CHANNEL
5500 {
5501 typval_T tv;
5502 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005503 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005504
5505 tv.v_type = VAR_JOB;
5506 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005507 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005508 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5509 }
5510#endif
5511 break;
5512 case ISN_PUSHEXC:
5513 smsg("%s%4d PUSH v:exception", pfx, current);
5514 break;
5515 case ISN_UNLET:
5516 smsg("%s%4d UNLET%s %s", pfx, current,
5517 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5518 iptr->isn_arg.unlet.ul_name);
5519 break;
5520 case ISN_UNLETENV:
5521 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5522 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5523 iptr->isn_arg.unlet.ul_name);
5524 break;
5525 case ISN_UNLETINDEX:
5526 smsg("%s%4d UNLETINDEX", pfx, current);
5527 break;
5528 case ISN_UNLETRANGE:
5529 smsg("%s%4d UNLETRANGE", pfx, current);
5530 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005531 case ISN_LOCKUNLOCK:
5532 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5533 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005534 case ISN_LOCKCONST:
5535 smsg("%s%4d LOCKCONST", pfx, current);
5536 break;
5537 case ISN_NEWLIST:
5538 smsg("%s%4d NEWLIST size %lld", pfx, current,
5539 (varnumber_T)(iptr->isn_arg.number));
5540 break;
5541 case ISN_NEWDICT:
5542 smsg("%s%4d NEWDICT size %lld", pfx, current,
5543 (varnumber_T)(iptr->isn_arg.number));
5544 break;
5545
5546 // function call
5547 case ISN_BCALL:
5548 {
5549 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5550
5551 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5552 internal_func_name(cbfunc->cbf_idx),
5553 cbfunc->cbf_argcount);
5554 }
5555 break;
5556 case ISN_DCALL:
5557 {
5558 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5559 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5560 + cdfunc->cdf_idx;
5561
5562 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005563 printable_func_name(df->df_ufunc),
5564 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005565 }
5566 break;
5567 case ISN_UCALL:
5568 {
5569 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5570
5571 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5572 cufunc->cuf_name, cufunc->cuf_argcount);
5573 }
5574 break;
5575 case ISN_PCALL:
5576 {
5577 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5578
5579 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5580 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5581 }
5582 break;
5583 case ISN_PCALL_END:
5584 smsg("%s%4d PCALL end", pfx, current);
5585 break;
5586 case ISN_RETURN:
5587 smsg("%s%4d RETURN", pfx, current);
5588 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005589 case ISN_RETURN_VOID:
5590 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005591 break;
5592 case ISN_FUNCREF:
5593 {
5594 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005595 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005596
Bram Moolenaar38453522021-11-28 22:00:12 +00005597 if (funcref->fr_func_name == NULL)
5598 {
5599 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5600 + funcref->fr_dfunc_idx;
5601 name = df->df_ufunc->uf_name;
5602 }
5603 else
5604 name = funcref->fr_func_name;
5605 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005606 }
5607 break;
5608
5609 case ISN_NEWFUNC:
5610 {
5611 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5612
5613 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5614 newfunc->nf_lambda, newfunc->nf_global);
5615 }
5616 break;
5617
5618 case ISN_DEF:
5619 {
5620 char_u *name = iptr->isn_arg.string;
5621
5622 smsg("%s%4d DEF %s", pfx, current,
5623 name == NULL ? (char_u *)"" : name);
5624 }
5625 break;
5626
5627 case ISN_JUMP:
5628 {
5629 char *when = "?";
5630
5631 switch (iptr->isn_arg.jump.jump_when)
5632 {
5633 case JUMP_ALWAYS:
5634 when = "JUMP";
5635 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005636 case JUMP_NEVER:
5637 iemsg("JUMP_NEVER should not be used");
5638 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005639 case JUMP_AND_KEEP_IF_TRUE:
5640 when = "JUMP_AND_KEEP_IF_TRUE";
5641 break;
5642 case JUMP_IF_FALSE:
5643 when = "JUMP_IF_FALSE";
5644 break;
5645 case JUMP_AND_KEEP_IF_FALSE:
5646 when = "JUMP_AND_KEEP_IF_FALSE";
5647 break;
5648 case JUMP_IF_COND_FALSE:
5649 when = "JUMP_IF_COND_FALSE";
5650 break;
5651 case JUMP_IF_COND_TRUE:
5652 when = "JUMP_IF_COND_TRUE";
5653 break;
5654 }
5655 smsg("%s%4d %s -> %d", pfx, current, when,
5656 iptr->isn_arg.jump.jump_where);
5657 }
5658 break;
5659
5660 case ISN_JUMP_IF_ARG_SET:
5661 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5662 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5663 iptr->isn_arg.jump.jump_where);
5664 break;
5665
5666 case ISN_FOR:
5667 {
5668 forloop_T *forloop = &iptr->isn_arg.forloop;
5669
5670 smsg("%s%4d FOR $%d -> %d", pfx, current,
5671 forloop->for_idx, forloop->for_end);
5672 }
5673 break;
5674
5675 case ISN_TRY:
5676 {
5677 try_T *try = &iptr->isn_arg.try;
5678
5679 if (try->try_ref->try_finally == 0)
5680 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5681 pfx, current,
5682 try->try_ref->try_catch,
5683 try->try_ref->try_endtry);
5684 else
5685 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5686 pfx, current,
5687 try->try_ref->try_catch,
5688 try->try_ref->try_finally,
5689 try->try_ref->try_endtry);
5690 }
5691 break;
5692 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005693 smsg("%s%4d CATCH", pfx, current);
5694 break;
5695 case ISN_TRYCONT:
5696 {
5697 trycont_T *trycont = &iptr->isn_arg.trycont;
5698
5699 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5700 trycont->tct_levels,
5701 trycont->tct_levels == 1 ? "" : "s",
5702 trycont->tct_where);
5703 }
5704 break;
5705 case ISN_FINALLY:
5706 smsg("%s%4d FINALLY", pfx, current);
5707 break;
5708 case ISN_ENDTRY:
5709 smsg("%s%4d ENDTRY", pfx, current);
5710 break;
5711 case ISN_THROW:
5712 smsg("%s%4d THROW", pfx, current);
5713 break;
5714
5715 // expression operations on number
5716 case ISN_OPNR:
5717 case ISN_OPFLOAT:
5718 case ISN_OPANY:
5719 {
5720 char *what;
5721 char *ins;
5722
5723 switch (iptr->isn_arg.op.op_type)
5724 {
5725 case EXPR_MULT: what = "*"; break;
5726 case EXPR_DIV: what = "/"; break;
5727 case EXPR_REM: what = "%"; break;
5728 case EXPR_SUB: what = "-"; break;
5729 case EXPR_ADD: what = "+"; break;
5730 default: what = "???"; break;
5731 }
5732 switch (iptr->isn_type)
5733 {
5734 case ISN_OPNR: ins = "OPNR"; break;
5735 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5736 case ISN_OPANY: ins = "OPANY"; break;
5737 default: ins = "???"; break;
5738 }
5739 smsg("%s%4d %s %s", pfx, current, ins, what);
5740 }
5741 break;
5742
5743 case ISN_COMPAREBOOL:
5744 case ISN_COMPARESPECIAL:
5745 case ISN_COMPARENR:
5746 case ISN_COMPAREFLOAT:
5747 case ISN_COMPARESTRING:
5748 case ISN_COMPAREBLOB:
5749 case ISN_COMPARELIST:
5750 case ISN_COMPAREDICT:
5751 case ISN_COMPAREFUNC:
5752 case ISN_COMPAREANY:
5753 {
5754 char *p;
5755 char buf[10];
5756 char *type;
5757
5758 switch (iptr->isn_arg.op.op_type)
5759 {
5760 case EXPR_EQUAL: p = "=="; break;
5761 case EXPR_NEQUAL: p = "!="; break;
5762 case EXPR_GREATER: p = ">"; break;
5763 case EXPR_GEQUAL: p = ">="; break;
5764 case EXPR_SMALLER: p = "<"; break;
5765 case EXPR_SEQUAL: p = "<="; break;
5766 case EXPR_MATCH: p = "=~"; break;
5767 case EXPR_IS: p = "is"; break;
5768 case EXPR_ISNOT: p = "isnot"; break;
5769 case EXPR_NOMATCH: p = "!~"; break;
5770 default: p = "???"; break;
5771 }
5772 STRCPY(buf, p);
5773 if (iptr->isn_arg.op.op_ic == TRUE)
5774 strcat(buf, "?");
5775 switch(iptr->isn_type)
5776 {
5777 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5778 case ISN_COMPARESPECIAL:
5779 type = "COMPARESPECIAL"; break;
5780 case ISN_COMPARENR: type = "COMPARENR"; break;
5781 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5782 case ISN_COMPARESTRING:
5783 type = "COMPARESTRING"; break;
5784 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5785 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5786 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5787 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5788 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5789 default: type = "???"; break;
5790 }
5791
5792 smsg("%s%4d %s %s", pfx, current, type, buf);
5793 }
5794 break;
5795
5796 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5797 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5798
5799 // expression operations
5800 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5801 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5802 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5803 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5804 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5805 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5806 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5807 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5808 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5809 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5810 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5811 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005812 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005813 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5814 iptr->isn_arg.getitem.gi_index,
5815 iptr->isn_arg.getitem.gi_with_op ?
5816 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005817 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5818 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5819 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005820 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5821 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5822
Bram Moolenaar4c137212021-04-19 16:48:48 +02005823 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5824
5825 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5826 case ISN_CHECKTYPE:
5827 {
5828 checktype_T *ct = &iptr->isn_arg.type;
5829 char *tofree;
5830
5831 if (ct->ct_arg_idx == 0)
5832 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5833 type_name(ct->ct_type, &tofree),
5834 (int)ct->ct_off);
5835 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005836 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5837 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005838 type_name(ct->ct_type, &tofree),
5839 (int)ct->ct_off,
5840 (int)ct->ct_arg_idx);
5841 vim_free(tofree);
5842 break;
5843 }
5844 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5845 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5846 iptr->isn_arg.checklen.cl_min_len);
5847 break;
5848 case ISN_SETTYPE:
5849 {
5850 char *tofree;
5851
5852 smsg("%s%4d SETTYPE %s", pfx, current,
5853 type_name(iptr->isn_arg.type.ct_type, &tofree));
5854 vim_free(tofree);
5855 break;
5856 }
5857 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005858 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5859 smsg("%s%4d INVERT %d (!val)", pfx, current,
5860 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005862 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5863 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005864 break;
5865 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005866 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005867 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005868 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5869 pfx, current,
5870 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005871 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005872 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5873 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005874 break;
5875 case ISN_PUT:
5876 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5877 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005878 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005879 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5880 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005881 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005882 else
5883 smsg("%s%4d PUT %c %ld", pfx, current,
5884 iptr->isn_arg.put.put_regname,
5885 (long)iptr->isn_arg.put.put_lnum);
5886 break;
5887
Bram Moolenaar4c137212021-04-19 16:48:48 +02005888 case ISN_CMDMOD:
5889 {
5890 char_u *buf;
5891 size_t len = produce_cmdmods(
5892 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5893
5894 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005895 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005896 {
5897 (void)produce_cmdmods(
5898 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5899 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5900 vim_free(buf);
5901 }
5902 break;
5903 }
5904 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5905
5906 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005907 smsg("%s%4d PROFILE START line %d", pfx, current,
5908 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005909 break;
5910
5911 case ISN_PROF_END:
5912 smsg("%s%4d PROFILE END", pfx, current);
5913 break;
5914
Bram Moolenaare99d4222021-06-13 14:01:26 +02005915 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005916 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5917 iptr->isn_arg.debug.dbg_break_lnum + 1,
5918 iptr->isn_lnum,
5919 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005920 break;
5921
Bram Moolenaar4c137212021-04-19 16:48:48 +02005922 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5923 iptr->isn_arg.unpack.unp_count,
5924 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5925 break;
5926 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5927 iptr->isn_arg.shuffle.shfl_item,
5928 iptr->isn_arg.shuffle.shfl_up);
5929 break;
5930 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5931
5932 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5933 return;
5934 }
5935
5936 out_flush(); // output one line at a time
5937 ui_breakcheck();
5938 if (got_int)
5939 break;
5940 }
5941}
5942
5943/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005944 * Handle command line completion for the :disassemble command.
5945 */
5946 void
5947set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5948{
5949 char_u *p;
5950
5951 // Default: expand user functions, "debug" and "profile"
5952 xp->xp_context = EXPAND_DISASSEMBLE;
5953 xp->xp_pattern = arg;
5954
5955 // first argument already typed: only user function names
5956 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5957 {
5958 xp->xp_context = EXPAND_USER_FUNC;
5959 xp->xp_pattern = skipwhite(p);
5960 }
5961}
5962
5963/*
5964 * Function given to ExpandGeneric() to obtain the list of :disassemble
5965 * arguments.
5966 */
5967 char_u *
5968get_disassemble_argument(expand_T *xp, int idx)
5969{
5970 if (idx == 0)
5971 return (char_u *)"debug";
5972 if (idx == 1)
5973 return (char_u *)"profile";
5974 return get_user_func_name(xp, idx - 2);
5975}
5976
5977/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005978 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005979 * We don't really need this at runtime, but we do have tests that require it,
5980 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981 */
5982 void
5983ex_disassemble(exarg_T *eap)
5984{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005985 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005986 char_u *fname;
5987 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 dfunc_T *dfunc;
5989 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005990 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005991 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005992 compiletype_T compile_type = CT_NONE;
5993
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005994 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005995 {
5996 compile_type = CT_PROFILE;
5997 arg = skipwhite(arg + 7);
5998 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005999 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006000 {
6001 compile_type = CT_DEBUG;
6002 arg = skipwhite(arg + 5);
6003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006004
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006005 if (STRNCMP(arg, "<lambda>", 8) == 0)
6006 {
6007 arg += 8;
6008 (void)getdigits(&arg);
6009 fname = vim_strnsave(eap->arg, arg - eap->arg);
6010 }
6011 else
6012 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006013 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006014 if (fname == NULL)
6015 {
6016 semsg(_(e_invarg2), eap->arg);
6017 return;
6018 }
6019
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006020 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006021 if (ufunc == NULL)
6022 {
6023 char_u *p = untrans_function_name(fname);
6024
6025 if (p != NULL)
6026 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006027 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006028 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006029 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006030 if (ufunc == NULL)
6031 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006032 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033 return;
6034 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006035 if (func_needs_compiling(ufunc, compile_type)
6036 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006037 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006038 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006040 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041 return;
6042 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006043 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006044
6045 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006046 switch (compile_type)
6047 {
6048 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006049#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006050 instr = dfunc->df_instr_prof;
6051 instr_count = dfunc->df_instr_prof_count;
6052 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006053#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006054 // FALLTHROUGH
6055 case CT_NONE:
6056 instr = dfunc->df_instr;
6057 instr_count = dfunc->df_instr_count;
6058 break;
6059 case CT_DEBUG:
6060 instr = dfunc->df_instr_debug;
6061 instr_count = dfunc->df_instr_debug_count;
6062 break;
6063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064
Bram Moolenaar4c137212021-04-19 16:48:48 +02006065 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066}
6067
6068/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006069 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070 * list, etc. Mostly like what JavaScript does, except that empty list and
6071 * empty dictionary are FALSE.
6072 */
6073 int
6074tv2bool(typval_T *tv)
6075{
6076 switch (tv->v_type)
6077 {
6078 case VAR_NUMBER:
6079 return tv->vval.v_number != 0;
6080 case VAR_FLOAT:
6081#ifdef FEAT_FLOAT
6082 return tv->vval.v_float != 0.0;
6083#else
6084 break;
6085#endif
6086 case VAR_PARTIAL:
6087 return tv->vval.v_partial != NULL;
6088 case VAR_FUNC:
6089 case VAR_STRING:
6090 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6091 case VAR_LIST:
6092 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6093 case VAR_DICT:
6094 return tv->vval.v_dict != NULL
6095 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6096 case VAR_BOOL:
6097 case VAR_SPECIAL:
6098 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6099 case VAR_JOB:
6100#ifdef FEAT_JOB_CHANNEL
6101 return tv->vval.v_job != NULL;
6102#else
6103 break;
6104#endif
6105 case VAR_CHANNEL:
6106#ifdef FEAT_JOB_CHANNEL
6107 return tv->vval.v_channel != NULL;
6108#else
6109 break;
6110#endif
6111 case VAR_BLOB:
6112 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6113 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006114 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006115 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006116 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117 break;
6118 }
6119 return FALSE;
6120}
6121
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006122 void
6123emsg_using_string_as(typval_T *tv, int as_number)
6124{
6125 semsg(_(as_number ? e_using_string_as_number_str
6126 : e_using_string_as_bool_str),
6127 tv->vval.v_string == NULL
6128 ? (char_u *)"" : tv->vval.v_string);
6129}
6130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131/*
6132 * If "tv" is a string give an error and return FAIL.
6133 */
6134 int
6135check_not_string(typval_T *tv)
6136{
6137 if (tv->v_type == VAR_STRING)
6138 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006139 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140 clear_tv(tv);
6141 return FAIL;
6142 }
6143 return OK;
6144}
6145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146
6147#endif // FEAT_EVAL