blob: 14091b0483a75464e6bf4df11f7b6721e292b281 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200140 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
155 static void
156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200168static garray_T dict_stack = GA_EMPTY;
169
170/*
171 * Put a value on the dict stack. This consumes "tv".
172 */
173 static int
174dict_stack_save(typval_T *tv)
175{
176 if (dict_stack.ga_growsize == 0)
177 ga_init2(&dict_stack, (int)sizeof(typval_T), 10);
178 if (ga_grow(&dict_stack, 1) == FAIL)
179 return FAIL;
180 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
181 ++dict_stack.ga_len;
182 return OK;
183}
184
185/*
186 * Get the typval at top of the dict stack.
187 */
188 static typval_T *
189dict_stack_get_tv(void)
190{
191 if (dict_stack.ga_len == 0)
192 return NULL;
193 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
194}
195
196/*
197 * Get the dict at top of the dict stack.
198 */
199 static dict_T *
200dict_stack_get_dict(void)
201{
202 typval_T *tv;
203
204 if (dict_stack.ga_len == 0)
205 return NULL;
206 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
207 if (tv->v_type == VAR_DICT)
208 return tv->vval.v_dict;
209 return NULL;
210}
211
212/*
213 * Drop an item from the dict stack.
214 */
215 static void
216dict_stack_drop(void)
217{
218 if (dict_stack.ga_len == 0)
219 {
220 iemsg("Dict stack underflow");
221 return;
222 }
223 --dict_stack.ga_len;
224 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
225}
226
227/*
228 * Drop items from the dict stack until the length is equal to "len".
229 */
230 static void
231dict_stack_clear(int len)
232{
233 while (dict_stack.ga_len > len)
234 dict_stack_drop();
235}
236
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100239 * This adds a stack frame and sets the instruction pointer to the start of the
240 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200241 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 *
243 * Stack has:
244 * - current arguments (already there)
245 * - omitted optional argument (default values) added here
246 * - stack frame:
247 * - pointer to calling function
248 * - Index of next instruction in calling function
249 * - previous frame pointer
250 * - reserved space for local variables
251 */
252 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100253call_dfunc(
254 int cdf_idx,
255 partial_T *pt,
256 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100257 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100259 int argcount = argcount_arg;
260 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
261 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200262 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100263 int arg_to_add;
264 int vararg_count = 0;
265 int varcount;
266 int idx;
267 estack_T *entry;
268 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200269 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270
271 if (dfunc->df_deleted)
272 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100273 // don't use ufunc->uf_name, it may have been freed
274 emsg_funcname(e_func_deleted,
275 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 return FAIL;
277 }
278
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100279#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100280 if (do_profiling == PROF_YES)
281 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200282 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100283 {
284 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
285 + profile_info_ga.ga_len;
286 ++profile_info_ga.ga_len;
287 CLEAR_POINTER(info);
288 profile_may_start_func(info, ufunc,
289 (((dfunc_T *)def_functions.ga_data)
290 + ectx->ec_dfunc_idx)->df_ufunc);
291 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100292 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100293#endif
294
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200295 // Update uf_has_breakpoint if needed.
296 update_has_breakpoint(ufunc);
297
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200298 // When debugging and using "cont" switches to the not-debugged
299 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200300 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
301 {
302 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
303
304 // compile_def_function() may cause def_functions.ga_data to change
305 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
306 }
307 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200308 {
309 if (did_emsg_cumul + did_emsg == did_emsg_before)
310 semsg(_(e_function_is_not_compiled_str),
311 printable_func_name(ufunc));
312 return FAIL;
313 }
314
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200315 if (ufunc->uf_va_name != NULL)
316 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200317 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200318 // Stack at time of call with 2 varargs:
319 // normal_arg
320 // optional_arg
321 // vararg_1
322 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200323 // After creating the list:
324 // normal_arg
325 // optional_arg
326 // vararg-list
327 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200328 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200329 // After creating the list
330 // normal_arg
331 // (space for optional_arg)
332 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200333 vararg_count = argcount - ufunc->uf_args.ga_len;
334 if (vararg_count < 0)
335 vararg_count = 0;
336 else
337 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200338 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200339 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200340
341 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200342 }
343
Bram Moolenaarfe270812020-04-11 22:31:27 +0200344 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200345 if (arg_to_add < 0)
346 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200347 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200348 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200349 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200350 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200351 return FAIL;
352 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200353
354 // Reserve space for:
355 // - missing arguments
356 // - stack frame
357 // - local variables
358 // - if needed: a counter for number of closures created in
359 // ectx->ec_funcrefs.
360 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200361 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 return FAIL;
363
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100364 // If depth of calling is getting too high, don't execute the function.
365 if (funcdepth_increment() == FAIL)
366 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200367 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100368
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100369 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200370 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100371 {
372 floc = ALLOC_ONE(funclocal_T);
373 if (floc == NULL)
374 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200375 *floc = ectx->ec_funclocal;
376 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100377 }
378
Bram Moolenaarfe270812020-04-11 22:31:27 +0200379 // Move the vararg-list to below the missing optional arguments.
380 if (vararg_count > 0 && arg_to_add > 0)
381 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100382
383 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200384 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200385 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200386 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387
388 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100389 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
390 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200391 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200392 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
393 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100395 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200396 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397
398 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200399 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200401 if (dfunc->df_has_closure)
402 {
403 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
404
405 tv->v_type = VAR_NUMBER;
406 tv->vval.v_number = 0;
407 }
408 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100410 if (pt != NULL || ufunc->uf_partial != NULL
411 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100412 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200413 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100414
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200415 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100416 return FAIL;
417 if (pt != NULL)
418 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200419 ref->or_outer = &pt->pt_outer;
420 ++pt->pt_refcount;
421 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100422 }
423 else if (ufunc->uf_partial != NULL)
424 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200425 ref->or_outer = &ufunc->uf_partial->pt_outer;
426 ++ufunc->uf_partial->pt_refcount;
427 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100428 }
429 else
430 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200431 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200432 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200433 {
434 vim_free(ref);
435 return FAIL;
436 }
437 ref->or_outer_allocated = TRUE;
438 ref->or_outer->out_stack = &ectx->ec_stack;
439 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
440 if (ectx->ec_outer_ref != NULL)
441 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100442 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200443 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100444 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100445 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200446 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100447
Bram Moolenaarc970e422021-03-17 15:03:04 +0100448 ++ufunc->uf_calls;
449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 // Set execution state to the start of the called function.
451 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100452 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100453 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200454 if (entry != NULL)
455 {
456 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200457 // Save the current context so it can be restored on return.
458 entry->es_save_sctx = current_sctx;
459 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200460 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100461
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200462 // Start execution at the first instruction.
463 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464
465 return OK;
466}
467
468// Get pointer to item in the stack.
469#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
470
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000471// Double linked list of funcstack_T in use.
472static funcstack_T *first_funcstack = NULL;
473
474 static void
475add_funcstack_to_list(funcstack_T *funcstack)
476{
477 // Link in list of funcstacks.
478 if (first_funcstack != NULL)
479 first_funcstack->fs_prev = funcstack;
480 funcstack->fs_next = first_funcstack;
481 funcstack->fs_prev = NULL;
482 first_funcstack = funcstack;
483}
484
485 static void
486remove_funcstack_from_list(funcstack_T *funcstack)
487{
488 if (funcstack->fs_prev == NULL)
489 first_funcstack = funcstack->fs_next;
490 else
491 funcstack->fs_prev->fs_next = funcstack->fs_next;
492 if (funcstack->fs_next != NULL)
493 funcstack->fs_next->fs_prev = funcstack->fs_prev;
494}
495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200497 * Used when returning from a function: Check if any closure is still
498 * referenced. If so then move the arguments and variables to a separate piece
499 * of stack to be used when the closure is called.
500 * When "free_arguments" is TRUE the arguments are to be freed.
501 * Returns FAIL when out of memory.
502 */
503 static int
504handle_closure_in_use(ectx_T *ectx, int free_arguments)
505{
506 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
507 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200508 int argcount;
509 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200510 int idx;
511 typval_T *tv;
512 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200513 garray_T *gap = &ectx->ec_funcrefs;
514 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200516 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200517 return OK; // function was freed
518 if (dfunc->df_has_closure == 0)
519 return OK; // no closures
520 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
521 closure_count = tv->vval.v_number;
522 if (closure_count == 0)
523 return OK; // no funcrefs created
524
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200525 argcount = ufunc_argcount(dfunc->df_ufunc);
526 top = ectx->ec_frame_idx - argcount;
527
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200528 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200529 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200531 partial_T *pt;
532 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200533
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200534 if (off < 0)
535 continue; // count is off or already done
536 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200537 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200538 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200540 int i;
541
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200542 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200543 // unreferenced on return.
544 for (i = 0; i < dfunc->df_varcount; ++i)
545 {
546 typval_T *stv = STACK_TV(ectx->ec_frame_idx
547 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200548 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200549 --refcount;
550 }
551 if (refcount > 1)
552 {
553 closure_in_use = TRUE;
554 break;
555 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200556 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200557 }
558
559 if (closure_in_use)
560 {
561 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
562 typval_T *stack;
563
564 // A closure is using the arguments and/or local variables.
565 // Move them to the called function.
566 if (funcstack == NULL)
567 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000568
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200569 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
570 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200571 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
572 funcstack->fs_ga.ga_data = stack;
573 if (stack == NULL)
574 {
575 vim_free(funcstack);
576 return FAIL;
577 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000578 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200579
580 // Move or copy the arguments.
581 for (idx = 0; idx < argcount; ++idx)
582 {
583 tv = STACK_TV(top + idx);
584 if (free_arguments)
585 {
586 *(stack + idx) = *tv;
587 tv->v_type = VAR_UNKNOWN;
588 }
589 else
590 copy_tv(tv, stack + idx);
591 }
592 // Move the local variables.
593 for (idx = 0; idx < dfunc->df_varcount; ++idx)
594 {
595 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200596
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200597 // A partial created for a local function, that is also used as a
598 // local variable, has a reference count for the variable, thus
599 // will never go down to zero. When all these refcounts are one
600 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000601 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200602 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
603 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200604 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200605
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200606 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200607 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
608 gap->ga_len - closure_count + i])
609 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200610 }
611
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200612 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200613 tv->v_type = VAR_UNKNOWN;
614 }
615
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200616 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200617 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200618 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
619 - closure_count + idx];
620 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200621 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200622 ++funcstack->fs_refcount;
623 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100624 pt->pt_outer.out_stack = &funcstack->fs_ga;
625 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 }
627 }
628 }
629
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200630 for (idx = 0; idx < closure_count; ++idx)
631 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
632 - closure_count + idx]);
633 gap->ga_len -= closure_count;
634 if (gap->ga_len == 0)
635 ga_clear(gap);
636
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 return OK;
638}
639
640/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200641 * Called when a partial is freed or its reference count goes down to one. The
642 * funcstack may be the only reference to the partials in the local variables.
643 * Go over all of them, the funcref and can be freed if all partials
644 * referencing the funcstack have a reference count of one.
645 */
646 void
647funcstack_check_refcount(funcstack_T *funcstack)
648{
649 int i;
650 garray_T *gap = &funcstack->fs_ga;
651 int done = 0;
652
653 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
654 return;
655 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
656 {
657 typval_T *tv = ((typval_T *)gap->ga_data) + i;
658
659 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
660 && tv->vval.v_partial->pt_funcstack == funcstack
661 && tv->vval.v_partial->pt_refcount == 1)
662 ++done;
663 }
664 if (done == funcstack->fs_min_refcount)
665 {
666 typval_T *stack = gap->ga_data;
667
668 // All partials referencing the funcstack have a reference count of
669 // one, thus the funcstack is no longer of use.
670 for (i = 0; i < gap->ga_len; ++i)
671 clear_tv(stack + i);
672 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000673 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200674 vim_free(funcstack);
675 }
676}
677
678/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000679 * For garbage collecting: set references in all variables referenced by
680 * all funcstacks.
681 */
682 int
683set_ref_in_funcstacks(int copyID)
684{
685 funcstack_T *funcstack;
686
687 for (funcstack = first_funcstack; funcstack != NULL;
688 funcstack = funcstack->fs_next)
689 {
690 typval_T *stack = funcstack->fs_ga.ga_data;
691 int i;
692
693 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
694 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
695 return TRUE; // abort
696 }
697 return FALSE;
698}
699
700/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 * Return from the current function.
702 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200703 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200704func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100707 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
709 + ectx->ec_dfunc_idx;
710 int argcount = ufunc_argcount(dfunc->df_ufunc);
711 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200712 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100713 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
714 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200715 funclocal_T *floc;
716#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100717 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
718 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719
Bram Moolenaar12d26532021-02-19 19:13:21 +0100720 if (do_profiling == PROF_YES)
721 {
722 ufunc_T *caller = prev_dfunc->df_ufunc;
723
724 if (dfunc->df_ufunc->uf_profiling
725 || (caller != NULL && caller->uf_profiling))
726 {
727 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
728 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
729 --profile_info_ga.ga_len;
730 }
731 }
732#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000733
734 // No check for uf_refcount being zero, cannot think of a way that would
735 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100736 --dfunc->df_ufunc->uf_calls;
737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200739 entry = estack_pop();
740 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200741 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 if (handle_closure_in_use(ectx, TRUE) == FAIL)
744 return FAIL;
745
746 // Clear the arguments.
747 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
748 clear_tv(STACK_TV(idx));
749
750 // Clear local variables and temp values, but not the return value.
751 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100752 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100754
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100755 // The return value should be on top of the stack. However, when aborting
756 // it may not be there and ec_frame_idx is the top of the stack.
757 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100758 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100759 ret_idx = 0;
760
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200761 if (ectx->ec_outer_ref != NULL)
762 {
763 if (ectx->ec_outer_ref->or_outer_allocated)
764 vim_free(ectx->ec_outer_ref->or_outer);
765 partial_unref(ectx->ec_outer_ref->or_partial);
766 vim_free(ectx->ec_outer_ref);
767 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100768
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100769 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100770 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100771 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
772 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200773 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
774 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200775 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100776 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100777 floc = (void *)STACK_TV(ectx->ec_frame_idx
778 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200779 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100780 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
781 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200782
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100783 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200784 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100785 else
786 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200787 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100788 vim_free(floc);
789 }
790
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100791 if (ret_idx > 0)
792 {
793 // Reset the stack to the position before the call, with a spot for the
794 // return value, moved there from above the frame.
795 ectx->ec_stack.ga_len = top + 1;
796 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
797 }
798 else
799 // Reset the stack to the position before the call.
800 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200801
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100802 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200803 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200804 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805}
806
807#undef STACK_TV
808
809/*
810 * Prepare arguments and rettv for calling a builtin or user function.
811 */
812 static int
813call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
814{
815 int idx;
816 typval_T *tv;
817
818 // Move arguments from bottom of the stack to argvars[] and add terminator.
819 for (idx = 0; idx < argcount; ++idx)
820 argvars[idx] = *STACK_TV_BOT(idx - argcount);
821 argvars[argcount].v_type = VAR_UNKNOWN;
822
823 // Result replaces the arguments on the stack.
824 if (argcount > 0)
825 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200826 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 return FAIL;
828 else
829 ++ectx->ec_stack.ga_len;
830
831 // Default return value is zero.
832 tv = STACK_TV_BOT(-1);
833 tv->v_type = VAR_NUMBER;
834 tv->vval.v_number = 0;
835
836 return OK;
837}
838
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200839// Ugly global to avoid passing the execution context around through many
840// layers.
841static ectx_T *current_ectx = NULL;
842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843/*
844 * Call a builtin function by index.
845 */
846 static int
847call_bfunc(int func_idx, int argcount, ectx_T *ectx)
848{
849 typval_T argvars[MAX_FUNC_ARGS];
850 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100851 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200852 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200853 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854
855 if (call_prepare(argcount, argvars, ectx) == FAIL)
856 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200857 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200859 // Call the builtin function. Set "current_ectx" so that when it
860 // recursively invokes call_def_function() a closure context can be set.
861 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200863 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200864 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865
866 // Clear the arguments.
867 for (idx = 0; idx < argcount; ++idx)
868 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200869
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100870 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200871 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 return OK;
873}
874
875/*
876 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100877 * If the function is compiled this will add a stack frame and set the
878 * instruction pointer at the start of the function.
879 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200880 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100881 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 */
883 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100884call_ufunc(
885 ufunc_T *ufunc,
886 partial_T *pt,
887 int argcount,
888 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200889 isn_T *iptr,
890 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891{
892 typval_T argvars[MAX_FUNC_ARGS];
893 funcexe_T funcexe;
894 int error;
895 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100896 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200897 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898
Bram Moolenaare99d4222021-06-13 14:01:26 +0200899 if (func_needs_compiling(ufunc, compile_type)
900 && compile_def_function(ufunc, FALSE, compile_type, NULL)
901 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200902 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200903 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100904 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100905 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100906 if (error != FCERR_UNKNOWN)
907 {
908 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000909 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100910 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000911 semsg(_(e_not_enough_arguments_for_function_str),
912 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100913 return FAIL;
914 }
915
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100916 // The function has been compiled, can call it quickly. For a function
917 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100918 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100919 if (iptr != NULL)
920 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100921 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100922 iptr->isn_type = ISN_DCALL;
923 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
924 iptr->isn_arg.dfunc.cdf_argcount = argcount;
925 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200926 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928
929 if (call_prepare(argcount, argvars, ectx) == FAIL)
930 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200931 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000932 funcexe.fe_evaluate = TRUE;
933 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935 // Call the user function. Result goes in last position on the stack.
936 // TODO: add selfdict if there is one
937 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000938 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939
940 // Clear the arguments.
941 for (idx = 0; idx < argcount; ++idx)
942 clear_tv(&argvars[idx]);
943
944 if (error != FCERR_NONE)
945 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000946 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100947 return FAIL;
948 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100949 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200950 // Error other than from calling the function itself.
951 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952 return OK;
953}
954
955/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100956 * If command modifiers were applied restore them.
957 */
958 static void
959may_restore_cmdmod(funclocal_T *funclocal)
960{
961 if (funclocal->floc_restore_cmdmod)
962 {
963 cmdmod.cmod_filter_regmatch.regprog = NULL;
964 undo_cmdmod(&cmdmod);
965 cmdmod = funclocal->floc_save_cmdmod;
966 funclocal->floc_restore_cmdmod = FALSE;
967 }
968}
969
970/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200971 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
972 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200973 */
974 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200975vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200976{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200977 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200978}
979
980/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 * Execute a function by "name".
982 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100983 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 * Returns FAIL if not found without an error message.
985 */
986 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100987call_by_name(
988 char_u *name,
989 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100990 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200991 isn_T *iptr,
992 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993{
994 ufunc_T *ufunc;
995
996 if (builtin_function(name, -1))
997 {
998 int func_idx = find_internal_func(name);
999
1000 if (func_idx < 0)
1001 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001002 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 return FAIL;
1004 return call_bfunc(func_idx, argcount, ectx);
1005 }
1006
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001007 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +02001008
1009 if (ufunc == NULL)
1010 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001011 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001012
1013 if (script_autoload(name, TRUE))
1014 // loaded a package, search for the function again
1015 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001016
1017 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001018 return FAIL; // bail out if loading the script caused an error
1019 }
1020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001022 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001023 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001024 {
1025 int i;
1026 typval_T *argv = STACK_TV_BOT(0) - argcount;
1027
1028 // The function can change at runtime, check that the argument
1029 // types are correct.
1030 for (i = 0; i < argcount; ++i)
1031 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001032 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001033
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001034 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001035 type = ufunc->uf_arg_types[i];
1036 else if (ufunc->uf_va_type != NULL)
1037 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001038 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001039 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001040 return FAIL;
1041 }
1042 }
1043
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001044 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001045 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046
1047 return FAIL;
1048}
1049
1050 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001051call_partial(
1052 typval_T *tv,
1053 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001054 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001056 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001057 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001059 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001060 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061
1062 if (tv->v_type == VAR_PARTIAL)
1063 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001064 partial_T *pt = tv->vval.v_partial;
1065 int i;
1066
1067 if (pt->pt_argc > 0)
1068 {
1069 // Make space for arguments from the partial, shift the "argcount"
1070 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001071 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001072 return FAIL;
1073 for (i = 1; i <= argcount; ++i)
1074 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1075 ectx->ec_stack.ga_len += pt->pt_argc;
1076 argcount += pt->pt_argc;
1077
1078 // copy the arguments from the partial onto the stack
1079 for (i = 0; i < pt->pt_argc; ++i)
1080 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1081 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001082 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083
1084 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001085 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 name = pt->pt_name;
1088 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001089 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001091 if (name != NULL)
1092 {
1093 char_u fname_buf[FLEN_FIXED + 1];
1094 char_u *tofree = NULL;
1095 int error = FCERR_NONE;
1096 char_u *fname;
1097
1098 // May need to translate <SNR>123_ to K_SNR.
1099 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1100 if (error != FCERR_NONE)
1101 res = FAIL;
1102 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001103 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001104 vim_free(tofree);
1105 }
1106
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001107 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 {
1109 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001110 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001111 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 return FAIL;
1113 }
1114 return OK;
1115}
1116
1117/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001118 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1119 * TRUE.
1120 */
1121 static int
1122error_if_locked(int lock, char *error)
1123{
1124 if (lock & (VAR_LOCKED | VAR_FIXED))
1125 {
1126 emsg(_(error));
1127 return TRUE;
1128 }
1129 return FALSE;
1130}
1131
1132/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001133 * Give an error if "tv" is not a number and return FAIL.
1134 */
1135 static int
1136check_for_number(typval_T *tv)
1137{
1138 if (tv->v_type != VAR_NUMBER)
1139 {
1140 semsg(_(e_expected_str_but_got_str),
1141 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1142 return FAIL;
1143 }
1144 return OK;
1145}
1146
1147/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001148 * Store "tv" in variable "name".
1149 * This is for s: and g: variables.
1150 */
1151 static void
1152store_var(char_u *name, typval_T *tv)
1153{
1154 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001155 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001156
Bram Moolenaard877a572021-04-01 19:42:48 +02001157 if (tv->v_lock)
1158 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001159 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001160 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001161 restore_funccal();
1162}
1163
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001164/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001165 * Convert "tv" to a string.
1166 * Return FAIL if not allowed.
1167 */
1168 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001169do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001170{
1171 if (tv->v_type != VAR_STRING)
1172 {
1173 char_u *str;
1174
1175 if (is_2string_any)
1176 {
1177 switch (tv->v_type)
1178 {
1179 case VAR_SPECIAL:
1180 case VAR_BOOL:
1181 case VAR_NUMBER:
1182 case VAR_FLOAT:
1183 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001184
1185 case VAR_LIST:
1186 if (tolerant)
1187 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001188 char_u *s, *e, *p;
1189 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001190
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001191 ga_init2(&ga, sizeof(char_u *), 1);
1192
1193 // Convert to NL separated items, then
1194 // escape the items and replace the NL with
1195 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001196 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001197 if (str == NULL)
1198 return FAIL;
1199 s = str;
1200 while ((e = vim_strchr(s, '\n')) != NULL)
1201 {
1202 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001203 p = vim_strsave_fnameescape(s,
1204 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001205 if (p != NULL)
1206 {
1207 ga_concat(&ga, p);
1208 ga_concat(&ga, (char_u *)" ");
1209 vim_free(p);
1210 }
1211 s = e + 1;
1212 }
1213 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001214 clear_tv(tv);
1215 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001216 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001217 return OK;
1218 }
1219 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001220 default: to_string_error(tv->v_type);
1221 return FAIL;
1222 }
1223 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001224 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001225 clear_tv(tv);
1226 tv->v_type = VAR_STRING;
1227 tv->vval.v_string = str;
1228 }
1229 return OK;
1230}
1231
1232/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001233 * When the value of "sv" is a null list of dict, allocate it.
1234 */
1235 static void
1236allocate_if_null(typval_T *tv)
1237{
1238 switch (tv->v_type)
1239 {
1240 case VAR_LIST:
1241 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001242 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001243 break;
1244 case VAR_DICT:
1245 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001246 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001247 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001248 case VAR_BLOB:
1249 if (tv->vval.v_blob == NULL)
1250 (void)rettv_blob_alloc(tv);
1251 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001252 default:
1253 break;
1254 }
1255}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001256
Bram Moolenaare7525c52021-01-09 13:20:37 +01001257/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001258 * Return the character "str[index]" where "index" is the character index,
1259 * including composing characters.
1260 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001261 */
1262 char_u *
1263char_from_string(char_u *str, varnumber_T index)
1264{
1265 size_t nbyte = 0;
1266 varnumber_T nchar = index;
1267 size_t slen;
1268
1269 if (str == NULL)
1270 return NULL;
1271 slen = STRLEN(str);
1272
Bram Moolenaarff871402021-03-26 13:34:05 +01001273 // Do the same as for a list: a negative index counts from the end.
1274 // Optimization to check the first byte to be below 0x80 (and no composing
1275 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001276 if (index < 0)
1277 {
1278 int clen = 0;
1279
1280 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001281 {
1282 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1283 ++nbyte;
1284 else if (enc_utf8)
1285 nbyte += utfc_ptr2len(str + nbyte);
1286 else
1287 nbyte += mb_ptr2len(str + nbyte);
1288 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001289 nchar = clen + index;
1290 if (nchar < 0)
1291 // unlike list: index out of range results in empty string
1292 return NULL;
1293 }
1294
1295 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001296 {
1297 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1298 ++nbyte;
1299 else if (enc_utf8)
1300 nbyte += utfc_ptr2len(str + nbyte);
1301 else
1302 nbyte += mb_ptr2len(str + nbyte);
1303 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001304 if (nbyte >= slen)
1305 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001306 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001307}
1308
1309/*
1310 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001311 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001312 * If going over the end return "str_len".
1313 * If "idx" is negative count from the end, -1 is the last character.
1314 * When going over the start return -1.
1315 */
1316 static long
1317char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1318{
1319 varnumber_T nchar = idx;
1320 size_t nbyte = 0;
1321
1322 if (nchar >= 0)
1323 {
1324 while (nchar > 0 && nbyte < str_len)
1325 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001326 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001327 --nchar;
1328 }
1329 }
1330 else
1331 {
1332 nbyte = str_len;
1333 while (nchar < 0 && nbyte > 0)
1334 {
1335 --nbyte;
1336 nbyte -= mb_head_off(str, str + nbyte);
1337 ++nchar;
1338 }
1339 if (nchar < 0)
1340 return -1;
1341 }
1342 return (long)nbyte;
1343}
1344
1345/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001346 * Return the slice "str[first : last]" using character indexes. Composing
1347 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001348 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001349 * Return NULL when the result is empty.
1350 */
1351 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001352string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001353{
1354 long start_byte, end_byte;
1355 size_t slen;
1356
1357 if (str == NULL)
1358 return NULL;
1359 slen = STRLEN(str);
1360 start_byte = char_idx2byte(str, slen, first);
1361 if (start_byte < 0)
1362 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001363 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001364 end_byte = (long)slen;
1365 else
1366 {
1367 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001368 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001369 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001370 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001371 }
1372
1373 if (start_byte >= (long)slen || end_byte <= start_byte)
1374 return NULL;
1375 return vim_strnsave(str + start_byte, end_byte - start_byte);
1376}
1377
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001378/*
1379 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1380 * When "dfunc_idx" is negative don't give an error.
1381 * Returns NULL for an error.
1382 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001383 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001384get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001385{
1386 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001387 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1388 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001389 svar_T *sv;
1390
1391 if (sref->sref_seq != si->sn_script_seq)
1392 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001393 // The script was reloaded after the function was compiled, the
1394 // script_idx may not be valid.
1395 if (dfunc != NULL)
1396 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1397 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001398 return NULL;
1399 }
1400 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001401 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001402 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001403 if (dfunc != NULL)
1404 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001405 return NULL;
1406 }
1407 return sv;
1408}
1409
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001410/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001411 * Function passed to do_cmdline() for splitting a script joined by NL
1412 * characters.
1413 */
1414 static char_u *
1415get_split_sourceline(
1416 int c UNUSED,
1417 void *cookie,
1418 int indent UNUSED,
1419 getline_opt_T options UNUSED)
1420{
1421 source_cookie_T *sp = (source_cookie_T *)cookie;
1422 char_u *p;
1423 char_u *line;
1424
1425 if (*sp->nextline == NUL)
1426 return NULL;
1427 p = vim_strchr(sp->nextline, '\n');
1428 if (p == NULL)
1429 {
1430 line = vim_strsave(sp->nextline);
1431 sp->nextline += STRLEN(sp->nextline);
1432 }
1433 else
1434 {
1435 line = vim_strnsave(sp->nextline, p - sp->nextline);
1436 sp->nextline = p + 1;
1437 }
1438 return line;
1439}
1440
1441/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 * Execute a function by "name".
1443 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001444 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 */
1446 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001447call_eval_func(
1448 char_u *name,
1449 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001450 ectx_T *ectx,
1451 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452{
Bram Moolenaared677f52020-08-12 16:38:10 +02001453 int called_emsg_before = called_emsg;
1454 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001456 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001457 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001459 dictitem_T *v;
1460
1461 v = find_var(name, NULL, FALSE);
1462 if (v == NULL)
1463 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001464 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001465 return FAIL;
1466 }
1467 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1468 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001469 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001470 return FAIL;
1471 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001472 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001474 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475}
1476
1477/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001478 * When a function reference is used, fill a partial with the information
1479 * needed, especially when it is used as a closure.
1480 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001481 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001482fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1483{
1484 pt->pt_func = ufunc;
1485 pt->pt_refcount = 1;
1486
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001487 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001488 {
1489 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1490 + ectx->ec_dfunc_idx;
1491
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001492 // The closure may need to find arguments and local variables in the
1493 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001494 pt->pt_outer.out_stack = &ectx->ec_stack;
1495 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001496 if (ectx->ec_outer_ref != NULL)
1497 {
1498 // The current context already has a context, link to that one.
1499 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1500 if (ectx->ec_outer_ref->or_partial != NULL)
1501 {
1502 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1503 ++pt->pt_outer.out_up_partial->pt_refcount;
1504 }
1505 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001506
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001507 // If this function returns and the closure is still being used, we
1508 // need to make a copy of the context (arguments and local variables).
1509 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001510 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001511 {
1512 vim_free(pt);
1513 return FAIL;
1514 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001515 // Extra variable keeps the count of closures created in the current
1516 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001517 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1518 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1519
1520 ((partial_T **)ectx->ec_funcrefs.ga_data)
1521 [ectx->ec_funcrefs.ga_len] = pt;
1522 ++pt->pt_refcount;
1523 ++ectx->ec_funcrefs.ga_len;
1524 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001525 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001526 return OK;
1527}
1528
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001529/*
1530 * Execute iptr->isn_arg.string as an Ex command.
1531 */
1532 static int
1533exec_command(isn_T *iptr)
1534{
1535 source_cookie_T cookie;
1536
1537 SOURCING_LNUM = iptr->isn_lnum;
1538 // Pass getsourceline to get an error for a missing ":end"
1539 // command.
1540 CLEAR_FIELD(cookie);
1541 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1542 if (do_cmdline(iptr->isn_arg.string,
1543 getsourceline, &cookie,
1544 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1545 || did_emsg)
1546 return FAIL;
1547 return OK;
1548}
1549
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001550// used for v_instr of typval of VAR_INSTR
1551struct instr_S {
1552 ectx_T *instr_ectx;
1553 isn_T *instr_instr;
1554};
1555
Bram Moolenaar4c137212021-04-19 16:48:48 +02001556// used for substitute_instr
1557typedef struct subs_expr_S {
1558 ectx_T *subs_ectx;
1559 isn_T *subs_instr;
1560 int subs_status;
1561} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562
1563// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001564#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565
1566// Get pointer to item at the bottom of the stack, -1 is the bottom.
1567#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001568#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 +01001569
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001570// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001571#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 +01001572
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001573// Set when calling do_debug().
1574static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001575static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001576
1577/*
1578 * When debugging lookup "name" and return the typeval.
1579 * When not found return NULL.
1580 */
1581 typval_T *
1582lookup_debug_var(char_u *name)
1583{
1584 int idx;
1585 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001586 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001587 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001588 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001589
1590 if (ectx == NULL)
1591 return NULL;
1592 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1593
1594 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001595 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001596 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001597 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001598 return STACK_TV_VAR(idx);
1599 }
1600
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001601 // Go through argument names.
1602 ufunc = dfunc->df_ufunc;
1603 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1604 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1605 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1606 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1607 - varargs_off + idx);
1608 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1609 return STACK_TV(ectx->ec_frame_idx - 1);
1610
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001611 return NULL;
1612}
1613
Bram Moolenaar26a44842021-09-02 18:49:06 +02001614/*
1615 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1616 * breakpoint was set in that function or when there is any expression.
1617 */
1618 int
1619may_break_in_function(ufunc_T *ufunc)
1620{
1621 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1622}
1623
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001624 static void
1625handle_debug(isn_T *iptr, ectx_T *ectx)
1626{
1627 char_u *line;
1628 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1629 + ectx->ec_dfunc_idx)->df_ufunc;
1630 isn_T *ni;
1631 int end_lnum = iptr->isn_lnum;
1632 garray_T ga;
1633 int lnum;
1634
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001635 if (ex_nesting_level > debug_break_level)
1636 {
1637 linenr_T breakpoint;
1638
Bram Moolenaar26a44842021-09-02 18:49:06 +02001639 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001640 return;
1641
1642 // check for the next breakpoint if needed
1643 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001644 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001645 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1646 return;
1647 }
1648
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001649 SOURCING_LNUM = iptr->isn_lnum;
1650 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001651 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001652
1653 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1654 if (ni->isn_type == ISN_DEBUG
1655 || ni->isn_type == ISN_RETURN
1656 || ni->isn_type == ISN_RETURN_VOID)
1657 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001658 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001659 break;
1660 }
1661
1662 if (end_lnum > iptr->isn_lnum)
1663 {
1664 ga_init2(&ga, sizeof(char_u *), 10);
1665 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001666 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001667 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001668
Bram Moolenaar303215d2021-07-07 20:10:43 +02001669 if (p == NULL)
1670 continue; // left over from continuation line
1671 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001672 if (*p == '#')
1673 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001674 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001675 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1676 if (STRNCMP(p, "def ", 4) == 0)
1677 break;
1678 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001679 line = ga_concat_strings(&ga, " ");
1680 vim_free(ga.ga_data);
1681 }
1682 else
1683 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001684
Dominique Pelle6e969552021-06-17 13:53:41 +02001685 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001686 debug_context = NULL;
1687
1688 if (end_lnum > iptr->isn_lnum)
1689 vim_free(line);
1690}
1691
Bram Moolenaar4c137212021-04-19 16:48:48 +02001692/*
1693 * Execute instructions in execution context "ectx".
1694 * Return OK or FAIL;
1695 */
1696 static int
1697exec_instructions(ectx_T *ectx)
1698{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001699 int ret = FAIL;
1700 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001701 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001702
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001703 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001704 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001705
Bram Moolenaarff652882021-05-16 15:24:49 +02001706 // Only catch exceptions in this instruction list.
1707 ectx->ec_trylevel_at_start = trylevel;
1708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 for (;;)
1710 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001711 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001713 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001714
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001715 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001716 {
1717 line_breakcheck();
1718 breakcheck_count = 0;
1719 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001720 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001721 {
1722 // Turn CTRL-C into an exception.
1723 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001724 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001725 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001726 did_throw = TRUE;
1727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001729 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001730 {
1731 // Turn an error message into an exception.
1732 did_emsg = FALSE;
1733 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001734 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001735 did_throw = TRUE;
1736 *msg_list = NULL;
1737 }
1738
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001739 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001741 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001742 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001743 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744
1745 // An exception jumps to the first catch, finally, or returns from
1746 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001747 while (index > 0)
1748 {
1749 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001750 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001751 break;
1752 // In the catch and finally block of this try we have to go up
1753 // one level.
1754 --index;
1755 trycmd = NULL;
1756 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001757 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001759 if (trycmd->tcd_in_catch)
1760 {
1761 // exception inside ":catch", jump to ":finally" once
1762 ectx->ec_iidx = trycmd->tcd_finally_idx;
1763 trycmd->tcd_finally_idx = 0;
1764 }
1765 else
1766 // jump to first ":catch"
1767 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001768 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001769 did_throw = FALSE; // don't come back here until :endtry
1770 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 }
1772 else
1773 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001774 // Not inside try or need to return from current functions.
1775 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001776 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001777 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001778 tv = STACK_TV_BOT(0);
1779 tv->v_type = VAR_NUMBER;
1780 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001781 ++ectx->ec_stack.ga_len;
1782 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001784 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001785 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001786 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001787 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 goto done;
1789 }
1790
Bram Moolenaar4c137212021-04-19 16:48:48 +02001791 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001792 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 }
1794 continue;
1795 }
1796
Bram Moolenaar4c137212021-04-19 16:48:48 +02001797 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 switch (iptr->isn_type)
1799 {
1800 // execute Ex command line
1801 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001802 if (exec_command(iptr) == FAIL)
1803 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 break;
1805
Bram Moolenaar20677332021-06-06 17:02:53 +02001806 // execute Ex command line split at NL characters.
1807 case ISN_EXEC_SPLIT:
1808 {
1809 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001810 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001811
1812 SOURCING_LNUM = iptr->isn_lnum;
1813 CLEAR_FIELD(cookie);
1814 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1815 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001816 line = get_split_sourceline(0, &cookie, 0, 0);
1817 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001818 get_split_sourceline, &cookie,
1819 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1820 == FAIL
1821 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001822 {
1823 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001824 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001825 }
1826 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001827 }
1828 break;
1829
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001830 // execute Ex command line that is only a range
1831 case ISN_EXECRANGE:
1832 {
1833 exarg_T ea;
1834 char *error = NULL;
1835
1836 CLEAR_FIELD(ea);
1837 ea.cmdidx = CMD_SIZE;
1838 ea.addr_type = ADDR_LINES;
1839 ea.cmd = iptr->isn_arg.string;
1840 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00001841 if (ea.cmd == NULL)
1842 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001843 if (error == NULL)
1844 error = ex_range_without_command(&ea);
1845 if (error != NULL)
1846 {
1847 SOURCING_LNUM = iptr->isn_lnum;
1848 emsg(error);
1849 goto on_error;
1850 }
1851 }
1852 break;
1853
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001854 // Evaluate an expression with legacy syntax, push it onto the
1855 // stack.
1856 case ISN_LEGACY_EVAL:
1857 {
1858 char_u *arg = iptr->isn_arg.string;
1859 int res;
1860 int save_flags = cmdmod.cmod_flags;
1861
Bram Moolenaar35578162021-08-02 19:10:38 +02001862 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001863 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001864 tv = STACK_TV_BOT(0);
1865 init_tv(tv);
1866 cmdmod.cmod_flags |= CMOD_LEGACY;
1867 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1868 cmdmod.cmod_flags = save_flags;
1869 if (res == FAIL)
1870 goto on_error;
1871 ++ectx->ec_stack.ga_len;
1872 }
1873 break;
1874
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001875 // push typeval VAR_INSTR with instructions to be executed
1876 case ISN_INSTR:
1877 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001878 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001879 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001880 tv = STACK_TV_BOT(0);
1881 tv->vval.v_instr = ALLOC_ONE(instr_T);
1882 if (tv->vval.v_instr == NULL)
1883 goto on_error;
1884 ++ectx->ec_stack.ga_len;
1885
1886 tv->v_type = VAR_INSTR;
1887 tv->vval.v_instr->instr_ectx = ectx;
1888 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1889 }
1890 break;
1891
Bram Moolenaar4c137212021-04-19 16:48:48 +02001892 // execute :substitute with an expression
1893 case ISN_SUBSTITUTE:
1894 {
1895 subs_T *subs = &iptr->isn_arg.subs;
1896 source_cookie_T cookie;
1897 struct subs_expr_S *save_instr = substitute_instr;
1898 struct subs_expr_S subs_instr;
1899 int res;
1900
1901 subs_instr.subs_ectx = ectx;
1902 subs_instr.subs_instr = subs->subs_instr;
1903 subs_instr.subs_status = OK;
1904 substitute_instr = &subs_instr;
1905
1906 SOURCING_LNUM = iptr->isn_lnum;
1907 // This is very much like ISN_EXEC
1908 CLEAR_FIELD(cookie);
1909 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1910 res = do_cmdline(subs->subs_cmd,
1911 getsourceline, &cookie,
1912 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1913 substitute_instr = save_instr;
1914
1915 if (res == FAIL || did_emsg
1916 || subs_instr.subs_status == FAIL)
1917 goto on_error;
1918 }
1919 break;
1920
1921 case ISN_FINISH:
1922 goto done;
1923
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001924 case ISN_REDIRSTART:
1925 // create a dummy entry for var_redir_str()
1926 if (alloc_redir_lval() == FAIL)
1927 goto on_error;
1928
1929 // The output is stored in growarray "redir_ga" until
1930 // redirection ends.
1931 init_redir_ga();
1932 redir_vname = 1;
1933 break;
1934
1935 case ISN_REDIREND:
1936 {
1937 char_u *res = get_clear_redir_ga();
1938
1939 // End redirection, put redirected text on the stack.
1940 clear_redir_lval();
1941 redir_vname = 0;
1942
Bram Moolenaar35578162021-08-02 19:10:38 +02001943 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001944 {
1945 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001946 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001947 }
1948 tv = STACK_TV_BOT(0);
1949 tv->v_type = VAR_STRING;
1950 tv->vval.v_string = res;
1951 ++ectx->ec_stack.ga_len;
1952 }
1953 break;
1954
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001955 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001956#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001957 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1958 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001959#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001960 break;
1961
1962 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001963#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001964 {
1965 exarg_T ea;
1966 int res;
1967
1968 CLEAR_FIELD(ea);
1969 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1970 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1971 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1972 --ectx->ec_stack.ga_len;
1973 tv = STACK_TV_BOT(0);
1974 res = cexpr_core(&ea, tv);
1975 clear_tv(tv);
1976 if (res == FAIL)
1977 goto on_error;
1978 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001979#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001980 break;
1981
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001982 // execute Ex command from pieces on the stack
1983 case ISN_EXECCONCAT:
1984 {
1985 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001986 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001987 int pass;
1988 int i;
1989 char_u *cmd = NULL;
1990 char_u *str;
1991
1992 for (pass = 1; pass <= 2; ++pass)
1993 {
1994 for (i = 0; i < count; ++i)
1995 {
1996 tv = STACK_TV_BOT(i - count);
1997 str = tv->vval.v_string;
1998 if (str != NULL && *str != NUL)
1999 {
2000 if (pass == 2)
2001 STRCPY(cmd + len, str);
2002 len += STRLEN(str);
2003 }
2004 if (pass == 2)
2005 clear_tv(tv);
2006 }
2007 if (pass == 1)
2008 {
2009 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002010 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002011 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002012 len = 0;
2013 }
2014 }
2015
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002016 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002017 do_cmdline_cmd(cmd);
2018 vim_free(cmd);
2019 }
2020 break;
2021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 // execute :echo {string} ...
2023 case ISN_ECHO:
2024 {
2025 int count = iptr->isn_arg.echo.echo_count;
2026 int atstart = TRUE;
2027 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002028 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029
2030 for (idx = 0; idx < count; ++idx)
2031 {
2032 tv = STACK_TV_BOT(idx - count);
2033 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2034 &atstart, &needclr);
2035 clear_tv(tv);
2036 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002037 if (needclr)
2038 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002039 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 }
2041 break;
2042
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002043 // :execute {string} ...
2044 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002045 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002046 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002047 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002048 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002049 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002050 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002051 {
2052 int count = iptr->isn_arg.number;
2053 garray_T ga;
2054 char_u buf[NUMBUFLEN];
2055 char_u *p;
2056 int len;
2057 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002058 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002059
2060 ga_init2(&ga, 1, 80);
2061 for (idx = 0; idx < count; ++idx)
2062 {
2063 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002064 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002065 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002066 if (tv->v_type == VAR_CHANNEL
2067 || tv->v_type == VAR_JOB)
2068 {
2069 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002070 semsg(_(e_using_invalid_value_as_string_str),
2071 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002072 break;
2073 }
2074 else
2075 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002076 }
2077 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002078 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002079
2080 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002081 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002082 failed = TRUE;
2083 else
2084 {
2085 if (ga.ga_len > 0)
2086 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2087 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2088 ga.ga_len += len;
2089 }
2090 clear_tv(tv);
2091 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002092 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002093 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002094 {
2095 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002096 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002097 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002098
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002099 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002100 {
2101 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002102 {
2103 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002104 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002105 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002106 {
2107 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002108 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002109 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002110 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002111 else
2112 {
2113 msg_sb_eol();
2114 if (iptr->isn_type == ISN_ECHOMSG)
2115 {
2116 msg_attr(ga.ga_data, echo_attr);
2117 out_flush();
2118 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002119 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2120 {
2121 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2122 TRUE);
2123 ui_write((char_u *)"\r\n", 2, TRUE);
2124 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002125 else
2126 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002127 SOURCING_LNUM = iptr->isn_lnum;
2128 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002129 }
2130 }
2131 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002132 ga_clear(&ga);
2133 }
2134 break;
2135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 // load local variable or argument
2137 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002138 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002139 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002141 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 break;
2143
2144 // load v: variable
2145 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002146 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002147 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002149 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 break;
2151
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002152 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 case ISN_LOADSCRIPT:
2154 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002155 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 svar_T *sv;
2157
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002158 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002159 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002160 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002161 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002162 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002163 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002165 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 }
2167 break;
2168
2169 // load s: variable in old script
2170 case ISN_LOADS:
2171 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002172 hashtab_T *ht = &SCRIPT_VARS(
2173 iptr->isn_arg.loadstore.ls_sid);
2174 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 if (di == NULL)
2178 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002179 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002180 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002181 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 }
2183 else
2184 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002185 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002186 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002188 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189 }
2190 }
2191 break;
2192
Bram Moolenaard3aac292020-04-19 14:32:17 +02002193 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002195 case ISN_LOADB:
2196 case ISN_LOADW:
2197 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002199 dictitem_T *di = NULL;
2200 hashtab_T *ht = NULL;
2201 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002202
Bram Moolenaard3aac292020-04-19 14:32:17 +02002203 switch (iptr->isn_type)
2204 {
2205 case ISN_LOADG:
2206 ht = get_globvar_ht();
2207 namespace = 'g';
2208 break;
2209 case ISN_LOADB:
2210 ht = &curbuf->b_vars->dv_hashtab;
2211 namespace = 'b';
2212 break;
2213 case ISN_LOADW:
2214 ht = &curwin->w_vars->dv_hashtab;
2215 namespace = 'w';
2216 break;
2217 case ISN_LOADT:
2218 ht = &curtab->tp_vars->dv_hashtab;
2219 namespace = 't';
2220 break;
2221 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002222 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002223 }
2224 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 if (di == NULL)
2227 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002228 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002229 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002230 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002231 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 }
2233 else
2234 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002235 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002236 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002238 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 }
2240 }
2241 break;
2242
Bram Moolenaar03290b82020-12-19 16:30:44 +01002243 // load autoload variable
2244 case ISN_LOADAUTO:
2245 {
2246 char_u *name = iptr->isn_arg.string;
2247
Bram Moolenaar35578162021-08-02 19:10:38 +02002248 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002249 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002250 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002251 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002252 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002253 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002254 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002255 }
2256 break;
2257
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002258 // load g:/b:/w:/t: namespace
2259 case ISN_LOADGDICT:
2260 case ISN_LOADBDICT:
2261 case ISN_LOADWDICT:
2262 case ISN_LOADTDICT:
2263 {
2264 dict_T *d = NULL;
2265
2266 switch (iptr->isn_type)
2267 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002268 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2269 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2270 case ISN_LOADWDICT: d = curwin->w_vars; break;
2271 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002272 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002273 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002274 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002275 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002276 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002277 tv = STACK_TV_BOT(0);
2278 tv->v_type = VAR_DICT;
2279 tv->v_lock = 0;
2280 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002281 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002282 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002283 }
2284 break;
2285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 // load &option
2287 case ISN_LOADOPT:
2288 {
2289 typval_T optval;
2290 char_u *name = iptr->isn_arg.string;
2291
Bram Moolenaara8c17702020-04-01 21:17:24 +02002292 // This is not expected to fail, name is checked during
2293 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002294 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002295 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002296 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002297 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002299 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 }
2301 break;
2302
2303 // load $ENV
2304 case ISN_LOADENV:
2305 {
2306 typval_T optval;
2307 char_u *name = iptr->isn_arg.string;
2308
Bram Moolenaar35578162021-08-02 19:10:38 +02002309 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002310 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002311 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002312 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002314 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 }
2316 break;
2317
2318 // load @register
2319 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002320 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002321 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 tv = STACK_TV_BOT(0);
2323 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002324 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002325 // This may result in NULL, which should be equivalent to an
2326 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 tv->vval.v_string = get_reg_contents(
2328 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002329 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 break;
2331
2332 // store local variable
2333 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002334 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 tv = STACK_TV_VAR(iptr->isn_arg.number);
2336 clear_tv(tv);
2337 *tv = *STACK_TV_BOT(0);
2338 break;
2339
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002340 // store s: variable in old script
2341 case ISN_STORES:
2342 {
2343 hashtab_T *ht = &SCRIPT_VARS(
2344 iptr->isn_arg.loadstore.ls_sid);
2345 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002346 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002347
Bram Moolenaar4c137212021-04-19 16:48:48 +02002348 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002349 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002350 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002351 else
2352 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002353 SOURCING_LNUM = iptr->isn_lnum;
2354 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002355 {
2356 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002357 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002358 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002359 clear_tv(&di->di_tv);
2360 di->di_tv = *STACK_TV_BOT(0);
2361 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002362 }
2363 break;
2364
2365 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366 case ISN_STORESCRIPT:
2367 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002368 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2369 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002371 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002372 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002373 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002374 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002375
2376 // "const" and "final" are checked at compile time, locking
2377 // the value needs to be checked here.
2378 SOURCING_LNUM = iptr->isn_lnum;
2379 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002380 {
2381 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002382 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002383 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 clear_tv(sv->sv_tv);
2386 *sv->sv_tv = *STACK_TV_BOT(0);
2387 }
2388 break;
2389
2390 // store option
2391 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002392 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002394 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
2395 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 long n = 0;
2397 char_u *s = NULL;
2398 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002399 char_u numbuf[NUMBUFLEN];
2400 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
Bram Moolenaar4c137212021-04-19 16:48:48 +02002402 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 tv = STACK_TV_BOT(0);
2404 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002405 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002407 if (s == NULL)
2408 s = (char_u *)"";
2409 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002410 else if (iptr->isn_type == ISN_STOREFUNCOPT)
2411 {
2412 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002413 // If the option can be set to a function reference or
2414 // a lambda and the passed value is a function
2415 // reference, then convert it to the name (string) of
2416 // the function reference.
2417 s = tv2string(tv, &tofree, numbuf, 0);
2418 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002419 {
2420 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002421 goto on_error;
2422 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002425 // must be VAR_NUMBER, CHECKTYPE makes sure
2426 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002427 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002428 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002429 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 if (msg != NULL)
2431 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002432 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002434 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 }
2437 break;
2438
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002439 // store $ENV
2440 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002441 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002442 tv = STACK_TV_BOT(0);
2443 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2444 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002445 break;
2446
2447 // store @r
2448 case ISN_STOREREG:
2449 {
2450 int reg = iptr->isn_arg.number;
2451
Bram Moolenaar4c137212021-04-19 16:48:48 +02002452 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002453 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002454 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002455 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002456 }
2457 break;
2458
2459 // store v: variable
2460 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002461 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002462 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2463 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002464 // should not happen, type is checked when compiling
2465 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002466 break;
2467
Bram Moolenaard3aac292020-04-19 14:32:17 +02002468 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002470 case ISN_STOREB:
2471 case ISN_STOREW:
2472 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002474 dictitem_T *di;
2475 hashtab_T *ht;
2476 char_u *name = iptr->isn_arg.string + 2;
2477
Bram Moolenaard3aac292020-04-19 14:32:17 +02002478 switch (iptr->isn_type)
2479 {
2480 case ISN_STOREG:
2481 ht = get_globvar_ht();
2482 break;
2483 case ISN_STOREB:
2484 ht = &curbuf->b_vars->dv_hashtab;
2485 break;
2486 case ISN_STOREW:
2487 ht = &curwin->w_vars->dv_hashtab;
2488 break;
2489 case ISN_STORET:
2490 ht = &curtab->tp_vars->dv_hashtab;
2491 break;
2492 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002493 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002494 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495
Bram Moolenaar4c137212021-04-19 16:48:48 +02002496 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002497 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002499 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 else
2501 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002502 SOURCING_LNUM = iptr->isn_lnum;
2503 if (var_check_permission(di, name) == FAIL)
2504 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 clear_tv(&di->di_tv);
2506 di->di_tv = *STACK_TV_BOT(0);
2507 }
2508 }
2509 break;
2510
Bram Moolenaar03290b82020-12-19 16:30:44 +01002511 // store an autoload variable
2512 case ISN_STOREAUTO:
2513 SOURCING_LNUM = iptr->isn_lnum;
2514 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2515 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002516 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002517 break;
2518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 // store number in local variable
2520 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002521 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 clear_tv(tv);
2523 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002524 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 break;
2526
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002527 // store value in list or dict variable
2528 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002529 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002530 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002531 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002532 typval_T *tv_dest = STACK_TV_BOT(-1);
2533 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002534
Bram Moolenaar752fc692021-01-04 21:57:11 +01002535 // Stack contains:
2536 // -3 value to be stored
2537 // -2 index
2538 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002539 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002540 SOURCING_LNUM = iptr->isn_lnum;
2541 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002542 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002543 dest_type = tv_dest->v_type;
2544 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002545 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002546 else if (dest_type == VAR_LIST
2547 && tv_idx->v_type != VAR_NUMBER)
2548 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002549 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002550 status = FAIL;
2551 }
2552 }
2553 else if (dest_type != tv_dest->v_type)
2554 {
2555 // just in case, should be OK
2556 semsg(_(e_expected_str_but_got_str),
2557 vartype_name(dest_type),
2558 vartype_name(tv_dest->v_type));
2559 status = FAIL;
2560 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002561
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002562 if (status == OK && dest_type == VAR_LIST)
2563 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002564 long lidx = (long)tv_idx->vval.v_number;
2565 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002566
2567 if (list == NULL)
2568 {
2569 emsg(_(e_list_not_set));
2570 goto on_error;
2571 }
2572 if (lidx < 0 && list->lv_len + lidx >= 0)
2573 // negative index is relative to the end
2574 lidx = list->lv_len + lidx;
2575 if (lidx < 0 || lidx > list->lv_len)
2576 {
2577 semsg(_(e_listidx), lidx);
2578 goto on_error;
2579 }
2580 if (lidx < list->lv_len)
2581 {
2582 listitem_T *li = list_find(list, lidx);
2583
2584 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002585 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002586 goto on_error;
2587 // overwrite existing list item
2588 clear_tv(&li->li_tv);
2589 li->li_tv = *tv;
2590 }
2591 else
2592 {
2593 if (error_if_locked(list->lv_lock,
2594 e_cannot_change_list))
2595 goto on_error;
2596 // append to list, only fails when out of memory
2597 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002598 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002599 clear_tv(tv);
2600 }
2601 }
2602 else if (status == OK && dest_type == VAR_DICT)
2603 {
2604 char_u *key = tv_idx->vval.v_string;
2605 dict_T *dict = tv_dest->vval.v_dict;
2606 dictitem_T *di;
2607
2608 SOURCING_LNUM = iptr->isn_lnum;
2609 if (dict == NULL)
2610 {
2611 emsg(_(e_dictionary_not_set));
2612 goto on_error;
2613 }
2614 if (key == NULL)
2615 key = (char_u *)"";
2616 di = dict_find(dict, key, -1);
2617 if (di != NULL)
2618 {
2619 if (error_if_locked(di->di_tv.v_lock,
2620 e_cannot_change_dict_item))
2621 goto on_error;
2622 // overwrite existing value
2623 clear_tv(&di->di_tv);
2624 di->di_tv = *tv;
2625 }
2626 else
2627 {
2628 if (error_if_locked(dict->dv_lock,
2629 e_cannot_change_dict))
2630 goto on_error;
2631 // add to dict, only fails when out of memory
2632 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002633 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002634 clear_tv(tv);
2635 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002636 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002637 else if (status == OK && dest_type == VAR_BLOB)
2638 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002639 long lidx = (long)tv_idx->vval.v_number;
2640 blob_T *blob = tv_dest->vval.v_blob;
2641 varnumber_T nr;
2642 int error = FALSE;
2643 int len;
2644
2645 if (blob == NULL)
2646 {
2647 emsg(_(e_blob_not_set));
2648 goto on_error;
2649 }
2650 len = blob_len(blob);
2651 if (lidx < 0 && len + lidx >= 0)
2652 // negative index is relative to the end
2653 lidx = len + lidx;
2654
2655 // Can add one byte at the end.
2656 if (lidx < 0 || lidx > len)
2657 {
2658 semsg(_(e_blobidx), lidx);
2659 goto on_error;
2660 }
2661 if (value_check_lock(blob->bv_lock,
2662 (char_u *)"blob", FALSE))
2663 goto on_error;
2664 nr = tv_get_number_chk(tv, &error);
2665 if (error)
2666 goto on_error;
2667 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002668 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002669 else
2670 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002671 status = FAIL;
2672 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002673 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002674
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002675 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002676 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002677 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002678 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002679 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002680 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002681 goto on_error;
2682 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002683 }
2684 break;
2685
Bram Moolenaar68452172021-04-12 21:21:02 +02002686 // store value in blob range
2687 case ISN_STORERANGE:
2688 {
2689 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2690 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2691 typval_T *tv_dest = STACK_TV_BOT(-1);
2692 int status = OK;
2693
2694 // Stack contains:
2695 // -4 value to be stored
2696 // -3 first index or "none"
2697 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002698 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002699 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002700 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002701 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002702 long n1;
2703 long n2;
2704 int error = FALSE;
2705
2706 SOURCING_LNUM = iptr->isn_lnum;
2707 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2708 if (error)
2709 status = FAIL;
2710 else
2711 {
2712 if (tv_idx2->v_type == VAR_SPECIAL
2713 && tv_idx2->vval.v_number == VVAL_NONE)
2714 n2 = list_len(tv_dest->vval.v_list) - 1;
2715 else
2716 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2717 if (error)
2718 status = FAIL;
2719 else
2720 {
2721 listitem_T *li1 = check_range_index_one(
2722 tv_dest->vval.v_list, &n1, FALSE);
2723
2724 if (li1 == NULL)
2725 status = FAIL;
2726 else
2727 {
2728 status = check_range_index_two(
2729 tv_dest->vval.v_list,
2730 &n1, li1, &n2, FALSE);
2731 if (status != FAIL)
2732 status = list_assign_range(
2733 tv_dest->vval.v_list,
2734 tv->vval.v_list,
2735 n1,
2736 n2,
2737 tv_idx2->v_type == VAR_SPECIAL,
2738 (char_u *)"=",
2739 (char_u *)"[unknown]");
2740 }
2741 }
2742 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002743 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002744 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002745 {
2746 varnumber_T n1;
2747 varnumber_T n2;
2748 int error = FALSE;
2749
2750 n1 = tv_get_number_chk(tv_idx1, &error);
2751 if (error)
2752 status = FAIL;
2753 else
2754 {
2755 if (tv_idx2->v_type == VAR_SPECIAL
2756 && tv_idx2->vval.v_number == VVAL_NONE)
2757 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2758 else
2759 n2 = tv_get_number_chk(tv_idx2, &error);
2760 if (error)
2761 status = FAIL;
2762 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002763 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002764 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002765
2766 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002767 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002768 || check_blob_range(bloblen,
2769 n1, n2, FALSE) == FAIL)
2770 status = FAIL;
2771 else
2772 status = blob_set_range(
2773 tv_dest->vval.v_blob, n1, n2, tv);
2774 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002775 }
2776 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002777 else
2778 {
2779 status = FAIL;
2780 emsg(_(e_blob_required));
2781 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002782
2783 clear_tv(tv_idx1);
2784 clear_tv(tv_idx2);
2785 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002786 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002787 clear_tv(tv);
2788
2789 if (status == FAIL)
2790 goto on_error;
2791 }
2792 break;
2793
Bram Moolenaar0186e582021-01-10 18:33:11 +01002794 // load or store variable or argument from outer scope
2795 case ISN_LOADOUTER:
2796 case ISN_STOREOUTER:
2797 {
2798 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002799 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2800 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002801
2802 while (depth > 1 && outer != NULL)
2803 {
2804 outer = outer->out_up;
2805 --depth;
2806 }
2807 if (outer == NULL)
2808 {
2809 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00002810 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
2811 || ectx->ec_outer_ref == NULL)
2812 // Possibly :def function called from legacy
2813 // context.
2814 emsg(_(e_closure_called_from_invalid_context));
2815 else
2816 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002817 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002818 }
2819 tv = ((typval_T *)outer->out_stack->ga_data)
2820 + outer->out_frame_idx + STACK_FRAME_SIZE
2821 + iptr->isn_arg.outer.outer_idx;
2822 if (iptr->isn_type == ISN_LOADOUTER)
2823 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002824 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002825 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002826 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002827 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002828 }
2829 else
2830 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002831 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002832 clear_tv(tv);
2833 *tv = *STACK_TV_BOT(0);
2834 }
2835 }
2836 break;
2837
Bram Moolenaar752fc692021-01-04 21:57:11 +01002838 // unlet item in list or dict variable
2839 case ISN_UNLETINDEX:
2840 {
2841 typval_T *tv_idx = STACK_TV_BOT(-2);
2842 typval_T *tv_dest = STACK_TV_BOT(-1);
2843 int status = OK;
2844
2845 // Stack contains:
2846 // -2 index
2847 // -1 dict or list
2848 if (tv_dest->v_type == VAR_DICT)
2849 {
2850 // unlet a dict item, index must be a string
2851 if (tv_idx->v_type != VAR_STRING)
2852 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002853 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002854 semsg(_(e_expected_str_but_got_str),
2855 vartype_name(VAR_STRING),
2856 vartype_name(tv_idx->v_type));
2857 status = FAIL;
2858 }
2859 else
2860 {
2861 dict_T *d = tv_dest->vval.v_dict;
2862 char_u *key = tv_idx->vval.v_string;
2863 dictitem_T *di = NULL;
2864
2865 if (key == NULL)
2866 key = (char_u *)"";
2867 if (d != NULL)
2868 di = dict_find(d, key, (int)STRLEN(key));
2869 if (di == NULL)
2870 {
2871 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002872 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002873 semsg(_(e_dictkey), key);
2874 status = FAIL;
2875 }
2876 else
2877 {
2878 // TODO: check for dict or item locked
2879 dictitem_remove(d, di);
2880 }
2881 }
2882 }
2883 else if (tv_dest->v_type == VAR_LIST)
2884 {
2885 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002886 SOURCING_LNUM = iptr->isn_lnum;
2887 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002888 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002889 status = FAIL;
2890 }
2891 else
2892 {
2893 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002894 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002895 listitem_T *li = NULL;
2896
2897 li = list_find(l, n);
2898 if (li == NULL)
2899 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002900 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002901 semsg(_(e_listidx), n);
2902 status = FAIL;
2903 }
2904 else
2905 // TODO: check for list or item locked
2906 listitem_remove(l, li);
2907 }
2908 }
2909 else
2910 {
2911 status = FAIL;
2912 semsg(_(e_cannot_index_str),
2913 vartype_name(tv_dest->v_type));
2914 }
2915
2916 clear_tv(tv_idx);
2917 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002918 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002919 if (status == FAIL)
2920 goto on_error;
2921 }
2922 break;
2923
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002924 // unlet range of items in list variable
2925 case ISN_UNLETRANGE:
2926 {
2927 // Stack contains:
2928 // -3 index1
2929 // -2 index2
2930 // -1 dict or list
2931 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2932 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2933 typval_T *tv_dest = STACK_TV_BOT(-1);
2934 int status = OK;
2935
2936 if (tv_dest->v_type == VAR_LIST)
2937 {
2938 // indexes must be a number
2939 SOURCING_LNUM = iptr->isn_lnum;
2940 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002941 || (tv_idx2->v_type != VAR_SPECIAL
2942 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002943 {
2944 status = FAIL;
2945 }
2946 else
2947 {
2948 list_T *l = tv_dest->vval.v_list;
2949 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002950 long n2 = tv_idx2->v_type == VAR_SPECIAL
2951 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002952 listitem_T *li;
2953
2954 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002955 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002956 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002957 else
2958 {
2959 if (n1 < 0)
2960 n1 = list_idx_of_item(l, li);
2961 if (n2 < 0)
2962 {
2963 listitem_T *li2 = list_find(l, n2);
2964
2965 if (li2 == NULL)
2966 status = FAIL;
2967 else
2968 n2 = list_idx_of_item(l, li2);
2969 }
2970 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002971 && tv_idx2->v_type != VAR_SPECIAL
2972 && n2 < n1)
2973 {
2974 semsg(_(e_listidx), n2);
2975 status = FAIL;
2976 }
2977 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002978 && list_unlet_range(l, li, NULL, n1,
2979 tv_idx2->v_type != VAR_SPECIAL, n2)
2980 == FAIL)
2981 status = FAIL;
2982 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002983 }
2984 }
2985 else
2986 {
2987 status = FAIL;
2988 SOURCING_LNUM = iptr->isn_lnum;
2989 semsg(_(e_cannot_index_str),
2990 vartype_name(tv_dest->v_type));
2991 }
2992
2993 clear_tv(tv_idx1);
2994 clear_tv(tv_idx2);
2995 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002996 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002997 if (status == FAIL)
2998 goto on_error;
2999 }
3000 break;
3001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 // push constant
3003 case ISN_PUSHNR:
3004 case ISN_PUSHBOOL:
3005 case ISN_PUSHSPEC:
3006 case ISN_PUSHF:
3007 case ISN_PUSHS:
3008 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003009 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003010 case ISN_PUSHCHANNEL:
3011 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003012 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003013 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003015 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003016 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 switch (iptr->isn_type)
3018 {
3019 case ISN_PUSHNR:
3020 tv->v_type = VAR_NUMBER;
3021 tv->vval.v_number = iptr->isn_arg.number;
3022 break;
3023 case ISN_PUSHBOOL:
3024 tv->v_type = VAR_BOOL;
3025 tv->vval.v_number = iptr->isn_arg.number;
3026 break;
3027 case ISN_PUSHSPEC:
3028 tv->v_type = VAR_SPECIAL;
3029 tv->vval.v_number = iptr->isn_arg.number;
3030 break;
3031#ifdef FEAT_FLOAT
3032 case ISN_PUSHF:
3033 tv->v_type = VAR_FLOAT;
3034 tv->vval.v_float = iptr->isn_arg.fnumber;
3035 break;
3036#endif
3037 case ISN_PUSHBLOB:
3038 blob_copy(iptr->isn_arg.blob, tv);
3039 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003040 case ISN_PUSHFUNC:
3041 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003042 if (iptr->isn_arg.string == NULL)
3043 tv->vval.v_string = NULL;
3044 else
3045 tv->vval.v_string =
3046 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003047 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003048 case ISN_PUSHCHANNEL:
3049#ifdef FEAT_JOB_CHANNEL
3050 tv->v_type = VAR_CHANNEL;
3051 tv->vval.v_channel = iptr->isn_arg.channel;
3052 if (tv->vval.v_channel != NULL)
3053 ++tv->vval.v_channel->ch_refcount;
3054#endif
3055 break;
3056 case ISN_PUSHJOB:
3057#ifdef FEAT_JOB_CHANNEL
3058 tv->v_type = VAR_JOB;
3059 tv->vval.v_job = iptr->isn_arg.job;
3060 if (tv->vval.v_job != NULL)
3061 ++tv->vval.v_job->jv_refcount;
3062#endif
3063 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 default:
3065 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003066 tv->vval.v_string = vim_strsave(
3067 iptr->isn_arg.string == NULL
3068 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 }
3070 break;
3071
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003072 case ISN_UNLET:
3073 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3074 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003075 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003076 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003077 case ISN_UNLETENV:
3078 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3079 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003080
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003081 case ISN_LOCKUNLOCK:
3082 {
3083 typval_T *lval_root_save = lval_root;
3084 int res;
3085
3086 // Stack has the local variable, argument the whole :lock
3087 // or :unlock command, like ISN_EXEC.
3088 --ectx->ec_stack.ga_len;
3089 lval_root = STACK_TV_BOT(0);
3090 res = exec_command(iptr);
3091 clear_tv(lval_root);
3092 lval_root = lval_root_save;
3093 if (res == FAIL)
3094 goto on_error;
3095 }
3096 break;
3097
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003098 case ISN_LOCKCONST:
3099 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3100 break;
3101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 // create a list from items on the stack; uses a single allocation
3103 // for the list header and the items
3104 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003105 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003106 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 break;
3108
3109 // create a dict from items on the stack
3110 case ISN_NEWDICT:
3111 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003112 int count = iptr->isn_arg.number;
3113 dict_T *dict = dict_alloc();
3114 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003115 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003116 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003118 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003119 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 for (idx = 0; idx < count; ++idx)
3121 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003122 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003124 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003125 key = tv->vval.v_string == NULL
3126 ? (char_u *)"" : tv->vval.v_string;
3127 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003128 if (item != NULL)
3129 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003130 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003131 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003132 dict_unref(dict);
3133 goto on_error;
3134 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003135 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003137 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003138 {
3139 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003140 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3143 item->di_tv.v_lock = 0;
3144 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003145 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003146 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003147 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003148 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 }
3151
3152 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003153 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003154 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003155 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003157 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 tv = STACK_TV_BOT(-1);
3159 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003160 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 tv->vval.v_dict = dict;
3162 ++dict->dv_refcount;
3163 }
3164 break;
3165
3166 // call a :def function
3167 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003168 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003169 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3170 NULL,
3171 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003172 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003173 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 break;
3175
3176 // call a builtin function
3177 case ISN_BCALL:
3178 SOURCING_LNUM = iptr->isn_lnum;
3179 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3180 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003181 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003182 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 break;
3184
3185 // call a funcref or partial
3186 case ISN_PCALL:
3187 {
3188 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3189 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003190 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191
3192 SOURCING_LNUM = iptr->isn_lnum;
3193 if (pfunc->cpf_top)
3194 {
3195 // funcref is above the arguments
3196 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3197 }
3198 else
3199 {
3200 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003201 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003202 partial_tv = *STACK_TV_BOT(0);
3203 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003205 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003206 if (tv == &partial_tv)
3207 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003209 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 }
3211 break;
3212
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003213 case ISN_PCALL_END:
3214 // PCALL finished, arguments have been consumed and replaced by
3215 // the return value. Now clear the funcref from the stack,
3216 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003218 clear_tv(STACK_TV_BOT(-1));
3219 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3220 break;
3221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 // call a user defined function or funcref/partial
3223 case ISN_UCALL:
3224 {
3225 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3226
3227 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003228 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003229 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003230 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 }
3232 break;
3233
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003234 // return from a :def function call without a value
3235 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003236 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003237 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003238 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003239 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003240 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003241 tv->vval.v_number = 0;
3242 tv->v_lock = 0;
3243 // FALLTHROUGH
3244
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003245 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 case ISN_RETURN:
3247 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003248 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003249 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003250
3251 if (trystack->ga_len > 0)
3252 trycmd = ((trycmd_T *)trystack->ga_data)
3253 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003254 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003255 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003257 // jump to ":finally" or ":endtry"
3258 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003259 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003260 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003261 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 trycmd->tcd_return = TRUE;
3263 }
3264 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003265 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 }
3267 break;
3268
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003269 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 case ISN_FUNCREF:
3271 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003272 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003273 ufunc_T *ufunc;
3274 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003277 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003278 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003279 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003280 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003281 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003282 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003283 if (funcref->fr_func_name == NULL)
3284 {
3285 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3286 + funcref->fr_dfunc_idx;
3287
3288 ufunc = pt_dfunc->df_ufunc;
3289 }
3290 else
3291 {
3292 ufunc = find_func(funcref->fr_func_name, FALSE, NULL);
3293 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003294 if (ufunc == NULL)
3295 {
3296 SOURCING_LNUM = iptr->isn_lnum;
3297 emsg(_(e_function_reference_invalid));
3298 goto theend;
3299 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003300 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003301 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003303 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 tv->vval.v_partial = pt;
3305 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003306 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 }
3308 break;
3309
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003310 // Create a global function from a lambda.
3311 case ISN_NEWFUNC:
3312 {
3313 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3314
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003315 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003316 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003317 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003318 }
3319 break;
3320
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003321 // List functions
3322 case ISN_DEF:
3323 if (iptr->isn_arg.string == NULL)
3324 list_functions(NULL);
3325 else
3326 {
3327 exarg_T ea;
3328
3329 CLEAR_FIELD(ea);
3330 ea.cmd = ea.arg = iptr->isn_arg.string;
3331 define_function(&ea, NULL);
3332 }
3333 break;
3334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 // jump if a condition is met
3336 case ISN_JUMP:
3337 {
3338 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003339 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 int jump = TRUE;
3341
3342 if (when != JUMP_ALWAYS)
3343 {
3344 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003345 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003346 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003347 || when == JUMP_IF_COND_TRUE)
3348 {
3349 SOURCING_LNUM = iptr->isn_lnum;
3350 jump = tv_get_bool_chk(tv, &error);
3351 if (error)
3352 goto on_error;
3353 }
3354 else
3355 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003357 || when == JUMP_AND_KEEP_IF_FALSE
3358 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003360 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 {
3362 // drop the value from the stack
3363 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003364 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 }
3366 }
3367 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003368 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 }
3370 break;
3371
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003372 // Jump if an argument with a default value was already set and not
3373 // v:none.
3374 case ISN_JUMP_IF_ARG_SET:
3375 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3376 if (tv->v_type != VAR_UNKNOWN
3377 && !(tv->v_type == VAR_SPECIAL
3378 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003379 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003380 break;
3381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 // top of a for loop
3383 case ISN_FOR:
3384 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003385 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 typval_T *idxtv =
3387 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3388
Bram Moolenaar35578162021-08-02 19:10:38 +02003389 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003390 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003391 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003392 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003393 list_T *list = ltv->vval.v_list;
3394
3395 // push the next item from the list
3396 ++idxtv->vval.v_number;
3397 if (list == NULL
3398 || idxtv->vval.v_number >= list->lv_len)
3399 {
3400 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003401 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3402 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003403 }
3404 else if (list->lv_first == &range_list_item)
3405 {
3406 // non-materialized range() list
3407 tv = STACK_TV_BOT(0);
3408 tv->v_type = VAR_NUMBER;
3409 tv->v_lock = 0;
3410 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003412 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003413 }
3414 else
3415 {
3416 listitem_T *li = list_find(list,
3417 idxtv->vval.v_number);
3418
3419 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003420 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003421 }
3422 }
3423 else if (ltv->v_type == VAR_STRING)
3424 {
3425 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003426
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003427 // The index is for the last byte of the previous
3428 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003429 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003430 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003431 {
3432 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003433 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3434 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003435 }
3436 else
3437 {
3438 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3439
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003440 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003441 tv = STACK_TV_BOT(0);
3442 tv->v_type = VAR_STRING;
3443 tv->vval.v_string = vim_strnsave(
3444 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003445 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003446 idxtv->vval.v_number += clen - 1;
3447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003449 else if (ltv->v_type == VAR_BLOB)
3450 {
3451 blob_T *blob = ltv->vval.v_blob;
3452
3453 // When we get here the first time make a copy of the
3454 // blob, so that the iteration still works when it is
3455 // changed.
3456 if (idxtv->vval.v_number == -1 && blob != NULL)
3457 {
3458 blob_copy(blob, ltv);
3459 blob_unref(blob);
3460 blob = ltv->vval.v_blob;
3461 }
3462
3463 // The index is for the previous byte.
3464 ++idxtv->vval.v_number;
3465 if (blob == NULL
3466 || idxtv->vval.v_number >= blob_len(blob))
3467 {
3468 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003469 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3470 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003471 }
3472 else
3473 {
3474 // Push the next byte from the blob.
3475 tv = STACK_TV_BOT(0);
3476 tv->v_type = VAR_NUMBER;
3477 tv->vval.v_number = blob_get(blob,
3478 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003479 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003480 }
3481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 else
3483 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003484 semsg(_(e_for_loop_on_str_not_supported),
3485 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003486 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 }
3488 }
3489 break;
3490
3491 // start of ":try" block
3492 case ISN_TRY:
3493 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003494 trycmd_T *trycmd = NULL;
3495
Bram Moolenaar35578162021-08-02 19:10:38 +02003496 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003497 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003498 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3499 + ectx->ec_trystack.ga_len;
3500 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003502 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003503 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3504 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003505 trycmd->tcd_catch_idx =
3506 iptr->isn_arg.try.try_ref->try_catch;
3507 trycmd->tcd_finally_idx =
3508 iptr->isn_arg.try.try_ref->try_finally;
3509 trycmd->tcd_endtry_idx =
3510 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 }
3512 break;
3513
3514 case ISN_PUSHEXC:
3515 if (current_exception == NULL)
3516 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003519 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003521 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003522 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003524 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003526 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 tv->vval.v_string = vim_strsave(
3528 (char_u *)current_exception->value);
3529 break;
3530
3531 case ISN_CATCH:
3532 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003533 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534
Bram Moolenaar4c137212021-04-19 16:48:48 +02003535 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 if (trystack->ga_len > 0)
3537 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003538 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 + trystack->ga_len - 1;
3540 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003541 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 }
3543 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003544 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 catch_exception(current_exception);
3546 }
3547 break;
3548
Bram Moolenaarc150c092021-02-13 15:02:46 +01003549 case ISN_TRYCONT:
3550 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003551 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003552 trycont_T *trycont = &iptr->isn_arg.trycont;
3553 int i;
3554 trycmd_T *trycmd;
3555 int iidx = trycont->tct_where;
3556
3557 if (trystack->ga_len < trycont->tct_levels)
3558 {
3559 siemsg("TRYCONT: expected %d levels, found %d",
3560 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003561 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003562 }
3563 // Make :endtry jump to any outer try block and the last
3564 // :endtry inside the loop to the loop start.
3565 for (i = trycont->tct_levels; i > 0; --i)
3566 {
3567 trycmd = ((trycmd_T *)trystack->ga_data)
3568 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003569 // Add one to tcd_cont to be able to jump to
3570 // instruction with index zero.
3571 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003572 iidx = trycmd->tcd_finally_idx == 0
3573 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003574 }
3575 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003576 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003577 }
3578 break;
3579
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003580 case ISN_FINALLY:
3581 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003582 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003583 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3584 + trystack->ga_len - 1;
3585
3586 // Reset the index to avoid a return statement jumps here
3587 // again.
3588 trycmd->tcd_finally_idx = 0;
3589 break;
3590 }
3591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 // end of ":try" block
3593 case ISN_ENDTRY:
3594 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003595 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596
3597 if (trystack->ga_len > 0)
3598 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003599 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 --trystack->ga_len;
3602 --trylevel;
3603 trycmd = ((trycmd_T *)trystack->ga_data)
3604 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003605 if (trycmd->tcd_did_throw)
3606 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003607 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 {
3609 // discard the exception
3610 if (caught_stack == current_exception)
3611 caught_stack = caught_stack->caught;
3612 discard_current_exception();
3613 }
3614
3615 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003616 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003617
Bram Moolenaar4c137212021-04-19 16:48:48 +02003618 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003619 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003620 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003621 clear_tv(STACK_TV_BOT(0));
3622 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003623 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003624 // handling :continue: jump to outer try block or
3625 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003626 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 }
3628 }
3629 break;
3630
3631 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003632 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003633 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003634
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003635 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3636 {
3637 // throwing an exception while using "silent!" causes
3638 // the function to abort but not display an error.
3639 tv = STACK_TV_BOT(-1);
3640 clear_tv(tv);
3641 tv->v_type = VAR_NUMBER;
3642 tv->vval.v_number = 0;
3643 goto done;
3644 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003645 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003646 tv = STACK_TV_BOT(0);
3647 if (tv->vval.v_string == NULL
3648 || *skipwhite(tv->vval.v_string) == NUL)
3649 {
3650 vim_free(tv->vval.v_string);
3651 SOURCING_LNUM = iptr->isn_lnum;
3652 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003653 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003654 }
3655
3656 // Inside a "catch" we need to first discard the caught
3657 // exception.
3658 if (trystack->ga_len > 0)
3659 {
3660 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3661 + trystack->ga_len - 1;
3662 if (trycmd->tcd_caught && current_exception != NULL)
3663 {
3664 // discard the exception
3665 if (caught_stack == current_exception)
3666 caught_stack = caught_stack->caught;
3667 discard_current_exception();
3668 trycmd->tcd_caught = FALSE;
3669 }
3670 }
3671
3672 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3673 == FAIL)
3674 {
3675 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003676 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003677 }
3678 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 break;
3681
3682 // compare with special values
3683 case ISN_COMPAREBOOL:
3684 case ISN_COMPARESPECIAL:
3685 {
3686 typval_T *tv1 = STACK_TV_BOT(-2);
3687 typval_T *tv2 = STACK_TV_BOT(-1);
3688 varnumber_T arg1 = tv1->vval.v_number;
3689 varnumber_T arg2 = tv2->vval.v_number;
3690 int res;
3691
3692 switch (iptr->isn_arg.op.op_type)
3693 {
3694 case EXPR_EQUAL: res = arg1 == arg2; break;
3695 case EXPR_NEQUAL: res = arg1 != arg2; break;
3696 default: res = 0; break;
3697 }
3698
Bram Moolenaar4c137212021-04-19 16:48:48 +02003699 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 tv1->v_type = VAR_BOOL;
3701 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3702 }
3703 break;
3704
3705 // Operation with two number arguments
3706 case ISN_OPNR:
3707 case ISN_COMPARENR:
3708 {
3709 typval_T *tv1 = STACK_TV_BOT(-2);
3710 typval_T *tv2 = STACK_TV_BOT(-1);
3711 varnumber_T arg1 = tv1->vval.v_number;
3712 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003713 varnumber_T res = 0;
3714 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715
3716 switch (iptr->isn_arg.op.op_type)
3717 {
3718 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003719 case EXPR_DIV: if (arg2 == 0)
3720 div_zero = TRUE;
3721 else
3722 res = arg1 / arg2;
3723 break;
3724 case EXPR_REM: if (arg2 == 0)
3725 div_zero = TRUE;
3726 else
3727 res = arg1 % arg2;
3728 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 case EXPR_SUB: res = arg1 - arg2; break;
3730 case EXPR_ADD: res = arg1 + arg2; break;
3731
3732 case EXPR_EQUAL: res = arg1 == arg2; break;
3733 case EXPR_NEQUAL: res = arg1 != arg2; break;
3734 case EXPR_GREATER: res = arg1 > arg2; break;
3735 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3736 case EXPR_SMALLER: res = arg1 < arg2; break;
3737 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003738 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 }
3740
Bram Moolenaar4c137212021-04-19 16:48:48 +02003741 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 if (iptr->isn_type == ISN_COMPARENR)
3743 {
3744 tv1->v_type = VAR_BOOL;
3745 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3746 }
3747 else
3748 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003749 if (div_zero)
3750 {
3751 SOURCING_LNUM = iptr->isn_lnum;
3752 emsg(_(e_divide_by_zero));
3753 goto on_error;
3754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 }
3756 break;
3757
3758 // Computation with two float arguments
3759 case ISN_OPFLOAT:
3760 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003761#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 {
3763 typval_T *tv1 = STACK_TV_BOT(-2);
3764 typval_T *tv2 = STACK_TV_BOT(-1);
3765 float_T arg1 = tv1->vval.v_float;
3766 float_T arg2 = tv2->vval.v_float;
3767 float_T res = 0;
3768 int cmp = FALSE;
3769
3770 switch (iptr->isn_arg.op.op_type)
3771 {
3772 case EXPR_MULT: res = arg1 * arg2; break;
3773 case EXPR_DIV: res = arg1 / arg2; break;
3774 case EXPR_SUB: res = arg1 - arg2; break;
3775 case EXPR_ADD: res = arg1 + arg2; break;
3776
3777 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3778 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3779 case EXPR_GREATER: cmp = arg1 > arg2; break;
3780 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3781 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3782 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3783 default: cmp = 0; break;
3784 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003785 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 if (iptr->isn_type == ISN_COMPAREFLOAT)
3787 {
3788 tv1->v_type = VAR_BOOL;
3789 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3790 }
3791 else
3792 tv1->vval.v_float = res;
3793 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003794#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 break;
3796
3797 case ISN_COMPARELIST:
3798 {
3799 typval_T *tv1 = STACK_TV_BOT(-2);
3800 typval_T *tv2 = STACK_TV_BOT(-1);
3801 list_T *arg1 = tv1->vval.v_list;
3802 list_T *arg2 = tv2->vval.v_list;
3803 int cmp = FALSE;
3804 int ic = iptr->isn_arg.op.op_ic;
3805
3806 switch (iptr->isn_arg.op.op_type)
3807 {
3808 case EXPR_EQUAL: cmp =
3809 list_equal(arg1, arg2, ic, FALSE); break;
3810 case EXPR_NEQUAL: cmp =
3811 !list_equal(arg1, arg2, ic, FALSE); break;
3812 case EXPR_IS: cmp = arg1 == arg2; break;
3813 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3814 default: cmp = 0; break;
3815 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003816 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 clear_tv(tv1);
3818 clear_tv(tv2);
3819 tv1->v_type = VAR_BOOL;
3820 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3821 }
3822 break;
3823
3824 case ISN_COMPAREBLOB:
3825 {
3826 typval_T *tv1 = STACK_TV_BOT(-2);
3827 typval_T *tv2 = STACK_TV_BOT(-1);
3828 blob_T *arg1 = tv1->vval.v_blob;
3829 blob_T *arg2 = tv2->vval.v_blob;
3830 int cmp = FALSE;
3831
3832 switch (iptr->isn_arg.op.op_type)
3833 {
3834 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3835 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3836 case EXPR_IS: cmp = arg1 == arg2; break;
3837 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3838 default: cmp = 0; break;
3839 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003840 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 clear_tv(tv1);
3842 clear_tv(tv2);
3843 tv1->v_type = VAR_BOOL;
3844 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3845 }
3846 break;
3847
3848 // TODO: handle separately
3849 case ISN_COMPARESTRING:
3850 case ISN_COMPAREDICT:
3851 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 case ISN_COMPAREANY:
3853 {
3854 typval_T *tv1 = STACK_TV_BOT(-2);
3855 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003856 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 int ic = iptr->isn_arg.op.op_ic;
3858
Bram Moolenaareb26f432020-09-14 16:50:05 +02003859 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003860 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003862 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 }
3864 break;
3865
3866 case ISN_ADDLIST:
3867 case ISN_ADDBLOB:
3868 {
3869 typval_T *tv1 = STACK_TV_BOT(-2);
3870 typval_T *tv2 = STACK_TV_BOT(-1);
3871
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003872 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003874 {
3875 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3876 && tv1->vval.v_list != NULL)
3877 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3878 NULL);
3879 else
3880 eval_addlist(tv1, tv2);
3881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 else
3883 eval_addblob(tv1, tv2);
3884 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003885 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 }
3887 break;
3888
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003889 case ISN_LISTAPPEND:
3890 {
3891 typval_T *tv1 = STACK_TV_BOT(-2);
3892 typval_T *tv2 = STACK_TV_BOT(-1);
3893 list_T *l = tv1->vval.v_list;
3894
3895 // add an item to a list
3896 if (l == NULL)
3897 {
3898 SOURCING_LNUM = iptr->isn_lnum;
3899 emsg(_(e_cannot_add_to_null_list));
3900 goto on_error;
3901 }
3902 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003903 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003904 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003905 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003906 }
3907 break;
3908
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003909 case ISN_BLOBAPPEND:
3910 {
3911 typval_T *tv1 = STACK_TV_BOT(-2);
3912 typval_T *tv2 = STACK_TV_BOT(-1);
3913 blob_T *b = tv1->vval.v_blob;
3914 int error = FALSE;
3915 varnumber_T n;
3916
3917 // add a number to a blob
3918 if (b == NULL)
3919 {
3920 SOURCING_LNUM = iptr->isn_lnum;
3921 emsg(_(e_cannot_add_to_null_blob));
3922 goto on_error;
3923 }
3924 n = tv_get_number_chk(tv2, &error);
3925 if (error)
3926 goto on_error;
3927 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003928 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003929 }
3930 break;
3931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 // Computation with two arguments of unknown type
3933 case ISN_OPANY:
3934 {
3935 typval_T *tv1 = STACK_TV_BOT(-2);
3936 typval_T *tv2 = STACK_TV_BOT(-1);
3937 varnumber_T n1, n2;
3938#ifdef FEAT_FLOAT
3939 float_T f1 = 0, f2 = 0;
3940#endif
3941 int error = FALSE;
3942
3943 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3944 {
3945 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3946 {
3947 eval_addlist(tv1, tv2);
3948 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003949 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 break;
3951 }
3952 else if (tv1->v_type == VAR_BLOB
3953 && tv2->v_type == VAR_BLOB)
3954 {
3955 eval_addblob(tv1, tv2);
3956 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003957 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 break;
3959 }
3960 }
3961#ifdef FEAT_FLOAT
3962 if (tv1->v_type == VAR_FLOAT)
3963 {
3964 f1 = tv1->vval.v_float;
3965 n1 = 0;
3966 }
3967 else
3968#endif
3969 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003970 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 n1 = tv_get_number_chk(tv1, &error);
3972 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003973 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974#ifdef FEAT_FLOAT
3975 if (tv2->v_type == VAR_FLOAT)
3976 f1 = n1;
3977#endif
3978 }
3979#ifdef FEAT_FLOAT
3980 if (tv2->v_type == VAR_FLOAT)
3981 {
3982 f2 = tv2->vval.v_float;
3983 n2 = 0;
3984 }
3985 else
3986#endif
3987 {
3988 n2 = tv_get_number_chk(tv2, &error);
3989 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003990 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991#ifdef FEAT_FLOAT
3992 if (tv1->v_type == VAR_FLOAT)
3993 f2 = n2;
3994#endif
3995 }
3996#ifdef FEAT_FLOAT
3997 // if there is a float on either side the result is a float
3998 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3999 {
4000 switch (iptr->isn_arg.op.op_type)
4001 {
4002 case EXPR_MULT: f1 = f1 * f2; break;
4003 case EXPR_DIV: f1 = f1 / f2; break;
4004 case EXPR_SUB: f1 = f1 - f2; break;
4005 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004006 default: SOURCING_LNUM = iptr->isn_lnum;
4007 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004008 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 }
4010 clear_tv(tv1);
4011 clear_tv(tv2);
4012 tv1->v_type = VAR_FLOAT;
4013 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004014 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 }
4016 else
4017#endif
4018 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004019 int failed = FALSE;
4020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 switch (iptr->isn_arg.op.op_type)
4022 {
4023 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004024 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4025 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004026 goto on_error;
4027 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 case EXPR_SUB: n1 = n1 - n2; break;
4029 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004030 default: n1 = num_modulus(n1, n2, &failed);
4031 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004032 goto on_error;
4033 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 }
4035 clear_tv(tv1);
4036 clear_tv(tv2);
4037 tv1->v_type = VAR_NUMBER;
4038 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004039 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 }
4041 }
4042 break;
4043
4044 case ISN_CONCAT:
4045 {
4046 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4047 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4048 char_u *res;
4049
4050 res = concat_str(str1, str2);
4051 clear_tv(STACK_TV_BOT(-2));
4052 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004053 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 STACK_TV_BOT(-1)->vval.v_string = res;
4055 }
4056 break;
4057
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004058 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004059 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004060 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004061 int is_slice = iptr->isn_type == ISN_STRSLICE;
4062 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004063 char_u *res;
4064
4065 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004066 // string slice: string is at stack-3, first index at
4067 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004068 if (is_slice)
4069 {
4070 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004071 n1 = tv->vval.v_number;
4072 }
4073
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004074 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004075 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004076
Bram Moolenaar4c137212021-04-19 16:48:48 +02004077 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004078 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004079 if (is_slice)
4080 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004081 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004082 else
4083 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004084 // single character (including composing characters).
4085 // If the index is too big or negative the result is
4086 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004087 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004088 vim_free(tv->vval.v_string);
4089 tv->vval.v_string = res;
4090 }
4091 break;
4092
4093 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004094 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004095 case ISN_BLOBINDEX:
4096 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004098 int is_slice = iptr->isn_type == ISN_LISTSLICE
4099 || iptr->isn_type == ISN_BLOBSLICE;
4100 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4101 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004102 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004103 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104
4105 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004106 // list slice: list is at stack-3, indexes at stack-2 and
4107 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004108 // Same for blob.
4109 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110
4111 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004112 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004114
4115 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 {
Bram Moolenaared591872020-08-15 22:14:53 +02004117 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004118 n1 = tv->vval.v_number;
4119 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 }
Bram Moolenaared591872020-08-15 22:14:53 +02004121
Bram Moolenaar4c137212021-04-19 16:48:48 +02004122 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004123 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004124 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004125 if (is_blob)
4126 {
4127 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4128 n1, n2, FALSE, tv) == FAIL)
4129 goto on_error;
4130 }
4131 else
4132 {
4133 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4134 n1, n2, FALSE, tv, TRUE) == FAIL)
4135 goto on_error;
4136 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 }
4138 break;
4139
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004140 case ISN_ANYINDEX:
4141 case ISN_ANYSLICE:
4142 {
4143 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4144 typval_T *var1, *var2;
4145 int res;
4146
4147 // index: composite is at stack-2, index at stack-1
4148 // slice: composite is at stack-3, indexes at stack-2 and
4149 // stack-1
4150 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004151 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004152 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4153 goto on_error;
4154 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4155 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004156 res = eval_index_inner(tv, is_slice, var1, var2,
4157 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 clear_tv(var1);
4159 if (is_slice)
4160 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004161 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 if (res == FAIL)
4163 goto on_error;
4164 }
4165 break;
4166
Bram Moolenaar9af78762020-06-16 11:34:42 +02004167 case ISN_SLICE:
4168 {
4169 list_T *list;
4170 int count = iptr->isn_arg.number;
4171
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004172 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004173 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004174 list = tv->vval.v_list;
4175
4176 // no error for short list, expect it to be checked earlier
4177 if (list != NULL && list->lv_len >= count)
4178 {
4179 list_T *newlist = list_slice(list,
4180 count, list->lv_len - 1);
4181
4182 if (newlist != NULL)
4183 {
4184 list_unref(list);
4185 tv->vval.v_list = newlist;
4186 ++newlist->lv_refcount;
4187 }
4188 }
4189 }
4190 break;
4191
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004192 case ISN_GETITEM:
4193 {
4194 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004195 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004196
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004197 // Get list item: list is at stack-1, push item.
4198 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004199 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4200 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004201
Bram Moolenaar35578162021-08-02 19:10:38 +02004202 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004203 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004204 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004205 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004206
4207 // Useful when used in unpack assignment. Reset at
4208 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004209 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004210 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004211 }
4212 break;
4213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 case ISN_MEMBER:
4215 {
4216 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004217 char_u *key;
4218 dictitem_T *di;
4219
4220 // dict member: dict is at stack-2, key at stack-1
4221 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004222 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004223 dict = tv->vval.v_dict;
4224
4225 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004226 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004227 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004228 if (key == NULL)
4229 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004230
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004231 if ((di = dict_find(dict, key, -1)) == NULL)
4232 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004233 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004234 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004235
4236 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004237 // stack contents makes sense and the dict stack is
4238 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004239 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004240 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004241 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004242 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004243 tv->v_type = VAR_NUMBER;
4244 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004245 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004246 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004247 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004248 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004249 // Put the dict used on the dict stack, it might be used by
4250 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004251 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004252 if (dict_stack_save(tv) == FAIL)
4253 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004254 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004255 }
4256 break;
4257
4258 // dict member with string key
4259 case ISN_STRINGMEMBER:
4260 {
4261 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 dictitem_T *di;
4263
4264 tv = STACK_TV_BOT(-1);
4265 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4266 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004267 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004269 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 }
4271 dict = tv->vval.v_dict;
4272
4273 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4274 == NULL)
4275 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004276 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004278 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004280 // Put the dict used on the dict stack, it might be used by
4281 // a dict function later.
4282 if (dict_stack_save(tv) == FAIL)
4283 goto on_fatal_error;
4284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004286 }
4287 break;
4288
4289 case ISN_CLEARDICT:
4290 dict_stack_drop();
4291 break;
4292
4293 case ISN_USEDICT:
4294 {
4295 typval_T *dict_tv = dict_stack_get_tv();
4296
4297 // Turn "dict.Func" into a partial for "Func" bound to
4298 // "dict". Don't do this when "Func" is already a partial
4299 // that was bound explicitly (pt_auto is FALSE).
4300 tv = STACK_TV_BOT(-1);
4301 if (dict_tv != NULL
4302 && dict_tv->v_type == VAR_DICT
4303 && dict_tv->vval.v_dict != NULL
4304 && (tv->v_type == VAR_FUNC
4305 || (tv->v_type == VAR_PARTIAL
4306 && (tv->vval.v_partial->pt_auto
4307 || tv->vval.v_partial->pt_dict == NULL))))
4308 dict_tv->vval.v_dict =
4309 make_partial(dict_tv->vval.v_dict, tv);
4310 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 }
4312 break;
4313
4314 case ISN_NEGATENR:
4315 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004316 if (tv->v_type != VAR_NUMBER
4317#ifdef FEAT_FLOAT
4318 && tv->v_type != VAR_FLOAT
4319#endif
4320 )
4321 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004322 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004323 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004324 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004325 }
4326#ifdef FEAT_FLOAT
4327 if (tv->v_type == VAR_FLOAT)
4328 tv->vval.v_float = -tv->vval.v_float;
4329 else
4330#endif
4331 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 break;
4333
4334 case ISN_CHECKNR:
4335 {
4336 int error = FALSE;
4337
4338 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004339 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004341 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 (void)tv_get_number_chk(tv, &error);
4343 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004344 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 }
4346 break;
4347
4348 case ISN_CHECKTYPE:
4349 {
4350 checktype_T *ct = &iptr->isn_arg.type;
4351
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004352 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004353 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004354 if (!ectx->ec_where.wt_variable)
4355 ectx->ec_where.wt_index = ct->ct_arg_idx;
4356 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4357 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004358 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004359 if (!ectx->ec_where.wt_variable)
4360 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004361
4362 // number 0 is FALSE, number 1 is TRUE
4363 if (tv->v_type == VAR_NUMBER
4364 && ct->ct_type->tt_type == VAR_BOOL
4365 && (tv->vval.v_number == 0
4366 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004368 tv->v_type = VAR_BOOL;
4369 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004370 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 }
4372 }
4373 break;
4374
Bram Moolenaar9af78762020-06-16 11:34:42 +02004375 case ISN_CHECKLEN:
4376 {
4377 int min_len = iptr->isn_arg.checklen.cl_min_len;
4378 list_T *list = NULL;
4379
4380 tv = STACK_TV_BOT(-1);
4381 if (tv->v_type == VAR_LIST)
4382 list = tv->vval.v_list;
4383 if (list == NULL || list->lv_len < min_len
4384 || (list->lv_len > min_len
4385 && !iptr->isn_arg.checklen.cl_more_OK))
4386 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004387 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004388 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004389 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004390 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004391 }
4392 }
4393 break;
4394
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004395 case ISN_SETTYPE:
4396 {
4397 checktype_T *ct = &iptr->isn_arg.type;
4398
4399 tv = STACK_TV_BOT(-1);
4400 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4401 {
4402 free_type(tv->vval.v_dict->dv_type);
4403 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4404 }
4405 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4406 {
4407 free_type(tv->vval.v_list->lv_type);
4408 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4409 }
4410 }
4411 break;
4412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004414 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 {
4416 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004417 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004419 if (iptr->isn_type == ISN_2BOOL)
4420 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004421 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004422 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004423 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004424 n = !n;
4425 }
4426 else
4427 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004428 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004429 SOURCING_LNUM = iptr->isn_lnum;
4430 n = tv_get_bool_chk(tv, &error);
4431 if (error)
4432 goto on_error;
4433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 clear_tv(tv);
4435 tv->v_type = VAR_BOOL;
4436 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4437 }
4438 break;
4439
4440 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004441 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004442 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004443 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4444 iptr->isn_type == ISN_2STRING_ANY,
4445 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004446 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 break;
4448
Bram Moolenaar08597872020-12-10 19:43:40 +01004449 case ISN_RANGE:
4450 {
4451 exarg_T ea;
4452 char *errormsg;
4453
Bram Moolenaarece0b872021-01-08 20:40:45 +01004454 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004455 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004456 ea.addr_type = ADDR_LINES;
4457 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004458 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004459 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004460 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004461
Bram Moolenaar35578162021-08-02 19:10:38 +02004462 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004463 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004464 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004465 tv = STACK_TV_BOT(-1);
4466 tv->v_type = VAR_NUMBER;
4467 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004468 if (ea.addr_count == 0)
4469 tv->vval.v_number = curwin->w_cursor.lnum;
4470 else
4471 tv->vval.v_number = ea.line2;
4472 }
4473 break;
4474
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004475 case ISN_PUT:
4476 {
4477 int regname = iptr->isn_arg.put.put_regname;
4478 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4479 char_u *expr = NULL;
4480 int dir = FORWARD;
4481
Bram Moolenaar08597872020-12-10 19:43:40 +01004482 if (lnum < -2)
4483 {
4484 // line number was put on the stack by ISN_RANGE
4485 tv = STACK_TV_BOT(-1);
4486 curwin->w_cursor.lnum = tv->vval.v_number;
4487 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4488 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004489 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004490 }
4491 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004492 // :put! above cursor
4493 dir = BACKWARD;
4494 else if (lnum >= 0)
4495 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004496
4497 if (regname == '=')
4498 {
4499 tv = STACK_TV_BOT(-1);
4500 if (tv->v_type == VAR_STRING)
4501 expr = tv->vval.v_string;
4502 else
4503 {
4504 expr = typval2string(tv, TRUE); // allocates value
4505 clear_tv(tv);
4506 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004507 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004508 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004509 check_cursor();
4510 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4511 vim_free(expr);
4512 }
4513 break;
4514
Bram Moolenaar02194d22020-10-24 23:08:38 +02004515 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004516 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4517 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4518 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4519 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004520 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4521 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004522 break;
4523
Bram Moolenaar02194d22020-10-24 23:08:38 +02004524 case ISN_CMDMOD_REV:
4525 // filter regprog is owned by the instruction, don't free it
4526 cmdmod.cmod_filter_regmatch.regprog = NULL;
4527 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004528 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4529 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004530 break;
4531
Bram Moolenaar792f7862020-11-23 08:31:18 +01004532 case ISN_UNPACK:
4533 {
4534 int count = iptr->isn_arg.unpack.unp_count;
4535 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4536 list_T *l;
4537 listitem_T *li;
4538 int i;
4539
4540 // Check there is a valid list to unpack.
4541 tv = STACK_TV_BOT(-1);
4542 if (tv->v_type != VAR_LIST)
4543 {
4544 SOURCING_LNUM = iptr->isn_lnum;
4545 emsg(_(e_for_argument_must_be_sequence_of_lists));
4546 goto on_error;
4547 }
4548 l = tv->vval.v_list;
4549 if (l == NULL
4550 || l->lv_len < (semicolon ? count - 1 : count))
4551 {
4552 SOURCING_LNUM = iptr->isn_lnum;
4553 emsg(_(e_list_value_does_not_have_enough_items));
4554 goto on_error;
4555 }
4556 else if (!semicolon && l->lv_len > count)
4557 {
4558 SOURCING_LNUM = iptr->isn_lnum;
4559 emsg(_(e_list_value_has_more_items_than_targets));
4560 goto on_error;
4561 }
4562
4563 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004564 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004565 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004566 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004567
4568 // Variable after semicolon gets a list with the remaining
4569 // items.
4570 if (semicolon)
4571 {
4572 list_T *rem_list =
4573 list_alloc_with_items(l->lv_len - count + 1);
4574
4575 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004576 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004577 tv = STACK_TV_BOT(-count);
4578 tv->vval.v_list = rem_list;
4579 ++rem_list->lv_refcount;
4580 tv->v_lock = 0;
4581 li = l->lv_first;
4582 for (i = 0; i < count - 1; ++i)
4583 li = li->li_next;
4584 for (i = 0; li != NULL; ++i)
4585 {
4586 list_set_item(rem_list, i, &li->li_tv);
4587 li = li->li_next;
4588 }
4589 --count;
4590 }
4591
4592 // Produce the values in reverse order, first item last.
4593 li = l->lv_first;
4594 for (i = 0; i < count; ++i)
4595 {
4596 tv = STACK_TV_BOT(-i - 1);
4597 copy_tv(&li->li_tv, tv);
4598 li = li->li_next;
4599 }
4600
4601 list_unref(l);
4602 }
4603 break;
4604
Bram Moolenaarb2049902021-01-24 12:53:53 +01004605 case ISN_PROF_START:
4606 case ISN_PROF_END:
4607 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004608#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004609 funccall_T cookie;
4610 ufunc_T *cur_ufunc =
4611 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004612 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004613
4614 cookie.func = cur_ufunc;
4615 if (iptr->isn_type == ISN_PROF_START)
4616 {
4617 func_line_start(&cookie, iptr->isn_lnum);
4618 // if we get here the instruction is executed
4619 func_line_exec(&cookie);
4620 }
4621 else
4622 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004623#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004624 }
4625 break;
4626
Bram Moolenaare99d4222021-06-13 14:01:26 +02004627 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004628 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004629 break;
4630
Bram Moolenaar389df252020-07-09 21:20:47 +02004631 case ISN_SHUFFLE:
4632 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004633 typval_T tmp_tv;
4634 int item = iptr->isn_arg.shuffle.shfl_item;
4635 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004636
4637 tmp_tv = *STACK_TV_BOT(-item);
4638 for ( ; up > 0 && item > 1; --up)
4639 {
4640 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4641 --item;
4642 }
4643 *STACK_TV_BOT(-item) = tmp_tv;
4644 }
4645 break;
4646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004648 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004650 ectx->ec_where.wt_index = 0;
4651 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 break;
4653 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004654 continue;
4655
Bram Moolenaard032f342020-07-18 18:13:02 +02004656func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004657 // Restore previous function. If the frame pointer is where we started
4658 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004659 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004660 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004661
Bram Moolenaar4c137212021-04-19 16:48:48 +02004662 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004663 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004664 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004665 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004666
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004667on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004668 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004669 // If "emsg_silent" is set then ignore the error, unless it was set
4670 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004671 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004672 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004673 {
4674 // If a sequence of instructions causes an error while ":silent!"
4675 // was used, restore the stack length and jump ahead to restoring
4676 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004677 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004678 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004679 while (ectx->ec_stack.ga_len
4680 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004681 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004682 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004683 clear_tv(STACK_TV_BOT(0));
4684 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004685 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4686 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004687 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004688 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004689 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004690on_fatal_error:
4691 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004692 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004693 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004694 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 }
4696
4697done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004698 ret = OK;
4699theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004700 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004701 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4702 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004703}
4704
4705/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004706 * Execute the instructions from a VAR_INSTR typeval and put the result in
4707 * "rettv".
4708 * Return OK or FAIL.
4709 */
4710 int
4711exe_typval_instr(typval_T *tv, typval_T *rettv)
4712{
4713 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4714 isn_T *save_instr = ectx->ec_instr;
4715 int save_iidx = ectx->ec_iidx;
4716 int res;
4717
4718 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4719 res = exec_instructions(ectx);
4720 if (res == OK)
4721 {
4722 *rettv = *STACK_TV_BOT(-1);
4723 --ectx->ec_stack.ga_len;
4724 }
4725
4726 ectx->ec_instr = save_instr;
4727 ectx->ec_iidx = save_iidx;
4728
4729 return res;
4730}
4731
4732/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004733 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4734 * "substitute_instr".
4735 */
4736 char_u *
4737exe_substitute_instr(void)
4738{
4739 ectx_T *ectx = substitute_instr->subs_ectx;
4740 isn_T *save_instr = ectx->ec_instr;
4741 int save_iidx = ectx->ec_iidx;
4742 char_u *res;
4743
4744 ectx->ec_instr = substitute_instr->subs_instr;
4745 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004746 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004747 typval_T *tv = STACK_TV_BOT(-1);
4748
Bram Moolenaar27523602021-06-05 21:36:19 +02004749 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004750 --ectx->ec_stack.ga_len;
4751 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004752 }
4753 else
4754 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004755 substitute_instr->subs_status = FAIL;
4756 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758
Bram Moolenaar4c137212021-04-19 16:48:48 +02004759 ectx->ec_instr = save_instr;
4760 ectx->ec_iidx = save_iidx;
4761
4762 return res;
4763}
4764
4765/*
4766 * Call a "def" function from old Vim script.
4767 * Return OK or FAIL.
4768 */
4769 int
4770call_def_function(
4771 ufunc_T *ufunc,
4772 int argc_arg, // nr of arguments
4773 typval_T *argv, // arguments
4774 partial_T *partial, // optional partial for context
4775 typval_T *rettv) // return value
4776{
4777 ectx_T ectx; // execution context
4778 int argc = argc_arg;
4779 typval_T *tv;
4780 int idx;
4781 int ret = FAIL;
4782 int defcount = ufunc->uf_args.ga_len - argc;
4783 sctx_T save_current_sctx = current_sctx;
4784 int did_emsg_before = did_emsg_cumul + did_emsg;
4785 int save_suppress_errthrow = suppress_errthrow;
4786 msglist_T **saved_msg_list = NULL;
4787 msglist_T *private_msg_list = NULL;
4788 int save_emsg_silent_def = emsg_silent_def;
4789 int save_did_emsg_def = did_emsg_def;
4790 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004791 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004792
4793// Get pointer to item in the stack.
4794#undef STACK_TV
4795#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4796
4797// Get pointer to item at the bottom of the stack, -1 is the bottom.
4798#undef STACK_TV_BOT
4799#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4800
4801// Get pointer to a local variable on the stack. Negative for arguments.
4802#undef STACK_TV_VAR
4803#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4804
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004805 // Update uf_has_breakpoint if needed.
4806 update_has_breakpoint(ufunc);
4807
Bram Moolenaar4c137212021-04-19 16:48:48 +02004808 if (ufunc->uf_def_status == UF_NOT_COMPILED
4809 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004810 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4811 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004812 == FAIL))
4813 {
4814 if (did_emsg_cumul + did_emsg == did_emsg_before)
4815 semsg(_(e_function_is_not_compiled_str),
4816 printable_func_name(ufunc));
4817 return FAIL;
4818 }
4819
4820 {
4821 // Check the function was really compiled.
4822 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4823 + ufunc->uf_dfunc_idx;
4824 if (INSTRUCTIONS(dfunc) == NULL)
4825 {
4826 iemsg("using call_def_function() on not compiled function");
4827 return FAIL;
4828 }
4829 }
4830
4831 // If depth of calling is getting too high, don't execute the function.
4832 orig_funcdepth = funcdepth_get();
4833 if (funcdepth_increment() == FAIL)
4834 return FAIL;
4835
4836 CLEAR_FIELD(ectx);
4837 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4838 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004839 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004840 {
4841 funcdepth_decrement();
4842 return FAIL;
4843 }
4844 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4845 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4846 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004847 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004848
4849 idx = argc - ufunc->uf_args.ga_len;
4850 if (idx > 0 && ufunc->uf_va_name == NULL)
4851 {
4852 if (idx == 1)
4853 emsg(_(e_one_argument_too_many));
4854 else
4855 semsg(_(e_nr_arguments_too_many), idx);
4856 goto failed_early;
4857 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004858 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4859 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004860 {
4861 if (idx == -1)
4862 emsg(_(e_one_argument_too_few));
4863 else
4864 semsg(_(e_nr_arguments_too_few), -idx);
4865 goto failed_early;
4866 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004867
4868 // Put arguments on the stack, but no more than what the function expects.
4869 // A lambda can be called with more arguments than it uses.
4870 for (idx = 0; idx < argc
4871 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4872 ++idx)
4873 {
4874 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4875 && argv[idx].v_type == VAR_SPECIAL
4876 && argv[idx].vval.v_number == VVAL_NONE)
4877 {
4878 // Use the default value.
4879 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4880 }
4881 else
4882 {
4883 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4884 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004885 ufunc->uf_arg_types[idx], &argv[idx],
4886 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004887 goto failed_early;
4888 copy_tv(&argv[idx], STACK_TV_BOT(0));
4889 }
4890 ++ectx.ec_stack.ga_len;
4891 }
4892
4893 // Turn varargs into a list. Empty list if no args.
4894 if (ufunc->uf_va_name != NULL)
4895 {
4896 int vararg_count = argc - ufunc->uf_args.ga_len;
4897
4898 if (vararg_count < 0)
4899 vararg_count = 0;
4900 else
4901 argc -= vararg_count;
4902 if (exe_newlist(vararg_count, &ectx) == FAIL)
4903 goto failed_early;
4904
4905 // Check the type of the list items.
4906 tv = STACK_TV_BOT(-1);
4907 if (ufunc->uf_va_type != NULL
4908 && ufunc->uf_va_type != &t_list_any
4909 && ufunc->uf_va_type->tt_member != &t_any
4910 && tv->vval.v_list != NULL)
4911 {
4912 type_T *expected = ufunc->uf_va_type->tt_member;
4913 listitem_T *li = tv->vval.v_list->lv_first;
4914
4915 for (idx = 0; idx < vararg_count; ++idx)
4916 {
4917 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004918 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004919 goto failed_early;
4920 li = li->li_next;
4921 }
4922 }
4923
4924 if (defcount > 0)
4925 // Move varargs list to below missing default arguments.
4926 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4927 --ectx.ec_stack.ga_len;
4928 }
4929
4930 // Make space for omitted arguments, will store default value below.
4931 // Any varargs list goes after them.
4932 if (defcount > 0)
4933 for (idx = 0; idx < defcount; ++idx)
4934 {
4935 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4936 ++ectx.ec_stack.ga_len;
4937 }
4938 if (ufunc->uf_va_name != NULL)
4939 ++ectx.ec_stack.ga_len;
4940
4941 // Frame pointer points to just after arguments.
4942 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4943 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4944
4945 {
4946 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4947 + ufunc->uf_dfunc_idx;
4948 ufunc_T *base_ufunc = dfunc->df_ufunc;
4949
4950 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4951 // by copy_func().
4952 if (partial != NULL || base_ufunc->uf_partial != NULL)
4953 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004954 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4955 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004956 goto failed_early;
4957 if (partial != NULL)
4958 {
4959 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4960 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004961 if (current_ectx->ec_outer_ref != NULL
4962 && current_ectx->ec_outer_ref->or_outer != NULL)
4963 ectx.ec_outer_ref->or_outer =
4964 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004965 }
4966 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004967 {
4968 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4969 ++partial->pt_refcount;
4970 ectx.ec_outer_ref->or_partial = partial;
4971 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004972 }
4973 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004974 {
4975 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4976 ++base_ufunc->uf_partial->pt_refcount;
4977 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4978 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004979 }
4980 }
4981
4982 // dummy frame entries
4983 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4984 {
4985 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4986 ++ectx.ec_stack.ga_len;
4987 }
4988
4989 {
4990 // Reserve space for local variables and any closure reference count.
4991 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4992 + ufunc->uf_dfunc_idx;
4993
4994 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4995 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4996 ectx.ec_stack.ga_len += dfunc->df_varcount;
4997 if (dfunc->df_has_closure)
4998 {
4999 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5000 STACK_TV_VAR(idx)->vval.v_number = 0;
5001 ++ectx.ec_stack.ga_len;
5002 }
5003
5004 ectx.ec_instr = INSTRUCTIONS(dfunc);
5005 }
5006
5007 // Following errors are in the function, not the caller.
5008 // Commands behave like vim9script.
5009 estack_push_ufunc(ufunc, 1);
5010 current_sctx = ufunc->uf_script_ctx;
5011 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5012
5013 // Use a specific location for storing error messages to be converted to an
5014 // exception.
5015 saved_msg_list = msg_list;
5016 msg_list = &private_msg_list;
5017
5018 // Do turn errors into exceptions.
5019 suppress_errthrow = FALSE;
5020
5021 // Do not delete the function while executing it.
5022 ++ufunc->uf_calls;
5023
5024 // When ":silent!" was used before calling then we still abort the
5025 // function. If ":silent!" is used in the function then we don't.
5026 emsg_silent_def = emsg_silent;
5027 did_emsg_def = 0;
5028
5029 ectx.ec_where.wt_index = 0;
5030 ectx.ec_where.wt_variable = FALSE;
5031
5032 // Execute the instructions until done.
5033 ret = exec_instructions(&ectx);
5034 if (ret == OK)
5035 {
5036 // function finished, get result from the stack.
5037 if (ufunc->uf_ret_type == &t_void)
5038 {
5039 rettv->v_type = VAR_VOID;
5040 }
5041 else
5042 {
5043 tv = STACK_TV_BOT(-1);
5044 *rettv = *tv;
5045 tv->v_type = VAR_UNKNOWN;
5046 }
5047 }
5048
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005049 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005050 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5051 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005052
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005053 // Deal with any remaining closures, they may be in use somewhere.
5054 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005055 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005056 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005057 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
5058 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005059
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005060 estack_pop();
5061 current_sctx = save_current_sctx;
5062
Bram Moolenaar2336c372021-12-06 15:06:54 +00005063 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5064 // Function was unreferenced while being used, free it now.
5065 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005066
Bram Moolenaar352134b2020-10-17 22:04:08 +02005067 if (*msg_list != NULL && saved_msg_list != NULL)
5068 {
5069 msglist_T **plist = saved_msg_list;
5070
5071 // Append entries from the current msg_list (uncaught exceptions) to
5072 // the saved msg_list.
5073 while (*plist != NULL)
5074 plist = &(*plist)->next;
5075
5076 *plist = *msg_list;
5077 }
5078 msg_list = saved_msg_list;
5079
Bram Moolenaar4c137212021-04-19 16:48:48 +02005080 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005081 {
5082 cmdmod.cmod_filter_regmatch.regprog = NULL;
5083 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005084 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005085 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005086 emsg_silent_def = save_emsg_silent_def;
5087 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005088
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005089failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005090 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5092 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005093 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005096 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005097 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005098 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005099 if (ectx.ec_outer_ref->or_outer_allocated)
5100 vim_free(ectx.ec_outer_ref->or_outer);
5101 partial_unref(ectx.ec_outer_ref->or_partial);
5102 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005103 }
5104
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005105 // Not sure if this is necessary.
5106 suppress_errthrow = save_suppress_errthrow;
5107
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005108 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5109 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005110 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005111 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005112 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 return ret;
5114}
5115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005117 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5118 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5119 * "pfx" is prefixed to every line.
5120 */
5121 static void
5122list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5123{
5124 int line_idx = 0;
5125 int prev_current = 0;
5126 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005127 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005128
5129 for (current = 0; current < instr_count; ++current)
5130 {
5131 isn_T *iptr = &instr[current];
5132 char *line;
5133
5134 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005135 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005136 while (line_idx < iptr->isn_lnum
5137 && line_idx < ufunc->uf_lines.ga_len)
5138 {
5139 if (current > prev_current)
5140 {
5141 msg_puts("\n\n");
5142 prev_current = current;
5143 }
5144 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5145 if (line != NULL)
5146 msg(line);
5147 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005148 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5149 {
5150 int first_def_arg = ufunc->uf_args.ga_len
5151 - ufunc->uf_def_args.ga_len;
5152
5153 if (def_arg_idx > 0)
5154 msg_puts("\n\n");
5155 msg_start();
5156 msg_puts(" ");
5157 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5158 first_def_arg + def_arg_idx]);
5159 msg_puts(" = ");
5160 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5161 msg_clr_eos();
5162 msg_end();
5163 }
5164 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005165
5166 switch (iptr->isn_type)
5167 {
5168 case ISN_EXEC:
5169 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5170 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005171 case ISN_EXEC_SPLIT:
5172 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5173 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005174 case ISN_EXECRANGE:
5175 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5176 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005177 case ISN_LEGACY_EVAL:
5178 smsg("%s%4d EVAL legacy %s", pfx, current,
5179 iptr->isn_arg.string);
5180 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005181 case ISN_REDIRSTART:
5182 smsg("%s%4d REDIR", pfx, current);
5183 break;
5184 case ISN_REDIREND:
5185 smsg("%s%4d REDIR END%s", pfx, current,
5186 iptr->isn_arg.number ? " append" : "");
5187 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005188 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005189#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005190 smsg("%s%4d CEXPR pre %s", pfx, current,
5191 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005192#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005193 break;
5194 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005195#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005196 {
5197 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5198
5199 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5200 cexpr_get_auname(cer->cer_cmdidx),
5201 cer->cer_forceit ? "!" : "",
5202 cer->cer_cmdline);
5203 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005204#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005205 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005206 case ISN_INSTR:
5207 {
5208 smsg("%s%4d INSTR", pfx, current);
5209 list_instructions(" ", iptr->isn_arg.instr,
5210 INT_MAX, NULL);
5211 msg(" -------------");
5212 }
5213 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005214 case ISN_SUBSTITUTE:
5215 {
5216 subs_T *subs = &iptr->isn_arg.subs;
5217
5218 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5219 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5220 msg(" -------------");
5221 }
5222 break;
5223 case ISN_EXECCONCAT:
5224 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5225 (varnumber_T)iptr->isn_arg.number);
5226 break;
5227 case ISN_ECHO:
5228 {
5229 echo_T *echo = &iptr->isn_arg.echo;
5230
5231 smsg("%s%4d %s %d", pfx, current,
5232 echo->echo_with_white ? "ECHO" : "ECHON",
5233 echo->echo_count);
5234 }
5235 break;
5236 case ISN_EXECUTE:
5237 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005238 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005239 break;
5240 case ISN_ECHOMSG:
5241 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005242 (varnumber_T)(iptr->isn_arg.number));
5243 break;
5244 case ISN_ECHOCONSOLE:
5245 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5246 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005247 break;
5248 case ISN_ECHOERR:
5249 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005250 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005251 break;
5252 case ISN_LOAD:
5253 {
5254 if (iptr->isn_arg.number < 0)
5255 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5256 (varnumber_T)(iptr->isn_arg.number
5257 + STACK_FRAME_SIZE));
5258 else
5259 smsg("%s%4d LOAD $%lld", pfx, current,
5260 (varnumber_T)(iptr->isn_arg.number));
5261 }
5262 break;
5263 case ISN_LOADOUTER:
5264 {
5265 if (iptr->isn_arg.number < 0)
5266 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5267 iptr->isn_arg.outer.outer_depth,
5268 iptr->isn_arg.outer.outer_idx
5269 + STACK_FRAME_SIZE);
5270 else
5271 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5272 iptr->isn_arg.outer.outer_depth,
5273 iptr->isn_arg.outer.outer_idx);
5274 }
5275 break;
5276 case ISN_LOADV:
5277 smsg("%s%4d LOADV v:%s", pfx, current,
5278 get_vim_var_name(iptr->isn_arg.number));
5279 break;
5280 case ISN_LOADSCRIPT:
5281 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005282 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5283 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5284 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005285
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005286 sv = get_script_svar(sref, -1);
5287 if (sv == NULL)
5288 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5289 pfx, current, si->sn_name);
5290 else
5291 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005292 sv->sv_name,
5293 sref->sref_idx,
5294 si->sn_name);
5295 }
5296 break;
5297 case ISN_LOADS:
5298 {
5299 scriptitem_T *si = SCRIPT_ITEM(
5300 iptr->isn_arg.loadstore.ls_sid);
5301
5302 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5303 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5304 }
5305 break;
5306 case ISN_LOADAUTO:
5307 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5308 break;
5309 case ISN_LOADG:
5310 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5311 break;
5312 case ISN_LOADB:
5313 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5314 break;
5315 case ISN_LOADW:
5316 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5317 break;
5318 case ISN_LOADT:
5319 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5320 break;
5321 case ISN_LOADGDICT:
5322 smsg("%s%4d LOAD g:", pfx, current);
5323 break;
5324 case ISN_LOADBDICT:
5325 smsg("%s%4d LOAD b:", pfx, current);
5326 break;
5327 case ISN_LOADWDICT:
5328 smsg("%s%4d LOAD w:", pfx, current);
5329 break;
5330 case ISN_LOADTDICT:
5331 smsg("%s%4d LOAD t:", pfx, current);
5332 break;
5333 case ISN_LOADOPT:
5334 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5335 break;
5336 case ISN_LOADENV:
5337 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5338 break;
5339 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005340 smsg("%s%4d LOADREG @%c", pfx, current,
5341 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005342 break;
5343
5344 case ISN_STORE:
5345 if (iptr->isn_arg.number < 0)
5346 smsg("%s%4d STORE arg[%lld]", pfx, current,
5347 iptr->isn_arg.number + STACK_FRAME_SIZE);
5348 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005349 smsg("%s%4d STORE $%lld", pfx, current,
5350 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005351 break;
5352 case ISN_STOREOUTER:
5353 {
5354 if (iptr->isn_arg.number < 0)
5355 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5356 iptr->isn_arg.outer.outer_depth,
5357 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5358 else
5359 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5360 iptr->isn_arg.outer.outer_depth,
5361 iptr->isn_arg.outer.outer_idx);
5362 }
5363 break;
5364 case ISN_STOREV:
5365 smsg("%s%4d STOREV v:%s", pfx, current,
5366 get_vim_var_name(iptr->isn_arg.number));
5367 break;
5368 case ISN_STOREAUTO:
5369 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5370 break;
5371 case ISN_STOREG:
5372 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5373 break;
5374 case ISN_STOREB:
5375 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5376 break;
5377 case ISN_STOREW:
5378 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5379 break;
5380 case ISN_STORET:
5381 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5382 break;
5383 case ISN_STORES:
5384 {
5385 scriptitem_T *si = SCRIPT_ITEM(
5386 iptr->isn_arg.loadstore.ls_sid);
5387
5388 smsg("%s%4d STORES %s in %s", pfx, current,
5389 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5390 }
5391 break;
5392 case ISN_STORESCRIPT:
5393 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005394 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5395 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5396 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005397
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005398 sv = get_script_svar(sref, -1);
5399 if (sv == NULL)
5400 smsg("%s%4d STORESCRIPT [deleted] in %s",
5401 pfx, current, si->sn_name);
5402 else
5403 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005404 sv->sv_name,
5405 sref->sref_idx,
5406 si->sn_name);
5407 }
5408 break;
5409 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005410 case ISN_STOREFUNCOPT:
5411 smsg("%s%4d %s &%s", pfx, current,
5412 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005413 iptr->isn_arg.storeopt.so_name);
5414 break;
5415 case ISN_STOREENV:
5416 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5417 break;
5418 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005419 smsg("%s%4d STOREREG @%c", pfx, current,
5420 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005421 break;
5422 case ISN_STORENR:
5423 smsg("%s%4d STORE %lld in $%d", pfx, current,
5424 iptr->isn_arg.storenr.stnr_val,
5425 iptr->isn_arg.storenr.stnr_idx);
5426 break;
5427
5428 case ISN_STOREINDEX:
5429 smsg("%s%4d STOREINDEX %s", pfx, current,
5430 vartype_name(iptr->isn_arg.vartype));
5431 break;
5432
5433 case ISN_STORERANGE:
5434 smsg("%s%4d STORERANGE", pfx, current);
5435 break;
5436
5437 // constants
5438 case ISN_PUSHNR:
5439 smsg("%s%4d PUSHNR %lld", pfx, current,
5440 (varnumber_T)(iptr->isn_arg.number));
5441 break;
5442 case ISN_PUSHBOOL:
5443 case ISN_PUSHSPEC:
5444 smsg("%s%4d PUSH %s", pfx, current,
5445 get_var_special_name(iptr->isn_arg.number));
5446 break;
5447 case ISN_PUSHF:
5448#ifdef FEAT_FLOAT
5449 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5450#endif
5451 break;
5452 case ISN_PUSHS:
5453 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5454 break;
5455 case ISN_PUSHBLOB:
5456 {
5457 char_u *r;
5458 char_u numbuf[NUMBUFLEN];
5459 char_u *tofree;
5460
5461 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5462 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5463 vim_free(tofree);
5464 }
5465 break;
5466 case ISN_PUSHFUNC:
5467 {
5468 char *name = (char *)iptr->isn_arg.string;
5469
5470 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5471 name == NULL ? "[none]" : name);
5472 }
5473 break;
5474 case ISN_PUSHCHANNEL:
5475#ifdef FEAT_JOB_CHANNEL
5476 {
5477 channel_T *channel = iptr->isn_arg.channel;
5478
5479 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5480 channel == NULL ? 0 : channel->ch_id);
5481 }
5482#endif
5483 break;
5484 case ISN_PUSHJOB:
5485#ifdef FEAT_JOB_CHANNEL
5486 {
5487 typval_T tv;
5488 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005489 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005490
5491 tv.v_type = VAR_JOB;
5492 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005493 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005494 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5495 }
5496#endif
5497 break;
5498 case ISN_PUSHEXC:
5499 smsg("%s%4d PUSH v:exception", pfx, current);
5500 break;
5501 case ISN_UNLET:
5502 smsg("%s%4d UNLET%s %s", pfx, current,
5503 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5504 iptr->isn_arg.unlet.ul_name);
5505 break;
5506 case ISN_UNLETENV:
5507 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5508 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5509 iptr->isn_arg.unlet.ul_name);
5510 break;
5511 case ISN_UNLETINDEX:
5512 smsg("%s%4d UNLETINDEX", pfx, current);
5513 break;
5514 case ISN_UNLETRANGE:
5515 smsg("%s%4d UNLETRANGE", pfx, current);
5516 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005517 case ISN_LOCKUNLOCK:
5518 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5519 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005520 case ISN_LOCKCONST:
5521 smsg("%s%4d LOCKCONST", pfx, current);
5522 break;
5523 case ISN_NEWLIST:
5524 smsg("%s%4d NEWLIST size %lld", pfx, current,
5525 (varnumber_T)(iptr->isn_arg.number));
5526 break;
5527 case ISN_NEWDICT:
5528 smsg("%s%4d NEWDICT size %lld", pfx, current,
5529 (varnumber_T)(iptr->isn_arg.number));
5530 break;
5531
5532 // function call
5533 case ISN_BCALL:
5534 {
5535 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5536
5537 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5538 internal_func_name(cbfunc->cbf_idx),
5539 cbfunc->cbf_argcount);
5540 }
5541 break;
5542 case ISN_DCALL:
5543 {
5544 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5545 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5546 + cdfunc->cdf_idx;
5547
5548 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005549 printable_func_name(df->df_ufunc),
5550 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005551 }
5552 break;
5553 case ISN_UCALL:
5554 {
5555 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5556
5557 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5558 cufunc->cuf_name, cufunc->cuf_argcount);
5559 }
5560 break;
5561 case ISN_PCALL:
5562 {
5563 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5564
5565 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5566 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5567 }
5568 break;
5569 case ISN_PCALL_END:
5570 smsg("%s%4d PCALL end", pfx, current);
5571 break;
5572 case ISN_RETURN:
5573 smsg("%s%4d RETURN", pfx, current);
5574 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005575 case ISN_RETURN_VOID:
5576 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005577 break;
5578 case ISN_FUNCREF:
5579 {
5580 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005581 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005582
Bram Moolenaar38453522021-11-28 22:00:12 +00005583 if (funcref->fr_func_name == NULL)
5584 {
5585 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5586 + funcref->fr_dfunc_idx;
5587 name = df->df_ufunc->uf_name;
5588 }
5589 else
5590 name = funcref->fr_func_name;
5591 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005592 }
5593 break;
5594
5595 case ISN_NEWFUNC:
5596 {
5597 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5598
5599 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5600 newfunc->nf_lambda, newfunc->nf_global);
5601 }
5602 break;
5603
5604 case ISN_DEF:
5605 {
5606 char_u *name = iptr->isn_arg.string;
5607
5608 smsg("%s%4d DEF %s", pfx, current,
5609 name == NULL ? (char_u *)"" : name);
5610 }
5611 break;
5612
5613 case ISN_JUMP:
5614 {
5615 char *when = "?";
5616
5617 switch (iptr->isn_arg.jump.jump_when)
5618 {
5619 case JUMP_ALWAYS:
5620 when = "JUMP";
5621 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005622 case JUMP_NEVER:
5623 iemsg("JUMP_NEVER should not be used");
5624 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005625 case JUMP_AND_KEEP_IF_TRUE:
5626 when = "JUMP_AND_KEEP_IF_TRUE";
5627 break;
5628 case JUMP_IF_FALSE:
5629 when = "JUMP_IF_FALSE";
5630 break;
5631 case JUMP_AND_KEEP_IF_FALSE:
5632 when = "JUMP_AND_KEEP_IF_FALSE";
5633 break;
5634 case JUMP_IF_COND_FALSE:
5635 when = "JUMP_IF_COND_FALSE";
5636 break;
5637 case JUMP_IF_COND_TRUE:
5638 when = "JUMP_IF_COND_TRUE";
5639 break;
5640 }
5641 smsg("%s%4d %s -> %d", pfx, current, when,
5642 iptr->isn_arg.jump.jump_where);
5643 }
5644 break;
5645
5646 case ISN_JUMP_IF_ARG_SET:
5647 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5648 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5649 iptr->isn_arg.jump.jump_where);
5650 break;
5651
5652 case ISN_FOR:
5653 {
5654 forloop_T *forloop = &iptr->isn_arg.forloop;
5655
5656 smsg("%s%4d FOR $%d -> %d", pfx, current,
5657 forloop->for_idx, forloop->for_end);
5658 }
5659 break;
5660
5661 case ISN_TRY:
5662 {
5663 try_T *try = &iptr->isn_arg.try;
5664
5665 if (try->try_ref->try_finally == 0)
5666 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5667 pfx, current,
5668 try->try_ref->try_catch,
5669 try->try_ref->try_endtry);
5670 else
5671 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5672 pfx, current,
5673 try->try_ref->try_catch,
5674 try->try_ref->try_finally,
5675 try->try_ref->try_endtry);
5676 }
5677 break;
5678 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005679 smsg("%s%4d CATCH", pfx, current);
5680 break;
5681 case ISN_TRYCONT:
5682 {
5683 trycont_T *trycont = &iptr->isn_arg.trycont;
5684
5685 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5686 trycont->tct_levels,
5687 trycont->tct_levels == 1 ? "" : "s",
5688 trycont->tct_where);
5689 }
5690 break;
5691 case ISN_FINALLY:
5692 smsg("%s%4d FINALLY", pfx, current);
5693 break;
5694 case ISN_ENDTRY:
5695 smsg("%s%4d ENDTRY", pfx, current);
5696 break;
5697 case ISN_THROW:
5698 smsg("%s%4d THROW", pfx, current);
5699 break;
5700
5701 // expression operations on number
5702 case ISN_OPNR:
5703 case ISN_OPFLOAT:
5704 case ISN_OPANY:
5705 {
5706 char *what;
5707 char *ins;
5708
5709 switch (iptr->isn_arg.op.op_type)
5710 {
5711 case EXPR_MULT: what = "*"; break;
5712 case EXPR_DIV: what = "/"; break;
5713 case EXPR_REM: what = "%"; break;
5714 case EXPR_SUB: what = "-"; break;
5715 case EXPR_ADD: what = "+"; break;
5716 default: what = "???"; break;
5717 }
5718 switch (iptr->isn_type)
5719 {
5720 case ISN_OPNR: ins = "OPNR"; break;
5721 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5722 case ISN_OPANY: ins = "OPANY"; break;
5723 default: ins = "???"; break;
5724 }
5725 smsg("%s%4d %s %s", pfx, current, ins, what);
5726 }
5727 break;
5728
5729 case ISN_COMPAREBOOL:
5730 case ISN_COMPARESPECIAL:
5731 case ISN_COMPARENR:
5732 case ISN_COMPAREFLOAT:
5733 case ISN_COMPARESTRING:
5734 case ISN_COMPAREBLOB:
5735 case ISN_COMPARELIST:
5736 case ISN_COMPAREDICT:
5737 case ISN_COMPAREFUNC:
5738 case ISN_COMPAREANY:
5739 {
5740 char *p;
5741 char buf[10];
5742 char *type;
5743
5744 switch (iptr->isn_arg.op.op_type)
5745 {
5746 case EXPR_EQUAL: p = "=="; break;
5747 case EXPR_NEQUAL: p = "!="; break;
5748 case EXPR_GREATER: p = ">"; break;
5749 case EXPR_GEQUAL: p = ">="; break;
5750 case EXPR_SMALLER: p = "<"; break;
5751 case EXPR_SEQUAL: p = "<="; break;
5752 case EXPR_MATCH: p = "=~"; break;
5753 case EXPR_IS: p = "is"; break;
5754 case EXPR_ISNOT: p = "isnot"; break;
5755 case EXPR_NOMATCH: p = "!~"; break;
5756 default: p = "???"; break;
5757 }
5758 STRCPY(buf, p);
5759 if (iptr->isn_arg.op.op_ic == TRUE)
5760 strcat(buf, "?");
5761 switch(iptr->isn_type)
5762 {
5763 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5764 case ISN_COMPARESPECIAL:
5765 type = "COMPARESPECIAL"; break;
5766 case ISN_COMPARENR: type = "COMPARENR"; break;
5767 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5768 case ISN_COMPARESTRING:
5769 type = "COMPARESTRING"; break;
5770 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5771 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5772 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5773 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5774 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5775 default: type = "???"; break;
5776 }
5777
5778 smsg("%s%4d %s %s", pfx, current, type, buf);
5779 }
5780 break;
5781
5782 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5783 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5784
5785 // expression operations
5786 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5787 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5788 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5789 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5790 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5791 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5792 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5793 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5794 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5795 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5796 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5797 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005798 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005799 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5800 iptr->isn_arg.getitem.gi_index,
5801 iptr->isn_arg.getitem.gi_with_op ?
5802 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005803 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5804 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5805 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005806 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5807 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5808
Bram Moolenaar4c137212021-04-19 16:48:48 +02005809 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5810
5811 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5812 case ISN_CHECKTYPE:
5813 {
5814 checktype_T *ct = &iptr->isn_arg.type;
5815 char *tofree;
5816
5817 if (ct->ct_arg_idx == 0)
5818 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5819 type_name(ct->ct_type, &tofree),
5820 (int)ct->ct_off);
5821 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005822 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5823 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005824 type_name(ct->ct_type, &tofree),
5825 (int)ct->ct_off,
5826 (int)ct->ct_arg_idx);
5827 vim_free(tofree);
5828 break;
5829 }
5830 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5831 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5832 iptr->isn_arg.checklen.cl_min_len);
5833 break;
5834 case ISN_SETTYPE:
5835 {
5836 char *tofree;
5837
5838 smsg("%s%4d SETTYPE %s", pfx, current,
5839 type_name(iptr->isn_arg.type.ct_type, &tofree));
5840 vim_free(tofree);
5841 break;
5842 }
5843 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005844 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5845 smsg("%s%4d INVERT %d (!val)", pfx, current,
5846 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005847 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005848 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5849 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005850 break;
5851 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005852 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005854 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5855 pfx, current,
5856 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005857 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005858 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5859 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860 break;
5861 case ISN_PUT:
5862 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5863 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005864 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005865 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5866 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005867 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005868 else
5869 smsg("%s%4d PUT %c %ld", pfx, current,
5870 iptr->isn_arg.put.put_regname,
5871 (long)iptr->isn_arg.put.put_lnum);
5872 break;
5873
Bram Moolenaar4c137212021-04-19 16:48:48 +02005874 case ISN_CMDMOD:
5875 {
5876 char_u *buf;
5877 size_t len = produce_cmdmods(
5878 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5879
5880 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005881 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005882 {
5883 (void)produce_cmdmods(
5884 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5885 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5886 vim_free(buf);
5887 }
5888 break;
5889 }
5890 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5891
5892 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005893 smsg("%s%4d PROFILE START line %d", pfx, current,
5894 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005895 break;
5896
5897 case ISN_PROF_END:
5898 smsg("%s%4d PROFILE END", pfx, current);
5899 break;
5900
Bram Moolenaare99d4222021-06-13 14:01:26 +02005901 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005902 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5903 iptr->isn_arg.debug.dbg_break_lnum + 1,
5904 iptr->isn_lnum,
5905 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005906 break;
5907
Bram Moolenaar4c137212021-04-19 16:48:48 +02005908 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5909 iptr->isn_arg.unpack.unp_count,
5910 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5911 break;
5912 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5913 iptr->isn_arg.shuffle.shfl_item,
5914 iptr->isn_arg.shuffle.shfl_up);
5915 break;
5916 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5917
5918 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5919 return;
5920 }
5921
5922 out_flush(); // output one line at a time
5923 ui_breakcheck();
5924 if (got_int)
5925 break;
5926 }
5927}
5928
5929/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005930 * Handle command line completion for the :disassemble command.
5931 */
5932 void
5933set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5934{
5935 char_u *p;
5936
5937 // Default: expand user functions, "debug" and "profile"
5938 xp->xp_context = EXPAND_DISASSEMBLE;
5939 xp->xp_pattern = arg;
5940
5941 // first argument already typed: only user function names
5942 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5943 {
5944 xp->xp_context = EXPAND_USER_FUNC;
5945 xp->xp_pattern = skipwhite(p);
5946 }
5947}
5948
5949/*
5950 * Function given to ExpandGeneric() to obtain the list of :disassemble
5951 * arguments.
5952 */
5953 char_u *
5954get_disassemble_argument(expand_T *xp, int idx)
5955{
5956 if (idx == 0)
5957 return (char_u *)"debug";
5958 if (idx == 1)
5959 return (char_u *)"profile";
5960 return get_user_func_name(xp, idx - 2);
5961}
5962
5963/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005964 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005965 * We don't really need this at runtime, but we do have tests that require it,
5966 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967 */
5968 void
5969ex_disassemble(exarg_T *eap)
5970{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005971 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005972 char_u *fname;
5973 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 dfunc_T *dfunc;
5975 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005976 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005977 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005978 compiletype_T compile_type = CT_NONE;
5979
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005980 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005981 {
5982 compile_type = CT_PROFILE;
5983 arg = skipwhite(arg + 7);
5984 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005985 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005986 {
5987 compile_type = CT_DEBUG;
5988 arg = skipwhite(arg + 5);
5989 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005991 if (STRNCMP(arg, "<lambda>", 8) == 0)
5992 {
5993 arg += 8;
5994 (void)getdigits(&arg);
5995 fname = vim_strnsave(eap->arg, arg - eap->arg);
5996 }
5997 else
5998 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005999 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006000 if (fname == NULL)
6001 {
6002 semsg(_(e_invarg2), eap->arg);
6003 return;
6004 }
6005
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006006 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006007 if (ufunc == NULL)
6008 {
6009 char_u *p = untrans_function_name(fname);
6010
6011 if (p != NULL)
6012 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006013 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006014 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006015 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 if (ufunc == NULL)
6017 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006018 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019 return;
6020 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006021 if (func_needs_compiling(ufunc, compile_type)
6022 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006023 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006024 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006026 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006027 return;
6028 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006029 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006030
6031 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006032 switch (compile_type)
6033 {
6034 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006035#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006036 instr = dfunc->df_instr_prof;
6037 instr_count = dfunc->df_instr_prof_count;
6038 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006039#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006040 // FALLTHROUGH
6041 case CT_NONE:
6042 instr = dfunc->df_instr;
6043 instr_count = dfunc->df_instr_count;
6044 break;
6045 case CT_DEBUG:
6046 instr = dfunc->df_instr_debug;
6047 instr_count = dfunc->df_instr_debug_count;
6048 break;
6049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050
Bram Moolenaar4c137212021-04-19 16:48:48 +02006051 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052}
6053
6054/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006055 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 * list, etc. Mostly like what JavaScript does, except that empty list and
6057 * empty dictionary are FALSE.
6058 */
6059 int
6060tv2bool(typval_T *tv)
6061{
6062 switch (tv->v_type)
6063 {
6064 case VAR_NUMBER:
6065 return tv->vval.v_number != 0;
6066 case VAR_FLOAT:
6067#ifdef FEAT_FLOAT
6068 return tv->vval.v_float != 0.0;
6069#else
6070 break;
6071#endif
6072 case VAR_PARTIAL:
6073 return tv->vval.v_partial != NULL;
6074 case VAR_FUNC:
6075 case VAR_STRING:
6076 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6077 case VAR_LIST:
6078 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6079 case VAR_DICT:
6080 return tv->vval.v_dict != NULL
6081 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6082 case VAR_BOOL:
6083 case VAR_SPECIAL:
6084 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6085 case VAR_JOB:
6086#ifdef FEAT_JOB_CHANNEL
6087 return tv->vval.v_job != NULL;
6088#else
6089 break;
6090#endif
6091 case VAR_CHANNEL:
6092#ifdef FEAT_JOB_CHANNEL
6093 return tv->vval.v_channel != NULL;
6094#else
6095 break;
6096#endif
6097 case VAR_BLOB:
6098 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6099 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006100 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006101 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006102 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103 break;
6104 }
6105 return FALSE;
6106}
6107
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006108 void
6109emsg_using_string_as(typval_T *tv, int as_number)
6110{
6111 semsg(_(as_number ? e_using_string_as_number_str
6112 : e_using_string_as_bool_str),
6113 tv->vval.v_string == NULL
6114 ? (char_u *)"" : tv->vval.v_string);
6115}
6116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117/*
6118 * If "tv" is a string give an error and return FAIL.
6119 */
6120 int
6121check_not_string(typval_T *tv)
6122{
6123 if (tv->v_type == VAR_STRING)
6124 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006125 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006126 clear_tv(tv);
6127 return FAIL;
6128 }
6129 return OK;
6130}
6131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132
6133#endif // FEAT_EVAL