blob: 6a11e8a6bc9292e859cf0543a84fc5a21dd9f1e1 [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
46// A stack is used to store:
47// - arguments passed to a :def function
48// - info about the calling function, to use when returning
49// - local variables
50// - temporary values
51//
52// In detail (FP == Frame Pointer):
53// arg1 first argument from caller (if present)
54// arg2 second argument from caller (if present)
55// extra_arg1 any missing optional argument default value
56// FP -> cur_func calling function
57// current previous instruction pointer
58// frame_ptr previous Frame Pointer
59// var1 space for local variable
60// var2 space for local variable
61// .... fixed space for max. number of local variables
62// temp temporary values
63// .... flexible space for temporary values (can grow big)
64
65/*
66 * Execution context.
67 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010068struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010069 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020070 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020071 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072
Bram Moolenaar0186e582021-01-10 18:33:11 +010073 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020074 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076 garray_T ec_trystack; // stack of trycmd_T values
77 int ec_in_catch; // when TRUE in catch or finally block
78
79 int ec_dfunc_idx; // current function index
80 isn_T *ec_instr; // array with instructions
81 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020082
83 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020084
85 int ec_did_emsg_before;
86 int ec_trylevel_at_start;
87 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010088};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010089
Bram Moolenaar12d26532021-02-19 19:13:21 +010090#ifdef FEAT_PROFILE
91// stack of profinfo_T used when profiling.
92static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
93#endif
94
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020096#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 +010097
Bram Moolenaar418f1df2020-08-12 21:34:49 +020098 void
99to_string_error(vartype_T vartype)
100{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200101 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200102}
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100105 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106 */
107 static int
108ufunc_argcount(ufunc_T *ufunc)
109{
110 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100144 * This adds a stack frame and sets the instruction pointer to the start of the
145 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100146 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147 *
148 * Stack has:
149 * - current arguments (already there)
150 * - omitted optional argument (default values) added here
151 * - stack frame:
152 * - pointer to calling function
153 * - Index of next instruction in calling function
154 * - previous frame pointer
155 * - reserved space for local variables
156 */
157 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100158call_dfunc(
159 int cdf_idx,
160 partial_T *pt,
161 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100162 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100164 int argcount = argcount_arg;
165 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
166 ufunc_T *ufunc = dfunc->df_ufunc;
167 int arg_to_add;
168 int vararg_count = 0;
169 int varcount;
170 int idx;
171 estack_T *entry;
172 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100176 // don't use ufunc->uf_name, it may have been freed
177 emsg_funcname(e_func_deleted,
178 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 return FAIL;
180 }
181
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100182#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100183 if (do_profiling == PROF_YES)
184 {
185 if (ga_grow(&profile_info_ga, 1) == OK)
186 {
187 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
188 + profile_info_ga.ga_len;
189 ++profile_info_ga.ga_len;
190 CLEAR_POINTER(info);
191 profile_may_start_func(info, ufunc,
192 (((dfunc_T *)def_functions.ga_data)
193 + ectx->ec_dfunc_idx)->df_ufunc);
194 }
195
196 // Profiling might be enabled/disabled along the way. This should not
197 // fail, since the function was compiled before and toggling profiling
198 // doesn't change any errors.
199 if (func_needs_compiling(ufunc, PROFILING(ufunc))
200 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100201 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100202 return FAIL;
203 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100204#endif
205
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200206 if (ufunc->uf_va_name != NULL)
207 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200208 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200209 // Stack at time of call with 2 varargs:
210 // normal_arg
211 // optional_arg
212 // vararg_1
213 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200214 // After creating the list:
215 // normal_arg
216 // optional_arg
217 // vararg-list
218 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200219 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200220 // After creating the list
221 // normal_arg
222 // (space for optional_arg)
223 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200224 vararg_count = argcount - ufunc->uf_args.ga_len;
225 if (vararg_count < 0)
226 vararg_count = 0;
227 else
228 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200229 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200230 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200231
232 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200233 }
234
Bram Moolenaarfe270812020-04-11 22:31:27 +0200235 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200236 if (arg_to_add < 0)
237 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200238 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200239 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200240 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200241 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200242 return FAIL;
243 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200244
245 // Reserve space for:
246 // - missing arguments
247 // - stack frame
248 // - local variables
249 // - if needed: a counter for number of closures created in
250 // ectx->ec_funcrefs.
251 varcount = dfunc->df_varcount + dfunc->df_has_closure;
252 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
253 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254 return FAIL;
255
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100256 // If depth of calling is getting too high, don't execute the function.
257 if (funcdepth_increment() == FAIL)
258 return FAIL;
259
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100260 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200261 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100262 {
263 floc = ALLOC_ONE(funclocal_T);
264 if (floc == NULL)
265 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200266 *floc = ectx->ec_funclocal;
267 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100268 }
269
Bram Moolenaarfe270812020-04-11 22:31:27 +0200270 // Move the vararg-list to below the missing optional arguments.
271 if (vararg_count > 0 && arg_to_add > 0)
272 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100273
274 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200275 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200276 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200277 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278
279 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
281 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100282 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100283 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100284 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200285 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286
287 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200288 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200290 if (dfunc->df_has_closure)
291 {
292 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
293
294 tv->v_type = VAR_NUMBER;
295 tv->vval.v_number = 0;
296 }
297 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100299 if (pt != NULL || ufunc->uf_partial != NULL
300 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100301 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100302 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
303
304 if (outer == NULL)
305 return FAIL;
306 if (pt != NULL)
307 {
308 *outer = pt->pt_outer;
309 outer->out_up_is_copy = TRUE;
310 }
311 else if (ufunc->uf_partial != NULL)
312 {
313 *outer = ufunc->uf_partial->pt_outer;
314 outer->out_up_is_copy = TRUE;
315 }
316 else
317 {
318 outer->out_stack = &ectx->ec_stack;
319 outer->out_frame_idx = ectx->ec_frame_idx;
320 outer->out_up = ectx->ec_outer;
321 }
322 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100323 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100324 else
325 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100326
Bram Moolenaarc970e422021-03-17 15:03:04 +0100327 ++ufunc->uf_calls;
328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329 // Set execution state to the start of the called function.
330 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100331 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100332 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200333 if (entry != NULL)
334 {
335 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200336 // Save the current context so it can be restored on return.
337 entry->es_save_sctx = current_sctx;
338 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200339 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100340
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200341 // Start execution at the first instruction.
342 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343
344 return OK;
345}
346
347// Get pointer to item in the stack.
348#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
349
350/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200351 * Used when returning from a function: Check if any closure is still
352 * referenced. If so then move the arguments and variables to a separate piece
353 * of stack to be used when the closure is called.
354 * When "free_arguments" is TRUE the arguments are to be freed.
355 * Returns FAIL when out of memory.
356 */
357 static int
358handle_closure_in_use(ectx_T *ectx, int free_arguments)
359{
360 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
361 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200362 int argcount;
363 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200364 int idx;
365 typval_T *tv;
366 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200367 garray_T *gap = &ectx->ec_funcrefs;
368 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200370 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200371 return OK; // function was freed
372 if (dfunc->df_has_closure == 0)
373 return OK; // no closures
374 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
375 closure_count = tv->vval.v_number;
376 if (closure_count == 0)
377 return OK; // no funcrefs created
378
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200379 argcount = ufunc_argcount(dfunc->df_ufunc);
380 top = ectx->ec_frame_idx - argcount;
381
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200382 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200383 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200384 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200385 partial_T *pt;
386 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200387
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200388 if (off < 0)
389 continue; // count is off or already done
390 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200391 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200392 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200393 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200394 int i;
395
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200397 // unreferenced on return.
398 for (i = 0; i < dfunc->df_varcount; ++i)
399 {
400 typval_T *stv = STACK_TV(ectx->ec_frame_idx
401 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200402 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200403 --refcount;
404 }
405 if (refcount > 1)
406 {
407 closure_in_use = TRUE;
408 break;
409 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200410 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200411 }
412
413 if (closure_in_use)
414 {
415 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
416 typval_T *stack;
417
418 // A closure is using the arguments and/or local variables.
419 // Move them to the called function.
420 if (funcstack == NULL)
421 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200422 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
423 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200424 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
425 funcstack->fs_ga.ga_data = stack;
426 if (stack == NULL)
427 {
428 vim_free(funcstack);
429 return FAIL;
430 }
431
432 // Move or copy the arguments.
433 for (idx = 0; idx < argcount; ++idx)
434 {
435 tv = STACK_TV(top + idx);
436 if (free_arguments)
437 {
438 *(stack + idx) = *tv;
439 tv->v_type = VAR_UNKNOWN;
440 }
441 else
442 copy_tv(tv, stack + idx);
443 }
444 // Move the local variables.
445 for (idx = 0; idx < dfunc->df_varcount; ++idx)
446 {
447 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200448
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200449 // A partial created for a local function, that is also used as a
450 // local variable, has a reference count for the variable, thus
451 // will never go down to zero. When all these refcounts are one
452 // then the funcstack is unused. We need to count how many we have
453 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200454 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
455 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200456 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200457
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200458 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200459 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
460 gap->ga_len - closure_count + i])
461 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200462 }
463
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200464 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200465 tv->v_type = VAR_UNKNOWN;
466 }
467
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200468 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200469 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200470 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
471 - closure_count + idx];
472 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200473 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200474 ++funcstack->fs_refcount;
475 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100476 pt->pt_outer.out_stack = &funcstack->fs_ga;
477 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
478 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200479 }
480 }
481 }
482
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200483 for (idx = 0; idx < closure_count; ++idx)
484 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
485 - closure_count + idx]);
486 gap->ga_len -= closure_count;
487 if (gap->ga_len == 0)
488 ga_clear(gap);
489
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490 return OK;
491}
492
493/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200494 * Called when a partial is freed or its reference count goes down to one. The
495 * funcstack may be the only reference to the partials in the local variables.
496 * Go over all of them, the funcref and can be freed if all partials
497 * referencing the funcstack have a reference count of one.
498 */
499 void
500funcstack_check_refcount(funcstack_T *funcstack)
501{
502 int i;
503 garray_T *gap = &funcstack->fs_ga;
504 int done = 0;
505
506 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
507 return;
508 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
509 {
510 typval_T *tv = ((typval_T *)gap->ga_data) + i;
511
512 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
513 && tv->vval.v_partial->pt_funcstack == funcstack
514 && tv->vval.v_partial->pt_refcount == 1)
515 ++done;
516 }
517 if (done == funcstack->fs_min_refcount)
518 {
519 typval_T *stack = gap->ga_data;
520
521 // All partials referencing the funcstack have a reference count of
522 // one, thus the funcstack is no longer of use.
523 for (i = 0; i < gap->ga_len; ++i)
524 clear_tv(stack + i);
525 vim_free(stack);
526 vim_free(funcstack);
527 }
528}
529
530/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 * Return from the current function.
532 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200533 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200534func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100537 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200538 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
539 + ectx->ec_dfunc_idx;
540 int argcount = ufunc_argcount(dfunc->df_ufunc);
541 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200542 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100543 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
544 + STACK_FRAME_FUNC_OFF)->vval.v_number;
545 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
546 + prev_dfunc_idx;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100547 funclocal_T *floc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548
Bram Moolenaar12d26532021-02-19 19:13:21 +0100549#ifdef FEAT_PROFILE
550 if (do_profiling == PROF_YES)
551 {
552 ufunc_T *caller = prev_dfunc->df_ufunc;
553
554 if (dfunc->df_ufunc->uf_profiling
555 || (caller != NULL && caller->uf_profiling))
556 {
557 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
558 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
559 --profile_info_ga.ga_len;
560 }
561 }
562#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100563 // TODO: when is it safe to delete the function when it is no longer used?
564 --dfunc->df_ufunc->uf_calls;
565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200567 entry = estack_pop();
568 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200569 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200571 if (handle_closure_in_use(ectx, TRUE) == FAIL)
572 return FAIL;
573
574 // Clear the arguments.
575 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
576 clear_tv(STACK_TV(idx));
577
578 // Clear local variables and temp values, but not the return value.
579 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100580 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100582
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100583 // The return value should be on top of the stack. However, when aborting
584 // it may not be there and ec_frame_idx is the top of the stack.
585 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100586 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100587 ret_idx = 0;
588
Bram Moolenaar0186e582021-01-10 18:33:11 +0100589 vim_free(ectx->ec_outer);
590
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100591 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100592 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
594 + STACK_FRAME_IIDX_OFF)->vval.v_number;
595 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
596 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100597 floc = (void *)STACK_TV(ectx->ec_frame_idx
598 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200599 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100600 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
601 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100602 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100603
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100604 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200605 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100606 else
607 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200608 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100609 vim_free(floc);
610 }
611
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100612 if (ret_idx > 0)
613 {
614 // Reset the stack to the position before the call, with a spot for the
615 // return value, moved there from above the frame.
616 ectx->ec_stack.ga_len = top + 1;
617 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
618 }
619 else
620 // Reset the stack to the position before the call.
621 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100623 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200624 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625}
626
627#undef STACK_TV
628
629/*
630 * Prepare arguments and rettv for calling a builtin or user function.
631 */
632 static int
633call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
634{
635 int idx;
636 typval_T *tv;
637
638 // Move arguments from bottom of the stack to argvars[] and add terminator.
639 for (idx = 0; idx < argcount; ++idx)
640 argvars[idx] = *STACK_TV_BOT(idx - argcount);
641 argvars[argcount].v_type = VAR_UNKNOWN;
642
643 // Result replaces the arguments on the stack.
644 if (argcount > 0)
645 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200646 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 return FAIL;
648 else
649 ++ectx->ec_stack.ga_len;
650
651 // Default return value is zero.
652 tv = STACK_TV_BOT(-1);
653 tv->v_type = VAR_NUMBER;
654 tv->vval.v_number = 0;
655
656 return OK;
657}
658
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200659// Ugly global to avoid passing the execution context around through many
660// layers.
661static ectx_T *current_ectx = NULL;
662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663/*
664 * Call a builtin function by index.
665 */
666 static int
667call_bfunc(int func_idx, int argcount, ectx_T *ectx)
668{
669 typval_T argvars[MAX_FUNC_ARGS];
670 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100671 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200672 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 if (call_prepare(argcount, argvars, ectx) == FAIL)
675 return FAIL;
676
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200677 // Call the builtin function. Set "current_ectx" so that when it
678 // recursively invokes call_def_function() a closure context can be set.
679 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200681 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682
683 // Clear the arguments.
684 for (idx = 0; idx < argcount; ++idx)
685 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200686
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100687 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200688 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 return OK;
690}
691
692/*
693 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100694 * If the function is compiled this will add a stack frame and set the
695 * instruction pointer at the start of the function.
696 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100697 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100698 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 */
700 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100701call_ufunc(
702 ufunc_T *ufunc,
703 partial_T *pt,
704 int argcount,
705 ectx_T *ectx,
706 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707{
708 typval_T argvars[MAX_FUNC_ARGS];
709 funcexe_T funcexe;
710 int error;
711 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100712 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100713#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100714 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100715#else
716# define profiling FALSE
717#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718
Bram Moolenaarb2049902021-01-24 12:53:53 +0100719 if (func_needs_compiling(ufunc, profiling)
720 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200721 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200722 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100723 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100724 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100725 if (error != FCERR_UNKNOWN)
726 {
727 if (error == FCERR_TOOMANY)
728 semsg(_(e_toomanyarg), ufunc->uf_name);
729 else
730 semsg(_(e_toofewarg), ufunc->uf_name);
731 return FAIL;
732 }
733
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100734 // The function has been compiled, can call it quickly. For a function
735 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100736 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100737 if (iptr != NULL)
738 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100739 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100740 iptr->isn_type = ISN_DCALL;
741 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
742 iptr->isn_arg.dfunc.cdf_argcount = argcount;
743 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200744 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746
747 if (call_prepare(argcount, argvars, ectx) == FAIL)
748 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200749 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 funcexe.evaluate = TRUE;
751
752 // Call the user function. Result goes in last position on the stack.
753 // TODO: add selfdict if there is one
754 error = call_user_func_check(ufunc, argcount, argvars,
755 STACK_TV_BOT(-1), &funcexe, NULL);
756
757 // Clear the arguments.
758 for (idx = 0; idx < argcount; ++idx)
759 clear_tv(&argvars[idx]);
760
761 if (error != FCERR_NONE)
762 {
763 user_func_error(error, ufunc->uf_name);
764 return FAIL;
765 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100766 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200767 // Error other than from calling the function itself.
768 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 return OK;
770}
771
772/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100773 * If command modifiers were applied restore them.
774 */
775 static void
776may_restore_cmdmod(funclocal_T *funclocal)
777{
778 if (funclocal->floc_restore_cmdmod)
779 {
780 cmdmod.cmod_filter_regmatch.regprog = NULL;
781 undo_cmdmod(&cmdmod);
782 cmdmod = funclocal->floc_save_cmdmod;
783 funclocal->floc_restore_cmdmod = FALSE;
784 }
785}
786
787/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200788 * Return TRUE if an error was given or CTRL-C was pressed.
789 */
790 static int
791vim9_aborting(int prev_called_emsg)
792{
793 return called_emsg > prev_called_emsg || got_int || did_throw;
794}
795
796/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 * Execute a function by "name".
798 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100799 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 * Returns FAIL if not found without an error message.
801 */
802 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100803call_by_name(
804 char_u *name,
805 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100806 ectx_T *ectx,
807 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808{
809 ufunc_T *ufunc;
810
811 if (builtin_function(name, -1))
812 {
813 int func_idx = find_internal_func(name);
814
815 if (func_idx < 0)
816 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200817 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818 return FAIL;
819 return call_bfunc(func_idx, argcount, ectx);
820 }
821
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200822 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200823
824 if (ufunc == NULL)
825 {
826 int called_emsg_before = called_emsg;
827
828 if (script_autoload(name, TRUE))
829 // loaded a package, search for the function again
830 ufunc = find_func(name, FALSE, NULL);
831 if (vim9_aborting(called_emsg_before))
832 return FAIL; // bail out if loading the script caused an error
833 }
834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100836 {
837 if (ufunc->uf_arg_types != NULL)
838 {
839 int i;
840 typval_T *argv = STACK_TV_BOT(0) - argcount;
841
842 // The function can change at runtime, check that the argument
843 // types are correct.
844 for (i = 0; i < argcount; ++i)
845 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100846 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100847
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100848 if (i < ufunc->uf_args.ga_len)
849 type = ufunc->uf_arg_types[i];
850 else if (ufunc->uf_va_type != NULL)
851 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100852 if (type != NULL && check_typval_arg_type(type,
853 &argv[i], i + 1) == FAIL)
854 return FAIL;
855 }
856 }
857
Bram Moolenaar4c137212021-04-19 16:48:48 +0200858 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860
861 return FAIL;
862}
863
864 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100865call_partial(
866 typval_T *tv,
867 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100868 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200870 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200871 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100873 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874
875 if (tv->v_type == VAR_PARTIAL)
876 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200877 partial_T *pt = tv->vval.v_partial;
878 int i;
879
880 if (pt->pt_argc > 0)
881 {
882 // Make space for arguments from the partial, shift the "argcount"
883 // arguments up.
884 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
885 return FAIL;
886 for (i = 1; i <= argcount; ++i)
887 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
888 ectx->ec_stack.ga_len += pt->pt_argc;
889 argcount += pt->pt_argc;
890
891 // copy the arguments from the partial onto the stack
892 for (i = 0; i < pt->pt_argc; ++i)
893 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895
896 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200897 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 name = pt->pt_name;
900 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200901 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200903 if (name != NULL)
904 {
905 char_u fname_buf[FLEN_FIXED + 1];
906 char_u *tofree = NULL;
907 int error = FCERR_NONE;
908 char_u *fname;
909
910 // May need to translate <SNR>123_ to K_SNR.
911 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
912 if (error != FCERR_NONE)
913 res = FAIL;
914 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200915 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200916 vim_free(tofree);
917 }
918
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100919 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 {
921 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200922 semsg(_(e_unknownfunc),
923 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 return FAIL;
925 }
926 return OK;
927}
928
929/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200930 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
931 * TRUE.
932 */
933 static int
934error_if_locked(int lock, char *error)
935{
936 if (lock & (VAR_LOCKED | VAR_FIXED))
937 {
938 emsg(_(error));
939 return TRUE;
940 }
941 return FALSE;
942}
943
944/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100945 * Give an error if "tv" is not a number and return FAIL.
946 */
947 static int
948check_for_number(typval_T *tv)
949{
950 if (tv->v_type != VAR_NUMBER)
951 {
952 semsg(_(e_expected_str_but_got_str),
953 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
954 return FAIL;
955 }
956 return OK;
957}
958
959/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100960 * Store "tv" in variable "name".
961 * This is for s: and g: variables.
962 */
963 static void
964store_var(char_u *name, typval_T *tv)
965{
966 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200967 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100968
Bram Moolenaard877a572021-04-01 19:42:48 +0200969 if (tv->v_lock)
970 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100971 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +0200972 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100973 restore_funccal();
974}
975
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100976/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100977 * Convert "tv" to a string.
978 * Return FAIL if not allowed.
979 */
980 static int
981do_2string(typval_T *tv, int is_2string_any)
982{
983 if (tv->v_type != VAR_STRING)
984 {
985 char_u *str;
986
987 if (is_2string_any)
988 {
989 switch (tv->v_type)
990 {
991 case VAR_SPECIAL:
992 case VAR_BOOL:
993 case VAR_NUMBER:
994 case VAR_FLOAT:
995 case VAR_BLOB: break;
996 default: to_string_error(tv->v_type);
997 return FAIL;
998 }
999 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001000 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001001 clear_tv(tv);
1002 tv->v_type = VAR_STRING;
1003 tv->vval.v_string = str;
1004 }
1005 return OK;
1006}
1007
1008/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001009 * When the value of "sv" is a null list of dict, allocate it.
1010 */
1011 static void
1012allocate_if_null(typval_T *tv)
1013{
1014 switch (tv->v_type)
1015 {
1016 case VAR_LIST:
1017 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001018 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001019 break;
1020 case VAR_DICT:
1021 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001022 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001023 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001024 case VAR_BLOB:
1025 if (tv->vval.v_blob == NULL)
1026 (void)rettv_blob_alloc(tv);
1027 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001028 default:
1029 break;
1030 }
1031}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001032
Bram Moolenaare7525c52021-01-09 13:20:37 +01001033/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001034 * Return the character "str[index]" where "index" is the character index,
1035 * including composing characters.
1036 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001037 */
1038 char_u *
1039char_from_string(char_u *str, varnumber_T index)
1040{
1041 size_t nbyte = 0;
1042 varnumber_T nchar = index;
1043 size_t slen;
1044
1045 if (str == NULL)
1046 return NULL;
1047 slen = STRLEN(str);
1048
Bram Moolenaarff871402021-03-26 13:34:05 +01001049 // Do the same as for a list: a negative index counts from the end.
1050 // Optimization to check the first byte to be below 0x80 (and no composing
1051 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001052 if (index < 0)
1053 {
1054 int clen = 0;
1055
1056 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001057 {
1058 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1059 ++nbyte;
1060 else if (enc_utf8)
1061 nbyte += utfc_ptr2len(str + nbyte);
1062 else
1063 nbyte += mb_ptr2len(str + nbyte);
1064 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001065 nchar = clen + index;
1066 if (nchar < 0)
1067 // unlike list: index out of range results in empty string
1068 return NULL;
1069 }
1070
1071 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001072 {
1073 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1074 ++nbyte;
1075 else if (enc_utf8)
1076 nbyte += utfc_ptr2len(str + nbyte);
1077 else
1078 nbyte += mb_ptr2len(str + nbyte);
1079 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001080 if (nbyte >= slen)
1081 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001082 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001083}
1084
1085/*
1086 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001087 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001088 * If going over the end return "str_len".
1089 * If "idx" is negative count from the end, -1 is the last character.
1090 * When going over the start return -1.
1091 */
1092 static long
1093char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1094{
1095 varnumber_T nchar = idx;
1096 size_t nbyte = 0;
1097
1098 if (nchar >= 0)
1099 {
1100 while (nchar > 0 && nbyte < str_len)
1101 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001102 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001103 --nchar;
1104 }
1105 }
1106 else
1107 {
1108 nbyte = str_len;
1109 while (nchar < 0 && nbyte > 0)
1110 {
1111 --nbyte;
1112 nbyte -= mb_head_off(str, str + nbyte);
1113 ++nchar;
1114 }
1115 if (nchar < 0)
1116 return -1;
1117 }
1118 return (long)nbyte;
1119}
1120
1121/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001122 * Return the slice "str[first : last]" using character indexes. Composing
1123 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001124 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001125 * Return NULL when the result is empty.
1126 */
1127 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001128string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001129{
1130 long start_byte, end_byte;
1131 size_t slen;
1132
1133 if (str == NULL)
1134 return NULL;
1135 slen = STRLEN(str);
1136 start_byte = char_idx2byte(str, slen, first);
1137 if (start_byte < 0)
1138 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001139 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001140 end_byte = (long)slen;
1141 else
1142 {
1143 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001144 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001145 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001146 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001147 }
1148
1149 if (start_byte >= (long)slen || end_byte <= start_byte)
1150 return NULL;
1151 return vim_strnsave(str + start_byte, end_byte - start_byte);
1152}
1153
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001154 static svar_T *
1155get_script_svar(scriptref_T *sref, ectx_T *ectx)
1156{
1157 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1159 + ectx->ec_dfunc_idx;
1160 svar_T *sv;
1161
1162 if (sref->sref_seq != si->sn_script_seq)
1163 {
1164 // The script was reloaded after the function was
1165 // compiled, the script_idx may not be valid.
1166 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1167 dfunc->df_ufunc->uf_name_exp);
1168 return NULL;
1169 }
1170 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1171 if (!equal_type(sv->sv_type, sref->sref_type))
1172 {
1173 emsg(_(e_script_variable_type_changed));
1174 return NULL;
1175 }
1176 return sv;
1177}
1178
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001179/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 * Execute a function by "name".
1181 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001182 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 */
1184 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001185call_eval_func(
1186 char_u *name,
1187 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001188 ectx_T *ectx,
1189 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190{
Bram Moolenaared677f52020-08-12 16:38:10 +02001191 int called_emsg_before = called_emsg;
1192 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193
Bram Moolenaar4c137212021-04-19 16:48:48 +02001194 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001195 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001197 dictitem_T *v;
1198
1199 v = find_var(name, NULL, FALSE);
1200 if (v == NULL)
1201 {
1202 semsg(_(e_unknownfunc), name);
1203 return FAIL;
1204 }
1205 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1206 {
1207 semsg(_(e_unknownfunc), name);
1208 return FAIL;
1209 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001210 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001212 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213}
1214
1215/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001216 * When a function reference is used, fill a partial with the information
1217 * needed, especially when it is used as a closure.
1218 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001219 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001220fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1221{
1222 pt->pt_func = ufunc;
1223 pt->pt_refcount = 1;
1224
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001225 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001226 {
1227 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1228 + ectx->ec_dfunc_idx;
1229
1230 // The closure needs to find arguments and local
1231 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001232 pt->pt_outer.out_stack = &ectx->ec_stack;
1233 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1234 pt->pt_outer.out_up = ectx->ec_outer;
1235 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001236
1237 // If this function returns and the closure is still
1238 // being used, we need to make a copy of the context
1239 // (arguments and local variables). Store a reference
1240 // to the partial so we can handle that.
1241 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1242 {
1243 vim_free(pt);
1244 return FAIL;
1245 }
1246 // Extra variable keeps the count of closures created
1247 // in the current function call.
1248 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1249 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1250
1251 ((partial_T **)ectx->ec_funcrefs.ga_data)
1252 [ectx->ec_funcrefs.ga_len] = pt;
1253 ++pt->pt_refcount;
1254 ++ectx->ec_funcrefs.ga_len;
1255 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001256 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001257 return OK;
1258}
1259
Bram Moolenaar4c137212021-04-19 16:48:48 +02001260// used for substitute_instr
1261typedef struct subs_expr_S {
1262 ectx_T *subs_ectx;
1263 isn_T *subs_instr;
1264 int subs_status;
1265} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266
1267// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001268#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269
1270// Get pointer to item at the bottom of the stack, -1 is the bottom.
1271#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001272#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 +01001273
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001274// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001275#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 +01001276
Bram Moolenaar4c137212021-04-19 16:48:48 +02001277/*
1278 * Execute instructions in execution context "ectx".
1279 * Return OK or FAIL;
1280 */
1281 static int
1282exec_instructions(ectx_T *ectx)
1283{
1284 int breakcheck_count = 0;
1285 typval_T *tv;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001286
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001287 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001288 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 for (;;)
1291 {
1292 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001293
Bram Moolenaar270d0382020-05-15 21:42:53 +02001294 if (++breakcheck_count >= 100)
1295 {
1296 line_breakcheck();
1297 breakcheck_count = 0;
1298 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001299 if (got_int)
1300 {
1301 // Turn CTRL-C into an exception.
1302 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001303 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001304 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001305 did_throw = TRUE;
1306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307
Bram Moolenaara26b9702020-04-18 19:53:28 +02001308 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1309 {
1310 // Turn an error message into an exception.
1311 did_emsg = FALSE;
1312 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001313 return FAIL;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001314 did_throw = TRUE;
1315 *msg_list = NULL;
1316 }
1317
Bram Moolenaar4c137212021-04-19 16:48:48 +02001318 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001320 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001321 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322
1323 // An exception jumps to the first catch, finally, or returns from
1324 // the current function.
1325 if (trystack->ga_len > 0)
1326 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001327 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 {
1329 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001330 ectx->ec_in_catch = TRUE;
1331 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 }
1333 else
1334 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001335 // Not inside try or need to return from current functions.
1336 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001337 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1338 return FAIL;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001339 tv = STACK_TV_BOT(0);
1340 tv->v_type = VAR_NUMBER;
1341 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001342 ++ectx->ec_stack.ga_len;
1343 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001345 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001346 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001347 if (handle_closure_in_use(ectx, FALSE) == FAIL)
1348 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 goto done;
1350 }
1351
Bram Moolenaar4c137212021-04-19 16:48:48 +02001352 if (func_return(ectx) == FAIL)
1353 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 }
1355 continue;
1356 }
1357
Bram Moolenaar4c137212021-04-19 16:48:48 +02001358 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 switch (iptr->isn_type)
1360 {
1361 // execute Ex command line
1362 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001363 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001364 source_cookie_T cookie;
1365
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001366 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001367 // Pass getsourceline to get an error for a missing ":end"
1368 // command.
1369 CLEAR_FIELD(cookie);
1370 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1371 if (do_cmdline(iptr->isn_arg.string,
1372 getsourceline, &cookie,
1373 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1374 == FAIL
1375 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001376 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 break;
1379
Bram Moolenaar4c137212021-04-19 16:48:48 +02001380 // execute :substitute with an expression
1381 case ISN_SUBSTITUTE:
1382 {
1383 subs_T *subs = &iptr->isn_arg.subs;
1384 source_cookie_T cookie;
1385 struct subs_expr_S *save_instr = substitute_instr;
1386 struct subs_expr_S subs_instr;
1387 int res;
1388
1389 subs_instr.subs_ectx = ectx;
1390 subs_instr.subs_instr = subs->subs_instr;
1391 subs_instr.subs_status = OK;
1392 substitute_instr = &subs_instr;
1393
1394 SOURCING_LNUM = iptr->isn_lnum;
1395 // This is very much like ISN_EXEC
1396 CLEAR_FIELD(cookie);
1397 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1398 res = do_cmdline(subs->subs_cmd,
1399 getsourceline, &cookie,
1400 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1401 substitute_instr = save_instr;
1402
1403 if (res == FAIL || did_emsg
1404 || subs_instr.subs_status == FAIL)
1405 goto on_error;
1406 }
1407 break;
1408
1409 case ISN_FINISH:
1410 goto done;
1411
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001412 // execute Ex command from pieces on the stack
1413 case ISN_EXECCONCAT:
1414 {
1415 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001416 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001417 int pass;
1418 int i;
1419 char_u *cmd = NULL;
1420 char_u *str;
1421
1422 for (pass = 1; pass <= 2; ++pass)
1423 {
1424 for (i = 0; i < count; ++i)
1425 {
1426 tv = STACK_TV_BOT(i - count);
1427 str = tv->vval.v_string;
1428 if (str != NULL && *str != NUL)
1429 {
1430 if (pass == 2)
1431 STRCPY(cmd + len, str);
1432 len += STRLEN(str);
1433 }
1434 if (pass == 2)
1435 clear_tv(tv);
1436 }
1437 if (pass == 1)
1438 {
1439 cmd = alloc(len + 1);
1440 if (cmd == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001441 return FAIL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001442 len = 0;
1443 }
1444 }
1445
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001446 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001447 do_cmdline_cmd(cmd);
1448 vim_free(cmd);
1449 }
1450 break;
1451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 // execute :echo {string} ...
1453 case ISN_ECHO:
1454 {
1455 int count = iptr->isn_arg.echo.echo_count;
1456 int atstart = TRUE;
1457 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001458 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459
1460 for (idx = 0; idx < count; ++idx)
1461 {
1462 tv = STACK_TV_BOT(idx - count);
1463 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1464 &atstart, &needclr);
1465 clear_tv(tv);
1466 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001467 if (needclr)
1468 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001469 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 }
1471 break;
1472
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001473 // :execute {string} ...
1474 // :echomsg {string} ...
1475 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001476 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001477 case ISN_ECHOMSG:
1478 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001479 {
1480 int count = iptr->isn_arg.number;
1481 garray_T ga;
1482 char_u buf[NUMBUFLEN];
1483 char_u *p;
1484 int len;
1485 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001486 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001487
1488 ga_init2(&ga, 1, 80);
1489 for (idx = 0; idx < count; ++idx)
1490 {
1491 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001492 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001493 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001494 if (tv->v_type == VAR_CHANNEL
1495 || tv->v_type == VAR_JOB)
1496 {
1497 SOURCING_LNUM = iptr->isn_lnum;
1498 emsg(_(e_inval_string));
1499 break;
1500 }
1501 else
1502 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001503 }
1504 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001505 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001506
1507 len = (int)STRLEN(p);
1508 if (ga_grow(&ga, len + 2) == FAIL)
1509 failed = TRUE;
1510 else
1511 {
1512 if (ga.ga_len > 0)
1513 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1514 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1515 ga.ga_len += len;
1516 }
1517 clear_tv(tv);
1518 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001519 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001520 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001521 {
1522 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001523 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001524 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001525
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001526 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001527 {
1528 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001529 {
1530 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001531 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001532 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001533 {
1534 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001535 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001536 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001537 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001538 else
1539 {
1540 msg_sb_eol();
1541 if (iptr->isn_type == ISN_ECHOMSG)
1542 {
1543 msg_attr(ga.ga_data, echo_attr);
1544 out_flush();
1545 }
1546 else
1547 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001548 SOURCING_LNUM = iptr->isn_lnum;
1549 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001550 }
1551 }
1552 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001553 ga_clear(&ga);
1554 }
1555 break;
1556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 // load local variable or argument
1558 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001559 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1560 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001562 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 break;
1564
1565 // load v: variable
1566 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001567 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1568 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001570 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 break;
1572
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001573 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 case ISN_LOADSCRIPT:
1575 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001576 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 svar_T *sv;
1578
Bram Moolenaar4c137212021-04-19 16:48:48 +02001579 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001580 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001581 return FAIL;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001582 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001583 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1584 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001586 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 }
1588 break;
1589
1590 // load s: variable in old script
1591 case ISN_LOADS:
1592 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001593 hashtab_T *ht = &SCRIPT_VARS(
1594 iptr->isn_arg.loadstore.ls_sid);
1595 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 if (di == NULL)
1599 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001600 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001601 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001602 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 }
1604 else
1605 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001606 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1607 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001609 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 }
1611 }
1612 break;
1613
Bram Moolenaard3aac292020-04-19 14:32:17 +02001614 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001616 case ISN_LOADB:
1617 case ISN_LOADW:
1618 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001620 dictitem_T *di = NULL;
1621 hashtab_T *ht = NULL;
1622 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001623
Bram Moolenaard3aac292020-04-19 14:32:17 +02001624 switch (iptr->isn_type)
1625 {
1626 case ISN_LOADG:
1627 ht = get_globvar_ht();
1628 namespace = 'g';
1629 break;
1630 case ISN_LOADB:
1631 ht = &curbuf->b_vars->dv_hashtab;
1632 namespace = 'b';
1633 break;
1634 case ISN_LOADW:
1635 ht = &curwin->w_vars->dv_hashtab;
1636 namespace = 'w';
1637 break;
1638 case ISN_LOADT:
1639 ht = &curtab->tp_vars->dv_hashtab;
1640 namespace = 't';
1641 break;
1642 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001643 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001644 }
1645 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 if (di == NULL)
1648 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001649 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001650 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001651 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001652 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 }
1654 else
1655 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001656 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1657 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001659 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 }
1661 }
1662 break;
1663
Bram Moolenaar03290b82020-12-19 16:30:44 +01001664 // load autoload variable
1665 case ISN_LOADAUTO:
1666 {
1667 char_u *name = iptr->isn_arg.string;
1668
Bram Moolenaar4c137212021-04-19 16:48:48 +02001669 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1670 return FAIL;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001671 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001672 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001673 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001674 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001675 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001676 }
1677 break;
1678
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001679 // load g:/b:/w:/t: namespace
1680 case ISN_LOADGDICT:
1681 case ISN_LOADBDICT:
1682 case ISN_LOADWDICT:
1683 case ISN_LOADTDICT:
1684 {
1685 dict_T *d = NULL;
1686
1687 switch (iptr->isn_type)
1688 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001689 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1690 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1691 case ISN_LOADWDICT: d = curwin->w_vars; break;
1692 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001693 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001694 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001695 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001696 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1697 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001698 tv = STACK_TV_BOT(0);
1699 tv->v_type = VAR_DICT;
1700 tv->v_lock = 0;
1701 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001702 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001703 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001704 }
1705 break;
1706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 // load &option
1708 case ISN_LOADOPT:
1709 {
1710 typval_T optval;
1711 char_u *name = iptr->isn_arg.string;
1712
Bram Moolenaara8c17702020-04-01 21:17:24 +02001713 // This is not expected to fail, name is checked during
1714 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001715 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1716 return FAIL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001717 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001718 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001720 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 }
1722 break;
1723
1724 // load $ENV
1725 case ISN_LOADENV:
1726 {
1727 typval_T optval;
1728 char_u *name = iptr->isn_arg.string;
1729
Bram Moolenaar4c137212021-04-19 16:48:48 +02001730 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1731 return FAIL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001732 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001733 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001735 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 }
1737 break;
1738
1739 // load @register
1740 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001741 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1742 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 tv = STACK_TV_BOT(0);
1744 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001745 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001746 // This may result in NULL, which should be equivalent to an
1747 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 tv->vval.v_string = get_reg_contents(
1749 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001750 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 break;
1752
1753 // store local variable
1754 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001755 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 tv = STACK_TV_VAR(iptr->isn_arg.number);
1757 clear_tv(tv);
1758 *tv = *STACK_TV_BOT(0);
1759 break;
1760
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001761 // store s: variable in old script
1762 case ISN_STORES:
1763 {
1764 hashtab_T *ht = &SCRIPT_VARS(
1765 iptr->isn_arg.loadstore.ls_sid);
1766 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001767 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001768
Bram Moolenaar4c137212021-04-19 16:48:48 +02001769 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001770 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001771 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001772 else
1773 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001774 SOURCING_LNUM = iptr->isn_lnum;
1775 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001776 {
1777 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001778 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001779 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001780 clear_tv(&di->di_tv);
1781 di->di_tv = *STACK_TV_BOT(0);
1782 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001783 }
1784 break;
1785
1786 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 case ISN_STORESCRIPT:
1788 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001789 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1790 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791
Bram Moolenaar4c137212021-04-19 16:48:48 +02001792 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001793 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001794 return FAIL;
1795 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001796
1797 // "const" and "final" are checked at compile time, locking
1798 // the value needs to be checked here.
1799 SOURCING_LNUM = iptr->isn_lnum;
1800 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001801 {
1802 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001803 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001804 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 clear_tv(sv->sv_tv);
1807 *sv->sv_tv = *STACK_TV_BOT(0);
1808 }
1809 break;
1810
1811 // store option
1812 case ISN_STOREOPT:
1813 {
1814 long n = 0;
1815 char_u *s = NULL;
1816 char *msg;
1817
Bram Moolenaar4c137212021-04-19 16:48:48 +02001818 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 tv = STACK_TV_BOT(0);
1820 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001821 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001823 if (s == NULL)
1824 s = (char_u *)"";
1825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001827 // must be VAR_NUMBER, CHECKTYPE makes sure
1828 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1830 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001831 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 if (msg != NULL)
1833 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001834 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001836 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 }
1839 break;
1840
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001841 // store $ENV
1842 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001843 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001844 tv = STACK_TV_BOT(0);
1845 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1846 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001847 break;
1848
1849 // store @r
1850 case ISN_STOREREG:
1851 {
1852 int reg = iptr->isn_arg.number;
1853
Bram Moolenaar4c137212021-04-19 16:48:48 +02001854 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001855 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001856 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001857 tv_get_string(tv), -1, FALSE);
1858 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001859 }
1860 break;
1861
1862 // store v: variable
1863 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001864 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001865 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1866 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001867 // should not happen, type is checked when compiling
1868 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001869 break;
1870
Bram Moolenaard3aac292020-04-19 14:32:17 +02001871 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001873 case ISN_STOREB:
1874 case ISN_STOREW:
1875 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001877 dictitem_T *di;
1878 hashtab_T *ht;
1879 char_u *name = iptr->isn_arg.string + 2;
1880
Bram Moolenaard3aac292020-04-19 14:32:17 +02001881 switch (iptr->isn_type)
1882 {
1883 case ISN_STOREG:
1884 ht = get_globvar_ht();
1885 break;
1886 case ISN_STOREB:
1887 ht = &curbuf->b_vars->dv_hashtab;
1888 break;
1889 case ISN_STOREW:
1890 ht = &curwin->w_vars->dv_hashtab;
1891 break;
1892 case ISN_STORET:
1893 ht = &curtab->tp_vars->dv_hashtab;
1894 break;
1895 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001896 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898
Bram Moolenaar4c137212021-04-19 16:48:48 +02001899 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001900 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001902 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 else
1904 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001905 SOURCING_LNUM = iptr->isn_lnum;
1906 if (var_check_permission(di, name) == FAIL)
1907 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 clear_tv(&di->di_tv);
1909 di->di_tv = *STACK_TV_BOT(0);
1910 }
1911 }
1912 break;
1913
Bram Moolenaar03290b82020-12-19 16:30:44 +01001914 // store an autoload variable
1915 case ISN_STOREAUTO:
1916 SOURCING_LNUM = iptr->isn_lnum;
1917 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1918 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001919 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001920 break;
1921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 // store number in local variable
1923 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001924 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 clear_tv(tv);
1926 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001927 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 break;
1929
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001930 // store value in list or dict variable
1931 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001932 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001933 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001934 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001935 typval_T *tv_dest = STACK_TV_BOT(-1);
1936 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001937
Bram Moolenaar752fc692021-01-04 21:57:11 +01001938 // Stack contains:
1939 // -3 value to be stored
1940 // -2 index
1941 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001942 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001943 SOURCING_LNUM = iptr->isn_lnum;
1944 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001945 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001946 dest_type = tv_dest->v_type;
1947 if (dest_type == VAR_DICT)
1948 status = do_2string(tv_idx, TRUE);
1949 else if (dest_type == VAR_LIST
1950 && tv_idx->v_type != VAR_NUMBER)
1951 {
1952 emsg(_(e_number_exp));
1953 status = FAIL;
1954 }
1955 }
1956 else if (dest_type != tv_dest->v_type)
1957 {
1958 // just in case, should be OK
1959 semsg(_(e_expected_str_but_got_str),
1960 vartype_name(dest_type),
1961 vartype_name(tv_dest->v_type));
1962 status = FAIL;
1963 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001964
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001965 if (status == OK && dest_type == VAR_LIST)
1966 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001967 long lidx = (long)tv_idx->vval.v_number;
1968 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001969
1970 if (list == NULL)
1971 {
1972 emsg(_(e_list_not_set));
1973 goto on_error;
1974 }
1975 if (lidx < 0 && list->lv_len + lidx >= 0)
1976 // negative index is relative to the end
1977 lidx = list->lv_len + lidx;
1978 if (lidx < 0 || lidx > list->lv_len)
1979 {
1980 semsg(_(e_listidx), lidx);
1981 goto on_error;
1982 }
1983 if (lidx < list->lv_len)
1984 {
1985 listitem_T *li = list_find(list, lidx);
1986
1987 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001988 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001989 goto on_error;
1990 // overwrite existing list item
1991 clear_tv(&li->li_tv);
1992 li->li_tv = *tv;
1993 }
1994 else
1995 {
1996 if (error_if_locked(list->lv_lock,
1997 e_cannot_change_list))
1998 goto on_error;
1999 // append to list, only fails when out of memory
2000 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002001 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002002 clear_tv(tv);
2003 }
2004 }
2005 else if (status == OK && dest_type == VAR_DICT)
2006 {
2007 char_u *key = tv_idx->vval.v_string;
2008 dict_T *dict = tv_dest->vval.v_dict;
2009 dictitem_T *di;
2010
2011 SOURCING_LNUM = iptr->isn_lnum;
2012 if (dict == NULL)
2013 {
2014 emsg(_(e_dictionary_not_set));
2015 goto on_error;
2016 }
2017 if (key == NULL)
2018 key = (char_u *)"";
2019 di = dict_find(dict, key, -1);
2020 if (di != NULL)
2021 {
2022 if (error_if_locked(di->di_tv.v_lock,
2023 e_cannot_change_dict_item))
2024 goto on_error;
2025 // overwrite existing value
2026 clear_tv(&di->di_tv);
2027 di->di_tv = *tv;
2028 }
2029 else
2030 {
2031 if (error_if_locked(dict->dv_lock,
2032 e_cannot_change_dict))
2033 goto on_error;
2034 // add to dict, only fails when out of memory
2035 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002036 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002037 clear_tv(tv);
2038 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002039 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002040 else if (status == OK && dest_type == VAR_BLOB)
2041 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002042 long lidx = (long)tv_idx->vval.v_number;
2043 blob_T *blob = tv_dest->vval.v_blob;
2044 varnumber_T nr;
2045 int error = FALSE;
2046 int len;
2047
2048 if (blob == NULL)
2049 {
2050 emsg(_(e_blob_not_set));
2051 goto on_error;
2052 }
2053 len = blob_len(blob);
2054 if (lidx < 0 && len + lidx >= 0)
2055 // negative index is relative to the end
2056 lidx = len + lidx;
2057
2058 // Can add one byte at the end.
2059 if (lidx < 0 || lidx > len)
2060 {
2061 semsg(_(e_blobidx), lidx);
2062 goto on_error;
2063 }
2064 if (value_check_lock(blob->bv_lock,
2065 (char_u *)"blob", FALSE))
2066 goto on_error;
2067 nr = tv_get_number_chk(tv, &error);
2068 if (error)
2069 goto on_error;
2070 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002071 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002072 else
2073 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002074 status = FAIL;
2075 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002076 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002077
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002078 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002079 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002080 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002081 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002082 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002083 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002084 goto on_error;
2085 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002086 }
2087 break;
2088
Bram Moolenaar68452172021-04-12 21:21:02 +02002089 // store value in blob range
2090 case ISN_STORERANGE:
2091 {
2092 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2093 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2094 typval_T *tv_dest = STACK_TV_BOT(-1);
2095 int status = OK;
2096
2097 // Stack contains:
2098 // -4 value to be stored
2099 // -3 first index or "none"
2100 // -2 second index or "none"
2101 // -1 destination blob
2102 tv = STACK_TV_BOT(-4);
2103 if (tv_dest->v_type != VAR_BLOB)
2104 {
2105 status = FAIL;
2106 emsg(_(e_blob_required));
2107 }
2108 else
2109 {
2110 varnumber_T n1;
2111 varnumber_T n2;
2112 int error = FALSE;
2113
2114 n1 = tv_get_number_chk(tv_idx1, &error);
2115 if (error)
2116 status = FAIL;
2117 else
2118 {
2119 if (tv_idx2->v_type == VAR_SPECIAL
2120 && tv_idx2->vval.v_number == VVAL_NONE)
2121 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2122 else
2123 n2 = tv_get_number_chk(tv_idx2, &error);
2124 if (error)
2125 status = FAIL;
2126 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002127 {
2128 long bloblen = blob_len(tv_dest->vval.v_blob);
2129
2130 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002131 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002132 || check_blob_range(bloblen,
2133 n1, n2, FALSE) == FAIL)
2134 status = FAIL;
2135 else
2136 status = blob_set_range(
2137 tv_dest->vval.v_blob, n1, n2, tv);
2138 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002139 }
2140 }
2141
2142 clear_tv(tv_idx1);
2143 clear_tv(tv_idx2);
2144 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002145 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002146 clear_tv(tv);
2147
2148 if (status == FAIL)
2149 goto on_error;
2150 }
2151 break;
2152
Bram Moolenaar0186e582021-01-10 18:33:11 +01002153 // load or store variable or argument from outer scope
2154 case ISN_LOADOUTER:
2155 case ISN_STOREOUTER:
2156 {
2157 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002158 outer_T *outer = ectx->ec_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002159
2160 while (depth > 1 && outer != NULL)
2161 {
2162 outer = outer->out_up;
2163 --depth;
2164 }
2165 if (outer == NULL)
2166 {
2167 SOURCING_LNUM = iptr->isn_lnum;
2168 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002169 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002170 }
2171 tv = ((typval_T *)outer->out_stack->ga_data)
2172 + outer->out_frame_idx + STACK_FRAME_SIZE
2173 + iptr->isn_arg.outer.outer_idx;
2174 if (iptr->isn_type == ISN_LOADOUTER)
2175 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002176 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2177 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002178 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002179 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002180 }
2181 else
2182 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002183 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002184 clear_tv(tv);
2185 *tv = *STACK_TV_BOT(0);
2186 }
2187 }
2188 break;
2189
Bram Moolenaar752fc692021-01-04 21:57:11 +01002190 // unlet item in list or dict variable
2191 case ISN_UNLETINDEX:
2192 {
2193 typval_T *tv_idx = STACK_TV_BOT(-2);
2194 typval_T *tv_dest = STACK_TV_BOT(-1);
2195 int status = OK;
2196
2197 // Stack contains:
2198 // -2 index
2199 // -1 dict or list
2200 if (tv_dest->v_type == VAR_DICT)
2201 {
2202 // unlet a dict item, index must be a string
2203 if (tv_idx->v_type != VAR_STRING)
2204 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002206 semsg(_(e_expected_str_but_got_str),
2207 vartype_name(VAR_STRING),
2208 vartype_name(tv_idx->v_type));
2209 status = FAIL;
2210 }
2211 else
2212 {
2213 dict_T *d = tv_dest->vval.v_dict;
2214 char_u *key = tv_idx->vval.v_string;
2215 dictitem_T *di = NULL;
2216
2217 if (key == NULL)
2218 key = (char_u *)"";
2219 if (d != NULL)
2220 di = dict_find(d, key, (int)STRLEN(key));
2221 if (di == NULL)
2222 {
2223 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002224 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002225 semsg(_(e_dictkey), key);
2226 status = FAIL;
2227 }
2228 else
2229 {
2230 // TODO: check for dict or item locked
2231 dictitem_remove(d, di);
2232 }
2233 }
2234 }
2235 else if (tv_dest->v_type == VAR_LIST)
2236 {
2237 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002238 SOURCING_LNUM = iptr->isn_lnum;
2239 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002240 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002241 status = FAIL;
2242 }
2243 else
2244 {
2245 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002246 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002247 listitem_T *li = NULL;
2248
2249 li = list_find(l, n);
2250 if (li == NULL)
2251 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002252 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002253 semsg(_(e_listidx), n);
2254 status = FAIL;
2255 }
2256 else
2257 // TODO: check for list or item locked
2258 listitem_remove(l, li);
2259 }
2260 }
2261 else
2262 {
2263 status = FAIL;
2264 semsg(_(e_cannot_index_str),
2265 vartype_name(tv_dest->v_type));
2266 }
2267
2268 clear_tv(tv_idx);
2269 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002270 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002271 if (status == FAIL)
2272 goto on_error;
2273 }
2274 break;
2275
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002276 // unlet range of items in list variable
2277 case ISN_UNLETRANGE:
2278 {
2279 // Stack contains:
2280 // -3 index1
2281 // -2 index2
2282 // -1 dict or list
2283 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2284 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2285 typval_T *tv_dest = STACK_TV_BOT(-1);
2286 int status = OK;
2287
2288 if (tv_dest->v_type == VAR_LIST)
2289 {
2290 // indexes must be a number
2291 SOURCING_LNUM = iptr->isn_lnum;
2292 if (check_for_number(tv_idx1) == FAIL
2293 || check_for_number(tv_idx2) == FAIL)
2294 {
2295 status = FAIL;
2296 }
2297 else
2298 {
2299 list_T *l = tv_dest->vval.v_list;
2300 long n1 = (long)tv_idx1->vval.v_number;
2301 long n2 = (long)tv_idx2->vval.v_number;
2302 listitem_T *li;
2303
2304 li = list_find_index(l, &n1);
2305 if (li == NULL
2306 || list_unlet_range(l, li, NULL, n1,
2307 TRUE, n2) == FAIL)
2308 status = FAIL;
2309 }
2310 }
2311 else
2312 {
2313 status = FAIL;
2314 SOURCING_LNUM = iptr->isn_lnum;
2315 semsg(_(e_cannot_index_str),
2316 vartype_name(tv_dest->v_type));
2317 }
2318
2319 clear_tv(tv_idx1);
2320 clear_tv(tv_idx2);
2321 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002322 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002323 if (status == FAIL)
2324 goto on_error;
2325 }
2326 break;
2327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 // push constant
2329 case ISN_PUSHNR:
2330 case ISN_PUSHBOOL:
2331 case ISN_PUSHSPEC:
2332 case ISN_PUSHF:
2333 case ISN_PUSHS:
2334 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002335 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002336 case ISN_PUSHCHANNEL:
2337 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002338 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2339 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002341 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002342 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 switch (iptr->isn_type)
2344 {
2345 case ISN_PUSHNR:
2346 tv->v_type = VAR_NUMBER;
2347 tv->vval.v_number = iptr->isn_arg.number;
2348 break;
2349 case ISN_PUSHBOOL:
2350 tv->v_type = VAR_BOOL;
2351 tv->vval.v_number = iptr->isn_arg.number;
2352 break;
2353 case ISN_PUSHSPEC:
2354 tv->v_type = VAR_SPECIAL;
2355 tv->vval.v_number = iptr->isn_arg.number;
2356 break;
2357#ifdef FEAT_FLOAT
2358 case ISN_PUSHF:
2359 tv->v_type = VAR_FLOAT;
2360 tv->vval.v_float = iptr->isn_arg.fnumber;
2361 break;
2362#endif
2363 case ISN_PUSHBLOB:
2364 blob_copy(iptr->isn_arg.blob, tv);
2365 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002366 case ISN_PUSHFUNC:
2367 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002368 if (iptr->isn_arg.string == NULL)
2369 tv->vval.v_string = NULL;
2370 else
2371 tv->vval.v_string =
2372 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002373 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002374 case ISN_PUSHCHANNEL:
2375#ifdef FEAT_JOB_CHANNEL
2376 tv->v_type = VAR_CHANNEL;
2377 tv->vval.v_channel = iptr->isn_arg.channel;
2378 if (tv->vval.v_channel != NULL)
2379 ++tv->vval.v_channel->ch_refcount;
2380#endif
2381 break;
2382 case ISN_PUSHJOB:
2383#ifdef FEAT_JOB_CHANNEL
2384 tv->v_type = VAR_JOB;
2385 tv->vval.v_job = iptr->isn_arg.job;
2386 if (tv->vval.v_job != NULL)
2387 ++tv->vval.v_job->jv_refcount;
2388#endif
2389 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 default:
2391 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002392 tv->vval.v_string = vim_strsave(
2393 iptr->isn_arg.string == NULL
2394 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 }
2396 break;
2397
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002398 case ISN_UNLET:
2399 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2400 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002401 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002402 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002403 case ISN_UNLETENV:
2404 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2405 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002406
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002407 case ISN_LOCKCONST:
2408 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2409 break;
2410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 // create a list from items on the stack; uses a single allocation
2412 // for the list header and the items
2413 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002414 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
2415 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 break;
2417
2418 // create a dict from items on the stack
2419 case ISN_NEWDICT:
2420 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002421 int count = iptr->isn_arg.number;
2422 dict_T *dict = dict_alloc();
2423 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002424 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002425 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426
2427 if (dict == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002428 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 for (idx = 0; idx < count; ++idx)
2430 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002431 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002433 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002434 key = tv->vval.v_string == NULL
2435 ? (char_u *)"" : tv->vval.v_string;
2436 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002437 if (item != NULL)
2438 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002439 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002440 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002441 dict_unref(dict);
2442 goto on_error;
2443 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002444 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 clear_tv(tv);
2446 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002447 {
2448 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002449 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2452 item->di_tv.v_lock = 0;
2453 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002454 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002455 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002456 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002457 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002458 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 }
2460
2461 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002462 ectx->ec_stack.ga_len -= 2 * count - 1;
2463 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2464 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002466 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 tv = STACK_TV_BOT(-1);
2468 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002469 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 tv->vval.v_dict = dict;
2471 ++dict->dv_refcount;
2472 }
2473 break;
2474
2475 // call a :def function
2476 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002477 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002478 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2479 NULL,
2480 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002481 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002482 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 break;
2484
2485 // call a builtin function
2486 case ISN_BCALL:
2487 SOURCING_LNUM = iptr->isn_lnum;
2488 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2489 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002490 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002491 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 break;
2493
2494 // call a funcref or partial
2495 case ISN_PCALL:
2496 {
2497 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2498 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002499 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500
2501 SOURCING_LNUM = iptr->isn_lnum;
2502 if (pfunc->cpf_top)
2503 {
2504 // funcref is above the arguments
2505 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2506 }
2507 else
2508 {
2509 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002510 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002511 partial_tv = *STACK_TV_BOT(0);
2512 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002514 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002515 if (tv == &partial_tv)
2516 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002518 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 }
2520 break;
2521
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002522 case ISN_PCALL_END:
2523 // PCALL finished, arguments have been consumed and replaced by
2524 // the return value. Now clear the funcref from the stack,
2525 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002526 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002527 clear_tv(STACK_TV_BOT(-1));
2528 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2529 break;
2530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 // call a user defined function or funcref/partial
2532 case ISN_UCALL:
2533 {
2534 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2535
2536 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002537 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002538 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002539 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 }
2541 break;
2542
2543 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002544 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002545 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2546 return FAIL;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002547 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002548 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002549 tv->v_type = VAR_NUMBER;
2550 tv->vval.v_number = 0;
2551 tv->v_lock = 0;
2552 // FALLTHROUGH
2553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 case ISN_RETURN:
2555 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002556 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002557 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002558
2559 if (trystack->ga_len > 0)
2560 trycmd = ((trycmd_T *)trystack->ga_data)
2561 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002562 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002563 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002565 // jump to ":finally" or ":endtry"
2566 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002567 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002568 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002569 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 trycmd->tcd_return = TRUE;
2571 }
2572 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002573 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 }
2575 break;
2576
2577 // push a function reference to a compiled function
2578 case ISN_FUNCREF:
2579 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002580 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2581 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2582 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 if (pt == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002585 return FAIL;
2586 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002587 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002588 vim_free(pt);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002589 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002590 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002591 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002592 ectx) == FAIL)
2593 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002596 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 tv->vval.v_partial = pt;
2598 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002599 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 }
2601 break;
2602
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002603 // Create a global function from a lambda.
2604 case ISN_NEWFUNC:
2605 {
2606 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2607
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002608 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002609 ectx) == FAIL)
2610 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002611 }
2612 break;
2613
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002614 // List functions
2615 case ISN_DEF:
2616 if (iptr->isn_arg.string == NULL)
2617 list_functions(NULL);
2618 else
2619 {
2620 exarg_T ea;
2621
2622 CLEAR_FIELD(ea);
2623 ea.cmd = ea.arg = iptr->isn_arg.string;
2624 define_function(&ea, NULL);
2625 }
2626 break;
2627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 // jump if a condition is met
2629 case ISN_JUMP:
2630 {
2631 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002632 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 int jump = TRUE;
2634
2635 if (when != JUMP_ALWAYS)
2636 {
2637 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002638 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002639 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002640 || when == JUMP_IF_COND_TRUE)
2641 {
2642 SOURCING_LNUM = iptr->isn_lnum;
2643 jump = tv_get_bool_chk(tv, &error);
2644 if (error)
2645 goto on_error;
2646 }
2647 else
2648 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002650 || when == JUMP_AND_KEEP_IF_FALSE
2651 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002653 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 {
2655 // drop the value from the stack
2656 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002657 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 }
2659 }
2660 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002661 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 }
2663 break;
2664
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002665 // Jump if an argument with a default value was already set and not
2666 // v:none.
2667 case ISN_JUMP_IF_ARG_SET:
2668 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2669 if (tv->v_type != VAR_UNKNOWN
2670 && !(tv->v_type == VAR_SPECIAL
2671 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002672 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002673 break;
2674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 // top of a for loop
2676 case ISN_FOR:
2677 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002678 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 typval_T *idxtv =
2680 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2681
Bram Moolenaar4c137212021-04-19 16:48:48 +02002682 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2683 return FAIL;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002684 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002685 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002686 list_T *list = ltv->vval.v_list;
2687
2688 // push the next item from the list
2689 ++idxtv->vval.v_number;
2690 if (list == NULL
2691 || idxtv->vval.v_number >= list->lv_len)
2692 {
2693 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002694 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2695 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002696 }
2697 else if (list->lv_first == &range_list_item)
2698 {
2699 // non-materialized range() list
2700 tv = STACK_TV_BOT(0);
2701 tv->v_type = VAR_NUMBER;
2702 tv->v_lock = 0;
2703 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002705 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002706 }
2707 else
2708 {
2709 listitem_T *li = list_find(list,
2710 idxtv->vval.v_number);
2711
2712 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002713 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002714 }
2715 }
2716 else if (ltv->v_type == VAR_STRING)
2717 {
2718 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002719
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002720 // The index is for the last byte of the previous
2721 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002722 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002723 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002724 {
2725 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002726 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2727 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002728 }
2729 else
2730 {
2731 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2732
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002733 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002734 tv = STACK_TV_BOT(0);
2735 tv->v_type = VAR_STRING;
2736 tv->vval.v_string = vim_strnsave(
2737 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002738 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002739 idxtv->vval.v_number += clen - 1;
2740 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002742 else if (ltv->v_type == VAR_BLOB)
2743 {
2744 blob_T *blob = ltv->vval.v_blob;
2745
2746 // When we get here the first time make a copy of the
2747 // blob, so that the iteration still works when it is
2748 // changed.
2749 if (idxtv->vval.v_number == -1 && blob != NULL)
2750 {
2751 blob_copy(blob, ltv);
2752 blob_unref(blob);
2753 blob = ltv->vval.v_blob;
2754 }
2755
2756 // The index is for the previous byte.
2757 ++idxtv->vval.v_number;
2758 if (blob == NULL
2759 || idxtv->vval.v_number >= blob_len(blob))
2760 {
2761 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002762 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2763 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002764 }
2765 else
2766 {
2767 // Push the next byte from the blob.
2768 tv = STACK_TV_BOT(0);
2769 tv->v_type = VAR_NUMBER;
2770 tv->vval.v_number = blob_get(blob,
2771 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002772 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002773 }
2774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 else
2776 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002777 semsg(_(e_for_loop_on_str_not_supported),
2778 vartype_name(ltv->v_type));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002779 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 }
2781 }
2782 break;
2783
2784 // start of ":try" block
2785 case ISN_TRY:
2786 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002787 trycmd_T *trycmd = NULL;
2788
Bram Moolenaar4c137212021-04-19 16:48:48 +02002789 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
2790 return FAIL;
2791 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
2792 + ectx->ec_trystack.ga_len;
2793 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002795 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002796 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
2797 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002798 trycmd->tcd_catch_idx =
2799 iptr->isn_arg.try.try_ref->try_catch;
2800 trycmd->tcd_finally_idx =
2801 iptr->isn_arg.try.try_ref->try_finally;
2802 trycmd->tcd_endtry_idx =
2803 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 }
2805 break;
2806
2807 case ISN_PUSHEXC:
2808 if (current_exception == NULL)
2809 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002810 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002812 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002814 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2815 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002817 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002819 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 tv->vval.v_string = vim_strsave(
2821 (char_u *)current_exception->value);
2822 break;
2823
2824 case ISN_CATCH:
2825 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002826 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827
Bram Moolenaar4c137212021-04-19 16:48:48 +02002828 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 if (trystack->ga_len > 0)
2830 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002831 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 + trystack->ga_len - 1;
2833 trycmd->tcd_caught = TRUE;
2834 }
2835 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002836 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 catch_exception(current_exception);
2838 }
2839 break;
2840
Bram Moolenaarc150c092021-02-13 15:02:46 +01002841 case ISN_TRYCONT:
2842 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002843 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002844 trycont_T *trycont = &iptr->isn_arg.trycont;
2845 int i;
2846 trycmd_T *trycmd;
2847 int iidx = trycont->tct_where;
2848
2849 if (trystack->ga_len < trycont->tct_levels)
2850 {
2851 siemsg("TRYCONT: expected %d levels, found %d",
2852 trycont->tct_levels, trystack->ga_len);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002853 return FAIL;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002854 }
2855 // Make :endtry jump to any outer try block and the last
2856 // :endtry inside the loop to the loop start.
2857 for (i = trycont->tct_levels; i > 0; --i)
2858 {
2859 trycmd = ((trycmd_T *)trystack->ga_data)
2860 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002861 // Add one to tcd_cont to be able to jump to
2862 // instruction with index zero.
2863 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002864 iidx = trycmd->tcd_finally_idx == 0
2865 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002866 }
2867 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02002868 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002869 }
2870 break;
2871
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002872 case ISN_FINALLY:
2873 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002874 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002875 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2876 + trystack->ga_len - 1;
2877
2878 // Reset the index to avoid a return statement jumps here
2879 // again.
2880 trycmd->tcd_finally_idx = 0;
2881 break;
2882 }
2883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 // end of ":try" block
2885 case ISN_ENDTRY:
2886 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002887 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888
2889 if (trystack->ga_len > 0)
2890 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002891 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 --trystack->ga_len;
2894 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002895 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 trycmd = ((trycmd_T *)trystack->ga_data)
2897 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002898 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 {
2900 // discard the exception
2901 if (caught_stack == current_exception)
2902 caught_stack = caught_stack->caught;
2903 discard_current_exception();
2904 }
2905
2906 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002907 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002908
Bram Moolenaar4c137212021-04-19 16:48:48 +02002909 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01002910 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002911 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002912 clear_tv(STACK_TV_BOT(0));
2913 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002914 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002915 // handling :continue: jump to outer try block or
2916 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02002917 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 }
2919 }
2920 break;
2921
2922 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002923 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002924 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002925
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002926 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2927 {
2928 // throwing an exception while using "silent!" causes
2929 // the function to abort but not display an error.
2930 tv = STACK_TV_BOT(-1);
2931 clear_tv(tv);
2932 tv->v_type = VAR_NUMBER;
2933 tv->vval.v_number = 0;
2934 goto done;
2935 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002936 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002937 tv = STACK_TV_BOT(0);
2938 if (tv->vval.v_string == NULL
2939 || *skipwhite(tv->vval.v_string) == NUL)
2940 {
2941 vim_free(tv->vval.v_string);
2942 SOURCING_LNUM = iptr->isn_lnum;
2943 emsg(_(e_throw_with_empty_string));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002944 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002945 }
2946
2947 // Inside a "catch" we need to first discard the caught
2948 // exception.
2949 if (trystack->ga_len > 0)
2950 {
2951 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2952 + trystack->ga_len - 1;
2953 if (trycmd->tcd_caught && current_exception != NULL)
2954 {
2955 // discard the exception
2956 if (caught_stack == current_exception)
2957 caught_stack = caught_stack->caught;
2958 discard_current_exception();
2959 trycmd->tcd_caught = FALSE;
2960 }
2961 }
2962
2963 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2964 == FAIL)
2965 {
2966 vim_free(tv->vval.v_string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002967 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002968 }
2969 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 break;
2972
2973 // compare with special values
2974 case ISN_COMPAREBOOL:
2975 case ISN_COMPARESPECIAL:
2976 {
2977 typval_T *tv1 = STACK_TV_BOT(-2);
2978 typval_T *tv2 = STACK_TV_BOT(-1);
2979 varnumber_T arg1 = tv1->vval.v_number;
2980 varnumber_T arg2 = tv2->vval.v_number;
2981 int res;
2982
2983 switch (iptr->isn_arg.op.op_type)
2984 {
2985 case EXPR_EQUAL: res = arg1 == arg2; break;
2986 case EXPR_NEQUAL: res = arg1 != arg2; break;
2987 default: res = 0; break;
2988 }
2989
Bram Moolenaar4c137212021-04-19 16:48:48 +02002990 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 tv1->v_type = VAR_BOOL;
2992 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2993 }
2994 break;
2995
2996 // Operation with two number arguments
2997 case ISN_OPNR:
2998 case ISN_COMPARENR:
2999 {
3000 typval_T *tv1 = STACK_TV_BOT(-2);
3001 typval_T *tv2 = STACK_TV_BOT(-1);
3002 varnumber_T arg1 = tv1->vval.v_number;
3003 varnumber_T arg2 = tv2->vval.v_number;
3004 varnumber_T res;
3005
3006 switch (iptr->isn_arg.op.op_type)
3007 {
3008 case EXPR_MULT: res = arg1 * arg2; break;
3009 case EXPR_DIV: res = arg1 / arg2; break;
3010 case EXPR_REM: res = arg1 % arg2; break;
3011 case EXPR_SUB: res = arg1 - arg2; break;
3012 case EXPR_ADD: res = arg1 + arg2; break;
3013
3014 case EXPR_EQUAL: res = arg1 == arg2; break;
3015 case EXPR_NEQUAL: res = arg1 != arg2; break;
3016 case EXPR_GREATER: res = arg1 > arg2; break;
3017 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3018 case EXPR_SMALLER: res = arg1 < arg2; break;
3019 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3020 default: res = 0; break;
3021 }
3022
Bram Moolenaar4c137212021-04-19 16:48:48 +02003023 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 if (iptr->isn_type == ISN_COMPARENR)
3025 {
3026 tv1->v_type = VAR_BOOL;
3027 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3028 }
3029 else
3030 tv1->vval.v_number = res;
3031 }
3032 break;
3033
3034 // Computation with two float arguments
3035 case ISN_OPFLOAT:
3036 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003037#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 {
3039 typval_T *tv1 = STACK_TV_BOT(-2);
3040 typval_T *tv2 = STACK_TV_BOT(-1);
3041 float_T arg1 = tv1->vval.v_float;
3042 float_T arg2 = tv2->vval.v_float;
3043 float_T res = 0;
3044 int cmp = FALSE;
3045
3046 switch (iptr->isn_arg.op.op_type)
3047 {
3048 case EXPR_MULT: res = arg1 * arg2; break;
3049 case EXPR_DIV: res = arg1 / arg2; break;
3050 case EXPR_SUB: res = arg1 - arg2; break;
3051 case EXPR_ADD: res = arg1 + arg2; break;
3052
3053 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3054 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3055 case EXPR_GREATER: cmp = arg1 > arg2; break;
3056 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3057 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3058 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3059 default: cmp = 0; break;
3060 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003061 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 if (iptr->isn_type == ISN_COMPAREFLOAT)
3063 {
3064 tv1->v_type = VAR_BOOL;
3065 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3066 }
3067 else
3068 tv1->vval.v_float = res;
3069 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003070#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 break;
3072
3073 case ISN_COMPARELIST:
3074 {
3075 typval_T *tv1 = STACK_TV_BOT(-2);
3076 typval_T *tv2 = STACK_TV_BOT(-1);
3077 list_T *arg1 = tv1->vval.v_list;
3078 list_T *arg2 = tv2->vval.v_list;
3079 int cmp = FALSE;
3080 int ic = iptr->isn_arg.op.op_ic;
3081
3082 switch (iptr->isn_arg.op.op_type)
3083 {
3084 case EXPR_EQUAL: cmp =
3085 list_equal(arg1, arg2, ic, FALSE); break;
3086 case EXPR_NEQUAL: cmp =
3087 !list_equal(arg1, arg2, ic, FALSE); break;
3088 case EXPR_IS: cmp = arg1 == arg2; break;
3089 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3090 default: cmp = 0; break;
3091 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003092 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 clear_tv(tv1);
3094 clear_tv(tv2);
3095 tv1->v_type = VAR_BOOL;
3096 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3097 }
3098 break;
3099
3100 case ISN_COMPAREBLOB:
3101 {
3102 typval_T *tv1 = STACK_TV_BOT(-2);
3103 typval_T *tv2 = STACK_TV_BOT(-1);
3104 blob_T *arg1 = tv1->vval.v_blob;
3105 blob_T *arg2 = tv2->vval.v_blob;
3106 int cmp = FALSE;
3107
3108 switch (iptr->isn_arg.op.op_type)
3109 {
3110 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3111 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3112 case EXPR_IS: cmp = arg1 == arg2; break;
3113 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3114 default: cmp = 0; break;
3115 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003116 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 clear_tv(tv1);
3118 clear_tv(tv2);
3119 tv1->v_type = VAR_BOOL;
3120 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3121 }
3122 break;
3123
3124 // TODO: handle separately
3125 case ISN_COMPARESTRING:
3126 case ISN_COMPAREDICT:
3127 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 case ISN_COMPAREANY:
3129 {
3130 typval_T *tv1 = STACK_TV_BOT(-2);
3131 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003132 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 int ic = iptr->isn_arg.op.op_ic;
3134
Bram Moolenaareb26f432020-09-14 16:50:05 +02003135 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003136 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003138 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 }
3140 break;
3141
3142 case ISN_ADDLIST:
3143 case ISN_ADDBLOB:
3144 {
3145 typval_T *tv1 = STACK_TV_BOT(-2);
3146 typval_T *tv2 = STACK_TV_BOT(-1);
3147
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003148 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 if (iptr->isn_type == ISN_ADDLIST)
3150 eval_addlist(tv1, tv2);
3151 else
3152 eval_addblob(tv1, tv2);
3153 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003154 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 }
3156 break;
3157
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003158 case ISN_LISTAPPEND:
3159 {
3160 typval_T *tv1 = STACK_TV_BOT(-2);
3161 typval_T *tv2 = STACK_TV_BOT(-1);
3162 list_T *l = tv1->vval.v_list;
3163
3164 // add an item to a list
3165 if (l == NULL)
3166 {
3167 SOURCING_LNUM = iptr->isn_lnum;
3168 emsg(_(e_cannot_add_to_null_list));
3169 goto on_error;
3170 }
3171 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003172 return FAIL;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003173 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003174 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003175 }
3176 break;
3177
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003178 case ISN_BLOBAPPEND:
3179 {
3180 typval_T *tv1 = STACK_TV_BOT(-2);
3181 typval_T *tv2 = STACK_TV_BOT(-1);
3182 blob_T *b = tv1->vval.v_blob;
3183 int error = FALSE;
3184 varnumber_T n;
3185
3186 // add a number to a blob
3187 if (b == NULL)
3188 {
3189 SOURCING_LNUM = iptr->isn_lnum;
3190 emsg(_(e_cannot_add_to_null_blob));
3191 goto on_error;
3192 }
3193 n = tv_get_number_chk(tv2, &error);
3194 if (error)
3195 goto on_error;
3196 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003197 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003198 }
3199 break;
3200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 // Computation with two arguments of unknown type
3202 case ISN_OPANY:
3203 {
3204 typval_T *tv1 = STACK_TV_BOT(-2);
3205 typval_T *tv2 = STACK_TV_BOT(-1);
3206 varnumber_T n1, n2;
3207#ifdef FEAT_FLOAT
3208 float_T f1 = 0, f2 = 0;
3209#endif
3210 int error = FALSE;
3211
3212 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3213 {
3214 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3215 {
3216 eval_addlist(tv1, tv2);
3217 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003218 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 break;
3220 }
3221 else if (tv1->v_type == VAR_BLOB
3222 && tv2->v_type == VAR_BLOB)
3223 {
3224 eval_addblob(tv1, tv2);
3225 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003226 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 break;
3228 }
3229 }
3230#ifdef FEAT_FLOAT
3231 if (tv1->v_type == VAR_FLOAT)
3232 {
3233 f1 = tv1->vval.v_float;
3234 n1 = 0;
3235 }
3236 else
3237#endif
3238 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003239 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 n1 = tv_get_number_chk(tv1, &error);
3241 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003242 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243#ifdef FEAT_FLOAT
3244 if (tv2->v_type == VAR_FLOAT)
3245 f1 = n1;
3246#endif
3247 }
3248#ifdef FEAT_FLOAT
3249 if (tv2->v_type == VAR_FLOAT)
3250 {
3251 f2 = tv2->vval.v_float;
3252 n2 = 0;
3253 }
3254 else
3255#endif
3256 {
3257 n2 = tv_get_number_chk(tv2, &error);
3258 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003259 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260#ifdef FEAT_FLOAT
3261 if (tv1->v_type == VAR_FLOAT)
3262 f2 = n2;
3263#endif
3264 }
3265#ifdef FEAT_FLOAT
3266 // if there is a float on either side the result is a float
3267 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3268 {
3269 switch (iptr->isn_arg.op.op_type)
3270 {
3271 case EXPR_MULT: f1 = f1 * f2; break;
3272 case EXPR_DIV: f1 = f1 / f2; break;
3273 case EXPR_SUB: f1 = f1 - f2; break;
3274 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003275 default: SOURCING_LNUM = iptr->isn_lnum;
3276 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003277 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 }
3279 clear_tv(tv1);
3280 clear_tv(tv2);
3281 tv1->v_type = VAR_FLOAT;
3282 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003283 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 }
3285 else
3286#endif
3287 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003288 int failed = FALSE;
3289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 switch (iptr->isn_arg.op.op_type)
3291 {
3292 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003293 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3294 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003295 goto on_error;
3296 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 case EXPR_SUB: n1 = n1 - n2; break;
3298 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003299 default: n1 = num_modulus(n1, n2, &failed);
3300 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003301 goto on_error;
3302 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 }
3304 clear_tv(tv1);
3305 clear_tv(tv2);
3306 tv1->v_type = VAR_NUMBER;
3307 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003308 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 }
3310 }
3311 break;
3312
3313 case ISN_CONCAT:
3314 {
3315 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3316 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3317 char_u *res;
3318
3319 res = concat_str(str1, str2);
3320 clear_tv(STACK_TV_BOT(-2));
3321 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003322 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 STACK_TV_BOT(-1)->vval.v_string = res;
3324 }
3325 break;
3326
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003327 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003328 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003329 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003330 int is_slice = iptr->isn_type == ISN_STRSLICE;
3331 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003332 char_u *res;
3333
3334 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003335 // string slice: string is at stack-3, first index at
3336 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003337 if (is_slice)
3338 {
3339 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003340 n1 = tv->vval.v_number;
3341 }
3342
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003343 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003344 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003345
Bram Moolenaar4c137212021-04-19 16:48:48 +02003346 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003347 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003348 if (is_slice)
3349 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003350 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003351 else
3352 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003353 // single character (including composing characters).
3354 // If the index is too big or negative the result is
3355 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003356 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003357 vim_free(tv->vval.v_string);
3358 tv->vval.v_string = res;
3359 }
3360 break;
3361
3362 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003363 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003364 case ISN_BLOBINDEX:
3365 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003367 int is_slice = iptr->isn_type == ISN_LISTSLICE
3368 || iptr->isn_type == ISN_BLOBSLICE;
3369 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3370 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003371 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003372 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373
3374 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003375 // list slice: list is at stack-3, indexes at stack-2 and
3376 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003377 // Same for blob.
3378 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379
3380 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003381 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003383
3384 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 {
Bram Moolenaared591872020-08-15 22:14:53 +02003386 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003387 n1 = tv->vval.v_number;
3388 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 }
Bram Moolenaared591872020-08-15 22:14:53 +02003390
Bram Moolenaar4c137212021-04-19 16:48:48 +02003391 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003392 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003393 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003394 if (is_blob)
3395 {
3396 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3397 n1, n2, FALSE, tv) == FAIL)
3398 goto on_error;
3399 }
3400 else
3401 {
3402 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3403 n1, n2, FALSE, tv, TRUE) == FAIL)
3404 goto on_error;
3405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 }
3407 break;
3408
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003409 case ISN_ANYINDEX:
3410 case ISN_ANYSLICE:
3411 {
3412 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3413 typval_T *var1, *var2;
3414 int res;
3415
3416 // index: composite is at stack-2, index at stack-1
3417 // slice: composite is at stack-3, indexes at stack-2 and
3418 // stack-1
3419 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003420 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003421 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3422 goto on_error;
3423 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3424 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003425 res = eval_index_inner(tv, is_slice, var1, var2,
3426 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003427 clear_tv(var1);
3428 if (is_slice)
3429 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003430 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003431 if (res == FAIL)
3432 goto on_error;
3433 }
3434 break;
3435
Bram Moolenaar9af78762020-06-16 11:34:42 +02003436 case ISN_SLICE:
3437 {
3438 list_T *list;
3439 int count = iptr->isn_arg.number;
3440
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003441 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003442 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003443 list = tv->vval.v_list;
3444
3445 // no error for short list, expect it to be checked earlier
3446 if (list != NULL && list->lv_len >= count)
3447 {
3448 list_T *newlist = list_slice(list,
3449 count, list->lv_len - 1);
3450
3451 if (newlist != NULL)
3452 {
3453 list_unref(list);
3454 tv->vval.v_list = newlist;
3455 ++newlist->lv_refcount;
3456 }
3457 }
3458 }
3459 break;
3460
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003461 case ISN_GETITEM:
3462 {
3463 listitem_T *li;
3464 int index = iptr->isn_arg.number;
3465
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003466 // Get list item: list is at stack-1, push item.
3467 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003468 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003469 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003470
Bram Moolenaar4c137212021-04-19 16:48:48 +02003471 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3472 return FAIL;
3473 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003474 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003475
3476 // Useful when used in unpack assignment. Reset at
3477 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003478 ectx->ec_where.wt_index = index + 1;
3479 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003480 }
3481 break;
3482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 case ISN_MEMBER:
3484 {
3485 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003486 char_u *key;
3487 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003488 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003489
3490 // dict member: dict is at stack-2, key at stack-1
3491 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003492 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003493 dict = tv->vval.v_dict;
3494
3495 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003496 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003497 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003498 if (key == NULL)
3499 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003500
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003501 if ((di = dict_find(dict, key, -1)) == NULL)
3502 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003503 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003504 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003505
3506 // If :silent! is used we will continue, make sure the
3507 // stack contents makes sense.
3508 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003509 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003510 tv = STACK_TV_BOT(-1);
3511 clear_tv(tv);
3512 tv->v_type = VAR_NUMBER;
3513 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003514 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003515 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003516 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003517 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003518 // Clear the dict only after getting the item, to avoid
3519 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003520 tv = STACK_TV_BOT(-1);
3521 temp_tv = *tv;
3522 copy_tv(&di->di_tv, tv);
3523 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003524 }
3525 break;
3526
3527 // dict member with string key
3528 case ISN_STRINGMEMBER:
3529 {
3530 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003532 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533
3534 tv = STACK_TV_BOT(-1);
3535 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3536 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003537 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003539 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 }
3541 dict = tv->vval.v_dict;
3542
3543 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3544 == NULL)
3545 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003546 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003548 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003550 // Clear the dict after getting the item, to avoid that it
3551 // make the item invalid.
3552 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003554 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 }
3556 break;
3557
3558 case ISN_NEGATENR:
3559 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003560 if (tv->v_type != VAR_NUMBER
3561#ifdef FEAT_FLOAT
3562 && tv->v_type != VAR_FLOAT
3563#endif
3564 )
3565 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003566 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003567 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003568 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003569 }
3570#ifdef FEAT_FLOAT
3571 if (tv->v_type == VAR_FLOAT)
3572 tv->vval.v_float = -tv->vval.v_float;
3573 else
3574#endif
3575 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 break;
3577
3578 case ISN_CHECKNR:
3579 {
3580 int error = FALSE;
3581
3582 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003583 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003585 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 (void)tv_get_number_chk(tv, &error);
3587 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003588 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 }
3590 break;
3591
3592 case ISN_CHECKTYPE:
3593 {
3594 checktype_T *ct = &iptr->isn_arg.type;
3595
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003596 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003597 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003598 if (!ectx->ec_where.wt_variable)
3599 ectx->ec_where.wt_index = ct->ct_arg_idx;
3600 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3601 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003602 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003603 if (!ectx->ec_where.wt_variable)
3604 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003605
3606 // number 0 is FALSE, number 1 is TRUE
3607 if (tv->v_type == VAR_NUMBER
3608 && ct->ct_type->tt_type == VAR_BOOL
3609 && (tv->vval.v_number == 0
3610 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003612 tv->v_type = VAR_BOOL;
3613 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003614 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 }
3616 }
3617 break;
3618
Bram Moolenaar9af78762020-06-16 11:34:42 +02003619 case ISN_CHECKLEN:
3620 {
3621 int min_len = iptr->isn_arg.checklen.cl_min_len;
3622 list_T *list = NULL;
3623
3624 tv = STACK_TV_BOT(-1);
3625 if (tv->v_type == VAR_LIST)
3626 list = tv->vval.v_list;
3627 if (list == NULL || list->lv_len < min_len
3628 || (list->lv_len > min_len
3629 && !iptr->isn_arg.checklen.cl_more_OK))
3630 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003631 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003632 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003633 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003634 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003635 }
3636 }
3637 break;
3638
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003639 case ISN_SETTYPE:
3640 {
3641 checktype_T *ct = &iptr->isn_arg.type;
3642
3643 tv = STACK_TV_BOT(-1);
3644 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3645 {
3646 free_type(tv->vval.v_dict->dv_type);
3647 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3648 }
3649 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3650 {
3651 free_type(tv->vval.v_list->lv_type);
3652 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3653 }
3654 }
3655 break;
3656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003658 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 {
3660 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003661 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662
3663 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003664 if (iptr->isn_type == ISN_2BOOL)
3665 {
3666 n = tv2bool(tv);
3667 if (iptr->isn_arg.number) // invert
3668 n = !n;
3669 }
3670 else
3671 {
3672 SOURCING_LNUM = iptr->isn_lnum;
3673 n = tv_get_bool_chk(tv, &error);
3674 if (error)
3675 goto on_error;
3676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 clear_tv(tv);
3678 tv->v_type = VAR_BOOL;
3679 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3680 }
3681 break;
3682
3683 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003684 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003685 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003686 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3687 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3688 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 break;
3690
Bram Moolenaar08597872020-12-10 19:43:40 +01003691 case ISN_RANGE:
3692 {
3693 exarg_T ea;
3694 char *errormsg;
3695
Bram Moolenaarece0b872021-01-08 20:40:45 +01003696 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003697 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003698 ea.addr_type = ADDR_LINES;
3699 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003700 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003701 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003702 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003703
Bram Moolenaar4c137212021-04-19 16:48:48 +02003704 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3705 return FAIL;
3706 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003707 tv = STACK_TV_BOT(-1);
3708 tv->v_type = VAR_NUMBER;
3709 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003710 if (ea.addr_count == 0)
3711 tv->vval.v_number = curwin->w_cursor.lnum;
3712 else
3713 tv->vval.v_number = ea.line2;
3714 }
3715 break;
3716
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003717 case ISN_PUT:
3718 {
3719 int regname = iptr->isn_arg.put.put_regname;
3720 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3721 char_u *expr = NULL;
3722 int dir = FORWARD;
3723
Bram Moolenaar08597872020-12-10 19:43:40 +01003724 if (lnum < -2)
3725 {
3726 // line number was put on the stack by ISN_RANGE
3727 tv = STACK_TV_BOT(-1);
3728 curwin->w_cursor.lnum = tv->vval.v_number;
3729 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3730 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003731 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003732 }
3733 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003734 // :put! above cursor
3735 dir = BACKWARD;
3736 else if (lnum >= 0)
3737 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003738
3739 if (regname == '=')
3740 {
3741 tv = STACK_TV_BOT(-1);
3742 if (tv->v_type == VAR_STRING)
3743 expr = tv->vval.v_string;
3744 else
3745 {
3746 expr = typval2string(tv, TRUE); // allocates value
3747 clear_tv(tv);
3748 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003749 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003750 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003751 check_cursor();
3752 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3753 vim_free(expr);
3754 }
3755 break;
3756
Bram Moolenaar02194d22020-10-24 23:08:38 +02003757 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003758 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3759 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3760 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3761 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003762 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3763 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003764 break;
3765
Bram Moolenaar02194d22020-10-24 23:08:38 +02003766 case ISN_CMDMOD_REV:
3767 // filter regprog is owned by the instruction, don't free it
3768 cmdmod.cmod_filter_regmatch.regprog = NULL;
3769 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003770 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
3771 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003772 break;
3773
Bram Moolenaar792f7862020-11-23 08:31:18 +01003774 case ISN_UNPACK:
3775 {
3776 int count = iptr->isn_arg.unpack.unp_count;
3777 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3778 list_T *l;
3779 listitem_T *li;
3780 int i;
3781
3782 // Check there is a valid list to unpack.
3783 tv = STACK_TV_BOT(-1);
3784 if (tv->v_type != VAR_LIST)
3785 {
3786 SOURCING_LNUM = iptr->isn_lnum;
3787 emsg(_(e_for_argument_must_be_sequence_of_lists));
3788 goto on_error;
3789 }
3790 l = tv->vval.v_list;
3791 if (l == NULL
3792 || l->lv_len < (semicolon ? count - 1 : count))
3793 {
3794 SOURCING_LNUM = iptr->isn_lnum;
3795 emsg(_(e_list_value_does_not_have_enough_items));
3796 goto on_error;
3797 }
3798 else if (!semicolon && l->lv_len > count)
3799 {
3800 SOURCING_LNUM = iptr->isn_lnum;
3801 emsg(_(e_list_value_has_more_items_than_targets));
3802 goto on_error;
3803 }
3804
3805 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003806 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
3807 return FAIL;
3808 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003809
3810 // Variable after semicolon gets a list with the remaining
3811 // items.
3812 if (semicolon)
3813 {
3814 list_T *rem_list =
3815 list_alloc_with_items(l->lv_len - count + 1);
3816
3817 if (rem_list == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003818 return FAIL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003819 tv = STACK_TV_BOT(-count);
3820 tv->vval.v_list = rem_list;
3821 ++rem_list->lv_refcount;
3822 tv->v_lock = 0;
3823 li = l->lv_first;
3824 for (i = 0; i < count - 1; ++i)
3825 li = li->li_next;
3826 for (i = 0; li != NULL; ++i)
3827 {
3828 list_set_item(rem_list, i, &li->li_tv);
3829 li = li->li_next;
3830 }
3831 --count;
3832 }
3833
3834 // Produce the values in reverse order, first item last.
3835 li = l->lv_first;
3836 for (i = 0; i < count; ++i)
3837 {
3838 tv = STACK_TV_BOT(-i - 1);
3839 copy_tv(&li->li_tv, tv);
3840 li = li->li_next;
3841 }
3842
3843 list_unref(l);
3844 }
3845 break;
3846
Bram Moolenaarb2049902021-01-24 12:53:53 +01003847 case ISN_PROF_START:
3848 case ISN_PROF_END:
3849 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003850#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003851 funccall_T cookie;
3852 ufunc_T *cur_ufunc =
3853 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003854 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003855
3856 cookie.func = cur_ufunc;
3857 if (iptr->isn_type == ISN_PROF_START)
3858 {
3859 func_line_start(&cookie, iptr->isn_lnum);
3860 // if we get here the instruction is executed
3861 func_line_exec(&cookie);
3862 }
3863 else
3864 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003865#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003866 }
3867 break;
3868
Bram Moolenaar389df252020-07-09 21:20:47 +02003869 case ISN_SHUFFLE:
3870 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003871 typval_T tmp_tv;
3872 int item = iptr->isn_arg.shuffle.shfl_item;
3873 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003874
3875 tmp_tv = *STACK_TV_BOT(-item);
3876 for ( ; up > 0 && item > 1; --up)
3877 {
3878 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3879 --item;
3880 }
3881 *STACK_TV_BOT(-item) = tmp_tv;
3882 }
3883 break;
3884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003886 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003888 ectx->ec_where.wt_index = 0;
3889 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 break;
3891 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003892 continue;
3893
Bram Moolenaard032f342020-07-18 18:13:02 +02003894func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003895 // Restore previous function. If the frame pointer is where we started
3896 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003897 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003898 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003899
Bram Moolenaar4c137212021-04-19 16:48:48 +02003900 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003901 // only fails when out of memory
Bram Moolenaar4c137212021-04-19 16:48:48 +02003902 return FAIL;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003903 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003904
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003905on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003906 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003907 // If "emsg_silent" is set then ignore the error, unless it was set
3908 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003909 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003910 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003911 {
3912 // If a sequence of instructions causes an error while ":silent!"
3913 // was used, restore the stack length and jump ahead to restoring
3914 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003915 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003916 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003917 while (ectx->ec_stack.ga_len
3918 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003919 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003920 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003921 clear_tv(STACK_TV_BOT(0));
3922 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003923 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
3924 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003925 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003926 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003927 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003928on_fatal_error:
3929 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003930 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003931 if (trylevel <= ectx->ec_trylevel_at_start)
3932 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 }
3934
3935done:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003936 return OK;
3937}
3938
3939/*
3940 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
3941 * "substitute_instr".
3942 */
3943 char_u *
3944exe_substitute_instr(void)
3945{
3946 ectx_T *ectx = substitute_instr->subs_ectx;
3947 isn_T *save_instr = ectx->ec_instr;
3948 int save_iidx = ectx->ec_iidx;
3949 char_u *res;
3950
3951 ectx->ec_instr = substitute_instr->subs_instr;
3952 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02003953 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 typval_T *tv = STACK_TV_BOT(-1);
3955
3956 res = vim_strsave(tv_get_string(tv));
3957 --ectx->ec_stack.ga_len;
3958 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02003959 }
3960 else
3961 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003962 substitute_instr->subs_status = FAIL;
3963 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02003964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965
Bram Moolenaar4c137212021-04-19 16:48:48 +02003966 ectx->ec_instr = save_instr;
3967 ectx->ec_iidx = save_iidx;
3968
3969 return res;
3970}
3971
3972/*
3973 * Call a "def" function from old Vim script.
3974 * Return OK or FAIL.
3975 */
3976 int
3977call_def_function(
3978 ufunc_T *ufunc,
3979 int argc_arg, // nr of arguments
3980 typval_T *argv, // arguments
3981 partial_T *partial, // optional partial for context
3982 typval_T *rettv) // return value
3983{
3984 ectx_T ectx; // execution context
3985 int argc = argc_arg;
3986 typval_T *tv;
3987 int idx;
3988 int ret = FAIL;
3989 int defcount = ufunc->uf_args.ga_len - argc;
3990 sctx_T save_current_sctx = current_sctx;
3991 int did_emsg_before = did_emsg_cumul + did_emsg;
3992 int save_suppress_errthrow = suppress_errthrow;
3993 msglist_T **saved_msg_list = NULL;
3994 msglist_T *private_msg_list = NULL;
3995 int save_emsg_silent_def = emsg_silent_def;
3996 int save_did_emsg_def = did_emsg_def;
3997 int orig_funcdepth;
3998
3999// Get pointer to item in the stack.
4000#undef STACK_TV
4001#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4002
4003// Get pointer to item at the bottom of the stack, -1 is the bottom.
4004#undef STACK_TV_BOT
4005#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4006
4007// Get pointer to a local variable on the stack. Negative for arguments.
4008#undef STACK_TV_VAR
4009#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4010
4011 if (ufunc->uf_def_status == UF_NOT_COMPILED
4012 || ufunc->uf_def_status == UF_COMPILE_ERROR
4013 || (func_needs_compiling(ufunc, PROFILING(ufunc))
4014 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
4015 == FAIL))
4016 {
4017 if (did_emsg_cumul + did_emsg == did_emsg_before)
4018 semsg(_(e_function_is_not_compiled_str),
4019 printable_func_name(ufunc));
4020 return FAIL;
4021 }
4022
4023 {
4024 // Check the function was really compiled.
4025 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4026 + ufunc->uf_dfunc_idx;
4027 if (INSTRUCTIONS(dfunc) == NULL)
4028 {
4029 iemsg("using call_def_function() on not compiled function");
4030 return FAIL;
4031 }
4032 }
4033
4034 // If depth of calling is getting too high, don't execute the function.
4035 orig_funcdepth = funcdepth_get();
4036 if (funcdepth_increment() == FAIL)
4037 return FAIL;
4038
4039 CLEAR_FIELD(ectx);
4040 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4041 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4042 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4043 {
4044 funcdepth_decrement();
4045 return FAIL;
4046 }
4047 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4048 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4049 ectx.ec_did_emsg_before = did_emsg_before;
4050 ectx.ec_trylevel_at_start = trylevel;
4051
4052 idx = argc - ufunc->uf_args.ga_len;
4053 if (idx > 0 && ufunc->uf_va_name == NULL)
4054 {
4055 if (idx == 1)
4056 emsg(_(e_one_argument_too_many));
4057 else
4058 semsg(_(e_nr_arguments_too_many), idx);
4059 goto failed_early;
4060 }
4061
4062 // Put arguments on the stack, but no more than what the function expects.
4063 // A lambda can be called with more arguments than it uses.
4064 for (idx = 0; idx < argc
4065 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4066 ++idx)
4067 {
4068 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4069 && argv[idx].v_type == VAR_SPECIAL
4070 && argv[idx].vval.v_number == VVAL_NONE)
4071 {
4072 // Use the default value.
4073 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4074 }
4075 else
4076 {
4077 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4078 && check_typval_arg_type(
4079 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4080 goto failed_early;
4081 copy_tv(&argv[idx], STACK_TV_BOT(0));
4082 }
4083 ++ectx.ec_stack.ga_len;
4084 }
4085
4086 // Turn varargs into a list. Empty list if no args.
4087 if (ufunc->uf_va_name != NULL)
4088 {
4089 int vararg_count = argc - ufunc->uf_args.ga_len;
4090
4091 if (vararg_count < 0)
4092 vararg_count = 0;
4093 else
4094 argc -= vararg_count;
4095 if (exe_newlist(vararg_count, &ectx) == FAIL)
4096 goto failed_early;
4097
4098 // Check the type of the list items.
4099 tv = STACK_TV_BOT(-1);
4100 if (ufunc->uf_va_type != NULL
4101 && ufunc->uf_va_type != &t_list_any
4102 && ufunc->uf_va_type->tt_member != &t_any
4103 && tv->vval.v_list != NULL)
4104 {
4105 type_T *expected = ufunc->uf_va_type->tt_member;
4106 listitem_T *li = tv->vval.v_list->lv_first;
4107
4108 for (idx = 0; idx < vararg_count; ++idx)
4109 {
4110 if (check_typval_arg_type(expected, &li->li_tv,
4111 argc + idx + 1) == FAIL)
4112 goto failed_early;
4113 li = li->li_next;
4114 }
4115 }
4116
4117 if (defcount > 0)
4118 // Move varargs list to below missing default arguments.
4119 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4120 --ectx.ec_stack.ga_len;
4121 }
4122
4123 // Make space for omitted arguments, will store default value below.
4124 // Any varargs list goes after them.
4125 if (defcount > 0)
4126 for (idx = 0; idx < defcount; ++idx)
4127 {
4128 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4129 ++ectx.ec_stack.ga_len;
4130 }
4131 if (ufunc->uf_va_name != NULL)
4132 ++ectx.ec_stack.ga_len;
4133
4134 // Frame pointer points to just after arguments.
4135 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4136 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4137
4138 {
4139 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4140 + ufunc->uf_dfunc_idx;
4141 ufunc_T *base_ufunc = dfunc->df_ufunc;
4142
4143 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4144 // by copy_func().
4145 if (partial != NULL || base_ufunc->uf_partial != NULL)
4146 {
4147 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
4148 if (ectx.ec_outer == NULL)
4149 goto failed_early;
4150 if (partial != NULL)
4151 {
4152 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4153 {
4154 if (current_ectx->ec_outer != NULL)
4155 *ectx.ec_outer = *current_ectx->ec_outer;
4156 }
4157 else
4158 *ectx.ec_outer = partial->pt_outer;
4159 }
4160 else
4161 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
4162 ectx.ec_outer->out_up_is_copy = TRUE;
4163 }
4164 }
4165
4166 // dummy frame entries
4167 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4168 {
4169 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4170 ++ectx.ec_stack.ga_len;
4171 }
4172
4173 {
4174 // Reserve space for local variables and any closure reference count.
4175 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4176 + ufunc->uf_dfunc_idx;
4177
4178 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4179 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4180 ectx.ec_stack.ga_len += dfunc->df_varcount;
4181 if (dfunc->df_has_closure)
4182 {
4183 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4184 STACK_TV_VAR(idx)->vval.v_number = 0;
4185 ++ectx.ec_stack.ga_len;
4186 }
4187
4188 ectx.ec_instr = INSTRUCTIONS(dfunc);
4189 }
4190
4191 // Following errors are in the function, not the caller.
4192 // Commands behave like vim9script.
4193 estack_push_ufunc(ufunc, 1);
4194 current_sctx = ufunc->uf_script_ctx;
4195 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4196
4197 // Use a specific location for storing error messages to be converted to an
4198 // exception.
4199 saved_msg_list = msg_list;
4200 msg_list = &private_msg_list;
4201
4202 // Do turn errors into exceptions.
4203 suppress_errthrow = FALSE;
4204
4205 // Do not delete the function while executing it.
4206 ++ufunc->uf_calls;
4207
4208 // When ":silent!" was used before calling then we still abort the
4209 // function. If ":silent!" is used in the function then we don't.
4210 emsg_silent_def = emsg_silent;
4211 did_emsg_def = 0;
4212
4213 ectx.ec_where.wt_index = 0;
4214 ectx.ec_where.wt_variable = FALSE;
4215
4216 // Execute the instructions until done.
4217 ret = exec_instructions(&ectx);
4218 if (ret == OK)
4219 {
4220 // function finished, get result from the stack.
4221 if (ufunc->uf_ret_type == &t_void)
4222 {
4223 rettv->v_type = VAR_VOID;
4224 }
4225 else
4226 {
4227 tv = STACK_TV_BOT(-1);
4228 *rettv = *tv;
4229 tv->v_type = VAR_UNKNOWN;
4230 }
4231 }
4232
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004233 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004234 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4235 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004236
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004237 // Deal with any remaining closures, they may be in use somewhere.
4238 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004239 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004240 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004241 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4242 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004243
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004244 estack_pop();
4245 current_sctx = save_current_sctx;
4246
Bram Moolenaarc970e422021-03-17 15:03:04 +01004247 // TODO: when is it safe to delete the function if it is no longer used?
4248 --ufunc->uf_calls;
4249
Bram Moolenaar352134b2020-10-17 22:04:08 +02004250 if (*msg_list != NULL && saved_msg_list != NULL)
4251 {
4252 msglist_T **plist = saved_msg_list;
4253
4254 // Append entries from the current msg_list (uncaught exceptions) to
4255 // the saved msg_list.
4256 while (*plist != NULL)
4257 plist = &(*plist)->next;
4258
4259 *plist = *msg_list;
4260 }
4261 msg_list = saved_msg_list;
4262
Bram Moolenaar4c137212021-04-19 16:48:48 +02004263 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004264 {
4265 cmdmod.cmod_filter_regmatch.regprog = NULL;
4266 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004267 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004268 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004269 emsg_silent_def = save_emsg_silent_def;
4270 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004271
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004272failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004273 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4275 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004278 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004279
Bram Moolenaar0186e582021-01-10 18:33:11 +01004280 while (ectx.ec_outer != NULL)
4281 {
4282 outer_T *up = ectx.ec_outer->out_up_is_copy
4283 ? NULL : ectx.ec_outer->out_up;
4284
4285 vim_free(ectx.ec_outer);
4286 ectx.ec_outer = up;
4287 }
4288
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004289 // Not sure if this is necessary.
4290 suppress_errthrow = save_suppress_errthrow;
4291
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004292 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004293 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004294 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004295 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 return ret;
4297}
4298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004300 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4301 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4302 * "pfx" is prefixed to every line.
4303 */
4304 static void
4305list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4306{
4307 int line_idx = 0;
4308 int prev_current = 0;
4309 int current;
4310
4311 for (current = 0; current < instr_count; ++current)
4312 {
4313 isn_T *iptr = &instr[current];
4314 char *line;
4315
4316 if (ufunc != NULL)
4317 while (line_idx < iptr->isn_lnum
4318 && line_idx < ufunc->uf_lines.ga_len)
4319 {
4320 if (current > prev_current)
4321 {
4322 msg_puts("\n\n");
4323 prev_current = current;
4324 }
4325 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4326 if (line != NULL)
4327 msg(line);
4328 }
4329
4330 switch (iptr->isn_type)
4331 {
4332 case ISN_EXEC:
4333 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4334 break;
4335 case ISN_SUBSTITUTE:
4336 {
4337 subs_T *subs = &iptr->isn_arg.subs;
4338
4339 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4340 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4341 msg(" -------------");
4342 }
4343 break;
4344 case ISN_EXECCONCAT:
4345 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4346 (varnumber_T)iptr->isn_arg.number);
4347 break;
4348 case ISN_ECHO:
4349 {
4350 echo_T *echo = &iptr->isn_arg.echo;
4351
4352 smsg("%s%4d %s %d", pfx, current,
4353 echo->echo_with_white ? "ECHO" : "ECHON",
4354 echo->echo_count);
4355 }
4356 break;
4357 case ISN_EXECUTE:
4358 smsg("%s%4d EXECUTE %lld", pfx, current,
4359 (varnumber_T)(iptr->isn_arg.number));
4360 break;
4361 case ISN_ECHOMSG:
4362 smsg("%s%4d ECHOMSG %lld", pfx, current,
4363 (varnumber_T)(iptr->isn_arg.number));
4364 break;
4365 case ISN_ECHOERR:
4366 smsg("%s%4d ECHOERR %lld", pfx, current,
4367 (varnumber_T)(iptr->isn_arg.number));
4368 break;
4369 case ISN_LOAD:
4370 {
4371 if (iptr->isn_arg.number < 0)
4372 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4373 (varnumber_T)(iptr->isn_arg.number
4374 + STACK_FRAME_SIZE));
4375 else
4376 smsg("%s%4d LOAD $%lld", pfx, current,
4377 (varnumber_T)(iptr->isn_arg.number));
4378 }
4379 break;
4380 case ISN_LOADOUTER:
4381 {
4382 if (iptr->isn_arg.number < 0)
4383 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4384 iptr->isn_arg.outer.outer_depth,
4385 iptr->isn_arg.outer.outer_idx
4386 + STACK_FRAME_SIZE);
4387 else
4388 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4389 iptr->isn_arg.outer.outer_depth,
4390 iptr->isn_arg.outer.outer_idx);
4391 }
4392 break;
4393 case ISN_LOADV:
4394 smsg("%s%4d LOADV v:%s", pfx, current,
4395 get_vim_var_name(iptr->isn_arg.number));
4396 break;
4397 case ISN_LOADSCRIPT:
4398 {
4399 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4400 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4401 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4402 + sref->sref_idx;
4403
4404 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4405 sv->sv_name,
4406 sref->sref_idx,
4407 si->sn_name);
4408 }
4409 break;
4410 case ISN_LOADS:
4411 {
4412 scriptitem_T *si = SCRIPT_ITEM(
4413 iptr->isn_arg.loadstore.ls_sid);
4414
4415 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4416 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4417 }
4418 break;
4419 case ISN_LOADAUTO:
4420 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4421 break;
4422 case ISN_LOADG:
4423 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4424 break;
4425 case ISN_LOADB:
4426 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4427 break;
4428 case ISN_LOADW:
4429 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4430 break;
4431 case ISN_LOADT:
4432 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4433 break;
4434 case ISN_LOADGDICT:
4435 smsg("%s%4d LOAD g:", pfx, current);
4436 break;
4437 case ISN_LOADBDICT:
4438 smsg("%s%4d LOAD b:", pfx, current);
4439 break;
4440 case ISN_LOADWDICT:
4441 smsg("%s%4d LOAD w:", pfx, current);
4442 break;
4443 case ISN_LOADTDICT:
4444 smsg("%s%4d LOAD t:", pfx, current);
4445 break;
4446 case ISN_LOADOPT:
4447 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4448 break;
4449 case ISN_LOADENV:
4450 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4451 break;
4452 case ISN_LOADREG:
4453 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4454 break;
4455
4456 case ISN_STORE:
4457 if (iptr->isn_arg.number < 0)
4458 smsg("%s%4d STORE arg[%lld]", pfx, current,
4459 iptr->isn_arg.number + STACK_FRAME_SIZE);
4460 else
4461 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4462 break;
4463 case ISN_STOREOUTER:
4464 {
4465 if (iptr->isn_arg.number < 0)
4466 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4467 iptr->isn_arg.outer.outer_depth,
4468 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4469 else
4470 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4471 iptr->isn_arg.outer.outer_depth,
4472 iptr->isn_arg.outer.outer_idx);
4473 }
4474 break;
4475 case ISN_STOREV:
4476 smsg("%s%4d STOREV v:%s", pfx, current,
4477 get_vim_var_name(iptr->isn_arg.number));
4478 break;
4479 case ISN_STOREAUTO:
4480 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4481 break;
4482 case ISN_STOREG:
4483 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4484 break;
4485 case ISN_STOREB:
4486 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4487 break;
4488 case ISN_STOREW:
4489 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4490 break;
4491 case ISN_STORET:
4492 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4493 break;
4494 case ISN_STORES:
4495 {
4496 scriptitem_T *si = SCRIPT_ITEM(
4497 iptr->isn_arg.loadstore.ls_sid);
4498
4499 smsg("%s%4d STORES %s in %s", pfx, current,
4500 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4501 }
4502 break;
4503 case ISN_STORESCRIPT:
4504 {
4505 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4506 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4507 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4508 + sref->sref_idx;
4509
4510 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4511 sv->sv_name,
4512 sref->sref_idx,
4513 si->sn_name);
4514 }
4515 break;
4516 case ISN_STOREOPT:
4517 smsg("%s%4d STOREOPT &%s", pfx, current,
4518 iptr->isn_arg.storeopt.so_name);
4519 break;
4520 case ISN_STOREENV:
4521 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4522 break;
4523 case ISN_STOREREG:
4524 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4525 break;
4526 case ISN_STORENR:
4527 smsg("%s%4d STORE %lld in $%d", pfx, current,
4528 iptr->isn_arg.storenr.stnr_val,
4529 iptr->isn_arg.storenr.stnr_idx);
4530 break;
4531
4532 case ISN_STOREINDEX:
4533 smsg("%s%4d STOREINDEX %s", pfx, current,
4534 vartype_name(iptr->isn_arg.vartype));
4535 break;
4536
4537 case ISN_STORERANGE:
4538 smsg("%s%4d STORERANGE", pfx, current);
4539 break;
4540
4541 // constants
4542 case ISN_PUSHNR:
4543 smsg("%s%4d PUSHNR %lld", pfx, current,
4544 (varnumber_T)(iptr->isn_arg.number));
4545 break;
4546 case ISN_PUSHBOOL:
4547 case ISN_PUSHSPEC:
4548 smsg("%s%4d PUSH %s", pfx, current,
4549 get_var_special_name(iptr->isn_arg.number));
4550 break;
4551 case ISN_PUSHF:
4552#ifdef FEAT_FLOAT
4553 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4554#endif
4555 break;
4556 case ISN_PUSHS:
4557 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4558 break;
4559 case ISN_PUSHBLOB:
4560 {
4561 char_u *r;
4562 char_u numbuf[NUMBUFLEN];
4563 char_u *tofree;
4564
4565 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4566 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4567 vim_free(tofree);
4568 }
4569 break;
4570 case ISN_PUSHFUNC:
4571 {
4572 char *name = (char *)iptr->isn_arg.string;
4573
4574 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4575 name == NULL ? "[none]" : name);
4576 }
4577 break;
4578 case ISN_PUSHCHANNEL:
4579#ifdef FEAT_JOB_CHANNEL
4580 {
4581 channel_T *channel = iptr->isn_arg.channel;
4582
4583 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4584 channel == NULL ? 0 : channel->ch_id);
4585 }
4586#endif
4587 break;
4588 case ISN_PUSHJOB:
4589#ifdef FEAT_JOB_CHANNEL
4590 {
4591 typval_T tv;
4592 char_u *name;
4593
4594 tv.v_type = VAR_JOB;
4595 tv.vval.v_job = iptr->isn_arg.job;
4596 name = tv_get_string(&tv);
4597 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4598 }
4599#endif
4600 break;
4601 case ISN_PUSHEXC:
4602 smsg("%s%4d PUSH v:exception", pfx, current);
4603 break;
4604 case ISN_UNLET:
4605 smsg("%s%4d UNLET%s %s", pfx, current,
4606 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4607 iptr->isn_arg.unlet.ul_name);
4608 break;
4609 case ISN_UNLETENV:
4610 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4611 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4612 iptr->isn_arg.unlet.ul_name);
4613 break;
4614 case ISN_UNLETINDEX:
4615 smsg("%s%4d UNLETINDEX", pfx, current);
4616 break;
4617 case ISN_UNLETRANGE:
4618 smsg("%s%4d UNLETRANGE", pfx, current);
4619 break;
4620 case ISN_LOCKCONST:
4621 smsg("%s%4d LOCKCONST", pfx, current);
4622 break;
4623 case ISN_NEWLIST:
4624 smsg("%s%4d NEWLIST size %lld", pfx, current,
4625 (varnumber_T)(iptr->isn_arg.number));
4626 break;
4627 case ISN_NEWDICT:
4628 smsg("%s%4d NEWDICT size %lld", pfx, current,
4629 (varnumber_T)(iptr->isn_arg.number));
4630 break;
4631
4632 // function call
4633 case ISN_BCALL:
4634 {
4635 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4636
4637 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4638 internal_func_name(cbfunc->cbf_idx),
4639 cbfunc->cbf_argcount);
4640 }
4641 break;
4642 case ISN_DCALL:
4643 {
4644 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4645 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4646 + cdfunc->cdf_idx;
4647
4648 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
4649 df->df_ufunc->uf_name_exp != NULL
4650 ? df->df_ufunc->uf_name_exp
4651 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4652 }
4653 break;
4654 case ISN_UCALL:
4655 {
4656 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4657
4658 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
4659 cufunc->cuf_name, cufunc->cuf_argcount);
4660 }
4661 break;
4662 case ISN_PCALL:
4663 {
4664 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4665
4666 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
4667 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4668 }
4669 break;
4670 case ISN_PCALL_END:
4671 smsg("%s%4d PCALL end", pfx, current);
4672 break;
4673 case ISN_RETURN:
4674 smsg("%s%4d RETURN", pfx, current);
4675 break;
4676 case ISN_RETURN_ZERO:
4677 smsg("%s%4d RETURN 0", pfx, current);
4678 break;
4679 case ISN_FUNCREF:
4680 {
4681 funcref_T *funcref = &iptr->isn_arg.funcref;
4682 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4683 + funcref->fr_func;
4684
4685 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
4686 }
4687 break;
4688
4689 case ISN_NEWFUNC:
4690 {
4691 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4692
4693 smsg("%s%4d NEWFUNC %s %s", pfx, current,
4694 newfunc->nf_lambda, newfunc->nf_global);
4695 }
4696 break;
4697
4698 case ISN_DEF:
4699 {
4700 char_u *name = iptr->isn_arg.string;
4701
4702 smsg("%s%4d DEF %s", pfx, current,
4703 name == NULL ? (char_u *)"" : name);
4704 }
4705 break;
4706
4707 case ISN_JUMP:
4708 {
4709 char *when = "?";
4710
4711 switch (iptr->isn_arg.jump.jump_when)
4712 {
4713 case JUMP_ALWAYS:
4714 when = "JUMP";
4715 break;
4716 case JUMP_AND_KEEP_IF_TRUE:
4717 when = "JUMP_AND_KEEP_IF_TRUE";
4718 break;
4719 case JUMP_IF_FALSE:
4720 when = "JUMP_IF_FALSE";
4721 break;
4722 case JUMP_AND_KEEP_IF_FALSE:
4723 when = "JUMP_AND_KEEP_IF_FALSE";
4724 break;
4725 case JUMP_IF_COND_FALSE:
4726 when = "JUMP_IF_COND_FALSE";
4727 break;
4728 case JUMP_IF_COND_TRUE:
4729 when = "JUMP_IF_COND_TRUE";
4730 break;
4731 }
4732 smsg("%s%4d %s -> %d", pfx, current, when,
4733 iptr->isn_arg.jump.jump_where);
4734 }
4735 break;
4736
4737 case ISN_JUMP_IF_ARG_SET:
4738 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
4739 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4740 iptr->isn_arg.jump.jump_where);
4741 break;
4742
4743 case ISN_FOR:
4744 {
4745 forloop_T *forloop = &iptr->isn_arg.forloop;
4746
4747 smsg("%s%4d FOR $%d -> %d", pfx, current,
4748 forloop->for_idx, forloop->for_end);
4749 }
4750 break;
4751
4752 case ISN_TRY:
4753 {
4754 try_T *try = &iptr->isn_arg.try;
4755
4756 if (try->try_ref->try_finally == 0)
4757 smsg("%s%4d TRY catch -> %d, endtry -> %d",
4758 pfx, current,
4759 try->try_ref->try_catch,
4760 try->try_ref->try_endtry);
4761 else
4762 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4763 pfx, current,
4764 try->try_ref->try_catch,
4765 try->try_ref->try_finally,
4766 try->try_ref->try_endtry);
4767 }
4768 break;
4769 case ISN_CATCH:
4770 // TODO
4771 smsg("%s%4d CATCH", pfx, current);
4772 break;
4773 case ISN_TRYCONT:
4774 {
4775 trycont_T *trycont = &iptr->isn_arg.trycont;
4776
4777 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
4778 trycont->tct_levels,
4779 trycont->tct_levels == 1 ? "" : "s",
4780 trycont->tct_where);
4781 }
4782 break;
4783 case ISN_FINALLY:
4784 smsg("%s%4d FINALLY", pfx, current);
4785 break;
4786 case ISN_ENDTRY:
4787 smsg("%s%4d ENDTRY", pfx, current);
4788 break;
4789 case ISN_THROW:
4790 smsg("%s%4d THROW", pfx, current);
4791 break;
4792
4793 // expression operations on number
4794 case ISN_OPNR:
4795 case ISN_OPFLOAT:
4796 case ISN_OPANY:
4797 {
4798 char *what;
4799 char *ins;
4800
4801 switch (iptr->isn_arg.op.op_type)
4802 {
4803 case EXPR_MULT: what = "*"; break;
4804 case EXPR_DIV: what = "/"; break;
4805 case EXPR_REM: what = "%"; break;
4806 case EXPR_SUB: what = "-"; break;
4807 case EXPR_ADD: what = "+"; break;
4808 default: what = "???"; break;
4809 }
4810 switch (iptr->isn_type)
4811 {
4812 case ISN_OPNR: ins = "OPNR"; break;
4813 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4814 case ISN_OPANY: ins = "OPANY"; break;
4815 default: ins = "???"; break;
4816 }
4817 smsg("%s%4d %s %s", pfx, current, ins, what);
4818 }
4819 break;
4820
4821 case ISN_COMPAREBOOL:
4822 case ISN_COMPARESPECIAL:
4823 case ISN_COMPARENR:
4824 case ISN_COMPAREFLOAT:
4825 case ISN_COMPARESTRING:
4826 case ISN_COMPAREBLOB:
4827 case ISN_COMPARELIST:
4828 case ISN_COMPAREDICT:
4829 case ISN_COMPAREFUNC:
4830 case ISN_COMPAREANY:
4831 {
4832 char *p;
4833 char buf[10];
4834 char *type;
4835
4836 switch (iptr->isn_arg.op.op_type)
4837 {
4838 case EXPR_EQUAL: p = "=="; break;
4839 case EXPR_NEQUAL: p = "!="; break;
4840 case EXPR_GREATER: p = ">"; break;
4841 case EXPR_GEQUAL: p = ">="; break;
4842 case EXPR_SMALLER: p = "<"; break;
4843 case EXPR_SEQUAL: p = "<="; break;
4844 case EXPR_MATCH: p = "=~"; break;
4845 case EXPR_IS: p = "is"; break;
4846 case EXPR_ISNOT: p = "isnot"; break;
4847 case EXPR_NOMATCH: p = "!~"; break;
4848 default: p = "???"; break;
4849 }
4850 STRCPY(buf, p);
4851 if (iptr->isn_arg.op.op_ic == TRUE)
4852 strcat(buf, "?");
4853 switch(iptr->isn_type)
4854 {
4855 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4856 case ISN_COMPARESPECIAL:
4857 type = "COMPARESPECIAL"; break;
4858 case ISN_COMPARENR: type = "COMPARENR"; break;
4859 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4860 case ISN_COMPARESTRING:
4861 type = "COMPARESTRING"; break;
4862 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4863 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4864 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4865 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
4866 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4867 default: type = "???"; break;
4868 }
4869
4870 smsg("%s%4d %s %s", pfx, current, type, buf);
4871 }
4872 break;
4873
4874 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
4875 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
4876
4877 // expression operations
4878 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
4879 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
4880 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
4881 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
4882 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
4883 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
4884 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
4885 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
4886 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
4887 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
4888 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
4889 case ISN_SLICE: smsg("%s%4d SLICE %lld",
4890 pfx, current, iptr->isn_arg.number); break;
4891 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
4892 pfx, current, iptr->isn_arg.number); break;
4893 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
4894 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
4895 iptr->isn_arg.string); break;
4896 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
4897
4898 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
4899 case ISN_CHECKTYPE:
4900 {
4901 checktype_T *ct = &iptr->isn_arg.type;
4902 char *tofree;
4903
4904 if (ct->ct_arg_idx == 0)
4905 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
4906 type_name(ct->ct_type, &tofree),
4907 (int)ct->ct_off);
4908 else
4909 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
4910 type_name(ct->ct_type, &tofree),
4911 (int)ct->ct_off,
4912 (int)ct->ct_arg_idx);
4913 vim_free(tofree);
4914 break;
4915 }
4916 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
4917 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4918 iptr->isn_arg.checklen.cl_min_len);
4919 break;
4920 case ISN_SETTYPE:
4921 {
4922 char *tofree;
4923
4924 smsg("%s%4d SETTYPE %s", pfx, current,
4925 type_name(iptr->isn_arg.type.ct_type, &tofree));
4926 vim_free(tofree);
4927 break;
4928 }
4929 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
4930 case ISN_2BOOL: if (iptr->isn_arg.number)
4931 smsg("%s%4d INVERT (!val)", pfx, current);
4932 else
4933 smsg("%s%4d 2BOOL (!!val)", pfx, current);
4934 break;
4935 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
4936 (varnumber_T)(iptr->isn_arg.number));
4937 break;
4938 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]", pfx, current,
4939 (varnumber_T)(iptr->isn_arg.number));
4940 break;
4941 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current, iptr->isn_arg.string);
4942 break;
4943 case ISN_PUT:
4944 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4945 smsg("%s%4d PUT %c above range",
4946 pfx, current, iptr->isn_arg.put.put_regname);
4947 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4948 smsg("%s%4d PUT %c range",
4949 pfx, current, iptr->isn_arg.put.put_regname);
4950 else
4951 smsg("%s%4d PUT %c %ld", pfx, current,
4952 iptr->isn_arg.put.put_regname,
4953 (long)iptr->isn_arg.put.put_lnum);
4954 break;
4955
4956 // TODO: summarize modifiers
4957 case ISN_CMDMOD:
4958 {
4959 char_u *buf;
4960 size_t len = produce_cmdmods(
4961 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4962
4963 buf = alloc(len + 1);
4964 if (buf != NULL)
4965 {
4966 (void)produce_cmdmods(
4967 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4968 smsg("%s%4d CMDMOD %s", pfx, current, buf);
4969 vim_free(buf);
4970 }
4971 break;
4972 }
4973 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
4974
4975 case ISN_PROF_START:
4976 smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum);
4977 break;
4978
4979 case ISN_PROF_END:
4980 smsg("%s%4d PROFILE END", pfx, current);
4981 break;
4982
4983 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
4984 iptr->isn_arg.unpack.unp_count,
4985 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4986 break;
4987 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
4988 iptr->isn_arg.shuffle.shfl_item,
4989 iptr->isn_arg.shuffle.shfl_up);
4990 break;
4991 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
4992
4993 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
4994 return;
4995 }
4996
4997 out_flush(); // output one line at a time
4998 ui_breakcheck();
4999 if (got_int)
5000 break;
5001 }
5002}
5003
5004/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005005 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005006 * We don't really need this at runtime, but we do have tests that require it,
5007 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 */
5009 void
5010ex_disassemble(exarg_T *eap)
5011{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005012 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005013 char_u *fname;
5014 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 dfunc_T *dfunc;
5016 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005017 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005018 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005020 if (STRNCMP(arg, "<lambda>", 8) == 0)
5021 {
5022 arg += 8;
5023 (void)getdigits(&arg);
5024 fname = vim_strnsave(eap->arg, arg - eap->arg);
5025 }
5026 else
5027 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005028 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005029 if (fname == NULL)
5030 {
5031 semsg(_(e_invarg2), eap->arg);
5032 return;
5033 }
5034
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005035 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005036 if (ufunc == NULL)
5037 {
5038 char_u *p = untrans_function_name(fname);
5039
5040 if (p != NULL)
5041 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005042 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005043 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005044 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 if (ufunc == NULL)
5046 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005047 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 return;
5049 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005050 if (func_needs_compiling(ufunc, eap->forceit)
5051 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005052 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005053 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005055 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 return;
5057 }
5058 if (ufunc->uf_name_exp != NULL)
5059 msg((char *)ufunc->uf_name_exp);
5060 else
5061 msg((char *)ufunc->uf_name);
5062
5063 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005064#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005065 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
5066 instr_count = eap->forceit ? dfunc->df_instr_prof_count
5067 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005068#else
5069 instr = dfunc->df_instr;
5070 instr_count = dfunc->df_instr_count;
5071#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072
Bram Moolenaar4c137212021-04-19 16:48:48 +02005073 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074}
5075
5076/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005077 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 * list, etc. Mostly like what JavaScript does, except that empty list and
5079 * empty dictionary are FALSE.
5080 */
5081 int
5082tv2bool(typval_T *tv)
5083{
5084 switch (tv->v_type)
5085 {
5086 case VAR_NUMBER:
5087 return tv->vval.v_number != 0;
5088 case VAR_FLOAT:
5089#ifdef FEAT_FLOAT
5090 return tv->vval.v_float != 0.0;
5091#else
5092 break;
5093#endif
5094 case VAR_PARTIAL:
5095 return tv->vval.v_partial != NULL;
5096 case VAR_FUNC:
5097 case VAR_STRING:
5098 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5099 case VAR_LIST:
5100 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5101 case VAR_DICT:
5102 return tv->vval.v_dict != NULL
5103 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5104 case VAR_BOOL:
5105 case VAR_SPECIAL:
5106 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5107 case VAR_JOB:
5108#ifdef FEAT_JOB_CHANNEL
5109 return tv->vval.v_job != NULL;
5110#else
5111 break;
5112#endif
5113 case VAR_CHANNEL:
5114#ifdef FEAT_JOB_CHANNEL
5115 return tv->vval.v_channel != NULL;
5116#else
5117 break;
5118#endif
5119 case VAR_BLOB:
5120 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5121 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005122 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 case VAR_VOID:
5124 break;
5125 }
5126 return FALSE;
5127}
5128
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005129 void
5130emsg_using_string_as(typval_T *tv, int as_number)
5131{
5132 semsg(_(as_number ? e_using_string_as_number_str
5133 : e_using_string_as_bool_str),
5134 tv->vval.v_string == NULL
5135 ? (char_u *)"" : tv->vval.v_string);
5136}
5137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138/*
5139 * If "tv" is a string give an error and return FAIL.
5140 */
5141 int
5142check_not_string(typval_T *tv)
5143{
5144 if (tv->v_type == VAR_STRING)
5145 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005146 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 clear_tv(tv);
5148 return FAIL;
5149 }
5150 return OK;
5151}
5152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153
5154#endif // FEAT_EVAL