blob: c947d6a0ce110a5f1cb1b803cef114af09834b80 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200140 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
155 static void
156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200168static garray_T dict_stack = GA_EMPTY;
169
170/*
171 * Put a value on the dict stack. This consumes "tv".
172 */
173 static int
174dict_stack_save(typval_T *tv)
175{
176 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000177 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200178 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
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000274 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100275 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 Moolenaar5cd64792021-12-25 18:23:24 +0000400 {
401 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
402
403 tv->v_type = VAR_NUMBER;
404 tv->vval.v_number = 0;
405 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200406 if (dfunc->df_has_closure)
407 {
408 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
409
410 tv->v_type = VAR_NUMBER;
411 tv->vval.v_number = 0;
412 }
413 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100415 if (pt != NULL || ufunc->uf_partial != NULL
416 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100417 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200418 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100419
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200420 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100421 return FAIL;
422 if (pt != NULL)
423 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200424 ref->or_outer = &pt->pt_outer;
425 ++pt->pt_refcount;
426 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100427 }
428 else if (ufunc->uf_partial != NULL)
429 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200430 ref->or_outer = &ufunc->uf_partial->pt_outer;
431 ++ufunc->uf_partial->pt_refcount;
432 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100433 }
434 else
435 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200436 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200437 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200438 {
439 vim_free(ref);
440 return FAIL;
441 }
442 ref->or_outer_allocated = TRUE;
443 ref->or_outer->out_stack = &ectx->ec_stack;
444 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
445 if (ectx->ec_outer_ref != NULL)
446 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100447 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200448 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100449 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100450 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200451 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100452
Bram Moolenaarc970e422021-03-17 15:03:04 +0100453 ++ufunc->uf_calls;
454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 // Set execution state to the start of the called function.
456 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100457 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100458 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200459 if (entry != NULL)
460 {
461 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200462 // Save the current context so it can be restored on return.
463 entry->es_save_sctx = current_sctx;
464 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200465 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100466
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200467 // Start execution at the first instruction.
468 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469
470 return OK;
471}
472
473// Get pointer to item in the stack.
474#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
475
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000476// Double linked list of funcstack_T in use.
477static funcstack_T *first_funcstack = NULL;
478
479 static void
480add_funcstack_to_list(funcstack_T *funcstack)
481{
482 // Link in list of funcstacks.
483 if (first_funcstack != NULL)
484 first_funcstack->fs_prev = funcstack;
485 funcstack->fs_next = first_funcstack;
486 funcstack->fs_prev = NULL;
487 first_funcstack = funcstack;
488}
489
490 static void
491remove_funcstack_from_list(funcstack_T *funcstack)
492{
493 if (funcstack->fs_prev == NULL)
494 first_funcstack = funcstack->fs_next;
495 else
496 funcstack->fs_prev->fs_next = funcstack->fs_next;
497 if (funcstack->fs_next != NULL)
498 funcstack->fs_next->fs_prev = funcstack->fs_prev;
499}
500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200502 * Used when returning from a function: Check if any closure is still
503 * referenced. If so then move the arguments and variables to a separate piece
504 * of stack to be used when the closure is called.
505 * When "free_arguments" is TRUE the arguments are to be freed.
506 * Returns FAIL when out of memory.
507 */
508 static int
509handle_closure_in_use(ectx_T *ectx, int free_arguments)
510{
511 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
512 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200513 int argcount;
514 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515 int idx;
516 typval_T *tv;
517 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200518 garray_T *gap = &ectx->ec_funcrefs;
519 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200520
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200521 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200522 return OK; // function was freed
523 if (dfunc->df_has_closure == 0)
524 return OK; // no closures
525 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
526 closure_count = tv->vval.v_number;
527 if (closure_count == 0)
528 return OK; // no funcrefs created
529
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200530 argcount = ufunc_argcount(dfunc->df_ufunc);
531 top = ectx->ec_frame_idx - argcount;
532
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200533 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200535 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200536 partial_T *pt;
537 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200538
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200539 if (off < 0)
540 continue; // count is off or already done
541 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200542 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200543 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200544 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200545 int i;
546
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000547 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200548 // unreferenced on return.
549 for (i = 0; i < dfunc->df_varcount; ++i)
550 {
551 typval_T *stv = STACK_TV(ectx->ec_frame_idx
552 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200553 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200554 --refcount;
555 }
556 if (refcount > 1)
557 {
558 closure_in_use = TRUE;
559 break;
560 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200561 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200562 }
563
564 if (closure_in_use)
565 {
566 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
567 typval_T *stack;
568
569 // A closure is using the arguments and/or local variables.
570 // Move them to the called function.
571 if (funcstack == NULL)
572 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000573
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200574 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
575 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200576 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
577 funcstack->fs_ga.ga_data = stack;
578 if (stack == NULL)
579 {
580 vim_free(funcstack);
581 return FAIL;
582 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000583 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200584
585 // Move or copy the arguments.
586 for (idx = 0; idx < argcount; ++idx)
587 {
588 tv = STACK_TV(top + idx);
589 if (free_arguments)
590 {
591 *(stack + idx) = *tv;
592 tv->v_type = VAR_UNKNOWN;
593 }
594 else
595 copy_tv(tv, stack + idx);
596 }
597 // Move the local variables.
598 for (idx = 0; idx < dfunc->df_varcount; ++idx)
599 {
600 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200601
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200602 // A partial created for a local function, that is also used as a
603 // local variable, has a reference count for the variable, thus
604 // will never go down to zero. When all these refcounts are one
605 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000606 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200607 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
608 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200609 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200610
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200611 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200612 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
613 gap->ga_len - closure_count + i])
614 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200615 }
616
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200617 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200618 tv->v_type = VAR_UNKNOWN;
619 }
620
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200621 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200623 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
624 - closure_count + idx];
625 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200627 ++funcstack->fs_refcount;
628 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100629 pt->pt_outer.out_stack = &funcstack->fs_ga;
630 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200631 }
632 }
633 }
634
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200635 for (idx = 0; idx < closure_count; ++idx)
636 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
637 - closure_count + idx]);
638 gap->ga_len -= closure_count;
639 if (gap->ga_len == 0)
640 ga_clear(gap);
641
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200642 return OK;
643}
644
645/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200646 * Called when a partial is freed or its reference count goes down to one. The
647 * funcstack may be the only reference to the partials in the local variables.
648 * Go over all of them, the funcref and can be freed if all partials
649 * referencing the funcstack have a reference count of one.
650 */
651 void
652funcstack_check_refcount(funcstack_T *funcstack)
653{
654 int i;
655 garray_T *gap = &funcstack->fs_ga;
656 int done = 0;
657
658 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
659 return;
660 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
661 {
662 typval_T *tv = ((typval_T *)gap->ga_data) + i;
663
664 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
665 && tv->vval.v_partial->pt_funcstack == funcstack
666 && tv->vval.v_partial->pt_refcount == 1)
667 ++done;
668 }
669 if (done == funcstack->fs_min_refcount)
670 {
671 typval_T *stack = gap->ga_data;
672
673 // All partials referencing the funcstack have a reference count of
674 // one, thus the funcstack is no longer of use.
675 for (i = 0; i < gap->ga_len; ++i)
676 clear_tv(stack + i);
677 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000678 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200679 vim_free(funcstack);
680 }
681}
682
683/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000684 * For garbage collecting: set references in all variables referenced by
685 * all funcstacks.
686 */
687 int
688set_ref_in_funcstacks(int copyID)
689{
690 funcstack_T *funcstack;
691
692 for (funcstack = first_funcstack; funcstack != NULL;
693 funcstack = funcstack->fs_next)
694 {
695 typval_T *stack = funcstack->fs_ga.ga_data;
696 int i;
697
698 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
699 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
700 return TRUE; // abort
701 }
702 return FALSE;
703}
704
705/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 * Return from the current function.
707 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200709func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100712 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200713 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
714 + ectx->ec_dfunc_idx;
715 int argcount = ufunc_argcount(dfunc->df_ufunc);
716 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200717 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100718 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
719 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200720 funclocal_T *floc;
721#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100722 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
723 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724
Bram Moolenaar12d26532021-02-19 19:13:21 +0100725 if (do_profiling == PROF_YES)
726 {
727 ufunc_T *caller = prev_dfunc->df_ufunc;
728
729 if (dfunc->df_ufunc->uf_profiling
730 || (caller != NULL && caller->uf_profiling))
731 {
732 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
733 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
734 --profile_info_ga.ga_len;
735 }
736 }
737#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000738
739 // No check for uf_refcount being zero, cannot think of a way that would
740 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100741 --dfunc->df_ufunc->uf_calls;
742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200744 entry = estack_pop();
745 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200746 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200748 if (handle_closure_in_use(ectx, TRUE) == FAIL)
749 return FAIL;
750
751 // Clear the arguments.
752 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
753 clear_tv(STACK_TV(idx));
754
755 // Clear local variables and temp values, but not the return value.
756 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100757 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100759
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100760 // The return value should be on top of the stack. However, when aborting
761 // it may not be there and ec_frame_idx is the top of the stack.
762 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100763 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100764 ret_idx = 0;
765
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200766 if (ectx->ec_outer_ref != NULL)
767 {
768 if (ectx->ec_outer_ref->or_outer_allocated)
769 vim_free(ectx->ec_outer_ref->or_outer);
770 partial_unref(ectx->ec_outer_ref->or_partial);
771 vim_free(ectx->ec_outer_ref);
772 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100773
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100774 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100775 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100776 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
777 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200778 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
779 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200780 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100781 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100782 floc = (void *)STACK_TV(ectx->ec_frame_idx
783 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200784 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100785 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
786 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200787
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100788 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200789 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100790 else
791 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200792 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100793 vim_free(floc);
794 }
795
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100796 if (ret_idx > 0)
797 {
798 // Reset the stack to the position before the call, with a spot for the
799 // return value, moved there from above the frame.
800 ectx->ec_stack.ga_len = top + 1;
801 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
802 }
803 else
804 // Reset the stack to the position before the call.
805 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200806
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100807 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200808 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200809 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810}
811
812#undef STACK_TV
813
814/*
815 * Prepare arguments and rettv for calling a builtin or user function.
816 */
817 static int
818call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
819{
820 int idx;
821 typval_T *tv;
822
823 // Move arguments from bottom of the stack to argvars[] and add terminator.
824 for (idx = 0; idx < argcount; ++idx)
825 argvars[idx] = *STACK_TV_BOT(idx - argcount);
826 argvars[argcount].v_type = VAR_UNKNOWN;
827
828 // Result replaces the arguments on the stack.
829 if (argcount > 0)
830 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200831 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 return FAIL;
833 else
834 ++ectx->ec_stack.ga_len;
835
836 // Default return value is zero.
837 tv = STACK_TV_BOT(-1);
838 tv->v_type = VAR_NUMBER;
839 tv->vval.v_number = 0;
840
841 return OK;
842}
843
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200844// Ugly global to avoid passing the execution context around through many
845// layers.
846static ectx_T *current_ectx = NULL;
847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848/*
849 * Call a builtin function by index.
850 */
851 static int
852call_bfunc(int func_idx, int argcount, ectx_T *ectx)
853{
854 typval_T argvars[MAX_FUNC_ARGS];
855 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100856 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200857 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200858 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859
860 if (call_prepare(argcount, argvars, ectx) == FAIL)
861 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200862 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200864 // Call the builtin function. Set "current_ectx" so that when it
865 // recursively invokes call_def_function() a closure context can be set.
866 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200868 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200869 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870
871 // Clear the arguments.
872 for (idx = 0; idx < argcount; ++idx)
873 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200874
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100875 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200876 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 return OK;
878}
879
880/*
881 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100882 * If the function is compiled this will add a stack frame and set the
883 * instruction pointer at the start of the function.
884 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200885 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100886 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 */
888 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100889call_ufunc(
890 ufunc_T *ufunc,
891 partial_T *pt,
892 int argcount,
893 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200894 isn_T *iptr,
895 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896{
897 typval_T argvars[MAX_FUNC_ARGS];
898 funcexe_T funcexe;
899 int error;
900 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100901 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200902 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903
Bram Moolenaare99d4222021-06-13 14:01:26 +0200904 if (func_needs_compiling(ufunc, compile_type)
905 && compile_def_function(ufunc, FALSE, compile_type, NULL)
906 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200907 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200908 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100909 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100910 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100911 if (error != FCERR_UNKNOWN)
912 {
913 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000914 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100915 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000916 semsg(_(e_not_enough_arguments_for_function_str),
917 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100918 return FAIL;
919 }
920
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100921 // The function has been compiled, can call it quickly. For a function
922 // that was defined later: we can call it directly next time.
923 if (iptr != NULL)
924 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100925 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100926 iptr->isn_type = ISN_DCALL;
927 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
928 iptr->isn_arg.dfunc.cdf_argcount = argcount;
929 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200930 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932
933 if (call_prepare(argcount, argvars, ectx) == FAIL)
934 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200935 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000936 funcexe.fe_evaluate = TRUE;
937 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938
939 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000941 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942
943 // Clear the arguments.
944 for (idx = 0; idx < argcount; ++idx)
945 clear_tv(&argvars[idx]);
946
947 if (error != FCERR_NONE)
948 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000949 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 return FAIL;
951 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100952 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200953 // Error other than from calling the function itself.
954 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 return OK;
956}
957
958/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100959 * If command modifiers were applied restore them.
960 */
961 static void
962may_restore_cmdmod(funclocal_T *funclocal)
963{
964 if (funclocal->floc_restore_cmdmod)
965 {
966 cmdmod.cmod_filter_regmatch.regprog = NULL;
967 undo_cmdmod(&cmdmod);
968 cmdmod = funclocal->floc_save_cmdmod;
969 funclocal->floc_restore_cmdmod = FALSE;
970 }
971}
972
973/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200974 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
975 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200976 */
977 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200978vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200979{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200980 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200981}
982
983/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 * Execute a function by "name".
985 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100986 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 * Returns FAIL if not found without an error message.
988 */
989 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100990call_by_name(
991 char_u *name,
992 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100993 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200994 isn_T *iptr,
995 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996{
997 ufunc_T *ufunc;
998
999 if (builtin_function(name, -1))
1000 {
1001 int func_idx = find_internal_func(name);
1002
1003 if (func_idx < 0)
1004 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001005 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 return FAIL;
1007 return call_bfunc(func_idx, argcount, ectx);
1008 }
1009
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001010 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001011
1012 if (ufunc == NULL)
1013 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001014 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001015
1016 if (script_autoload(name, TRUE))
1017 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001018 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001019
1020 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001021 return FAIL; // bail out if loading the script caused an error
1022 }
1023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001025 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001026 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001027 {
1028 int i;
1029 typval_T *argv = STACK_TV_BOT(0) - argcount;
1030
1031 // The function can change at runtime, check that the argument
1032 // types are correct.
1033 for (i = 0; i < argcount; ++i)
1034 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001035 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001036
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001037 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001038 type = ufunc->uf_arg_types[i];
1039 else if (ufunc->uf_va_type != NULL)
1040 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001041 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001042 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001043 return FAIL;
1044 }
1045 }
1046
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001047 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049
1050 return FAIL;
1051}
1052
1053 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001054call_partial(
1055 typval_T *tv,
1056 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001057 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001059 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001060 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001062 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001063 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064
1065 if (tv->v_type == VAR_PARTIAL)
1066 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001067 partial_T *pt = tv->vval.v_partial;
1068 int i;
1069
1070 if (pt->pt_argc > 0)
1071 {
1072 // Make space for arguments from the partial, shift the "argcount"
1073 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001074 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001075 return FAIL;
1076 for (i = 1; i <= argcount; ++i)
1077 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1078 ectx->ec_stack.ga_len += pt->pt_argc;
1079 argcount += pt->pt_argc;
1080
1081 // copy the arguments from the partial onto the stack
1082 for (i = 0; i < pt->pt_argc; ++i)
1083 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1084 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001085 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086
1087 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001088 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090 name = pt->pt_name;
1091 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001092 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001094 if (name != NULL)
1095 {
1096 char_u fname_buf[FLEN_FIXED + 1];
1097 char_u *tofree = NULL;
1098 int error = FCERR_NONE;
1099 char_u *fname;
1100
1101 // May need to translate <SNR>123_ to K_SNR.
1102 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1103 if (error != FCERR_NONE)
1104 res = FAIL;
1105 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001106 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001107 vim_free(tofree);
1108 }
1109
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001110 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 {
1112 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001113 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001114 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 return FAIL;
1116 }
1117 return OK;
1118}
1119
1120/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001121 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1122 * TRUE.
1123 */
1124 static int
1125error_if_locked(int lock, char *error)
1126{
1127 if (lock & (VAR_LOCKED | VAR_FIXED))
1128 {
1129 emsg(_(error));
1130 return TRUE;
1131 }
1132 return FALSE;
1133}
1134
1135/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001136 * Give an error if "tv" is not a number and return FAIL.
1137 */
1138 static int
1139check_for_number(typval_T *tv)
1140{
1141 if (tv->v_type != VAR_NUMBER)
1142 {
1143 semsg(_(e_expected_str_but_got_str),
1144 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1145 return FAIL;
1146 }
1147 return OK;
1148}
1149
1150/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001151 * Store "tv" in variable "name".
1152 * This is for s: and g: variables.
1153 */
1154 static void
1155store_var(char_u *name, typval_T *tv)
1156{
1157 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001158 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001159
Bram Moolenaard877a572021-04-01 19:42:48 +02001160 if (tv->v_lock)
1161 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001162 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001163 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001164 restore_funccal();
1165}
1166
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001167/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001168 * Convert "tv" to a string.
1169 * Return FAIL if not allowed.
1170 */
1171 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001172do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001173{
1174 if (tv->v_type != VAR_STRING)
1175 {
1176 char_u *str;
1177
1178 if (is_2string_any)
1179 {
1180 switch (tv->v_type)
1181 {
1182 case VAR_SPECIAL:
1183 case VAR_BOOL:
1184 case VAR_NUMBER:
1185 case VAR_FLOAT:
1186 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001187
1188 case VAR_LIST:
1189 if (tolerant)
1190 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001191 char_u *s, *e, *p;
1192 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001193
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001194 ga_init2(&ga, sizeof(char_u *), 1);
1195
1196 // Convert to NL separated items, then
1197 // escape the items and replace the NL with
1198 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001199 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001200 if (str == NULL)
1201 return FAIL;
1202 s = str;
1203 while ((e = vim_strchr(s, '\n')) != NULL)
1204 {
1205 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001206 p = vim_strsave_fnameescape(s,
1207 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001208 if (p != NULL)
1209 {
1210 ga_concat(&ga, p);
1211 ga_concat(&ga, (char_u *)" ");
1212 vim_free(p);
1213 }
1214 s = e + 1;
1215 }
1216 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001217 clear_tv(tv);
1218 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001219 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001220 return OK;
1221 }
1222 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001223 default: to_string_error(tv->v_type);
1224 return FAIL;
1225 }
1226 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001227 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001228 clear_tv(tv);
1229 tv->v_type = VAR_STRING;
1230 tv->vval.v_string = str;
1231 }
1232 return OK;
1233}
1234
1235/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001236 * When the value of "sv" is a null list of dict, allocate it.
1237 */
1238 static void
1239allocate_if_null(typval_T *tv)
1240{
1241 switch (tv->v_type)
1242 {
1243 case VAR_LIST:
1244 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001245 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001246 break;
1247 case VAR_DICT:
1248 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001249 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001250 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001251 case VAR_BLOB:
1252 if (tv->vval.v_blob == NULL)
1253 (void)rettv_blob_alloc(tv);
1254 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001255 default:
1256 break;
1257 }
1258}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001259
Bram Moolenaare7525c52021-01-09 13:20:37 +01001260/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001261 * Return the character "str[index]" where "index" is the character index,
1262 * including composing characters.
1263 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001264 */
1265 char_u *
1266char_from_string(char_u *str, varnumber_T index)
1267{
1268 size_t nbyte = 0;
1269 varnumber_T nchar = index;
1270 size_t slen;
1271
1272 if (str == NULL)
1273 return NULL;
1274 slen = STRLEN(str);
1275
Bram Moolenaarff871402021-03-26 13:34:05 +01001276 // Do the same as for a list: a negative index counts from the end.
1277 // Optimization to check the first byte to be below 0x80 (and no composing
1278 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001279 if (index < 0)
1280 {
1281 int clen = 0;
1282
1283 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001284 {
1285 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1286 ++nbyte;
1287 else if (enc_utf8)
1288 nbyte += utfc_ptr2len(str + nbyte);
1289 else
1290 nbyte += mb_ptr2len(str + nbyte);
1291 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001292 nchar = clen + index;
1293 if (nchar < 0)
1294 // unlike list: index out of range results in empty string
1295 return NULL;
1296 }
1297
1298 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001299 {
1300 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1301 ++nbyte;
1302 else if (enc_utf8)
1303 nbyte += utfc_ptr2len(str + nbyte);
1304 else
1305 nbyte += mb_ptr2len(str + nbyte);
1306 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001307 if (nbyte >= slen)
1308 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001309 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001310}
1311
1312/*
1313 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001314 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001315 * If going over the end return "str_len".
1316 * If "idx" is negative count from the end, -1 is the last character.
1317 * When going over the start return -1.
1318 */
1319 static long
1320char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1321{
1322 varnumber_T nchar = idx;
1323 size_t nbyte = 0;
1324
1325 if (nchar >= 0)
1326 {
1327 while (nchar > 0 && nbyte < str_len)
1328 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001329 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001330 --nchar;
1331 }
1332 }
1333 else
1334 {
1335 nbyte = str_len;
1336 while (nchar < 0 && nbyte > 0)
1337 {
1338 --nbyte;
1339 nbyte -= mb_head_off(str, str + nbyte);
1340 ++nchar;
1341 }
1342 if (nchar < 0)
1343 return -1;
1344 }
1345 return (long)nbyte;
1346}
1347
1348/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001349 * Return the slice "str[first : last]" using character indexes. Composing
1350 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001351 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001352 * Return NULL when the result is empty.
1353 */
1354 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001355string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001356{
1357 long start_byte, end_byte;
1358 size_t slen;
1359
1360 if (str == NULL)
1361 return NULL;
1362 slen = STRLEN(str);
1363 start_byte = char_idx2byte(str, slen, first);
1364 if (start_byte < 0)
1365 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001366 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001367 end_byte = (long)slen;
1368 else
1369 {
1370 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001371 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001372 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001373 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001374 }
1375
1376 if (start_byte >= (long)slen || end_byte <= start_byte)
1377 return NULL;
1378 return vim_strnsave(str + start_byte, end_byte - start_byte);
1379}
1380
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001381/*
1382 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1383 * When "dfunc_idx" is negative don't give an error.
1384 * Returns NULL for an error.
1385 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001386 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001387get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001388{
1389 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001390 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1391 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001392 svar_T *sv;
1393
1394 if (sref->sref_seq != si->sn_script_seq)
1395 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001396 // The script was reloaded after the function was compiled, the
1397 // script_idx may not be valid.
1398 if (dfunc != NULL)
1399 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1400 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001401 return NULL;
1402 }
1403 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001404 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001405 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001406 if (dfunc != NULL)
1407 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001408 return NULL;
1409 }
1410 return sv;
1411}
1412
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001413/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001414 * Function passed to do_cmdline() for splitting a script joined by NL
1415 * characters.
1416 */
1417 static char_u *
1418get_split_sourceline(
1419 int c UNUSED,
1420 void *cookie,
1421 int indent UNUSED,
1422 getline_opt_T options UNUSED)
1423{
1424 source_cookie_T *sp = (source_cookie_T *)cookie;
1425 char_u *p;
1426 char_u *line;
1427
1428 if (*sp->nextline == NUL)
1429 return NULL;
1430 p = vim_strchr(sp->nextline, '\n');
1431 if (p == NULL)
1432 {
1433 line = vim_strsave(sp->nextline);
1434 sp->nextline += STRLEN(sp->nextline);
1435 }
1436 else
1437 {
1438 line = vim_strnsave(sp->nextline, p - sp->nextline);
1439 sp->nextline = p + 1;
1440 }
1441 return line;
1442}
1443
1444/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 * Execute a function by "name".
1446 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001447 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 */
1449 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001450call_eval_func(
1451 char_u *name,
1452 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001453 ectx_T *ectx,
1454 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455{
Bram Moolenaared677f52020-08-12 16:38:10 +02001456 int called_emsg_before = called_emsg;
1457 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001459 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001460 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001462 dictitem_T *v;
1463
1464 v = find_var(name, NULL, FALSE);
1465 if (v == NULL)
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 }
1470 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1471 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001472 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001473 return FAIL;
1474 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001475 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001477 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478}
1479
1480/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001481 * When a function reference is used, fill a partial with the information
1482 * needed, especially when it is used as a closure.
1483 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001484 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001485fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1486{
1487 pt->pt_func = ufunc;
1488 pt->pt_refcount = 1;
1489
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001490 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001491 {
1492 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1493 + ectx->ec_dfunc_idx;
1494
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001495 // The closure may need to find arguments and local variables in the
1496 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001497 pt->pt_outer.out_stack = &ectx->ec_stack;
1498 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001499 if (ectx->ec_outer_ref != NULL)
1500 {
1501 // The current context already has a context, link to that one.
1502 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1503 if (ectx->ec_outer_ref->or_partial != NULL)
1504 {
1505 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1506 ++pt->pt_outer.out_up_partial->pt_refcount;
1507 }
1508 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001509
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001510 // If this function returns and the closure is still being used, we
1511 // need to make a copy of the context (arguments and local variables).
1512 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001513 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001514 {
1515 vim_free(pt);
1516 return FAIL;
1517 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001518 // Extra variable keeps the count of closures created in the current
1519 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001520 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1521 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1522
1523 ((partial_T **)ectx->ec_funcrefs.ga_data)
1524 [ectx->ec_funcrefs.ga_len] = pt;
1525 ++pt->pt_refcount;
1526 ++ectx->ec_funcrefs.ga_len;
1527 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001528 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001529 return OK;
1530}
1531
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001532/*
1533 * Execute iptr->isn_arg.string as an Ex command.
1534 */
1535 static int
1536exec_command(isn_T *iptr)
1537{
1538 source_cookie_T cookie;
1539
1540 SOURCING_LNUM = iptr->isn_lnum;
1541 // Pass getsourceline to get an error for a missing ":end"
1542 // command.
1543 CLEAR_FIELD(cookie);
1544 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1545 if (do_cmdline(iptr->isn_arg.string,
1546 getsourceline, &cookie,
1547 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1548 || did_emsg)
1549 return FAIL;
1550 return OK;
1551}
1552
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001553// used for v_instr of typval of VAR_INSTR
1554struct instr_S {
1555 ectx_T *instr_ectx;
1556 isn_T *instr_instr;
1557};
1558
Bram Moolenaar4c137212021-04-19 16:48:48 +02001559// used for substitute_instr
1560typedef struct subs_expr_S {
1561 ectx_T *subs_ectx;
1562 isn_T *subs_instr;
1563 int subs_status;
1564} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565
1566// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001567#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568
1569// Get pointer to item at the bottom of the stack, -1 is the bottom.
1570#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001571#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 +01001572
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001573// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001574#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 +01001575
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001576// Set when calling do_debug().
1577static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001578static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001579
1580/*
1581 * When debugging lookup "name" and return the typeval.
1582 * When not found return NULL.
1583 */
1584 typval_T *
1585lookup_debug_var(char_u *name)
1586{
1587 int idx;
1588 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001589 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001590 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001591 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001592
1593 if (ectx == NULL)
1594 return NULL;
1595 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1596
1597 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001598 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001599 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001600 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001601 return STACK_TV_VAR(idx);
1602 }
1603
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001604 // Go through argument names.
1605 ufunc = dfunc->df_ufunc;
1606 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1607 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1608 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1609 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1610 - varargs_off + idx);
1611 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1612 return STACK_TV(ectx->ec_frame_idx - 1);
1613
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001614 return NULL;
1615}
1616
Bram Moolenaar26a44842021-09-02 18:49:06 +02001617/*
1618 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1619 * breakpoint was set in that function or when there is any expression.
1620 */
1621 int
1622may_break_in_function(ufunc_T *ufunc)
1623{
1624 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1625}
1626
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001627 static void
1628handle_debug(isn_T *iptr, ectx_T *ectx)
1629{
1630 char_u *line;
1631 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1632 + ectx->ec_dfunc_idx)->df_ufunc;
1633 isn_T *ni;
1634 int end_lnum = iptr->isn_lnum;
1635 garray_T ga;
1636 int lnum;
1637
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001638 if (ex_nesting_level > debug_break_level)
1639 {
1640 linenr_T breakpoint;
1641
Bram Moolenaar26a44842021-09-02 18:49:06 +02001642 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001643 return;
1644
1645 // check for the next breakpoint if needed
1646 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001647 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001648 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1649 return;
1650 }
1651
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001652 SOURCING_LNUM = iptr->isn_lnum;
1653 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001654 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001655
1656 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1657 if (ni->isn_type == ISN_DEBUG
1658 || ni->isn_type == ISN_RETURN
1659 || ni->isn_type == ISN_RETURN_VOID)
1660 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001661 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001662 break;
1663 }
1664
1665 if (end_lnum > iptr->isn_lnum)
1666 {
1667 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001668 for (lnum = iptr->isn_lnum; lnum < end_lnum
1669 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001670 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001671 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001672
Bram Moolenaar303215d2021-07-07 20:10:43 +02001673 if (p == NULL)
1674 continue; // left over from continuation line
1675 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001676 if (*p == '#')
1677 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001678 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001679 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1680 if (STRNCMP(p, "def ", 4) == 0)
1681 break;
1682 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001683 line = ga_concat_strings(&ga, " ");
1684 vim_free(ga.ga_data);
1685 }
1686 else
1687 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001688
Dominique Pelle6e969552021-06-17 13:53:41 +02001689 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001690 debug_context = NULL;
1691
1692 if (end_lnum > iptr->isn_lnum)
1693 vim_free(line);
1694}
1695
Bram Moolenaar4c137212021-04-19 16:48:48 +02001696/*
1697 * Execute instructions in execution context "ectx".
1698 * Return OK or FAIL;
1699 */
1700 static int
1701exec_instructions(ectx_T *ectx)
1702{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001703 int ret = FAIL;
1704 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001705 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001706
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001707 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001708 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001709
Bram Moolenaarff652882021-05-16 15:24:49 +02001710 // Only catch exceptions in this instruction list.
1711 ectx->ec_trylevel_at_start = trylevel;
1712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 for (;;)
1714 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001715 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001717 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001718
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001719 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001720 {
1721 line_breakcheck();
1722 breakcheck_count = 0;
1723 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001724 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001725 {
1726 // Turn CTRL-C into an exception.
1727 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001728 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001729 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001730 did_throw = TRUE;
1731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001733 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001734 {
1735 // Turn an error message into an exception.
1736 did_emsg = FALSE;
1737 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001738 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001739 did_throw = TRUE;
1740 *msg_list = NULL;
1741 }
1742
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001743 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001745 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001746 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001747 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748
1749 // An exception jumps to the first catch, finally, or returns from
1750 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001751 while (index > 0)
1752 {
1753 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001754 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001755 break;
1756 // In the catch and finally block of this try we have to go up
1757 // one level.
1758 --index;
1759 trycmd = NULL;
1760 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001761 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001763 if (trycmd->tcd_in_catch)
1764 {
1765 // exception inside ":catch", jump to ":finally" once
1766 ectx->ec_iidx = trycmd->tcd_finally_idx;
1767 trycmd->tcd_finally_idx = 0;
1768 }
1769 else
1770 // jump to first ":catch"
1771 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001772 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001773 did_throw = FALSE; // don't come back here until :endtry
1774 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 }
1776 else
1777 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001778 // Not inside try or need to return from current functions.
1779 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001780 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001781 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001782 tv = STACK_TV_BOT(0);
1783 tv->v_type = VAR_NUMBER;
1784 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001785 ++ectx->ec_stack.ga_len;
1786 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001788 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001789 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001790 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001791 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 goto done;
1793 }
1794
Bram Moolenaar4c137212021-04-19 16:48:48 +02001795 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001796 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 }
1798 continue;
1799 }
1800
Bram Moolenaar4c137212021-04-19 16:48:48 +02001801 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 switch (iptr->isn_type)
1803 {
1804 // execute Ex command line
1805 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001806 if (exec_command(iptr) == FAIL)
1807 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 break;
1809
Bram Moolenaar20677332021-06-06 17:02:53 +02001810 // execute Ex command line split at NL characters.
1811 case ISN_EXEC_SPLIT:
1812 {
1813 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001814 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001815
1816 SOURCING_LNUM = iptr->isn_lnum;
1817 CLEAR_FIELD(cookie);
1818 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1819 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001820 line = get_split_sourceline(0, &cookie, 0, 0);
1821 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001822 get_split_sourceline, &cookie,
1823 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1824 == FAIL
1825 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001826 {
1827 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001828 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001829 }
1830 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001831 }
1832 break;
1833
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001834 // execute Ex command line that is only a range
1835 case ISN_EXECRANGE:
1836 {
1837 exarg_T ea;
1838 char *error = NULL;
1839
1840 CLEAR_FIELD(ea);
1841 ea.cmdidx = CMD_SIZE;
1842 ea.addr_type = ADDR_LINES;
1843 ea.cmd = iptr->isn_arg.string;
1844 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00001845 if (ea.cmd == NULL)
1846 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001847 if (error == NULL)
1848 error = ex_range_without_command(&ea);
1849 if (error != NULL)
1850 {
1851 SOURCING_LNUM = iptr->isn_lnum;
1852 emsg(error);
1853 goto on_error;
1854 }
1855 }
1856 break;
1857
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001858 // Evaluate an expression with legacy syntax, push it onto the
1859 // stack.
1860 case ISN_LEGACY_EVAL:
1861 {
1862 char_u *arg = iptr->isn_arg.string;
1863 int res;
1864 int save_flags = cmdmod.cmod_flags;
1865
Bram Moolenaar35578162021-08-02 19:10:38 +02001866 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001867 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001868 tv = STACK_TV_BOT(0);
1869 init_tv(tv);
1870 cmdmod.cmod_flags |= CMOD_LEGACY;
1871 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1872 cmdmod.cmod_flags = save_flags;
1873 if (res == FAIL)
1874 goto on_error;
1875 ++ectx->ec_stack.ga_len;
1876 }
1877 break;
1878
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001879 // push typeval VAR_INSTR with instructions to be executed
1880 case ISN_INSTR:
1881 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001882 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001883 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001884 tv = STACK_TV_BOT(0);
1885 tv->vval.v_instr = ALLOC_ONE(instr_T);
1886 if (tv->vval.v_instr == NULL)
1887 goto on_error;
1888 ++ectx->ec_stack.ga_len;
1889
1890 tv->v_type = VAR_INSTR;
1891 tv->vval.v_instr->instr_ectx = ectx;
1892 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1893 }
1894 break;
1895
Bram Moolenaar4c137212021-04-19 16:48:48 +02001896 // execute :substitute with an expression
1897 case ISN_SUBSTITUTE:
1898 {
1899 subs_T *subs = &iptr->isn_arg.subs;
1900 source_cookie_T cookie;
1901 struct subs_expr_S *save_instr = substitute_instr;
1902 struct subs_expr_S subs_instr;
1903 int res;
1904
1905 subs_instr.subs_ectx = ectx;
1906 subs_instr.subs_instr = subs->subs_instr;
1907 subs_instr.subs_status = OK;
1908 substitute_instr = &subs_instr;
1909
1910 SOURCING_LNUM = iptr->isn_lnum;
1911 // This is very much like ISN_EXEC
1912 CLEAR_FIELD(cookie);
1913 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1914 res = do_cmdline(subs->subs_cmd,
1915 getsourceline, &cookie,
1916 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1917 substitute_instr = save_instr;
1918
1919 if (res == FAIL || did_emsg
1920 || subs_instr.subs_status == FAIL)
1921 goto on_error;
1922 }
1923 break;
1924
1925 case ISN_FINISH:
1926 goto done;
1927
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001928 case ISN_REDIRSTART:
1929 // create a dummy entry for var_redir_str()
1930 if (alloc_redir_lval() == FAIL)
1931 goto on_error;
1932
1933 // The output is stored in growarray "redir_ga" until
1934 // redirection ends.
1935 init_redir_ga();
1936 redir_vname = 1;
1937 break;
1938
1939 case ISN_REDIREND:
1940 {
1941 char_u *res = get_clear_redir_ga();
1942
1943 // End redirection, put redirected text on the stack.
1944 clear_redir_lval();
1945 redir_vname = 0;
1946
Bram Moolenaar35578162021-08-02 19:10:38 +02001947 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001948 {
1949 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001950 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001951 }
1952 tv = STACK_TV_BOT(0);
1953 tv->v_type = VAR_STRING;
1954 tv->vval.v_string = res;
1955 ++ectx->ec_stack.ga_len;
1956 }
1957 break;
1958
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001959 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001960#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001961 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1962 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001963#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001964 break;
1965
1966 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001967#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001968 {
1969 exarg_T ea;
1970 int res;
1971
1972 CLEAR_FIELD(ea);
1973 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1974 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1975 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1976 --ectx->ec_stack.ga_len;
1977 tv = STACK_TV_BOT(0);
1978 res = cexpr_core(&ea, tv);
1979 clear_tv(tv);
1980 if (res == FAIL)
1981 goto on_error;
1982 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001983#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001984 break;
1985
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001986 // execute Ex command from pieces on the stack
1987 case ISN_EXECCONCAT:
1988 {
1989 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001990 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001991 int pass;
1992 int i;
1993 char_u *cmd = NULL;
1994 char_u *str;
1995
1996 for (pass = 1; pass <= 2; ++pass)
1997 {
1998 for (i = 0; i < count; ++i)
1999 {
2000 tv = STACK_TV_BOT(i - count);
2001 str = tv->vval.v_string;
2002 if (str != NULL && *str != NUL)
2003 {
2004 if (pass == 2)
2005 STRCPY(cmd + len, str);
2006 len += STRLEN(str);
2007 }
2008 if (pass == 2)
2009 clear_tv(tv);
2010 }
2011 if (pass == 1)
2012 {
2013 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002014 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002015 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002016 len = 0;
2017 }
2018 }
2019
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002020 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002021 do_cmdline_cmd(cmd);
2022 vim_free(cmd);
2023 }
2024 break;
2025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 // execute :echo {string} ...
2027 case ISN_ECHO:
2028 {
2029 int count = iptr->isn_arg.echo.echo_count;
2030 int atstart = TRUE;
2031 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002032 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033
2034 for (idx = 0; idx < count; ++idx)
2035 {
2036 tv = STACK_TV_BOT(idx - count);
2037 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2038 &atstart, &needclr);
2039 clear_tv(tv);
2040 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002041 if (needclr)
2042 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002043 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 }
2045 break;
2046
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002047 // :execute {string} ...
2048 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002049 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002050 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002051 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002052 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002053 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002054 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002055 {
2056 int count = iptr->isn_arg.number;
2057 garray_T ga;
2058 char_u buf[NUMBUFLEN];
2059 char_u *p;
2060 int len;
2061 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002062 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002063
2064 ga_init2(&ga, 1, 80);
2065 for (idx = 0; idx < count; ++idx)
2066 {
2067 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002068 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002069 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002070 if (tv->v_type == VAR_CHANNEL
2071 || tv->v_type == VAR_JOB)
2072 {
2073 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002074 semsg(_(e_using_invalid_value_as_string_str),
2075 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002076 break;
2077 }
2078 else
2079 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002080 }
2081 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002082 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002083
2084 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002085 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002086 failed = TRUE;
2087 else
2088 {
2089 if (ga.ga_len > 0)
2090 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2091 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2092 ga.ga_len += len;
2093 }
2094 clear_tv(tv);
2095 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002096 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002097 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002098 {
2099 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002100 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002101 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002102
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002103 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002104 {
2105 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002106 {
2107 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002108 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002109 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002110 {
2111 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002112 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002113 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002114 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002115 else
2116 {
2117 msg_sb_eol();
2118 if (iptr->isn_type == ISN_ECHOMSG)
2119 {
2120 msg_attr(ga.ga_data, echo_attr);
2121 out_flush();
2122 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002123 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2124 {
2125 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2126 TRUE);
2127 ui_write((char_u *)"\r\n", 2, TRUE);
2128 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002129 else
2130 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002131 SOURCING_LNUM = iptr->isn_lnum;
2132 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002133 }
2134 }
2135 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002136 ga_clear(&ga);
2137 }
2138 break;
2139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 // load local variable or argument
2141 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002142 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002143 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002145 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 break;
2147
2148 // load v: variable
2149 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002150 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002151 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002153 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 break;
2155
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002156 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 case ISN_LOADSCRIPT:
2158 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002159 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 svar_T *sv;
2161
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002162 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002163 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002164 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002165 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002166 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002167 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002169 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 }
2171 break;
2172
2173 // load s: variable in old script
2174 case ISN_LOADS:
2175 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002176 hashtab_T *ht = &SCRIPT_VARS(
2177 iptr->isn_arg.loadstore.ls_sid);
2178 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 if (di == NULL)
2182 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002183 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002184 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002185 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 }
2187 else
2188 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002189 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002190 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002192 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 }
2194 }
2195 break;
2196
Bram Moolenaard3aac292020-04-19 14:32:17 +02002197 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002199 case ISN_LOADB:
2200 case ISN_LOADW:
2201 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002203 dictitem_T *di = NULL;
2204 hashtab_T *ht = NULL;
2205 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002206
Bram Moolenaard3aac292020-04-19 14:32:17 +02002207 switch (iptr->isn_type)
2208 {
2209 case ISN_LOADG:
2210 ht = get_globvar_ht();
2211 namespace = 'g';
2212 break;
2213 case ISN_LOADB:
2214 ht = &curbuf->b_vars->dv_hashtab;
2215 namespace = 'b';
2216 break;
2217 case ISN_LOADW:
2218 ht = &curwin->w_vars->dv_hashtab;
2219 namespace = 'w';
2220 break;
2221 case ISN_LOADT:
2222 ht = &curtab->tp_vars->dv_hashtab;
2223 namespace = 't';
2224 break;
2225 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002226 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002227 }
2228 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002229
Bram Moolenaard041f422022-01-12 19:54:00 +00002230 if (di == NULL && ht == get_globvar_ht())
2231 {
2232 // may need to load autoload script
2233 if (script_autoload(iptr->isn_arg.string, FALSE))
2234 di = find_var_in_ht(ht, 0,
2235 iptr->isn_arg.string, TRUE);
2236 if (did_emsg)
2237 goto on_error;
2238 }
2239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 if (di == NULL)
2241 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002242 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002243 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002244 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002245 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 }
2247 else
2248 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002249 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002250 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002252 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 }
2254 }
2255 break;
2256
Bram Moolenaar03290b82020-12-19 16:30:44 +01002257 // load autoload variable
2258 case ISN_LOADAUTO:
2259 {
2260 char_u *name = iptr->isn_arg.string;
2261
Bram Moolenaar35578162021-08-02 19:10:38 +02002262 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002263 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002264 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002265 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002266 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002267 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002268 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002269 }
2270 break;
2271
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002272 // load g:/b:/w:/t: namespace
2273 case ISN_LOADGDICT:
2274 case ISN_LOADBDICT:
2275 case ISN_LOADWDICT:
2276 case ISN_LOADTDICT:
2277 {
2278 dict_T *d = NULL;
2279
2280 switch (iptr->isn_type)
2281 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002282 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2283 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2284 case ISN_LOADWDICT: d = curwin->w_vars; break;
2285 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002286 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002287 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002288 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002289 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002290 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002291 tv = STACK_TV_BOT(0);
2292 tv->v_type = VAR_DICT;
2293 tv->v_lock = 0;
2294 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002295 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002296 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002297 }
2298 break;
2299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 // load &option
2301 case ISN_LOADOPT:
2302 {
2303 typval_T optval;
2304 char_u *name = iptr->isn_arg.string;
2305
Bram Moolenaara8c17702020-04-01 21:17:24 +02002306 // This is not expected to fail, name is checked during
2307 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002308 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002309 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002310 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002311 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002313 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 }
2315 break;
2316
2317 // load $ENV
2318 case ISN_LOADENV:
2319 {
2320 typval_T optval;
2321 char_u *name = iptr->isn_arg.string;
2322
Bram Moolenaar35578162021-08-02 19:10:38 +02002323 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002324 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002325 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002326 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002328 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 }
2330 break;
2331
2332 // load @register
2333 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002334 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002335 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 tv = STACK_TV_BOT(0);
2337 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002338 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002339 // This may result in NULL, which should be equivalent to an
2340 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 tv->vval.v_string = get_reg_contents(
2342 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002343 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 break;
2345
2346 // store local variable
2347 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002348 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 tv = STACK_TV_VAR(iptr->isn_arg.number);
2350 clear_tv(tv);
2351 *tv = *STACK_TV_BOT(0);
2352 break;
2353
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002354 // store s: variable in old script
2355 case ISN_STORES:
2356 {
2357 hashtab_T *ht = &SCRIPT_VARS(
2358 iptr->isn_arg.loadstore.ls_sid);
2359 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002360 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002361
Bram Moolenaar4c137212021-04-19 16:48:48 +02002362 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002363 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002364 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002365 else
2366 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002367 SOURCING_LNUM = iptr->isn_lnum;
2368 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002369 {
2370 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002371 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002372 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002373 clear_tv(&di->di_tv);
2374 di->di_tv = *STACK_TV_BOT(0);
2375 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002376 }
2377 break;
2378
2379 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 case ISN_STORESCRIPT:
2381 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002382 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2383 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002385 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002386 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002387 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002388 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002389
2390 // "const" and "final" are checked at compile time, locking
2391 // the value needs to be checked here.
2392 SOURCING_LNUM = iptr->isn_lnum;
2393 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002394 {
2395 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002396 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002397 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 clear_tv(sv->sv_tv);
2400 *sv->sv_tv = *STACK_TV_BOT(0);
2401 }
2402 break;
2403
2404 // store option
2405 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002406 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002408 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
2409 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 long n = 0;
2411 char_u *s = NULL;
2412 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002413 char_u numbuf[NUMBUFLEN];
2414 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415
Bram Moolenaar4c137212021-04-19 16:48:48 +02002416 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 tv = STACK_TV_BOT(0);
2418 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002419 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002421 if (s == NULL)
2422 s = (char_u *)"";
2423 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002424 else if (iptr->isn_type == ISN_STOREFUNCOPT)
2425 {
2426 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002427 // If the option can be set to a function reference or
2428 // a lambda and the passed value is a function
2429 // reference, then convert it to the name (string) of
2430 // the function reference.
2431 s = tv2string(tv, &tofree, numbuf, 0);
2432 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002433 {
2434 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002435 goto on_error;
2436 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002439 // must be VAR_NUMBER, CHECKTYPE makes sure
2440 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002441 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002442 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002443 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 if (msg != NULL)
2445 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002446 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002448 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 }
2451 break;
2452
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002453 // store $ENV
2454 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002455 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002456 tv = STACK_TV_BOT(0);
2457 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2458 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002459 break;
2460
2461 // store @r
2462 case ISN_STOREREG:
2463 {
2464 int reg = iptr->isn_arg.number;
2465
Bram Moolenaar4c137212021-04-19 16:48:48 +02002466 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002467 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002468 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002469 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002470 }
2471 break;
2472
2473 // store v: variable
2474 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002475 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002476 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2477 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002478 // should not happen, type is checked when compiling
2479 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002480 break;
2481
Bram Moolenaard3aac292020-04-19 14:32:17 +02002482 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002484 case ISN_STOREB:
2485 case ISN_STOREW:
2486 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002488 dictitem_T *di;
2489 hashtab_T *ht;
2490 char_u *name = iptr->isn_arg.string + 2;
2491
Bram Moolenaard3aac292020-04-19 14:32:17 +02002492 switch (iptr->isn_type)
2493 {
2494 case ISN_STOREG:
2495 ht = get_globvar_ht();
2496 break;
2497 case ISN_STOREB:
2498 ht = &curbuf->b_vars->dv_hashtab;
2499 break;
2500 case ISN_STOREW:
2501 ht = &curwin->w_vars->dv_hashtab;
2502 break;
2503 case ISN_STORET:
2504 ht = &curtab->tp_vars->dv_hashtab;
2505 break;
2506 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002507 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509
Bram Moolenaar4c137212021-04-19 16:48:48 +02002510 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002511 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002513 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 else
2515 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002516 SOURCING_LNUM = iptr->isn_lnum;
2517 if (var_check_permission(di, name) == FAIL)
2518 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 clear_tv(&di->di_tv);
2520 di->di_tv = *STACK_TV_BOT(0);
2521 }
2522 }
2523 break;
2524
Bram Moolenaar03290b82020-12-19 16:30:44 +01002525 // store an autoload variable
2526 case ISN_STOREAUTO:
2527 SOURCING_LNUM = iptr->isn_lnum;
2528 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2529 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002530 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002531 break;
2532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 // store number in local variable
2534 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002535 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 clear_tv(tv);
2537 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002538 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 break;
2540
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002541 // store value in list or dict variable
2542 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002543 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002544 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002545 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002546 typval_T *tv_dest = STACK_TV_BOT(-1);
2547 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002548
Bram Moolenaar752fc692021-01-04 21:57:11 +01002549 // Stack contains:
2550 // -3 value to be stored
2551 // -2 index
2552 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002553 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002554 SOURCING_LNUM = iptr->isn_lnum;
2555 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002556 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002557 dest_type = tv_dest->v_type;
2558 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002559 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002560 else if (dest_type == VAR_LIST
2561 && tv_idx->v_type != VAR_NUMBER)
2562 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002563 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002564 status = FAIL;
2565 }
2566 }
2567 else if (dest_type != tv_dest->v_type)
2568 {
2569 // just in case, should be OK
2570 semsg(_(e_expected_str_but_got_str),
2571 vartype_name(dest_type),
2572 vartype_name(tv_dest->v_type));
2573 status = FAIL;
2574 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002575
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002576 if (status == OK && dest_type == VAR_LIST)
2577 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002578 long lidx = (long)tv_idx->vval.v_number;
2579 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002580
2581 if (list == NULL)
2582 {
2583 emsg(_(e_list_not_set));
2584 goto on_error;
2585 }
2586 if (lidx < 0 && list->lv_len + lidx >= 0)
2587 // negative index is relative to the end
2588 lidx = list->lv_len + lidx;
2589 if (lidx < 0 || lidx > list->lv_len)
2590 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002591 semsg(_(e_list_index_out_of_range_nr), lidx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002592 goto on_error;
2593 }
2594 if (lidx < list->lv_len)
2595 {
2596 listitem_T *li = list_find(list, lidx);
2597
2598 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002599 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002600 goto on_error;
2601 // overwrite existing list item
2602 clear_tv(&li->li_tv);
2603 li->li_tv = *tv;
2604 }
2605 else
2606 {
2607 if (error_if_locked(list->lv_lock,
2608 e_cannot_change_list))
2609 goto on_error;
2610 // append to list, only fails when out of memory
2611 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002612 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002613 clear_tv(tv);
2614 }
2615 }
2616 else if (status == OK && dest_type == VAR_DICT)
2617 {
2618 char_u *key = tv_idx->vval.v_string;
2619 dict_T *dict = tv_dest->vval.v_dict;
2620 dictitem_T *di;
2621
2622 SOURCING_LNUM = iptr->isn_lnum;
2623 if (dict == NULL)
2624 {
2625 emsg(_(e_dictionary_not_set));
2626 goto on_error;
2627 }
2628 if (key == NULL)
2629 key = (char_u *)"";
2630 di = dict_find(dict, key, -1);
2631 if (di != NULL)
2632 {
2633 if (error_if_locked(di->di_tv.v_lock,
2634 e_cannot_change_dict_item))
2635 goto on_error;
2636 // overwrite existing value
2637 clear_tv(&di->di_tv);
2638 di->di_tv = *tv;
2639 }
2640 else
2641 {
2642 if (error_if_locked(dict->dv_lock,
2643 e_cannot_change_dict))
2644 goto on_error;
2645 // add to dict, only fails when out of memory
2646 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002647 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002648 clear_tv(tv);
2649 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002650 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002651 else if (status == OK && dest_type == VAR_BLOB)
2652 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002653 long lidx = (long)tv_idx->vval.v_number;
2654 blob_T *blob = tv_dest->vval.v_blob;
2655 varnumber_T nr;
2656 int error = FALSE;
2657 int len;
2658
2659 if (blob == NULL)
2660 {
2661 emsg(_(e_blob_not_set));
2662 goto on_error;
2663 }
2664 len = blob_len(blob);
2665 if (lidx < 0 && len + lidx >= 0)
2666 // negative index is relative to the end
2667 lidx = len + lidx;
2668
2669 // Can add one byte at the end.
2670 if (lidx < 0 || lidx > len)
2671 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002672 semsg(_(e_blob_index_out_of_range_nr), lidx);
Bram Moolenaar51e93322021-04-17 20:44:56 +02002673 goto on_error;
2674 }
2675 if (value_check_lock(blob->bv_lock,
2676 (char_u *)"blob", FALSE))
2677 goto on_error;
2678 nr = tv_get_number_chk(tv, &error);
2679 if (error)
2680 goto on_error;
2681 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002682 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002683 else
2684 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002685 status = FAIL;
2686 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002687 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002688
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002689 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002690 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002691 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002692 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002693 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002694 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002695 goto on_error;
2696 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002697 }
2698 break;
2699
Bram Moolenaar68452172021-04-12 21:21:02 +02002700 // store value in blob range
2701 case ISN_STORERANGE:
2702 {
2703 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2704 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2705 typval_T *tv_dest = STACK_TV_BOT(-1);
2706 int status = OK;
2707
2708 // Stack contains:
2709 // -4 value to be stored
2710 // -3 first index or "none"
2711 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002712 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002713 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002714 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002715 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002716 long n1;
2717 long n2;
2718 int error = FALSE;
2719
2720 SOURCING_LNUM = iptr->isn_lnum;
2721 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2722 if (error)
2723 status = FAIL;
2724 else
2725 {
2726 if (tv_idx2->v_type == VAR_SPECIAL
2727 && tv_idx2->vval.v_number == VVAL_NONE)
2728 n2 = list_len(tv_dest->vval.v_list) - 1;
2729 else
2730 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2731 if (error)
2732 status = FAIL;
2733 else
2734 {
2735 listitem_T *li1 = check_range_index_one(
2736 tv_dest->vval.v_list, &n1, FALSE);
2737
2738 if (li1 == NULL)
2739 status = FAIL;
2740 else
2741 {
2742 status = check_range_index_two(
2743 tv_dest->vval.v_list,
2744 &n1, li1, &n2, FALSE);
2745 if (status != FAIL)
2746 status = list_assign_range(
2747 tv_dest->vval.v_list,
2748 tv->vval.v_list,
2749 n1,
2750 n2,
2751 tv_idx2->v_type == VAR_SPECIAL,
2752 (char_u *)"=",
2753 (char_u *)"[unknown]");
2754 }
2755 }
2756 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002757 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002758 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002759 {
2760 varnumber_T n1;
2761 varnumber_T n2;
2762 int error = FALSE;
2763
2764 n1 = tv_get_number_chk(tv_idx1, &error);
2765 if (error)
2766 status = FAIL;
2767 else
2768 {
2769 if (tv_idx2->v_type == VAR_SPECIAL
2770 && tv_idx2->vval.v_number == VVAL_NONE)
2771 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2772 else
2773 n2 = tv_get_number_chk(tv_idx2, &error);
2774 if (error)
2775 status = FAIL;
2776 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002777 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002778 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002779
2780 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002781 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002782 || check_blob_range(bloblen,
2783 n1, n2, FALSE) == FAIL)
2784 status = FAIL;
2785 else
2786 status = blob_set_range(
2787 tv_dest->vval.v_blob, n1, n2, tv);
2788 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002789 }
2790 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002791 else
2792 {
2793 status = FAIL;
2794 emsg(_(e_blob_required));
2795 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002796
2797 clear_tv(tv_idx1);
2798 clear_tv(tv_idx2);
2799 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002800 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002801 clear_tv(tv);
2802
2803 if (status == FAIL)
2804 goto on_error;
2805 }
2806 break;
2807
Bram Moolenaar0186e582021-01-10 18:33:11 +01002808 // load or store variable or argument from outer scope
2809 case ISN_LOADOUTER:
2810 case ISN_STOREOUTER:
2811 {
2812 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002813 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2814 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002815
2816 while (depth > 1 && outer != NULL)
2817 {
2818 outer = outer->out_up;
2819 --depth;
2820 }
2821 if (outer == NULL)
2822 {
2823 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00002824 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
2825 || ectx->ec_outer_ref == NULL)
2826 // Possibly :def function called from legacy
2827 // context.
2828 emsg(_(e_closure_called_from_invalid_context));
2829 else
2830 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002831 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002832 }
2833 tv = ((typval_T *)outer->out_stack->ga_data)
2834 + outer->out_frame_idx + STACK_FRAME_SIZE
2835 + iptr->isn_arg.outer.outer_idx;
2836 if (iptr->isn_type == ISN_LOADOUTER)
2837 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002838 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002839 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002840 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002841 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002842 }
2843 else
2844 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002845 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002846 clear_tv(tv);
2847 *tv = *STACK_TV_BOT(0);
2848 }
2849 }
2850 break;
2851
Bram Moolenaar752fc692021-01-04 21:57:11 +01002852 // unlet item in list or dict variable
2853 case ISN_UNLETINDEX:
2854 {
2855 typval_T *tv_idx = STACK_TV_BOT(-2);
2856 typval_T *tv_dest = STACK_TV_BOT(-1);
2857 int status = OK;
2858
2859 // Stack contains:
2860 // -2 index
2861 // -1 dict or list
2862 if (tv_dest->v_type == VAR_DICT)
2863 {
2864 // unlet a dict item, index must be a string
2865 if (tv_idx->v_type != VAR_STRING)
2866 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002867 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002868 semsg(_(e_expected_str_but_got_str),
2869 vartype_name(VAR_STRING),
2870 vartype_name(tv_idx->v_type));
2871 status = FAIL;
2872 }
2873 else
2874 {
2875 dict_T *d = tv_dest->vval.v_dict;
2876 char_u *key = tv_idx->vval.v_string;
2877 dictitem_T *di = NULL;
2878
Bram Moolenaar71b76852021-12-17 20:15:38 +00002879 if (d != NULL && value_check_lock(
2880 d->dv_lock, NULL, FALSE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002881 status = FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002882 else
2883 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002884 SOURCING_LNUM = iptr->isn_lnum;
2885 if (key == NULL)
2886 key = (char_u *)"";
2887 if (d != NULL)
2888 di = dict_find(d, key, (int)STRLEN(key));
2889 if (di == NULL)
2890 {
2891 // NULL dict is equivalent to empty dict
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002892 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar71b76852021-12-17 20:15:38 +00002893 status = FAIL;
2894 }
2895 else if (var_check_fixed(di->di_flags,
2896 NULL, FALSE)
2897 || var_check_ro(di->di_flags,
2898 NULL, FALSE))
2899 status = FAIL;
2900 else
2901 dictitem_remove(d, di);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002902 }
2903 }
2904 }
2905 else if (tv_dest->v_type == VAR_LIST)
2906 {
2907 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002908 SOURCING_LNUM = iptr->isn_lnum;
2909 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002910 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002911 status = FAIL;
2912 }
2913 else
2914 {
2915 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002916 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002917
Bram Moolenaar422085f2021-12-17 20:36:15 +00002918 if (l != NULL && value_check_lock(
2919 l->lv_lock, NULL, FALSE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002920 status = FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002921 else
Bram Moolenaar422085f2021-12-17 20:36:15 +00002922 {
2923 listitem_T *li = list_find(l, n);
2924
2925 if (li == NULL)
2926 {
2927 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002928 semsg(_(e_list_index_out_of_range_nr), n);
Bram Moolenaar422085f2021-12-17 20:36:15 +00002929 status = FAIL;
2930 }
2931 else if (value_check_lock(li->li_tv.v_lock,
2932 NULL, FALSE))
2933 status = FAIL;
2934 else
2935 listitem_remove(l, li);
2936 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002937 }
2938 }
2939 else
2940 {
2941 status = FAIL;
2942 semsg(_(e_cannot_index_str),
2943 vartype_name(tv_dest->v_type));
2944 }
2945
2946 clear_tv(tv_idx);
2947 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002948 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002949 if (status == FAIL)
2950 goto on_error;
2951 }
2952 break;
2953
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002954 // unlet range of items in list variable
2955 case ISN_UNLETRANGE:
2956 {
2957 // Stack contains:
2958 // -3 index1
2959 // -2 index2
2960 // -1 dict or list
2961 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2962 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2963 typval_T *tv_dest = STACK_TV_BOT(-1);
2964 int status = OK;
2965
2966 if (tv_dest->v_type == VAR_LIST)
2967 {
2968 // indexes must be a number
2969 SOURCING_LNUM = iptr->isn_lnum;
2970 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002971 || (tv_idx2->v_type != VAR_SPECIAL
2972 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002973 {
2974 status = FAIL;
2975 }
2976 else
2977 {
2978 list_T *l = tv_dest->vval.v_list;
2979 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002980 long n2 = tv_idx2->v_type == VAR_SPECIAL
2981 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002982 listitem_T *li;
2983
2984 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002985 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002986 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002987 else
2988 {
2989 if (n1 < 0)
2990 n1 = list_idx_of_item(l, li);
2991 if (n2 < 0)
2992 {
2993 listitem_T *li2 = list_find(l, n2);
2994
2995 if (li2 == NULL)
2996 status = FAIL;
2997 else
2998 n2 = list_idx_of_item(l, li2);
2999 }
3000 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003001 && tv_idx2->v_type != VAR_SPECIAL
3002 && n2 < n1)
3003 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003004 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003005 status = FAIL;
3006 }
3007 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02003008 && list_unlet_range(l, li, NULL, n1,
3009 tv_idx2->v_type != VAR_SPECIAL, n2)
3010 == FAIL)
3011 status = FAIL;
3012 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003013 }
3014 }
3015 else
3016 {
3017 status = FAIL;
3018 SOURCING_LNUM = iptr->isn_lnum;
3019 semsg(_(e_cannot_index_str),
3020 vartype_name(tv_dest->v_type));
3021 }
3022
3023 clear_tv(tv_idx1);
3024 clear_tv(tv_idx2);
3025 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003026 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003027 if (status == FAIL)
3028 goto on_error;
3029 }
3030 break;
3031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 // push constant
3033 case ISN_PUSHNR:
3034 case ISN_PUSHBOOL:
3035 case ISN_PUSHSPEC:
3036 case ISN_PUSHF:
3037 case ISN_PUSHS:
3038 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003039 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003040 case ISN_PUSHCHANNEL:
3041 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003042 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003043 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003045 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003046 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 switch (iptr->isn_type)
3048 {
3049 case ISN_PUSHNR:
3050 tv->v_type = VAR_NUMBER;
3051 tv->vval.v_number = iptr->isn_arg.number;
3052 break;
3053 case ISN_PUSHBOOL:
3054 tv->v_type = VAR_BOOL;
3055 tv->vval.v_number = iptr->isn_arg.number;
3056 break;
3057 case ISN_PUSHSPEC:
3058 tv->v_type = VAR_SPECIAL;
3059 tv->vval.v_number = iptr->isn_arg.number;
3060 break;
3061#ifdef FEAT_FLOAT
3062 case ISN_PUSHF:
3063 tv->v_type = VAR_FLOAT;
3064 tv->vval.v_float = iptr->isn_arg.fnumber;
3065 break;
3066#endif
3067 case ISN_PUSHBLOB:
3068 blob_copy(iptr->isn_arg.blob, tv);
3069 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003070 case ISN_PUSHFUNC:
3071 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003072 if (iptr->isn_arg.string == NULL)
3073 tv->vval.v_string = NULL;
3074 else
3075 tv->vval.v_string =
3076 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003077 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003078 case ISN_PUSHCHANNEL:
3079#ifdef FEAT_JOB_CHANNEL
3080 tv->v_type = VAR_CHANNEL;
3081 tv->vval.v_channel = iptr->isn_arg.channel;
3082 if (tv->vval.v_channel != NULL)
3083 ++tv->vval.v_channel->ch_refcount;
3084#endif
3085 break;
3086 case ISN_PUSHJOB:
3087#ifdef FEAT_JOB_CHANNEL
3088 tv->v_type = VAR_JOB;
3089 tv->vval.v_job = iptr->isn_arg.job;
3090 if (tv->vval.v_job != NULL)
3091 ++tv->vval.v_job->jv_refcount;
3092#endif
3093 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 default:
3095 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003096 tv->vval.v_string = vim_strsave(
3097 iptr->isn_arg.string == NULL
3098 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 }
3100 break;
3101
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003102 case ISN_UNLET:
3103 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3104 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003105 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003106 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003107 case ISN_UNLETENV:
3108 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3109 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003110
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003111 case ISN_LOCKUNLOCK:
3112 {
3113 typval_T *lval_root_save = lval_root;
3114 int res;
3115
3116 // Stack has the local variable, argument the whole :lock
3117 // or :unlock command, like ISN_EXEC.
3118 --ectx->ec_stack.ga_len;
3119 lval_root = STACK_TV_BOT(0);
3120 res = exec_command(iptr);
3121 clear_tv(lval_root);
3122 lval_root = lval_root_save;
3123 if (res == FAIL)
3124 goto on_error;
3125 }
3126 break;
3127
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003128 case ISN_LOCKCONST:
3129 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3130 break;
3131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 // create a list from items on the stack; uses a single allocation
3133 // for the list header and the items
3134 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003135 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003136 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 break;
3138
3139 // create a dict from items on the stack
3140 case ISN_NEWDICT:
3141 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003142 int count = iptr->isn_arg.number;
3143 dict_T *dict = dict_alloc();
3144 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003145 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003146 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003148 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003149 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 for (idx = 0; idx < count; ++idx)
3151 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003152 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003154 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003155 key = tv->vval.v_string == NULL
3156 ? (char_u *)"" : tv->vval.v_string;
3157 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003158 if (item != NULL)
3159 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003160 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar74409f62022-01-01 15:58:22 +00003161 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003162 dict_unref(dict);
3163 goto on_error;
3164 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003165 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003167 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003168 {
3169 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003170 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3173 item->di_tv.v_lock = 0;
3174 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003175 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003176 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003177 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003178 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 }
3181
3182 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003183 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003184 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003185 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003187 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 tv = STACK_TV_BOT(-1);
3189 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003190 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 tv->vval.v_dict = dict;
3192 ++dict->dv_refcount;
3193 }
3194 break;
3195
3196 // call a :def function
3197 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003198 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003199 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3200 NULL,
3201 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003202 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003203 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 break;
3205
3206 // call a builtin function
3207 case ISN_BCALL:
3208 SOURCING_LNUM = iptr->isn_lnum;
3209 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3210 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003211 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003212 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 break;
3214
3215 // call a funcref or partial
3216 case ISN_PCALL:
3217 {
3218 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3219 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003220 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221
3222 SOURCING_LNUM = iptr->isn_lnum;
3223 if (pfunc->cpf_top)
3224 {
3225 // funcref is above the arguments
3226 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3227 }
3228 else
3229 {
3230 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003231 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003232 partial_tv = *STACK_TV_BOT(0);
3233 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003235 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003236 if (tv == &partial_tv)
3237 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003239 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 }
3241 break;
3242
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003243 case ISN_PCALL_END:
3244 // PCALL finished, arguments have been consumed and replaced by
3245 // the return value. Now clear the funcref from the stack,
3246 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003247 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003248 clear_tv(STACK_TV_BOT(-1));
3249 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3250 break;
3251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 // call a user defined function or funcref/partial
3253 case ISN_UCALL:
3254 {
3255 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3256
3257 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003258 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003259 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003260 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 }
3262 break;
3263
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003264 // return from a :def function call without a value
3265 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003266 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003267 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003268 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003269 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003270 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003271 tv->vval.v_number = 0;
3272 tv->v_lock = 0;
3273 // FALLTHROUGH
3274
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003275 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 case ISN_RETURN:
3277 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003278 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003279 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003280
3281 if (trystack->ga_len > 0)
3282 trycmd = ((trycmd_T *)trystack->ga_data)
3283 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003284 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003285 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003287 // jump to ":finally" or ":endtry"
3288 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003289 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003290 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003291 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 trycmd->tcd_return = TRUE;
3293 }
3294 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003295 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 }
3297 break;
3298
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003299 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 case ISN_FUNCREF:
3301 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003302 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003303 ufunc_T *ufunc;
3304 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003307 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003308 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003309 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003310 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003311 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003312 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003313 if (funcref->fr_func_name == NULL)
3314 {
3315 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3316 + funcref->fr_dfunc_idx;
3317
3318 ufunc = pt_dfunc->df_ufunc;
3319 }
3320 else
3321 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003322 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003323 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003324 if (ufunc == NULL)
3325 {
3326 SOURCING_LNUM = iptr->isn_lnum;
3327 emsg(_(e_function_reference_invalid));
3328 goto theend;
3329 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003330 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003331 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003333 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 tv->vval.v_partial = pt;
3335 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003336 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 }
3338 break;
3339
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003340 // Create a global function from a lambda.
3341 case ISN_NEWFUNC:
3342 {
3343 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3344
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003345 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003346 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003347 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003348 }
3349 break;
3350
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003351 // List functions
3352 case ISN_DEF:
3353 if (iptr->isn_arg.string == NULL)
3354 list_functions(NULL);
3355 else
3356 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003357 exarg_T ea;
3358 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003359
3360 CLEAR_FIELD(ea);
3361 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003362 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3363 define_function(&ea, NULL, &lines_to_free);
3364 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003365 }
3366 break;
3367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 // jump if a condition is met
3369 case ISN_JUMP:
3370 {
3371 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003372 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 int jump = TRUE;
3374
3375 if (when != JUMP_ALWAYS)
3376 {
3377 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003378 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003379 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003380 || when == JUMP_IF_COND_TRUE)
3381 {
3382 SOURCING_LNUM = iptr->isn_lnum;
3383 jump = tv_get_bool_chk(tv, &error);
3384 if (error)
3385 goto on_error;
3386 }
3387 else
3388 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003390 || when == JUMP_AND_KEEP_IF_FALSE
3391 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003393 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 {
3395 // drop the value from the stack
3396 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003397 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 }
3399 }
3400 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003401 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 }
3403 break;
3404
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003405 // Jump if an argument with a default value was already set and not
3406 // v:none.
3407 case ISN_JUMP_IF_ARG_SET:
3408 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3409 if (tv->v_type != VAR_UNKNOWN
3410 && !(tv->v_type == VAR_SPECIAL
3411 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003412 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003413 break;
3414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 // top of a for loop
3416 case ISN_FOR:
3417 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003418 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 typval_T *idxtv =
3420 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3421
Bram Moolenaar35578162021-08-02 19:10:38 +02003422 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003423 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003424 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003425 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003426 list_T *list = ltv->vval.v_list;
3427
3428 // push the next item from the list
3429 ++idxtv->vval.v_number;
3430 if (list == NULL
3431 || idxtv->vval.v_number >= list->lv_len)
3432 {
3433 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003434 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3435 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003436 }
3437 else if (list->lv_first == &range_list_item)
3438 {
3439 // non-materialized range() list
3440 tv = STACK_TV_BOT(0);
3441 tv->v_type = VAR_NUMBER;
3442 tv->v_lock = 0;
3443 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003445 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003446 }
3447 else
3448 {
3449 listitem_T *li = list_find(list,
3450 idxtv->vval.v_number);
3451
3452 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003453 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003454 }
3455 }
3456 else if (ltv->v_type == VAR_STRING)
3457 {
3458 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003459
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003460 // The index is for the last byte of the previous
3461 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003462 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003463 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003464 {
3465 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003466 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3467 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003468 }
3469 else
3470 {
3471 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3472
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003473 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003474 tv = STACK_TV_BOT(0);
3475 tv->v_type = VAR_STRING;
3476 tv->vval.v_string = vim_strnsave(
3477 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003478 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003479 idxtv->vval.v_number += clen - 1;
3480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003482 else if (ltv->v_type == VAR_BLOB)
3483 {
3484 blob_T *blob = ltv->vval.v_blob;
3485
3486 // When we get here the first time make a copy of the
3487 // blob, so that the iteration still works when it is
3488 // changed.
3489 if (idxtv->vval.v_number == -1 && blob != NULL)
3490 {
3491 blob_copy(blob, ltv);
3492 blob_unref(blob);
3493 blob = ltv->vval.v_blob;
3494 }
3495
3496 // The index is for the previous byte.
3497 ++idxtv->vval.v_number;
3498 if (blob == NULL
3499 || idxtv->vval.v_number >= blob_len(blob))
3500 {
3501 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003502 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3503 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003504 }
3505 else
3506 {
3507 // Push the next byte from the blob.
3508 tv = STACK_TV_BOT(0);
3509 tv->v_type = VAR_NUMBER;
3510 tv->vval.v_number = blob_get(blob,
3511 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003512 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003513 }
3514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 else
3516 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003517 semsg(_(e_for_loop_on_str_not_supported),
3518 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003519 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 }
3521 }
3522 break;
3523
3524 // start of ":try" block
3525 case ISN_TRY:
3526 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003527 trycmd_T *trycmd = NULL;
3528
Bram Moolenaar35578162021-08-02 19:10:38 +02003529 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003530 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003531 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3532 + ectx->ec_trystack.ga_len;
3533 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003535 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003536 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3537 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003538 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003539 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003540 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003541 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003542 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003543 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 }
3545 break;
3546
3547 case ISN_PUSHEXC:
3548 if (current_exception == NULL)
3549 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003550 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003552 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003554 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003555 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003557 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003559 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 tv->vval.v_string = vim_strsave(
3561 (char_u *)current_exception->value);
3562 break;
3563
3564 case ISN_CATCH:
3565 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003566 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567
Bram Moolenaar4c137212021-04-19 16:48:48 +02003568 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 if (trystack->ga_len > 0)
3570 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003571 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 + trystack->ga_len - 1;
3573 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003574 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 }
3576 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003577 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 catch_exception(current_exception);
3579 }
3580 break;
3581
Bram Moolenaarc150c092021-02-13 15:02:46 +01003582 case ISN_TRYCONT:
3583 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003584 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003585 trycont_T *trycont = &iptr->isn_arg.trycont;
3586 int i;
3587 trycmd_T *trycmd;
3588 int iidx = trycont->tct_where;
3589
3590 if (trystack->ga_len < trycont->tct_levels)
3591 {
3592 siemsg("TRYCONT: expected %d levels, found %d",
3593 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003594 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003595 }
3596 // Make :endtry jump to any outer try block and the last
3597 // :endtry inside the loop to the loop start.
3598 for (i = trycont->tct_levels; i > 0; --i)
3599 {
3600 trycmd = ((trycmd_T *)trystack->ga_data)
3601 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003602 // Add one to tcd_cont to be able to jump to
3603 // instruction with index zero.
3604 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003605 iidx = trycmd->tcd_finally_idx == 0
3606 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003607 }
3608 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003610 }
3611 break;
3612
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003613 case ISN_FINALLY:
3614 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003615 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003616 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3617 + trystack->ga_len - 1;
3618
3619 // Reset the index to avoid a return statement jumps here
3620 // again.
3621 trycmd->tcd_finally_idx = 0;
3622 break;
3623 }
3624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 // end of ":try" block
3626 case ISN_ENDTRY:
3627 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003628 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629
3630 if (trystack->ga_len > 0)
3631 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003632 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 --trystack->ga_len;
3635 --trylevel;
3636 trycmd = ((trycmd_T *)trystack->ga_data)
3637 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003638 if (trycmd->tcd_did_throw)
3639 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003640 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 {
3642 // discard the exception
3643 if (caught_stack == current_exception)
3644 caught_stack = caught_stack->caught;
3645 discard_current_exception();
3646 }
3647
3648 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003649 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003650
Bram Moolenaar4c137212021-04-19 16:48:48 +02003651 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003652 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003653 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003654 clear_tv(STACK_TV_BOT(0));
3655 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003656 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003657 // handling :continue: jump to outer try block or
3658 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003659 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 }
3661 }
3662 break;
3663
3664 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003665 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003666 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003667
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003668 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3669 {
3670 // throwing an exception while using "silent!" causes
3671 // the function to abort but not display an error.
3672 tv = STACK_TV_BOT(-1);
3673 clear_tv(tv);
3674 tv->v_type = VAR_NUMBER;
3675 tv->vval.v_number = 0;
3676 goto done;
3677 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003678 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003679 tv = STACK_TV_BOT(0);
3680 if (tv->vval.v_string == NULL
3681 || *skipwhite(tv->vval.v_string) == NUL)
3682 {
3683 vim_free(tv->vval.v_string);
3684 SOURCING_LNUM = iptr->isn_lnum;
3685 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003686 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003687 }
3688
3689 // Inside a "catch" we need to first discard the caught
3690 // exception.
3691 if (trystack->ga_len > 0)
3692 {
3693 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3694 + trystack->ga_len - 1;
3695 if (trycmd->tcd_caught && current_exception != NULL)
3696 {
3697 // discard the exception
3698 if (caught_stack == current_exception)
3699 caught_stack = caught_stack->caught;
3700 discard_current_exception();
3701 trycmd->tcd_caught = FALSE;
3702 }
3703 }
3704
3705 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3706 == FAIL)
3707 {
3708 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003709 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003710 }
3711 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 break;
3714
3715 // compare with special values
3716 case ISN_COMPAREBOOL:
3717 case ISN_COMPARESPECIAL:
3718 {
3719 typval_T *tv1 = STACK_TV_BOT(-2);
3720 typval_T *tv2 = STACK_TV_BOT(-1);
3721 varnumber_T arg1 = tv1->vval.v_number;
3722 varnumber_T arg2 = tv2->vval.v_number;
3723 int res;
3724
3725 switch (iptr->isn_arg.op.op_type)
3726 {
3727 case EXPR_EQUAL: res = arg1 == arg2; break;
3728 case EXPR_NEQUAL: res = arg1 != arg2; break;
3729 default: res = 0; break;
3730 }
3731
Bram Moolenaar4c137212021-04-19 16:48:48 +02003732 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 tv1->v_type = VAR_BOOL;
3734 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3735 }
3736 break;
3737
3738 // Operation with two number arguments
3739 case ISN_OPNR:
3740 case ISN_COMPARENR:
3741 {
3742 typval_T *tv1 = STACK_TV_BOT(-2);
3743 typval_T *tv2 = STACK_TV_BOT(-1);
3744 varnumber_T arg1 = tv1->vval.v_number;
3745 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003746 varnumber_T res = 0;
3747 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748
3749 switch (iptr->isn_arg.op.op_type)
3750 {
3751 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003752 case EXPR_DIV: if (arg2 == 0)
3753 div_zero = TRUE;
3754 else
3755 res = arg1 / arg2;
3756 break;
3757 case EXPR_REM: if (arg2 == 0)
3758 div_zero = TRUE;
3759 else
3760 res = arg1 % arg2;
3761 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 case EXPR_SUB: res = arg1 - arg2; break;
3763 case EXPR_ADD: res = arg1 + arg2; break;
3764
3765 case EXPR_EQUAL: res = arg1 == arg2; break;
3766 case EXPR_NEQUAL: res = arg1 != arg2; break;
3767 case EXPR_GREATER: res = arg1 > arg2; break;
3768 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3769 case EXPR_SMALLER: res = arg1 < arg2; break;
3770 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003771 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 }
3773
Bram Moolenaar4c137212021-04-19 16:48:48 +02003774 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 if (iptr->isn_type == ISN_COMPARENR)
3776 {
3777 tv1->v_type = VAR_BOOL;
3778 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3779 }
3780 else
3781 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003782 if (div_zero)
3783 {
3784 SOURCING_LNUM = iptr->isn_lnum;
3785 emsg(_(e_divide_by_zero));
3786 goto on_error;
3787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 }
3789 break;
3790
3791 // Computation with two float arguments
3792 case ISN_OPFLOAT:
3793 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003794#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 {
3796 typval_T *tv1 = STACK_TV_BOT(-2);
3797 typval_T *tv2 = STACK_TV_BOT(-1);
3798 float_T arg1 = tv1->vval.v_float;
3799 float_T arg2 = tv2->vval.v_float;
3800 float_T res = 0;
3801 int cmp = FALSE;
3802
3803 switch (iptr->isn_arg.op.op_type)
3804 {
3805 case EXPR_MULT: res = arg1 * arg2; break;
3806 case EXPR_DIV: res = arg1 / arg2; break;
3807 case EXPR_SUB: res = arg1 - arg2; break;
3808 case EXPR_ADD: res = arg1 + arg2; break;
3809
3810 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3811 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3812 case EXPR_GREATER: cmp = arg1 > arg2; break;
3813 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3814 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3815 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3816 default: cmp = 0; break;
3817 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003818 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 if (iptr->isn_type == ISN_COMPAREFLOAT)
3820 {
3821 tv1->v_type = VAR_BOOL;
3822 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3823 }
3824 else
3825 tv1->vval.v_float = res;
3826 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003827#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 break;
3829
3830 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00003831 case ISN_COMPAREDICT:
3832 case ISN_COMPAREFUNC:
3833 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 case ISN_COMPAREBLOB:
3835 {
3836 typval_T *tv1 = STACK_TV_BOT(-2);
3837 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00003838 exprtype_T exprtype = iptr->isn_arg.op.op_type;
3839 int ic = iptr->isn_arg.op.op_ic;
3840 int res = FALSE;
3841 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842
Bram Moolenaar265f8112021-12-19 12:33:05 +00003843 SOURCING_LNUM = iptr->isn_lnum;
3844 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00003846 status = typval_compare_list(tv1, tv2,
3847 exprtype, ic, &res);
3848 }
3849 else if (iptr->isn_type == ISN_COMPAREDICT)
3850 {
3851 status = typval_compare_dict(tv1, tv2,
3852 exprtype, ic, &res);
3853 }
3854 else if (iptr->isn_type == ISN_COMPAREFUNC)
3855 {
3856 status = typval_compare_func(tv1, tv2,
3857 exprtype, ic, &res);
3858 }
3859 else if (iptr->isn_type == ISN_COMPARESTRING)
3860 {
3861 status = typval_compare_string(tv1, tv2,
3862 exprtype, ic, &res);
3863 }
3864 else
3865 {
3866 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003868 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 clear_tv(tv1);
3870 clear_tv(tv2);
3871 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003872 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3873 if (status == FAIL)
3874 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 }
3876 break;
3877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 case ISN_COMPAREANY:
3879 {
3880 typval_T *tv1 = STACK_TV_BOT(-2);
3881 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003882 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003884 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885
Bram Moolenaareb26f432020-09-14 16:50:05 +02003886 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003887 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003889 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00003890 if (status == FAIL)
3891 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 }
3893 break;
3894
3895 case ISN_ADDLIST:
3896 case ISN_ADDBLOB:
3897 {
3898 typval_T *tv1 = STACK_TV_BOT(-2);
3899 typval_T *tv2 = STACK_TV_BOT(-1);
3900
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003901 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003903 {
3904 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3905 && tv1->vval.v_list != NULL)
3906 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3907 NULL);
3908 else
3909 eval_addlist(tv1, tv2);
3910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 else
3912 eval_addblob(tv1, tv2);
3913 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003914 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 }
3916 break;
3917
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003918 case ISN_LISTAPPEND:
3919 {
3920 typval_T *tv1 = STACK_TV_BOT(-2);
3921 typval_T *tv2 = STACK_TV_BOT(-1);
3922 list_T *l = tv1->vval.v_list;
3923
3924 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00003925 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003926 if (l == NULL)
3927 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003928 emsg(_(e_cannot_add_to_null_list));
3929 goto on_error;
3930 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00003931 if (value_check_lock(l->lv_lock, NULL, FALSE))
3932 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003933 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003934 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003935 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003936 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003937 }
3938 break;
3939
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003940 case ISN_BLOBAPPEND:
3941 {
3942 typval_T *tv1 = STACK_TV_BOT(-2);
3943 typval_T *tv2 = STACK_TV_BOT(-1);
3944 blob_T *b = tv1->vval.v_blob;
3945 int error = FALSE;
3946 varnumber_T n;
3947
3948 // add a number to a blob
3949 if (b == NULL)
3950 {
3951 SOURCING_LNUM = iptr->isn_lnum;
3952 emsg(_(e_cannot_add_to_null_blob));
3953 goto on_error;
3954 }
3955 n = tv_get_number_chk(tv2, &error);
3956 if (error)
3957 goto on_error;
3958 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003959 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003960 }
3961 break;
3962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 // Computation with two arguments of unknown type
3964 case ISN_OPANY:
3965 {
3966 typval_T *tv1 = STACK_TV_BOT(-2);
3967 typval_T *tv2 = STACK_TV_BOT(-1);
3968 varnumber_T n1, n2;
3969#ifdef FEAT_FLOAT
3970 float_T f1 = 0, f2 = 0;
3971#endif
3972 int error = FALSE;
3973
3974 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3975 {
3976 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3977 {
3978 eval_addlist(tv1, tv2);
3979 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003980 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 break;
3982 }
3983 else if (tv1->v_type == VAR_BLOB
3984 && tv2->v_type == VAR_BLOB)
3985 {
3986 eval_addblob(tv1, tv2);
3987 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003988 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 break;
3990 }
3991 }
3992#ifdef FEAT_FLOAT
3993 if (tv1->v_type == VAR_FLOAT)
3994 {
3995 f1 = tv1->vval.v_float;
3996 n1 = 0;
3997 }
3998 else
3999#endif
4000 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004001 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 n1 = tv_get_number_chk(tv1, &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 (tv2->v_type == VAR_FLOAT)
4007 f1 = n1;
4008#endif
4009 }
4010#ifdef FEAT_FLOAT
4011 if (tv2->v_type == VAR_FLOAT)
4012 {
4013 f2 = tv2->vval.v_float;
4014 n2 = 0;
4015 }
4016 else
4017#endif
4018 {
4019 n2 = tv_get_number_chk(tv2, &error);
4020 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004021 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022#ifdef FEAT_FLOAT
4023 if (tv1->v_type == VAR_FLOAT)
4024 f2 = n2;
4025#endif
4026 }
4027#ifdef FEAT_FLOAT
4028 // if there is a float on either side the result is a float
4029 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4030 {
4031 switch (iptr->isn_arg.op.op_type)
4032 {
4033 case EXPR_MULT: f1 = f1 * f2; break;
4034 case EXPR_DIV: f1 = f1 / f2; break;
4035 case EXPR_SUB: f1 = f1 - f2; break;
4036 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004037 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004038 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004039 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 }
4041 clear_tv(tv1);
4042 clear_tv(tv2);
4043 tv1->v_type = VAR_FLOAT;
4044 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004045 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 }
4047 else
4048#endif
4049 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004050 int failed = FALSE;
4051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 switch (iptr->isn_arg.op.op_type)
4053 {
4054 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004055 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4056 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004057 goto on_error;
4058 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 case EXPR_SUB: n1 = n1 - n2; break;
4060 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004061 default: n1 = num_modulus(n1, n2, &failed);
4062 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004063 goto on_error;
4064 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 }
4066 clear_tv(tv1);
4067 clear_tv(tv2);
4068 tv1->v_type = VAR_NUMBER;
4069 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004070 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 }
4072 }
4073 break;
4074
4075 case ISN_CONCAT:
4076 {
4077 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4078 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4079 char_u *res;
4080
4081 res = concat_str(str1, str2);
4082 clear_tv(STACK_TV_BOT(-2));
4083 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004084 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 STACK_TV_BOT(-1)->vval.v_string = res;
4086 }
4087 break;
4088
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004089 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004090 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004091 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004092 int is_slice = iptr->isn_type == ISN_STRSLICE;
4093 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004094 char_u *res;
4095
4096 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004097 // string slice: string is at stack-3, first index at
4098 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004099 if (is_slice)
4100 {
4101 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004102 n1 = tv->vval.v_number;
4103 }
4104
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004105 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004106 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004107
Bram Moolenaar4c137212021-04-19 16:48:48 +02004108 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004109 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004110 if (is_slice)
4111 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004112 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004113 else
4114 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004115 // single character (including composing characters).
4116 // If the index is too big or negative the result is
4117 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004118 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004119 vim_free(tv->vval.v_string);
4120 tv->vval.v_string = res;
4121 }
4122 break;
4123
4124 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004125 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004126 case ISN_BLOBINDEX:
4127 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004129 int is_slice = iptr->isn_type == ISN_LISTSLICE
4130 || iptr->isn_type == ISN_BLOBSLICE;
4131 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4132 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004133 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004134 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135
4136 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004137 // list slice: list is at stack-3, indexes at stack-2 and
4138 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004139 // Same for blob.
4140 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141
4142 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004143 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004145
4146 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 {
Bram Moolenaared591872020-08-15 22:14:53 +02004148 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004149 n1 = tv->vval.v_number;
4150 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 }
Bram Moolenaared591872020-08-15 22:14:53 +02004152
Bram Moolenaar4c137212021-04-19 16:48:48 +02004153 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004154 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004155 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004156 if (is_blob)
4157 {
4158 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4159 n1, n2, FALSE, tv) == FAIL)
4160 goto on_error;
4161 }
4162 else
4163 {
4164 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4165 n1, n2, FALSE, tv, TRUE) == FAIL)
4166 goto on_error;
4167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 }
4169 break;
4170
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004171 case ISN_ANYINDEX:
4172 case ISN_ANYSLICE:
4173 {
4174 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4175 typval_T *var1, *var2;
4176 int res;
4177
4178 // index: composite is at stack-2, index at stack-1
4179 // slice: composite is at stack-3, indexes at stack-2 and
4180 // stack-1
4181 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004182 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004183 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4184 goto on_error;
4185 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4186 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004187 res = eval_index_inner(tv, is_slice, var1, var2,
4188 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004189 clear_tv(var1);
4190 if (is_slice)
4191 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004192 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004193 if (res == FAIL)
4194 goto on_error;
4195 }
4196 break;
4197
Bram Moolenaar9af78762020-06-16 11:34:42 +02004198 case ISN_SLICE:
4199 {
4200 list_T *list;
4201 int count = iptr->isn_arg.number;
4202
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004203 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004204 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004205 list = tv->vval.v_list;
4206
4207 // no error for short list, expect it to be checked earlier
4208 if (list != NULL && list->lv_len >= count)
4209 {
4210 list_T *newlist = list_slice(list,
4211 count, list->lv_len - 1);
4212
4213 if (newlist != NULL)
4214 {
4215 list_unref(list);
4216 tv->vval.v_list = newlist;
4217 ++newlist->lv_refcount;
4218 }
4219 }
4220 }
4221 break;
4222
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004223 case ISN_GETITEM:
4224 {
4225 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004226 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004227
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004228 // Get list item: list is at stack-1, push item.
4229 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004230 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4231 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004232
Bram Moolenaar35578162021-08-02 19:10:38 +02004233 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004234 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004235 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004236 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004237
4238 // Useful when used in unpack assignment. Reset at
4239 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004240 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004241 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004242 }
4243 break;
4244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 case ISN_MEMBER:
4246 {
4247 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004248 char_u *key;
4249 dictitem_T *di;
4250
4251 // dict member: dict is at stack-2, key at stack-1
4252 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004253 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004254 dict = tv->vval.v_dict;
4255
4256 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004257 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004258 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004259 if (key == NULL)
4260 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004261
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004262 if ((di = dict_find(dict, key, -1)) == NULL)
4263 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004264 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004265 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004266
4267 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004268 // stack contents makes sense and the dict stack is
4269 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004270 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004271 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004272 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004273 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004274 tv->v_type = VAR_NUMBER;
4275 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004276 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004277 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004278 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004279 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004280 // Put the dict used on the dict stack, it might be used by
4281 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004282 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004283 if (dict_stack_save(tv) == FAIL)
4284 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004285 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004286 }
4287 break;
4288
4289 // dict member with string key
4290 case ISN_STRINGMEMBER:
4291 {
4292 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 dictitem_T *di;
4294
4295 tv = STACK_TV_BOT(-1);
4296 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4297 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004298 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004299 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004300 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 }
4302 dict = tv->vval.v_dict;
4303
4304 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4305 == NULL)
4306 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004307 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004308 semsg(_(e_key_not_present_in_dictionary), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004309 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004311 // Put the dict used on the dict stack, it might be used by
4312 // a dict function later.
4313 if (dict_stack_save(tv) == FAIL)
4314 goto on_fatal_error;
4315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004317 }
4318 break;
4319
4320 case ISN_CLEARDICT:
4321 dict_stack_drop();
4322 break;
4323
4324 case ISN_USEDICT:
4325 {
4326 typval_T *dict_tv = dict_stack_get_tv();
4327
4328 // Turn "dict.Func" into a partial for "Func" bound to
4329 // "dict". Don't do this when "Func" is already a partial
4330 // that was bound explicitly (pt_auto is FALSE).
4331 tv = STACK_TV_BOT(-1);
4332 if (dict_tv != NULL
4333 && dict_tv->v_type == VAR_DICT
4334 && dict_tv->vval.v_dict != NULL
4335 && (tv->v_type == VAR_FUNC
4336 || (tv->v_type == VAR_PARTIAL
4337 && (tv->vval.v_partial->pt_auto
4338 || tv->vval.v_partial->pt_dict == NULL))))
4339 dict_tv->vval.v_dict =
4340 make_partial(dict_tv->vval.v_dict, tv);
4341 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 }
4343 break;
4344
4345 case ISN_NEGATENR:
4346 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004347 if (tv->v_type != VAR_NUMBER
4348#ifdef FEAT_FLOAT
4349 && tv->v_type != VAR_FLOAT
4350#endif
4351 )
4352 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004353 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004354 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004355 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004356 }
4357#ifdef FEAT_FLOAT
4358 if (tv->v_type == VAR_FLOAT)
4359 tv->vval.v_float = -tv->vval.v_float;
4360 else
4361#endif
4362 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 break;
4364
4365 case ISN_CHECKNR:
4366 {
4367 int error = FALSE;
4368
4369 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004370 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004372 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 (void)tv_get_number_chk(tv, &error);
4374 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004375 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
4377 break;
4378
4379 case ISN_CHECKTYPE:
4380 {
4381 checktype_T *ct = &iptr->isn_arg.type;
4382
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004383 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004384 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385 if (!ectx->ec_where.wt_variable)
4386 ectx->ec_where.wt_index = ct->ct_arg_idx;
4387 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4388 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004389 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004390 if (!ectx->ec_where.wt_variable)
4391 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004392
4393 // number 0 is FALSE, number 1 is TRUE
4394 if (tv->v_type == VAR_NUMBER
4395 && ct->ct_type->tt_type == VAR_BOOL
4396 && (tv->vval.v_number == 0
4397 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004399 tv->v_type = VAR_BOOL;
4400 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004401 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 }
4403 }
4404 break;
4405
Bram Moolenaar9af78762020-06-16 11:34:42 +02004406 case ISN_CHECKLEN:
4407 {
4408 int min_len = iptr->isn_arg.checklen.cl_min_len;
4409 list_T *list = NULL;
4410
4411 tv = STACK_TV_BOT(-1);
4412 if (tv->v_type == VAR_LIST)
4413 list = tv->vval.v_list;
4414 if (list == NULL || list->lv_len < min_len
4415 || (list->lv_len > min_len
4416 && !iptr->isn_arg.checklen.cl_more_OK))
4417 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004418 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004419 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004420 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004421 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004422 }
4423 }
4424 break;
4425
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004426 case ISN_SETTYPE:
4427 {
4428 checktype_T *ct = &iptr->isn_arg.type;
4429
4430 tv = STACK_TV_BOT(-1);
4431 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4432 {
4433 free_type(tv->vval.v_dict->dv_type);
4434 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4435 }
4436 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4437 {
4438 free_type(tv->vval.v_list->lv_type);
4439 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4440 }
4441 }
4442 break;
4443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004445 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 {
4447 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004448 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004450 if (iptr->isn_type == ISN_2BOOL)
4451 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004452 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004453 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004454 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004455 n = !n;
4456 }
4457 else
4458 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004459 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004460 SOURCING_LNUM = iptr->isn_lnum;
4461 n = tv_get_bool_chk(tv, &error);
4462 if (error)
4463 goto on_error;
4464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 clear_tv(tv);
4466 tv->v_type = VAR_BOOL;
4467 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4468 }
4469 break;
4470
4471 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004472 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004473 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004474 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4475 iptr->isn_type == ISN_2STRING_ANY,
4476 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004477 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 break;
4479
Bram Moolenaar08597872020-12-10 19:43:40 +01004480 case ISN_RANGE:
4481 {
4482 exarg_T ea;
4483 char *errormsg;
4484
Bram Moolenaarece0b872021-01-08 20:40:45 +01004485 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004486 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004487 ea.addr_type = ADDR_LINES;
4488 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004489 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004490 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004491 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004492
Bram Moolenaar35578162021-08-02 19:10:38 +02004493 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004494 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004495 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004496 tv = STACK_TV_BOT(-1);
4497 tv->v_type = VAR_NUMBER;
4498 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004499 if (ea.addr_count == 0)
4500 tv->vval.v_number = curwin->w_cursor.lnum;
4501 else
4502 tv->vval.v_number = ea.line2;
4503 }
4504 break;
4505
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004506 case ISN_PUT:
4507 {
4508 int regname = iptr->isn_arg.put.put_regname;
4509 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4510 char_u *expr = NULL;
4511 int dir = FORWARD;
4512
Bram Moolenaar08597872020-12-10 19:43:40 +01004513 if (lnum < -2)
4514 {
4515 // line number was put on the stack by ISN_RANGE
4516 tv = STACK_TV_BOT(-1);
4517 curwin->w_cursor.lnum = tv->vval.v_number;
4518 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4519 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004520 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004521 }
4522 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004523 // :put! above cursor
4524 dir = BACKWARD;
4525 else if (lnum >= 0)
4526 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004527
4528 if (regname == '=')
4529 {
4530 tv = STACK_TV_BOT(-1);
4531 if (tv->v_type == VAR_STRING)
4532 expr = tv->vval.v_string;
4533 else
4534 {
4535 expr = typval2string(tv, TRUE); // allocates value
4536 clear_tv(tv);
4537 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004538 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004539 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004540 check_cursor();
4541 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4542 vim_free(expr);
4543 }
4544 break;
4545
Bram Moolenaar02194d22020-10-24 23:08:38 +02004546 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004547 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4548 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4549 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4550 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004551 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4552 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004553 break;
4554
Bram Moolenaar02194d22020-10-24 23:08:38 +02004555 case ISN_CMDMOD_REV:
4556 // filter regprog is owned by the instruction, don't free it
4557 cmdmod.cmod_filter_regmatch.regprog = NULL;
4558 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004559 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4560 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004561 break;
4562
Bram Moolenaar792f7862020-11-23 08:31:18 +01004563 case ISN_UNPACK:
4564 {
4565 int count = iptr->isn_arg.unpack.unp_count;
4566 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4567 list_T *l;
4568 listitem_T *li;
4569 int i;
4570
4571 // Check there is a valid list to unpack.
4572 tv = STACK_TV_BOT(-1);
4573 if (tv->v_type != VAR_LIST)
4574 {
4575 SOURCING_LNUM = iptr->isn_lnum;
4576 emsg(_(e_for_argument_must_be_sequence_of_lists));
4577 goto on_error;
4578 }
4579 l = tv->vval.v_list;
4580 if (l == NULL
4581 || l->lv_len < (semicolon ? count - 1 : count))
4582 {
4583 SOURCING_LNUM = iptr->isn_lnum;
4584 emsg(_(e_list_value_does_not_have_enough_items));
4585 goto on_error;
4586 }
4587 else if (!semicolon && l->lv_len > count)
4588 {
4589 SOURCING_LNUM = iptr->isn_lnum;
4590 emsg(_(e_list_value_has_more_items_than_targets));
4591 goto on_error;
4592 }
4593
4594 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004595 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004596 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004597 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004598
4599 // Variable after semicolon gets a list with the remaining
4600 // items.
4601 if (semicolon)
4602 {
4603 list_T *rem_list =
4604 list_alloc_with_items(l->lv_len - count + 1);
4605
4606 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004607 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004608 tv = STACK_TV_BOT(-count);
4609 tv->vval.v_list = rem_list;
4610 ++rem_list->lv_refcount;
4611 tv->v_lock = 0;
4612 li = l->lv_first;
4613 for (i = 0; i < count - 1; ++i)
4614 li = li->li_next;
4615 for (i = 0; li != NULL; ++i)
4616 {
4617 list_set_item(rem_list, i, &li->li_tv);
4618 li = li->li_next;
4619 }
4620 --count;
4621 }
4622
4623 // Produce the values in reverse order, first item last.
4624 li = l->lv_first;
4625 for (i = 0; i < count; ++i)
4626 {
4627 tv = STACK_TV_BOT(-i - 1);
4628 copy_tv(&li->li_tv, tv);
4629 li = li->li_next;
4630 }
4631
4632 list_unref(l);
4633 }
4634 break;
4635
Bram Moolenaarb2049902021-01-24 12:53:53 +01004636 case ISN_PROF_START:
4637 case ISN_PROF_END:
4638 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004639#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004640 funccall_T cookie;
4641 ufunc_T *cur_ufunc =
4642 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004643 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004644
4645 cookie.func = cur_ufunc;
4646 if (iptr->isn_type == ISN_PROF_START)
4647 {
4648 func_line_start(&cookie, iptr->isn_lnum);
4649 // if we get here the instruction is executed
4650 func_line_exec(&cookie);
4651 }
4652 else
4653 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004654#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004655 }
4656 break;
4657
Bram Moolenaare99d4222021-06-13 14:01:26 +02004658 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004659 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004660 break;
4661
Bram Moolenaar389df252020-07-09 21:20:47 +02004662 case ISN_SHUFFLE:
4663 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004664 typval_T tmp_tv;
4665 int item = iptr->isn_arg.shuffle.shfl_item;
4666 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004667
4668 tmp_tv = *STACK_TV_BOT(-item);
4669 for ( ; up > 0 && item > 1; --up)
4670 {
4671 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4672 --item;
4673 }
4674 *STACK_TV_BOT(-item) = tmp_tv;
4675 }
4676 break;
4677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004679 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004681 ectx->ec_where.wt_index = 0;
4682 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 break;
4684 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004685 continue;
4686
Bram Moolenaard032f342020-07-18 18:13:02 +02004687func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004688 // Restore previous function. If the frame pointer is where we started
4689 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004690 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004691 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004692
Bram Moolenaar4c137212021-04-19 16:48:48 +02004693 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004694 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004695 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004696 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004697
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004698on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004699 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004700 // If "emsg_silent" is set then ignore the error, unless it was set
4701 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004702 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004703 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004704 {
4705 // If a sequence of instructions causes an error while ":silent!"
4706 // was used, restore the stack length and jump ahead to restoring
4707 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004708 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004709 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004710 while (ectx->ec_stack.ga_len
4711 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004712 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004713 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004714 clear_tv(STACK_TV_BOT(0));
4715 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004716 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4717 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004718 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004719 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004720 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004721on_fatal_error:
4722 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004723 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004724 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004725 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 }
4727
4728done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004729 ret = OK;
4730theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004731 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004732 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4733 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004734}
4735
4736/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004737 * Execute the instructions from a VAR_INSTR typeval and put the result in
4738 * "rettv".
4739 * Return OK or FAIL.
4740 */
4741 int
4742exe_typval_instr(typval_T *tv, typval_T *rettv)
4743{
4744 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4745 isn_T *save_instr = ectx->ec_instr;
4746 int save_iidx = ectx->ec_iidx;
4747 int res;
4748
4749 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4750 res = exec_instructions(ectx);
4751 if (res == OK)
4752 {
4753 *rettv = *STACK_TV_BOT(-1);
4754 --ectx->ec_stack.ga_len;
4755 }
4756
4757 ectx->ec_instr = save_instr;
4758 ectx->ec_iidx = save_iidx;
4759
4760 return res;
4761}
4762
4763/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004764 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4765 * "substitute_instr".
4766 */
4767 char_u *
4768exe_substitute_instr(void)
4769{
4770 ectx_T *ectx = substitute_instr->subs_ectx;
4771 isn_T *save_instr = ectx->ec_instr;
4772 int save_iidx = ectx->ec_iidx;
4773 char_u *res;
4774
4775 ectx->ec_instr = substitute_instr->subs_instr;
4776 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004777 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004778 typval_T *tv = STACK_TV_BOT(-1);
4779
Bram Moolenaar27523602021-06-05 21:36:19 +02004780 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004781 --ectx->ec_stack.ga_len;
4782 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004783 }
4784 else
4785 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004786 substitute_instr->subs_status = FAIL;
4787 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789
Bram Moolenaar4c137212021-04-19 16:48:48 +02004790 ectx->ec_instr = save_instr;
4791 ectx->ec_iidx = save_iidx;
4792
4793 return res;
4794}
4795
4796/*
4797 * Call a "def" function from old Vim script.
4798 * Return OK or FAIL.
4799 */
4800 int
4801call_def_function(
4802 ufunc_T *ufunc,
4803 int argc_arg, // nr of arguments
4804 typval_T *argv, // arguments
4805 partial_T *partial, // optional partial for context
4806 typval_T *rettv) // return value
4807{
4808 ectx_T ectx; // execution context
4809 int argc = argc_arg;
4810 typval_T *tv;
4811 int idx;
4812 int ret = FAIL;
4813 int defcount = ufunc->uf_args.ga_len - argc;
4814 sctx_T save_current_sctx = current_sctx;
4815 int did_emsg_before = did_emsg_cumul + did_emsg;
4816 int save_suppress_errthrow = suppress_errthrow;
4817 msglist_T **saved_msg_list = NULL;
4818 msglist_T *private_msg_list = NULL;
4819 int save_emsg_silent_def = emsg_silent_def;
4820 int save_did_emsg_def = did_emsg_def;
4821 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004822 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004823
4824// Get pointer to item in the stack.
4825#undef STACK_TV
4826#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4827
4828// Get pointer to item at the bottom of the stack, -1 is the bottom.
4829#undef STACK_TV_BOT
4830#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4831
4832// Get pointer to a local variable on the stack. Negative for arguments.
4833#undef STACK_TV_VAR
4834#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4835
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004836 // Update uf_has_breakpoint if needed.
4837 update_has_breakpoint(ufunc);
4838
Bram Moolenaar4c137212021-04-19 16:48:48 +02004839 if (ufunc->uf_def_status == UF_NOT_COMPILED
4840 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004841 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4842 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004843 == FAIL))
4844 {
4845 if (did_emsg_cumul + did_emsg == did_emsg_before)
4846 semsg(_(e_function_is_not_compiled_str),
4847 printable_func_name(ufunc));
4848 return FAIL;
4849 }
4850
4851 {
4852 // Check the function was really compiled.
4853 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4854 + ufunc->uf_dfunc_idx;
4855 if (INSTRUCTIONS(dfunc) == NULL)
4856 {
4857 iemsg("using call_def_function() on not compiled function");
4858 return FAIL;
4859 }
4860 }
4861
4862 // If depth of calling is getting too high, don't execute the function.
4863 orig_funcdepth = funcdepth_get();
4864 if (funcdepth_increment() == FAIL)
4865 return FAIL;
4866
4867 CLEAR_FIELD(ectx);
4868 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4869 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004870 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004871 {
4872 funcdepth_decrement();
4873 return FAIL;
4874 }
4875 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4876 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4877 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004878 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004879
4880 idx = argc - ufunc->uf_args.ga_len;
4881 if (idx > 0 && ufunc->uf_va_name == NULL)
4882 {
4883 if (idx == 1)
4884 emsg(_(e_one_argument_too_many));
4885 else
4886 semsg(_(e_nr_arguments_too_many), idx);
4887 goto failed_early;
4888 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004889 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4890 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004891 {
4892 if (idx == -1)
4893 emsg(_(e_one_argument_too_few));
4894 else
4895 semsg(_(e_nr_arguments_too_few), -idx);
4896 goto failed_early;
4897 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004898
4899 // Put arguments on the stack, but no more than what the function expects.
4900 // A lambda can be called with more arguments than it uses.
4901 for (idx = 0; idx < argc
4902 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4903 ++idx)
4904 {
4905 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4906 && argv[idx].v_type == VAR_SPECIAL
4907 && argv[idx].vval.v_number == VVAL_NONE)
4908 {
4909 // Use the default value.
4910 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4911 }
4912 else
4913 {
4914 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4915 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004916 ufunc->uf_arg_types[idx], &argv[idx],
4917 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004918 goto failed_early;
4919 copy_tv(&argv[idx], STACK_TV_BOT(0));
4920 }
4921 ++ectx.ec_stack.ga_len;
4922 }
4923
4924 // Turn varargs into a list. Empty list if no args.
4925 if (ufunc->uf_va_name != NULL)
4926 {
4927 int vararg_count = argc - ufunc->uf_args.ga_len;
4928
4929 if (vararg_count < 0)
4930 vararg_count = 0;
4931 else
4932 argc -= vararg_count;
4933 if (exe_newlist(vararg_count, &ectx) == FAIL)
4934 goto failed_early;
4935
4936 // Check the type of the list items.
4937 tv = STACK_TV_BOT(-1);
4938 if (ufunc->uf_va_type != NULL
4939 && ufunc->uf_va_type != &t_list_any
4940 && ufunc->uf_va_type->tt_member != &t_any
4941 && tv->vval.v_list != NULL)
4942 {
4943 type_T *expected = ufunc->uf_va_type->tt_member;
4944 listitem_T *li = tv->vval.v_list->lv_first;
4945
4946 for (idx = 0; idx < vararg_count; ++idx)
4947 {
4948 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004949 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004950 goto failed_early;
4951 li = li->li_next;
4952 }
4953 }
4954
4955 if (defcount > 0)
4956 // Move varargs list to below missing default arguments.
4957 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4958 --ectx.ec_stack.ga_len;
4959 }
4960
4961 // Make space for omitted arguments, will store default value below.
4962 // Any varargs list goes after them.
4963 if (defcount > 0)
4964 for (idx = 0; idx < defcount; ++idx)
4965 {
4966 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4967 ++ectx.ec_stack.ga_len;
4968 }
4969 if (ufunc->uf_va_name != NULL)
4970 ++ectx.ec_stack.ga_len;
4971
4972 // Frame pointer points to just after arguments.
4973 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4974 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4975
4976 {
4977 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4978 + ufunc->uf_dfunc_idx;
4979 ufunc_T *base_ufunc = dfunc->df_ufunc;
4980
4981 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4982 // by copy_func().
4983 if (partial != NULL || base_ufunc->uf_partial != NULL)
4984 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004985 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4986 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004987 goto failed_early;
4988 if (partial != NULL)
4989 {
4990 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4991 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004992 if (current_ectx->ec_outer_ref != NULL
4993 && current_ectx->ec_outer_ref->or_outer != NULL)
4994 ectx.ec_outer_ref->or_outer =
4995 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004996 }
4997 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004998 {
4999 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
5000 ++partial->pt_refcount;
5001 ectx.ec_outer_ref->or_partial = partial;
5002 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005003 }
5004 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005005 {
5006 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5007 ++base_ufunc->uf_partial->pt_refcount;
5008 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5009 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005010 }
5011 }
5012
5013 // dummy frame entries
5014 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5015 {
5016 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5017 ++ectx.ec_stack.ga_len;
5018 }
5019
5020 {
5021 // Reserve space for local variables and any closure reference count.
5022 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5023 + ufunc->uf_dfunc_idx;
5024
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005025 // Initialize variables to zero. That avoids having to generate
5026 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005027 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005028 {
5029 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5030 STACK_TV_VAR(idx)->vval.v_number = 0;
5031 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005032 ectx.ec_stack.ga_len += dfunc->df_varcount;
5033 if (dfunc->df_has_closure)
5034 {
5035 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5036 STACK_TV_VAR(idx)->vval.v_number = 0;
5037 ++ectx.ec_stack.ga_len;
5038 }
5039
5040 ectx.ec_instr = INSTRUCTIONS(dfunc);
5041 }
5042
5043 // Following errors are in the function, not the caller.
5044 // Commands behave like vim9script.
5045 estack_push_ufunc(ufunc, 1);
5046 current_sctx = ufunc->uf_script_ctx;
5047 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5048
5049 // Use a specific location for storing error messages to be converted to an
5050 // exception.
5051 saved_msg_list = msg_list;
5052 msg_list = &private_msg_list;
5053
5054 // Do turn errors into exceptions.
5055 suppress_errthrow = FALSE;
5056
5057 // Do not delete the function while executing it.
5058 ++ufunc->uf_calls;
5059
5060 // When ":silent!" was used before calling then we still abort the
5061 // function. If ":silent!" is used in the function then we don't.
5062 emsg_silent_def = emsg_silent;
5063 did_emsg_def = 0;
5064
5065 ectx.ec_where.wt_index = 0;
5066 ectx.ec_where.wt_variable = FALSE;
5067
5068 // Execute the instructions until done.
5069 ret = exec_instructions(&ectx);
5070 if (ret == OK)
5071 {
5072 // function finished, get result from the stack.
5073 if (ufunc->uf_ret_type == &t_void)
5074 {
5075 rettv->v_type = VAR_VOID;
5076 }
5077 else
5078 {
5079 tv = STACK_TV_BOT(-1);
5080 *rettv = *tv;
5081 tv->v_type = VAR_UNKNOWN;
5082 }
5083 }
5084
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005085 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005086 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5087 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005088
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005089 // Deal with any remaining closures, they may be in use somewhere.
5090 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005091 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005092 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005093 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005094 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005095
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005096 estack_pop();
5097 current_sctx = save_current_sctx;
5098
Bram Moolenaar2336c372021-12-06 15:06:54 +00005099 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5100 // Function was unreferenced while being used, free it now.
5101 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005102
Bram Moolenaar352134b2020-10-17 22:04:08 +02005103 if (*msg_list != NULL && saved_msg_list != NULL)
5104 {
5105 msglist_T **plist = saved_msg_list;
5106
5107 // Append entries from the current msg_list (uncaught exceptions) to
5108 // the saved msg_list.
5109 while (*plist != NULL)
5110 plist = &(*plist)->next;
5111
5112 *plist = *msg_list;
5113 }
5114 msg_list = saved_msg_list;
5115
Bram Moolenaar4c137212021-04-19 16:48:48 +02005116 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005117 {
5118 cmdmod.cmod_filter_regmatch.regprog = NULL;
5119 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005120 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005121 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005122 emsg_silent_def = save_emsg_silent_def;
5123 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005124
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005125failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005126 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5128 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005129 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005132 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005133 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005134 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005135 if (ectx.ec_outer_ref->or_outer_allocated)
5136 vim_free(ectx.ec_outer_ref->or_outer);
5137 partial_unref(ectx.ec_outer_ref->or_partial);
5138 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005139 }
5140
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005141 // Not sure if this is necessary.
5142 suppress_errthrow = save_suppress_errthrow;
5143
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005144 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5145 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005146 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005147 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005148 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 return ret;
5150}
5151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005153 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5154 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5155 * "pfx" is prefixed to every line.
5156 */
5157 static void
5158list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5159{
5160 int line_idx = 0;
5161 int prev_current = 0;
5162 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005163 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005164
5165 for (current = 0; current < instr_count; ++current)
5166 {
5167 isn_T *iptr = &instr[current];
5168 char *line;
5169
5170 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005171 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005172 while (line_idx < iptr->isn_lnum
5173 && line_idx < ufunc->uf_lines.ga_len)
5174 {
5175 if (current > prev_current)
5176 {
5177 msg_puts("\n\n");
5178 prev_current = current;
5179 }
5180 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5181 if (line != NULL)
5182 msg(line);
5183 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005184 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5185 {
5186 int first_def_arg = ufunc->uf_args.ga_len
5187 - ufunc->uf_def_args.ga_len;
5188
5189 if (def_arg_idx > 0)
5190 msg_puts("\n\n");
5191 msg_start();
5192 msg_puts(" ");
5193 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5194 first_def_arg + def_arg_idx]);
5195 msg_puts(" = ");
5196 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5197 msg_clr_eos();
5198 msg_end();
5199 }
5200 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005201
5202 switch (iptr->isn_type)
5203 {
5204 case ISN_EXEC:
5205 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5206 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005207 case ISN_EXEC_SPLIT:
5208 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5209 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005210 case ISN_EXECRANGE:
5211 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5212 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005213 case ISN_LEGACY_EVAL:
5214 smsg("%s%4d EVAL legacy %s", pfx, current,
5215 iptr->isn_arg.string);
5216 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005217 case ISN_REDIRSTART:
5218 smsg("%s%4d REDIR", pfx, current);
5219 break;
5220 case ISN_REDIREND:
5221 smsg("%s%4d REDIR END%s", pfx, current,
5222 iptr->isn_arg.number ? " append" : "");
5223 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005224 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005225#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005226 smsg("%s%4d CEXPR pre %s", pfx, current,
5227 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005228#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005229 break;
5230 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005231#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005232 {
5233 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5234
5235 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5236 cexpr_get_auname(cer->cer_cmdidx),
5237 cer->cer_forceit ? "!" : "",
5238 cer->cer_cmdline);
5239 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005240#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005241 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005242 case ISN_INSTR:
5243 {
5244 smsg("%s%4d INSTR", pfx, current);
5245 list_instructions(" ", iptr->isn_arg.instr,
5246 INT_MAX, NULL);
5247 msg(" -------------");
5248 }
5249 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005250 case ISN_SUBSTITUTE:
5251 {
5252 subs_T *subs = &iptr->isn_arg.subs;
5253
5254 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5255 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5256 msg(" -------------");
5257 }
5258 break;
5259 case ISN_EXECCONCAT:
5260 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5261 (varnumber_T)iptr->isn_arg.number);
5262 break;
5263 case ISN_ECHO:
5264 {
5265 echo_T *echo = &iptr->isn_arg.echo;
5266
5267 smsg("%s%4d %s %d", pfx, current,
5268 echo->echo_with_white ? "ECHO" : "ECHON",
5269 echo->echo_count);
5270 }
5271 break;
5272 case ISN_EXECUTE:
5273 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005274 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005275 break;
5276 case ISN_ECHOMSG:
5277 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005278 (varnumber_T)(iptr->isn_arg.number));
5279 break;
5280 case ISN_ECHOCONSOLE:
5281 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5282 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005283 break;
5284 case ISN_ECHOERR:
5285 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005286 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005287 break;
5288 case ISN_LOAD:
5289 {
5290 if (iptr->isn_arg.number < 0)
5291 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5292 (varnumber_T)(iptr->isn_arg.number
5293 + STACK_FRAME_SIZE));
5294 else
5295 smsg("%s%4d LOAD $%lld", pfx, current,
5296 (varnumber_T)(iptr->isn_arg.number));
5297 }
5298 break;
5299 case ISN_LOADOUTER:
5300 {
5301 if (iptr->isn_arg.number < 0)
5302 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5303 iptr->isn_arg.outer.outer_depth,
5304 iptr->isn_arg.outer.outer_idx
5305 + STACK_FRAME_SIZE);
5306 else
5307 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5308 iptr->isn_arg.outer.outer_depth,
5309 iptr->isn_arg.outer.outer_idx);
5310 }
5311 break;
5312 case ISN_LOADV:
5313 smsg("%s%4d LOADV v:%s", pfx, current,
5314 get_vim_var_name(iptr->isn_arg.number));
5315 break;
5316 case ISN_LOADSCRIPT:
5317 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005318 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5319 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5320 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005321
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005322 sv = get_script_svar(sref, -1);
5323 if (sv == NULL)
5324 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5325 pfx, current, si->sn_name);
5326 else
5327 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005328 sv->sv_name,
5329 sref->sref_idx,
5330 si->sn_name);
5331 }
5332 break;
5333 case ISN_LOADS:
5334 {
5335 scriptitem_T *si = SCRIPT_ITEM(
5336 iptr->isn_arg.loadstore.ls_sid);
5337
5338 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5339 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5340 }
5341 break;
5342 case ISN_LOADAUTO:
5343 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5344 break;
5345 case ISN_LOADG:
5346 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5347 break;
5348 case ISN_LOADB:
5349 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5350 break;
5351 case ISN_LOADW:
5352 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5353 break;
5354 case ISN_LOADT:
5355 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5356 break;
5357 case ISN_LOADGDICT:
5358 smsg("%s%4d LOAD g:", pfx, current);
5359 break;
5360 case ISN_LOADBDICT:
5361 smsg("%s%4d LOAD b:", pfx, current);
5362 break;
5363 case ISN_LOADWDICT:
5364 smsg("%s%4d LOAD w:", pfx, current);
5365 break;
5366 case ISN_LOADTDICT:
5367 smsg("%s%4d LOAD t:", pfx, current);
5368 break;
5369 case ISN_LOADOPT:
5370 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5371 break;
5372 case ISN_LOADENV:
5373 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5374 break;
5375 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005376 smsg("%s%4d LOADREG @%c", pfx, current,
5377 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005378 break;
5379
5380 case ISN_STORE:
5381 if (iptr->isn_arg.number < 0)
5382 smsg("%s%4d STORE arg[%lld]", pfx, current,
5383 iptr->isn_arg.number + STACK_FRAME_SIZE);
5384 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005385 smsg("%s%4d STORE $%lld", pfx, current,
5386 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005387 break;
5388 case ISN_STOREOUTER:
5389 {
5390 if (iptr->isn_arg.number < 0)
5391 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5392 iptr->isn_arg.outer.outer_depth,
5393 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5394 else
5395 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5396 iptr->isn_arg.outer.outer_depth,
5397 iptr->isn_arg.outer.outer_idx);
5398 }
5399 break;
5400 case ISN_STOREV:
5401 smsg("%s%4d STOREV v:%s", pfx, current,
5402 get_vim_var_name(iptr->isn_arg.number));
5403 break;
5404 case ISN_STOREAUTO:
5405 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5406 break;
5407 case ISN_STOREG:
5408 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5409 break;
5410 case ISN_STOREB:
5411 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5412 break;
5413 case ISN_STOREW:
5414 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5415 break;
5416 case ISN_STORET:
5417 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5418 break;
5419 case ISN_STORES:
5420 {
5421 scriptitem_T *si = SCRIPT_ITEM(
5422 iptr->isn_arg.loadstore.ls_sid);
5423
5424 smsg("%s%4d STORES %s in %s", pfx, current,
5425 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5426 }
5427 break;
5428 case ISN_STORESCRIPT:
5429 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005430 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5431 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5432 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005433
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005434 sv = get_script_svar(sref, -1);
5435 if (sv == NULL)
5436 smsg("%s%4d STORESCRIPT [deleted] in %s",
5437 pfx, current, si->sn_name);
5438 else
5439 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005440 sv->sv_name,
5441 sref->sref_idx,
5442 si->sn_name);
5443 }
5444 break;
5445 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005446 case ISN_STOREFUNCOPT:
5447 smsg("%s%4d %s &%s", pfx, current,
5448 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005449 iptr->isn_arg.storeopt.so_name);
5450 break;
5451 case ISN_STOREENV:
5452 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5453 break;
5454 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005455 smsg("%s%4d STOREREG @%c", pfx, current,
5456 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005457 break;
5458 case ISN_STORENR:
5459 smsg("%s%4d STORE %lld in $%d", pfx, current,
5460 iptr->isn_arg.storenr.stnr_val,
5461 iptr->isn_arg.storenr.stnr_idx);
5462 break;
5463
5464 case ISN_STOREINDEX:
5465 smsg("%s%4d STOREINDEX %s", pfx, current,
5466 vartype_name(iptr->isn_arg.vartype));
5467 break;
5468
5469 case ISN_STORERANGE:
5470 smsg("%s%4d STORERANGE", pfx, current);
5471 break;
5472
5473 // constants
5474 case ISN_PUSHNR:
5475 smsg("%s%4d PUSHNR %lld", pfx, current,
5476 (varnumber_T)(iptr->isn_arg.number));
5477 break;
5478 case ISN_PUSHBOOL:
5479 case ISN_PUSHSPEC:
5480 smsg("%s%4d PUSH %s", pfx, current,
5481 get_var_special_name(iptr->isn_arg.number));
5482 break;
5483 case ISN_PUSHF:
5484#ifdef FEAT_FLOAT
5485 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5486#endif
5487 break;
5488 case ISN_PUSHS:
5489 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5490 break;
5491 case ISN_PUSHBLOB:
5492 {
5493 char_u *r;
5494 char_u numbuf[NUMBUFLEN];
5495 char_u *tofree;
5496
5497 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5498 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5499 vim_free(tofree);
5500 }
5501 break;
5502 case ISN_PUSHFUNC:
5503 {
5504 char *name = (char *)iptr->isn_arg.string;
5505
5506 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5507 name == NULL ? "[none]" : name);
5508 }
5509 break;
5510 case ISN_PUSHCHANNEL:
5511#ifdef FEAT_JOB_CHANNEL
5512 {
5513 channel_T *channel = iptr->isn_arg.channel;
5514
5515 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5516 channel == NULL ? 0 : channel->ch_id);
5517 }
5518#endif
5519 break;
5520 case ISN_PUSHJOB:
5521#ifdef FEAT_JOB_CHANNEL
5522 {
5523 typval_T tv;
5524 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005525 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005526
5527 tv.v_type = VAR_JOB;
5528 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005529 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005530 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5531 }
5532#endif
5533 break;
5534 case ISN_PUSHEXC:
5535 smsg("%s%4d PUSH v:exception", pfx, current);
5536 break;
5537 case ISN_UNLET:
5538 smsg("%s%4d UNLET%s %s", pfx, current,
5539 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5540 iptr->isn_arg.unlet.ul_name);
5541 break;
5542 case ISN_UNLETENV:
5543 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5544 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5545 iptr->isn_arg.unlet.ul_name);
5546 break;
5547 case ISN_UNLETINDEX:
5548 smsg("%s%4d UNLETINDEX", pfx, current);
5549 break;
5550 case ISN_UNLETRANGE:
5551 smsg("%s%4d UNLETRANGE", pfx, current);
5552 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005553 case ISN_LOCKUNLOCK:
5554 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5555 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005556 case ISN_LOCKCONST:
5557 smsg("%s%4d LOCKCONST", pfx, current);
5558 break;
5559 case ISN_NEWLIST:
5560 smsg("%s%4d NEWLIST size %lld", pfx, current,
5561 (varnumber_T)(iptr->isn_arg.number));
5562 break;
5563 case ISN_NEWDICT:
5564 smsg("%s%4d NEWDICT size %lld", pfx, current,
5565 (varnumber_T)(iptr->isn_arg.number));
5566 break;
5567
5568 // function call
5569 case ISN_BCALL:
5570 {
5571 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5572
5573 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5574 internal_func_name(cbfunc->cbf_idx),
5575 cbfunc->cbf_argcount);
5576 }
5577 break;
5578 case ISN_DCALL:
5579 {
5580 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5581 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5582 + cdfunc->cdf_idx;
5583
5584 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005585 printable_func_name(df->df_ufunc),
5586 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005587 }
5588 break;
5589 case ISN_UCALL:
5590 {
5591 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5592
5593 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5594 cufunc->cuf_name, cufunc->cuf_argcount);
5595 }
5596 break;
5597 case ISN_PCALL:
5598 {
5599 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5600
5601 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5602 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5603 }
5604 break;
5605 case ISN_PCALL_END:
5606 smsg("%s%4d PCALL end", pfx, current);
5607 break;
5608 case ISN_RETURN:
5609 smsg("%s%4d RETURN", pfx, current);
5610 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005611 case ISN_RETURN_VOID:
5612 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005613 break;
5614 case ISN_FUNCREF:
5615 {
5616 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005617 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005618
Bram Moolenaar38453522021-11-28 22:00:12 +00005619 if (funcref->fr_func_name == NULL)
5620 {
5621 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5622 + funcref->fr_dfunc_idx;
5623 name = df->df_ufunc->uf_name;
5624 }
5625 else
5626 name = funcref->fr_func_name;
5627 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 }
5629 break;
5630
5631 case ISN_NEWFUNC:
5632 {
5633 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5634
5635 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5636 newfunc->nf_lambda, newfunc->nf_global);
5637 }
5638 break;
5639
5640 case ISN_DEF:
5641 {
5642 char_u *name = iptr->isn_arg.string;
5643
5644 smsg("%s%4d DEF %s", pfx, current,
5645 name == NULL ? (char_u *)"" : name);
5646 }
5647 break;
5648
5649 case ISN_JUMP:
5650 {
5651 char *when = "?";
5652
5653 switch (iptr->isn_arg.jump.jump_when)
5654 {
5655 case JUMP_ALWAYS:
5656 when = "JUMP";
5657 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005658 case JUMP_NEVER:
5659 iemsg("JUMP_NEVER should not be used");
5660 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005661 case JUMP_AND_KEEP_IF_TRUE:
5662 when = "JUMP_AND_KEEP_IF_TRUE";
5663 break;
5664 case JUMP_IF_FALSE:
5665 when = "JUMP_IF_FALSE";
5666 break;
5667 case JUMP_AND_KEEP_IF_FALSE:
5668 when = "JUMP_AND_KEEP_IF_FALSE";
5669 break;
5670 case JUMP_IF_COND_FALSE:
5671 when = "JUMP_IF_COND_FALSE";
5672 break;
5673 case JUMP_IF_COND_TRUE:
5674 when = "JUMP_IF_COND_TRUE";
5675 break;
5676 }
5677 smsg("%s%4d %s -> %d", pfx, current, when,
5678 iptr->isn_arg.jump.jump_where);
5679 }
5680 break;
5681
5682 case ISN_JUMP_IF_ARG_SET:
5683 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5684 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5685 iptr->isn_arg.jump.jump_where);
5686 break;
5687
5688 case ISN_FOR:
5689 {
5690 forloop_T *forloop = &iptr->isn_arg.forloop;
5691
5692 smsg("%s%4d FOR $%d -> %d", pfx, current,
5693 forloop->for_idx, forloop->for_end);
5694 }
5695 break;
5696
5697 case ISN_TRY:
5698 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005699 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005700
5701 if (try->try_ref->try_finally == 0)
5702 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5703 pfx, current,
5704 try->try_ref->try_catch,
5705 try->try_ref->try_endtry);
5706 else
5707 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5708 pfx, current,
5709 try->try_ref->try_catch,
5710 try->try_ref->try_finally,
5711 try->try_ref->try_endtry);
5712 }
5713 break;
5714 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005715 smsg("%s%4d CATCH", pfx, current);
5716 break;
5717 case ISN_TRYCONT:
5718 {
5719 trycont_T *trycont = &iptr->isn_arg.trycont;
5720
5721 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5722 trycont->tct_levels,
5723 trycont->tct_levels == 1 ? "" : "s",
5724 trycont->tct_where);
5725 }
5726 break;
5727 case ISN_FINALLY:
5728 smsg("%s%4d FINALLY", pfx, current);
5729 break;
5730 case ISN_ENDTRY:
5731 smsg("%s%4d ENDTRY", pfx, current);
5732 break;
5733 case ISN_THROW:
5734 smsg("%s%4d THROW", pfx, current);
5735 break;
5736
5737 // expression operations on number
5738 case ISN_OPNR:
5739 case ISN_OPFLOAT:
5740 case ISN_OPANY:
5741 {
5742 char *what;
5743 char *ins;
5744
5745 switch (iptr->isn_arg.op.op_type)
5746 {
5747 case EXPR_MULT: what = "*"; break;
5748 case EXPR_DIV: what = "/"; break;
5749 case EXPR_REM: what = "%"; break;
5750 case EXPR_SUB: what = "-"; break;
5751 case EXPR_ADD: what = "+"; break;
5752 default: what = "???"; break;
5753 }
5754 switch (iptr->isn_type)
5755 {
5756 case ISN_OPNR: ins = "OPNR"; break;
5757 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5758 case ISN_OPANY: ins = "OPANY"; break;
5759 default: ins = "???"; break;
5760 }
5761 smsg("%s%4d %s %s", pfx, current, ins, what);
5762 }
5763 break;
5764
5765 case ISN_COMPAREBOOL:
5766 case ISN_COMPARESPECIAL:
5767 case ISN_COMPARENR:
5768 case ISN_COMPAREFLOAT:
5769 case ISN_COMPARESTRING:
5770 case ISN_COMPAREBLOB:
5771 case ISN_COMPARELIST:
5772 case ISN_COMPAREDICT:
5773 case ISN_COMPAREFUNC:
5774 case ISN_COMPAREANY:
5775 {
5776 char *p;
5777 char buf[10];
5778 char *type;
5779
5780 switch (iptr->isn_arg.op.op_type)
5781 {
5782 case EXPR_EQUAL: p = "=="; break;
5783 case EXPR_NEQUAL: p = "!="; break;
5784 case EXPR_GREATER: p = ">"; break;
5785 case EXPR_GEQUAL: p = ">="; break;
5786 case EXPR_SMALLER: p = "<"; break;
5787 case EXPR_SEQUAL: p = "<="; break;
5788 case EXPR_MATCH: p = "=~"; break;
5789 case EXPR_IS: p = "is"; break;
5790 case EXPR_ISNOT: p = "isnot"; break;
5791 case EXPR_NOMATCH: p = "!~"; break;
5792 default: p = "???"; break;
5793 }
5794 STRCPY(buf, p);
5795 if (iptr->isn_arg.op.op_ic == TRUE)
5796 strcat(buf, "?");
5797 switch(iptr->isn_type)
5798 {
5799 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5800 case ISN_COMPARESPECIAL:
5801 type = "COMPARESPECIAL"; break;
5802 case ISN_COMPARENR: type = "COMPARENR"; break;
5803 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5804 case ISN_COMPARESTRING:
5805 type = "COMPARESTRING"; break;
5806 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5807 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5808 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5809 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5810 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5811 default: type = "???"; break;
5812 }
5813
5814 smsg("%s%4d %s %s", pfx, current, type, buf);
5815 }
5816 break;
5817
5818 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5819 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5820
5821 // expression operations
5822 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5823 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5824 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5825 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5826 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5827 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5828 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5829 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5830 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5831 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5832 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5833 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005834 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005835 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5836 iptr->isn_arg.getitem.gi_index,
5837 iptr->isn_arg.getitem.gi_with_op ?
5838 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005839 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5840 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5841 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005842 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5843 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5844
Bram Moolenaar4c137212021-04-19 16:48:48 +02005845 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5846
5847 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5848 case ISN_CHECKTYPE:
5849 {
5850 checktype_T *ct = &iptr->isn_arg.type;
5851 char *tofree;
5852
5853 if (ct->ct_arg_idx == 0)
5854 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5855 type_name(ct->ct_type, &tofree),
5856 (int)ct->ct_off);
5857 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005858 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5859 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860 type_name(ct->ct_type, &tofree),
5861 (int)ct->ct_off,
5862 (int)ct->ct_arg_idx);
5863 vim_free(tofree);
5864 break;
5865 }
5866 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5867 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5868 iptr->isn_arg.checklen.cl_min_len);
5869 break;
5870 case ISN_SETTYPE:
5871 {
5872 char *tofree;
5873
5874 smsg("%s%4d SETTYPE %s", pfx, current,
5875 type_name(iptr->isn_arg.type.ct_type, &tofree));
5876 vim_free(tofree);
5877 break;
5878 }
5879 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005880 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5881 smsg("%s%4d INVERT %d (!val)", pfx, current,
5882 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005883 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005884 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5885 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 break;
5887 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005888 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005889 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005890 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5891 pfx, current,
5892 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005893 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005894 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5895 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005896 break;
5897 case ISN_PUT:
5898 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5899 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005900 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005901 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5902 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005903 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005904 else
5905 smsg("%s%4d PUT %c %ld", pfx, current,
5906 iptr->isn_arg.put.put_regname,
5907 (long)iptr->isn_arg.put.put_lnum);
5908 break;
5909
Bram Moolenaar4c137212021-04-19 16:48:48 +02005910 case ISN_CMDMOD:
5911 {
5912 char_u *buf;
5913 size_t len = produce_cmdmods(
5914 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5915
5916 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005917 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005918 {
5919 (void)produce_cmdmods(
5920 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5921 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5922 vim_free(buf);
5923 }
5924 break;
5925 }
5926 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5927
5928 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005929 smsg("%s%4d PROFILE START line %d", pfx, current,
5930 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005931 break;
5932
5933 case ISN_PROF_END:
5934 smsg("%s%4d PROFILE END", pfx, current);
5935 break;
5936
Bram Moolenaare99d4222021-06-13 14:01:26 +02005937 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005938 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5939 iptr->isn_arg.debug.dbg_break_lnum + 1,
5940 iptr->isn_lnum,
5941 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005942 break;
5943
Bram Moolenaar4c137212021-04-19 16:48:48 +02005944 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5945 iptr->isn_arg.unpack.unp_count,
5946 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5947 break;
5948 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5949 iptr->isn_arg.shuffle.shfl_item,
5950 iptr->isn_arg.shuffle.shfl_up);
5951 break;
5952 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5953
5954 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5955 return;
5956 }
5957
5958 out_flush(); // output one line at a time
5959 ui_breakcheck();
5960 if (got_int)
5961 break;
5962 }
5963}
5964
5965/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005966 * Handle command line completion for the :disassemble command.
5967 */
5968 void
5969set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5970{
5971 char_u *p;
5972
5973 // Default: expand user functions, "debug" and "profile"
5974 xp->xp_context = EXPAND_DISASSEMBLE;
5975 xp->xp_pattern = arg;
5976
5977 // first argument already typed: only user function names
5978 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5979 {
5980 xp->xp_context = EXPAND_USER_FUNC;
5981 xp->xp_pattern = skipwhite(p);
5982 }
5983}
5984
5985/*
5986 * Function given to ExpandGeneric() to obtain the list of :disassemble
5987 * arguments.
5988 */
5989 char_u *
5990get_disassemble_argument(expand_T *xp, int idx)
5991{
5992 if (idx == 0)
5993 return (char_u *)"debug";
5994 if (idx == 1)
5995 return (char_u *)"profile";
5996 return get_user_func_name(xp, idx - 2);
5997}
5998
5999/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006000 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006001 * We don't really need this at runtime, but we do have tests that require it,
6002 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 */
6004 void
6005ex_disassemble(exarg_T *eap)
6006{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006007 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006008 char_u *fname;
6009 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010 dfunc_T *dfunc;
6011 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006012 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006013 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006014 compiletype_T compile_type = CT_NONE;
6015
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006016 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006017 {
6018 compile_type = CT_PROFILE;
6019 arg = skipwhite(arg + 7);
6020 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006021 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006022 {
6023 compile_type = CT_DEBUG;
6024 arg = skipwhite(arg + 5);
6025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006027 if (STRNCMP(arg, "<lambda>", 8) == 0)
6028 {
6029 arg += 8;
6030 (void)getdigits(&arg);
6031 fname = vim_strnsave(eap->arg, arg - eap->arg);
6032 }
6033 else
6034 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006035 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006036 if (fname == NULL)
6037 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006038 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006039 return;
6040 }
6041
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006042 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006043 if (ufunc == NULL)
6044 {
6045 char_u *p = untrans_function_name(fname);
6046
6047 if (p != NULL)
6048 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006049 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006050 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006051 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 if (ufunc == NULL)
6053 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006054 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 return;
6056 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006057 if (func_needs_compiling(ufunc, compile_type)
6058 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006059 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006060 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006062 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063 return;
6064 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006065 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066
6067 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006068 switch (compile_type)
6069 {
6070 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006071#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006072 instr = dfunc->df_instr_prof;
6073 instr_count = dfunc->df_instr_prof_count;
6074 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006075#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006076 // FALLTHROUGH
6077 case CT_NONE:
6078 instr = dfunc->df_instr;
6079 instr_count = dfunc->df_instr_count;
6080 break;
6081 case CT_DEBUG:
6082 instr = dfunc->df_instr_debug;
6083 instr_count = dfunc->df_instr_debug_count;
6084 break;
6085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086
Bram Moolenaar4c137212021-04-19 16:48:48 +02006087 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088}
6089
6090/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006091 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092 * list, etc. Mostly like what JavaScript does, except that empty list and
6093 * empty dictionary are FALSE.
6094 */
6095 int
6096tv2bool(typval_T *tv)
6097{
6098 switch (tv->v_type)
6099 {
6100 case VAR_NUMBER:
6101 return tv->vval.v_number != 0;
6102 case VAR_FLOAT:
6103#ifdef FEAT_FLOAT
6104 return tv->vval.v_float != 0.0;
6105#else
6106 break;
6107#endif
6108 case VAR_PARTIAL:
6109 return tv->vval.v_partial != NULL;
6110 case VAR_FUNC:
6111 case VAR_STRING:
6112 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6113 case VAR_LIST:
6114 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6115 case VAR_DICT:
6116 return tv->vval.v_dict != NULL
6117 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6118 case VAR_BOOL:
6119 case VAR_SPECIAL:
6120 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6121 case VAR_JOB:
6122#ifdef FEAT_JOB_CHANNEL
6123 return tv->vval.v_job != NULL;
6124#else
6125 break;
6126#endif
6127 case VAR_CHANNEL:
6128#ifdef FEAT_JOB_CHANNEL
6129 return tv->vval.v_channel != NULL;
6130#else
6131 break;
6132#endif
6133 case VAR_BLOB:
6134 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6135 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006136 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006138 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139 break;
6140 }
6141 return FALSE;
6142}
6143
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006144 void
6145emsg_using_string_as(typval_T *tv, int as_number)
6146{
6147 semsg(_(as_number ? e_using_string_as_number_str
6148 : e_using_string_as_bool_str),
6149 tv->vval.v_string == NULL
6150 ? (char_u *)"" : tv->vval.v_string);
6151}
6152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153/*
6154 * If "tv" is a string give an error and return FAIL.
6155 */
6156 int
6157check_not_string(typval_T *tv)
6158{
6159 if (tv->v_type == VAR_STRING)
6160 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006161 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162 clear_tv(tv);
6163 return FAIL;
6164 }
6165 return OK;
6166}
6167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168
6169#endif // FEAT_EVAL