blob: 7f6ce5f33ae576a7d1c51d218c38a2457d30ee11 [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 Moolenaard386e922021-04-25 14:48:49 +0200604 // If the call was inside an ISN_SUBSTITUTE instruction need to use its
605 // list of instructions.
606 if (ectx->ec_instr[ectx->ec_iidx - 1].isn_type == ISN_SUBSTITUTE)
607 ectx->ec_instr = ectx->ec_instr[ectx->ec_iidx - 1]
608 .isn_arg.subs.subs_instr;
609
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100610 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200611 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100612 else
613 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200614 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100615 vim_free(floc);
616 }
617
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100618 if (ret_idx > 0)
619 {
620 // Reset the stack to the position before the call, with a spot for the
621 // return value, moved there from above the frame.
622 ectx->ec_stack.ga_len = top + 1;
623 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
624 }
625 else
626 // Reset the stack to the position before the call.
627 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200628
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100629 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200630 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631}
632
633#undef STACK_TV
634
635/*
636 * Prepare arguments and rettv for calling a builtin or user function.
637 */
638 static int
639call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
640{
641 int idx;
642 typval_T *tv;
643
644 // Move arguments from bottom of the stack to argvars[] and add terminator.
645 for (idx = 0; idx < argcount; ++idx)
646 argvars[idx] = *STACK_TV_BOT(idx - argcount);
647 argvars[argcount].v_type = VAR_UNKNOWN;
648
649 // Result replaces the arguments on the stack.
650 if (argcount > 0)
651 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200652 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 return FAIL;
654 else
655 ++ectx->ec_stack.ga_len;
656
657 // Default return value is zero.
658 tv = STACK_TV_BOT(-1);
659 tv->v_type = VAR_NUMBER;
660 tv->vval.v_number = 0;
661
662 return OK;
663}
664
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200665// Ugly global to avoid passing the execution context around through many
666// layers.
667static ectx_T *current_ectx = NULL;
668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669/*
670 * Call a builtin function by index.
671 */
672 static int
673call_bfunc(int func_idx, int argcount, ectx_T *ectx)
674{
675 typval_T argvars[MAX_FUNC_ARGS];
676 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100677 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200678 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679
680 if (call_prepare(argcount, argvars, ectx) == FAIL)
681 return FAIL;
682
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200683 // Call the builtin function. Set "current_ectx" so that when it
684 // recursively invokes call_def_function() a closure context can be set.
685 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200687 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688
689 // Clear the arguments.
690 for (idx = 0; idx < argcount; ++idx)
691 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200692
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100693 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200694 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 return OK;
696}
697
698/*
699 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100700 * If the function is compiled this will add a stack frame and set the
701 * instruction pointer at the start of the function.
702 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100703 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100704 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 */
706 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100707call_ufunc(
708 ufunc_T *ufunc,
709 partial_T *pt,
710 int argcount,
711 ectx_T *ectx,
712 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713{
714 typval_T argvars[MAX_FUNC_ARGS];
715 funcexe_T funcexe;
716 int error;
717 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100718 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100719#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100720 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100721#else
722# define profiling FALSE
723#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724
Bram Moolenaarb2049902021-01-24 12:53:53 +0100725 if (func_needs_compiling(ufunc, profiling)
726 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200727 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200728 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100729 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100730 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100731 if (error != FCERR_UNKNOWN)
732 {
733 if (error == FCERR_TOOMANY)
734 semsg(_(e_toomanyarg), ufunc->uf_name);
735 else
736 semsg(_(e_toofewarg), ufunc->uf_name);
737 return FAIL;
738 }
739
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100740 // The function has been compiled, can call it quickly. For a function
741 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100742 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100743 if (iptr != NULL)
744 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100745 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100746 iptr->isn_type = ISN_DCALL;
747 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
748 iptr->isn_arg.dfunc.cdf_argcount = argcount;
749 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200750 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752
753 if (call_prepare(argcount, argvars, ectx) == FAIL)
754 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200755 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 funcexe.evaluate = TRUE;
757
758 // Call the user function. Result goes in last position on the stack.
759 // TODO: add selfdict if there is one
760 error = call_user_func_check(ufunc, argcount, argvars,
761 STACK_TV_BOT(-1), &funcexe, NULL);
762
763 // Clear the arguments.
764 for (idx = 0; idx < argcount; ++idx)
765 clear_tv(&argvars[idx]);
766
767 if (error != FCERR_NONE)
768 {
769 user_func_error(error, ufunc->uf_name);
770 return FAIL;
771 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100772 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200773 // Error other than from calling the function itself.
774 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 return OK;
776}
777
778/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100779 * If command modifiers were applied restore them.
780 */
781 static void
782may_restore_cmdmod(funclocal_T *funclocal)
783{
784 if (funclocal->floc_restore_cmdmod)
785 {
786 cmdmod.cmod_filter_regmatch.regprog = NULL;
787 undo_cmdmod(&cmdmod);
788 cmdmod = funclocal->floc_save_cmdmod;
789 funclocal->floc_restore_cmdmod = FALSE;
790 }
791}
792
793/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200794 * Return TRUE if an error was given or CTRL-C was pressed.
795 */
796 static int
797vim9_aborting(int prev_called_emsg)
798{
799 return called_emsg > prev_called_emsg || got_int || did_throw;
800}
801
802/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 * Execute a function by "name".
804 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100805 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 * Returns FAIL if not found without an error message.
807 */
808 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100809call_by_name(
810 char_u *name,
811 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100812 ectx_T *ectx,
813 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814{
815 ufunc_T *ufunc;
816
817 if (builtin_function(name, -1))
818 {
819 int func_idx = find_internal_func(name);
820
821 if (func_idx < 0)
822 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200823 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 return FAIL;
825 return call_bfunc(func_idx, argcount, ectx);
826 }
827
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200828 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200829
830 if (ufunc == NULL)
831 {
832 int called_emsg_before = called_emsg;
833
834 if (script_autoload(name, TRUE))
835 // loaded a package, search for the function again
836 ufunc = find_func(name, FALSE, NULL);
837 if (vim9_aborting(called_emsg_before))
838 return FAIL; // bail out if loading the script caused an error
839 }
840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100842 {
843 if (ufunc->uf_arg_types != NULL)
844 {
845 int i;
846 typval_T *argv = STACK_TV_BOT(0) - argcount;
847
848 // The function can change at runtime, check that the argument
849 // types are correct.
850 for (i = 0; i < argcount; ++i)
851 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100852 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100853
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100854 if (i < ufunc->uf_args.ga_len)
855 type = ufunc->uf_arg_types[i];
856 else if (ufunc->uf_va_type != NULL)
857 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100858 if (type != NULL && check_typval_arg_type(type,
859 &argv[i], i + 1) == FAIL)
860 return FAIL;
861 }
862 }
863
Bram Moolenaar4c137212021-04-19 16:48:48 +0200864 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866
867 return FAIL;
868}
869
870 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100871call_partial(
872 typval_T *tv,
873 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100874 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200876 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200877 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100879 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880
881 if (tv->v_type == VAR_PARTIAL)
882 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200883 partial_T *pt = tv->vval.v_partial;
884 int i;
885
886 if (pt->pt_argc > 0)
887 {
888 // Make space for arguments from the partial, shift the "argcount"
889 // arguments up.
890 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
891 return FAIL;
892 for (i = 1; i <= argcount; ++i)
893 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
894 ectx->ec_stack.ga_len += pt->pt_argc;
895 argcount += pt->pt_argc;
896
897 // copy the arguments from the partial onto the stack
898 for (i = 0; i < pt->pt_argc; ++i)
899 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901
902 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200903 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 name = pt->pt_name;
906 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200907 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200909 if (name != NULL)
910 {
911 char_u fname_buf[FLEN_FIXED + 1];
912 char_u *tofree = NULL;
913 int error = FCERR_NONE;
914 char_u *fname;
915
916 // May need to translate <SNR>123_ to K_SNR.
917 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
918 if (error != FCERR_NONE)
919 res = FAIL;
920 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200921 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200922 vim_free(tofree);
923 }
924
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100925 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 {
927 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200928 semsg(_(e_unknownfunc),
929 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 return FAIL;
931 }
932 return OK;
933}
934
935/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200936 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
937 * TRUE.
938 */
939 static int
940error_if_locked(int lock, char *error)
941{
942 if (lock & (VAR_LOCKED | VAR_FIXED))
943 {
944 emsg(_(error));
945 return TRUE;
946 }
947 return FALSE;
948}
949
950/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100951 * Give an error if "tv" is not a number and return FAIL.
952 */
953 static int
954check_for_number(typval_T *tv)
955{
956 if (tv->v_type != VAR_NUMBER)
957 {
958 semsg(_(e_expected_str_but_got_str),
959 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
960 return FAIL;
961 }
962 return OK;
963}
964
965/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100966 * Store "tv" in variable "name".
967 * This is for s: and g: variables.
968 */
969 static void
970store_var(char_u *name, typval_T *tv)
971{
972 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200973 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100974
Bram Moolenaard877a572021-04-01 19:42:48 +0200975 if (tv->v_lock)
976 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100977 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +0200978 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100979 restore_funccal();
980}
981
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100982/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100983 * Convert "tv" to a string.
984 * Return FAIL if not allowed.
985 */
986 static int
987do_2string(typval_T *tv, int is_2string_any)
988{
989 if (tv->v_type != VAR_STRING)
990 {
991 char_u *str;
992
993 if (is_2string_any)
994 {
995 switch (tv->v_type)
996 {
997 case VAR_SPECIAL:
998 case VAR_BOOL:
999 case VAR_NUMBER:
1000 case VAR_FLOAT:
1001 case VAR_BLOB: break;
1002 default: to_string_error(tv->v_type);
1003 return FAIL;
1004 }
1005 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001006 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001007 clear_tv(tv);
1008 tv->v_type = VAR_STRING;
1009 tv->vval.v_string = str;
1010 }
1011 return OK;
1012}
1013
1014/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001015 * When the value of "sv" is a null list of dict, allocate it.
1016 */
1017 static void
1018allocate_if_null(typval_T *tv)
1019{
1020 switch (tv->v_type)
1021 {
1022 case VAR_LIST:
1023 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001024 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001025 break;
1026 case VAR_DICT:
1027 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001028 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001029 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001030 case VAR_BLOB:
1031 if (tv->vval.v_blob == NULL)
1032 (void)rettv_blob_alloc(tv);
1033 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001034 default:
1035 break;
1036 }
1037}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001038
Bram Moolenaare7525c52021-01-09 13:20:37 +01001039/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001040 * Return the character "str[index]" where "index" is the character index,
1041 * including composing characters.
1042 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001043 */
1044 char_u *
1045char_from_string(char_u *str, varnumber_T index)
1046{
1047 size_t nbyte = 0;
1048 varnumber_T nchar = index;
1049 size_t slen;
1050
1051 if (str == NULL)
1052 return NULL;
1053 slen = STRLEN(str);
1054
Bram Moolenaarff871402021-03-26 13:34:05 +01001055 // Do the same as for a list: a negative index counts from the end.
1056 // Optimization to check the first byte to be below 0x80 (and no composing
1057 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001058 if (index < 0)
1059 {
1060 int clen = 0;
1061
1062 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001063 {
1064 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1065 ++nbyte;
1066 else if (enc_utf8)
1067 nbyte += utfc_ptr2len(str + nbyte);
1068 else
1069 nbyte += mb_ptr2len(str + nbyte);
1070 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001071 nchar = clen + index;
1072 if (nchar < 0)
1073 // unlike list: index out of range results in empty string
1074 return NULL;
1075 }
1076
1077 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001078 {
1079 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1080 ++nbyte;
1081 else if (enc_utf8)
1082 nbyte += utfc_ptr2len(str + nbyte);
1083 else
1084 nbyte += mb_ptr2len(str + nbyte);
1085 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001086 if (nbyte >= slen)
1087 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001088 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001089}
1090
1091/*
1092 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001093 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001094 * If going over the end return "str_len".
1095 * If "idx" is negative count from the end, -1 is the last character.
1096 * When going over the start return -1.
1097 */
1098 static long
1099char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1100{
1101 varnumber_T nchar = idx;
1102 size_t nbyte = 0;
1103
1104 if (nchar >= 0)
1105 {
1106 while (nchar > 0 && nbyte < str_len)
1107 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001108 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001109 --nchar;
1110 }
1111 }
1112 else
1113 {
1114 nbyte = str_len;
1115 while (nchar < 0 && nbyte > 0)
1116 {
1117 --nbyte;
1118 nbyte -= mb_head_off(str, str + nbyte);
1119 ++nchar;
1120 }
1121 if (nchar < 0)
1122 return -1;
1123 }
1124 return (long)nbyte;
1125}
1126
1127/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001128 * Return the slice "str[first : last]" using character indexes. Composing
1129 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001130 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001131 * Return NULL when the result is empty.
1132 */
1133 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001134string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001135{
1136 long start_byte, end_byte;
1137 size_t slen;
1138
1139 if (str == NULL)
1140 return NULL;
1141 slen = STRLEN(str);
1142 start_byte = char_idx2byte(str, slen, first);
1143 if (start_byte < 0)
1144 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001145 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001146 end_byte = (long)slen;
1147 else
1148 {
1149 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001150 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001151 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001152 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001153 }
1154
1155 if (start_byte >= (long)slen || end_byte <= start_byte)
1156 return NULL;
1157 return vim_strnsave(str + start_byte, end_byte - start_byte);
1158}
1159
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001160 static svar_T *
1161get_script_svar(scriptref_T *sref, ectx_T *ectx)
1162{
1163 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1165 + ectx->ec_dfunc_idx;
1166 svar_T *sv;
1167
1168 if (sref->sref_seq != si->sn_script_seq)
1169 {
1170 // The script was reloaded after the function was
1171 // compiled, the script_idx may not be valid.
1172 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1173 dfunc->df_ufunc->uf_name_exp);
1174 return NULL;
1175 }
1176 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1177 if (!equal_type(sv->sv_type, sref->sref_type))
1178 {
1179 emsg(_(e_script_variable_type_changed));
1180 return NULL;
1181 }
1182 return sv;
1183}
1184
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001185/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 * Execute a function by "name".
1187 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001188 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 */
1190 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001191call_eval_func(
1192 char_u *name,
1193 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001194 ectx_T *ectx,
1195 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196{
Bram Moolenaared677f52020-08-12 16:38:10 +02001197 int called_emsg_before = called_emsg;
1198 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199
Bram Moolenaar4c137212021-04-19 16:48:48 +02001200 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001201 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001203 dictitem_T *v;
1204
1205 v = find_var(name, NULL, FALSE);
1206 if (v == NULL)
1207 {
1208 semsg(_(e_unknownfunc), name);
1209 return FAIL;
1210 }
1211 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1212 {
1213 semsg(_(e_unknownfunc), name);
1214 return FAIL;
1215 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001216 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001218 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219}
1220
1221/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001222 * When a function reference is used, fill a partial with the information
1223 * needed, especially when it is used as a closure.
1224 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001225 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001226fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1227{
1228 pt->pt_func = ufunc;
1229 pt->pt_refcount = 1;
1230
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001231 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001232 {
1233 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1234 + ectx->ec_dfunc_idx;
1235
1236 // The closure needs to find arguments and local
1237 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001238 pt->pt_outer.out_stack = &ectx->ec_stack;
1239 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1240 pt->pt_outer.out_up = ectx->ec_outer;
1241 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001242
1243 // If this function returns and the closure is still
1244 // being used, we need to make a copy of the context
1245 // (arguments and local variables). Store a reference
1246 // to the partial so we can handle that.
1247 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1248 {
1249 vim_free(pt);
1250 return FAIL;
1251 }
1252 // Extra variable keeps the count of closures created
1253 // in the current function call.
1254 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1255 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1256
1257 ((partial_T **)ectx->ec_funcrefs.ga_data)
1258 [ectx->ec_funcrefs.ga_len] = pt;
1259 ++pt->pt_refcount;
1260 ++ectx->ec_funcrefs.ga_len;
1261 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001262 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001263 return OK;
1264}
1265
Bram Moolenaar4c137212021-04-19 16:48:48 +02001266// used for substitute_instr
1267typedef struct subs_expr_S {
1268 ectx_T *subs_ectx;
1269 isn_T *subs_instr;
1270 int subs_status;
1271} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272
1273// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001274#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275
1276// Get pointer to item at the bottom of the stack, -1 is the bottom.
1277#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001278#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 +01001279
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001280// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001281#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 +01001282
Bram Moolenaar4c137212021-04-19 16:48:48 +02001283/*
1284 * Execute instructions in execution context "ectx".
1285 * Return OK or FAIL;
1286 */
1287 static int
1288exec_instructions(ectx_T *ectx)
1289{
1290 int breakcheck_count = 0;
1291 typval_T *tv;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001292
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001293 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001294 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 for (;;)
1297 {
1298 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001299
Bram Moolenaar270d0382020-05-15 21:42:53 +02001300 if (++breakcheck_count >= 100)
1301 {
1302 line_breakcheck();
1303 breakcheck_count = 0;
1304 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001305 if (got_int)
1306 {
1307 // Turn CTRL-C into an exception.
1308 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001309 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001310 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001311 did_throw = TRUE;
1312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313
Bram Moolenaara26b9702020-04-18 19:53:28 +02001314 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1315 {
1316 // Turn an error message into an exception.
1317 did_emsg = FALSE;
1318 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001319 return FAIL;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001320 did_throw = TRUE;
1321 *msg_list = NULL;
1322 }
1323
Bram Moolenaar4c137212021-04-19 16:48:48 +02001324 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001326 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001327 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328
1329 // An exception jumps to the first catch, finally, or returns from
1330 // the current function.
1331 if (trystack->ga_len > 0)
1332 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001333 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 {
1335 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001336 ectx->ec_in_catch = TRUE;
1337 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 }
1339 else
1340 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001341 // Not inside try or need to return from current functions.
1342 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001343 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1344 return FAIL;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001345 tv = STACK_TV_BOT(0);
1346 tv->v_type = VAR_NUMBER;
1347 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001348 ++ectx->ec_stack.ga_len;
1349 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001351 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001352 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001353 if (handle_closure_in_use(ectx, FALSE) == FAIL)
1354 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 goto done;
1356 }
1357
Bram Moolenaar4c137212021-04-19 16:48:48 +02001358 if (func_return(ectx) == FAIL)
1359 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 }
1361 continue;
1362 }
1363
Bram Moolenaar4c137212021-04-19 16:48:48 +02001364 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 switch (iptr->isn_type)
1366 {
1367 // execute Ex command line
1368 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001369 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001370 source_cookie_T cookie;
1371
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001372 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001373 // Pass getsourceline to get an error for a missing ":end"
1374 // command.
1375 CLEAR_FIELD(cookie);
1376 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1377 if (do_cmdline(iptr->isn_arg.string,
1378 getsourceline, &cookie,
1379 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1380 == FAIL
1381 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001382 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001383 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 break;
1385
Bram Moolenaar4c137212021-04-19 16:48:48 +02001386 // execute :substitute with an expression
1387 case ISN_SUBSTITUTE:
1388 {
1389 subs_T *subs = &iptr->isn_arg.subs;
1390 source_cookie_T cookie;
1391 struct subs_expr_S *save_instr = substitute_instr;
1392 struct subs_expr_S subs_instr;
1393 int res;
1394
1395 subs_instr.subs_ectx = ectx;
1396 subs_instr.subs_instr = subs->subs_instr;
1397 subs_instr.subs_status = OK;
1398 substitute_instr = &subs_instr;
1399
1400 SOURCING_LNUM = iptr->isn_lnum;
1401 // This is very much like ISN_EXEC
1402 CLEAR_FIELD(cookie);
1403 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1404 res = do_cmdline(subs->subs_cmd,
1405 getsourceline, &cookie,
1406 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1407 substitute_instr = save_instr;
1408
1409 if (res == FAIL || did_emsg
1410 || subs_instr.subs_status == FAIL)
1411 goto on_error;
1412 }
1413 break;
1414
1415 case ISN_FINISH:
1416 goto done;
1417
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001418 case ISN_REDIRSTART:
1419 // create a dummy entry for var_redir_str()
1420 if (alloc_redir_lval() == FAIL)
1421 goto on_error;
1422
1423 // The output is stored in growarray "redir_ga" until
1424 // redirection ends.
1425 init_redir_ga();
1426 redir_vname = 1;
1427 break;
1428
1429 case ISN_REDIREND:
1430 {
1431 char_u *res = get_clear_redir_ga();
1432
1433 // End redirection, put redirected text on the stack.
1434 clear_redir_lval();
1435 redir_vname = 0;
1436
1437 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1438 {
1439 vim_free(res);
1440 return FAIL;
1441 }
1442 tv = STACK_TV_BOT(0);
1443 tv->v_type = VAR_STRING;
1444 tv->vval.v_string = res;
1445 ++ectx->ec_stack.ga_len;
1446 }
1447 break;
1448
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001449 // execute Ex command from pieces on the stack
1450 case ISN_EXECCONCAT:
1451 {
1452 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001453 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001454 int pass;
1455 int i;
1456 char_u *cmd = NULL;
1457 char_u *str;
1458
1459 for (pass = 1; pass <= 2; ++pass)
1460 {
1461 for (i = 0; i < count; ++i)
1462 {
1463 tv = STACK_TV_BOT(i - count);
1464 str = tv->vval.v_string;
1465 if (str != NULL && *str != NUL)
1466 {
1467 if (pass == 2)
1468 STRCPY(cmd + len, str);
1469 len += STRLEN(str);
1470 }
1471 if (pass == 2)
1472 clear_tv(tv);
1473 }
1474 if (pass == 1)
1475 {
1476 cmd = alloc(len + 1);
1477 if (cmd == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001478 return FAIL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001479 len = 0;
1480 }
1481 }
1482
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001483 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001484 do_cmdline_cmd(cmd);
1485 vim_free(cmd);
1486 }
1487 break;
1488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 // execute :echo {string} ...
1490 case ISN_ECHO:
1491 {
1492 int count = iptr->isn_arg.echo.echo_count;
1493 int atstart = TRUE;
1494 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001495 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496
1497 for (idx = 0; idx < count; ++idx)
1498 {
1499 tv = STACK_TV_BOT(idx - count);
1500 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1501 &atstart, &needclr);
1502 clear_tv(tv);
1503 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001504 if (needclr)
1505 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001506 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 }
1508 break;
1509
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001510 // :execute {string} ...
1511 // :echomsg {string} ...
1512 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001513 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001514 case ISN_ECHOMSG:
1515 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001516 {
1517 int count = iptr->isn_arg.number;
1518 garray_T ga;
1519 char_u buf[NUMBUFLEN];
1520 char_u *p;
1521 int len;
1522 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001523 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001524
1525 ga_init2(&ga, 1, 80);
1526 for (idx = 0; idx < count; ++idx)
1527 {
1528 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001529 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001530 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001531 if (tv->v_type == VAR_CHANNEL
1532 || tv->v_type == VAR_JOB)
1533 {
1534 SOURCING_LNUM = iptr->isn_lnum;
1535 emsg(_(e_inval_string));
1536 break;
1537 }
1538 else
1539 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001540 }
1541 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001542 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001543
1544 len = (int)STRLEN(p);
1545 if (ga_grow(&ga, len + 2) == FAIL)
1546 failed = TRUE;
1547 else
1548 {
1549 if (ga.ga_len > 0)
1550 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1551 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1552 ga.ga_len += len;
1553 }
1554 clear_tv(tv);
1555 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001556 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001557 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001558 {
1559 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001560 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001561 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001562
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001563 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001564 {
1565 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001566 {
1567 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001568 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001569 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001570 {
1571 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001572 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001573 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001574 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001575 else
1576 {
1577 msg_sb_eol();
1578 if (iptr->isn_type == ISN_ECHOMSG)
1579 {
1580 msg_attr(ga.ga_data, echo_attr);
1581 out_flush();
1582 }
1583 else
1584 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001585 SOURCING_LNUM = iptr->isn_lnum;
1586 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001587 }
1588 }
1589 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001590 ga_clear(&ga);
1591 }
1592 break;
1593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 // load local variable or argument
1595 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001596 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1597 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001599 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 break;
1601
1602 // load v: variable
1603 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001604 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1605 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001607 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 break;
1609
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001610 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 case ISN_LOADSCRIPT:
1612 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001613 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 svar_T *sv;
1615
Bram Moolenaar4c137212021-04-19 16:48:48 +02001616 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001617 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001618 return FAIL;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001619 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001620 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1621 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001623 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 }
1625 break;
1626
1627 // load s: variable in old script
1628 case ISN_LOADS:
1629 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001630 hashtab_T *ht = &SCRIPT_VARS(
1631 iptr->isn_arg.loadstore.ls_sid);
1632 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 if (di == NULL)
1636 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001637 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001638 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001639 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 }
1641 else
1642 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001643 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1644 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001646 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 }
1648 }
1649 break;
1650
Bram Moolenaard3aac292020-04-19 14:32:17 +02001651 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001653 case ISN_LOADB:
1654 case ISN_LOADW:
1655 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001657 dictitem_T *di = NULL;
1658 hashtab_T *ht = NULL;
1659 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001660
Bram Moolenaard3aac292020-04-19 14:32:17 +02001661 switch (iptr->isn_type)
1662 {
1663 case ISN_LOADG:
1664 ht = get_globvar_ht();
1665 namespace = 'g';
1666 break;
1667 case ISN_LOADB:
1668 ht = &curbuf->b_vars->dv_hashtab;
1669 namespace = 'b';
1670 break;
1671 case ISN_LOADW:
1672 ht = &curwin->w_vars->dv_hashtab;
1673 namespace = 'w';
1674 break;
1675 case ISN_LOADT:
1676 ht = &curtab->tp_vars->dv_hashtab;
1677 namespace = 't';
1678 break;
1679 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001680 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001681 }
1682 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 if (di == NULL)
1685 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001686 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001687 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001688 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001689 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 }
1691 else
1692 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001693 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1694 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001696 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 }
1698 }
1699 break;
1700
Bram Moolenaar03290b82020-12-19 16:30:44 +01001701 // load autoload variable
1702 case ISN_LOADAUTO:
1703 {
1704 char_u *name = iptr->isn_arg.string;
1705
Bram Moolenaar4c137212021-04-19 16:48:48 +02001706 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1707 return FAIL;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001708 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001709 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001710 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001711 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001712 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001713 }
1714 break;
1715
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001716 // load g:/b:/w:/t: namespace
1717 case ISN_LOADGDICT:
1718 case ISN_LOADBDICT:
1719 case ISN_LOADWDICT:
1720 case ISN_LOADTDICT:
1721 {
1722 dict_T *d = NULL;
1723
1724 switch (iptr->isn_type)
1725 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001726 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1727 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1728 case ISN_LOADWDICT: d = curwin->w_vars; break;
1729 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001730 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001731 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001732 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001733 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1734 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001735 tv = STACK_TV_BOT(0);
1736 tv->v_type = VAR_DICT;
1737 tv->v_lock = 0;
1738 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001739 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001740 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001741 }
1742 break;
1743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 // load &option
1745 case ISN_LOADOPT:
1746 {
1747 typval_T optval;
1748 char_u *name = iptr->isn_arg.string;
1749
Bram Moolenaara8c17702020-04-01 21:17:24 +02001750 // This is not expected to fail, name is checked during
1751 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001752 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1753 return FAIL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001754 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001755 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001757 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 }
1759 break;
1760
1761 // load $ENV
1762 case ISN_LOADENV:
1763 {
1764 typval_T optval;
1765 char_u *name = iptr->isn_arg.string;
1766
Bram Moolenaar4c137212021-04-19 16:48:48 +02001767 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1768 return FAIL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001769 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001770 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001772 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773 }
1774 break;
1775
1776 // load @register
1777 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001778 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1779 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 tv = STACK_TV_BOT(0);
1781 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001782 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001783 // This may result in NULL, which should be equivalent to an
1784 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 tv->vval.v_string = get_reg_contents(
1786 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001787 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 break;
1789
1790 // store local variable
1791 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001792 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 tv = STACK_TV_VAR(iptr->isn_arg.number);
1794 clear_tv(tv);
1795 *tv = *STACK_TV_BOT(0);
1796 break;
1797
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001798 // store s: variable in old script
1799 case ISN_STORES:
1800 {
1801 hashtab_T *ht = &SCRIPT_VARS(
1802 iptr->isn_arg.loadstore.ls_sid);
1803 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001804 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001805
Bram Moolenaar4c137212021-04-19 16:48:48 +02001806 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001807 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001808 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001809 else
1810 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001811 SOURCING_LNUM = iptr->isn_lnum;
1812 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001813 {
1814 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001815 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001816 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001817 clear_tv(&di->di_tv);
1818 di->di_tv = *STACK_TV_BOT(0);
1819 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001820 }
1821 break;
1822
1823 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 case ISN_STORESCRIPT:
1825 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001826 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1827 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828
Bram Moolenaar4c137212021-04-19 16:48:48 +02001829 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001830 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001831 return FAIL;
1832 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001833
1834 // "const" and "final" are checked at compile time, locking
1835 // the value needs to be checked here.
1836 SOURCING_LNUM = iptr->isn_lnum;
1837 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001838 {
1839 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001840 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001841 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 clear_tv(sv->sv_tv);
1844 *sv->sv_tv = *STACK_TV_BOT(0);
1845 }
1846 break;
1847
1848 // store option
1849 case ISN_STOREOPT:
1850 {
1851 long n = 0;
1852 char_u *s = NULL;
1853 char *msg;
1854
Bram Moolenaar4c137212021-04-19 16:48:48 +02001855 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 tv = STACK_TV_BOT(0);
1857 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001858 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001860 if (s == NULL)
1861 s = (char_u *)"";
1862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001864 // must be VAR_NUMBER, CHECKTYPE makes sure
1865 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1867 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001868 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 if (msg != NULL)
1870 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001871 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001873 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 }
1876 break;
1877
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001878 // store $ENV
1879 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001880 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001881 tv = STACK_TV_BOT(0);
1882 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1883 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001884 break;
1885
1886 // store @r
1887 case ISN_STOREREG:
1888 {
1889 int reg = iptr->isn_arg.number;
1890
Bram Moolenaar4c137212021-04-19 16:48:48 +02001891 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001892 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001893 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001894 tv_get_string(tv), -1, FALSE);
1895 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001896 }
1897 break;
1898
1899 // store v: variable
1900 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001901 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001902 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1903 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001904 // should not happen, type is checked when compiling
1905 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001906 break;
1907
Bram Moolenaard3aac292020-04-19 14:32:17 +02001908 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001910 case ISN_STOREB:
1911 case ISN_STOREW:
1912 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001914 dictitem_T *di;
1915 hashtab_T *ht;
1916 char_u *name = iptr->isn_arg.string + 2;
1917
Bram Moolenaard3aac292020-04-19 14:32:17 +02001918 switch (iptr->isn_type)
1919 {
1920 case ISN_STOREG:
1921 ht = get_globvar_ht();
1922 break;
1923 case ISN_STOREB:
1924 ht = &curbuf->b_vars->dv_hashtab;
1925 break;
1926 case ISN_STOREW:
1927 ht = &curwin->w_vars->dv_hashtab;
1928 break;
1929 case ISN_STORET:
1930 ht = &curtab->tp_vars->dv_hashtab;
1931 break;
1932 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001933 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935
Bram Moolenaar4c137212021-04-19 16:48:48 +02001936 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001937 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001939 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 else
1941 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001942 SOURCING_LNUM = iptr->isn_lnum;
1943 if (var_check_permission(di, name) == FAIL)
1944 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 clear_tv(&di->di_tv);
1946 di->di_tv = *STACK_TV_BOT(0);
1947 }
1948 }
1949 break;
1950
Bram Moolenaar03290b82020-12-19 16:30:44 +01001951 // store an autoload variable
1952 case ISN_STOREAUTO:
1953 SOURCING_LNUM = iptr->isn_lnum;
1954 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1955 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001956 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001957 break;
1958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 // store number in local variable
1960 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001961 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 clear_tv(tv);
1963 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001964 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 break;
1966
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001967 // store value in list or dict variable
1968 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001969 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001970 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001971 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001972 typval_T *tv_dest = STACK_TV_BOT(-1);
1973 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001974
Bram Moolenaar752fc692021-01-04 21:57:11 +01001975 // Stack contains:
1976 // -3 value to be stored
1977 // -2 index
1978 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001979 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001980 SOURCING_LNUM = iptr->isn_lnum;
1981 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001982 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001983 dest_type = tv_dest->v_type;
1984 if (dest_type == VAR_DICT)
1985 status = do_2string(tv_idx, TRUE);
1986 else if (dest_type == VAR_LIST
1987 && tv_idx->v_type != VAR_NUMBER)
1988 {
1989 emsg(_(e_number_exp));
1990 status = FAIL;
1991 }
1992 }
1993 else if (dest_type != tv_dest->v_type)
1994 {
1995 // just in case, should be OK
1996 semsg(_(e_expected_str_but_got_str),
1997 vartype_name(dest_type),
1998 vartype_name(tv_dest->v_type));
1999 status = FAIL;
2000 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002001
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002002 if (status == OK && dest_type == VAR_LIST)
2003 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002004 long lidx = (long)tv_idx->vval.v_number;
2005 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002006
2007 if (list == NULL)
2008 {
2009 emsg(_(e_list_not_set));
2010 goto on_error;
2011 }
2012 if (lidx < 0 && list->lv_len + lidx >= 0)
2013 // negative index is relative to the end
2014 lidx = list->lv_len + lidx;
2015 if (lidx < 0 || lidx > list->lv_len)
2016 {
2017 semsg(_(e_listidx), lidx);
2018 goto on_error;
2019 }
2020 if (lidx < list->lv_len)
2021 {
2022 listitem_T *li = list_find(list, lidx);
2023
2024 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002025 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002026 goto on_error;
2027 // overwrite existing list item
2028 clear_tv(&li->li_tv);
2029 li->li_tv = *tv;
2030 }
2031 else
2032 {
2033 if (error_if_locked(list->lv_lock,
2034 e_cannot_change_list))
2035 goto on_error;
2036 // append to list, only fails when out of memory
2037 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002038 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002039 clear_tv(tv);
2040 }
2041 }
2042 else if (status == OK && dest_type == VAR_DICT)
2043 {
2044 char_u *key = tv_idx->vval.v_string;
2045 dict_T *dict = tv_dest->vval.v_dict;
2046 dictitem_T *di;
2047
2048 SOURCING_LNUM = iptr->isn_lnum;
2049 if (dict == NULL)
2050 {
2051 emsg(_(e_dictionary_not_set));
2052 goto on_error;
2053 }
2054 if (key == NULL)
2055 key = (char_u *)"";
2056 di = dict_find(dict, key, -1);
2057 if (di != NULL)
2058 {
2059 if (error_if_locked(di->di_tv.v_lock,
2060 e_cannot_change_dict_item))
2061 goto on_error;
2062 // overwrite existing value
2063 clear_tv(&di->di_tv);
2064 di->di_tv = *tv;
2065 }
2066 else
2067 {
2068 if (error_if_locked(dict->dv_lock,
2069 e_cannot_change_dict))
2070 goto on_error;
2071 // add to dict, only fails when out of memory
2072 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002073 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002074 clear_tv(tv);
2075 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002076 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002077 else if (status == OK && dest_type == VAR_BLOB)
2078 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002079 long lidx = (long)tv_idx->vval.v_number;
2080 blob_T *blob = tv_dest->vval.v_blob;
2081 varnumber_T nr;
2082 int error = FALSE;
2083 int len;
2084
2085 if (blob == NULL)
2086 {
2087 emsg(_(e_blob_not_set));
2088 goto on_error;
2089 }
2090 len = blob_len(blob);
2091 if (lidx < 0 && len + lidx >= 0)
2092 // negative index is relative to the end
2093 lidx = len + lidx;
2094
2095 // Can add one byte at the end.
2096 if (lidx < 0 || lidx > len)
2097 {
2098 semsg(_(e_blobidx), lidx);
2099 goto on_error;
2100 }
2101 if (value_check_lock(blob->bv_lock,
2102 (char_u *)"blob", FALSE))
2103 goto on_error;
2104 nr = tv_get_number_chk(tv, &error);
2105 if (error)
2106 goto on_error;
2107 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002108 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002109 else
2110 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002111 status = FAIL;
2112 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002113 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002114
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002115 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002116 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002117 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002118 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002119 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002120 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002121 goto on_error;
2122 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002123 }
2124 break;
2125
Bram Moolenaar68452172021-04-12 21:21:02 +02002126 // store value in blob range
2127 case ISN_STORERANGE:
2128 {
2129 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2130 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2131 typval_T *tv_dest = STACK_TV_BOT(-1);
2132 int status = OK;
2133
2134 // Stack contains:
2135 // -4 value to be stored
2136 // -3 first index or "none"
2137 // -2 second index or "none"
2138 // -1 destination blob
2139 tv = STACK_TV_BOT(-4);
2140 if (tv_dest->v_type != VAR_BLOB)
2141 {
2142 status = FAIL;
2143 emsg(_(e_blob_required));
2144 }
2145 else
2146 {
2147 varnumber_T n1;
2148 varnumber_T n2;
2149 int error = FALSE;
2150
2151 n1 = tv_get_number_chk(tv_idx1, &error);
2152 if (error)
2153 status = FAIL;
2154 else
2155 {
2156 if (tv_idx2->v_type == VAR_SPECIAL
2157 && tv_idx2->vval.v_number == VVAL_NONE)
2158 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2159 else
2160 n2 = tv_get_number_chk(tv_idx2, &error);
2161 if (error)
2162 status = FAIL;
2163 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002164 {
2165 long bloblen = blob_len(tv_dest->vval.v_blob);
2166
2167 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002168 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002169 || check_blob_range(bloblen,
2170 n1, n2, FALSE) == FAIL)
2171 status = FAIL;
2172 else
2173 status = blob_set_range(
2174 tv_dest->vval.v_blob, n1, n2, tv);
2175 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002176 }
2177 }
2178
2179 clear_tv(tv_idx1);
2180 clear_tv(tv_idx2);
2181 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002182 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002183 clear_tv(tv);
2184
2185 if (status == FAIL)
2186 goto on_error;
2187 }
2188 break;
2189
Bram Moolenaar0186e582021-01-10 18:33:11 +01002190 // load or store variable or argument from outer scope
2191 case ISN_LOADOUTER:
2192 case ISN_STOREOUTER:
2193 {
2194 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002195 outer_T *outer = ectx->ec_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002196
2197 while (depth > 1 && outer != NULL)
2198 {
2199 outer = outer->out_up;
2200 --depth;
2201 }
2202 if (outer == NULL)
2203 {
2204 SOURCING_LNUM = iptr->isn_lnum;
2205 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002206 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002207 }
2208 tv = ((typval_T *)outer->out_stack->ga_data)
2209 + outer->out_frame_idx + STACK_FRAME_SIZE
2210 + iptr->isn_arg.outer.outer_idx;
2211 if (iptr->isn_type == ISN_LOADOUTER)
2212 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002213 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2214 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002215 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002216 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002217 }
2218 else
2219 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002220 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002221 clear_tv(tv);
2222 *tv = *STACK_TV_BOT(0);
2223 }
2224 }
2225 break;
2226
Bram Moolenaar752fc692021-01-04 21:57:11 +01002227 // unlet item in list or dict variable
2228 case ISN_UNLETINDEX:
2229 {
2230 typval_T *tv_idx = STACK_TV_BOT(-2);
2231 typval_T *tv_dest = STACK_TV_BOT(-1);
2232 int status = OK;
2233
2234 // Stack contains:
2235 // -2 index
2236 // -1 dict or list
2237 if (tv_dest->v_type == VAR_DICT)
2238 {
2239 // unlet a dict item, index must be a string
2240 if (tv_idx->v_type != VAR_STRING)
2241 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002242 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002243 semsg(_(e_expected_str_but_got_str),
2244 vartype_name(VAR_STRING),
2245 vartype_name(tv_idx->v_type));
2246 status = FAIL;
2247 }
2248 else
2249 {
2250 dict_T *d = tv_dest->vval.v_dict;
2251 char_u *key = tv_idx->vval.v_string;
2252 dictitem_T *di = NULL;
2253
2254 if (key == NULL)
2255 key = (char_u *)"";
2256 if (d != NULL)
2257 di = dict_find(d, key, (int)STRLEN(key));
2258 if (di == NULL)
2259 {
2260 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002261 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002262 semsg(_(e_dictkey), key);
2263 status = FAIL;
2264 }
2265 else
2266 {
2267 // TODO: check for dict or item locked
2268 dictitem_remove(d, di);
2269 }
2270 }
2271 }
2272 else if (tv_dest->v_type == VAR_LIST)
2273 {
2274 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002275 SOURCING_LNUM = iptr->isn_lnum;
2276 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002277 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002278 status = FAIL;
2279 }
2280 else
2281 {
2282 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002283 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002284 listitem_T *li = NULL;
2285
2286 li = list_find(l, n);
2287 if (li == NULL)
2288 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002289 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002290 semsg(_(e_listidx), n);
2291 status = FAIL;
2292 }
2293 else
2294 // TODO: check for list or item locked
2295 listitem_remove(l, li);
2296 }
2297 }
2298 else
2299 {
2300 status = FAIL;
2301 semsg(_(e_cannot_index_str),
2302 vartype_name(tv_dest->v_type));
2303 }
2304
2305 clear_tv(tv_idx);
2306 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002307 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002308 if (status == FAIL)
2309 goto on_error;
2310 }
2311 break;
2312
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002313 // unlet range of items in list variable
2314 case ISN_UNLETRANGE:
2315 {
2316 // Stack contains:
2317 // -3 index1
2318 // -2 index2
2319 // -1 dict or list
2320 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2321 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2322 typval_T *tv_dest = STACK_TV_BOT(-1);
2323 int status = OK;
2324
2325 if (tv_dest->v_type == VAR_LIST)
2326 {
2327 // indexes must be a number
2328 SOURCING_LNUM = iptr->isn_lnum;
2329 if (check_for_number(tv_idx1) == FAIL
2330 || check_for_number(tv_idx2) == FAIL)
2331 {
2332 status = FAIL;
2333 }
2334 else
2335 {
2336 list_T *l = tv_dest->vval.v_list;
2337 long n1 = (long)tv_idx1->vval.v_number;
2338 long n2 = (long)tv_idx2->vval.v_number;
2339 listitem_T *li;
2340
2341 li = list_find_index(l, &n1);
2342 if (li == NULL
2343 || list_unlet_range(l, li, NULL, n1,
2344 TRUE, n2) == FAIL)
2345 status = FAIL;
2346 }
2347 }
2348 else
2349 {
2350 status = FAIL;
2351 SOURCING_LNUM = iptr->isn_lnum;
2352 semsg(_(e_cannot_index_str),
2353 vartype_name(tv_dest->v_type));
2354 }
2355
2356 clear_tv(tv_idx1);
2357 clear_tv(tv_idx2);
2358 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002359 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002360 if (status == FAIL)
2361 goto on_error;
2362 }
2363 break;
2364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 // push constant
2366 case ISN_PUSHNR:
2367 case ISN_PUSHBOOL:
2368 case ISN_PUSHSPEC:
2369 case ISN_PUSHF:
2370 case ISN_PUSHS:
2371 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002372 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002373 case ISN_PUSHCHANNEL:
2374 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002375 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2376 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002378 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002379 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 switch (iptr->isn_type)
2381 {
2382 case ISN_PUSHNR:
2383 tv->v_type = VAR_NUMBER;
2384 tv->vval.v_number = iptr->isn_arg.number;
2385 break;
2386 case ISN_PUSHBOOL:
2387 tv->v_type = VAR_BOOL;
2388 tv->vval.v_number = iptr->isn_arg.number;
2389 break;
2390 case ISN_PUSHSPEC:
2391 tv->v_type = VAR_SPECIAL;
2392 tv->vval.v_number = iptr->isn_arg.number;
2393 break;
2394#ifdef FEAT_FLOAT
2395 case ISN_PUSHF:
2396 tv->v_type = VAR_FLOAT;
2397 tv->vval.v_float = iptr->isn_arg.fnumber;
2398 break;
2399#endif
2400 case ISN_PUSHBLOB:
2401 blob_copy(iptr->isn_arg.blob, tv);
2402 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002403 case ISN_PUSHFUNC:
2404 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002405 if (iptr->isn_arg.string == NULL)
2406 tv->vval.v_string = NULL;
2407 else
2408 tv->vval.v_string =
2409 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002410 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002411 case ISN_PUSHCHANNEL:
2412#ifdef FEAT_JOB_CHANNEL
2413 tv->v_type = VAR_CHANNEL;
2414 tv->vval.v_channel = iptr->isn_arg.channel;
2415 if (tv->vval.v_channel != NULL)
2416 ++tv->vval.v_channel->ch_refcount;
2417#endif
2418 break;
2419 case ISN_PUSHJOB:
2420#ifdef FEAT_JOB_CHANNEL
2421 tv->v_type = VAR_JOB;
2422 tv->vval.v_job = iptr->isn_arg.job;
2423 if (tv->vval.v_job != NULL)
2424 ++tv->vval.v_job->jv_refcount;
2425#endif
2426 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 default:
2428 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002429 tv->vval.v_string = vim_strsave(
2430 iptr->isn_arg.string == NULL
2431 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 }
2433 break;
2434
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002435 case ISN_UNLET:
2436 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2437 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002438 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002439 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002440 case ISN_UNLETENV:
2441 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2442 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002443
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002444 case ISN_LOCKCONST:
2445 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2446 break;
2447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 // create a list from items on the stack; uses a single allocation
2449 // for the list header and the items
2450 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002451 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
2452 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 break;
2454
2455 // create a dict from items on the stack
2456 case ISN_NEWDICT:
2457 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002458 int count = iptr->isn_arg.number;
2459 dict_T *dict = dict_alloc();
2460 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002461 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002462 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463
2464 if (dict == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002465 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 for (idx = 0; idx < count; ++idx)
2467 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002468 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002470 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002471 key = tv->vval.v_string == NULL
2472 ? (char_u *)"" : tv->vval.v_string;
2473 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002474 if (item != NULL)
2475 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002476 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002477 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002478 dict_unref(dict);
2479 goto on_error;
2480 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002481 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 clear_tv(tv);
2483 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002484 {
2485 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002486 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2489 item->di_tv.v_lock = 0;
2490 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002491 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002492 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002493 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002494 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 }
2497
2498 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002499 ectx->ec_stack.ga_len -= 2 * count - 1;
2500 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2501 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002503 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 tv = STACK_TV_BOT(-1);
2505 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002506 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 tv->vval.v_dict = dict;
2508 ++dict->dv_refcount;
2509 }
2510 break;
2511
2512 // call a :def function
2513 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002514 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002515 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2516 NULL,
2517 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002518 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002519 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 break;
2521
2522 // call a builtin function
2523 case ISN_BCALL:
2524 SOURCING_LNUM = iptr->isn_lnum;
2525 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2526 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002527 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002528 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 break;
2530
2531 // call a funcref or partial
2532 case ISN_PCALL:
2533 {
2534 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2535 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002536 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537
2538 SOURCING_LNUM = iptr->isn_lnum;
2539 if (pfunc->cpf_top)
2540 {
2541 // funcref is above the arguments
2542 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2543 }
2544 else
2545 {
2546 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002547 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002548 partial_tv = *STACK_TV_BOT(0);
2549 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002551 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002552 if (tv == &partial_tv)
2553 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002555 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 }
2557 break;
2558
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002559 case ISN_PCALL_END:
2560 // PCALL finished, arguments have been consumed and replaced by
2561 // the return value. Now clear the funcref from the stack,
2562 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002563 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002564 clear_tv(STACK_TV_BOT(-1));
2565 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2566 break;
2567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 // call a user defined function or funcref/partial
2569 case ISN_UCALL:
2570 {
2571 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2572
2573 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002574 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002575 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002576 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 }
2578 break;
2579
2580 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002581 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002582 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2583 return FAIL;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002584 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002585 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002586 tv->v_type = VAR_NUMBER;
2587 tv->vval.v_number = 0;
2588 tv->v_lock = 0;
2589 // FALLTHROUGH
2590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 case ISN_RETURN:
2592 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002593 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002594 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002595
2596 if (trystack->ga_len > 0)
2597 trycmd = ((trycmd_T *)trystack->ga_data)
2598 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002599 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002600 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002602 // jump to ":finally" or ":endtry"
2603 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002604 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002605 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002606 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 trycmd->tcd_return = TRUE;
2608 }
2609 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002610 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 }
2612 break;
2613
2614 // push a function reference to a compiled function
2615 case ISN_FUNCREF:
2616 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002617 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2618 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2619 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 if (pt == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002622 return FAIL;
2623 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002624 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002625 vim_free(pt);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002626 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002627 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002628 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002629 ectx) == FAIL)
2630 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002633 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 tv->vval.v_partial = pt;
2635 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002636 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 }
2638 break;
2639
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002640 // Create a global function from a lambda.
2641 case ISN_NEWFUNC:
2642 {
2643 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2644
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002645 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002646 ectx) == FAIL)
2647 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002648 }
2649 break;
2650
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002651 // List functions
2652 case ISN_DEF:
2653 if (iptr->isn_arg.string == NULL)
2654 list_functions(NULL);
2655 else
2656 {
2657 exarg_T ea;
2658
2659 CLEAR_FIELD(ea);
2660 ea.cmd = ea.arg = iptr->isn_arg.string;
2661 define_function(&ea, NULL);
2662 }
2663 break;
2664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 // jump if a condition is met
2666 case ISN_JUMP:
2667 {
2668 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002669 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 int jump = TRUE;
2671
2672 if (when != JUMP_ALWAYS)
2673 {
2674 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002675 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002676 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002677 || when == JUMP_IF_COND_TRUE)
2678 {
2679 SOURCING_LNUM = iptr->isn_lnum;
2680 jump = tv_get_bool_chk(tv, &error);
2681 if (error)
2682 goto on_error;
2683 }
2684 else
2685 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002687 || when == JUMP_AND_KEEP_IF_FALSE
2688 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002690 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 {
2692 // drop the value from the stack
2693 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002694 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 }
2696 }
2697 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002698 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 }
2700 break;
2701
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002702 // Jump if an argument with a default value was already set and not
2703 // v:none.
2704 case ISN_JUMP_IF_ARG_SET:
2705 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2706 if (tv->v_type != VAR_UNKNOWN
2707 && !(tv->v_type == VAR_SPECIAL
2708 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002709 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002710 break;
2711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 // top of a for loop
2713 case ISN_FOR:
2714 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002715 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 typval_T *idxtv =
2717 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2718
Bram Moolenaar4c137212021-04-19 16:48:48 +02002719 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2720 return FAIL;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002721 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002722 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002723 list_T *list = ltv->vval.v_list;
2724
2725 // push the next item from the list
2726 ++idxtv->vval.v_number;
2727 if (list == NULL
2728 || idxtv->vval.v_number >= list->lv_len)
2729 {
2730 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002731 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2732 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002733 }
2734 else if (list->lv_first == &range_list_item)
2735 {
2736 // non-materialized range() list
2737 tv = STACK_TV_BOT(0);
2738 tv->v_type = VAR_NUMBER;
2739 tv->v_lock = 0;
2740 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002742 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002743 }
2744 else
2745 {
2746 listitem_T *li = list_find(list,
2747 idxtv->vval.v_number);
2748
2749 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002750 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002751 }
2752 }
2753 else if (ltv->v_type == VAR_STRING)
2754 {
2755 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002756
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002757 // The index is for the last byte of the previous
2758 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002759 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002760 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002761 {
2762 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002763 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2764 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002765 }
2766 else
2767 {
2768 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2769
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002770 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002771 tv = STACK_TV_BOT(0);
2772 tv->v_type = VAR_STRING;
2773 tv->vval.v_string = vim_strnsave(
2774 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002775 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002776 idxtv->vval.v_number += clen - 1;
2777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002779 else if (ltv->v_type == VAR_BLOB)
2780 {
2781 blob_T *blob = ltv->vval.v_blob;
2782
2783 // When we get here the first time make a copy of the
2784 // blob, so that the iteration still works when it is
2785 // changed.
2786 if (idxtv->vval.v_number == -1 && blob != NULL)
2787 {
2788 blob_copy(blob, ltv);
2789 blob_unref(blob);
2790 blob = ltv->vval.v_blob;
2791 }
2792
2793 // The index is for the previous byte.
2794 ++idxtv->vval.v_number;
2795 if (blob == NULL
2796 || idxtv->vval.v_number >= blob_len(blob))
2797 {
2798 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002799 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2800 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002801 }
2802 else
2803 {
2804 // Push the next byte from the blob.
2805 tv = STACK_TV_BOT(0);
2806 tv->v_type = VAR_NUMBER;
2807 tv->vval.v_number = blob_get(blob,
2808 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002809 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002810 }
2811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 else
2813 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002814 semsg(_(e_for_loop_on_str_not_supported),
2815 vartype_name(ltv->v_type));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002816 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 }
2818 }
2819 break;
2820
2821 // start of ":try" block
2822 case ISN_TRY:
2823 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002824 trycmd_T *trycmd = NULL;
2825
Bram Moolenaar4c137212021-04-19 16:48:48 +02002826 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
2827 return FAIL;
2828 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
2829 + ectx->ec_trystack.ga_len;
2830 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002832 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002833 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
2834 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002835 trycmd->tcd_catch_idx =
2836 iptr->isn_arg.try.try_ref->try_catch;
2837 trycmd->tcd_finally_idx =
2838 iptr->isn_arg.try.try_ref->try_finally;
2839 trycmd->tcd_endtry_idx =
2840 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 }
2842 break;
2843
2844 case ISN_PUSHEXC:
2845 if (current_exception == NULL)
2846 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002847 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002849 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002851 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2852 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002854 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002856 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 tv->vval.v_string = vim_strsave(
2858 (char_u *)current_exception->value);
2859 break;
2860
2861 case ISN_CATCH:
2862 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002863 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864
Bram Moolenaar4c137212021-04-19 16:48:48 +02002865 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 if (trystack->ga_len > 0)
2867 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002868 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 + trystack->ga_len - 1;
2870 trycmd->tcd_caught = TRUE;
2871 }
2872 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002873 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 catch_exception(current_exception);
2875 }
2876 break;
2877
Bram Moolenaarc150c092021-02-13 15:02:46 +01002878 case ISN_TRYCONT:
2879 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002880 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002881 trycont_T *trycont = &iptr->isn_arg.trycont;
2882 int i;
2883 trycmd_T *trycmd;
2884 int iidx = trycont->tct_where;
2885
2886 if (trystack->ga_len < trycont->tct_levels)
2887 {
2888 siemsg("TRYCONT: expected %d levels, found %d",
2889 trycont->tct_levels, trystack->ga_len);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002890 return FAIL;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002891 }
2892 // Make :endtry jump to any outer try block and the last
2893 // :endtry inside the loop to the loop start.
2894 for (i = trycont->tct_levels; i > 0; --i)
2895 {
2896 trycmd = ((trycmd_T *)trystack->ga_data)
2897 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002898 // Add one to tcd_cont to be able to jump to
2899 // instruction with index zero.
2900 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002901 iidx = trycmd->tcd_finally_idx == 0
2902 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002903 }
2904 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02002905 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002906 }
2907 break;
2908
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002909 case ISN_FINALLY:
2910 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002911 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002912 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2913 + trystack->ga_len - 1;
2914
2915 // Reset the index to avoid a return statement jumps here
2916 // again.
2917 trycmd->tcd_finally_idx = 0;
2918 break;
2919 }
2920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 // end of ":try" block
2922 case ISN_ENDTRY:
2923 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002924 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925
2926 if (trystack->ga_len > 0)
2927 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002928 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 --trystack->ga_len;
2931 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002932 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 trycmd = ((trycmd_T *)trystack->ga_data)
2934 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002935 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 {
2937 // discard the exception
2938 if (caught_stack == current_exception)
2939 caught_stack = caught_stack->caught;
2940 discard_current_exception();
2941 }
2942
2943 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002944 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002945
Bram Moolenaar4c137212021-04-19 16:48:48 +02002946 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01002947 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002948 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002949 clear_tv(STACK_TV_BOT(0));
2950 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002951 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002952 // handling :continue: jump to outer try block or
2953 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02002954 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 }
2956 }
2957 break;
2958
2959 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002960 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002961 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002962
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002963 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2964 {
2965 // throwing an exception while using "silent!" causes
2966 // the function to abort but not display an error.
2967 tv = STACK_TV_BOT(-1);
2968 clear_tv(tv);
2969 tv->v_type = VAR_NUMBER;
2970 tv->vval.v_number = 0;
2971 goto done;
2972 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002973 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002974 tv = STACK_TV_BOT(0);
2975 if (tv->vval.v_string == NULL
2976 || *skipwhite(tv->vval.v_string) == NUL)
2977 {
2978 vim_free(tv->vval.v_string);
2979 SOURCING_LNUM = iptr->isn_lnum;
2980 emsg(_(e_throw_with_empty_string));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002981 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002982 }
2983
2984 // Inside a "catch" we need to first discard the caught
2985 // exception.
2986 if (trystack->ga_len > 0)
2987 {
2988 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2989 + trystack->ga_len - 1;
2990 if (trycmd->tcd_caught && current_exception != NULL)
2991 {
2992 // discard the exception
2993 if (caught_stack == current_exception)
2994 caught_stack = caught_stack->caught;
2995 discard_current_exception();
2996 trycmd->tcd_caught = FALSE;
2997 }
2998 }
2999
3000 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3001 == FAIL)
3002 {
3003 vim_free(tv->vval.v_string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003004 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003005 }
3006 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 break;
3009
3010 // compare with special values
3011 case ISN_COMPAREBOOL:
3012 case ISN_COMPARESPECIAL:
3013 {
3014 typval_T *tv1 = STACK_TV_BOT(-2);
3015 typval_T *tv2 = STACK_TV_BOT(-1);
3016 varnumber_T arg1 = tv1->vval.v_number;
3017 varnumber_T arg2 = tv2->vval.v_number;
3018 int res;
3019
3020 switch (iptr->isn_arg.op.op_type)
3021 {
3022 case EXPR_EQUAL: res = arg1 == arg2; break;
3023 case EXPR_NEQUAL: res = arg1 != arg2; break;
3024 default: res = 0; break;
3025 }
3026
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 tv1->v_type = VAR_BOOL;
3029 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3030 }
3031 break;
3032
3033 // Operation with two number arguments
3034 case ISN_OPNR:
3035 case ISN_COMPARENR:
3036 {
3037 typval_T *tv1 = STACK_TV_BOT(-2);
3038 typval_T *tv2 = STACK_TV_BOT(-1);
3039 varnumber_T arg1 = tv1->vval.v_number;
3040 varnumber_T arg2 = tv2->vval.v_number;
3041 varnumber_T res;
3042
3043 switch (iptr->isn_arg.op.op_type)
3044 {
3045 case EXPR_MULT: res = arg1 * arg2; break;
3046 case EXPR_DIV: res = arg1 / arg2; break;
3047 case EXPR_REM: res = arg1 % arg2; break;
3048 case EXPR_SUB: res = arg1 - arg2; break;
3049 case EXPR_ADD: res = arg1 + arg2; break;
3050
3051 case EXPR_EQUAL: res = arg1 == arg2; break;
3052 case EXPR_NEQUAL: res = arg1 != arg2; break;
3053 case EXPR_GREATER: res = arg1 > arg2; break;
3054 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3055 case EXPR_SMALLER: res = arg1 < arg2; break;
3056 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3057 default: res = 0; break;
3058 }
3059
Bram Moolenaar4c137212021-04-19 16:48:48 +02003060 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 if (iptr->isn_type == ISN_COMPARENR)
3062 {
3063 tv1->v_type = VAR_BOOL;
3064 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3065 }
3066 else
3067 tv1->vval.v_number = res;
3068 }
3069 break;
3070
3071 // Computation with two float arguments
3072 case ISN_OPFLOAT:
3073 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003074#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 {
3076 typval_T *tv1 = STACK_TV_BOT(-2);
3077 typval_T *tv2 = STACK_TV_BOT(-1);
3078 float_T arg1 = tv1->vval.v_float;
3079 float_T arg2 = tv2->vval.v_float;
3080 float_T res = 0;
3081 int cmp = FALSE;
3082
3083 switch (iptr->isn_arg.op.op_type)
3084 {
3085 case EXPR_MULT: res = arg1 * arg2; break;
3086 case EXPR_DIV: res = arg1 / arg2; break;
3087 case EXPR_SUB: res = arg1 - arg2; break;
3088 case EXPR_ADD: res = arg1 + arg2; break;
3089
3090 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3091 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3092 case EXPR_GREATER: cmp = arg1 > arg2; break;
3093 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3094 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3095 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3096 default: cmp = 0; break;
3097 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003098 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 if (iptr->isn_type == ISN_COMPAREFLOAT)
3100 {
3101 tv1->v_type = VAR_BOOL;
3102 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3103 }
3104 else
3105 tv1->vval.v_float = res;
3106 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003107#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 break;
3109
3110 case ISN_COMPARELIST:
3111 {
3112 typval_T *tv1 = STACK_TV_BOT(-2);
3113 typval_T *tv2 = STACK_TV_BOT(-1);
3114 list_T *arg1 = tv1->vval.v_list;
3115 list_T *arg2 = tv2->vval.v_list;
3116 int cmp = FALSE;
3117 int ic = iptr->isn_arg.op.op_ic;
3118
3119 switch (iptr->isn_arg.op.op_type)
3120 {
3121 case EXPR_EQUAL: cmp =
3122 list_equal(arg1, arg2, ic, FALSE); break;
3123 case EXPR_NEQUAL: cmp =
3124 !list_equal(arg1, arg2, ic, FALSE); break;
3125 case EXPR_IS: cmp = arg1 == arg2; break;
3126 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3127 default: cmp = 0; break;
3128 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003129 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 clear_tv(tv1);
3131 clear_tv(tv2);
3132 tv1->v_type = VAR_BOOL;
3133 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3134 }
3135 break;
3136
3137 case ISN_COMPAREBLOB:
3138 {
3139 typval_T *tv1 = STACK_TV_BOT(-2);
3140 typval_T *tv2 = STACK_TV_BOT(-1);
3141 blob_T *arg1 = tv1->vval.v_blob;
3142 blob_T *arg2 = tv2->vval.v_blob;
3143 int cmp = FALSE;
3144
3145 switch (iptr->isn_arg.op.op_type)
3146 {
3147 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3148 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3149 case EXPR_IS: cmp = arg1 == arg2; break;
3150 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3151 default: cmp = 0; break;
3152 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003153 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 clear_tv(tv1);
3155 clear_tv(tv2);
3156 tv1->v_type = VAR_BOOL;
3157 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3158 }
3159 break;
3160
3161 // TODO: handle separately
3162 case ISN_COMPARESTRING:
3163 case ISN_COMPAREDICT:
3164 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 case ISN_COMPAREANY:
3166 {
3167 typval_T *tv1 = STACK_TV_BOT(-2);
3168 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003169 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 int ic = iptr->isn_arg.op.op_ic;
3171
Bram Moolenaareb26f432020-09-14 16:50:05 +02003172 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003173 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003175 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 }
3177 break;
3178
3179 case ISN_ADDLIST:
3180 case ISN_ADDBLOB:
3181 {
3182 typval_T *tv1 = STACK_TV_BOT(-2);
3183 typval_T *tv2 = STACK_TV_BOT(-1);
3184
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003185 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 if (iptr->isn_type == ISN_ADDLIST)
3187 eval_addlist(tv1, tv2);
3188 else
3189 eval_addblob(tv1, tv2);
3190 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003191 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 }
3193 break;
3194
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003195 case ISN_LISTAPPEND:
3196 {
3197 typval_T *tv1 = STACK_TV_BOT(-2);
3198 typval_T *tv2 = STACK_TV_BOT(-1);
3199 list_T *l = tv1->vval.v_list;
3200
3201 // add an item to a list
3202 if (l == NULL)
3203 {
3204 SOURCING_LNUM = iptr->isn_lnum;
3205 emsg(_(e_cannot_add_to_null_list));
3206 goto on_error;
3207 }
3208 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003209 return FAIL;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003210 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003211 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003212 }
3213 break;
3214
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003215 case ISN_BLOBAPPEND:
3216 {
3217 typval_T *tv1 = STACK_TV_BOT(-2);
3218 typval_T *tv2 = STACK_TV_BOT(-1);
3219 blob_T *b = tv1->vval.v_blob;
3220 int error = FALSE;
3221 varnumber_T n;
3222
3223 // add a number to a blob
3224 if (b == NULL)
3225 {
3226 SOURCING_LNUM = iptr->isn_lnum;
3227 emsg(_(e_cannot_add_to_null_blob));
3228 goto on_error;
3229 }
3230 n = tv_get_number_chk(tv2, &error);
3231 if (error)
3232 goto on_error;
3233 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003234 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003235 }
3236 break;
3237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 // Computation with two arguments of unknown type
3239 case ISN_OPANY:
3240 {
3241 typval_T *tv1 = STACK_TV_BOT(-2);
3242 typval_T *tv2 = STACK_TV_BOT(-1);
3243 varnumber_T n1, n2;
3244#ifdef FEAT_FLOAT
3245 float_T f1 = 0, f2 = 0;
3246#endif
3247 int error = FALSE;
3248
3249 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3250 {
3251 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3252 {
3253 eval_addlist(tv1, tv2);
3254 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003255 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 break;
3257 }
3258 else if (tv1->v_type == VAR_BLOB
3259 && tv2->v_type == VAR_BLOB)
3260 {
3261 eval_addblob(tv1, tv2);
3262 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003263 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 break;
3265 }
3266 }
3267#ifdef FEAT_FLOAT
3268 if (tv1->v_type == VAR_FLOAT)
3269 {
3270 f1 = tv1->vval.v_float;
3271 n1 = 0;
3272 }
3273 else
3274#endif
3275 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003276 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 n1 = tv_get_number_chk(tv1, &error);
3278 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003279 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280#ifdef FEAT_FLOAT
3281 if (tv2->v_type == VAR_FLOAT)
3282 f1 = n1;
3283#endif
3284 }
3285#ifdef FEAT_FLOAT
3286 if (tv2->v_type == VAR_FLOAT)
3287 {
3288 f2 = tv2->vval.v_float;
3289 n2 = 0;
3290 }
3291 else
3292#endif
3293 {
3294 n2 = tv_get_number_chk(tv2, &error);
3295 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003296 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297#ifdef FEAT_FLOAT
3298 if (tv1->v_type == VAR_FLOAT)
3299 f2 = n2;
3300#endif
3301 }
3302#ifdef FEAT_FLOAT
3303 // if there is a float on either side the result is a float
3304 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3305 {
3306 switch (iptr->isn_arg.op.op_type)
3307 {
3308 case EXPR_MULT: f1 = f1 * f2; break;
3309 case EXPR_DIV: f1 = f1 / f2; break;
3310 case EXPR_SUB: f1 = f1 - f2; break;
3311 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003312 default: SOURCING_LNUM = iptr->isn_lnum;
3313 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003314 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 }
3316 clear_tv(tv1);
3317 clear_tv(tv2);
3318 tv1->v_type = VAR_FLOAT;
3319 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003320 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 }
3322 else
3323#endif
3324 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003325 int failed = FALSE;
3326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 switch (iptr->isn_arg.op.op_type)
3328 {
3329 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003330 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3331 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003332 goto on_error;
3333 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 case EXPR_SUB: n1 = n1 - n2; break;
3335 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003336 default: n1 = num_modulus(n1, n2, &failed);
3337 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003338 goto on_error;
3339 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 }
3341 clear_tv(tv1);
3342 clear_tv(tv2);
3343 tv1->v_type = VAR_NUMBER;
3344 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003345 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 }
3347 }
3348 break;
3349
3350 case ISN_CONCAT:
3351 {
3352 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3353 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3354 char_u *res;
3355
3356 res = concat_str(str1, str2);
3357 clear_tv(STACK_TV_BOT(-2));
3358 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003359 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 STACK_TV_BOT(-1)->vval.v_string = res;
3361 }
3362 break;
3363
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003364 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003365 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003366 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003367 int is_slice = iptr->isn_type == ISN_STRSLICE;
3368 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003369 char_u *res;
3370
3371 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003372 // string slice: string is at stack-3, first index at
3373 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003374 if (is_slice)
3375 {
3376 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003377 n1 = tv->vval.v_number;
3378 }
3379
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003380 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003381 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003382
Bram Moolenaar4c137212021-04-19 16:48:48 +02003383 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003384 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003385 if (is_slice)
3386 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003387 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003388 else
3389 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003390 // single character (including composing characters).
3391 // If the index is too big or negative the result is
3392 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003393 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003394 vim_free(tv->vval.v_string);
3395 tv->vval.v_string = res;
3396 }
3397 break;
3398
3399 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003400 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003401 case ISN_BLOBINDEX:
3402 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003404 int is_slice = iptr->isn_type == ISN_LISTSLICE
3405 || iptr->isn_type == ISN_BLOBSLICE;
3406 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3407 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003408 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003409 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410
3411 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003412 // list slice: list is at stack-3, indexes at stack-2 and
3413 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003414 // Same for blob.
3415 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416
3417 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003418 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003420
3421 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 {
Bram Moolenaared591872020-08-15 22:14:53 +02003423 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003424 n1 = tv->vval.v_number;
3425 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 }
Bram Moolenaared591872020-08-15 22:14:53 +02003427
Bram Moolenaar4c137212021-04-19 16:48:48 +02003428 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003429 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003430 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003431 if (is_blob)
3432 {
3433 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3434 n1, n2, FALSE, tv) == FAIL)
3435 goto on_error;
3436 }
3437 else
3438 {
3439 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3440 n1, n2, FALSE, tv, TRUE) == FAIL)
3441 goto on_error;
3442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 }
3444 break;
3445
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003446 case ISN_ANYINDEX:
3447 case ISN_ANYSLICE:
3448 {
3449 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3450 typval_T *var1, *var2;
3451 int res;
3452
3453 // index: composite is at stack-2, index at stack-1
3454 // slice: composite is at stack-3, indexes at stack-2 and
3455 // stack-1
3456 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003457 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003458 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3459 goto on_error;
3460 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3461 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003462 res = eval_index_inner(tv, is_slice, var1, var2,
3463 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003464 clear_tv(var1);
3465 if (is_slice)
3466 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003467 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003468 if (res == FAIL)
3469 goto on_error;
3470 }
3471 break;
3472
Bram Moolenaar9af78762020-06-16 11:34:42 +02003473 case ISN_SLICE:
3474 {
3475 list_T *list;
3476 int count = iptr->isn_arg.number;
3477
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003478 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003479 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003480 list = tv->vval.v_list;
3481
3482 // no error for short list, expect it to be checked earlier
3483 if (list != NULL && list->lv_len >= count)
3484 {
3485 list_T *newlist = list_slice(list,
3486 count, list->lv_len - 1);
3487
3488 if (newlist != NULL)
3489 {
3490 list_unref(list);
3491 tv->vval.v_list = newlist;
3492 ++newlist->lv_refcount;
3493 }
3494 }
3495 }
3496 break;
3497
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003498 case ISN_GETITEM:
3499 {
3500 listitem_T *li;
3501 int index = iptr->isn_arg.number;
3502
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003503 // Get list item: list is at stack-1, push item.
3504 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003505 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003506 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003507
Bram Moolenaar4c137212021-04-19 16:48:48 +02003508 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3509 return FAIL;
3510 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003511 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003512
3513 // Useful when used in unpack assignment. Reset at
3514 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003515 ectx->ec_where.wt_index = index + 1;
3516 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003517 }
3518 break;
3519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 case ISN_MEMBER:
3521 {
3522 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003523 char_u *key;
3524 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003525 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003526
3527 // dict member: dict is at stack-2, key at stack-1
3528 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003529 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003530 dict = tv->vval.v_dict;
3531
3532 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003533 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003534 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003535 if (key == NULL)
3536 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003537
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003538 if ((di = dict_find(dict, key, -1)) == NULL)
3539 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003540 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003541 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003542
3543 // If :silent! is used we will continue, make sure the
3544 // stack contents makes sense.
3545 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003546 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003547 tv = STACK_TV_BOT(-1);
3548 clear_tv(tv);
3549 tv->v_type = VAR_NUMBER;
3550 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003551 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003552 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003553 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003554 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003555 // Clear the dict only after getting the item, to avoid
3556 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003557 tv = STACK_TV_BOT(-1);
3558 temp_tv = *tv;
3559 copy_tv(&di->di_tv, tv);
3560 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003561 }
3562 break;
3563
3564 // dict member with string key
3565 case ISN_STRINGMEMBER:
3566 {
3567 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003569 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570
3571 tv = STACK_TV_BOT(-1);
3572 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3573 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003574 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003576 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 }
3578 dict = tv->vval.v_dict;
3579
3580 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3581 == NULL)
3582 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003583 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003585 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003587 // Clear the dict after getting the item, to avoid that it
3588 // make the item invalid.
3589 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003591 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 }
3593 break;
3594
3595 case ISN_NEGATENR:
3596 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003597 if (tv->v_type != VAR_NUMBER
3598#ifdef FEAT_FLOAT
3599 && tv->v_type != VAR_FLOAT
3600#endif
3601 )
3602 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003603 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003604 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003605 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003606 }
3607#ifdef FEAT_FLOAT
3608 if (tv->v_type == VAR_FLOAT)
3609 tv->vval.v_float = -tv->vval.v_float;
3610 else
3611#endif
3612 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 break;
3614
3615 case ISN_CHECKNR:
3616 {
3617 int error = FALSE;
3618
3619 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003620 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003622 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 (void)tv_get_number_chk(tv, &error);
3624 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003625 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 }
3627 break;
3628
3629 case ISN_CHECKTYPE:
3630 {
3631 checktype_T *ct = &iptr->isn_arg.type;
3632
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003633 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003634 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003635 if (!ectx->ec_where.wt_variable)
3636 ectx->ec_where.wt_index = ct->ct_arg_idx;
3637 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3638 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003639 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003640 if (!ectx->ec_where.wt_variable)
3641 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003642
3643 // number 0 is FALSE, number 1 is TRUE
3644 if (tv->v_type == VAR_NUMBER
3645 && ct->ct_type->tt_type == VAR_BOOL
3646 && (tv->vval.v_number == 0
3647 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003649 tv->v_type = VAR_BOOL;
3650 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003651 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 }
3653 }
3654 break;
3655
Bram Moolenaar9af78762020-06-16 11:34:42 +02003656 case ISN_CHECKLEN:
3657 {
3658 int min_len = iptr->isn_arg.checklen.cl_min_len;
3659 list_T *list = NULL;
3660
3661 tv = STACK_TV_BOT(-1);
3662 if (tv->v_type == VAR_LIST)
3663 list = tv->vval.v_list;
3664 if (list == NULL || list->lv_len < min_len
3665 || (list->lv_len > min_len
3666 && !iptr->isn_arg.checklen.cl_more_OK))
3667 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003668 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003669 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003670 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003671 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003672 }
3673 }
3674 break;
3675
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003676 case ISN_SETTYPE:
3677 {
3678 checktype_T *ct = &iptr->isn_arg.type;
3679
3680 tv = STACK_TV_BOT(-1);
3681 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3682 {
3683 free_type(tv->vval.v_dict->dv_type);
3684 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3685 }
3686 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3687 {
3688 free_type(tv->vval.v_list->lv_type);
3689 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3690 }
3691 }
3692 break;
3693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003695 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 {
3697 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003698 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
3700 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003701 if (iptr->isn_type == ISN_2BOOL)
3702 {
3703 n = tv2bool(tv);
3704 if (iptr->isn_arg.number) // invert
3705 n = !n;
3706 }
3707 else
3708 {
3709 SOURCING_LNUM = iptr->isn_lnum;
3710 n = tv_get_bool_chk(tv, &error);
3711 if (error)
3712 goto on_error;
3713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 clear_tv(tv);
3715 tv->v_type = VAR_BOOL;
3716 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3717 }
3718 break;
3719
3720 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003721 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003722 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003723 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3724 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3725 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 break;
3727
Bram Moolenaar08597872020-12-10 19:43:40 +01003728 case ISN_RANGE:
3729 {
3730 exarg_T ea;
3731 char *errormsg;
3732
Bram Moolenaarece0b872021-01-08 20:40:45 +01003733 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003734 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003735 ea.addr_type = ADDR_LINES;
3736 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003737 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003738 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003739 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003740
Bram Moolenaar4c137212021-04-19 16:48:48 +02003741 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3742 return FAIL;
3743 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003744 tv = STACK_TV_BOT(-1);
3745 tv->v_type = VAR_NUMBER;
3746 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003747 if (ea.addr_count == 0)
3748 tv->vval.v_number = curwin->w_cursor.lnum;
3749 else
3750 tv->vval.v_number = ea.line2;
3751 }
3752 break;
3753
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003754 case ISN_PUT:
3755 {
3756 int regname = iptr->isn_arg.put.put_regname;
3757 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3758 char_u *expr = NULL;
3759 int dir = FORWARD;
3760
Bram Moolenaar08597872020-12-10 19:43:40 +01003761 if (lnum < -2)
3762 {
3763 // line number was put on the stack by ISN_RANGE
3764 tv = STACK_TV_BOT(-1);
3765 curwin->w_cursor.lnum = tv->vval.v_number;
3766 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3767 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003768 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003769 }
3770 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003771 // :put! above cursor
3772 dir = BACKWARD;
3773 else if (lnum >= 0)
3774 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003775
3776 if (regname == '=')
3777 {
3778 tv = STACK_TV_BOT(-1);
3779 if (tv->v_type == VAR_STRING)
3780 expr = tv->vval.v_string;
3781 else
3782 {
3783 expr = typval2string(tv, TRUE); // allocates value
3784 clear_tv(tv);
3785 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003786 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003787 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003788 check_cursor();
3789 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3790 vim_free(expr);
3791 }
3792 break;
3793
Bram Moolenaar02194d22020-10-24 23:08:38 +02003794 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003795 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3796 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3797 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3798 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003799 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3800 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003801 break;
3802
Bram Moolenaar02194d22020-10-24 23:08:38 +02003803 case ISN_CMDMOD_REV:
3804 // filter regprog is owned by the instruction, don't free it
3805 cmdmod.cmod_filter_regmatch.regprog = NULL;
3806 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003807 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
3808 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003809 break;
3810
Bram Moolenaar792f7862020-11-23 08:31:18 +01003811 case ISN_UNPACK:
3812 {
3813 int count = iptr->isn_arg.unpack.unp_count;
3814 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3815 list_T *l;
3816 listitem_T *li;
3817 int i;
3818
3819 // Check there is a valid list to unpack.
3820 tv = STACK_TV_BOT(-1);
3821 if (tv->v_type != VAR_LIST)
3822 {
3823 SOURCING_LNUM = iptr->isn_lnum;
3824 emsg(_(e_for_argument_must_be_sequence_of_lists));
3825 goto on_error;
3826 }
3827 l = tv->vval.v_list;
3828 if (l == NULL
3829 || l->lv_len < (semicolon ? count - 1 : count))
3830 {
3831 SOURCING_LNUM = iptr->isn_lnum;
3832 emsg(_(e_list_value_does_not_have_enough_items));
3833 goto on_error;
3834 }
3835 else if (!semicolon && l->lv_len > count)
3836 {
3837 SOURCING_LNUM = iptr->isn_lnum;
3838 emsg(_(e_list_value_has_more_items_than_targets));
3839 goto on_error;
3840 }
3841
3842 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003843 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
3844 return FAIL;
3845 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003846
3847 // Variable after semicolon gets a list with the remaining
3848 // items.
3849 if (semicolon)
3850 {
3851 list_T *rem_list =
3852 list_alloc_with_items(l->lv_len - count + 1);
3853
3854 if (rem_list == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003855 return FAIL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003856 tv = STACK_TV_BOT(-count);
3857 tv->vval.v_list = rem_list;
3858 ++rem_list->lv_refcount;
3859 tv->v_lock = 0;
3860 li = l->lv_first;
3861 for (i = 0; i < count - 1; ++i)
3862 li = li->li_next;
3863 for (i = 0; li != NULL; ++i)
3864 {
3865 list_set_item(rem_list, i, &li->li_tv);
3866 li = li->li_next;
3867 }
3868 --count;
3869 }
3870
3871 // Produce the values in reverse order, first item last.
3872 li = l->lv_first;
3873 for (i = 0; i < count; ++i)
3874 {
3875 tv = STACK_TV_BOT(-i - 1);
3876 copy_tv(&li->li_tv, tv);
3877 li = li->li_next;
3878 }
3879
3880 list_unref(l);
3881 }
3882 break;
3883
Bram Moolenaarb2049902021-01-24 12:53:53 +01003884 case ISN_PROF_START:
3885 case ISN_PROF_END:
3886 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003887#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003888 funccall_T cookie;
3889 ufunc_T *cur_ufunc =
3890 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003891 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003892
3893 cookie.func = cur_ufunc;
3894 if (iptr->isn_type == ISN_PROF_START)
3895 {
3896 func_line_start(&cookie, iptr->isn_lnum);
3897 // if we get here the instruction is executed
3898 func_line_exec(&cookie);
3899 }
3900 else
3901 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003902#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003903 }
3904 break;
3905
Bram Moolenaar389df252020-07-09 21:20:47 +02003906 case ISN_SHUFFLE:
3907 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003908 typval_T tmp_tv;
3909 int item = iptr->isn_arg.shuffle.shfl_item;
3910 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003911
3912 tmp_tv = *STACK_TV_BOT(-item);
3913 for ( ; up > 0 && item > 1; --up)
3914 {
3915 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3916 --item;
3917 }
3918 *STACK_TV_BOT(-item) = tmp_tv;
3919 }
3920 break;
3921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003923 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003925 ectx->ec_where.wt_index = 0;
3926 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 break;
3928 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003929 continue;
3930
Bram Moolenaard032f342020-07-18 18:13:02 +02003931func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003932 // Restore previous function. If the frame pointer is where we started
3933 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003934 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003935 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003936
Bram Moolenaar4c137212021-04-19 16:48:48 +02003937 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003938 // only fails when out of memory
Bram Moolenaar4c137212021-04-19 16:48:48 +02003939 return FAIL;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003940 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003941
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003942on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003943 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003944 // If "emsg_silent" is set then ignore the error, unless it was set
3945 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003946 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003947 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003948 {
3949 // If a sequence of instructions causes an error while ":silent!"
3950 // was used, restore the stack length and jump ahead to restoring
3951 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003952 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003953 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 while (ectx->ec_stack.ga_len
3955 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003956 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003957 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003958 clear_tv(STACK_TV_BOT(0));
3959 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003960 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
3961 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003962 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003963 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003964 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003965on_fatal_error:
3966 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003967 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003968 if (trylevel <= ectx->ec_trylevel_at_start)
3969 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 }
3971
3972done:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003973 return OK;
3974}
3975
3976/*
3977 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
3978 * "substitute_instr".
3979 */
3980 char_u *
3981exe_substitute_instr(void)
3982{
3983 ectx_T *ectx = substitute_instr->subs_ectx;
3984 isn_T *save_instr = ectx->ec_instr;
3985 int save_iidx = ectx->ec_iidx;
3986 char_u *res;
3987
3988 ectx->ec_instr = substitute_instr->subs_instr;
3989 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02003990 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003991 typval_T *tv = STACK_TV_BOT(-1);
3992
3993 res = vim_strsave(tv_get_string(tv));
3994 --ectx->ec_stack.ga_len;
3995 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02003996 }
3997 else
3998 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003999 substitute_instr->subs_status = FAIL;
4000 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002
Bram Moolenaar4c137212021-04-19 16:48:48 +02004003 ectx->ec_instr = save_instr;
4004 ectx->ec_iidx = save_iidx;
4005
4006 return res;
4007}
4008
4009/*
4010 * Call a "def" function from old Vim script.
4011 * Return OK or FAIL.
4012 */
4013 int
4014call_def_function(
4015 ufunc_T *ufunc,
4016 int argc_arg, // nr of arguments
4017 typval_T *argv, // arguments
4018 partial_T *partial, // optional partial for context
4019 typval_T *rettv) // return value
4020{
4021 ectx_T ectx; // execution context
4022 int argc = argc_arg;
4023 typval_T *tv;
4024 int idx;
4025 int ret = FAIL;
4026 int defcount = ufunc->uf_args.ga_len - argc;
4027 sctx_T save_current_sctx = current_sctx;
4028 int did_emsg_before = did_emsg_cumul + did_emsg;
4029 int save_suppress_errthrow = suppress_errthrow;
4030 msglist_T **saved_msg_list = NULL;
4031 msglist_T *private_msg_list = NULL;
4032 int save_emsg_silent_def = emsg_silent_def;
4033 int save_did_emsg_def = did_emsg_def;
4034 int orig_funcdepth;
4035
4036// Get pointer to item in the stack.
4037#undef STACK_TV
4038#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4039
4040// Get pointer to item at the bottom of the stack, -1 is the bottom.
4041#undef STACK_TV_BOT
4042#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4043
4044// Get pointer to a local variable on the stack. Negative for arguments.
4045#undef STACK_TV_VAR
4046#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4047
4048 if (ufunc->uf_def_status == UF_NOT_COMPILED
4049 || ufunc->uf_def_status == UF_COMPILE_ERROR
4050 || (func_needs_compiling(ufunc, PROFILING(ufunc))
4051 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
4052 == FAIL))
4053 {
4054 if (did_emsg_cumul + did_emsg == did_emsg_before)
4055 semsg(_(e_function_is_not_compiled_str),
4056 printable_func_name(ufunc));
4057 return FAIL;
4058 }
4059
4060 {
4061 // Check the function was really compiled.
4062 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4063 + ufunc->uf_dfunc_idx;
4064 if (INSTRUCTIONS(dfunc) == NULL)
4065 {
4066 iemsg("using call_def_function() on not compiled function");
4067 return FAIL;
4068 }
4069 }
4070
4071 // If depth of calling is getting too high, don't execute the function.
4072 orig_funcdepth = funcdepth_get();
4073 if (funcdepth_increment() == FAIL)
4074 return FAIL;
4075
4076 CLEAR_FIELD(ectx);
4077 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4078 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4079 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4080 {
4081 funcdepth_decrement();
4082 return FAIL;
4083 }
4084 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4085 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4086 ectx.ec_did_emsg_before = did_emsg_before;
4087 ectx.ec_trylevel_at_start = trylevel;
4088
4089 idx = argc - ufunc->uf_args.ga_len;
4090 if (idx > 0 && ufunc->uf_va_name == NULL)
4091 {
4092 if (idx == 1)
4093 emsg(_(e_one_argument_too_many));
4094 else
4095 semsg(_(e_nr_arguments_too_many), idx);
4096 goto failed_early;
4097 }
4098
4099 // Put arguments on the stack, but no more than what the function expects.
4100 // A lambda can be called with more arguments than it uses.
4101 for (idx = 0; idx < argc
4102 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4103 ++idx)
4104 {
4105 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4106 && argv[idx].v_type == VAR_SPECIAL
4107 && argv[idx].vval.v_number == VVAL_NONE)
4108 {
4109 // Use the default value.
4110 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4111 }
4112 else
4113 {
4114 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4115 && check_typval_arg_type(
4116 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4117 goto failed_early;
4118 copy_tv(&argv[idx], STACK_TV_BOT(0));
4119 }
4120 ++ectx.ec_stack.ga_len;
4121 }
4122
4123 // Turn varargs into a list. Empty list if no args.
4124 if (ufunc->uf_va_name != NULL)
4125 {
4126 int vararg_count = argc - ufunc->uf_args.ga_len;
4127
4128 if (vararg_count < 0)
4129 vararg_count = 0;
4130 else
4131 argc -= vararg_count;
4132 if (exe_newlist(vararg_count, &ectx) == FAIL)
4133 goto failed_early;
4134
4135 // Check the type of the list items.
4136 tv = STACK_TV_BOT(-1);
4137 if (ufunc->uf_va_type != NULL
4138 && ufunc->uf_va_type != &t_list_any
4139 && ufunc->uf_va_type->tt_member != &t_any
4140 && tv->vval.v_list != NULL)
4141 {
4142 type_T *expected = ufunc->uf_va_type->tt_member;
4143 listitem_T *li = tv->vval.v_list->lv_first;
4144
4145 for (idx = 0; idx < vararg_count; ++idx)
4146 {
4147 if (check_typval_arg_type(expected, &li->li_tv,
4148 argc + idx + 1) == FAIL)
4149 goto failed_early;
4150 li = li->li_next;
4151 }
4152 }
4153
4154 if (defcount > 0)
4155 // Move varargs list to below missing default arguments.
4156 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4157 --ectx.ec_stack.ga_len;
4158 }
4159
4160 // Make space for omitted arguments, will store default value below.
4161 // Any varargs list goes after them.
4162 if (defcount > 0)
4163 for (idx = 0; idx < defcount; ++idx)
4164 {
4165 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4166 ++ectx.ec_stack.ga_len;
4167 }
4168 if (ufunc->uf_va_name != NULL)
4169 ++ectx.ec_stack.ga_len;
4170
4171 // Frame pointer points to just after arguments.
4172 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4173 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4174
4175 {
4176 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4177 + ufunc->uf_dfunc_idx;
4178 ufunc_T *base_ufunc = dfunc->df_ufunc;
4179
4180 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4181 // by copy_func().
4182 if (partial != NULL || base_ufunc->uf_partial != NULL)
4183 {
4184 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
4185 if (ectx.ec_outer == NULL)
4186 goto failed_early;
4187 if (partial != NULL)
4188 {
4189 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4190 {
4191 if (current_ectx->ec_outer != NULL)
4192 *ectx.ec_outer = *current_ectx->ec_outer;
4193 }
4194 else
4195 *ectx.ec_outer = partial->pt_outer;
4196 }
4197 else
4198 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
4199 ectx.ec_outer->out_up_is_copy = TRUE;
4200 }
4201 }
4202
4203 // dummy frame entries
4204 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4205 {
4206 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4207 ++ectx.ec_stack.ga_len;
4208 }
4209
4210 {
4211 // Reserve space for local variables and any closure reference count.
4212 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4213 + ufunc->uf_dfunc_idx;
4214
4215 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4216 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4217 ectx.ec_stack.ga_len += dfunc->df_varcount;
4218 if (dfunc->df_has_closure)
4219 {
4220 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4221 STACK_TV_VAR(idx)->vval.v_number = 0;
4222 ++ectx.ec_stack.ga_len;
4223 }
4224
4225 ectx.ec_instr = INSTRUCTIONS(dfunc);
4226 }
4227
4228 // Following errors are in the function, not the caller.
4229 // Commands behave like vim9script.
4230 estack_push_ufunc(ufunc, 1);
4231 current_sctx = ufunc->uf_script_ctx;
4232 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4233
4234 // Use a specific location for storing error messages to be converted to an
4235 // exception.
4236 saved_msg_list = msg_list;
4237 msg_list = &private_msg_list;
4238
4239 // Do turn errors into exceptions.
4240 suppress_errthrow = FALSE;
4241
4242 // Do not delete the function while executing it.
4243 ++ufunc->uf_calls;
4244
4245 // When ":silent!" was used before calling then we still abort the
4246 // function. If ":silent!" is used in the function then we don't.
4247 emsg_silent_def = emsg_silent;
4248 did_emsg_def = 0;
4249
4250 ectx.ec_where.wt_index = 0;
4251 ectx.ec_where.wt_variable = FALSE;
4252
4253 // Execute the instructions until done.
4254 ret = exec_instructions(&ectx);
4255 if (ret == OK)
4256 {
4257 // function finished, get result from the stack.
4258 if (ufunc->uf_ret_type == &t_void)
4259 {
4260 rettv->v_type = VAR_VOID;
4261 }
4262 else
4263 {
4264 tv = STACK_TV_BOT(-1);
4265 *rettv = *tv;
4266 tv->v_type = VAR_UNKNOWN;
4267 }
4268 }
4269
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004270 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004271 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4272 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004273
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004274 // Deal with any remaining closures, they may be in use somewhere.
4275 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004276 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004277 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004278 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4279 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004280
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004281 estack_pop();
4282 current_sctx = save_current_sctx;
4283
Bram Moolenaarc970e422021-03-17 15:03:04 +01004284 // TODO: when is it safe to delete the function if it is no longer used?
4285 --ufunc->uf_calls;
4286
Bram Moolenaar352134b2020-10-17 22:04:08 +02004287 if (*msg_list != NULL && saved_msg_list != NULL)
4288 {
4289 msglist_T **plist = saved_msg_list;
4290
4291 // Append entries from the current msg_list (uncaught exceptions) to
4292 // the saved msg_list.
4293 while (*plist != NULL)
4294 plist = &(*plist)->next;
4295
4296 *plist = *msg_list;
4297 }
4298 msg_list = saved_msg_list;
4299
Bram Moolenaar4c137212021-04-19 16:48:48 +02004300 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004301 {
4302 cmdmod.cmod_filter_regmatch.regprog = NULL;
4303 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004304 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004305 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004306 emsg_silent_def = save_emsg_silent_def;
4307 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004308
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004309failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004310 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4312 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004315 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004316
Bram Moolenaar0186e582021-01-10 18:33:11 +01004317 while (ectx.ec_outer != NULL)
4318 {
4319 outer_T *up = ectx.ec_outer->out_up_is_copy
4320 ? NULL : ectx.ec_outer->out_up;
4321
4322 vim_free(ectx.ec_outer);
4323 ectx.ec_outer = up;
4324 }
4325
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004326 // Not sure if this is necessary.
4327 suppress_errthrow = save_suppress_errthrow;
4328
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004329 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004330 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004331 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004332 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 return ret;
4334}
4335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004337 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4338 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4339 * "pfx" is prefixed to every line.
4340 */
4341 static void
4342list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4343{
4344 int line_idx = 0;
4345 int prev_current = 0;
4346 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004347 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004348
4349 for (current = 0; current < instr_count; ++current)
4350 {
4351 isn_T *iptr = &instr[current];
4352 char *line;
4353
4354 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004355 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004356 while (line_idx < iptr->isn_lnum
4357 && line_idx < ufunc->uf_lines.ga_len)
4358 {
4359 if (current > prev_current)
4360 {
4361 msg_puts("\n\n");
4362 prev_current = current;
4363 }
4364 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4365 if (line != NULL)
4366 msg(line);
4367 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004368 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4369 {
4370 int first_def_arg = ufunc->uf_args.ga_len
4371 - ufunc->uf_def_args.ga_len;
4372
4373 if (def_arg_idx > 0)
4374 msg_puts("\n\n");
4375 msg_start();
4376 msg_puts(" ");
4377 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4378 first_def_arg + def_arg_idx]);
4379 msg_puts(" = ");
4380 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4381 msg_clr_eos();
4382 msg_end();
4383 }
4384 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385
4386 switch (iptr->isn_type)
4387 {
4388 case ISN_EXEC:
4389 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4390 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004391 case ISN_REDIRSTART:
4392 smsg("%s%4d REDIR", pfx, current);
4393 break;
4394 case ISN_REDIREND:
4395 smsg("%s%4d REDIR END%s", pfx, current,
4396 iptr->isn_arg.number ? " append" : "");
4397 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004398 case ISN_SUBSTITUTE:
4399 {
4400 subs_T *subs = &iptr->isn_arg.subs;
4401
4402 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4403 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4404 msg(" -------------");
4405 }
4406 break;
4407 case ISN_EXECCONCAT:
4408 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4409 (varnumber_T)iptr->isn_arg.number);
4410 break;
4411 case ISN_ECHO:
4412 {
4413 echo_T *echo = &iptr->isn_arg.echo;
4414
4415 smsg("%s%4d %s %d", pfx, current,
4416 echo->echo_with_white ? "ECHO" : "ECHON",
4417 echo->echo_count);
4418 }
4419 break;
4420 case ISN_EXECUTE:
4421 smsg("%s%4d EXECUTE %lld", pfx, current,
4422 (varnumber_T)(iptr->isn_arg.number));
4423 break;
4424 case ISN_ECHOMSG:
4425 smsg("%s%4d ECHOMSG %lld", pfx, current,
4426 (varnumber_T)(iptr->isn_arg.number));
4427 break;
4428 case ISN_ECHOERR:
4429 smsg("%s%4d ECHOERR %lld", pfx, current,
4430 (varnumber_T)(iptr->isn_arg.number));
4431 break;
4432 case ISN_LOAD:
4433 {
4434 if (iptr->isn_arg.number < 0)
4435 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4436 (varnumber_T)(iptr->isn_arg.number
4437 + STACK_FRAME_SIZE));
4438 else
4439 smsg("%s%4d LOAD $%lld", pfx, current,
4440 (varnumber_T)(iptr->isn_arg.number));
4441 }
4442 break;
4443 case ISN_LOADOUTER:
4444 {
4445 if (iptr->isn_arg.number < 0)
4446 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4447 iptr->isn_arg.outer.outer_depth,
4448 iptr->isn_arg.outer.outer_idx
4449 + STACK_FRAME_SIZE);
4450 else
4451 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4452 iptr->isn_arg.outer.outer_depth,
4453 iptr->isn_arg.outer.outer_idx);
4454 }
4455 break;
4456 case ISN_LOADV:
4457 smsg("%s%4d LOADV v:%s", pfx, current,
4458 get_vim_var_name(iptr->isn_arg.number));
4459 break;
4460 case ISN_LOADSCRIPT:
4461 {
4462 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4463 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4464 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4465 + sref->sref_idx;
4466
4467 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4468 sv->sv_name,
4469 sref->sref_idx,
4470 si->sn_name);
4471 }
4472 break;
4473 case ISN_LOADS:
4474 {
4475 scriptitem_T *si = SCRIPT_ITEM(
4476 iptr->isn_arg.loadstore.ls_sid);
4477
4478 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4479 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4480 }
4481 break;
4482 case ISN_LOADAUTO:
4483 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4484 break;
4485 case ISN_LOADG:
4486 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4487 break;
4488 case ISN_LOADB:
4489 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4490 break;
4491 case ISN_LOADW:
4492 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4493 break;
4494 case ISN_LOADT:
4495 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4496 break;
4497 case ISN_LOADGDICT:
4498 smsg("%s%4d LOAD g:", pfx, current);
4499 break;
4500 case ISN_LOADBDICT:
4501 smsg("%s%4d LOAD b:", pfx, current);
4502 break;
4503 case ISN_LOADWDICT:
4504 smsg("%s%4d LOAD w:", pfx, current);
4505 break;
4506 case ISN_LOADTDICT:
4507 smsg("%s%4d LOAD t:", pfx, current);
4508 break;
4509 case ISN_LOADOPT:
4510 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4511 break;
4512 case ISN_LOADENV:
4513 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4514 break;
4515 case ISN_LOADREG:
4516 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4517 break;
4518
4519 case ISN_STORE:
4520 if (iptr->isn_arg.number < 0)
4521 smsg("%s%4d STORE arg[%lld]", pfx, current,
4522 iptr->isn_arg.number + STACK_FRAME_SIZE);
4523 else
4524 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4525 break;
4526 case ISN_STOREOUTER:
4527 {
4528 if (iptr->isn_arg.number < 0)
4529 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4530 iptr->isn_arg.outer.outer_depth,
4531 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4532 else
4533 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4534 iptr->isn_arg.outer.outer_depth,
4535 iptr->isn_arg.outer.outer_idx);
4536 }
4537 break;
4538 case ISN_STOREV:
4539 smsg("%s%4d STOREV v:%s", pfx, current,
4540 get_vim_var_name(iptr->isn_arg.number));
4541 break;
4542 case ISN_STOREAUTO:
4543 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4544 break;
4545 case ISN_STOREG:
4546 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4547 break;
4548 case ISN_STOREB:
4549 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4550 break;
4551 case ISN_STOREW:
4552 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4553 break;
4554 case ISN_STORET:
4555 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4556 break;
4557 case ISN_STORES:
4558 {
4559 scriptitem_T *si = SCRIPT_ITEM(
4560 iptr->isn_arg.loadstore.ls_sid);
4561
4562 smsg("%s%4d STORES %s in %s", pfx, current,
4563 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4564 }
4565 break;
4566 case ISN_STORESCRIPT:
4567 {
4568 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4569 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4570 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4571 + sref->sref_idx;
4572
4573 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4574 sv->sv_name,
4575 sref->sref_idx,
4576 si->sn_name);
4577 }
4578 break;
4579 case ISN_STOREOPT:
4580 smsg("%s%4d STOREOPT &%s", pfx, current,
4581 iptr->isn_arg.storeopt.so_name);
4582 break;
4583 case ISN_STOREENV:
4584 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4585 break;
4586 case ISN_STOREREG:
4587 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4588 break;
4589 case ISN_STORENR:
4590 smsg("%s%4d STORE %lld in $%d", pfx, current,
4591 iptr->isn_arg.storenr.stnr_val,
4592 iptr->isn_arg.storenr.stnr_idx);
4593 break;
4594
4595 case ISN_STOREINDEX:
4596 smsg("%s%4d STOREINDEX %s", pfx, current,
4597 vartype_name(iptr->isn_arg.vartype));
4598 break;
4599
4600 case ISN_STORERANGE:
4601 smsg("%s%4d STORERANGE", pfx, current);
4602 break;
4603
4604 // constants
4605 case ISN_PUSHNR:
4606 smsg("%s%4d PUSHNR %lld", pfx, current,
4607 (varnumber_T)(iptr->isn_arg.number));
4608 break;
4609 case ISN_PUSHBOOL:
4610 case ISN_PUSHSPEC:
4611 smsg("%s%4d PUSH %s", pfx, current,
4612 get_var_special_name(iptr->isn_arg.number));
4613 break;
4614 case ISN_PUSHF:
4615#ifdef FEAT_FLOAT
4616 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4617#endif
4618 break;
4619 case ISN_PUSHS:
4620 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4621 break;
4622 case ISN_PUSHBLOB:
4623 {
4624 char_u *r;
4625 char_u numbuf[NUMBUFLEN];
4626 char_u *tofree;
4627
4628 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4629 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4630 vim_free(tofree);
4631 }
4632 break;
4633 case ISN_PUSHFUNC:
4634 {
4635 char *name = (char *)iptr->isn_arg.string;
4636
4637 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4638 name == NULL ? "[none]" : name);
4639 }
4640 break;
4641 case ISN_PUSHCHANNEL:
4642#ifdef FEAT_JOB_CHANNEL
4643 {
4644 channel_T *channel = iptr->isn_arg.channel;
4645
4646 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4647 channel == NULL ? 0 : channel->ch_id);
4648 }
4649#endif
4650 break;
4651 case ISN_PUSHJOB:
4652#ifdef FEAT_JOB_CHANNEL
4653 {
4654 typval_T tv;
4655 char_u *name;
4656
4657 tv.v_type = VAR_JOB;
4658 tv.vval.v_job = iptr->isn_arg.job;
4659 name = tv_get_string(&tv);
4660 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4661 }
4662#endif
4663 break;
4664 case ISN_PUSHEXC:
4665 smsg("%s%4d PUSH v:exception", pfx, current);
4666 break;
4667 case ISN_UNLET:
4668 smsg("%s%4d UNLET%s %s", pfx, current,
4669 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4670 iptr->isn_arg.unlet.ul_name);
4671 break;
4672 case ISN_UNLETENV:
4673 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4674 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4675 iptr->isn_arg.unlet.ul_name);
4676 break;
4677 case ISN_UNLETINDEX:
4678 smsg("%s%4d UNLETINDEX", pfx, current);
4679 break;
4680 case ISN_UNLETRANGE:
4681 smsg("%s%4d UNLETRANGE", pfx, current);
4682 break;
4683 case ISN_LOCKCONST:
4684 smsg("%s%4d LOCKCONST", pfx, current);
4685 break;
4686 case ISN_NEWLIST:
4687 smsg("%s%4d NEWLIST size %lld", pfx, current,
4688 (varnumber_T)(iptr->isn_arg.number));
4689 break;
4690 case ISN_NEWDICT:
4691 smsg("%s%4d NEWDICT size %lld", pfx, current,
4692 (varnumber_T)(iptr->isn_arg.number));
4693 break;
4694
4695 // function call
4696 case ISN_BCALL:
4697 {
4698 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4699
4700 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4701 internal_func_name(cbfunc->cbf_idx),
4702 cbfunc->cbf_argcount);
4703 }
4704 break;
4705 case ISN_DCALL:
4706 {
4707 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4708 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4709 + cdfunc->cdf_idx;
4710
4711 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
4712 df->df_ufunc->uf_name_exp != NULL
4713 ? df->df_ufunc->uf_name_exp
4714 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4715 }
4716 break;
4717 case ISN_UCALL:
4718 {
4719 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4720
4721 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
4722 cufunc->cuf_name, cufunc->cuf_argcount);
4723 }
4724 break;
4725 case ISN_PCALL:
4726 {
4727 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4728
4729 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
4730 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4731 }
4732 break;
4733 case ISN_PCALL_END:
4734 smsg("%s%4d PCALL end", pfx, current);
4735 break;
4736 case ISN_RETURN:
4737 smsg("%s%4d RETURN", pfx, current);
4738 break;
4739 case ISN_RETURN_ZERO:
4740 smsg("%s%4d RETURN 0", pfx, current);
4741 break;
4742 case ISN_FUNCREF:
4743 {
4744 funcref_T *funcref = &iptr->isn_arg.funcref;
4745 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4746 + funcref->fr_func;
4747
4748 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
4749 }
4750 break;
4751
4752 case ISN_NEWFUNC:
4753 {
4754 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4755
4756 smsg("%s%4d NEWFUNC %s %s", pfx, current,
4757 newfunc->nf_lambda, newfunc->nf_global);
4758 }
4759 break;
4760
4761 case ISN_DEF:
4762 {
4763 char_u *name = iptr->isn_arg.string;
4764
4765 smsg("%s%4d DEF %s", pfx, current,
4766 name == NULL ? (char_u *)"" : name);
4767 }
4768 break;
4769
4770 case ISN_JUMP:
4771 {
4772 char *when = "?";
4773
4774 switch (iptr->isn_arg.jump.jump_when)
4775 {
4776 case JUMP_ALWAYS:
4777 when = "JUMP";
4778 break;
4779 case JUMP_AND_KEEP_IF_TRUE:
4780 when = "JUMP_AND_KEEP_IF_TRUE";
4781 break;
4782 case JUMP_IF_FALSE:
4783 when = "JUMP_IF_FALSE";
4784 break;
4785 case JUMP_AND_KEEP_IF_FALSE:
4786 when = "JUMP_AND_KEEP_IF_FALSE";
4787 break;
4788 case JUMP_IF_COND_FALSE:
4789 when = "JUMP_IF_COND_FALSE";
4790 break;
4791 case JUMP_IF_COND_TRUE:
4792 when = "JUMP_IF_COND_TRUE";
4793 break;
4794 }
4795 smsg("%s%4d %s -> %d", pfx, current, when,
4796 iptr->isn_arg.jump.jump_where);
4797 }
4798 break;
4799
4800 case ISN_JUMP_IF_ARG_SET:
4801 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
4802 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4803 iptr->isn_arg.jump.jump_where);
4804 break;
4805
4806 case ISN_FOR:
4807 {
4808 forloop_T *forloop = &iptr->isn_arg.forloop;
4809
4810 smsg("%s%4d FOR $%d -> %d", pfx, current,
4811 forloop->for_idx, forloop->for_end);
4812 }
4813 break;
4814
4815 case ISN_TRY:
4816 {
4817 try_T *try = &iptr->isn_arg.try;
4818
4819 if (try->try_ref->try_finally == 0)
4820 smsg("%s%4d TRY catch -> %d, endtry -> %d",
4821 pfx, current,
4822 try->try_ref->try_catch,
4823 try->try_ref->try_endtry);
4824 else
4825 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4826 pfx, current,
4827 try->try_ref->try_catch,
4828 try->try_ref->try_finally,
4829 try->try_ref->try_endtry);
4830 }
4831 break;
4832 case ISN_CATCH:
4833 // TODO
4834 smsg("%s%4d CATCH", pfx, current);
4835 break;
4836 case ISN_TRYCONT:
4837 {
4838 trycont_T *trycont = &iptr->isn_arg.trycont;
4839
4840 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
4841 trycont->tct_levels,
4842 trycont->tct_levels == 1 ? "" : "s",
4843 trycont->tct_where);
4844 }
4845 break;
4846 case ISN_FINALLY:
4847 smsg("%s%4d FINALLY", pfx, current);
4848 break;
4849 case ISN_ENDTRY:
4850 smsg("%s%4d ENDTRY", pfx, current);
4851 break;
4852 case ISN_THROW:
4853 smsg("%s%4d THROW", pfx, current);
4854 break;
4855
4856 // expression operations on number
4857 case ISN_OPNR:
4858 case ISN_OPFLOAT:
4859 case ISN_OPANY:
4860 {
4861 char *what;
4862 char *ins;
4863
4864 switch (iptr->isn_arg.op.op_type)
4865 {
4866 case EXPR_MULT: what = "*"; break;
4867 case EXPR_DIV: what = "/"; break;
4868 case EXPR_REM: what = "%"; break;
4869 case EXPR_SUB: what = "-"; break;
4870 case EXPR_ADD: what = "+"; break;
4871 default: what = "???"; break;
4872 }
4873 switch (iptr->isn_type)
4874 {
4875 case ISN_OPNR: ins = "OPNR"; break;
4876 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4877 case ISN_OPANY: ins = "OPANY"; break;
4878 default: ins = "???"; break;
4879 }
4880 smsg("%s%4d %s %s", pfx, current, ins, what);
4881 }
4882 break;
4883
4884 case ISN_COMPAREBOOL:
4885 case ISN_COMPARESPECIAL:
4886 case ISN_COMPARENR:
4887 case ISN_COMPAREFLOAT:
4888 case ISN_COMPARESTRING:
4889 case ISN_COMPAREBLOB:
4890 case ISN_COMPARELIST:
4891 case ISN_COMPAREDICT:
4892 case ISN_COMPAREFUNC:
4893 case ISN_COMPAREANY:
4894 {
4895 char *p;
4896 char buf[10];
4897 char *type;
4898
4899 switch (iptr->isn_arg.op.op_type)
4900 {
4901 case EXPR_EQUAL: p = "=="; break;
4902 case EXPR_NEQUAL: p = "!="; break;
4903 case EXPR_GREATER: p = ">"; break;
4904 case EXPR_GEQUAL: p = ">="; break;
4905 case EXPR_SMALLER: p = "<"; break;
4906 case EXPR_SEQUAL: p = "<="; break;
4907 case EXPR_MATCH: p = "=~"; break;
4908 case EXPR_IS: p = "is"; break;
4909 case EXPR_ISNOT: p = "isnot"; break;
4910 case EXPR_NOMATCH: p = "!~"; break;
4911 default: p = "???"; break;
4912 }
4913 STRCPY(buf, p);
4914 if (iptr->isn_arg.op.op_ic == TRUE)
4915 strcat(buf, "?");
4916 switch(iptr->isn_type)
4917 {
4918 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4919 case ISN_COMPARESPECIAL:
4920 type = "COMPARESPECIAL"; break;
4921 case ISN_COMPARENR: type = "COMPARENR"; break;
4922 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4923 case ISN_COMPARESTRING:
4924 type = "COMPARESTRING"; break;
4925 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4926 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4927 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4928 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
4929 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4930 default: type = "???"; break;
4931 }
4932
4933 smsg("%s%4d %s %s", pfx, current, type, buf);
4934 }
4935 break;
4936
4937 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
4938 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
4939
4940 // expression operations
4941 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
4942 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
4943 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
4944 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
4945 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
4946 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
4947 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
4948 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
4949 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
4950 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
4951 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
4952 case ISN_SLICE: smsg("%s%4d SLICE %lld",
4953 pfx, current, iptr->isn_arg.number); break;
4954 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
4955 pfx, current, iptr->isn_arg.number); break;
4956 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
4957 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
4958 iptr->isn_arg.string); break;
4959 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
4960
4961 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
4962 case ISN_CHECKTYPE:
4963 {
4964 checktype_T *ct = &iptr->isn_arg.type;
4965 char *tofree;
4966
4967 if (ct->ct_arg_idx == 0)
4968 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
4969 type_name(ct->ct_type, &tofree),
4970 (int)ct->ct_off);
4971 else
4972 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
4973 type_name(ct->ct_type, &tofree),
4974 (int)ct->ct_off,
4975 (int)ct->ct_arg_idx);
4976 vim_free(tofree);
4977 break;
4978 }
4979 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
4980 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4981 iptr->isn_arg.checklen.cl_min_len);
4982 break;
4983 case ISN_SETTYPE:
4984 {
4985 char *tofree;
4986
4987 smsg("%s%4d SETTYPE %s", pfx, current,
4988 type_name(iptr->isn_arg.type.ct_type, &tofree));
4989 vim_free(tofree);
4990 break;
4991 }
4992 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
4993 case ISN_2BOOL: if (iptr->isn_arg.number)
4994 smsg("%s%4d INVERT (!val)", pfx, current);
4995 else
4996 smsg("%s%4d 2BOOL (!!val)", pfx, current);
4997 break;
4998 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
4999 (varnumber_T)(iptr->isn_arg.number));
5000 break;
5001 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]", pfx, current,
5002 (varnumber_T)(iptr->isn_arg.number));
5003 break;
5004 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current, iptr->isn_arg.string);
5005 break;
5006 case ISN_PUT:
5007 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5008 smsg("%s%4d PUT %c above range",
5009 pfx, current, iptr->isn_arg.put.put_regname);
5010 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5011 smsg("%s%4d PUT %c range",
5012 pfx, current, iptr->isn_arg.put.put_regname);
5013 else
5014 smsg("%s%4d PUT %c %ld", pfx, current,
5015 iptr->isn_arg.put.put_regname,
5016 (long)iptr->isn_arg.put.put_lnum);
5017 break;
5018
5019 // TODO: summarize modifiers
5020 case ISN_CMDMOD:
5021 {
5022 char_u *buf;
5023 size_t len = produce_cmdmods(
5024 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5025
5026 buf = alloc(len + 1);
5027 if (buf != NULL)
5028 {
5029 (void)produce_cmdmods(
5030 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5031 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5032 vim_free(buf);
5033 }
5034 break;
5035 }
5036 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5037
5038 case ISN_PROF_START:
5039 smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum);
5040 break;
5041
5042 case ISN_PROF_END:
5043 smsg("%s%4d PROFILE END", pfx, current);
5044 break;
5045
5046 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5047 iptr->isn_arg.unpack.unp_count,
5048 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5049 break;
5050 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5051 iptr->isn_arg.shuffle.shfl_item,
5052 iptr->isn_arg.shuffle.shfl_up);
5053 break;
5054 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5055
5056 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5057 return;
5058 }
5059
5060 out_flush(); // output one line at a time
5061 ui_breakcheck();
5062 if (got_int)
5063 break;
5064 }
5065}
5066
5067/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005068 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005069 * We don't really need this at runtime, but we do have tests that require it,
5070 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 */
5072 void
5073ex_disassemble(exarg_T *eap)
5074{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005075 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005076 char_u *fname;
5077 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 dfunc_T *dfunc;
5079 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005080 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005081 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005083 if (STRNCMP(arg, "<lambda>", 8) == 0)
5084 {
5085 arg += 8;
5086 (void)getdigits(&arg);
5087 fname = vim_strnsave(eap->arg, arg - eap->arg);
5088 }
5089 else
5090 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005091 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005092 if (fname == NULL)
5093 {
5094 semsg(_(e_invarg2), eap->arg);
5095 return;
5096 }
5097
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005098 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005099 if (ufunc == NULL)
5100 {
5101 char_u *p = untrans_function_name(fname);
5102
5103 if (p != NULL)
5104 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005105 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005106 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005107 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 if (ufunc == NULL)
5109 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005110 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 return;
5112 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005113 if (func_needs_compiling(ufunc, eap->forceit)
5114 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005115 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005116 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005118 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 return;
5120 }
5121 if (ufunc->uf_name_exp != NULL)
5122 msg((char *)ufunc->uf_name_exp);
5123 else
5124 msg((char *)ufunc->uf_name);
5125
5126 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005127#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005128 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
5129 instr_count = eap->forceit ? dfunc->df_instr_prof_count
5130 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005131#else
5132 instr = dfunc->df_instr;
5133 instr_count = dfunc->df_instr_count;
5134#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135
Bram Moolenaar4c137212021-04-19 16:48:48 +02005136 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137}
5138
5139/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005140 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 * list, etc. Mostly like what JavaScript does, except that empty list and
5142 * empty dictionary are FALSE.
5143 */
5144 int
5145tv2bool(typval_T *tv)
5146{
5147 switch (tv->v_type)
5148 {
5149 case VAR_NUMBER:
5150 return tv->vval.v_number != 0;
5151 case VAR_FLOAT:
5152#ifdef FEAT_FLOAT
5153 return tv->vval.v_float != 0.0;
5154#else
5155 break;
5156#endif
5157 case VAR_PARTIAL:
5158 return tv->vval.v_partial != NULL;
5159 case VAR_FUNC:
5160 case VAR_STRING:
5161 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5162 case VAR_LIST:
5163 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5164 case VAR_DICT:
5165 return tv->vval.v_dict != NULL
5166 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5167 case VAR_BOOL:
5168 case VAR_SPECIAL:
5169 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5170 case VAR_JOB:
5171#ifdef FEAT_JOB_CHANNEL
5172 return tv->vval.v_job != NULL;
5173#else
5174 break;
5175#endif
5176 case VAR_CHANNEL:
5177#ifdef FEAT_JOB_CHANNEL
5178 return tv->vval.v_channel != NULL;
5179#else
5180 break;
5181#endif
5182 case VAR_BLOB:
5183 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5184 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005185 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 case VAR_VOID:
5187 break;
5188 }
5189 return FALSE;
5190}
5191
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005192 void
5193emsg_using_string_as(typval_T *tv, int as_number)
5194{
5195 semsg(_(as_number ? e_using_string_as_number_str
5196 : e_using_string_as_bool_str),
5197 tv->vval.v_string == NULL
5198 ? (char_u *)"" : tv->vval.v_string);
5199}
5200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201/*
5202 * If "tv" is a string give an error and return FAIL.
5203 */
5204 int
5205check_not_string(typval_T *tv)
5206{
5207 if (tv->v_type == VAR_STRING)
5208 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005209 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 clear_tv(tv);
5211 return FAIL;
5212 }
5213 return OK;
5214}
5215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216
5217#endif // FEAT_EVAL