blob: d55fa0c9d3aaf9c3694608e5016261c3a27908b0 [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 Moolenaar6d1792d2021-06-13 14:33:20 +0200741 compiletype_T compile_type = CT_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
Bram Moolenaar6d1792d2021-06-13 14:33:20 +0200743#ifdef FEAT_PROFILE
744 if (do_profiling == PROF_YES && ufunc->uf_profiling)
745 compile_type = CT_PROFILE;
746#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +0200747 if (func_needs_compiling(ufunc, compile_type)
748 && compile_def_function(ufunc, FALSE, compile_type, NULL)
749 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200750 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200751 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100752 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100753 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100754 if (error != FCERR_UNKNOWN)
755 {
756 if (error == FCERR_TOOMANY)
757 semsg(_(e_toomanyarg), ufunc->uf_name);
758 else
759 semsg(_(e_toofewarg), ufunc->uf_name);
760 return FAIL;
761 }
762
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100763 // The function has been compiled, can call it quickly. For a function
764 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100765 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100766 if (iptr != NULL)
767 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100768 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100769 iptr->isn_type = ISN_DCALL;
770 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
771 iptr->isn_arg.dfunc.cdf_argcount = argcount;
772 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200773 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775
776 if (call_prepare(argcount, argvars, ectx) == FAIL)
777 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200778 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 funcexe.evaluate = TRUE;
780
781 // Call the user function. Result goes in last position on the stack.
782 // TODO: add selfdict if there is one
783 error = call_user_func_check(ufunc, argcount, argvars,
784 STACK_TV_BOT(-1), &funcexe, NULL);
785
786 // Clear the arguments.
787 for (idx = 0; idx < argcount; ++idx)
788 clear_tv(&argvars[idx]);
789
790 if (error != FCERR_NONE)
791 {
792 user_func_error(error, ufunc->uf_name);
793 return FAIL;
794 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100795 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200796 // Error other than from calling the function itself.
797 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 return OK;
799}
800
801/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100802 * If command modifiers were applied restore them.
803 */
804 static void
805may_restore_cmdmod(funclocal_T *funclocal)
806{
807 if (funclocal->floc_restore_cmdmod)
808 {
809 cmdmod.cmod_filter_regmatch.regprog = NULL;
810 undo_cmdmod(&cmdmod);
811 cmdmod = funclocal->floc_save_cmdmod;
812 funclocal->floc_restore_cmdmod = FALSE;
813 }
814}
815
816/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200817 * Return TRUE if an error was given or CTRL-C was pressed.
818 */
819 static int
820vim9_aborting(int prev_called_emsg)
821{
822 return called_emsg > prev_called_emsg || got_int || did_throw;
823}
824
825/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 * Execute a function by "name".
827 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100828 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 * Returns FAIL if not found without an error message.
830 */
831 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100832call_by_name(
833 char_u *name,
834 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100835 ectx_T *ectx,
836 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837{
838 ufunc_T *ufunc;
839
840 if (builtin_function(name, -1))
841 {
842 int func_idx = find_internal_func(name);
843
844 if (func_idx < 0)
845 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200846 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 return FAIL;
848 return call_bfunc(func_idx, argcount, ectx);
849 }
850
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200851 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200852
853 if (ufunc == NULL)
854 {
855 int called_emsg_before = called_emsg;
856
857 if (script_autoload(name, TRUE))
858 // loaded a package, search for the function again
859 ufunc = find_func(name, FALSE, NULL);
860 if (vim9_aborting(called_emsg_before))
861 return FAIL; // bail out if loading the script caused an error
862 }
863
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100865 {
866 if (ufunc->uf_arg_types != NULL)
867 {
868 int i;
869 typval_T *argv = STACK_TV_BOT(0) - argcount;
870
871 // The function can change at runtime, check that the argument
872 // types are correct.
873 for (i = 0; i < argcount; ++i)
874 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100875 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100876
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100877 if (i < ufunc->uf_args.ga_len)
878 type = ufunc->uf_arg_types[i];
879 else if (ufunc->uf_va_type != NULL)
880 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100881 if (type != NULL && check_typval_arg_type(type,
882 &argv[i], i + 1) == FAIL)
883 return FAIL;
884 }
885 }
886
Bram Moolenaar4c137212021-04-19 16:48:48 +0200887 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889
890 return FAIL;
891}
892
893 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100894call_partial(
895 typval_T *tv,
896 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100897 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200899 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200900 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100902 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903
904 if (tv->v_type == VAR_PARTIAL)
905 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200906 partial_T *pt = tv->vval.v_partial;
907 int i;
908
909 if (pt->pt_argc > 0)
910 {
911 // Make space for arguments from the partial, shift the "argcount"
912 // arguments up.
913 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
914 return FAIL;
915 for (i = 1; i <= argcount; ++i)
916 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
917 ectx->ec_stack.ga_len += pt->pt_argc;
918 argcount += pt->pt_argc;
919
920 // copy the arguments from the partial onto the stack
921 for (i = 0; i < pt->pt_argc; ++i)
922 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924
925 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200926 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 name = pt->pt_name;
929 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200930 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200932 if (name != NULL)
933 {
934 char_u fname_buf[FLEN_FIXED + 1];
935 char_u *tofree = NULL;
936 int error = FCERR_NONE;
937 char_u *fname;
938
939 // May need to translate <SNR>123_ to K_SNR.
940 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
941 if (error != FCERR_NONE)
942 res = FAIL;
943 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200944 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200945 vim_free(tofree);
946 }
947
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100948 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949 {
950 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200951 semsg(_(e_unknownfunc),
952 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 return FAIL;
954 }
955 return OK;
956}
957
958/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200959 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
960 * TRUE.
961 */
962 static int
963error_if_locked(int lock, char *error)
964{
965 if (lock & (VAR_LOCKED | VAR_FIXED))
966 {
967 emsg(_(error));
968 return TRUE;
969 }
970 return FALSE;
971}
972
973/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100974 * Give an error if "tv" is not a number and return FAIL.
975 */
976 static int
977check_for_number(typval_T *tv)
978{
979 if (tv->v_type != VAR_NUMBER)
980 {
981 semsg(_(e_expected_str_but_got_str),
982 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
983 return FAIL;
984 }
985 return OK;
986}
987
988/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100989 * Store "tv" in variable "name".
990 * This is for s: and g: variables.
991 */
992 static void
993store_var(char_u *name, typval_T *tv)
994{
995 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200996 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100997
Bram Moolenaard877a572021-04-01 19:42:48 +0200998 if (tv->v_lock)
999 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001000 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001001 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001002 restore_funccal();
1003}
1004
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001005/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001006 * Convert "tv" to a string.
1007 * Return FAIL if not allowed.
1008 */
1009 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001010do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001011{
1012 if (tv->v_type != VAR_STRING)
1013 {
1014 char_u *str;
1015
1016 if (is_2string_any)
1017 {
1018 switch (tv->v_type)
1019 {
1020 case VAR_SPECIAL:
1021 case VAR_BOOL:
1022 case VAR_NUMBER:
1023 case VAR_FLOAT:
1024 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001025
1026 case VAR_LIST:
1027 if (tolerant)
1028 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001029 char_u *s, *e, *p;
1030 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001031
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001032 ga_init2(&ga, sizeof(char_u *), 1);
1033
1034 // Convert to NL separated items, then
1035 // escape the items and replace the NL with
1036 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001037 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001038 if (str == NULL)
1039 return FAIL;
1040 s = str;
1041 while ((e = vim_strchr(s, '\n')) != NULL)
1042 {
1043 *e = NUL;
1044 p = vim_strsave_fnameescape(s, FALSE);
1045 if (p != NULL)
1046 {
1047 ga_concat(&ga, p);
1048 ga_concat(&ga, (char_u *)" ");
1049 vim_free(p);
1050 }
1051 s = e + 1;
1052 }
1053 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001054 clear_tv(tv);
1055 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001056 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001057 return OK;
1058 }
1059 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001060 default: to_string_error(tv->v_type);
1061 return FAIL;
1062 }
1063 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001064 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001065 clear_tv(tv);
1066 tv->v_type = VAR_STRING;
1067 tv->vval.v_string = str;
1068 }
1069 return OK;
1070}
1071
1072/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001073 * When the value of "sv" is a null list of dict, allocate it.
1074 */
1075 static void
1076allocate_if_null(typval_T *tv)
1077{
1078 switch (tv->v_type)
1079 {
1080 case VAR_LIST:
1081 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001082 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001083 break;
1084 case VAR_DICT:
1085 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001086 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001087 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001088 case VAR_BLOB:
1089 if (tv->vval.v_blob == NULL)
1090 (void)rettv_blob_alloc(tv);
1091 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001092 default:
1093 break;
1094 }
1095}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001096
Bram Moolenaare7525c52021-01-09 13:20:37 +01001097/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001098 * Return the character "str[index]" where "index" is the character index,
1099 * including composing characters.
1100 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001101 */
1102 char_u *
1103char_from_string(char_u *str, varnumber_T index)
1104{
1105 size_t nbyte = 0;
1106 varnumber_T nchar = index;
1107 size_t slen;
1108
1109 if (str == NULL)
1110 return NULL;
1111 slen = STRLEN(str);
1112
Bram Moolenaarff871402021-03-26 13:34:05 +01001113 // Do the same as for a list: a negative index counts from the end.
1114 // Optimization to check the first byte to be below 0x80 (and no composing
1115 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001116 if (index < 0)
1117 {
1118 int clen = 0;
1119
1120 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001121 {
1122 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1123 ++nbyte;
1124 else if (enc_utf8)
1125 nbyte += utfc_ptr2len(str + nbyte);
1126 else
1127 nbyte += mb_ptr2len(str + nbyte);
1128 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001129 nchar = clen + index;
1130 if (nchar < 0)
1131 // unlike list: index out of range results in empty string
1132 return NULL;
1133 }
1134
1135 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001136 {
1137 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1138 ++nbyte;
1139 else if (enc_utf8)
1140 nbyte += utfc_ptr2len(str + nbyte);
1141 else
1142 nbyte += mb_ptr2len(str + nbyte);
1143 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001144 if (nbyte >= slen)
1145 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001146 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001147}
1148
1149/*
1150 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001151 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001152 * If going over the end return "str_len".
1153 * If "idx" is negative count from the end, -1 is the last character.
1154 * When going over the start return -1.
1155 */
1156 static long
1157char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1158{
1159 varnumber_T nchar = idx;
1160 size_t nbyte = 0;
1161
1162 if (nchar >= 0)
1163 {
1164 while (nchar > 0 && nbyte < str_len)
1165 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001166 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001167 --nchar;
1168 }
1169 }
1170 else
1171 {
1172 nbyte = str_len;
1173 while (nchar < 0 && nbyte > 0)
1174 {
1175 --nbyte;
1176 nbyte -= mb_head_off(str, str + nbyte);
1177 ++nchar;
1178 }
1179 if (nchar < 0)
1180 return -1;
1181 }
1182 return (long)nbyte;
1183}
1184
1185/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001186 * Return the slice "str[first : last]" using character indexes. Composing
1187 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001188 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001189 * Return NULL when the result is empty.
1190 */
1191 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001192string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001193{
1194 long start_byte, end_byte;
1195 size_t slen;
1196
1197 if (str == NULL)
1198 return NULL;
1199 slen = STRLEN(str);
1200 start_byte = char_idx2byte(str, slen, first);
1201 if (start_byte < 0)
1202 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001203 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001204 end_byte = (long)slen;
1205 else
1206 {
1207 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001208 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001209 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001210 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001211 }
1212
1213 if (start_byte >= (long)slen || end_byte <= start_byte)
1214 return NULL;
1215 return vim_strnsave(str + start_byte, end_byte - start_byte);
1216}
1217
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001218 static svar_T *
1219get_script_svar(scriptref_T *sref, ectx_T *ectx)
1220{
1221 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1222 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1223 + ectx->ec_dfunc_idx;
1224 svar_T *sv;
1225
1226 if (sref->sref_seq != si->sn_script_seq)
1227 {
1228 // The script was reloaded after the function was
1229 // compiled, the script_idx may not be valid.
1230 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1231 dfunc->df_ufunc->uf_name_exp);
1232 return NULL;
1233 }
1234 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1235 if (!equal_type(sv->sv_type, sref->sref_type))
1236 {
1237 emsg(_(e_script_variable_type_changed));
1238 return NULL;
1239 }
1240 return sv;
1241}
1242
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001243/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001244 * Function passed to do_cmdline() for splitting a script joined by NL
1245 * characters.
1246 */
1247 static char_u *
1248get_split_sourceline(
1249 int c UNUSED,
1250 void *cookie,
1251 int indent UNUSED,
1252 getline_opt_T options UNUSED)
1253{
1254 source_cookie_T *sp = (source_cookie_T *)cookie;
1255 char_u *p;
1256 char_u *line;
1257
1258 if (*sp->nextline == NUL)
1259 return NULL;
1260 p = vim_strchr(sp->nextline, '\n');
1261 if (p == NULL)
1262 {
1263 line = vim_strsave(sp->nextline);
1264 sp->nextline += STRLEN(sp->nextline);
1265 }
1266 else
1267 {
1268 line = vim_strnsave(sp->nextline, p - sp->nextline);
1269 sp->nextline = p + 1;
1270 }
1271 return line;
1272}
1273
1274/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 * Execute a function by "name".
1276 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001277 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 */
1279 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001280call_eval_func(
1281 char_u *name,
1282 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001283 ectx_T *ectx,
1284 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285{
Bram Moolenaared677f52020-08-12 16:38:10 +02001286 int called_emsg_before = called_emsg;
1287 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288
Bram Moolenaar4c137212021-04-19 16:48:48 +02001289 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001290 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001292 dictitem_T *v;
1293
1294 v = find_var(name, NULL, FALSE);
1295 if (v == NULL)
1296 {
1297 semsg(_(e_unknownfunc), name);
1298 return FAIL;
1299 }
1300 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1301 {
1302 semsg(_(e_unknownfunc), name);
1303 return FAIL;
1304 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001305 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001307 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308}
1309
1310/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001311 * When a function reference is used, fill a partial with the information
1312 * needed, especially when it is used as a closure.
1313 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001314 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001315fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1316{
1317 pt->pt_func = ufunc;
1318 pt->pt_refcount = 1;
1319
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001320 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001321 {
1322 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1323 + ectx->ec_dfunc_idx;
1324
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001325 // The closure may need to find arguments and local variables in the
1326 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001327 pt->pt_outer.out_stack = &ectx->ec_stack;
1328 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001329 if (ectx->ec_outer_ref != NULL)
1330 {
1331 // The current context already has a context, link to that one.
1332 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1333 if (ectx->ec_outer_ref->or_partial != NULL)
1334 {
1335 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1336 ++pt->pt_outer.out_up_partial->pt_refcount;
1337 }
1338 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001339
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001340 // If this function returns and the closure is still being used, we
1341 // need to make a copy of the context (arguments and local variables).
1342 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001343 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1344 {
1345 vim_free(pt);
1346 return FAIL;
1347 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001348 // Extra variable keeps the count of closures created in the current
1349 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001350 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1351 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1352
1353 ((partial_T **)ectx->ec_funcrefs.ga_data)
1354 [ectx->ec_funcrefs.ga_len] = pt;
1355 ++pt->pt_refcount;
1356 ++ectx->ec_funcrefs.ga_len;
1357 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001358 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001359 return OK;
1360}
1361
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001362// used for v_instr of typval of VAR_INSTR
1363struct instr_S {
1364 ectx_T *instr_ectx;
1365 isn_T *instr_instr;
1366};
1367
Bram Moolenaar4c137212021-04-19 16:48:48 +02001368// used for substitute_instr
1369typedef struct subs_expr_S {
1370 ectx_T *subs_ectx;
1371 isn_T *subs_instr;
1372 int subs_status;
1373} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374
1375// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001376#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
1378// Get pointer to item at the bottom of the stack, -1 is the bottom.
1379#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001380#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 +01001381
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001382// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001383#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 +01001384
Bram Moolenaar4c137212021-04-19 16:48:48 +02001385/*
1386 * Execute instructions in execution context "ectx".
1387 * Return OK or FAIL;
1388 */
1389 static int
1390exec_instructions(ectx_T *ectx)
1391{
1392 int breakcheck_count = 0;
1393 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001394 int ret = FAIL;
1395 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001396
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001397 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001398 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001399
Bram Moolenaarff652882021-05-16 15:24:49 +02001400 // Only catch exceptions in this instruction list.
1401 ectx->ec_trylevel_at_start = trylevel;
1402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 for (;;)
1404 {
1405 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001406
Bram Moolenaar270d0382020-05-15 21:42:53 +02001407 if (++breakcheck_count >= 100)
1408 {
1409 line_breakcheck();
1410 breakcheck_count = 0;
1411 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001412 if (got_int)
1413 {
1414 // Turn CTRL-C into an exception.
1415 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001416 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001417 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001418 did_throw = TRUE;
1419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420
Bram Moolenaara26b9702020-04-18 19:53:28 +02001421 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1422 {
1423 // Turn an error message into an exception.
1424 did_emsg = FALSE;
1425 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001426 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001427 did_throw = TRUE;
1428 *msg_list = NULL;
1429 }
1430
Bram Moolenaar4c137212021-04-19 16:48:48 +02001431 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001433 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001434 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435
1436 // An exception jumps to the first catch, finally, or returns from
1437 // the current function.
1438 if (trystack->ga_len > 0)
1439 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001440 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 {
1442 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001443 ectx->ec_in_catch = TRUE;
1444 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 }
1446 else
1447 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001448 // Not inside try or need to return from current functions.
1449 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001450 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001451 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001452 tv = STACK_TV_BOT(0);
1453 tv->v_type = VAR_NUMBER;
1454 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001455 ++ectx->ec_stack.ga_len;
1456 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001458 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001459 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001460 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001461 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 goto done;
1463 }
1464
Bram Moolenaar4c137212021-04-19 16:48:48 +02001465 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001466 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 }
1468 continue;
1469 }
1470
Bram Moolenaar4c137212021-04-19 16:48:48 +02001471 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 switch (iptr->isn_type)
1473 {
1474 // execute Ex command line
1475 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001476 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001477 source_cookie_T cookie;
1478
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001479 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001480 // Pass getsourceline to get an error for a missing ":end"
1481 // command.
1482 CLEAR_FIELD(cookie);
1483 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1484 if (do_cmdline(iptr->isn_arg.string,
1485 getsourceline, &cookie,
1486 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1487 == FAIL
1488 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001489 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 break;
1492
Bram Moolenaar20677332021-06-06 17:02:53 +02001493 // execute Ex command line split at NL characters.
1494 case ISN_EXEC_SPLIT:
1495 {
1496 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001497 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001498
1499 SOURCING_LNUM = iptr->isn_lnum;
1500 CLEAR_FIELD(cookie);
1501 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1502 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001503 line = get_split_sourceline(0, &cookie, 0, 0);
1504 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001505 get_split_sourceline, &cookie,
1506 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1507 == FAIL
1508 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001509 {
1510 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001511 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001512 }
1513 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001514 }
1515 break;
1516
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001517 // Evaluate an expression with legacy syntax, push it onto the
1518 // stack.
1519 case ISN_LEGACY_EVAL:
1520 {
1521 char_u *arg = iptr->isn_arg.string;
1522 int res;
1523 int save_flags = cmdmod.cmod_flags;
1524
1525 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001526 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001527 tv = STACK_TV_BOT(0);
1528 init_tv(tv);
1529 cmdmod.cmod_flags |= CMOD_LEGACY;
1530 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1531 cmdmod.cmod_flags = save_flags;
1532 if (res == FAIL)
1533 goto on_error;
1534 ++ectx->ec_stack.ga_len;
1535 }
1536 break;
1537
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001538 // push typeval VAR_INSTR with instructions to be executed
1539 case ISN_INSTR:
1540 {
1541 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001542 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001543 tv = STACK_TV_BOT(0);
1544 tv->vval.v_instr = ALLOC_ONE(instr_T);
1545 if (tv->vval.v_instr == NULL)
1546 goto on_error;
1547 ++ectx->ec_stack.ga_len;
1548
1549 tv->v_type = VAR_INSTR;
1550 tv->vval.v_instr->instr_ectx = ectx;
1551 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1552 }
1553 break;
1554
Bram Moolenaar4c137212021-04-19 16:48:48 +02001555 // execute :substitute with an expression
1556 case ISN_SUBSTITUTE:
1557 {
1558 subs_T *subs = &iptr->isn_arg.subs;
1559 source_cookie_T cookie;
1560 struct subs_expr_S *save_instr = substitute_instr;
1561 struct subs_expr_S subs_instr;
1562 int res;
1563
1564 subs_instr.subs_ectx = ectx;
1565 subs_instr.subs_instr = subs->subs_instr;
1566 subs_instr.subs_status = OK;
1567 substitute_instr = &subs_instr;
1568
1569 SOURCING_LNUM = iptr->isn_lnum;
1570 // This is very much like ISN_EXEC
1571 CLEAR_FIELD(cookie);
1572 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1573 res = do_cmdline(subs->subs_cmd,
1574 getsourceline, &cookie,
1575 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1576 substitute_instr = save_instr;
1577
1578 if (res == FAIL || did_emsg
1579 || subs_instr.subs_status == FAIL)
1580 goto on_error;
1581 }
1582 break;
1583
1584 case ISN_FINISH:
1585 goto done;
1586
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001587 case ISN_REDIRSTART:
1588 // create a dummy entry for var_redir_str()
1589 if (alloc_redir_lval() == FAIL)
1590 goto on_error;
1591
1592 // The output is stored in growarray "redir_ga" until
1593 // redirection ends.
1594 init_redir_ga();
1595 redir_vname = 1;
1596 break;
1597
1598 case ISN_REDIREND:
1599 {
1600 char_u *res = get_clear_redir_ga();
1601
1602 // End redirection, put redirected text on the stack.
1603 clear_redir_lval();
1604 redir_vname = 0;
1605
1606 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1607 {
1608 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001609 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001610 }
1611 tv = STACK_TV_BOT(0);
1612 tv->v_type = VAR_STRING;
1613 tv->vval.v_string = res;
1614 ++ectx->ec_stack.ga_len;
1615 }
1616 break;
1617
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001618 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001619#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001620 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1621 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001622#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001623 break;
1624
1625 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001626#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001627 {
1628 exarg_T ea;
1629 int res;
1630
1631 CLEAR_FIELD(ea);
1632 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1633 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1634 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1635 --ectx->ec_stack.ga_len;
1636 tv = STACK_TV_BOT(0);
1637 res = cexpr_core(&ea, tv);
1638 clear_tv(tv);
1639 if (res == FAIL)
1640 goto on_error;
1641 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001642#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001643 break;
1644
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001645 // execute Ex command from pieces on the stack
1646 case ISN_EXECCONCAT:
1647 {
1648 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001649 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001650 int pass;
1651 int i;
1652 char_u *cmd = NULL;
1653 char_u *str;
1654
1655 for (pass = 1; pass <= 2; ++pass)
1656 {
1657 for (i = 0; i < count; ++i)
1658 {
1659 tv = STACK_TV_BOT(i - count);
1660 str = tv->vval.v_string;
1661 if (str != NULL && *str != NUL)
1662 {
1663 if (pass == 2)
1664 STRCPY(cmd + len, str);
1665 len += STRLEN(str);
1666 }
1667 if (pass == 2)
1668 clear_tv(tv);
1669 }
1670 if (pass == 1)
1671 {
1672 cmd = alloc(len + 1);
1673 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001674 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001675 len = 0;
1676 }
1677 }
1678
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001679 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001680 do_cmdline_cmd(cmd);
1681 vim_free(cmd);
1682 }
1683 break;
1684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 // execute :echo {string} ...
1686 case ISN_ECHO:
1687 {
1688 int count = iptr->isn_arg.echo.echo_count;
1689 int atstart = TRUE;
1690 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001691 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692
1693 for (idx = 0; idx < count; ++idx)
1694 {
1695 tv = STACK_TV_BOT(idx - count);
1696 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1697 &atstart, &needclr);
1698 clear_tv(tv);
1699 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001700 if (needclr)
1701 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001702 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 }
1704 break;
1705
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001706 // :execute {string} ...
1707 // :echomsg {string} ...
1708 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001709 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001710 case ISN_ECHOMSG:
1711 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001712 {
1713 int count = iptr->isn_arg.number;
1714 garray_T ga;
1715 char_u buf[NUMBUFLEN];
1716 char_u *p;
1717 int len;
1718 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001719 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001720
1721 ga_init2(&ga, 1, 80);
1722 for (idx = 0; idx < count; ++idx)
1723 {
1724 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001725 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001726 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001727 if (tv->v_type == VAR_CHANNEL
1728 || tv->v_type == VAR_JOB)
1729 {
1730 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001731 semsg(_(e_using_invalid_value_as_string_str),
1732 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001733 break;
1734 }
1735 else
1736 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001737 }
1738 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001739 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001740
1741 len = (int)STRLEN(p);
1742 if (ga_grow(&ga, len + 2) == FAIL)
1743 failed = TRUE;
1744 else
1745 {
1746 if (ga.ga_len > 0)
1747 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1748 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1749 ga.ga_len += len;
1750 }
1751 clear_tv(tv);
1752 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001753 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001754 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001755 {
1756 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001757 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001758 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001759
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001760 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001761 {
1762 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001763 {
1764 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001765 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001766 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001767 {
1768 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001769 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001770 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001771 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001772 else
1773 {
1774 msg_sb_eol();
1775 if (iptr->isn_type == ISN_ECHOMSG)
1776 {
1777 msg_attr(ga.ga_data, echo_attr);
1778 out_flush();
1779 }
1780 else
1781 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001782 SOURCING_LNUM = iptr->isn_lnum;
1783 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001784 }
1785 }
1786 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001787 ga_clear(&ga);
1788 }
1789 break;
1790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 // load local variable or argument
1792 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001793 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001794 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001796 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 break;
1798
1799 // load v: variable
1800 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001801 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001802 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001804 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 break;
1806
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001807 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 case ISN_LOADSCRIPT:
1809 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001810 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 svar_T *sv;
1812
Bram Moolenaar4c137212021-04-19 16:48:48 +02001813 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001814 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001815 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001816 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001817 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001818 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001820 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 }
1822 break;
1823
1824 // load s: variable in old script
1825 case ISN_LOADS:
1826 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001827 hashtab_T *ht = &SCRIPT_VARS(
1828 iptr->isn_arg.loadstore.ls_sid);
1829 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 if (di == NULL)
1833 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001834 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001835 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001836 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 }
1838 else
1839 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001840 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001841 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001843 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 }
1845 }
1846 break;
1847
Bram Moolenaard3aac292020-04-19 14:32:17 +02001848 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001850 case ISN_LOADB:
1851 case ISN_LOADW:
1852 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001854 dictitem_T *di = NULL;
1855 hashtab_T *ht = NULL;
1856 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001857
Bram Moolenaard3aac292020-04-19 14:32:17 +02001858 switch (iptr->isn_type)
1859 {
1860 case ISN_LOADG:
1861 ht = get_globvar_ht();
1862 namespace = 'g';
1863 break;
1864 case ISN_LOADB:
1865 ht = &curbuf->b_vars->dv_hashtab;
1866 namespace = 'b';
1867 break;
1868 case ISN_LOADW:
1869 ht = &curwin->w_vars->dv_hashtab;
1870 namespace = 'w';
1871 break;
1872 case ISN_LOADT:
1873 ht = &curtab->tp_vars->dv_hashtab;
1874 namespace = 't';
1875 break;
1876 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001877 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001878 }
1879 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 if (di == NULL)
1882 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001883 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001884 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001885 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001886 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 }
1888 else
1889 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001890 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001891 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001893 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 }
1895 }
1896 break;
1897
Bram Moolenaar03290b82020-12-19 16:30:44 +01001898 // load autoload variable
1899 case ISN_LOADAUTO:
1900 {
1901 char_u *name = iptr->isn_arg.string;
1902
Bram Moolenaar4c137212021-04-19 16:48:48 +02001903 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001904 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001905 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001906 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001907 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001908 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001909 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001910 }
1911 break;
1912
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001913 // load g:/b:/w:/t: namespace
1914 case ISN_LOADGDICT:
1915 case ISN_LOADBDICT:
1916 case ISN_LOADWDICT:
1917 case ISN_LOADTDICT:
1918 {
1919 dict_T *d = NULL;
1920
1921 switch (iptr->isn_type)
1922 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001923 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1924 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1925 case ISN_LOADWDICT: d = curwin->w_vars; break;
1926 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001927 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001928 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001929 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001930 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001931 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001932 tv = STACK_TV_BOT(0);
1933 tv->v_type = VAR_DICT;
1934 tv->v_lock = 0;
1935 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001936 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001937 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001938 }
1939 break;
1940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 // load &option
1942 case ISN_LOADOPT:
1943 {
1944 typval_T optval;
1945 char_u *name = iptr->isn_arg.string;
1946
Bram Moolenaara8c17702020-04-01 21:17:24 +02001947 // This is not expected to fail, name is checked during
1948 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001949 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001950 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001951 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001952 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001954 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 }
1956 break;
1957
1958 // load $ENV
1959 case ISN_LOADENV:
1960 {
1961 typval_T optval;
1962 char_u *name = iptr->isn_arg.string;
1963
Bram Moolenaar4c137212021-04-19 16:48:48 +02001964 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001965 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001966 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001967 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001969 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 }
1971 break;
1972
1973 // load @register
1974 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001975 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001976 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 tv = STACK_TV_BOT(0);
1978 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001979 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001980 // This may result in NULL, which should be equivalent to an
1981 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 tv->vval.v_string = get_reg_contents(
1983 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001984 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 break;
1986
1987 // store local variable
1988 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001989 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 tv = STACK_TV_VAR(iptr->isn_arg.number);
1991 clear_tv(tv);
1992 *tv = *STACK_TV_BOT(0);
1993 break;
1994
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001995 // store s: variable in old script
1996 case ISN_STORES:
1997 {
1998 hashtab_T *ht = &SCRIPT_VARS(
1999 iptr->isn_arg.loadstore.ls_sid);
2000 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002001 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002002
Bram Moolenaar4c137212021-04-19 16:48:48 +02002003 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002004 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002005 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002006 else
2007 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002008 SOURCING_LNUM = iptr->isn_lnum;
2009 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002010 {
2011 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002012 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002013 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002014 clear_tv(&di->di_tv);
2015 di->di_tv = *STACK_TV_BOT(0);
2016 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002017 }
2018 break;
2019
2020 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 case ISN_STORESCRIPT:
2022 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002023 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2024 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025
Bram Moolenaar4c137212021-04-19 16:48:48 +02002026 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002027 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002028 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002029 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002030
2031 // "const" and "final" are checked at compile time, locking
2032 // the value needs to be checked here.
2033 SOURCING_LNUM = iptr->isn_lnum;
2034 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002035 {
2036 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002037 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002038 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 clear_tv(sv->sv_tv);
2041 *sv->sv_tv = *STACK_TV_BOT(0);
2042 }
2043 break;
2044
2045 // store option
2046 case ISN_STOREOPT:
2047 {
2048 long n = 0;
2049 char_u *s = NULL;
2050 char *msg;
2051
Bram Moolenaar4c137212021-04-19 16:48:48 +02002052 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 tv = STACK_TV_BOT(0);
2054 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002055 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002057 if (s == NULL)
2058 s = (char_u *)"";
2059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002061 // must be VAR_NUMBER, CHECKTYPE makes sure
2062 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2064 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002065 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 if (msg != NULL)
2067 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002068 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002070 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 }
2073 break;
2074
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002075 // store $ENV
2076 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002077 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002078 tv = STACK_TV_BOT(0);
2079 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2080 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002081 break;
2082
2083 // store @r
2084 case ISN_STOREREG:
2085 {
2086 int reg = iptr->isn_arg.number;
2087
Bram Moolenaar4c137212021-04-19 16:48:48 +02002088 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002089 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002090 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002091 tv_get_string(tv), -1, FALSE);
2092 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002093 }
2094 break;
2095
2096 // store v: variable
2097 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002098 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002099 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2100 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002101 // should not happen, type is checked when compiling
2102 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002103 break;
2104
Bram Moolenaard3aac292020-04-19 14:32:17 +02002105 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002107 case ISN_STOREB:
2108 case ISN_STOREW:
2109 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002111 dictitem_T *di;
2112 hashtab_T *ht;
2113 char_u *name = iptr->isn_arg.string + 2;
2114
Bram Moolenaard3aac292020-04-19 14:32:17 +02002115 switch (iptr->isn_type)
2116 {
2117 case ISN_STOREG:
2118 ht = get_globvar_ht();
2119 break;
2120 case ISN_STOREB:
2121 ht = &curbuf->b_vars->dv_hashtab;
2122 break;
2123 case ISN_STOREW:
2124 ht = &curwin->w_vars->dv_hashtab;
2125 break;
2126 case ISN_STORET:
2127 ht = &curtab->tp_vars->dv_hashtab;
2128 break;
2129 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002130 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132
Bram Moolenaar4c137212021-04-19 16:48:48 +02002133 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002134 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002136 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 else
2138 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002139 SOURCING_LNUM = iptr->isn_lnum;
2140 if (var_check_permission(di, name) == FAIL)
2141 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 clear_tv(&di->di_tv);
2143 di->di_tv = *STACK_TV_BOT(0);
2144 }
2145 }
2146 break;
2147
Bram Moolenaar03290b82020-12-19 16:30:44 +01002148 // store an autoload variable
2149 case ISN_STOREAUTO:
2150 SOURCING_LNUM = iptr->isn_lnum;
2151 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2152 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002153 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002154 break;
2155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 // store number in local variable
2157 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002158 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 clear_tv(tv);
2160 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002161 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 break;
2163
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002164 // store value in list or dict variable
2165 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002166 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002167 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002168 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002169 typval_T *tv_dest = STACK_TV_BOT(-1);
2170 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002171
Bram Moolenaar752fc692021-01-04 21:57:11 +01002172 // Stack contains:
2173 // -3 value to be stored
2174 // -2 index
2175 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002176 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002177 SOURCING_LNUM = iptr->isn_lnum;
2178 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002179 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002180 dest_type = tv_dest->v_type;
2181 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002182 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002183 else if (dest_type == VAR_LIST
2184 && tv_idx->v_type != VAR_NUMBER)
2185 {
2186 emsg(_(e_number_exp));
2187 status = FAIL;
2188 }
2189 }
2190 else if (dest_type != tv_dest->v_type)
2191 {
2192 // just in case, should be OK
2193 semsg(_(e_expected_str_but_got_str),
2194 vartype_name(dest_type),
2195 vartype_name(tv_dest->v_type));
2196 status = FAIL;
2197 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002198
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002199 if (status == OK && dest_type == VAR_LIST)
2200 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002201 long lidx = (long)tv_idx->vval.v_number;
2202 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002203
2204 if (list == NULL)
2205 {
2206 emsg(_(e_list_not_set));
2207 goto on_error;
2208 }
2209 if (lidx < 0 && list->lv_len + lidx >= 0)
2210 // negative index is relative to the end
2211 lidx = list->lv_len + lidx;
2212 if (lidx < 0 || lidx > list->lv_len)
2213 {
2214 semsg(_(e_listidx), lidx);
2215 goto on_error;
2216 }
2217 if (lidx < list->lv_len)
2218 {
2219 listitem_T *li = list_find(list, lidx);
2220
2221 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002222 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002223 goto on_error;
2224 // overwrite existing list item
2225 clear_tv(&li->li_tv);
2226 li->li_tv = *tv;
2227 }
2228 else
2229 {
2230 if (error_if_locked(list->lv_lock,
2231 e_cannot_change_list))
2232 goto on_error;
2233 // append to list, only fails when out of memory
2234 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002235 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002236 clear_tv(tv);
2237 }
2238 }
2239 else if (status == OK && dest_type == VAR_DICT)
2240 {
2241 char_u *key = tv_idx->vval.v_string;
2242 dict_T *dict = tv_dest->vval.v_dict;
2243 dictitem_T *di;
2244
2245 SOURCING_LNUM = iptr->isn_lnum;
2246 if (dict == NULL)
2247 {
2248 emsg(_(e_dictionary_not_set));
2249 goto on_error;
2250 }
2251 if (key == NULL)
2252 key = (char_u *)"";
2253 di = dict_find(dict, key, -1);
2254 if (di != NULL)
2255 {
2256 if (error_if_locked(di->di_tv.v_lock,
2257 e_cannot_change_dict_item))
2258 goto on_error;
2259 // overwrite existing value
2260 clear_tv(&di->di_tv);
2261 di->di_tv = *tv;
2262 }
2263 else
2264 {
2265 if (error_if_locked(dict->dv_lock,
2266 e_cannot_change_dict))
2267 goto on_error;
2268 // add to dict, only fails when out of memory
2269 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002270 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002271 clear_tv(tv);
2272 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002273 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002274 else if (status == OK && dest_type == VAR_BLOB)
2275 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002276 long lidx = (long)tv_idx->vval.v_number;
2277 blob_T *blob = tv_dest->vval.v_blob;
2278 varnumber_T nr;
2279 int error = FALSE;
2280 int len;
2281
2282 if (blob == NULL)
2283 {
2284 emsg(_(e_blob_not_set));
2285 goto on_error;
2286 }
2287 len = blob_len(blob);
2288 if (lidx < 0 && len + lidx >= 0)
2289 // negative index is relative to the end
2290 lidx = len + lidx;
2291
2292 // Can add one byte at the end.
2293 if (lidx < 0 || lidx > len)
2294 {
2295 semsg(_(e_blobidx), lidx);
2296 goto on_error;
2297 }
2298 if (value_check_lock(blob->bv_lock,
2299 (char_u *)"blob", FALSE))
2300 goto on_error;
2301 nr = tv_get_number_chk(tv, &error);
2302 if (error)
2303 goto on_error;
2304 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002305 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002306 else
2307 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002308 status = FAIL;
2309 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002310 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002311
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002312 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002313 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002314 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002315 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002316 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002317 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002318 goto on_error;
2319 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002320 }
2321 break;
2322
Bram Moolenaar68452172021-04-12 21:21:02 +02002323 // store value in blob range
2324 case ISN_STORERANGE:
2325 {
2326 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2327 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2328 typval_T *tv_dest = STACK_TV_BOT(-1);
2329 int status = OK;
2330
2331 // Stack contains:
2332 // -4 value to be stored
2333 // -3 first index or "none"
2334 // -2 second index or "none"
2335 // -1 destination blob
2336 tv = STACK_TV_BOT(-4);
2337 if (tv_dest->v_type != VAR_BLOB)
2338 {
2339 status = FAIL;
2340 emsg(_(e_blob_required));
2341 }
2342 else
2343 {
2344 varnumber_T n1;
2345 varnumber_T n2;
2346 int error = FALSE;
2347
2348 n1 = tv_get_number_chk(tv_idx1, &error);
2349 if (error)
2350 status = FAIL;
2351 else
2352 {
2353 if (tv_idx2->v_type == VAR_SPECIAL
2354 && tv_idx2->vval.v_number == VVAL_NONE)
2355 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2356 else
2357 n2 = tv_get_number_chk(tv_idx2, &error);
2358 if (error)
2359 status = FAIL;
2360 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002361 {
2362 long bloblen = blob_len(tv_dest->vval.v_blob);
2363
2364 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002365 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002366 || check_blob_range(bloblen,
2367 n1, n2, FALSE) == FAIL)
2368 status = FAIL;
2369 else
2370 status = blob_set_range(
2371 tv_dest->vval.v_blob, n1, n2, tv);
2372 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002373 }
2374 }
2375
2376 clear_tv(tv_idx1);
2377 clear_tv(tv_idx2);
2378 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002379 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002380 clear_tv(tv);
2381
2382 if (status == FAIL)
2383 goto on_error;
2384 }
2385 break;
2386
Bram Moolenaar0186e582021-01-10 18:33:11 +01002387 // load or store variable or argument from outer scope
2388 case ISN_LOADOUTER:
2389 case ISN_STOREOUTER:
2390 {
2391 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002392 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2393 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002394
2395 while (depth > 1 && outer != NULL)
2396 {
2397 outer = outer->out_up;
2398 --depth;
2399 }
2400 if (outer == NULL)
2401 {
2402 SOURCING_LNUM = iptr->isn_lnum;
2403 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002404 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002405 }
2406 tv = ((typval_T *)outer->out_stack->ga_data)
2407 + outer->out_frame_idx + STACK_FRAME_SIZE
2408 + iptr->isn_arg.outer.outer_idx;
2409 if (iptr->isn_type == ISN_LOADOUTER)
2410 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002411 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002412 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002413 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002414 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002415 }
2416 else
2417 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002418 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002419 clear_tv(tv);
2420 *tv = *STACK_TV_BOT(0);
2421 }
2422 }
2423 break;
2424
Bram Moolenaar752fc692021-01-04 21:57:11 +01002425 // unlet item in list or dict variable
2426 case ISN_UNLETINDEX:
2427 {
2428 typval_T *tv_idx = STACK_TV_BOT(-2);
2429 typval_T *tv_dest = STACK_TV_BOT(-1);
2430 int status = OK;
2431
2432 // Stack contains:
2433 // -2 index
2434 // -1 dict or list
2435 if (tv_dest->v_type == VAR_DICT)
2436 {
2437 // unlet a dict item, index must be a string
2438 if (tv_idx->v_type != VAR_STRING)
2439 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002440 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002441 semsg(_(e_expected_str_but_got_str),
2442 vartype_name(VAR_STRING),
2443 vartype_name(tv_idx->v_type));
2444 status = FAIL;
2445 }
2446 else
2447 {
2448 dict_T *d = tv_dest->vval.v_dict;
2449 char_u *key = tv_idx->vval.v_string;
2450 dictitem_T *di = NULL;
2451
2452 if (key == NULL)
2453 key = (char_u *)"";
2454 if (d != NULL)
2455 di = dict_find(d, key, (int)STRLEN(key));
2456 if (di == NULL)
2457 {
2458 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002459 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002460 semsg(_(e_dictkey), key);
2461 status = FAIL;
2462 }
2463 else
2464 {
2465 // TODO: check for dict or item locked
2466 dictitem_remove(d, di);
2467 }
2468 }
2469 }
2470 else if (tv_dest->v_type == VAR_LIST)
2471 {
2472 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002473 SOURCING_LNUM = iptr->isn_lnum;
2474 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002475 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002476 status = FAIL;
2477 }
2478 else
2479 {
2480 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002481 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002482 listitem_T *li = NULL;
2483
2484 li = list_find(l, n);
2485 if (li == NULL)
2486 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002487 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002488 semsg(_(e_listidx), n);
2489 status = FAIL;
2490 }
2491 else
2492 // TODO: check for list or item locked
2493 listitem_remove(l, li);
2494 }
2495 }
2496 else
2497 {
2498 status = FAIL;
2499 semsg(_(e_cannot_index_str),
2500 vartype_name(tv_dest->v_type));
2501 }
2502
2503 clear_tv(tv_idx);
2504 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002505 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002506 if (status == FAIL)
2507 goto on_error;
2508 }
2509 break;
2510
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002511 // unlet range of items in list variable
2512 case ISN_UNLETRANGE:
2513 {
2514 // Stack contains:
2515 // -3 index1
2516 // -2 index2
2517 // -1 dict or list
2518 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2519 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2520 typval_T *tv_dest = STACK_TV_BOT(-1);
2521 int status = OK;
2522
2523 if (tv_dest->v_type == VAR_LIST)
2524 {
2525 // indexes must be a number
2526 SOURCING_LNUM = iptr->isn_lnum;
2527 if (check_for_number(tv_idx1) == FAIL
2528 || check_for_number(tv_idx2) == FAIL)
2529 {
2530 status = FAIL;
2531 }
2532 else
2533 {
2534 list_T *l = tv_dest->vval.v_list;
2535 long n1 = (long)tv_idx1->vval.v_number;
2536 long n2 = (long)tv_idx2->vval.v_number;
2537 listitem_T *li;
2538
2539 li = list_find_index(l, &n1);
2540 if (li == NULL
2541 || list_unlet_range(l, li, NULL, n1,
2542 TRUE, n2) == FAIL)
2543 status = FAIL;
2544 }
2545 }
2546 else
2547 {
2548 status = FAIL;
2549 SOURCING_LNUM = iptr->isn_lnum;
2550 semsg(_(e_cannot_index_str),
2551 vartype_name(tv_dest->v_type));
2552 }
2553
2554 clear_tv(tv_idx1);
2555 clear_tv(tv_idx2);
2556 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002557 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002558 if (status == FAIL)
2559 goto on_error;
2560 }
2561 break;
2562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 // push constant
2564 case ISN_PUSHNR:
2565 case ISN_PUSHBOOL:
2566 case ISN_PUSHSPEC:
2567 case ISN_PUSHF:
2568 case ISN_PUSHS:
2569 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002570 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002571 case ISN_PUSHCHANNEL:
2572 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002573 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002574 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002576 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002577 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 switch (iptr->isn_type)
2579 {
2580 case ISN_PUSHNR:
2581 tv->v_type = VAR_NUMBER;
2582 tv->vval.v_number = iptr->isn_arg.number;
2583 break;
2584 case ISN_PUSHBOOL:
2585 tv->v_type = VAR_BOOL;
2586 tv->vval.v_number = iptr->isn_arg.number;
2587 break;
2588 case ISN_PUSHSPEC:
2589 tv->v_type = VAR_SPECIAL;
2590 tv->vval.v_number = iptr->isn_arg.number;
2591 break;
2592#ifdef FEAT_FLOAT
2593 case ISN_PUSHF:
2594 tv->v_type = VAR_FLOAT;
2595 tv->vval.v_float = iptr->isn_arg.fnumber;
2596 break;
2597#endif
2598 case ISN_PUSHBLOB:
2599 blob_copy(iptr->isn_arg.blob, tv);
2600 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002601 case ISN_PUSHFUNC:
2602 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002603 if (iptr->isn_arg.string == NULL)
2604 tv->vval.v_string = NULL;
2605 else
2606 tv->vval.v_string =
2607 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002608 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002609 case ISN_PUSHCHANNEL:
2610#ifdef FEAT_JOB_CHANNEL
2611 tv->v_type = VAR_CHANNEL;
2612 tv->vval.v_channel = iptr->isn_arg.channel;
2613 if (tv->vval.v_channel != NULL)
2614 ++tv->vval.v_channel->ch_refcount;
2615#endif
2616 break;
2617 case ISN_PUSHJOB:
2618#ifdef FEAT_JOB_CHANNEL
2619 tv->v_type = VAR_JOB;
2620 tv->vval.v_job = iptr->isn_arg.job;
2621 if (tv->vval.v_job != NULL)
2622 ++tv->vval.v_job->jv_refcount;
2623#endif
2624 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 default:
2626 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002627 tv->vval.v_string = vim_strsave(
2628 iptr->isn_arg.string == NULL
2629 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 }
2631 break;
2632
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002633 case ISN_UNLET:
2634 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2635 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002636 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002637 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002638 case ISN_UNLETENV:
2639 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2640 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002641
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002642 case ISN_LOCKCONST:
2643 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2644 break;
2645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 // create a list from items on the stack; uses a single allocation
2647 // for the list header and the items
2648 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002649 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002650 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 break;
2652
2653 // create a dict from items on the stack
2654 case ISN_NEWDICT:
2655 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002656 int count = iptr->isn_arg.number;
2657 dict_T *dict = dict_alloc();
2658 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002659 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002660 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661
2662 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002663 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 for (idx = 0; idx < count; ++idx)
2665 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002666 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002668 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002669 key = tv->vval.v_string == NULL
2670 ? (char_u *)"" : tv->vval.v_string;
2671 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002672 if (item != NULL)
2673 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002674 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002675 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002676 dict_unref(dict);
2677 goto on_error;
2678 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002679 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 clear_tv(tv);
2681 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002682 {
2683 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002684 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2687 item->di_tv.v_lock = 0;
2688 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002689 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002690 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002691 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002692 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 }
2695
2696 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002697 ectx->ec_stack.ga_len -= 2 * count - 1;
2698 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002699 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002701 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 tv = STACK_TV_BOT(-1);
2703 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002704 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 tv->vval.v_dict = dict;
2706 ++dict->dv_refcount;
2707 }
2708 break;
2709
2710 // call a :def function
2711 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002712 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002713 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2714 NULL,
2715 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002716 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002717 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 break;
2719
2720 // call a builtin function
2721 case ISN_BCALL:
2722 SOURCING_LNUM = iptr->isn_lnum;
2723 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2724 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002725 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002726 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 break;
2728
2729 // call a funcref or partial
2730 case ISN_PCALL:
2731 {
2732 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2733 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002734 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735
2736 SOURCING_LNUM = iptr->isn_lnum;
2737 if (pfunc->cpf_top)
2738 {
2739 // funcref is above the arguments
2740 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2741 }
2742 else
2743 {
2744 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002745 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002746 partial_tv = *STACK_TV_BOT(0);
2747 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002749 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002750 if (tv == &partial_tv)
2751 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002753 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 }
2755 break;
2756
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002757 case ISN_PCALL_END:
2758 // PCALL finished, arguments have been consumed and replaced by
2759 // the return value. Now clear the funcref from the stack,
2760 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002761 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002762 clear_tv(STACK_TV_BOT(-1));
2763 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2764 break;
2765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 // call a user defined function or funcref/partial
2767 case ISN_UCALL:
2768 {
2769 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2770
2771 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002772 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002773 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002774 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 }
2776 break;
2777
2778 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002779 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002780 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002781 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002782 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002783 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002784 tv->v_type = VAR_NUMBER;
2785 tv->vval.v_number = 0;
2786 tv->v_lock = 0;
2787 // FALLTHROUGH
2788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 case ISN_RETURN:
2790 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002791 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002792 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002793
2794 if (trystack->ga_len > 0)
2795 trycmd = ((trycmd_T *)trystack->ga_data)
2796 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002797 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002798 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002800 // jump to ":finally" or ":endtry"
2801 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002802 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002803 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002804 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 trycmd->tcd_return = TRUE;
2806 }
2807 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002808 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 }
2810 break;
2811
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002812 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 case ISN_FUNCREF:
2814 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002815 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2816 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2817 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002820 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002821 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002822 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002823 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002824 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002825 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002826 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002827 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002828 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002830 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 tv->vval.v_partial = pt;
2832 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002833 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 }
2835 break;
2836
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002837 // Create a global function from a lambda.
2838 case ISN_NEWFUNC:
2839 {
2840 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2841
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002842 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002843 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002844 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002845 }
2846 break;
2847
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002848 // List functions
2849 case ISN_DEF:
2850 if (iptr->isn_arg.string == NULL)
2851 list_functions(NULL);
2852 else
2853 {
2854 exarg_T ea;
2855
2856 CLEAR_FIELD(ea);
2857 ea.cmd = ea.arg = iptr->isn_arg.string;
2858 define_function(&ea, NULL);
2859 }
2860 break;
2861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 // jump if a condition is met
2863 case ISN_JUMP:
2864 {
2865 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002866 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 int jump = TRUE;
2868
2869 if (when != JUMP_ALWAYS)
2870 {
2871 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002872 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002873 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002874 || when == JUMP_IF_COND_TRUE)
2875 {
2876 SOURCING_LNUM = iptr->isn_lnum;
2877 jump = tv_get_bool_chk(tv, &error);
2878 if (error)
2879 goto on_error;
2880 }
2881 else
2882 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002884 || when == JUMP_AND_KEEP_IF_FALSE
2885 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002887 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 {
2889 // drop the value from the stack
2890 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002891 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 }
2893 }
2894 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002895 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 }
2897 break;
2898
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002899 // Jump if an argument with a default value was already set and not
2900 // v:none.
2901 case ISN_JUMP_IF_ARG_SET:
2902 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2903 if (tv->v_type != VAR_UNKNOWN
2904 && !(tv->v_type == VAR_SPECIAL
2905 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002906 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002907 break;
2908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 // top of a for loop
2910 case ISN_FOR:
2911 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002912 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 typval_T *idxtv =
2914 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2915
Bram Moolenaar4c137212021-04-19 16:48:48 +02002916 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002917 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002918 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002919 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002920 list_T *list = ltv->vval.v_list;
2921
2922 // push the next item from the list
2923 ++idxtv->vval.v_number;
2924 if (list == NULL
2925 || idxtv->vval.v_number >= list->lv_len)
2926 {
2927 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002928 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2929 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002930 }
2931 else if (list->lv_first == &range_list_item)
2932 {
2933 // non-materialized range() list
2934 tv = STACK_TV_BOT(0);
2935 tv->v_type = VAR_NUMBER;
2936 tv->v_lock = 0;
2937 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002939 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002940 }
2941 else
2942 {
2943 listitem_T *li = list_find(list,
2944 idxtv->vval.v_number);
2945
2946 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002947 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002948 }
2949 }
2950 else if (ltv->v_type == VAR_STRING)
2951 {
2952 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002953
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002954 // The index is for the last byte of the previous
2955 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002956 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002957 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002958 {
2959 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002960 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2961 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002962 }
2963 else
2964 {
2965 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2966
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002967 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002968 tv = STACK_TV_BOT(0);
2969 tv->v_type = VAR_STRING;
2970 tv->vval.v_string = vim_strnsave(
2971 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002972 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002973 idxtv->vval.v_number += clen - 1;
2974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002976 else if (ltv->v_type == VAR_BLOB)
2977 {
2978 blob_T *blob = ltv->vval.v_blob;
2979
2980 // When we get here the first time make a copy of the
2981 // blob, so that the iteration still works when it is
2982 // changed.
2983 if (idxtv->vval.v_number == -1 && blob != NULL)
2984 {
2985 blob_copy(blob, ltv);
2986 blob_unref(blob);
2987 blob = ltv->vval.v_blob;
2988 }
2989
2990 // The index is for the previous byte.
2991 ++idxtv->vval.v_number;
2992 if (blob == NULL
2993 || idxtv->vval.v_number >= blob_len(blob))
2994 {
2995 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002996 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2997 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002998 }
2999 else
3000 {
3001 // Push the next byte from the blob.
3002 tv = STACK_TV_BOT(0);
3003 tv->v_type = VAR_NUMBER;
3004 tv->vval.v_number = blob_get(blob,
3005 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003006 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003007 }
3008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 else
3010 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003011 semsg(_(e_for_loop_on_str_not_supported),
3012 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003013 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 }
3015 }
3016 break;
3017
3018 // start of ":try" block
3019 case ISN_TRY:
3020 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003021 trycmd_T *trycmd = NULL;
3022
Bram Moolenaar4c137212021-04-19 16:48:48 +02003023 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003024 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003025 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3026 + ectx->ec_trystack.ga_len;
3027 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003029 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003030 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3031 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003032 trycmd->tcd_catch_idx =
3033 iptr->isn_arg.try.try_ref->try_catch;
3034 trycmd->tcd_finally_idx =
3035 iptr->isn_arg.try.try_ref->try_finally;
3036 trycmd->tcd_endtry_idx =
3037 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 }
3039 break;
3040
3041 case ISN_PUSHEXC:
3042 if (current_exception == NULL)
3043 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003044 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003046 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003048 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003049 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003051 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003053 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 tv->vval.v_string = vim_strsave(
3055 (char_u *)current_exception->value);
3056 break;
3057
3058 case ISN_CATCH:
3059 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003060 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061
Bram Moolenaar4c137212021-04-19 16:48:48 +02003062 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 if (trystack->ga_len > 0)
3064 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003065 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 + trystack->ga_len - 1;
3067 trycmd->tcd_caught = TRUE;
3068 }
3069 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003070 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 catch_exception(current_exception);
3072 }
3073 break;
3074
Bram Moolenaarc150c092021-02-13 15:02:46 +01003075 case ISN_TRYCONT:
3076 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003077 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003078 trycont_T *trycont = &iptr->isn_arg.trycont;
3079 int i;
3080 trycmd_T *trycmd;
3081 int iidx = trycont->tct_where;
3082
3083 if (trystack->ga_len < trycont->tct_levels)
3084 {
3085 siemsg("TRYCONT: expected %d levels, found %d",
3086 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003087 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003088 }
3089 // Make :endtry jump to any outer try block and the last
3090 // :endtry inside the loop to the loop start.
3091 for (i = trycont->tct_levels; i > 0; --i)
3092 {
3093 trycmd = ((trycmd_T *)trystack->ga_data)
3094 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003095 // Add one to tcd_cont to be able to jump to
3096 // instruction with index zero.
3097 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003098 iidx = trycmd->tcd_finally_idx == 0
3099 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003100 }
3101 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003102 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003103 }
3104 break;
3105
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003106 case ISN_FINALLY:
3107 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003108 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003109 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3110 + trystack->ga_len - 1;
3111
3112 // Reset the index to avoid a return statement jumps here
3113 // again.
3114 trycmd->tcd_finally_idx = 0;
3115 break;
3116 }
3117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 // end of ":try" block
3119 case ISN_ENDTRY:
3120 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003121 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122
3123 if (trystack->ga_len > 0)
3124 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003125 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 --trystack->ga_len;
3128 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003129 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 trycmd = ((trycmd_T *)trystack->ga_data)
3131 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003132 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 {
3134 // discard the exception
3135 if (caught_stack == current_exception)
3136 caught_stack = caught_stack->caught;
3137 discard_current_exception();
3138 }
3139
3140 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003141 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003142
Bram Moolenaar4c137212021-04-19 16:48:48 +02003143 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003144 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003145 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003146 clear_tv(STACK_TV_BOT(0));
3147 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003148 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003149 // handling :continue: jump to outer try block or
3150 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003151 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 }
3153 }
3154 break;
3155
3156 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003157 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003158 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003159
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003160 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3161 {
3162 // throwing an exception while using "silent!" causes
3163 // the function to abort but not display an error.
3164 tv = STACK_TV_BOT(-1);
3165 clear_tv(tv);
3166 tv->v_type = VAR_NUMBER;
3167 tv->vval.v_number = 0;
3168 goto done;
3169 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003170 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003171 tv = STACK_TV_BOT(0);
3172 if (tv->vval.v_string == NULL
3173 || *skipwhite(tv->vval.v_string) == NUL)
3174 {
3175 vim_free(tv->vval.v_string);
3176 SOURCING_LNUM = iptr->isn_lnum;
3177 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003178 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003179 }
3180
3181 // Inside a "catch" we need to first discard the caught
3182 // exception.
3183 if (trystack->ga_len > 0)
3184 {
3185 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3186 + trystack->ga_len - 1;
3187 if (trycmd->tcd_caught && current_exception != NULL)
3188 {
3189 // discard the exception
3190 if (caught_stack == current_exception)
3191 caught_stack = caught_stack->caught;
3192 discard_current_exception();
3193 trycmd->tcd_caught = FALSE;
3194 }
3195 }
3196
3197 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3198 == FAIL)
3199 {
3200 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003201 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003202 }
3203 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 break;
3206
3207 // compare with special values
3208 case ISN_COMPAREBOOL:
3209 case ISN_COMPARESPECIAL:
3210 {
3211 typval_T *tv1 = STACK_TV_BOT(-2);
3212 typval_T *tv2 = STACK_TV_BOT(-1);
3213 varnumber_T arg1 = tv1->vval.v_number;
3214 varnumber_T arg2 = tv2->vval.v_number;
3215 int res;
3216
3217 switch (iptr->isn_arg.op.op_type)
3218 {
3219 case EXPR_EQUAL: res = arg1 == arg2; break;
3220 case EXPR_NEQUAL: res = arg1 != arg2; break;
3221 default: res = 0; break;
3222 }
3223
Bram Moolenaar4c137212021-04-19 16:48:48 +02003224 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 tv1->v_type = VAR_BOOL;
3226 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3227 }
3228 break;
3229
3230 // Operation with two number arguments
3231 case ISN_OPNR:
3232 case ISN_COMPARENR:
3233 {
3234 typval_T *tv1 = STACK_TV_BOT(-2);
3235 typval_T *tv2 = STACK_TV_BOT(-1);
3236 varnumber_T arg1 = tv1->vval.v_number;
3237 varnumber_T arg2 = tv2->vval.v_number;
3238 varnumber_T res;
3239
3240 switch (iptr->isn_arg.op.op_type)
3241 {
3242 case EXPR_MULT: res = arg1 * arg2; break;
3243 case EXPR_DIV: res = arg1 / arg2; break;
3244 case EXPR_REM: res = arg1 % arg2; break;
3245 case EXPR_SUB: res = arg1 - arg2; break;
3246 case EXPR_ADD: res = arg1 + arg2; break;
3247
3248 case EXPR_EQUAL: res = arg1 == arg2; break;
3249 case EXPR_NEQUAL: res = arg1 != arg2; break;
3250 case EXPR_GREATER: res = arg1 > arg2; break;
3251 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3252 case EXPR_SMALLER: res = arg1 < arg2; break;
3253 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3254 default: res = 0; break;
3255 }
3256
Bram Moolenaar4c137212021-04-19 16:48:48 +02003257 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 if (iptr->isn_type == ISN_COMPARENR)
3259 {
3260 tv1->v_type = VAR_BOOL;
3261 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3262 }
3263 else
3264 tv1->vval.v_number = res;
3265 }
3266 break;
3267
3268 // Computation with two float arguments
3269 case ISN_OPFLOAT:
3270 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003271#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 {
3273 typval_T *tv1 = STACK_TV_BOT(-2);
3274 typval_T *tv2 = STACK_TV_BOT(-1);
3275 float_T arg1 = tv1->vval.v_float;
3276 float_T arg2 = tv2->vval.v_float;
3277 float_T res = 0;
3278 int cmp = FALSE;
3279
3280 switch (iptr->isn_arg.op.op_type)
3281 {
3282 case EXPR_MULT: res = arg1 * arg2; break;
3283 case EXPR_DIV: res = arg1 / arg2; break;
3284 case EXPR_SUB: res = arg1 - arg2; break;
3285 case EXPR_ADD: res = arg1 + arg2; break;
3286
3287 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3288 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3289 case EXPR_GREATER: cmp = arg1 > arg2; break;
3290 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3291 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3292 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3293 default: cmp = 0; break;
3294 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003295 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 if (iptr->isn_type == ISN_COMPAREFLOAT)
3297 {
3298 tv1->v_type = VAR_BOOL;
3299 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3300 }
3301 else
3302 tv1->vval.v_float = res;
3303 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003304#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 break;
3306
3307 case ISN_COMPARELIST:
3308 {
3309 typval_T *tv1 = STACK_TV_BOT(-2);
3310 typval_T *tv2 = STACK_TV_BOT(-1);
3311 list_T *arg1 = tv1->vval.v_list;
3312 list_T *arg2 = tv2->vval.v_list;
3313 int cmp = FALSE;
3314 int ic = iptr->isn_arg.op.op_ic;
3315
3316 switch (iptr->isn_arg.op.op_type)
3317 {
3318 case EXPR_EQUAL: cmp =
3319 list_equal(arg1, arg2, ic, FALSE); break;
3320 case EXPR_NEQUAL: cmp =
3321 !list_equal(arg1, arg2, ic, FALSE); break;
3322 case EXPR_IS: cmp = arg1 == arg2; break;
3323 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3324 default: cmp = 0; break;
3325 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003326 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 clear_tv(tv1);
3328 clear_tv(tv2);
3329 tv1->v_type = VAR_BOOL;
3330 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3331 }
3332 break;
3333
3334 case ISN_COMPAREBLOB:
3335 {
3336 typval_T *tv1 = STACK_TV_BOT(-2);
3337 typval_T *tv2 = STACK_TV_BOT(-1);
3338 blob_T *arg1 = tv1->vval.v_blob;
3339 blob_T *arg2 = tv2->vval.v_blob;
3340 int cmp = FALSE;
3341
3342 switch (iptr->isn_arg.op.op_type)
3343 {
3344 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3345 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3346 case EXPR_IS: cmp = arg1 == arg2; break;
3347 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3348 default: cmp = 0; break;
3349 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003350 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 clear_tv(tv1);
3352 clear_tv(tv2);
3353 tv1->v_type = VAR_BOOL;
3354 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3355 }
3356 break;
3357
3358 // TODO: handle separately
3359 case ISN_COMPARESTRING:
3360 case ISN_COMPAREDICT:
3361 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 case ISN_COMPAREANY:
3363 {
3364 typval_T *tv1 = STACK_TV_BOT(-2);
3365 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003366 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 int ic = iptr->isn_arg.op.op_ic;
3368
Bram Moolenaareb26f432020-09-14 16:50:05 +02003369 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003370 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003372 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 }
3374 break;
3375
3376 case ISN_ADDLIST:
3377 case ISN_ADDBLOB:
3378 {
3379 typval_T *tv1 = STACK_TV_BOT(-2);
3380 typval_T *tv2 = STACK_TV_BOT(-1);
3381
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003382 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 if (iptr->isn_type == ISN_ADDLIST)
3384 eval_addlist(tv1, tv2);
3385 else
3386 eval_addblob(tv1, tv2);
3387 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003388 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 }
3390 break;
3391
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003392 case ISN_LISTAPPEND:
3393 {
3394 typval_T *tv1 = STACK_TV_BOT(-2);
3395 typval_T *tv2 = STACK_TV_BOT(-1);
3396 list_T *l = tv1->vval.v_list;
3397
3398 // add an item to a list
3399 if (l == NULL)
3400 {
3401 SOURCING_LNUM = iptr->isn_lnum;
3402 emsg(_(e_cannot_add_to_null_list));
3403 goto on_error;
3404 }
3405 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003406 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003407 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003408 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003409 }
3410 break;
3411
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003412 case ISN_BLOBAPPEND:
3413 {
3414 typval_T *tv1 = STACK_TV_BOT(-2);
3415 typval_T *tv2 = STACK_TV_BOT(-1);
3416 blob_T *b = tv1->vval.v_blob;
3417 int error = FALSE;
3418 varnumber_T n;
3419
3420 // add a number to a blob
3421 if (b == NULL)
3422 {
3423 SOURCING_LNUM = iptr->isn_lnum;
3424 emsg(_(e_cannot_add_to_null_blob));
3425 goto on_error;
3426 }
3427 n = tv_get_number_chk(tv2, &error);
3428 if (error)
3429 goto on_error;
3430 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003431 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003432 }
3433 break;
3434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 // Computation with two arguments of unknown type
3436 case ISN_OPANY:
3437 {
3438 typval_T *tv1 = STACK_TV_BOT(-2);
3439 typval_T *tv2 = STACK_TV_BOT(-1);
3440 varnumber_T n1, n2;
3441#ifdef FEAT_FLOAT
3442 float_T f1 = 0, f2 = 0;
3443#endif
3444 int error = FALSE;
3445
3446 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3447 {
3448 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3449 {
3450 eval_addlist(tv1, tv2);
3451 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003452 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 break;
3454 }
3455 else if (tv1->v_type == VAR_BLOB
3456 && tv2->v_type == VAR_BLOB)
3457 {
3458 eval_addblob(tv1, tv2);
3459 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003460 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 break;
3462 }
3463 }
3464#ifdef FEAT_FLOAT
3465 if (tv1->v_type == VAR_FLOAT)
3466 {
3467 f1 = tv1->vval.v_float;
3468 n1 = 0;
3469 }
3470 else
3471#endif
3472 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003473 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 n1 = tv_get_number_chk(tv1, &error);
3475 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003476 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477#ifdef FEAT_FLOAT
3478 if (tv2->v_type == VAR_FLOAT)
3479 f1 = n1;
3480#endif
3481 }
3482#ifdef FEAT_FLOAT
3483 if (tv2->v_type == VAR_FLOAT)
3484 {
3485 f2 = tv2->vval.v_float;
3486 n2 = 0;
3487 }
3488 else
3489#endif
3490 {
3491 n2 = tv_get_number_chk(tv2, &error);
3492 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003493 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494#ifdef FEAT_FLOAT
3495 if (tv1->v_type == VAR_FLOAT)
3496 f2 = n2;
3497#endif
3498 }
3499#ifdef FEAT_FLOAT
3500 // if there is a float on either side the result is a float
3501 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3502 {
3503 switch (iptr->isn_arg.op.op_type)
3504 {
3505 case EXPR_MULT: f1 = f1 * f2; break;
3506 case EXPR_DIV: f1 = f1 / f2; break;
3507 case EXPR_SUB: f1 = f1 - f2; break;
3508 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003509 default: SOURCING_LNUM = iptr->isn_lnum;
3510 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003511 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 }
3513 clear_tv(tv1);
3514 clear_tv(tv2);
3515 tv1->v_type = VAR_FLOAT;
3516 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003517 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 }
3519 else
3520#endif
3521 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003522 int failed = FALSE;
3523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 switch (iptr->isn_arg.op.op_type)
3525 {
3526 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003527 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3528 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003529 goto on_error;
3530 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 case EXPR_SUB: n1 = n1 - n2; break;
3532 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003533 default: n1 = num_modulus(n1, n2, &failed);
3534 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003535 goto on_error;
3536 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 }
3538 clear_tv(tv1);
3539 clear_tv(tv2);
3540 tv1->v_type = VAR_NUMBER;
3541 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003542 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 }
3544 }
3545 break;
3546
3547 case ISN_CONCAT:
3548 {
3549 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3550 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3551 char_u *res;
3552
3553 res = concat_str(str1, str2);
3554 clear_tv(STACK_TV_BOT(-2));
3555 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003556 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 STACK_TV_BOT(-1)->vval.v_string = res;
3558 }
3559 break;
3560
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003561 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003562 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003563 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003564 int is_slice = iptr->isn_type == ISN_STRSLICE;
3565 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003566 char_u *res;
3567
3568 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003569 // string slice: string is at stack-3, first index at
3570 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003571 if (is_slice)
3572 {
3573 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003574 n1 = tv->vval.v_number;
3575 }
3576
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003577 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003578 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003579
Bram Moolenaar4c137212021-04-19 16:48:48 +02003580 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003581 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003582 if (is_slice)
3583 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003584 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003585 else
3586 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003587 // single character (including composing characters).
3588 // If the index is too big or negative the result is
3589 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003590 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003591 vim_free(tv->vval.v_string);
3592 tv->vval.v_string = res;
3593 }
3594 break;
3595
3596 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003597 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003598 case ISN_BLOBINDEX:
3599 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003601 int is_slice = iptr->isn_type == ISN_LISTSLICE
3602 || iptr->isn_type == ISN_BLOBSLICE;
3603 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3604 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003605 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003606 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607
3608 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003609 // list slice: list is at stack-3, indexes at stack-2 and
3610 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003611 // Same for blob.
3612 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613
3614 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003615 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003617
3618 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 {
Bram Moolenaared591872020-08-15 22:14:53 +02003620 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003621 n1 = tv->vval.v_number;
3622 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 }
Bram Moolenaared591872020-08-15 22:14:53 +02003624
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003626 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003627 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003628 if (is_blob)
3629 {
3630 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3631 n1, n2, FALSE, tv) == FAIL)
3632 goto on_error;
3633 }
3634 else
3635 {
3636 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3637 n1, n2, FALSE, tv, TRUE) == FAIL)
3638 goto on_error;
3639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 }
3641 break;
3642
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003643 case ISN_ANYINDEX:
3644 case ISN_ANYSLICE:
3645 {
3646 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3647 typval_T *var1, *var2;
3648 int res;
3649
3650 // index: composite is at stack-2, index at stack-1
3651 // slice: composite is at stack-3, indexes at stack-2 and
3652 // stack-1
3653 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003654 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003655 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3656 goto on_error;
3657 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3658 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003659 res = eval_index_inner(tv, is_slice, var1, var2,
3660 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003661 clear_tv(var1);
3662 if (is_slice)
3663 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003664 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003665 if (res == FAIL)
3666 goto on_error;
3667 }
3668 break;
3669
Bram Moolenaar9af78762020-06-16 11:34:42 +02003670 case ISN_SLICE:
3671 {
3672 list_T *list;
3673 int count = iptr->isn_arg.number;
3674
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003675 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003676 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003677 list = tv->vval.v_list;
3678
3679 // no error for short list, expect it to be checked earlier
3680 if (list != NULL && list->lv_len >= count)
3681 {
3682 list_T *newlist = list_slice(list,
3683 count, list->lv_len - 1);
3684
3685 if (newlist != NULL)
3686 {
3687 list_unref(list);
3688 tv->vval.v_list = newlist;
3689 ++newlist->lv_refcount;
3690 }
3691 }
3692 }
3693 break;
3694
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003695 case ISN_GETITEM:
3696 {
3697 listitem_T *li;
3698 int index = iptr->isn_arg.number;
3699
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003700 // Get list item: list is at stack-1, push item.
3701 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003702 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003703 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003704
Bram Moolenaar4c137212021-04-19 16:48:48 +02003705 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003706 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003707 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003708 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003709
3710 // Useful when used in unpack assignment. Reset at
3711 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003712 ectx->ec_where.wt_index = index + 1;
3713 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003714 }
3715 break;
3716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 case ISN_MEMBER:
3718 {
3719 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003720 char_u *key;
3721 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003722 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003723
3724 // dict member: dict is at stack-2, key at stack-1
3725 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003726 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003727 dict = tv->vval.v_dict;
3728
3729 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003730 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003731 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003732 if (key == NULL)
3733 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003734
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003735 if ((di = dict_find(dict, key, -1)) == NULL)
3736 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003737 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003738 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003739
3740 // If :silent! is used we will continue, make sure the
3741 // stack contents makes sense.
3742 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003743 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003744 tv = STACK_TV_BOT(-1);
3745 clear_tv(tv);
3746 tv->v_type = VAR_NUMBER;
3747 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003748 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003749 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003750 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003751 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003752 // Clear the dict only after getting the item, to avoid
3753 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003754 tv = STACK_TV_BOT(-1);
3755 temp_tv = *tv;
3756 copy_tv(&di->di_tv, tv);
3757 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003758 }
3759 break;
3760
3761 // dict member with string key
3762 case ISN_STRINGMEMBER:
3763 {
3764 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003766 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767
3768 tv = STACK_TV_BOT(-1);
3769 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3770 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003771 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003773 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 }
3775 dict = tv->vval.v_dict;
3776
3777 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3778 == NULL)
3779 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003780 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003782 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003784 // Clear the dict after getting the item, to avoid that it
3785 // make the item invalid.
3786 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003788 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 }
3790 break;
3791
3792 case ISN_NEGATENR:
3793 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003794 if (tv->v_type != VAR_NUMBER
3795#ifdef FEAT_FLOAT
3796 && tv->v_type != VAR_FLOAT
3797#endif
3798 )
3799 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003800 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003801 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003802 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003803 }
3804#ifdef FEAT_FLOAT
3805 if (tv->v_type == VAR_FLOAT)
3806 tv->vval.v_float = -tv->vval.v_float;
3807 else
3808#endif
3809 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 break;
3811
3812 case ISN_CHECKNR:
3813 {
3814 int error = FALSE;
3815
3816 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003817 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003819 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 (void)tv_get_number_chk(tv, &error);
3821 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003822 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 }
3824 break;
3825
3826 case ISN_CHECKTYPE:
3827 {
3828 checktype_T *ct = &iptr->isn_arg.type;
3829
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003830 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003832 if (!ectx->ec_where.wt_variable)
3833 ectx->ec_where.wt_index = ct->ct_arg_idx;
3834 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3835 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003836 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003837 if (!ectx->ec_where.wt_variable)
3838 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003839
3840 // number 0 is FALSE, number 1 is TRUE
3841 if (tv->v_type == VAR_NUMBER
3842 && ct->ct_type->tt_type == VAR_BOOL
3843 && (tv->vval.v_number == 0
3844 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003846 tv->v_type = VAR_BOOL;
3847 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003848 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 }
3850 }
3851 break;
3852
Bram Moolenaar9af78762020-06-16 11:34:42 +02003853 case ISN_CHECKLEN:
3854 {
3855 int min_len = iptr->isn_arg.checklen.cl_min_len;
3856 list_T *list = NULL;
3857
3858 tv = STACK_TV_BOT(-1);
3859 if (tv->v_type == VAR_LIST)
3860 list = tv->vval.v_list;
3861 if (list == NULL || list->lv_len < min_len
3862 || (list->lv_len > min_len
3863 && !iptr->isn_arg.checklen.cl_more_OK))
3864 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003865 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003866 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003867 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003868 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003869 }
3870 }
3871 break;
3872
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003873 case ISN_SETTYPE:
3874 {
3875 checktype_T *ct = &iptr->isn_arg.type;
3876
3877 tv = STACK_TV_BOT(-1);
3878 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3879 {
3880 free_type(tv->vval.v_dict->dv_type);
3881 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3882 }
3883 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3884 {
3885 free_type(tv->vval.v_list->lv_type);
3886 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3887 }
3888 }
3889 break;
3890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003892 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 {
3894 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003895 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003897 if (iptr->isn_type == ISN_2BOOL)
3898 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003899 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003900 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003901 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003902 n = !n;
3903 }
3904 else
3905 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003906 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003907 SOURCING_LNUM = iptr->isn_lnum;
3908 n = tv_get_bool_chk(tv, &error);
3909 if (error)
3910 goto on_error;
3911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 clear_tv(tv);
3913 tv->v_type = VAR_BOOL;
3914 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3915 }
3916 break;
3917
3918 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003919 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003920 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003921 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3922 iptr->isn_type == ISN_2STRING_ANY,
3923 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003924 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 break;
3926
Bram Moolenaar08597872020-12-10 19:43:40 +01003927 case ISN_RANGE:
3928 {
3929 exarg_T ea;
3930 char *errormsg;
3931
Bram Moolenaarece0b872021-01-08 20:40:45 +01003932 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003933 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003934 ea.addr_type = ADDR_LINES;
3935 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003936 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003937 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003938 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003939
Bram Moolenaar4c137212021-04-19 16:48:48 +02003940 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003941 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003942 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003943 tv = STACK_TV_BOT(-1);
3944 tv->v_type = VAR_NUMBER;
3945 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003946 if (ea.addr_count == 0)
3947 tv->vval.v_number = curwin->w_cursor.lnum;
3948 else
3949 tv->vval.v_number = ea.line2;
3950 }
3951 break;
3952
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003953 case ISN_PUT:
3954 {
3955 int regname = iptr->isn_arg.put.put_regname;
3956 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3957 char_u *expr = NULL;
3958 int dir = FORWARD;
3959
Bram Moolenaar08597872020-12-10 19:43:40 +01003960 if (lnum < -2)
3961 {
3962 // line number was put on the stack by ISN_RANGE
3963 tv = STACK_TV_BOT(-1);
3964 curwin->w_cursor.lnum = tv->vval.v_number;
3965 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3966 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003967 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003968 }
3969 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003970 // :put! above cursor
3971 dir = BACKWARD;
3972 else if (lnum >= 0)
3973 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003974
3975 if (regname == '=')
3976 {
3977 tv = STACK_TV_BOT(-1);
3978 if (tv->v_type == VAR_STRING)
3979 expr = tv->vval.v_string;
3980 else
3981 {
3982 expr = typval2string(tv, TRUE); // allocates value
3983 clear_tv(tv);
3984 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003985 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003986 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003987 check_cursor();
3988 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3989 vim_free(expr);
3990 }
3991 break;
3992
Bram Moolenaar02194d22020-10-24 23:08:38 +02003993 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003994 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3995 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3996 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3997 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003998 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3999 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004000 break;
4001
Bram Moolenaar02194d22020-10-24 23:08:38 +02004002 case ISN_CMDMOD_REV:
4003 // filter regprog is owned by the instruction, don't free it
4004 cmdmod.cmod_filter_regmatch.regprog = NULL;
4005 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004006 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4007 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004008 break;
4009
Bram Moolenaar792f7862020-11-23 08:31:18 +01004010 case ISN_UNPACK:
4011 {
4012 int count = iptr->isn_arg.unpack.unp_count;
4013 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4014 list_T *l;
4015 listitem_T *li;
4016 int i;
4017
4018 // Check there is a valid list to unpack.
4019 tv = STACK_TV_BOT(-1);
4020 if (tv->v_type != VAR_LIST)
4021 {
4022 SOURCING_LNUM = iptr->isn_lnum;
4023 emsg(_(e_for_argument_must_be_sequence_of_lists));
4024 goto on_error;
4025 }
4026 l = tv->vval.v_list;
4027 if (l == NULL
4028 || l->lv_len < (semicolon ? count - 1 : count))
4029 {
4030 SOURCING_LNUM = iptr->isn_lnum;
4031 emsg(_(e_list_value_does_not_have_enough_items));
4032 goto on_error;
4033 }
4034 else if (!semicolon && l->lv_len > count)
4035 {
4036 SOURCING_LNUM = iptr->isn_lnum;
4037 emsg(_(e_list_value_has_more_items_than_targets));
4038 goto on_error;
4039 }
4040
4041 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004042 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004043 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004044 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004045
4046 // Variable after semicolon gets a list with the remaining
4047 // items.
4048 if (semicolon)
4049 {
4050 list_T *rem_list =
4051 list_alloc_with_items(l->lv_len - count + 1);
4052
4053 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004054 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004055 tv = STACK_TV_BOT(-count);
4056 tv->vval.v_list = rem_list;
4057 ++rem_list->lv_refcount;
4058 tv->v_lock = 0;
4059 li = l->lv_first;
4060 for (i = 0; i < count - 1; ++i)
4061 li = li->li_next;
4062 for (i = 0; li != NULL; ++i)
4063 {
4064 list_set_item(rem_list, i, &li->li_tv);
4065 li = li->li_next;
4066 }
4067 --count;
4068 }
4069
4070 // Produce the values in reverse order, first item last.
4071 li = l->lv_first;
4072 for (i = 0; i < count; ++i)
4073 {
4074 tv = STACK_TV_BOT(-i - 1);
4075 copy_tv(&li->li_tv, tv);
4076 li = li->li_next;
4077 }
4078
4079 list_unref(l);
4080 }
4081 break;
4082
Bram Moolenaarb2049902021-01-24 12:53:53 +01004083 case ISN_PROF_START:
4084 case ISN_PROF_END:
4085 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004086#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004087 funccall_T cookie;
4088 ufunc_T *cur_ufunc =
4089 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004090 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004091
4092 cookie.func = cur_ufunc;
4093 if (iptr->isn_type == ISN_PROF_START)
4094 {
4095 func_line_start(&cookie, iptr->isn_lnum);
4096 // if we get here the instruction is executed
4097 func_line_exec(&cookie);
4098 }
4099 else
4100 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004101#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004102 }
4103 break;
4104
Bram Moolenaare99d4222021-06-13 14:01:26 +02004105 case ISN_DEBUG:
4106 if (ex_nesting_level <= debug_break_level)
4107 {
4108 char_u *line;
4109 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
4110 + ectx->ec_dfunc_idx)->df_ufunc;
4111
4112 SOURCING_LNUM = iptr->isn_lnum;
4113 line = ((char_u **)ufunc->uf_lines.ga_data)[
4114 iptr->isn_lnum - 1];
4115 if (line == NULL)
4116 line = (char_u *)"[empty]";
4117 do_debug(line);
4118 }
4119 break;
4120
Bram Moolenaar389df252020-07-09 21:20:47 +02004121 case ISN_SHUFFLE:
4122 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004123 typval_T tmp_tv;
4124 int item = iptr->isn_arg.shuffle.shfl_item;
4125 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004126
4127 tmp_tv = *STACK_TV_BOT(-item);
4128 for ( ; up > 0 && item > 1; --up)
4129 {
4130 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4131 --item;
4132 }
4133 *STACK_TV_BOT(-item) = tmp_tv;
4134 }
4135 break;
4136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004138 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004140 ectx->ec_where.wt_index = 0;
4141 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 break;
4143 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004144 continue;
4145
Bram Moolenaard032f342020-07-18 18:13:02 +02004146func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004147 // Restore previous function. If the frame pointer is where we started
4148 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004149 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004150 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004151
Bram Moolenaar4c137212021-04-19 16:48:48 +02004152 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004153 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004154 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004155 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004156
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004157on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004158 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004159 // If "emsg_silent" is set then ignore the error, unless it was set
4160 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004161 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004162 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004163 {
4164 // If a sequence of instructions causes an error while ":silent!"
4165 // was used, restore the stack length and jump ahead to restoring
4166 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004167 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004168 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004169 while (ectx->ec_stack.ga_len
4170 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004171 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004172 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004173 clear_tv(STACK_TV_BOT(0));
4174 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004175 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4176 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004177 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004178 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004179 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004180on_fatal_error:
4181 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004182 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004183 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004184 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 }
4186
4187done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004188 ret = OK;
4189theend:
4190 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4191 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004192}
4193
4194/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004195 * Execute the instructions from a VAR_INSTR typeval and put the result in
4196 * "rettv".
4197 * Return OK or FAIL.
4198 */
4199 int
4200exe_typval_instr(typval_T *tv, typval_T *rettv)
4201{
4202 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4203 isn_T *save_instr = ectx->ec_instr;
4204 int save_iidx = ectx->ec_iidx;
4205 int res;
4206
4207 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4208 res = exec_instructions(ectx);
4209 if (res == OK)
4210 {
4211 *rettv = *STACK_TV_BOT(-1);
4212 --ectx->ec_stack.ga_len;
4213 }
4214
4215 ectx->ec_instr = save_instr;
4216 ectx->ec_iidx = save_iidx;
4217
4218 return res;
4219}
4220
4221/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004222 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4223 * "substitute_instr".
4224 */
4225 char_u *
4226exe_substitute_instr(void)
4227{
4228 ectx_T *ectx = substitute_instr->subs_ectx;
4229 isn_T *save_instr = ectx->ec_instr;
4230 int save_iidx = ectx->ec_iidx;
4231 char_u *res;
4232
4233 ectx->ec_instr = substitute_instr->subs_instr;
4234 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004235 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004236 typval_T *tv = STACK_TV_BOT(-1);
4237
Bram Moolenaar27523602021-06-05 21:36:19 +02004238 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004239 --ectx->ec_stack.ga_len;
4240 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004241 }
4242 else
4243 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004244 substitute_instr->subs_status = FAIL;
4245 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247
Bram Moolenaar4c137212021-04-19 16:48:48 +02004248 ectx->ec_instr = save_instr;
4249 ectx->ec_iidx = save_iidx;
4250
4251 return res;
4252}
4253
4254/*
4255 * Call a "def" function from old Vim script.
4256 * Return OK or FAIL.
4257 */
4258 int
4259call_def_function(
4260 ufunc_T *ufunc,
4261 int argc_arg, // nr of arguments
4262 typval_T *argv, // arguments
4263 partial_T *partial, // optional partial for context
4264 typval_T *rettv) // return value
4265{
4266 ectx_T ectx; // execution context
4267 int argc = argc_arg;
4268 typval_T *tv;
4269 int idx;
4270 int ret = FAIL;
4271 int defcount = ufunc->uf_args.ga_len - argc;
4272 sctx_T save_current_sctx = current_sctx;
4273 int did_emsg_before = did_emsg_cumul + did_emsg;
4274 int save_suppress_errthrow = suppress_errthrow;
4275 msglist_T **saved_msg_list = NULL;
4276 msglist_T *private_msg_list = NULL;
4277 int save_emsg_silent_def = emsg_silent_def;
4278 int save_did_emsg_def = did_emsg_def;
4279 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004280 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004281
4282// Get pointer to item in the stack.
4283#undef STACK_TV
4284#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4285
4286// Get pointer to item at the bottom of the stack, -1 is the bottom.
4287#undef STACK_TV_BOT
4288#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4289
4290// Get pointer to a local variable on the stack. Negative for arguments.
4291#undef STACK_TV_VAR
4292#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4293
4294 if (ufunc->uf_def_status == UF_NOT_COMPILED
4295 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004296 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4297 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004298 == FAIL))
4299 {
4300 if (did_emsg_cumul + did_emsg == did_emsg_before)
4301 semsg(_(e_function_is_not_compiled_str),
4302 printable_func_name(ufunc));
4303 return FAIL;
4304 }
4305
4306 {
4307 // Check the function was really compiled.
4308 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4309 + ufunc->uf_dfunc_idx;
4310 if (INSTRUCTIONS(dfunc) == NULL)
4311 {
4312 iemsg("using call_def_function() on not compiled function");
4313 return FAIL;
4314 }
4315 }
4316
4317 // If depth of calling is getting too high, don't execute the function.
4318 orig_funcdepth = funcdepth_get();
4319 if (funcdepth_increment() == FAIL)
4320 return FAIL;
4321
4322 CLEAR_FIELD(ectx);
4323 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4324 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4325 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4326 {
4327 funcdepth_decrement();
4328 return FAIL;
4329 }
4330 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4331 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4332 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004333 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004334
4335 idx = argc - ufunc->uf_args.ga_len;
4336 if (idx > 0 && ufunc->uf_va_name == NULL)
4337 {
4338 if (idx == 1)
4339 emsg(_(e_one_argument_too_many));
4340 else
4341 semsg(_(e_nr_arguments_too_many), idx);
4342 goto failed_early;
4343 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004344 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4345 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004346 {
4347 if (idx == -1)
4348 emsg(_(e_one_argument_too_few));
4349 else
4350 semsg(_(e_nr_arguments_too_few), -idx);
4351 goto failed_early;
4352 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004353
4354 // Put arguments on the stack, but no more than what the function expects.
4355 // A lambda can be called with more arguments than it uses.
4356 for (idx = 0; idx < argc
4357 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4358 ++idx)
4359 {
4360 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4361 && argv[idx].v_type == VAR_SPECIAL
4362 && argv[idx].vval.v_number == VVAL_NONE)
4363 {
4364 // Use the default value.
4365 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4366 }
4367 else
4368 {
4369 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4370 && check_typval_arg_type(
4371 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4372 goto failed_early;
4373 copy_tv(&argv[idx], STACK_TV_BOT(0));
4374 }
4375 ++ectx.ec_stack.ga_len;
4376 }
4377
4378 // Turn varargs into a list. Empty list if no args.
4379 if (ufunc->uf_va_name != NULL)
4380 {
4381 int vararg_count = argc - ufunc->uf_args.ga_len;
4382
4383 if (vararg_count < 0)
4384 vararg_count = 0;
4385 else
4386 argc -= vararg_count;
4387 if (exe_newlist(vararg_count, &ectx) == FAIL)
4388 goto failed_early;
4389
4390 // Check the type of the list items.
4391 tv = STACK_TV_BOT(-1);
4392 if (ufunc->uf_va_type != NULL
4393 && ufunc->uf_va_type != &t_list_any
4394 && ufunc->uf_va_type->tt_member != &t_any
4395 && tv->vval.v_list != NULL)
4396 {
4397 type_T *expected = ufunc->uf_va_type->tt_member;
4398 listitem_T *li = tv->vval.v_list->lv_first;
4399
4400 for (idx = 0; idx < vararg_count; ++idx)
4401 {
4402 if (check_typval_arg_type(expected, &li->li_tv,
4403 argc + idx + 1) == FAIL)
4404 goto failed_early;
4405 li = li->li_next;
4406 }
4407 }
4408
4409 if (defcount > 0)
4410 // Move varargs list to below missing default arguments.
4411 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4412 --ectx.ec_stack.ga_len;
4413 }
4414
4415 // Make space for omitted arguments, will store default value below.
4416 // Any varargs list goes after them.
4417 if (defcount > 0)
4418 for (idx = 0; idx < defcount; ++idx)
4419 {
4420 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4421 ++ectx.ec_stack.ga_len;
4422 }
4423 if (ufunc->uf_va_name != NULL)
4424 ++ectx.ec_stack.ga_len;
4425
4426 // Frame pointer points to just after arguments.
4427 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4428 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4429
4430 {
4431 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4432 + ufunc->uf_dfunc_idx;
4433 ufunc_T *base_ufunc = dfunc->df_ufunc;
4434
4435 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4436 // by copy_func().
4437 if (partial != NULL || base_ufunc->uf_partial != NULL)
4438 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004439 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4440 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004441 goto failed_early;
4442 if (partial != NULL)
4443 {
4444 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4445 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004446 if (current_ectx->ec_outer_ref != NULL
4447 && current_ectx->ec_outer_ref->or_outer != NULL)
4448 ectx.ec_outer_ref->or_outer =
4449 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004450 }
4451 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004452 {
4453 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4454 ++partial->pt_refcount;
4455 ectx.ec_outer_ref->or_partial = partial;
4456 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004457 }
4458 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004459 {
4460 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4461 ++base_ufunc->uf_partial->pt_refcount;
4462 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4463 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004464 }
4465 }
4466
4467 // dummy frame entries
4468 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4469 {
4470 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4471 ++ectx.ec_stack.ga_len;
4472 }
4473
4474 {
4475 // Reserve space for local variables and any closure reference count.
4476 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4477 + ufunc->uf_dfunc_idx;
4478
4479 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4480 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4481 ectx.ec_stack.ga_len += dfunc->df_varcount;
4482 if (dfunc->df_has_closure)
4483 {
4484 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4485 STACK_TV_VAR(idx)->vval.v_number = 0;
4486 ++ectx.ec_stack.ga_len;
4487 }
4488
4489 ectx.ec_instr = INSTRUCTIONS(dfunc);
4490 }
4491
4492 // Following errors are in the function, not the caller.
4493 // Commands behave like vim9script.
4494 estack_push_ufunc(ufunc, 1);
4495 current_sctx = ufunc->uf_script_ctx;
4496 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4497
4498 // Use a specific location for storing error messages to be converted to an
4499 // exception.
4500 saved_msg_list = msg_list;
4501 msg_list = &private_msg_list;
4502
4503 // Do turn errors into exceptions.
4504 suppress_errthrow = FALSE;
4505
4506 // Do not delete the function while executing it.
4507 ++ufunc->uf_calls;
4508
4509 // When ":silent!" was used before calling then we still abort the
4510 // function. If ":silent!" is used in the function then we don't.
4511 emsg_silent_def = emsg_silent;
4512 did_emsg_def = 0;
4513
4514 ectx.ec_where.wt_index = 0;
4515 ectx.ec_where.wt_variable = FALSE;
4516
4517 // Execute the instructions until done.
4518 ret = exec_instructions(&ectx);
4519 if (ret == OK)
4520 {
4521 // function finished, get result from the stack.
4522 if (ufunc->uf_ret_type == &t_void)
4523 {
4524 rettv->v_type = VAR_VOID;
4525 }
4526 else
4527 {
4528 tv = STACK_TV_BOT(-1);
4529 *rettv = *tv;
4530 tv->v_type = VAR_UNKNOWN;
4531 }
4532 }
4533
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004534 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004535 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4536 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004537
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004538 // Deal with any remaining closures, they may be in use somewhere.
4539 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004540 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004541 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004542 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4543 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004544
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004545 estack_pop();
4546 current_sctx = save_current_sctx;
4547
Bram Moolenaarc970e422021-03-17 15:03:04 +01004548 // TODO: when is it safe to delete the function if it is no longer used?
4549 --ufunc->uf_calls;
4550
Bram Moolenaar352134b2020-10-17 22:04:08 +02004551 if (*msg_list != NULL && saved_msg_list != NULL)
4552 {
4553 msglist_T **plist = saved_msg_list;
4554
4555 // Append entries from the current msg_list (uncaught exceptions) to
4556 // the saved msg_list.
4557 while (*plist != NULL)
4558 plist = &(*plist)->next;
4559
4560 *plist = *msg_list;
4561 }
4562 msg_list = saved_msg_list;
4563
Bram Moolenaar4c137212021-04-19 16:48:48 +02004564 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004565 {
4566 cmdmod.cmod_filter_regmatch.regprog = NULL;
4567 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004568 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004569 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004570 emsg_silent_def = save_emsg_silent_def;
4571 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004572
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004573failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004574 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4576 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02004577 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004580 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004581 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004582 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004583 if (ectx.ec_outer_ref->or_outer_allocated)
4584 vim_free(ectx.ec_outer_ref->or_outer);
4585 partial_unref(ectx.ec_outer_ref->or_partial);
4586 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004587 }
4588
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004589 // Not sure if this is necessary.
4590 suppress_errthrow = save_suppress_errthrow;
4591
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004592 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004593 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004594 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004595 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 return ret;
4597}
4598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004600 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4601 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4602 * "pfx" is prefixed to every line.
4603 */
4604 static void
4605list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4606{
4607 int line_idx = 0;
4608 int prev_current = 0;
4609 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004610 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004611
4612 for (current = 0; current < instr_count; ++current)
4613 {
4614 isn_T *iptr = &instr[current];
4615 char *line;
4616
4617 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004618 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004619 while (line_idx < iptr->isn_lnum
4620 && line_idx < ufunc->uf_lines.ga_len)
4621 {
4622 if (current > prev_current)
4623 {
4624 msg_puts("\n\n");
4625 prev_current = current;
4626 }
4627 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4628 if (line != NULL)
4629 msg(line);
4630 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004631 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4632 {
4633 int first_def_arg = ufunc->uf_args.ga_len
4634 - ufunc->uf_def_args.ga_len;
4635
4636 if (def_arg_idx > 0)
4637 msg_puts("\n\n");
4638 msg_start();
4639 msg_puts(" ");
4640 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4641 first_def_arg + def_arg_idx]);
4642 msg_puts(" = ");
4643 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4644 msg_clr_eos();
4645 msg_end();
4646 }
4647 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004648
4649 switch (iptr->isn_type)
4650 {
4651 case ISN_EXEC:
4652 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4653 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004654 case ISN_EXEC_SPLIT:
4655 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4656 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004657 case ISN_LEGACY_EVAL:
4658 smsg("%s%4d EVAL legacy %s", pfx, current,
4659 iptr->isn_arg.string);
4660 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004661 case ISN_REDIRSTART:
4662 smsg("%s%4d REDIR", pfx, current);
4663 break;
4664 case ISN_REDIREND:
4665 smsg("%s%4d REDIR END%s", pfx, current,
4666 iptr->isn_arg.number ? " append" : "");
4667 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004668 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004669#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004670 smsg("%s%4d CEXPR pre %s", pfx, current,
4671 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004672#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004673 break;
4674 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004675#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004676 {
4677 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4678
4679 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4680 cexpr_get_auname(cer->cer_cmdidx),
4681 cer->cer_forceit ? "!" : "",
4682 cer->cer_cmdline);
4683 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004684#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004685 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004686 case ISN_INSTR:
4687 {
4688 smsg("%s%4d INSTR", pfx, current);
4689 list_instructions(" ", iptr->isn_arg.instr,
4690 INT_MAX, NULL);
4691 msg(" -------------");
4692 }
4693 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004694 case ISN_SUBSTITUTE:
4695 {
4696 subs_T *subs = &iptr->isn_arg.subs;
4697
4698 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4699 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4700 msg(" -------------");
4701 }
4702 break;
4703 case ISN_EXECCONCAT:
4704 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4705 (varnumber_T)iptr->isn_arg.number);
4706 break;
4707 case ISN_ECHO:
4708 {
4709 echo_T *echo = &iptr->isn_arg.echo;
4710
4711 smsg("%s%4d %s %d", pfx, current,
4712 echo->echo_with_white ? "ECHO" : "ECHON",
4713 echo->echo_count);
4714 }
4715 break;
4716 case ISN_EXECUTE:
4717 smsg("%s%4d EXECUTE %lld", pfx, current,
4718 (varnumber_T)(iptr->isn_arg.number));
4719 break;
4720 case ISN_ECHOMSG:
4721 smsg("%s%4d ECHOMSG %lld", pfx, current,
4722 (varnumber_T)(iptr->isn_arg.number));
4723 break;
4724 case ISN_ECHOERR:
4725 smsg("%s%4d ECHOERR %lld", pfx, current,
4726 (varnumber_T)(iptr->isn_arg.number));
4727 break;
4728 case ISN_LOAD:
4729 {
4730 if (iptr->isn_arg.number < 0)
4731 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4732 (varnumber_T)(iptr->isn_arg.number
4733 + STACK_FRAME_SIZE));
4734 else
4735 smsg("%s%4d LOAD $%lld", pfx, current,
4736 (varnumber_T)(iptr->isn_arg.number));
4737 }
4738 break;
4739 case ISN_LOADOUTER:
4740 {
4741 if (iptr->isn_arg.number < 0)
4742 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4743 iptr->isn_arg.outer.outer_depth,
4744 iptr->isn_arg.outer.outer_idx
4745 + STACK_FRAME_SIZE);
4746 else
4747 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4748 iptr->isn_arg.outer.outer_depth,
4749 iptr->isn_arg.outer.outer_idx);
4750 }
4751 break;
4752 case ISN_LOADV:
4753 smsg("%s%4d LOADV v:%s", pfx, current,
4754 get_vim_var_name(iptr->isn_arg.number));
4755 break;
4756 case ISN_LOADSCRIPT:
4757 {
4758 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4759 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4760 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4761 + sref->sref_idx;
4762
4763 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4764 sv->sv_name,
4765 sref->sref_idx,
4766 si->sn_name);
4767 }
4768 break;
4769 case ISN_LOADS:
4770 {
4771 scriptitem_T *si = SCRIPT_ITEM(
4772 iptr->isn_arg.loadstore.ls_sid);
4773
4774 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4775 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4776 }
4777 break;
4778 case ISN_LOADAUTO:
4779 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4780 break;
4781 case ISN_LOADG:
4782 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4783 break;
4784 case ISN_LOADB:
4785 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4786 break;
4787 case ISN_LOADW:
4788 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4789 break;
4790 case ISN_LOADT:
4791 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4792 break;
4793 case ISN_LOADGDICT:
4794 smsg("%s%4d LOAD g:", pfx, current);
4795 break;
4796 case ISN_LOADBDICT:
4797 smsg("%s%4d LOAD b:", pfx, current);
4798 break;
4799 case ISN_LOADWDICT:
4800 smsg("%s%4d LOAD w:", pfx, current);
4801 break;
4802 case ISN_LOADTDICT:
4803 smsg("%s%4d LOAD t:", pfx, current);
4804 break;
4805 case ISN_LOADOPT:
4806 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4807 break;
4808 case ISN_LOADENV:
4809 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4810 break;
4811 case ISN_LOADREG:
4812 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4813 break;
4814
4815 case ISN_STORE:
4816 if (iptr->isn_arg.number < 0)
4817 smsg("%s%4d STORE arg[%lld]", pfx, current,
4818 iptr->isn_arg.number + STACK_FRAME_SIZE);
4819 else
4820 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4821 break;
4822 case ISN_STOREOUTER:
4823 {
4824 if (iptr->isn_arg.number < 0)
4825 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4826 iptr->isn_arg.outer.outer_depth,
4827 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4828 else
4829 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4830 iptr->isn_arg.outer.outer_depth,
4831 iptr->isn_arg.outer.outer_idx);
4832 }
4833 break;
4834 case ISN_STOREV:
4835 smsg("%s%4d STOREV v:%s", pfx, current,
4836 get_vim_var_name(iptr->isn_arg.number));
4837 break;
4838 case ISN_STOREAUTO:
4839 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4840 break;
4841 case ISN_STOREG:
4842 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4843 break;
4844 case ISN_STOREB:
4845 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4846 break;
4847 case ISN_STOREW:
4848 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4849 break;
4850 case ISN_STORET:
4851 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4852 break;
4853 case ISN_STORES:
4854 {
4855 scriptitem_T *si = SCRIPT_ITEM(
4856 iptr->isn_arg.loadstore.ls_sid);
4857
4858 smsg("%s%4d STORES %s in %s", pfx, current,
4859 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4860 }
4861 break;
4862 case ISN_STORESCRIPT:
4863 {
4864 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4865 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4866 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4867 + sref->sref_idx;
4868
4869 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4870 sv->sv_name,
4871 sref->sref_idx,
4872 si->sn_name);
4873 }
4874 break;
4875 case ISN_STOREOPT:
4876 smsg("%s%4d STOREOPT &%s", pfx, current,
4877 iptr->isn_arg.storeopt.so_name);
4878 break;
4879 case ISN_STOREENV:
4880 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4881 break;
4882 case ISN_STOREREG:
4883 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4884 break;
4885 case ISN_STORENR:
4886 smsg("%s%4d STORE %lld in $%d", pfx, current,
4887 iptr->isn_arg.storenr.stnr_val,
4888 iptr->isn_arg.storenr.stnr_idx);
4889 break;
4890
4891 case ISN_STOREINDEX:
4892 smsg("%s%4d STOREINDEX %s", pfx, current,
4893 vartype_name(iptr->isn_arg.vartype));
4894 break;
4895
4896 case ISN_STORERANGE:
4897 smsg("%s%4d STORERANGE", pfx, current);
4898 break;
4899
4900 // constants
4901 case ISN_PUSHNR:
4902 smsg("%s%4d PUSHNR %lld", pfx, current,
4903 (varnumber_T)(iptr->isn_arg.number));
4904 break;
4905 case ISN_PUSHBOOL:
4906 case ISN_PUSHSPEC:
4907 smsg("%s%4d PUSH %s", pfx, current,
4908 get_var_special_name(iptr->isn_arg.number));
4909 break;
4910 case ISN_PUSHF:
4911#ifdef FEAT_FLOAT
4912 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4913#endif
4914 break;
4915 case ISN_PUSHS:
4916 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4917 break;
4918 case ISN_PUSHBLOB:
4919 {
4920 char_u *r;
4921 char_u numbuf[NUMBUFLEN];
4922 char_u *tofree;
4923
4924 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4925 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4926 vim_free(tofree);
4927 }
4928 break;
4929 case ISN_PUSHFUNC:
4930 {
4931 char *name = (char *)iptr->isn_arg.string;
4932
4933 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4934 name == NULL ? "[none]" : name);
4935 }
4936 break;
4937 case ISN_PUSHCHANNEL:
4938#ifdef FEAT_JOB_CHANNEL
4939 {
4940 channel_T *channel = iptr->isn_arg.channel;
4941
4942 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4943 channel == NULL ? 0 : channel->ch_id);
4944 }
4945#endif
4946 break;
4947 case ISN_PUSHJOB:
4948#ifdef FEAT_JOB_CHANNEL
4949 {
4950 typval_T tv;
4951 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004952 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02004953
4954 tv.v_type = VAR_JOB;
4955 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004956 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004957 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4958 }
4959#endif
4960 break;
4961 case ISN_PUSHEXC:
4962 smsg("%s%4d PUSH v:exception", pfx, current);
4963 break;
4964 case ISN_UNLET:
4965 smsg("%s%4d UNLET%s %s", pfx, current,
4966 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4967 iptr->isn_arg.unlet.ul_name);
4968 break;
4969 case ISN_UNLETENV:
4970 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4971 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4972 iptr->isn_arg.unlet.ul_name);
4973 break;
4974 case ISN_UNLETINDEX:
4975 smsg("%s%4d UNLETINDEX", pfx, current);
4976 break;
4977 case ISN_UNLETRANGE:
4978 smsg("%s%4d UNLETRANGE", pfx, current);
4979 break;
4980 case ISN_LOCKCONST:
4981 smsg("%s%4d LOCKCONST", pfx, current);
4982 break;
4983 case ISN_NEWLIST:
4984 smsg("%s%4d NEWLIST size %lld", pfx, current,
4985 (varnumber_T)(iptr->isn_arg.number));
4986 break;
4987 case ISN_NEWDICT:
4988 smsg("%s%4d NEWDICT size %lld", pfx, current,
4989 (varnumber_T)(iptr->isn_arg.number));
4990 break;
4991
4992 // function call
4993 case ISN_BCALL:
4994 {
4995 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4996
4997 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4998 internal_func_name(cbfunc->cbf_idx),
4999 cbfunc->cbf_argcount);
5000 }
5001 break;
5002 case ISN_DCALL:
5003 {
5004 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5005 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5006 + cdfunc->cdf_idx;
5007
5008 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
5009 df->df_ufunc->uf_name_exp != NULL
5010 ? df->df_ufunc->uf_name_exp
5011 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
5012 }
5013 break;
5014 case ISN_UCALL:
5015 {
5016 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5017
5018 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5019 cufunc->cuf_name, cufunc->cuf_argcount);
5020 }
5021 break;
5022 case ISN_PCALL:
5023 {
5024 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5025
5026 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5027 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5028 }
5029 break;
5030 case ISN_PCALL_END:
5031 smsg("%s%4d PCALL end", pfx, current);
5032 break;
5033 case ISN_RETURN:
5034 smsg("%s%4d RETURN", pfx, current);
5035 break;
5036 case ISN_RETURN_ZERO:
5037 smsg("%s%4d RETURN 0", pfx, current);
5038 break;
5039 case ISN_FUNCREF:
5040 {
5041 funcref_T *funcref = &iptr->isn_arg.funcref;
5042 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5043 + funcref->fr_func;
5044
5045 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5046 }
5047 break;
5048
5049 case ISN_NEWFUNC:
5050 {
5051 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5052
5053 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5054 newfunc->nf_lambda, newfunc->nf_global);
5055 }
5056 break;
5057
5058 case ISN_DEF:
5059 {
5060 char_u *name = iptr->isn_arg.string;
5061
5062 smsg("%s%4d DEF %s", pfx, current,
5063 name == NULL ? (char_u *)"" : name);
5064 }
5065 break;
5066
5067 case ISN_JUMP:
5068 {
5069 char *when = "?";
5070
5071 switch (iptr->isn_arg.jump.jump_when)
5072 {
5073 case JUMP_ALWAYS:
5074 when = "JUMP";
5075 break;
5076 case JUMP_AND_KEEP_IF_TRUE:
5077 when = "JUMP_AND_KEEP_IF_TRUE";
5078 break;
5079 case JUMP_IF_FALSE:
5080 when = "JUMP_IF_FALSE";
5081 break;
5082 case JUMP_AND_KEEP_IF_FALSE:
5083 when = "JUMP_AND_KEEP_IF_FALSE";
5084 break;
5085 case JUMP_IF_COND_FALSE:
5086 when = "JUMP_IF_COND_FALSE";
5087 break;
5088 case JUMP_IF_COND_TRUE:
5089 when = "JUMP_IF_COND_TRUE";
5090 break;
5091 }
5092 smsg("%s%4d %s -> %d", pfx, current, when,
5093 iptr->isn_arg.jump.jump_where);
5094 }
5095 break;
5096
5097 case ISN_JUMP_IF_ARG_SET:
5098 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5099 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5100 iptr->isn_arg.jump.jump_where);
5101 break;
5102
5103 case ISN_FOR:
5104 {
5105 forloop_T *forloop = &iptr->isn_arg.forloop;
5106
5107 smsg("%s%4d FOR $%d -> %d", pfx, current,
5108 forloop->for_idx, forloop->for_end);
5109 }
5110 break;
5111
5112 case ISN_TRY:
5113 {
5114 try_T *try = &iptr->isn_arg.try;
5115
5116 if (try->try_ref->try_finally == 0)
5117 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5118 pfx, current,
5119 try->try_ref->try_catch,
5120 try->try_ref->try_endtry);
5121 else
5122 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5123 pfx, current,
5124 try->try_ref->try_catch,
5125 try->try_ref->try_finally,
5126 try->try_ref->try_endtry);
5127 }
5128 break;
5129 case ISN_CATCH:
5130 // TODO
5131 smsg("%s%4d CATCH", pfx, current);
5132 break;
5133 case ISN_TRYCONT:
5134 {
5135 trycont_T *trycont = &iptr->isn_arg.trycont;
5136
5137 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5138 trycont->tct_levels,
5139 trycont->tct_levels == 1 ? "" : "s",
5140 trycont->tct_where);
5141 }
5142 break;
5143 case ISN_FINALLY:
5144 smsg("%s%4d FINALLY", pfx, current);
5145 break;
5146 case ISN_ENDTRY:
5147 smsg("%s%4d ENDTRY", pfx, current);
5148 break;
5149 case ISN_THROW:
5150 smsg("%s%4d THROW", pfx, current);
5151 break;
5152
5153 // expression operations on number
5154 case ISN_OPNR:
5155 case ISN_OPFLOAT:
5156 case ISN_OPANY:
5157 {
5158 char *what;
5159 char *ins;
5160
5161 switch (iptr->isn_arg.op.op_type)
5162 {
5163 case EXPR_MULT: what = "*"; break;
5164 case EXPR_DIV: what = "/"; break;
5165 case EXPR_REM: what = "%"; break;
5166 case EXPR_SUB: what = "-"; break;
5167 case EXPR_ADD: what = "+"; break;
5168 default: what = "???"; break;
5169 }
5170 switch (iptr->isn_type)
5171 {
5172 case ISN_OPNR: ins = "OPNR"; break;
5173 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5174 case ISN_OPANY: ins = "OPANY"; break;
5175 default: ins = "???"; break;
5176 }
5177 smsg("%s%4d %s %s", pfx, current, ins, what);
5178 }
5179 break;
5180
5181 case ISN_COMPAREBOOL:
5182 case ISN_COMPARESPECIAL:
5183 case ISN_COMPARENR:
5184 case ISN_COMPAREFLOAT:
5185 case ISN_COMPARESTRING:
5186 case ISN_COMPAREBLOB:
5187 case ISN_COMPARELIST:
5188 case ISN_COMPAREDICT:
5189 case ISN_COMPAREFUNC:
5190 case ISN_COMPAREANY:
5191 {
5192 char *p;
5193 char buf[10];
5194 char *type;
5195
5196 switch (iptr->isn_arg.op.op_type)
5197 {
5198 case EXPR_EQUAL: p = "=="; break;
5199 case EXPR_NEQUAL: p = "!="; break;
5200 case EXPR_GREATER: p = ">"; break;
5201 case EXPR_GEQUAL: p = ">="; break;
5202 case EXPR_SMALLER: p = "<"; break;
5203 case EXPR_SEQUAL: p = "<="; break;
5204 case EXPR_MATCH: p = "=~"; break;
5205 case EXPR_IS: p = "is"; break;
5206 case EXPR_ISNOT: p = "isnot"; break;
5207 case EXPR_NOMATCH: p = "!~"; break;
5208 default: p = "???"; break;
5209 }
5210 STRCPY(buf, p);
5211 if (iptr->isn_arg.op.op_ic == TRUE)
5212 strcat(buf, "?");
5213 switch(iptr->isn_type)
5214 {
5215 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5216 case ISN_COMPARESPECIAL:
5217 type = "COMPARESPECIAL"; break;
5218 case ISN_COMPARENR: type = "COMPARENR"; break;
5219 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5220 case ISN_COMPARESTRING:
5221 type = "COMPARESTRING"; break;
5222 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5223 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5224 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5225 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5226 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5227 default: type = "???"; break;
5228 }
5229
5230 smsg("%s%4d %s %s", pfx, current, type, buf);
5231 }
5232 break;
5233
5234 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5235 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5236
5237 // expression operations
5238 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5239 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5240 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5241 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5242 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5243 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5244 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5245 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5246 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5247 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5248 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5249 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5250 pfx, current, iptr->isn_arg.number); break;
5251 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5252 pfx, current, iptr->isn_arg.number); break;
5253 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5254 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5255 iptr->isn_arg.string); break;
5256 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5257
5258 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5259 case ISN_CHECKTYPE:
5260 {
5261 checktype_T *ct = &iptr->isn_arg.type;
5262 char *tofree;
5263
5264 if (ct->ct_arg_idx == 0)
5265 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5266 type_name(ct->ct_type, &tofree),
5267 (int)ct->ct_off);
5268 else
5269 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5270 type_name(ct->ct_type, &tofree),
5271 (int)ct->ct_off,
5272 (int)ct->ct_arg_idx);
5273 vim_free(tofree);
5274 break;
5275 }
5276 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5277 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5278 iptr->isn_arg.checklen.cl_min_len);
5279 break;
5280 case ISN_SETTYPE:
5281 {
5282 char *tofree;
5283
5284 smsg("%s%4d SETTYPE %s", pfx, current,
5285 type_name(iptr->isn_arg.type.ct_type, &tofree));
5286 vim_free(tofree);
5287 break;
5288 }
5289 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005290 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5291 smsg("%s%4d INVERT %d (!val)", pfx, current,
5292 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005293 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005294 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5295 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005296 break;
5297 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005298 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005299 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005300 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5301 pfx, current,
5302 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005303 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005304 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5305 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005306 break;
5307 case ISN_PUT:
5308 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5309 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005310 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005311 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5312 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005313 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005314 else
5315 smsg("%s%4d PUT %c %ld", pfx, current,
5316 iptr->isn_arg.put.put_regname,
5317 (long)iptr->isn_arg.put.put_lnum);
5318 break;
5319
5320 // TODO: summarize modifiers
5321 case ISN_CMDMOD:
5322 {
5323 char_u *buf;
5324 size_t len = produce_cmdmods(
5325 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5326
5327 buf = alloc(len + 1);
5328 if (buf != NULL)
5329 {
5330 (void)produce_cmdmods(
5331 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5332 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5333 vim_free(buf);
5334 }
5335 break;
5336 }
5337 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5338
5339 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005340 smsg("%s%4d PROFILE START line %d", pfx, current,
5341 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005342 break;
5343
5344 case ISN_PROF_END:
5345 smsg("%s%4d PROFILE END", pfx, current);
5346 break;
5347
Bram Moolenaare99d4222021-06-13 14:01:26 +02005348 case ISN_DEBUG:
5349 smsg("%s%4d DEBUG line %d", pfx, current, iptr->isn_lnum);
5350 break;
5351
Bram Moolenaar4c137212021-04-19 16:48:48 +02005352 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5353 iptr->isn_arg.unpack.unp_count,
5354 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5355 break;
5356 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5357 iptr->isn_arg.shuffle.shfl_item,
5358 iptr->isn_arg.shuffle.shfl_up);
5359 break;
5360 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5361
5362 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5363 return;
5364 }
5365
5366 out_flush(); // output one line at a time
5367 ui_breakcheck();
5368 if (got_int)
5369 break;
5370 }
5371}
5372
5373/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005374 * Handle command line completion for the :disassemble command.
5375 */
5376 void
5377set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5378{
5379 char_u *p;
5380
5381 // Default: expand user functions, "debug" and "profile"
5382 xp->xp_context = EXPAND_DISASSEMBLE;
5383 xp->xp_pattern = arg;
5384
5385 // first argument already typed: only user function names
5386 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5387 {
5388 xp->xp_context = EXPAND_USER_FUNC;
5389 xp->xp_pattern = skipwhite(p);
5390 }
5391}
5392
5393/*
5394 * Function given to ExpandGeneric() to obtain the list of :disassemble
5395 * arguments.
5396 */
5397 char_u *
5398get_disassemble_argument(expand_T *xp, int idx)
5399{
5400 if (idx == 0)
5401 return (char_u *)"debug";
5402 if (idx == 1)
5403 return (char_u *)"profile";
5404 return get_user_func_name(xp, idx - 2);
5405}
5406
5407/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005408 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005409 * We don't really need this at runtime, but we do have tests that require it,
5410 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005411 */
5412 void
5413ex_disassemble(exarg_T *eap)
5414{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005415 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005416 char_u *fname;
5417 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 dfunc_T *dfunc;
5419 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005420 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005421 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005422 compiletype_T compile_type = CT_NONE;
5423
5424 if (STRNCMP(arg, "profile", 7) == 0)
5425 {
5426 compile_type = CT_PROFILE;
5427 arg = skipwhite(arg + 7);
5428 }
5429 else if (STRNCMP(arg, "debug", 5) == 0)
5430 {
5431 compile_type = CT_DEBUG;
5432 arg = skipwhite(arg + 5);
5433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005435 if (STRNCMP(arg, "<lambda>", 8) == 0)
5436 {
5437 arg += 8;
5438 (void)getdigits(&arg);
5439 fname = vim_strnsave(eap->arg, arg - eap->arg);
5440 }
5441 else
5442 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005443 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005444 if (fname == NULL)
5445 {
5446 semsg(_(e_invarg2), eap->arg);
5447 return;
5448 }
5449
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005450 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005451 if (ufunc == NULL)
5452 {
5453 char_u *p = untrans_function_name(fname);
5454
5455 if (p != NULL)
5456 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005457 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005458 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005459 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460 if (ufunc == NULL)
5461 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005462 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463 return;
5464 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02005465 if (func_needs_compiling(ufunc, compile_type)
5466 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005467 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005468 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005470 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471 return;
5472 }
5473 if (ufunc->uf_name_exp != NULL)
5474 msg((char *)ufunc->uf_name_exp);
5475 else
5476 msg((char *)ufunc->uf_name);
5477
5478 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005479 switch (compile_type)
5480 {
5481 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01005482#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02005483 instr = dfunc->df_instr_prof;
5484 instr_count = dfunc->df_instr_prof_count;
5485 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005486#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02005487 // FALLTHROUGH
5488 case CT_NONE:
5489 instr = dfunc->df_instr;
5490 instr_count = dfunc->df_instr_count;
5491 break;
5492 case CT_DEBUG:
5493 instr = dfunc->df_instr_debug;
5494 instr_count = dfunc->df_instr_debug_count;
5495 break;
5496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497
Bram Moolenaar4c137212021-04-19 16:48:48 +02005498 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499}
5500
5501/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005502 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503 * list, etc. Mostly like what JavaScript does, except that empty list and
5504 * empty dictionary are FALSE.
5505 */
5506 int
5507tv2bool(typval_T *tv)
5508{
5509 switch (tv->v_type)
5510 {
5511 case VAR_NUMBER:
5512 return tv->vval.v_number != 0;
5513 case VAR_FLOAT:
5514#ifdef FEAT_FLOAT
5515 return tv->vval.v_float != 0.0;
5516#else
5517 break;
5518#endif
5519 case VAR_PARTIAL:
5520 return tv->vval.v_partial != NULL;
5521 case VAR_FUNC:
5522 case VAR_STRING:
5523 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5524 case VAR_LIST:
5525 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5526 case VAR_DICT:
5527 return tv->vval.v_dict != NULL
5528 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5529 case VAR_BOOL:
5530 case VAR_SPECIAL:
5531 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5532 case VAR_JOB:
5533#ifdef FEAT_JOB_CHANNEL
5534 return tv->vval.v_job != NULL;
5535#else
5536 break;
5537#endif
5538 case VAR_CHANNEL:
5539#ifdef FEAT_JOB_CHANNEL
5540 return tv->vval.v_channel != NULL;
5541#else
5542 break;
5543#endif
5544 case VAR_BLOB:
5545 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5546 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005547 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005549 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 break;
5551 }
5552 return FALSE;
5553}
5554
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005555 void
5556emsg_using_string_as(typval_T *tv, int as_number)
5557{
5558 semsg(_(as_number ? e_using_string_as_number_str
5559 : e_using_string_as_bool_str),
5560 tv->vval.v_string == NULL
5561 ? (char_u *)"" : tv->vval.v_string);
5562}
5563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564/*
5565 * If "tv" is a string give an error and return FAIL.
5566 */
5567 int
5568check_not_string(typval_T *tv)
5569{
5570 if (tv->v_type == VAR_STRING)
5571 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005572 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573 clear_tv(tv);
5574 return FAIL;
5575 }
5576 return OK;
5577}
5578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579
5580#endif // FEAT_EVAL