blob: 53beee10343fe7e5cac55663f5952e70bf642fd5 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200140 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
155 static void
156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200168static garray_T dict_stack = GA_EMPTY;
169
170/*
171 * Put a value on the dict stack. This consumes "tv".
172 */
173 static int
174dict_stack_save(typval_T *tv)
175{
176 if (dict_stack.ga_growsize == 0)
177 ga_init2(&dict_stack, (int)sizeof(typval_T), 10);
178 if (ga_grow(&dict_stack, 1) == FAIL)
179 return FAIL;
180 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
181 ++dict_stack.ga_len;
182 return OK;
183}
184
185/*
186 * Get the typval at top of the dict stack.
187 */
188 static typval_T *
189dict_stack_get_tv(void)
190{
191 if (dict_stack.ga_len == 0)
192 return NULL;
193 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
194}
195
196/*
197 * Get the dict at top of the dict stack.
198 */
199 static dict_T *
200dict_stack_get_dict(void)
201{
202 typval_T *tv;
203
204 if (dict_stack.ga_len == 0)
205 return NULL;
206 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
207 if (tv->v_type == VAR_DICT)
208 return tv->vval.v_dict;
209 return NULL;
210}
211
212/*
213 * Drop an item from the dict stack.
214 */
215 static void
216dict_stack_drop(void)
217{
218 if (dict_stack.ga_len == 0)
219 {
220 iemsg("Dict stack underflow");
221 return;
222 }
223 --dict_stack.ga_len;
224 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
225}
226
227/*
228 * Drop items from the dict stack until the length is equal to "len".
229 */
230 static void
231dict_stack_clear(int len)
232{
233 while (dict_stack.ga_len > len)
234 dict_stack_drop();
235}
236
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100239 * This adds a stack frame and sets the instruction pointer to the start of the
240 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200241 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 *
243 * Stack has:
244 * - current arguments (already there)
245 * - omitted optional argument (default values) added here
246 * - stack frame:
247 * - pointer to calling function
248 * - Index of next instruction in calling function
249 * - previous frame pointer
250 * - reserved space for local variables
251 */
252 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100253call_dfunc(
254 int cdf_idx,
255 partial_T *pt,
256 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100257 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100259 int argcount = argcount_arg;
260 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
261 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200262 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100263 int arg_to_add;
264 int vararg_count = 0;
265 int varcount;
266 int idx;
267 estack_T *entry;
268 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200269 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270
271 if (dfunc->df_deleted)
272 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100273 // don't use ufunc->uf_name, it may have been freed
274 emsg_funcname(e_func_deleted,
275 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 return FAIL;
277 }
278
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100279#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100280 if (do_profiling == PROF_YES)
281 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200282 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100283 {
284 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
285 + profile_info_ga.ga_len;
286 ++profile_info_ga.ga_len;
287 CLEAR_POINTER(info);
288 profile_may_start_func(info, ufunc,
289 (((dfunc_T *)def_functions.ga_data)
290 + ectx->ec_dfunc_idx)->df_ufunc);
291 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100292 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100293#endif
294
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200295 // Update uf_has_breakpoint if needed.
296 update_has_breakpoint(ufunc);
297
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200298 // When debugging and using "cont" switches to the not-debugged
299 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200300 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
301 {
302 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
303
304 // compile_def_function() may cause def_functions.ga_data to change
305 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
306 }
307 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200308 {
309 if (did_emsg_cumul + did_emsg == did_emsg_before)
310 semsg(_(e_function_is_not_compiled_str),
311 printable_func_name(ufunc));
312 return FAIL;
313 }
314
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200315 if (ufunc->uf_va_name != NULL)
316 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200317 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200318 // Stack at time of call with 2 varargs:
319 // normal_arg
320 // optional_arg
321 // vararg_1
322 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200323 // After creating the list:
324 // normal_arg
325 // optional_arg
326 // vararg-list
327 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200328 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200329 // After creating the list
330 // normal_arg
331 // (space for optional_arg)
332 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200333 vararg_count = argcount - ufunc->uf_args.ga_len;
334 if (vararg_count < 0)
335 vararg_count = 0;
336 else
337 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200338 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200339 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200340
341 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200342 }
343
Bram Moolenaarfe270812020-04-11 22:31:27 +0200344 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200345 if (arg_to_add < 0)
346 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200347 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200348 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200349 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200350 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200351 return FAIL;
352 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200353
354 // Reserve space for:
355 // - missing arguments
356 // - stack frame
357 // - local variables
358 // - if needed: a counter for number of closures created in
359 // ectx->ec_funcrefs.
360 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200361 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 return FAIL;
363
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100364 // If depth of calling is getting too high, don't execute the function.
365 if (funcdepth_increment() == FAIL)
366 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200367 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100368
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100369 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200370 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100371 {
372 floc = ALLOC_ONE(funclocal_T);
373 if (floc == NULL)
374 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200375 *floc = ectx->ec_funclocal;
376 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100377 }
378
Bram Moolenaarfe270812020-04-11 22:31:27 +0200379 // Move the vararg-list to below the missing optional arguments.
380 if (vararg_count > 0 && arg_to_add > 0)
381 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100382
383 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200384 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200385 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200386 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387
388 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100389 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
390 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200391 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200392 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
393 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100395 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200396 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397
398 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200399 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200401 if (dfunc->df_has_closure)
402 {
403 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
404
405 tv->v_type = VAR_NUMBER;
406 tv->vval.v_number = 0;
407 }
408 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100410 if (pt != NULL || ufunc->uf_partial != NULL
411 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100412 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200413 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100414
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200415 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100416 return FAIL;
417 if (pt != NULL)
418 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200419 ref->or_outer = &pt->pt_outer;
420 ++pt->pt_refcount;
421 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100422 }
423 else if (ufunc->uf_partial != NULL)
424 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200425 ref->or_outer = &ufunc->uf_partial->pt_outer;
426 ++ufunc->uf_partial->pt_refcount;
427 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100428 }
429 else
430 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200431 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200432 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200433 {
434 vim_free(ref);
435 return FAIL;
436 }
437 ref->or_outer_allocated = TRUE;
438 ref->or_outer->out_stack = &ectx->ec_stack;
439 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
440 if (ectx->ec_outer_ref != NULL)
441 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100442 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200443 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100444 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100445 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200446 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100447
Bram Moolenaarc970e422021-03-17 15:03:04 +0100448 ++ufunc->uf_calls;
449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 // Set execution state to the start of the called function.
451 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100452 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100453 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200454 if (entry != NULL)
455 {
456 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200457 // Save the current context so it can be restored on return.
458 entry->es_save_sctx = current_sctx;
459 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200460 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100461
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200462 // Start execution at the first instruction.
463 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464
465 return OK;
466}
467
468// Get pointer to item in the stack.
469#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
470
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000471// Double linked list of funcstack_T in use.
472static funcstack_T *first_funcstack = NULL;
473
474 static void
475add_funcstack_to_list(funcstack_T *funcstack)
476{
477 // Link in list of funcstacks.
478 if (first_funcstack != NULL)
479 first_funcstack->fs_prev = funcstack;
480 funcstack->fs_next = first_funcstack;
481 funcstack->fs_prev = NULL;
482 first_funcstack = funcstack;
483}
484
485 static void
486remove_funcstack_from_list(funcstack_T *funcstack)
487{
488 if (funcstack->fs_prev == NULL)
489 first_funcstack = funcstack->fs_next;
490 else
491 funcstack->fs_prev->fs_next = funcstack->fs_next;
492 if (funcstack->fs_next != NULL)
493 funcstack->fs_next->fs_prev = funcstack->fs_prev;
494}
495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200497 * Used when returning from a function: Check if any closure is still
498 * referenced. If so then move the arguments and variables to a separate piece
499 * of stack to be used when the closure is called.
500 * When "free_arguments" is TRUE the arguments are to be freed.
501 * Returns FAIL when out of memory.
502 */
503 static int
504handle_closure_in_use(ectx_T *ectx, int free_arguments)
505{
506 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
507 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200508 int argcount;
509 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200510 int idx;
511 typval_T *tv;
512 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200513 garray_T *gap = &ectx->ec_funcrefs;
514 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200516 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200517 return OK; // function was freed
518 if (dfunc->df_has_closure == 0)
519 return OK; // no closures
520 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
521 closure_count = tv->vval.v_number;
522 if (closure_count == 0)
523 return OK; // no funcrefs created
524
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200525 argcount = ufunc_argcount(dfunc->df_ufunc);
526 top = ectx->ec_frame_idx - argcount;
527
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200528 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200529 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200531 partial_T *pt;
532 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200533
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200534 if (off < 0)
535 continue; // count is off or already done
536 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200537 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200538 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200540 int i;
541
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200542 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200543 // unreferenced on return.
544 for (i = 0; i < dfunc->df_varcount; ++i)
545 {
546 typval_T *stv = STACK_TV(ectx->ec_frame_idx
547 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200548 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200549 --refcount;
550 }
551 if (refcount > 1)
552 {
553 closure_in_use = TRUE;
554 break;
555 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200556 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200557 }
558
559 if (closure_in_use)
560 {
561 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
562 typval_T *stack;
563
564 // A closure is using the arguments and/or local variables.
565 // Move them to the called function.
566 if (funcstack == NULL)
567 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000568
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200569 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
570 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200571 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
572 funcstack->fs_ga.ga_data = stack;
573 if (stack == NULL)
574 {
575 vim_free(funcstack);
576 return FAIL;
577 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000578 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200579
580 // Move or copy the arguments.
581 for (idx = 0; idx < argcount; ++idx)
582 {
583 tv = STACK_TV(top + idx);
584 if (free_arguments)
585 {
586 *(stack + idx) = *tv;
587 tv->v_type = VAR_UNKNOWN;
588 }
589 else
590 copy_tv(tv, stack + idx);
591 }
592 // Move the local variables.
593 for (idx = 0; idx < dfunc->df_varcount; ++idx)
594 {
595 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200596
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200597 // A partial created for a local function, that is also used as a
598 // local variable, has a reference count for the variable, thus
599 // will never go down to zero. When all these refcounts are one
600 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000601 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200602 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
603 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200604 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200605
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200606 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200607 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
608 gap->ga_len - closure_count + i])
609 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200610 }
611
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200612 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200613 tv->v_type = VAR_UNKNOWN;
614 }
615
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200616 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200617 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200618 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
619 - closure_count + idx];
620 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200621 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200622 ++funcstack->fs_refcount;
623 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100624 pt->pt_outer.out_stack = &funcstack->fs_ga;
625 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 }
627 }
628 }
629
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200630 for (idx = 0; idx < closure_count; ++idx)
631 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
632 - closure_count + idx]);
633 gap->ga_len -= closure_count;
634 if (gap->ga_len == 0)
635 ga_clear(gap);
636
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 return OK;
638}
639
640/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200641 * Called when a partial is freed or its reference count goes down to one. The
642 * funcstack may be the only reference to the partials in the local variables.
643 * Go over all of them, the funcref and can be freed if all partials
644 * referencing the funcstack have a reference count of one.
645 */
646 void
647funcstack_check_refcount(funcstack_T *funcstack)
648{
649 int i;
650 garray_T *gap = &funcstack->fs_ga;
651 int done = 0;
652
653 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
654 return;
655 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
656 {
657 typval_T *tv = ((typval_T *)gap->ga_data) + i;
658
659 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
660 && tv->vval.v_partial->pt_funcstack == funcstack
661 && tv->vval.v_partial->pt_refcount == 1)
662 ++done;
663 }
664 if (done == funcstack->fs_min_refcount)
665 {
666 typval_T *stack = gap->ga_data;
667
668 // All partials referencing the funcstack have a reference count of
669 // one, thus the funcstack is no longer of use.
670 for (i = 0; i < gap->ga_len; ++i)
671 clear_tv(stack + i);
672 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000673 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200674 vim_free(funcstack);
675 }
676}
677
678/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000679 * For garbage collecting: set references in all variables referenced by
680 * all funcstacks.
681 */
682 int
683set_ref_in_funcstacks(int copyID)
684{
685 funcstack_T *funcstack;
686
687 for (funcstack = first_funcstack; funcstack != NULL;
688 funcstack = funcstack->fs_next)
689 {
690 typval_T *stack = funcstack->fs_ga.ga_data;
691 int i;
692
693 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
694 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
695 return TRUE; // abort
696 }
697 return FALSE;
698}
699
700/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 * Return from the current function.
702 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200703 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200704func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100707 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
709 + ectx->ec_dfunc_idx;
710 int argcount = ufunc_argcount(dfunc->df_ufunc);
711 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200712 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100713 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
714 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200715 funclocal_T *floc;
716#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100717 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
718 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719
Bram Moolenaar12d26532021-02-19 19:13:21 +0100720 if (do_profiling == PROF_YES)
721 {
722 ufunc_T *caller = prev_dfunc->df_ufunc;
723
724 if (dfunc->df_ufunc->uf_profiling
725 || (caller != NULL && caller->uf_profiling))
726 {
727 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
728 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
729 --profile_info_ga.ga_len;
730 }
731 }
732#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000733
734 // No check for uf_refcount being zero, cannot think of a way that would
735 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100736 --dfunc->df_ufunc->uf_calls;
737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200739 entry = estack_pop();
740 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200741 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 if (handle_closure_in_use(ectx, TRUE) == FAIL)
744 return FAIL;
745
746 // Clear the arguments.
747 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
748 clear_tv(STACK_TV(idx));
749
750 // Clear local variables and temp values, but not the return value.
751 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100752 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100754
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100755 // The return value should be on top of the stack. However, when aborting
756 // it may not be there and ec_frame_idx is the top of the stack.
757 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100758 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100759 ret_idx = 0;
760
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200761 if (ectx->ec_outer_ref != NULL)
762 {
763 if (ectx->ec_outer_ref->or_outer_allocated)
764 vim_free(ectx->ec_outer_ref->or_outer);
765 partial_unref(ectx->ec_outer_ref->or_partial);
766 vim_free(ectx->ec_outer_ref);
767 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100768
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100769 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100770 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100771 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
772 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200773 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
774 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200775 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100776 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100777 floc = (void *)STACK_TV(ectx->ec_frame_idx
778 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200779 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100780 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
781 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200782
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100783 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200784 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100785 else
786 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200787 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100788 vim_free(floc);
789 }
790
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100791 if (ret_idx > 0)
792 {
793 // Reset the stack to the position before the call, with a spot for the
794 // return value, moved there from above the frame.
795 ectx->ec_stack.ga_len = top + 1;
796 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
797 }
798 else
799 // Reset the stack to the position before the call.
800 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200801
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100802 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200803 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200804 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805}
806
807#undef STACK_TV
808
809/*
810 * Prepare arguments and rettv for calling a builtin or user function.
811 */
812 static int
813call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
814{
815 int idx;
816 typval_T *tv;
817
818 // Move arguments from bottom of the stack to argvars[] and add terminator.
819 for (idx = 0; idx < argcount; ++idx)
820 argvars[idx] = *STACK_TV_BOT(idx - argcount);
821 argvars[argcount].v_type = VAR_UNKNOWN;
822
823 // Result replaces the arguments on the stack.
824 if (argcount > 0)
825 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200826 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 return FAIL;
828 else
829 ++ectx->ec_stack.ga_len;
830
831 // Default return value is zero.
832 tv = STACK_TV_BOT(-1);
833 tv->v_type = VAR_NUMBER;
834 tv->vval.v_number = 0;
835
836 return OK;
837}
838
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200839// Ugly global to avoid passing the execution context around through many
840// layers.
841static ectx_T *current_ectx = NULL;
842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843/*
844 * Call a builtin function by index.
845 */
846 static int
847call_bfunc(int func_idx, int argcount, ectx_T *ectx)
848{
849 typval_T argvars[MAX_FUNC_ARGS];
850 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100851 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200852 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200853 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854
855 if (call_prepare(argcount, argvars, ectx) == FAIL)
856 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200857 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200859 // Call the builtin function. Set "current_ectx" so that when it
860 // recursively invokes call_def_function() a closure context can be set.
861 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200863 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200864 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865
866 // Clear the arguments.
867 for (idx = 0; idx < argcount; ++idx)
868 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200869
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100870 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200871 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 return OK;
873}
874
875/*
876 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100877 * If the function is compiled this will add a stack frame and set the
878 * instruction pointer at the start of the function.
879 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200880 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100881 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 */
883 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100884call_ufunc(
885 ufunc_T *ufunc,
886 partial_T *pt,
887 int argcount,
888 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200889 isn_T *iptr,
890 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891{
892 typval_T argvars[MAX_FUNC_ARGS];
893 funcexe_T funcexe;
894 int error;
895 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100896 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200897 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898
Bram Moolenaare99d4222021-06-13 14:01:26 +0200899 if (func_needs_compiling(ufunc, compile_type)
900 && compile_def_function(ufunc, FALSE, compile_type, NULL)
901 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200902 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200903 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100904 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100905 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100906 if (error != FCERR_UNKNOWN)
907 {
908 if (error == FCERR_TOOMANY)
909 semsg(_(e_toomanyarg), ufunc->uf_name);
910 else
911 semsg(_(e_toofewarg), ufunc->uf_name);
912 return FAIL;
913 }
914
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100915 // The function has been compiled, can call it quickly. For a function
916 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100917 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100918 if (iptr != NULL)
919 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100920 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100921 iptr->isn_type = ISN_DCALL;
922 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
923 iptr->isn_arg.dfunc.cdf_argcount = argcount;
924 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200925 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
928 if (call_prepare(argcount, argvars, ectx) == FAIL)
929 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200930 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000931 funcexe.fe_evaluate = TRUE;
932 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
934 // Call the user function. Result goes in last position on the stack.
935 // TODO: add selfdict if there is one
936 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000937 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938
939 // Clear the arguments.
940 for (idx = 0; idx < argcount; ++idx)
941 clear_tv(&argvars[idx]);
942
943 if (error != FCERR_NONE)
944 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000945 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 return FAIL;
947 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100948 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200949 // Error other than from calling the function itself.
950 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 return OK;
952}
953
954/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100955 * If command modifiers were applied restore them.
956 */
957 static void
958may_restore_cmdmod(funclocal_T *funclocal)
959{
960 if (funclocal->floc_restore_cmdmod)
961 {
962 cmdmod.cmod_filter_regmatch.regprog = NULL;
963 undo_cmdmod(&cmdmod);
964 cmdmod = funclocal->floc_save_cmdmod;
965 funclocal->floc_restore_cmdmod = FALSE;
966 }
967}
968
969/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200970 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
971 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200972 */
973 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200974vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200975{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200976 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200977}
978
979/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 * Execute a function by "name".
981 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100982 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 * Returns FAIL if not found without an error message.
984 */
985 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100986call_by_name(
987 char_u *name,
988 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100989 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200990 isn_T *iptr,
991 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992{
993 ufunc_T *ufunc;
994
995 if (builtin_function(name, -1))
996 {
997 int func_idx = find_internal_func(name);
998
999 if (func_idx < 0)
1000 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001001 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 return FAIL;
1003 return call_bfunc(func_idx, argcount, ectx);
1004 }
1005
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001006 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +02001007
1008 if (ufunc == NULL)
1009 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001010 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001011
1012 if (script_autoload(name, TRUE))
1013 // loaded a package, search for the function again
1014 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001015
1016 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001017 return FAIL; // bail out if loading the script caused an error
1018 }
1019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001021 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001022 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001023 {
1024 int i;
1025 typval_T *argv = STACK_TV_BOT(0) - argcount;
1026
1027 // The function can change at runtime, check that the argument
1028 // types are correct.
1029 for (i = 0; i < argcount; ++i)
1030 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001031 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001032
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001033 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001034 type = ufunc->uf_arg_types[i];
1035 else if (ufunc->uf_va_type != NULL)
1036 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001037 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001038 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001039 return FAIL;
1040 }
1041 }
1042
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001043 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001044 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045
1046 return FAIL;
1047}
1048
1049 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001050call_partial(
1051 typval_T *tv,
1052 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001053 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001055 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001056 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001058 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001059 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060
1061 if (tv->v_type == VAR_PARTIAL)
1062 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001063 partial_T *pt = tv->vval.v_partial;
1064 int i;
1065
1066 if (pt->pt_argc > 0)
1067 {
1068 // Make space for arguments from the partial, shift the "argcount"
1069 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001070 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001071 return FAIL;
1072 for (i = 1; i <= argcount; ++i)
1073 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1074 ectx->ec_stack.ga_len += pt->pt_argc;
1075 argcount += pt->pt_argc;
1076
1077 // copy the arguments from the partial onto the stack
1078 for (i = 0; i < pt->pt_argc; ++i)
1079 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1080 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001081 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082
1083 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001084 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 name = pt->pt_name;
1087 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001088 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001090 if (name != NULL)
1091 {
1092 char_u fname_buf[FLEN_FIXED + 1];
1093 char_u *tofree = NULL;
1094 int error = FCERR_NONE;
1095 char_u *fname;
1096
1097 // May need to translate <SNR>123_ to K_SNR.
1098 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1099 if (error != FCERR_NONE)
1100 res = FAIL;
1101 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001102 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001103 vim_free(tofree);
1104 }
1105
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001106 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 {
1108 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001109 semsg(_(e_unknownfunc),
1110 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 return FAIL;
1112 }
1113 return OK;
1114}
1115
1116/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001117 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1118 * TRUE.
1119 */
1120 static int
1121error_if_locked(int lock, char *error)
1122{
1123 if (lock & (VAR_LOCKED | VAR_FIXED))
1124 {
1125 emsg(_(error));
1126 return TRUE;
1127 }
1128 return FALSE;
1129}
1130
1131/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001132 * Give an error if "tv" is not a number and return FAIL.
1133 */
1134 static int
1135check_for_number(typval_T *tv)
1136{
1137 if (tv->v_type != VAR_NUMBER)
1138 {
1139 semsg(_(e_expected_str_but_got_str),
1140 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1141 return FAIL;
1142 }
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001147 * Store "tv" in variable "name".
1148 * This is for s: and g: variables.
1149 */
1150 static void
1151store_var(char_u *name, typval_T *tv)
1152{
1153 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001154 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001155
Bram Moolenaard877a572021-04-01 19:42:48 +02001156 if (tv->v_lock)
1157 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001158 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001159 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001160 restore_funccal();
1161}
1162
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001163/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001164 * Convert "tv" to a string.
1165 * Return FAIL if not allowed.
1166 */
1167 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001168do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001169{
1170 if (tv->v_type != VAR_STRING)
1171 {
1172 char_u *str;
1173
1174 if (is_2string_any)
1175 {
1176 switch (tv->v_type)
1177 {
1178 case VAR_SPECIAL:
1179 case VAR_BOOL:
1180 case VAR_NUMBER:
1181 case VAR_FLOAT:
1182 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001183
1184 case VAR_LIST:
1185 if (tolerant)
1186 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001187 char_u *s, *e, *p;
1188 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001189
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001190 ga_init2(&ga, sizeof(char_u *), 1);
1191
1192 // Convert to NL separated items, then
1193 // escape the items and replace the NL with
1194 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001195 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001196 if (str == NULL)
1197 return FAIL;
1198 s = str;
1199 while ((e = vim_strchr(s, '\n')) != NULL)
1200 {
1201 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001202 p = vim_strsave_fnameescape(s,
1203 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001204 if (p != NULL)
1205 {
1206 ga_concat(&ga, p);
1207 ga_concat(&ga, (char_u *)" ");
1208 vim_free(p);
1209 }
1210 s = e + 1;
1211 }
1212 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001213 clear_tv(tv);
1214 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001215 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001216 return OK;
1217 }
1218 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001219 default: to_string_error(tv->v_type);
1220 return FAIL;
1221 }
1222 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001223 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001224 clear_tv(tv);
1225 tv->v_type = VAR_STRING;
1226 tv->vval.v_string = str;
1227 }
1228 return OK;
1229}
1230
1231/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001232 * When the value of "sv" is a null list of dict, allocate it.
1233 */
1234 static void
1235allocate_if_null(typval_T *tv)
1236{
1237 switch (tv->v_type)
1238 {
1239 case VAR_LIST:
1240 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001241 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001242 break;
1243 case VAR_DICT:
1244 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001245 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001246 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001247 case VAR_BLOB:
1248 if (tv->vval.v_blob == NULL)
1249 (void)rettv_blob_alloc(tv);
1250 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001251 default:
1252 break;
1253 }
1254}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001255
Bram Moolenaare7525c52021-01-09 13:20:37 +01001256/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001257 * Return the character "str[index]" where "index" is the character index,
1258 * including composing characters.
1259 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001260 */
1261 char_u *
1262char_from_string(char_u *str, varnumber_T index)
1263{
1264 size_t nbyte = 0;
1265 varnumber_T nchar = index;
1266 size_t slen;
1267
1268 if (str == NULL)
1269 return NULL;
1270 slen = STRLEN(str);
1271
Bram Moolenaarff871402021-03-26 13:34:05 +01001272 // Do the same as for a list: a negative index counts from the end.
1273 // Optimization to check the first byte to be below 0x80 (and no composing
1274 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001275 if (index < 0)
1276 {
1277 int clen = 0;
1278
1279 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001280 {
1281 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1282 ++nbyte;
1283 else if (enc_utf8)
1284 nbyte += utfc_ptr2len(str + nbyte);
1285 else
1286 nbyte += mb_ptr2len(str + nbyte);
1287 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001288 nchar = clen + index;
1289 if (nchar < 0)
1290 // unlike list: index out of range results in empty string
1291 return NULL;
1292 }
1293
1294 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001295 {
1296 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1297 ++nbyte;
1298 else if (enc_utf8)
1299 nbyte += utfc_ptr2len(str + nbyte);
1300 else
1301 nbyte += mb_ptr2len(str + nbyte);
1302 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001303 if (nbyte >= slen)
1304 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001305 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001306}
1307
1308/*
1309 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001310 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001311 * If going over the end return "str_len".
1312 * If "idx" is negative count from the end, -1 is the last character.
1313 * When going over the start return -1.
1314 */
1315 static long
1316char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1317{
1318 varnumber_T nchar = idx;
1319 size_t nbyte = 0;
1320
1321 if (nchar >= 0)
1322 {
1323 while (nchar > 0 && nbyte < str_len)
1324 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001325 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001326 --nchar;
1327 }
1328 }
1329 else
1330 {
1331 nbyte = str_len;
1332 while (nchar < 0 && nbyte > 0)
1333 {
1334 --nbyte;
1335 nbyte -= mb_head_off(str, str + nbyte);
1336 ++nchar;
1337 }
1338 if (nchar < 0)
1339 return -1;
1340 }
1341 return (long)nbyte;
1342}
1343
1344/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001345 * Return the slice "str[first : last]" using character indexes. Composing
1346 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001347 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001348 * Return NULL when the result is empty.
1349 */
1350 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001351string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001352{
1353 long start_byte, end_byte;
1354 size_t slen;
1355
1356 if (str == NULL)
1357 return NULL;
1358 slen = STRLEN(str);
1359 start_byte = char_idx2byte(str, slen, first);
1360 if (start_byte < 0)
1361 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001362 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001363 end_byte = (long)slen;
1364 else
1365 {
1366 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001367 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001368 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001369 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001370 }
1371
1372 if (start_byte >= (long)slen || end_byte <= start_byte)
1373 return NULL;
1374 return vim_strnsave(str + start_byte, end_byte - start_byte);
1375}
1376
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001377/*
1378 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1379 * When "dfunc_idx" is negative don't give an error.
1380 * Returns NULL for an error.
1381 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001382 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001383get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001384{
1385 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001386 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1387 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001388 svar_T *sv;
1389
1390 if (sref->sref_seq != si->sn_script_seq)
1391 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001392 // The script was reloaded after the function was compiled, the
1393 // script_idx may not be valid.
1394 if (dfunc != NULL)
1395 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1396 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001397 return NULL;
1398 }
1399 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001400 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001401 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001402 if (dfunc != NULL)
1403 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001404 return NULL;
1405 }
1406 return sv;
1407}
1408
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001409/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001410 * Function passed to do_cmdline() for splitting a script joined by NL
1411 * characters.
1412 */
1413 static char_u *
1414get_split_sourceline(
1415 int c UNUSED,
1416 void *cookie,
1417 int indent UNUSED,
1418 getline_opt_T options UNUSED)
1419{
1420 source_cookie_T *sp = (source_cookie_T *)cookie;
1421 char_u *p;
1422 char_u *line;
1423
1424 if (*sp->nextline == NUL)
1425 return NULL;
1426 p = vim_strchr(sp->nextline, '\n');
1427 if (p == NULL)
1428 {
1429 line = vim_strsave(sp->nextline);
1430 sp->nextline += STRLEN(sp->nextline);
1431 }
1432 else
1433 {
1434 line = vim_strnsave(sp->nextline, p - sp->nextline);
1435 sp->nextline = p + 1;
1436 }
1437 return line;
1438}
1439
1440/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 * Execute a function by "name".
1442 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001443 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 */
1445 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001446call_eval_func(
1447 char_u *name,
1448 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001449 ectx_T *ectx,
1450 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451{
Bram Moolenaared677f52020-08-12 16:38:10 +02001452 int called_emsg_before = called_emsg;
1453 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001455 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001456 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001458 dictitem_T *v;
1459
1460 v = find_var(name, NULL, FALSE);
1461 if (v == NULL)
1462 {
1463 semsg(_(e_unknownfunc), name);
1464 return FAIL;
1465 }
1466 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1467 {
1468 semsg(_(e_unknownfunc), name);
1469 return FAIL;
1470 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001471 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001473 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474}
1475
1476/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001477 * When a function reference is used, fill a partial with the information
1478 * needed, especially when it is used as a closure.
1479 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001480 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001481fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1482{
1483 pt->pt_func = ufunc;
1484 pt->pt_refcount = 1;
1485
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001486 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001487 {
1488 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1489 + ectx->ec_dfunc_idx;
1490
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001491 // The closure may need to find arguments and local variables in the
1492 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001493 pt->pt_outer.out_stack = &ectx->ec_stack;
1494 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001495 if (ectx->ec_outer_ref != NULL)
1496 {
1497 // The current context already has a context, link to that one.
1498 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1499 if (ectx->ec_outer_ref->or_partial != NULL)
1500 {
1501 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1502 ++pt->pt_outer.out_up_partial->pt_refcount;
1503 }
1504 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001505
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001506 // If this function returns and the closure is still being used, we
1507 // need to make a copy of the context (arguments and local variables).
1508 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001509 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001510 {
1511 vim_free(pt);
1512 return FAIL;
1513 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001514 // Extra variable keeps the count of closures created in the current
1515 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001516 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1517 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1518
1519 ((partial_T **)ectx->ec_funcrefs.ga_data)
1520 [ectx->ec_funcrefs.ga_len] = pt;
1521 ++pt->pt_refcount;
1522 ++ectx->ec_funcrefs.ga_len;
1523 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001524 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001525 return OK;
1526}
1527
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001528/*
1529 * Execute iptr->isn_arg.string as an Ex command.
1530 */
1531 static int
1532exec_command(isn_T *iptr)
1533{
1534 source_cookie_T cookie;
1535
1536 SOURCING_LNUM = iptr->isn_lnum;
1537 // Pass getsourceline to get an error for a missing ":end"
1538 // command.
1539 CLEAR_FIELD(cookie);
1540 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1541 if (do_cmdline(iptr->isn_arg.string,
1542 getsourceline, &cookie,
1543 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1544 || did_emsg)
1545 return FAIL;
1546 return OK;
1547}
1548
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001549// used for v_instr of typval of VAR_INSTR
1550struct instr_S {
1551 ectx_T *instr_ectx;
1552 isn_T *instr_instr;
1553};
1554
Bram Moolenaar4c137212021-04-19 16:48:48 +02001555// used for substitute_instr
1556typedef struct subs_expr_S {
1557 ectx_T *subs_ectx;
1558 isn_T *subs_instr;
1559 int subs_status;
1560} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561
1562// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001563#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564
1565// Get pointer to item at the bottom of the stack, -1 is the bottom.
1566#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001567#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 +01001568
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001569// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001570#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 +01001571
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001572// Set when calling do_debug().
1573static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001574static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001575
1576/*
1577 * When debugging lookup "name" and return the typeval.
1578 * When not found return NULL.
1579 */
1580 typval_T *
1581lookup_debug_var(char_u *name)
1582{
1583 int idx;
1584 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001585 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001586 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001587 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001588
1589 if (ectx == NULL)
1590 return NULL;
1591 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1592
1593 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001594 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001595 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001596 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001597 return STACK_TV_VAR(idx);
1598 }
1599
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001600 // Go through argument names.
1601 ufunc = dfunc->df_ufunc;
1602 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1603 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1604 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1605 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1606 - varargs_off + idx);
1607 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1608 return STACK_TV(ectx->ec_frame_idx - 1);
1609
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001610 return NULL;
1611}
1612
Bram Moolenaar26a44842021-09-02 18:49:06 +02001613/*
1614 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1615 * breakpoint was set in that function or when there is any expression.
1616 */
1617 int
1618may_break_in_function(ufunc_T *ufunc)
1619{
1620 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1621}
1622
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001623 static void
1624handle_debug(isn_T *iptr, ectx_T *ectx)
1625{
1626 char_u *line;
1627 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1628 + ectx->ec_dfunc_idx)->df_ufunc;
1629 isn_T *ni;
1630 int end_lnum = iptr->isn_lnum;
1631 garray_T ga;
1632 int lnum;
1633
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001634 if (ex_nesting_level > debug_break_level)
1635 {
1636 linenr_T breakpoint;
1637
Bram Moolenaar26a44842021-09-02 18:49:06 +02001638 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001639 return;
1640
1641 // check for the next breakpoint if needed
1642 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001643 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001644 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1645 return;
1646 }
1647
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001648 SOURCING_LNUM = iptr->isn_lnum;
1649 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001650 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001651
1652 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1653 if (ni->isn_type == ISN_DEBUG
1654 || ni->isn_type == ISN_RETURN
1655 || ni->isn_type == ISN_RETURN_VOID)
1656 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001657 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001658 break;
1659 }
1660
1661 if (end_lnum > iptr->isn_lnum)
1662 {
1663 ga_init2(&ga, sizeof(char_u *), 10);
1664 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001665 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001666 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001667
Bram Moolenaar303215d2021-07-07 20:10:43 +02001668 if (p == NULL)
1669 continue; // left over from continuation line
1670 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001671 if (*p == '#')
1672 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001673 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001674 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1675 if (STRNCMP(p, "def ", 4) == 0)
1676 break;
1677 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001678 line = ga_concat_strings(&ga, " ");
1679 vim_free(ga.ga_data);
1680 }
1681 else
1682 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001683
Dominique Pelle6e969552021-06-17 13:53:41 +02001684 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001685 debug_context = NULL;
1686
1687 if (end_lnum > iptr->isn_lnum)
1688 vim_free(line);
1689}
1690
Bram Moolenaar4c137212021-04-19 16:48:48 +02001691/*
1692 * Execute instructions in execution context "ectx".
1693 * Return OK or FAIL;
1694 */
1695 static int
1696exec_instructions(ectx_T *ectx)
1697{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001698 int ret = FAIL;
1699 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001700 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001701
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001702 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001703 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001704
Bram Moolenaarff652882021-05-16 15:24:49 +02001705 // Only catch exceptions in this instruction list.
1706 ectx->ec_trylevel_at_start = trylevel;
1707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 for (;;)
1709 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001710 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001712 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001713
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001714 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001715 {
1716 line_breakcheck();
1717 breakcheck_count = 0;
1718 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001719 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001720 {
1721 // Turn CTRL-C into an exception.
1722 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001723 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001724 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001725 did_throw = TRUE;
1726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001728 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001729 {
1730 // Turn an error message into an exception.
1731 did_emsg = FALSE;
1732 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001733 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001734 did_throw = TRUE;
1735 *msg_list = NULL;
1736 }
1737
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001738 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001740 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001741 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001742 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
1744 // An exception jumps to the first catch, finally, or returns from
1745 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001746 while (index > 0)
1747 {
1748 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001749 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001750 break;
1751 // In the catch and finally block of this try we have to go up
1752 // one level.
1753 --index;
1754 trycmd = NULL;
1755 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001756 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001758 if (trycmd->tcd_in_catch)
1759 {
1760 // exception inside ":catch", jump to ":finally" once
1761 ectx->ec_iidx = trycmd->tcd_finally_idx;
1762 trycmd->tcd_finally_idx = 0;
1763 }
1764 else
1765 // jump to first ":catch"
1766 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001767 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001768 did_throw = FALSE; // don't come back here until :endtry
1769 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 }
1771 else
1772 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001773 // Not inside try or need to return from current functions.
1774 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001775 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001776 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001777 tv = STACK_TV_BOT(0);
1778 tv->v_type = VAR_NUMBER;
1779 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001780 ++ectx->ec_stack.ga_len;
1781 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001783 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001784 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001785 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001786 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 goto done;
1788 }
1789
Bram Moolenaar4c137212021-04-19 16:48:48 +02001790 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001791 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 }
1793 continue;
1794 }
1795
Bram Moolenaar4c137212021-04-19 16:48:48 +02001796 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 switch (iptr->isn_type)
1798 {
1799 // execute Ex command line
1800 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001801 if (exec_command(iptr) == FAIL)
1802 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 break;
1804
Bram Moolenaar20677332021-06-06 17:02:53 +02001805 // execute Ex command line split at NL characters.
1806 case ISN_EXEC_SPLIT:
1807 {
1808 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001809 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001810
1811 SOURCING_LNUM = iptr->isn_lnum;
1812 CLEAR_FIELD(cookie);
1813 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1814 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001815 line = get_split_sourceline(0, &cookie, 0, 0);
1816 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001817 get_split_sourceline, &cookie,
1818 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1819 == FAIL
1820 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001821 {
1822 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001823 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001824 }
1825 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001826 }
1827 break;
1828
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001829 // execute Ex command line that is only a range
1830 case ISN_EXECRANGE:
1831 {
1832 exarg_T ea;
1833 char *error = NULL;
1834
1835 CLEAR_FIELD(ea);
1836 ea.cmdidx = CMD_SIZE;
1837 ea.addr_type = ADDR_LINES;
1838 ea.cmd = iptr->isn_arg.string;
1839 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00001840 if (ea.cmd == NULL)
1841 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001842 if (error == NULL)
1843 error = ex_range_without_command(&ea);
1844 if (error != NULL)
1845 {
1846 SOURCING_LNUM = iptr->isn_lnum;
1847 emsg(error);
1848 goto on_error;
1849 }
1850 }
1851 break;
1852
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001853 // Evaluate an expression with legacy syntax, push it onto the
1854 // stack.
1855 case ISN_LEGACY_EVAL:
1856 {
1857 char_u *arg = iptr->isn_arg.string;
1858 int res;
1859 int save_flags = cmdmod.cmod_flags;
1860
Bram Moolenaar35578162021-08-02 19:10:38 +02001861 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001862 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001863 tv = STACK_TV_BOT(0);
1864 init_tv(tv);
1865 cmdmod.cmod_flags |= CMOD_LEGACY;
1866 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1867 cmdmod.cmod_flags = save_flags;
1868 if (res == FAIL)
1869 goto on_error;
1870 ++ectx->ec_stack.ga_len;
1871 }
1872 break;
1873
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001874 // push typeval VAR_INSTR with instructions to be executed
1875 case ISN_INSTR:
1876 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001877 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001878 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001879 tv = STACK_TV_BOT(0);
1880 tv->vval.v_instr = ALLOC_ONE(instr_T);
1881 if (tv->vval.v_instr == NULL)
1882 goto on_error;
1883 ++ectx->ec_stack.ga_len;
1884
1885 tv->v_type = VAR_INSTR;
1886 tv->vval.v_instr->instr_ectx = ectx;
1887 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1888 }
1889 break;
1890
Bram Moolenaar4c137212021-04-19 16:48:48 +02001891 // execute :substitute with an expression
1892 case ISN_SUBSTITUTE:
1893 {
1894 subs_T *subs = &iptr->isn_arg.subs;
1895 source_cookie_T cookie;
1896 struct subs_expr_S *save_instr = substitute_instr;
1897 struct subs_expr_S subs_instr;
1898 int res;
1899
1900 subs_instr.subs_ectx = ectx;
1901 subs_instr.subs_instr = subs->subs_instr;
1902 subs_instr.subs_status = OK;
1903 substitute_instr = &subs_instr;
1904
1905 SOURCING_LNUM = iptr->isn_lnum;
1906 // This is very much like ISN_EXEC
1907 CLEAR_FIELD(cookie);
1908 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1909 res = do_cmdline(subs->subs_cmd,
1910 getsourceline, &cookie,
1911 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1912 substitute_instr = save_instr;
1913
1914 if (res == FAIL || did_emsg
1915 || subs_instr.subs_status == FAIL)
1916 goto on_error;
1917 }
1918 break;
1919
1920 case ISN_FINISH:
1921 goto done;
1922
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001923 case ISN_REDIRSTART:
1924 // create a dummy entry for var_redir_str()
1925 if (alloc_redir_lval() == FAIL)
1926 goto on_error;
1927
1928 // The output is stored in growarray "redir_ga" until
1929 // redirection ends.
1930 init_redir_ga();
1931 redir_vname = 1;
1932 break;
1933
1934 case ISN_REDIREND:
1935 {
1936 char_u *res = get_clear_redir_ga();
1937
1938 // End redirection, put redirected text on the stack.
1939 clear_redir_lval();
1940 redir_vname = 0;
1941
Bram Moolenaar35578162021-08-02 19:10:38 +02001942 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001943 {
1944 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001945 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001946 }
1947 tv = STACK_TV_BOT(0);
1948 tv->v_type = VAR_STRING;
1949 tv->vval.v_string = res;
1950 ++ectx->ec_stack.ga_len;
1951 }
1952 break;
1953
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001954 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001955#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001956 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1957 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001958#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001959 break;
1960
1961 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001962#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001963 {
1964 exarg_T ea;
1965 int res;
1966
1967 CLEAR_FIELD(ea);
1968 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1969 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1970 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1971 --ectx->ec_stack.ga_len;
1972 tv = STACK_TV_BOT(0);
1973 res = cexpr_core(&ea, tv);
1974 clear_tv(tv);
1975 if (res == FAIL)
1976 goto on_error;
1977 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001978#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001979 break;
1980
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001981 // execute Ex command from pieces on the stack
1982 case ISN_EXECCONCAT:
1983 {
1984 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001985 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001986 int pass;
1987 int i;
1988 char_u *cmd = NULL;
1989 char_u *str;
1990
1991 for (pass = 1; pass <= 2; ++pass)
1992 {
1993 for (i = 0; i < count; ++i)
1994 {
1995 tv = STACK_TV_BOT(i - count);
1996 str = tv->vval.v_string;
1997 if (str != NULL && *str != NUL)
1998 {
1999 if (pass == 2)
2000 STRCPY(cmd + len, str);
2001 len += STRLEN(str);
2002 }
2003 if (pass == 2)
2004 clear_tv(tv);
2005 }
2006 if (pass == 1)
2007 {
2008 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002009 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002010 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002011 len = 0;
2012 }
2013 }
2014
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002015 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002016 do_cmdline_cmd(cmd);
2017 vim_free(cmd);
2018 }
2019 break;
2020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 // execute :echo {string} ...
2022 case ISN_ECHO:
2023 {
2024 int count = iptr->isn_arg.echo.echo_count;
2025 int atstart = TRUE;
2026 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002027 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028
2029 for (idx = 0; idx < count; ++idx)
2030 {
2031 tv = STACK_TV_BOT(idx - count);
2032 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2033 &atstart, &needclr);
2034 clear_tv(tv);
2035 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002036 if (needclr)
2037 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002038 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 }
2040 break;
2041
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002042 // :execute {string} ...
2043 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002044 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002045 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002046 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002047 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002048 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002049 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002050 {
2051 int count = iptr->isn_arg.number;
2052 garray_T ga;
2053 char_u buf[NUMBUFLEN];
2054 char_u *p;
2055 int len;
2056 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002057 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002058
2059 ga_init2(&ga, 1, 80);
2060 for (idx = 0; idx < count; ++idx)
2061 {
2062 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002063 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002064 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002065 if (tv->v_type == VAR_CHANNEL
2066 || tv->v_type == VAR_JOB)
2067 {
2068 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002069 semsg(_(e_using_invalid_value_as_string_str),
2070 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002071 break;
2072 }
2073 else
2074 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002075 }
2076 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002077 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002078
2079 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002080 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002081 failed = TRUE;
2082 else
2083 {
2084 if (ga.ga_len > 0)
2085 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2086 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2087 ga.ga_len += len;
2088 }
2089 clear_tv(tv);
2090 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002091 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002092 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002093 {
2094 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002095 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002096 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002097
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002098 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002099 {
2100 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002101 {
2102 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002103 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002104 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002105 {
2106 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002107 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002108 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002109 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002110 else
2111 {
2112 msg_sb_eol();
2113 if (iptr->isn_type == ISN_ECHOMSG)
2114 {
2115 msg_attr(ga.ga_data, echo_attr);
2116 out_flush();
2117 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002118 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2119 {
2120 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2121 TRUE);
2122 ui_write((char_u *)"\r\n", 2, TRUE);
2123 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002124 else
2125 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002126 SOURCING_LNUM = iptr->isn_lnum;
2127 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002128 }
2129 }
2130 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002131 ga_clear(&ga);
2132 }
2133 break;
2134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 // load local variable or argument
2136 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002137 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002138 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002140 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 break;
2142
2143 // load v: variable
2144 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002145 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002146 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002148 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 break;
2150
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002151 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152 case ISN_LOADSCRIPT:
2153 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002154 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 svar_T *sv;
2156
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002157 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002158 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002159 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002160 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002161 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002162 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002164 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 }
2166 break;
2167
2168 // load s: variable in old script
2169 case ISN_LOADS:
2170 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002171 hashtab_T *ht = &SCRIPT_VARS(
2172 iptr->isn_arg.loadstore.ls_sid);
2173 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 if (di == NULL)
2177 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002178 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002179 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002180 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 }
2182 else
2183 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002184 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002185 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002187 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 }
2189 }
2190 break;
2191
Bram Moolenaard3aac292020-04-19 14:32:17 +02002192 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002194 case ISN_LOADB:
2195 case ISN_LOADW:
2196 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002198 dictitem_T *di = NULL;
2199 hashtab_T *ht = NULL;
2200 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002201
Bram Moolenaard3aac292020-04-19 14:32:17 +02002202 switch (iptr->isn_type)
2203 {
2204 case ISN_LOADG:
2205 ht = get_globvar_ht();
2206 namespace = 'g';
2207 break;
2208 case ISN_LOADB:
2209 ht = &curbuf->b_vars->dv_hashtab;
2210 namespace = 'b';
2211 break;
2212 case ISN_LOADW:
2213 ht = &curwin->w_vars->dv_hashtab;
2214 namespace = 'w';
2215 break;
2216 case ISN_LOADT:
2217 ht = &curtab->tp_vars->dv_hashtab;
2218 namespace = 't';
2219 break;
2220 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002221 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002222 }
2223 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 if (di == NULL)
2226 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002227 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002228 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002229 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002230 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 }
2232 else
2233 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002234 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002235 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002237 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 }
2239 }
2240 break;
2241
Bram Moolenaar03290b82020-12-19 16:30:44 +01002242 // load autoload variable
2243 case ISN_LOADAUTO:
2244 {
2245 char_u *name = iptr->isn_arg.string;
2246
Bram Moolenaar35578162021-08-02 19:10:38 +02002247 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002248 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002249 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002250 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002251 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002252 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002253 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002254 }
2255 break;
2256
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002257 // load g:/b:/w:/t: namespace
2258 case ISN_LOADGDICT:
2259 case ISN_LOADBDICT:
2260 case ISN_LOADWDICT:
2261 case ISN_LOADTDICT:
2262 {
2263 dict_T *d = NULL;
2264
2265 switch (iptr->isn_type)
2266 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002267 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2268 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2269 case ISN_LOADWDICT: d = curwin->w_vars; break;
2270 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002271 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002272 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002273 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002274 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002275 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002276 tv = STACK_TV_BOT(0);
2277 tv->v_type = VAR_DICT;
2278 tv->v_lock = 0;
2279 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002280 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002281 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002282 }
2283 break;
2284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 // load &option
2286 case ISN_LOADOPT:
2287 {
2288 typval_T optval;
2289 char_u *name = iptr->isn_arg.string;
2290
Bram Moolenaara8c17702020-04-01 21:17:24 +02002291 // This is not expected to fail, name is checked during
2292 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002293 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002294 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002295 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002296 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002298 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 }
2300 break;
2301
2302 // load $ENV
2303 case ISN_LOADENV:
2304 {
2305 typval_T optval;
2306 char_u *name = iptr->isn_arg.string;
2307
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 Moolenaar0bbf7222020-02-19 22:31:48 +01002310 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002311 (void)eval_env_var(&name, &optval, TRUE);
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 @register
2318 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002319 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002320 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 tv = STACK_TV_BOT(0);
2322 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002323 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002324 // This may result in NULL, which should be equivalent to an
2325 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 tv->vval.v_string = get_reg_contents(
2327 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002328 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 break;
2330
2331 // store local variable
2332 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002333 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 tv = STACK_TV_VAR(iptr->isn_arg.number);
2335 clear_tv(tv);
2336 *tv = *STACK_TV_BOT(0);
2337 break;
2338
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002339 // store s: variable in old script
2340 case ISN_STORES:
2341 {
2342 hashtab_T *ht = &SCRIPT_VARS(
2343 iptr->isn_arg.loadstore.ls_sid);
2344 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002345 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002346
Bram Moolenaar4c137212021-04-19 16:48:48 +02002347 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002348 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002349 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002350 else
2351 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002352 SOURCING_LNUM = iptr->isn_lnum;
2353 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002354 {
2355 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002356 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002357 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002358 clear_tv(&di->di_tv);
2359 di->di_tv = *STACK_TV_BOT(0);
2360 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002361 }
2362 break;
2363
2364 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 case ISN_STORESCRIPT:
2366 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002367 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2368 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002370 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002371 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002372 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002373 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002374
2375 // "const" and "final" are checked at compile time, locking
2376 // the value needs to be checked here.
2377 SOURCING_LNUM = iptr->isn_lnum;
2378 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002379 {
2380 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002381 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002382 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 clear_tv(sv->sv_tv);
2385 *sv->sv_tv = *STACK_TV_BOT(0);
2386 }
2387 break;
2388
2389 // store option
2390 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002391 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002393 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
2394 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 long n = 0;
2396 char_u *s = NULL;
2397 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002398 char_u numbuf[NUMBUFLEN];
2399 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400
Bram Moolenaar4c137212021-04-19 16:48:48 +02002401 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 tv = STACK_TV_BOT(0);
2403 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002404 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002406 if (s == NULL)
2407 s = (char_u *)"";
2408 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002409 else if (iptr->isn_type == ISN_STOREFUNCOPT)
2410 {
2411 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002412 // If the option can be set to a function reference or
2413 // a lambda and the passed value is a function
2414 // reference, then convert it to the name (string) of
2415 // the function reference.
2416 s = tv2string(tv, &tofree, numbuf, 0);
2417 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002418 {
2419 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002420 goto on_error;
2421 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002424 // must be VAR_NUMBER, CHECKTYPE makes sure
2425 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002426 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002427 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002428 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 if (msg != NULL)
2430 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002431 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002433 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 }
2436 break;
2437
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002438 // store $ENV
2439 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002440 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002441 tv = STACK_TV_BOT(0);
2442 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2443 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002444 break;
2445
2446 // store @r
2447 case ISN_STOREREG:
2448 {
2449 int reg = iptr->isn_arg.number;
2450
Bram Moolenaar4c137212021-04-19 16:48:48 +02002451 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002452 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002453 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002454 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002455 }
2456 break;
2457
2458 // store v: variable
2459 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002460 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002461 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2462 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002463 // should not happen, type is checked when compiling
2464 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002465 break;
2466
Bram Moolenaard3aac292020-04-19 14:32:17 +02002467 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002469 case ISN_STOREB:
2470 case ISN_STOREW:
2471 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002473 dictitem_T *di;
2474 hashtab_T *ht;
2475 char_u *name = iptr->isn_arg.string + 2;
2476
Bram Moolenaard3aac292020-04-19 14:32:17 +02002477 switch (iptr->isn_type)
2478 {
2479 case ISN_STOREG:
2480 ht = get_globvar_ht();
2481 break;
2482 case ISN_STOREB:
2483 ht = &curbuf->b_vars->dv_hashtab;
2484 break;
2485 case ISN_STOREW:
2486 ht = &curwin->w_vars->dv_hashtab;
2487 break;
2488 case ISN_STORET:
2489 ht = &curtab->tp_vars->dv_hashtab;
2490 break;
2491 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002492 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494
Bram Moolenaar4c137212021-04-19 16:48:48 +02002495 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002496 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002498 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 else
2500 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002501 SOURCING_LNUM = iptr->isn_lnum;
2502 if (var_check_permission(di, name) == FAIL)
2503 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 clear_tv(&di->di_tv);
2505 di->di_tv = *STACK_TV_BOT(0);
2506 }
2507 }
2508 break;
2509
Bram Moolenaar03290b82020-12-19 16:30:44 +01002510 // store an autoload variable
2511 case ISN_STOREAUTO:
2512 SOURCING_LNUM = iptr->isn_lnum;
2513 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2514 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002515 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002516 break;
2517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 // store number in local variable
2519 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002520 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 clear_tv(tv);
2522 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002523 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 break;
2525
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002526 // store value in list or dict variable
2527 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002528 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002529 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002530 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002531 typval_T *tv_dest = STACK_TV_BOT(-1);
2532 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002533
Bram Moolenaar752fc692021-01-04 21:57:11 +01002534 // Stack contains:
2535 // -3 value to be stored
2536 // -2 index
2537 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002538 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002539 SOURCING_LNUM = iptr->isn_lnum;
2540 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002541 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002542 dest_type = tv_dest->v_type;
2543 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002544 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002545 else if (dest_type == VAR_LIST
2546 && tv_idx->v_type != VAR_NUMBER)
2547 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002548 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002549 status = FAIL;
2550 }
2551 }
2552 else if (dest_type != tv_dest->v_type)
2553 {
2554 // just in case, should be OK
2555 semsg(_(e_expected_str_but_got_str),
2556 vartype_name(dest_type),
2557 vartype_name(tv_dest->v_type));
2558 status = FAIL;
2559 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002560
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002561 if (status == OK && dest_type == VAR_LIST)
2562 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002563 long lidx = (long)tv_idx->vval.v_number;
2564 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002565
2566 if (list == NULL)
2567 {
2568 emsg(_(e_list_not_set));
2569 goto on_error;
2570 }
2571 if (lidx < 0 && list->lv_len + lidx >= 0)
2572 // negative index is relative to the end
2573 lidx = list->lv_len + lidx;
2574 if (lidx < 0 || lidx > list->lv_len)
2575 {
2576 semsg(_(e_listidx), lidx);
2577 goto on_error;
2578 }
2579 if (lidx < list->lv_len)
2580 {
2581 listitem_T *li = list_find(list, lidx);
2582
2583 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002584 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002585 goto on_error;
2586 // overwrite existing list item
2587 clear_tv(&li->li_tv);
2588 li->li_tv = *tv;
2589 }
2590 else
2591 {
2592 if (error_if_locked(list->lv_lock,
2593 e_cannot_change_list))
2594 goto on_error;
2595 // append to list, only fails when out of memory
2596 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002597 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002598 clear_tv(tv);
2599 }
2600 }
2601 else if (status == OK && dest_type == VAR_DICT)
2602 {
2603 char_u *key = tv_idx->vval.v_string;
2604 dict_T *dict = tv_dest->vval.v_dict;
2605 dictitem_T *di;
2606
2607 SOURCING_LNUM = iptr->isn_lnum;
2608 if (dict == NULL)
2609 {
2610 emsg(_(e_dictionary_not_set));
2611 goto on_error;
2612 }
2613 if (key == NULL)
2614 key = (char_u *)"";
2615 di = dict_find(dict, key, -1);
2616 if (di != NULL)
2617 {
2618 if (error_if_locked(di->di_tv.v_lock,
2619 e_cannot_change_dict_item))
2620 goto on_error;
2621 // overwrite existing value
2622 clear_tv(&di->di_tv);
2623 di->di_tv = *tv;
2624 }
2625 else
2626 {
2627 if (error_if_locked(dict->dv_lock,
2628 e_cannot_change_dict))
2629 goto on_error;
2630 // add to dict, only fails when out of memory
2631 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002632 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002633 clear_tv(tv);
2634 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002635 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002636 else if (status == OK && dest_type == VAR_BLOB)
2637 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002638 long lidx = (long)tv_idx->vval.v_number;
2639 blob_T *blob = tv_dest->vval.v_blob;
2640 varnumber_T nr;
2641 int error = FALSE;
2642 int len;
2643
2644 if (blob == NULL)
2645 {
2646 emsg(_(e_blob_not_set));
2647 goto on_error;
2648 }
2649 len = blob_len(blob);
2650 if (lidx < 0 && len + lidx >= 0)
2651 // negative index is relative to the end
2652 lidx = len + lidx;
2653
2654 // Can add one byte at the end.
2655 if (lidx < 0 || lidx > len)
2656 {
2657 semsg(_(e_blobidx), lidx);
2658 goto on_error;
2659 }
2660 if (value_check_lock(blob->bv_lock,
2661 (char_u *)"blob", FALSE))
2662 goto on_error;
2663 nr = tv_get_number_chk(tv, &error);
2664 if (error)
2665 goto on_error;
2666 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002667 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002668 else
2669 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002670 status = FAIL;
2671 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002672 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002673
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002674 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002675 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002676 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002677 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002678 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002679 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002680 goto on_error;
2681 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002682 }
2683 break;
2684
Bram Moolenaar68452172021-04-12 21:21:02 +02002685 // store value in blob range
2686 case ISN_STORERANGE:
2687 {
2688 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2689 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2690 typval_T *tv_dest = STACK_TV_BOT(-1);
2691 int status = OK;
2692
2693 // Stack contains:
2694 // -4 value to be stored
2695 // -3 first index or "none"
2696 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002697 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002698 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002699 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002700 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002701 long n1;
2702 long n2;
2703 int error = FALSE;
2704
2705 SOURCING_LNUM = iptr->isn_lnum;
2706 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2707 if (error)
2708 status = FAIL;
2709 else
2710 {
2711 if (tv_idx2->v_type == VAR_SPECIAL
2712 && tv_idx2->vval.v_number == VVAL_NONE)
2713 n2 = list_len(tv_dest->vval.v_list) - 1;
2714 else
2715 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2716 if (error)
2717 status = FAIL;
2718 else
2719 {
2720 listitem_T *li1 = check_range_index_one(
2721 tv_dest->vval.v_list, &n1, FALSE);
2722
2723 if (li1 == NULL)
2724 status = FAIL;
2725 else
2726 {
2727 status = check_range_index_two(
2728 tv_dest->vval.v_list,
2729 &n1, li1, &n2, FALSE);
2730 if (status != FAIL)
2731 status = list_assign_range(
2732 tv_dest->vval.v_list,
2733 tv->vval.v_list,
2734 n1,
2735 n2,
2736 tv_idx2->v_type == VAR_SPECIAL,
2737 (char_u *)"=",
2738 (char_u *)"[unknown]");
2739 }
2740 }
2741 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002742 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002743 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002744 {
2745 varnumber_T n1;
2746 varnumber_T n2;
2747 int error = FALSE;
2748
2749 n1 = tv_get_number_chk(tv_idx1, &error);
2750 if (error)
2751 status = FAIL;
2752 else
2753 {
2754 if (tv_idx2->v_type == VAR_SPECIAL
2755 && tv_idx2->vval.v_number == VVAL_NONE)
2756 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2757 else
2758 n2 = tv_get_number_chk(tv_idx2, &error);
2759 if (error)
2760 status = FAIL;
2761 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002762 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002763 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002764
2765 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002766 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002767 || check_blob_range(bloblen,
2768 n1, n2, FALSE) == FAIL)
2769 status = FAIL;
2770 else
2771 status = blob_set_range(
2772 tv_dest->vval.v_blob, n1, n2, tv);
2773 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002774 }
2775 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002776 else
2777 {
2778 status = FAIL;
2779 emsg(_(e_blob_required));
2780 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002781
2782 clear_tv(tv_idx1);
2783 clear_tv(tv_idx2);
2784 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002785 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002786 clear_tv(tv);
2787
2788 if (status == FAIL)
2789 goto on_error;
2790 }
2791 break;
2792
Bram Moolenaar0186e582021-01-10 18:33:11 +01002793 // load or store variable or argument from outer scope
2794 case ISN_LOADOUTER:
2795 case ISN_STOREOUTER:
2796 {
2797 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002798 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2799 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002800
2801 while (depth > 1 && outer != NULL)
2802 {
2803 outer = outer->out_up;
2804 --depth;
2805 }
2806 if (outer == NULL)
2807 {
2808 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00002809 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
2810 || ectx->ec_outer_ref == NULL)
2811 // Possibly :def function called from legacy
2812 // context.
2813 emsg(_(e_closure_called_from_invalid_context));
2814 else
2815 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002816 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002817 }
2818 tv = ((typval_T *)outer->out_stack->ga_data)
2819 + outer->out_frame_idx + STACK_FRAME_SIZE
2820 + iptr->isn_arg.outer.outer_idx;
2821 if (iptr->isn_type == ISN_LOADOUTER)
2822 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002823 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002824 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002825 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002826 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002827 }
2828 else
2829 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002830 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002831 clear_tv(tv);
2832 *tv = *STACK_TV_BOT(0);
2833 }
2834 }
2835 break;
2836
Bram Moolenaar752fc692021-01-04 21:57:11 +01002837 // unlet item in list or dict variable
2838 case ISN_UNLETINDEX:
2839 {
2840 typval_T *tv_idx = STACK_TV_BOT(-2);
2841 typval_T *tv_dest = STACK_TV_BOT(-1);
2842 int status = OK;
2843
2844 // Stack contains:
2845 // -2 index
2846 // -1 dict or list
2847 if (tv_dest->v_type == VAR_DICT)
2848 {
2849 // unlet a dict item, index must be a string
2850 if (tv_idx->v_type != VAR_STRING)
2851 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002852 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002853 semsg(_(e_expected_str_but_got_str),
2854 vartype_name(VAR_STRING),
2855 vartype_name(tv_idx->v_type));
2856 status = FAIL;
2857 }
2858 else
2859 {
2860 dict_T *d = tv_dest->vval.v_dict;
2861 char_u *key = tv_idx->vval.v_string;
2862 dictitem_T *di = NULL;
2863
2864 if (key == NULL)
2865 key = (char_u *)"";
2866 if (d != NULL)
2867 di = dict_find(d, key, (int)STRLEN(key));
2868 if (di == NULL)
2869 {
2870 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002871 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002872 semsg(_(e_dictkey), key);
2873 status = FAIL;
2874 }
2875 else
2876 {
2877 // TODO: check for dict or item locked
2878 dictitem_remove(d, di);
2879 }
2880 }
2881 }
2882 else if (tv_dest->v_type == VAR_LIST)
2883 {
2884 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002885 SOURCING_LNUM = iptr->isn_lnum;
2886 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002887 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002888 status = FAIL;
2889 }
2890 else
2891 {
2892 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002893 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002894 listitem_T *li = NULL;
2895
2896 li = list_find(l, n);
2897 if (li == NULL)
2898 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002899 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002900 semsg(_(e_listidx), n);
2901 status = FAIL;
2902 }
2903 else
2904 // TODO: check for list or item locked
2905 listitem_remove(l, li);
2906 }
2907 }
2908 else
2909 {
2910 status = FAIL;
2911 semsg(_(e_cannot_index_str),
2912 vartype_name(tv_dest->v_type));
2913 }
2914
2915 clear_tv(tv_idx);
2916 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002917 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002918 if (status == FAIL)
2919 goto on_error;
2920 }
2921 break;
2922
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002923 // unlet range of items in list variable
2924 case ISN_UNLETRANGE:
2925 {
2926 // Stack contains:
2927 // -3 index1
2928 // -2 index2
2929 // -1 dict or list
2930 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2931 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2932 typval_T *tv_dest = STACK_TV_BOT(-1);
2933 int status = OK;
2934
2935 if (tv_dest->v_type == VAR_LIST)
2936 {
2937 // indexes must be a number
2938 SOURCING_LNUM = iptr->isn_lnum;
2939 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002940 || (tv_idx2->v_type != VAR_SPECIAL
2941 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002942 {
2943 status = FAIL;
2944 }
2945 else
2946 {
2947 list_T *l = tv_dest->vval.v_list;
2948 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002949 long n2 = tv_idx2->v_type == VAR_SPECIAL
2950 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002951 listitem_T *li;
2952
2953 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002954 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002955 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002956 else
2957 {
2958 if (n1 < 0)
2959 n1 = list_idx_of_item(l, li);
2960 if (n2 < 0)
2961 {
2962 listitem_T *li2 = list_find(l, n2);
2963
2964 if (li2 == NULL)
2965 status = FAIL;
2966 else
2967 n2 = list_idx_of_item(l, li2);
2968 }
2969 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002970 && tv_idx2->v_type != VAR_SPECIAL
2971 && n2 < n1)
2972 {
2973 semsg(_(e_listidx), n2);
2974 status = FAIL;
2975 }
2976 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002977 && list_unlet_range(l, li, NULL, n1,
2978 tv_idx2->v_type != VAR_SPECIAL, n2)
2979 == FAIL)
2980 status = FAIL;
2981 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002982 }
2983 }
2984 else
2985 {
2986 status = FAIL;
2987 SOURCING_LNUM = iptr->isn_lnum;
2988 semsg(_(e_cannot_index_str),
2989 vartype_name(tv_dest->v_type));
2990 }
2991
2992 clear_tv(tv_idx1);
2993 clear_tv(tv_idx2);
2994 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002995 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002996 if (status == FAIL)
2997 goto on_error;
2998 }
2999 break;
3000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 // push constant
3002 case ISN_PUSHNR:
3003 case ISN_PUSHBOOL:
3004 case ISN_PUSHSPEC:
3005 case ISN_PUSHF:
3006 case ISN_PUSHS:
3007 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003008 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003009 case ISN_PUSHCHANNEL:
3010 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003011 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003012 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003014 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003015 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 switch (iptr->isn_type)
3017 {
3018 case ISN_PUSHNR:
3019 tv->v_type = VAR_NUMBER;
3020 tv->vval.v_number = iptr->isn_arg.number;
3021 break;
3022 case ISN_PUSHBOOL:
3023 tv->v_type = VAR_BOOL;
3024 tv->vval.v_number = iptr->isn_arg.number;
3025 break;
3026 case ISN_PUSHSPEC:
3027 tv->v_type = VAR_SPECIAL;
3028 tv->vval.v_number = iptr->isn_arg.number;
3029 break;
3030#ifdef FEAT_FLOAT
3031 case ISN_PUSHF:
3032 tv->v_type = VAR_FLOAT;
3033 tv->vval.v_float = iptr->isn_arg.fnumber;
3034 break;
3035#endif
3036 case ISN_PUSHBLOB:
3037 blob_copy(iptr->isn_arg.blob, tv);
3038 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003039 case ISN_PUSHFUNC:
3040 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003041 if (iptr->isn_arg.string == NULL)
3042 tv->vval.v_string = NULL;
3043 else
3044 tv->vval.v_string =
3045 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003046 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003047 case ISN_PUSHCHANNEL:
3048#ifdef FEAT_JOB_CHANNEL
3049 tv->v_type = VAR_CHANNEL;
3050 tv->vval.v_channel = iptr->isn_arg.channel;
3051 if (tv->vval.v_channel != NULL)
3052 ++tv->vval.v_channel->ch_refcount;
3053#endif
3054 break;
3055 case ISN_PUSHJOB:
3056#ifdef FEAT_JOB_CHANNEL
3057 tv->v_type = VAR_JOB;
3058 tv->vval.v_job = iptr->isn_arg.job;
3059 if (tv->vval.v_job != NULL)
3060 ++tv->vval.v_job->jv_refcount;
3061#endif
3062 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 default:
3064 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003065 tv->vval.v_string = vim_strsave(
3066 iptr->isn_arg.string == NULL
3067 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 }
3069 break;
3070
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003071 case ISN_UNLET:
3072 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3073 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003074 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003075 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003076 case ISN_UNLETENV:
3077 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3078 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003079
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003080 case ISN_LOCKUNLOCK:
3081 {
3082 typval_T *lval_root_save = lval_root;
3083 int res;
3084
3085 // Stack has the local variable, argument the whole :lock
3086 // or :unlock command, like ISN_EXEC.
3087 --ectx->ec_stack.ga_len;
3088 lval_root = STACK_TV_BOT(0);
3089 res = exec_command(iptr);
3090 clear_tv(lval_root);
3091 lval_root = lval_root_save;
3092 if (res == FAIL)
3093 goto on_error;
3094 }
3095 break;
3096
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003097 case ISN_LOCKCONST:
3098 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3099 break;
3100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 // create a list from items on the stack; uses a single allocation
3102 // for the list header and the items
3103 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003104 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003105 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 break;
3107
3108 // create a dict from items on the stack
3109 case ISN_NEWDICT:
3110 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003111 int count = iptr->isn_arg.number;
3112 dict_T *dict = dict_alloc();
3113 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003114 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003115 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003117 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003118 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 for (idx = 0; idx < count; ++idx)
3120 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003121 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003123 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003124 key = tv->vval.v_string == NULL
3125 ? (char_u *)"" : tv->vval.v_string;
3126 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003127 if (item != NULL)
3128 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003129 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003130 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003131 dict_unref(dict);
3132 goto on_error;
3133 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003134 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003136 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003137 {
3138 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003139 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3142 item->di_tv.v_lock = 0;
3143 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003144 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003145 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003146 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003147 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 }
3150
3151 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003152 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003153 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003154 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003156 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 tv = STACK_TV_BOT(-1);
3158 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003159 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 tv->vval.v_dict = dict;
3161 ++dict->dv_refcount;
3162 }
3163 break;
3164
3165 // call a :def function
3166 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003167 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003168 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3169 NULL,
3170 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003171 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003172 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 break;
3174
3175 // call a builtin function
3176 case ISN_BCALL:
3177 SOURCING_LNUM = iptr->isn_lnum;
3178 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3179 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003180 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003181 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 break;
3183
3184 // call a funcref or partial
3185 case ISN_PCALL:
3186 {
3187 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3188 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003189 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190
3191 SOURCING_LNUM = iptr->isn_lnum;
3192 if (pfunc->cpf_top)
3193 {
3194 // funcref is above the arguments
3195 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3196 }
3197 else
3198 {
3199 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003200 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003201 partial_tv = *STACK_TV_BOT(0);
3202 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003204 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003205 if (tv == &partial_tv)
3206 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003208 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 }
3210 break;
3211
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003212 case ISN_PCALL_END:
3213 // PCALL finished, arguments have been consumed and replaced by
3214 // the return value. Now clear the funcref from the stack,
3215 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003216 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003217 clear_tv(STACK_TV_BOT(-1));
3218 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3219 break;
3220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 // call a user defined function or funcref/partial
3222 case ISN_UCALL:
3223 {
3224 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3225
3226 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003227 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003228 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003229 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 }
3231 break;
3232
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003233 // return from a :def function call without a value
3234 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003235 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003236 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003237 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003238 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003239 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003240 tv->vval.v_number = 0;
3241 tv->v_lock = 0;
3242 // FALLTHROUGH
3243
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003244 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 case ISN_RETURN:
3246 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003247 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003248 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003249
3250 if (trystack->ga_len > 0)
3251 trycmd = ((trycmd_T *)trystack->ga_data)
3252 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003253 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003254 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003256 // jump to ":finally" or ":endtry"
3257 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003258 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003259 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003260 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 trycmd->tcd_return = TRUE;
3262 }
3263 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003264 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 }
3266 break;
3267
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003268 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 case ISN_FUNCREF:
3270 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003271 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003272 ufunc_T *ufunc;
3273 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003276 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003277 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003278 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003279 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003280 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003281 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003282 if (funcref->fr_func_name == NULL)
3283 {
3284 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3285 + funcref->fr_dfunc_idx;
3286
3287 ufunc = pt_dfunc->df_ufunc;
3288 }
3289 else
3290 {
3291 ufunc = find_func(funcref->fr_func_name, FALSE, NULL);
3292 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003293 if (ufunc == NULL)
3294 {
3295 SOURCING_LNUM = iptr->isn_lnum;
3296 emsg(_(e_function_reference_invalid));
3297 goto theend;
3298 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003299 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003300 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003302 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 tv->vval.v_partial = pt;
3304 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003305 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 }
3307 break;
3308
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003309 // Create a global function from a lambda.
3310 case ISN_NEWFUNC:
3311 {
3312 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3313
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003314 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003315 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003316 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003317 }
3318 break;
3319
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003320 // List functions
3321 case ISN_DEF:
3322 if (iptr->isn_arg.string == NULL)
3323 list_functions(NULL);
3324 else
3325 {
3326 exarg_T ea;
3327
3328 CLEAR_FIELD(ea);
3329 ea.cmd = ea.arg = iptr->isn_arg.string;
3330 define_function(&ea, NULL);
3331 }
3332 break;
3333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 // jump if a condition is met
3335 case ISN_JUMP:
3336 {
3337 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003338 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 int jump = TRUE;
3340
3341 if (when != JUMP_ALWAYS)
3342 {
3343 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003344 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003345 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003346 || when == JUMP_IF_COND_TRUE)
3347 {
3348 SOURCING_LNUM = iptr->isn_lnum;
3349 jump = tv_get_bool_chk(tv, &error);
3350 if (error)
3351 goto on_error;
3352 }
3353 else
3354 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003356 || when == JUMP_AND_KEEP_IF_FALSE
3357 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003359 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 {
3361 // drop the value from the stack
3362 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003363 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 }
3365 }
3366 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003367 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 }
3369 break;
3370
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003371 // Jump if an argument with a default value was already set and not
3372 // v:none.
3373 case ISN_JUMP_IF_ARG_SET:
3374 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3375 if (tv->v_type != VAR_UNKNOWN
3376 && !(tv->v_type == VAR_SPECIAL
3377 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003378 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003379 break;
3380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 // top of a for loop
3382 case ISN_FOR:
3383 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003384 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 typval_T *idxtv =
3386 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3387
Bram Moolenaar35578162021-08-02 19:10:38 +02003388 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003389 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003390 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003391 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003392 list_T *list = ltv->vval.v_list;
3393
3394 // push the next item from the list
3395 ++idxtv->vval.v_number;
3396 if (list == NULL
3397 || idxtv->vval.v_number >= list->lv_len)
3398 {
3399 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003400 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3401 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003402 }
3403 else if (list->lv_first == &range_list_item)
3404 {
3405 // non-materialized range() list
3406 tv = STACK_TV_BOT(0);
3407 tv->v_type = VAR_NUMBER;
3408 tv->v_lock = 0;
3409 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003411 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003412 }
3413 else
3414 {
3415 listitem_T *li = list_find(list,
3416 idxtv->vval.v_number);
3417
3418 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003419 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003420 }
3421 }
3422 else if (ltv->v_type == VAR_STRING)
3423 {
3424 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003425
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003426 // The index is for the last byte of the previous
3427 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003428 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003429 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003430 {
3431 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003432 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3433 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003434 }
3435 else
3436 {
3437 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3438
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003439 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003440 tv = STACK_TV_BOT(0);
3441 tv->v_type = VAR_STRING;
3442 tv->vval.v_string = vim_strnsave(
3443 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003444 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003445 idxtv->vval.v_number += clen - 1;
3446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003448 else if (ltv->v_type == VAR_BLOB)
3449 {
3450 blob_T *blob = ltv->vval.v_blob;
3451
3452 // When we get here the first time make a copy of the
3453 // blob, so that the iteration still works when it is
3454 // changed.
3455 if (idxtv->vval.v_number == -1 && blob != NULL)
3456 {
3457 blob_copy(blob, ltv);
3458 blob_unref(blob);
3459 blob = ltv->vval.v_blob;
3460 }
3461
3462 // The index is for the previous byte.
3463 ++idxtv->vval.v_number;
3464 if (blob == NULL
3465 || idxtv->vval.v_number >= blob_len(blob))
3466 {
3467 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003468 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3469 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003470 }
3471 else
3472 {
3473 // Push the next byte from the blob.
3474 tv = STACK_TV_BOT(0);
3475 tv->v_type = VAR_NUMBER;
3476 tv->vval.v_number = blob_get(blob,
3477 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003478 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003479 }
3480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 else
3482 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003483 semsg(_(e_for_loop_on_str_not_supported),
3484 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003485 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 }
3487 }
3488 break;
3489
3490 // start of ":try" block
3491 case ISN_TRY:
3492 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003493 trycmd_T *trycmd = NULL;
3494
Bram Moolenaar35578162021-08-02 19:10:38 +02003495 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003496 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003497 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3498 + ectx->ec_trystack.ga_len;
3499 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003501 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003502 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3503 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003504 trycmd->tcd_catch_idx =
3505 iptr->isn_arg.try.try_ref->try_catch;
3506 trycmd->tcd_finally_idx =
3507 iptr->isn_arg.try.try_ref->try_finally;
3508 trycmd->tcd_endtry_idx =
3509 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 }
3511 break;
3512
3513 case ISN_PUSHEXC:
3514 if (current_exception == NULL)
3515 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003516 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003518 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003520 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003521 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003523 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003525 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 tv->vval.v_string = vim_strsave(
3527 (char_u *)current_exception->value);
3528 break;
3529
3530 case ISN_CATCH:
3531 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003532 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533
Bram Moolenaar4c137212021-04-19 16:48:48 +02003534 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 if (trystack->ga_len > 0)
3536 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003537 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 + trystack->ga_len - 1;
3539 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003540 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 }
3542 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003543 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 catch_exception(current_exception);
3545 }
3546 break;
3547
Bram Moolenaarc150c092021-02-13 15:02:46 +01003548 case ISN_TRYCONT:
3549 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003550 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003551 trycont_T *trycont = &iptr->isn_arg.trycont;
3552 int i;
3553 trycmd_T *trycmd;
3554 int iidx = trycont->tct_where;
3555
3556 if (trystack->ga_len < trycont->tct_levels)
3557 {
3558 siemsg("TRYCONT: expected %d levels, found %d",
3559 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003560 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003561 }
3562 // Make :endtry jump to any outer try block and the last
3563 // :endtry inside the loop to the loop start.
3564 for (i = trycont->tct_levels; i > 0; --i)
3565 {
3566 trycmd = ((trycmd_T *)trystack->ga_data)
3567 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003568 // Add one to tcd_cont to be able to jump to
3569 // instruction with index zero.
3570 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003571 iidx = trycmd->tcd_finally_idx == 0
3572 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003573 }
3574 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003575 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003576 }
3577 break;
3578
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003579 case ISN_FINALLY:
3580 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003581 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003582 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3583 + trystack->ga_len - 1;
3584
3585 // Reset the index to avoid a return statement jumps here
3586 // again.
3587 trycmd->tcd_finally_idx = 0;
3588 break;
3589 }
3590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 // end of ":try" block
3592 case ISN_ENDTRY:
3593 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003594 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595
3596 if (trystack->ga_len > 0)
3597 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003598 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 --trystack->ga_len;
3601 --trylevel;
3602 trycmd = ((trycmd_T *)trystack->ga_data)
3603 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003604 if (trycmd->tcd_did_throw)
3605 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003606 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 {
3608 // discard the exception
3609 if (caught_stack == current_exception)
3610 caught_stack = caught_stack->caught;
3611 discard_current_exception();
3612 }
3613
3614 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003615 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003616
Bram Moolenaar4c137212021-04-19 16:48:48 +02003617 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003618 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003619 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003620 clear_tv(STACK_TV_BOT(0));
3621 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003622 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003623 // handling :continue: jump to outer try block or
3624 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 }
3627 }
3628 break;
3629
3630 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003631 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003632 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003633
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003634 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3635 {
3636 // throwing an exception while using "silent!" causes
3637 // the function to abort but not display an error.
3638 tv = STACK_TV_BOT(-1);
3639 clear_tv(tv);
3640 tv->v_type = VAR_NUMBER;
3641 tv->vval.v_number = 0;
3642 goto done;
3643 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003644 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003645 tv = STACK_TV_BOT(0);
3646 if (tv->vval.v_string == NULL
3647 || *skipwhite(tv->vval.v_string) == NUL)
3648 {
3649 vim_free(tv->vval.v_string);
3650 SOURCING_LNUM = iptr->isn_lnum;
3651 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003652 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003653 }
3654
3655 // Inside a "catch" we need to first discard the caught
3656 // exception.
3657 if (trystack->ga_len > 0)
3658 {
3659 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3660 + trystack->ga_len - 1;
3661 if (trycmd->tcd_caught && current_exception != NULL)
3662 {
3663 // discard the exception
3664 if (caught_stack == current_exception)
3665 caught_stack = caught_stack->caught;
3666 discard_current_exception();
3667 trycmd->tcd_caught = FALSE;
3668 }
3669 }
3670
3671 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3672 == FAIL)
3673 {
3674 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003675 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003676 }
3677 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 break;
3680
3681 // compare with special values
3682 case ISN_COMPAREBOOL:
3683 case ISN_COMPARESPECIAL:
3684 {
3685 typval_T *tv1 = STACK_TV_BOT(-2);
3686 typval_T *tv2 = STACK_TV_BOT(-1);
3687 varnumber_T arg1 = tv1->vval.v_number;
3688 varnumber_T arg2 = tv2->vval.v_number;
3689 int res;
3690
3691 switch (iptr->isn_arg.op.op_type)
3692 {
3693 case EXPR_EQUAL: res = arg1 == arg2; break;
3694 case EXPR_NEQUAL: res = arg1 != arg2; break;
3695 default: res = 0; break;
3696 }
3697
Bram Moolenaar4c137212021-04-19 16:48:48 +02003698 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 tv1->v_type = VAR_BOOL;
3700 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3701 }
3702 break;
3703
3704 // Operation with two number arguments
3705 case ISN_OPNR:
3706 case ISN_COMPARENR:
3707 {
3708 typval_T *tv1 = STACK_TV_BOT(-2);
3709 typval_T *tv2 = STACK_TV_BOT(-1);
3710 varnumber_T arg1 = tv1->vval.v_number;
3711 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003712 varnumber_T res = 0;
3713 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714
3715 switch (iptr->isn_arg.op.op_type)
3716 {
3717 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003718 case EXPR_DIV: if (arg2 == 0)
3719 div_zero = TRUE;
3720 else
3721 res = arg1 / arg2;
3722 break;
3723 case EXPR_REM: if (arg2 == 0)
3724 div_zero = TRUE;
3725 else
3726 res = arg1 % arg2;
3727 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 case EXPR_SUB: res = arg1 - arg2; break;
3729 case EXPR_ADD: res = arg1 + arg2; break;
3730
3731 case EXPR_EQUAL: res = arg1 == arg2; break;
3732 case EXPR_NEQUAL: res = arg1 != arg2; break;
3733 case EXPR_GREATER: res = arg1 > arg2; break;
3734 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3735 case EXPR_SMALLER: res = arg1 < arg2; break;
3736 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003737 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 }
3739
Bram Moolenaar4c137212021-04-19 16:48:48 +02003740 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 if (iptr->isn_type == ISN_COMPARENR)
3742 {
3743 tv1->v_type = VAR_BOOL;
3744 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3745 }
3746 else
3747 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003748 if (div_zero)
3749 {
3750 SOURCING_LNUM = iptr->isn_lnum;
3751 emsg(_(e_divide_by_zero));
3752 goto on_error;
3753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 }
3755 break;
3756
3757 // Computation with two float arguments
3758 case ISN_OPFLOAT:
3759 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003760#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 {
3762 typval_T *tv1 = STACK_TV_BOT(-2);
3763 typval_T *tv2 = STACK_TV_BOT(-1);
3764 float_T arg1 = tv1->vval.v_float;
3765 float_T arg2 = tv2->vval.v_float;
3766 float_T res = 0;
3767 int cmp = FALSE;
3768
3769 switch (iptr->isn_arg.op.op_type)
3770 {
3771 case EXPR_MULT: res = arg1 * arg2; break;
3772 case EXPR_DIV: res = arg1 / arg2; break;
3773 case EXPR_SUB: res = arg1 - arg2; break;
3774 case EXPR_ADD: res = arg1 + arg2; break;
3775
3776 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3777 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3778 case EXPR_GREATER: cmp = arg1 > arg2; break;
3779 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3780 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3781 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3782 default: cmp = 0; break;
3783 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 if (iptr->isn_type == ISN_COMPAREFLOAT)
3786 {
3787 tv1->v_type = VAR_BOOL;
3788 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3789 }
3790 else
3791 tv1->vval.v_float = res;
3792 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003793#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 break;
3795
3796 case ISN_COMPARELIST:
3797 {
3798 typval_T *tv1 = STACK_TV_BOT(-2);
3799 typval_T *tv2 = STACK_TV_BOT(-1);
3800 list_T *arg1 = tv1->vval.v_list;
3801 list_T *arg2 = tv2->vval.v_list;
3802 int cmp = FALSE;
3803 int ic = iptr->isn_arg.op.op_ic;
3804
3805 switch (iptr->isn_arg.op.op_type)
3806 {
3807 case EXPR_EQUAL: cmp =
3808 list_equal(arg1, arg2, ic, FALSE); break;
3809 case EXPR_NEQUAL: cmp =
3810 !list_equal(arg1, arg2, ic, FALSE); break;
3811 case EXPR_IS: cmp = arg1 == arg2; break;
3812 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3813 default: cmp = 0; break;
3814 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003815 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 clear_tv(tv1);
3817 clear_tv(tv2);
3818 tv1->v_type = VAR_BOOL;
3819 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3820 }
3821 break;
3822
3823 case ISN_COMPAREBLOB:
3824 {
3825 typval_T *tv1 = STACK_TV_BOT(-2);
3826 typval_T *tv2 = STACK_TV_BOT(-1);
3827 blob_T *arg1 = tv1->vval.v_blob;
3828 blob_T *arg2 = tv2->vval.v_blob;
3829 int cmp = FALSE;
3830
3831 switch (iptr->isn_arg.op.op_type)
3832 {
3833 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3834 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3835 case EXPR_IS: cmp = arg1 == arg2; break;
3836 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3837 default: cmp = 0; break;
3838 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 clear_tv(tv1);
3841 clear_tv(tv2);
3842 tv1->v_type = VAR_BOOL;
3843 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3844 }
3845 break;
3846
3847 // TODO: handle separately
3848 case ISN_COMPARESTRING:
3849 case ISN_COMPAREDICT:
3850 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 case ISN_COMPAREANY:
3852 {
3853 typval_T *tv1 = STACK_TV_BOT(-2);
3854 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003855 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 int ic = iptr->isn_arg.op.op_ic;
3857
Bram Moolenaareb26f432020-09-14 16:50:05 +02003858 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003859 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003861 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 }
3863 break;
3864
3865 case ISN_ADDLIST:
3866 case ISN_ADDBLOB:
3867 {
3868 typval_T *tv1 = STACK_TV_BOT(-2);
3869 typval_T *tv2 = STACK_TV_BOT(-1);
3870
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003871 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003873 {
3874 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3875 && tv1->vval.v_list != NULL)
3876 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3877 NULL);
3878 else
3879 eval_addlist(tv1, tv2);
3880 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 else
3882 eval_addblob(tv1, tv2);
3883 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003884 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 }
3886 break;
3887
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003888 case ISN_LISTAPPEND:
3889 {
3890 typval_T *tv1 = STACK_TV_BOT(-2);
3891 typval_T *tv2 = STACK_TV_BOT(-1);
3892 list_T *l = tv1->vval.v_list;
3893
3894 // add an item to a list
3895 if (l == NULL)
3896 {
3897 SOURCING_LNUM = iptr->isn_lnum;
3898 emsg(_(e_cannot_add_to_null_list));
3899 goto on_error;
3900 }
3901 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003902 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003903 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003904 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003905 }
3906 break;
3907
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003908 case ISN_BLOBAPPEND:
3909 {
3910 typval_T *tv1 = STACK_TV_BOT(-2);
3911 typval_T *tv2 = STACK_TV_BOT(-1);
3912 blob_T *b = tv1->vval.v_blob;
3913 int error = FALSE;
3914 varnumber_T n;
3915
3916 // add a number to a blob
3917 if (b == NULL)
3918 {
3919 SOURCING_LNUM = iptr->isn_lnum;
3920 emsg(_(e_cannot_add_to_null_blob));
3921 goto on_error;
3922 }
3923 n = tv_get_number_chk(tv2, &error);
3924 if (error)
3925 goto on_error;
3926 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003927 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003928 }
3929 break;
3930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 // Computation with two arguments of unknown type
3932 case ISN_OPANY:
3933 {
3934 typval_T *tv1 = STACK_TV_BOT(-2);
3935 typval_T *tv2 = STACK_TV_BOT(-1);
3936 varnumber_T n1, n2;
3937#ifdef FEAT_FLOAT
3938 float_T f1 = 0, f2 = 0;
3939#endif
3940 int error = FALSE;
3941
3942 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3943 {
3944 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3945 {
3946 eval_addlist(tv1, tv2);
3947 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003948 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 break;
3950 }
3951 else if (tv1->v_type == VAR_BLOB
3952 && tv2->v_type == VAR_BLOB)
3953 {
3954 eval_addblob(tv1, tv2);
3955 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003956 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 break;
3958 }
3959 }
3960#ifdef FEAT_FLOAT
3961 if (tv1->v_type == VAR_FLOAT)
3962 {
3963 f1 = tv1->vval.v_float;
3964 n1 = 0;
3965 }
3966 else
3967#endif
3968 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003969 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 n1 = tv_get_number_chk(tv1, &error);
3971 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003972 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973#ifdef FEAT_FLOAT
3974 if (tv2->v_type == VAR_FLOAT)
3975 f1 = n1;
3976#endif
3977 }
3978#ifdef FEAT_FLOAT
3979 if (tv2->v_type == VAR_FLOAT)
3980 {
3981 f2 = tv2->vval.v_float;
3982 n2 = 0;
3983 }
3984 else
3985#endif
3986 {
3987 n2 = tv_get_number_chk(tv2, &error);
3988 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003989 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990#ifdef FEAT_FLOAT
3991 if (tv1->v_type == VAR_FLOAT)
3992 f2 = n2;
3993#endif
3994 }
3995#ifdef FEAT_FLOAT
3996 // if there is a float on either side the result is a float
3997 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3998 {
3999 switch (iptr->isn_arg.op.op_type)
4000 {
4001 case EXPR_MULT: f1 = f1 * f2; break;
4002 case EXPR_DIV: f1 = f1 / f2; break;
4003 case EXPR_SUB: f1 = f1 - f2; break;
4004 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004005 default: SOURCING_LNUM = iptr->isn_lnum;
4006 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004007 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 }
4009 clear_tv(tv1);
4010 clear_tv(tv2);
4011 tv1->v_type = VAR_FLOAT;
4012 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004013 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 }
4015 else
4016#endif
4017 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004018 int failed = FALSE;
4019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 switch (iptr->isn_arg.op.op_type)
4021 {
4022 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004023 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4024 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004025 goto on_error;
4026 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 case EXPR_SUB: n1 = n1 - n2; break;
4028 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004029 default: n1 = num_modulus(n1, n2, &failed);
4030 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004031 goto on_error;
4032 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 }
4034 clear_tv(tv1);
4035 clear_tv(tv2);
4036 tv1->v_type = VAR_NUMBER;
4037 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004038 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 }
4040 }
4041 break;
4042
4043 case ISN_CONCAT:
4044 {
4045 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4046 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4047 char_u *res;
4048
4049 res = concat_str(str1, str2);
4050 clear_tv(STACK_TV_BOT(-2));
4051 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004052 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 STACK_TV_BOT(-1)->vval.v_string = res;
4054 }
4055 break;
4056
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004057 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004058 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004059 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004060 int is_slice = iptr->isn_type == ISN_STRSLICE;
4061 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004062 char_u *res;
4063
4064 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004065 // string slice: string is at stack-3, first index at
4066 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004067 if (is_slice)
4068 {
4069 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004070 n1 = tv->vval.v_number;
4071 }
4072
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004073 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004074 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004075
Bram Moolenaar4c137212021-04-19 16:48:48 +02004076 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004077 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004078 if (is_slice)
4079 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004080 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004081 else
4082 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004083 // single character (including composing characters).
4084 // If the index is too big or negative the result is
4085 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004086 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004087 vim_free(tv->vval.v_string);
4088 tv->vval.v_string = res;
4089 }
4090 break;
4091
4092 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004093 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004094 case ISN_BLOBINDEX:
4095 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004097 int is_slice = iptr->isn_type == ISN_LISTSLICE
4098 || iptr->isn_type == ISN_BLOBSLICE;
4099 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4100 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004101 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004102 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103
4104 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004105 // list slice: list is at stack-3, indexes at stack-2 and
4106 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004107 // Same for blob.
4108 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109
4110 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004111 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004113
4114 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 {
Bram Moolenaared591872020-08-15 22:14:53 +02004116 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004117 n1 = tv->vval.v_number;
4118 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 }
Bram Moolenaared591872020-08-15 22:14:53 +02004120
Bram Moolenaar4c137212021-04-19 16:48:48 +02004121 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004122 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004123 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004124 if (is_blob)
4125 {
4126 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4127 n1, n2, FALSE, tv) == FAIL)
4128 goto on_error;
4129 }
4130 else
4131 {
4132 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4133 n1, n2, FALSE, tv, TRUE) == FAIL)
4134 goto on_error;
4135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 }
4137 break;
4138
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004139 case ISN_ANYINDEX:
4140 case ISN_ANYSLICE:
4141 {
4142 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4143 typval_T *var1, *var2;
4144 int res;
4145
4146 // index: composite is at stack-2, index at stack-1
4147 // slice: composite is at stack-3, indexes at stack-2 and
4148 // stack-1
4149 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004150 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004151 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4152 goto on_error;
4153 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4154 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004155 res = eval_index_inner(tv, is_slice, var1, var2,
4156 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004157 clear_tv(var1);
4158 if (is_slice)
4159 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004160 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004161 if (res == FAIL)
4162 goto on_error;
4163 }
4164 break;
4165
Bram Moolenaar9af78762020-06-16 11:34:42 +02004166 case ISN_SLICE:
4167 {
4168 list_T *list;
4169 int count = iptr->isn_arg.number;
4170
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004171 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004172 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004173 list = tv->vval.v_list;
4174
4175 // no error for short list, expect it to be checked earlier
4176 if (list != NULL && list->lv_len >= count)
4177 {
4178 list_T *newlist = list_slice(list,
4179 count, list->lv_len - 1);
4180
4181 if (newlist != NULL)
4182 {
4183 list_unref(list);
4184 tv->vval.v_list = newlist;
4185 ++newlist->lv_refcount;
4186 }
4187 }
4188 }
4189 break;
4190
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004191 case ISN_GETITEM:
4192 {
4193 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004194 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004195
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004196 // Get list item: list is at stack-1, push item.
4197 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004198 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4199 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004200
Bram Moolenaar35578162021-08-02 19:10:38 +02004201 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004202 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004203 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004204 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004205
4206 // Useful when used in unpack assignment. Reset at
4207 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004208 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004209 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004210 }
4211 break;
4212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 case ISN_MEMBER:
4214 {
4215 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004216 char_u *key;
4217 dictitem_T *di;
4218
4219 // dict member: dict is at stack-2, key at stack-1
4220 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004221 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004222 dict = tv->vval.v_dict;
4223
4224 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004225 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004226 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004227 if (key == NULL)
4228 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004229
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004230 if ((di = dict_find(dict, key, -1)) == NULL)
4231 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004232 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004233 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004234
4235 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004236 // stack contents makes sense and the dict stack is
4237 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004238 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004239 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004240 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004241 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004242 tv->v_type = VAR_NUMBER;
4243 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004244 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004245 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004246 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004247 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004248 // Put the dict used on the dict stack, it might be used by
4249 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004250 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004251 if (dict_stack_save(tv) == FAIL)
4252 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004253 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004254 }
4255 break;
4256
4257 // dict member with string key
4258 case ISN_STRINGMEMBER:
4259 {
4260 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 dictitem_T *di;
4262
4263 tv = STACK_TV_BOT(-1);
4264 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4265 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004266 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004268 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 }
4270 dict = tv->vval.v_dict;
4271
4272 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4273 == NULL)
4274 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004275 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004277 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004279 // Put the dict used on the dict stack, it might be used by
4280 // a dict function later.
4281 if (dict_stack_save(tv) == FAIL)
4282 goto on_fatal_error;
4283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004285 }
4286 break;
4287
4288 case ISN_CLEARDICT:
4289 dict_stack_drop();
4290 break;
4291
4292 case ISN_USEDICT:
4293 {
4294 typval_T *dict_tv = dict_stack_get_tv();
4295
4296 // Turn "dict.Func" into a partial for "Func" bound to
4297 // "dict". Don't do this when "Func" is already a partial
4298 // that was bound explicitly (pt_auto is FALSE).
4299 tv = STACK_TV_BOT(-1);
4300 if (dict_tv != NULL
4301 && dict_tv->v_type == VAR_DICT
4302 && dict_tv->vval.v_dict != NULL
4303 && (tv->v_type == VAR_FUNC
4304 || (tv->v_type == VAR_PARTIAL
4305 && (tv->vval.v_partial->pt_auto
4306 || tv->vval.v_partial->pt_dict == NULL))))
4307 dict_tv->vval.v_dict =
4308 make_partial(dict_tv->vval.v_dict, tv);
4309 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 }
4311 break;
4312
4313 case ISN_NEGATENR:
4314 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004315 if (tv->v_type != VAR_NUMBER
4316#ifdef FEAT_FLOAT
4317 && tv->v_type != VAR_FLOAT
4318#endif
4319 )
4320 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004321 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004322 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004323 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004324 }
4325#ifdef FEAT_FLOAT
4326 if (tv->v_type == VAR_FLOAT)
4327 tv->vval.v_float = -tv->vval.v_float;
4328 else
4329#endif
4330 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 break;
4332
4333 case ISN_CHECKNR:
4334 {
4335 int error = FALSE;
4336
4337 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004338 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004340 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 (void)tv_get_number_chk(tv, &error);
4342 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004343 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 }
4345 break;
4346
4347 case ISN_CHECKTYPE:
4348 {
4349 checktype_T *ct = &iptr->isn_arg.type;
4350
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004351 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004352 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004353 if (!ectx->ec_where.wt_variable)
4354 ectx->ec_where.wt_index = ct->ct_arg_idx;
4355 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4356 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004357 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004358 if (!ectx->ec_where.wt_variable)
4359 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004360
4361 // number 0 is FALSE, number 1 is TRUE
4362 if (tv->v_type == VAR_NUMBER
4363 && ct->ct_type->tt_type == VAR_BOOL
4364 && (tv->vval.v_number == 0
4365 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004367 tv->v_type = VAR_BOOL;
4368 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004369 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 }
4371 }
4372 break;
4373
Bram Moolenaar9af78762020-06-16 11:34:42 +02004374 case ISN_CHECKLEN:
4375 {
4376 int min_len = iptr->isn_arg.checklen.cl_min_len;
4377 list_T *list = NULL;
4378
4379 tv = STACK_TV_BOT(-1);
4380 if (tv->v_type == VAR_LIST)
4381 list = tv->vval.v_list;
4382 if (list == NULL || list->lv_len < min_len
4383 || (list->lv_len > min_len
4384 && !iptr->isn_arg.checklen.cl_more_OK))
4385 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004386 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004387 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004388 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004389 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004390 }
4391 }
4392 break;
4393
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004394 case ISN_SETTYPE:
4395 {
4396 checktype_T *ct = &iptr->isn_arg.type;
4397
4398 tv = STACK_TV_BOT(-1);
4399 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4400 {
4401 free_type(tv->vval.v_dict->dv_type);
4402 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4403 }
4404 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4405 {
4406 free_type(tv->vval.v_list->lv_type);
4407 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4408 }
4409 }
4410 break;
4411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004413 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 {
4415 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004416 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004418 if (iptr->isn_type == ISN_2BOOL)
4419 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004420 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004421 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004422 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004423 n = !n;
4424 }
4425 else
4426 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004427 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004428 SOURCING_LNUM = iptr->isn_lnum;
4429 n = tv_get_bool_chk(tv, &error);
4430 if (error)
4431 goto on_error;
4432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 clear_tv(tv);
4434 tv->v_type = VAR_BOOL;
4435 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4436 }
4437 break;
4438
4439 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004440 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004441 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004442 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4443 iptr->isn_type == ISN_2STRING_ANY,
4444 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004445 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 break;
4447
Bram Moolenaar08597872020-12-10 19:43:40 +01004448 case ISN_RANGE:
4449 {
4450 exarg_T ea;
4451 char *errormsg;
4452
Bram Moolenaarece0b872021-01-08 20:40:45 +01004453 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004454 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004455 ea.addr_type = ADDR_LINES;
4456 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004457 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004458 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004459 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004460
Bram Moolenaar35578162021-08-02 19:10:38 +02004461 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004462 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004463 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004464 tv = STACK_TV_BOT(-1);
4465 tv->v_type = VAR_NUMBER;
4466 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004467 if (ea.addr_count == 0)
4468 tv->vval.v_number = curwin->w_cursor.lnum;
4469 else
4470 tv->vval.v_number = ea.line2;
4471 }
4472 break;
4473
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004474 case ISN_PUT:
4475 {
4476 int regname = iptr->isn_arg.put.put_regname;
4477 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4478 char_u *expr = NULL;
4479 int dir = FORWARD;
4480
Bram Moolenaar08597872020-12-10 19:43:40 +01004481 if (lnum < -2)
4482 {
4483 // line number was put on the stack by ISN_RANGE
4484 tv = STACK_TV_BOT(-1);
4485 curwin->w_cursor.lnum = tv->vval.v_number;
4486 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4487 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004488 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004489 }
4490 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004491 // :put! above cursor
4492 dir = BACKWARD;
4493 else if (lnum >= 0)
4494 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004495
4496 if (regname == '=')
4497 {
4498 tv = STACK_TV_BOT(-1);
4499 if (tv->v_type == VAR_STRING)
4500 expr = tv->vval.v_string;
4501 else
4502 {
4503 expr = typval2string(tv, TRUE); // allocates value
4504 clear_tv(tv);
4505 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004506 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004507 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004508 check_cursor();
4509 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4510 vim_free(expr);
4511 }
4512 break;
4513
Bram Moolenaar02194d22020-10-24 23:08:38 +02004514 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004515 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4516 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4517 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4518 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004519 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4520 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004521 break;
4522
Bram Moolenaar02194d22020-10-24 23:08:38 +02004523 case ISN_CMDMOD_REV:
4524 // filter regprog is owned by the instruction, don't free it
4525 cmdmod.cmod_filter_regmatch.regprog = NULL;
4526 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004527 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4528 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004529 break;
4530
Bram Moolenaar792f7862020-11-23 08:31:18 +01004531 case ISN_UNPACK:
4532 {
4533 int count = iptr->isn_arg.unpack.unp_count;
4534 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4535 list_T *l;
4536 listitem_T *li;
4537 int i;
4538
4539 // Check there is a valid list to unpack.
4540 tv = STACK_TV_BOT(-1);
4541 if (tv->v_type != VAR_LIST)
4542 {
4543 SOURCING_LNUM = iptr->isn_lnum;
4544 emsg(_(e_for_argument_must_be_sequence_of_lists));
4545 goto on_error;
4546 }
4547 l = tv->vval.v_list;
4548 if (l == NULL
4549 || l->lv_len < (semicolon ? count - 1 : count))
4550 {
4551 SOURCING_LNUM = iptr->isn_lnum;
4552 emsg(_(e_list_value_does_not_have_enough_items));
4553 goto on_error;
4554 }
4555 else if (!semicolon && l->lv_len > count)
4556 {
4557 SOURCING_LNUM = iptr->isn_lnum;
4558 emsg(_(e_list_value_has_more_items_than_targets));
4559 goto on_error;
4560 }
4561
4562 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004563 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004564 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004565 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004566
4567 // Variable after semicolon gets a list with the remaining
4568 // items.
4569 if (semicolon)
4570 {
4571 list_T *rem_list =
4572 list_alloc_with_items(l->lv_len - count + 1);
4573
4574 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004575 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004576 tv = STACK_TV_BOT(-count);
4577 tv->vval.v_list = rem_list;
4578 ++rem_list->lv_refcount;
4579 tv->v_lock = 0;
4580 li = l->lv_first;
4581 for (i = 0; i < count - 1; ++i)
4582 li = li->li_next;
4583 for (i = 0; li != NULL; ++i)
4584 {
4585 list_set_item(rem_list, i, &li->li_tv);
4586 li = li->li_next;
4587 }
4588 --count;
4589 }
4590
4591 // Produce the values in reverse order, first item last.
4592 li = l->lv_first;
4593 for (i = 0; i < count; ++i)
4594 {
4595 tv = STACK_TV_BOT(-i - 1);
4596 copy_tv(&li->li_tv, tv);
4597 li = li->li_next;
4598 }
4599
4600 list_unref(l);
4601 }
4602 break;
4603
Bram Moolenaarb2049902021-01-24 12:53:53 +01004604 case ISN_PROF_START:
4605 case ISN_PROF_END:
4606 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004607#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004608 funccall_T cookie;
4609 ufunc_T *cur_ufunc =
4610 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004611 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004612
4613 cookie.func = cur_ufunc;
4614 if (iptr->isn_type == ISN_PROF_START)
4615 {
4616 func_line_start(&cookie, iptr->isn_lnum);
4617 // if we get here the instruction is executed
4618 func_line_exec(&cookie);
4619 }
4620 else
4621 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004622#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004623 }
4624 break;
4625
Bram Moolenaare99d4222021-06-13 14:01:26 +02004626 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004627 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004628 break;
4629
Bram Moolenaar389df252020-07-09 21:20:47 +02004630 case ISN_SHUFFLE:
4631 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004632 typval_T tmp_tv;
4633 int item = iptr->isn_arg.shuffle.shfl_item;
4634 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004635
4636 tmp_tv = *STACK_TV_BOT(-item);
4637 for ( ; up > 0 && item > 1; --up)
4638 {
4639 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4640 --item;
4641 }
4642 *STACK_TV_BOT(-item) = tmp_tv;
4643 }
4644 break;
4645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004647 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004649 ectx->ec_where.wt_index = 0;
4650 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 break;
4652 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004653 continue;
4654
Bram Moolenaard032f342020-07-18 18:13:02 +02004655func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004656 // Restore previous function. If the frame pointer is where we started
4657 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004658 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004659 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004660
Bram Moolenaar4c137212021-04-19 16:48:48 +02004661 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004662 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004663 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004664 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004665
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004666on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004667 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004668 // If "emsg_silent" is set then ignore the error, unless it was set
4669 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004670 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004671 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004672 {
4673 // If a sequence of instructions causes an error while ":silent!"
4674 // was used, restore the stack length and jump ahead to restoring
4675 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004676 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004677 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004678 while (ectx->ec_stack.ga_len
4679 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004680 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004681 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004682 clear_tv(STACK_TV_BOT(0));
4683 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004684 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4685 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004686 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004687 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004688 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004689on_fatal_error:
4690 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004691 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004692 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004693 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 }
4695
4696done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004697 ret = OK;
4698theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004699 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004700 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4701 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004702}
4703
4704/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004705 * Execute the instructions from a VAR_INSTR typeval and put the result in
4706 * "rettv".
4707 * Return OK or FAIL.
4708 */
4709 int
4710exe_typval_instr(typval_T *tv, typval_T *rettv)
4711{
4712 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4713 isn_T *save_instr = ectx->ec_instr;
4714 int save_iidx = ectx->ec_iidx;
4715 int res;
4716
4717 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4718 res = exec_instructions(ectx);
4719 if (res == OK)
4720 {
4721 *rettv = *STACK_TV_BOT(-1);
4722 --ectx->ec_stack.ga_len;
4723 }
4724
4725 ectx->ec_instr = save_instr;
4726 ectx->ec_iidx = save_iidx;
4727
4728 return res;
4729}
4730
4731/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004732 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4733 * "substitute_instr".
4734 */
4735 char_u *
4736exe_substitute_instr(void)
4737{
4738 ectx_T *ectx = substitute_instr->subs_ectx;
4739 isn_T *save_instr = ectx->ec_instr;
4740 int save_iidx = ectx->ec_iidx;
4741 char_u *res;
4742
4743 ectx->ec_instr = substitute_instr->subs_instr;
4744 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004745 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004746 typval_T *tv = STACK_TV_BOT(-1);
4747
Bram Moolenaar27523602021-06-05 21:36:19 +02004748 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004749 --ectx->ec_stack.ga_len;
4750 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004751 }
4752 else
4753 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004754 substitute_instr->subs_status = FAIL;
4755 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757
Bram Moolenaar4c137212021-04-19 16:48:48 +02004758 ectx->ec_instr = save_instr;
4759 ectx->ec_iidx = save_iidx;
4760
4761 return res;
4762}
4763
4764/*
4765 * Call a "def" function from old Vim script.
4766 * Return OK or FAIL.
4767 */
4768 int
4769call_def_function(
4770 ufunc_T *ufunc,
4771 int argc_arg, // nr of arguments
4772 typval_T *argv, // arguments
4773 partial_T *partial, // optional partial for context
4774 typval_T *rettv) // return value
4775{
4776 ectx_T ectx; // execution context
4777 int argc = argc_arg;
4778 typval_T *tv;
4779 int idx;
4780 int ret = FAIL;
4781 int defcount = ufunc->uf_args.ga_len - argc;
4782 sctx_T save_current_sctx = current_sctx;
4783 int did_emsg_before = did_emsg_cumul + did_emsg;
4784 int save_suppress_errthrow = suppress_errthrow;
4785 msglist_T **saved_msg_list = NULL;
4786 msglist_T *private_msg_list = NULL;
4787 int save_emsg_silent_def = emsg_silent_def;
4788 int save_did_emsg_def = did_emsg_def;
4789 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004790 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004791
4792// Get pointer to item in the stack.
4793#undef STACK_TV
4794#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4795
4796// Get pointer to item at the bottom of the stack, -1 is the bottom.
4797#undef STACK_TV_BOT
4798#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4799
4800// Get pointer to a local variable on the stack. Negative for arguments.
4801#undef STACK_TV_VAR
4802#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4803
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004804 // Update uf_has_breakpoint if needed.
4805 update_has_breakpoint(ufunc);
4806
Bram Moolenaar4c137212021-04-19 16:48:48 +02004807 if (ufunc->uf_def_status == UF_NOT_COMPILED
4808 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004809 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4810 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004811 == FAIL))
4812 {
4813 if (did_emsg_cumul + did_emsg == did_emsg_before)
4814 semsg(_(e_function_is_not_compiled_str),
4815 printable_func_name(ufunc));
4816 return FAIL;
4817 }
4818
4819 {
4820 // Check the function was really compiled.
4821 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4822 + ufunc->uf_dfunc_idx;
4823 if (INSTRUCTIONS(dfunc) == NULL)
4824 {
4825 iemsg("using call_def_function() on not compiled function");
4826 return FAIL;
4827 }
4828 }
4829
4830 // If depth of calling is getting too high, don't execute the function.
4831 orig_funcdepth = funcdepth_get();
4832 if (funcdepth_increment() == FAIL)
4833 return FAIL;
4834
4835 CLEAR_FIELD(ectx);
4836 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4837 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004838 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004839 {
4840 funcdepth_decrement();
4841 return FAIL;
4842 }
4843 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4844 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4845 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004846 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004847
4848 idx = argc - ufunc->uf_args.ga_len;
4849 if (idx > 0 && ufunc->uf_va_name == NULL)
4850 {
4851 if (idx == 1)
4852 emsg(_(e_one_argument_too_many));
4853 else
4854 semsg(_(e_nr_arguments_too_many), idx);
4855 goto failed_early;
4856 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004857 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4858 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004859 {
4860 if (idx == -1)
4861 emsg(_(e_one_argument_too_few));
4862 else
4863 semsg(_(e_nr_arguments_too_few), -idx);
4864 goto failed_early;
4865 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004866
4867 // Put arguments on the stack, but no more than what the function expects.
4868 // A lambda can be called with more arguments than it uses.
4869 for (idx = 0; idx < argc
4870 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4871 ++idx)
4872 {
4873 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4874 && argv[idx].v_type == VAR_SPECIAL
4875 && argv[idx].vval.v_number == VVAL_NONE)
4876 {
4877 // Use the default value.
4878 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4879 }
4880 else
4881 {
4882 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4883 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004884 ufunc->uf_arg_types[idx], &argv[idx],
4885 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004886 goto failed_early;
4887 copy_tv(&argv[idx], STACK_TV_BOT(0));
4888 }
4889 ++ectx.ec_stack.ga_len;
4890 }
4891
4892 // Turn varargs into a list. Empty list if no args.
4893 if (ufunc->uf_va_name != NULL)
4894 {
4895 int vararg_count = argc - ufunc->uf_args.ga_len;
4896
4897 if (vararg_count < 0)
4898 vararg_count = 0;
4899 else
4900 argc -= vararg_count;
4901 if (exe_newlist(vararg_count, &ectx) == FAIL)
4902 goto failed_early;
4903
4904 // Check the type of the list items.
4905 tv = STACK_TV_BOT(-1);
4906 if (ufunc->uf_va_type != NULL
4907 && ufunc->uf_va_type != &t_list_any
4908 && ufunc->uf_va_type->tt_member != &t_any
4909 && tv->vval.v_list != NULL)
4910 {
4911 type_T *expected = ufunc->uf_va_type->tt_member;
4912 listitem_T *li = tv->vval.v_list->lv_first;
4913
4914 for (idx = 0; idx < vararg_count; ++idx)
4915 {
4916 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004917 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004918 goto failed_early;
4919 li = li->li_next;
4920 }
4921 }
4922
4923 if (defcount > 0)
4924 // Move varargs list to below missing default arguments.
4925 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4926 --ectx.ec_stack.ga_len;
4927 }
4928
4929 // Make space for omitted arguments, will store default value below.
4930 // Any varargs list goes after them.
4931 if (defcount > 0)
4932 for (idx = 0; idx < defcount; ++idx)
4933 {
4934 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4935 ++ectx.ec_stack.ga_len;
4936 }
4937 if (ufunc->uf_va_name != NULL)
4938 ++ectx.ec_stack.ga_len;
4939
4940 // Frame pointer points to just after arguments.
4941 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4942 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4943
4944 {
4945 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4946 + ufunc->uf_dfunc_idx;
4947 ufunc_T *base_ufunc = dfunc->df_ufunc;
4948
4949 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4950 // by copy_func().
4951 if (partial != NULL || base_ufunc->uf_partial != NULL)
4952 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004953 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4954 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004955 goto failed_early;
4956 if (partial != NULL)
4957 {
4958 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4959 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004960 if (current_ectx->ec_outer_ref != NULL
4961 && current_ectx->ec_outer_ref->or_outer != NULL)
4962 ectx.ec_outer_ref->or_outer =
4963 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004964 }
4965 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004966 {
4967 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4968 ++partial->pt_refcount;
4969 ectx.ec_outer_ref->or_partial = partial;
4970 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004971 }
4972 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004973 {
4974 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4975 ++base_ufunc->uf_partial->pt_refcount;
4976 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4977 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004978 }
4979 }
4980
4981 // dummy frame entries
4982 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4983 {
4984 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4985 ++ectx.ec_stack.ga_len;
4986 }
4987
4988 {
4989 // Reserve space for local variables and any closure reference count.
4990 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4991 + ufunc->uf_dfunc_idx;
4992
4993 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4994 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4995 ectx.ec_stack.ga_len += dfunc->df_varcount;
4996 if (dfunc->df_has_closure)
4997 {
4998 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4999 STACK_TV_VAR(idx)->vval.v_number = 0;
5000 ++ectx.ec_stack.ga_len;
5001 }
5002
5003 ectx.ec_instr = INSTRUCTIONS(dfunc);
5004 }
5005
5006 // Following errors are in the function, not the caller.
5007 // Commands behave like vim9script.
5008 estack_push_ufunc(ufunc, 1);
5009 current_sctx = ufunc->uf_script_ctx;
5010 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5011
5012 // Use a specific location for storing error messages to be converted to an
5013 // exception.
5014 saved_msg_list = msg_list;
5015 msg_list = &private_msg_list;
5016
5017 // Do turn errors into exceptions.
5018 suppress_errthrow = FALSE;
5019
5020 // Do not delete the function while executing it.
5021 ++ufunc->uf_calls;
5022
5023 // When ":silent!" was used before calling then we still abort the
5024 // function. If ":silent!" is used in the function then we don't.
5025 emsg_silent_def = emsg_silent;
5026 did_emsg_def = 0;
5027
5028 ectx.ec_where.wt_index = 0;
5029 ectx.ec_where.wt_variable = FALSE;
5030
5031 // Execute the instructions until done.
5032 ret = exec_instructions(&ectx);
5033 if (ret == OK)
5034 {
5035 // function finished, get result from the stack.
5036 if (ufunc->uf_ret_type == &t_void)
5037 {
5038 rettv->v_type = VAR_VOID;
5039 }
5040 else
5041 {
5042 tv = STACK_TV_BOT(-1);
5043 *rettv = *tv;
5044 tv->v_type = VAR_UNKNOWN;
5045 }
5046 }
5047
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005048 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005049 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5050 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005051
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005052 // Deal with any remaining closures, they may be in use somewhere.
5053 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005054 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005055 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005056 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
5057 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005058
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005059 estack_pop();
5060 current_sctx = save_current_sctx;
5061
Bram Moolenaar2336c372021-12-06 15:06:54 +00005062 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5063 // Function was unreferenced while being used, free it now.
5064 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005065
Bram Moolenaar352134b2020-10-17 22:04:08 +02005066 if (*msg_list != NULL && saved_msg_list != NULL)
5067 {
5068 msglist_T **plist = saved_msg_list;
5069
5070 // Append entries from the current msg_list (uncaught exceptions) to
5071 // the saved msg_list.
5072 while (*plist != NULL)
5073 plist = &(*plist)->next;
5074
5075 *plist = *msg_list;
5076 }
5077 msg_list = saved_msg_list;
5078
Bram Moolenaar4c137212021-04-19 16:48:48 +02005079 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005080 {
5081 cmdmod.cmod_filter_regmatch.regprog = NULL;
5082 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005083 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005084 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005085 emsg_silent_def = save_emsg_silent_def;
5086 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005087
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005088failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005089 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5091 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005092 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005095 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005096 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005097 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005098 if (ectx.ec_outer_ref->or_outer_allocated)
5099 vim_free(ectx.ec_outer_ref->or_outer);
5100 partial_unref(ectx.ec_outer_ref->or_partial);
5101 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005102 }
5103
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005104 // Not sure if this is necessary.
5105 suppress_errthrow = save_suppress_errthrow;
5106
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005107 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5108 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005109 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005110 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005111 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 return ret;
5113}
5114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005116 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5117 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5118 * "pfx" is prefixed to every line.
5119 */
5120 static void
5121list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5122{
5123 int line_idx = 0;
5124 int prev_current = 0;
5125 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005126 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005127
5128 for (current = 0; current < instr_count; ++current)
5129 {
5130 isn_T *iptr = &instr[current];
5131 char *line;
5132
5133 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005134 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005135 while (line_idx < iptr->isn_lnum
5136 && line_idx < ufunc->uf_lines.ga_len)
5137 {
5138 if (current > prev_current)
5139 {
5140 msg_puts("\n\n");
5141 prev_current = current;
5142 }
5143 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5144 if (line != NULL)
5145 msg(line);
5146 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005147 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5148 {
5149 int first_def_arg = ufunc->uf_args.ga_len
5150 - ufunc->uf_def_args.ga_len;
5151
5152 if (def_arg_idx > 0)
5153 msg_puts("\n\n");
5154 msg_start();
5155 msg_puts(" ");
5156 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5157 first_def_arg + def_arg_idx]);
5158 msg_puts(" = ");
5159 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5160 msg_clr_eos();
5161 msg_end();
5162 }
5163 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005164
5165 switch (iptr->isn_type)
5166 {
5167 case ISN_EXEC:
5168 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5169 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005170 case ISN_EXEC_SPLIT:
5171 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5172 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005173 case ISN_EXECRANGE:
5174 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5175 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005176 case ISN_LEGACY_EVAL:
5177 smsg("%s%4d EVAL legacy %s", pfx, current,
5178 iptr->isn_arg.string);
5179 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005180 case ISN_REDIRSTART:
5181 smsg("%s%4d REDIR", pfx, current);
5182 break;
5183 case ISN_REDIREND:
5184 smsg("%s%4d REDIR END%s", pfx, current,
5185 iptr->isn_arg.number ? " append" : "");
5186 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005187 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005188#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005189 smsg("%s%4d CEXPR pre %s", pfx, current,
5190 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005191#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005192 break;
5193 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005194#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005195 {
5196 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5197
5198 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5199 cexpr_get_auname(cer->cer_cmdidx),
5200 cer->cer_forceit ? "!" : "",
5201 cer->cer_cmdline);
5202 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005203#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005204 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005205 case ISN_INSTR:
5206 {
5207 smsg("%s%4d INSTR", pfx, current);
5208 list_instructions(" ", iptr->isn_arg.instr,
5209 INT_MAX, NULL);
5210 msg(" -------------");
5211 }
5212 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005213 case ISN_SUBSTITUTE:
5214 {
5215 subs_T *subs = &iptr->isn_arg.subs;
5216
5217 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5218 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5219 msg(" -------------");
5220 }
5221 break;
5222 case ISN_EXECCONCAT:
5223 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5224 (varnumber_T)iptr->isn_arg.number);
5225 break;
5226 case ISN_ECHO:
5227 {
5228 echo_T *echo = &iptr->isn_arg.echo;
5229
5230 smsg("%s%4d %s %d", pfx, current,
5231 echo->echo_with_white ? "ECHO" : "ECHON",
5232 echo->echo_count);
5233 }
5234 break;
5235 case ISN_EXECUTE:
5236 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005237 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005238 break;
5239 case ISN_ECHOMSG:
5240 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005241 (varnumber_T)(iptr->isn_arg.number));
5242 break;
5243 case ISN_ECHOCONSOLE:
5244 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5245 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005246 break;
5247 case ISN_ECHOERR:
5248 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005249 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005250 break;
5251 case ISN_LOAD:
5252 {
5253 if (iptr->isn_arg.number < 0)
5254 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5255 (varnumber_T)(iptr->isn_arg.number
5256 + STACK_FRAME_SIZE));
5257 else
5258 smsg("%s%4d LOAD $%lld", pfx, current,
5259 (varnumber_T)(iptr->isn_arg.number));
5260 }
5261 break;
5262 case ISN_LOADOUTER:
5263 {
5264 if (iptr->isn_arg.number < 0)
5265 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5266 iptr->isn_arg.outer.outer_depth,
5267 iptr->isn_arg.outer.outer_idx
5268 + STACK_FRAME_SIZE);
5269 else
5270 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5271 iptr->isn_arg.outer.outer_depth,
5272 iptr->isn_arg.outer.outer_idx);
5273 }
5274 break;
5275 case ISN_LOADV:
5276 smsg("%s%4d LOADV v:%s", pfx, current,
5277 get_vim_var_name(iptr->isn_arg.number));
5278 break;
5279 case ISN_LOADSCRIPT:
5280 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005281 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5282 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5283 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005284
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005285 sv = get_script_svar(sref, -1);
5286 if (sv == NULL)
5287 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5288 pfx, current, si->sn_name);
5289 else
5290 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005291 sv->sv_name,
5292 sref->sref_idx,
5293 si->sn_name);
5294 }
5295 break;
5296 case ISN_LOADS:
5297 {
5298 scriptitem_T *si = SCRIPT_ITEM(
5299 iptr->isn_arg.loadstore.ls_sid);
5300
5301 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5302 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5303 }
5304 break;
5305 case ISN_LOADAUTO:
5306 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5307 break;
5308 case ISN_LOADG:
5309 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5310 break;
5311 case ISN_LOADB:
5312 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5313 break;
5314 case ISN_LOADW:
5315 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5316 break;
5317 case ISN_LOADT:
5318 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5319 break;
5320 case ISN_LOADGDICT:
5321 smsg("%s%4d LOAD g:", pfx, current);
5322 break;
5323 case ISN_LOADBDICT:
5324 smsg("%s%4d LOAD b:", pfx, current);
5325 break;
5326 case ISN_LOADWDICT:
5327 smsg("%s%4d LOAD w:", pfx, current);
5328 break;
5329 case ISN_LOADTDICT:
5330 smsg("%s%4d LOAD t:", pfx, current);
5331 break;
5332 case ISN_LOADOPT:
5333 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5334 break;
5335 case ISN_LOADENV:
5336 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5337 break;
5338 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005339 smsg("%s%4d LOADREG @%c", pfx, current,
5340 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005341 break;
5342
5343 case ISN_STORE:
5344 if (iptr->isn_arg.number < 0)
5345 smsg("%s%4d STORE arg[%lld]", pfx, current,
5346 iptr->isn_arg.number + STACK_FRAME_SIZE);
5347 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005348 smsg("%s%4d STORE $%lld", pfx, current,
5349 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005350 break;
5351 case ISN_STOREOUTER:
5352 {
5353 if (iptr->isn_arg.number < 0)
5354 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5355 iptr->isn_arg.outer.outer_depth,
5356 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5357 else
5358 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5359 iptr->isn_arg.outer.outer_depth,
5360 iptr->isn_arg.outer.outer_idx);
5361 }
5362 break;
5363 case ISN_STOREV:
5364 smsg("%s%4d STOREV v:%s", pfx, current,
5365 get_vim_var_name(iptr->isn_arg.number));
5366 break;
5367 case ISN_STOREAUTO:
5368 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5369 break;
5370 case ISN_STOREG:
5371 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5372 break;
5373 case ISN_STOREB:
5374 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5375 break;
5376 case ISN_STOREW:
5377 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5378 break;
5379 case ISN_STORET:
5380 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5381 break;
5382 case ISN_STORES:
5383 {
5384 scriptitem_T *si = SCRIPT_ITEM(
5385 iptr->isn_arg.loadstore.ls_sid);
5386
5387 smsg("%s%4d STORES %s in %s", pfx, current,
5388 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5389 }
5390 break;
5391 case ISN_STORESCRIPT:
5392 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005393 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5394 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5395 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005396
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005397 sv = get_script_svar(sref, -1);
5398 if (sv == NULL)
5399 smsg("%s%4d STORESCRIPT [deleted] in %s",
5400 pfx, current, si->sn_name);
5401 else
5402 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005403 sv->sv_name,
5404 sref->sref_idx,
5405 si->sn_name);
5406 }
5407 break;
5408 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005409 case ISN_STOREFUNCOPT:
5410 smsg("%s%4d %s &%s", pfx, current,
5411 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005412 iptr->isn_arg.storeopt.so_name);
5413 break;
5414 case ISN_STOREENV:
5415 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5416 break;
5417 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005418 smsg("%s%4d STOREREG @%c", pfx, current,
5419 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005420 break;
5421 case ISN_STORENR:
5422 smsg("%s%4d STORE %lld in $%d", pfx, current,
5423 iptr->isn_arg.storenr.stnr_val,
5424 iptr->isn_arg.storenr.stnr_idx);
5425 break;
5426
5427 case ISN_STOREINDEX:
5428 smsg("%s%4d STOREINDEX %s", pfx, current,
5429 vartype_name(iptr->isn_arg.vartype));
5430 break;
5431
5432 case ISN_STORERANGE:
5433 smsg("%s%4d STORERANGE", pfx, current);
5434 break;
5435
5436 // constants
5437 case ISN_PUSHNR:
5438 smsg("%s%4d PUSHNR %lld", pfx, current,
5439 (varnumber_T)(iptr->isn_arg.number));
5440 break;
5441 case ISN_PUSHBOOL:
5442 case ISN_PUSHSPEC:
5443 smsg("%s%4d PUSH %s", pfx, current,
5444 get_var_special_name(iptr->isn_arg.number));
5445 break;
5446 case ISN_PUSHF:
5447#ifdef FEAT_FLOAT
5448 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5449#endif
5450 break;
5451 case ISN_PUSHS:
5452 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5453 break;
5454 case ISN_PUSHBLOB:
5455 {
5456 char_u *r;
5457 char_u numbuf[NUMBUFLEN];
5458 char_u *tofree;
5459
5460 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5461 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5462 vim_free(tofree);
5463 }
5464 break;
5465 case ISN_PUSHFUNC:
5466 {
5467 char *name = (char *)iptr->isn_arg.string;
5468
5469 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5470 name == NULL ? "[none]" : name);
5471 }
5472 break;
5473 case ISN_PUSHCHANNEL:
5474#ifdef FEAT_JOB_CHANNEL
5475 {
5476 channel_T *channel = iptr->isn_arg.channel;
5477
5478 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5479 channel == NULL ? 0 : channel->ch_id);
5480 }
5481#endif
5482 break;
5483 case ISN_PUSHJOB:
5484#ifdef FEAT_JOB_CHANNEL
5485 {
5486 typval_T tv;
5487 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005488 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005489
5490 tv.v_type = VAR_JOB;
5491 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005492 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005493 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5494 }
5495#endif
5496 break;
5497 case ISN_PUSHEXC:
5498 smsg("%s%4d PUSH v:exception", pfx, current);
5499 break;
5500 case ISN_UNLET:
5501 smsg("%s%4d UNLET%s %s", pfx, current,
5502 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5503 iptr->isn_arg.unlet.ul_name);
5504 break;
5505 case ISN_UNLETENV:
5506 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5507 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5508 iptr->isn_arg.unlet.ul_name);
5509 break;
5510 case ISN_UNLETINDEX:
5511 smsg("%s%4d UNLETINDEX", pfx, current);
5512 break;
5513 case ISN_UNLETRANGE:
5514 smsg("%s%4d UNLETRANGE", pfx, current);
5515 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005516 case ISN_LOCKUNLOCK:
5517 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5518 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005519 case ISN_LOCKCONST:
5520 smsg("%s%4d LOCKCONST", pfx, current);
5521 break;
5522 case ISN_NEWLIST:
5523 smsg("%s%4d NEWLIST size %lld", pfx, current,
5524 (varnumber_T)(iptr->isn_arg.number));
5525 break;
5526 case ISN_NEWDICT:
5527 smsg("%s%4d NEWDICT size %lld", pfx, current,
5528 (varnumber_T)(iptr->isn_arg.number));
5529 break;
5530
5531 // function call
5532 case ISN_BCALL:
5533 {
5534 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5535
5536 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5537 internal_func_name(cbfunc->cbf_idx),
5538 cbfunc->cbf_argcount);
5539 }
5540 break;
5541 case ISN_DCALL:
5542 {
5543 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5544 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5545 + cdfunc->cdf_idx;
5546
5547 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005548 printable_func_name(df->df_ufunc),
5549 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005550 }
5551 break;
5552 case ISN_UCALL:
5553 {
5554 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5555
5556 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5557 cufunc->cuf_name, cufunc->cuf_argcount);
5558 }
5559 break;
5560 case ISN_PCALL:
5561 {
5562 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5563
5564 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5565 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5566 }
5567 break;
5568 case ISN_PCALL_END:
5569 smsg("%s%4d PCALL end", pfx, current);
5570 break;
5571 case ISN_RETURN:
5572 smsg("%s%4d RETURN", pfx, current);
5573 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005574 case ISN_RETURN_VOID:
5575 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005576 break;
5577 case ISN_FUNCREF:
5578 {
5579 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005580 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005581
Bram Moolenaar38453522021-11-28 22:00:12 +00005582 if (funcref->fr_func_name == NULL)
5583 {
5584 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5585 + funcref->fr_dfunc_idx;
5586 name = df->df_ufunc->uf_name;
5587 }
5588 else
5589 name = funcref->fr_func_name;
5590 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005591 }
5592 break;
5593
5594 case ISN_NEWFUNC:
5595 {
5596 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5597
5598 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5599 newfunc->nf_lambda, newfunc->nf_global);
5600 }
5601 break;
5602
5603 case ISN_DEF:
5604 {
5605 char_u *name = iptr->isn_arg.string;
5606
5607 smsg("%s%4d DEF %s", pfx, current,
5608 name == NULL ? (char_u *)"" : name);
5609 }
5610 break;
5611
5612 case ISN_JUMP:
5613 {
5614 char *when = "?";
5615
5616 switch (iptr->isn_arg.jump.jump_when)
5617 {
5618 case JUMP_ALWAYS:
5619 when = "JUMP";
5620 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005621 case JUMP_NEVER:
5622 iemsg("JUMP_NEVER should not be used");
5623 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005624 case JUMP_AND_KEEP_IF_TRUE:
5625 when = "JUMP_AND_KEEP_IF_TRUE";
5626 break;
5627 case JUMP_IF_FALSE:
5628 when = "JUMP_IF_FALSE";
5629 break;
5630 case JUMP_AND_KEEP_IF_FALSE:
5631 when = "JUMP_AND_KEEP_IF_FALSE";
5632 break;
5633 case JUMP_IF_COND_FALSE:
5634 when = "JUMP_IF_COND_FALSE";
5635 break;
5636 case JUMP_IF_COND_TRUE:
5637 when = "JUMP_IF_COND_TRUE";
5638 break;
5639 }
5640 smsg("%s%4d %s -> %d", pfx, current, when,
5641 iptr->isn_arg.jump.jump_where);
5642 }
5643 break;
5644
5645 case ISN_JUMP_IF_ARG_SET:
5646 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5647 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5648 iptr->isn_arg.jump.jump_where);
5649 break;
5650
5651 case ISN_FOR:
5652 {
5653 forloop_T *forloop = &iptr->isn_arg.forloop;
5654
5655 smsg("%s%4d FOR $%d -> %d", pfx, current,
5656 forloop->for_idx, forloop->for_end);
5657 }
5658 break;
5659
5660 case ISN_TRY:
5661 {
5662 try_T *try = &iptr->isn_arg.try;
5663
5664 if (try->try_ref->try_finally == 0)
5665 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5666 pfx, current,
5667 try->try_ref->try_catch,
5668 try->try_ref->try_endtry);
5669 else
5670 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5671 pfx, current,
5672 try->try_ref->try_catch,
5673 try->try_ref->try_finally,
5674 try->try_ref->try_endtry);
5675 }
5676 break;
5677 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005678 smsg("%s%4d CATCH", pfx, current);
5679 break;
5680 case ISN_TRYCONT:
5681 {
5682 trycont_T *trycont = &iptr->isn_arg.trycont;
5683
5684 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5685 trycont->tct_levels,
5686 trycont->tct_levels == 1 ? "" : "s",
5687 trycont->tct_where);
5688 }
5689 break;
5690 case ISN_FINALLY:
5691 smsg("%s%4d FINALLY", pfx, current);
5692 break;
5693 case ISN_ENDTRY:
5694 smsg("%s%4d ENDTRY", pfx, current);
5695 break;
5696 case ISN_THROW:
5697 smsg("%s%4d THROW", pfx, current);
5698 break;
5699
5700 // expression operations on number
5701 case ISN_OPNR:
5702 case ISN_OPFLOAT:
5703 case ISN_OPANY:
5704 {
5705 char *what;
5706 char *ins;
5707
5708 switch (iptr->isn_arg.op.op_type)
5709 {
5710 case EXPR_MULT: what = "*"; break;
5711 case EXPR_DIV: what = "/"; break;
5712 case EXPR_REM: what = "%"; break;
5713 case EXPR_SUB: what = "-"; break;
5714 case EXPR_ADD: what = "+"; break;
5715 default: what = "???"; break;
5716 }
5717 switch (iptr->isn_type)
5718 {
5719 case ISN_OPNR: ins = "OPNR"; break;
5720 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5721 case ISN_OPANY: ins = "OPANY"; break;
5722 default: ins = "???"; break;
5723 }
5724 smsg("%s%4d %s %s", pfx, current, ins, what);
5725 }
5726 break;
5727
5728 case ISN_COMPAREBOOL:
5729 case ISN_COMPARESPECIAL:
5730 case ISN_COMPARENR:
5731 case ISN_COMPAREFLOAT:
5732 case ISN_COMPARESTRING:
5733 case ISN_COMPAREBLOB:
5734 case ISN_COMPARELIST:
5735 case ISN_COMPAREDICT:
5736 case ISN_COMPAREFUNC:
5737 case ISN_COMPAREANY:
5738 {
5739 char *p;
5740 char buf[10];
5741 char *type;
5742
5743 switch (iptr->isn_arg.op.op_type)
5744 {
5745 case EXPR_EQUAL: p = "=="; break;
5746 case EXPR_NEQUAL: p = "!="; break;
5747 case EXPR_GREATER: p = ">"; break;
5748 case EXPR_GEQUAL: p = ">="; break;
5749 case EXPR_SMALLER: p = "<"; break;
5750 case EXPR_SEQUAL: p = "<="; break;
5751 case EXPR_MATCH: p = "=~"; break;
5752 case EXPR_IS: p = "is"; break;
5753 case EXPR_ISNOT: p = "isnot"; break;
5754 case EXPR_NOMATCH: p = "!~"; break;
5755 default: p = "???"; break;
5756 }
5757 STRCPY(buf, p);
5758 if (iptr->isn_arg.op.op_ic == TRUE)
5759 strcat(buf, "?");
5760 switch(iptr->isn_type)
5761 {
5762 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5763 case ISN_COMPARESPECIAL:
5764 type = "COMPARESPECIAL"; break;
5765 case ISN_COMPARENR: type = "COMPARENR"; break;
5766 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5767 case ISN_COMPARESTRING:
5768 type = "COMPARESTRING"; break;
5769 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5770 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5771 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5772 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5773 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5774 default: type = "???"; break;
5775 }
5776
5777 smsg("%s%4d %s %s", pfx, current, type, buf);
5778 }
5779 break;
5780
5781 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5782 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5783
5784 // expression operations
5785 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5786 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5787 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5788 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5789 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5790 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5791 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5792 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5793 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5794 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5795 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5796 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005797 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005798 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5799 iptr->isn_arg.getitem.gi_index,
5800 iptr->isn_arg.getitem.gi_with_op ?
5801 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005802 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5803 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5804 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005805 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5806 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5807
Bram Moolenaar4c137212021-04-19 16:48:48 +02005808 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5809
5810 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5811 case ISN_CHECKTYPE:
5812 {
5813 checktype_T *ct = &iptr->isn_arg.type;
5814 char *tofree;
5815
5816 if (ct->ct_arg_idx == 0)
5817 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5818 type_name(ct->ct_type, &tofree),
5819 (int)ct->ct_off);
5820 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005821 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5822 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005823 type_name(ct->ct_type, &tofree),
5824 (int)ct->ct_off,
5825 (int)ct->ct_arg_idx);
5826 vim_free(tofree);
5827 break;
5828 }
5829 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5830 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5831 iptr->isn_arg.checklen.cl_min_len);
5832 break;
5833 case ISN_SETTYPE:
5834 {
5835 char *tofree;
5836
5837 smsg("%s%4d SETTYPE %s", pfx, current,
5838 type_name(iptr->isn_arg.type.ct_type, &tofree));
5839 vim_free(tofree);
5840 break;
5841 }
5842 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005843 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5844 smsg("%s%4d INVERT %d (!val)", pfx, current,
5845 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005846 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005847 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5848 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005849 break;
5850 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005851 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005852 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005853 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5854 pfx, current,
5855 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005856 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005857 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5858 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005859 break;
5860 case ISN_PUT:
5861 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5862 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005863 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005864 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5865 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005866 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005867 else
5868 smsg("%s%4d PUT %c %ld", pfx, current,
5869 iptr->isn_arg.put.put_regname,
5870 (long)iptr->isn_arg.put.put_lnum);
5871 break;
5872
Bram Moolenaar4c137212021-04-19 16:48:48 +02005873 case ISN_CMDMOD:
5874 {
5875 char_u *buf;
5876 size_t len = produce_cmdmods(
5877 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5878
5879 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005880 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005881 {
5882 (void)produce_cmdmods(
5883 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5884 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5885 vim_free(buf);
5886 }
5887 break;
5888 }
5889 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5890
5891 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005892 smsg("%s%4d PROFILE START line %d", pfx, current,
5893 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005894 break;
5895
5896 case ISN_PROF_END:
5897 smsg("%s%4d PROFILE END", pfx, current);
5898 break;
5899
Bram Moolenaare99d4222021-06-13 14:01:26 +02005900 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005901 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5902 iptr->isn_arg.debug.dbg_break_lnum + 1,
5903 iptr->isn_lnum,
5904 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005905 break;
5906
Bram Moolenaar4c137212021-04-19 16:48:48 +02005907 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5908 iptr->isn_arg.unpack.unp_count,
5909 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5910 break;
5911 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5912 iptr->isn_arg.shuffle.shfl_item,
5913 iptr->isn_arg.shuffle.shfl_up);
5914 break;
5915 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5916
5917 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5918 return;
5919 }
5920
5921 out_flush(); // output one line at a time
5922 ui_breakcheck();
5923 if (got_int)
5924 break;
5925 }
5926}
5927
5928/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005929 * Handle command line completion for the :disassemble command.
5930 */
5931 void
5932set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5933{
5934 char_u *p;
5935
5936 // Default: expand user functions, "debug" and "profile"
5937 xp->xp_context = EXPAND_DISASSEMBLE;
5938 xp->xp_pattern = arg;
5939
5940 // first argument already typed: only user function names
5941 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5942 {
5943 xp->xp_context = EXPAND_USER_FUNC;
5944 xp->xp_pattern = skipwhite(p);
5945 }
5946}
5947
5948/*
5949 * Function given to ExpandGeneric() to obtain the list of :disassemble
5950 * arguments.
5951 */
5952 char_u *
5953get_disassemble_argument(expand_T *xp, int idx)
5954{
5955 if (idx == 0)
5956 return (char_u *)"debug";
5957 if (idx == 1)
5958 return (char_u *)"profile";
5959 return get_user_func_name(xp, idx - 2);
5960}
5961
5962/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005963 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005964 * We don't really need this at runtime, but we do have tests that require it,
5965 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966 */
5967 void
5968ex_disassemble(exarg_T *eap)
5969{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005970 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005971 char_u *fname;
5972 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973 dfunc_T *dfunc;
5974 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005975 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005976 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005977 compiletype_T compile_type = CT_NONE;
5978
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005979 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005980 {
5981 compile_type = CT_PROFILE;
5982 arg = skipwhite(arg + 7);
5983 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005984 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005985 {
5986 compile_type = CT_DEBUG;
5987 arg = skipwhite(arg + 5);
5988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005990 if (STRNCMP(arg, "<lambda>", 8) == 0)
5991 {
5992 arg += 8;
5993 (void)getdigits(&arg);
5994 fname = vim_strnsave(eap->arg, arg - eap->arg);
5995 }
5996 else
5997 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005998 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005999 if (fname == NULL)
6000 {
6001 semsg(_(e_invarg2), eap->arg);
6002 return;
6003 }
6004
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006005 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006006 if (ufunc == NULL)
6007 {
6008 char_u *p = untrans_function_name(fname);
6009
6010 if (p != NULL)
6011 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006012 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006013 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006014 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 if (ufunc == NULL)
6016 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006017 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018 return;
6019 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006020 if (func_needs_compiling(ufunc, compile_type)
6021 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006022 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006023 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006025 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 return;
6027 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006028 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029
6030 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006031 switch (compile_type)
6032 {
6033 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006034#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006035 instr = dfunc->df_instr_prof;
6036 instr_count = dfunc->df_instr_prof_count;
6037 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006038#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006039 // FALLTHROUGH
6040 case CT_NONE:
6041 instr = dfunc->df_instr;
6042 instr_count = dfunc->df_instr_count;
6043 break;
6044 case CT_DEBUG:
6045 instr = dfunc->df_instr_debug;
6046 instr_count = dfunc->df_instr_debug_count;
6047 break;
6048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006049
Bram Moolenaar4c137212021-04-19 16:48:48 +02006050 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051}
6052
6053/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006054 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 * list, etc. Mostly like what JavaScript does, except that empty list and
6056 * empty dictionary are FALSE.
6057 */
6058 int
6059tv2bool(typval_T *tv)
6060{
6061 switch (tv->v_type)
6062 {
6063 case VAR_NUMBER:
6064 return tv->vval.v_number != 0;
6065 case VAR_FLOAT:
6066#ifdef FEAT_FLOAT
6067 return tv->vval.v_float != 0.0;
6068#else
6069 break;
6070#endif
6071 case VAR_PARTIAL:
6072 return tv->vval.v_partial != NULL;
6073 case VAR_FUNC:
6074 case VAR_STRING:
6075 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6076 case VAR_LIST:
6077 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6078 case VAR_DICT:
6079 return tv->vval.v_dict != NULL
6080 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6081 case VAR_BOOL:
6082 case VAR_SPECIAL:
6083 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6084 case VAR_JOB:
6085#ifdef FEAT_JOB_CHANNEL
6086 return tv->vval.v_job != NULL;
6087#else
6088 break;
6089#endif
6090 case VAR_CHANNEL:
6091#ifdef FEAT_JOB_CHANNEL
6092 return tv->vval.v_channel != NULL;
6093#else
6094 break;
6095#endif
6096 case VAR_BLOB:
6097 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6098 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006099 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006101 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 break;
6103 }
6104 return FALSE;
6105}
6106
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006107 void
6108emsg_using_string_as(typval_T *tv, int as_number)
6109{
6110 semsg(_(as_number ? e_using_string_as_number_str
6111 : e_using_string_as_bool_str),
6112 tv->vval.v_string == NULL
6113 ? (char_u *)"" : tv->vval.v_string);
6114}
6115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116/*
6117 * If "tv" is a string give an error and return FAIL.
6118 */
6119 int
6120check_not_string(typval_T *tv)
6121{
6122 if (tv->v_type == VAR_STRING)
6123 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006124 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125 clear_tv(tv);
6126 return FAIL;
6127 }
6128 return OK;
6129}
6130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131
6132#endif // FEAT_EVAL