blob: c1e40a68f48b6958dd7dd98b9a54ce66c08fd70e [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 Moolenaar7e82c5f2021-02-21 21:32:45 +010029 int tcd_catch_idx; // instruction of the first :catch or :finally
30 int tcd_finally_idx; // instruction of the :finally block or zero
31 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010032 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010033 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_return; // when TRUE return from end of :finally
35} trycmd_T;
36
Bram Moolenaar4c137212021-04-19 16:48:48 +020037// Data local to a function.
38// On a function call, if not empty, is saved on the stack and restored when
39// returning.
40typedef struct {
41 int floc_restore_cmdmod;
42 cmdmod_T floc_save_cmdmod;
43 int floc_restore_cmdmod_stacklen;
44} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010045
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020046// Structure to hold a reference to an outer_T, with information of whether it
47// was allocated.
48typedef struct {
49 outer_T *or_outer;
50 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
51 int or_outer_allocated; // free "or_outer" later
52} outer_ref_T;
53
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010054// A stack is used to store:
55// - arguments passed to a :def function
56// - info about the calling function, to use when returning
57// - local variables
58// - temporary values
59//
60// In detail (FP == Frame Pointer):
61// arg1 first argument from caller (if present)
62// arg2 second argument from caller (if present)
63// extra_arg1 any missing optional argument default value
64// FP -> cur_func calling function
65// current previous instruction pointer
66// frame_ptr previous Frame Pointer
67// var1 space for local variable
68// var2 space for local variable
69// .... fixed space for max. number of local variables
70// temp temporary values
71// .... flexible space for temporary values (can grow big)
72
73/*
74 * Execution context.
75 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010076struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020078 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020079 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020081 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020082 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 garray_T ec_trystack; // stack of trycmd_T values
85 int ec_in_catch; // when TRUE in catch or finally block
86
87 int ec_dfunc_idx; // current function index
88 isn_T *ec_instr; // array with instructions
89 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020090
91 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020092
93 int ec_did_emsg_before;
94 int ec_trylevel_at_start;
95 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010096};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097
Bram Moolenaar12d26532021-02-19 19:13:21 +010098#ifdef FEAT_PROFILE
99// stack of profinfo_T used when profiling.
100static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
101#endif
102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200104#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 +0100105
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200106 void
107to_string_error(vartype_T vartype)
108{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200109 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200110}
111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100113 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 */
115 static int
116ufunc_argcount(ufunc_T *ufunc)
117{
118 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100152 * This adds a stack frame and sets the instruction pointer to the start of the
153 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200154 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 *
156 * Stack has:
157 * - current arguments (already there)
158 * - omitted optional argument (default values) added here
159 * - stack frame:
160 * - pointer to calling function
161 * - Index of next instruction in calling function
162 * - previous frame pointer
163 * - reserved space for local variables
164 */
165 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100166call_dfunc(
167 int cdf_idx,
168 partial_T *pt,
169 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100170 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100172 int argcount = argcount_arg;
173 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
174 ufunc_T *ufunc = dfunc->df_ufunc;
175 int arg_to_add;
176 int vararg_count = 0;
177 int varcount;
178 int idx;
179 estack_T *entry;
180 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181
182 if (dfunc->df_deleted)
183 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100184 // don't use ufunc->uf_name, it may have been freed
185 emsg_funcname(e_func_deleted,
186 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187 return FAIL;
188 }
189
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100190#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100191 if (do_profiling == PROF_YES)
192 {
193 if (ga_grow(&profile_info_ga, 1) == OK)
194 {
195 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
196 + profile_info_ga.ga_len;
197 ++profile_info_ga.ga_len;
198 CLEAR_POINTER(info);
199 profile_may_start_func(info, ufunc,
200 (((dfunc_T *)def_functions.ga_data)
201 + ectx->ec_dfunc_idx)->df_ufunc);
202 }
203
204 // Profiling might be enabled/disabled along the way. This should not
205 // fail, since the function was compiled before and toggling profiling
206 // doesn't change any errors.
Bram Moolenaare99d4222021-06-13 14:01:26 +0200207 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
208 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100209 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100210 return FAIL;
211 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100212#endif
213
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200214 if (ufunc->uf_va_name != NULL)
215 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200217 // Stack at time of call with 2 varargs:
218 // normal_arg
219 // optional_arg
220 // vararg_1
221 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200222 // After creating the list:
223 // normal_arg
224 // optional_arg
225 // vararg-list
226 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200227 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200228 // After creating the list
229 // normal_arg
230 // (space for optional_arg)
231 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200232 vararg_count = argcount - ufunc->uf_args.ga_len;
233 if (vararg_count < 0)
234 vararg_count = 0;
235 else
236 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200237 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200238 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200239
240 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200241 }
242
Bram Moolenaarfe270812020-04-11 22:31:27 +0200243 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200244 if (arg_to_add < 0)
245 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200246 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200247 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200248 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200249 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200250 return FAIL;
251 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200252
253 // Reserve space for:
254 // - missing arguments
255 // - stack frame
256 // - local variables
257 // - if needed: a counter for number of closures created in
258 // ectx->ec_funcrefs.
259 varcount = dfunc->df_varcount + dfunc->df_has_closure;
260 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
261 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 return FAIL;
263
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100264 // If depth of calling is getting too high, don't execute the function.
265 if (funcdepth_increment() == FAIL)
266 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200267 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100268
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100269 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200270 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100271 {
272 floc = ALLOC_ONE(funclocal_T);
273 if (floc == NULL)
274 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200275 *floc = ectx->ec_funclocal;
276 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100277 }
278
Bram Moolenaarfe270812020-04-11 22:31:27 +0200279 // Move the vararg-list to below the missing optional arguments.
280 if (vararg_count > 0 && arg_to_add > 0)
281 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100282
283 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200284 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200285 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200286 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287
288 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100289 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
290 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200291 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200292 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
293 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100294 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100295 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200296 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
298 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200299 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200301 if (dfunc->df_has_closure)
302 {
303 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
304
305 tv->v_type = VAR_NUMBER;
306 tv->vval.v_number = 0;
307 }
308 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100310 if (pt != NULL || ufunc->uf_partial != NULL
311 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100312 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200313 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100314
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200315 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100316 return FAIL;
317 if (pt != NULL)
318 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200319 ref->or_outer = &pt->pt_outer;
320 ++pt->pt_refcount;
321 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100322 }
323 else if (ufunc->uf_partial != NULL)
324 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200325 ref->or_outer = &ufunc->uf_partial->pt_outer;
326 ++ufunc->uf_partial->pt_refcount;
327 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100328 }
329 else
330 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200331 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
332 if (ref->or_outer == NULL)
333 {
334 vim_free(ref);
335 return FAIL;
336 }
337 ref->or_outer_allocated = TRUE;
338 ref->or_outer->out_stack = &ectx->ec_stack;
339 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
340 if (ectx->ec_outer_ref != NULL)
341 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100342 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200343 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100344 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100345 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200346 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100347
Bram Moolenaarc970e422021-03-17 15:03:04 +0100348 ++ufunc->uf_calls;
349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350 // Set execution state to the start of the called function.
351 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100352 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100353 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200354 if (entry != NULL)
355 {
356 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200357 // Save the current context so it can be restored on return.
358 entry->es_save_sctx = current_sctx;
359 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200360 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100361
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200362 // Start execution at the first instruction.
363 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364
365 return OK;
366}
367
368// Get pointer to item in the stack.
369#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
370
371/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200372 * Used when returning from a function: Check if any closure is still
373 * referenced. If so then move the arguments and variables to a separate piece
374 * of stack to be used when the closure is called.
375 * When "free_arguments" is TRUE the arguments are to be freed.
376 * Returns FAIL when out of memory.
377 */
378 static int
379handle_closure_in_use(ectx_T *ectx, int free_arguments)
380{
381 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
382 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200383 int argcount;
384 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200385 int idx;
386 typval_T *tv;
387 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388 garray_T *gap = &ectx->ec_funcrefs;
389 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200390
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200391 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200392 return OK; // function was freed
393 if (dfunc->df_has_closure == 0)
394 return OK; // no closures
395 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
396 closure_count = tv->vval.v_number;
397 if (closure_count == 0)
398 return OK; // no funcrefs created
399
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200400 argcount = ufunc_argcount(dfunc->df_ufunc);
401 top = ectx->ec_frame_idx - argcount;
402
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200404 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200405 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200406 partial_T *pt;
407 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200408
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200409 if (off < 0)
410 continue; // count is off or already done
411 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200412 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200413 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200414 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200415 int i;
416
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200417 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200418 // unreferenced on return.
419 for (i = 0; i < dfunc->df_varcount; ++i)
420 {
421 typval_T *stv = STACK_TV(ectx->ec_frame_idx
422 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200423 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200424 --refcount;
425 }
426 if (refcount > 1)
427 {
428 closure_in_use = TRUE;
429 break;
430 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200431 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200432 }
433
434 if (closure_in_use)
435 {
436 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
437 typval_T *stack;
438
439 // A closure is using the arguments and/or local variables.
440 // Move them to the called function.
441 if (funcstack == NULL)
442 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200443 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
444 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200445 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
446 funcstack->fs_ga.ga_data = stack;
447 if (stack == NULL)
448 {
449 vim_free(funcstack);
450 return FAIL;
451 }
452
453 // Move or copy the arguments.
454 for (idx = 0; idx < argcount; ++idx)
455 {
456 tv = STACK_TV(top + idx);
457 if (free_arguments)
458 {
459 *(stack + idx) = *tv;
460 tv->v_type = VAR_UNKNOWN;
461 }
462 else
463 copy_tv(tv, stack + idx);
464 }
465 // Move the local variables.
466 for (idx = 0; idx < dfunc->df_varcount; ++idx)
467 {
468 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200469
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200470 // A partial created for a local function, that is also used as a
471 // local variable, has a reference count for the variable, thus
472 // will never go down to zero. When all these refcounts are one
473 // then the funcstack is unused. We need to count how many we have
474 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200475 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
476 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200477 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200478
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200479 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200480 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
481 gap->ga_len - closure_count + i])
482 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200483 }
484
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200485 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200486 tv->v_type = VAR_UNKNOWN;
487 }
488
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200489 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200491 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
492 - closure_count + idx];
493 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200494 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200495 ++funcstack->fs_refcount;
496 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100497 pt->pt_outer.out_stack = &funcstack->fs_ga;
498 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200499 }
500 }
501 }
502
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200503 for (idx = 0; idx < closure_count; ++idx)
504 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
505 - closure_count + idx]);
506 gap->ga_len -= closure_count;
507 if (gap->ga_len == 0)
508 ga_clear(gap);
509
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200510 return OK;
511}
512
513/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200514 * Called when a partial is freed or its reference count goes down to one. The
515 * funcstack may be the only reference to the partials in the local variables.
516 * Go over all of them, the funcref and can be freed if all partials
517 * referencing the funcstack have a reference count of one.
518 */
519 void
520funcstack_check_refcount(funcstack_T *funcstack)
521{
522 int i;
523 garray_T *gap = &funcstack->fs_ga;
524 int done = 0;
525
526 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
527 return;
528 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
529 {
530 typval_T *tv = ((typval_T *)gap->ga_data) + i;
531
532 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
533 && tv->vval.v_partial->pt_funcstack == funcstack
534 && tv->vval.v_partial->pt_refcount == 1)
535 ++done;
536 }
537 if (done == funcstack->fs_min_refcount)
538 {
539 typval_T *stack = gap->ga_data;
540
541 // All partials referencing the funcstack have a reference count of
542 // one, thus the funcstack is no longer of use.
543 for (i = 0; i < gap->ga_len; ++i)
544 clear_tv(stack + i);
545 vim_free(stack);
546 vim_free(funcstack);
547 }
548}
549
550/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 * Return from the current function.
552 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200553 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200554func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100557 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200558 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
559 + ectx->ec_dfunc_idx;
560 int argcount = ufunc_argcount(dfunc->df_ufunc);
561 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200562 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100563 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
564 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200565 funclocal_T *floc;
566#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100567 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
568 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569
Bram Moolenaar12d26532021-02-19 19:13:21 +0100570 if (do_profiling == PROF_YES)
571 {
572 ufunc_T *caller = prev_dfunc->df_ufunc;
573
574 if (dfunc->df_ufunc->uf_profiling
575 || (caller != NULL && caller->uf_profiling))
576 {
577 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
578 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
579 --profile_info_ga.ga_len;
580 }
581 }
582#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100583 // TODO: when is it safe to delete the function when it is no longer used?
584 --dfunc->df_ufunc->uf_calls;
585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200587 entry = estack_pop();
588 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200589 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200591 if (handle_closure_in_use(ectx, TRUE) == FAIL)
592 return FAIL;
593
594 // Clear the arguments.
595 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
596 clear_tv(STACK_TV(idx));
597
598 // Clear local variables and temp values, but not the return value.
599 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100600 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100602
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100603 // The return value should be on top of the stack. However, when aborting
604 // it may not be there and ec_frame_idx is the top of the stack.
605 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100606 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100607 ret_idx = 0;
608
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200609 if (ectx->ec_outer_ref != NULL)
610 {
611 if (ectx->ec_outer_ref->or_outer_allocated)
612 vim_free(ectx->ec_outer_ref->or_outer);
613 partial_unref(ectx->ec_outer_ref->or_partial);
614 vim_free(ectx->ec_outer_ref);
615 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100616
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100617 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100618 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100619 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
620 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200621 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
622 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200623 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100624 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100625 floc = (void *)STACK_TV(ectx->ec_frame_idx
626 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200627 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100628 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
629 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200630
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100631 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200632 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100633 else
634 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200635 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100636 vim_free(floc);
637 }
638
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100639 if (ret_idx > 0)
640 {
641 // Reset the stack to the position before the call, with a spot for the
642 // return value, moved there from above the frame.
643 ectx->ec_stack.ga_len = top + 1;
644 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
645 }
646 else
647 // Reset the stack to the position before the call.
648 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200649
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100650 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200651 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200652 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653}
654
655#undef STACK_TV
656
657/*
658 * Prepare arguments and rettv for calling a builtin or user function.
659 */
660 static int
661call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
662{
663 int idx;
664 typval_T *tv;
665
666 // Move arguments from bottom of the stack to argvars[] and add terminator.
667 for (idx = 0; idx < argcount; ++idx)
668 argvars[idx] = *STACK_TV_BOT(idx - argcount);
669 argvars[argcount].v_type = VAR_UNKNOWN;
670
671 // Result replaces the arguments on the stack.
672 if (argcount > 0)
673 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200674 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 return FAIL;
676 else
677 ++ectx->ec_stack.ga_len;
678
679 // Default return value is zero.
680 tv = STACK_TV_BOT(-1);
681 tv->v_type = VAR_NUMBER;
682 tv->vval.v_number = 0;
683
684 return OK;
685}
686
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200687// Ugly global to avoid passing the execution context around through many
688// layers.
689static ectx_T *current_ectx = NULL;
690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691/*
692 * Call a builtin function by index.
693 */
694 static int
695call_bfunc(int func_idx, int argcount, ectx_T *ectx)
696{
697 typval_T argvars[MAX_FUNC_ARGS];
698 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100699 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200700 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701
702 if (call_prepare(argcount, argvars, ectx) == FAIL)
703 return FAIL;
704
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200705 // Call the builtin function. Set "current_ectx" so that when it
706 // recursively invokes call_def_function() a closure context can be set.
707 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200709 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710
711 // Clear the arguments.
712 for (idx = 0; idx < argcount; ++idx)
713 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200714
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100715 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200716 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 return OK;
718}
719
720/*
721 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100722 * If the function is compiled this will add a stack frame and set the
723 * instruction pointer at the start of the function.
724 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200725 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100726 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 */
728 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100729call_ufunc(
730 ufunc_T *ufunc,
731 partial_T *pt,
732 int argcount,
733 ectx_T *ectx,
734 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735{
736 typval_T argvars[MAX_FUNC_ARGS];
737 funcexe_T funcexe;
738 int error;
739 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100740 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100741#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +0200742 compiletype_T compile_type = do_profiling == PROF_YES
743 && ufunc->uf_profiling ? CT_PROFILE : CT_NONE;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100744#else
Bram Moolenaare99d4222021-06-13 14:01:26 +0200745# define compile_type CT_NONE
Bram Moolenaarf002a412021-01-24 13:34:18 +0100746#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747
Bram Moolenaare99d4222021-06-13 14:01:26 +0200748 if (func_needs_compiling(ufunc, compile_type)
749 && compile_def_function(ufunc, FALSE, compile_type, NULL)
750 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200751 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200752 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100753 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100754 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100755 if (error != FCERR_UNKNOWN)
756 {
757 if (error == FCERR_TOOMANY)
758 semsg(_(e_toomanyarg), ufunc->uf_name);
759 else
760 semsg(_(e_toofewarg), ufunc->uf_name);
761 return FAIL;
762 }
763
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100764 // The function has been compiled, can call it quickly. For a function
765 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100766 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100767 if (iptr != NULL)
768 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100769 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100770 iptr->isn_type = ISN_DCALL;
771 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
772 iptr->isn_arg.dfunc.cdf_argcount = argcount;
773 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200774 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776
777 if (call_prepare(argcount, argvars, ectx) == FAIL)
778 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200779 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 funcexe.evaluate = TRUE;
781
782 // Call the user function. Result goes in last position on the stack.
783 // TODO: add selfdict if there is one
784 error = call_user_func_check(ufunc, argcount, argvars,
785 STACK_TV_BOT(-1), &funcexe, NULL);
786
787 // Clear the arguments.
788 for (idx = 0; idx < argcount; ++idx)
789 clear_tv(&argvars[idx]);
790
791 if (error != FCERR_NONE)
792 {
793 user_func_error(error, ufunc->uf_name);
794 return FAIL;
795 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100796 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200797 // Error other than from calling the function itself.
798 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 return OK;
800}
801
802/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100803 * If command modifiers were applied restore them.
804 */
805 static void
806may_restore_cmdmod(funclocal_T *funclocal)
807{
808 if (funclocal->floc_restore_cmdmod)
809 {
810 cmdmod.cmod_filter_regmatch.regprog = NULL;
811 undo_cmdmod(&cmdmod);
812 cmdmod = funclocal->floc_save_cmdmod;
813 funclocal->floc_restore_cmdmod = FALSE;
814 }
815}
816
817/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200818 * Return TRUE if an error was given or CTRL-C was pressed.
819 */
820 static int
821vim9_aborting(int prev_called_emsg)
822{
823 return called_emsg > prev_called_emsg || got_int || did_throw;
824}
825
826/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 * Execute a function by "name".
828 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100829 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 * Returns FAIL if not found without an error message.
831 */
832 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100833call_by_name(
834 char_u *name,
835 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100836 ectx_T *ectx,
837 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838{
839 ufunc_T *ufunc;
840
841 if (builtin_function(name, -1))
842 {
843 int func_idx = find_internal_func(name);
844
845 if (func_idx < 0)
846 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200847 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 return FAIL;
849 return call_bfunc(func_idx, argcount, ectx);
850 }
851
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200852 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200853
854 if (ufunc == NULL)
855 {
856 int called_emsg_before = called_emsg;
857
858 if (script_autoload(name, TRUE))
859 // loaded a package, search for the function again
860 ufunc = find_func(name, FALSE, NULL);
861 if (vim9_aborting(called_emsg_before))
862 return FAIL; // bail out if loading the script caused an error
863 }
864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100866 {
867 if (ufunc->uf_arg_types != NULL)
868 {
869 int i;
870 typval_T *argv = STACK_TV_BOT(0) - argcount;
871
872 // The function can change at runtime, check that the argument
873 // types are correct.
874 for (i = 0; i < argcount; ++i)
875 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100876 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100877
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100878 if (i < ufunc->uf_args.ga_len)
879 type = ufunc->uf_arg_types[i];
880 else if (ufunc->uf_va_type != NULL)
881 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100882 if (type != NULL && check_typval_arg_type(type,
883 &argv[i], i + 1) == FAIL)
884 return FAIL;
885 }
886 }
887
Bram Moolenaar4c137212021-04-19 16:48:48 +0200888 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
891 return FAIL;
892}
893
894 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100895call_partial(
896 typval_T *tv,
897 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100898 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200900 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200901 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100903 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904
905 if (tv->v_type == VAR_PARTIAL)
906 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200907 partial_T *pt = tv->vval.v_partial;
908 int i;
909
910 if (pt->pt_argc > 0)
911 {
912 // Make space for arguments from the partial, shift the "argcount"
913 // arguments up.
914 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
915 return FAIL;
916 for (i = 1; i <= argcount; ++i)
917 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
918 ectx->ec_stack.ga_len += pt->pt_argc;
919 argcount += pt->pt_argc;
920
921 // copy the arguments from the partial onto the stack
922 for (i = 0; i < pt->pt_argc; ++i)
923 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925
926 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200927 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929 name = pt->pt_name;
930 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200931 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200933 if (name != NULL)
934 {
935 char_u fname_buf[FLEN_FIXED + 1];
936 char_u *tofree = NULL;
937 int error = FCERR_NONE;
938 char_u *fname;
939
940 // May need to translate <SNR>123_ to K_SNR.
941 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
942 if (error != FCERR_NONE)
943 res = FAIL;
944 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200945 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200946 vim_free(tofree);
947 }
948
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100949 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 {
951 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200952 semsg(_(e_unknownfunc),
953 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954 return FAIL;
955 }
956 return OK;
957}
958
959/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200960 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
961 * TRUE.
962 */
963 static int
964error_if_locked(int lock, char *error)
965{
966 if (lock & (VAR_LOCKED | VAR_FIXED))
967 {
968 emsg(_(error));
969 return TRUE;
970 }
971 return FALSE;
972}
973
974/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100975 * Give an error if "tv" is not a number and return FAIL.
976 */
977 static int
978check_for_number(typval_T *tv)
979{
980 if (tv->v_type != VAR_NUMBER)
981 {
982 semsg(_(e_expected_str_but_got_str),
983 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
984 return FAIL;
985 }
986 return OK;
987}
988
989/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100990 * Store "tv" in variable "name".
991 * This is for s: and g: variables.
992 */
993 static void
994store_var(char_u *name, typval_T *tv)
995{
996 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200997 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100998
Bram Moolenaard877a572021-04-01 19:42:48 +0200999 if (tv->v_lock)
1000 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001001 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001002 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001003 restore_funccal();
1004}
1005
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001006/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001007 * Convert "tv" to a string.
1008 * Return FAIL if not allowed.
1009 */
1010 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001011do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001012{
1013 if (tv->v_type != VAR_STRING)
1014 {
1015 char_u *str;
1016
1017 if (is_2string_any)
1018 {
1019 switch (tv->v_type)
1020 {
1021 case VAR_SPECIAL:
1022 case VAR_BOOL:
1023 case VAR_NUMBER:
1024 case VAR_FLOAT:
1025 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001026
1027 case VAR_LIST:
1028 if (tolerant)
1029 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001030 char_u *s, *e, *p;
1031 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001032
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001033 ga_init2(&ga, sizeof(char_u *), 1);
1034
1035 // Convert to NL separated items, then
1036 // escape the items and replace the NL with
1037 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001038 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001039 if (str == NULL)
1040 return FAIL;
1041 s = str;
1042 while ((e = vim_strchr(s, '\n')) != NULL)
1043 {
1044 *e = NUL;
1045 p = vim_strsave_fnameescape(s, FALSE);
1046 if (p != NULL)
1047 {
1048 ga_concat(&ga, p);
1049 ga_concat(&ga, (char_u *)" ");
1050 vim_free(p);
1051 }
1052 s = e + 1;
1053 }
1054 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001055 clear_tv(tv);
1056 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001057 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001058 return OK;
1059 }
1060 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001061 default: to_string_error(tv->v_type);
1062 return FAIL;
1063 }
1064 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001065 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001066 clear_tv(tv);
1067 tv->v_type = VAR_STRING;
1068 tv->vval.v_string = str;
1069 }
1070 return OK;
1071}
1072
1073/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001074 * When the value of "sv" is a null list of dict, allocate it.
1075 */
1076 static void
1077allocate_if_null(typval_T *tv)
1078{
1079 switch (tv->v_type)
1080 {
1081 case VAR_LIST:
1082 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001083 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001084 break;
1085 case VAR_DICT:
1086 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001087 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001088 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001089 case VAR_BLOB:
1090 if (tv->vval.v_blob == NULL)
1091 (void)rettv_blob_alloc(tv);
1092 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001093 default:
1094 break;
1095 }
1096}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001097
Bram Moolenaare7525c52021-01-09 13:20:37 +01001098/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001099 * Return the character "str[index]" where "index" is the character index,
1100 * including composing characters.
1101 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001102 */
1103 char_u *
1104char_from_string(char_u *str, varnumber_T index)
1105{
1106 size_t nbyte = 0;
1107 varnumber_T nchar = index;
1108 size_t slen;
1109
1110 if (str == NULL)
1111 return NULL;
1112 slen = STRLEN(str);
1113
Bram Moolenaarff871402021-03-26 13:34:05 +01001114 // Do the same as for a list: a negative index counts from the end.
1115 // Optimization to check the first byte to be below 0x80 (and no composing
1116 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001117 if (index < 0)
1118 {
1119 int clen = 0;
1120
1121 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001122 {
1123 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1124 ++nbyte;
1125 else if (enc_utf8)
1126 nbyte += utfc_ptr2len(str + nbyte);
1127 else
1128 nbyte += mb_ptr2len(str + nbyte);
1129 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001130 nchar = clen + index;
1131 if (nchar < 0)
1132 // unlike list: index out of range results in empty string
1133 return NULL;
1134 }
1135
1136 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001137 {
1138 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1139 ++nbyte;
1140 else if (enc_utf8)
1141 nbyte += utfc_ptr2len(str + nbyte);
1142 else
1143 nbyte += mb_ptr2len(str + nbyte);
1144 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001145 if (nbyte >= slen)
1146 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001147 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001148}
1149
1150/*
1151 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001152 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001153 * If going over the end return "str_len".
1154 * If "idx" is negative count from the end, -1 is the last character.
1155 * When going over the start return -1.
1156 */
1157 static long
1158char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1159{
1160 varnumber_T nchar = idx;
1161 size_t nbyte = 0;
1162
1163 if (nchar >= 0)
1164 {
1165 while (nchar > 0 && nbyte < str_len)
1166 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001167 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001168 --nchar;
1169 }
1170 }
1171 else
1172 {
1173 nbyte = str_len;
1174 while (nchar < 0 && nbyte > 0)
1175 {
1176 --nbyte;
1177 nbyte -= mb_head_off(str, str + nbyte);
1178 ++nchar;
1179 }
1180 if (nchar < 0)
1181 return -1;
1182 }
1183 return (long)nbyte;
1184}
1185
1186/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001187 * Return the slice "str[first : last]" using character indexes. Composing
1188 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001189 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001190 * Return NULL when the result is empty.
1191 */
1192 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001193string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001194{
1195 long start_byte, end_byte;
1196 size_t slen;
1197
1198 if (str == NULL)
1199 return NULL;
1200 slen = STRLEN(str);
1201 start_byte = char_idx2byte(str, slen, first);
1202 if (start_byte < 0)
1203 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001204 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001205 end_byte = (long)slen;
1206 else
1207 {
1208 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001209 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001210 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001211 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001212 }
1213
1214 if (start_byte >= (long)slen || end_byte <= start_byte)
1215 return NULL;
1216 return vim_strnsave(str + start_byte, end_byte - start_byte);
1217}
1218
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001219 static svar_T *
1220get_script_svar(scriptref_T *sref, ectx_T *ectx)
1221{
1222 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1223 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1224 + ectx->ec_dfunc_idx;
1225 svar_T *sv;
1226
1227 if (sref->sref_seq != si->sn_script_seq)
1228 {
1229 // The script was reloaded after the function was
1230 // compiled, the script_idx may not be valid.
1231 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1232 dfunc->df_ufunc->uf_name_exp);
1233 return NULL;
1234 }
1235 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1236 if (!equal_type(sv->sv_type, sref->sref_type))
1237 {
1238 emsg(_(e_script_variable_type_changed));
1239 return NULL;
1240 }
1241 return sv;
1242}
1243
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001244/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001245 * Function passed to do_cmdline() for splitting a script joined by NL
1246 * characters.
1247 */
1248 static char_u *
1249get_split_sourceline(
1250 int c UNUSED,
1251 void *cookie,
1252 int indent UNUSED,
1253 getline_opt_T options UNUSED)
1254{
1255 source_cookie_T *sp = (source_cookie_T *)cookie;
1256 char_u *p;
1257 char_u *line;
1258
1259 if (*sp->nextline == NUL)
1260 return NULL;
1261 p = vim_strchr(sp->nextline, '\n');
1262 if (p == NULL)
1263 {
1264 line = vim_strsave(sp->nextline);
1265 sp->nextline += STRLEN(sp->nextline);
1266 }
1267 else
1268 {
1269 line = vim_strnsave(sp->nextline, p - sp->nextline);
1270 sp->nextline = p + 1;
1271 }
1272 return line;
1273}
1274
1275/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 * Execute a function by "name".
1277 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001278 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 */
1280 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001281call_eval_func(
1282 char_u *name,
1283 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001284 ectx_T *ectx,
1285 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286{
Bram Moolenaared677f52020-08-12 16:38:10 +02001287 int called_emsg_before = called_emsg;
1288 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289
Bram Moolenaar4c137212021-04-19 16:48:48 +02001290 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001291 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001293 dictitem_T *v;
1294
1295 v = find_var(name, NULL, FALSE);
1296 if (v == NULL)
1297 {
1298 semsg(_(e_unknownfunc), name);
1299 return FAIL;
1300 }
1301 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1302 {
1303 semsg(_(e_unknownfunc), name);
1304 return FAIL;
1305 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001306 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001308 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309}
1310
1311/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001312 * When a function reference is used, fill a partial with the information
1313 * needed, especially when it is used as a closure.
1314 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001315 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001316fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1317{
1318 pt->pt_func = ufunc;
1319 pt->pt_refcount = 1;
1320
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001321 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001322 {
1323 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1324 + ectx->ec_dfunc_idx;
1325
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001326 // The closure may need to find arguments and local variables in the
1327 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001328 pt->pt_outer.out_stack = &ectx->ec_stack;
1329 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001330 if (ectx->ec_outer_ref != NULL)
1331 {
1332 // The current context already has a context, link to that one.
1333 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1334 if (ectx->ec_outer_ref->or_partial != NULL)
1335 {
1336 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1337 ++pt->pt_outer.out_up_partial->pt_refcount;
1338 }
1339 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001340
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001341 // If this function returns and the closure is still being used, we
1342 // need to make a copy of the context (arguments and local variables).
1343 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001344 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1345 {
1346 vim_free(pt);
1347 return FAIL;
1348 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001349 // Extra variable keeps the count of closures created in the current
1350 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001351 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1352 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1353
1354 ((partial_T **)ectx->ec_funcrefs.ga_data)
1355 [ectx->ec_funcrefs.ga_len] = pt;
1356 ++pt->pt_refcount;
1357 ++ectx->ec_funcrefs.ga_len;
1358 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001359 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001360 return OK;
1361}
1362
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001363// used for v_instr of typval of VAR_INSTR
1364struct instr_S {
1365 ectx_T *instr_ectx;
1366 isn_T *instr_instr;
1367};
1368
Bram Moolenaar4c137212021-04-19 16:48:48 +02001369// used for substitute_instr
1370typedef struct subs_expr_S {
1371 ectx_T *subs_ectx;
1372 isn_T *subs_instr;
1373 int subs_status;
1374} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375
1376// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001377#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378
1379// Get pointer to item at the bottom of the stack, -1 is the bottom.
1380#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001381#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 +01001382
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001383// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001384#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 +01001385
Bram Moolenaar4c137212021-04-19 16:48:48 +02001386/*
1387 * Execute instructions in execution context "ectx".
1388 * Return OK or FAIL;
1389 */
1390 static int
1391exec_instructions(ectx_T *ectx)
1392{
1393 int breakcheck_count = 0;
1394 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001395 int ret = FAIL;
1396 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001397
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001398 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001399 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001400
Bram Moolenaarff652882021-05-16 15:24:49 +02001401 // Only catch exceptions in this instruction list.
1402 ectx->ec_trylevel_at_start = trylevel;
1403
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 for (;;)
1405 {
1406 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001407
Bram Moolenaar270d0382020-05-15 21:42:53 +02001408 if (++breakcheck_count >= 100)
1409 {
1410 line_breakcheck();
1411 breakcheck_count = 0;
1412 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001413 if (got_int)
1414 {
1415 // Turn CTRL-C into an exception.
1416 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001417 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001418 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001419 did_throw = TRUE;
1420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421
Bram Moolenaara26b9702020-04-18 19:53:28 +02001422 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1423 {
1424 // Turn an error message into an exception.
1425 did_emsg = FALSE;
1426 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001427 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001428 did_throw = TRUE;
1429 *msg_list = NULL;
1430 }
1431
Bram Moolenaar4c137212021-04-19 16:48:48 +02001432 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001434 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001435 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436
1437 // An exception jumps to the first catch, finally, or returns from
1438 // the current function.
1439 if (trystack->ga_len > 0)
1440 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001441 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 {
1443 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001444 ectx->ec_in_catch = TRUE;
1445 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 }
1447 else
1448 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001449 // Not inside try or need to return from current functions.
1450 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001451 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001452 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001453 tv = STACK_TV_BOT(0);
1454 tv->v_type = VAR_NUMBER;
1455 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001456 ++ectx->ec_stack.ga_len;
1457 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001459 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001460 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001461 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001462 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 goto done;
1464 }
1465
Bram Moolenaar4c137212021-04-19 16:48:48 +02001466 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001467 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 }
1469 continue;
1470 }
1471
Bram Moolenaar4c137212021-04-19 16:48:48 +02001472 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 switch (iptr->isn_type)
1474 {
1475 // execute Ex command line
1476 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001477 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001478 source_cookie_T cookie;
1479
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001480 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001481 // Pass getsourceline to get an error for a missing ":end"
1482 // command.
1483 CLEAR_FIELD(cookie);
1484 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1485 if (do_cmdline(iptr->isn_arg.string,
1486 getsourceline, &cookie,
1487 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1488 == FAIL
1489 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001490 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 break;
1493
Bram Moolenaar20677332021-06-06 17:02:53 +02001494 // execute Ex command line split at NL characters.
1495 case ISN_EXEC_SPLIT:
1496 {
1497 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001498 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001499
1500 SOURCING_LNUM = iptr->isn_lnum;
1501 CLEAR_FIELD(cookie);
1502 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1503 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001504 line = get_split_sourceline(0, &cookie, 0, 0);
1505 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001506 get_split_sourceline, &cookie,
1507 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1508 == FAIL
1509 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001510 {
1511 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001512 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001513 }
1514 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001515 }
1516 break;
1517
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001518 // Evaluate an expression with legacy syntax, push it onto the
1519 // stack.
1520 case ISN_LEGACY_EVAL:
1521 {
1522 char_u *arg = iptr->isn_arg.string;
1523 int res;
1524 int save_flags = cmdmod.cmod_flags;
1525
1526 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001527 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001528 tv = STACK_TV_BOT(0);
1529 init_tv(tv);
1530 cmdmod.cmod_flags |= CMOD_LEGACY;
1531 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1532 cmdmod.cmod_flags = save_flags;
1533 if (res == FAIL)
1534 goto on_error;
1535 ++ectx->ec_stack.ga_len;
1536 }
1537 break;
1538
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001539 // push typeval VAR_INSTR with instructions to be executed
1540 case ISN_INSTR:
1541 {
1542 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001543 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001544 tv = STACK_TV_BOT(0);
1545 tv->vval.v_instr = ALLOC_ONE(instr_T);
1546 if (tv->vval.v_instr == NULL)
1547 goto on_error;
1548 ++ectx->ec_stack.ga_len;
1549
1550 tv->v_type = VAR_INSTR;
1551 tv->vval.v_instr->instr_ectx = ectx;
1552 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1553 }
1554 break;
1555
Bram Moolenaar4c137212021-04-19 16:48:48 +02001556 // execute :substitute with an expression
1557 case ISN_SUBSTITUTE:
1558 {
1559 subs_T *subs = &iptr->isn_arg.subs;
1560 source_cookie_T cookie;
1561 struct subs_expr_S *save_instr = substitute_instr;
1562 struct subs_expr_S subs_instr;
1563 int res;
1564
1565 subs_instr.subs_ectx = ectx;
1566 subs_instr.subs_instr = subs->subs_instr;
1567 subs_instr.subs_status = OK;
1568 substitute_instr = &subs_instr;
1569
1570 SOURCING_LNUM = iptr->isn_lnum;
1571 // This is very much like ISN_EXEC
1572 CLEAR_FIELD(cookie);
1573 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1574 res = do_cmdline(subs->subs_cmd,
1575 getsourceline, &cookie,
1576 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1577 substitute_instr = save_instr;
1578
1579 if (res == FAIL || did_emsg
1580 || subs_instr.subs_status == FAIL)
1581 goto on_error;
1582 }
1583 break;
1584
1585 case ISN_FINISH:
1586 goto done;
1587
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001588 case ISN_REDIRSTART:
1589 // create a dummy entry for var_redir_str()
1590 if (alloc_redir_lval() == FAIL)
1591 goto on_error;
1592
1593 // The output is stored in growarray "redir_ga" until
1594 // redirection ends.
1595 init_redir_ga();
1596 redir_vname = 1;
1597 break;
1598
1599 case ISN_REDIREND:
1600 {
1601 char_u *res = get_clear_redir_ga();
1602
1603 // End redirection, put redirected text on the stack.
1604 clear_redir_lval();
1605 redir_vname = 0;
1606
1607 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1608 {
1609 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001610 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001611 }
1612 tv = STACK_TV_BOT(0);
1613 tv->v_type = VAR_STRING;
1614 tv->vval.v_string = res;
1615 ++ectx->ec_stack.ga_len;
1616 }
1617 break;
1618
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001619 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001620#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001621 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1622 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001623#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001624 break;
1625
1626 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001627#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001628 {
1629 exarg_T ea;
1630 int res;
1631
1632 CLEAR_FIELD(ea);
1633 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1634 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1635 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1636 --ectx->ec_stack.ga_len;
1637 tv = STACK_TV_BOT(0);
1638 res = cexpr_core(&ea, tv);
1639 clear_tv(tv);
1640 if (res == FAIL)
1641 goto on_error;
1642 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001643#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001644 break;
1645
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001646 // execute Ex command from pieces on the stack
1647 case ISN_EXECCONCAT:
1648 {
1649 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001650 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001651 int pass;
1652 int i;
1653 char_u *cmd = NULL;
1654 char_u *str;
1655
1656 for (pass = 1; pass <= 2; ++pass)
1657 {
1658 for (i = 0; i < count; ++i)
1659 {
1660 tv = STACK_TV_BOT(i - count);
1661 str = tv->vval.v_string;
1662 if (str != NULL && *str != NUL)
1663 {
1664 if (pass == 2)
1665 STRCPY(cmd + len, str);
1666 len += STRLEN(str);
1667 }
1668 if (pass == 2)
1669 clear_tv(tv);
1670 }
1671 if (pass == 1)
1672 {
1673 cmd = alloc(len + 1);
1674 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001675 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001676 len = 0;
1677 }
1678 }
1679
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001680 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001681 do_cmdline_cmd(cmd);
1682 vim_free(cmd);
1683 }
1684 break;
1685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 // execute :echo {string} ...
1687 case ISN_ECHO:
1688 {
1689 int count = iptr->isn_arg.echo.echo_count;
1690 int atstart = TRUE;
1691 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001692 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693
1694 for (idx = 0; idx < count; ++idx)
1695 {
1696 tv = STACK_TV_BOT(idx - count);
1697 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1698 &atstart, &needclr);
1699 clear_tv(tv);
1700 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001701 if (needclr)
1702 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001703 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 }
1705 break;
1706
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001707 // :execute {string} ...
1708 // :echomsg {string} ...
1709 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001710 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001711 case ISN_ECHOMSG:
1712 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001713 {
1714 int count = iptr->isn_arg.number;
1715 garray_T ga;
1716 char_u buf[NUMBUFLEN];
1717 char_u *p;
1718 int len;
1719 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001720 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001721
1722 ga_init2(&ga, 1, 80);
1723 for (idx = 0; idx < count; ++idx)
1724 {
1725 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001726 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001727 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001728 if (tv->v_type == VAR_CHANNEL
1729 || tv->v_type == VAR_JOB)
1730 {
1731 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001732 semsg(_(e_using_invalid_value_as_string_str),
1733 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001734 break;
1735 }
1736 else
1737 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001738 }
1739 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001740 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001741
1742 len = (int)STRLEN(p);
1743 if (ga_grow(&ga, len + 2) == FAIL)
1744 failed = TRUE;
1745 else
1746 {
1747 if (ga.ga_len > 0)
1748 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1749 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1750 ga.ga_len += len;
1751 }
1752 clear_tv(tv);
1753 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001754 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001755 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001756 {
1757 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001758 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001759 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001760
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001761 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001762 {
1763 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001764 {
1765 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001766 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001767 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001768 {
1769 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001770 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001771 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001772 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001773 else
1774 {
1775 msg_sb_eol();
1776 if (iptr->isn_type == ISN_ECHOMSG)
1777 {
1778 msg_attr(ga.ga_data, echo_attr);
1779 out_flush();
1780 }
1781 else
1782 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001783 SOURCING_LNUM = iptr->isn_lnum;
1784 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001785 }
1786 }
1787 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001788 ga_clear(&ga);
1789 }
1790 break;
1791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 // load local variable or argument
1793 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001794 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001795 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001797 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 break;
1799
1800 // load v: variable
1801 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001802 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001803 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001805 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 break;
1807
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001808 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 case ISN_LOADSCRIPT:
1810 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001811 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 svar_T *sv;
1813
Bram Moolenaar4c137212021-04-19 16:48:48 +02001814 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001815 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001816 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001817 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001818 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001819 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001821 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 }
1823 break;
1824
1825 // load s: variable in old script
1826 case ISN_LOADS:
1827 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001828 hashtab_T *ht = &SCRIPT_VARS(
1829 iptr->isn_arg.loadstore.ls_sid);
1830 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001832
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 if (di == NULL)
1834 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001836 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001837 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 }
1839 else
1840 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001841 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001842 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001844 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 }
1846 }
1847 break;
1848
Bram Moolenaard3aac292020-04-19 14:32:17 +02001849 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001851 case ISN_LOADB:
1852 case ISN_LOADW:
1853 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001855 dictitem_T *di = NULL;
1856 hashtab_T *ht = NULL;
1857 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001858
Bram Moolenaard3aac292020-04-19 14:32:17 +02001859 switch (iptr->isn_type)
1860 {
1861 case ISN_LOADG:
1862 ht = get_globvar_ht();
1863 namespace = 'g';
1864 break;
1865 case ISN_LOADB:
1866 ht = &curbuf->b_vars->dv_hashtab;
1867 namespace = 'b';
1868 break;
1869 case ISN_LOADW:
1870 ht = &curwin->w_vars->dv_hashtab;
1871 namespace = 'w';
1872 break;
1873 case ISN_LOADT:
1874 ht = &curtab->tp_vars->dv_hashtab;
1875 namespace = 't';
1876 break;
1877 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001878 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001879 }
1880 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 if (di == NULL)
1883 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001884 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001885 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001886 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001887 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 }
1889 else
1890 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001891 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001892 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001894 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 }
1896 }
1897 break;
1898
Bram Moolenaar03290b82020-12-19 16:30:44 +01001899 // load autoload variable
1900 case ISN_LOADAUTO:
1901 {
1902 char_u *name = iptr->isn_arg.string;
1903
Bram Moolenaar4c137212021-04-19 16:48:48 +02001904 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001905 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001906 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001907 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001908 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001909 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001910 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001911 }
1912 break;
1913
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001914 // load g:/b:/w:/t: namespace
1915 case ISN_LOADGDICT:
1916 case ISN_LOADBDICT:
1917 case ISN_LOADWDICT:
1918 case ISN_LOADTDICT:
1919 {
1920 dict_T *d = NULL;
1921
1922 switch (iptr->isn_type)
1923 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001924 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1925 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1926 case ISN_LOADWDICT: d = curwin->w_vars; break;
1927 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001928 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001929 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001930 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001931 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001932 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001933 tv = STACK_TV_BOT(0);
1934 tv->v_type = VAR_DICT;
1935 tv->v_lock = 0;
1936 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001937 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001938 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001939 }
1940 break;
1941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 // load &option
1943 case ISN_LOADOPT:
1944 {
1945 typval_T optval;
1946 char_u *name = iptr->isn_arg.string;
1947
Bram Moolenaara8c17702020-04-01 21:17:24 +02001948 // This is not expected to fail, name is checked during
1949 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001950 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001951 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001952 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001953 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001955 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 }
1957 break;
1958
1959 // load $ENV
1960 case ISN_LOADENV:
1961 {
1962 typval_T optval;
1963 char_u *name = iptr->isn_arg.string;
1964
Bram Moolenaar4c137212021-04-19 16:48:48 +02001965 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001966 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001967 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001968 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001970 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 }
1972 break;
1973
1974 // load @register
1975 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001976 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001977 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 tv = STACK_TV_BOT(0);
1979 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001980 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001981 // This may result in NULL, which should be equivalent to an
1982 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 tv->vval.v_string = get_reg_contents(
1984 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001985 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 break;
1987
1988 // store local variable
1989 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001990 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 tv = STACK_TV_VAR(iptr->isn_arg.number);
1992 clear_tv(tv);
1993 *tv = *STACK_TV_BOT(0);
1994 break;
1995
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001996 // store s: variable in old script
1997 case ISN_STORES:
1998 {
1999 hashtab_T *ht = &SCRIPT_VARS(
2000 iptr->isn_arg.loadstore.ls_sid);
2001 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002002 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002003
Bram Moolenaar4c137212021-04-19 16:48:48 +02002004 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002005 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002006 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002007 else
2008 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002009 SOURCING_LNUM = iptr->isn_lnum;
2010 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002011 {
2012 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002013 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002014 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002015 clear_tv(&di->di_tv);
2016 di->di_tv = *STACK_TV_BOT(0);
2017 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002018 }
2019 break;
2020
2021 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 case ISN_STORESCRIPT:
2023 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002024 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2025 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026
Bram Moolenaar4c137212021-04-19 16:48:48 +02002027 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002028 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002029 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002030 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002031
2032 // "const" and "final" are checked at compile time, locking
2033 // the value needs to be checked here.
2034 SOURCING_LNUM = iptr->isn_lnum;
2035 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002036 {
2037 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002038 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002039 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 clear_tv(sv->sv_tv);
2042 *sv->sv_tv = *STACK_TV_BOT(0);
2043 }
2044 break;
2045
2046 // store option
2047 case ISN_STOREOPT:
2048 {
2049 long n = 0;
2050 char_u *s = NULL;
2051 char *msg;
2052
Bram Moolenaar4c137212021-04-19 16:48:48 +02002053 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 tv = STACK_TV_BOT(0);
2055 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002056 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002058 if (s == NULL)
2059 s = (char_u *)"";
2060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002062 // must be VAR_NUMBER, CHECKTYPE makes sure
2063 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2065 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002066 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 if (msg != NULL)
2068 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002069 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002071 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 }
2074 break;
2075
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002076 // store $ENV
2077 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002078 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002079 tv = STACK_TV_BOT(0);
2080 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2081 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002082 break;
2083
2084 // store @r
2085 case ISN_STOREREG:
2086 {
2087 int reg = iptr->isn_arg.number;
2088
Bram Moolenaar4c137212021-04-19 16:48:48 +02002089 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002090 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002091 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002092 tv_get_string(tv), -1, FALSE);
2093 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002094 }
2095 break;
2096
2097 // store v: variable
2098 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002099 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002100 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2101 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002102 // should not happen, type is checked when compiling
2103 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002104 break;
2105
Bram Moolenaard3aac292020-04-19 14:32:17 +02002106 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002108 case ISN_STOREB:
2109 case ISN_STOREW:
2110 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002112 dictitem_T *di;
2113 hashtab_T *ht;
2114 char_u *name = iptr->isn_arg.string + 2;
2115
Bram Moolenaard3aac292020-04-19 14:32:17 +02002116 switch (iptr->isn_type)
2117 {
2118 case ISN_STOREG:
2119 ht = get_globvar_ht();
2120 break;
2121 case ISN_STOREB:
2122 ht = &curbuf->b_vars->dv_hashtab;
2123 break;
2124 case ISN_STOREW:
2125 ht = &curwin->w_vars->dv_hashtab;
2126 break;
2127 case ISN_STORET:
2128 ht = &curtab->tp_vars->dv_hashtab;
2129 break;
2130 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002131 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133
Bram Moolenaar4c137212021-04-19 16:48:48 +02002134 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002135 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002137 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 else
2139 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002140 SOURCING_LNUM = iptr->isn_lnum;
2141 if (var_check_permission(di, name) == FAIL)
2142 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 clear_tv(&di->di_tv);
2144 di->di_tv = *STACK_TV_BOT(0);
2145 }
2146 }
2147 break;
2148
Bram Moolenaar03290b82020-12-19 16:30:44 +01002149 // store an autoload variable
2150 case ISN_STOREAUTO:
2151 SOURCING_LNUM = iptr->isn_lnum;
2152 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2153 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002154 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002155 break;
2156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 // store number in local variable
2158 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002159 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 clear_tv(tv);
2161 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002162 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 break;
2164
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002165 // store value in list or dict variable
2166 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002167 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002168 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002169 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002170 typval_T *tv_dest = STACK_TV_BOT(-1);
2171 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002172
Bram Moolenaar752fc692021-01-04 21:57:11 +01002173 // Stack contains:
2174 // -3 value to be stored
2175 // -2 index
2176 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002177 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002178 SOURCING_LNUM = iptr->isn_lnum;
2179 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002180 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002181 dest_type = tv_dest->v_type;
2182 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002183 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002184 else if (dest_type == VAR_LIST
2185 && tv_idx->v_type != VAR_NUMBER)
2186 {
2187 emsg(_(e_number_exp));
2188 status = FAIL;
2189 }
2190 }
2191 else if (dest_type != tv_dest->v_type)
2192 {
2193 // just in case, should be OK
2194 semsg(_(e_expected_str_but_got_str),
2195 vartype_name(dest_type),
2196 vartype_name(tv_dest->v_type));
2197 status = FAIL;
2198 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002199
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002200 if (status == OK && dest_type == VAR_LIST)
2201 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002202 long lidx = (long)tv_idx->vval.v_number;
2203 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002204
2205 if (list == NULL)
2206 {
2207 emsg(_(e_list_not_set));
2208 goto on_error;
2209 }
2210 if (lidx < 0 && list->lv_len + lidx >= 0)
2211 // negative index is relative to the end
2212 lidx = list->lv_len + lidx;
2213 if (lidx < 0 || lidx > list->lv_len)
2214 {
2215 semsg(_(e_listidx), lidx);
2216 goto on_error;
2217 }
2218 if (lidx < list->lv_len)
2219 {
2220 listitem_T *li = list_find(list, lidx);
2221
2222 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002223 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002224 goto on_error;
2225 // overwrite existing list item
2226 clear_tv(&li->li_tv);
2227 li->li_tv = *tv;
2228 }
2229 else
2230 {
2231 if (error_if_locked(list->lv_lock,
2232 e_cannot_change_list))
2233 goto on_error;
2234 // append to list, only fails when out of memory
2235 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002236 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002237 clear_tv(tv);
2238 }
2239 }
2240 else if (status == OK && dest_type == VAR_DICT)
2241 {
2242 char_u *key = tv_idx->vval.v_string;
2243 dict_T *dict = tv_dest->vval.v_dict;
2244 dictitem_T *di;
2245
2246 SOURCING_LNUM = iptr->isn_lnum;
2247 if (dict == NULL)
2248 {
2249 emsg(_(e_dictionary_not_set));
2250 goto on_error;
2251 }
2252 if (key == NULL)
2253 key = (char_u *)"";
2254 di = dict_find(dict, key, -1);
2255 if (di != NULL)
2256 {
2257 if (error_if_locked(di->di_tv.v_lock,
2258 e_cannot_change_dict_item))
2259 goto on_error;
2260 // overwrite existing value
2261 clear_tv(&di->di_tv);
2262 di->di_tv = *tv;
2263 }
2264 else
2265 {
2266 if (error_if_locked(dict->dv_lock,
2267 e_cannot_change_dict))
2268 goto on_error;
2269 // add to dict, only fails when out of memory
2270 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002271 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002272 clear_tv(tv);
2273 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002274 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002275 else if (status == OK && dest_type == VAR_BLOB)
2276 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002277 long lidx = (long)tv_idx->vval.v_number;
2278 blob_T *blob = tv_dest->vval.v_blob;
2279 varnumber_T nr;
2280 int error = FALSE;
2281 int len;
2282
2283 if (blob == NULL)
2284 {
2285 emsg(_(e_blob_not_set));
2286 goto on_error;
2287 }
2288 len = blob_len(blob);
2289 if (lidx < 0 && len + lidx >= 0)
2290 // negative index is relative to the end
2291 lidx = len + lidx;
2292
2293 // Can add one byte at the end.
2294 if (lidx < 0 || lidx > len)
2295 {
2296 semsg(_(e_blobidx), lidx);
2297 goto on_error;
2298 }
2299 if (value_check_lock(blob->bv_lock,
2300 (char_u *)"blob", FALSE))
2301 goto on_error;
2302 nr = tv_get_number_chk(tv, &error);
2303 if (error)
2304 goto on_error;
2305 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002306 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002307 else
2308 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002309 status = FAIL;
2310 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002311 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002312
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002313 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002314 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002315 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002316 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002317 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002318 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002319 goto on_error;
2320 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002321 }
2322 break;
2323
Bram Moolenaar68452172021-04-12 21:21:02 +02002324 // store value in blob range
2325 case ISN_STORERANGE:
2326 {
2327 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2328 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2329 typval_T *tv_dest = STACK_TV_BOT(-1);
2330 int status = OK;
2331
2332 // Stack contains:
2333 // -4 value to be stored
2334 // -3 first index or "none"
2335 // -2 second index or "none"
2336 // -1 destination blob
2337 tv = STACK_TV_BOT(-4);
2338 if (tv_dest->v_type != VAR_BLOB)
2339 {
2340 status = FAIL;
2341 emsg(_(e_blob_required));
2342 }
2343 else
2344 {
2345 varnumber_T n1;
2346 varnumber_T n2;
2347 int error = FALSE;
2348
2349 n1 = tv_get_number_chk(tv_idx1, &error);
2350 if (error)
2351 status = FAIL;
2352 else
2353 {
2354 if (tv_idx2->v_type == VAR_SPECIAL
2355 && tv_idx2->vval.v_number == VVAL_NONE)
2356 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2357 else
2358 n2 = tv_get_number_chk(tv_idx2, &error);
2359 if (error)
2360 status = FAIL;
2361 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002362 {
2363 long bloblen = blob_len(tv_dest->vval.v_blob);
2364
2365 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002366 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002367 || check_blob_range(bloblen,
2368 n1, n2, FALSE) == FAIL)
2369 status = FAIL;
2370 else
2371 status = blob_set_range(
2372 tv_dest->vval.v_blob, n1, n2, tv);
2373 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002374 }
2375 }
2376
2377 clear_tv(tv_idx1);
2378 clear_tv(tv_idx2);
2379 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002380 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002381 clear_tv(tv);
2382
2383 if (status == FAIL)
2384 goto on_error;
2385 }
2386 break;
2387
Bram Moolenaar0186e582021-01-10 18:33:11 +01002388 // load or store variable or argument from outer scope
2389 case ISN_LOADOUTER:
2390 case ISN_STOREOUTER:
2391 {
2392 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002393 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2394 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002395
2396 while (depth > 1 && outer != NULL)
2397 {
2398 outer = outer->out_up;
2399 --depth;
2400 }
2401 if (outer == NULL)
2402 {
2403 SOURCING_LNUM = iptr->isn_lnum;
2404 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002405 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002406 }
2407 tv = ((typval_T *)outer->out_stack->ga_data)
2408 + outer->out_frame_idx + STACK_FRAME_SIZE
2409 + iptr->isn_arg.outer.outer_idx;
2410 if (iptr->isn_type == ISN_LOADOUTER)
2411 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002412 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002413 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002414 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002415 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002416 }
2417 else
2418 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002419 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002420 clear_tv(tv);
2421 *tv = *STACK_TV_BOT(0);
2422 }
2423 }
2424 break;
2425
Bram Moolenaar752fc692021-01-04 21:57:11 +01002426 // unlet item in list or dict variable
2427 case ISN_UNLETINDEX:
2428 {
2429 typval_T *tv_idx = STACK_TV_BOT(-2);
2430 typval_T *tv_dest = STACK_TV_BOT(-1);
2431 int status = OK;
2432
2433 // Stack contains:
2434 // -2 index
2435 // -1 dict or list
2436 if (tv_dest->v_type == VAR_DICT)
2437 {
2438 // unlet a dict item, index must be a string
2439 if (tv_idx->v_type != VAR_STRING)
2440 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002441 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002442 semsg(_(e_expected_str_but_got_str),
2443 vartype_name(VAR_STRING),
2444 vartype_name(tv_idx->v_type));
2445 status = FAIL;
2446 }
2447 else
2448 {
2449 dict_T *d = tv_dest->vval.v_dict;
2450 char_u *key = tv_idx->vval.v_string;
2451 dictitem_T *di = NULL;
2452
2453 if (key == NULL)
2454 key = (char_u *)"";
2455 if (d != NULL)
2456 di = dict_find(d, key, (int)STRLEN(key));
2457 if (di == NULL)
2458 {
2459 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002460 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002461 semsg(_(e_dictkey), key);
2462 status = FAIL;
2463 }
2464 else
2465 {
2466 // TODO: check for dict or item locked
2467 dictitem_remove(d, di);
2468 }
2469 }
2470 }
2471 else if (tv_dest->v_type == VAR_LIST)
2472 {
2473 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002474 SOURCING_LNUM = iptr->isn_lnum;
2475 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002476 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002477 status = FAIL;
2478 }
2479 else
2480 {
2481 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002482 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002483 listitem_T *li = NULL;
2484
2485 li = list_find(l, n);
2486 if (li == NULL)
2487 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002488 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002489 semsg(_(e_listidx), n);
2490 status = FAIL;
2491 }
2492 else
2493 // TODO: check for list or item locked
2494 listitem_remove(l, li);
2495 }
2496 }
2497 else
2498 {
2499 status = FAIL;
2500 semsg(_(e_cannot_index_str),
2501 vartype_name(tv_dest->v_type));
2502 }
2503
2504 clear_tv(tv_idx);
2505 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002506 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002507 if (status == FAIL)
2508 goto on_error;
2509 }
2510 break;
2511
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002512 // unlet range of items in list variable
2513 case ISN_UNLETRANGE:
2514 {
2515 // Stack contains:
2516 // -3 index1
2517 // -2 index2
2518 // -1 dict or list
2519 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2520 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2521 typval_T *tv_dest = STACK_TV_BOT(-1);
2522 int status = OK;
2523
2524 if (tv_dest->v_type == VAR_LIST)
2525 {
2526 // indexes must be a number
2527 SOURCING_LNUM = iptr->isn_lnum;
2528 if (check_for_number(tv_idx1) == FAIL
2529 || check_for_number(tv_idx2) == FAIL)
2530 {
2531 status = FAIL;
2532 }
2533 else
2534 {
2535 list_T *l = tv_dest->vval.v_list;
2536 long n1 = (long)tv_idx1->vval.v_number;
2537 long n2 = (long)tv_idx2->vval.v_number;
2538 listitem_T *li;
2539
2540 li = list_find_index(l, &n1);
2541 if (li == NULL
2542 || list_unlet_range(l, li, NULL, n1,
2543 TRUE, n2) == FAIL)
2544 status = FAIL;
2545 }
2546 }
2547 else
2548 {
2549 status = FAIL;
2550 SOURCING_LNUM = iptr->isn_lnum;
2551 semsg(_(e_cannot_index_str),
2552 vartype_name(tv_dest->v_type));
2553 }
2554
2555 clear_tv(tv_idx1);
2556 clear_tv(tv_idx2);
2557 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002558 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002559 if (status == FAIL)
2560 goto on_error;
2561 }
2562 break;
2563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 // push constant
2565 case ISN_PUSHNR:
2566 case ISN_PUSHBOOL:
2567 case ISN_PUSHSPEC:
2568 case ISN_PUSHF:
2569 case ISN_PUSHS:
2570 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002571 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002572 case ISN_PUSHCHANNEL:
2573 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002574 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002575 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002577 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002578 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 switch (iptr->isn_type)
2580 {
2581 case ISN_PUSHNR:
2582 tv->v_type = VAR_NUMBER;
2583 tv->vval.v_number = iptr->isn_arg.number;
2584 break;
2585 case ISN_PUSHBOOL:
2586 tv->v_type = VAR_BOOL;
2587 tv->vval.v_number = iptr->isn_arg.number;
2588 break;
2589 case ISN_PUSHSPEC:
2590 tv->v_type = VAR_SPECIAL;
2591 tv->vval.v_number = iptr->isn_arg.number;
2592 break;
2593#ifdef FEAT_FLOAT
2594 case ISN_PUSHF:
2595 tv->v_type = VAR_FLOAT;
2596 tv->vval.v_float = iptr->isn_arg.fnumber;
2597 break;
2598#endif
2599 case ISN_PUSHBLOB:
2600 blob_copy(iptr->isn_arg.blob, tv);
2601 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002602 case ISN_PUSHFUNC:
2603 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002604 if (iptr->isn_arg.string == NULL)
2605 tv->vval.v_string = NULL;
2606 else
2607 tv->vval.v_string =
2608 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002609 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002610 case ISN_PUSHCHANNEL:
2611#ifdef FEAT_JOB_CHANNEL
2612 tv->v_type = VAR_CHANNEL;
2613 tv->vval.v_channel = iptr->isn_arg.channel;
2614 if (tv->vval.v_channel != NULL)
2615 ++tv->vval.v_channel->ch_refcount;
2616#endif
2617 break;
2618 case ISN_PUSHJOB:
2619#ifdef FEAT_JOB_CHANNEL
2620 tv->v_type = VAR_JOB;
2621 tv->vval.v_job = iptr->isn_arg.job;
2622 if (tv->vval.v_job != NULL)
2623 ++tv->vval.v_job->jv_refcount;
2624#endif
2625 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 default:
2627 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002628 tv->vval.v_string = vim_strsave(
2629 iptr->isn_arg.string == NULL
2630 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 }
2632 break;
2633
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002634 case ISN_UNLET:
2635 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2636 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002637 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002638 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002639 case ISN_UNLETENV:
2640 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2641 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002642
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002643 case ISN_LOCKCONST:
2644 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2645 break;
2646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 // create a list from items on the stack; uses a single allocation
2648 // for the list header and the items
2649 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002650 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002651 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 break;
2653
2654 // create a dict from items on the stack
2655 case ISN_NEWDICT:
2656 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002657 int count = iptr->isn_arg.number;
2658 dict_T *dict = dict_alloc();
2659 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002660 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002661 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662
2663 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002664 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 for (idx = 0; idx < count; ++idx)
2666 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002667 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002669 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002670 key = tv->vval.v_string == NULL
2671 ? (char_u *)"" : tv->vval.v_string;
2672 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002673 if (item != NULL)
2674 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002675 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002676 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002677 dict_unref(dict);
2678 goto on_error;
2679 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002680 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 clear_tv(tv);
2682 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002683 {
2684 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002685 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2688 item->di_tv.v_lock = 0;
2689 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002690 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002691 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002692 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002693 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 }
2696
2697 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002698 ectx->ec_stack.ga_len -= 2 * count - 1;
2699 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002700 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002702 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 tv = STACK_TV_BOT(-1);
2704 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002705 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 tv->vval.v_dict = dict;
2707 ++dict->dv_refcount;
2708 }
2709 break;
2710
2711 // call a :def function
2712 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002713 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002714 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2715 NULL,
2716 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002717 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002718 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 break;
2720
2721 // call a builtin function
2722 case ISN_BCALL:
2723 SOURCING_LNUM = iptr->isn_lnum;
2724 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2725 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002726 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002727 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 break;
2729
2730 // call a funcref or partial
2731 case ISN_PCALL:
2732 {
2733 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2734 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002735 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736
2737 SOURCING_LNUM = iptr->isn_lnum;
2738 if (pfunc->cpf_top)
2739 {
2740 // funcref is above the arguments
2741 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2742 }
2743 else
2744 {
2745 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002746 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002747 partial_tv = *STACK_TV_BOT(0);
2748 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002750 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002751 if (tv == &partial_tv)
2752 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002754 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 }
2756 break;
2757
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002758 case ISN_PCALL_END:
2759 // PCALL finished, arguments have been consumed and replaced by
2760 // the return value. Now clear the funcref from the stack,
2761 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002762 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002763 clear_tv(STACK_TV_BOT(-1));
2764 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2765 break;
2766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 // call a user defined function or funcref/partial
2768 case ISN_UCALL:
2769 {
2770 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2771
2772 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002773 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002774 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002775 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 }
2777 break;
2778
2779 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002780 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002781 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002782 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002783 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002784 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002785 tv->v_type = VAR_NUMBER;
2786 tv->vval.v_number = 0;
2787 tv->v_lock = 0;
2788 // FALLTHROUGH
2789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 case ISN_RETURN:
2791 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002792 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002793 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002794
2795 if (trystack->ga_len > 0)
2796 trycmd = ((trycmd_T *)trystack->ga_data)
2797 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002798 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002799 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002801 // jump to ":finally" or ":endtry"
2802 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002803 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002804 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002805 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 trycmd->tcd_return = TRUE;
2807 }
2808 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002809 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 }
2811 break;
2812
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002813 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 case ISN_FUNCREF:
2815 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002816 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2817 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2818 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002821 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002822 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002823 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002824 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002825 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002826 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002827 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002828 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002829 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002831 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 tv->vval.v_partial = pt;
2833 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002834 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 }
2836 break;
2837
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002838 // Create a global function from a lambda.
2839 case ISN_NEWFUNC:
2840 {
2841 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2842
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002843 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002844 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002845 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002846 }
2847 break;
2848
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002849 // List functions
2850 case ISN_DEF:
2851 if (iptr->isn_arg.string == NULL)
2852 list_functions(NULL);
2853 else
2854 {
2855 exarg_T ea;
2856
2857 CLEAR_FIELD(ea);
2858 ea.cmd = ea.arg = iptr->isn_arg.string;
2859 define_function(&ea, NULL);
2860 }
2861 break;
2862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 // jump if a condition is met
2864 case ISN_JUMP:
2865 {
2866 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002867 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 int jump = TRUE;
2869
2870 if (when != JUMP_ALWAYS)
2871 {
2872 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002873 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002874 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002875 || when == JUMP_IF_COND_TRUE)
2876 {
2877 SOURCING_LNUM = iptr->isn_lnum;
2878 jump = tv_get_bool_chk(tv, &error);
2879 if (error)
2880 goto on_error;
2881 }
2882 else
2883 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002885 || when == JUMP_AND_KEEP_IF_FALSE
2886 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002888 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 {
2890 // drop the value from the stack
2891 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002892 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 }
2894 }
2895 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002896 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 }
2898 break;
2899
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002900 // Jump if an argument with a default value was already set and not
2901 // v:none.
2902 case ISN_JUMP_IF_ARG_SET:
2903 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2904 if (tv->v_type != VAR_UNKNOWN
2905 && !(tv->v_type == VAR_SPECIAL
2906 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002907 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002908 break;
2909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 // top of a for loop
2911 case ISN_FOR:
2912 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002913 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 typval_T *idxtv =
2915 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2916
Bram Moolenaar4c137212021-04-19 16:48:48 +02002917 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002918 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002919 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002920 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002921 list_T *list = ltv->vval.v_list;
2922
2923 // push the next item from the list
2924 ++idxtv->vval.v_number;
2925 if (list == NULL
2926 || idxtv->vval.v_number >= list->lv_len)
2927 {
2928 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002929 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2930 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002931 }
2932 else if (list->lv_first == &range_list_item)
2933 {
2934 // non-materialized range() list
2935 tv = STACK_TV_BOT(0);
2936 tv->v_type = VAR_NUMBER;
2937 tv->v_lock = 0;
2938 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002940 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002941 }
2942 else
2943 {
2944 listitem_T *li = list_find(list,
2945 idxtv->vval.v_number);
2946
2947 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002948 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002949 }
2950 }
2951 else if (ltv->v_type == VAR_STRING)
2952 {
2953 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002954
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002955 // The index is for the last byte of the previous
2956 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002957 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002958 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002959 {
2960 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002961 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2962 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002963 }
2964 else
2965 {
2966 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2967
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002968 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002969 tv = STACK_TV_BOT(0);
2970 tv->v_type = VAR_STRING;
2971 tv->vval.v_string = vim_strnsave(
2972 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002973 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002974 idxtv->vval.v_number += clen - 1;
2975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002977 else if (ltv->v_type == VAR_BLOB)
2978 {
2979 blob_T *blob = ltv->vval.v_blob;
2980
2981 // When we get here the first time make a copy of the
2982 // blob, so that the iteration still works when it is
2983 // changed.
2984 if (idxtv->vval.v_number == -1 && blob != NULL)
2985 {
2986 blob_copy(blob, ltv);
2987 blob_unref(blob);
2988 blob = ltv->vval.v_blob;
2989 }
2990
2991 // The index is for the previous byte.
2992 ++idxtv->vval.v_number;
2993 if (blob == NULL
2994 || idxtv->vval.v_number >= blob_len(blob))
2995 {
2996 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002997 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2998 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002999 }
3000 else
3001 {
3002 // Push the next byte from the blob.
3003 tv = STACK_TV_BOT(0);
3004 tv->v_type = VAR_NUMBER;
3005 tv->vval.v_number = blob_get(blob,
3006 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003007 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003008 }
3009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 else
3011 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003012 semsg(_(e_for_loop_on_str_not_supported),
3013 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003014 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 }
3016 }
3017 break;
3018
3019 // start of ":try" block
3020 case ISN_TRY:
3021 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003022 trycmd_T *trycmd = NULL;
3023
Bram Moolenaar4c137212021-04-19 16:48:48 +02003024 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003025 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003026 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3027 + ectx->ec_trystack.ga_len;
3028 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003030 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003031 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3032 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003033 trycmd->tcd_catch_idx =
3034 iptr->isn_arg.try.try_ref->try_catch;
3035 trycmd->tcd_finally_idx =
3036 iptr->isn_arg.try.try_ref->try_finally;
3037 trycmd->tcd_endtry_idx =
3038 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 }
3040 break;
3041
3042 case ISN_PUSHEXC:
3043 if (current_exception == NULL)
3044 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003045 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003047 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003049 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003050 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003052 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003054 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 tv->vval.v_string = vim_strsave(
3056 (char_u *)current_exception->value);
3057 break;
3058
3059 case ISN_CATCH:
3060 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003061 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062
Bram Moolenaar4c137212021-04-19 16:48:48 +02003063 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 if (trystack->ga_len > 0)
3065 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003066 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 + trystack->ga_len - 1;
3068 trycmd->tcd_caught = TRUE;
3069 }
3070 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003071 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 catch_exception(current_exception);
3073 }
3074 break;
3075
Bram Moolenaarc150c092021-02-13 15:02:46 +01003076 case ISN_TRYCONT:
3077 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003078 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003079 trycont_T *trycont = &iptr->isn_arg.trycont;
3080 int i;
3081 trycmd_T *trycmd;
3082 int iidx = trycont->tct_where;
3083
3084 if (trystack->ga_len < trycont->tct_levels)
3085 {
3086 siemsg("TRYCONT: expected %d levels, found %d",
3087 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003088 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003089 }
3090 // Make :endtry jump to any outer try block and the last
3091 // :endtry inside the loop to the loop start.
3092 for (i = trycont->tct_levels; i > 0; --i)
3093 {
3094 trycmd = ((trycmd_T *)trystack->ga_data)
3095 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003096 // Add one to tcd_cont to be able to jump to
3097 // instruction with index zero.
3098 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003099 iidx = trycmd->tcd_finally_idx == 0
3100 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003101 }
3102 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003103 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003104 }
3105 break;
3106
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003107 case ISN_FINALLY:
3108 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003109 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003110 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3111 + trystack->ga_len - 1;
3112
3113 // Reset the index to avoid a return statement jumps here
3114 // again.
3115 trycmd->tcd_finally_idx = 0;
3116 break;
3117 }
3118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 // end of ":try" block
3120 case ISN_ENDTRY:
3121 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003122 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123
3124 if (trystack->ga_len > 0)
3125 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003126 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 --trystack->ga_len;
3129 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 trycmd = ((trycmd_T *)trystack->ga_data)
3132 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003133 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 {
3135 // discard the exception
3136 if (caught_stack == current_exception)
3137 caught_stack = caught_stack->caught;
3138 discard_current_exception();
3139 }
3140
3141 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003142 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003143
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003145 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003146 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003147 clear_tv(STACK_TV_BOT(0));
3148 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003149 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003150 // handling :continue: jump to outer try block or
3151 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003152 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 }
3154 }
3155 break;
3156
3157 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003158 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003159 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003160
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003161 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3162 {
3163 // throwing an exception while using "silent!" causes
3164 // the function to abort but not display an error.
3165 tv = STACK_TV_BOT(-1);
3166 clear_tv(tv);
3167 tv->v_type = VAR_NUMBER;
3168 tv->vval.v_number = 0;
3169 goto done;
3170 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003171 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003172 tv = STACK_TV_BOT(0);
3173 if (tv->vval.v_string == NULL
3174 || *skipwhite(tv->vval.v_string) == NUL)
3175 {
3176 vim_free(tv->vval.v_string);
3177 SOURCING_LNUM = iptr->isn_lnum;
3178 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003179 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003180 }
3181
3182 // Inside a "catch" we need to first discard the caught
3183 // exception.
3184 if (trystack->ga_len > 0)
3185 {
3186 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3187 + trystack->ga_len - 1;
3188 if (trycmd->tcd_caught && current_exception != NULL)
3189 {
3190 // discard the exception
3191 if (caught_stack == current_exception)
3192 caught_stack = caught_stack->caught;
3193 discard_current_exception();
3194 trycmd->tcd_caught = FALSE;
3195 }
3196 }
3197
3198 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3199 == FAIL)
3200 {
3201 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003202 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003203 }
3204 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 break;
3207
3208 // compare with special values
3209 case ISN_COMPAREBOOL:
3210 case ISN_COMPARESPECIAL:
3211 {
3212 typval_T *tv1 = STACK_TV_BOT(-2);
3213 typval_T *tv2 = STACK_TV_BOT(-1);
3214 varnumber_T arg1 = tv1->vval.v_number;
3215 varnumber_T arg2 = tv2->vval.v_number;
3216 int res;
3217
3218 switch (iptr->isn_arg.op.op_type)
3219 {
3220 case EXPR_EQUAL: res = arg1 == arg2; break;
3221 case EXPR_NEQUAL: res = arg1 != arg2; break;
3222 default: res = 0; break;
3223 }
3224
Bram Moolenaar4c137212021-04-19 16:48:48 +02003225 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 tv1->v_type = VAR_BOOL;
3227 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3228 }
3229 break;
3230
3231 // Operation with two number arguments
3232 case ISN_OPNR:
3233 case ISN_COMPARENR:
3234 {
3235 typval_T *tv1 = STACK_TV_BOT(-2);
3236 typval_T *tv2 = STACK_TV_BOT(-1);
3237 varnumber_T arg1 = tv1->vval.v_number;
3238 varnumber_T arg2 = tv2->vval.v_number;
3239 varnumber_T res;
3240
3241 switch (iptr->isn_arg.op.op_type)
3242 {
3243 case EXPR_MULT: res = arg1 * arg2; break;
3244 case EXPR_DIV: res = arg1 / arg2; break;
3245 case EXPR_REM: res = arg1 % arg2; break;
3246 case EXPR_SUB: res = arg1 - arg2; break;
3247 case EXPR_ADD: res = arg1 + arg2; break;
3248
3249 case EXPR_EQUAL: res = arg1 == arg2; break;
3250 case EXPR_NEQUAL: res = arg1 != arg2; break;
3251 case EXPR_GREATER: res = arg1 > arg2; break;
3252 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3253 case EXPR_SMALLER: res = arg1 < arg2; break;
3254 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3255 default: res = 0; break;
3256 }
3257
Bram Moolenaar4c137212021-04-19 16:48:48 +02003258 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 if (iptr->isn_type == ISN_COMPARENR)
3260 {
3261 tv1->v_type = VAR_BOOL;
3262 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3263 }
3264 else
3265 tv1->vval.v_number = res;
3266 }
3267 break;
3268
3269 // Computation with two float arguments
3270 case ISN_OPFLOAT:
3271 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003272#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 {
3274 typval_T *tv1 = STACK_TV_BOT(-2);
3275 typval_T *tv2 = STACK_TV_BOT(-1);
3276 float_T arg1 = tv1->vval.v_float;
3277 float_T arg2 = tv2->vval.v_float;
3278 float_T res = 0;
3279 int cmp = FALSE;
3280
3281 switch (iptr->isn_arg.op.op_type)
3282 {
3283 case EXPR_MULT: res = arg1 * arg2; break;
3284 case EXPR_DIV: res = arg1 / arg2; break;
3285 case EXPR_SUB: res = arg1 - arg2; break;
3286 case EXPR_ADD: res = arg1 + arg2; break;
3287
3288 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3289 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3290 case EXPR_GREATER: cmp = arg1 > arg2; break;
3291 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3292 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3293 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3294 default: cmp = 0; break;
3295 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003296 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 if (iptr->isn_type == ISN_COMPAREFLOAT)
3298 {
3299 tv1->v_type = VAR_BOOL;
3300 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3301 }
3302 else
3303 tv1->vval.v_float = res;
3304 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003305#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 break;
3307
3308 case ISN_COMPARELIST:
3309 {
3310 typval_T *tv1 = STACK_TV_BOT(-2);
3311 typval_T *tv2 = STACK_TV_BOT(-1);
3312 list_T *arg1 = tv1->vval.v_list;
3313 list_T *arg2 = tv2->vval.v_list;
3314 int cmp = FALSE;
3315 int ic = iptr->isn_arg.op.op_ic;
3316
3317 switch (iptr->isn_arg.op.op_type)
3318 {
3319 case EXPR_EQUAL: cmp =
3320 list_equal(arg1, arg2, ic, FALSE); break;
3321 case EXPR_NEQUAL: cmp =
3322 !list_equal(arg1, arg2, ic, FALSE); break;
3323 case EXPR_IS: cmp = arg1 == arg2; break;
3324 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3325 default: cmp = 0; break;
3326 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003327 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 clear_tv(tv1);
3329 clear_tv(tv2);
3330 tv1->v_type = VAR_BOOL;
3331 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3332 }
3333 break;
3334
3335 case ISN_COMPAREBLOB:
3336 {
3337 typval_T *tv1 = STACK_TV_BOT(-2);
3338 typval_T *tv2 = STACK_TV_BOT(-1);
3339 blob_T *arg1 = tv1->vval.v_blob;
3340 blob_T *arg2 = tv2->vval.v_blob;
3341 int cmp = FALSE;
3342
3343 switch (iptr->isn_arg.op.op_type)
3344 {
3345 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3346 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3347 case EXPR_IS: cmp = arg1 == arg2; break;
3348 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3349 default: cmp = 0; break;
3350 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003351 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 clear_tv(tv1);
3353 clear_tv(tv2);
3354 tv1->v_type = VAR_BOOL;
3355 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3356 }
3357 break;
3358
3359 // TODO: handle separately
3360 case ISN_COMPARESTRING:
3361 case ISN_COMPAREDICT:
3362 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 case ISN_COMPAREANY:
3364 {
3365 typval_T *tv1 = STACK_TV_BOT(-2);
3366 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003367 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 int ic = iptr->isn_arg.op.op_ic;
3369
Bram Moolenaareb26f432020-09-14 16:50:05 +02003370 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003371 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003373 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 }
3375 break;
3376
3377 case ISN_ADDLIST:
3378 case ISN_ADDBLOB:
3379 {
3380 typval_T *tv1 = STACK_TV_BOT(-2);
3381 typval_T *tv2 = STACK_TV_BOT(-1);
3382
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003383 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 if (iptr->isn_type == ISN_ADDLIST)
3385 eval_addlist(tv1, tv2);
3386 else
3387 eval_addblob(tv1, tv2);
3388 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003389 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 }
3391 break;
3392
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003393 case ISN_LISTAPPEND:
3394 {
3395 typval_T *tv1 = STACK_TV_BOT(-2);
3396 typval_T *tv2 = STACK_TV_BOT(-1);
3397 list_T *l = tv1->vval.v_list;
3398
3399 // add an item to a list
3400 if (l == NULL)
3401 {
3402 SOURCING_LNUM = iptr->isn_lnum;
3403 emsg(_(e_cannot_add_to_null_list));
3404 goto on_error;
3405 }
3406 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003407 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003408 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003409 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003410 }
3411 break;
3412
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003413 case ISN_BLOBAPPEND:
3414 {
3415 typval_T *tv1 = STACK_TV_BOT(-2);
3416 typval_T *tv2 = STACK_TV_BOT(-1);
3417 blob_T *b = tv1->vval.v_blob;
3418 int error = FALSE;
3419 varnumber_T n;
3420
3421 // add a number to a blob
3422 if (b == NULL)
3423 {
3424 SOURCING_LNUM = iptr->isn_lnum;
3425 emsg(_(e_cannot_add_to_null_blob));
3426 goto on_error;
3427 }
3428 n = tv_get_number_chk(tv2, &error);
3429 if (error)
3430 goto on_error;
3431 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003432 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003433 }
3434 break;
3435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 // Computation with two arguments of unknown type
3437 case ISN_OPANY:
3438 {
3439 typval_T *tv1 = STACK_TV_BOT(-2);
3440 typval_T *tv2 = STACK_TV_BOT(-1);
3441 varnumber_T n1, n2;
3442#ifdef FEAT_FLOAT
3443 float_T f1 = 0, f2 = 0;
3444#endif
3445 int error = FALSE;
3446
3447 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3448 {
3449 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3450 {
3451 eval_addlist(tv1, tv2);
3452 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003453 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 break;
3455 }
3456 else if (tv1->v_type == VAR_BLOB
3457 && tv2->v_type == VAR_BLOB)
3458 {
3459 eval_addblob(tv1, tv2);
3460 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003461 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 break;
3463 }
3464 }
3465#ifdef FEAT_FLOAT
3466 if (tv1->v_type == VAR_FLOAT)
3467 {
3468 f1 = tv1->vval.v_float;
3469 n1 = 0;
3470 }
3471 else
3472#endif
3473 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003474 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 n1 = tv_get_number_chk(tv1, &error);
3476 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003477 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478#ifdef FEAT_FLOAT
3479 if (tv2->v_type == VAR_FLOAT)
3480 f1 = n1;
3481#endif
3482 }
3483#ifdef FEAT_FLOAT
3484 if (tv2->v_type == VAR_FLOAT)
3485 {
3486 f2 = tv2->vval.v_float;
3487 n2 = 0;
3488 }
3489 else
3490#endif
3491 {
3492 n2 = tv_get_number_chk(tv2, &error);
3493 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003494 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495#ifdef FEAT_FLOAT
3496 if (tv1->v_type == VAR_FLOAT)
3497 f2 = n2;
3498#endif
3499 }
3500#ifdef FEAT_FLOAT
3501 // if there is a float on either side the result is a float
3502 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3503 {
3504 switch (iptr->isn_arg.op.op_type)
3505 {
3506 case EXPR_MULT: f1 = f1 * f2; break;
3507 case EXPR_DIV: f1 = f1 / f2; break;
3508 case EXPR_SUB: f1 = f1 - f2; break;
3509 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003510 default: SOURCING_LNUM = iptr->isn_lnum;
3511 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003512 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 }
3514 clear_tv(tv1);
3515 clear_tv(tv2);
3516 tv1->v_type = VAR_FLOAT;
3517 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003518 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 }
3520 else
3521#endif
3522 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003523 int failed = FALSE;
3524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 switch (iptr->isn_arg.op.op_type)
3526 {
3527 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003528 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3529 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003530 goto on_error;
3531 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 case EXPR_SUB: n1 = n1 - n2; break;
3533 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003534 default: n1 = num_modulus(n1, n2, &failed);
3535 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003536 goto on_error;
3537 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 }
3539 clear_tv(tv1);
3540 clear_tv(tv2);
3541 tv1->v_type = VAR_NUMBER;
3542 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003543 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 }
3545 }
3546 break;
3547
3548 case ISN_CONCAT:
3549 {
3550 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3551 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3552 char_u *res;
3553
3554 res = concat_str(str1, str2);
3555 clear_tv(STACK_TV_BOT(-2));
3556 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003557 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 STACK_TV_BOT(-1)->vval.v_string = res;
3559 }
3560 break;
3561
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003562 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003563 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003564 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003565 int is_slice = iptr->isn_type == ISN_STRSLICE;
3566 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003567 char_u *res;
3568
3569 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003570 // string slice: string is at stack-3, first index at
3571 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003572 if (is_slice)
3573 {
3574 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003575 n1 = tv->vval.v_number;
3576 }
3577
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003578 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003579 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003580
Bram Moolenaar4c137212021-04-19 16:48:48 +02003581 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003582 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003583 if (is_slice)
3584 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003585 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003586 else
3587 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003588 // single character (including composing characters).
3589 // If the index is too big or negative the result is
3590 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003591 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003592 vim_free(tv->vval.v_string);
3593 tv->vval.v_string = res;
3594 }
3595 break;
3596
3597 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003598 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003599 case ISN_BLOBINDEX:
3600 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003602 int is_slice = iptr->isn_type == ISN_LISTSLICE
3603 || iptr->isn_type == ISN_BLOBSLICE;
3604 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3605 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003606 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003607 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608
3609 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003610 // list slice: list is at stack-3, indexes at stack-2 and
3611 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003612 // Same for blob.
3613 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614
3615 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003616 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003618
3619 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 {
Bram Moolenaared591872020-08-15 22:14:53 +02003621 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003622 n1 = tv->vval.v_number;
3623 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 }
Bram Moolenaared591872020-08-15 22:14:53 +02003625
Bram Moolenaar4c137212021-04-19 16:48:48 +02003626 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003627 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003628 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003629 if (is_blob)
3630 {
3631 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3632 n1, n2, FALSE, tv) == FAIL)
3633 goto on_error;
3634 }
3635 else
3636 {
3637 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3638 n1, n2, FALSE, tv, TRUE) == FAIL)
3639 goto on_error;
3640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 }
3642 break;
3643
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003644 case ISN_ANYINDEX:
3645 case ISN_ANYSLICE:
3646 {
3647 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3648 typval_T *var1, *var2;
3649 int res;
3650
3651 // index: composite is at stack-2, index at stack-1
3652 // slice: composite is at stack-3, indexes at stack-2 and
3653 // stack-1
3654 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003655 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003656 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3657 goto on_error;
3658 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3659 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003660 res = eval_index_inner(tv, is_slice, var1, var2,
3661 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003662 clear_tv(var1);
3663 if (is_slice)
3664 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003665 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003666 if (res == FAIL)
3667 goto on_error;
3668 }
3669 break;
3670
Bram Moolenaar9af78762020-06-16 11:34:42 +02003671 case ISN_SLICE:
3672 {
3673 list_T *list;
3674 int count = iptr->isn_arg.number;
3675
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003676 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003677 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003678 list = tv->vval.v_list;
3679
3680 // no error for short list, expect it to be checked earlier
3681 if (list != NULL && list->lv_len >= count)
3682 {
3683 list_T *newlist = list_slice(list,
3684 count, list->lv_len - 1);
3685
3686 if (newlist != NULL)
3687 {
3688 list_unref(list);
3689 tv->vval.v_list = newlist;
3690 ++newlist->lv_refcount;
3691 }
3692 }
3693 }
3694 break;
3695
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003696 case ISN_GETITEM:
3697 {
3698 listitem_T *li;
3699 int index = iptr->isn_arg.number;
3700
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003701 // Get list item: list is at stack-1, push item.
3702 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003703 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003704 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003705
Bram Moolenaar4c137212021-04-19 16:48:48 +02003706 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003707 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003708 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003709 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003710
3711 // Useful when used in unpack assignment. Reset at
3712 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003713 ectx->ec_where.wt_index = index + 1;
3714 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003715 }
3716 break;
3717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 case ISN_MEMBER:
3719 {
3720 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003721 char_u *key;
3722 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003723 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003724
3725 // dict member: dict is at stack-2, key at stack-1
3726 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003727 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003728 dict = tv->vval.v_dict;
3729
3730 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003731 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003732 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003733 if (key == NULL)
3734 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003735
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003736 if ((di = dict_find(dict, key, -1)) == NULL)
3737 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003738 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003739 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003740
3741 // If :silent! is used we will continue, make sure the
3742 // stack contents makes sense.
3743 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003744 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003745 tv = STACK_TV_BOT(-1);
3746 clear_tv(tv);
3747 tv->v_type = VAR_NUMBER;
3748 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003749 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003750 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003751 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003752 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003753 // Clear the dict only after getting the item, to avoid
3754 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003755 tv = STACK_TV_BOT(-1);
3756 temp_tv = *tv;
3757 copy_tv(&di->di_tv, tv);
3758 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003759 }
3760 break;
3761
3762 // dict member with string key
3763 case ISN_STRINGMEMBER:
3764 {
3765 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003767 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768
3769 tv = STACK_TV_BOT(-1);
3770 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3771 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003772 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003774 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 }
3776 dict = tv->vval.v_dict;
3777
3778 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3779 == NULL)
3780 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003781 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003783 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003785 // Clear the dict after getting the item, to avoid that it
3786 // make the item invalid.
3787 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003789 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 }
3791 break;
3792
3793 case ISN_NEGATENR:
3794 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003795 if (tv->v_type != VAR_NUMBER
3796#ifdef FEAT_FLOAT
3797 && tv->v_type != VAR_FLOAT
3798#endif
3799 )
3800 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003801 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003802 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003803 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003804 }
3805#ifdef FEAT_FLOAT
3806 if (tv->v_type == VAR_FLOAT)
3807 tv->vval.v_float = -tv->vval.v_float;
3808 else
3809#endif
3810 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 break;
3812
3813 case ISN_CHECKNR:
3814 {
3815 int error = FALSE;
3816
3817 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003818 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003820 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 (void)tv_get_number_chk(tv, &error);
3822 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003823 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 }
3825 break;
3826
3827 case ISN_CHECKTYPE:
3828 {
3829 checktype_T *ct = &iptr->isn_arg.type;
3830
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003831 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003832 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003833 if (!ectx->ec_where.wt_variable)
3834 ectx->ec_where.wt_index = ct->ct_arg_idx;
3835 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3836 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003837 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 if (!ectx->ec_where.wt_variable)
3839 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003840
3841 // number 0 is FALSE, number 1 is TRUE
3842 if (tv->v_type == VAR_NUMBER
3843 && ct->ct_type->tt_type == VAR_BOOL
3844 && (tv->vval.v_number == 0
3845 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003847 tv->v_type = VAR_BOOL;
3848 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003849 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 }
3851 }
3852 break;
3853
Bram Moolenaar9af78762020-06-16 11:34:42 +02003854 case ISN_CHECKLEN:
3855 {
3856 int min_len = iptr->isn_arg.checklen.cl_min_len;
3857 list_T *list = NULL;
3858
3859 tv = STACK_TV_BOT(-1);
3860 if (tv->v_type == VAR_LIST)
3861 list = tv->vval.v_list;
3862 if (list == NULL || list->lv_len < min_len
3863 || (list->lv_len > min_len
3864 && !iptr->isn_arg.checklen.cl_more_OK))
3865 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003866 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003867 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003868 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003869 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003870 }
3871 }
3872 break;
3873
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003874 case ISN_SETTYPE:
3875 {
3876 checktype_T *ct = &iptr->isn_arg.type;
3877
3878 tv = STACK_TV_BOT(-1);
3879 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3880 {
3881 free_type(tv->vval.v_dict->dv_type);
3882 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3883 }
3884 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3885 {
3886 free_type(tv->vval.v_list->lv_type);
3887 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3888 }
3889 }
3890 break;
3891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003893 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 {
3895 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003896 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003898 if (iptr->isn_type == ISN_2BOOL)
3899 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003900 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003901 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003902 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003903 n = !n;
3904 }
3905 else
3906 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003907 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003908 SOURCING_LNUM = iptr->isn_lnum;
3909 n = tv_get_bool_chk(tv, &error);
3910 if (error)
3911 goto on_error;
3912 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 clear_tv(tv);
3914 tv->v_type = VAR_BOOL;
3915 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3916 }
3917 break;
3918
3919 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003920 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003921 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003922 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3923 iptr->isn_type == ISN_2STRING_ANY,
3924 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003925 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 break;
3927
Bram Moolenaar08597872020-12-10 19:43:40 +01003928 case ISN_RANGE:
3929 {
3930 exarg_T ea;
3931 char *errormsg;
3932
Bram Moolenaarece0b872021-01-08 20:40:45 +01003933 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003934 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003935 ea.addr_type = ADDR_LINES;
3936 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003937 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003938 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003939 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003940
Bram Moolenaar4c137212021-04-19 16:48:48 +02003941 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003942 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003943 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003944 tv = STACK_TV_BOT(-1);
3945 tv->v_type = VAR_NUMBER;
3946 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003947 if (ea.addr_count == 0)
3948 tv->vval.v_number = curwin->w_cursor.lnum;
3949 else
3950 tv->vval.v_number = ea.line2;
3951 }
3952 break;
3953
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003954 case ISN_PUT:
3955 {
3956 int regname = iptr->isn_arg.put.put_regname;
3957 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3958 char_u *expr = NULL;
3959 int dir = FORWARD;
3960
Bram Moolenaar08597872020-12-10 19:43:40 +01003961 if (lnum < -2)
3962 {
3963 // line number was put on the stack by ISN_RANGE
3964 tv = STACK_TV_BOT(-1);
3965 curwin->w_cursor.lnum = tv->vval.v_number;
3966 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3967 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003968 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003969 }
3970 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003971 // :put! above cursor
3972 dir = BACKWARD;
3973 else if (lnum >= 0)
3974 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003975
3976 if (regname == '=')
3977 {
3978 tv = STACK_TV_BOT(-1);
3979 if (tv->v_type == VAR_STRING)
3980 expr = tv->vval.v_string;
3981 else
3982 {
3983 expr = typval2string(tv, TRUE); // allocates value
3984 clear_tv(tv);
3985 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003986 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003987 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003988 check_cursor();
3989 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3990 vim_free(expr);
3991 }
3992 break;
3993
Bram Moolenaar02194d22020-10-24 23:08:38 +02003994 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003995 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3996 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3997 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3998 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003999 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4000 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004001 break;
4002
Bram Moolenaar02194d22020-10-24 23:08:38 +02004003 case ISN_CMDMOD_REV:
4004 // filter regprog is owned by the instruction, don't free it
4005 cmdmod.cmod_filter_regmatch.regprog = NULL;
4006 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004007 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4008 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004009 break;
4010
Bram Moolenaar792f7862020-11-23 08:31:18 +01004011 case ISN_UNPACK:
4012 {
4013 int count = iptr->isn_arg.unpack.unp_count;
4014 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4015 list_T *l;
4016 listitem_T *li;
4017 int i;
4018
4019 // Check there is a valid list to unpack.
4020 tv = STACK_TV_BOT(-1);
4021 if (tv->v_type != VAR_LIST)
4022 {
4023 SOURCING_LNUM = iptr->isn_lnum;
4024 emsg(_(e_for_argument_must_be_sequence_of_lists));
4025 goto on_error;
4026 }
4027 l = tv->vval.v_list;
4028 if (l == NULL
4029 || l->lv_len < (semicolon ? count - 1 : count))
4030 {
4031 SOURCING_LNUM = iptr->isn_lnum;
4032 emsg(_(e_list_value_does_not_have_enough_items));
4033 goto on_error;
4034 }
4035 else if (!semicolon && l->lv_len > count)
4036 {
4037 SOURCING_LNUM = iptr->isn_lnum;
4038 emsg(_(e_list_value_has_more_items_than_targets));
4039 goto on_error;
4040 }
4041
4042 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004043 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004044 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004045 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004046
4047 // Variable after semicolon gets a list with the remaining
4048 // items.
4049 if (semicolon)
4050 {
4051 list_T *rem_list =
4052 list_alloc_with_items(l->lv_len - count + 1);
4053
4054 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004055 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004056 tv = STACK_TV_BOT(-count);
4057 tv->vval.v_list = rem_list;
4058 ++rem_list->lv_refcount;
4059 tv->v_lock = 0;
4060 li = l->lv_first;
4061 for (i = 0; i < count - 1; ++i)
4062 li = li->li_next;
4063 for (i = 0; li != NULL; ++i)
4064 {
4065 list_set_item(rem_list, i, &li->li_tv);
4066 li = li->li_next;
4067 }
4068 --count;
4069 }
4070
4071 // Produce the values in reverse order, first item last.
4072 li = l->lv_first;
4073 for (i = 0; i < count; ++i)
4074 {
4075 tv = STACK_TV_BOT(-i - 1);
4076 copy_tv(&li->li_tv, tv);
4077 li = li->li_next;
4078 }
4079
4080 list_unref(l);
4081 }
4082 break;
4083
Bram Moolenaarb2049902021-01-24 12:53:53 +01004084 case ISN_PROF_START:
4085 case ISN_PROF_END:
4086 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004087#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004088 funccall_T cookie;
4089 ufunc_T *cur_ufunc =
4090 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004091 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004092
4093 cookie.func = cur_ufunc;
4094 if (iptr->isn_type == ISN_PROF_START)
4095 {
4096 func_line_start(&cookie, iptr->isn_lnum);
4097 // if we get here the instruction is executed
4098 func_line_exec(&cookie);
4099 }
4100 else
4101 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004102#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004103 }
4104 break;
4105
Bram Moolenaare99d4222021-06-13 14:01:26 +02004106 case ISN_DEBUG:
4107 if (ex_nesting_level <= debug_break_level)
4108 {
4109 char_u *line;
4110 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
4111 + ectx->ec_dfunc_idx)->df_ufunc;
4112
4113 SOURCING_LNUM = iptr->isn_lnum;
4114 line = ((char_u **)ufunc->uf_lines.ga_data)[
4115 iptr->isn_lnum - 1];
4116 if (line == NULL)
4117 line = (char_u *)"[empty]";
4118 do_debug(line);
4119 }
4120 break;
4121
Bram Moolenaar389df252020-07-09 21:20:47 +02004122 case ISN_SHUFFLE:
4123 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004124 typval_T tmp_tv;
4125 int item = iptr->isn_arg.shuffle.shfl_item;
4126 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004127
4128 tmp_tv = *STACK_TV_BOT(-item);
4129 for ( ; up > 0 && item > 1; --up)
4130 {
4131 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4132 --item;
4133 }
4134 *STACK_TV_BOT(-item) = tmp_tv;
4135 }
4136 break;
4137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004139 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004141 ectx->ec_where.wt_index = 0;
4142 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 break;
4144 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004145 continue;
4146
Bram Moolenaard032f342020-07-18 18:13:02 +02004147func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004148 // Restore previous function. If the frame pointer is where we started
4149 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004150 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004151 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004152
Bram Moolenaar4c137212021-04-19 16:48:48 +02004153 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004154 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004155 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004156 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004157
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004158on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004159 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004160 // If "emsg_silent" is set then ignore the error, unless it was set
4161 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004162 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004163 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004164 {
4165 // If a sequence of instructions causes an error while ":silent!"
4166 // was used, restore the stack length and jump ahead to restoring
4167 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004168 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004169 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004170 while (ectx->ec_stack.ga_len
4171 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004172 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004173 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004174 clear_tv(STACK_TV_BOT(0));
4175 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004176 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4177 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004178 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004179 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004180 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004181on_fatal_error:
4182 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004183 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004184 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004185 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 }
4187
4188done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004189 ret = OK;
4190theend:
4191 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4192 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004193}
4194
4195/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004196 * Execute the instructions from a VAR_INSTR typeval and put the result in
4197 * "rettv".
4198 * Return OK or FAIL.
4199 */
4200 int
4201exe_typval_instr(typval_T *tv, typval_T *rettv)
4202{
4203 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4204 isn_T *save_instr = ectx->ec_instr;
4205 int save_iidx = ectx->ec_iidx;
4206 int res;
4207
4208 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4209 res = exec_instructions(ectx);
4210 if (res == OK)
4211 {
4212 *rettv = *STACK_TV_BOT(-1);
4213 --ectx->ec_stack.ga_len;
4214 }
4215
4216 ectx->ec_instr = save_instr;
4217 ectx->ec_iidx = save_iidx;
4218
4219 return res;
4220}
4221
4222/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004223 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4224 * "substitute_instr".
4225 */
4226 char_u *
4227exe_substitute_instr(void)
4228{
4229 ectx_T *ectx = substitute_instr->subs_ectx;
4230 isn_T *save_instr = ectx->ec_instr;
4231 int save_iidx = ectx->ec_iidx;
4232 char_u *res;
4233
4234 ectx->ec_instr = substitute_instr->subs_instr;
4235 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004236 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004237 typval_T *tv = STACK_TV_BOT(-1);
4238
Bram Moolenaar27523602021-06-05 21:36:19 +02004239 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004240 --ectx->ec_stack.ga_len;
4241 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004242 }
4243 else
4244 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004245 substitute_instr->subs_status = FAIL;
4246 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248
Bram Moolenaar4c137212021-04-19 16:48:48 +02004249 ectx->ec_instr = save_instr;
4250 ectx->ec_iidx = save_iidx;
4251
4252 return res;
4253}
4254
4255/*
4256 * Call a "def" function from old Vim script.
4257 * Return OK or FAIL.
4258 */
4259 int
4260call_def_function(
4261 ufunc_T *ufunc,
4262 int argc_arg, // nr of arguments
4263 typval_T *argv, // arguments
4264 partial_T *partial, // optional partial for context
4265 typval_T *rettv) // return value
4266{
4267 ectx_T ectx; // execution context
4268 int argc = argc_arg;
4269 typval_T *tv;
4270 int idx;
4271 int ret = FAIL;
4272 int defcount = ufunc->uf_args.ga_len - argc;
4273 sctx_T save_current_sctx = current_sctx;
4274 int did_emsg_before = did_emsg_cumul + did_emsg;
4275 int save_suppress_errthrow = suppress_errthrow;
4276 msglist_T **saved_msg_list = NULL;
4277 msglist_T *private_msg_list = NULL;
4278 int save_emsg_silent_def = emsg_silent_def;
4279 int save_did_emsg_def = did_emsg_def;
4280 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004281 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004282
4283// Get pointer to item in the stack.
4284#undef STACK_TV
4285#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4286
4287// Get pointer to item at the bottom of the stack, -1 is the bottom.
4288#undef STACK_TV_BOT
4289#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4290
4291// Get pointer to a local variable on the stack. Negative for arguments.
4292#undef STACK_TV_VAR
4293#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4294
4295 if (ufunc->uf_def_status == UF_NOT_COMPILED
4296 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004297 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4298 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004299 == FAIL))
4300 {
4301 if (did_emsg_cumul + did_emsg == did_emsg_before)
4302 semsg(_(e_function_is_not_compiled_str),
4303 printable_func_name(ufunc));
4304 return FAIL;
4305 }
4306
4307 {
4308 // Check the function was really compiled.
4309 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4310 + ufunc->uf_dfunc_idx;
4311 if (INSTRUCTIONS(dfunc) == NULL)
4312 {
4313 iemsg("using call_def_function() on not compiled function");
4314 return FAIL;
4315 }
4316 }
4317
4318 // If depth of calling is getting too high, don't execute the function.
4319 orig_funcdepth = funcdepth_get();
4320 if (funcdepth_increment() == FAIL)
4321 return FAIL;
4322
4323 CLEAR_FIELD(ectx);
4324 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4325 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4326 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4327 {
4328 funcdepth_decrement();
4329 return FAIL;
4330 }
4331 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4332 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4333 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004334 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004335
4336 idx = argc - ufunc->uf_args.ga_len;
4337 if (idx > 0 && ufunc->uf_va_name == NULL)
4338 {
4339 if (idx == 1)
4340 emsg(_(e_one_argument_too_many));
4341 else
4342 semsg(_(e_nr_arguments_too_many), idx);
4343 goto failed_early;
4344 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004345 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4346 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004347 {
4348 if (idx == -1)
4349 emsg(_(e_one_argument_too_few));
4350 else
4351 semsg(_(e_nr_arguments_too_few), -idx);
4352 goto failed_early;
4353 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004354
4355 // Put arguments on the stack, but no more than what the function expects.
4356 // A lambda can be called with more arguments than it uses.
4357 for (idx = 0; idx < argc
4358 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4359 ++idx)
4360 {
4361 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4362 && argv[idx].v_type == VAR_SPECIAL
4363 && argv[idx].vval.v_number == VVAL_NONE)
4364 {
4365 // Use the default value.
4366 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4367 }
4368 else
4369 {
4370 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4371 && check_typval_arg_type(
4372 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4373 goto failed_early;
4374 copy_tv(&argv[idx], STACK_TV_BOT(0));
4375 }
4376 ++ectx.ec_stack.ga_len;
4377 }
4378
4379 // Turn varargs into a list. Empty list if no args.
4380 if (ufunc->uf_va_name != NULL)
4381 {
4382 int vararg_count = argc - ufunc->uf_args.ga_len;
4383
4384 if (vararg_count < 0)
4385 vararg_count = 0;
4386 else
4387 argc -= vararg_count;
4388 if (exe_newlist(vararg_count, &ectx) == FAIL)
4389 goto failed_early;
4390
4391 // Check the type of the list items.
4392 tv = STACK_TV_BOT(-1);
4393 if (ufunc->uf_va_type != NULL
4394 && ufunc->uf_va_type != &t_list_any
4395 && ufunc->uf_va_type->tt_member != &t_any
4396 && tv->vval.v_list != NULL)
4397 {
4398 type_T *expected = ufunc->uf_va_type->tt_member;
4399 listitem_T *li = tv->vval.v_list->lv_first;
4400
4401 for (idx = 0; idx < vararg_count; ++idx)
4402 {
4403 if (check_typval_arg_type(expected, &li->li_tv,
4404 argc + idx + 1) == FAIL)
4405 goto failed_early;
4406 li = li->li_next;
4407 }
4408 }
4409
4410 if (defcount > 0)
4411 // Move varargs list to below missing default arguments.
4412 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4413 --ectx.ec_stack.ga_len;
4414 }
4415
4416 // Make space for omitted arguments, will store default value below.
4417 // Any varargs list goes after them.
4418 if (defcount > 0)
4419 for (idx = 0; idx < defcount; ++idx)
4420 {
4421 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4422 ++ectx.ec_stack.ga_len;
4423 }
4424 if (ufunc->uf_va_name != NULL)
4425 ++ectx.ec_stack.ga_len;
4426
4427 // Frame pointer points to just after arguments.
4428 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4429 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4430
4431 {
4432 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4433 + ufunc->uf_dfunc_idx;
4434 ufunc_T *base_ufunc = dfunc->df_ufunc;
4435
4436 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4437 // by copy_func().
4438 if (partial != NULL || base_ufunc->uf_partial != NULL)
4439 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004440 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4441 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004442 goto failed_early;
4443 if (partial != NULL)
4444 {
4445 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4446 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004447 if (current_ectx->ec_outer_ref != NULL
4448 && current_ectx->ec_outer_ref->or_outer != NULL)
4449 ectx.ec_outer_ref->or_outer =
4450 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004451 }
4452 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004453 {
4454 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4455 ++partial->pt_refcount;
4456 ectx.ec_outer_ref->or_partial = partial;
4457 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004458 }
4459 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004460 {
4461 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4462 ++base_ufunc->uf_partial->pt_refcount;
4463 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4464 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004465 }
4466 }
4467
4468 // dummy frame entries
4469 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4470 {
4471 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4472 ++ectx.ec_stack.ga_len;
4473 }
4474
4475 {
4476 // Reserve space for local variables and any closure reference count.
4477 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4478 + ufunc->uf_dfunc_idx;
4479
4480 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4481 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4482 ectx.ec_stack.ga_len += dfunc->df_varcount;
4483 if (dfunc->df_has_closure)
4484 {
4485 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4486 STACK_TV_VAR(idx)->vval.v_number = 0;
4487 ++ectx.ec_stack.ga_len;
4488 }
4489
4490 ectx.ec_instr = INSTRUCTIONS(dfunc);
4491 }
4492
4493 // Following errors are in the function, not the caller.
4494 // Commands behave like vim9script.
4495 estack_push_ufunc(ufunc, 1);
4496 current_sctx = ufunc->uf_script_ctx;
4497 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4498
4499 // Use a specific location for storing error messages to be converted to an
4500 // exception.
4501 saved_msg_list = msg_list;
4502 msg_list = &private_msg_list;
4503
4504 // Do turn errors into exceptions.
4505 suppress_errthrow = FALSE;
4506
4507 // Do not delete the function while executing it.
4508 ++ufunc->uf_calls;
4509
4510 // When ":silent!" was used before calling then we still abort the
4511 // function. If ":silent!" is used in the function then we don't.
4512 emsg_silent_def = emsg_silent;
4513 did_emsg_def = 0;
4514
4515 ectx.ec_where.wt_index = 0;
4516 ectx.ec_where.wt_variable = FALSE;
4517
4518 // Execute the instructions until done.
4519 ret = exec_instructions(&ectx);
4520 if (ret == OK)
4521 {
4522 // function finished, get result from the stack.
4523 if (ufunc->uf_ret_type == &t_void)
4524 {
4525 rettv->v_type = VAR_VOID;
4526 }
4527 else
4528 {
4529 tv = STACK_TV_BOT(-1);
4530 *rettv = *tv;
4531 tv->v_type = VAR_UNKNOWN;
4532 }
4533 }
4534
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004535 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004536 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4537 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004538
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004539 // Deal with any remaining closures, they may be in use somewhere.
4540 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004541 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004542 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004543 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4544 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004545
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004546 estack_pop();
4547 current_sctx = save_current_sctx;
4548
Bram Moolenaarc970e422021-03-17 15:03:04 +01004549 // TODO: when is it safe to delete the function if it is no longer used?
4550 --ufunc->uf_calls;
4551
Bram Moolenaar352134b2020-10-17 22:04:08 +02004552 if (*msg_list != NULL && saved_msg_list != NULL)
4553 {
4554 msglist_T **plist = saved_msg_list;
4555
4556 // Append entries from the current msg_list (uncaught exceptions) to
4557 // the saved msg_list.
4558 while (*plist != NULL)
4559 plist = &(*plist)->next;
4560
4561 *plist = *msg_list;
4562 }
4563 msg_list = saved_msg_list;
4564
Bram Moolenaar4c137212021-04-19 16:48:48 +02004565 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004566 {
4567 cmdmod.cmod_filter_regmatch.regprog = NULL;
4568 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004569 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004570 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004571 emsg_silent_def = save_emsg_silent_def;
4572 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004573
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004574failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004575 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4577 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004578 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004581 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004582 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004583 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004584 if (ectx.ec_outer_ref->or_outer_allocated)
4585 vim_free(ectx.ec_outer_ref->or_outer);
4586 partial_unref(ectx.ec_outer_ref->or_partial);
4587 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004588 }
4589
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004590 // Not sure if this is necessary.
4591 suppress_errthrow = save_suppress_errthrow;
4592
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004593 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004594 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004595 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004596 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 return ret;
4598}
4599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004601 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4602 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4603 * "pfx" is prefixed to every line.
4604 */
4605 static void
4606list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4607{
4608 int line_idx = 0;
4609 int prev_current = 0;
4610 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004611 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004612
4613 for (current = 0; current < instr_count; ++current)
4614 {
4615 isn_T *iptr = &instr[current];
4616 char *line;
4617
4618 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004619 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004620 while (line_idx < iptr->isn_lnum
4621 && line_idx < ufunc->uf_lines.ga_len)
4622 {
4623 if (current > prev_current)
4624 {
4625 msg_puts("\n\n");
4626 prev_current = current;
4627 }
4628 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4629 if (line != NULL)
4630 msg(line);
4631 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004632 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4633 {
4634 int first_def_arg = ufunc->uf_args.ga_len
4635 - ufunc->uf_def_args.ga_len;
4636
4637 if (def_arg_idx > 0)
4638 msg_puts("\n\n");
4639 msg_start();
4640 msg_puts(" ");
4641 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4642 first_def_arg + def_arg_idx]);
4643 msg_puts(" = ");
4644 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4645 msg_clr_eos();
4646 msg_end();
4647 }
4648 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004649
4650 switch (iptr->isn_type)
4651 {
4652 case ISN_EXEC:
4653 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4654 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004655 case ISN_EXEC_SPLIT:
4656 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4657 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004658 case ISN_LEGACY_EVAL:
4659 smsg("%s%4d EVAL legacy %s", pfx, current,
4660 iptr->isn_arg.string);
4661 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004662 case ISN_REDIRSTART:
4663 smsg("%s%4d REDIR", pfx, current);
4664 break;
4665 case ISN_REDIREND:
4666 smsg("%s%4d REDIR END%s", pfx, current,
4667 iptr->isn_arg.number ? " append" : "");
4668 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004669 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004670#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004671 smsg("%s%4d CEXPR pre %s", pfx, current,
4672 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004673#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004674 break;
4675 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004676#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004677 {
4678 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4679
4680 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4681 cexpr_get_auname(cer->cer_cmdidx),
4682 cer->cer_forceit ? "!" : "",
4683 cer->cer_cmdline);
4684 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004685#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004686 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004687 case ISN_INSTR:
4688 {
4689 smsg("%s%4d INSTR", pfx, current);
4690 list_instructions(" ", iptr->isn_arg.instr,
4691 INT_MAX, NULL);
4692 msg(" -------------");
4693 }
4694 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004695 case ISN_SUBSTITUTE:
4696 {
4697 subs_T *subs = &iptr->isn_arg.subs;
4698
4699 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4700 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4701 msg(" -------------");
4702 }
4703 break;
4704 case ISN_EXECCONCAT:
4705 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4706 (varnumber_T)iptr->isn_arg.number);
4707 break;
4708 case ISN_ECHO:
4709 {
4710 echo_T *echo = &iptr->isn_arg.echo;
4711
4712 smsg("%s%4d %s %d", pfx, current,
4713 echo->echo_with_white ? "ECHO" : "ECHON",
4714 echo->echo_count);
4715 }
4716 break;
4717 case ISN_EXECUTE:
4718 smsg("%s%4d EXECUTE %lld", pfx, current,
4719 (varnumber_T)(iptr->isn_arg.number));
4720 break;
4721 case ISN_ECHOMSG:
4722 smsg("%s%4d ECHOMSG %lld", pfx, current,
4723 (varnumber_T)(iptr->isn_arg.number));
4724 break;
4725 case ISN_ECHOERR:
4726 smsg("%s%4d ECHOERR %lld", pfx, current,
4727 (varnumber_T)(iptr->isn_arg.number));
4728 break;
4729 case ISN_LOAD:
4730 {
4731 if (iptr->isn_arg.number < 0)
4732 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4733 (varnumber_T)(iptr->isn_arg.number
4734 + STACK_FRAME_SIZE));
4735 else
4736 smsg("%s%4d LOAD $%lld", pfx, current,
4737 (varnumber_T)(iptr->isn_arg.number));
4738 }
4739 break;
4740 case ISN_LOADOUTER:
4741 {
4742 if (iptr->isn_arg.number < 0)
4743 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4744 iptr->isn_arg.outer.outer_depth,
4745 iptr->isn_arg.outer.outer_idx
4746 + STACK_FRAME_SIZE);
4747 else
4748 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4749 iptr->isn_arg.outer.outer_depth,
4750 iptr->isn_arg.outer.outer_idx);
4751 }
4752 break;
4753 case ISN_LOADV:
4754 smsg("%s%4d LOADV v:%s", pfx, current,
4755 get_vim_var_name(iptr->isn_arg.number));
4756 break;
4757 case ISN_LOADSCRIPT:
4758 {
4759 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4760 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4761 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4762 + sref->sref_idx;
4763
4764 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4765 sv->sv_name,
4766 sref->sref_idx,
4767 si->sn_name);
4768 }
4769 break;
4770 case ISN_LOADS:
4771 {
4772 scriptitem_T *si = SCRIPT_ITEM(
4773 iptr->isn_arg.loadstore.ls_sid);
4774
4775 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4776 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4777 }
4778 break;
4779 case ISN_LOADAUTO:
4780 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4781 break;
4782 case ISN_LOADG:
4783 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4784 break;
4785 case ISN_LOADB:
4786 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4787 break;
4788 case ISN_LOADW:
4789 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4790 break;
4791 case ISN_LOADT:
4792 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4793 break;
4794 case ISN_LOADGDICT:
4795 smsg("%s%4d LOAD g:", pfx, current);
4796 break;
4797 case ISN_LOADBDICT:
4798 smsg("%s%4d LOAD b:", pfx, current);
4799 break;
4800 case ISN_LOADWDICT:
4801 smsg("%s%4d LOAD w:", pfx, current);
4802 break;
4803 case ISN_LOADTDICT:
4804 smsg("%s%4d LOAD t:", pfx, current);
4805 break;
4806 case ISN_LOADOPT:
4807 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4808 break;
4809 case ISN_LOADENV:
4810 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4811 break;
4812 case ISN_LOADREG:
4813 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4814 break;
4815
4816 case ISN_STORE:
4817 if (iptr->isn_arg.number < 0)
4818 smsg("%s%4d STORE arg[%lld]", pfx, current,
4819 iptr->isn_arg.number + STACK_FRAME_SIZE);
4820 else
4821 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4822 break;
4823 case ISN_STOREOUTER:
4824 {
4825 if (iptr->isn_arg.number < 0)
4826 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4827 iptr->isn_arg.outer.outer_depth,
4828 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4829 else
4830 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4831 iptr->isn_arg.outer.outer_depth,
4832 iptr->isn_arg.outer.outer_idx);
4833 }
4834 break;
4835 case ISN_STOREV:
4836 smsg("%s%4d STOREV v:%s", pfx, current,
4837 get_vim_var_name(iptr->isn_arg.number));
4838 break;
4839 case ISN_STOREAUTO:
4840 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4841 break;
4842 case ISN_STOREG:
4843 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4844 break;
4845 case ISN_STOREB:
4846 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4847 break;
4848 case ISN_STOREW:
4849 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4850 break;
4851 case ISN_STORET:
4852 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4853 break;
4854 case ISN_STORES:
4855 {
4856 scriptitem_T *si = SCRIPT_ITEM(
4857 iptr->isn_arg.loadstore.ls_sid);
4858
4859 smsg("%s%4d STORES %s in %s", pfx, current,
4860 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4861 }
4862 break;
4863 case ISN_STORESCRIPT:
4864 {
4865 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4866 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4867 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4868 + sref->sref_idx;
4869
4870 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4871 sv->sv_name,
4872 sref->sref_idx,
4873 si->sn_name);
4874 }
4875 break;
4876 case ISN_STOREOPT:
4877 smsg("%s%4d STOREOPT &%s", pfx, current,
4878 iptr->isn_arg.storeopt.so_name);
4879 break;
4880 case ISN_STOREENV:
4881 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4882 break;
4883 case ISN_STOREREG:
4884 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4885 break;
4886 case ISN_STORENR:
4887 smsg("%s%4d STORE %lld in $%d", pfx, current,
4888 iptr->isn_arg.storenr.stnr_val,
4889 iptr->isn_arg.storenr.stnr_idx);
4890 break;
4891
4892 case ISN_STOREINDEX:
4893 smsg("%s%4d STOREINDEX %s", pfx, current,
4894 vartype_name(iptr->isn_arg.vartype));
4895 break;
4896
4897 case ISN_STORERANGE:
4898 smsg("%s%4d STORERANGE", pfx, current);
4899 break;
4900
4901 // constants
4902 case ISN_PUSHNR:
4903 smsg("%s%4d PUSHNR %lld", pfx, current,
4904 (varnumber_T)(iptr->isn_arg.number));
4905 break;
4906 case ISN_PUSHBOOL:
4907 case ISN_PUSHSPEC:
4908 smsg("%s%4d PUSH %s", pfx, current,
4909 get_var_special_name(iptr->isn_arg.number));
4910 break;
4911 case ISN_PUSHF:
4912#ifdef FEAT_FLOAT
4913 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4914#endif
4915 break;
4916 case ISN_PUSHS:
4917 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4918 break;
4919 case ISN_PUSHBLOB:
4920 {
4921 char_u *r;
4922 char_u numbuf[NUMBUFLEN];
4923 char_u *tofree;
4924
4925 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4926 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4927 vim_free(tofree);
4928 }
4929 break;
4930 case ISN_PUSHFUNC:
4931 {
4932 char *name = (char *)iptr->isn_arg.string;
4933
4934 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4935 name == NULL ? "[none]" : name);
4936 }
4937 break;
4938 case ISN_PUSHCHANNEL:
4939#ifdef FEAT_JOB_CHANNEL
4940 {
4941 channel_T *channel = iptr->isn_arg.channel;
4942
4943 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4944 channel == NULL ? 0 : channel->ch_id);
4945 }
4946#endif
4947 break;
4948 case ISN_PUSHJOB:
4949#ifdef FEAT_JOB_CHANNEL
4950 {
4951 typval_T tv;
4952 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004953 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02004954
4955 tv.v_type = VAR_JOB;
4956 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004957 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004958 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4959 }
4960#endif
4961 break;
4962 case ISN_PUSHEXC:
4963 smsg("%s%4d PUSH v:exception", pfx, current);
4964 break;
4965 case ISN_UNLET:
4966 smsg("%s%4d UNLET%s %s", pfx, current,
4967 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4968 iptr->isn_arg.unlet.ul_name);
4969 break;
4970 case ISN_UNLETENV:
4971 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4972 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4973 iptr->isn_arg.unlet.ul_name);
4974 break;
4975 case ISN_UNLETINDEX:
4976 smsg("%s%4d UNLETINDEX", pfx, current);
4977 break;
4978 case ISN_UNLETRANGE:
4979 smsg("%s%4d UNLETRANGE", pfx, current);
4980 break;
4981 case ISN_LOCKCONST:
4982 smsg("%s%4d LOCKCONST", pfx, current);
4983 break;
4984 case ISN_NEWLIST:
4985 smsg("%s%4d NEWLIST size %lld", pfx, current,
4986 (varnumber_T)(iptr->isn_arg.number));
4987 break;
4988 case ISN_NEWDICT:
4989 smsg("%s%4d NEWDICT size %lld", pfx, current,
4990 (varnumber_T)(iptr->isn_arg.number));
4991 break;
4992
4993 // function call
4994 case ISN_BCALL:
4995 {
4996 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4997
4998 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4999 internal_func_name(cbfunc->cbf_idx),
5000 cbfunc->cbf_argcount);
5001 }
5002 break;
5003 case ISN_DCALL:
5004 {
5005 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5006 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5007 + cdfunc->cdf_idx;
5008
5009 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5010 df->df_ufunc->uf_name_exp != NULL
5011 ? df->df_ufunc->uf_name_exp
5012 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5013 }
5014 break;
5015 case ISN_UCALL:
5016 {
5017 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5018
5019 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5020 cufunc->cuf_name, cufunc->cuf_argcount);
5021 }
5022 break;
5023 case ISN_PCALL:
5024 {
5025 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5026
5027 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5028 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5029 }
5030 break;
5031 case ISN_PCALL_END:
5032 smsg("%s%4d PCALL end", pfx, current);
5033 break;
5034 case ISN_RETURN:
5035 smsg("%s%4d RETURN", pfx, current);
5036 break;
5037 case ISN_RETURN_ZERO:
5038 smsg("%s%4d RETURN 0", pfx, current);
5039 break;
5040 case ISN_FUNCREF:
5041 {
5042 funcref_T *funcref = &iptr->isn_arg.funcref;
5043 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5044 + funcref->fr_func;
5045
5046 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5047 }
5048 break;
5049
5050 case ISN_NEWFUNC:
5051 {
5052 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5053
5054 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5055 newfunc->nf_lambda, newfunc->nf_global);
5056 }
5057 break;
5058
5059 case ISN_DEF:
5060 {
5061 char_u *name = iptr->isn_arg.string;
5062
5063 smsg("%s%4d DEF %s", pfx, current,
5064 name == NULL ? (char_u *)"" : name);
5065 }
5066 break;
5067
5068 case ISN_JUMP:
5069 {
5070 char *when = "?";
5071
5072 switch (iptr->isn_arg.jump.jump_when)
5073 {
5074 case JUMP_ALWAYS:
5075 when = "JUMP";
5076 break;
5077 case JUMP_AND_KEEP_IF_TRUE:
5078 when = "JUMP_AND_KEEP_IF_TRUE";
5079 break;
5080 case JUMP_IF_FALSE:
5081 when = "JUMP_IF_FALSE";
5082 break;
5083 case JUMP_AND_KEEP_IF_FALSE:
5084 when = "JUMP_AND_KEEP_IF_FALSE";
5085 break;
5086 case JUMP_IF_COND_FALSE:
5087 when = "JUMP_IF_COND_FALSE";
5088 break;
5089 case JUMP_IF_COND_TRUE:
5090 when = "JUMP_IF_COND_TRUE";
5091 break;
5092 }
5093 smsg("%s%4d %s -> %d", pfx, current, when,
5094 iptr->isn_arg.jump.jump_where);
5095 }
5096 break;
5097
5098 case ISN_JUMP_IF_ARG_SET:
5099 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5100 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5101 iptr->isn_arg.jump.jump_where);
5102 break;
5103
5104 case ISN_FOR:
5105 {
5106 forloop_T *forloop = &iptr->isn_arg.forloop;
5107
5108 smsg("%s%4d FOR $%d -> %d", pfx, current,
5109 forloop->for_idx, forloop->for_end);
5110 }
5111 break;
5112
5113 case ISN_TRY:
5114 {
5115 try_T *try = &iptr->isn_arg.try;
5116
5117 if (try->try_ref->try_finally == 0)
5118 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5119 pfx, current,
5120 try->try_ref->try_catch,
5121 try->try_ref->try_endtry);
5122 else
5123 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5124 pfx, current,
5125 try->try_ref->try_catch,
5126 try->try_ref->try_finally,
5127 try->try_ref->try_endtry);
5128 }
5129 break;
5130 case ISN_CATCH:
5131 // TODO
5132 smsg("%s%4d CATCH", pfx, current);
5133 break;
5134 case ISN_TRYCONT:
5135 {
5136 trycont_T *trycont = &iptr->isn_arg.trycont;
5137
5138 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5139 trycont->tct_levels,
5140 trycont->tct_levels == 1 ? "" : "s",
5141 trycont->tct_where);
5142 }
5143 break;
5144 case ISN_FINALLY:
5145 smsg("%s%4d FINALLY", pfx, current);
5146 break;
5147 case ISN_ENDTRY:
5148 smsg("%s%4d ENDTRY", pfx, current);
5149 break;
5150 case ISN_THROW:
5151 smsg("%s%4d THROW", pfx, current);
5152 break;
5153
5154 // expression operations on number
5155 case ISN_OPNR:
5156 case ISN_OPFLOAT:
5157 case ISN_OPANY:
5158 {
5159 char *what;
5160 char *ins;
5161
5162 switch (iptr->isn_arg.op.op_type)
5163 {
5164 case EXPR_MULT: what = "*"; break;
5165 case EXPR_DIV: what = "/"; break;
5166 case EXPR_REM: what = "%"; break;
5167 case EXPR_SUB: what = "-"; break;
5168 case EXPR_ADD: what = "+"; break;
5169 default: what = "???"; break;
5170 }
5171 switch (iptr->isn_type)
5172 {
5173 case ISN_OPNR: ins = "OPNR"; break;
5174 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5175 case ISN_OPANY: ins = "OPANY"; break;
5176 default: ins = "???"; break;
5177 }
5178 smsg("%s%4d %s %s", pfx, current, ins, what);
5179 }
5180 break;
5181
5182 case ISN_COMPAREBOOL:
5183 case ISN_COMPARESPECIAL:
5184 case ISN_COMPARENR:
5185 case ISN_COMPAREFLOAT:
5186 case ISN_COMPARESTRING:
5187 case ISN_COMPAREBLOB:
5188 case ISN_COMPARELIST:
5189 case ISN_COMPAREDICT:
5190 case ISN_COMPAREFUNC:
5191 case ISN_COMPAREANY:
5192 {
5193 char *p;
5194 char buf[10];
5195 char *type;
5196
5197 switch (iptr->isn_arg.op.op_type)
5198 {
5199 case EXPR_EQUAL: p = "=="; break;
5200 case EXPR_NEQUAL: p = "!="; break;
5201 case EXPR_GREATER: p = ">"; break;
5202 case EXPR_GEQUAL: p = ">="; break;
5203 case EXPR_SMALLER: p = "<"; break;
5204 case EXPR_SEQUAL: p = "<="; break;
5205 case EXPR_MATCH: p = "=~"; break;
5206 case EXPR_IS: p = "is"; break;
5207 case EXPR_ISNOT: p = "isnot"; break;
5208 case EXPR_NOMATCH: p = "!~"; break;
5209 default: p = "???"; break;
5210 }
5211 STRCPY(buf, p);
5212 if (iptr->isn_arg.op.op_ic == TRUE)
5213 strcat(buf, "?");
5214 switch(iptr->isn_type)
5215 {
5216 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5217 case ISN_COMPARESPECIAL:
5218 type = "COMPARESPECIAL"; break;
5219 case ISN_COMPARENR: type = "COMPARENR"; break;
5220 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5221 case ISN_COMPARESTRING:
5222 type = "COMPARESTRING"; break;
5223 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5224 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5225 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5226 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5227 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5228 default: type = "???"; break;
5229 }
5230
5231 smsg("%s%4d %s %s", pfx, current, type, buf);
5232 }
5233 break;
5234
5235 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5236 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5237
5238 // expression operations
5239 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5240 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5241 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5242 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5243 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5244 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5245 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5246 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5247 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5248 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5249 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5250 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5251 pfx, current, iptr->isn_arg.number); break;
5252 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5253 pfx, current, iptr->isn_arg.number); break;
5254 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5255 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5256 iptr->isn_arg.string); break;
5257 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5258
5259 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5260 case ISN_CHECKTYPE:
5261 {
5262 checktype_T *ct = &iptr->isn_arg.type;
5263 char *tofree;
5264
5265 if (ct->ct_arg_idx == 0)
5266 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5267 type_name(ct->ct_type, &tofree),
5268 (int)ct->ct_off);
5269 else
5270 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5271 type_name(ct->ct_type, &tofree),
5272 (int)ct->ct_off,
5273 (int)ct->ct_arg_idx);
5274 vim_free(tofree);
5275 break;
5276 }
5277 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5278 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5279 iptr->isn_arg.checklen.cl_min_len);
5280 break;
5281 case ISN_SETTYPE:
5282 {
5283 char *tofree;
5284
5285 smsg("%s%4d SETTYPE %s", pfx, current,
5286 type_name(iptr->isn_arg.type.ct_type, &tofree));
5287 vim_free(tofree);
5288 break;
5289 }
5290 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005291 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5292 smsg("%s%4d INVERT %d (!val)", pfx, current,
5293 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005294 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005295 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5296 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005297 break;
5298 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005299 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005300 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005301 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5302 pfx, current,
5303 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005304 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005305 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5306 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005307 break;
5308 case ISN_PUT:
5309 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5310 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005311 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005312 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5313 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005314 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005315 else
5316 smsg("%s%4d PUT %c %ld", pfx, current,
5317 iptr->isn_arg.put.put_regname,
5318 (long)iptr->isn_arg.put.put_lnum);
5319 break;
5320
5321 // TODO: summarize modifiers
5322 case ISN_CMDMOD:
5323 {
5324 char_u *buf;
5325 size_t len = produce_cmdmods(
5326 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5327
5328 buf = alloc(len + 1);
5329 if (buf != NULL)
5330 {
5331 (void)produce_cmdmods(
5332 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5333 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5334 vim_free(buf);
5335 }
5336 break;
5337 }
5338 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5339
5340 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005341 smsg("%s%4d PROFILE START line %d", pfx, current,
5342 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005343 break;
5344
5345 case ISN_PROF_END:
5346 smsg("%s%4d PROFILE END", pfx, current);
5347 break;
5348
Bram Moolenaare99d4222021-06-13 14:01:26 +02005349 case ISN_DEBUG:
5350 smsg("%s%4d DEBUG line %d", pfx, current, iptr->isn_lnum);
5351 break;
5352
Bram Moolenaar4c137212021-04-19 16:48:48 +02005353 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5354 iptr->isn_arg.unpack.unp_count,
5355 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5356 break;
5357 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5358 iptr->isn_arg.shuffle.shfl_item,
5359 iptr->isn_arg.shuffle.shfl_up);
5360 break;
5361 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5362
5363 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5364 return;
5365 }
5366
5367 out_flush(); // output one line at a time
5368 ui_breakcheck();
5369 if (got_int)
5370 break;
5371 }
5372}
5373
5374/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005375 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005376 * We don't really need this at runtime, but we do have tests that require it,
5377 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 */
5379 void
5380ex_disassemble(exarg_T *eap)
5381{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005382 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005383 char_u *fname;
5384 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005385 dfunc_T *dfunc;
5386 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005387 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005388 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005389 compiletype_T compile_type = CT_NONE;
5390
5391 if (STRNCMP(arg, "profile", 7) == 0)
5392 {
5393 compile_type = CT_PROFILE;
5394 arg = skipwhite(arg + 7);
5395 }
5396 else if (STRNCMP(arg, "debug", 5) == 0)
5397 {
5398 compile_type = CT_DEBUG;
5399 arg = skipwhite(arg + 5);
5400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005401
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005402 if (STRNCMP(arg, "<lambda>", 8) == 0)
5403 {
5404 arg += 8;
5405 (void)getdigits(&arg);
5406 fname = vim_strnsave(eap->arg, arg - eap->arg);
5407 }
5408 else
5409 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005410 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005411 if (fname == NULL)
5412 {
5413 semsg(_(e_invarg2), eap->arg);
5414 return;
5415 }
5416
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005417 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005418 if (ufunc == NULL)
5419 {
5420 char_u *p = untrans_function_name(fname);
5421
5422 if (p != NULL)
5423 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005424 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005425 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005426 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427 if (ufunc == NULL)
5428 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005429 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 return;
5431 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005432 if (func_needs_compiling(ufunc, compile_type)
5433 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005434 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005435 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005437 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438 return;
5439 }
5440 if (ufunc->uf_name_exp != NULL)
5441 msg((char *)ufunc->uf_name_exp);
5442 else
5443 msg((char *)ufunc->uf_name);
5444
5445 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005446 switch (compile_type)
5447 {
5448 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005449#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005450 instr = dfunc->df_instr_prof;
5451 instr_count = dfunc->df_instr_prof_count;
5452 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005453#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005454 // FALLTHROUGH
5455 case CT_NONE:
5456 instr = dfunc->df_instr;
5457 instr_count = dfunc->df_instr_count;
5458 break;
5459 case CT_DEBUG:
5460 instr = dfunc->df_instr_debug;
5461 instr_count = dfunc->df_instr_debug_count;
5462 break;
5463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464
Bram Moolenaar4c137212021-04-19 16:48:48 +02005465 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466}
5467
5468/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005469 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470 * list, etc. Mostly like what JavaScript does, except that empty list and
5471 * empty dictionary are FALSE.
5472 */
5473 int
5474tv2bool(typval_T *tv)
5475{
5476 switch (tv->v_type)
5477 {
5478 case VAR_NUMBER:
5479 return tv->vval.v_number != 0;
5480 case VAR_FLOAT:
5481#ifdef FEAT_FLOAT
5482 return tv->vval.v_float != 0.0;
5483#else
5484 break;
5485#endif
5486 case VAR_PARTIAL:
5487 return tv->vval.v_partial != NULL;
5488 case VAR_FUNC:
5489 case VAR_STRING:
5490 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5491 case VAR_LIST:
5492 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5493 case VAR_DICT:
5494 return tv->vval.v_dict != NULL
5495 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5496 case VAR_BOOL:
5497 case VAR_SPECIAL:
5498 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5499 case VAR_JOB:
5500#ifdef FEAT_JOB_CHANNEL
5501 return tv->vval.v_job != NULL;
5502#else
5503 break;
5504#endif
5505 case VAR_CHANNEL:
5506#ifdef FEAT_JOB_CHANNEL
5507 return tv->vval.v_channel != NULL;
5508#else
5509 break;
5510#endif
5511 case VAR_BLOB:
5512 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5513 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005514 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005516 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005517 break;
5518 }
5519 return FALSE;
5520}
5521
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005522 void
5523emsg_using_string_as(typval_T *tv, int as_number)
5524{
5525 semsg(_(as_number ? e_using_string_as_number_str
5526 : e_using_string_as_bool_str),
5527 tv->vval.v_string == NULL
5528 ? (char_u *)"" : tv->vval.v_string);
5529}
5530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531/*
5532 * If "tv" is a string give an error and return FAIL.
5533 */
5534 int
5535check_not_string(typval_T *tv)
5536{
5537 if (tv->v_type == VAR_STRING)
5538 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005539 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540 clear_tv(tv);
5541 return FAIL;
5542 }
5543 return OK;
5544}
5545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546
5547#endif // FEAT_EVAL