blob: c90fb65b679fba689800f2b2a8232d27a3715eb2 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010029 int tcd_catch_idx; // instruction of the first :catch or :finally
30 int tcd_finally_idx; // instruction of the :finally block or zero
31 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010032 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010033 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_return; // when TRUE return from end of :finally
35} trycmd_T;
36
Bram Moolenaar4c137212021-04-19 16:48:48 +020037// Data local to a function.
38// On a function call, if not empty, is saved on the stack and restored when
39// returning.
40typedef struct {
41 int floc_restore_cmdmod;
42 cmdmod_T floc_save_cmdmod;
43 int floc_restore_cmdmod_stacklen;
44} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010045
46// A stack is used to store:
47// - arguments passed to a :def function
48// - info about the calling function, to use when returning
49// - local variables
50// - temporary values
51//
52// In detail (FP == Frame Pointer):
53// arg1 first argument from caller (if present)
54// arg2 second argument from caller (if present)
55// extra_arg1 any missing optional argument default value
56// FP -> cur_func calling function
57// current previous instruction pointer
58// frame_ptr previous Frame Pointer
59// var1 space for local variable
60// var2 space for local variable
61// .... fixed space for max. number of local variables
62// temp temporary values
63// .... flexible space for temporary values (can grow big)
64
65/*
66 * Execution context.
67 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010068struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010069 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020070 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020071 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072
Bram Moolenaar0186e582021-01-10 18:33:11 +010073 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020074 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076 garray_T ec_trystack; // stack of trycmd_T values
77 int ec_in_catch; // when TRUE in catch or finally block
78
79 int ec_dfunc_idx; // current function index
80 isn_T *ec_instr; // array with instructions
81 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020082
83 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020084
85 int ec_did_emsg_before;
86 int ec_trylevel_at_start;
87 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010088};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010089
Bram Moolenaar12d26532021-02-19 19:13:21 +010090#ifdef FEAT_PROFILE
91// stack of profinfo_T used when profiling.
92static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
93#endif
94
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020096#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097
Bram Moolenaar418f1df2020-08-12 21:34:49 +020098 void
99to_string_error(vartype_T vartype)
100{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200101 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200102}
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100105 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106 */
107 static int
108ufunc_argcount(ufunc_T *ufunc)
109{
110 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100144 * This adds a stack frame and sets the instruction pointer to the start of the
145 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100146 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147 *
148 * Stack has:
149 * - current arguments (already there)
150 * - omitted optional argument (default values) added here
151 * - stack frame:
152 * - pointer to calling function
153 * - Index of next instruction in calling function
154 * - previous frame pointer
155 * - reserved space for local variables
156 */
157 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100158call_dfunc(
159 int cdf_idx,
160 partial_T *pt,
161 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100162 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100164 int argcount = argcount_arg;
165 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
166 ufunc_T *ufunc = dfunc->df_ufunc;
167 int arg_to_add;
168 int vararg_count = 0;
169 int varcount;
170 int idx;
171 estack_T *entry;
172 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100176 // don't use ufunc->uf_name, it may have been freed
177 emsg_funcname(e_func_deleted,
178 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 return FAIL;
180 }
181
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100182#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100183 if (do_profiling == PROF_YES)
184 {
185 if (ga_grow(&profile_info_ga, 1) == OK)
186 {
187 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
188 + profile_info_ga.ga_len;
189 ++profile_info_ga.ga_len;
190 CLEAR_POINTER(info);
191 profile_may_start_func(info, ufunc,
192 (((dfunc_T *)def_functions.ga_data)
193 + ectx->ec_dfunc_idx)->df_ufunc);
194 }
195
196 // Profiling might be enabled/disabled along the way. This should not
197 // fail, since the function was compiled before and toggling profiling
198 // doesn't change any errors.
199 if (func_needs_compiling(ufunc, PROFILING(ufunc))
200 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100201 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100202 return FAIL;
203 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100204#endif
205
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200206 if (ufunc->uf_va_name != NULL)
207 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200208 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200209 // Stack at time of call with 2 varargs:
210 // normal_arg
211 // optional_arg
212 // vararg_1
213 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200214 // After creating the list:
215 // normal_arg
216 // optional_arg
217 // vararg-list
218 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200219 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200220 // After creating the list
221 // normal_arg
222 // (space for optional_arg)
223 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200224 vararg_count = argcount - ufunc->uf_args.ga_len;
225 if (vararg_count < 0)
226 vararg_count = 0;
227 else
228 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200229 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200230 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200231
232 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200233 }
234
Bram Moolenaarfe270812020-04-11 22:31:27 +0200235 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200236 if (arg_to_add < 0)
237 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200238 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200239 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200240 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200241 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200242 return FAIL;
243 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200244
245 // Reserve space for:
246 // - missing arguments
247 // - stack frame
248 // - local variables
249 // - if needed: a counter for number of closures created in
250 // ectx->ec_funcrefs.
251 varcount = dfunc->df_varcount + dfunc->df_has_closure;
252 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
253 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254 return FAIL;
255
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100256 // If depth of calling is getting too high, don't execute the function.
257 if (funcdepth_increment() == FAIL)
258 return FAIL;
259
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100260 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200261 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100262 {
263 floc = ALLOC_ONE(funclocal_T);
264 if (floc == NULL)
265 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200266 *floc = ectx->ec_funclocal;
267 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100268 }
269
Bram Moolenaarfe270812020-04-11 22:31:27 +0200270 // Move the vararg-list to below the missing optional arguments.
271 if (vararg_count > 0 && arg_to_add > 0)
272 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100273
274 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200275 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200276 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200277 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278
279 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
281 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100282 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100283 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100284 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200285 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286
287 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200288 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200290 if (dfunc->df_has_closure)
291 {
292 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
293
294 tv->v_type = VAR_NUMBER;
295 tv->vval.v_number = 0;
296 }
297 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100299 if (pt != NULL || ufunc->uf_partial != NULL
300 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100301 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100302 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
303
304 if (outer == NULL)
305 return FAIL;
306 if (pt != NULL)
307 {
308 *outer = pt->pt_outer;
309 outer->out_up_is_copy = TRUE;
310 }
311 else if (ufunc->uf_partial != NULL)
312 {
313 *outer = ufunc->uf_partial->pt_outer;
314 outer->out_up_is_copy = TRUE;
315 }
316 else
317 {
318 outer->out_stack = &ectx->ec_stack;
319 outer->out_frame_idx = ectx->ec_frame_idx;
320 outer->out_up = ectx->ec_outer;
321 }
322 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100323 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100324 else
325 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100326
Bram Moolenaarc970e422021-03-17 15:03:04 +0100327 ++ufunc->uf_calls;
328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329 // Set execution state to the start of the called function.
330 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100331 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100332 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200333 if (entry != NULL)
334 {
335 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200336 // Save the current context so it can be restored on return.
337 entry->es_save_sctx = current_sctx;
338 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200339 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100340
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200341 // Start execution at the first instruction.
342 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343
344 return OK;
345}
346
347// Get pointer to item in the stack.
348#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
349
350/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200351 * Used when returning from a function: Check if any closure is still
352 * referenced. If so then move the arguments and variables to a separate piece
353 * of stack to be used when the closure is called.
354 * When "free_arguments" is TRUE the arguments are to be freed.
355 * Returns FAIL when out of memory.
356 */
357 static int
358handle_closure_in_use(ectx_T *ectx, int free_arguments)
359{
360 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
361 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200362 int argcount;
363 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200364 int idx;
365 typval_T *tv;
366 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200367 garray_T *gap = &ectx->ec_funcrefs;
368 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200370 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200371 return OK; // function was freed
372 if (dfunc->df_has_closure == 0)
373 return OK; // no closures
374 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
375 closure_count = tv->vval.v_number;
376 if (closure_count == 0)
377 return OK; // no funcrefs created
378
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200379 argcount = ufunc_argcount(dfunc->df_ufunc);
380 top = ectx->ec_frame_idx - argcount;
381
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200382 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200383 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200384 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200385 partial_T *pt;
386 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200387
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200388 if (off < 0)
389 continue; // count is off or already done
390 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200391 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200392 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200393 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200394 int i;
395
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200397 // unreferenced on return.
398 for (i = 0; i < dfunc->df_varcount; ++i)
399 {
400 typval_T *stv = STACK_TV(ectx->ec_frame_idx
401 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200402 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200403 --refcount;
404 }
405 if (refcount > 1)
406 {
407 closure_in_use = TRUE;
408 break;
409 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200410 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200411 }
412
413 if (closure_in_use)
414 {
415 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
416 typval_T *stack;
417
418 // A closure is using the arguments and/or local variables.
419 // Move them to the called function.
420 if (funcstack == NULL)
421 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200422 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
423 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200424 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
425 funcstack->fs_ga.ga_data = stack;
426 if (stack == NULL)
427 {
428 vim_free(funcstack);
429 return FAIL;
430 }
431
432 // Move or copy the arguments.
433 for (idx = 0; idx < argcount; ++idx)
434 {
435 tv = STACK_TV(top + idx);
436 if (free_arguments)
437 {
438 *(stack + idx) = *tv;
439 tv->v_type = VAR_UNKNOWN;
440 }
441 else
442 copy_tv(tv, stack + idx);
443 }
444 // Move the local variables.
445 for (idx = 0; idx < dfunc->df_varcount; ++idx)
446 {
447 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200448
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200449 // A partial created for a local function, that is also used as a
450 // local variable, has a reference count for the variable, thus
451 // will never go down to zero. When all these refcounts are one
452 // then the funcstack is unused. We need to count how many we have
453 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200454 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
455 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200456 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200457
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200458 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200459 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
460 gap->ga_len - closure_count + i])
461 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200462 }
463
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200464 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200465 tv->v_type = VAR_UNKNOWN;
466 }
467
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200468 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200469 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200470 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
471 - closure_count + idx];
472 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200473 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200474 ++funcstack->fs_refcount;
475 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100476 pt->pt_outer.out_stack = &funcstack->fs_ga;
477 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
478 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200479 }
480 }
481 }
482
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200483 for (idx = 0; idx < closure_count; ++idx)
484 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
485 - closure_count + idx]);
486 gap->ga_len -= closure_count;
487 if (gap->ga_len == 0)
488 ga_clear(gap);
489
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490 return OK;
491}
492
493/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200494 * Called when a partial is freed or its reference count goes down to one. The
495 * funcstack may be the only reference to the partials in the local variables.
496 * Go over all of them, the funcref and can be freed if all partials
497 * referencing the funcstack have a reference count of one.
498 */
499 void
500funcstack_check_refcount(funcstack_T *funcstack)
501{
502 int i;
503 garray_T *gap = &funcstack->fs_ga;
504 int done = 0;
505
506 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
507 return;
508 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
509 {
510 typval_T *tv = ((typval_T *)gap->ga_data) + i;
511
512 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
513 && tv->vval.v_partial->pt_funcstack == funcstack
514 && tv->vval.v_partial->pt_refcount == 1)
515 ++done;
516 }
517 if (done == funcstack->fs_min_refcount)
518 {
519 typval_T *stack = gap->ga_data;
520
521 // All partials referencing the funcstack have a reference count of
522 // one, thus the funcstack is no longer of use.
523 for (i = 0; i < gap->ga_len; ++i)
524 clear_tv(stack + i);
525 vim_free(stack);
526 vim_free(funcstack);
527 }
528}
529
530/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 * Return from the current function.
532 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200533 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200534func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100537 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200538 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
539 + ectx->ec_dfunc_idx;
540 int argcount = ufunc_argcount(dfunc->df_ufunc);
541 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200542 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100543 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
544 + STACK_FRAME_FUNC_OFF)->vval.v_number;
545 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
546 + prev_dfunc_idx;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100547 funclocal_T *floc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548
Bram Moolenaar12d26532021-02-19 19:13:21 +0100549#ifdef FEAT_PROFILE
550 if (do_profiling == PROF_YES)
551 {
552 ufunc_T *caller = prev_dfunc->df_ufunc;
553
554 if (dfunc->df_ufunc->uf_profiling
555 || (caller != NULL && caller->uf_profiling))
556 {
557 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
558 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
559 --profile_info_ga.ga_len;
560 }
561 }
562#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100563 // TODO: when is it safe to delete the function when it is no longer used?
564 --dfunc->df_ufunc->uf_calls;
565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200567 entry = estack_pop();
568 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200569 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200571 if (handle_closure_in_use(ectx, TRUE) == FAIL)
572 return FAIL;
573
574 // Clear the arguments.
575 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
576 clear_tv(STACK_TV(idx));
577
578 // Clear local variables and temp values, but not the return value.
579 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100580 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100582
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100583 // The return value should be on top of the stack. However, when aborting
584 // it may not be there and ec_frame_idx is the top of the stack.
585 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100586 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100587 ret_idx = 0;
588
Bram Moolenaar0186e582021-01-10 18:33:11 +0100589 vim_free(ectx->ec_outer);
590
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100591 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100592 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
594 + STACK_FRAME_IIDX_OFF)->vval.v_number;
595 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
596 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100597 floc = (void *)STACK_TV(ectx->ec_frame_idx
598 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200599 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100600 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
601 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100602 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100603
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100604 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200605 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100606 else
607 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200608 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100609 vim_free(floc);
610 }
611
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100612 if (ret_idx > 0)
613 {
614 // Reset the stack to the position before the call, with a spot for the
615 // return value, moved there from above the frame.
616 ectx->ec_stack.ga_len = top + 1;
617 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
618 }
619 else
620 // Reset the stack to the position before the call.
621 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100623 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200624 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625}
626
627#undef STACK_TV
628
629/*
630 * Prepare arguments and rettv for calling a builtin or user function.
631 */
632 static int
633call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
634{
635 int idx;
636 typval_T *tv;
637
638 // Move arguments from bottom of the stack to argvars[] and add terminator.
639 for (idx = 0; idx < argcount; ++idx)
640 argvars[idx] = *STACK_TV_BOT(idx - argcount);
641 argvars[argcount].v_type = VAR_UNKNOWN;
642
643 // Result replaces the arguments on the stack.
644 if (argcount > 0)
645 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200646 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 return FAIL;
648 else
649 ++ectx->ec_stack.ga_len;
650
651 // Default return value is zero.
652 tv = STACK_TV_BOT(-1);
653 tv->v_type = VAR_NUMBER;
654 tv->vval.v_number = 0;
655
656 return OK;
657}
658
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200659// Ugly global to avoid passing the execution context around through many
660// layers.
661static ectx_T *current_ectx = NULL;
662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663/*
664 * Call a builtin function by index.
665 */
666 static int
667call_bfunc(int func_idx, int argcount, ectx_T *ectx)
668{
669 typval_T argvars[MAX_FUNC_ARGS];
670 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100671 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200672 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 if (call_prepare(argcount, argvars, ectx) == FAIL)
675 return FAIL;
676
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200677 // Call the builtin function. Set "current_ectx" so that when it
678 // recursively invokes call_def_function() a closure context can be set.
679 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200681 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682
683 // Clear the arguments.
684 for (idx = 0; idx < argcount; ++idx)
685 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200686
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100687 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200688 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 return OK;
690}
691
692/*
693 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100694 * If the function is compiled this will add a stack frame and set the
695 * instruction pointer at the start of the function.
696 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100697 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100698 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 */
700 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100701call_ufunc(
702 ufunc_T *ufunc,
703 partial_T *pt,
704 int argcount,
705 ectx_T *ectx,
706 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707{
708 typval_T argvars[MAX_FUNC_ARGS];
709 funcexe_T funcexe;
710 int error;
711 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100712 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100713#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100714 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100715#else
716# define profiling FALSE
717#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718
Bram Moolenaarb2049902021-01-24 12:53:53 +0100719 if (func_needs_compiling(ufunc, profiling)
720 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200721 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200722 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100723 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100724 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100725 if (error != FCERR_UNKNOWN)
726 {
727 if (error == FCERR_TOOMANY)
728 semsg(_(e_toomanyarg), ufunc->uf_name);
729 else
730 semsg(_(e_toofewarg), ufunc->uf_name);
731 return FAIL;
732 }
733
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100734 // The function has been compiled, can call it quickly. For a function
735 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100736 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100737 if (iptr != NULL)
738 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100739 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100740 iptr->isn_type = ISN_DCALL;
741 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
742 iptr->isn_arg.dfunc.cdf_argcount = argcount;
743 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200744 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746
747 if (call_prepare(argcount, argvars, ectx) == FAIL)
748 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200749 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 funcexe.evaluate = TRUE;
751
752 // Call the user function. Result goes in last position on the stack.
753 // TODO: add selfdict if there is one
754 error = call_user_func_check(ufunc, argcount, argvars,
755 STACK_TV_BOT(-1), &funcexe, NULL);
756
757 // Clear the arguments.
758 for (idx = 0; idx < argcount; ++idx)
759 clear_tv(&argvars[idx]);
760
761 if (error != FCERR_NONE)
762 {
763 user_func_error(error, ufunc->uf_name);
764 return FAIL;
765 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100766 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200767 // Error other than from calling the function itself.
768 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 return OK;
770}
771
772/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100773 * If command modifiers were applied restore them.
774 */
775 static void
776may_restore_cmdmod(funclocal_T *funclocal)
777{
778 if (funclocal->floc_restore_cmdmod)
779 {
780 cmdmod.cmod_filter_regmatch.regprog = NULL;
781 undo_cmdmod(&cmdmod);
782 cmdmod = funclocal->floc_save_cmdmod;
783 funclocal->floc_restore_cmdmod = FALSE;
784 }
785}
786
787/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200788 * Return TRUE if an error was given or CTRL-C was pressed.
789 */
790 static int
791vim9_aborting(int prev_called_emsg)
792{
793 return called_emsg > prev_called_emsg || got_int || did_throw;
794}
795
796/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 * Execute a function by "name".
798 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100799 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 * Returns FAIL if not found without an error message.
801 */
802 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100803call_by_name(
804 char_u *name,
805 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100806 ectx_T *ectx,
807 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808{
809 ufunc_T *ufunc;
810
811 if (builtin_function(name, -1))
812 {
813 int func_idx = find_internal_func(name);
814
815 if (func_idx < 0)
816 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200817 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818 return FAIL;
819 return call_bfunc(func_idx, argcount, ectx);
820 }
821
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200822 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200823
824 if (ufunc == NULL)
825 {
826 int called_emsg_before = called_emsg;
827
828 if (script_autoload(name, TRUE))
829 // loaded a package, search for the function again
830 ufunc = find_func(name, FALSE, NULL);
831 if (vim9_aborting(called_emsg_before))
832 return FAIL; // bail out if loading the script caused an error
833 }
834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100836 {
837 if (ufunc->uf_arg_types != NULL)
838 {
839 int i;
840 typval_T *argv = STACK_TV_BOT(0) - argcount;
841
842 // The function can change at runtime, check that the argument
843 // types are correct.
844 for (i = 0; i < argcount; ++i)
845 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100846 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100847
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100848 if (i < ufunc->uf_args.ga_len)
849 type = ufunc->uf_arg_types[i];
850 else if (ufunc->uf_va_type != NULL)
851 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100852 if (type != NULL && check_typval_arg_type(type,
853 &argv[i], i + 1) == FAIL)
854 return FAIL;
855 }
856 }
857
Bram Moolenaar4c137212021-04-19 16:48:48 +0200858 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860
861 return FAIL;
862}
863
864 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100865call_partial(
866 typval_T *tv,
867 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100868 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200870 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200871 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100873 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874
875 if (tv->v_type == VAR_PARTIAL)
876 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200877 partial_T *pt = tv->vval.v_partial;
878 int i;
879
880 if (pt->pt_argc > 0)
881 {
882 // Make space for arguments from the partial, shift the "argcount"
883 // arguments up.
884 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
885 return FAIL;
886 for (i = 1; i <= argcount; ++i)
887 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
888 ectx->ec_stack.ga_len += pt->pt_argc;
889 argcount += pt->pt_argc;
890
891 // copy the arguments from the partial onto the stack
892 for (i = 0; i < pt->pt_argc; ++i)
893 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895
896 if (pt->pt_func != NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200897 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 name = pt->pt_name;
900 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200901 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200903 if (name != NULL)
904 {
905 char_u fname_buf[FLEN_FIXED + 1];
906 char_u *tofree = NULL;
907 int error = FCERR_NONE;
908 char_u *fname;
909
910 // May need to translate <SNR>123_ to K_SNR.
911 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
912 if (error != FCERR_NONE)
913 res = FAIL;
914 else
Bram Moolenaar4c137212021-04-19 16:48:48 +0200915 res = call_by_name(fname, argcount, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200916 vim_free(tofree);
917 }
918
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100919 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 {
921 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200922 semsg(_(e_unknownfunc),
923 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 return FAIL;
925 }
926 return OK;
927}
928
929/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200930 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
931 * TRUE.
932 */
933 static int
934error_if_locked(int lock, char *error)
935{
936 if (lock & (VAR_LOCKED | VAR_FIXED))
937 {
938 emsg(_(error));
939 return TRUE;
940 }
941 return FALSE;
942}
943
944/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100945 * Give an error if "tv" is not a number and return FAIL.
946 */
947 static int
948check_for_number(typval_T *tv)
949{
950 if (tv->v_type != VAR_NUMBER)
951 {
952 semsg(_(e_expected_str_but_got_str),
953 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
954 return FAIL;
955 }
956 return OK;
957}
958
959/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100960 * Store "tv" in variable "name".
961 * This is for s: and g: variables.
962 */
963 static void
964store_var(char_u *name, typval_T *tv)
965{
966 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +0200967 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100968
Bram Moolenaard877a572021-04-01 19:42:48 +0200969 if (tv->v_lock)
970 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100971 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +0200972 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100973 restore_funccal();
974}
975
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100976/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100977 * Convert "tv" to a string.
978 * Return FAIL if not allowed.
979 */
980 static int
981do_2string(typval_T *tv, int is_2string_any)
982{
983 if (tv->v_type != VAR_STRING)
984 {
985 char_u *str;
986
987 if (is_2string_any)
988 {
989 switch (tv->v_type)
990 {
991 case VAR_SPECIAL:
992 case VAR_BOOL:
993 case VAR_NUMBER:
994 case VAR_FLOAT:
995 case VAR_BLOB: break;
996 default: to_string_error(tv->v_type);
997 return FAIL;
998 }
999 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001000 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001001 clear_tv(tv);
1002 tv->v_type = VAR_STRING;
1003 tv->vval.v_string = str;
1004 }
1005 return OK;
1006}
1007
1008/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001009 * When the value of "sv" is a null list of dict, allocate it.
1010 */
1011 static void
1012allocate_if_null(typval_T *tv)
1013{
1014 switch (tv->v_type)
1015 {
1016 case VAR_LIST:
1017 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001018 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001019 break;
1020 case VAR_DICT:
1021 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001022 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001023 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001024 case VAR_BLOB:
1025 if (tv->vval.v_blob == NULL)
1026 (void)rettv_blob_alloc(tv);
1027 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001028 default:
1029 break;
1030 }
1031}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001032
Bram Moolenaare7525c52021-01-09 13:20:37 +01001033/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001034 * Return the character "str[index]" where "index" is the character index,
1035 * including composing characters.
1036 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001037 */
1038 char_u *
1039char_from_string(char_u *str, varnumber_T index)
1040{
1041 size_t nbyte = 0;
1042 varnumber_T nchar = index;
1043 size_t slen;
1044
1045 if (str == NULL)
1046 return NULL;
1047 slen = STRLEN(str);
1048
Bram Moolenaarff871402021-03-26 13:34:05 +01001049 // Do the same as for a list: a negative index counts from the end.
1050 // Optimization to check the first byte to be below 0x80 (and no composing
1051 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001052 if (index < 0)
1053 {
1054 int clen = 0;
1055
1056 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001057 {
1058 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1059 ++nbyte;
1060 else if (enc_utf8)
1061 nbyte += utfc_ptr2len(str + nbyte);
1062 else
1063 nbyte += mb_ptr2len(str + nbyte);
1064 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001065 nchar = clen + index;
1066 if (nchar < 0)
1067 // unlike list: index out of range results in empty string
1068 return NULL;
1069 }
1070
1071 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001072 {
1073 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1074 ++nbyte;
1075 else if (enc_utf8)
1076 nbyte += utfc_ptr2len(str + nbyte);
1077 else
1078 nbyte += mb_ptr2len(str + nbyte);
1079 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001080 if (nbyte >= slen)
1081 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001082 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001083}
1084
1085/*
1086 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001087 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001088 * If going over the end return "str_len".
1089 * If "idx" is negative count from the end, -1 is the last character.
1090 * When going over the start return -1.
1091 */
1092 static long
1093char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1094{
1095 varnumber_T nchar = idx;
1096 size_t nbyte = 0;
1097
1098 if (nchar >= 0)
1099 {
1100 while (nchar > 0 && nbyte < str_len)
1101 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001102 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001103 --nchar;
1104 }
1105 }
1106 else
1107 {
1108 nbyte = str_len;
1109 while (nchar < 0 && nbyte > 0)
1110 {
1111 --nbyte;
1112 nbyte -= mb_head_off(str, str + nbyte);
1113 ++nchar;
1114 }
1115 if (nchar < 0)
1116 return -1;
1117 }
1118 return (long)nbyte;
1119}
1120
1121/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001122 * Return the slice "str[first : last]" using character indexes. Composing
1123 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001124 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001125 * Return NULL when the result is empty.
1126 */
1127 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001128string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001129{
1130 long start_byte, end_byte;
1131 size_t slen;
1132
1133 if (str == NULL)
1134 return NULL;
1135 slen = STRLEN(str);
1136 start_byte = char_idx2byte(str, slen, first);
1137 if (start_byte < 0)
1138 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001139 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001140 end_byte = (long)slen;
1141 else
1142 {
1143 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001144 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001145 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001146 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001147 }
1148
1149 if (start_byte >= (long)slen || end_byte <= start_byte)
1150 return NULL;
1151 return vim_strnsave(str + start_byte, end_byte - start_byte);
1152}
1153
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001154 static svar_T *
1155get_script_svar(scriptref_T *sref, ectx_T *ectx)
1156{
1157 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1159 + ectx->ec_dfunc_idx;
1160 svar_T *sv;
1161
1162 if (sref->sref_seq != si->sn_script_seq)
1163 {
1164 // The script was reloaded after the function was
1165 // compiled, the script_idx may not be valid.
1166 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1167 dfunc->df_ufunc->uf_name_exp);
1168 return NULL;
1169 }
1170 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1171 if (!equal_type(sv->sv_type, sref->sref_type))
1172 {
1173 emsg(_(e_script_variable_type_changed));
1174 return NULL;
1175 }
1176 return sv;
1177}
1178
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001179/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 * Execute a function by "name".
1181 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001182 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 */
1184 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001185call_eval_func(
1186 char_u *name,
1187 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001188 ectx_T *ectx,
1189 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190{
Bram Moolenaared677f52020-08-12 16:38:10 +02001191 int called_emsg_before = called_emsg;
1192 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193
Bram Moolenaar4c137212021-04-19 16:48:48 +02001194 res = call_by_name(name, argcount, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001195 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001197 dictitem_T *v;
1198
1199 v = find_var(name, NULL, FALSE);
1200 if (v == NULL)
1201 {
1202 semsg(_(e_unknownfunc), name);
1203 return FAIL;
1204 }
1205 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1206 {
1207 semsg(_(e_unknownfunc), name);
1208 return FAIL;
1209 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001210 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001212 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213}
1214
1215/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001216 * When a function reference is used, fill a partial with the information
1217 * needed, especially when it is used as a closure.
1218 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001219 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001220fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1221{
1222 pt->pt_func = ufunc;
1223 pt->pt_refcount = 1;
1224
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001225 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001226 {
1227 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1228 + ectx->ec_dfunc_idx;
1229
1230 // The closure needs to find arguments and local
1231 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001232 pt->pt_outer.out_stack = &ectx->ec_stack;
1233 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1234 pt->pt_outer.out_up = ectx->ec_outer;
1235 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001236
1237 // If this function returns and the closure is still
1238 // being used, we need to make a copy of the context
1239 // (arguments and local variables). Store a reference
1240 // to the partial so we can handle that.
1241 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1242 {
1243 vim_free(pt);
1244 return FAIL;
1245 }
1246 // Extra variable keeps the count of closures created
1247 // in the current function call.
1248 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1249 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1250
1251 ((partial_T **)ectx->ec_funcrefs.ga_data)
1252 [ectx->ec_funcrefs.ga_len] = pt;
1253 ++pt->pt_refcount;
1254 ++ectx->ec_funcrefs.ga_len;
1255 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001256 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001257 return OK;
1258}
1259
Bram Moolenaar4c137212021-04-19 16:48:48 +02001260// used for substitute_instr
1261typedef struct subs_expr_S {
1262 ectx_T *subs_ectx;
1263 isn_T *subs_instr;
1264 int subs_status;
1265} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266
1267// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001268#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269
1270// Get pointer to item at the bottom of the stack, -1 is the bottom.
1271#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001272#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001274// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001275#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276
Bram Moolenaar4c137212021-04-19 16:48:48 +02001277/*
1278 * Execute instructions in execution context "ectx".
1279 * Return OK or FAIL;
1280 */
1281 static int
1282exec_instructions(ectx_T *ectx)
1283{
1284 int breakcheck_count = 0;
1285 typval_T *tv;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001286
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001287 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001288 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 for (;;)
1291 {
1292 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001293
Bram Moolenaar270d0382020-05-15 21:42:53 +02001294 if (++breakcheck_count >= 100)
1295 {
1296 line_breakcheck();
1297 breakcheck_count = 0;
1298 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001299 if (got_int)
1300 {
1301 // Turn CTRL-C into an exception.
1302 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001303 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001304 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001305 did_throw = TRUE;
1306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307
Bram Moolenaara26b9702020-04-18 19:53:28 +02001308 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1309 {
1310 // Turn an error message into an exception.
1311 did_emsg = FALSE;
1312 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001313 return FAIL;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001314 did_throw = TRUE;
1315 *msg_list = NULL;
1316 }
1317
Bram Moolenaar4c137212021-04-19 16:48:48 +02001318 if (did_throw && !ectx->ec_in_catch)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001320 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001321 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322
1323 // An exception jumps to the first catch, finally, or returns from
1324 // the current function.
1325 if (trystack->ga_len > 0)
1326 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001327 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 {
1329 // jump to ":catch" or ":finally"
Bram Moolenaar4c137212021-04-19 16:48:48 +02001330 ectx->ec_in_catch = TRUE;
1331 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 }
1333 else
1334 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001335 // Not inside try or need to return from current functions.
1336 // Push a dummy return value.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001337 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1338 return FAIL;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001339 tv = STACK_TV_BOT(0);
1340 tv->v_type = VAR_NUMBER;
1341 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001342 ++ectx->ec_stack.ga_len;
1343 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001345 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001346 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001347 if (handle_closure_in_use(ectx, FALSE) == FAIL)
1348 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 goto done;
1350 }
1351
Bram Moolenaar4c137212021-04-19 16:48:48 +02001352 if (func_return(ectx) == FAIL)
1353 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 }
1355 continue;
1356 }
1357
Bram Moolenaar4c137212021-04-19 16:48:48 +02001358 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 switch (iptr->isn_type)
1360 {
1361 // execute Ex command line
1362 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001363 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001364 source_cookie_T cookie;
1365
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001366 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001367 // Pass getsourceline to get an error for a missing ":end"
1368 // command.
1369 CLEAR_FIELD(cookie);
1370 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1371 if (do_cmdline(iptr->isn_arg.string,
1372 getsourceline, &cookie,
1373 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1374 == FAIL
1375 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001376 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 break;
1379
Bram Moolenaar4c137212021-04-19 16:48:48 +02001380 // execute :substitute with an expression
1381 case ISN_SUBSTITUTE:
1382 {
1383 subs_T *subs = &iptr->isn_arg.subs;
1384 source_cookie_T cookie;
1385 struct subs_expr_S *save_instr = substitute_instr;
1386 struct subs_expr_S subs_instr;
1387 int res;
1388
1389 subs_instr.subs_ectx = ectx;
1390 subs_instr.subs_instr = subs->subs_instr;
1391 subs_instr.subs_status = OK;
1392 substitute_instr = &subs_instr;
1393
1394 SOURCING_LNUM = iptr->isn_lnum;
1395 // This is very much like ISN_EXEC
1396 CLEAR_FIELD(cookie);
1397 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1398 res = do_cmdline(subs->subs_cmd,
1399 getsourceline, &cookie,
1400 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1401 substitute_instr = save_instr;
1402
1403 if (res == FAIL || did_emsg
1404 || subs_instr.subs_status == FAIL)
1405 goto on_error;
1406 }
1407 break;
1408
1409 case ISN_FINISH:
1410 goto done;
1411
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001412 case ISN_REDIRSTART:
1413 // create a dummy entry for var_redir_str()
1414 if (alloc_redir_lval() == FAIL)
1415 goto on_error;
1416
1417 // The output is stored in growarray "redir_ga" until
1418 // redirection ends.
1419 init_redir_ga();
1420 redir_vname = 1;
1421 break;
1422
1423 case ISN_REDIREND:
1424 {
1425 char_u *res = get_clear_redir_ga();
1426
1427 // End redirection, put redirected text on the stack.
1428 clear_redir_lval();
1429 redir_vname = 0;
1430
1431 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1432 {
1433 vim_free(res);
1434 return FAIL;
1435 }
1436 tv = STACK_TV_BOT(0);
1437 tv->v_type = VAR_STRING;
1438 tv->vval.v_string = res;
1439 ++ectx->ec_stack.ga_len;
1440 }
1441 break;
1442
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001443 // execute Ex command from pieces on the stack
1444 case ISN_EXECCONCAT:
1445 {
1446 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001447 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001448 int pass;
1449 int i;
1450 char_u *cmd = NULL;
1451 char_u *str;
1452
1453 for (pass = 1; pass <= 2; ++pass)
1454 {
1455 for (i = 0; i < count; ++i)
1456 {
1457 tv = STACK_TV_BOT(i - count);
1458 str = tv->vval.v_string;
1459 if (str != NULL && *str != NUL)
1460 {
1461 if (pass == 2)
1462 STRCPY(cmd + len, str);
1463 len += STRLEN(str);
1464 }
1465 if (pass == 2)
1466 clear_tv(tv);
1467 }
1468 if (pass == 1)
1469 {
1470 cmd = alloc(len + 1);
1471 if (cmd == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001472 return FAIL;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001473 len = 0;
1474 }
1475 }
1476
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001477 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001478 do_cmdline_cmd(cmd);
1479 vim_free(cmd);
1480 }
1481 break;
1482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 // execute :echo {string} ...
1484 case ISN_ECHO:
1485 {
1486 int count = iptr->isn_arg.echo.echo_count;
1487 int atstart = TRUE;
1488 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001489 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490
1491 for (idx = 0; idx < count; ++idx)
1492 {
1493 tv = STACK_TV_BOT(idx - count);
1494 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1495 &atstart, &needclr);
1496 clear_tv(tv);
1497 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001498 if (needclr)
1499 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02001500 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 }
1502 break;
1503
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001504 // :execute {string} ...
1505 // :echomsg {string} ...
1506 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001507 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001508 case ISN_ECHOMSG:
1509 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001510 {
1511 int count = iptr->isn_arg.number;
1512 garray_T ga;
1513 char_u buf[NUMBUFLEN];
1514 char_u *p;
1515 int len;
1516 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001517 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001518
1519 ga_init2(&ga, 1, 80);
1520 for (idx = 0; idx < count; ++idx)
1521 {
1522 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001523 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001524 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001525 if (tv->v_type == VAR_CHANNEL
1526 || tv->v_type == VAR_JOB)
1527 {
1528 SOURCING_LNUM = iptr->isn_lnum;
1529 emsg(_(e_inval_string));
1530 break;
1531 }
1532 else
1533 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001534 }
1535 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001536 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001537
1538 len = (int)STRLEN(p);
1539 if (ga_grow(&ga, len + 2) == FAIL)
1540 failed = TRUE;
1541 else
1542 {
1543 if (ga.ga_len > 0)
1544 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1545 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1546 ga.ga_len += len;
1547 }
1548 clear_tv(tv);
1549 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001550 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001551 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001552 {
1553 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001554 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001555 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001556
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001557 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001558 {
1559 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001560 {
1561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001562 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001563 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001564 {
1565 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001566 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001567 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001568 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001569 else
1570 {
1571 msg_sb_eol();
1572 if (iptr->isn_type == ISN_ECHOMSG)
1573 {
1574 msg_attr(ga.ga_data, echo_attr);
1575 out_flush();
1576 }
1577 else
1578 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001579 SOURCING_LNUM = iptr->isn_lnum;
1580 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001581 }
1582 }
1583 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001584 ga_clear(&ga);
1585 }
1586 break;
1587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 // load local variable or argument
1589 case ISN_LOAD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001590 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1591 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001593 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 break;
1595
1596 // load v: variable
1597 case ISN_LOADV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001598 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1599 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001601 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 break;
1603
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001604 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 case ISN_LOADSCRIPT:
1606 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001607 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 svar_T *sv;
1609
Bram Moolenaar4c137212021-04-19 16:48:48 +02001610 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001611 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001612 return FAIL;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001613 allocate_if_null(sv->sv_tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001614 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1615 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001617 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 }
1619 break;
1620
1621 // load s: variable in old script
1622 case ISN_LOADS:
1623 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001624 hashtab_T *ht = &SCRIPT_VARS(
1625 iptr->isn_arg.loadstore.ls_sid);
1626 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 if (di == NULL)
1630 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001631 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001632 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001633 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 }
1635 else
1636 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001637 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1638 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001640 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 }
1642 }
1643 break;
1644
Bram Moolenaard3aac292020-04-19 14:32:17 +02001645 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001647 case ISN_LOADB:
1648 case ISN_LOADW:
1649 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001651 dictitem_T *di = NULL;
1652 hashtab_T *ht = NULL;
1653 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001654
Bram Moolenaard3aac292020-04-19 14:32:17 +02001655 switch (iptr->isn_type)
1656 {
1657 case ISN_LOADG:
1658 ht = get_globvar_ht();
1659 namespace = 'g';
1660 break;
1661 case ISN_LOADB:
1662 ht = &curbuf->b_vars->dv_hashtab;
1663 namespace = 'b';
1664 break;
1665 case ISN_LOADW:
1666 ht = &curwin->w_vars->dv_hashtab;
1667 namespace = 'w';
1668 break;
1669 case ISN_LOADT:
1670 ht = &curtab->tp_vars->dv_hashtab;
1671 namespace = 't';
1672 break;
1673 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001674 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001675 }
1676 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 if (di == NULL)
1679 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001680 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001681 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001682 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001683 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 }
1685 else
1686 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001687 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1688 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001690 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 }
1692 }
1693 break;
1694
Bram Moolenaar03290b82020-12-19 16:30:44 +01001695 // load autoload variable
1696 case ISN_LOADAUTO:
1697 {
1698 char_u *name = iptr->isn_arg.string;
1699
Bram Moolenaar4c137212021-04-19 16:48:48 +02001700 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1701 return FAIL;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001702 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001703 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001704 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001705 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001706 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001707 }
1708 break;
1709
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001710 // load g:/b:/w:/t: namespace
1711 case ISN_LOADGDICT:
1712 case ISN_LOADBDICT:
1713 case ISN_LOADWDICT:
1714 case ISN_LOADTDICT:
1715 {
1716 dict_T *d = NULL;
1717
1718 switch (iptr->isn_type)
1719 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001720 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1721 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1722 case ISN_LOADWDICT: d = curwin->w_vars; break;
1723 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001724 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001725 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001726 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001727 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1728 return FAIL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001729 tv = STACK_TV_BOT(0);
1730 tv->v_type = VAR_DICT;
1731 tv->v_lock = 0;
1732 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001733 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001734 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001735 }
1736 break;
1737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 // load &option
1739 case ISN_LOADOPT:
1740 {
1741 typval_T optval;
1742 char_u *name = iptr->isn_arg.string;
1743
Bram Moolenaara8c17702020-04-01 21:17:24 +02001744 // This is not expected to fail, name is checked during
1745 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001746 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1747 return FAIL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001748 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001749 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001751 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 }
1753 break;
1754
1755 // load $ENV
1756 case ISN_LOADENV:
1757 {
1758 typval_T optval;
1759 char_u *name = iptr->isn_arg.string;
1760
Bram Moolenaar4c137212021-04-19 16:48:48 +02001761 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1762 return FAIL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001763 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001764 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001766 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 }
1768 break;
1769
1770 // load @register
1771 case ISN_LOADREG:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001772 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
1773 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 tv = STACK_TV_BOT(0);
1775 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001776 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001777 // This may result in NULL, which should be equivalent to an
1778 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 tv->vval.v_string = get_reg_contents(
1780 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02001781 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 break;
1783
1784 // store local variable
1785 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001786 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 tv = STACK_TV_VAR(iptr->isn_arg.number);
1788 clear_tv(tv);
1789 *tv = *STACK_TV_BOT(0);
1790 break;
1791
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001792 // store s: variable in old script
1793 case ISN_STORES:
1794 {
1795 hashtab_T *ht = &SCRIPT_VARS(
1796 iptr->isn_arg.loadstore.ls_sid);
1797 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001798 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001799
Bram Moolenaar4c137212021-04-19 16:48:48 +02001800 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001801 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001802 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001803 else
1804 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001805 SOURCING_LNUM = iptr->isn_lnum;
1806 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001807 {
1808 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02001809 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001810 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001811 clear_tv(&di->di_tv);
1812 di->di_tv = *STACK_TV_BOT(0);
1813 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001814 }
1815 break;
1816
1817 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 case ISN_STORESCRIPT:
1819 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001820 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1821 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822
Bram Moolenaar4c137212021-04-19 16:48:48 +02001823 sv = get_script_svar(sref, ectx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001824 if (sv == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001825 return FAIL;
1826 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001827
1828 // "const" and "final" are checked at compile time, locking
1829 // the value needs to be checked here.
1830 SOURCING_LNUM = iptr->isn_lnum;
1831 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001832 {
1833 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001834 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02001835 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02001836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 clear_tv(sv->sv_tv);
1838 *sv->sv_tv = *STACK_TV_BOT(0);
1839 }
1840 break;
1841
1842 // store option
1843 case ISN_STOREOPT:
1844 {
1845 long n = 0;
1846 char_u *s = NULL;
1847 char *msg;
1848
Bram Moolenaar4c137212021-04-19 16:48:48 +02001849 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 tv = STACK_TV_BOT(0);
1851 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001852 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001854 if (s == NULL)
1855 s = (char_u *)"";
1856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001858 // must be VAR_NUMBER, CHECKTYPE makes sure
1859 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1861 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001862 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 if (msg != NULL)
1864 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001865 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001867 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 }
1870 break;
1871
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001872 // store $ENV
1873 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001874 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001875 tv = STACK_TV_BOT(0);
1876 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1877 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001878 break;
1879
1880 // store @r
1881 case ISN_STOREREG:
1882 {
1883 int reg = iptr->isn_arg.number;
1884
Bram Moolenaar4c137212021-04-19 16:48:48 +02001885 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001886 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001887 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001888 tv_get_string(tv), -1, FALSE);
1889 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001890 }
1891 break;
1892
1893 // store v: variable
1894 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02001895 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001896 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1897 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001898 // should not happen, type is checked when compiling
1899 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001900 break;
1901
Bram Moolenaard3aac292020-04-19 14:32:17 +02001902 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001904 case ISN_STOREB:
1905 case ISN_STOREW:
1906 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001908 dictitem_T *di;
1909 hashtab_T *ht;
1910 char_u *name = iptr->isn_arg.string + 2;
1911
Bram Moolenaard3aac292020-04-19 14:32:17 +02001912 switch (iptr->isn_type)
1913 {
1914 case ISN_STOREG:
1915 ht = get_globvar_ht();
1916 break;
1917 case ISN_STOREB:
1918 ht = &curbuf->b_vars->dv_hashtab;
1919 break;
1920 case ISN_STOREW:
1921 ht = &curwin->w_vars->dv_hashtab;
1922 break;
1923 case ISN_STORET:
1924 ht = &curtab->tp_vars->dv_hashtab;
1925 break;
1926 default: // Cannot reach here
Bram Moolenaar4c137212021-04-19 16:48:48 +02001927 return FAIL;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001928 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929
Bram Moolenaar4c137212021-04-19 16:48:48 +02001930 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001931 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001933 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 else
1935 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001936 SOURCING_LNUM = iptr->isn_lnum;
1937 if (var_check_permission(di, name) == FAIL)
1938 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 clear_tv(&di->di_tv);
1940 di->di_tv = *STACK_TV_BOT(0);
1941 }
1942 }
1943 break;
1944
Bram Moolenaar03290b82020-12-19 16:30:44 +01001945 // store an autoload variable
1946 case ISN_STOREAUTO:
1947 SOURCING_LNUM = iptr->isn_lnum;
1948 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1949 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02001950 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001951 break;
1952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 // store number in local variable
1954 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001955 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 clear_tv(tv);
1957 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001958 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 break;
1960
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001961 // store value in list or dict variable
1962 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001963 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001964 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001965 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001966 typval_T *tv_dest = STACK_TV_BOT(-1);
1967 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001968
Bram Moolenaar752fc692021-01-04 21:57:11 +01001969 // Stack contains:
1970 // -3 value to be stored
1971 // -2 index
1972 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001973 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001974 SOURCING_LNUM = iptr->isn_lnum;
1975 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001976 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001977 dest_type = tv_dest->v_type;
1978 if (dest_type == VAR_DICT)
1979 status = do_2string(tv_idx, TRUE);
1980 else if (dest_type == VAR_LIST
1981 && tv_idx->v_type != VAR_NUMBER)
1982 {
1983 emsg(_(e_number_exp));
1984 status = FAIL;
1985 }
1986 }
1987 else if (dest_type != tv_dest->v_type)
1988 {
1989 // just in case, should be OK
1990 semsg(_(e_expected_str_but_got_str),
1991 vartype_name(dest_type),
1992 vartype_name(tv_dest->v_type));
1993 status = FAIL;
1994 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001995
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001996 if (status == OK && dest_type == VAR_LIST)
1997 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001998 long lidx = (long)tv_idx->vval.v_number;
1999 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002000
2001 if (list == NULL)
2002 {
2003 emsg(_(e_list_not_set));
2004 goto on_error;
2005 }
2006 if (lidx < 0 && list->lv_len + lidx >= 0)
2007 // negative index is relative to the end
2008 lidx = list->lv_len + lidx;
2009 if (lidx < 0 || lidx > list->lv_len)
2010 {
2011 semsg(_(e_listidx), lidx);
2012 goto on_error;
2013 }
2014 if (lidx < list->lv_len)
2015 {
2016 listitem_T *li = list_find(list, lidx);
2017
2018 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002019 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002020 goto on_error;
2021 // overwrite existing list item
2022 clear_tv(&li->li_tv);
2023 li->li_tv = *tv;
2024 }
2025 else
2026 {
2027 if (error_if_locked(list->lv_lock,
2028 e_cannot_change_list))
2029 goto on_error;
2030 // append to list, only fails when out of memory
2031 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002032 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002033 clear_tv(tv);
2034 }
2035 }
2036 else if (status == OK && dest_type == VAR_DICT)
2037 {
2038 char_u *key = tv_idx->vval.v_string;
2039 dict_T *dict = tv_dest->vval.v_dict;
2040 dictitem_T *di;
2041
2042 SOURCING_LNUM = iptr->isn_lnum;
2043 if (dict == NULL)
2044 {
2045 emsg(_(e_dictionary_not_set));
2046 goto on_error;
2047 }
2048 if (key == NULL)
2049 key = (char_u *)"";
2050 di = dict_find(dict, key, -1);
2051 if (di != NULL)
2052 {
2053 if (error_if_locked(di->di_tv.v_lock,
2054 e_cannot_change_dict_item))
2055 goto on_error;
2056 // overwrite existing value
2057 clear_tv(&di->di_tv);
2058 di->di_tv = *tv;
2059 }
2060 else
2061 {
2062 if (error_if_locked(dict->dv_lock,
2063 e_cannot_change_dict))
2064 goto on_error;
2065 // add to dict, only fails when out of memory
2066 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002067 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002068 clear_tv(tv);
2069 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002070 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002071 else if (status == OK && dest_type == VAR_BLOB)
2072 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002073 long lidx = (long)tv_idx->vval.v_number;
2074 blob_T *blob = tv_dest->vval.v_blob;
2075 varnumber_T nr;
2076 int error = FALSE;
2077 int len;
2078
2079 if (blob == NULL)
2080 {
2081 emsg(_(e_blob_not_set));
2082 goto on_error;
2083 }
2084 len = blob_len(blob);
2085 if (lidx < 0 && len + lidx >= 0)
2086 // negative index is relative to the end
2087 lidx = len + lidx;
2088
2089 // Can add one byte at the end.
2090 if (lidx < 0 || lidx > len)
2091 {
2092 semsg(_(e_blobidx), lidx);
2093 goto on_error;
2094 }
2095 if (value_check_lock(blob->bv_lock,
2096 (char_u *)"blob", FALSE))
2097 goto on_error;
2098 nr = tv_get_number_chk(tv, &error);
2099 if (error)
2100 goto on_error;
2101 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002102 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002103 else
2104 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002105 status = FAIL;
2106 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002107 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002108
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002109 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002110 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002111 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002112 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002113 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002114 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002115 goto on_error;
2116 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002117 }
2118 break;
2119
Bram Moolenaar68452172021-04-12 21:21:02 +02002120 // store value in blob range
2121 case ISN_STORERANGE:
2122 {
2123 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2124 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2125 typval_T *tv_dest = STACK_TV_BOT(-1);
2126 int status = OK;
2127
2128 // Stack contains:
2129 // -4 value to be stored
2130 // -3 first index or "none"
2131 // -2 second index or "none"
2132 // -1 destination blob
2133 tv = STACK_TV_BOT(-4);
2134 if (tv_dest->v_type != VAR_BLOB)
2135 {
2136 status = FAIL;
2137 emsg(_(e_blob_required));
2138 }
2139 else
2140 {
2141 varnumber_T n1;
2142 varnumber_T n2;
2143 int error = FALSE;
2144
2145 n1 = tv_get_number_chk(tv_idx1, &error);
2146 if (error)
2147 status = FAIL;
2148 else
2149 {
2150 if (tv_idx2->v_type == VAR_SPECIAL
2151 && tv_idx2->vval.v_number == VVAL_NONE)
2152 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2153 else
2154 n2 = tv_get_number_chk(tv_idx2, &error);
2155 if (error)
2156 status = FAIL;
2157 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002158 {
2159 long bloblen = blob_len(tv_dest->vval.v_blob);
2160
2161 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002162 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002163 || check_blob_range(bloblen,
2164 n1, n2, FALSE) == FAIL)
2165 status = FAIL;
2166 else
2167 status = blob_set_range(
2168 tv_dest->vval.v_blob, n1, n2, tv);
2169 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002170 }
2171 }
2172
2173 clear_tv(tv_idx1);
2174 clear_tv(tv_idx2);
2175 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002176 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002177 clear_tv(tv);
2178
2179 if (status == FAIL)
2180 goto on_error;
2181 }
2182 break;
2183
Bram Moolenaar0186e582021-01-10 18:33:11 +01002184 // load or store variable or argument from outer scope
2185 case ISN_LOADOUTER:
2186 case ISN_STOREOUTER:
2187 {
2188 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002189 outer_T *outer = ectx->ec_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002190
2191 while (depth > 1 && outer != NULL)
2192 {
2193 outer = outer->out_up;
2194 --depth;
2195 }
2196 if (outer == NULL)
2197 {
2198 SOURCING_LNUM = iptr->isn_lnum;
2199 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002200 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002201 }
2202 tv = ((typval_T *)outer->out_stack->ga_data)
2203 + outer->out_frame_idx + STACK_FRAME_SIZE
2204 + iptr->isn_arg.outer.outer_idx;
2205 if (iptr->isn_type == ISN_LOADOUTER)
2206 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002207 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2208 return FAIL;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002209 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002210 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002211 }
2212 else
2213 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002214 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002215 clear_tv(tv);
2216 *tv = *STACK_TV_BOT(0);
2217 }
2218 }
2219 break;
2220
Bram Moolenaar752fc692021-01-04 21:57:11 +01002221 // unlet item in list or dict variable
2222 case ISN_UNLETINDEX:
2223 {
2224 typval_T *tv_idx = STACK_TV_BOT(-2);
2225 typval_T *tv_dest = STACK_TV_BOT(-1);
2226 int status = OK;
2227
2228 // Stack contains:
2229 // -2 index
2230 // -1 dict or list
2231 if (tv_dest->v_type == VAR_DICT)
2232 {
2233 // unlet a dict item, index must be a string
2234 if (tv_idx->v_type != VAR_STRING)
2235 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002236 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002237 semsg(_(e_expected_str_but_got_str),
2238 vartype_name(VAR_STRING),
2239 vartype_name(tv_idx->v_type));
2240 status = FAIL;
2241 }
2242 else
2243 {
2244 dict_T *d = tv_dest->vval.v_dict;
2245 char_u *key = tv_idx->vval.v_string;
2246 dictitem_T *di = NULL;
2247
2248 if (key == NULL)
2249 key = (char_u *)"";
2250 if (d != NULL)
2251 di = dict_find(d, key, (int)STRLEN(key));
2252 if (di == NULL)
2253 {
2254 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002255 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002256 semsg(_(e_dictkey), key);
2257 status = FAIL;
2258 }
2259 else
2260 {
2261 // TODO: check for dict or item locked
2262 dictitem_remove(d, di);
2263 }
2264 }
2265 }
2266 else if (tv_dest->v_type == VAR_LIST)
2267 {
2268 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002269 SOURCING_LNUM = iptr->isn_lnum;
2270 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002271 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002272 status = FAIL;
2273 }
2274 else
2275 {
2276 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002277 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002278 listitem_T *li = NULL;
2279
2280 li = list_find(l, n);
2281 if (li == NULL)
2282 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002283 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002284 semsg(_(e_listidx), n);
2285 status = FAIL;
2286 }
2287 else
2288 // TODO: check for list or item locked
2289 listitem_remove(l, li);
2290 }
2291 }
2292 else
2293 {
2294 status = FAIL;
2295 semsg(_(e_cannot_index_str),
2296 vartype_name(tv_dest->v_type));
2297 }
2298
2299 clear_tv(tv_idx);
2300 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002301 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002302 if (status == FAIL)
2303 goto on_error;
2304 }
2305 break;
2306
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002307 // unlet range of items in list variable
2308 case ISN_UNLETRANGE:
2309 {
2310 // Stack contains:
2311 // -3 index1
2312 // -2 index2
2313 // -1 dict or list
2314 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2315 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2316 typval_T *tv_dest = STACK_TV_BOT(-1);
2317 int status = OK;
2318
2319 if (tv_dest->v_type == VAR_LIST)
2320 {
2321 // indexes must be a number
2322 SOURCING_LNUM = iptr->isn_lnum;
2323 if (check_for_number(tv_idx1) == FAIL
2324 || check_for_number(tv_idx2) == FAIL)
2325 {
2326 status = FAIL;
2327 }
2328 else
2329 {
2330 list_T *l = tv_dest->vval.v_list;
2331 long n1 = (long)tv_idx1->vval.v_number;
2332 long n2 = (long)tv_idx2->vval.v_number;
2333 listitem_T *li;
2334
2335 li = list_find_index(l, &n1);
2336 if (li == NULL
2337 || list_unlet_range(l, li, NULL, n1,
2338 TRUE, n2) == FAIL)
2339 status = FAIL;
2340 }
2341 }
2342 else
2343 {
2344 status = FAIL;
2345 SOURCING_LNUM = iptr->isn_lnum;
2346 semsg(_(e_cannot_index_str),
2347 vartype_name(tv_dest->v_type));
2348 }
2349
2350 clear_tv(tv_idx1);
2351 clear_tv(tv_idx2);
2352 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002353 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002354 if (status == FAIL)
2355 goto on_error;
2356 }
2357 break;
2358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 // push constant
2360 case ISN_PUSHNR:
2361 case ISN_PUSHBOOL:
2362 case ISN_PUSHSPEC:
2363 case ISN_PUSHF:
2364 case ISN_PUSHS:
2365 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002366 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002367 case ISN_PUSHCHANNEL:
2368 case ISN_PUSHJOB:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002369 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2370 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002372 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002373 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 switch (iptr->isn_type)
2375 {
2376 case ISN_PUSHNR:
2377 tv->v_type = VAR_NUMBER;
2378 tv->vval.v_number = iptr->isn_arg.number;
2379 break;
2380 case ISN_PUSHBOOL:
2381 tv->v_type = VAR_BOOL;
2382 tv->vval.v_number = iptr->isn_arg.number;
2383 break;
2384 case ISN_PUSHSPEC:
2385 tv->v_type = VAR_SPECIAL;
2386 tv->vval.v_number = iptr->isn_arg.number;
2387 break;
2388#ifdef FEAT_FLOAT
2389 case ISN_PUSHF:
2390 tv->v_type = VAR_FLOAT;
2391 tv->vval.v_float = iptr->isn_arg.fnumber;
2392 break;
2393#endif
2394 case ISN_PUSHBLOB:
2395 blob_copy(iptr->isn_arg.blob, tv);
2396 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002397 case ISN_PUSHFUNC:
2398 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002399 if (iptr->isn_arg.string == NULL)
2400 tv->vval.v_string = NULL;
2401 else
2402 tv->vval.v_string =
2403 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002404 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002405 case ISN_PUSHCHANNEL:
2406#ifdef FEAT_JOB_CHANNEL
2407 tv->v_type = VAR_CHANNEL;
2408 tv->vval.v_channel = iptr->isn_arg.channel;
2409 if (tv->vval.v_channel != NULL)
2410 ++tv->vval.v_channel->ch_refcount;
2411#endif
2412 break;
2413 case ISN_PUSHJOB:
2414#ifdef FEAT_JOB_CHANNEL
2415 tv->v_type = VAR_JOB;
2416 tv->vval.v_job = iptr->isn_arg.job;
2417 if (tv->vval.v_job != NULL)
2418 ++tv->vval.v_job->jv_refcount;
2419#endif
2420 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 default:
2422 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002423 tv->vval.v_string = vim_strsave(
2424 iptr->isn_arg.string == NULL
2425 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 }
2427 break;
2428
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002429 case ISN_UNLET:
2430 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2431 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002432 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002433 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002434 case ISN_UNLETENV:
2435 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2436 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002437
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002438 case ISN_LOCKCONST:
2439 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2440 break;
2441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 // create a list from items on the stack; uses a single allocation
2443 // for the list header and the items
2444 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002445 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
2446 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 break;
2448
2449 // create a dict from items on the stack
2450 case ISN_NEWDICT:
2451 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002452 int count = iptr->isn_arg.number;
2453 dict_T *dict = dict_alloc();
2454 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002455 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002456 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457
2458 if (dict == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002459 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 for (idx = 0; idx < count; ++idx)
2461 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002462 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002464 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002465 key = tv->vval.v_string == NULL
2466 ? (char_u *)"" : tv->vval.v_string;
2467 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002468 if (item != NULL)
2469 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002470 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002471 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002472 dict_unref(dict);
2473 goto on_error;
2474 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002475 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 clear_tv(tv);
2477 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002478 {
2479 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002480 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2483 item->di_tv.v_lock = 0;
2484 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002485 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002486 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002487 dict_unref(dict);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002488 return FAIL;
Bram Moolenaare8593122020-07-18 15:17:02 +02002489 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 }
2491
2492 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002493 ectx->ec_stack.ga_len -= 2 * count - 1;
2494 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2495 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002497 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 tv = STACK_TV_BOT(-1);
2499 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002500 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 tv->vval.v_dict = dict;
2502 ++dict->dv_refcount;
2503 }
2504 break;
2505
2506 // call a :def function
2507 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002508 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002509 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2510 NULL,
2511 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002512 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002513 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 break;
2515
2516 // call a builtin function
2517 case ISN_BCALL:
2518 SOURCING_LNUM = iptr->isn_lnum;
2519 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2520 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002521 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002522 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 break;
2524
2525 // call a funcref or partial
2526 case ISN_PCALL:
2527 {
2528 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2529 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002530 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531
2532 SOURCING_LNUM = iptr->isn_lnum;
2533 if (pfunc->cpf_top)
2534 {
2535 // funcref is above the arguments
2536 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2537 }
2538 else
2539 {
2540 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002541 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002542 partial_tv = *STACK_TV_BOT(0);
2543 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002545 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002546 if (tv == &partial_tv)
2547 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002549 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 }
2551 break;
2552
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002553 case ISN_PCALL_END:
2554 // PCALL finished, arguments have been consumed and replaced by
2555 // the return value. Now clear the funcref from the stack,
2556 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002557 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002558 clear_tv(STACK_TV_BOT(-1));
2559 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2560 break;
2561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 // call a user defined function or funcref/partial
2563 case ISN_UCALL:
2564 {
2565 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2566
2567 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002568 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002569 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002570 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
2572 break;
2573
2574 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002575 case ISN_RETURN_ZERO:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002576 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2577 return FAIL;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002578 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002579 ++ectx->ec_stack.ga_len;
Bram Moolenaar299f3032021-01-08 20:53:09 +01002580 tv->v_type = VAR_NUMBER;
2581 tv->vval.v_number = 0;
2582 tv->v_lock = 0;
2583 // FALLTHROUGH
2584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 case ISN_RETURN:
2586 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002587 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002588 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002589
2590 if (trystack->ga_len > 0)
2591 trycmd = ((trycmd_T *)trystack->ga_data)
2592 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002593 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02002594 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002596 // jump to ":finally" or ":endtry"
2597 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002598 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002599 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02002600 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 trycmd->tcd_return = TRUE;
2602 }
2603 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002604 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 }
2606 break;
2607
2608 // push a function reference to a compiled function
2609 case ISN_FUNCREF:
2610 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002611 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2612 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2613 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 if (pt == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002616 return FAIL;
2617 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002618 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002619 vim_free(pt);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002620 return FAIL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002621 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002622 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002623 ectx) == FAIL)
2624 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002627 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 tv->vval.v_partial = pt;
2629 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002630 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 }
2632 break;
2633
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002634 // Create a global function from a lambda.
2635 case ISN_NEWFUNC:
2636 {
2637 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2638
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002639 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02002640 ectx) == FAIL)
2641 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002642 }
2643 break;
2644
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002645 // List functions
2646 case ISN_DEF:
2647 if (iptr->isn_arg.string == NULL)
2648 list_functions(NULL);
2649 else
2650 {
2651 exarg_T ea;
2652
2653 CLEAR_FIELD(ea);
2654 ea.cmd = ea.arg = iptr->isn_arg.string;
2655 define_function(&ea, NULL);
2656 }
2657 break;
2658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 // jump if a condition is met
2660 case ISN_JUMP:
2661 {
2662 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002663 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 int jump = TRUE;
2665
2666 if (when != JUMP_ALWAYS)
2667 {
2668 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002669 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002670 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002671 || when == JUMP_IF_COND_TRUE)
2672 {
2673 SOURCING_LNUM = iptr->isn_lnum;
2674 jump = tv_get_bool_chk(tv, &error);
2675 if (error)
2676 goto on_error;
2677 }
2678 else
2679 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002681 || when == JUMP_AND_KEEP_IF_FALSE
2682 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002684 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 {
2686 // drop the value from the stack
2687 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002688 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 }
2690 }
2691 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02002692 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 }
2694 break;
2695
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002696 // Jump if an argument with a default value was already set and not
2697 // v:none.
2698 case ISN_JUMP_IF_ARG_SET:
2699 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
2700 if (tv->v_type != VAR_UNKNOWN
2701 && !(tv->v_type == VAR_SPECIAL
2702 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02002703 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002704 break;
2705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 // top of a for loop
2707 case ISN_FOR:
2708 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002709 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 typval_T *idxtv =
2711 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2712
Bram Moolenaar4c137212021-04-19 16:48:48 +02002713 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2714 return FAIL;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002715 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002716 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002717 list_T *list = ltv->vval.v_list;
2718
2719 // push the next item from the list
2720 ++idxtv->vval.v_number;
2721 if (list == NULL
2722 || idxtv->vval.v_number >= list->lv_len)
2723 {
2724 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002725 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2726 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002727 }
2728 else if (list->lv_first == &range_list_item)
2729 {
2730 // non-materialized range() list
2731 tv = STACK_TV_BOT(0);
2732 tv->v_type = VAR_NUMBER;
2733 tv->v_lock = 0;
2734 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002736 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002737 }
2738 else
2739 {
2740 listitem_T *li = list_find(list,
2741 idxtv->vval.v_number);
2742
2743 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002744 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002745 }
2746 }
2747 else if (ltv->v_type == VAR_STRING)
2748 {
2749 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002750
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002751 // The index is for the last byte of the previous
2752 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002753 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02002754 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002755 {
2756 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002757 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2758 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002759 }
2760 else
2761 {
2762 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2763
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002764 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002765 tv = STACK_TV_BOT(0);
2766 tv->v_type = VAR_STRING;
2767 tv->vval.v_string = vim_strnsave(
2768 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002769 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002770 idxtv->vval.v_number += clen - 1;
2771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002773 else if (ltv->v_type == VAR_BLOB)
2774 {
2775 blob_T *blob = ltv->vval.v_blob;
2776
2777 // When we get here the first time make a copy of the
2778 // blob, so that the iteration still works when it is
2779 // changed.
2780 if (idxtv->vval.v_number == -1 && blob != NULL)
2781 {
2782 blob_copy(blob, ltv);
2783 blob_unref(blob);
2784 blob = ltv->vval.v_blob;
2785 }
2786
2787 // The index is for the previous byte.
2788 ++idxtv->vval.v_number;
2789 if (blob == NULL
2790 || idxtv->vval.v_number >= blob_len(blob))
2791 {
2792 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02002793 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2794 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002795 }
2796 else
2797 {
2798 // Push the next byte from the blob.
2799 tv = STACK_TV_BOT(0);
2800 tv->v_type = VAR_NUMBER;
2801 tv->vval.v_number = blob_get(blob,
2802 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002803 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02002804 }
2805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 else
2807 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002808 semsg(_(e_for_loop_on_str_not_supported),
2809 vartype_name(ltv->v_type));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002810 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 }
2812 }
2813 break;
2814
2815 // start of ":try" block
2816 case ISN_TRY:
2817 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002818 trycmd_T *trycmd = NULL;
2819
Bram Moolenaar4c137212021-04-19 16:48:48 +02002820 if (GA_GROW(&ectx->ec_trystack, 1) == FAIL)
2821 return FAIL;
2822 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
2823 + ectx->ec_trystack.ga_len;
2824 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002826 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002827 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
2828 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002829 trycmd->tcd_catch_idx =
2830 iptr->isn_arg.try.try_ref->try_catch;
2831 trycmd->tcd_finally_idx =
2832 iptr->isn_arg.try.try_ref->try_finally;
2833 trycmd->tcd_endtry_idx =
2834 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 }
2836 break;
2837
2838 case ISN_PUSHEXC:
2839 if (current_exception == NULL)
2840 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002841 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaar4c137212021-04-19 16:48:48 +02002843 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002845 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
2846 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002848 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002850 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 tv->vval.v_string = vim_strsave(
2852 (char_u *)current_exception->value);
2853 break;
2854
2855 case ISN_CATCH:
2856 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002857 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858
Bram Moolenaar4c137212021-04-19 16:48:48 +02002859 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 if (trystack->ga_len > 0)
2861 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002862 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 + trystack->ga_len - 1;
2864 trycmd->tcd_caught = TRUE;
2865 }
2866 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002867 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 catch_exception(current_exception);
2869 }
2870 break;
2871
Bram Moolenaarc150c092021-02-13 15:02:46 +01002872 case ISN_TRYCONT:
2873 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002874 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002875 trycont_T *trycont = &iptr->isn_arg.trycont;
2876 int i;
2877 trycmd_T *trycmd;
2878 int iidx = trycont->tct_where;
2879
2880 if (trystack->ga_len < trycont->tct_levels)
2881 {
2882 siemsg("TRYCONT: expected %d levels, found %d",
2883 trycont->tct_levels, trystack->ga_len);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002884 return FAIL;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002885 }
2886 // Make :endtry jump to any outer try block and the last
2887 // :endtry inside the loop to the loop start.
2888 for (i = trycont->tct_levels; i > 0; --i)
2889 {
2890 trycmd = ((trycmd_T *)trystack->ga_data)
2891 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002892 // Add one to tcd_cont to be able to jump to
2893 // instruction with index zero.
2894 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002895 iidx = trycmd->tcd_finally_idx == 0
2896 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002897 }
2898 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02002899 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002900 }
2901 break;
2902
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002903 case ISN_FINALLY:
2904 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002905 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002906 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2907 + trystack->ga_len - 1;
2908
2909 // Reset the index to avoid a return statement jumps here
2910 // again.
2911 trycmd->tcd_finally_idx = 0;
2912 break;
2913 }
2914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 // end of ":try" block
2916 case ISN_ENDTRY:
2917 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002918 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919
2920 if (trystack->ga_len > 0)
2921 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002922 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 --trystack->ga_len;
2925 --trylevel;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002926 ectx->ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 trycmd = ((trycmd_T *)trystack->ga_data)
2928 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002929 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 {
2931 // discard the exception
2932 if (caught_stack == current_exception)
2933 caught_stack = caught_stack->caught;
2934 discard_current_exception();
2935 }
2936
2937 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002938 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002939
Bram Moolenaar4c137212021-04-19 16:48:48 +02002940 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01002941 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002942 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002943 clear_tv(STACK_TV_BOT(0));
2944 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002945 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002946 // handling :continue: jump to outer try block or
2947 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02002948 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 }
2950 }
2951 break;
2952
2953 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002954 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002955 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002956
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002957 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2958 {
2959 // throwing an exception while using "silent!" causes
2960 // the function to abort but not display an error.
2961 tv = STACK_TV_BOT(-1);
2962 clear_tv(tv);
2963 tv->v_type = VAR_NUMBER;
2964 tv->vval.v_number = 0;
2965 goto done;
2966 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002967 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002968 tv = STACK_TV_BOT(0);
2969 if (tv->vval.v_string == NULL
2970 || *skipwhite(tv->vval.v_string) == NUL)
2971 {
2972 vim_free(tv->vval.v_string);
2973 SOURCING_LNUM = iptr->isn_lnum;
2974 emsg(_(e_throw_with_empty_string));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002975 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002976 }
2977
2978 // Inside a "catch" we need to first discard the caught
2979 // exception.
2980 if (trystack->ga_len > 0)
2981 {
2982 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2983 + trystack->ga_len - 1;
2984 if (trycmd->tcd_caught && current_exception != NULL)
2985 {
2986 // discard the exception
2987 if (caught_stack == current_exception)
2988 caught_stack = caught_stack->caught;
2989 discard_current_exception();
2990 trycmd->tcd_caught = FALSE;
2991 }
2992 }
2993
2994 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2995 == FAIL)
2996 {
2997 vim_free(tv->vval.v_string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002998 return FAIL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002999 }
3000 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 break;
3003
3004 // compare with special values
3005 case ISN_COMPAREBOOL:
3006 case ISN_COMPARESPECIAL:
3007 {
3008 typval_T *tv1 = STACK_TV_BOT(-2);
3009 typval_T *tv2 = STACK_TV_BOT(-1);
3010 varnumber_T arg1 = tv1->vval.v_number;
3011 varnumber_T arg2 = tv2->vval.v_number;
3012 int res;
3013
3014 switch (iptr->isn_arg.op.op_type)
3015 {
3016 case EXPR_EQUAL: res = arg1 == arg2; break;
3017 case EXPR_NEQUAL: res = arg1 != arg2; break;
3018 default: res = 0; break;
3019 }
3020
Bram Moolenaar4c137212021-04-19 16:48:48 +02003021 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 tv1->v_type = VAR_BOOL;
3023 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3024 }
3025 break;
3026
3027 // Operation with two number arguments
3028 case ISN_OPNR:
3029 case ISN_COMPARENR:
3030 {
3031 typval_T *tv1 = STACK_TV_BOT(-2);
3032 typval_T *tv2 = STACK_TV_BOT(-1);
3033 varnumber_T arg1 = tv1->vval.v_number;
3034 varnumber_T arg2 = tv2->vval.v_number;
3035 varnumber_T res;
3036
3037 switch (iptr->isn_arg.op.op_type)
3038 {
3039 case EXPR_MULT: res = arg1 * arg2; break;
3040 case EXPR_DIV: res = arg1 / arg2; break;
3041 case EXPR_REM: res = arg1 % arg2; break;
3042 case EXPR_SUB: res = arg1 - arg2; break;
3043 case EXPR_ADD: res = arg1 + arg2; break;
3044
3045 case EXPR_EQUAL: res = arg1 == arg2; break;
3046 case EXPR_NEQUAL: res = arg1 != arg2; break;
3047 case EXPR_GREATER: res = arg1 > arg2; break;
3048 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3049 case EXPR_SMALLER: res = arg1 < arg2; break;
3050 case EXPR_SEQUAL: res = arg1 <= arg2; break;
3051 default: res = 0; break;
3052 }
3053
Bram Moolenaar4c137212021-04-19 16:48:48 +02003054 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 if (iptr->isn_type == ISN_COMPARENR)
3056 {
3057 tv1->v_type = VAR_BOOL;
3058 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3059 }
3060 else
3061 tv1->vval.v_number = res;
3062 }
3063 break;
3064
3065 // Computation with two float arguments
3066 case ISN_OPFLOAT:
3067 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003068#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 {
3070 typval_T *tv1 = STACK_TV_BOT(-2);
3071 typval_T *tv2 = STACK_TV_BOT(-1);
3072 float_T arg1 = tv1->vval.v_float;
3073 float_T arg2 = tv2->vval.v_float;
3074 float_T res = 0;
3075 int cmp = FALSE;
3076
3077 switch (iptr->isn_arg.op.op_type)
3078 {
3079 case EXPR_MULT: res = arg1 * arg2; break;
3080 case EXPR_DIV: res = arg1 / arg2; break;
3081 case EXPR_SUB: res = arg1 - arg2; break;
3082 case EXPR_ADD: res = arg1 + arg2; break;
3083
3084 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3085 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3086 case EXPR_GREATER: cmp = arg1 > arg2; break;
3087 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3088 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3089 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3090 default: cmp = 0; break;
3091 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003092 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 if (iptr->isn_type == ISN_COMPAREFLOAT)
3094 {
3095 tv1->v_type = VAR_BOOL;
3096 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3097 }
3098 else
3099 tv1->vval.v_float = res;
3100 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003101#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 break;
3103
3104 case ISN_COMPARELIST:
3105 {
3106 typval_T *tv1 = STACK_TV_BOT(-2);
3107 typval_T *tv2 = STACK_TV_BOT(-1);
3108 list_T *arg1 = tv1->vval.v_list;
3109 list_T *arg2 = tv2->vval.v_list;
3110 int cmp = FALSE;
3111 int ic = iptr->isn_arg.op.op_ic;
3112
3113 switch (iptr->isn_arg.op.op_type)
3114 {
3115 case EXPR_EQUAL: cmp =
3116 list_equal(arg1, arg2, ic, FALSE); break;
3117 case EXPR_NEQUAL: cmp =
3118 !list_equal(arg1, arg2, ic, FALSE); break;
3119 case EXPR_IS: cmp = arg1 == arg2; break;
3120 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3121 default: cmp = 0; break;
3122 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003123 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 clear_tv(tv1);
3125 clear_tv(tv2);
3126 tv1->v_type = VAR_BOOL;
3127 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3128 }
3129 break;
3130
3131 case ISN_COMPAREBLOB:
3132 {
3133 typval_T *tv1 = STACK_TV_BOT(-2);
3134 typval_T *tv2 = STACK_TV_BOT(-1);
3135 blob_T *arg1 = tv1->vval.v_blob;
3136 blob_T *arg2 = tv2->vval.v_blob;
3137 int cmp = FALSE;
3138
3139 switch (iptr->isn_arg.op.op_type)
3140 {
3141 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3142 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3143 case EXPR_IS: cmp = arg1 == arg2; break;
3144 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3145 default: cmp = 0; break;
3146 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003147 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 clear_tv(tv1);
3149 clear_tv(tv2);
3150 tv1->v_type = VAR_BOOL;
3151 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3152 }
3153 break;
3154
3155 // TODO: handle separately
3156 case ISN_COMPARESTRING:
3157 case ISN_COMPAREDICT:
3158 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 case ISN_COMPAREANY:
3160 {
3161 typval_T *tv1 = STACK_TV_BOT(-2);
3162 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003163 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 int ic = iptr->isn_arg.op.op_ic;
3165
Bram Moolenaareb26f432020-09-14 16:50:05 +02003166 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003167 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003169 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 }
3171 break;
3172
3173 case ISN_ADDLIST:
3174 case ISN_ADDBLOB:
3175 {
3176 typval_T *tv1 = STACK_TV_BOT(-2);
3177 typval_T *tv2 = STACK_TV_BOT(-1);
3178
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003179 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 if (iptr->isn_type == ISN_ADDLIST)
3181 eval_addlist(tv1, tv2);
3182 else
3183 eval_addblob(tv1, tv2);
3184 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003185 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 }
3187 break;
3188
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003189 case ISN_LISTAPPEND:
3190 {
3191 typval_T *tv1 = STACK_TV_BOT(-2);
3192 typval_T *tv2 = STACK_TV_BOT(-1);
3193 list_T *l = tv1->vval.v_list;
3194
3195 // add an item to a list
3196 if (l == NULL)
3197 {
3198 SOURCING_LNUM = iptr->isn_lnum;
3199 emsg(_(e_cannot_add_to_null_list));
3200 goto on_error;
3201 }
3202 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003203 return FAIL;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003204 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003205 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003206 }
3207 break;
3208
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003209 case ISN_BLOBAPPEND:
3210 {
3211 typval_T *tv1 = STACK_TV_BOT(-2);
3212 typval_T *tv2 = STACK_TV_BOT(-1);
3213 blob_T *b = tv1->vval.v_blob;
3214 int error = FALSE;
3215 varnumber_T n;
3216
3217 // add a number to a blob
3218 if (b == NULL)
3219 {
3220 SOURCING_LNUM = iptr->isn_lnum;
3221 emsg(_(e_cannot_add_to_null_blob));
3222 goto on_error;
3223 }
3224 n = tv_get_number_chk(tv2, &error);
3225 if (error)
3226 goto on_error;
3227 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003228 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003229 }
3230 break;
3231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 // Computation with two arguments of unknown type
3233 case ISN_OPANY:
3234 {
3235 typval_T *tv1 = STACK_TV_BOT(-2);
3236 typval_T *tv2 = STACK_TV_BOT(-1);
3237 varnumber_T n1, n2;
3238#ifdef FEAT_FLOAT
3239 float_T f1 = 0, f2 = 0;
3240#endif
3241 int error = FALSE;
3242
3243 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3244 {
3245 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3246 {
3247 eval_addlist(tv1, tv2);
3248 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003249 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 break;
3251 }
3252 else if (tv1->v_type == VAR_BLOB
3253 && tv2->v_type == VAR_BLOB)
3254 {
3255 eval_addblob(tv1, tv2);
3256 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003257 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 break;
3259 }
3260 }
3261#ifdef FEAT_FLOAT
3262 if (tv1->v_type == VAR_FLOAT)
3263 {
3264 f1 = tv1->vval.v_float;
3265 n1 = 0;
3266 }
3267 else
3268#endif
3269 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003270 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 n1 = tv_get_number_chk(tv1, &error);
3272 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003273 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274#ifdef FEAT_FLOAT
3275 if (tv2->v_type == VAR_FLOAT)
3276 f1 = n1;
3277#endif
3278 }
3279#ifdef FEAT_FLOAT
3280 if (tv2->v_type == VAR_FLOAT)
3281 {
3282 f2 = tv2->vval.v_float;
3283 n2 = 0;
3284 }
3285 else
3286#endif
3287 {
3288 n2 = tv_get_number_chk(tv2, &error);
3289 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003290 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291#ifdef FEAT_FLOAT
3292 if (tv1->v_type == VAR_FLOAT)
3293 f2 = n2;
3294#endif
3295 }
3296#ifdef FEAT_FLOAT
3297 // if there is a float on either side the result is a float
3298 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3299 {
3300 switch (iptr->isn_arg.op.op_type)
3301 {
3302 case EXPR_MULT: f1 = f1 * f2; break;
3303 case EXPR_DIV: f1 = f1 / f2; break;
3304 case EXPR_SUB: f1 = f1 - f2; break;
3305 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003306 default: SOURCING_LNUM = iptr->isn_lnum;
3307 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003308 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 }
3310 clear_tv(tv1);
3311 clear_tv(tv2);
3312 tv1->v_type = VAR_FLOAT;
3313 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003314 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 }
3316 else
3317#endif
3318 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003319 int failed = FALSE;
3320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 switch (iptr->isn_arg.op.op_type)
3322 {
3323 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003324 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3325 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003326 goto on_error;
3327 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 case EXPR_SUB: n1 = n1 - n2; break;
3329 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003330 default: n1 = num_modulus(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 }
3335 clear_tv(tv1);
3336 clear_tv(tv2);
3337 tv1->v_type = VAR_NUMBER;
3338 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003339 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 }
3341 }
3342 break;
3343
3344 case ISN_CONCAT:
3345 {
3346 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3347 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3348 char_u *res;
3349
3350 res = concat_str(str1, str2);
3351 clear_tv(STACK_TV_BOT(-2));
3352 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003353 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 STACK_TV_BOT(-1)->vval.v_string = res;
3355 }
3356 break;
3357
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003358 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003359 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003360 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003361 int is_slice = iptr->isn_type == ISN_STRSLICE;
3362 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003363 char_u *res;
3364
3365 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003366 // string slice: string is at stack-3, first index at
3367 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003368 if (is_slice)
3369 {
3370 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003371 n1 = tv->vval.v_number;
3372 }
3373
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003374 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003375 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003376
Bram Moolenaar4c137212021-04-19 16:48:48 +02003377 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003378 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003379 if (is_slice)
3380 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003381 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003382 else
3383 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003384 // single character (including composing characters).
3385 // If the index is too big or negative the result is
3386 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003387 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003388 vim_free(tv->vval.v_string);
3389 tv->vval.v_string = res;
3390 }
3391 break;
3392
3393 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003394 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003395 case ISN_BLOBINDEX:
3396 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003398 int is_slice = iptr->isn_type == ISN_LISTSLICE
3399 || iptr->isn_type == ISN_BLOBSLICE;
3400 int is_blob = iptr->isn_type == ISN_BLOBINDEX
3401 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02003402 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003403 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404
3405 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003406 // list slice: list is at stack-3, indexes at stack-2 and
3407 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003408 // Same for blob.
3409 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410
3411 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003412 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003414
3415 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 {
Bram Moolenaared591872020-08-15 22:14:53 +02003417 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003418 n1 = tv->vval.v_number;
3419 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 }
Bram Moolenaared591872020-08-15 22:14:53 +02003421
Bram Moolenaar4c137212021-04-19 16:48:48 +02003422 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003423 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003424 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02003425 if (is_blob)
3426 {
3427 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
3428 n1, n2, FALSE, tv) == FAIL)
3429 goto on_error;
3430 }
3431 else
3432 {
3433 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
3434 n1, n2, FALSE, tv, TRUE) == FAIL)
3435 goto on_error;
3436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 }
3438 break;
3439
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003440 case ISN_ANYINDEX:
3441 case ISN_ANYSLICE:
3442 {
3443 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3444 typval_T *var1, *var2;
3445 int res;
3446
3447 // index: composite is at stack-2, index at stack-1
3448 // slice: composite is at stack-3, indexes at stack-2 and
3449 // stack-1
3450 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003451 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003452 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3453 goto on_error;
3454 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3455 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003456 res = eval_index_inner(tv, is_slice, var1, var2,
3457 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003458 clear_tv(var1);
3459 if (is_slice)
3460 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003461 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003462 if (res == FAIL)
3463 goto on_error;
3464 }
3465 break;
3466
Bram Moolenaar9af78762020-06-16 11:34:42 +02003467 case ISN_SLICE:
3468 {
3469 list_T *list;
3470 int count = iptr->isn_arg.number;
3471
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003472 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003473 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003474 list = tv->vval.v_list;
3475
3476 // no error for short list, expect it to be checked earlier
3477 if (list != NULL && list->lv_len >= count)
3478 {
3479 list_T *newlist = list_slice(list,
3480 count, list->lv_len - 1);
3481
3482 if (newlist != NULL)
3483 {
3484 list_unref(list);
3485 tv->vval.v_list = newlist;
3486 ++newlist->lv_refcount;
3487 }
3488 }
3489 }
3490 break;
3491
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003492 case ISN_GETITEM:
3493 {
3494 listitem_T *li;
3495 int index = iptr->isn_arg.number;
3496
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003497 // Get list item: list is at stack-1, push item.
3498 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003499 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003500 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003501
Bram Moolenaar4c137212021-04-19 16:48:48 +02003502 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3503 return FAIL;
3504 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003505 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003506
3507 // Useful when used in unpack assignment. Reset at
3508 // ISN_DROP.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003509 ectx->ec_where.wt_index = index + 1;
3510 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003511 }
3512 break;
3513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 case ISN_MEMBER:
3515 {
3516 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003517 char_u *key;
3518 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003519 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003520
3521 // dict member: dict is at stack-2, key at stack-1
3522 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003523 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003524 dict = tv->vval.v_dict;
3525
3526 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003527 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003528 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003529 if (key == NULL)
3530 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003531
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003532 if ((di = dict_find(dict, key, -1)) == NULL)
3533 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003534 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003535 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003536
3537 // If :silent! is used we will continue, make sure the
3538 // stack contents makes sense.
3539 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003540 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003541 tv = STACK_TV_BOT(-1);
3542 clear_tv(tv);
3543 tv->v_type = VAR_NUMBER;
3544 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003545 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003546 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003547 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003548 --ectx->ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003549 // Clear the dict only after getting the item, to avoid
3550 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003551 tv = STACK_TV_BOT(-1);
3552 temp_tv = *tv;
3553 copy_tv(&di->di_tv, tv);
3554 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003555 }
3556 break;
3557
3558 // dict member with string key
3559 case ISN_STRINGMEMBER:
3560 {
3561 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003563 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564
3565 tv = STACK_TV_BOT(-1);
3566 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3567 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003568 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003570 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 }
3572 dict = tv->vval.v_dict;
3573
3574 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3575 == NULL)
3576 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003577 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003579 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003581 // Clear the dict after getting the item, to avoid that it
3582 // make the item invalid.
3583 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003585 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 }
3587 break;
3588
3589 case ISN_NEGATENR:
3590 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003591 if (tv->v_type != VAR_NUMBER
3592#ifdef FEAT_FLOAT
3593 && tv->v_type != VAR_FLOAT
3594#endif
3595 )
3596 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003597 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003598 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003599 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003600 }
3601#ifdef FEAT_FLOAT
3602 if (tv->v_type == VAR_FLOAT)
3603 tv->vval.v_float = -tv->vval.v_float;
3604 else
3605#endif
3606 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 break;
3608
3609 case ISN_CHECKNR:
3610 {
3611 int error = FALSE;
3612
3613 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003614 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003616 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 (void)tv_get_number_chk(tv, &error);
3618 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003619 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 }
3621 break;
3622
3623 case ISN_CHECKTYPE:
3624 {
3625 checktype_T *ct = &iptr->isn_arg.type;
3626
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003627 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003628 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003629 if (!ectx->ec_where.wt_variable)
3630 ectx->ec_where.wt_index = ct->ct_arg_idx;
3631 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
3632 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003633 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003634 if (!ectx->ec_where.wt_variable)
3635 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003636
3637 // number 0 is FALSE, number 1 is TRUE
3638 if (tv->v_type == VAR_NUMBER
3639 && ct->ct_type->tt_type == VAR_BOOL
3640 && (tv->vval.v_number == 0
3641 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003643 tv->v_type = VAR_BOOL;
3644 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003645 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 }
3647 }
3648 break;
3649
Bram Moolenaar9af78762020-06-16 11:34:42 +02003650 case ISN_CHECKLEN:
3651 {
3652 int min_len = iptr->isn_arg.checklen.cl_min_len;
3653 list_T *list = NULL;
3654
3655 tv = STACK_TV_BOT(-1);
3656 if (tv->v_type == VAR_LIST)
3657 list = tv->vval.v_list;
3658 if (list == NULL || list->lv_len < min_len
3659 || (list->lv_len > min_len
3660 && !iptr->isn_arg.checklen.cl_more_OK))
3661 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003662 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003663 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003664 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003665 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003666 }
3667 }
3668 break;
3669
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003670 case ISN_SETTYPE:
3671 {
3672 checktype_T *ct = &iptr->isn_arg.type;
3673
3674 tv = STACK_TV_BOT(-1);
3675 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3676 {
3677 free_type(tv->vval.v_dict->dv_type);
3678 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3679 }
3680 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3681 {
3682 free_type(tv->vval.v_list->lv_type);
3683 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3684 }
3685 }
3686 break;
3687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003689 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 {
3691 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003692 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693
3694 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003695 if (iptr->isn_type == ISN_2BOOL)
3696 {
3697 n = tv2bool(tv);
3698 if (iptr->isn_arg.number) // invert
3699 n = !n;
3700 }
3701 else
3702 {
3703 SOURCING_LNUM = iptr->isn_lnum;
3704 n = tv_get_bool_chk(tv, &error);
3705 if (error)
3706 goto on_error;
3707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 clear_tv(tv);
3709 tv->v_type = VAR_BOOL;
3710 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3711 }
3712 break;
3713
3714 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003715 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003716 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003717 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3718 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3719 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 break;
3721
Bram Moolenaar08597872020-12-10 19:43:40 +01003722 case ISN_RANGE:
3723 {
3724 exarg_T ea;
3725 char *errormsg;
3726
Bram Moolenaarece0b872021-01-08 20:40:45 +01003727 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003728 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003729 ea.addr_type = ADDR_LINES;
3730 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003731 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003732 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003733 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003734
Bram Moolenaar4c137212021-04-19 16:48:48 +02003735 if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
3736 return FAIL;
3737 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003738 tv = STACK_TV_BOT(-1);
3739 tv->v_type = VAR_NUMBER;
3740 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003741 if (ea.addr_count == 0)
3742 tv->vval.v_number = curwin->w_cursor.lnum;
3743 else
3744 tv->vval.v_number = ea.line2;
3745 }
3746 break;
3747
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003748 case ISN_PUT:
3749 {
3750 int regname = iptr->isn_arg.put.put_regname;
3751 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3752 char_u *expr = NULL;
3753 int dir = FORWARD;
3754
Bram Moolenaar08597872020-12-10 19:43:40 +01003755 if (lnum < -2)
3756 {
3757 // line number was put on the stack by ISN_RANGE
3758 tv = STACK_TV_BOT(-1);
3759 curwin->w_cursor.lnum = tv->vval.v_number;
3760 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3761 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003762 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01003763 }
3764 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003765 // :put! above cursor
3766 dir = BACKWARD;
3767 else if (lnum >= 0)
3768 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003769
3770 if (regname == '=')
3771 {
3772 tv = STACK_TV_BOT(-1);
3773 if (tv->v_type == VAR_STRING)
3774 expr = tv->vval.v_string;
3775 else
3776 {
3777 expr = typval2string(tv, TRUE); // allocates value
3778 clear_tv(tv);
3779 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003780 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003781 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003782 check_cursor();
3783 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3784 vim_free(expr);
3785 }
3786 break;
3787
Bram Moolenaar02194d22020-10-24 23:08:38 +02003788 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003789 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
3790 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
3791 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
3792 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003793 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3794 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003795 break;
3796
Bram Moolenaar02194d22020-10-24 23:08:38 +02003797 case ISN_CMDMOD_REV:
3798 // filter regprog is owned by the instruction, don't free it
3799 cmdmod.cmod_filter_regmatch.regprog = NULL;
3800 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003801 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
3802 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003803 break;
3804
Bram Moolenaar792f7862020-11-23 08:31:18 +01003805 case ISN_UNPACK:
3806 {
3807 int count = iptr->isn_arg.unpack.unp_count;
3808 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3809 list_T *l;
3810 listitem_T *li;
3811 int i;
3812
3813 // Check there is a valid list to unpack.
3814 tv = STACK_TV_BOT(-1);
3815 if (tv->v_type != VAR_LIST)
3816 {
3817 SOURCING_LNUM = iptr->isn_lnum;
3818 emsg(_(e_for_argument_must_be_sequence_of_lists));
3819 goto on_error;
3820 }
3821 l = tv->vval.v_list;
3822 if (l == NULL
3823 || l->lv_len < (semicolon ? count - 1 : count))
3824 {
3825 SOURCING_LNUM = iptr->isn_lnum;
3826 emsg(_(e_list_value_does_not_have_enough_items));
3827 goto on_error;
3828 }
3829 else if (!semicolon && l->lv_len > count)
3830 {
3831 SOURCING_LNUM = iptr->isn_lnum;
3832 emsg(_(e_list_value_has_more_items_than_targets));
3833 goto on_error;
3834 }
3835
3836 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003837 if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL)
3838 return FAIL;
3839 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003840
3841 // Variable after semicolon gets a list with the remaining
3842 // items.
3843 if (semicolon)
3844 {
3845 list_T *rem_list =
3846 list_alloc_with_items(l->lv_len - count + 1);
3847
3848 if (rem_list == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003849 return FAIL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01003850 tv = STACK_TV_BOT(-count);
3851 tv->vval.v_list = rem_list;
3852 ++rem_list->lv_refcount;
3853 tv->v_lock = 0;
3854 li = l->lv_first;
3855 for (i = 0; i < count - 1; ++i)
3856 li = li->li_next;
3857 for (i = 0; li != NULL; ++i)
3858 {
3859 list_set_item(rem_list, i, &li->li_tv);
3860 li = li->li_next;
3861 }
3862 --count;
3863 }
3864
3865 // Produce the values in reverse order, first item last.
3866 li = l->lv_first;
3867 for (i = 0; i < count; ++i)
3868 {
3869 tv = STACK_TV_BOT(-i - 1);
3870 copy_tv(&li->li_tv, tv);
3871 li = li->li_next;
3872 }
3873
3874 list_unref(l);
3875 }
3876 break;
3877
Bram Moolenaarb2049902021-01-24 12:53:53 +01003878 case ISN_PROF_START:
3879 case ISN_PROF_END:
3880 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003881#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003882 funccall_T cookie;
3883 ufunc_T *cur_ufunc =
3884 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003885 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003886
3887 cookie.func = cur_ufunc;
3888 if (iptr->isn_type == ISN_PROF_START)
3889 {
3890 func_line_start(&cookie, iptr->isn_lnum);
3891 // if we get here the instruction is executed
3892 func_line_exec(&cookie);
3893 }
3894 else
3895 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003896#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003897 }
3898 break;
3899
Bram Moolenaar389df252020-07-09 21:20:47 +02003900 case ISN_SHUFFLE:
3901 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003902 typval_T tmp_tv;
3903 int item = iptr->isn_arg.shuffle.shfl_item;
3904 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003905
3906 tmp_tv = *STACK_TV_BOT(-item);
3907 for ( ; up > 0 && item > 1; --up)
3908 {
3909 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3910 --item;
3911 }
3912 *STACK_TV_BOT(-item) = tmp_tv;
3913 }
3914 break;
3915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003917 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003919 ectx->ec_where.wt_index = 0;
3920 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 break;
3922 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003923 continue;
3924
Bram Moolenaard032f342020-07-18 18:13:02 +02003925func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003926 // Restore previous function. If the frame pointer is where we started
3927 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003928 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003929 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003930
Bram Moolenaar4c137212021-04-19 16:48:48 +02003931 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003932 // only fails when out of memory
Bram Moolenaar4c137212021-04-19 16:48:48 +02003933 return FAIL;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003934 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003935
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003936on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003937 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003938 // If "emsg_silent" is set then ignore the error, unless it was set
3939 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003940 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003941 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003942 {
3943 // If a sequence of instructions causes an error while ":silent!"
3944 // was used, restore the stack length and jump ahead to restoring
3945 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003946 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003947 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003948 while (ectx->ec_stack.ga_len
3949 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003950 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003951 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003952 clear_tv(STACK_TV_BOT(0));
3953 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
3955 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003956 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003957 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003958 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003959on_fatal_error:
3960 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003961 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003962 if (trylevel <= ectx->ec_trylevel_at_start)
3963 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 }
3965
3966done:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003967 return OK;
3968}
3969
3970/*
3971 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
3972 * "substitute_instr".
3973 */
3974 char_u *
3975exe_substitute_instr(void)
3976{
3977 ectx_T *ectx = substitute_instr->subs_ectx;
3978 isn_T *save_instr = ectx->ec_instr;
3979 int save_iidx = ectx->ec_iidx;
3980 char_u *res;
3981
3982 ectx->ec_instr = substitute_instr->subs_instr;
3983 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02003984 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003985 typval_T *tv = STACK_TV_BOT(-1);
3986
3987 res = vim_strsave(tv_get_string(tv));
3988 --ectx->ec_stack.ga_len;
3989 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02003990 }
3991 else
3992 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003993 substitute_instr->subs_status = FAIL;
3994 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02003995 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996
Bram Moolenaar4c137212021-04-19 16:48:48 +02003997 ectx->ec_instr = save_instr;
3998 ectx->ec_iidx = save_iidx;
3999
4000 return res;
4001}
4002
4003/*
4004 * Call a "def" function from old Vim script.
4005 * Return OK or FAIL.
4006 */
4007 int
4008call_def_function(
4009 ufunc_T *ufunc,
4010 int argc_arg, // nr of arguments
4011 typval_T *argv, // arguments
4012 partial_T *partial, // optional partial for context
4013 typval_T *rettv) // return value
4014{
4015 ectx_T ectx; // execution context
4016 int argc = argc_arg;
4017 typval_T *tv;
4018 int idx;
4019 int ret = FAIL;
4020 int defcount = ufunc->uf_args.ga_len - argc;
4021 sctx_T save_current_sctx = current_sctx;
4022 int did_emsg_before = did_emsg_cumul + did_emsg;
4023 int save_suppress_errthrow = suppress_errthrow;
4024 msglist_T **saved_msg_list = NULL;
4025 msglist_T *private_msg_list = NULL;
4026 int save_emsg_silent_def = emsg_silent_def;
4027 int save_did_emsg_def = did_emsg_def;
4028 int orig_funcdepth;
4029
4030// Get pointer to item in the stack.
4031#undef STACK_TV
4032#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4033
4034// Get pointer to item at the bottom of the stack, -1 is the bottom.
4035#undef STACK_TV_BOT
4036#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4037
4038// Get pointer to a local variable on the stack. Negative for arguments.
4039#undef STACK_TV_VAR
4040#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4041
4042 if (ufunc->uf_def_status == UF_NOT_COMPILED
4043 || ufunc->uf_def_status == UF_COMPILE_ERROR
4044 || (func_needs_compiling(ufunc, PROFILING(ufunc))
4045 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
4046 == FAIL))
4047 {
4048 if (did_emsg_cumul + did_emsg == did_emsg_before)
4049 semsg(_(e_function_is_not_compiled_str),
4050 printable_func_name(ufunc));
4051 return FAIL;
4052 }
4053
4054 {
4055 // Check the function was really compiled.
4056 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4057 + ufunc->uf_dfunc_idx;
4058 if (INSTRUCTIONS(dfunc) == NULL)
4059 {
4060 iemsg("using call_def_function() on not compiled function");
4061 return FAIL;
4062 }
4063 }
4064
4065 // If depth of calling is getting too high, don't execute the function.
4066 orig_funcdepth = funcdepth_get();
4067 if (funcdepth_increment() == FAIL)
4068 return FAIL;
4069
4070 CLEAR_FIELD(ectx);
4071 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4072 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
4073 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
4074 {
4075 funcdepth_decrement();
4076 return FAIL;
4077 }
4078 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4079 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4080 ectx.ec_did_emsg_before = did_emsg_before;
4081 ectx.ec_trylevel_at_start = trylevel;
4082
4083 idx = argc - ufunc->uf_args.ga_len;
4084 if (idx > 0 && ufunc->uf_va_name == NULL)
4085 {
4086 if (idx == 1)
4087 emsg(_(e_one_argument_too_many));
4088 else
4089 semsg(_(e_nr_arguments_too_many), idx);
4090 goto failed_early;
4091 }
4092
4093 // Put arguments on the stack, but no more than what the function expects.
4094 // A lambda can be called with more arguments than it uses.
4095 for (idx = 0; idx < argc
4096 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4097 ++idx)
4098 {
4099 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4100 && argv[idx].v_type == VAR_SPECIAL
4101 && argv[idx].vval.v_number == VVAL_NONE)
4102 {
4103 // Use the default value.
4104 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4105 }
4106 else
4107 {
4108 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4109 && check_typval_arg_type(
4110 ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL)
4111 goto failed_early;
4112 copy_tv(&argv[idx], STACK_TV_BOT(0));
4113 }
4114 ++ectx.ec_stack.ga_len;
4115 }
4116
4117 // Turn varargs into a list. Empty list if no args.
4118 if (ufunc->uf_va_name != NULL)
4119 {
4120 int vararg_count = argc - ufunc->uf_args.ga_len;
4121
4122 if (vararg_count < 0)
4123 vararg_count = 0;
4124 else
4125 argc -= vararg_count;
4126 if (exe_newlist(vararg_count, &ectx) == FAIL)
4127 goto failed_early;
4128
4129 // Check the type of the list items.
4130 tv = STACK_TV_BOT(-1);
4131 if (ufunc->uf_va_type != NULL
4132 && ufunc->uf_va_type != &t_list_any
4133 && ufunc->uf_va_type->tt_member != &t_any
4134 && tv->vval.v_list != NULL)
4135 {
4136 type_T *expected = ufunc->uf_va_type->tt_member;
4137 listitem_T *li = tv->vval.v_list->lv_first;
4138
4139 for (idx = 0; idx < vararg_count; ++idx)
4140 {
4141 if (check_typval_arg_type(expected, &li->li_tv,
4142 argc + idx + 1) == FAIL)
4143 goto failed_early;
4144 li = li->li_next;
4145 }
4146 }
4147
4148 if (defcount > 0)
4149 // Move varargs list to below missing default arguments.
4150 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4151 --ectx.ec_stack.ga_len;
4152 }
4153
4154 // Make space for omitted arguments, will store default value below.
4155 // Any varargs list goes after them.
4156 if (defcount > 0)
4157 for (idx = 0; idx < defcount; ++idx)
4158 {
4159 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4160 ++ectx.ec_stack.ga_len;
4161 }
4162 if (ufunc->uf_va_name != NULL)
4163 ++ectx.ec_stack.ga_len;
4164
4165 // Frame pointer points to just after arguments.
4166 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4167 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4168
4169 {
4170 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4171 + ufunc->uf_dfunc_idx;
4172 ufunc_T *base_ufunc = dfunc->df_ufunc;
4173
4174 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4175 // by copy_func().
4176 if (partial != NULL || base_ufunc->uf_partial != NULL)
4177 {
4178 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
4179 if (ectx.ec_outer == NULL)
4180 goto failed_early;
4181 if (partial != NULL)
4182 {
4183 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4184 {
4185 if (current_ectx->ec_outer != NULL)
4186 *ectx.ec_outer = *current_ectx->ec_outer;
4187 }
4188 else
4189 *ectx.ec_outer = partial->pt_outer;
4190 }
4191 else
4192 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
4193 ectx.ec_outer->out_up_is_copy = TRUE;
4194 }
4195 }
4196
4197 // dummy frame entries
4198 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4199 {
4200 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4201 ++ectx.ec_stack.ga_len;
4202 }
4203
4204 {
4205 // Reserve space for local variables and any closure reference count.
4206 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4207 + ufunc->uf_dfunc_idx;
4208
4209 for (idx = 0; idx < dfunc->df_varcount; ++idx)
4210 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
4211 ectx.ec_stack.ga_len += dfunc->df_varcount;
4212 if (dfunc->df_has_closure)
4213 {
4214 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
4215 STACK_TV_VAR(idx)->vval.v_number = 0;
4216 ++ectx.ec_stack.ga_len;
4217 }
4218
4219 ectx.ec_instr = INSTRUCTIONS(dfunc);
4220 }
4221
4222 // Following errors are in the function, not the caller.
4223 // Commands behave like vim9script.
4224 estack_push_ufunc(ufunc, 1);
4225 current_sctx = ufunc->uf_script_ctx;
4226 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4227
4228 // Use a specific location for storing error messages to be converted to an
4229 // exception.
4230 saved_msg_list = msg_list;
4231 msg_list = &private_msg_list;
4232
4233 // Do turn errors into exceptions.
4234 suppress_errthrow = FALSE;
4235
4236 // Do not delete the function while executing it.
4237 ++ufunc->uf_calls;
4238
4239 // When ":silent!" was used before calling then we still abort the
4240 // function. If ":silent!" is used in the function then we don't.
4241 emsg_silent_def = emsg_silent;
4242 did_emsg_def = 0;
4243
4244 ectx.ec_where.wt_index = 0;
4245 ectx.ec_where.wt_variable = FALSE;
4246
4247 // Execute the instructions until done.
4248 ret = exec_instructions(&ectx);
4249 if (ret == OK)
4250 {
4251 // function finished, get result from the stack.
4252 if (ufunc->uf_ret_type == &t_void)
4253 {
4254 rettv->v_type = VAR_VOID;
4255 }
4256 else
4257 {
4258 tv = STACK_TV_BOT(-1);
4259 *rettv = *tv;
4260 tv->v_type = VAR_UNKNOWN;
4261 }
4262 }
4263
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01004264 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004265 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
4266 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004267
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004268 // Deal with any remaining closures, they may be in use somewhere.
4269 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01004270 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004271 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01004272 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
4273 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02004274
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004275 estack_pop();
4276 current_sctx = save_current_sctx;
4277
Bram Moolenaarc970e422021-03-17 15:03:04 +01004278 // TODO: when is it safe to delete the function if it is no longer used?
4279 --ufunc->uf_calls;
4280
Bram Moolenaar352134b2020-10-17 22:04:08 +02004281 if (*msg_list != NULL && saved_msg_list != NULL)
4282 {
4283 msglist_T **plist = saved_msg_list;
4284
4285 // Append entries from the current msg_list (uncaught exceptions) to
4286 // the saved msg_list.
4287 while (*plist != NULL)
4288 plist = &(*plist)->next;
4289
4290 *plist = *msg_list;
4291 }
4292 msg_list = saved_msg_list;
4293
Bram Moolenaar4c137212021-04-19 16:48:48 +02004294 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02004295 {
4296 cmdmod.cmod_filter_regmatch.regprog = NULL;
4297 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004298 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004299 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004300 emsg_silent_def = save_emsg_silent_def;
4301 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004302
Bram Moolenaaree8580e2020-08-28 17:19:07 +02004303failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004304 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
4306 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01004309 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004310
Bram Moolenaar0186e582021-01-10 18:33:11 +01004311 while (ectx.ec_outer != NULL)
4312 {
4313 outer_T *up = ectx.ec_outer->out_up_is_copy
4314 ? NULL : ectx.ec_outer->out_up;
4315
4316 vim_free(ectx.ec_outer);
4317 ectx.ec_outer = up;
4318 }
4319
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02004320 // Not sure if this is necessary.
4321 suppress_errthrow = save_suppress_errthrow;
4322
Bram Moolenaareeece9e2020-11-20 19:26:48 +01004323 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004324 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004325 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01004326 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 return ret;
4328}
4329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004331 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
4332 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
4333 * "pfx" is prefixed to every line.
4334 */
4335 static void
4336list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
4337{
4338 int line_idx = 0;
4339 int prev_current = 0;
4340 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004341 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004342
4343 for (current = 0; current < instr_count; ++current)
4344 {
4345 isn_T *iptr = &instr[current];
4346 char *line;
4347
4348 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004349 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004350 while (line_idx < iptr->isn_lnum
4351 && line_idx < ufunc->uf_lines.ga_len)
4352 {
4353 if (current > prev_current)
4354 {
4355 msg_puts("\n\n");
4356 prev_current = current;
4357 }
4358 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4359 if (line != NULL)
4360 msg(line);
4361 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02004362 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
4363 {
4364 int first_def_arg = ufunc->uf_args.ga_len
4365 - ufunc->uf_def_args.ga_len;
4366
4367 if (def_arg_idx > 0)
4368 msg_puts("\n\n");
4369 msg_start();
4370 msg_puts(" ");
4371 msg_puts(((char **)(ufunc->uf_args.ga_data))[
4372 first_def_arg + def_arg_idx]);
4373 msg_puts(" = ");
4374 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
4375 msg_clr_eos();
4376 msg_end();
4377 }
4378 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004379
4380 switch (iptr->isn_type)
4381 {
4382 case ISN_EXEC:
4383 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
4384 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004385 case ISN_REDIRSTART:
4386 smsg("%s%4d REDIR", pfx, current);
4387 break;
4388 case ISN_REDIREND:
4389 smsg("%s%4d REDIR END%s", pfx, current,
4390 iptr->isn_arg.number ? " append" : "");
4391 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004392 case ISN_SUBSTITUTE:
4393 {
4394 subs_T *subs = &iptr->isn_arg.subs;
4395
4396 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
4397 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
4398 msg(" -------------");
4399 }
4400 break;
4401 case ISN_EXECCONCAT:
4402 smsg("%s%4d EXECCONCAT %lld", pfx, current,
4403 (varnumber_T)iptr->isn_arg.number);
4404 break;
4405 case ISN_ECHO:
4406 {
4407 echo_T *echo = &iptr->isn_arg.echo;
4408
4409 smsg("%s%4d %s %d", pfx, current,
4410 echo->echo_with_white ? "ECHO" : "ECHON",
4411 echo->echo_count);
4412 }
4413 break;
4414 case ISN_EXECUTE:
4415 smsg("%s%4d EXECUTE %lld", pfx, current,
4416 (varnumber_T)(iptr->isn_arg.number));
4417 break;
4418 case ISN_ECHOMSG:
4419 smsg("%s%4d ECHOMSG %lld", pfx, current,
4420 (varnumber_T)(iptr->isn_arg.number));
4421 break;
4422 case ISN_ECHOERR:
4423 smsg("%s%4d ECHOERR %lld", pfx, current,
4424 (varnumber_T)(iptr->isn_arg.number));
4425 break;
4426 case ISN_LOAD:
4427 {
4428 if (iptr->isn_arg.number < 0)
4429 smsg("%s%4d LOAD arg[%lld]", pfx, current,
4430 (varnumber_T)(iptr->isn_arg.number
4431 + STACK_FRAME_SIZE));
4432 else
4433 smsg("%s%4d LOAD $%lld", pfx, current,
4434 (varnumber_T)(iptr->isn_arg.number));
4435 }
4436 break;
4437 case ISN_LOADOUTER:
4438 {
4439 if (iptr->isn_arg.number < 0)
4440 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
4441 iptr->isn_arg.outer.outer_depth,
4442 iptr->isn_arg.outer.outer_idx
4443 + STACK_FRAME_SIZE);
4444 else
4445 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
4446 iptr->isn_arg.outer.outer_depth,
4447 iptr->isn_arg.outer.outer_idx);
4448 }
4449 break;
4450 case ISN_LOADV:
4451 smsg("%s%4d LOADV v:%s", pfx, current,
4452 get_vim_var_name(iptr->isn_arg.number));
4453 break;
4454 case ISN_LOADSCRIPT:
4455 {
4456 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4457 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4458 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4459 + sref->sref_idx;
4460
4461 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
4462 sv->sv_name,
4463 sref->sref_idx,
4464 si->sn_name);
4465 }
4466 break;
4467 case ISN_LOADS:
4468 {
4469 scriptitem_T *si = SCRIPT_ITEM(
4470 iptr->isn_arg.loadstore.ls_sid);
4471
4472 smsg("%s%4d LOADS s:%s from %s", pfx, current,
4473 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4474 }
4475 break;
4476 case ISN_LOADAUTO:
4477 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
4478 break;
4479 case ISN_LOADG:
4480 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
4481 break;
4482 case ISN_LOADB:
4483 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
4484 break;
4485 case ISN_LOADW:
4486 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
4487 break;
4488 case ISN_LOADT:
4489 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
4490 break;
4491 case ISN_LOADGDICT:
4492 smsg("%s%4d LOAD g:", pfx, current);
4493 break;
4494 case ISN_LOADBDICT:
4495 smsg("%s%4d LOAD b:", pfx, current);
4496 break;
4497 case ISN_LOADWDICT:
4498 smsg("%s%4d LOAD w:", pfx, current);
4499 break;
4500 case ISN_LOADTDICT:
4501 smsg("%s%4d LOAD t:", pfx, current);
4502 break;
4503 case ISN_LOADOPT:
4504 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
4505 break;
4506 case ISN_LOADENV:
4507 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
4508 break;
4509 case ISN_LOADREG:
4510 smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number));
4511 break;
4512
4513 case ISN_STORE:
4514 if (iptr->isn_arg.number < 0)
4515 smsg("%s%4d STORE arg[%lld]", pfx, current,
4516 iptr->isn_arg.number + STACK_FRAME_SIZE);
4517 else
4518 smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number);
4519 break;
4520 case ISN_STOREOUTER:
4521 {
4522 if (iptr->isn_arg.number < 0)
4523 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
4524 iptr->isn_arg.outer.outer_depth,
4525 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
4526 else
4527 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
4528 iptr->isn_arg.outer.outer_depth,
4529 iptr->isn_arg.outer.outer_idx);
4530 }
4531 break;
4532 case ISN_STOREV:
4533 smsg("%s%4d STOREV v:%s", pfx, current,
4534 get_vim_var_name(iptr->isn_arg.number));
4535 break;
4536 case ISN_STOREAUTO:
4537 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
4538 break;
4539 case ISN_STOREG:
4540 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
4541 break;
4542 case ISN_STOREB:
4543 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
4544 break;
4545 case ISN_STOREW:
4546 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
4547 break;
4548 case ISN_STORET:
4549 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
4550 break;
4551 case ISN_STORES:
4552 {
4553 scriptitem_T *si = SCRIPT_ITEM(
4554 iptr->isn_arg.loadstore.ls_sid);
4555
4556 smsg("%s%4d STORES %s in %s", pfx, current,
4557 iptr->isn_arg.loadstore.ls_name, si->sn_name);
4558 }
4559 break;
4560 case ISN_STORESCRIPT:
4561 {
4562 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4563 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
4564 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
4565 + sref->sref_idx;
4566
4567 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
4568 sv->sv_name,
4569 sref->sref_idx,
4570 si->sn_name);
4571 }
4572 break;
4573 case ISN_STOREOPT:
4574 smsg("%s%4d STOREOPT &%s", pfx, current,
4575 iptr->isn_arg.storeopt.so_name);
4576 break;
4577 case ISN_STOREENV:
4578 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
4579 break;
4580 case ISN_STOREREG:
4581 smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number);
4582 break;
4583 case ISN_STORENR:
4584 smsg("%s%4d STORE %lld in $%d", pfx, current,
4585 iptr->isn_arg.storenr.stnr_val,
4586 iptr->isn_arg.storenr.stnr_idx);
4587 break;
4588
4589 case ISN_STOREINDEX:
4590 smsg("%s%4d STOREINDEX %s", pfx, current,
4591 vartype_name(iptr->isn_arg.vartype));
4592 break;
4593
4594 case ISN_STORERANGE:
4595 smsg("%s%4d STORERANGE", pfx, current);
4596 break;
4597
4598 // constants
4599 case ISN_PUSHNR:
4600 smsg("%s%4d PUSHNR %lld", pfx, current,
4601 (varnumber_T)(iptr->isn_arg.number));
4602 break;
4603 case ISN_PUSHBOOL:
4604 case ISN_PUSHSPEC:
4605 smsg("%s%4d PUSH %s", pfx, current,
4606 get_var_special_name(iptr->isn_arg.number));
4607 break;
4608 case ISN_PUSHF:
4609#ifdef FEAT_FLOAT
4610 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
4611#endif
4612 break;
4613 case ISN_PUSHS:
4614 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
4615 break;
4616 case ISN_PUSHBLOB:
4617 {
4618 char_u *r;
4619 char_u numbuf[NUMBUFLEN];
4620 char_u *tofree;
4621
4622 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
4623 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
4624 vim_free(tofree);
4625 }
4626 break;
4627 case ISN_PUSHFUNC:
4628 {
4629 char *name = (char *)iptr->isn_arg.string;
4630
4631 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
4632 name == NULL ? "[none]" : name);
4633 }
4634 break;
4635 case ISN_PUSHCHANNEL:
4636#ifdef FEAT_JOB_CHANNEL
4637 {
4638 channel_T *channel = iptr->isn_arg.channel;
4639
4640 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
4641 channel == NULL ? 0 : channel->ch_id);
4642 }
4643#endif
4644 break;
4645 case ISN_PUSHJOB:
4646#ifdef FEAT_JOB_CHANNEL
4647 {
4648 typval_T tv;
4649 char_u *name;
4650
4651 tv.v_type = VAR_JOB;
4652 tv.vval.v_job = iptr->isn_arg.job;
4653 name = tv_get_string(&tv);
4654 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
4655 }
4656#endif
4657 break;
4658 case ISN_PUSHEXC:
4659 smsg("%s%4d PUSH v:exception", pfx, current);
4660 break;
4661 case ISN_UNLET:
4662 smsg("%s%4d UNLET%s %s", pfx, current,
4663 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4664 iptr->isn_arg.unlet.ul_name);
4665 break;
4666 case ISN_UNLETENV:
4667 smsg("%s%4d UNLETENV%s $%s", pfx, current,
4668 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4669 iptr->isn_arg.unlet.ul_name);
4670 break;
4671 case ISN_UNLETINDEX:
4672 smsg("%s%4d UNLETINDEX", pfx, current);
4673 break;
4674 case ISN_UNLETRANGE:
4675 smsg("%s%4d UNLETRANGE", pfx, current);
4676 break;
4677 case ISN_LOCKCONST:
4678 smsg("%s%4d LOCKCONST", pfx, current);
4679 break;
4680 case ISN_NEWLIST:
4681 smsg("%s%4d NEWLIST size %lld", pfx, current,
4682 (varnumber_T)(iptr->isn_arg.number));
4683 break;
4684 case ISN_NEWDICT:
4685 smsg("%s%4d NEWDICT size %lld", pfx, current,
4686 (varnumber_T)(iptr->isn_arg.number));
4687 break;
4688
4689 // function call
4690 case ISN_BCALL:
4691 {
4692 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4693
4694 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
4695 internal_func_name(cbfunc->cbf_idx),
4696 cbfunc->cbf_argcount);
4697 }
4698 break;
4699 case ISN_DCALL:
4700 {
4701 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4702 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4703 + cdfunc->cdf_idx;
4704
4705 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
4706 df->df_ufunc->uf_name_exp != NULL
4707 ? df->df_ufunc->uf_name_exp
4708 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4709 }
4710 break;
4711 case ISN_UCALL:
4712 {
4713 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4714
4715 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
4716 cufunc->cuf_name, cufunc->cuf_argcount);
4717 }
4718 break;
4719 case ISN_PCALL:
4720 {
4721 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4722
4723 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
4724 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4725 }
4726 break;
4727 case ISN_PCALL_END:
4728 smsg("%s%4d PCALL end", pfx, current);
4729 break;
4730 case ISN_RETURN:
4731 smsg("%s%4d RETURN", pfx, current);
4732 break;
4733 case ISN_RETURN_ZERO:
4734 smsg("%s%4d RETURN 0", pfx, current);
4735 break;
4736 case ISN_FUNCREF:
4737 {
4738 funcref_T *funcref = &iptr->isn_arg.funcref;
4739 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4740 + funcref->fr_func;
4741
4742 smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name);
4743 }
4744 break;
4745
4746 case ISN_NEWFUNC:
4747 {
4748 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4749
4750 smsg("%s%4d NEWFUNC %s %s", pfx, current,
4751 newfunc->nf_lambda, newfunc->nf_global);
4752 }
4753 break;
4754
4755 case ISN_DEF:
4756 {
4757 char_u *name = iptr->isn_arg.string;
4758
4759 smsg("%s%4d DEF %s", pfx, current,
4760 name == NULL ? (char_u *)"" : name);
4761 }
4762 break;
4763
4764 case ISN_JUMP:
4765 {
4766 char *when = "?";
4767
4768 switch (iptr->isn_arg.jump.jump_when)
4769 {
4770 case JUMP_ALWAYS:
4771 when = "JUMP";
4772 break;
4773 case JUMP_AND_KEEP_IF_TRUE:
4774 when = "JUMP_AND_KEEP_IF_TRUE";
4775 break;
4776 case JUMP_IF_FALSE:
4777 when = "JUMP_IF_FALSE";
4778 break;
4779 case JUMP_AND_KEEP_IF_FALSE:
4780 when = "JUMP_AND_KEEP_IF_FALSE";
4781 break;
4782 case JUMP_IF_COND_FALSE:
4783 when = "JUMP_IF_COND_FALSE";
4784 break;
4785 case JUMP_IF_COND_TRUE:
4786 when = "JUMP_IF_COND_TRUE";
4787 break;
4788 }
4789 smsg("%s%4d %s -> %d", pfx, current, when,
4790 iptr->isn_arg.jump.jump_where);
4791 }
4792 break;
4793
4794 case ISN_JUMP_IF_ARG_SET:
4795 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
4796 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
4797 iptr->isn_arg.jump.jump_where);
4798 break;
4799
4800 case ISN_FOR:
4801 {
4802 forloop_T *forloop = &iptr->isn_arg.forloop;
4803
4804 smsg("%s%4d FOR $%d -> %d", pfx, current,
4805 forloop->for_idx, forloop->for_end);
4806 }
4807 break;
4808
4809 case ISN_TRY:
4810 {
4811 try_T *try = &iptr->isn_arg.try;
4812
4813 if (try->try_ref->try_finally == 0)
4814 smsg("%s%4d TRY catch -> %d, endtry -> %d",
4815 pfx, current,
4816 try->try_ref->try_catch,
4817 try->try_ref->try_endtry);
4818 else
4819 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4820 pfx, current,
4821 try->try_ref->try_catch,
4822 try->try_ref->try_finally,
4823 try->try_ref->try_endtry);
4824 }
4825 break;
4826 case ISN_CATCH:
4827 // TODO
4828 smsg("%s%4d CATCH", pfx, current);
4829 break;
4830 case ISN_TRYCONT:
4831 {
4832 trycont_T *trycont = &iptr->isn_arg.trycont;
4833
4834 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
4835 trycont->tct_levels,
4836 trycont->tct_levels == 1 ? "" : "s",
4837 trycont->tct_where);
4838 }
4839 break;
4840 case ISN_FINALLY:
4841 smsg("%s%4d FINALLY", pfx, current);
4842 break;
4843 case ISN_ENDTRY:
4844 smsg("%s%4d ENDTRY", pfx, current);
4845 break;
4846 case ISN_THROW:
4847 smsg("%s%4d THROW", pfx, current);
4848 break;
4849
4850 // expression operations on number
4851 case ISN_OPNR:
4852 case ISN_OPFLOAT:
4853 case ISN_OPANY:
4854 {
4855 char *what;
4856 char *ins;
4857
4858 switch (iptr->isn_arg.op.op_type)
4859 {
4860 case EXPR_MULT: what = "*"; break;
4861 case EXPR_DIV: what = "/"; break;
4862 case EXPR_REM: what = "%"; break;
4863 case EXPR_SUB: what = "-"; break;
4864 case EXPR_ADD: what = "+"; break;
4865 default: what = "???"; break;
4866 }
4867 switch (iptr->isn_type)
4868 {
4869 case ISN_OPNR: ins = "OPNR"; break;
4870 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4871 case ISN_OPANY: ins = "OPANY"; break;
4872 default: ins = "???"; break;
4873 }
4874 smsg("%s%4d %s %s", pfx, current, ins, what);
4875 }
4876 break;
4877
4878 case ISN_COMPAREBOOL:
4879 case ISN_COMPARESPECIAL:
4880 case ISN_COMPARENR:
4881 case ISN_COMPAREFLOAT:
4882 case ISN_COMPARESTRING:
4883 case ISN_COMPAREBLOB:
4884 case ISN_COMPARELIST:
4885 case ISN_COMPAREDICT:
4886 case ISN_COMPAREFUNC:
4887 case ISN_COMPAREANY:
4888 {
4889 char *p;
4890 char buf[10];
4891 char *type;
4892
4893 switch (iptr->isn_arg.op.op_type)
4894 {
4895 case EXPR_EQUAL: p = "=="; break;
4896 case EXPR_NEQUAL: p = "!="; break;
4897 case EXPR_GREATER: p = ">"; break;
4898 case EXPR_GEQUAL: p = ">="; break;
4899 case EXPR_SMALLER: p = "<"; break;
4900 case EXPR_SEQUAL: p = "<="; break;
4901 case EXPR_MATCH: p = "=~"; break;
4902 case EXPR_IS: p = "is"; break;
4903 case EXPR_ISNOT: p = "isnot"; break;
4904 case EXPR_NOMATCH: p = "!~"; break;
4905 default: p = "???"; break;
4906 }
4907 STRCPY(buf, p);
4908 if (iptr->isn_arg.op.op_ic == TRUE)
4909 strcat(buf, "?");
4910 switch(iptr->isn_type)
4911 {
4912 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4913 case ISN_COMPARESPECIAL:
4914 type = "COMPARESPECIAL"; break;
4915 case ISN_COMPARENR: type = "COMPARENR"; break;
4916 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4917 case ISN_COMPARESTRING:
4918 type = "COMPARESTRING"; break;
4919 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4920 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4921 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4922 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
4923 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4924 default: type = "???"; break;
4925 }
4926
4927 smsg("%s%4d %s %s", pfx, current, type, buf);
4928 }
4929 break;
4930
4931 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
4932 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
4933
4934 // expression operations
4935 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
4936 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
4937 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
4938 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
4939 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
4940 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
4941 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
4942 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
4943 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
4944 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
4945 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
4946 case ISN_SLICE: smsg("%s%4d SLICE %lld",
4947 pfx, current, iptr->isn_arg.number); break;
4948 case ISN_GETITEM: smsg("%s%4d ITEM %lld",
4949 pfx, current, iptr->isn_arg.number); break;
4950 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
4951 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
4952 iptr->isn_arg.string); break;
4953 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
4954
4955 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
4956 case ISN_CHECKTYPE:
4957 {
4958 checktype_T *ct = &iptr->isn_arg.type;
4959 char *tofree;
4960
4961 if (ct->ct_arg_idx == 0)
4962 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
4963 type_name(ct->ct_type, &tofree),
4964 (int)ct->ct_off);
4965 else
4966 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current,
4967 type_name(ct->ct_type, &tofree),
4968 (int)ct->ct_off,
4969 (int)ct->ct_arg_idx);
4970 vim_free(tofree);
4971 break;
4972 }
4973 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
4974 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4975 iptr->isn_arg.checklen.cl_min_len);
4976 break;
4977 case ISN_SETTYPE:
4978 {
4979 char *tofree;
4980
4981 smsg("%s%4d SETTYPE %s", pfx, current,
4982 type_name(iptr->isn_arg.type.ct_type, &tofree));
4983 vim_free(tofree);
4984 break;
4985 }
4986 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
4987 case ISN_2BOOL: if (iptr->isn_arg.number)
4988 smsg("%s%4d INVERT (!val)", pfx, current);
4989 else
4990 smsg("%s%4d 2BOOL (!!val)", pfx, current);
4991 break;
4992 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
4993 (varnumber_T)(iptr->isn_arg.number));
4994 break;
4995 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]", pfx, current,
4996 (varnumber_T)(iptr->isn_arg.number));
4997 break;
4998 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current, iptr->isn_arg.string);
4999 break;
5000 case ISN_PUT:
5001 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5002 smsg("%s%4d PUT %c above range",
5003 pfx, current, iptr->isn_arg.put.put_regname);
5004 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5005 smsg("%s%4d PUT %c range",
5006 pfx, current, iptr->isn_arg.put.put_regname);
5007 else
5008 smsg("%s%4d PUT %c %ld", pfx, current,
5009 iptr->isn_arg.put.put_regname,
5010 (long)iptr->isn_arg.put.put_lnum);
5011 break;
5012
5013 // TODO: summarize modifiers
5014 case ISN_CMDMOD:
5015 {
5016 char_u *buf;
5017 size_t len = produce_cmdmods(
5018 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5019
5020 buf = alloc(len + 1);
5021 if (buf != NULL)
5022 {
5023 (void)produce_cmdmods(
5024 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5025 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5026 vim_free(buf);
5027 }
5028 break;
5029 }
5030 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5031
5032 case ISN_PROF_START:
5033 smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum);
5034 break;
5035
5036 case ISN_PROF_END:
5037 smsg("%s%4d PROFILE END", pfx, current);
5038 break;
5039
5040 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5041 iptr->isn_arg.unpack.unp_count,
5042 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5043 break;
5044 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5045 iptr->isn_arg.shuffle.shfl_item,
5046 iptr->isn_arg.shuffle.shfl_up);
5047 break;
5048 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5049
5050 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5051 return;
5052 }
5053
5054 out_flush(); // output one line at a time
5055 ui_breakcheck();
5056 if (got_int)
5057 break;
5058 }
5059}
5060
5061/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005062 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005063 * We don't really need this at runtime, but we do have tests that require it,
5064 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 */
5066 void
5067ex_disassemble(exarg_T *eap)
5068{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005069 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005070 char_u *fname;
5071 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 dfunc_T *dfunc;
5073 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005074 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005075 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005077 if (STRNCMP(arg, "<lambda>", 8) == 0)
5078 {
5079 arg += 8;
5080 (void)getdigits(&arg);
5081 fname = vim_strnsave(eap->arg, arg - eap->arg);
5082 }
5083 else
5084 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005085 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005086 if (fname == NULL)
5087 {
5088 semsg(_(e_invarg2), eap->arg);
5089 return;
5090 }
5091
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005092 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005093 if (ufunc == NULL)
5094 {
5095 char_u *p = untrans_function_name(fname);
5096
5097 if (p != NULL)
5098 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005099 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005100 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005101 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 if (ufunc == NULL)
5103 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005104 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 return;
5106 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005107 if (func_needs_compiling(ufunc, eap->forceit)
5108 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005109 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005110 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005112 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 return;
5114 }
5115 if (ufunc->uf_name_exp != NULL)
5116 msg((char *)ufunc->uf_name_exp);
5117 else
5118 msg((char *)ufunc->uf_name);
5119
5120 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005121#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01005122 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
5123 instr_count = eap->forceit ? dfunc->df_instr_prof_count
5124 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01005125#else
5126 instr = dfunc->df_instr;
5127 instr_count = dfunc->df_instr_count;
5128#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129
Bram Moolenaar4c137212021-04-19 16:48:48 +02005130 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131}
5132
5133/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005134 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135 * list, etc. Mostly like what JavaScript does, except that empty list and
5136 * empty dictionary are FALSE.
5137 */
5138 int
5139tv2bool(typval_T *tv)
5140{
5141 switch (tv->v_type)
5142 {
5143 case VAR_NUMBER:
5144 return tv->vval.v_number != 0;
5145 case VAR_FLOAT:
5146#ifdef FEAT_FLOAT
5147 return tv->vval.v_float != 0.0;
5148#else
5149 break;
5150#endif
5151 case VAR_PARTIAL:
5152 return tv->vval.v_partial != NULL;
5153 case VAR_FUNC:
5154 case VAR_STRING:
5155 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
5156 case VAR_LIST:
5157 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
5158 case VAR_DICT:
5159 return tv->vval.v_dict != NULL
5160 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
5161 case VAR_BOOL:
5162 case VAR_SPECIAL:
5163 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
5164 case VAR_JOB:
5165#ifdef FEAT_JOB_CHANNEL
5166 return tv->vval.v_job != NULL;
5167#else
5168 break;
5169#endif
5170 case VAR_CHANNEL:
5171#ifdef FEAT_JOB_CHANNEL
5172 return tv->vval.v_channel != NULL;
5173#else
5174 break;
5175#endif
5176 case VAR_BLOB:
5177 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
5178 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005179 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 case VAR_VOID:
5181 break;
5182 }
5183 return FALSE;
5184}
5185
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005186 void
5187emsg_using_string_as(typval_T *tv, int as_number)
5188{
5189 semsg(_(as_number ? e_using_string_as_number_str
5190 : e_using_string_as_bool_str),
5191 tv->vval.v_string == NULL
5192 ? (char_u *)"" : tv->vval.v_string);
5193}
5194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195/*
5196 * If "tv" is a string give an error and return FAIL.
5197 */
5198 int
5199check_not_string(typval_T *tv)
5200{
5201 if (tv->v_type == VAR_STRING)
5202 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01005203 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 clear_tv(tv);
5205 return FAIL;
5206 }
5207 return OK;
5208}
5209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210
5211#endif // FEAT_EVAL