blob: 767dcea3c82bc896f927925ee2f514d3c6a89a45 [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.
207 if (func_needs_compiling(ufunc, PROFILING(ufunc))
208 && compile_def_function(ufunc, FALSE, PROFILING(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;
267
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100268 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200269 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100270 {
271 floc = ALLOC_ONE(funclocal_T);
272 if (floc == NULL)
273 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200274 *floc = ectx->ec_funclocal;
275 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100276 }
277
Bram Moolenaarfe270812020-04-11 22:31:27 +0200278 // Move the vararg-list to below the missing optional arguments.
279 if (vararg_count > 0 && arg_to_add > 0)
280 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100281
282 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200283 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200284 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200285 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286
287 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100288 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
289 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200290 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200291 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
292 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100293 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100294 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200295 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296
297 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200298 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100299 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200300 if (dfunc->df_has_closure)
301 {
302 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
303
304 tv->v_type = VAR_NUMBER;
305 tv->vval.v_number = 0;
306 }
307 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100309 if (pt != NULL || ufunc->uf_partial != NULL
310 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100311 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200312 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100313
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200314 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100315 return FAIL;
316 if (pt != NULL)
317 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200318 ref->or_outer = &pt->pt_outer;
319 ++pt->pt_refcount;
320 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100321 }
322 else if (ufunc->uf_partial != NULL)
323 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200324 ref->or_outer = &ufunc->uf_partial->pt_outer;
325 ++ufunc->uf_partial->pt_refcount;
326 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100327 }
328 else
329 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200330 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
331 if (ref->or_outer == NULL)
332 {
333 vim_free(ref);
334 return FAIL;
335 }
336 ref->or_outer_allocated = TRUE;
337 ref->or_outer->out_stack = &ectx->ec_stack;
338 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
339 if (ectx->ec_outer_ref != NULL)
340 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100341 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200342 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100343 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100344 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200345 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100346
Bram Moolenaarc970e422021-03-17 15:03:04 +0100347 ++ufunc->uf_calls;
348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349 // Set execution state to the start of the called function.
350 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100351 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100352 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200353 if (entry != NULL)
354 {
355 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200356 // Save the current context so it can be restored on return.
357 entry->es_save_sctx = current_sctx;
358 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200359 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100360
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200361 // Start execution at the first instruction.
362 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363
364 return OK;
365}
366
367// Get pointer to item in the stack.
368#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
369
370/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200371 * Used when returning from a function: Check if any closure is still
372 * referenced. If so then move the arguments and variables to a separate piece
373 * of stack to be used when the closure is called.
374 * When "free_arguments" is TRUE the arguments are to be freed.
375 * Returns FAIL when out of memory.
376 */
377 static int
378handle_closure_in_use(ectx_T *ectx, int free_arguments)
379{
380 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
381 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200382 int argcount;
383 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200384 int idx;
385 typval_T *tv;
386 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200387 garray_T *gap = &ectx->ec_funcrefs;
388 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200389
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200390 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200391 return OK; // function was freed
392 if (dfunc->df_has_closure == 0)
393 return OK; // no closures
394 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
395 closure_count = tv->vval.v_number;
396 if (closure_count == 0)
397 return OK; // no funcrefs created
398
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200399 argcount = ufunc_argcount(dfunc->df_ufunc);
400 top = ectx->ec_frame_idx - argcount;
401
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200402 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200403 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200405 partial_T *pt;
406 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200407
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200408 if (off < 0)
409 continue; // count is off or already done
410 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200411 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200412 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200413 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200414 int i;
415
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200416 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200417 // unreferenced on return.
418 for (i = 0; i < dfunc->df_varcount; ++i)
419 {
420 typval_T *stv = STACK_TV(ectx->ec_frame_idx
421 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200422 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200423 --refcount;
424 }
425 if (refcount > 1)
426 {
427 closure_in_use = TRUE;
428 break;
429 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200430 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200431 }
432
433 if (closure_in_use)
434 {
435 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
436 typval_T *stack;
437
438 // A closure is using the arguments and/or local variables.
439 // Move them to the called function.
440 if (funcstack == NULL)
441 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200442 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
443 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200444 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
445 funcstack->fs_ga.ga_data = stack;
446 if (stack == NULL)
447 {
448 vim_free(funcstack);
449 return FAIL;
450 }
451
452 // Move or copy the arguments.
453 for (idx = 0; idx < argcount; ++idx)
454 {
455 tv = STACK_TV(top + idx);
456 if (free_arguments)
457 {
458 *(stack + idx) = *tv;
459 tv->v_type = VAR_UNKNOWN;
460 }
461 else
462 copy_tv(tv, stack + idx);
463 }
464 // Move the local variables.
465 for (idx = 0; idx < dfunc->df_varcount; ++idx)
466 {
467 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200468
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200469 // A partial created for a local function, that is also used as a
470 // local variable, has a reference count for the variable, thus
471 // will never go down to zero. When all these refcounts are one
472 // then the funcstack is unused. We need to count how many we have
473 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200474 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
475 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200476 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200477
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200478 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200479 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
480 gap->ga_len - closure_count + i])
481 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200482 }
483
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200484 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200485 tv->v_type = VAR_UNKNOWN;
486 }
487
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200488 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200489 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200490 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
491 - closure_count + idx];
492 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200493 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200494 ++funcstack->fs_refcount;
495 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100496 pt->pt_outer.out_stack = &funcstack->fs_ga;
497 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200498 }
499 }
500 }
501
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200502 for (idx = 0; idx < closure_count; ++idx)
503 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
504 - closure_count + idx]);
505 gap->ga_len -= closure_count;
506 if (gap->ga_len == 0)
507 ga_clear(gap);
508
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200509 return OK;
510}
511
512/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200513 * Called when a partial is freed or its reference count goes down to one. The
514 * funcstack may be the only reference to the partials in the local variables.
515 * Go over all of them, the funcref and can be freed if all partials
516 * referencing the funcstack have a reference count of one.
517 */
518 void
519funcstack_check_refcount(funcstack_T *funcstack)
520{
521 int i;
522 garray_T *gap = &funcstack->fs_ga;
523 int done = 0;
524
525 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
526 return;
527 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
528 {
529 typval_T *tv = ((typval_T *)gap->ga_data) + i;
530
531 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
532 && tv->vval.v_partial->pt_funcstack == funcstack
533 && tv->vval.v_partial->pt_refcount == 1)
534 ++done;
535 }
536 if (done == funcstack->fs_min_refcount)
537 {
538 typval_T *stack = gap->ga_data;
539
540 // All partials referencing the funcstack have a reference count of
541 // one, thus the funcstack is no longer of use.
542 for (i = 0; i < gap->ga_len; ++i)
543 clear_tv(stack + i);
544 vim_free(stack);
545 vim_free(funcstack);
546 }
547}
548
549/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 * Return from the current function.
551 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200552 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200553func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100556 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200557 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
558 + ectx->ec_dfunc_idx;
559 int argcount = ufunc_argcount(dfunc->df_ufunc);
560 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200561 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100562 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
563 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200564 funclocal_T *floc;
565#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100566 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
567 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568
Bram Moolenaar12d26532021-02-19 19:13:21 +0100569 if (do_profiling == PROF_YES)
570 {
571 ufunc_T *caller = prev_dfunc->df_ufunc;
572
573 if (dfunc->df_ufunc->uf_profiling
574 || (caller != NULL && caller->uf_profiling))
575 {
576 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
577 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
578 --profile_info_ga.ga_len;
579 }
580 }
581#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100582 // TODO: when is it safe to delete the function when it is no longer used?
583 --dfunc->df_ufunc->uf_calls;
584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200586 entry = estack_pop();
587 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200588 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200590 if (handle_closure_in_use(ectx, TRUE) == FAIL)
591 return FAIL;
592
593 // Clear the arguments.
594 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
595 clear_tv(STACK_TV(idx));
596
597 // Clear local variables and temp values, but not the return value.
598 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100599 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100601
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100602 // The return value should be on top of the stack. However, when aborting
603 // it may not be there and ec_frame_idx is the top of the stack.
604 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100605 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100606 ret_idx = 0;
607
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200608 if (ectx->ec_outer_ref != NULL)
609 {
610 if (ectx->ec_outer_ref->or_outer_allocated)
611 vim_free(ectx->ec_outer_ref->or_outer);
612 partial_unref(ectx->ec_outer_ref->or_partial);
613 vim_free(ectx->ec_outer_ref);
614 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100615
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100616 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100617 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100618 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
619 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200620 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
621 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200622 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100623 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100624 floc = (void *)STACK_TV(ectx->ec_frame_idx
625 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200626 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100627 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
628 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200629
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100630 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200631 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100632 else
633 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200634 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100635 vim_free(floc);
636 }
637
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100638 if (ret_idx > 0)
639 {
640 // Reset the stack to the position before the call, with a spot for the
641 // return value, moved there from above the frame.
642 ectx->ec_stack.ga_len = top + 1;
643 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
644 }
645 else
646 // Reset the stack to the position before the call.
647 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200648
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100649 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200650 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651}
652
653#undef STACK_TV
654
655/*
656 * Prepare arguments and rettv for calling a builtin or user function.
657 */
658 static int
659call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
660{
661 int idx;
662 typval_T *tv;
663
664 // Move arguments from bottom of the stack to argvars[] and add terminator.
665 for (idx = 0; idx < argcount; ++idx)
666 argvars[idx] = *STACK_TV_BOT(idx - argcount);
667 argvars[argcount].v_type = VAR_UNKNOWN;
668
669 // Result replaces the arguments on the stack.
670 if (argcount > 0)
671 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200672 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 return FAIL;
674 else
675 ++ectx->ec_stack.ga_len;
676
677 // Default return value is zero.
678 tv = STACK_TV_BOT(-1);
679 tv->v_type = VAR_NUMBER;
680 tv->vval.v_number = 0;
681
682 return OK;
683}
684
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200685// Ugly global to avoid passing the execution context around through many
686// layers.
687static ectx_T *current_ectx = NULL;
688
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689/*
690 * Call a builtin function by index.
691 */
692 static int
693call_bfunc(int func_idx, int argcount, ectx_T *ectx)
694{
695 typval_T argvars[MAX_FUNC_ARGS];
696 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100697 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200698 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699
700 if (call_prepare(argcount, argvars, ectx) == FAIL)
701 return FAIL;
702
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200703 // Call the builtin function. Set "current_ectx" so that when it
704 // recursively invokes call_def_function() a closure context can be set.
705 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200707 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708
709 // Clear the arguments.
710 for (idx = 0; idx < argcount; ++idx)
711 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200712
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100713 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200714 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 return OK;
716}
717
718/*
719 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100720 * If the function is compiled this will add a stack frame and set the
721 * instruction pointer at the start of the function.
722 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200723 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100724 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 */
726 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100727call_ufunc(
728 ufunc_T *ufunc,
729 partial_T *pt,
730 int argcount,
731 ectx_T *ectx,
732 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733{
734 typval_T argvars[MAX_FUNC_ARGS];
735 funcexe_T funcexe;
736 int error;
737 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100738 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100739#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100740 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100741#else
742# define profiling FALSE
743#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744
Bram Moolenaarb2049902021-01-24 12:53:53 +0100745 if (func_needs_compiling(ufunc, profiling)
746 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200747 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200748 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100749 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100750 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100751 if (error != FCERR_UNKNOWN)
752 {
753 if (error == FCERR_TOOMANY)
754 semsg(_(e_toomanyarg), ufunc->uf_name);
755 else
756 semsg(_(e_toofewarg), ufunc->uf_name);
757 return FAIL;
758 }
759
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100760 // The function has been compiled, can call it quickly. For a function
761 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100762 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100763 if (iptr != NULL)
764 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100765 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100766 iptr->isn_type = ISN_DCALL;
767 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
768 iptr->isn_arg.dfunc.cdf_argcount = argcount;
769 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200770 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772
773 if (call_prepare(argcount, argvars, ectx) == FAIL)
774 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200775 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 funcexe.evaluate = TRUE;
777
778 // Call the user function. Result goes in last position on the stack.
779 // TODO: add selfdict if there is one
780 error = call_user_func_check(ufunc, argcount, argvars,
781 STACK_TV_BOT(-1), &funcexe, NULL);
782
783 // Clear the arguments.
784 for (idx = 0; idx < argcount; ++idx)
785 clear_tv(&argvars[idx]);
786
787 if (error != FCERR_NONE)
788 {
789 user_func_error(error, ufunc->uf_name);
790 return FAIL;
791 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100792 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200793 // Error other than from calling the function itself.
794 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 return OK;
796}
797
798/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100799 * If command modifiers were applied restore them.
800 */
801 static void
802may_restore_cmdmod(funclocal_T *funclocal)
803{
804 if (funclocal->floc_restore_cmdmod)
805 {
806 cmdmod.cmod_filter_regmatch.regprog = NULL;
807 undo_cmdmod(&cmdmod);
808 cmdmod = funclocal->floc_save_cmdmod;
809 funclocal->floc_restore_cmdmod = FALSE;
810 }
811}
812
813/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200814 * Return TRUE if an error was given or CTRL-C was pressed.
815 */
816 static int
817vim9_aborting(int prev_called_emsg)
818{
819 return called_emsg > prev_called_emsg || got_int || did_throw;
820}
821
822/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 * Execute a function by "name".
824 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100825 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 * Returns FAIL if not found without an error message.
827 */
828 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100829call_by_name(
830 char_u *name,
831 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100832 ectx_T *ectx,
833 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834{
835 ufunc_T *ufunc;
836
837 if (builtin_function(name, -1))
838 {
839 int func_idx = find_internal_func(name);
840
841 if (func_idx < 0)
842 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200843 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844 return FAIL;
845 return call_bfunc(func_idx, argcount, ectx);
846 }
847
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200848 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200849
850 if (ufunc == NULL)
851 {
852 int called_emsg_before = called_emsg;
853
854 if (script_autoload(name, TRUE))
855 // loaded a package, search for the function again
856 ufunc = find_func(name, FALSE, NULL);
857 if (vim9_aborting(called_emsg_before))
858 return FAIL; // bail out if loading the script caused an error
859 }
860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100862 {
863 if (ufunc->uf_arg_types != NULL)
864 {
865 int i;
866 typval_T *argv = STACK_TV_BOT(0) - argcount;
867
868 // The function can change at runtime, check that the argument
869 // types are correct.
870 for (i = 0; i < argcount; ++i)
871 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100872 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100873
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100874 if (i < ufunc->uf_args.ga_len)
875 type = ufunc->uf_arg_types[i];
876 else if (ufunc->uf_va_type != NULL)
877 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100878 if (type != NULL && check_typval_arg_type(type,
879 &argv[i], i + 1) == FAIL)
880 return FAIL;
881 }
882 }
883
Bram Moolenaar4c137212021-04-19 16:48:48 +0200884 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886
887 return FAIL;
888}
889
890 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100891call_partial(
892 typval_T *tv,
893 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100894 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200896 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200897 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100899 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
901 if (tv->v_type == VAR_PARTIAL)
902 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200903 partial_T *pt = tv->vval.v_partial;
904 int i;
905
906 if (pt->pt_argc > 0)
907 {
908 // Make space for arguments from the partial, shift the "argcount"
909 // arguments up.
910 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
911 return FAIL;
912 for (i = 1; i <= argcount; ++i)
913 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
914 ectx->ec_stack.ga_len += pt->pt_argc;
915 argcount += pt->pt_argc;
916
917 // copy the arguments from the partial onto the stack
918 for (i = 0; i < pt->pt_argc; ++i)
919 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921
922 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200923 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 name = pt->pt_name;
926 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200927 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200929 if (name != NULL)
930 {
931 char_u fname_buf[FLEN_FIXED + 1];
932 char_u *tofree = NULL;
933 int error = FCERR_NONE;
934 char_u *fname;
935
936 // May need to translate <SNR>123_ to K_SNR.
937 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
938 if (error != FCERR_NONE)
939 res = FAIL;
940 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200941 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200942 vim_free(tofree);
943 }
944
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100945 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 {
947 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200948 semsg(_(e_unknownfunc),
949 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 return FAIL;
951 }
952 return OK;
953}
954
955/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200956 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
957 * TRUE.
958 */
959 static int
960error_if_locked(int lock, char *error)
961{
962 if (lock & (VAR_LOCKED | VAR_FIXED))
963 {
964 emsg(_(error));
965 return TRUE;
966 }
967 return FALSE;
968}
969
970/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100971 * Give an error if "tv" is not a number and return FAIL.
972 */
973 static int
974check_for_number(typval_T *tv)
975{
976 if (tv->v_type != VAR_NUMBER)
977 {
978 semsg(_(e_expected_str_but_got_str),
979 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
980 return FAIL;
981 }
982 return OK;
983}
984
985/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100986 * Store "tv" in variable "name".
987 * This is for s: and g: variables.
988 */
989 static void
990store_var(char_u *name, typval_T *tv)
991{
992 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200993 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100994
Bram Moolenaard877a572021-04-01 19:42:48 +0200995 if (tv->v_lock)
996 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100997 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +0200998 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100999 restore_funccal();
1000}
1001
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001002/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001003 * Convert "tv" to a string.
1004 * Return FAIL if not allowed.
1005 */
1006 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001007do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001008{
1009 if (tv->v_type != VAR_STRING)
1010 {
1011 char_u *str;
1012
1013 if (is_2string_any)
1014 {
1015 switch (tv->v_type)
1016 {
1017 case VAR_SPECIAL:
1018 case VAR_BOOL:
1019 case VAR_NUMBER:
1020 case VAR_FLOAT:
1021 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001022
1023 case VAR_LIST:
1024 if (tolerant)
1025 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001026 char_u *s, *e, *p;
1027 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001028
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001029 ga_init2(&ga, sizeof(char_u *), 1);
1030
1031 // Convert to NL separated items, then
1032 // escape the items and replace the NL with
1033 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001034 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001035 if (str == NULL)
1036 return FAIL;
1037 s = str;
1038 while ((e = vim_strchr(s, '\n')) != NULL)
1039 {
1040 *e = NUL;
1041 p = vim_strsave_fnameescape(s, FALSE);
1042 if (p != NULL)
1043 {
1044 ga_concat(&ga, p);
1045 ga_concat(&ga, (char_u *)" ");
1046 vim_free(p);
1047 }
1048 s = e + 1;
1049 }
1050 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001051 clear_tv(tv);
1052 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001053 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001054 return OK;
1055 }
1056 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001057 default: to_string_error(tv->v_type);
1058 return FAIL;
1059 }
1060 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001061 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001062 clear_tv(tv);
1063 tv->v_type = VAR_STRING;
1064 tv->vval.v_string = str;
1065 }
1066 return OK;
1067}
1068
1069/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001070 * When the value of "sv" is a null list of dict, allocate it.
1071 */
1072 static void
1073allocate_if_null(typval_T *tv)
1074{
1075 switch (tv->v_type)
1076 {
1077 case VAR_LIST:
1078 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001079 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001080 break;
1081 case VAR_DICT:
1082 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001083 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001084 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001085 case VAR_BLOB:
1086 if (tv->vval.v_blob == NULL)
1087 (void)rettv_blob_alloc(tv);
1088 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001089 default:
1090 break;
1091 }
1092}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001093
Bram Moolenaare7525c52021-01-09 13:20:37 +01001094/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001095 * Return the character "str[index]" where "index" is the character index,
1096 * including composing characters.
1097 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001098 */
1099 char_u *
1100char_from_string(char_u *str, varnumber_T index)
1101{
1102 size_t nbyte = 0;
1103 varnumber_T nchar = index;
1104 size_t slen;
1105
1106 if (str == NULL)
1107 return NULL;
1108 slen = STRLEN(str);
1109
Bram Moolenaarff871402021-03-26 13:34:05 +01001110 // Do the same as for a list: a negative index counts from the end.
1111 // Optimization to check the first byte to be below 0x80 (and no composing
1112 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001113 if (index < 0)
1114 {
1115 int clen = 0;
1116
1117 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001118 {
1119 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1120 ++nbyte;
1121 else if (enc_utf8)
1122 nbyte += utfc_ptr2len(str + nbyte);
1123 else
1124 nbyte += mb_ptr2len(str + nbyte);
1125 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001126 nchar = clen + index;
1127 if (nchar < 0)
1128 // unlike list: index out of range results in empty string
1129 return NULL;
1130 }
1131
1132 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001133 {
1134 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1135 ++nbyte;
1136 else if (enc_utf8)
1137 nbyte += utfc_ptr2len(str + nbyte);
1138 else
1139 nbyte += mb_ptr2len(str + nbyte);
1140 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001141 if (nbyte >= slen)
1142 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001143 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001144}
1145
1146/*
1147 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001148 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001149 * If going over the end return "str_len".
1150 * If "idx" is negative count from the end, -1 is the last character.
1151 * When going over the start return -1.
1152 */
1153 static long
1154char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1155{
1156 varnumber_T nchar = idx;
1157 size_t nbyte = 0;
1158
1159 if (nchar >= 0)
1160 {
1161 while (nchar > 0 && nbyte < str_len)
1162 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001163 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001164 --nchar;
1165 }
1166 }
1167 else
1168 {
1169 nbyte = str_len;
1170 while (nchar < 0 && nbyte > 0)
1171 {
1172 --nbyte;
1173 nbyte -= mb_head_off(str, str + nbyte);
1174 ++nchar;
1175 }
1176 if (nchar < 0)
1177 return -1;
1178 }
1179 return (long)nbyte;
1180}
1181
1182/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001183 * Return the slice "str[first : last]" using character indexes. Composing
1184 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001185 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001186 * Return NULL when the result is empty.
1187 */
1188 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001189string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001190{
1191 long start_byte, end_byte;
1192 size_t slen;
1193
1194 if (str == NULL)
1195 return NULL;
1196 slen = STRLEN(str);
1197 start_byte = char_idx2byte(str, slen, first);
1198 if (start_byte < 0)
1199 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001200 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001201 end_byte = (long)slen;
1202 else
1203 {
1204 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001205 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001206 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001207 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001208 }
1209
1210 if (start_byte >= (long)slen || end_byte <= start_byte)
1211 return NULL;
1212 return vim_strnsave(str + start_byte, end_byte - start_byte);
1213}
1214
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001215 static svar_T *
1216get_script_svar(scriptref_T *sref, ectx_T *ectx)
1217{
1218 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1219 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1220 + ectx->ec_dfunc_idx;
1221 svar_T *sv;
1222
1223 if (sref->sref_seq != si->sn_script_seq)
1224 {
1225 // The script was reloaded after the function was
1226 // compiled, the script_idx may not be valid.
1227 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1228 dfunc->df_ufunc->uf_name_exp);
1229 return NULL;
1230 }
1231 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1232 if (!equal_type(sv->sv_type, sref->sref_type))
1233 {
1234 emsg(_(e_script_variable_type_changed));
1235 return NULL;
1236 }
1237 return sv;
1238}
1239
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001240/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001241 * Function passed to do_cmdline() for splitting a script joined by NL
1242 * characters.
1243 */
1244 static char_u *
1245get_split_sourceline(
1246 int c UNUSED,
1247 void *cookie,
1248 int indent UNUSED,
1249 getline_opt_T options UNUSED)
1250{
1251 source_cookie_T *sp = (source_cookie_T *)cookie;
1252 char_u *p;
1253 char_u *line;
1254
1255 if (*sp->nextline == NUL)
1256 return NULL;
1257 p = vim_strchr(sp->nextline, '\n');
1258 if (p == NULL)
1259 {
1260 line = vim_strsave(sp->nextline);
1261 sp->nextline += STRLEN(sp->nextline);
1262 }
1263 else
1264 {
1265 line = vim_strnsave(sp->nextline, p - sp->nextline);
1266 sp->nextline = p + 1;
1267 }
1268 return line;
1269}
1270
1271/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 * Execute a function by "name".
1273 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001274 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 */
1276 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001277call_eval_func(
1278 char_u *name,
1279 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001280 ectx_T *ectx,
1281 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282{
Bram Moolenaared677f52020-08-12 16:38:10 +02001283 int called_emsg_before = called_emsg;
1284 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285
Bram Moolenaar4c137212021-04-19 16:48:48 +02001286 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001287 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001289 dictitem_T *v;
1290
1291 v = find_var(name, NULL, FALSE);
1292 if (v == NULL)
1293 {
1294 semsg(_(e_unknownfunc), name);
1295 return FAIL;
1296 }
1297 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1298 {
1299 semsg(_(e_unknownfunc), name);
1300 return FAIL;
1301 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001302 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001304 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305}
1306
1307/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001308 * When a function reference is used, fill a partial with the information
1309 * needed, especially when it is used as a closure.
1310 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001311 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001312fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1313{
1314 pt->pt_func = ufunc;
1315 pt->pt_refcount = 1;
1316
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001317 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001318 {
1319 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1320 + ectx->ec_dfunc_idx;
1321
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001322 // The closure may need to find arguments and local variables in the
1323 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001324 pt->pt_outer.out_stack = &ectx->ec_stack;
1325 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001326 if (ectx->ec_outer_ref != NULL)
1327 {
1328 // The current context already has a context, link to that one.
1329 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1330 if (ectx->ec_outer_ref->or_partial != NULL)
1331 {
1332 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1333 ++pt->pt_outer.out_up_partial->pt_refcount;
1334 }
1335 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001336
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001337 // If this function returns and the closure is still being used, we
1338 // need to make a copy of the context (arguments and local variables).
1339 // Store a reference to the partial so we can handle that.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001340 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1341 {
1342 vim_free(pt);
1343 return FAIL;
1344 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001345 // Extra variable keeps the count of closures created in the current
1346 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001347 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1348 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1349
1350 ((partial_T **)ectx->ec_funcrefs.ga_data)
1351 [ectx->ec_funcrefs.ga_len] = pt;
1352 ++pt->pt_refcount;
1353 ++ectx->ec_funcrefs.ga_len;
1354 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001355 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001356 return OK;
1357}
1358
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001359// used for v_instr of typval of VAR_INSTR
1360struct instr_S {
1361 ectx_T *instr_ectx;
1362 isn_T *instr_instr;
1363};
1364
Bram Moolenaar4c137212021-04-19 16:48:48 +02001365// used for substitute_instr
1366typedef struct subs_expr_S {
1367 ectx_T *subs_ectx;
1368 isn_T *subs_instr;
1369 int subs_status;
1370} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371
1372// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001373#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374
1375// Get pointer to item at the bottom of the stack, -1 is the bottom.
1376#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001377#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 +01001378
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001379// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001380#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 +01001381
Bram Moolenaar4c137212021-04-19 16:48:48 +02001382/*
1383 * Execute instructions in execution context "ectx".
1384 * Return OK or FAIL;
1385 */
1386 static int
1387exec_instructions(ectx_T *ectx)
1388{
1389 int breakcheck_count = 0;
1390 typval_T *tv;
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001391 int ret = FAIL;
1392 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001393
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001394 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001395 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001396
Bram Moolenaarff652882021-05-16 15:24:49 +02001397 // Only catch exceptions in this instruction list.
1398 ectx->ec_trylevel_at_start = trylevel;
1399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 for (;;)
1401 {
1402 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001403
Bram Moolenaar270d0382020-05-15 21:42:53 +02001404 if (++breakcheck_count >= 100)
1405 {
1406 line_breakcheck();
1407 breakcheck_count = 0;
1408 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001409 if (got_int)
1410 {
1411 // Turn CTRL-C into an exception.
1412 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001413 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001414 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001415 did_throw = TRUE;
1416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417
Bram Moolenaara26b9702020-04-18 19:53:28 +02001418 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1419 {
1420 // Turn an error message into an exception.
1421 did_emsg = FALSE;
1422 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001423 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001424 did_throw = TRUE;
1425 *msg_list = NULL;
1426 }
1427
Bram Moolenaar4c137212021-04-19 16:48:48 +02001428 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001430 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001431 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432
1433 // An exception jumps to the first catch, finally, or returns from
1434 // the current function.
1435 if (trystack->ga_len > 0)
1436 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001437 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 {
1439 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001440 ectx->ec_in_catch = TRUE;
1441 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 }
1443 else
1444 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001445 // Not inside try or need to return from current functions.
1446 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001447 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001448 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001449 tv = STACK_TV_BOT(0);
1450 tv->v_type = VAR_NUMBER;
1451 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001452 ++ectx->ec_stack.ga_len;
1453 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001455 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001456 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001457 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001458 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 goto done;
1460 }
1461
Bram Moolenaar4c137212021-04-19 16:48:48 +02001462 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001463 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 }
1465 continue;
1466 }
1467
Bram Moolenaar4c137212021-04-19 16:48:48 +02001468 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 switch (iptr->isn_type)
1470 {
1471 // execute Ex command line
1472 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001473 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001474 source_cookie_T cookie;
1475
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001476 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001477 // Pass getsourceline to get an error for a missing ":end"
1478 // command.
1479 CLEAR_FIELD(cookie);
1480 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1481 if (do_cmdline(iptr->isn_arg.string,
1482 getsourceline, &cookie,
1483 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1484 == FAIL
1485 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001486 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 break;
1489
Bram Moolenaar20677332021-06-06 17:02:53 +02001490 // execute Ex command line split at NL characters.
1491 case ISN_EXEC_SPLIT:
1492 {
1493 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001494 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001495
1496 SOURCING_LNUM = iptr->isn_lnum;
1497 CLEAR_FIELD(cookie);
1498 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1499 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001500 line = get_split_sourceline(0, &cookie, 0, 0);
1501 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001502 get_split_sourceline, &cookie,
1503 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1504 == FAIL
1505 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001506 {
1507 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001508 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001509 }
1510 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001511 }
1512 break;
1513
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001514 // Evaluate an expression with legacy syntax, push it onto the
1515 // stack.
1516 case ISN_LEGACY_EVAL:
1517 {
1518 char_u *arg = iptr->isn_arg.string;
1519 int res;
1520 int save_flags = cmdmod.cmod_flags;
1521
1522 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001523 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001524 tv = STACK_TV_BOT(0);
1525 init_tv(tv);
1526 cmdmod.cmod_flags |= CMOD_LEGACY;
1527 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1528 cmdmod.cmod_flags = save_flags;
1529 if (res == FAIL)
1530 goto on_error;
1531 ++ectx->ec_stack.ga_len;
1532 }
1533 break;
1534
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001535 // push typeval VAR_INSTR with instructions to be executed
1536 case ISN_INSTR:
1537 {
1538 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001539 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001540 tv = STACK_TV_BOT(0);
1541 tv->vval.v_instr = ALLOC_ONE(instr_T);
1542 if (tv->vval.v_instr == NULL)
1543 goto on_error;
1544 ++ectx->ec_stack.ga_len;
1545
1546 tv->v_type = VAR_INSTR;
1547 tv->vval.v_instr->instr_ectx = ectx;
1548 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1549 }
1550 break;
1551
Bram Moolenaar4c137212021-04-19 16:48:48 +02001552 // execute :substitute with an expression
1553 case ISN_SUBSTITUTE:
1554 {
1555 subs_T *subs = &iptr->isn_arg.subs;
1556 source_cookie_T cookie;
1557 struct subs_expr_S *save_instr = substitute_instr;
1558 struct subs_expr_S subs_instr;
1559 int res;
1560
1561 subs_instr.subs_ectx = ectx;
1562 subs_instr.subs_instr = subs->subs_instr;
1563 subs_instr.subs_status = OK;
1564 substitute_instr = &subs_instr;
1565
1566 SOURCING_LNUM = iptr->isn_lnum;
1567 // This is very much like ISN_EXEC
1568 CLEAR_FIELD(cookie);
1569 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1570 res = do_cmdline(subs->subs_cmd,
1571 getsourceline, &cookie,
1572 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1573 substitute_instr = save_instr;
1574
1575 if (res == FAIL || did_emsg
1576 || subs_instr.subs_status == FAIL)
1577 goto on_error;
1578 }
1579 break;
1580
1581 case ISN_FINISH:
1582 goto done;
1583
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001584 case ISN_REDIRSTART:
1585 // create a dummy entry for var_redir_str()
1586 if (alloc_redir_lval() == FAIL)
1587 goto on_error;
1588
1589 // The output is stored in growarray "redir_ga" until
1590 // redirection ends.
1591 init_redir_ga();
1592 redir_vname = 1;
1593 break;
1594
1595 case ISN_REDIREND:
1596 {
1597 char_u *res = get_clear_redir_ga();
1598
1599 // End redirection, put redirected text on the stack.
1600 clear_redir_lval();
1601 redir_vname = 0;
1602
1603 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1604 {
1605 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001606 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001607 }
1608 tv = STACK_TV_BOT(0);
1609 tv->v_type = VAR_STRING;
1610 tv->vval.v_string = res;
1611 ++ectx->ec_stack.ga_len;
1612 }
1613 break;
1614
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001615 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001616#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001617 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1618 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001619#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001620 break;
1621
1622 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001623#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001624 {
1625 exarg_T ea;
1626 int res;
1627
1628 CLEAR_FIELD(ea);
1629 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1630 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1631 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1632 --ectx->ec_stack.ga_len;
1633 tv = STACK_TV_BOT(0);
1634 res = cexpr_core(&ea, tv);
1635 clear_tv(tv);
1636 if (res == FAIL)
1637 goto on_error;
1638 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001639#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001640 break;
1641
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001642 // execute Ex command from pieces on the stack
1643 case ISN_EXECCONCAT:
1644 {
1645 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001646 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001647 int pass;
1648 int i;
1649 char_u *cmd = NULL;
1650 char_u *str;
1651
1652 for (pass = 1; pass <= 2; ++pass)
1653 {
1654 for (i = 0; i < count; ++i)
1655 {
1656 tv = STACK_TV_BOT(i - count);
1657 str = tv->vval.v_string;
1658 if (str != NULL && *str != NUL)
1659 {
1660 if (pass == 2)
1661 STRCPY(cmd + len, str);
1662 len += STRLEN(str);
1663 }
1664 if (pass == 2)
1665 clear_tv(tv);
1666 }
1667 if (pass == 1)
1668 {
1669 cmd = alloc(len + 1);
1670 if (cmd == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001671 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001672 len = 0;
1673 }
1674 }
1675
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001676 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001677 do_cmdline_cmd(cmd);
1678 vim_free(cmd);
1679 }
1680 break;
1681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 // execute :echo {string} ...
1683 case ISN_ECHO:
1684 {
1685 int count = iptr->isn_arg.echo.echo_count;
1686 int atstart = TRUE;
1687 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001688 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689
1690 for (idx = 0; idx < count; ++idx)
1691 {
1692 tv = STACK_TV_BOT(idx - count);
1693 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1694 &atstart, &needclr);
1695 clear_tv(tv);
1696 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001697 if (needclr)
1698 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001699 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 }
1701 break;
1702
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001703 // :execute {string} ...
1704 // :echomsg {string} ...
1705 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001706 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001707 case ISN_ECHOMSG:
1708 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001709 {
1710 int count = iptr->isn_arg.number;
1711 garray_T ga;
1712 char_u buf[NUMBUFLEN];
1713 char_u *p;
1714 int len;
1715 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001716 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001717
1718 ga_init2(&ga, 1, 80);
1719 for (idx = 0; idx < count; ++idx)
1720 {
1721 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001722 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001723 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001724 if (tv->v_type == VAR_CHANNEL
1725 || tv->v_type == VAR_JOB)
1726 {
1727 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02001728 semsg(_(e_using_invalid_value_as_string_str),
1729 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001730 break;
1731 }
1732 else
1733 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001734 }
1735 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001736 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001737
1738 len = (int)STRLEN(p);
1739 if (ga_grow(&ga, len + 2) == FAIL)
1740 failed = TRUE;
1741 else
1742 {
1743 if (ga.ga_len > 0)
1744 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1745 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1746 ga.ga_len += len;
1747 }
1748 clear_tv(tv);
1749 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001750 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001751 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001752 {
1753 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001754 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001755 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001756
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001757 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001758 {
1759 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001760 {
1761 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001762 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001763 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001764 {
1765 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001766 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001767 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001768 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001769 else
1770 {
1771 msg_sb_eol();
1772 if (iptr->isn_type == ISN_ECHOMSG)
1773 {
1774 msg_attr(ga.ga_data, echo_attr);
1775 out_flush();
1776 }
1777 else
1778 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001779 SOURCING_LNUM = iptr->isn_lnum;
1780 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001781 }
1782 }
1783 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001784 ga_clear(&ga);
1785 }
1786 break;
1787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 // load local variable or argument
1789 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001790 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001791 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001793 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 break;
1795
1796 // load v: variable
1797 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001798 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001799 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001801 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 break;
1803
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001804 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 case ISN_LOADSCRIPT:
1806 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001807 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 svar_T *sv;
1809
Bram Moolenaar4c137212021-04-19 16:48:48 +02001810 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001811 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001812 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001813 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001814 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001815 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001817 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 }
1819 break;
1820
1821 // load s: variable in old script
1822 case ISN_LOADS:
1823 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001824 hashtab_T *ht = &SCRIPT_VARS(
1825 iptr->isn_arg.loadstore.ls_sid);
1826 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 if (di == NULL)
1830 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001832 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001833 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 }
1835 else
1836 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001837 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001838 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001840 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 }
1842 }
1843 break;
1844
Bram Moolenaard3aac292020-04-19 14:32:17 +02001845 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001847 case ISN_LOADB:
1848 case ISN_LOADW:
1849 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001851 dictitem_T *di = NULL;
1852 hashtab_T *ht = NULL;
1853 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001854
Bram Moolenaard3aac292020-04-19 14:32:17 +02001855 switch (iptr->isn_type)
1856 {
1857 case ISN_LOADG:
1858 ht = get_globvar_ht();
1859 namespace = 'g';
1860 break;
1861 case ISN_LOADB:
1862 ht = &curbuf->b_vars->dv_hashtab;
1863 namespace = 'b';
1864 break;
1865 case ISN_LOADW:
1866 ht = &curwin->w_vars->dv_hashtab;
1867 namespace = 'w';
1868 break;
1869 case ISN_LOADT:
1870 ht = &curtab->tp_vars->dv_hashtab;
1871 namespace = 't';
1872 break;
1873 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001874 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001875 }
1876 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 if (di == NULL)
1879 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001880 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001881 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001882 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001883 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 }
1885 else
1886 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001887 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001888 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001890 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 }
1892 }
1893 break;
1894
Bram Moolenaar03290b82020-12-19 16:30:44 +01001895 // load autoload variable
1896 case ISN_LOADAUTO:
1897 {
1898 char_u *name = iptr->isn_arg.string;
1899
Bram Moolenaar4c137212021-04-19 16:48:48 +02001900 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001901 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001902 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001903 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001904 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001905 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001906 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001907 }
1908 break;
1909
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001910 // load g:/b:/w:/t: namespace
1911 case ISN_LOADGDICT:
1912 case ISN_LOADBDICT:
1913 case ISN_LOADWDICT:
1914 case ISN_LOADTDICT:
1915 {
1916 dict_T *d = NULL;
1917
1918 switch (iptr->isn_type)
1919 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001920 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1921 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1922 case ISN_LOADWDICT: d = curwin->w_vars; break;
1923 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001924 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001925 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001926 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001927 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001928 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001929 tv = STACK_TV_BOT(0);
1930 tv->v_type = VAR_DICT;
1931 tv->v_lock = 0;
1932 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001933 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001934 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001935 }
1936 break;
1937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 // load &option
1939 case ISN_LOADOPT:
1940 {
1941 typval_T optval;
1942 char_u *name = iptr->isn_arg.string;
1943
Bram Moolenaara8c17702020-04-01 21:17:24 +02001944 // This is not expected to fail, name is checked during
1945 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001946 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001947 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001948 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001949 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001951 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 }
1953 break;
1954
1955 // load $ENV
1956 case ISN_LOADENV:
1957 {
1958 typval_T optval;
1959 char_u *name = iptr->isn_arg.string;
1960
Bram Moolenaar4c137212021-04-19 16:48:48 +02001961 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001962 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001963 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001964 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001966 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 }
1968 break;
1969
1970 // load @register
1971 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001972 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001973 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 tv = STACK_TV_BOT(0);
1975 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001976 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001977 // This may result in NULL, which should be equivalent to an
1978 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 tv->vval.v_string = get_reg_contents(
1980 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001981 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 break;
1983
1984 // store local variable
1985 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001986 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 tv = STACK_TV_VAR(iptr->isn_arg.number);
1988 clear_tv(tv);
1989 *tv = *STACK_TV_BOT(0);
1990 break;
1991
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001992 // store s: variable in old script
1993 case ISN_STORES:
1994 {
1995 hashtab_T *ht = &SCRIPT_VARS(
1996 iptr->isn_arg.loadstore.ls_sid);
1997 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001998 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001999
Bram Moolenaar4c137212021-04-19 16:48:48 +02002000 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002001 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002002 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002003 else
2004 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002005 SOURCING_LNUM = iptr->isn_lnum;
2006 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002007 {
2008 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002009 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002010 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002011 clear_tv(&di->di_tv);
2012 di->di_tv = *STACK_TV_BOT(0);
2013 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002014 }
2015 break;
2016
2017 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 case ISN_STORESCRIPT:
2019 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002020 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2021 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022
Bram Moolenaar4c137212021-04-19 16:48:48 +02002023 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002024 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002025 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002026 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002027
2028 // "const" and "final" are checked at compile time, locking
2029 // the value needs to be checked here.
2030 SOURCING_LNUM = iptr->isn_lnum;
2031 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002032 {
2033 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002034 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002035 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 clear_tv(sv->sv_tv);
2038 *sv->sv_tv = *STACK_TV_BOT(0);
2039 }
2040 break;
2041
2042 // store option
2043 case ISN_STOREOPT:
2044 {
2045 long n = 0;
2046 char_u *s = NULL;
2047 char *msg;
2048
Bram Moolenaar4c137212021-04-19 16:48:48 +02002049 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 tv = STACK_TV_BOT(0);
2051 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002052 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002054 if (s == NULL)
2055 s = (char_u *)"";
2056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002058 // must be VAR_NUMBER, CHECKTYPE makes sure
2059 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
2061 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002062 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 if (msg != NULL)
2064 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002065 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002067 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 }
2070 break;
2071
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002072 // store $ENV
2073 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002074 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002075 tv = STACK_TV_BOT(0);
2076 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2077 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002078 break;
2079
2080 // store @r
2081 case ISN_STOREREG:
2082 {
2083 int reg = iptr->isn_arg.number;
2084
Bram Moolenaar4c137212021-04-19 16:48:48 +02002085 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002086 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002087 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002088 tv_get_string(tv), -1, FALSE);
2089 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002090 }
2091 break;
2092
2093 // store v: variable
2094 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002095 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002096 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2097 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002098 // should not happen, type is checked when compiling
2099 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002100 break;
2101
Bram Moolenaard3aac292020-04-19 14:32:17 +02002102 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002104 case ISN_STOREB:
2105 case ISN_STOREW:
2106 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002108 dictitem_T *di;
2109 hashtab_T *ht;
2110 char_u *name = iptr->isn_arg.string + 2;
2111
Bram Moolenaard3aac292020-04-19 14:32:17 +02002112 switch (iptr->isn_type)
2113 {
2114 case ISN_STOREG:
2115 ht = get_globvar_ht();
2116 break;
2117 case ISN_STOREB:
2118 ht = &curbuf->b_vars->dv_hashtab;
2119 break;
2120 case ISN_STOREW:
2121 ht = &curwin->w_vars->dv_hashtab;
2122 break;
2123 case ISN_STORET:
2124 ht = &curtab->tp_vars->dv_hashtab;
2125 break;
2126 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002127 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129
Bram Moolenaar4c137212021-04-19 16:48:48 +02002130 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002131 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002133 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 else
2135 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002136 SOURCING_LNUM = iptr->isn_lnum;
2137 if (var_check_permission(di, name) == FAIL)
2138 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 clear_tv(&di->di_tv);
2140 di->di_tv = *STACK_TV_BOT(0);
2141 }
2142 }
2143 break;
2144
Bram Moolenaar03290b82020-12-19 16:30:44 +01002145 // store an autoload variable
2146 case ISN_STOREAUTO:
2147 SOURCING_LNUM = iptr->isn_lnum;
2148 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2149 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002150 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002151 break;
2152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 // store number in local variable
2154 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002155 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 clear_tv(tv);
2157 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002158 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 break;
2160
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002161 // store value in list or dict variable
2162 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002163 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002164 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002165 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002166 typval_T *tv_dest = STACK_TV_BOT(-1);
2167 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002168
Bram Moolenaar752fc692021-01-04 21:57:11 +01002169 // Stack contains:
2170 // -3 value to be stored
2171 // -2 index
2172 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002173 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002174 SOURCING_LNUM = iptr->isn_lnum;
2175 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002176 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002177 dest_type = tv_dest->v_type;
2178 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002179 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002180 else if (dest_type == VAR_LIST
2181 && tv_idx->v_type != VAR_NUMBER)
2182 {
2183 emsg(_(e_number_exp));
2184 status = FAIL;
2185 }
2186 }
2187 else if (dest_type != tv_dest->v_type)
2188 {
2189 // just in case, should be OK
2190 semsg(_(e_expected_str_but_got_str),
2191 vartype_name(dest_type),
2192 vartype_name(tv_dest->v_type));
2193 status = FAIL;
2194 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002195
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002196 if (status == OK && dest_type == VAR_LIST)
2197 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002198 long lidx = (long)tv_idx->vval.v_number;
2199 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002200
2201 if (list == NULL)
2202 {
2203 emsg(_(e_list_not_set));
2204 goto on_error;
2205 }
2206 if (lidx < 0 && list->lv_len + lidx >= 0)
2207 // negative index is relative to the end
2208 lidx = list->lv_len + lidx;
2209 if (lidx < 0 || lidx > list->lv_len)
2210 {
2211 semsg(_(e_listidx), lidx);
2212 goto on_error;
2213 }
2214 if (lidx < list->lv_len)
2215 {
2216 listitem_T *li = list_find(list, lidx);
2217
2218 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002219 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002220 goto on_error;
2221 // overwrite existing list item
2222 clear_tv(&li->li_tv);
2223 li->li_tv = *tv;
2224 }
2225 else
2226 {
2227 if (error_if_locked(list->lv_lock,
2228 e_cannot_change_list))
2229 goto on_error;
2230 // append to list, only fails when out of memory
2231 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002232 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002233 clear_tv(tv);
2234 }
2235 }
2236 else if (status == OK && dest_type == VAR_DICT)
2237 {
2238 char_u *key = tv_idx->vval.v_string;
2239 dict_T *dict = tv_dest->vval.v_dict;
2240 dictitem_T *di;
2241
2242 SOURCING_LNUM = iptr->isn_lnum;
2243 if (dict == NULL)
2244 {
2245 emsg(_(e_dictionary_not_set));
2246 goto on_error;
2247 }
2248 if (key == NULL)
2249 key = (char_u *)"";
2250 di = dict_find(dict, key, -1);
2251 if (di != NULL)
2252 {
2253 if (error_if_locked(di->di_tv.v_lock,
2254 e_cannot_change_dict_item))
2255 goto on_error;
2256 // overwrite existing value
2257 clear_tv(&di->di_tv);
2258 di->di_tv = *tv;
2259 }
2260 else
2261 {
2262 if (error_if_locked(dict->dv_lock,
2263 e_cannot_change_dict))
2264 goto on_error;
2265 // add to dict, only fails when out of memory
2266 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002267 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002268 clear_tv(tv);
2269 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002270 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002271 else if (status == OK && dest_type == VAR_BLOB)
2272 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002273 long lidx = (long)tv_idx->vval.v_number;
2274 blob_T *blob = tv_dest->vval.v_blob;
2275 varnumber_T nr;
2276 int error = FALSE;
2277 int len;
2278
2279 if (blob == NULL)
2280 {
2281 emsg(_(e_blob_not_set));
2282 goto on_error;
2283 }
2284 len = blob_len(blob);
2285 if (lidx < 0 && len + lidx >= 0)
2286 // negative index is relative to the end
2287 lidx = len + lidx;
2288
2289 // Can add one byte at the end.
2290 if (lidx < 0 || lidx > len)
2291 {
2292 semsg(_(e_blobidx), lidx);
2293 goto on_error;
2294 }
2295 if (value_check_lock(blob->bv_lock,
2296 (char_u *)"blob", FALSE))
2297 goto on_error;
2298 nr = tv_get_number_chk(tv, &error);
2299 if (error)
2300 goto on_error;
2301 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002302 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002303 else
2304 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002305 status = FAIL;
2306 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002307 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002308
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002309 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002310 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002311 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002312 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002313 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002314 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002315 goto on_error;
2316 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002317 }
2318 break;
2319
Bram Moolenaar68452172021-04-12 21:21:02 +02002320 // store value in blob range
2321 case ISN_STORERANGE:
2322 {
2323 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2324 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2325 typval_T *tv_dest = STACK_TV_BOT(-1);
2326 int status = OK;
2327
2328 // Stack contains:
2329 // -4 value to be stored
2330 // -3 first index or "none"
2331 // -2 second index or "none"
2332 // -1 destination blob
2333 tv = STACK_TV_BOT(-4);
2334 if (tv_dest->v_type != VAR_BLOB)
2335 {
2336 status = FAIL;
2337 emsg(_(e_blob_required));
2338 }
2339 else
2340 {
2341 varnumber_T n1;
2342 varnumber_T n2;
2343 int error = FALSE;
2344
2345 n1 = tv_get_number_chk(tv_idx1, &error);
2346 if (error)
2347 status = FAIL;
2348 else
2349 {
2350 if (tv_idx2->v_type == VAR_SPECIAL
2351 && tv_idx2->vval.v_number == VVAL_NONE)
2352 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2353 else
2354 n2 = tv_get_number_chk(tv_idx2, &error);
2355 if (error)
2356 status = FAIL;
2357 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002358 {
2359 long bloblen = blob_len(tv_dest->vval.v_blob);
2360
2361 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002362 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002363 || check_blob_range(bloblen,
2364 n1, n2, FALSE) == FAIL)
2365 status = FAIL;
2366 else
2367 status = blob_set_range(
2368 tv_dest->vval.v_blob, n1, n2, tv);
2369 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002370 }
2371 }
2372
2373 clear_tv(tv_idx1);
2374 clear_tv(tv_idx2);
2375 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002376 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002377 clear_tv(tv);
2378
2379 if (status == FAIL)
2380 goto on_error;
2381 }
2382 break;
2383
Bram Moolenaar0186e582021-01-10 18:33:11 +01002384 // load or store variable or argument from outer scope
2385 case ISN_LOADOUTER:
2386 case ISN_STOREOUTER:
2387 {
2388 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002389 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2390 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002391
2392 while (depth > 1 && outer != NULL)
2393 {
2394 outer = outer->out_up;
2395 --depth;
2396 }
2397 if (outer == NULL)
2398 {
2399 SOURCING_LNUM = iptr->isn_lnum;
2400 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002401 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002402 }
2403 tv = ((typval_T *)outer->out_stack->ga_data)
2404 + outer->out_frame_idx + STACK_FRAME_SIZE
2405 + iptr->isn_arg.outer.outer_idx;
2406 if (iptr->isn_type == ISN_LOADOUTER)
2407 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002408 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002409 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002410 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002411 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002412 }
2413 else
2414 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002415 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002416 clear_tv(tv);
2417 *tv = *STACK_TV_BOT(0);
2418 }
2419 }
2420 break;
2421
Bram Moolenaar752fc692021-01-04 21:57:11 +01002422 // unlet item in list or dict variable
2423 case ISN_UNLETINDEX:
2424 {
2425 typval_T *tv_idx = STACK_TV_BOT(-2);
2426 typval_T *tv_dest = STACK_TV_BOT(-1);
2427 int status = OK;
2428
2429 // Stack contains:
2430 // -2 index
2431 // -1 dict or list
2432 if (tv_dest->v_type == VAR_DICT)
2433 {
2434 // unlet a dict item, index must be a string
2435 if (tv_idx->v_type != VAR_STRING)
2436 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002437 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002438 semsg(_(e_expected_str_but_got_str),
2439 vartype_name(VAR_STRING),
2440 vartype_name(tv_idx->v_type));
2441 status = FAIL;
2442 }
2443 else
2444 {
2445 dict_T *d = tv_dest->vval.v_dict;
2446 char_u *key = tv_idx->vval.v_string;
2447 dictitem_T *di = NULL;
2448
2449 if (key == NULL)
2450 key = (char_u *)"";
2451 if (d != NULL)
2452 di = dict_find(d, key, (int)STRLEN(key));
2453 if (di == NULL)
2454 {
2455 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002456 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002457 semsg(_(e_dictkey), key);
2458 status = FAIL;
2459 }
2460 else
2461 {
2462 // TODO: check for dict or item locked
2463 dictitem_remove(d, di);
2464 }
2465 }
2466 }
2467 else if (tv_dest->v_type == VAR_LIST)
2468 {
2469 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002470 SOURCING_LNUM = iptr->isn_lnum;
2471 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002472 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002473 status = FAIL;
2474 }
2475 else
2476 {
2477 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002478 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002479 listitem_T *li = NULL;
2480
2481 li = list_find(l, n);
2482 if (li == NULL)
2483 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002485 semsg(_(e_listidx), n);
2486 status = FAIL;
2487 }
2488 else
2489 // TODO: check for list or item locked
2490 listitem_remove(l, li);
2491 }
2492 }
2493 else
2494 {
2495 status = FAIL;
2496 semsg(_(e_cannot_index_str),
2497 vartype_name(tv_dest->v_type));
2498 }
2499
2500 clear_tv(tv_idx);
2501 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002502 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002503 if (status == FAIL)
2504 goto on_error;
2505 }
2506 break;
2507
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002508 // unlet range of items in list variable
2509 case ISN_UNLETRANGE:
2510 {
2511 // Stack contains:
2512 // -3 index1
2513 // -2 index2
2514 // -1 dict or list
2515 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2516 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2517 typval_T *tv_dest = STACK_TV_BOT(-1);
2518 int status = OK;
2519
2520 if (tv_dest->v_type == VAR_LIST)
2521 {
2522 // indexes must be a number
2523 SOURCING_LNUM = iptr->isn_lnum;
2524 if (check_for_number(tv_idx1) == FAIL
2525 || check_for_number(tv_idx2) == FAIL)
2526 {
2527 status = FAIL;
2528 }
2529 else
2530 {
2531 list_T *l = tv_dest->vval.v_list;
2532 long n1 = (long)tv_idx1->vval.v_number;
2533 long n2 = (long)tv_idx2->vval.v_number;
2534 listitem_T *li;
2535
2536 li = list_find_index(l, &n1);
2537 if (li == NULL
2538 || list_unlet_range(l, li, NULL, n1,
2539 TRUE, n2) == FAIL)
2540 status = FAIL;
2541 }
2542 }
2543 else
2544 {
2545 status = FAIL;
2546 SOURCING_LNUM = iptr->isn_lnum;
2547 semsg(_(e_cannot_index_str),
2548 vartype_name(tv_dest->v_type));
2549 }
2550
2551 clear_tv(tv_idx1);
2552 clear_tv(tv_idx2);
2553 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002554 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002555 if (status == FAIL)
2556 goto on_error;
2557 }
2558 break;
2559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 // push constant
2561 case ISN_PUSHNR:
2562 case ISN_PUSHBOOL:
2563 case ISN_PUSHSPEC:
2564 case ISN_PUSHF:
2565 case ISN_PUSHS:
2566 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002567 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002568 case ISN_PUSHCHANNEL:
2569 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002570 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002571 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002573 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002574 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 switch (iptr->isn_type)
2576 {
2577 case ISN_PUSHNR:
2578 tv->v_type = VAR_NUMBER;
2579 tv->vval.v_number = iptr->isn_arg.number;
2580 break;
2581 case ISN_PUSHBOOL:
2582 tv->v_type = VAR_BOOL;
2583 tv->vval.v_number = iptr->isn_arg.number;
2584 break;
2585 case ISN_PUSHSPEC:
2586 tv->v_type = VAR_SPECIAL;
2587 tv->vval.v_number = iptr->isn_arg.number;
2588 break;
2589#ifdef FEAT_FLOAT
2590 case ISN_PUSHF:
2591 tv->v_type = VAR_FLOAT;
2592 tv->vval.v_float = iptr->isn_arg.fnumber;
2593 break;
2594#endif
2595 case ISN_PUSHBLOB:
2596 blob_copy(iptr->isn_arg.blob, tv);
2597 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002598 case ISN_PUSHFUNC:
2599 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002600 if (iptr->isn_arg.string == NULL)
2601 tv->vval.v_string = NULL;
2602 else
2603 tv->vval.v_string =
2604 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002605 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002606 case ISN_PUSHCHANNEL:
2607#ifdef FEAT_JOB_CHANNEL
2608 tv->v_type = VAR_CHANNEL;
2609 tv->vval.v_channel = iptr->isn_arg.channel;
2610 if (tv->vval.v_channel != NULL)
2611 ++tv->vval.v_channel->ch_refcount;
2612#endif
2613 break;
2614 case ISN_PUSHJOB:
2615#ifdef FEAT_JOB_CHANNEL
2616 tv->v_type = VAR_JOB;
2617 tv->vval.v_job = iptr->isn_arg.job;
2618 if (tv->vval.v_job != NULL)
2619 ++tv->vval.v_job->jv_refcount;
2620#endif
2621 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 default:
2623 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002624 tv->vval.v_string = vim_strsave(
2625 iptr->isn_arg.string == NULL
2626 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 }
2628 break;
2629
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002630 case ISN_UNLET:
2631 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2632 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002633 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002634 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002635 case ISN_UNLETENV:
2636 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2637 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002638
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002639 case ISN_LOCKCONST:
2640 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2641 break;
2642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 // create a list from items on the stack; uses a single allocation
2644 // for the list header and the items
2645 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002646 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002647 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 break;
2649
2650 // create a dict from items on the stack
2651 case ISN_NEWDICT:
2652 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002653 int count = iptr->isn_arg.number;
2654 dict_T *dict = dict_alloc();
2655 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002656 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002657 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658
2659 if (dict == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002660 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 for (idx = 0; idx < count; ++idx)
2662 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002663 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002665 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002666 key = tv->vval.v_string == NULL
2667 ? (char_u *)"" : tv->vval.v_string;
2668 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002669 if (item != NULL)
2670 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002671 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002672 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002673 dict_unref(dict);
2674 goto on_error;
2675 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002676 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 clear_tv(tv);
2678 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002679 {
2680 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002681 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002682 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2684 item->di_tv.v_lock = 0;
2685 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002686 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002687 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002688 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002689 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02002690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 }
2692
2693 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002694 ectx->ec_stack.ga_len -= 2 * count - 1;
2695 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002696 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002698 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 tv = STACK_TV_BOT(-1);
2700 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002701 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 tv->vval.v_dict = dict;
2703 ++dict->dv_refcount;
2704 }
2705 break;
2706
2707 // call a :def function
2708 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002709 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002710 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2711 NULL,
2712 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002713 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002714 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 break;
2716
2717 // call a builtin function
2718 case ISN_BCALL:
2719 SOURCING_LNUM = iptr->isn_lnum;
2720 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2721 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002722 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002723 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 break;
2725
2726 // call a funcref or partial
2727 case ISN_PCALL:
2728 {
2729 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2730 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002731 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732
2733 SOURCING_LNUM = iptr->isn_lnum;
2734 if (pfunc->cpf_top)
2735 {
2736 // funcref is above the arguments
2737 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2738 }
2739 else
2740 {
2741 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002742 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002743 partial_tv = *STACK_TV_BOT(0);
2744 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002746 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002747 if (tv == &partial_tv)
2748 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002750 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 }
2752 break;
2753
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002754 case ISN_PCALL_END:
2755 // PCALL finished, arguments have been consumed and replaced by
2756 // the return value. Now clear the funcref from the stack,
2757 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002758 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002759 clear_tv(STACK_TV_BOT(-1));
2760 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2761 break;
2762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 // call a user defined function or funcref/partial
2764 case ISN_UCALL:
2765 {
2766 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2767
2768 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002769 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002770 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002771 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 }
2773 break;
2774
2775 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002776 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002777 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002778 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002779 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002780 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002781 tv->v_type = VAR_NUMBER;
2782 tv->vval.v_number = 0;
2783 tv->v_lock = 0;
2784 // FALLTHROUGH
2785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 case ISN_RETURN:
2787 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002788 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002789 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002790
2791 if (trystack->ga_len > 0)
2792 trycmd = ((trycmd_T *)trystack->ga_data)
2793 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002794 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002795 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002797 // jump to ":finally" or ":endtry"
2798 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002799 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002800 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002801 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 trycmd->tcd_return = TRUE;
2803 }
2804 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002805 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 }
2807 break;
2808
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002809 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 case ISN_FUNCREF:
2811 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002812 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2813 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2814 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002817 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002818 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002819 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002820 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002821 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002822 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002823 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002824 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002825 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002827 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 tv->vval.v_partial = pt;
2829 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002830 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 }
2832 break;
2833
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002834 // Create a global function from a lambda.
2835 case ISN_NEWFUNC:
2836 {
2837 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2838
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002839 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002840 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002841 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002842 }
2843 break;
2844
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002845 // List functions
2846 case ISN_DEF:
2847 if (iptr->isn_arg.string == NULL)
2848 list_functions(NULL);
2849 else
2850 {
2851 exarg_T ea;
2852
2853 CLEAR_FIELD(ea);
2854 ea.cmd = ea.arg = iptr->isn_arg.string;
2855 define_function(&ea, NULL);
2856 }
2857 break;
2858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 // jump if a condition is met
2860 case ISN_JUMP:
2861 {
2862 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002863 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 int jump = TRUE;
2865
2866 if (when != JUMP_ALWAYS)
2867 {
2868 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002869 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002870 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002871 || when == JUMP_IF_COND_TRUE)
2872 {
2873 SOURCING_LNUM = iptr->isn_lnum;
2874 jump = tv_get_bool_chk(tv, &error);
2875 if (error)
2876 goto on_error;
2877 }
2878 else
2879 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002881 || when == JUMP_AND_KEEP_IF_FALSE
2882 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002884 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 {
2886 // drop the value from the stack
2887 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002888 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 }
2890 }
2891 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002892 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 }
2894 break;
2895
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002896 // Jump if an argument with a default value was already set and not
2897 // v:none.
2898 case ISN_JUMP_IF_ARG_SET:
2899 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2900 if (tv->v_type != VAR_UNKNOWN
2901 && !(tv->v_type == VAR_SPECIAL
2902 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002903 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002904 break;
2905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 // top of a for loop
2907 case ISN_FOR:
2908 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002909 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 typval_T *idxtv =
2911 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2912
Bram Moolenaar4c137212021-04-19 16:48:48 +02002913 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002914 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002915 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002916 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002917 list_T *list = ltv->vval.v_list;
2918
2919 // push the next item from the list
2920 ++idxtv->vval.v_number;
2921 if (list == NULL
2922 || idxtv->vval.v_number >= list->lv_len)
2923 {
2924 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002925 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2926 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002927 }
2928 else if (list->lv_first == &range_list_item)
2929 {
2930 // non-materialized range() list
2931 tv = STACK_TV_BOT(0);
2932 tv->v_type = VAR_NUMBER;
2933 tv->v_lock = 0;
2934 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002937 }
2938 else
2939 {
2940 listitem_T *li = list_find(list,
2941 idxtv->vval.v_number);
2942
2943 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002944 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002945 }
2946 }
2947 else if (ltv->v_type == VAR_STRING)
2948 {
2949 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002950
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002951 // The index is for the last byte of the previous
2952 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002953 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002954 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002955 {
2956 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002957 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2958 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002959 }
2960 else
2961 {
2962 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2963
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002964 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002965 tv = STACK_TV_BOT(0);
2966 tv->v_type = VAR_STRING;
2967 tv->vval.v_string = vim_strnsave(
2968 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002969 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002970 idxtv->vval.v_number += clen - 1;
2971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002973 else if (ltv->v_type == VAR_BLOB)
2974 {
2975 blob_T *blob = ltv->vval.v_blob;
2976
2977 // When we get here the first time make a copy of the
2978 // blob, so that the iteration still works when it is
2979 // changed.
2980 if (idxtv->vval.v_number == -1 && blob != NULL)
2981 {
2982 blob_copy(blob, ltv);
2983 blob_unref(blob);
2984 blob = ltv->vval.v_blob;
2985 }
2986
2987 // The index is for the previous byte.
2988 ++idxtv->vval.v_number;
2989 if (blob == NULL
2990 || idxtv->vval.v_number >= blob_len(blob))
2991 {
2992 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002993 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2994 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002995 }
2996 else
2997 {
2998 // Push the next byte from the blob.
2999 tv = STACK_TV_BOT(0);
3000 tv->v_type = VAR_NUMBER;
3001 tv->vval.v_number = blob_get(blob,
3002 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003003 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003004 }
3005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 else
3007 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003008 semsg(_(e_for_loop_on_str_not_supported),
3009 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003010 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 }
3012 }
3013 break;
3014
3015 // start of ":try" block
3016 case ISN_TRY:
3017 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003018 trycmd_T *trycmd = NULL;
3019
Bram Moolenaar4c137212021-04-19 16:48:48 +02003020 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003021 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003022 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3023 + ectx->ec_trystack.ga_len;
3024 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003026 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3028 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003029 trycmd->tcd_catch_idx =
3030 iptr->isn_arg.try.try_ref->try_catch;
3031 trycmd->tcd_finally_idx =
3032 iptr->isn_arg.try.try_ref->try_finally;
3033 trycmd->tcd_endtry_idx =
3034 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 }
3036 break;
3037
3038 case ISN_PUSHEXC:
3039 if (current_exception == NULL)
3040 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003041 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003043 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003045 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003046 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003048 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003050 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 tv->vval.v_string = vim_strsave(
3052 (char_u *)current_exception->value);
3053 break;
3054
3055 case ISN_CATCH:
3056 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003057 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058
Bram Moolenaar4c137212021-04-19 16:48:48 +02003059 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 if (trystack->ga_len > 0)
3061 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003062 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 + trystack->ga_len - 1;
3064 trycmd->tcd_caught = TRUE;
3065 }
3066 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003067 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 catch_exception(current_exception);
3069 }
3070 break;
3071
Bram Moolenaarc150c092021-02-13 15:02:46 +01003072 case ISN_TRYCONT:
3073 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003074 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003075 trycont_T *trycont = &iptr->isn_arg.trycont;
3076 int i;
3077 trycmd_T *trycmd;
3078 int iidx = trycont->tct_where;
3079
3080 if (trystack->ga_len < trycont->tct_levels)
3081 {
3082 siemsg("TRYCONT: expected %d levels, found %d",
3083 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003084 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003085 }
3086 // Make :endtry jump to any outer try block and the last
3087 // :endtry inside the loop to the loop start.
3088 for (i = trycont->tct_levels; i > 0; --i)
3089 {
3090 trycmd = ((trycmd_T *)trystack->ga_data)
3091 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003092 // Add one to tcd_cont to be able to jump to
3093 // instruction with index zero.
3094 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003095 iidx = trycmd->tcd_finally_idx == 0
3096 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003097 }
3098 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003099 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003100 }
3101 break;
3102
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003103 case ISN_FINALLY:
3104 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003105 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003106 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3107 + trystack->ga_len - 1;
3108
3109 // Reset the index to avoid a return statement jumps here
3110 // again.
3111 trycmd->tcd_finally_idx = 0;
3112 break;
3113 }
3114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 // end of ":try" block
3116 case ISN_ENDTRY:
3117 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003118 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119
3120 if (trystack->ga_len > 0)
3121 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003122 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 --trystack->ga_len;
3125 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003126 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 trycmd = ((trycmd_T *)trystack->ga_data)
3128 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003129 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 {
3131 // discard the exception
3132 if (caught_stack == current_exception)
3133 caught_stack = caught_stack->caught;
3134 discard_current_exception();
3135 }
3136
3137 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003138 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003139
Bram Moolenaar4c137212021-04-19 16:48:48 +02003140 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003141 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003142 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003143 clear_tv(STACK_TV_BOT(0));
3144 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003145 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003146 // handling :continue: jump to outer try block or
3147 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003148 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 }
3150 }
3151 break;
3152
3153 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003154 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003155 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003156
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003157 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3158 {
3159 // throwing an exception while using "silent!" causes
3160 // the function to abort but not display an error.
3161 tv = STACK_TV_BOT(-1);
3162 clear_tv(tv);
3163 tv->v_type = VAR_NUMBER;
3164 tv->vval.v_number = 0;
3165 goto done;
3166 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003167 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003168 tv = STACK_TV_BOT(0);
3169 if (tv->vval.v_string == NULL
3170 || *skipwhite(tv->vval.v_string) == NUL)
3171 {
3172 vim_free(tv->vval.v_string);
3173 SOURCING_LNUM = iptr->isn_lnum;
3174 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003175 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003176 }
3177
3178 // Inside a "catch" we need to first discard the caught
3179 // exception.
3180 if (trystack->ga_len > 0)
3181 {
3182 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3183 + trystack->ga_len - 1;
3184 if (trycmd->tcd_caught && current_exception != NULL)
3185 {
3186 // discard the exception
3187 if (caught_stack == current_exception)
3188 caught_stack = caught_stack->caught;
3189 discard_current_exception();
3190 trycmd->tcd_caught = FALSE;
3191 }
3192 }
3193
3194 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3195 == FAIL)
3196 {
3197 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003198 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003199 }
3200 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 break;
3203
3204 // compare with special values
3205 case ISN_COMPAREBOOL:
3206 case ISN_COMPARESPECIAL:
3207 {
3208 typval_T *tv1 = STACK_TV_BOT(-2);
3209 typval_T *tv2 = STACK_TV_BOT(-1);
3210 varnumber_T arg1 = tv1->vval.v_number;
3211 varnumber_T arg2 = tv2->vval.v_number;
3212 int res;
3213
3214 switch (iptr->isn_arg.op.op_type)
3215 {
3216 case EXPR_EQUAL: res = arg1 == arg2; break;
3217 case EXPR_NEQUAL: res = arg1 != arg2; break;
3218 default: res = 0; break;
3219 }
3220
Bram Moolenaar4c137212021-04-19 16:48:48 +02003221 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 tv1->v_type = VAR_BOOL;
3223 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3224 }
3225 break;
3226
3227 // Operation with two number arguments
3228 case ISN_OPNR:
3229 case ISN_COMPARENR:
3230 {
3231 typval_T *tv1 = STACK_TV_BOT(-2);
3232 typval_T *tv2 = STACK_TV_BOT(-1);
3233 varnumber_T arg1 = tv1->vval.v_number;
3234 varnumber_T arg2 = tv2->vval.v_number;
3235 varnumber_T res;
3236
3237 switch (iptr->isn_arg.op.op_type)
3238 {
3239 case EXPR_MULT: res = arg1 * arg2; break;
3240 case EXPR_DIV: res = arg1 / arg2; break;
3241 case EXPR_REM: res = arg1 % arg2; break;
3242 case EXPR_SUB: res = arg1 - arg2; break;
3243 case EXPR_ADD: res = arg1 + arg2; break;
3244
3245 case EXPR_EQUAL: res = arg1 == arg2; break;
3246 case EXPR_NEQUAL: res = arg1 != arg2; break;
3247 case EXPR_GREATER: res = arg1 > arg2; break;
3248 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3249 case EXPR_SMALLER: res = arg1 < arg2; break;
3250 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3251 default: res = 0; break;
3252 }
3253
Bram Moolenaar4c137212021-04-19 16:48:48 +02003254 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 if (iptr->isn_type == ISN_COMPARENR)
3256 {
3257 tv1->v_type = VAR_BOOL;
3258 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3259 }
3260 else
3261 tv1->vval.v_number = res;
3262 }
3263 break;
3264
3265 // Computation with two float arguments
3266 case ISN_OPFLOAT:
3267 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003268#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 {
3270 typval_T *tv1 = STACK_TV_BOT(-2);
3271 typval_T *tv2 = STACK_TV_BOT(-1);
3272 float_T arg1 = tv1->vval.v_float;
3273 float_T arg2 = tv2->vval.v_float;
3274 float_T res = 0;
3275 int cmp = FALSE;
3276
3277 switch (iptr->isn_arg.op.op_type)
3278 {
3279 case EXPR_MULT: res = arg1 * arg2; break;
3280 case EXPR_DIV: res = arg1 / arg2; break;
3281 case EXPR_SUB: res = arg1 - arg2; break;
3282 case EXPR_ADD: res = arg1 + arg2; break;
3283
3284 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3285 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3286 case EXPR_GREATER: cmp = arg1 > arg2; break;
3287 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3288 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3289 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3290 default: cmp = 0; break;
3291 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003292 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 if (iptr->isn_type == ISN_COMPAREFLOAT)
3294 {
3295 tv1->v_type = VAR_BOOL;
3296 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3297 }
3298 else
3299 tv1->vval.v_float = res;
3300 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003301#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 break;
3303
3304 case ISN_COMPARELIST:
3305 {
3306 typval_T *tv1 = STACK_TV_BOT(-2);
3307 typval_T *tv2 = STACK_TV_BOT(-1);
3308 list_T *arg1 = tv1->vval.v_list;
3309 list_T *arg2 = tv2->vval.v_list;
3310 int cmp = FALSE;
3311 int ic = iptr->isn_arg.op.op_ic;
3312
3313 switch (iptr->isn_arg.op.op_type)
3314 {
3315 case EXPR_EQUAL: cmp =
3316 list_equal(arg1, arg2, ic, FALSE); break;
3317 case EXPR_NEQUAL: cmp =
3318 !list_equal(arg1, arg2, ic, FALSE); break;
3319 case EXPR_IS: cmp = arg1 == arg2; break;
3320 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3321 default: cmp = 0; break;
3322 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003323 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 clear_tv(tv1);
3325 clear_tv(tv2);
3326 tv1->v_type = VAR_BOOL;
3327 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3328 }
3329 break;
3330
3331 case ISN_COMPAREBLOB:
3332 {
3333 typval_T *tv1 = STACK_TV_BOT(-2);
3334 typval_T *tv2 = STACK_TV_BOT(-1);
3335 blob_T *arg1 = tv1->vval.v_blob;
3336 blob_T *arg2 = tv2->vval.v_blob;
3337 int cmp = FALSE;
3338
3339 switch (iptr->isn_arg.op.op_type)
3340 {
3341 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3342 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3343 case EXPR_IS: cmp = arg1 == arg2; break;
3344 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3345 default: cmp = 0; break;
3346 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003347 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 clear_tv(tv1);
3349 clear_tv(tv2);
3350 tv1->v_type = VAR_BOOL;
3351 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3352 }
3353 break;
3354
3355 // TODO: handle separately
3356 case ISN_COMPARESTRING:
3357 case ISN_COMPAREDICT:
3358 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 case ISN_COMPAREANY:
3360 {
3361 typval_T *tv1 = STACK_TV_BOT(-2);
3362 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003363 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 int ic = iptr->isn_arg.op.op_ic;
3365
Bram Moolenaareb26f432020-09-14 16:50:05 +02003366 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003367 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003369 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 }
3371 break;
3372
3373 case ISN_ADDLIST:
3374 case ISN_ADDBLOB:
3375 {
3376 typval_T *tv1 = STACK_TV_BOT(-2);
3377 typval_T *tv2 = STACK_TV_BOT(-1);
3378
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003379 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 if (iptr->isn_type == ISN_ADDLIST)
3381 eval_addlist(tv1, tv2);
3382 else
3383 eval_addblob(tv1, tv2);
3384 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003385 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 }
3387 break;
3388
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003389 case ISN_LISTAPPEND:
3390 {
3391 typval_T *tv1 = STACK_TV_BOT(-2);
3392 typval_T *tv2 = STACK_TV_BOT(-1);
3393 list_T *l = tv1->vval.v_list;
3394
3395 // add an item to a list
3396 if (l == NULL)
3397 {
3398 SOURCING_LNUM = iptr->isn_lnum;
3399 emsg(_(e_cannot_add_to_null_list));
3400 goto on_error;
3401 }
3402 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003403 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003404 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003405 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003406 }
3407 break;
3408
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003409 case ISN_BLOBAPPEND:
3410 {
3411 typval_T *tv1 = STACK_TV_BOT(-2);
3412 typval_T *tv2 = STACK_TV_BOT(-1);
3413 blob_T *b = tv1->vval.v_blob;
3414 int error = FALSE;
3415 varnumber_T n;
3416
3417 // add a number to a blob
3418 if (b == NULL)
3419 {
3420 SOURCING_LNUM = iptr->isn_lnum;
3421 emsg(_(e_cannot_add_to_null_blob));
3422 goto on_error;
3423 }
3424 n = tv_get_number_chk(tv2, &error);
3425 if (error)
3426 goto on_error;
3427 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003428 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003429 }
3430 break;
3431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 // Computation with two arguments of unknown type
3433 case ISN_OPANY:
3434 {
3435 typval_T *tv1 = STACK_TV_BOT(-2);
3436 typval_T *tv2 = STACK_TV_BOT(-1);
3437 varnumber_T n1, n2;
3438#ifdef FEAT_FLOAT
3439 float_T f1 = 0, f2 = 0;
3440#endif
3441 int error = FALSE;
3442
3443 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3444 {
3445 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3446 {
3447 eval_addlist(tv1, tv2);
3448 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003449 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 break;
3451 }
3452 else if (tv1->v_type == VAR_BLOB
3453 && tv2->v_type == VAR_BLOB)
3454 {
3455 eval_addblob(tv1, tv2);
3456 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003457 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 break;
3459 }
3460 }
3461#ifdef FEAT_FLOAT
3462 if (tv1->v_type == VAR_FLOAT)
3463 {
3464 f1 = tv1->vval.v_float;
3465 n1 = 0;
3466 }
3467 else
3468#endif
3469 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003470 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 n1 = tv_get_number_chk(tv1, &error);
3472 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003473 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474#ifdef FEAT_FLOAT
3475 if (tv2->v_type == VAR_FLOAT)
3476 f1 = n1;
3477#endif
3478 }
3479#ifdef FEAT_FLOAT
3480 if (tv2->v_type == VAR_FLOAT)
3481 {
3482 f2 = tv2->vval.v_float;
3483 n2 = 0;
3484 }
3485 else
3486#endif
3487 {
3488 n2 = tv_get_number_chk(tv2, &error);
3489 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003490 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491#ifdef FEAT_FLOAT
3492 if (tv1->v_type == VAR_FLOAT)
3493 f2 = n2;
3494#endif
3495 }
3496#ifdef FEAT_FLOAT
3497 // if there is a float on either side the result is a float
3498 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3499 {
3500 switch (iptr->isn_arg.op.op_type)
3501 {
3502 case EXPR_MULT: f1 = f1 * f2; break;
3503 case EXPR_DIV: f1 = f1 / f2; break;
3504 case EXPR_SUB: f1 = f1 - f2; break;
3505 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003506 default: SOURCING_LNUM = iptr->isn_lnum;
3507 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003508 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 }
3510 clear_tv(tv1);
3511 clear_tv(tv2);
3512 tv1->v_type = VAR_FLOAT;
3513 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003514 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 }
3516 else
3517#endif
3518 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003519 int failed = FALSE;
3520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 switch (iptr->isn_arg.op.op_type)
3522 {
3523 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003524 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3525 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003526 goto on_error;
3527 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 case EXPR_SUB: n1 = n1 - n2; break;
3529 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003530 default: n1 = num_modulus(n1, n2, &failed);
3531 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003532 goto on_error;
3533 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 }
3535 clear_tv(tv1);
3536 clear_tv(tv2);
3537 tv1->v_type = VAR_NUMBER;
3538 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003539 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 }
3541 }
3542 break;
3543
3544 case ISN_CONCAT:
3545 {
3546 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3547 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3548 char_u *res;
3549
3550 res = concat_str(str1, str2);
3551 clear_tv(STACK_TV_BOT(-2));
3552 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003553 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 STACK_TV_BOT(-1)->vval.v_string = res;
3555 }
3556 break;
3557
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003558 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003559 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003560 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003561 int is_slice = iptr->isn_type == ISN_STRSLICE;
3562 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003563 char_u *res;
3564
3565 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003566 // string slice: string is at stack-3, first index at
3567 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003568 if (is_slice)
3569 {
3570 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003571 n1 = tv->vval.v_number;
3572 }
3573
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003574 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003575 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003576
Bram Moolenaar4c137212021-04-19 16:48:48 +02003577 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003578 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003579 if (is_slice)
3580 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003581 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003582 else
3583 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003584 // single character (including composing characters).
3585 // If the index is too big or negative the result is
3586 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003587 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003588 vim_free(tv->vval.v_string);
3589 tv->vval.v_string = res;
3590 }
3591 break;
3592
3593 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003594 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003595 case ISN_BLOBINDEX:
3596 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003598 int is_slice = iptr->isn_type == ISN_LISTSLICE
3599 || iptr->isn_type == ISN_BLOBSLICE;
3600 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3601 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003602 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003603 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604
3605 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003606 // list slice: list is at stack-3, indexes at stack-2 and
3607 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003608 // Same for blob.
3609 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
3611 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003612 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003614
3615 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 {
Bram Moolenaared591872020-08-15 22:14:53 +02003617 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003618 n1 = tv->vval.v_number;
3619 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 }
Bram Moolenaared591872020-08-15 22:14:53 +02003621
Bram Moolenaar4c137212021-04-19 16:48:48 +02003622 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003623 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003624 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003625 if (is_blob)
3626 {
3627 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3628 n1, n2, FALSE, tv) == FAIL)
3629 goto on_error;
3630 }
3631 else
3632 {
3633 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3634 n1, n2, FALSE, tv, TRUE) == FAIL)
3635 goto on_error;
3636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 }
3638 break;
3639
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003640 case ISN_ANYINDEX:
3641 case ISN_ANYSLICE:
3642 {
3643 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3644 typval_T *var1, *var2;
3645 int res;
3646
3647 // index: composite is at stack-2, index at stack-1
3648 // slice: composite is at stack-3, indexes at stack-2 and
3649 // stack-1
3650 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003651 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003652 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3653 goto on_error;
3654 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3655 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003656 res = eval_index_inner(tv, is_slice, var1, var2,
3657 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003658 clear_tv(var1);
3659 if (is_slice)
3660 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003661 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003662 if (res == FAIL)
3663 goto on_error;
3664 }
3665 break;
3666
Bram Moolenaar9af78762020-06-16 11:34:42 +02003667 case ISN_SLICE:
3668 {
3669 list_T *list;
3670 int count = iptr->isn_arg.number;
3671
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003672 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003673 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003674 list = tv->vval.v_list;
3675
3676 // no error for short list, expect it to be checked earlier
3677 if (list != NULL && list->lv_len >= count)
3678 {
3679 list_T *newlist = list_slice(list,
3680 count, list->lv_len - 1);
3681
3682 if (newlist != NULL)
3683 {
3684 list_unref(list);
3685 tv->vval.v_list = newlist;
3686 ++newlist->lv_refcount;
3687 }
3688 }
3689 }
3690 break;
3691
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003692 case ISN_GETITEM:
3693 {
3694 listitem_T *li;
3695 int index = iptr->isn_arg.number;
3696
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003697 // Get list item: list is at stack-1, push item.
3698 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003699 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003700 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003701
Bram Moolenaar4c137212021-04-19 16:48:48 +02003702 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003703 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003704 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003705 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003706
3707 // Useful when used in unpack assignment. Reset at
3708 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003709 ectx->ec_where.wt_index = index + 1;
3710 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003711 }
3712 break;
3713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 case ISN_MEMBER:
3715 {
3716 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003717 char_u *key;
3718 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003719 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003720
3721 // dict member: dict is at stack-2, key at stack-1
3722 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003723 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003724 dict = tv->vval.v_dict;
3725
3726 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003727 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003728 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003729 if (key == NULL)
3730 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003731
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003732 if ((di = dict_find(dict, key, -1)) == NULL)
3733 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003734 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003735 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003736
3737 // If :silent! is used we will continue, make sure the
3738 // stack contents makes sense.
3739 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003740 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003741 tv = STACK_TV_BOT(-1);
3742 clear_tv(tv);
3743 tv->v_type = VAR_NUMBER;
3744 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003745 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003746 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003747 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003748 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003749 // Clear the dict only after getting the item, to avoid
3750 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003751 tv = STACK_TV_BOT(-1);
3752 temp_tv = *tv;
3753 copy_tv(&di->di_tv, tv);
3754 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003755 }
3756 break;
3757
3758 // dict member with string key
3759 case ISN_STRINGMEMBER:
3760 {
3761 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003763 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764
3765 tv = STACK_TV_BOT(-1);
3766 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3767 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003768 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003770 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 }
3772 dict = tv->vval.v_dict;
3773
3774 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3775 == NULL)
3776 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003777 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003779 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003781 // Clear the dict after getting the item, to avoid that it
3782 // make the item invalid.
3783 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003785 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 }
3787 break;
3788
3789 case ISN_NEGATENR:
3790 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003791 if (tv->v_type != VAR_NUMBER
3792#ifdef FEAT_FLOAT
3793 && tv->v_type != VAR_FLOAT
3794#endif
3795 )
3796 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003797 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003798 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003799 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003800 }
3801#ifdef FEAT_FLOAT
3802 if (tv->v_type == VAR_FLOAT)
3803 tv->vval.v_float = -tv->vval.v_float;
3804 else
3805#endif
3806 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 break;
3808
3809 case ISN_CHECKNR:
3810 {
3811 int error = FALSE;
3812
3813 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003814 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003816 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 (void)tv_get_number_chk(tv, &error);
3818 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003819 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 }
3821 break;
3822
3823 case ISN_CHECKTYPE:
3824 {
3825 checktype_T *ct = &iptr->isn_arg.type;
3826
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003827 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003828 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003829 if (!ectx->ec_where.wt_variable)
3830 ectx->ec_where.wt_index = ct->ct_arg_idx;
3831 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3832 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003833 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003834 if (!ectx->ec_where.wt_variable)
3835 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003836
3837 // number 0 is FALSE, number 1 is TRUE
3838 if (tv->v_type == VAR_NUMBER
3839 && ct->ct_type->tt_type == VAR_BOOL
3840 && (tv->vval.v_number == 0
3841 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003843 tv->v_type = VAR_BOOL;
3844 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003845 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 }
3847 }
3848 break;
3849
Bram Moolenaar9af78762020-06-16 11:34:42 +02003850 case ISN_CHECKLEN:
3851 {
3852 int min_len = iptr->isn_arg.checklen.cl_min_len;
3853 list_T *list = NULL;
3854
3855 tv = STACK_TV_BOT(-1);
3856 if (tv->v_type == VAR_LIST)
3857 list = tv->vval.v_list;
3858 if (list == NULL || list->lv_len < min_len
3859 || (list->lv_len > min_len
3860 && !iptr->isn_arg.checklen.cl_more_OK))
3861 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003862 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003863 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003864 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003865 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003866 }
3867 }
3868 break;
3869
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003870 case ISN_SETTYPE:
3871 {
3872 checktype_T *ct = &iptr->isn_arg.type;
3873
3874 tv = STACK_TV_BOT(-1);
3875 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3876 {
3877 free_type(tv->vval.v_dict->dv_type);
3878 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3879 }
3880 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3881 {
3882 free_type(tv->vval.v_list->lv_type);
3883 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3884 }
3885 }
3886 break;
3887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003889 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 {
3891 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003892 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003894 if (iptr->isn_type == ISN_2BOOL)
3895 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003896 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003897 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003898 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003899 n = !n;
3900 }
3901 else
3902 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003903 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003904 SOURCING_LNUM = iptr->isn_lnum;
3905 n = tv_get_bool_chk(tv, &error);
3906 if (error)
3907 goto on_error;
3908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 clear_tv(tv);
3910 tv->v_type = VAR_BOOL;
3911 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3912 }
3913 break;
3914
3915 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003916 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003917 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02003918 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
3919 iptr->isn_type == ISN_2STRING_ANY,
3920 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003921 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 break;
3923
Bram Moolenaar08597872020-12-10 19:43:40 +01003924 case ISN_RANGE:
3925 {
3926 exarg_T ea;
3927 char *errormsg;
3928
Bram Moolenaarece0b872021-01-08 20:40:45 +01003929 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003930 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003931 ea.addr_type = ADDR_LINES;
3932 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003933 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003934 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003935 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003936
Bram Moolenaar4c137212021-04-19 16:48:48 +02003937 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003938 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003939 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003940 tv = STACK_TV_BOT(-1);
3941 tv->v_type = VAR_NUMBER;
3942 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003943 if (ea.addr_count == 0)
3944 tv->vval.v_number = curwin->w_cursor.lnum;
3945 else
3946 tv->vval.v_number = ea.line2;
3947 }
3948 break;
3949
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003950 case ISN_PUT:
3951 {
3952 int regname = iptr->isn_arg.put.put_regname;
3953 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3954 char_u *expr = NULL;
3955 int dir = FORWARD;
3956
Bram Moolenaar08597872020-12-10 19:43:40 +01003957 if (lnum < -2)
3958 {
3959 // line number was put on the stack by ISN_RANGE
3960 tv = STACK_TV_BOT(-1);
3961 curwin->w_cursor.lnum = tv->vval.v_number;
3962 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3963 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003964 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003965 }
3966 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003967 // :put! above cursor
3968 dir = BACKWARD;
3969 else if (lnum >= 0)
3970 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003971
3972 if (regname == '=')
3973 {
3974 tv = STACK_TV_BOT(-1);
3975 if (tv->v_type == VAR_STRING)
3976 expr = tv->vval.v_string;
3977 else
3978 {
3979 expr = typval2string(tv, TRUE); // allocates value
3980 clear_tv(tv);
3981 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003982 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003983 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003984 check_cursor();
3985 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3986 vim_free(expr);
3987 }
3988 break;
3989
Bram Moolenaar02194d22020-10-24 23:08:38 +02003990 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003991 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3992 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3993 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3994 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003995 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3996 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003997 break;
3998
Bram Moolenaar02194d22020-10-24 23:08:38 +02003999 case ISN_CMDMOD_REV:
4000 // filter regprog is owned by the instruction, don't free it
4001 cmdmod.cmod_filter_regmatch.regprog = NULL;
4002 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004003 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4004 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004005 break;
4006
Bram Moolenaar792f7862020-11-23 08:31:18 +01004007 case ISN_UNPACK:
4008 {
4009 int count = iptr->isn_arg.unpack.unp_count;
4010 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4011 list_T *l;
4012 listitem_T *li;
4013 int i;
4014
4015 // Check there is a valid list to unpack.
4016 tv = STACK_TV_BOT(-1);
4017 if (tv->v_type != VAR_LIST)
4018 {
4019 SOURCING_LNUM = iptr->isn_lnum;
4020 emsg(_(e_for_argument_must_be_sequence_of_lists));
4021 goto on_error;
4022 }
4023 l = tv->vval.v_list;
4024 if (l == NULL
4025 || l->lv_len < (semicolon ? count - 1 : count))
4026 {
4027 SOURCING_LNUM = iptr->isn_lnum;
4028 emsg(_(e_list_value_does_not_have_enough_items));
4029 goto on_error;
4030 }
4031 else if (!semicolon && l->lv_len > count)
4032 {
4033 SOURCING_LNUM = iptr->isn_lnum;
4034 emsg(_(e_list_value_has_more_items_than_targets));
4035 goto on_error;
4036 }
4037
4038 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004039 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004040 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004041 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004042
4043 // Variable after semicolon gets a list with the remaining
4044 // items.
4045 if (semicolon)
4046 {
4047 list_T *rem_list =
4048 list_alloc_with_items(l->lv_len - count + 1);
4049
4050 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004051 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004052 tv = STACK_TV_BOT(-count);
4053 tv->vval.v_list = rem_list;
4054 ++rem_list->lv_refcount;
4055 tv->v_lock = 0;
4056 li = l->lv_first;
4057 for (i = 0; i < count - 1; ++i)
4058 li = li->li_next;
4059 for (i = 0; li != NULL; ++i)
4060 {
4061 list_set_item(rem_list, i, &li->li_tv);
4062 li = li->li_next;
4063 }
4064 --count;
4065 }
4066
4067 // Produce the values in reverse order, first item last.
4068 li = l->lv_first;
4069 for (i = 0; i < count; ++i)
4070 {
4071 tv = STACK_TV_BOT(-i - 1);
4072 copy_tv(&li->li_tv, tv);
4073 li = li->li_next;
4074 }
4075
4076 list_unref(l);
4077 }
4078 break;
4079
Bram Moolenaarb2049902021-01-24 12:53:53 +01004080 case ISN_PROF_START:
4081 case ISN_PROF_END:
4082 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004083#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004084 funccall_T cookie;
4085 ufunc_T *cur_ufunc =
4086 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004087 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004088
4089 cookie.func = cur_ufunc;
4090 if (iptr->isn_type == ISN_PROF_START)
4091 {
4092 func_line_start(&cookie, iptr->isn_lnum);
4093 // if we get here the instruction is executed
4094 func_line_exec(&cookie);
4095 }
4096 else
4097 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004098#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004099 }
4100 break;
4101
Bram Moolenaar389df252020-07-09 21:20:47 +02004102 case ISN_SHUFFLE:
4103 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004104 typval_T tmp_tv;
4105 int item = iptr->isn_arg.shuffle.shfl_item;
4106 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004107
4108 tmp_tv = *STACK_TV_BOT(-item);
4109 for ( ; up > 0 && item > 1; --up)
4110 {
4111 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4112 --item;
4113 }
4114 *STACK_TV_BOT(-item) = tmp_tv;
4115 }
4116 break;
4117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004119 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004121 ectx->ec_where.wt_index = 0;
4122 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 break;
4124 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004125 continue;
4126
Bram Moolenaard032f342020-07-18 18:13:02 +02004127func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004128 // Restore previous function. If the frame pointer is where we started
4129 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004130 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004131 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004132
Bram Moolenaar4c137212021-04-19 16:48:48 +02004133 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004134 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004135 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004136 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004137
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004138on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004139 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004140 // If "emsg_silent" is set then ignore the error, unless it was set
4141 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004142 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004143 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004144 {
4145 // If a sequence of instructions causes an error while ":silent!"
4146 // was used, restore the stack length and jump ahead to restoring
4147 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004148 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004149 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004150 while (ectx->ec_stack.ga_len
4151 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004152 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004153 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004154 clear_tv(STACK_TV_BOT(0));
4155 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004156 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4157 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004158 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004159 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004160 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004161on_fatal_error:
4162 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004163 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004164 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004165 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 }
4167
4168done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004169 ret = OK;
4170theend:
4171 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4172 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004173}
4174
4175/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004176 * Execute the instructions from a VAR_INSTR typeval and put the result in
4177 * "rettv".
4178 * Return OK or FAIL.
4179 */
4180 int
4181exe_typval_instr(typval_T *tv, typval_T *rettv)
4182{
4183 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4184 isn_T *save_instr = ectx->ec_instr;
4185 int save_iidx = ectx->ec_iidx;
4186 int res;
4187
4188 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4189 res = exec_instructions(ectx);
4190 if (res == OK)
4191 {
4192 *rettv = *STACK_TV_BOT(-1);
4193 --ectx->ec_stack.ga_len;
4194 }
4195
4196 ectx->ec_instr = save_instr;
4197 ectx->ec_iidx = save_iidx;
4198
4199 return res;
4200}
4201
4202/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004203 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4204 * "substitute_instr".
4205 */
4206 char_u *
4207exe_substitute_instr(void)
4208{
4209 ectx_T *ectx = substitute_instr->subs_ectx;
4210 isn_T *save_instr = ectx->ec_instr;
4211 int save_iidx = ectx->ec_iidx;
4212 char_u *res;
4213
4214 ectx->ec_instr = substitute_instr->subs_instr;
4215 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004216 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004217 typval_T *tv = STACK_TV_BOT(-1);
4218
Bram Moolenaar27523602021-06-05 21:36:19 +02004219 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004220 --ectx->ec_stack.ga_len;
4221 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004222 }
4223 else
4224 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004225 substitute_instr->subs_status = FAIL;
4226 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228
Bram Moolenaar4c137212021-04-19 16:48:48 +02004229 ectx->ec_instr = save_instr;
4230 ectx->ec_iidx = save_iidx;
4231
4232 return res;
4233}
4234
4235/*
4236 * Call a "def" function from old Vim script.
4237 * Return OK or FAIL.
4238 */
4239 int
4240call_def_function(
4241 ufunc_T *ufunc,
4242 int argc_arg, // nr of arguments
4243 typval_T *argv, // arguments
4244 partial_T *partial, // optional partial for context
4245 typval_T *rettv) // return value
4246{
4247 ectx_T ectx; // execution context
4248 int argc = argc_arg;
4249 typval_T *tv;
4250 int idx;
4251 int ret = FAIL;
4252 int defcount = ufunc->uf_args.ga_len - argc;
4253 sctx_T save_current_sctx = current_sctx;
4254 int did_emsg_before = did_emsg_cumul + did_emsg;
4255 int save_suppress_errthrow = suppress_errthrow;
4256 msglist_T **saved_msg_list = NULL;
4257 msglist_T *private_msg_list = NULL;
4258 int save_emsg_silent_def = emsg_silent_def;
4259 int save_did_emsg_def = did_emsg_def;
4260 int orig_funcdepth;
4261
4262// Get pointer to item in the stack.
4263#undef STACK_TV
4264#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4265
4266// Get pointer to item at the bottom of the stack, -1 is the bottom.
4267#undef STACK_TV_BOT
4268#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4269
4270// Get pointer to a local variable on the stack. Negative for arguments.
4271#undef STACK_TV_VAR
4272#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4273
4274 if (ufunc->uf_def_status == UF_NOT_COMPILED
4275 || ufunc->uf_def_status == UF_COMPILE_ERROR
4276 || (func_needs_compiling(ufunc, PROFILING(ufunc))
4277 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
4278 == FAIL))
4279 {
4280 if (did_emsg_cumul + did_emsg == did_emsg_before)
4281 semsg(_(e_function_is_not_compiled_str),
4282 printable_func_name(ufunc));
4283 return FAIL;
4284 }
4285
4286 {
4287 // Check the function was really compiled.
4288 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4289 + ufunc->uf_dfunc_idx;
4290 if (INSTRUCTIONS(dfunc) == NULL)
4291 {
4292 iemsg("using call_def_function() on not compiled function");
4293 return FAIL;
4294 }
4295 }
4296
4297 // If depth of calling is getting too high, don't execute the function.
4298 orig_funcdepth = funcdepth_get();
4299 if (funcdepth_increment() == FAIL)
4300 return FAIL;
4301
4302 CLEAR_FIELD(ectx);
4303 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4304 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4305 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4306 {
4307 funcdepth_decrement();
4308 return FAIL;
4309 }
4310 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4311 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4312 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004313
4314 idx = argc - ufunc->uf_args.ga_len;
4315 if (idx > 0 && ufunc->uf_va_name == NULL)
4316 {
4317 if (idx == 1)
4318 emsg(_(e_one_argument_too_many));
4319 else
4320 semsg(_(e_nr_arguments_too_many), idx);
4321 goto failed_early;
4322 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004323 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4324 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004325 {
4326 if (idx == -1)
4327 emsg(_(e_one_argument_too_few));
4328 else
4329 semsg(_(e_nr_arguments_too_few), -idx);
4330 goto failed_early;
4331 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004332
4333 // Put arguments on the stack, but no more than what the function expects.
4334 // A lambda can be called with more arguments than it uses.
4335 for (idx = 0; idx < argc
4336 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4337 ++idx)
4338 {
4339 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4340 && argv[idx].v_type == VAR_SPECIAL
4341 && argv[idx].vval.v_number == VVAL_NONE)
4342 {
4343 // Use the default value.
4344 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4345 }
4346 else
4347 {
4348 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4349 && check_typval_arg_type(
4350 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4351 goto failed_early;
4352 copy_tv(&argv[idx], STACK_TV_BOT(0));
4353 }
4354 ++ectx.ec_stack.ga_len;
4355 }
4356
4357 // Turn varargs into a list. Empty list if no args.
4358 if (ufunc->uf_va_name != NULL)
4359 {
4360 int vararg_count = argc - ufunc->uf_args.ga_len;
4361
4362 if (vararg_count < 0)
4363 vararg_count = 0;
4364 else
4365 argc -= vararg_count;
4366 if (exe_newlist(vararg_count, &ectx) == FAIL)
4367 goto failed_early;
4368
4369 // Check the type of the list items.
4370 tv = STACK_TV_BOT(-1);
4371 if (ufunc->uf_va_type != NULL
4372 && ufunc->uf_va_type != &t_list_any
4373 && ufunc->uf_va_type->tt_member != &t_any
4374 && tv->vval.v_list != NULL)
4375 {
4376 type_T *expected = ufunc->uf_va_type->tt_member;
4377 listitem_T *li = tv->vval.v_list->lv_first;
4378
4379 for (idx = 0; idx < vararg_count; ++idx)
4380 {
4381 if (check_typval_arg_type(expected, &li->li_tv,
4382 argc + idx + 1) == FAIL)
4383 goto failed_early;
4384 li = li->li_next;
4385 }
4386 }
4387
4388 if (defcount > 0)
4389 // Move varargs list to below missing default arguments.
4390 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4391 --ectx.ec_stack.ga_len;
4392 }
4393
4394 // Make space for omitted arguments, will store default value below.
4395 // Any varargs list goes after them.
4396 if (defcount > 0)
4397 for (idx = 0; idx < defcount; ++idx)
4398 {
4399 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4400 ++ectx.ec_stack.ga_len;
4401 }
4402 if (ufunc->uf_va_name != NULL)
4403 ++ectx.ec_stack.ga_len;
4404
4405 // Frame pointer points to just after arguments.
4406 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4407 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4408
4409 {
4410 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4411 + ufunc->uf_dfunc_idx;
4412 ufunc_T *base_ufunc = dfunc->df_ufunc;
4413
4414 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4415 // by copy_func().
4416 if (partial != NULL || base_ufunc->uf_partial != NULL)
4417 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004418 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4419 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004420 goto failed_early;
4421 if (partial != NULL)
4422 {
4423 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4424 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004425 if (current_ectx->ec_outer_ref != NULL
4426 && current_ectx->ec_outer_ref->or_outer != NULL)
4427 ectx.ec_outer_ref->or_outer =
4428 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004429 }
4430 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004431 {
4432 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4433 ++partial->pt_refcount;
4434 ectx.ec_outer_ref->or_partial = partial;
4435 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004436 }
4437 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004438 {
4439 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4440 ++base_ufunc->uf_partial->pt_refcount;
4441 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4442 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004443 }
4444 }
4445
4446 // dummy frame entries
4447 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4448 {
4449 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4450 ++ectx.ec_stack.ga_len;
4451 }
4452
4453 {
4454 // Reserve space for local variables and any closure reference count.
4455 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4456 + ufunc->uf_dfunc_idx;
4457
4458 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4459 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4460 ectx.ec_stack.ga_len += dfunc->df_varcount;
4461 if (dfunc->df_has_closure)
4462 {
4463 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4464 STACK_TV_VAR(idx)->vval.v_number = 0;
4465 ++ectx.ec_stack.ga_len;
4466 }
4467
4468 ectx.ec_instr = INSTRUCTIONS(dfunc);
4469 }
4470
4471 // Following errors are in the function, not the caller.
4472 // Commands behave like vim9script.
4473 estack_push_ufunc(ufunc, 1);
4474 current_sctx = ufunc->uf_script_ctx;
4475 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4476
4477 // Use a specific location for storing error messages to be converted to an
4478 // exception.
4479 saved_msg_list = msg_list;
4480 msg_list = &private_msg_list;
4481
4482 // Do turn errors into exceptions.
4483 suppress_errthrow = FALSE;
4484
4485 // Do not delete the function while executing it.
4486 ++ufunc->uf_calls;
4487
4488 // When ":silent!" was used before calling then we still abort the
4489 // function. If ":silent!" is used in the function then we don't.
4490 emsg_silent_def = emsg_silent;
4491 did_emsg_def = 0;
4492
4493 ectx.ec_where.wt_index = 0;
4494 ectx.ec_where.wt_variable = FALSE;
4495
4496 // Execute the instructions until done.
4497 ret = exec_instructions(&ectx);
4498 if (ret == OK)
4499 {
4500 // function finished, get result from the stack.
4501 if (ufunc->uf_ret_type == &t_void)
4502 {
4503 rettv->v_type = VAR_VOID;
4504 }
4505 else
4506 {
4507 tv = STACK_TV_BOT(-1);
4508 *rettv = *tv;
4509 tv->v_type = VAR_UNKNOWN;
4510 }
4511 }
4512
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004513 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004514 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4515 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004516
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004517 // Deal with any remaining closures, they may be in use somewhere.
4518 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004519 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004520 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004521 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4522 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004523
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004524 estack_pop();
4525 current_sctx = save_current_sctx;
4526
Bram Moolenaarc970e422021-03-17 15:03:04 +01004527 // TODO: when is it safe to delete the function if it is no longer used?
4528 --ufunc->uf_calls;
4529
Bram Moolenaar352134b2020-10-17 22:04:08 +02004530 if (*msg_list != NULL && saved_msg_list != NULL)
4531 {
4532 msglist_T **plist = saved_msg_list;
4533
4534 // Append entries from the current msg_list (uncaught exceptions) to
4535 // the saved msg_list.
4536 while (*plist != NULL)
4537 plist = &(*plist)->next;
4538
4539 *plist = *msg_list;
4540 }
4541 msg_list = saved_msg_list;
4542
Bram Moolenaar4c137212021-04-19 16:48:48 +02004543 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004544 {
4545 cmdmod.cmod_filter_regmatch.regprog = NULL;
4546 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004547 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004548 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004549 emsg_silent_def = save_emsg_silent_def;
4550 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004551
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004552failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004553 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4555 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004558 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004559 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01004560 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004561 if (ectx.ec_outer_ref->or_outer_allocated)
4562 vim_free(ectx.ec_outer_ref->or_outer);
4563 partial_unref(ectx.ec_outer_ref->or_partial);
4564 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01004565 }
4566
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004567 // Not sure if this is necessary.
4568 suppress_errthrow = save_suppress_errthrow;
4569
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004570 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004571 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004572 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004573 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 return ret;
4575}
4576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004578 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4579 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4580 * "pfx" is prefixed to every line.
4581 */
4582 static void
4583list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4584{
4585 int line_idx = 0;
4586 int prev_current = 0;
4587 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004588 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004589
4590 for (current = 0; current < instr_count; ++current)
4591 {
4592 isn_T *iptr = &instr[current];
4593 char *line;
4594
4595 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004596 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004597 while (line_idx < iptr->isn_lnum
4598 && line_idx < ufunc->uf_lines.ga_len)
4599 {
4600 if (current > prev_current)
4601 {
4602 msg_puts("\n\n");
4603 prev_current = current;
4604 }
4605 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4606 if (line != NULL)
4607 msg(line);
4608 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004609 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4610 {
4611 int first_def_arg = ufunc->uf_args.ga_len
4612 - ufunc->uf_def_args.ga_len;
4613
4614 if (def_arg_idx > 0)
4615 msg_puts("\n\n");
4616 msg_start();
4617 msg_puts(" ");
4618 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4619 first_def_arg + def_arg_idx]);
4620 msg_puts(" = ");
4621 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4622 msg_clr_eos();
4623 msg_end();
4624 }
4625 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004626
4627 switch (iptr->isn_type)
4628 {
4629 case ISN_EXEC:
4630 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4631 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02004632 case ISN_EXEC_SPLIT:
4633 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
4634 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02004635 case ISN_LEGACY_EVAL:
4636 smsg("%s%4d EVAL legacy %s", pfx, current,
4637 iptr->isn_arg.string);
4638 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004639 case ISN_REDIRSTART:
4640 smsg("%s%4d REDIR", pfx, current);
4641 break;
4642 case ISN_REDIREND:
4643 smsg("%s%4d REDIR END%s", pfx, current,
4644 iptr->isn_arg.number ? " append" : "");
4645 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004646 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004647#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004648 smsg("%s%4d CEXPR pre %s", pfx, current,
4649 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004650#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004651 break;
4652 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004653#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004654 {
4655 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
4656
4657 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
4658 cexpr_get_auname(cer->cer_cmdidx),
4659 cer->cer_forceit ? "!" : "",
4660 cer->cer_cmdline);
4661 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02004662#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02004663 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004664 case ISN_INSTR:
4665 {
4666 smsg("%s%4d INSTR", pfx, current);
4667 list_instructions(" ", iptr->isn_arg.instr,
4668 INT_MAX, NULL);
4669 msg(" -------------");
4670 }
4671 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004672 case ISN_SUBSTITUTE:
4673 {
4674 subs_T *subs = &iptr->isn_arg.subs;
4675
4676 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4677 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4678 msg(" -------------");
4679 }
4680 break;
4681 case ISN_EXECCONCAT:
4682 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4683 (varnumber_T)iptr->isn_arg.number);
4684 break;
4685 case ISN_ECHO:
4686 {
4687 echo_T *echo = &iptr->isn_arg.echo;
4688
4689 smsg("%s%4d %s %d", pfx, current,
4690 echo->echo_with_white ? "ECHO" : "ECHON",
4691 echo->echo_count);
4692 }
4693 break;
4694 case ISN_EXECUTE:
4695 smsg("%s%4d EXECUTE %lld", pfx, current,
4696 (varnumber_T)(iptr->isn_arg.number));
4697 break;
4698 case ISN_ECHOMSG:
4699 smsg("%s%4d ECHOMSG %lld", pfx, current,
4700 (varnumber_T)(iptr->isn_arg.number));
4701 break;
4702 case ISN_ECHOERR:
4703 smsg("%s%4d ECHOERR %lld", pfx, current,
4704 (varnumber_T)(iptr->isn_arg.number));
4705 break;
4706 case ISN_LOAD:
4707 {
4708 if (iptr->isn_arg.number < 0)
4709 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4710 (varnumber_T)(iptr->isn_arg.number
4711 + STACK_FRAME_SIZE));
4712 else
4713 smsg("%s%4d LOAD $%lld", pfx, current,
4714 (varnumber_T)(iptr->isn_arg.number));
4715 }
4716 break;
4717 case ISN_LOADOUTER:
4718 {
4719 if (iptr->isn_arg.number < 0)
4720 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4721 iptr->isn_arg.outer.outer_depth,
4722 iptr->isn_arg.outer.outer_idx
4723 + STACK_FRAME_SIZE);
4724 else
4725 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4726 iptr->isn_arg.outer.outer_depth,
4727 iptr->isn_arg.outer.outer_idx);
4728 }
4729 break;
4730 case ISN_LOADV:
4731 smsg("%s%4d LOADV v:%s", pfx, current,
4732 get_vim_var_name(iptr->isn_arg.number));
4733 break;
4734 case ISN_LOADSCRIPT:
4735 {
4736 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4737 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4738 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4739 + sref->sref_idx;
4740
4741 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4742 sv->sv_name,
4743 sref->sref_idx,
4744 si->sn_name);
4745 }
4746 break;
4747 case ISN_LOADS:
4748 {
4749 scriptitem_T *si = SCRIPT_ITEM(
4750 iptr->isn_arg.loadstore.ls_sid);
4751
4752 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4753 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4754 }
4755 break;
4756 case ISN_LOADAUTO:
4757 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4758 break;
4759 case ISN_LOADG:
4760 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4761 break;
4762 case ISN_LOADB:
4763 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4764 break;
4765 case ISN_LOADW:
4766 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4767 break;
4768 case ISN_LOADT:
4769 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4770 break;
4771 case ISN_LOADGDICT:
4772 smsg("%s%4d LOAD g:", pfx, current);
4773 break;
4774 case ISN_LOADBDICT:
4775 smsg("%s%4d LOAD b:", pfx, current);
4776 break;
4777 case ISN_LOADWDICT:
4778 smsg("%s%4d LOAD w:", pfx, current);
4779 break;
4780 case ISN_LOADTDICT:
4781 smsg("%s%4d LOAD t:", pfx, current);
4782 break;
4783 case ISN_LOADOPT:
4784 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4785 break;
4786 case ISN_LOADENV:
4787 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4788 break;
4789 case ISN_LOADREG:
4790 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4791 break;
4792
4793 case ISN_STORE:
4794 if (iptr->isn_arg.number < 0)
4795 smsg("%s%4d STORE arg[%lld]", pfx, current,
4796 iptr->isn_arg.number + STACK_FRAME_SIZE);
4797 else
4798 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4799 break;
4800 case ISN_STOREOUTER:
4801 {
4802 if (iptr->isn_arg.number < 0)
4803 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4804 iptr->isn_arg.outer.outer_depth,
4805 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4806 else
4807 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4808 iptr->isn_arg.outer.outer_depth,
4809 iptr->isn_arg.outer.outer_idx);
4810 }
4811 break;
4812 case ISN_STOREV:
4813 smsg("%s%4d STOREV v:%s", pfx, current,
4814 get_vim_var_name(iptr->isn_arg.number));
4815 break;
4816 case ISN_STOREAUTO:
4817 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4818 break;
4819 case ISN_STOREG:
4820 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4821 break;
4822 case ISN_STOREB:
4823 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4824 break;
4825 case ISN_STOREW:
4826 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4827 break;
4828 case ISN_STORET:
4829 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4830 break;
4831 case ISN_STORES:
4832 {
4833 scriptitem_T *si = SCRIPT_ITEM(
4834 iptr->isn_arg.loadstore.ls_sid);
4835
4836 smsg("%s%4d STORES %s in %s", pfx, current,
4837 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4838 }
4839 break;
4840 case ISN_STORESCRIPT:
4841 {
4842 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4843 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4844 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4845 + sref->sref_idx;
4846
4847 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4848 sv->sv_name,
4849 sref->sref_idx,
4850 si->sn_name);
4851 }
4852 break;
4853 case ISN_STOREOPT:
4854 smsg("%s%4d STOREOPT &%s", pfx, current,
4855 iptr->isn_arg.storeopt.so_name);
4856 break;
4857 case ISN_STOREENV:
4858 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4859 break;
4860 case ISN_STOREREG:
4861 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4862 break;
4863 case ISN_STORENR:
4864 smsg("%s%4d STORE %lld in $%d", pfx, current,
4865 iptr->isn_arg.storenr.stnr_val,
4866 iptr->isn_arg.storenr.stnr_idx);
4867 break;
4868
4869 case ISN_STOREINDEX:
4870 smsg("%s%4d STOREINDEX %s", pfx, current,
4871 vartype_name(iptr->isn_arg.vartype));
4872 break;
4873
4874 case ISN_STORERANGE:
4875 smsg("%s%4d STORERANGE", pfx, current);
4876 break;
4877
4878 // constants
4879 case ISN_PUSHNR:
4880 smsg("%s%4d PUSHNR %lld", pfx, current,
4881 (varnumber_T)(iptr->isn_arg.number));
4882 break;
4883 case ISN_PUSHBOOL:
4884 case ISN_PUSHSPEC:
4885 smsg("%s%4d PUSH %s", pfx, current,
4886 get_var_special_name(iptr->isn_arg.number));
4887 break;
4888 case ISN_PUSHF:
4889#ifdef FEAT_FLOAT
4890 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4891#endif
4892 break;
4893 case ISN_PUSHS:
4894 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4895 break;
4896 case ISN_PUSHBLOB:
4897 {
4898 char_u *r;
4899 char_u numbuf[NUMBUFLEN];
4900 char_u *tofree;
4901
4902 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4903 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4904 vim_free(tofree);
4905 }
4906 break;
4907 case ISN_PUSHFUNC:
4908 {
4909 char *name = (char *)iptr->isn_arg.string;
4910
4911 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4912 name == NULL ? "[none]" : name);
4913 }
4914 break;
4915 case ISN_PUSHCHANNEL:
4916#ifdef FEAT_JOB_CHANNEL
4917 {
4918 channel_T *channel = iptr->isn_arg.channel;
4919
4920 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4921 channel == NULL ? 0 : channel->ch_id);
4922 }
4923#endif
4924 break;
4925 case ISN_PUSHJOB:
4926#ifdef FEAT_JOB_CHANNEL
4927 {
4928 typval_T tv;
4929 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004930 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02004931
4932 tv.v_type = VAR_JOB;
4933 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02004934 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004935 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4936 }
4937#endif
4938 break;
4939 case ISN_PUSHEXC:
4940 smsg("%s%4d PUSH v:exception", pfx, current);
4941 break;
4942 case ISN_UNLET:
4943 smsg("%s%4d UNLET%s %s", pfx, current,
4944 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4945 iptr->isn_arg.unlet.ul_name);
4946 break;
4947 case ISN_UNLETENV:
4948 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4949 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4950 iptr->isn_arg.unlet.ul_name);
4951 break;
4952 case ISN_UNLETINDEX:
4953 smsg("%s%4d UNLETINDEX", pfx, current);
4954 break;
4955 case ISN_UNLETRANGE:
4956 smsg("%s%4d UNLETRANGE", pfx, current);
4957 break;
4958 case ISN_LOCKCONST:
4959 smsg("%s%4d LOCKCONST", pfx, current);
4960 break;
4961 case ISN_NEWLIST:
4962 smsg("%s%4d NEWLIST size %lld", pfx, current,
4963 (varnumber_T)(iptr->isn_arg.number));
4964 break;
4965 case ISN_NEWDICT:
4966 smsg("%s%4d NEWDICT size %lld", pfx, current,
4967 (varnumber_T)(iptr->isn_arg.number));
4968 break;
4969
4970 // function call
4971 case ISN_BCALL:
4972 {
4973 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4974
4975 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4976 internal_func_name(cbfunc->cbf_idx),
4977 cbfunc->cbf_argcount);
4978 }
4979 break;
4980 case ISN_DCALL:
4981 {
4982 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4983 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4984 + cdfunc->cdf_idx;
4985
4986 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
4987 df->df_ufunc->uf_name_exp != NULL
4988 ? df->df_ufunc->uf_name_exp
4989 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4990 }
4991 break;
4992 case ISN_UCALL:
4993 {
4994 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4995
4996 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
4997 cufunc->cuf_name, cufunc->cuf_argcount);
4998 }
4999 break;
5000 case ISN_PCALL:
5001 {
5002 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5003
5004 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5005 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5006 }
5007 break;
5008 case ISN_PCALL_END:
5009 smsg("%s%4d PCALL end", pfx, current);
5010 break;
5011 case ISN_RETURN:
5012 smsg("%s%4d RETURN", pfx, current);
5013 break;
5014 case ISN_RETURN_ZERO:
5015 smsg("%s%4d RETURN 0", pfx, current);
5016 break;
5017 case ISN_FUNCREF:
5018 {
5019 funcref_T *funcref = &iptr->isn_arg.funcref;
5020 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5021 + funcref->fr_func;
5022
5023 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
5024 }
5025 break;
5026
5027 case ISN_NEWFUNC:
5028 {
5029 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5030
5031 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5032 newfunc->nf_lambda, newfunc->nf_global);
5033 }
5034 break;
5035
5036 case ISN_DEF:
5037 {
5038 char_u *name = iptr->isn_arg.string;
5039
5040 smsg("%s%4d DEF %s", pfx, current,
5041 name == NULL ? (char_u *)"" : name);
5042 }
5043 break;
5044
5045 case ISN_JUMP:
5046 {
5047 char *when = "?";
5048
5049 switch (iptr->isn_arg.jump.jump_when)
5050 {
5051 case JUMP_ALWAYS:
5052 when = "JUMP";
5053 break;
5054 case JUMP_AND_KEEP_IF_TRUE:
5055 when = "JUMP_AND_KEEP_IF_TRUE";
5056 break;
5057 case JUMP_IF_FALSE:
5058 when = "JUMP_IF_FALSE";
5059 break;
5060 case JUMP_AND_KEEP_IF_FALSE:
5061 when = "JUMP_AND_KEEP_IF_FALSE";
5062 break;
5063 case JUMP_IF_COND_FALSE:
5064 when = "JUMP_IF_COND_FALSE";
5065 break;
5066 case JUMP_IF_COND_TRUE:
5067 when = "JUMP_IF_COND_TRUE";
5068 break;
5069 }
5070 smsg("%s%4d %s -> %d", pfx, current, when,
5071 iptr->isn_arg.jump.jump_where);
5072 }
5073 break;
5074
5075 case ISN_JUMP_IF_ARG_SET:
5076 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5077 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5078 iptr->isn_arg.jump.jump_where);
5079 break;
5080
5081 case ISN_FOR:
5082 {
5083 forloop_T *forloop = &iptr->isn_arg.forloop;
5084
5085 smsg("%s%4d FOR $%d -> %d", pfx, current,
5086 forloop->for_idx, forloop->for_end);
5087 }
5088 break;
5089
5090 case ISN_TRY:
5091 {
5092 try_T *try = &iptr->isn_arg.try;
5093
5094 if (try->try_ref->try_finally == 0)
5095 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5096 pfx, current,
5097 try->try_ref->try_catch,
5098 try->try_ref->try_endtry);
5099 else
5100 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5101 pfx, current,
5102 try->try_ref->try_catch,
5103 try->try_ref->try_finally,
5104 try->try_ref->try_endtry);
5105 }
5106 break;
5107 case ISN_CATCH:
5108 // TODO
5109 smsg("%s%4d CATCH", pfx, current);
5110 break;
5111 case ISN_TRYCONT:
5112 {
5113 trycont_T *trycont = &iptr->isn_arg.trycont;
5114
5115 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5116 trycont->tct_levels,
5117 trycont->tct_levels == 1 ? "" : "s",
5118 trycont->tct_where);
5119 }
5120 break;
5121 case ISN_FINALLY:
5122 smsg("%s%4d FINALLY", pfx, current);
5123 break;
5124 case ISN_ENDTRY:
5125 smsg("%s%4d ENDTRY", pfx, current);
5126 break;
5127 case ISN_THROW:
5128 smsg("%s%4d THROW", pfx, current);
5129 break;
5130
5131 // expression operations on number
5132 case ISN_OPNR:
5133 case ISN_OPFLOAT:
5134 case ISN_OPANY:
5135 {
5136 char *what;
5137 char *ins;
5138
5139 switch (iptr->isn_arg.op.op_type)
5140 {
5141 case EXPR_MULT: what = "*"; break;
5142 case EXPR_DIV: what = "/"; break;
5143 case EXPR_REM: what = "%"; break;
5144 case EXPR_SUB: what = "-"; break;
5145 case EXPR_ADD: what = "+"; break;
5146 default: what = "???"; break;
5147 }
5148 switch (iptr->isn_type)
5149 {
5150 case ISN_OPNR: ins = "OPNR"; break;
5151 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5152 case ISN_OPANY: ins = "OPANY"; break;
5153 default: ins = "???"; break;
5154 }
5155 smsg("%s%4d %s %s", pfx, current, ins, what);
5156 }
5157 break;
5158
5159 case ISN_COMPAREBOOL:
5160 case ISN_COMPARESPECIAL:
5161 case ISN_COMPARENR:
5162 case ISN_COMPAREFLOAT:
5163 case ISN_COMPARESTRING:
5164 case ISN_COMPAREBLOB:
5165 case ISN_COMPARELIST:
5166 case ISN_COMPAREDICT:
5167 case ISN_COMPAREFUNC:
5168 case ISN_COMPAREANY:
5169 {
5170 char *p;
5171 char buf[10];
5172 char *type;
5173
5174 switch (iptr->isn_arg.op.op_type)
5175 {
5176 case EXPR_EQUAL: p = "=="; break;
5177 case EXPR_NEQUAL: p = "!="; break;
5178 case EXPR_GREATER: p = ">"; break;
5179 case EXPR_GEQUAL: p = ">="; break;
5180 case EXPR_SMALLER: p = "<"; break;
5181 case EXPR_SEQUAL: p = "<="; break;
5182 case EXPR_MATCH: p = "=~"; break;
5183 case EXPR_IS: p = "is"; break;
5184 case EXPR_ISNOT: p = "isnot"; break;
5185 case EXPR_NOMATCH: p = "!~"; break;
5186 default: p = "???"; break;
5187 }
5188 STRCPY(buf, p);
5189 if (iptr->isn_arg.op.op_ic == TRUE)
5190 strcat(buf, "?");
5191 switch(iptr->isn_type)
5192 {
5193 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5194 case ISN_COMPARESPECIAL:
5195 type = "COMPARESPECIAL"; break;
5196 case ISN_COMPARENR: type = "COMPARENR"; break;
5197 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5198 case ISN_COMPARESTRING:
5199 type = "COMPARESTRING"; break;
5200 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5201 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5202 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5203 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5204 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5205 default: type = "???"; break;
5206 }
5207
5208 smsg("%s%4d %s %s", pfx, current, type, buf);
5209 }
5210 break;
5211
5212 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5213 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5214
5215 // expression operations
5216 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5217 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5218 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5219 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5220 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5221 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5222 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5223 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5224 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5225 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5226 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5227 case ISN_SLICE: smsg("%s%4d SLICE %lld",
5228 pfx, current, iptr->isn_arg.number); break;
5229 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
5230 pfx, current, iptr->isn_arg.number); break;
5231 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5232 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5233 iptr->isn_arg.string); break;
5234 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5235
5236 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5237 case ISN_CHECKTYPE:
5238 {
5239 checktype_T *ct = &iptr->isn_arg.type;
5240 char *tofree;
5241
5242 if (ct->ct_arg_idx == 0)
5243 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5244 type_name(ct->ct_type, &tofree),
5245 (int)ct->ct_off);
5246 else
5247 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
5248 type_name(ct->ct_type, &tofree),
5249 (int)ct->ct_off,
5250 (int)ct->ct_arg_idx);
5251 vim_free(tofree);
5252 break;
5253 }
5254 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5255 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5256 iptr->isn_arg.checklen.cl_min_len);
5257 break;
5258 case ISN_SETTYPE:
5259 {
5260 char *tofree;
5261
5262 smsg("%s%4d SETTYPE %s", pfx, current,
5263 type_name(iptr->isn_arg.type.ct_type, &tofree));
5264 vim_free(tofree);
5265 break;
5266 }
5267 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005268 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5269 smsg("%s%4d INVERT %d (!val)", pfx, current,
5270 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005271 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005272 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5273 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005274 break;
5275 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005276 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005277 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005278 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5279 pfx, current,
5280 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005281 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005282 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5283 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005284 break;
5285 case ISN_PUT:
5286 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5287 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005288 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005289 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5290 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005291 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005292 else
5293 smsg("%s%4d PUT %c %ld", pfx, current,
5294 iptr->isn_arg.put.put_regname,
5295 (long)iptr->isn_arg.put.put_lnum);
5296 break;
5297
5298 // TODO: summarize modifiers
5299 case ISN_CMDMOD:
5300 {
5301 char_u *buf;
5302 size_t len = produce_cmdmods(
5303 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5304
5305 buf = alloc(len + 1);
5306 if (buf != NULL)
5307 {
5308 (void)produce_cmdmods(
5309 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5310 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5311 vim_free(buf);
5312 }
5313 break;
5314 }
5315 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5316
5317 case ISN_PROF_START:
5318 smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum);
5319 break;
5320
5321 case ISN_PROF_END:
5322 smsg("%s%4d PROFILE END", pfx, current);
5323 break;
5324
5325 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5326 iptr->isn_arg.unpack.unp_count,
5327 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5328 break;
5329 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5330 iptr->isn_arg.shuffle.shfl_item,
5331 iptr->isn_arg.shuffle.shfl_up);
5332 break;
5333 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5334
5335 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5336 return;
5337 }
5338
5339 out_flush(); // output one line at a time
5340 ui_breakcheck();
5341 if (got_int)
5342 break;
5343 }
5344}
5345
5346/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005347 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005348 * We don't really need this at runtime, but we do have tests that require it,
5349 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350 */
5351 void
5352ex_disassemble(exarg_T *eap)
5353{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005354 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005355 char_u *fname;
5356 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357 dfunc_T *dfunc;
5358 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005359 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005360 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005362 if (STRNCMP(arg, "<lambda>", 8) == 0)
5363 {
5364 arg += 8;
5365 (void)getdigits(&arg);
5366 fname = vim_strnsave(eap->arg, arg - eap->arg);
5367 }
5368 else
5369 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005370 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005371 if (fname == NULL)
5372 {
5373 semsg(_(e_invarg2), eap->arg);
5374 return;
5375 }
5376
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005377 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005378 if (ufunc == NULL)
5379 {
5380 char_u *p = untrans_function_name(fname);
5381
5382 if (p != NULL)
5383 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005384 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005385 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005386 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387 if (ufunc == NULL)
5388 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005389 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390 return;
5391 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005392 if (func_needs_compiling(ufunc, eap->forceit)
5393 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005394 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005395 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005397 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398 return;
5399 }
5400 if (ufunc->uf_name_exp != NULL)
5401 msg((char *)ufunc->uf_name_exp);
5402 else
5403 msg((char *)ufunc->uf_name);
5404
5405 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005406#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005407 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
5408 instr_count = eap->forceit ? dfunc->df_instr_prof_count
5409 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005410#else
5411 instr = dfunc->df_instr;
5412 instr_count = dfunc->df_instr_count;
5413#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005414
Bram Moolenaar4c137212021-04-19 16:48:48 +02005415 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416}
5417
5418/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005419 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005420 * list, etc. Mostly like what JavaScript does, except that empty list and
5421 * empty dictionary are FALSE.
5422 */
5423 int
5424tv2bool(typval_T *tv)
5425{
5426 switch (tv->v_type)
5427 {
5428 case VAR_NUMBER:
5429 return tv->vval.v_number != 0;
5430 case VAR_FLOAT:
5431#ifdef FEAT_FLOAT
5432 return tv->vval.v_float != 0.0;
5433#else
5434 break;
5435#endif
5436 case VAR_PARTIAL:
5437 return tv->vval.v_partial != NULL;
5438 case VAR_FUNC:
5439 case VAR_STRING:
5440 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5441 case VAR_LIST:
5442 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5443 case VAR_DICT:
5444 return tv->vval.v_dict != NULL
5445 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5446 case VAR_BOOL:
5447 case VAR_SPECIAL:
5448 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5449 case VAR_JOB:
5450#ifdef FEAT_JOB_CHANNEL
5451 return tv->vval.v_job != NULL;
5452#else
5453 break;
5454#endif
5455 case VAR_CHANNEL:
5456#ifdef FEAT_JOB_CHANNEL
5457 return tv->vval.v_channel != NULL;
5458#else
5459 break;
5460#endif
5461 case VAR_BLOB:
5462 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5463 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005464 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005465 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005466 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005467 break;
5468 }
5469 return FALSE;
5470}
5471
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005472 void
5473emsg_using_string_as(typval_T *tv, int as_number)
5474{
5475 semsg(_(as_number ? e_using_string_as_number_str
5476 : e_using_string_as_bool_str),
5477 tv->vval.v_string == NULL
5478 ? (char_u *)"" : tv->vval.v_string);
5479}
5480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481/*
5482 * If "tv" is a string give an error and return FAIL.
5483 */
5484 int
5485check_not_string(typval_T *tv)
5486{
5487 if (tv->v_type == VAR_STRING)
5488 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005489 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 clear_tv(tv);
5491 return FAIL;
5492 }
5493 return OK;
5494}
5495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496
5497#endif // FEAT_EVAL