blob: 52c4193c6a55e9944e0c36411a395286599d2912 [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
37
38// A stack is used to store:
39// - arguments passed to a :def function
40// - info about the calling function, to use when returning
41// - local variables
42// - temporary values
43//
44// In detail (FP == Frame Pointer):
45// arg1 first argument from caller (if present)
46// arg2 second argument from caller (if present)
47// extra_arg1 any missing optional argument default value
48// FP -> cur_func calling function
49// current previous instruction pointer
50// frame_ptr previous Frame Pointer
51// var1 space for local variable
52// var2 space for local variable
53// .... fixed space for max. number of local variables
54// temp temporary values
55// .... flexible space for temporary values (can grow big)
56
57/*
58 * Execution context.
59 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010060struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020062 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063
Bram Moolenaar0186e582021-01-10 18:33:11 +010064 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 garray_T ec_trystack; // stack of trycmd_T values
67 int ec_in_catch; // when TRUE in catch or finally block
68
69 int ec_dfunc_idx; // current function index
70 isn_T *ec_instr; // array with instructions
71 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020072
73 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010074};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
Bram Moolenaar12d26532021-02-19 19:13:21 +010076#ifdef FEAT_PROFILE
77// stack of profinfo_T used when profiling.
78static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
79#endif
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020082#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 +010083
Bram Moolenaar418f1df2020-08-12 21:34:49 +020084 void
85to_string_error(vartype_T vartype)
86{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020087 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020088}
89
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 */
93 static int
94ufunc_argcount(ufunc_T *ufunc)
95{
96 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
97}
98
99/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100100 * Set the instruction index, depending on omitted arguments, where the default
101 * values are to be computed. If all optional arguments are present, start
102 * with the function body.
103 * The expression evaluation is at the start of the instructions:
104 * 0 -> EVAL default1
105 * STORE arg[-2]
106 * 1 -> EVAL default2
107 * STORE arg[-1]
108 * 2 -> function body
109 */
110 static void
111init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
112{
113 if (ufunc->uf_def_args.ga_len == 0)
114 ectx->ec_iidx = 0;
115 else
116 {
117 int defcount = ufunc->uf_args.ga_len - argcount;
118
119 // If there is a varargs argument defcount can be negative, no defaults
120 // to evaluate then.
121 if (defcount < 0)
122 defcount = 0;
123 ectx->ec_iidx = ufunc->uf_def_arg_idx[
124 ufunc->uf_def_args.ga_len - defcount];
125 }
126}
127
128/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200129 * Create a new list from "count" items at the bottom of the stack.
130 * When "count" is zero an empty list is added to the stack.
131 */
132 static int
133exe_newlist(int count, ectx_T *ectx)
134{
135 list_T *list = list_alloc_with_items(count);
136 int idx;
137 typval_T *tv;
138
139 if (list == NULL)
140 return FAIL;
141 for (idx = 0; idx < count; ++idx)
142 list_set_item(list, idx, STACK_TV_BOT(idx - count));
143
144 if (count > 0)
145 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200146 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200147 return FAIL;
148 else
149 ++ectx->ec_stack.ga_len;
150 tv = STACK_TV_BOT(-1);
151 tv->v_type = VAR_LIST;
152 tv->vval.v_list = list;
153 ++list->lv_refcount;
154 return OK;
155}
156
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100157// Data local to a function.
158// On a function call, if not empty, is saved on the stack and restored when
159// returning.
160typedef struct {
161 int floc_restore_cmdmod;
162 cmdmod_T floc_save_cmdmod;
163 int floc_restore_cmdmod_stacklen;
164} funclocal_T;
165
Bram Moolenaarfe270812020-04-11 22:31:27 +0200166/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100168 * This adds a stack frame and sets the instruction pointer to the start of the
169 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100170 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 *
172 * Stack has:
173 * - current arguments (already there)
174 * - omitted optional argument (default values) added here
175 * - stack frame:
176 * - pointer to calling function
177 * - Index of next instruction in calling function
178 * - previous frame pointer
179 * - reserved space for local variables
180 */
181 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100182call_dfunc(
183 int cdf_idx,
184 partial_T *pt,
185 int argcount_arg,
186 funclocal_T *funclocal,
187 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100189 int argcount = argcount_arg;
190 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
191 ufunc_T *ufunc = dfunc->df_ufunc;
192 int arg_to_add;
193 int vararg_count = 0;
194 int varcount;
195 int idx;
196 estack_T *entry;
197 funclocal_T *floc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198
199 if (dfunc->df_deleted)
200 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100201 // don't use ufunc->uf_name, it may have been freed
202 emsg_funcname(e_func_deleted,
203 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 return FAIL;
205 }
206
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100207#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100208 if (do_profiling == PROF_YES)
209 {
210 if (ga_grow(&profile_info_ga, 1) == OK)
211 {
212 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
213 + profile_info_ga.ga_len;
214 ++profile_info_ga.ga_len;
215 CLEAR_POINTER(info);
216 profile_may_start_func(info, ufunc,
217 (((dfunc_T *)def_functions.ga_data)
218 + ectx->ec_dfunc_idx)->df_ufunc);
219 }
220
221 // Profiling might be enabled/disabled along the way. This should not
222 // fail, since the function was compiled before and toggling profiling
223 // doesn't change any errors.
224 if (func_needs_compiling(ufunc, PROFILING(ufunc))
225 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100226 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100227 return FAIL;
228 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100229#endif
230
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200231 if (ufunc->uf_va_name != NULL)
232 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200233 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200234 // Stack at time of call with 2 varargs:
235 // normal_arg
236 // optional_arg
237 // vararg_1
238 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200239 // After creating the list:
240 // normal_arg
241 // optional_arg
242 // vararg-list
243 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200244 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200245 // After creating the list
246 // normal_arg
247 // (space for optional_arg)
248 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200249 vararg_count = argcount - ufunc->uf_args.ga_len;
250 if (vararg_count < 0)
251 vararg_count = 0;
252 else
253 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200254 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200255 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200256
257 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200258 }
259
Bram Moolenaarfe270812020-04-11 22:31:27 +0200260 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200261 if (arg_to_add < 0)
262 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200263 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200264 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200265 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200266 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200267 return FAIL;
268 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200269
270 // Reserve space for:
271 // - missing arguments
272 // - stack frame
273 // - local variables
274 // - if needed: a counter for number of closures created in
275 // ectx->ec_funcrefs.
276 varcount = dfunc->df_varcount + dfunc->df_has_closure;
277 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
278 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 return FAIL;
280
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100281 // If depth of calling is getting too high, don't execute the function.
282 if (funcdepth_increment() == FAIL)
283 return FAIL;
284
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100285 // Only make a copy of funclocal if it contains something to restore.
286 if (funclocal->floc_restore_cmdmod)
287 {
288 floc = ALLOC_ONE(funclocal_T);
289 if (floc == NULL)
290 return FAIL;
291 *floc = *funclocal;
292 funclocal->floc_restore_cmdmod = FALSE;
293 }
294
Bram Moolenaarfe270812020-04-11 22:31:27 +0200295 // Move the vararg-list to below the missing optional arguments.
296 if (vararg_count > 0 && arg_to_add > 0)
297 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100298
299 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200300 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200301 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200302 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303
304 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100305 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
306 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100307 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100308 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100309 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200310 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311
312 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200313 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200315 if (dfunc->df_has_closure)
316 {
317 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
318
319 tv->v_type = VAR_NUMBER;
320 tv->vval.v_number = 0;
321 }
322 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100324 if (pt != NULL || ufunc->uf_partial != NULL
325 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100326 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100327 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
328
329 if (outer == NULL)
330 return FAIL;
331 if (pt != NULL)
332 {
333 *outer = pt->pt_outer;
334 outer->out_up_is_copy = TRUE;
335 }
336 else if (ufunc->uf_partial != NULL)
337 {
338 *outer = ufunc->uf_partial->pt_outer;
339 outer->out_up_is_copy = TRUE;
340 }
341 else
342 {
343 outer->out_stack = &ectx->ec_stack;
344 outer->out_frame_idx = ectx->ec_frame_idx;
345 outer->out_up = ectx->ec_outer;
346 }
347 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100348 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100349 else
350 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100351
Bram Moolenaarc970e422021-03-17 15:03:04 +0100352 ++ufunc->uf_calls;
353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354 // Set execution state to the start of the called function.
355 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100356 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100357 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200358 if (entry != NULL)
359 {
360 // Set the script context to the script where the function was defined.
361 // TODO: save more than the SID?
362 entry->es_save_sid = current_sctx.sc_sid;
363 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
364 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100365
366 // Decide where to start execution, handles optional arguments.
367 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368
369 return OK;
370}
371
372// Get pointer to item in the stack.
373#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
374
375/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200376 * Used when returning from a function: Check if any closure is still
377 * referenced. If so then move the arguments and variables to a separate piece
378 * of stack to be used when the closure is called.
379 * When "free_arguments" is TRUE the arguments are to be freed.
380 * Returns FAIL when out of memory.
381 */
382 static int
383handle_closure_in_use(ectx_T *ectx, int free_arguments)
384{
385 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
386 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200387 int argcount;
388 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200389 int idx;
390 typval_T *tv;
391 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200392 garray_T *gap = &ectx->ec_funcrefs;
393 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200394
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200395 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200396 return OK; // function was freed
397 if (dfunc->df_has_closure == 0)
398 return OK; // no closures
399 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
400 closure_count = tv->vval.v_number;
401 if (closure_count == 0)
402 return OK; // no funcrefs created
403
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200404 argcount = ufunc_argcount(dfunc->df_ufunc);
405 top = ectx->ec_frame_idx - argcount;
406
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200408 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200409 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200410 partial_T *pt;
411 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200412
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200413 if (off < 0)
414 continue; // count is off or already done
415 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200416 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200417 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200418 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200419 int i;
420
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200421 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200422 // unreferenced on return.
423 for (i = 0; i < dfunc->df_varcount; ++i)
424 {
425 typval_T *stv = STACK_TV(ectx->ec_frame_idx
426 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200427 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200428 --refcount;
429 }
430 if (refcount > 1)
431 {
432 closure_in_use = TRUE;
433 break;
434 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200435 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200436 }
437
438 if (closure_in_use)
439 {
440 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
441 typval_T *stack;
442
443 // A closure is using the arguments and/or local variables.
444 // Move them to the called function.
445 if (funcstack == NULL)
446 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200447 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
448 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200449 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
450 funcstack->fs_ga.ga_data = stack;
451 if (stack == NULL)
452 {
453 vim_free(funcstack);
454 return FAIL;
455 }
456
457 // Move or copy the arguments.
458 for (idx = 0; idx < argcount; ++idx)
459 {
460 tv = STACK_TV(top + idx);
461 if (free_arguments)
462 {
463 *(stack + idx) = *tv;
464 tv->v_type = VAR_UNKNOWN;
465 }
466 else
467 copy_tv(tv, stack + idx);
468 }
469 // Move the local variables.
470 for (idx = 0; idx < dfunc->df_varcount; ++idx)
471 {
472 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200473
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200474 // A partial created for a local function, that is also used as a
475 // local variable, has a reference count for the variable, thus
476 // will never go down to zero. When all these refcounts are one
477 // then the funcstack is unused. We need to count how many we have
478 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200479 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
480 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200481 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200482
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200483 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200484 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
485 gap->ga_len - closure_count + i])
486 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200487 }
488
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200489 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490 tv->v_type = VAR_UNKNOWN;
491 }
492
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200493 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200494 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200495 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
496 - closure_count + idx];
497 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200498 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200499 ++funcstack->fs_refcount;
500 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100501 pt->pt_outer.out_stack = &funcstack->fs_ga;
502 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
503 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200504 }
505 }
506 }
507
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200508 for (idx = 0; idx < closure_count; ++idx)
509 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
510 - closure_count + idx]);
511 gap->ga_len -= closure_count;
512 if (gap->ga_len == 0)
513 ga_clear(gap);
514
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515 return OK;
516}
517
518/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200519 * Called when a partial is freed or its reference count goes down to one. The
520 * funcstack may be the only reference to the partials in the local variables.
521 * Go over all of them, the funcref and can be freed if all partials
522 * referencing the funcstack have a reference count of one.
523 */
524 void
525funcstack_check_refcount(funcstack_T *funcstack)
526{
527 int i;
528 garray_T *gap = &funcstack->fs_ga;
529 int done = 0;
530
531 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
532 return;
533 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
534 {
535 typval_T *tv = ((typval_T *)gap->ga_data) + i;
536
537 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
538 && tv->vval.v_partial->pt_funcstack == funcstack
539 && tv->vval.v_partial->pt_refcount == 1)
540 ++done;
541 }
542 if (done == funcstack->fs_min_refcount)
543 {
544 typval_T *stack = gap->ga_data;
545
546 // All partials referencing the funcstack have a reference count of
547 // one, thus the funcstack is no longer of use.
548 for (i = 0; i < gap->ga_len; ++i)
549 clear_tv(stack + i);
550 vim_free(stack);
551 vim_free(funcstack);
552 }
553}
554
555/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 * Return from the current function.
557 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200558 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100559func_return(funclocal_T *funclocal, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100562 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200563 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
564 + ectx->ec_dfunc_idx;
565 int argcount = ufunc_argcount(dfunc->df_ufunc);
566 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200567 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100568 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
569 + STACK_FRAME_FUNC_OFF)->vval.v_number;
570 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
571 + prev_dfunc_idx;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100572 funclocal_T *floc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573
Bram Moolenaar12d26532021-02-19 19:13:21 +0100574#ifdef FEAT_PROFILE
575 if (do_profiling == PROF_YES)
576 {
577 ufunc_T *caller = prev_dfunc->df_ufunc;
578
579 if (dfunc->df_ufunc->uf_profiling
580 || (caller != NULL && caller->uf_profiling))
581 {
582 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
583 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
584 --profile_info_ga.ga_len;
585 }
586 }
587#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100588 // TODO: when is it safe to delete the function when it is no longer used?
589 --dfunc->df_ufunc->uf_calls;
590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200592 entry = estack_pop();
593 if (entry != NULL)
594 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200596 if (handle_closure_in_use(ectx, TRUE) == FAIL)
597 return FAIL;
598
599 // Clear the arguments.
600 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
601 clear_tv(STACK_TV(idx));
602
603 // Clear local variables and temp values, but not the return value.
604 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100605 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100607
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100608 // The return value should be on top of the stack. However, when aborting
609 // it may not be there and ec_frame_idx is the top of the stack.
610 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100611 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100612 ret_idx = 0;
613
Bram Moolenaar0186e582021-01-10 18:33:11 +0100614 vim_free(ectx->ec_outer);
615
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100616 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100617 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100618 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
619 + STACK_FRAME_IIDX_OFF)->vval.v_number;
620 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
621 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100622 floc = (void *)STACK_TV(ectx->ec_frame_idx
623 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200624 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100625 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
626 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100627 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100628
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100629 if (floc == NULL)
630 funclocal->floc_restore_cmdmod = FALSE;
631 else
632 {
633 *funclocal = *floc;
634 vim_free(floc);
635 }
636
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100637 if (ret_idx > 0)
638 {
639 // Reset the stack to the position before the call, with a spot for the
640 // return value, moved there from above the frame.
641 ectx->ec_stack.ga_len = top + 1;
642 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
643 }
644 else
645 // Reset the stack to the position before the call.
646 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200647
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100648 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200649 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650}
651
652#undef STACK_TV
653
654/*
655 * Prepare arguments and rettv for calling a builtin or user function.
656 */
657 static int
658call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
659{
660 int idx;
661 typval_T *tv;
662
663 // Move arguments from bottom of the stack to argvars[] and add terminator.
664 for (idx = 0; idx < argcount; ++idx)
665 argvars[idx] = *STACK_TV_BOT(idx - argcount);
666 argvars[argcount].v_type = VAR_UNKNOWN;
667
668 // Result replaces the arguments on the stack.
669 if (argcount > 0)
670 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200671 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 return FAIL;
673 else
674 ++ectx->ec_stack.ga_len;
675
676 // Default return value is zero.
677 tv = STACK_TV_BOT(-1);
678 tv->v_type = VAR_NUMBER;
679 tv->vval.v_number = 0;
680
681 return OK;
682}
683
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200684// Ugly global to avoid passing the execution context around through many
685// layers.
686static ectx_T *current_ectx = NULL;
687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688/*
689 * Call a builtin function by index.
690 */
691 static int
692call_bfunc(int func_idx, int argcount, ectx_T *ectx)
693{
694 typval_T argvars[MAX_FUNC_ARGS];
695 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100696 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200697 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698
699 if (call_prepare(argcount, argvars, ectx) == FAIL)
700 return FAIL;
701
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200702 // Call the builtin function. Set "current_ectx" so that when it
703 // recursively invokes call_def_function() a closure context can be set.
704 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200706 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707
708 // Clear the arguments.
709 for (idx = 0; idx < argcount; ++idx)
710 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200711
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100712 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200713 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 return OK;
715}
716
717/*
718 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100719 * If the function is compiled this will add a stack frame and set the
720 * instruction pointer at the start of the function.
721 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100722 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100723 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 */
725 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100726call_ufunc(
727 ufunc_T *ufunc,
728 partial_T *pt,
729 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100730 funclocal_T *funclocal,
Bram Moolenaar0186e582021-01-10 18:33:11 +0100731 ectx_T *ectx,
732 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733{
734 typval_T argvars[MAX_FUNC_ARGS];
735 funcexe_T funcexe;
736 int error;
737 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100738 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100739#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100740 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100741#else
742# define profiling FALSE
743#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744
Bram Moolenaarb2049902021-01-24 12:53:53 +0100745 if (func_needs_compiling(ufunc, profiling)
746 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200747 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200748 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100749 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100750 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100751 if (error != FCERR_UNKNOWN)
752 {
753 if (error == FCERR_TOOMANY)
754 semsg(_(e_toomanyarg), ufunc->uf_name);
755 else
756 semsg(_(e_toofewarg), ufunc->uf_name);
757 return FAIL;
758 }
759
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100760 // The function has been compiled, can call it quickly. For a function
761 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100762 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100763 if (iptr != NULL)
764 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100765 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100766 iptr->isn_type = ISN_DCALL;
767 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
768 iptr->isn_arg.dfunc.cdf_argcount = argcount;
769 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100770 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, funclocal, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772
773 if (call_prepare(argcount, argvars, ectx) == FAIL)
774 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200775 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 funcexe.evaluate = TRUE;
777
778 // Call the user function. Result goes in last position on the stack.
779 // TODO: add selfdict if there is one
780 error = call_user_func_check(ufunc, argcount, argvars,
781 STACK_TV_BOT(-1), &funcexe, NULL);
782
783 // Clear the arguments.
784 for (idx = 0; idx < argcount; ++idx)
785 clear_tv(&argvars[idx]);
786
787 if (error != FCERR_NONE)
788 {
789 user_func_error(error, ufunc->uf_name);
790 return FAIL;
791 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100792 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200793 // Error other than from calling the function itself.
794 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 return OK;
796}
797
798/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200799 * Return TRUE if an error was given or CTRL-C was pressed.
800 */
801 static int
802vim9_aborting(int prev_called_emsg)
803{
804 return called_emsg > prev_called_emsg || got_int || did_throw;
805}
806
807/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 * Execute a function by "name".
809 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100810 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 * Returns FAIL if not found without an error message.
812 */
813 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100814call_by_name(
815 char_u *name,
816 int argcount,
817 funclocal_T *funclocal,
818 ectx_T *ectx,
819 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820{
821 ufunc_T *ufunc;
822
823 if (builtin_function(name, -1))
824 {
825 int func_idx = find_internal_func(name);
826
827 if (func_idx < 0)
828 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200829 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 return FAIL;
831 return call_bfunc(func_idx, argcount, ectx);
832 }
833
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200834 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200835
836 if (ufunc == NULL)
837 {
838 int called_emsg_before = called_emsg;
839
840 if (script_autoload(name, TRUE))
841 // loaded a package, search for the function again
842 ufunc = find_func(name, FALSE, NULL);
843 if (vim9_aborting(called_emsg_before))
844 return FAIL; // bail out if loading the script caused an error
845 }
846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100848 {
849 if (ufunc->uf_arg_types != NULL)
850 {
851 int i;
852 typval_T *argv = STACK_TV_BOT(0) - argcount;
853
854 // The function can change at runtime, check that the argument
855 // types are correct.
856 for (i = 0; i < argcount; ++i)
857 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100858 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100859
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100860 if (i < ufunc->uf_args.ga_len)
861 type = ufunc->uf_arg_types[i];
862 else if (ufunc->uf_va_type != NULL)
863 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100864 if (type != NULL && check_typval_arg_type(type,
865 &argv[i], i + 1) == FAIL)
866 return FAIL;
867 }
868 }
869
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100870 return call_ufunc(ufunc, NULL, argcount, funclocal, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872
873 return FAIL;
874}
875
876 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100877call_partial(
878 typval_T *tv,
879 int argcount_arg,
880 funclocal_T *funclocal,
881 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200883 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200884 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100886 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
888 if (tv->v_type == VAR_PARTIAL)
889 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200890 partial_T *pt = tv->vval.v_partial;
891 int i;
892
893 if (pt->pt_argc > 0)
894 {
895 // Make space for arguments from the partial, shift the "argcount"
896 // arguments up.
897 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
898 return FAIL;
899 for (i = 1; i <= argcount; ++i)
900 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
901 ectx->ec_stack.ga_len += pt->pt_argc;
902 argcount += pt->pt_argc;
903
904 // copy the arguments from the partial onto the stack
905 for (i = 0; i < pt->pt_argc; ++i)
906 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908
909 if (pt->pt_func != NULL)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100910 return call_ufunc(pt->pt_func, pt, argcount, funclocal, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912 name = pt->pt_name;
913 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200914 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200916 if (name != NULL)
917 {
918 char_u fname_buf[FLEN_FIXED + 1];
919 char_u *tofree = NULL;
920 int error = FCERR_NONE;
921 char_u *fname;
922
923 // May need to translate <SNR>123_ to K_SNR.
924 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
925 if (error != FCERR_NONE)
926 res = FAIL;
927 else
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100928 res = call_by_name(fname, argcount, funclocal, ectx, NULL);
Bram Moolenaar95006e32020-08-29 17:47:08 +0200929 vim_free(tofree);
930 }
931
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100932 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 {
934 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200935 semsg(_(e_unknownfunc),
936 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 return FAIL;
938 }
939 return OK;
940}
941
942/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200943 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
944 * TRUE.
945 */
946 static int
947error_if_locked(int lock, char *error)
948{
949 if (lock & (VAR_LOCKED | VAR_FIXED))
950 {
951 emsg(_(error));
952 return TRUE;
953 }
954 return FALSE;
955}
956
957/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100958 * Give an error if "tv" is not a number and return FAIL.
959 */
960 static int
961check_for_number(typval_T *tv)
962{
963 if (tv->v_type != VAR_NUMBER)
964 {
965 semsg(_(e_expected_str_but_got_str),
966 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
967 return FAIL;
968 }
969 return OK;
970}
971
972/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100973 * Store "tv" in variable "name".
974 * This is for s: and g: variables.
975 */
976 static void
977store_var(char_u *name, typval_T *tv)
978{
979 funccal_entry_T entry;
980
981 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100982 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100983 restore_funccal();
984}
985
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100986/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100987 * Convert "tv" to a string.
988 * Return FAIL if not allowed.
989 */
990 static int
991do_2string(typval_T *tv, int is_2string_any)
992{
993 if (tv->v_type != VAR_STRING)
994 {
995 char_u *str;
996
997 if (is_2string_any)
998 {
999 switch (tv->v_type)
1000 {
1001 case VAR_SPECIAL:
1002 case VAR_BOOL:
1003 case VAR_NUMBER:
1004 case VAR_FLOAT:
1005 case VAR_BLOB: break;
1006 default: to_string_error(tv->v_type);
1007 return FAIL;
1008 }
1009 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001010 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001011 clear_tv(tv);
1012 tv->v_type = VAR_STRING;
1013 tv->vval.v_string = str;
1014 }
1015 return OK;
1016}
1017
1018/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001019 * When the value of "sv" is a null list of dict, allocate it.
1020 */
1021 static void
1022allocate_if_null(typval_T *tv)
1023{
1024 switch (tv->v_type)
1025 {
1026 case VAR_LIST:
1027 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001028 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001029 break;
1030 case VAR_DICT:
1031 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001032 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001033 break;
1034 default:
1035 break;
1036 }
1037}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001038
Bram Moolenaare7525c52021-01-09 13:20:37 +01001039/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001040 * Return the character "str[index]" where "index" is the character index,
1041 * including composing characters.
1042 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001043 */
1044 char_u *
1045char_from_string(char_u *str, varnumber_T index)
1046{
1047 size_t nbyte = 0;
1048 varnumber_T nchar = index;
1049 size_t slen;
1050
1051 if (str == NULL)
1052 return NULL;
1053 slen = STRLEN(str);
1054
1055 // do the same as for a list: a negative index counts from the end
1056 if (index < 0)
1057 {
1058 int clen = 0;
1059
1060 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaar0289a092021-03-14 18:40:19 +01001061 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001062 nchar = clen + index;
1063 if (nchar < 0)
1064 // unlike list: index out of range results in empty string
1065 return NULL;
1066 }
1067
1068 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaar0289a092021-03-14 18:40:19 +01001069 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001070 if (nbyte >= slen)
1071 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001072 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001073}
1074
1075/*
1076 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001077 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001078 * If going over the end return "str_len".
1079 * If "idx" is negative count from the end, -1 is the last character.
1080 * When going over the start return -1.
1081 */
1082 static long
1083char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1084{
1085 varnumber_T nchar = idx;
1086 size_t nbyte = 0;
1087
1088 if (nchar >= 0)
1089 {
1090 while (nchar > 0 && nbyte < str_len)
1091 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001092 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001093 --nchar;
1094 }
1095 }
1096 else
1097 {
1098 nbyte = str_len;
1099 while (nchar < 0 && nbyte > 0)
1100 {
1101 --nbyte;
1102 nbyte -= mb_head_off(str, str + nbyte);
1103 ++nchar;
1104 }
1105 if (nchar < 0)
1106 return -1;
1107 }
1108 return (long)nbyte;
1109}
1110
1111/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001112 * Return the slice "str[first : last]" using character indexes. Composing
1113 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001114 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001115 * Return NULL when the result is empty.
1116 */
1117 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001118string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001119{
1120 long start_byte, end_byte;
1121 size_t slen;
1122
1123 if (str == NULL)
1124 return NULL;
1125 slen = STRLEN(str);
1126 start_byte = char_idx2byte(str, slen, first);
1127 if (start_byte < 0)
1128 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001129 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001130 end_byte = (long)slen;
1131 else
1132 {
1133 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001134 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001135 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001136 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001137 }
1138
1139 if (start_byte >= (long)slen || end_byte <= start_byte)
1140 return NULL;
1141 return vim_strnsave(str + start_byte, end_byte - start_byte);
1142}
1143
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001144 static svar_T *
1145get_script_svar(scriptref_T *sref, ectx_T *ectx)
1146{
1147 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1148 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1149 + ectx->ec_dfunc_idx;
1150 svar_T *sv;
1151
1152 if (sref->sref_seq != si->sn_script_seq)
1153 {
1154 // The script was reloaded after the function was
1155 // compiled, the script_idx may not be valid.
1156 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1157 dfunc->df_ufunc->uf_name_exp);
1158 return NULL;
1159 }
1160 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1161 if (!equal_type(sv->sv_type, sref->sref_type))
1162 {
1163 emsg(_(e_script_variable_type_changed));
1164 return NULL;
1165 }
1166 return sv;
1167}
1168
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001169/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 * Execute a function by "name".
1171 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001172 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 */
1174 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001175call_eval_func(
1176 char_u *name,
1177 int argcount,
1178 funclocal_T *funclocal,
1179 ectx_T *ectx,
1180 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181{
Bram Moolenaared677f52020-08-12 16:38:10 +02001182 int called_emsg_before = called_emsg;
1183 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001185 res = call_by_name(name, argcount, funclocal, ectx, iptr);
Bram Moolenaared677f52020-08-12 16:38:10 +02001186 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001188 dictitem_T *v;
1189
1190 v = find_var(name, NULL, FALSE);
1191 if (v == NULL)
1192 {
1193 semsg(_(e_unknownfunc), name);
1194 return FAIL;
1195 }
1196 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1197 {
1198 semsg(_(e_unknownfunc), name);
1199 return FAIL;
1200 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001201 return call_partial(&v->di_tv, argcount, funclocal, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001203 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204}
1205
1206/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001207 * When a function reference is used, fill a partial with the information
1208 * needed, especially when it is used as a closure.
1209 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001210 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001211fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1212{
1213 pt->pt_func = ufunc;
1214 pt->pt_refcount = 1;
1215
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001216 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001217 {
1218 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1219 + ectx->ec_dfunc_idx;
1220
1221 // The closure needs to find arguments and local
1222 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001223 pt->pt_outer.out_stack = &ectx->ec_stack;
1224 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1225 pt->pt_outer.out_up = ectx->ec_outer;
1226 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001227
1228 // If this function returns and the closure is still
1229 // being used, we need to make a copy of the context
1230 // (arguments and local variables). Store a reference
1231 // to the partial so we can handle that.
1232 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1233 {
1234 vim_free(pt);
1235 return FAIL;
1236 }
1237 // Extra variable keeps the count of closures created
1238 // in the current function call.
1239 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1240 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1241
1242 ((partial_T **)ectx->ec_funcrefs.ga_data)
1243 [ectx->ec_funcrefs.ga_len] = pt;
1244 ++pt->pt_refcount;
1245 ++ectx->ec_funcrefs.ga_len;
1246 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001247 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001248 return OK;
1249}
1250
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001251
Bram Moolenaarf112f302020-12-20 17:47:52 +01001252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 * Call a "def" function from old Vim script.
1254 * Return OK or FAIL.
1255 */
1256 int
1257call_def_function(
1258 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001259 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001261 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 typval_T *rettv) // return value
1263{
1264 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001265 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001266 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 typval_T *tv;
1268 int idx;
1269 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001270 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001271 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001272 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001273 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001274 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001275 msglist_T **saved_msg_list = NULL;
1276 msglist_T *private_msg_list = NULL;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001277 funclocal_T funclocal;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001278 int save_emsg_silent_def = emsg_silent_def;
1279 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001280 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001281 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001282 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283
1284// Get pointer to item in the stack.
1285#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1286
1287// Get pointer to item at the bottom of the stack, -1 is the bottom.
1288#undef STACK_TV_BOT
1289#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1290
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001291// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001292#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 +01001293
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001294 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001295 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1296 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001297 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001298 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001299 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001300 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001301 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001302 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001303 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001304
Bram Moolenaar09689a02020-05-09 22:50:08 +02001305 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001306 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001307 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1308 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001309 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001310 {
1311 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001312 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001313 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001316 // If depth of calling is getting too high, don't execute the function.
1317 orig_funcdepth = funcdepth_get();
1318 if (funcdepth_increment() == FAIL)
1319 return FAIL;
1320
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001321 CLEAR_FIELD(funclocal);
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001322 CLEAR_FIELD(ectx);
1323 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1324 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1325 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001326 {
1327 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001328 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001331 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001333 // Put arguments on the stack, but no more than what the function expects.
1334 // A lambda can be called with more arguments than it uses.
1335 for (idx = 0; idx < argc
1336 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1337 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001339 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001340 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001341 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001342 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 copy_tv(&argv[idx], STACK_TV_BOT(0));
1344 ++ectx.ec_stack.ga_len;
1345 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001346
1347 // Turn varargs into a list. Empty list if no args.
1348 if (ufunc->uf_va_name != NULL)
1349 {
1350 int vararg_count = argc - ufunc->uf_args.ga_len;
1351
1352 if (vararg_count < 0)
1353 vararg_count = 0;
1354 else
1355 argc -= vararg_count;
1356 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001357 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001358
1359 // Check the type of the list items.
1360 tv = STACK_TV_BOT(-1);
1361 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001362 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001363 && ufunc->uf_va_type->tt_member != &t_any
1364 && tv->vval.v_list != NULL)
1365 {
1366 type_T *expected = ufunc->uf_va_type->tt_member;
1367 listitem_T *li = tv->vval.v_list->lv_first;
1368
1369 for (idx = 0; idx < vararg_count; ++idx)
1370 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001371 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001372 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001373 goto failed_early;
1374 li = li->li_next;
1375 }
1376 }
1377
Bram Moolenaar23e03252020-04-12 22:22:31 +02001378 if (defcount > 0)
1379 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001380 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001381 --ectx.ec_stack.ga_len;
1382 }
1383
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001384 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001385 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001386 if (defcount > 0)
1387 for (idx = 0; idx < defcount; ++idx)
1388 {
1389 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1390 ++ectx.ec_stack.ga_len;
1391 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001392 if (ufunc->uf_va_name != NULL)
Bram Moolenaarc970e422021-03-17 15:03:04 +01001393 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394
1395 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001396 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1397 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001399 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001400 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1401 + ufunc->uf_dfunc_idx;
1402 ufunc_T *base_ufunc = dfunc->df_ufunc;
1403
1404 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1405 // by copy_func().
1406 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001407 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001408 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1409 if (ectx.ec_outer == NULL)
1410 goto failed_early;
1411 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001412 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001413 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1414 {
1415 if (current_ectx->ec_outer != NULL)
1416 *ectx.ec_outer = *current_ectx->ec_outer;
1417 }
1418 else
1419 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001420 }
1421 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001422 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1423 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001424 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001425 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 // dummy frame entries
1428 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1429 {
1430 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1431 ++ectx.ec_stack.ga_len;
1432 }
1433
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001434 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001435 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1437 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001439 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001440 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001441 ectx.ec_stack.ga_len += dfunc->df_varcount;
1442 if (dfunc->df_has_closure)
1443 {
1444 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1445 STACK_TV_VAR(idx)->vval.v_number = 0;
1446 ++ectx.ec_stack.ga_len;
1447 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001448
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001449 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001450 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001451
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001452 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001453 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001454 estack_push_ufunc(ufunc, 1);
1455 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001456 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1457
Bram Moolenaar352134b2020-10-17 22:04:08 +02001458 // Use a specific location for storing error messages to be converted to an
1459 // exception.
1460 saved_msg_list = msg_list;
1461 msg_list = &private_msg_list;
1462
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001463 // Do turn errors into exceptions.
1464 suppress_errthrow = FALSE;
1465
Bram Moolenaarc970e422021-03-17 15:03:04 +01001466 // Do not delete the function while executing it.
1467 ++ufunc->uf_calls;
1468
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001469 // When ":silent!" was used before calling then we still abort the
1470 // function. If ":silent!" is used in the function then we don't.
1471 emsg_silent_def = emsg_silent;
1472 did_emsg_def = 0;
1473
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001474 where.wt_index = 0;
1475 where.wt_variable = FALSE;
1476
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001477 // Decide where to start execution, handles optional arguments.
1478 init_instr_idx(ufunc, argc, &ectx);
1479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 for (;;)
1481 {
1482 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001483
Bram Moolenaar270d0382020-05-15 21:42:53 +02001484 if (++breakcheck_count >= 100)
1485 {
1486 line_breakcheck();
1487 breakcheck_count = 0;
1488 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001489 if (got_int)
1490 {
1491 // Turn CTRL-C into an exception.
1492 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001493 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001494 goto failed;
1495 did_throw = TRUE;
1496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497
Bram Moolenaara26b9702020-04-18 19:53:28 +02001498 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1499 {
1500 // Turn an error message into an exception.
1501 did_emsg = FALSE;
1502 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1503 goto failed;
1504 did_throw = TRUE;
1505 *msg_list = NULL;
1506 }
1507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 if (did_throw && !ectx.ec_in_catch)
1509 {
1510 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001511 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512
1513 // An exception jumps to the first catch, finally, or returns from
1514 // the current function.
1515 if (trystack->ga_len > 0)
1516 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001517 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 {
1519 // jump to ":catch" or ":finally"
1520 ectx.ec_in_catch = TRUE;
1521 ectx.ec_iidx = trycmd->tcd_catch_idx;
1522 }
1523 else
1524 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001525 // Not inside try or need to return from current functions.
1526 // Push a dummy return value.
1527 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1528 goto failed;
1529 tv = STACK_TV_BOT(0);
1530 tv->v_type = VAR_NUMBER;
1531 tv->vval.v_number = 0;
1532 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001533 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001535 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001536 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001537 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1538 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 goto done;
1540 }
1541
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001542 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001543 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 }
1545 continue;
1546 }
1547
1548 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1549 switch (iptr->isn_type)
1550 {
1551 // execute Ex command line
1552 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001553 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001554 source_cookie_T cookie;
1555
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001556 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001557 // Pass getsourceline to get an error for a missing ":end"
1558 // command.
1559 CLEAR_FIELD(cookie);
1560 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1561 if (do_cmdline(iptr->isn_arg.string,
1562 getsourceline, &cookie,
1563 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1564 == FAIL
1565 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001566 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 break;
1569
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001570 // execute Ex command from pieces on the stack
1571 case ISN_EXECCONCAT:
1572 {
1573 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001574 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001575 int pass;
1576 int i;
1577 char_u *cmd = NULL;
1578 char_u *str;
1579
1580 for (pass = 1; pass <= 2; ++pass)
1581 {
1582 for (i = 0; i < count; ++i)
1583 {
1584 tv = STACK_TV_BOT(i - count);
1585 str = tv->vval.v_string;
1586 if (str != NULL && *str != NUL)
1587 {
1588 if (pass == 2)
1589 STRCPY(cmd + len, str);
1590 len += STRLEN(str);
1591 }
1592 if (pass == 2)
1593 clear_tv(tv);
1594 }
1595 if (pass == 1)
1596 {
1597 cmd = alloc(len + 1);
1598 if (cmd == NULL)
1599 goto failed;
1600 len = 0;
1601 }
1602 }
1603
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001604 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001605 do_cmdline_cmd(cmd);
1606 vim_free(cmd);
1607 }
1608 break;
1609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 // execute :echo {string} ...
1611 case ISN_ECHO:
1612 {
1613 int count = iptr->isn_arg.echo.echo_count;
1614 int atstart = TRUE;
1615 int needclr = TRUE;
1616
1617 for (idx = 0; idx < count; ++idx)
1618 {
1619 tv = STACK_TV_BOT(idx - count);
1620 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1621 &atstart, &needclr);
1622 clear_tv(tv);
1623 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001624 if (needclr)
1625 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 ectx.ec_stack.ga_len -= count;
1627 }
1628 break;
1629
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001630 // :execute {string} ...
1631 // :echomsg {string} ...
1632 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001633 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001634 case ISN_ECHOMSG:
1635 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001636 {
1637 int count = iptr->isn_arg.number;
1638 garray_T ga;
1639 char_u buf[NUMBUFLEN];
1640 char_u *p;
1641 int len;
1642 int failed = FALSE;
1643
1644 ga_init2(&ga, 1, 80);
1645 for (idx = 0; idx < count; ++idx)
1646 {
1647 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001648 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001649 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001650 if (tv->v_type == VAR_CHANNEL
1651 || tv->v_type == VAR_JOB)
1652 {
1653 SOURCING_LNUM = iptr->isn_lnum;
1654 emsg(_(e_inval_string));
1655 break;
1656 }
1657 else
1658 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001659 }
1660 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001661 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001662
1663 len = (int)STRLEN(p);
1664 if (ga_grow(&ga, len + 2) == FAIL)
1665 failed = TRUE;
1666 else
1667 {
1668 if (ga.ga_len > 0)
1669 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1670 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1671 ga.ga_len += len;
1672 }
1673 clear_tv(tv);
1674 }
1675 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001676 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001677 {
1678 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001679 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001680 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001681
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001682 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001683 {
1684 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001685 {
1686 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001687 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001688 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001689 {
1690 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001691 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001692 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001693 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001694 else
1695 {
1696 msg_sb_eol();
1697 if (iptr->isn_type == ISN_ECHOMSG)
1698 {
1699 msg_attr(ga.ga_data, echo_attr);
1700 out_flush();
1701 }
1702 else
1703 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001704 SOURCING_LNUM = iptr->isn_lnum;
1705 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001706 }
1707 }
1708 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001709 ga_clear(&ga);
1710 }
1711 break;
1712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 // load local variable or argument
1714 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001715 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 goto failed;
1717 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1718 ++ectx.ec_stack.ga_len;
1719 break;
1720
1721 // load v: variable
1722 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001723 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 goto failed;
1725 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1726 ++ectx.ec_stack.ga_len;
1727 break;
1728
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001729 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 case ISN_LOADSCRIPT:
1731 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001732 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 svar_T *sv;
1734
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001735 sv = get_script_svar(sref, &ectx);
1736 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001737 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001738 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001739 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 goto failed;
1741 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1742 ++ectx.ec_stack.ga_len;
1743 }
1744 break;
1745
1746 // load s: variable in old script
1747 case ISN_LOADS:
1748 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001749 hashtab_T *ht = &SCRIPT_VARS(
1750 iptr->isn_arg.loadstore.ls_sid);
1751 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 if (di == NULL)
1755 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001756 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001757 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001758 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 }
1760 else
1761 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001762 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 goto failed;
1764 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1765 ++ectx.ec_stack.ga_len;
1766 }
1767 }
1768 break;
1769
Bram Moolenaard3aac292020-04-19 14:32:17 +02001770 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001772 case ISN_LOADB:
1773 case ISN_LOADW:
1774 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001776 dictitem_T *di = NULL;
1777 hashtab_T *ht = NULL;
1778 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001779
Bram Moolenaard3aac292020-04-19 14:32:17 +02001780 switch (iptr->isn_type)
1781 {
1782 case ISN_LOADG:
1783 ht = get_globvar_ht();
1784 namespace = 'g';
1785 break;
1786 case ISN_LOADB:
1787 ht = &curbuf->b_vars->dv_hashtab;
1788 namespace = 'b';
1789 break;
1790 case ISN_LOADW:
1791 ht = &curwin->w_vars->dv_hashtab;
1792 namespace = 'w';
1793 break;
1794 case ISN_LOADT:
1795 ht = &curtab->tp_vars->dv_hashtab;
1796 namespace = 't';
1797 break;
1798 default: // Cannot reach here
1799 goto failed;
1800 }
1801 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 if (di == NULL)
1804 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001805 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001806 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001807 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001808 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 }
1810 else
1811 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001812 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 goto failed;
1814 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1815 ++ectx.ec_stack.ga_len;
1816 }
1817 }
1818 break;
1819
Bram Moolenaar03290b82020-12-19 16:30:44 +01001820 // load autoload variable
1821 case ISN_LOADAUTO:
1822 {
1823 char_u *name = iptr->isn_arg.string;
1824
1825 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1826 goto failed;
1827 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001828 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001829 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001830 goto on_error;
1831 ++ectx.ec_stack.ga_len;
1832 }
1833 break;
1834
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001835 // load g:/b:/w:/t: namespace
1836 case ISN_LOADGDICT:
1837 case ISN_LOADBDICT:
1838 case ISN_LOADWDICT:
1839 case ISN_LOADTDICT:
1840 {
1841 dict_T *d = NULL;
1842
1843 switch (iptr->isn_type)
1844 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001845 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1846 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1847 case ISN_LOADWDICT: d = curwin->w_vars; break;
1848 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001849 default: // Cannot reach here
1850 goto failed;
1851 }
1852 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1853 goto failed;
1854 tv = STACK_TV_BOT(0);
1855 tv->v_type = VAR_DICT;
1856 tv->v_lock = 0;
1857 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001858 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001859 ++ectx.ec_stack.ga_len;
1860 }
1861 break;
1862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 // load &option
1864 case ISN_LOADOPT:
1865 {
1866 typval_T optval;
1867 char_u *name = iptr->isn_arg.string;
1868
Bram Moolenaara8c17702020-04-01 21:17:24 +02001869 // This is not expected to fail, name is checked during
1870 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001871 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001873 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001874 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 *STACK_TV_BOT(0) = optval;
1876 ++ectx.ec_stack.ga_len;
1877 }
1878 break;
1879
1880 // load $ENV
1881 case ISN_LOADENV:
1882 {
1883 typval_T optval;
1884 char_u *name = iptr->isn_arg.string;
1885
Bram Moolenaar270d0382020-05-15 21:42:53 +02001886 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001888 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001889 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 *STACK_TV_BOT(0) = optval;
1891 ++ectx.ec_stack.ga_len;
1892 }
1893 break;
1894
1895 // load @register
1896 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001897 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 goto failed;
1899 tv = STACK_TV_BOT(0);
1900 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001901 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001902 // This may result in NULL, which should be equivalent to an
1903 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 tv->vval.v_string = get_reg_contents(
1905 iptr->isn_arg.number, GREG_EXPR_SRC);
1906 ++ectx.ec_stack.ga_len;
1907 break;
1908
1909 // store local variable
1910 case ISN_STORE:
1911 --ectx.ec_stack.ga_len;
1912 tv = STACK_TV_VAR(iptr->isn_arg.number);
1913 clear_tv(tv);
1914 *tv = *STACK_TV_BOT(0);
1915 break;
1916
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001917 // store s: variable in old script
1918 case ISN_STORES:
1919 {
1920 hashtab_T *ht = &SCRIPT_VARS(
1921 iptr->isn_arg.loadstore.ls_sid);
1922 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001923 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001924
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001925 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001926 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001927 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001928 else
1929 {
1930 clear_tv(&di->di_tv);
1931 di->di_tv = *STACK_TV_BOT(0);
1932 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001933 }
1934 break;
1935
1936 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 case ISN_STORESCRIPT:
1938 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001939 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1940 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001942 sv = get_script_svar(sref, &ectx);
1943 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001944 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 --ectx.ec_stack.ga_len;
1946 clear_tv(sv->sv_tv);
1947 *sv->sv_tv = *STACK_TV_BOT(0);
1948 }
1949 break;
1950
1951 // store option
1952 case ISN_STOREOPT:
1953 {
1954 long n = 0;
1955 char_u *s = NULL;
1956 char *msg;
1957
1958 --ectx.ec_stack.ga_len;
1959 tv = STACK_TV_BOT(0);
1960 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001961 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001963 if (s == NULL)
1964 s = (char_u *)"";
1965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001967 // must be VAR_NUMBER, CHECKTYPE makes sure
1968 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1970 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001971 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 if (msg != NULL)
1973 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001974 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001976 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 }
1979 break;
1980
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001981 // store $ENV
1982 case ISN_STOREENV:
1983 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001984 tv = STACK_TV_BOT(0);
1985 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1986 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001987 break;
1988
1989 // store @r
1990 case ISN_STOREREG:
1991 {
1992 int reg = iptr->isn_arg.number;
1993
1994 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001995 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001996 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001997 tv_get_string(tv), -1, FALSE);
1998 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001999 }
2000 break;
2001
2002 // store v: variable
2003 case ISN_STOREV:
2004 --ectx.ec_stack.ga_len;
2005 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2006 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002007 // should not happen, type is checked when compiling
2008 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002009 break;
2010
Bram Moolenaard3aac292020-04-19 14:32:17 +02002011 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002013 case ISN_STOREB:
2014 case ISN_STOREW:
2015 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002017 dictitem_T *di;
2018 hashtab_T *ht;
2019 char_u *name = iptr->isn_arg.string + 2;
2020
Bram Moolenaard3aac292020-04-19 14:32:17 +02002021 switch (iptr->isn_type)
2022 {
2023 case ISN_STOREG:
2024 ht = get_globvar_ht();
2025 break;
2026 case ISN_STOREB:
2027 ht = &curbuf->b_vars->dv_hashtab;
2028 break;
2029 case ISN_STOREW:
2030 ht = &curwin->w_vars->dv_hashtab;
2031 break;
2032 case ISN_STORET:
2033 ht = &curtab->tp_vars->dv_hashtab;
2034 break;
2035 default: // Cannot reach here
2036 goto failed;
2037 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038
2039 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002040 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002042 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 else
2044 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002045 SOURCING_LNUM = iptr->isn_lnum;
2046 if (var_check_permission(di, name) == FAIL)
2047 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 clear_tv(&di->di_tv);
2049 di->di_tv = *STACK_TV_BOT(0);
2050 }
2051 }
2052 break;
2053
Bram Moolenaar03290b82020-12-19 16:30:44 +01002054 // store an autoload variable
2055 case ISN_STOREAUTO:
2056 SOURCING_LNUM = iptr->isn_lnum;
2057 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2058 clear_tv(STACK_TV_BOT(-1));
2059 --ectx.ec_stack.ga_len;
2060 break;
2061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 // store number in local variable
2063 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002064 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 clear_tv(tv);
2066 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002067 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 break;
2069
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002070 // store value in list or dict variable
2071 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002072 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002073 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002074 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002075 typval_T *tv_dest = STACK_TV_BOT(-1);
2076 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002077
Bram Moolenaar752fc692021-01-04 21:57:11 +01002078 // Stack contains:
2079 // -3 value to be stored
2080 // -2 index
2081 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002082 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002083 SOURCING_LNUM = iptr->isn_lnum;
2084 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002085 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002086 dest_type = tv_dest->v_type;
2087 if (dest_type == VAR_DICT)
2088 status = do_2string(tv_idx, TRUE);
2089 else if (dest_type == VAR_LIST
2090 && tv_idx->v_type != VAR_NUMBER)
2091 {
2092 emsg(_(e_number_exp));
2093 status = FAIL;
2094 }
2095 }
2096 else if (dest_type != tv_dest->v_type)
2097 {
2098 // just in case, should be OK
2099 semsg(_(e_expected_str_but_got_str),
2100 vartype_name(dest_type),
2101 vartype_name(tv_dest->v_type));
2102 status = FAIL;
2103 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002104
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002105 if (status == OK && dest_type == VAR_LIST)
2106 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002107 long lidx = (long)tv_idx->vval.v_number;
2108 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002109
2110 if (list == NULL)
2111 {
2112 emsg(_(e_list_not_set));
2113 goto on_error;
2114 }
2115 if (lidx < 0 && list->lv_len + lidx >= 0)
2116 // negative index is relative to the end
2117 lidx = list->lv_len + lidx;
2118 if (lidx < 0 || lidx > list->lv_len)
2119 {
2120 semsg(_(e_listidx), lidx);
2121 goto on_error;
2122 }
2123 if (lidx < list->lv_len)
2124 {
2125 listitem_T *li = list_find(list, lidx);
2126
2127 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002128 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002129 goto on_error;
2130 // overwrite existing list item
2131 clear_tv(&li->li_tv);
2132 li->li_tv = *tv;
2133 }
2134 else
2135 {
2136 if (error_if_locked(list->lv_lock,
2137 e_cannot_change_list))
2138 goto on_error;
2139 // append to list, only fails when out of memory
2140 if (list_append_tv(list, tv) == FAIL)
2141 goto failed;
2142 clear_tv(tv);
2143 }
2144 }
2145 else if (status == OK && dest_type == VAR_DICT)
2146 {
2147 char_u *key = tv_idx->vval.v_string;
2148 dict_T *dict = tv_dest->vval.v_dict;
2149 dictitem_T *di;
2150
2151 SOURCING_LNUM = iptr->isn_lnum;
2152 if (dict == NULL)
2153 {
2154 emsg(_(e_dictionary_not_set));
2155 goto on_error;
2156 }
2157 if (key == NULL)
2158 key = (char_u *)"";
2159 di = dict_find(dict, key, -1);
2160 if (di != NULL)
2161 {
2162 if (error_if_locked(di->di_tv.v_lock,
2163 e_cannot_change_dict_item))
2164 goto on_error;
2165 // overwrite existing value
2166 clear_tv(&di->di_tv);
2167 di->di_tv = *tv;
2168 }
2169 else
2170 {
2171 if (error_if_locked(dict->dv_lock,
2172 e_cannot_change_dict))
2173 goto on_error;
2174 // add to dict, only fails when out of memory
2175 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2176 goto failed;
2177 clear_tv(tv);
2178 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002179 }
2180 else
2181 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002182 status = FAIL;
2183 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002184 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002185
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002186 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002187 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002188 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002189 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002190 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002191 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002192 goto on_error;
2193 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002194 }
2195 break;
2196
Bram Moolenaar0186e582021-01-10 18:33:11 +01002197 // load or store variable or argument from outer scope
2198 case ISN_LOADOUTER:
2199 case ISN_STOREOUTER:
2200 {
2201 int depth = iptr->isn_arg.outer.outer_depth;
2202 outer_T *outer = ectx.ec_outer;
2203
2204 while (depth > 1 && outer != NULL)
2205 {
2206 outer = outer->out_up;
2207 --depth;
2208 }
2209 if (outer == NULL)
2210 {
2211 SOURCING_LNUM = iptr->isn_lnum;
2212 iemsg("LOADOUTER depth more than scope levels");
2213 goto failed;
2214 }
2215 tv = ((typval_T *)outer->out_stack->ga_data)
2216 + outer->out_frame_idx + STACK_FRAME_SIZE
2217 + iptr->isn_arg.outer.outer_idx;
2218 if (iptr->isn_type == ISN_LOADOUTER)
2219 {
2220 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2221 goto failed;
2222 copy_tv(tv, STACK_TV_BOT(0));
2223 ++ectx.ec_stack.ga_len;
2224 }
2225 else
2226 {
2227 --ectx.ec_stack.ga_len;
2228 clear_tv(tv);
2229 *tv = *STACK_TV_BOT(0);
2230 }
2231 }
2232 break;
2233
Bram Moolenaar752fc692021-01-04 21:57:11 +01002234 // unlet item in list or dict variable
2235 case ISN_UNLETINDEX:
2236 {
2237 typval_T *tv_idx = STACK_TV_BOT(-2);
2238 typval_T *tv_dest = STACK_TV_BOT(-1);
2239 int status = OK;
2240
2241 // Stack contains:
2242 // -2 index
2243 // -1 dict or list
2244 if (tv_dest->v_type == VAR_DICT)
2245 {
2246 // unlet a dict item, index must be a string
2247 if (tv_idx->v_type != VAR_STRING)
2248 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002249 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002250 semsg(_(e_expected_str_but_got_str),
2251 vartype_name(VAR_STRING),
2252 vartype_name(tv_idx->v_type));
2253 status = FAIL;
2254 }
2255 else
2256 {
2257 dict_T *d = tv_dest->vval.v_dict;
2258 char_u *key = tv_idx->vval.v_string;
2259 dictitem_T *di = NULL;
2260
2261 if (key == NULL)
2262 key = (char_u *)"";
2263 if (d != NULL)
2264 di = dict_find(d, key, (int)STRLEN(key));
2265 if (di == NULL)
2266 {
2267 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002268 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002269 semsg(_(e_dictkey), key);
2270 status = FAIL;
2271 }
2272 else
2273 {
2274 // TODO: check for dict or item locked
2275 dictitem_remove(d, di);
2276 }
2277 }
2278 }
2279 else if (tv_dest->v_type == VAR_LIST)
2280 {
2281 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002282 SOURCING_LNUM = iptr->isn_lnum;
2283 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002284 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002285 status = FAIL;
2286 }
2287 else
2288 {
2289 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002290 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002291 listitem_T *li = NULL;
2292
2293 li = list_find(l, n);
2294 if (li == NULL)
2295 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002296 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002297 semsg(_(e_listidx), n);
2298 status = FAIL;
2299 }
2300 else
2301 // TODO: check for list or item locked
2302 listitem_remove(l, li);
2303 }
2304 }
2305 else
2306 {
2307 status = FAIL;
2308 semsg(_(e_cannot_index_str),
2309 vartype_name(tv_dest->v_type));
2310 }
2311
2312 clear_tv(tv_idx);
2313 clear_tv(tv_dest);
2314 ectx.ec_stack.ga_len -= 2;
2315 if (status == FAIL)
2316 goto on_error;
2317 }
2318 break;
2319
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002320 // unlet range of items in list variable
2321 case ISN_UNLETRANGE:
2322 {
2323 // Stack contains:
2324 // -3 index1
2325 // -2 index2
2326 // -1 dict or list
2327 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2328 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2329 typval_T *tv_dest = STACK_TV_BOT(-1);
2330 int status = OK;
2331
2332 if (tv_dest->v_type == VAR_LIST)
2333 {
2334 // indexes must be a number
2335 SOURCING_LNUM = iptr->isn_lnum;
2336 if (check_for_number(tv_idx1) == FAIL
2337 || check_for_number(tv_idx2) == FAIL)
2338 {
2339 status = FAIL;
2340 }
2341 else
2342 {
2343 list_T *l = tv_dest->vval.v_list;
2344 long n1 = (long)tv_idx1->vval.v_number;
2345 long n2 = (long)tv_idx2->vval.v_number;
2346 listitem_T *li;
2347
2348 li = list_find_index(l, &n1);
2349 if (li == NULL
2350 || list_unlet_range(l, li, NULL, n1,
2351 TRUE, n2) == FAIL)
2352 status = FAIL;
2353 }
2354 }
2355 else
2356 {
2357 status = FAIL;
2358 SOURCING_LNUM = iptr->isn_lnum;
2359 semsg(_(e_cannot_index_str),
2360 vartype_name(tv_dest->v_type));
2361 }
2362
2363 clear_tv(tv_idx1);
2364 clear_tv(tv_idx2);
2365 clear_tv(tv_dest);
2366 ectx.ec_stack.ga_len -= 3;
2367 if (status == FAIL)
2368 goto on_error;
2369 }
2370 break;
2371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 // push constant
2373 case ISN_PUSHNR:
2374 case ISN_PUSHBOOL:
2375 case ISN_PUSHSPEC:
2376 case ISN_PUSHF:
2377 case ISN_PUSHS:
2378 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002379 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002380 case ISN_PUSHCHANNEL:
2381 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002382 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 goto failed;
2384 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002385 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 ++ectx.ec_stack.ga_len;
2387 switch (iptr->isn_type)
2388 {
2389 case ISN_PUSHNR:
2390 tv->v_type = VAR_NUMBER;
2391 tv->vval.v_number = iptr->isn_arg.number;
2392 break;
2393 case ISN_PUSHBOOL:
2394 tv->v_type = VAR_BOOL;
2395 tv->vval.v_number = iptr->isn_arg.number;
2396 break;
2397 case ISN_PUSHSPEC:
2398 tv->v_type = VAR_SPECIAL;
2399 tv->vval.v_number = iptr->isn_arg.number;
2400 break;
2401#ifdef FEAT_FLOAT
2402 case ISN_PUSHF:
2403 tv->v_type = VAR_FLOAT;
2404 tv->vval.v_float = iptr->isn_arg.fnumber;
2405 break;
2406#endif
2407 case ISN_PUSHBLOB:
2408 blob_copy(iptr->isn_arg.blob, tv);
2409 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002410 case ISN_PUSHFUNC:
2411 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002412 if (iptr->isn_arg.string == NULL)
2413 tv->vval.v_string = NULL;
2414 else
2415 tv->vval.v_string =
2416 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002417 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002418 case ISN_PUSHCHANNEL:
2419#ifdef FEAT_JOB_CHANNEL
2420 tv->v_type = VAR_CHANNEL;
2421 tv->vval.v_channel = iptr->isn_arg.channel;
2422 if (tv->vval.v_channel != NULL)
2423 ++tv->vval.v_channel->ch_refcount;
2424#endif
2425 break;
2426 case ISN_PUSHJOB:
2427#ifdef FEAT_JOB_CHANNEL
2428 tv->v_type = VAR_JOB;
2429 tv->vval.v_job = iptr->isn_arg.job;
2430 if (tv->vval.v_job != NULL)
2431 ++tv->vval.v_job->jv_refcount;
2432#endif
2433 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 default:
2435 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002436 tv->vval.v_string = vim_strsave(
2437 iptr->isn_arg.string == NULL
2438 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 }
2440 break;
2441
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002442 case ISN_UNLET:
2443 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2444 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002445 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002446 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002447 case ISN_UNLETENV:
2448 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2449 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002450
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002451 case ISN_LOCKCONST:
2452 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2453 break;
2454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 // create a list from items on the stack; uses a single allocation
2456 // for the list header and the items
2457 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002458 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2459 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 break;
2461
2462 // create a dict from items on the stack
2463 case ISN_NEWDICT:
2464 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002465 int count = iptr->isn_arg.number;
2466 dict_T *dict = dict_alloc();
2467 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002468 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469
2470 if (dict == NULL)
2471 goto failed;
2472 for (idx = 0; idx < count; ++idx)
2473 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002474 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002476 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002477 key = tv->vval.v_string == NULL
2478 ? (char_u *)"" : tv->vval.v_string;
2479 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002480 if (item != NULL)
2481 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002482 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002483 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002484 dict_unref(dict);
2485 goto on_error;
2486 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002487 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 clear_tv(tv);
2489 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002490 {
2491 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2495 item->di_tv.v_lock = 0;
2496 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002497 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002498 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002499 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 }
2503
2504 if (count > 0)
2505 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002506 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 goto failed;
2508 else
2509 ++ectx.ec_stack.ga_len;
2510 tv = STACK_TV_BOT(-1);
2511 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002512 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 tv->vval.v_dict = dict;
2514 ++dict->dv_refcount;
2515 }
2516 break;
2517
2518 // call a :def function
2519 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002521 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2522 NULL,
2523 iptr->isn_arg.dfunc.cdf_argcount,
2524 &funclocal,
2525 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002526 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 break;
2528
2529 // call a builtin function
2530 case ISN_BCALL:
2531 SOURCING_LNUM = iptr->isn_lnum;
2532 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2533 iptr->isn_arg.bfunc.cbf_argcount,
2534 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002535 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 break;
2537
2538 // call a funcref or partial
2539 case ISN_PCALL:
2540 {
2541 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2542 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002543 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544
2545 SOURCING_LNUM = iptr->isn_lnum;
2546 if (pfunc->cpf_top)
2547 {
2548 // funcref is above the arguments
2549 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2550 }
2551 else
2552 {
2553 // Get the funcref from the stack.
2554 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002555 partial_tv = *STACK_TV_BOT(0);
2556 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 }
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002558 r = call_partial(tv, pfunc->cpf_argcount,
2559 &funclocal, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002560 if (tv == &partial_tv)
2561 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002563 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 }
2565 break;
2566
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002567 case ISN_PCALL_END:
2568 // PCALL finished, arguments have been consumed and replaced by
2569 // the return value. Now clear the funcref from the stack,
2570 // and move the return value in its place.
2571 --ectx.ec_stack.ga_len;
2572 clear_tv(STACK_TV_BOT(-1));
2573 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2574 break;
2575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 // call a user defined function or funcref/partial
2577 case ISN_UCALL:
2578 {
2579 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2580
2581 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002582 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
2583 &funclocal, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002584 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 }
2586 break;
2587
2588 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002589 case ISN_RETURN_ZERO:
2590 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2591 goto failed;
2592 tv = STACK_TV_BOT(0);
2593 ++ectx.ec_stack.ga_len;
2594 tv->v_type = VAR_NUMBER;
2595 tv->vval.v_number = 0;
2596 tv->v_lock = 0;
2597 // FALLTHROUGH
2598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 case ISN_RETURN:
2600 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002601 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002602 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002603
2604 if (trystack->ga_len > 0)
2605 trycmd = ((trycmd_T *)trystack->ga_data)
2606 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002607 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002608 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002610 // jump to ":finally" or ":endtry"
2611 if (trycmd->tcd_finally_idx != 0)
2612 ectx.ec_iidx = trycmd->tcd_finally_idx;
2613 else
2614 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 trycmd->tcd_return = TRUE;
2616 }
2617 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002618 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 }
2620 break;
2621
2622 // push a function reference to a compiled function
2623 case ISN_FUNCREF:
2624 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002625 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2626 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2627 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 if (pt == NULL)
2630 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002631 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002632 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002633 vim_free(pt);
2634 goto failed;
2635 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002636 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2637 &ectx) == FAIL)
2638 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 tv = STACK_TV_BOT(0);
2641 ++ectx.ec_stack.ga_len;
2642 tv->vval.v_partial = pt;
2643 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002644 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 }
2646 break;
2647
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002648 // Create a global function from a lambda.
2649 case ISN_NEWFUNC:
2650 {
2651 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2652
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002653 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002654 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002655 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002656 }
2657 break;
2658
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002659 // List functions
2660 case ISN_DEF:
2661 if (iptr->isn_arg.string == NULL)
2662 list_functions(NULL);
2663 else
2664 {
2665 exarg_T ea;
2666
2667 CLEAR_FIELD(ea);
2668 ea.cmd = ea.arg = iptr->isn_arg.string;
2669 define_function(&ea, NULL);
2670 }
2671 break;
2672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 // jump if a condition is met
2674 case ISN_JUMP:
2675 {
2676 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002677 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 int jump = TRUE;
2679
2680 if (when != JUMP_ALWAYS)
2681 {
2682 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002683 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002684 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002685 || when == JUMP_IF_COND_TRUE)
2686 {
2687 SOURCING_LNUM = iptr->isn_lnum;
2688 jump = tv_get_bool_chk(tv, &error);
2689 if (error)
2690 goto on_error;
2691 }
2692 else
2693 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002695 || when == JUMP_AND_KEEP_IF_FALSE
2696 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002698 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 {
2700 // drop the value from the stack
2701 clear_tv(tv);
2702 --ectx.ec_stack.ga_len;
2703 }
2704 }
2705 if (jump)
2706 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2707 }
2708 break;
2709
2710 // top of a for loop
2711 case ISN_FOR:
2712 {
2713 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2714 typval_T *idxtv =
2715 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2716
2717 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002718 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002720 ++idxtv->vval.v_number;
2721 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 // past the end of the list, jump to "endfor"
2723 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2724 else if (list->lv_first == &range_list_item)
2725 {
2726 // non-materialized range() list
2727 tv = STACK_TV_BOT(0);
2728 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002729 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 tv->vval.v_number = list_find_nr(
2731 list, idxtv->vval.v_number, NULL);
2732 ++ectx.ec_stack.ga_len;
2733 }
2734 else
2735 {
2736 listitem_T *li = list_find(list, idxtv->vval.v_number);
2737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2739 ++ectx.ec_stack.ga_len;
2740 }
2741 }
2742 break;
2743
2744 // start of ":try" block
2745 case ISN_TRY:
2746 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002747 trycmd_T *trycmd = NULL;
2748
Bram Moolenaar270d0382020-05-15 21:42:53 +02002749 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 goto failed;
2751 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2752 + ectx.ec_trystack.ga_len;
2753 ++ectx.ec_trystack.ga_len;
2754 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002755 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002756 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002757 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002758 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_ref->try_catch;
2759 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_ref->try_finally;
2760 trycmd->tcd_endtry_idx = iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 }
2762 break;
2763
2764 case ISN_PUSHEXC:
2765 if (current_exception == NULL)
2766 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002767 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 iemsg("Evaluating catch while current_exception is NULL");
2769 goto failed;
2770 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002771 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 goto failed;
2773 tv = STACK_TV_BOT(0);
2774 ++ectx.ec_stack.ga_len;
2775 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002776 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 tv->vval.v_string = vim_strsave(
2778 (char_u *)current_exception->value);
2779 break;
2780
2781 case ISN_CATCH:
2782 {
2783 garray_T *trystack = &ectx.ec_trystack;
2784
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002785 if (funclocal.floc_restore_cmdmod)
Bram Moolenaar20a76292020-12-25 19:47:24 +01002786 {
2787 cmdmod.cmod_filter_regmatch.regprog = NULL;
2788 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01002789 cmdmod = funclocal.floc_save_cmdmod;
2790 funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar20a76292020-12-25 19:47:24 +01002791 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 if (trystack->ga_len > 0)
2793 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002794 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 + trystack->ga_len - 1;
2796 trycmd->tcd_caught = TRUE;
2797 }
2798 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002799 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 catch_exception(current_exception);
2801 }
2802 break;
2803
Bram Moolenaarc150c092021-02-13 15:02:46 +01002804 case ISN_TRYCONT:
2805 {
2806 garray_T *trystack = &ectx.ec_trystack;
2807 trycont_T *trycont = &iptr->isn_arg.trycont;
2808 int i;
2809 trycmd_T *trycmd;
2810 int iidx = trycont->tct_where;
2811
2812 if (trystack->ga_len < trycont->tct_levels)
2813 {
2814 siemsg("TRYCONT: expected %d levels, found %d",
2815 trycont->tct_levels, trystack->ga_len);
2816 goto failed;
2817 }
2818 // Make :endtry jump to any outer try block and the last
2819 // :endtry inside the loop to the loop start.
2820 for (i = trycont->tct_levels; i > 0; --i)
2821 {
2822 trycmd = ((trycmd_T *)trystack->ga_data)
2823 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002824 // Add one to tcd_cont to be able to jump to
2825 // instruction with index zero.
2826 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002827 iidx = trycmd->tcd_finally_idx == 0
2828 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002829 }
2830 // jump to :finally or :endtry of current try statement
2831 ectx.ec_iidx = iidx;
2832 }
2833 break;
2834
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002835 case ISN_FINALLY:
2836 {
2837 garray_T *trystack = &ectx.ec_trystack;
2838 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2839 + trystack->ga_len - 1;
2840
2841 // Reset the index to avoid a return statement jumps here
2842 // again.
2843 trycmd->tcd_finally_idx = 0;
2844 break;
2845 }
2846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 // end of ":try" block
2848 case ISN_ENDTRY:
2849 {
2850 garray_T *trystack = &ectx.ec_trystack;
2851
2852 if (trystack->ga_len > 0)
2853 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002854 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 --trystack->ga_len;
2857 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002858 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 trycmd = ((trycmd_T *)trystack->ga_data)
2860 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002861 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 {
2863 // discard the exception
2864 if (caught_stack == current_exception)
2865 caught_stack = caught_stack->caught;
2866 discard_current_exception();
2867 }
2868
2869 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002870 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002871
2872 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2873 {
2874 --ectx.ec_stack.ga_len;
2875 clear_tv(STACK_TV_BOT(0));
2876 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002877 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002878 // handling :continue: jump to outer try block or
2879 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002880 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 }
2882 }
2883 break;
2884
2885 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002886 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002887 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002888
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002889 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2890 {
2891 // throwing an exception while using "silent!" causes
2892 // the function to abort but not display an error.
2893 tv = STACK_TV_BOT(-1);
2894 clear_tv(tv);
2895 tv->v_type = VAR_NUMBER;
2896 tv->vval.v_number = 0;
2897 goto done;
2898 }
2899 --ectx.ec_stack.ga_len;
2900 tv = STACK_TV_BOT(0);
2901 if (tv->vval.v_string == NULL
2902 || *skipwhite(tv->vval.v_string) == NUL)
2903 {
2904 vim_free(tv->vval.v_string);
2905 SOURCING_LNUM = iptr->isn_lnum;
2906 emsg(_(e_throw_with_empty_string));
2907 goto failed;
2908 }
2909
2910 // Inside a "catch" we need to first discard the caught
2911 // exception.
2912 if (trystack->ga_len > 0)
2913 {
2914 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2915 + trystack->ga_len - 1;
2916 if (trycmd->tcd_caught && current_exception != NULL)
2917 {
2918 // discard the exception
2919 if (caught_stack == current_exception)
2920 caught_stack = caught_stack->caught;
2921 discard_current_exception();
2922 trycmd->tcd_caught = FALSE;
2923 }
2924 }
2925
2926 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2927 == FAIL)
2928 {
2929 vim_free(tv->vval.v_string);
2930 goto failed;
2931 }
2932 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 break;
2935
2936 // compare with special values
2937 case ISN_COMPAREBOOL:
2938 case ISN_COMPARESPECIAL:
2939 {
2940 typval_T *tv1 = STACK_TV_BOT(-2);
2941 typval_T *tv2 = STACK_TV_BOT(-1);
2942 varnumber_T arg1 = tv1->vval.v_number;
2943 varnumber_T arg2 = tv2->vval.v_number;
2944 int res;
2945
2946 switch (iptr->isn_arg.op.op_type)
2947 {
2948 case EXPR_EQUAL: res = arg1 == arg2; break;
2949 case EXPR_NEQUAL: res = arg1 != arg2; break;
2950 default: res = 0; break;
2951 }
2952
2953 --ectx.ec_stack.ga_len;
2954 tv1->v_type = VAR_BOOL;
2955 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2956 }
2957 break;
2958
2959 // Operation with two number arguments
2960 case ISN_OPNR:
2961 case ISN_COMPARENR:
2962 {
2963 typval_T *tv1 = STACK_TV_BOT(-2);
2964 typval_T *tv2 = STACK_TV_BOT(-1);
2965 varnumber_T arg1 = tv1->vval.v_number;
2966 varnumber_T arg2 = tv2->vval.v_number;
2967 varnumber_T res;
2968
2969 switch (iptr->isn_arg.op.op_type)
2970 {
2971 case EXPR_MULT: res = arg1 * arg2; break;
2972 case EXPR_DIV: res = arg1 / arg2; break;
2973 case EXPR_REM: res = arg1 % arg2; break;
2974 case EXPR_SUB: res = arg1 - arg2; break;
2975 case EXPR_ADD: res = arg1 + arg2; break;
2976
2977 case EXPR_EQUAL: res = arg1 == arg2; break;
2978 case EXPR_NEQUAL: res = arg1 != arg2; break;
2979 case EXPR_GREATER: res = arg1 > arg2; break;
2980 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2981 case EXPR_SMALLER: res = arg1 < arg2; break;
2982 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2983 default: res = 0; break;
2984 }
2985
2986 --ectx.ec_stack.ga_len;
2987 if (iptr->isn_type == ISN_COMPARENR)
2988 {
2989 tv1->v_type = VAR_BOOL;
2990 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2991 }
2992 else
2993 tv1->vval.v_number = res;
2994 }
2995 break;
2996
2997 // Computation with two float arguments
2998 case ISN_OPFLOAT:
2999 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003000#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 {
3002 typval_T *tv1 = STACK_TV_BOT(-2);
3003 typval_T *tv2 = STACK_TV_BOT(-1);
3004 float_T arg1 = tv1->vval.v_float;
3005 float_T arg2 = tv2->vval.v_float;
3006 float_T res = 0;
3007 int cmp = FALSE;
3008
3009 switch (iptr->isn_arg.op.op_type)
3010 {
3011 case EXPR_MULT: res = arg1 * arg2; break;
3012 case EXPR_DIV: res = arg1 / arg2; break;
3013 case EXPR_SUB: res = arg1 - arg2; break;
3014 case EXPR_ADD: res = arg1 + arg2; break;
3015
3016 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3017 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3018 case EXPR_GREATER: cmp = arg1 > arg2; break;
3019 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3020 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3021 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3022 default: cmp = 0; break;
3023 }
3024 --ectx.ec_stack.ga_len;
3025 if (iptr->isn_type == ISN_COMPAREFLOAT)
3026 {
3027 tv1->v_type = VAR_BOOL;
3028 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3029 }
3030 else
3031 tv1->vval.v_float = res;
3032 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003033#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 break;
3035
3036 case ISN_COMPARELIST:
3037 {
3038 typval_T *tv1 = STACK_TV_BOT(-2);
3039 typval_T *tv2 = STACK_TV_BOT(-1);
3040 list_T *arg1 = tv1->vval.v_list;
3041 list_T *arg2 = tv2->vval.v_list;
3042 int cmp = FALSE;
3043 int ic = iptr->isn_arg.op.op_ic;
3044
3045 switch (iptr->isn_arg.op.op_type)
3046 {
3047 case EXPR_EQUAL: cmp =
3048 list_equal(arg1, arg2, ic, FALSE); break;
3049 case EXPR_NEQUAL: cmp =
3050 !list_equal(arg1, arg2, ic, FALSE); break;
3051 case EXPR_IS: cmp = arg1 == arg2; break;
3052 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3053 default: cmp = 0; break;
3054 }
3055 --ectx.ec_stack.ga_len;
3056 clear_tv(tv1);
3057 clear_tv(tv2);
3058 tv1->v_type = VAR_BOOL;
3059 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3060 }
3061 break;
3062
3063 case ISN_COMPAREBLOB:
3064 {
3065 typval_T *tv1 = STACK_TV_BOT(-2);
3066 typval_T *tv2 = STACK_TV_BOT(-1);
3067 blob_T *arg1 = tv1->vval.v_blob;
3068 blob_T *arg2 = tv2->vval.v_blob;
3069 int cmp = FALSE;
3070
3071 switch (iptr->isn_arg.op.op_type)
3072 {
3073 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3074 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3075 case EXPR_IS: cmp = arg1 == arg2; break;
3076 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3077 default: cmp = 0; break;
3078 }
3079 --ectx.ec_stack.ga_len;
3080 clear_tv(tv1);
3081 clear_tv(tv2);
3082 tv1->v_type = VAR_BOOL;
3083 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3084 }
3085 break;
3086
3087 // TODO: handle separately
3088 case ISN_COMPARESTRING:
3089 case ISN_COMPAREDICT:
3090 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 case ISN_COMPAREANY:
3092 {
3093 typval_T *tv1 = STACK_TV_BOT(-2);
3094 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003095 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 int ic = iptr->isn_arg.op.op_ic;
3097
Bram Moolenaareb26f432020-09-14 16:50:05 +02003098 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003099 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 --ectx.ec_stack.ga_len;
3102 }
3103 break;
3104
3105 case ISN_ADDLIST:
3106 case ISN_ADDBLOB:
3107 {
3108 typval_T *tv1 = STACK_TV_BOT(-2);
3109 typval_T *tv2 = STACK_TV_BOT(-1);
3110
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003111 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 if (iptr->isn_type == ISN_ADDLIST)
3113 eval_addlist(tv1, tv2);
3114 else
3115 eval_addblob(tv1, tv2);
3116 clear_tv(tv2);
3117 --ectx.ec_stack.ga_len;
3118 }
3119 break;
3120
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003121 case ISN_LISTAPPEND:
3122 {
3123 typval_T *tv1 = STACK_TV_BOT(-2);
3124 typval_T *tv2 = STACK_TV_BOT(-1);
3125 list_T *l = tv1->vval.v_list;
3126
3127 // add an item to a list
3128 if (l == NULL)
3129 {
3130 SOURCING_LNUM = iptr->isn_lnum;
3131 emsg(_(e_cannot_add_to_null_list));
3132 goto on_error;
3133 }
3134 if (list_append_tv(l, tv2) == FAIL)
3135 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003136 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003137 --ectx.ec_stack.ga_len;
3138 }
3139 break;
3140
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003141 case ISN_BLOBAPPEND:
3142 {
3143 typval_T *tv1 = STACK_TV_BOT(-2);
3144 typval_T *tv2 = STACK_TV_BOT(-1);
3145 blob_T *b = tv1->vval.v_blob;
3146 int error = FALSE;
3147 varnumber_T n;
3148
3149 // add a number to a blob
3150 if (b == NULL)
3151 {
3152 SOURCING_LNUM = iptr->isn_lnum;
3153 emsg(_(e_cannot_add_to_null_blob));
3154 goto on_error;
3155 }
3156 n = tv_get_number_chk(tv2, &error);
3157 if (error)
3158 goto on_error;
3159 ga_append(&b->bv_ga, (int)n);
3160 --ectx.ec_stack.ga_len;
3161 }
3162 break;
3163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 // Computation with two arguments of unknown type
3165 case ISN_OPANY:
3166 {
3167 typval_T *tv1 = STACK_TV_BOT(-2);
3168 typval_T *tv2 = STACK_TV_BOT(-1);
3169 varnumber_T n1, n2;
3170#ifdef FEAT_FLOAT
3171 float_T f1 = 0, f2 = 0;
3172#endif
3173 int error = FALSE;
3174
3175 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3176 {
3177 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3178 {
3179 eval_addlist(tv1, tv2);
3180 clear_tv(tv2);
3181 --ectx.ec_stack.ga_len;
3182 break;
3183 }
3184 else if (tv1->v_type == VAR_BLOB
3185 && tv2->v_type == VAR_BLOB)
3186 {
3187 eval_addblob(tv1, tv2);
3188 clear_tv(tv2);
3189 --ectx.ec_stack.ga_len;
3190 break;
3191 }
3192 }
3193#ifdef FEAT_FLOAT
3194 if (tv1->v_type == VAR_FLOAT)
3195 {
3196 f1 = tv1->vval.v_float;
3197 n1 = 0;
3198 }
3199 else
3200#endif
3201 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003202 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 n1 = tv_get_number_chk(tv1, &error);
3204 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003205 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206#ifdef FEAT_FLOAT
3207 if (tv2->v_type == VAR_FLOAT)
3208 f1 = n1;
3209#endif
3210 }
3211#ifdef FEAT_FLOAT
3212 if (tv2->v_type == VAR_FLOAT)
3213 {
3214 f2 = tv2->vval.v_float;
3215 n2 = 0;
3216 }
3217 else
3218#endif
3219 {
3220 n2 = tv_get_number_chk(tv2, &error);
3221 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003222 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223#ifdef FEAT_FLOAT
3224 if (tv1->v_type == VAR_FLOAT)
3225 f2 = n2;
3226#endif
3227 }
3228#ifdef FEAT_FLOAT
3229 // if there is a float on either side the result is a float
3230 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3231 {
3232 switch (iptr->isn_arg.op.op_type)
3233 {
3234 case EXPR_MULT: f1 = f1 * f2; break;
3235 case EXPR_DIV: f1 = f1 / f2; break;
3236 case EXPR_SUB: f1 = f1 - f2; break;
3237 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003238 default: SOURCING_LNUM = iptr->isn_lnum;
3239 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003240 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 }
3242 clear_tv(tv1);
3243 clear_tv(tv2);
3244 tv1->v_type = VAR_FLOAT;
3245 tv1->vval.v_float = f1;
3246 --ectx.ec_stack.ga_len;
3247 }
3248 else
3249#endif
3250 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003251 int failed = FALSE;
3252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 switch (iptr->isn_arg.op.op_type)
3254 {
3255 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003256 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3257 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003258 goto on_error;
3259 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 case EXPR_SUB: n1 = n1 - n2; break;
3261 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003262 default: n1 = num_modulus(n1, n2, &failed);
3263 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003264 goto on_error;
3265 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 }
3267 clear_tv(tv1);
3268 clear_tv(tv2);
3269 tv1->v_type = VAR_NUMBER;
3270 tv1->vval.v_number = n1;
3271 --ectx.ec_stack.ga_len;
3272 }
3273 }
3274 break;
3275
3276 case ISN_CONCAT:
3277 {
3278 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3279 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3280 char_u *res;
3281
3282 res = concat_str(str1, str2);
3283 clear_tv(STACK_TV_BOT(-2));
3284 clear_tv(STACK_TV_BOT(-1));
3285 --ectx.ec_stack.ga_len;
3286 STACK_TV_BOT(-1)->vval.v_string = res;
3287 }
3288 break;
3289
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003290 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003291 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003292 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003293 int is_slice = iptr->isn_type == ISN_STRSLICE;
3294 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003295 char_u *res;
3296
3297 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003298 // string slice: string is at stack-3, first index at
3299 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003300 if (is_slice)
3301 {
3302 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003303 n1 = tv->vval.v_number;
3304 }
3305
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003306 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003307 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003308
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003309 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003310 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003311 if (is_slice)
3312 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003313 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003314 else
3315 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003316 // single character (including composing characters).
3317 // If the index is too big or negative the result is
3318 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003319 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003320 vim_free(tv->vval.v_string);
3321 tv->vval.v_string = res;
3322 }
3323 break;
3324
3325 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003326 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 {
Bram Moolenaared591872020-08-15 22:14:53 +02003328 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003330 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331
3332 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003333 // list slice: list is at stack-3, indexes at stack-2 and
3334 // stack-1
3335 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 list = tv->vval.v_list;
3337
3338 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003339 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003341
3342 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 {
Bram Moolenaared591872020-08-15 22:14:53 +02003344 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003345 n1 = tv->vval.v_number;
3346 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 }
Bram Moolenaared591872020-08-15 22:14:53 +02003348
3349 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003350 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003351 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003352 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3353 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003354 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 }
3356 break;
3357
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003358 case ISN_ANYINDEX:
3359 case ISN_ANYSLICE:
3360 {
3361 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3362 typval_T *var1, *var2;
3363 int res;
3364
3365 // index: composite is at stack-2, index at stack-1
3366 // slice: composite is at stack-3, indexes at stack-2 and
3367 // stack-1
3368 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003369 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003370 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3371 goto on_error;
3372 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3373 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003374 res = eval_index_inner(tv, is_slice, var1, var2,
3375 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003376 clear_tv(var1);
3377 if (is_slice)
3378 clear_tv(var2);
3379 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3380 if (res == FAIL)
3381 goto on_error;
3382 }
3383 break;
3384
Bram Moolenaar9af78762020-06-16 11:34:42 +02003385 case ISN_SLICE:
3386 {
3387 list_T *list;
3388 int count = iptr->isn_arg.number;
3389
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003390 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003391 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003392 list = tv->vval.v_list;
3393
3394 // no error for short list, expect it to be checked earlier
3395 if (list != NULL && list->lv_len >= count)
3396 {
3397 list_T *newlist = list_slice(list,
3398 count, list->lv_len - 1);
3399
3400 if (newlist != NULL)
3401 {
3402 list_unref(list);
3403 tv->vval.v_list = newlist;
3404 ++newlist->lv_refcount;
3405 }
3406 }
3407 }
3408 break;
3409
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003410 case ISN_GETITEM:
3411 {
3412 listitem_T *li;
3413 int index = iptr->isn_arg.number;
3414
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003415 // Get list item: list is at stack-1, push item.
3416 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003417 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003418 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003419
3420 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3421 goto failed;
3422 ++ectx.ec_stack.ga_len;
3423 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003424
3425 // Useful when used in unpack assignment. Reset at
3426 // ISN_DROP.
3427 where.wt_index = index + 1;
3428 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003429 }
3430 break;
3431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 case ISN_MEMBER:
3433 {
3434 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003435 char_u *key;
3436 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003437 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003438
3439 // dict member: dict is at stack-2, key at stack-1
3440 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003441 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003442 dict = tv->vval.v_dict;
3443
3444 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003445 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003446 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003447 if (key == NULL)
3448 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003449
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003450 if ((di = dict_find(dict, key, -1)) == NULL)
3451 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003452 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003453 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003454
3455 // If :silent! is used we will continue, make sure the
3456 // stack contents makes sense.
3457 clear_tv(tv);
3458 --ectx.ec_stack.ga_len;
3459 tv = STACK_TV_BOT(-1);
3460 clear_tv(tv);
3461 tv->v_type = VAR_NUMBER;
3462 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003463 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003464 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003465 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003466 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003467 // Clear the dict only after getting the item, to avoid
3468 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003469 tv = STACK_TV_BOT(-1);
3470 temp_tv = *tv;
3471 copy_tv(&di->di_tv, tv);
3472 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003473 }
3474 break;
3475
3476 // dict member with string key
3477 case ISN_STRINGMEMBER:
3478 {
3479 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003481 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482
3483 tv = STACK_TV_BOT(-1);
3484 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3485 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003486 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003488 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 }
3490 dict = tv->vval.v_dict;
3491
3492 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3493 == NULL)
3494 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003495 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003497 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003499 // Clear the dict after getting the item, to avoid that it
3500 // make the item invalid.
3501 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003503 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 }
3505 break;
3506
3507 case ISN_NEGATENR:
3508 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003509 if (tv->v_type != VAR_NUMBER
3510#ifdef FEAT_FLOAT
3511 && tv->v_type != VAR_FLOAT
3512#endif
3513 )
3514 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003515 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003516 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003517 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003518 }
3519#ifdef FEAT_FLOAT
3520 if (tv->v_type == VAR_FLOAT)
3521 tv->vval.v_float = -tv->vval.v_float;
3522 else
3523#endif
3524 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 break;
3526
3527 case ISN_CHECKNR:
3528 {
3529 int error = FALSE;
3530
3531 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003532 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003534 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 (void)tv_get_number_chk(tv, &error);
3536 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003537 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 }
3539 break;
3540
3541 case ISN_CHECKTYPE:
3542 {
3543 checktype_T *ct = &iptr->isn_arg.type;
3544
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003545 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003546 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003547 if (!where.wt_variable)
3548 where.wt_index = ct->ct_arg_idx;
3549 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003550 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003551 if (!where.wt_variable)
3552 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003553
3554 // number 0 is FALSE, number 1 is TRUE
3555 if (tv->v_type == VAR_NUMBER
3556 && ct->ct_type->tt_type == VAR_BOOL
3557 && (tv->vval.v_number == 0
3558 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003560 tv->v_type = VAR_BOOL;
3561 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003562 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 }
3564 }
3565 break;
3566
Bram Moolenaar9af78762020-06-16 11:34:42 +02003567 case ISN_CHECKLEN:
3568 {
3569 int min_len = iptr->isn_arg.checklen.cl_min_len;
3570 list_T *list = NULL;
3571
3572 tv = STACK_TV_BOT(-1);
3573 if (tv->v_type == VAR_LIST)
3574 list = tv->vval.v_list;
3575 if (list == NULL || list->lv_len < min_len
3576 || (list->lv_len > min_len
3577 && !iptr->isn_arg.checklen.cl_more_OK))
3578 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003579 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003580 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003581 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003582 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003583 }
3584 }
3585 break;
3586
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003587 case ISN_SETTYPE:
3588 {
3589 checktype_T *ct = &iptr->isn_arg.type;
3590
3591 tv = STACK_TV_BOT(-1);
3592 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3593 {
3594 free_type(tv->vval.v_dict->dv_type);
3595 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3596 }
3597 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3598 {
3599 free_type(tv->vval.v_list->lv_type);
3600 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3601 }
3602 }
3603 break;
3604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003606 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 {
3608 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003609 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
3611 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003612 if (iptr->isn_type == ISN_2BOOL)
3613 {
3614 n = tv2bool(tv);
3615 if (iptr->isn_arg.number) // invert
3616 n = !n;
3617 }
3618 else
3619 {
3620 SOURCING_LNUM = iptr->isn_lnum;
3621 n = tv_get_bool_chk(tv, &error);
3622 if (error)
3623 goto on_error;
3624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 clear_tv(tv);
3626 tv->v_type = VAR_BOOL;
3627 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3628 }
3629 break;
3630
3631 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003632 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003633 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003634 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3635 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3636 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 break;
3638
Bram Moolenaar08597872020-12-10 19:43:40 +01003639 case ISN_RANGE:
3640 {
3641 exarg_T ea;
3642 char *errormsg;
3643
Bram Moolenaarece0b872021-01-08 20:40:45 +01003644 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003645 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003646 ea.addr_type = ADDR_LINES;
3647 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003648 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003649 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003650 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003651
3652 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3653 goto failed;
3654 ++ectx.ec_stack.ga_len;
3655 tv = STACK_TV_BOT(-1);
3656 tv->v_type = VAR_NUMBER;
3657 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003658 if (ea.addr_count == 0)
3659 tv->vval.v_number = curwin->w_cursor.lnum;
3660 else
3661 tv->vval.v_number = ea.line2;
3662 }
3663 break;
3664
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003665 case ISN_PUT:
3666 {
3667 int regname = iptr->isn_arg.put.put_regname;
3668 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3669 char_u *expr = NULL;
3670 int dir = FORWARD;
3671
Bram Moolenaar08597872020-12-10 19:43:40 +01003672 if (lnum < -2)
3673 {
3674 // line number was put on the stack by ISN_RANGE
3675 tv = STACK_TV_BOT(-1);
3676 curwin->w_cursor.lnum = tv->vval.v_number;
3677 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3678 dir = BACKWARD;
3679 --ectx.ec_stack.ga_len;
3680 }
3681 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003682 // :put! above cursor
3683 dir = BACKWARD;
3684 else if (lnum >= 0)
3685 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003686
3687 if (regname == '=')
3688 {
3689 tv = STACK_TV_BOT(-1);
3690 if (tv->v_type == VAR_STRING)
3691 expr = tv->vval.v_string;
3692 else
3693 {
3694 expr = typval2string(tv, TRUE); // allocates value
3695 clear_tv(tv);
3696 }
3697 --ectx.ec_stack.ga_len;
3698 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003699 check_cursor();
3700 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3701 vim_free(expr);
3702 }
3703 break;
3704
Bram Moolenaar02194d22020-10-24 23:08:38 +02003705 case ISN_CMDMOD:
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003706 funclocal.floc_save_cmdmod = cmdmod;
3707 funclocal.floc_restore_cmdmod = TRUE;
3708 funclocal.floc_restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003709 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3710 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003711 break;
3712
Bram Moolenaar02194d22020-10-24 23:08:38 +02003713 case ISN_CMDMOD_REV:
3714 // filter regprog is owned by the instruction, don't free it
3715 cmdmod.cmod_filter_regmatch.regprog = NULL;
3716 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003717 cmdmod = funclocal.floc_save_cmdmod;
3718 funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003719 break;
3720
Bram Moolenaar792f7862020-11-23 08:31:18 +01003721 case ISN_UNPACK:
3722 {
3723 int count = iptr->isn_arg.unpack.unp_count;
3724 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3725 list_T *l;
3726 listitem_T *li;
3727 int i;
3728
3729 // Check there is a valid list to unpack.
3730 tv = STACK_TV_BOT(-1);
3731 if (tv->v_type != VAR_LIST)
3732 {
3733 SOURCING_LNUM = iptr->isn_lnum;
3734 emsg(_(e_for_argument_must_be_sequence_of_lists));
3735 goto on_error;
3736 }
3737 l = tv->vval.v_list;
3738 if (l == NULL
3739 || l->lv_len < (semicolon ? count - 1 : count))
3740 {
3741 SOURCING_LNUM = iptr->isn_lnum;
3742 emsg(_(e_list_value_does_not_have_enough_items));
3743 goto on_error;
3744 }
3745 else if (!semicolon && l->lv_len > count)
3746 {
3747 SOURCING_LNUM = iptr->isn_lnum;
3748 emsg(_(e_list_value_has_more_items_than_targets));
3749 goto on_error;
3750 }
3751
3752 CHECK_LIST_MATERIALIZE(l);
3753 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3754 goto failed;
3755 ectx.ec_stack.ga_len += count - 1;
3756
3757 // Variable after semicolon gets a list with the remaining
3758 // items.
3759 if (semicolon)
3760 {
3761 list_T *rem_list =
3762 list_alloc_with_items(l->lv_len - count + 1);
3763
3764 if (rem_list == NULL)
3765 goto failed;
3766 tv = STACK_TV_BOT(-count);
3767 tv->vval.v_list = rem_list;
3768 ++rem_list->lv_refcount;
3769 tv->v_lock = 0;
3770 li = l->lv_first;
3771 for (i = 0; i < count - 1; ++i)
3772 li = li->li_next;
3773 for (i = 0; li != NULL; ++i)
3774 {
3775 list_set_item(rem_list, i, &li->li_tv);
3776 li = li->li_next;
3777 }
3778 --count;
3779 }
3780
3781 // Produce the values in reverse order, first item last.
3782 li = l->lv_first;
3783 for (i = 0; i < count; ++i)
3784 {
3785 tv = STACK_TV_BOT(-i - 1);
3786 copy_tv(&li->li_tv, tv);
3787 li = li->li_next;
3788 }
3789
3790 list_unref(l);
3791 }
3792 break;
3793
Bram Moolenaarb2049902021-01-24 12:53:53 +01003794 case ISN_PROF_START:
3795 case ISN_PROF_END:
3796 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003797#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003798 funccall_T cookie;
3799 ufunc_T *cur_ufunc =
3800 (((dfunc_T *)def_functions.ga_data)
3801 + ectx.ec_dfunc_idx)->df_ufunc;
3802
3803 cookie.func = cur_ufunc;
3804 if (iptr->isn_type == ISN_PROF_START)
3805 {
3806 func_line_start(&cookie, iptr->isn_lnum);
3807 // if we get here the instruction is executed
3808 func_line_exec(&cookie);
3809 }
3810 else
3811 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003812#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003813 }
3814 break;
3815
Bram Moolenaar389df252020-07-09 21:20:47 +02003816 case ISN_SHUFFLE:
3817 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003818 typval_T tmp_tv;
3819 int item = iptr->isn_arg.shuffle.shfl_item;
3820 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003821
3822 tmp_tv = *STACK_TV_BOT(-item);
3823 for ( ; up > 0 && item > 1; --up)
3824 {
3825 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3826 --item;
3827 }
3828 *STACK_TV_BOT(-item) = tmp_tv;
3829 }
3830 break;
3831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 case ISN_DROP:
3833 --ectx.ec_stack.ga_len;
3834 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003835 where.wt_index = 0;
3836 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 break;
3838 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003839 continue;
3840
Bram Moolenaard032f342020-07-18 18:13:02 +02003841func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003842 // Restore previous function. If the frame pointer is where we started
3843 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003844 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003845 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003846
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003847 if (func_return(&funclocal, &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003848 // only fails when out of memory
3849 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003850 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003851
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003852on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003853 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003854 // If "emsg_silent" is set then ignore the error, unless it was set
3855 // when calling the function.
3856 if (did_emsg_cumul + did_emsg == did_emsg_before
3857 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003858 {
3859 // If a sequence of instructions causes an error while ":silent!"
3860 // was used, restore the stack length and jump ahead to restoring
3861 // the cmdmod.
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003862 if (funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003863 {
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003864 while (ectx.ec_stack.ga_len
3865 > funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003866 {
3867 --ectx.ec_stack.ga_len;
3868 clear_tv(STACK_TV_BOT(0));
3869 }
3870 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3871 ++ectx.ec_iidx;
3872 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003873 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003874 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003875on_fatal_error:
3876 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003877 // If we are not inside a try-catch started here, abort execution.
3878 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003879 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 }
3881
3882done:
3883 // function finished, get result from the stack.
3884 tv = STACK_TV_BOT(-1);
3885 *rettv = *tv;
3886 tv->v_type = VAR_UNKNOWN;
3887 ret = OK;
3888
3889failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003890 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003891 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003892 func_return(&funclocal, &ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003893
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003894 // Deal with any remaining closures, they may be in use somewhere.
3895 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003896 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003897 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003898 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3899 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003900
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003901 estack_pop();
3902 current_sctx = save_current_sctx;
3903
Bram Moolenaarc970e422021-03-17 15:03:04 +01003904 // TODO: when is it safe to delete the function if it is no longer used?
3905 --ufunc->uf_calls;
3906
Bram Moolenaar352134b2020-10-17 22:04:08 +02003907 if (*msg_list != NULL && saved_msg_list != NULL)
3908 {
3909 msglist_T **plist = saved_msg_list;
3910
3911 // Append entries from the current msg_list (uncaught exceptions) to
3912 // the saved msg_list.
3913 while (*plist != NULL)
3914 plist = &(*plist)->next;
3915
3916 *plist = *msg_list;
3917 }
3918 msg_list = saved_msg_list;
3919
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003920 if (funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02003921 {
3922 cmdmod.cmod_filter_regmatch.regprog = NULL;
3923 undo_cmdmod(&cmdmod);
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003924 cmdmod = funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003925 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003926 emsg_silent_def = save_emsg_silent_def;
3927 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003928
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003929failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003930 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3932 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003933
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003935 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003936
Bram Moolenaar0186e582021-01-10 18:33:11 +01003937 while (ectx.ec_outer != NULL)
3938 {
3939 outer_T *up = ectx.ec_outer->out_up_is_copy
3940 ? NULL : ectx.ec_outer->out_up;
3941
3942 vim_free(ectx.ec_outer);
3943 ectx.ec_outer = up;
3944 }
3945
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003946 // Not sure if this is necessary.
3947 suppress_errthrow = save_suppress_errthrow;
3948
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003949 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003950 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003951 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003952 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 return ret;
3954}
3955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003957 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003958 * We don't really need this at runtime, but we do have tests that require it,
3959 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 */
3961 void
3962ex_disassemble(exarg_T *eap)
3963{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003964 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003965 char_u *fname;
3966 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 dfunc_T *dfunc;
3968 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003969 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 int current;
3971 int line_idx = 0;
3972 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003973 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003975 if (STRNCMP(arg, "<lambda>", 8) == 0)
3976 {
3977 arg += 8;
3978 (void)getdigits(&arg);
3979 fname = vim_strnsave(eap->arg, arg - eap->arg);
3980 }
3981 else
3982 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003983 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003984 if (fname == NULL)
3985 {
3986 semsg(_(e_invarg2), eap->arg);
3987 return;
3988 }
3989
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003990 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003991 if (ufunc == NULL)
3992 {
3993 char_u *p = untrans_function_name(fname);
3994
3995 if (p != NULL)
3996 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003997 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003998 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003999 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 if (ufunc == NULL)
4001 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004002 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 return;
4004 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01004005 if (func_needs_compiling(ufunc, eap->forceit)
4006 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02004007 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004008 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004010 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 return;
4012 }
4013 if (ufunc->uf_name_exp != NULL)
4014 msg((char *)ufunc->uf_name_exp);
4015 else
4016 msg((char *)ufunc->uf_name);
4017
4018 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004019#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004020 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
4021 instr_count = eap->forceit ? dfunc->df_instr_prof_count
4022 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01004023#else
4024 instr = dfunc->df_instr;
4025 instr_count = dfunc->df_instr_count;
4026#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004027 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 {
4029 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004030 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031
4032 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
4033 {
4034 if (current > prev_current)
4035 {
4036 msg_puts("\n\n");
4037 prev_current = current;
4038 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004039 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
4040 if (line != NULL)
4041 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 }
4043
4044 switch (iptr->isn_type)
4045 {
4046 case ISN_EXEC:
4047 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
4048 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004049 case ISN_EXECCONCAT:
4050 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004051 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004052 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 case ISN_ECHO:
4054 {
4055 echo_T *echo = &iptr->isn_arg.echo;
4056
4057 smsg("%4d %s %d", current,
4058 echo->echo_with_white ? "ECHO" : "ECHON",
4059 echo->echo_count);
4060 }
4061 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004062 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01004063 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004064 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01004065 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004066 case ISN_ECHOMSG:
4067 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004068 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004069 break;
4070 case ISN_ECHOERR:
4071 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004072 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004073 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004075 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004076 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004077 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004078 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004079 + STACK_FRAME_SIZE));
4080 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004081 smsg("%4d LOAD $%lld", current,
4082 (varnumber_T)(iptr->isn_arg.number));
4083 }
4084 break;
4085 case ISN_LOADOUTER:
4086 {
4087 if (iptr->isn_arg.number < 0)
4088 smsg("%4d LOADOUTER level %d arg[%d]", current,
4089 iptr->isn_arg.outer.outer_depth,
4090 iptr->isn_arg.outer.outer_idx
4091 + STACK_FRAME_SIZE);
4092 else
4093 smsg("%4d LOADOUTER level %d $%d", current,
4094 iptr->isn_arg.outer.outer_depth,
4095 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 break;
4098 case ISN_LOADV:
4099 smsg("%4d LOADV v:%s", current,
4100 get_vim_var_name(iptr->isn_arg.number));
4101 break;
4102 case ISN_LOADSCRIPT:
4103 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004104 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4105 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004107 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004109 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4110 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004111 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004112 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 }
4114 break;
4115 case ISN_LOADS:
4116 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004117 scriptitem_T *si = SCRIPT_ITEM(
4118 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119
4120 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004121 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 }
4123 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004124 case ISN_LOADAUTO:
4125 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4126 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 case ISN_LOADG:
4128 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4129 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004130 case ISN_LOADB:
4131 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4132 break;
4133 case ISN_LOADW:
4134 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4135 break;
4136 case ISN_LOADT:
4137 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4138 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004139 case ISN_LOADGDICT:
4140 smsg("%4d LOAD g:", current);
4141 break;
4142 case ISN_LOADBDICT:
4143 smsg("%4d LOAD b:", current);
4144 break;
4145 case ISN_LOADWDICT:
4146 smsg("%4d LOAD w:", current);
4147 break;
4148 case ISN_LOADTDICT:
4149 smsg("%4d LOAD t:", current);
4150 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 case ISN_LOADOPT:
4152 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4153 break;
4154 case ISN_LOADENV:
4155 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4156 break;
4157 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004158 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 break;
4160
4161 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004162 if (iptr->isn_arg.number < 0)
4163 smsg("%4d STORE arg[%lld]", current,
4164 iptr->isn_arg.number + STACK_FRAME_SIZE);
4165 else
4166 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4167 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004168 case ISN_STOREOUTER:
4169 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004170 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004171 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4172 iptr->isn_arg.outer.outer_depth,
4173 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004174 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004175 smsg("%4d STOREOUTER level %d $%d", current,
4176 iptr->isn_arg.outer.outer_depth,
4177 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004180 case ISN_STOREV:
4181 smsg("%4d STOREV v:%s", current,
4182 get_vim_var_name(iptr->isn_arg.number));
4183 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004184 case ISN_STOREAUTO:
4185 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4186 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004188 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4189 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004190 case ISN_STOREB:
4191 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4192 break;
4193 case ISN_STOREW:
4194 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4195 break;
4196 case ISN_STORET:
4197 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4198 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004199 case ISN_STORES:
4200 {
4201 scriptitem_T *si = SCRIPT_ITEM(
4202 iptr->isn_arg.loadstore.ls_sid);
4203
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004204 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004205 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 break;
4208 case ISN_STORESCRIPT:
4209 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004210 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4211 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004213 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004215 smsg("%4d STORESCRIPT %s-%d in %s", current,
4216 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004217 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004218 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 }
4220 break;
4221 case ISN_STOREOPT:
4222 smsg("%4d STOREOPT &%s", current,
4223 iptr->isn_arg.storeopt.so_name);
4224 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004225 case ISN_STOREENV:
4226 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4227 break;
4228 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004229 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004230 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 case ISN_STORENR:
4232 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004233 iptr->isn_arg.storenr.stnr_val,
4234 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 break;
4236
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004237 case ISN_STOREINDEX:
4238 switch (iptr->isn_arg.vartype)
4239 {
4240 case VAR_LIST:
4241 smsg("%4d STORELIST", current);
4242 break;
4243 case VAR_DICT:
4244 smsg("%4d STOREDICT", current);
4245 break;
4246 case VAR_ANY:
4247 smsg("%4d STOREINDEX", current);
4248 break;
4249 default: break;
4250 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004251 break;
4252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 // constants
4254 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004255 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004256 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 break;
4258 case ISN_PUSHBOOL:
4259 case ISN_PUSHSPEC:
4260 smsg("%4d PUSH %s", current,
4261 get_var_special_name(iptr->isn_arg.number));
4262 break;
4263 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004264#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004266#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 break;
4268 case ISN_PUSHS:
4269 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4270 break;
4271 case ISN_PUSHBLOB:
4272 {
4273 char_u *r;
4274 char_u numbuf[NUMBUFLEN];
4275 char_u *tofree;
4276
4277 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004278 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 vim_free(tofree);
4280 }
4281 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004282 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004283 {
4284 char *name = (char *)iptr->isn_arg.string;
4285
4286 smsg("%4d PUSHFUNC \"%s\"", current,
4287 name == NULL ? "[none]" : name);
4288 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004289 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004290 case ISN_PUSHCHANNEL:
4291#ifdef FEAT_JOB_CHANNEL
4292 {
4293 channel_T *channel = iptr->isn_arg.channel;
4294
4295 smsg("%4d PUSHCHANNEL %d", current,
4296 channel == NULL ? 0 : channel->ch_id);
4297 }
4298#endif
4299 break;
4300 case ISN_PUSHJOB:
4301#ifdef FEAT_JOB_CHANNEL
4302 {
4303 typval_T tv;
4304 char_u *name;
4305
4306 tv.v_type = VAR_JOB;
4307 tv.vval.v_job = iptr->isn_arg.job;
4308 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004309 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004310 }
4311#endif
4312 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 case ISN_PUSHEXC:
4314 smsg("%4d PUSH v:exception", current);
4315 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004316 case ISN_UNLET:
4317 smsg("%4d UNLET%s %s", current,
4318 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4319 iptr->isn_arg.unlet.ul_name);
4320 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004321 case ISN_UNLETENV:
4322 smsg("%4d UNLETENV%s $%s", current,
4323 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4324 iptr->isn_arg.unlet.ul_name);
4325 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004326 case ISN_UNLETINDEX:
4327 smsg("%4d UNLETINDEX", current);
4328 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004329 case ISN_UNLETRANGE:
4330 smsg("%4d UNLETRANGE", current);
4331 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004332 case ISN_LOCKCONST:
4333 smsg("%4d LOCKCONST", current);
4334 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004336 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004337 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 break;
4339 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004340 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004341 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 break;
4343
4344 // function call
4345 case ISN_BCALL:
4346 {
4347 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4348
4349 smsg("%4d BCALL %s(argc %d)", current,
4350 internal_func_name(cbfunc->cbf_idx),
4351 cbfunc->cbf_argcount);
4352 }
4353 break;
4354 case ISN_DCALL:
4355 {
4356 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4357 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4358 + cdfunc->cdf_idx;
4359
4360 smsg("%4d DCALL %s(argc %d)", current,
4361 df->df_ufunc->uf_name_exp != NULL
4362 ? df->df_ufunc->uf_name_exp
4363 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4364 }
4365 break;
4366 case ISN_UCALL:
4367 {
4368 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4369
4370 smsg("%4d UCALL %s(argc %d)", current,
4371 cufunc->cuf_name, cufunc->cuf_argcount);
4372 }
4373 break;
4374 case ISN_PCALL:
4375 {
4376 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4377
4378 smsg("%4d PCALL%s (argc %d)", current,
4379 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4380 }
4381 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004382 case ISN_PCALL_END:
4383 smsg("%4d PCALL end", current);
4384 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 case ISN_RETURN:
4386 smsg("%4d RETURN", current);
4387 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004388 case ISN_RETURN_ZERO:
4389 smsg("%4d RETURN 0", current);
4390 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 case ISN_FUNCREF:
4392 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004393 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004395 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004397 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 }
4399 break;
4400
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004401 case ISN_NEWFUNC:
4402 {
4403 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4404
4405 smsg("%4d NEWFUNC %s %s", current,
4406 newfunc->nf_lambda, newfunc->nf_global);
4407 }
4408 break;
4409
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004410 case ISN_DEF:
4411 {
4412 char_u *name = iptr->isn_arg.string;
4413
4414 smsg("%4d DEF %s", current,
4415 name == NULL ? (char_u *)"" : name);
4416 }
4417 break;
4418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 case ISN_JUMP:
4420 {
4421 char *when = "?";
4422
4423 switch (iptr->isn_arg.jump.jump_when)
4424 {
4425 case JUMP_ALWAYS:
4426 when = "JUMP";
4427 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 case JUMP_AND_KEEP_IF_TRUE:
4429 when = "JUMP_AND_KEEP_IF_TRUE";
4430 break;
4431 case JUMP_IF_FALSE:
4432 when = "JUMP_IF_FALSE";
4433 break;
4434 case JUMP_AND_KEEP_IF_FALSE:
4435 when = "JUMP_AND_KEEP_IF_FALSE";
4436 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004437 case JUMP_IF_COND_FALSE:
4438 when = "JUMP_IF_COND_FALSE";
4439 break;
4440 case JUMP_IF_COND_TRUE:
4441 when = "JUMP_IF_COND_TRUE";
4442 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004444 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 iptr->isn_arg.jump.jump_where);
4446 }
4447 break;
4448
4449 case ISN_FOR:
4450 {
4451 forloop_T *forloop = &iptr->isn_arg.forloop;
4452
4453 smsg("%4d FOR $%d -> %d", current,
4454 forloop->for_idx, forloop->for_end);
4455 }
4456 break;
4457
4458 case ISN_TRY:
4459 {
4460 try_T *try = &iptr->isn_arg.try;
4461
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004462 if (try->try_ref->try_finally == 0)
4463 smsg("%4d TRY catch -> %d, endtry -> %d",
4464 current,
4465 try->try_ref->try_catch,
4466 try->try_ref->try_endtry);
4467 else
4468 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4469 current,
4470 try->try_ref->try_catch,
4471 try->try_ref->try_finally,
4472 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 }
4474 break;
4475 case ISN_CATCH:
4476 // TODO
4477 smsg("%4d CATCH", current);
4478 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004479 case ISN_TRYCONT:
4480 {
4481 trycont_T *trycont = &iptr->isn_arg.trycont;
4482
4483 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4484 trycont->tct_levels,
4485 trycont->tct_levels == 1 ? "" : "s",
4486 trycont->tct_where);
4487 }
4488 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004489 case ISN_FINALLY:
4490 smsg("%4d FINALLY", current);
4491 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 case ISN_ENDTRY:
4493 smsg("%4d ENDTRY", current);
4494 break;
4495 case ISN_THROW:
4496 smsg("%4d THROW", current);
4497 break;
4498
4499 // expression operations on number
4500 case ISN_OPNR:
4501 case ISN_OPFLOAT:
4502 case ISN_OPANY:
4503 {
4504 char *what;
4505 char *ins;
4506
4507 switch (iptr->isn_arg.op.op_type)
4508 {
4509 case EXPR_MULT: what = "*"; break;
4510 case EXPR_DIV: what = "/"; break;
4511 case EXPR_REM: what = "%"; break;
4512 case EXPR_SUB: what = "-"; break;
4513 case EXPR_ADD: what = "+"; break;
4514 default: what = "???"; break;
4515 }
4516 switch (iptr->isn_type)
4517 {
4518 case ISN_OPNR: ins = "OPNR"; break;
4519 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4520 case ISN_OPANY: ins = "OPANY"; break;
4521 default: ins = "???"; break;
4522 }
4523 smsg("%4d %s %s", current, ins, what);
4524 }
4525 break;
4526
4527 case ISN_COMPAREBOOL:
4528 case ISN_COMPARESPECIAL:
4529 case ISN_COMPARENR:
4530 case ISN_COMPAREFLOAT:
4531 case ISN_COMPARESTRING:
4532 case ISN_COMPAREBLOB:
4533 case ISN_COMPARELIST:
4534 case ISN_COMPAREDICT:
4535 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 case ISN_COMPAREANY:
4537 {
4538 char *p;
4539 char buf[10];
4540 char *type;
4541
4542 switch (iptr->isn_arg.op.op_type)
4543 {
4544 case EXPR_EQUAL: p = "=="; break;
4545 case EXPR_NEQUAL: p = "!="; break;
4546 case EXPR_GREATER: p = ">"; break;
4547 case EXPR_GEQUAL: p = ">="; break;
4548 case EXPR_SMALLER: p = "<"; break;
4549 case EXPR_SEQUAL: p = "<="; break;
4550 case EXPR_MATCH: p = "=~"; break;
4551 case EXPR_IS: p = "is"; break;
4552 case EXPR_ISNOT: p = "isnot"; break;
4553 case EXPR_NOMATCH: p = "!~"; break;
4554 default: p = "???"; break;
4555 }
4556 STRCPY(buf, p);
4557 if (iptr->isn_arg.op.op_ic == TRUE)
4558 strcat(buf, "?");
4559 switch(iptr->isn_type)
4560 {
4561 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4562 case ISN_COMPARESPECIAL:
4563 type = "COMPARESPECIAL"; break;
4564 case ISN_COMPARENR: type = "COMPARENR"; break;
4565 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4566 case ISN_COMPARESTRING:
4567 type = "COMPARESTRING"; break;
4568 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4569 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4570 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4571 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4573 default: type = "???"; break;
4574 }
4575
4576 smsg("%4d %s %s", current, type, buf);
4577 }
4578 break;
4579
4580 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4581 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4582
4583 // expression operations
4584 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004585 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004586 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004587 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004588 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004589 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004590 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004591 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4592 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004593 case ISN_SLICE: smsg("%4d SLICE %lld",
4594 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004595 case ISN_GETITEM: smsg("%4d ITEM %lld",
4596 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004597 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4598 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 iptr->isn_arg.string); break;
4600 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4601
4602 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004603 case ISN_CHECKTYPE:
4604 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004605 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004606 char *tofree;
4607
Bram Moolenaare32e5162021-01-21 20:21:29 +01004608 if (ct->ct_arg_idx == 0)
4609 smsg("%4d CHECKTYPE %s stack[%d]", current,
4610 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004611 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004612 else
4613 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4614 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004615 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004616 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004617 vim_free(tofree);
4618 break;
4619 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004620 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4621 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4622 iptr->isn_arg.checklen.cl_min_len);
4623 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004624 case ISN_SETTYPE:
4625 {
4626 char *tofree;
4627
4628 smsg("%4d SETTYPE %s", current,
4629 type_name(iptr->isn_arg.type.ct_type, &tofree));
4630 vim_free(tofree);
4631 break;
4632 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004633 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 case ISN_2BOOL: if (iptr->isn_arg.number)
4635 smsg("%4d INVERT (!val)", current);
4636 else
4637 smsg("%4d 2BOOL (!!val)", current);
4638 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004639 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004640 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004641 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004642 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004643 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004644 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004645 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4646 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004647 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004648 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4649 smsg("%4d PUT %c above range",
4650 current, iptr->isn_arg.put.put_regname);
4651 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4652 smsg("%4d PUT %c range",
4653 current, iptr->isn_arg.put.put_regname);
4654 else
4655 smsg("%4d PUT %c %ld", current,
4656 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004657 (long)iptr->isn_arg.put.put_lnum);
4658 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659
Bram Moolenaar02194d22020-10-24 23:08:38 +02004660 // TODO: summarize modifiers
4661 case ISN_CMDMOD:
4662 {
4663 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004664 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004665 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4666
4667 buf = alloc(len + 1);
4668 if (buf != NULL)
4669 {
4670 (void)produce_cmdmods(
4671 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4672 smsg("%4d CMDMOD %s", current, buf);
4673 vim_free(buf);
4674 }
4675 break;
4676 }
4677 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004678
Bram Moolenaarb2049902021-01-24 12:53:53 +01004679 case ISN_PROF_START:
4680 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4681 break;
4682
4683 case ISN_PROF_END:
4684 smsg("%4d PROFILE END", current);
4685 break;
4686
Bram Moolenaar792f7862020-11-23 08:31:18 +01004687 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4688 iptr->isn_arg.unpack.unp_count,
4689 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4690 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004691 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4692 iptr->isn_arg.shuffle.shfl_item,
4693 iptr->isn_arg.shuffle.shfl_up);
4694 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 case ISN_DROP: smsg("%4d DROP", current); break;
4696 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004697
4698 out_flush(); // output one line at a time
4699 ui_breakcheck();
4700 if (got_int)
4701 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703}
4704
4705/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004706 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 * list, etc. Mostly like what JavaScript does, except that empty list and
4708 * empty dictionary are FALSE.
4709 */
4710 int
4711tv2bool(typval_T *tv)
4712{
4713 switch (tv->v_type)
4714 {
4715 case VAR_NUMBER:
4716 return tv->vval.v_number != 0;
4717 case VAR_FLOAT:
4718#ifdef FEAT_FLOAT
4719 return tv->vval.v_float != 0.0;
4720#else
4721 break;
4722#endif
4723 case VAR_PARTIAL:
4724 return tv->vval.v_partial != NULL;
4725 case VAR_FUNC:
4726 case VAR_STRING:
4727 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4728 case VAR_LIST:
4729 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4730 case VAR_DICT:
4731 return tv->vval.v_dict != NULL
4732 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4733 case VAR_BOOL:
4734 case VAR_SPECIAL:
4735 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4736 case VAR_JOB:
4737#ifdef FEAT_JOB_CHANNEL
4738 return tv->vval.v_job != NULL;
4739#else
4740 break;
4741#endif
4742 case VAR_CHANNEL:
4743#ifdef FEAT_JOB_CHANNEL
4744 return tv->vval.v_channel != NULL;
4745#else
4746 break;
4747#endif
4748 case VAR_BLOB:
4749 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4750 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004751 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 case VAR_VOID:
4753 break;
4754 }
4755 return FALSE;
4756}
4757
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004758 void
4759emsg_using_string_as(typval_T *tv, int as_number)
4760{
4761 semsg(_(as_number ? e_using_string_as_number_str
4762 : e_using_string_as_bool_str),
4763 tv->vval.v_string == NULL
4764 ? (char_u *)"" : tv->vval.v_string);
4765}
4766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767/*
4768 * If "tv" is a string give an error and return FAIL.
4769 */
4770 int
4771check_not_string(typval_T *tv)
4772{
4773 if (tv->v_type == VAR_STRING)
4774 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004775 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 clear_tv(tv);
4777 return FAIL;
4778 }
4779 return OK;
4780}
4781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782
4783#endif // FEAT_EVAL